From 0163b4a48e5a34ace868484ee59139d83d6fd758 Mon Sep 17 00:00:00 2001 From: Ezrnest <1403718476@qq.com> Date: Fri, 20 Feb 2026 16:58:58 +0800 Subject: [PATCH] Fix Matrix.fromColumns indexing and add regression tests --- .../github/ezrnest/mathsymk/linear/Matrix.kt | 4 +-- src/test/kotlin/linear/MatrixTest.kt | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/io/github/ezrnest/mathsymk/linear/Matrix.kt b/src/main/kotlin/io/github/ezrnest/mathsymk/linear/Matrix.kt index 0ba5ac0..f2e44b1 100644 --- a/src/main/kotlin/io/github/ezrnest/mathsymk/linear/Matrix.kt +++ b/src/main/kotlin/io/github/ezrnest/mathsymk/linear/Matrix.kt @@ -252,7 +252,7 @@ interface Matrix : GenTuple { val row = columns.first().size val column = columns.size require(columns.all { it.size == row }) - return Matrix(row, column) { i, j -> columns[i][j] } + return Matrix(row, column) { i, j -> columns[j][i] } } /** @@ -1564,4 +1564,4 @@ internal class MatOverFieldImpl(override val model: Field, row: Int, colum // require(ps.all { it in this }) // return MatrixImpl.product(ps, model) // } -//} \ No newline at end of file +//} diff --git a/src/test/kotlin/linear/MatrixTest.kt b/src/test/kotlin/linear/MatrixTest.kt index 0bf29d0..73fd784 100644 --- a/src/test/kotlin/linear/MatrixTest.kt +++ b/src/test/kotlin/linear/MatrixTest.kt @@ -17,6 +17,38 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue class MatrixTest { + + @Test + fun fromColumnsKeepsColumnMajorPlacement() { + val columns = List(3) { col -> + Vector(3) { row -> 10 * col + row } + } + val matrix = Matrix.fromColumns(columns) + + for (row in 0 until matrix.row) { + for (col in 0 until matrix.column) { + assertEquals(columns[col][row], matrix[row, col]) + } + } + } + + @Test + fun fromColumnsKeepsColumnMajorPlacementForRectangularMatrix() { + val columns = List(3) { col -> + Vector(2) { row -> 10 * col + row } + } + val matrix = Matrix.fromColumns(columns) + + assertEquals(2, matrix.row) + assertEquals(3, matrix.column) + for (row in 0 until matrix.row) { + for (col in 0 until matrix.column) { + assertEquals(columns[col][row], matrix[row, col]) + } + } + } + + val Z = Models.ints() val Zmod7 = Models.intModP(7) val Zmod97 = Models.intModP(97)