Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update scalafmt-core to 3.9.0 #172

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Scala Steward: Reformat with scalafmt 3.9.0
3af3a4f28404c7ae15247c78d27a1e987de8e23d
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.7.17"
version = "3.9.0"
runner.dialect = scala3
maxColumn = 100
align {
Expand Down
80 changes: 34 additions & 46 deletions matr-api/shared/src/main/scala/matr/TupleSupport.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package matr

/** Auxiliary traits and givens supporting tuple-based construction of Matrices via `MatrixFactory.fromTuple`.
*/
/** Auxiliary traits and givens supporting tuple-based construction of Matrices via
* `MatrixFactory.fromTuple`.
*/
trait TupleSupport:

trait RowTupleReader[RowTuple, T]:
trait RowTupleReader[RowTuple, T]:
def readRow
(
rowIdx: Int,
(rowIdx: Int,
rowTuple: RowTuple,
colDim: Int,
setElem: (rowIdx: Int, colIdx: Int, elem: T) => Unit
Expand All @@ -16,62 +16,50 @@ trait TupleSupport:

given emptyRowTupleReader[T]: RowTupleReader[EmptyTuple, T] with
def readRow
(
rowIdx: Int,
rowTuple: EmptyTuple,
colDim: Int,
setElem: (rowIdx: Int, colIdx: Int, elem: T) => Unit
)
: Unit = ()
(rowIdx: Int,
rowTuple: EmptyTuple,
colDim: Int,
setElem: (rowIdx: Int, colIdx: Int, elem: T) => Unit
)
: Unit = ()

given inductiveRowTupleReader[Tail <: Tuple, T]
(using tailReader: RowTupleReader[Tail, T])
: RowTupleReader[T *: Tail, T] with
(using tailReader: RowTupleReader[Tail, T])
: RowTupleReader[T *: Tail, T] with
def readRow
(
rowIdx: Int,
rowTuple: T *: Tail,
colDim: Int,
setElem: (rowIdx: Int, colIdx: Int, elem: T) => Unit
)
: Unit =
val curElem: T = rowTuple.head
(rowIdx: Int,
rowTuple: T *: Tail,
colDim: Int,
setElem: (rowIdx: Int, colIdx: Int, elem: T) => Unit
)
: Unit =
val curElem: T = rowTuple.head
val remainingRow: Tail = rowTuple.tail
val colIdx: Int = colDim - remainingRow.size - 1
val colIdx: Int = colDim - remainingRow.size - 1
setElem(rowIdx, colIdx, curElem)
tailReader.readRow(rowIdx, remainingRow, colDim, setElem)

trait MatrixTupleReader[MatrixTuple, RowTuple]:
def readMatrix
(
m: MatrixTuple,
rowDim: Int,
setRow: (rowIdx: Int, rowTuple: RowTuple) => Unit
)
trait MatrixTupleReader[MatrixTuple, RowTuple]:
def readMatrix(m: MatrixTuple, rowDim: Int, setRow: (rowIdx: Int, rowTuple: RowTuple) => Unit)
: Unit

given emptyMatrixTupleReader[RowTuple]: MatrixTupleReader[EmptyTuple, RowTuple] with
def readMatrix
(
m: EmptyTuple,
rowDim: Int,
setRow: (rowIdx: Int, rowTuple: RowTuple) => Unit
)
: Unit = ()
(m: EmptyTuple, rowDim: Int, setRow: (rowIdx: Int, rowTuple: RowTuple) => Unit)
: Unit = ()

given inductiveMatrixTupleReader[MatrixTail <: Tuple, RowTuple]
(using matrixTailReader: MatrixTupleReader[MatrixTail, RowTuple])
: MatrixTupleReader[RowTuple *: MatrixTail, RowTuple] with
(using matrixTailReader: MatrixTupleReader[MatrixTail, RowTuple])
: MatrixTupleReader[RowTuple *: MatrixTail, RowTuple] with
def readMatrix
(
m: RowTuple *: MatrixTail,
rowDim: Int,
setRow: (rowIdx: Int, rowTuple: RowTuple) => Unit
)
: Unit =
val curRow: RowTuple = m.head
(m: RowTuple *: MatrixTail,
rowDim: Int,
setRow: (rowIdx: Int, rowTuple: RowTuple) => Unit
)
: Unit =
val curRow: RowTuple = m.head
val remainingMatrix: MatrixTail = m.tail
val rowIdx: Int = rowDim - remainingMatrix.size - 1
val rowIdx: Int = rowDim - remainingMatrix.size - 1
setRow(rowIdx, curRow)
matrixTailReader.readMatrix(remainingMatrix, rowDim, setRow)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ trait DefaultDeterminant:
calcDet2x2(get)
else
var current: T = num.zero
for
colIdx <- 0 until dim
do
for colIdx <- 0 until dim do
val _get = excludeRowAndCol(0, colIdx, get)
val coeff =
if colIdx % 2 == 0 then
Expand Down
57 changes: 31 additions & 26 deletions matr-dflt-ops/shared/src/main/scala/matr/dflt/DefaultInverse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import matr.ElementDivision
import matr.ElementMultiplication
import matr.MatrixNotInvertibleException

/** Default determinant implementation based on Gauss Jordan method.
/** Default determinant implementation based on Gauss Jordan method.
*/
trait DefaultInverse:

given defaultInverse[R <: Int, C <: Int, T]
(using Matrix.Requirements.IsSquare[R, C])
(using ElementMultiplication.Aux[T, T, T], ElementDivision.Aux[T, T, T], Numeric[T], MatrixFactory[R, C, T])
(using
ElementMultiplication.Aux[T, T, T],
ElementDivision.Aux[T, T, T],
Numeric[T],
MatrixFactory[R, C, T]
)
: Inverse[R, C, T] with

def inv(m: M): M =
Expand All @@ -27,32 +32,32 @@ trait DefaultInverse:
val a = m.modify
val b = mf.identity.modify

for j <- 0 until n do
var p = j
while p < n && a(p, j) == num.zero do
p = p + 1
if p == n then throw new MatrixNotInvertibleException()
if p != j then
swapRows(a, j, p)
swapRows(b, j, p)
val f = a(j, j)
for k <- 0 until n do
a(j, k) = div.div(a(j, k), f)
b(j, k) = div.div(b(j, k), f)
for i <- 0 until n do
if i != j then
val g = a(i, j)
for k <- 0 until n do
a(i, k) = a(i, k) - mul.times(g, a(j, k))
b(i, k) = b(i, k) - mul.times(g, b(j, k))
for j <- 0 until n do
var p = j
while p < n && a(p, j) == num.zero do
p = p + 1
if p == n then
throw new MatrixNotInvertibleException()
if p != j then
swapRows(a, j, p)
swapRows(b, j, p)
val f = a(j, j)
for k <- 0 until n do
a(j, k) = div.div(a(j, k), f)
b(j, k) = div.div(b(j, k), f)
for i <- 0 until n do
if i != j then
val g = a(i, j)
for k <- 0 until n do
a(i, k) = a(i, k) - mul.times(g, a(j, k))
b(i, k) = b(i, k) - mul.times(g, b(j, k))

b.result

private def swapRows(mb: MatrixFactory.Builder[R, C, T], r1Idx: Int, r2Idx: Int): Unit =
for idx <- 0 until mb.colDim do
val buf = mb(r1Idx, idx)
mb(r1Idx, idx) = mb(r2Idx, idx)
mb(r2Idx, idx) = buf

private def swapRows(mb: MatrixFactory.Builder[R, C, T], r1Idx: Int, r2Idx: Int): Unit =
for idx <- 0 until mb.colDim do
val buf = mb(r1Idx, idx)
mb(r1Idx, idx) = mb(r2Idx, idx)
mb(r2Idx, idx) = buf

object DefaultInverse extends DefaultInverse
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package matr.dflt

import matr.MatrixAddition
import matr.MatrixFactory
import matr.MatrixAddition
import matr.MatrixFactory


trait DefaultMatrixAddition:
trait DefaultMatrixAddition:

given defaultMatrixAddition[R <: Int, C <: Int, T]
(using num: Numeric[T])
(using mf: MatrixFactory[R, C, T])
: MatrixAddition[R, C, T] with
given defaultMatrixAddition[R <: Int, C <: Int, T]
(using num: Numeric[T])
(using mf: MatrixFactory[R, C, T])
: MatrixAddition[R, C, T] with

def plus(lhs: M, rhs: M): M =
lhs.combine(rhs)(num.plus)
def plus(lhs: M, rhs: M): M = lhs.combine(rhs)(num.plus)


object DefaultMatrixAddition extends DefaultMatrixAddition
object DefaultMatrixAddition extends DefaultMatrixAddition
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package matr.dflt

import matr.MatrixFactory
import matr.MatrixSubtraction
import matr.MatrixFactory
import matr.MatrixSubtraction


trait DefaultMatrixSubtraction:
trait DefaultMatrixSubtraction:

given defaultMatrixSubtraction[R <: Int, C <: Int, T]
(using num: Numeric[T])
(using mf: MatrixFactory[R, C, T])
: MatrixSubtraction[R, C, T] with
given defaultMatrixSubtraction[R <: Int, C <: Int, T]
(using num: Numeric[T])
(using mf: MatrixFactory[R, C, T])
: MatrixSubtraction[R, C, T] with

def minus(lhs: M, rhs: M): M =
lhs.combine(rhs)(num.minus)
def minus(lhs: M, rhs: M): M = lhs.combine(rhs)(num.minus)


object DefaultMatrixSubtraction extends DefaultMatrixSubtraction
object DefaultMatrixSubtraction extends DefaultMatrixSubtraction
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package matr.dflt

import matr.Matrix
import matr.MatrixFactory
import matr.Transpose
import matr.Matrix
import matr.MatrixFactory
import matr.Transpose

trait DefaultTranspose:
trait DefaultTranspose:

given defaultTranspose[R <: Int, C <: Int, T]
(using Matrix.Requirements.NonNegativeDimensions[C, R])
(using ValueOf[R], ValueOf[C])
: Transpose[R, C, T] with
given defaultTranspose[R <: Int, C <: Int, T]
(using Matrix.Requirements.NonNegativeDimensions[C, R])
(using ValueOf[R], ValueOf[C])
: Transpose[R, C, T] with

def transpose(m: Matrix[R, C, T]): Matrix[C, R, T] =
DefaultTranspose.TransposeView(m)
def transpose(m: Matrix[R, C, T]): Matrix[C, R, T] = DefaultTranspose.TransposeView(m)

object DefaultTranspose extends DefaultTranspose:

object DefaultTranspose extends DefaultTranspose:

final class TransposeView[OrigR <: Int, OrigC <: Int, T](orig: Matrix[OrigR, OrigC, T])
(using Matrix.Requirements.NonNegativeDimensions[OrigC, OrigR])
(using ValueOf[OrigR], ValueOf[OrigC])
extends Matrix[OrigC, OrigR, T]:

override def apply(rowIdx: Int, colIdx: Int): T =
Matrix.Requirements.positionWithinShape(rowIdx, colIdx, rowDim, colDim)
orig(colIdx, rowIdx)
final class TransposeView[OrigR <: Int, OrigC <: Int, T]
(orig: Matrix[OrigR, OrigC, T])
(using Matrix.Requirements.NonNegativeDimensions[OrigC, OrigR])
(using ValueOf[OrigR], ValueOf[OrigC])
extends Matrix[OrigC, OrigR, T]:

override def apply(rowIdx: Int, colIdx: Int): T =
Matrix.Requirements.positionWithinShape(rowIdx, colIdx, rowDim, colDim)
orig(colIdx, rowIdx)

override def toString(): String = s"TransposeView(${orig.toString})"
40 changes: 24 additions & 16 deletions matr-tests/shared/src/test/scala/matr/MatrixCombineSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,48 @@ import org.scalacheck.Arbitrary
class MatrixCombineSpec extends MatrFlatSpec:

"Combining two Matrices" should "require matrices with same shape" in {
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double]
.tabulate((_, _) => Random.nextDouble)
val m2: Matrix[4, 4, Double] = MatrixFactory[4, 4, Double]
.tabulate((_, _) => Random.nextDouble)
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double].tabulate((_, _) =>
Random.nextDouble
)
val m2: Matrix[4, 4, Double] = MatrixFactory[4, 4, Double].tabulate((_, _) =>
Random.nextDouble
)
val fn: (Double, Double) => Int = (a, b) => (a + b).toInt
assertTypeError("val res = m1.combine(m2)(fn)")
}

it should
"require a function that takes the data type of the elements (left-hand side as first argument)" in {
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double]
.tabulate((_, _) => Random.nextDouble)
val m2: Matrix[2, 3, Float] = MatrixFactory[2, 3, Float]
.tabulate((_, _) => Random.nextFloat)
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double].tabulate((_, _) =>
Random.nextDouble
)
val m2: Matrix[2, 3, Float] = MatrixFactory[2, 3, Float].tabulate((_, _) =>
Random.nextFloat
)
val fn: (Float, Float) => Int = (a, b) => (a + b).toInt
assertTypeError("val res = m1.combine(m2)(fn)")
}

it should
"require a function that takes the data type of the elements (right-hand side as second argument)" in {
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double]
.tabulate((_, _) => Random.nextDouble)
val m2: Matrix[2, 3, Float] = MatrixFactory[2, 3, Float]
.tabulate((_, _) => Random.nextFloat)
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double].tabulate((_, _) =>
Random.nextDouble
)
val m2: Matrix[2, 3, Float] = MatrixFactory[2, 3, Float].tabulate((_, _) =>
Random.nextFloat
)
val fn: (Double, Double) => Int = (a, b) => (a + b).toInt
assertTypeError("val res = m1.combine(m2)(fn)")
}

it should
"require a function that returns the data type of Matrix the result is assigned to" in {
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double]
.tabulate((_, _) => Random.nextDouble)
val m2: Matrix[2, 3, Float] = MatrixFactory[2, 3, Float]
.tabulate((_, _) => Random.nextFloat)
val m1: Matrix[2, 3, Double] = MatrixFactory[2, 3, Double].tabulate((_, _) =>
Random.nextDouble
)
val m2: Matrix[2, 3, Float] = MatrixFactory[2, 3, Float].tabulate((_, _) =>
Random.nextFloat
)
val fn: (Double, Double) => Int = (a, b) => (a + b).toInt

assertTypeError("val res: Matrix[2, 3, Float] = m1.combine(m2)(fn)")
Expand Down
Loading