Skip to content

Commit

Permalink
Add sparse matrix builder
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Jan 27, 2025
1 parent b230abe commit bdc9d35
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 222 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/publish.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Added
- Convenient matrix builders for rows, columns, vstacks and hstacks
- Spreadsheet matrix builder
- Sparse matrix builder

### Changed

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ allprojects {
}

group = "space.kscience"
version = "0.4.2-dev"
version = "0.4.2"
}

dependencies{
Expand Down
283 changes: 136 additions & 147 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 @@ -49,17 +49,10 @@ public class SparseMatrix<T>(
}
}

@UnstableKMathAPI
public fun <T> SparseMatrix<T>.fill(vararg elements: T): SparseMatrix<T> {
require(rowNum * colNum == elements.size) { "The number of elements ${elements.size} is not equal $rowNum * $colNum" }
for (i in 0 until rowNum) {
for (j in 0 until colNum) {
set(i, j, elements[i * rowNum + j])
}
}
return this
}

/**
* Create and optionally fill DOK [SparseMatrix]. Those matrices must be converted to dense or effective sparse form
* after creation for effective use.
*/
@UnstableKMathAPI
public fun <T> LinearSpace<T, Ring<T>>.sparse(
rows: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ public fun <T> LinearSpace<T, Ring<T>>.hstack(vararg matrices: Matrix<T>): Matri
columns[column][row]
}
}


/**
* Fill the matrix with given elements. The number of elements must be the same as the number of elements in the matrix.
*
* This method is used for small matrices and test purposes.
*/
@UnstableKMathAPI
public fun <T> MutableMatrix<T>.fill(vararg elements: T): MutableMatrix<T> {
require(rowNum * colNum == elements.size) { "The number of elements ${elements.size} is not equal $rowNum * $colNum" }
for (i in 0 until rowNum) {
for (j in 0 until colNum) {
set(i, j, elements[i * rowNum + j])
}
}
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package space.kscience.kmath.nd
import space.kscience.kmath.PerformancePitfall
import space.kscience.kmath.UnstableKMathAPI
import space.kscience.kmath.structures.Float64
import space.kscience.kmath.structures.Int32

public open class VirtualStructureND<T>(
override val shape: ShapeND,
Expand All @@ -22,13 +23,13 @@ public open class VirtualStructureND<T>(
}

@UnstableKMathAPI
public class VirtualDoubleStructureND(
public class VirtualFloat64StructureND(
shape: ShapeND,
producer: (IntArray) -> Double,
producer: (IntArray) -> Float64,
) : VirtualStructureND<Float64>(shape, producer)

@UnstableKMathAPI
public class VirtualIntStructureND(
public class VirtualInt32StructureND(
shape: ShapeND,
producer: (IntArray) -> Int,
producer: (IntArray) -> Int32,
) : VirtualStructureND<Int>(shape, producer)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
package space.kscience.kmath.linear

import space.kscience.kmath.UnstableKMathAPI
import space.kscience.kmath.nd.StructureND
import space.kscience.kmath.operations.algebra
import space.kscience.kmath.structures.Float64
import kotlin.test.Test
import kotlin.test.assertEquals

@UnstableKMathAPI
class MatrixBuilderTest {
Expand All @@ -31,8 +29,15 @@ class MatrixBuilderTest {
)
)

println(StructureND.toString(matrix))
val expected = sparse(5, 5).fill(
0.0, 0.0, 0.0, 0.0, 1.0,
1.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0
)

assertMatrixEquals(expected, matrix)

assertEquals(1.0, matrix[0, 4])
}
}
8 changes: 4 additions & 4 deletions kmath-viktor/api/kmath-viktor.api
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class space/kscience/kmath/viktor/ViktorFieldND : space/kscience/kmath/vi
public synthetic fun getOne ()Ljava/lang/Object;
public synthetic fun getOne ()Lspace/kscience/kmath/nd/StructureND;
public fun getOne ()Lspace/kscience/kmath/viktor/ViktorStructureND;
public fun getShape-IIYLAfE ()[I
public fun getShape ()Lspace/kscience/kmath/nd/ShapeND;
public synthetic fun getZero ()Ljava/lang/Object;
public synthetic fun getZero ()Lspace/kscience/kmath/nd/StructureND;
public fun getZero ()Lspace/kscience/kmath/viktor/ViktorStructureND;
Expand Down Expand Up @@ -70,8 +70,8 @@ public class space/kscience/kmath/viktor/ViktorFieldOpsND : space/kscience/kmath
public fun mapIndexed (Lspace/kscience/kmath/nd/StructureND;Lkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/viktor/ViktorStructureND;
public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
public fun minus (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;)Lspace/kscience/kmath/viktor/ViktorStructureND;
public synthetic fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND;
public fun mutableStructureND-qL90JFI ([ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/viktor/ViktorStructureND;
public synthetic fun mutableStructureND (Lspace/kscience/kmath/nd/ShapeND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/MutableStructureND;
public fun mutableStructureND (Lspace/kscience/kmath/nd/ShapeND;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/viktor/ViktorStructureND;
public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
public fun plus (Lspace/kscience/kmath/nd/StructureND;D)Lspace/kscience/kmath/viktor/ViktorStructureND;
public synthetic fun plus (Lspace/kscience/kmath/nd/StructureND;Ljava/lang/Object;)Lspace/kscience/kmath/nd/StructureND;
Expand Down Expand Up @@ -110,7 +110,7 @@ public final class space/kscience/kmath/viktor/ViktorStructureND : space/kscienc
public fun get ([I)Ljava/lang/Double;
public synthetic fun get ([I)Ljava/lang/Object;
public final fun getF64Buffer ()Lorg/jetbrains/bio/viktor/F64Array;
public fun getShape-IIYLAfE ()[I
public fun getShape ()Lspace/kscience/kmath/nd/ShapeND;
public fun set ([ID)V
public synthetic fun set ([ILjava/lang/Object;)V
}
Expand Down
2 changes: 2 additions & 0 deletions test-utils/api/test-utils.api
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
public final class space/kscience/kmath/testutils/AssertsKt {
public static final fun assertBufferEquals (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;D)V
public static synthetic fun assertBufferEquals$default (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;DILjava/lang/Object;)V
public static final fun assertStructureEquals (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;D)V
public static synthetic fun assertStructureEquals$default (Lspace/kscience/kmath/nd/StructureND;Lspace/kscience/kmath/nd/StructureND;DILjava/lang/Object;)V
}

public final class space/kscience/kmath/testutils/BufferEqualityKt {
Expand Down

0 comments on commit bdc9d35

Please sign in to comment.