forked from kscience/kmath
Added matrix builder for small matrices
This commit is contained in:
parent
ce29e0e26c
commit
569ff6357b
@ -107,26 +107,6 @@ interface GenericMatrixContext<T : Any, R : Ring<T>> : MatrixContext<T> {
|
||||
produce(rowNum, colNum) { i, j -> elementContext.run { get(i, j) * value } }
|
||||
}
|
||||
|
||||
/**
|
||||
* A marker interface representing some matrix feature like diagonal, sparce, zero, etc. Features used to optimize matrix
|
||||
* operations performance in some cases.
|
||||
*/
|
||||
interface MatrixFeature
|
||||
|
||||
object DiagonalFeature : MatrixFeature
|
||||
|
||||
object ZeroFeature : MatrixFeature
|
||||
|
||||
object UnitFeature : MatrixFeature
|
||||
|
||||
interface InverseMatrixFeature<T : Any> : MatrixFeature {
|
||||
val inverse: Matrix<T>
|
||||
}
|
||||
|
||||
interface DeterminantFeature<T : Any> : MatrixFeature {
|
||||
val determinant: T
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialized 2-d structure
|
||||
*/
|
||||
@ -173,6 +153,16 @@ interface Matrix<T : Any> : NDStructure<T> {
|
||||
val buffer = elements.asBuffer()
|
||||
return BufferMatrix(size, size, buffer)
|
||||
}
|
||||
|
||||
fun <T : Any> build(rows: Int, columns: Int): MatrixBuilder<T> = MatrixBuilder(rows, columns)
|
||||
}
|
||||
}
|
||||
|
||||
class MatrixBuilder<T : Any>(val rows: Int, val columns: Int) {
|
||||
operator fun invoke(vararg elements: T): Matrix<T> {
|
||||
if (rows * columns != elements.size) error("The number of elements ${elements.size} is not equal $rows * $columns")
|
||||
val buffer = elements.asBuffer()
|
||||
return BufferMatrix(rows, columns, buffer)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,30 @@
|
||||
package scientifik.kmath.linear
|
||||
|
||||
/**
|
||||
* A marker interface representing some matrix feature like diagonal, sparce, zero, etc. Features used to optimize matrix
|
||||
* operations performance in some cases.
|
||||
*/
|
||||
interface MatrixFeature
|
||||
|
||||
/**
|
||||
* The matrix with this feature is considered to have only diagonal non-null elements
|
||||
*/
|
||||
object DiagonalFeature : MatrixFeature
|
||||
|
||||
/**
|
||||
* Matix with this feature has all zero elements
|
||||
*/
|
||||
object ZeroFeature : MatrixFeature
|
||||
|
||||
/**
|
||||
* Matrix with this feature have unit elements on diagonal and zero elements in all other places
|
||||
*/
|
||||
object UnitFeature : MatrixFeature
|
||||
|
||||
interface InverseMatrixFeature<T : Any> : MatrixFeature {
|
||||
val inverse: Matrix<T>
|
||||
}
|
||||
|
||||
interface DeterminantFeature<T : Any> : MatrixFeature {
|
||||
val determinant: T
|
||||
}
|
@ -41,4 +41,14 @@ class MatrixTest {
|
||||
assertEquals(5.0, product[1, 0])
|
||||
assertEquals(6.0, product[2, 2])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBuilder() {
|
||||
val matrix = Matrix.build<Double>(2, 3)(
|
||||
1.0, 0.0, 0.0,
|
||||
0.0, 1.0, 2.0
|
||||
)
|
||||
|
||||
assertEquals(2.0, matrix[1, 2])
|
||||
}
|
||||
}
|
@ -8,8 +8,12 @@ repositories {
|
||||
|
||||
kotlin {
|
||||
jvm {
|
||||
compilations["main"].kotlinOptions.jvmTarget = "1.8"
|
||||
compilations["test"].kotlinOptions.jvmTarget = "1.8"
|
||||
compilations.all {
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
freeCompilerArgs += "-progressive"
|
||||
}
|
||||
}
|
||||
}
|
||||
js()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user