Skip to content

Commit

Permalink
Vector space refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Mar 13, 2021
1 parent 5e6f65a commit be9398b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 300 deletions.
327 changes: 61 additions & 266 deletions kmath-core/api/kmath-core.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ public class MatrixBuilder<T : Any, A : Ring<T>>(
//TODO add specific matrix builder functions like diagonal, etc
}

@UnstableKMathAPI
public fun <T : Any, A : Ring<T>> LinearSpace<T, A>.matrix(rows: Int, columns: Int): MatrixBuilder<T, A> =
MatrixBuilder(this, rows, columns)

/**
* Build a square matrix from given elements.
* Create a matrix builder with given number of rows and columns
*/
@UnstableKMathAPI
public fun <T : Any> LinearSpace<T, Ring<T>>.square(vararg elements: T): Matrix<T> {
val size: Int = kotlin.math.sqrt(elements.size.toDouble()).toInt()
return matrix(size,size)(*elements)
}
public fun <T : Any, A : Ring<T>> LinearSpace<T, A>.matrix(rows: Int, columns: Int): MatrixBuilder<T, A> =
MatrixBuilder(this, rows, columns)

@UnstableKMathAPI
public fun <T : Any> LinearSpace<T, Ring<T>>.vector(vararg elements: T): Vector<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package space.kscience.kmath.linear

import space.kscience.kmath.nd.NDStructure

/**
* The matrix where each element is evaluated each time when is being accessed.
*
Expand All @@ -17,12 +19,8 @@ public class VirtualMatrix<T : Any>(

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Matrix<*>) return false

if (rowNum != other.rowNum) return false
if (colNum != other.colNum) return false

return elements().all { (index, value) -> value == other[index] }
if (other !is NDStructure<*>) return false
return NDStructure.contentEquals(this, other)
}

override fun hashCode(): Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package space.kscience.kmath.nd

import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.VirtualBuffer
import kotlin.reflect.KClass

/**
* A structure that is guaranteed to be two-dimensional.
Expand Down Expand Up @@ -58,15 +60,22 @@ public interface Structure2D<T> : NDStructure<T> {
/**
* A 2D wrapper for nd-structure
*/
private inline class Structure2DWrapper<T>(val structure: NDStructure<T>) : Structure2D<T> {
private class Structure2DWrapper<T>(val structure: NDStructure<T>) : Structure2D<T> {
override val shape: IntArray get() = structure.shape

override val rowNum: Int get() = shape[0]
override val colNum: Int get() = shape[1]

override operator fun get(i: Int, j: Int): T = structure[i, j]

@UnstableKMathAPI
override fun <F : Any> getFeature(type: KClass<F>): F? = structure.getFeature(type)

override fun elements(): Sequence<Pair<IntArray, T>> = structure.elements()

override fun equals(other: Any?): Boolean = structure == other

override fun hashCode(): Int = structure.hashCode()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package space.kscience.kmath.linear

import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.nd.NDStructure
import space.kscience.kmath.nd.as2D
import kotlin.test.Test
import kotlin.test.assertEquals

@UnstableKMathAPI
@Suppress("UNUSED_VARIABLE")
class MatrixTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package space.kscience.kmath.linear

import space.kscience.kmath.misc.UnstableKMathAPI
import kotlin.test.Test
import kotlin.test.assertEquals

@UnstableKMathAPI
class RealLUSolverTest {

@Test
Expand All @@ -15,7 +17,7 @@ class RealLUSolverTest {
@Test
fun testDecomposition() {
LinearSpace.real.run {
val matrix = square(
val matrix = matrix(2,2)(
3.0, 1.0,
1.0, 3.0
)
Expand All @@ -31,14 +33,14 @@ class RealLUSolverTest {

@Test
fun testInvert() {
val matrix = LinearSpace.real.square(
val matrix = LinearSpace.real.matrix(2,2)(
3.0, 1.0,
1.0, 3.0
)

val inverted = LinearSpace.real.inverseWithLup(matrix)

val expected = LinearSpace.real.square(
val expected = LinearSpace.real.matrix(2,2)(
0.375, -0.125,
-0.125, 0.375
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public typealias RealMatrix = Matrix<Double>
public fun realMatrix(rowNum: Int, colNum: Int, initializer: RealField.(i: Int, j: Int) -> Double): RealMatrix =
LinearSpace.real.buildMatrix(rowNum, colNum, initializer)

@OptIn(UnstableKMathAPI::class)
public fun realMatrix(rowNum: Int, colNum: Int): MatrixBuilder<Double, RealField> =
LinearSpace.real.matrix(rowNum, colNum)

public fun Array<DoubleArray>.toMatrix(): RealMatrix {
return LinearSpace.real.buildMatrix(size, this[0].size) { row, col -> this@toMatrix[row][col] }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

@UnstableKMathAPI
internal class RealMatrixTest {
@Test
fun testSum() {
Expand All @@ -30,11 +31,11 @@ internal class RealMatrixTest {

@Test
fun testRepeatStackVertical() {
val matrix1 = LinearSpace.real.matrix(2, 3)(
val matrix1 = realMatrix(2, 3)(
1.0, 0.0, 0.0,
0.0, 1.0, 2.0
)
val matrix2 = LinearSpace.real.matrix(6, 3)(
val matrix2 = realMatrix(6, 3)(
1.0, 0.0, 0.0,
0.0, 1.0, 2.0,
1.0, 0.0, 0.0,
Expand All @@ -47,7 +48,7 @@ internal class RealMatrixTest {

@Test
fun testMatrixAndDouble() {
val matrix1 = LinearSpace.real.matrix(2, 3)(
val matrix1 = realMatrix(2, 3)(
1.0, 0.0, 3.0,
4.0, 6.0, 2.0
)
Expand All @@ -61,13 +62,13 @@ internal class RealMatrixTest {

@Test
fun testDoubleAndMatrix() {
val matrix1 = LinearSpace.real.matrix(2, 3)(
val matrix1 = realMatrix(2, 3)(
1.0, 0.0, 3.0,
4.0, 6.0, 2.0
)
val matrix2 = 20.0 - (10.0 + (5.0 * matrix1))
//val matrix2 = 10.0 + (5.0 * matrix1)
val expectedResult = LinearSpace.real.matrix(2, 3)(
val expectedResult = realMatrix(2, 3)(
5.0, 10.0, -5.0,
-10.0, -20.0, 0.0
)
Expand All @@ -76,15 +77,15 @@ internal class RealMatrixTest {

@Test
fun testSquareAndPower() {
val matrix1 = LinearSpace.real.matrix(2, 3)(
val matrix1 = realMatrix(2, 3)(
-1.0, 0.0, 3.0,
4.0, -6.0, -2.0
)
val matrix2 = LinearSpace.real.matrix(2, 3)(
val matrix2 = realMatrix(2, 3)(
1.0, 0.0, 9.0,
16.0, 36.0, 4.0
)
val matrix3 = LinearSpace.real.matrix(2, 3)(
val matrix3 = realMatrix(2, 3)(
-1.0, 0.0, 27.0,
64.0, -216.0, -8.0
)
Expand All @@ -95,16 +96,16 @@ internal class RealMatrixTest {
@OptIn(UnstableKMathAPI::class)
@Test
fun testTwoMatrixOperations() {
val matrix1 = LinearSpace.real.matrix(2, 3)(
val matrix1 = realMatrix(2, 3)(
-1.0, 0.0, 3.0,
4.0, -6.0, 7.0
)
val matrix2 = LinearSpace.real.matrix(2, 3)(
val matrix2 = realMatrix(2, 3)(
1.0, 0.0, 3.0,
4.0, 6.0, -2.0
)
val result = matrix1 * matrix2 + matrix1 - matrix2
val expectedResult = LinearSpace.real.matrix(2, 3)(
val expectedResult = realMatrix(2, 3)(
-3.0, 0.0, 9.0,
16.0, -48.0, -5.0
)
Expand All @@ -113,16 +114,16 @@ internal class RealMatrixTest {

@Test
fun testColumnOperations() {
val matrix1 = LinearSpace.real.matrix(2, 4)(
val matrix1 = realMatrix(2, 4)(
-1.0, 0.0, 3.0, 15.0,
4.0, -6.0, 7.0, -11.0
)
val matrix2 = LinearSpace.real.matrix(2, 5)(
val matrix2 = realMatrix(2, 5)(
-1.0, 0.0, 3.0, 15.0, -1.0,
4.0, -6.0, 7.0, -11.0, 4.0
)
val col1 = LinearSpace.real.matrix(2, 1)(0.0, -6.0)
val cols1to2 = LinearSpace.real.matrix(2, 2)(
val col1 = realMatrix(2, 1)(0.0, -6.0)
val cols1to2 = realMatrix(2, 2)(
0.0, 3.0,
-6.0, 7.0
)
Expand Down

0 comments on commit be9398b

Please sign in to comment.