Skip to content

Commit 5114e3b

Browse files
authored
Merge pull request #1 from carbotaniuman/master
Add explicit types to function returns and remove reified Nat<T> from Mat
2 parents ac3789e + 8784ceb commit 5114e3b

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

src/main/kotlin/frc/team4069/keigen/Matrix.kt

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,60 @@ import org.ejml.dense.row.CommonOps_DDRM
44
import org.ejml.dense.row.NormOps_DDRM
55
import org.ejml.simple.SimpleMatrix
66

7-
open class Matrix<R: Num, C: Num>(private val rows: Nat<R>, private val cols: Nat<C>, internal val storage: SimpleMatrix) {
8-
val numCols get() = cols.i
7+
open class Matrix<R: Num, C: Num> internal constructor(internal val storage: SimpleMatrix) {
8+
val numCols get(): Int = storage.numCols()
9+
val numRows get(): Int = storage.numRows()
910

10-
val numRows get() = rows.i
11-
12-
operator fun get(i: Int, j: Int) = storage[i, j]
11+
operator fun get(i: Int, j: Int): Double = storage[i, j]
1312

1413
operator fun set(i: Int, j: Int, k: Double) {
1514
storage[i, j] = k
1615
}
1716

18-
fun diag() = Matrix(rows, cols, storage.diag())
19-
fun maxInternal() = CommonOps_DDRM.elementMax(this.storage.ddrm)
20-
fun minInternal() = CommonOps_DDRM.elementMin(this.storage.ddrm)
17+
fun diag(): Matrix<R, C> = Matrix(storage.diag())
18+
fun maxInternal(): Double = CommonOps_DDRM.elementMax(this.storage.ddrm)
19+
fun minInternal(): Double = CommonOps_DDRM.elementMin(this.storage.ddrm)
2120
fun mean() = elementSum() / storage.numElements.toDouble()
2221

2322
operator fun <C2: Num> times(other: Matrix<C, C2>): Matrix<R, C2>
24-
= Matrix(rows, other.cols, this.storage.mult(other.storage))
23+
= Matrix(this.storage.mult(other.storage))
2524

2625
operator fun times(value: Double): Matrix<R, C>
27-
= Matrix(rows, cols, this.storage.scale(value))
26+
= Matrix(this.storage.scale(value))
2827

2928
fun elementTimes(other: Matrix<R, C>): Matrix<R, C>
30-
= Matrix(rows, cols, this.storage.elementMult(other.storage))
29+
= Matrix(this.storage.elementMult(other.storage))
3130

32-
operator fun unaryMinus() = Matrix(rows, cols, storage.scale(-1.0))
31+
operator fun unaryMinus(): Matrix<R, C> = Matrix(storage.scale(-1.0))
3332

34-
operator fun minus(value: Double) = Matrix(rows, cols, storage.minus(value))
33+
operator fun minus(value: Double): Matrix<R, C> = Matrix(storage.minus(value))
3534

36-
operator fun minus(value: Matrix<R, C>) = Matrix(rows, cols, storage.minus(value.storage))
35+
operator fun minus(value: Matrix<R, C>): Matrix<R, C> = Matrix(storage.minus(value.storage))
3736

38-
operator fun plus(value: Double) = Matrix(rows, cols, storage.plus(value))
37+
operator fun plus(value: Double): Matrix<R, C> = Matrix(storage.plus(value))
3938

40-
operator fun plus(value: Matrix<R, C>) = Matrix(rows, cols, storage.plus(value.storage))
39+
operator fun plus(value: Matrix<R, C>): Matrix<R, C> = Matrix(storage.plus(value.storage))
4140

42-
operator fun div(value: Int) = Matrix(rows, cols, storage.divide(value.toDouble()))
41+
operator fun div(value: Int): Matrix<R, C> = Matrix(storage.divide(value.toDouble()))
4342

44-
operator fun div(value: Double) = Matrix(rows, cols, storage.divide(value))
43+
operator fun div(value: Double): Matrix<R, C> = Matrix(storage.divide(value))
4544

46-
fun transpose(): Matrix<C, R> = Matrix(cols, rows, storage.transpose())
45+
fun transpose(): Matrix<C, R> = Matrix(storage.transpose())
4746

48-
fun copy() = Matrix(cols, rows, storage.copy())
47+
fun copy(): Matrix<R, C> = Matrix(storage.copy())
4948

50-
fun inv(): Matrix<R, C> = Matrix(rows, cols, storage.invert())
49+
fun inv(): Matrix<R, C> = Matrix(storage.invert())
5150

5251
fun det(): Double = storage.determinant()
5352

54-
fun normF() = storage.normF()
53+
fun normF(): Double = storage.normF()
5554

56-
fun normIndP1() = NormOps_DDRM.inducedP1(this.storage.ddrm)
55+
fun normIndP1(): Double = NormOps_DDRM.inducedP1(this.storage.ddrm)
5756

58-
fun elementSum() = this.storage.elementSum()
57+
fun elementSum(): Double = this.storage.elementSum()
5958

60-
fun trace() = this.storage.trace()
59+
fun trace(): Double = this.storage.trace()
6160

62-
fun epow(other: Double): Matrix<R, C> = Matrix(rows, cols, storage.elementPower(other))
63-
fun epow(other: Int): Matrix<R, C> = Matrix(rows, cols, storage.elementPower(other.toDouble()))
61+
fun epow(other: Double): Matrix<R, C> = Matrix(storage.elementPower(other))
62+
fun epow(other: Int): Matrix<R, C> = Matrix(storage.elementPower(other.toDouble()))
6463
}

src/main/kotlin/frc/team4069/keigen/creatorDsl.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MatBuilder<R : Num, C : Num>(val rows: Nat<R>, val cols: Nat<C>) {
1313
}
1414

1515
val matData = data.toList().chunked(cols.i).map { it.toDoubleArray() }.toTypedArray()
16-
return Matrix(rows, cols, SimpleMatrix(matData))
16+
return Matrix(SimpleMatrix(matData))
1717
}
1818

1919
fun fill(vararg data: Int): Matrix<R, C> {
@@ -22,7 +22,7 @@ class MatBuilder<R : Num, C : Num>(val rows: Nat<R>, val cols: Nat<C>) {
2222
}
2323

2424
val matData = data.map { it.toDouble() }.chunked(cols.i).map { it.toDoubleArray() }.toTypedArray()
25-
return Matrix(rows, cols, SimpleMatrix(matData))
25+
return Matrix(SimpleMatrix(matData))
2626
}
2727
}
2828

@@ -32,14 +32,14 @@ class VecBuilder<D : Num>(val dim: Nat<D>) {
3232
throw IllegalArgumentException("Invalid number of elements for ${dim.i}-dimensional vector. got ${data.size} elements")
3333
}
3434

35-
return Matrix(dim, `1`, SimpleMatrix(dim.i, 1, false, data))
35+
return Matrix(SimpleMatrix(dim.i, 1, false, data))
3636
}
3737

3838
fun fill(vararg data: Int): Vector<D> {
3939
if (data.size != dim.i) {
4040
throw IllegalArgumentException("Invalid number of elements for ${dim.i}-dimensional vector. got ${data.size} elements")
4141
}
4242

43-
return Matrix(dim, `1`, SimpleMatrix(dim.i, 1, false, data.map { it.toDouble() }.toDoubleArray()))
43+
return Matrix(SimpleMatrix(dim.i, 1, false, data.map { it.toDouble() }.toDoubleArray()))
4444
}
4545
}

src/main/kotlin/frc/team4069/keigen/creators.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ package frc.team4069.keigen
33
import org.ejml.dense.row.CommonOps_DDRM
44
import org.ejml.simple.SimpleMatrix
55

6-
fun <R: Num, C: Num> zeros(rows: Nat<R>, cols: Nat<C>) = Matrix(rows, cols, SimpleMatrix(rows.i, cols.i))
7-
fun <D: Num> zeros(size: Nat<D>) = Matrix(size, `1`, SimpleMatrix(size.i, 1))
6+
fun <R: Num, C: Num> zeros(rows: Nat<R>, cols: Nat<C>): Matrix<R, C> = Matrix(SimpleMatrix(rows.i, cols.i))
7+
fun <D: Num> zeros(size: Nat<D>): Matrix<D, `1`> = Matrix(SimpleMatrix(size.i, 1))
88

9-
fun <D: Num> eye(size: Nat<D>) = Matrix(size, size, SimpleMatrix.identity(size.i))
9+
fun <D: Num> eye(size: Nat<D>): Matrix<D, D> = Matrix(SimpleMatrix.identity(size.i))
1010

1111
fun <R: Num, C: Num> ones(rows: Nat<R>, cols: Nat<C>): Matrix<R, C> {
1212
val out = SimpleMatrix(rows.i, cols.i)
1313
CommonOps_DDRM.fill(out.ddrm, 1.0)
14-
return Matrix(rows, cols, out)
14+
return Matrix(out)
1515
}
1616
fun <D: Num> ones(size: Nat<D>): Matrix<D, `1`> {
1717
val out = SimpleMatrix(size.i, 1)
1818
CommonOps_DDRM.fill(out.ddrm, 1.0)
19-
20-
return Matrix(size, `1`, out)
19+
return Matrix(out)
2120
}
2221

src/test/kotlin/frc/team4069/keigen/MatrixTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ class MatrixTest {
4343

4444
Assert.assertTrue(MatrixFeatures_DDRM.isEquals(targetVec.storage.ddrm, res.storage.ddrm))
4545
}
46+
47+
@Test
48+
fun testMatNumColRow() {
49+
val m = mat(`2`, `3`).fill(1, 2, 3, 4, 5, 6)
50+
51+
Assert.assertEquals(m.numRows, 2)
52+
Assert.assertEquals(m.numCols, 3)
53+
}
4654
}

0 commit comments

Comments
 (0)