AlgebraUtils { public static RealVector flattenMatrixByColumns(RealMatrix matrix) { int m = matrix.getColumnDimension(); int n = matrix.getRowDimension(); RealVector vector = matrix.getColumnVector(0); for (int i = 1; i < m; i++) { vector = vector.append(matrix.getColumnVector(i)); } return vector; }  static RealMatrix invertMatrix(RealMatrix matrix); static RealVector flattenMatrixByColumns(RealMatrix matrix); static RealMatrix reshapeMatrixByColumns(RealVector vector, RealMatrix shapeMatrix); static RealMatrix reshapeMatrixByColumns(RealVector vector, int width, int height); static double[][] getBoundingBox(List<Datum> data);  }
@Test public void testFlattenSquareMatrixByColumns() { double[][] matrixContents = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, }; double[] flattenedMatrixContents = { 1, 4, 7, 2, 5, 8, 3, 6, 9, }; double[] vectorContents = { 1, 2, 3, }; RealMatrix matrix = new BlockRealMatrix(matrixContents); RealVector vectorizedMatrix = AlgebraUtils.flattenMatrixByColumns(matrix); RealVector vector = new ArrayRealVector(vectorContents); assertEquals(vectorizedMatrix, new ArrayRealVector(flattenedMatrixContents)); assertEquals(vector.dotProduct(matrix.operate(vector)), vectorizedMatrix.dotProduct(AlgebraUtils.flattenMatrixByColumns(vector.outerProduct(vector)))); }
@Test public void testFlattenMatrixByColumns() { double[][] matrixContents = { {1, 2, 3}, {4, 5, 6}, }; double[] flattenedMatrixContents = { 1, 4, 2, 5, 3, 6, }; RealMatrix matrix = new BlockRealMatrix(matrixContents); RealVector vectorizedMatrix = AlgebraUtils.flattenMatrixByColumns(matrix); assertEquals(new ArrayRealVector(flattenedMatrixContents), vectorizedMatrix); }