-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix dot bug introduced in the last refactor. Add test for parallel li…
…near algebra.
- Loading branch information
Showing
6 changed files
with
83 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
kmath-core/src/jvmTest/kotlin/space/kscience/kmath/linear/ParallelMatrixTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2018-2024 KMath contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package space.kscience.kmath.linear | ||
|
||
import space.kscience.kmath.PerformancePitfall | ||
import space.kscience.kmath.UnstableKMathAPI | ||
import space.kscience.kmath.nd.StructureND | ||
import space.kscience.kmath.operations.Float64Field | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
@UnstableKMathAPI | ||
@OptIn(PerformancePitfall::class) | ||
@Suppress("UNUSED_VARIABLE") | ||
class ParallelMatrixTest { | ||
|
||
@Test | ||
fun testTranspose() = Float64Field.linearSpace.parallel{ | ||
val matrix = one(3, 3) | ||
val transposed = matrix.transposed() | ||
assertTrue { StructureND.contentEquals(matrix, transposed) } | ||
} | ||
|
||
@Test | ||
fun testBuilder() = Float64Field.linearSpace.parallel{ | ||
val matrix = matrix(2, 3)( | ||
1.0, 0.0, 0.0, | ||
0.0, 1.0, 2.0 | ||
) | ||
|
||
assertEquals(2.0, matrix[1, 2]) | ||
} | ||
|
||
@Test | ||
fun testMatrixExtension() = Float64Field.linearSpace.parallel{ | ||
val transitionMatrix: Matrix<Double> = VirtualMatrix(type,6, 6) { row, col -> | ||
when { | ||
col == 0 -> .50 | ||
row + 1 == col -> .50 | ||
row == 5 && col == 5 -> 1.0 | ||
else -> 0.0 | ||
} | ||
} | ||
|
||
infix fun Matrix<Double>.pow(power: Int): Matrix<Double> { | ||
var res = this | ||
repeat(power - 1) { | ||
res = res dot this@pow | ||
} | ||
return res | ||
} | ||
|
||
val toTenthPower = transitionMatrix pow 10 | ||
} | ||
|
||
@Test | ||
fun test2DDot() = Float64Field.linearSpace.parallel { | ||
val firstMatrix = buildMatrix(2, 3) { i, j -> (i + j).toDouble() } | ||
val secondMatrix = buildMatrix(3, 2) { i, j -> (i + j).toDouble() } | ||
|
||
// val firstMatrix = produce(2, 3) { i, j -> (i + j).toDouble() } | ||
// val secondMatrix = produce(3, 2) { i, j -> (i + j).toDouble() } | ||
val result = firstMatrix dot secondMatrix | ||
assertEquals(2, result.rowNum) | ||
assertEquals(2, result.colNum) | ||
assertEquals(8.0, result[0, 1]) | ||
assertEquals(8.0, result[1, 0]) | ||
assertEquals(14.0, result[1, 1]) | ||
} | ||
} |