Skip to content

Commit 845aea0

Browse files
committed
Merge pull request 'Replace FlagAttributes with BooleanAttributes for matrices to solve KS-626' (!530) from feature/boolean-flag-attributes into dev
Reviewed-on: https://git.sciprog.center/kscience/kmath/pulls/530 Reviewed-by: Gleb Minaev <glebminaev02@yandex.ru>
2 parents 73a6ad2 + 7267880 commit 845aea0

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ allprojects {
1515
}
1616

1717
group = "space.kscience"
18-
version = "0.4.3-dev-1"
18+
version = "0.5.0-dev-1"
1919
}
2020

2121
dependencies {
@@ -91,7 +91,7 @@ kscienceProject {
9191
@OptIn(ExperimentalAbiValidation::class)
9292
abiValidation {
9393
filters {
94-
excluded{
94+
excluded {
9595
annotatedWith.add("space.kscience.kmath.UnstableKMathAPI")
9696
}
9797
}

kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import space.kscience.kmath.PerformancePitfall
1515
import space.kscience.kmath.UnstableKMathAPI
1616
import space.kscience.kmath.nd.*
1717
import space.kscience.kmath.operations.*
18-
import space.kscience.kmath.structures.*
18+
import space.kscience.kmath.structures.Float64
19+
import space.kscience.kmath.structures.IntBuffer
20+
import space.kscience.kmath.structures.asBuffer
1921

2022
public interface LupDecomposition<T> {
2123
public val pivot: IntBuffer
@@ -47,7 +49,7 @@ public class GenericLupDecomposition<T>(
4749

4850

4951
override val l: Matrix<T>
50-
get() = VirtualMatrix(lu.rowNum, lu.colNum, attributes = Attributes(LowerTriangular)) { i, j ->
52+
get() = VirtualMatrix(lu.rowNum, lu.colNum, attributes = Attributes(LowerTriangular, true)) { i, j ->
5153
when {
5254
j < i -> lu[i, j]
5355
j == i -> elementAlgebra.one
@@ -56,7 +58,7 @@ public class GenericLupDecomposition<T>(
5658
}
5759

5860
override val u: Matrix<T>
59-
get() = VirtualMatrix(lu.rowNum, lu.colNum, attributes = Attributes(UpperTriangular)) { i, j ->
61+
get() = VirtualMatrix(lu.rowNum, lu.colNum, attributes = Attributes(UpperTriangular,true)) { i, j ->
6062
if (j >= i) lu[i, j] else elementAlgebra.zero
6163
}
6264

kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public fun <T, A : Ring<T>> MatrixBuilder<T, A>.diagonal(
8080
builder: A.(Int) -> T
8181
): Matrix<T> = with(linearSpace.elementAlgebra) {
8282
require(colNum == rowNum) { "In order to build symmetric matrix, number of rows $rowNum should be equal to number of columns $colNum" }
83-
return VirtualMatrix(rowNum, colNum, attributes = Attributes(IsDiagonal)) { i, j ->
83+
return VirtualMatrix(rowNum, colNum, attributes = Attributes(IsDiagonal, true)) { i, j ->
8484
check(i in 0 until rowNum) { "$i out of bounds: 0..<$rowNum" }
8585
check(j in 0 until colNum) { "$j out of bounds: 0..<$colNum" }
8686
if (i == j) {
@@ -97,7 +97,8 @@ public fun <T, A : Ring<T>> MatrixBuilder<T, A>.diagonal(
9797
@UnstableKMathAPI
9898
public fun <T> MatrixBuilder<T, Ring<T>>.diagonal(vararg elements: T): Matrix<T> {
9999
require(colNum == rowNum) { "In order to build symmetric matrix, number of rows $rowNum should be equal to number of columns $colNum" }
100-
return return VirtualMatrix(rowNum, colNum, attributes = Attributes(IsDiagonal)) { i, j ->
100+
101+
return VirtualMatrix(rowNum, colNum, attributes = Attributes(IsDiagonal,true)) { i, j ->
101102
check(i in 0 until rowNum) { "$i out of bounds: 0..<$rowNum" }
102103
check(j in 0 until colNum) { "$j out of bounds: 0..<$colNum" }
103104
if (i == j) {

kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,35 @@ public val <T : Any> Matrix<T>.origin: Matrix<T>
3636
/**
3737
* Add a single feature to a [Matrix]
3838
*/
39-
public fun <T, A : Attribute<T>> Matrix<T>.withAttribute(
40-
attribute: A,
39+
public fun <T> Matrix<T>.withAttribute(
40+
attribute: Attribute<T>,
4141
attrValue: T,
4242
): MatrixWrapper<T> = if (this is MatrixWrapper) {
4343
MatrixWrapper(origin, attributes.withAttribute(attribute, attrValue))
4444
} else {
4545
MatrixWrapper(this, Attributes(attribute, attrValue))
4646
}
4747

48-
public fun <T, A : Attribute<Unit>> Matrix<T>.withAttribute(
49-
attribute: A,
48+
public fun <T> Matrix<T>.withAttribute(
49+
attribute: Attribute<Unit>,
5050
): MatrixWrapper<T> = if (this is MatrixWrapper) {
5151
MatrixWrapper(origin, attributes.withFlag(attribute))
5252
} else {
5353
MatrixWrapper(this, Attributes(attribute, Unit))
5454
}
5555

56+
/**
57+
* Add boolean attribute with default value `true`
58+
*/
59+
public fun <T> Matrix<T>.withAttribute(
60+
attribute: Attribute<Boolean>,
61+
value: Boolean = true
62+
): MatrixWrapper<T> = if (this is MatrixWrapper) {
63+
MatrixWrapper(origin, attributes.withAttribute(attribute, value))
64+
} else {
65+
MatrixWrapper(this, Attributes(attribute, value))
66+
}
67+
5668
/**
5769
* Modify matrix attributes
5870
*/

kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/matrixAttributes.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ public interface MatrixScope<T> : WithType<T>
2323
*/
2424
public interface MatrixAttribute<T> : StructureAttribute<T>
2525

26+
public typealias BooleanAttribute = Attribute<Boolean>
27+
28+
2629
/**
2730
* Matrices with this feature are symmetric, meaning `matrix[i,j] == matrix[j,i]`
2831
*/
29-
public interface Symmetric : MatrixAttribute<Unit>, FlagAttribute{
30-
public companion object: Symmetric
32+
public interface Symmetric : MatrixAttribute<Boolean> {
33+
public companion object : Symmetric
3134
}
3235

3336
/**
@@ -72,12 +75,12 @@ public val <T> MatrixScope<T>.Determinant: Determinant<T> get() = Determinant(ty
7275
/**
7376
* Matrices with this feature are lower triangular ones.
7477
*/
75-
public object LowerTriangular : MatrixAttribute<Unit>, FlagAttribute
78+
public object LowerTriangular : MatrixAttribute<Boolean>
7679

7780
/**
7881
* Matrices with this feature are upper triangular ones.
7982
*/
80-
public object UpperTriangular : MatrixAttribute<Unit>, FlagAttribute
83+
public object UpperTriangular : MatrixAttribute<Boolean>
8184

8285
/**
8386
* Matrices with this feature support LU factorization: *a = [l] &middot; [u]* where *a* is the owning matrix.
@@ -102,7 +105,7 @@ public val <T> MatrixScope<T>.LU: LuDecompositionAttribute<T> get() = LuDecompos
102105
* Matrices with this feature are orthogonal ones: *a &middot; a<sup>T</sup> = u* where *a* is the owning matrix, *u*
103106
* is the unit matrix ([IsUnit]).
104107
*/
105-
public object OrthogonalAttribute : MatrixAttribute<Unit>, FlagAttribute
108+
public object OrthogonalAttribute : MatrixAttribute<Boolean>
106109

107110

108111
public interface QRDecomposition<out T> {

0 commit comments

Comments
 (0)