diff --git a/.gitignore b/.gitignore index 37a60052..3cc89f9c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.project /target/ /.idea/ +Demo.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bca17a1..7dbd0420 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ la4j-0.5.0 `May 2014` * New vector method: `foldNonZero` * New factory method: `createDiagonalMatrix` * New matrix source: `DiagonalMatrixSource` + * New entity: `MatrixBuilder` la4j-0.4.9 `Jan 2014` * Bug fix in `align()` method for big sparse matrices (reported by Michael Kapper) diff --git a/src/main/java/org/la4j/factory/Basic1DFactory.java b/src/main/java/org/la4j/factory/Basic1DFactory.java index 793fa7ac..d38faf4b 100644 --- a/src/main/java/org/la4j/factory/Basic1DFactory.java +++ b/src/main/java/org/la4j/factory/Basic1DFactory.java @@ -28,7 +28,7 @@ import org.la4j.matrix.dense.Basic1DMatrix; import org.la4j.matrix.source.MatrixSource; -public class Basic1DFactory extends BasicFactory implements Factory { +public class Basic1DFactory extends BasicFactory { private static final long serialVersionUID = 4071505L; @@ -42,6 +42,11 @@ public Matrix createMatrix(int rows, int columns) { return new Basic1DMatrix(rows, columns); } + @Override + public Matrix createMatrix(int rows, int columns, double[] array) { + return new Basic1DMatrix(rows, columns, array); + } + @Override public Matrix createMatrix(double[][] array) { return new Basic1DMatrix(array); @@ -66,29 +71,25 @@ public Matrix createConstantMatrix(int rows, int columns, double value) { } @Override - public Matrix createRandomMatrix(int rows, int columns) { + public Matrix createRandomMatrix(int rows, int columns, Random random) { double array[] = new double[rows * columns]; - Random rnd = new Random(); - for (int i = 0; i < rows * columns; i++) { - array[i] = rnd.nextDouble(); + array[i] = random.nextDouble(); } return new Basic1DMatrix(rows, columns, array); } @Override - public Matrix createRandomSymmetricMatrix(int size) { + public Matrix createRandomSymmetricMatrix(int size, Random random) { double array[] = new double[size * size]; - Random rnd = new Random(); - for (int i = 0; i < size; i++) { for (int j = i; j < size; j++) { - double value = rnd.nextDouble(); + double value = random.nextDouble(); array[i * size + j] = value; array[j * size + i] = value; } diff --git a/src/main/java/org/la4j/factory/Basic2DFactory.java b/src/main/java/org/la4j/factory/Basic2DFactory.java index f9114cb3..3556ed4a 100644 --- a/src/main/java/org/la4j/factory/Basic2DFactory.java +++ b/src/main/java/org/la4j/factory/Basic2DFactory.java @@ -28,7 +28,7 @@ import org.la4j.matrix.dense.Basic2DMatrix; import org.la4j.matrix.source.MatrixSource; -public class Basic2DFactory extends BasicFactory implements Factory { +public class Basic2DFactory extends BasicFactory { private static final long serialVersionUID = 4071505L; @@ -42,6 +42,11 @@ public Matrix createMatrix(int rows, int columns) { return new Basic2DMatrix(rows, columns); } + @Override + public Matrix createMatrix(int rows, int columns, double[] array) { + return new Basic2DMatrix(rows, columns, array); + } + @Override public Matrix createMatrix(double array[][]) { return new Basic2DMatrix(array); @@ -70,15 +75,13 @@ public Matrix createConstantMatrix(int rows, int columns, double value) { } @Override - public Matrix createRandomMatrix(int rows, int columns) { + public Matrix createRandomMatrix(int rows, int columns, Random random) { double array[][] = new double[rows][columns]; - Random rnd = new Random(); - for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { - array[i][j] = rnd.nextDouble(); + array[i][j] = random.nextDouble(); } } @@ -86,15 +89,13 @@ public Matrix createRandomMatrix(int rows, int columns) { } @Override - public Matrix createRandomSymmetricMatrix(int size) { + public Matrix createRandomSymmetricMatrix(int size, Random random) { double array[][] = new double[size][size]; - Random rnd = new Random(); - for (int i = 0; i < size; i++) { for (int j = i; j < size; j++) { - double value = rnd.nextDouble(); + double value = random.nextDouble(); array[i][j] = value; array[j][i] = value; } diff --git a/src/main/java/org/la4j/factory/BasicFactory.java b/src/main/java/org/la4j/factory/BasicFactory.java index d47f7c3d..a6d37467 100644 --- a/src/main/java/org/la4j/factory/BasicFactory.java +++ b/src/main/java/org/la4j/factory/BasicFactory.java @@ -24,12 +24,13 @@ import java.util.Arrays; import java.util.Random; +import org.la4j.matrix.Matrix; import org.la4j.vector.Vector; import org.la4j.vector.dense.BasicVector; import org.la4j.vector.source.VectorSource; -public abstract class BasicFactory implements Factory { +public abstract class BasicFactory extends Factory { private static final long serialVersionUID = 4071505L; @@ -69,13 +70,11 @@ public Vector createConstantVector(int length, double value) { @Override - public Vector createRandomVector(int length) { - - Random rnd = new Random(); + public Vector createRandomVector(int length, Random random) { double array[] = new double[length]; for (int i = 0; i < length; i++) { - array[i] = rnd.nextDouble(); + array[i] = random.nextDouble(); } return new BasicVector(array); diff --git a/src/main/java/org/la4j/factory/CCSFactory.java b/src/main/java/org/la4j/factory/CCSFactory.java index a56e34ff..72b79bdb 100644 --- a/src/main/java/org/la4j/factory/CCSFactory.java +++ b/src/main/java/org/la4j/factory/CCSFactory.java @@ -31,7 +31,7 @@ import org.la4j.matrix.sparse.CCSMatrix; import org.la4j.matrix.sparse.CRSMatrix; -public class CCSFactory extends CompressedFactory implements Factory { +public class CCSFactory extends CompressedFactory { private static final long serialVersionUID = 4071505L; @@ -45,6 +45,11 @@ public Matrix createMatrix(int rows, int columns) { return new CCSMatrix(rows, columns); } + @Override + public Matrix createMatrix(int rows, int columns, double array[]) { + return new CCSMatrix(rows, columns, array); + } + @Override public Matrix createMatrix(double[][] array) { return new CCSMatrix(array); @@ -84,9 +89,7 @@ public Matrix createConstantMatrix(int rows, int columns, double value) { } @Override - public Matrix createRandomMatrix(int rows, int columns) { - - Random random = new Random(); + public Matrix createRandomMatrix(int rows, int columns, Random random) { int cardinality = (rows * columns) / DENSITY; @@ -128,14 +131,12 @@ public Matrix createRandomMatrix(int rows, int columns) { } @Override - public Matrix createRandomSymmetricMatrix(int size) { + public Matrix createRandomSymmetricMatrix(int size, Random random) { // TODO: Issue 15 int cardinality = (size * size) / DENSITY; - Random random = new Random(); - Matrix matrix = new CCSMatrix(size, size, cardinality); for (int k = 0; k < cardinality / 2; k++) { diff --git a/src/main/java/org/la4j/factory/CRSFactory.java b/src/main/java/org/la4j/factory/CRSFactory.java index 055dd01b..e0166e05 100644 --- a/src/main/java/org/la4j/factory/CRSFactory.java +++ b/src/main/java/org/la4j/factory/CRSFactory.java @@ -30,7 +30,7 @@ import org.la4j.matrix.source.MatrixSource; import org.la4j.matrix.sparse.CRSMatrix; -public class CRSFactory extends CompressedFactory implements Factory { +public class CRSFactory extends CompressedFactory { private static final long serialVersionUID = 4071505L; @@ -44,6 +44,11 @@ public Matrix createMatrix(int rows, int columns) { return new CRSMatrix(rows, columns); } + @Override + public Matrix createMatrix(int rows, int columns, double[] array) { + return new CRSMatrix(rows, columns, array); + } + @Override public Matrix createMatrix(double[][] array) { return new CRSMatrix(array); @@ -83,9 +88,7 @@ public Matrix createConstantMatrix(int rows, int columns, double value) { } @Override - public Matrix createRandomMatrix(int rows, int columns) { - - Random random = new Random(); + public Matrix createRandomMatrix(int rows, int columns, Random random) { int cardinality = (rows * columns) / DENSITY; @@ -127,14 +130,12 @@ public Matrix createRandomMatrix(int rows, int columns) { } @Override - public Matrix createRandomSymmetricMatrix(int size) { + public Matrix createRandomSymmetricMatrix(int size, Random random) { // TODO: Issue 15 int cardinality = (size * size) / DENSITY; - Random random = new Random(); - Matrix matrix = new CRSMatrix(size, size, cardinality); for (int k = 0; k < cardinality / 2; k++) { diff --git a/src/main/java/org/la4j/factory/CompressedFactory.java b/src/main/java/org/la4j/factory/CompressedFactory.java index 7f71f696..3ca50b02 100644 --- a/src/main/java/org/la4j/factory/CompressedFactory.java +++ b/src/main/java/org/la4j/factory/CompressedFactory.java @@ -25,11 +25,12 @@ import java.util.Arrays; import java.util.Random; +import org.la4j.matrix.Matrix; import org.la4j.vector.Vector; import org.la4j.vector.source.VectorSource; import org.la4j.vector.sparse.CompressedVector; -public abstract class CompressedFactory implements Factory { +public abstract class CompressedFactory extends Factory { private static final long serialVersionUID = 4071505L; @@ -75,9 +76,7 @@ public Vector createConstantVector(int length, double value) { } @Override - public Vector createRandomVector(int length) { - - Random random = new Random(); + public Vector createRandomVector(int length, Random random) { int cardinality = length / DENSITY; diff --git a/src/main/java/org/la4j/factory/Factory.java b/src/main/java/org/la4j/factory/Factory.java index f896fb20..60f27fd6 100644 --- a/src/main/java/org/la4j/factory/Factory.java +++ b/src/main/java/org/la4j/factory/Factory.java @@ -22,20 +22,21 @@ package org.la4j.factory; import java.io.Serializable; +import java.util.Random; import org.la4j.matrix.Matrix; import org.la4j.matrix.source.MatrixSource; import org.la4j.vector.Vector; import org.la4j.vector.source.VectorSource; -public interface Factory extends Serializable { +public abstract class Factory implements Serializable { /** * Creates an empty matrix. * * @return an empty matrix */ - Matrix createMatrix(); + public abstract Matrix createMatrix(); /** * Creates a matrix of specified shape. @@ -45,7 +46,18 @@ public interface Factory extends Serializable { * * @return a new matrix of given shape */ - Matrix createMatrix(int rows, int columns); + public abstract Matrix createMatrix(int rows, int columns); + + /** + * Creates a matrix of specified shape. + * + * @param rows the number of matrix rows + * @param columns the number of matrix columns + * @param array the source 1D array + * + * @return a new matrix of given shape + */ + public abstract Matrix createMatrix(int rows, int columns, double array[]); /** * Creates a matrix from given {@code array}. @@ -54,7 +66,7 @@ public interface Factory extends Serializable { * * @return a new matrix of given array */ - Matrix createMatrix(double array[][]); + public abstract Matrix createMatrix(double array[][]); /** * Creates a matrix from another {@code matrix}. @@ -63,7 +75,7 @@ public interface Factory extends Serializable { * * @return a new matrix */ - Matrix createMatrix(Matrix matrix); + public abstract Matrix createMatrix(Matrix matrix); /** * Creates a matrix from given matrix {@code source}. @@ -72,7 +84,7 @@ public interface Factory extends Serializable { * * @return a new matrix */ - Matrix createMatrix(MatrixSource source); + public abstract Matrix createMatrix(MatrixSource source); /** * Creates a constant matrix of given shape with {@code value} stored in @@ -83,7 +95,7 @@ public interface Factory extends Serializable { * * @return a constant matrix */ - Matrix createConstantMatrix(int rows, int columns, double value); + public abstract Matrix createConstantMatrix(int rows, int columns, double value); /** * Creates a random matrix of given shape. @@ -93,7 +105,20 @@ public interface Factory extends Serializable { * * @return a random matrix */ - Matrix createRandomMatrix(int rows, int columns); + public Matrix createRandomMatrix(int rows, int columns) { + return createRandomMatrix(rows, columns, new Random()); + } + + /** + * Creates a random matrix of given shape. + * + * @param rows the number of matrix rows + * @param columns the number of matrix columns + * @param random the random object instance + * + * @return a random matrix + */ + public abstract Matrix createRandomMatrix(int rows, int columns, Random random); /** * Creates a square random symmetric matrix of given {@code size}. @@ -102,7 +127,19 @@ public interface Factory extends Serializable { * * @return a square random symmetric matrix */ - Matrix createRandomSymmetricMatrix(int size); + Matrix createRandomSymmetricMatrix(int size) { + return createRandomSymmetricMatrix(size, new Random()); + } + + /** + * Creates a square random symmetric matrix of given {@code size}. + * + * @param size the number of matrix rows/columns + * @param random the random object instance + * + * @return a square random symmetric matrix + */ + public abstract Matrix createRandomSymmetricMatrix(int size, Random random); /** * Creates a square matrix of given {@code size}. @@ -111,7 +148,7 @@ public interface Factory extends Serializable { * * @return a square matrix */ - Matrix createSquareMatrix(int size); + public abstract Matrix createSquareMatrix(int size); /** * Creates an identity matrix of given {@code size}. An identity matrix @@ -121,7 +158,7 @@ public interface Factory extends Serializable { * * @return an identity matrix */ - Matrix createIdentityMatrix(int size); + public abstract Matrix createIdentityMatrix(int size); /** * Creates a matrix from given blocks. @@ -139,7 +176,7 @@ public interface Factory extends Serializable { * * @return a block matrix */ - Matrix createBlockMatrix(Matrix a, Matrix b, Matrix c, Matrix d); + public abstract Matrix createBlockMatrix(Matrix a, Matrix b, Matrix c, Matrix d); /** * Creates a diagonal matrix of given {@code diagonal}. @@ -148,14 +185,14 @@ public interface Factory extends Serializable { * * @return a diagonal matrix */ - Matrix createDiagonalMatrix(double diagonal[]); + public abstract Matrix createDiagonalMatrix(double diagonal[]); /** * Creates an empty vector. * * @return empty vector */ - Vector createVector(); + public abstract Vector createVector(); /** * Creates a vector of given {@code length}. @@ -164,7 +201,7 @@ public interface Factory extends Serializable { * * @return a new vector */ - Vector createVector(int length); + public abstract Vector createVector(int length); /** * Creates a vector from given {@code array}. @@ -173,7 +210,7 @@ public interface Factory extends Serializable { * * @return a new vector */ - Vector createVector(double array[]); + public abstract Vector createVector(double array[]); /** * Creates a vector from another {@code vector}. @@ -182,7 +219,7 @@ public interface Factory extends Serializable { * * @return a new vector */ - Vector createVector(Vector vector); + public abstract Vector createVector(Vector vector); /** * Creates a vector of given {@code source}. @@ -191,7 +228,7 @@ public interface Factory extends Serializable { * * @return a new vector */ - Vector createVector(VectorSource source); + public abstract Vector createVector(VectorSource source); /** * Creates a constant vector of given {@code length} and constant @@ -202,7 +239,7 @@ public interface Factory extends Serializable { * * @return a constant vector */ - Vector createConstantVector(int length, double value); + public abstract Vector createConstantVector(int length, double value); /** * Creates a random vector of given {@code length}. @@ -211,5 +248,17 @@ public interface Factory extends Serializable { * * @return a random vector */ - Vector createRandomVector(int length); + public Vector createRandomVector(int length) { + return createRandomVector(length, new Random()); + } + + /** + * Creates a random vector of given {@code length}. + * + * @param length the vector's length + * @param random the random object instance + * + * @return a random vector + */ + public abstract Vector createRandomVector(int length, Random random); } diff --git a/src/main/java/org/la4j/matrix/Matrices.java b/src/main/java/org/la4j/matrix/Matrices.java index 2c2df8fa..175fda05 100644 --- a/src/main/java/org/la4j/matrix/Matrices.java +++ b/src/main/java/org/la4j/matrix/Matrices.java @@ -26,8 +26,11 @@ package org.la4j.matrix; import org.la4j.LinearAlgebra; +import org.la4j.factory.Factory; import org.la4j.io.MatrixMarketStream; import org.la4j.io.SymbolSeparatedStream; +import org.la4j.matrix.builder.TerminalMatrixBuilder; +import org.la4j.matrix.builder.MatrixBuilder; import org.la4j.matrix.functor.AdvancedMatrixPredicate; import org.la4j.matrix.functor.MatrixAccumulator; import org.la4j.matrix.functor.MatrixFunction; @@ -585,7 +588,7 @@ public static MatrixFunction asModFunction(double value) { * diagonal * matrix. */ - public static final MatrixPredicate DIAGONAL_MATRIX = + public static final MatrixPredicate DIAGONAL_MATRIX = new DiagonalMatrixPredicate(); /** @@ -902,6 +905,17 @@ public static MatrixSource asSymbolSeparatedSource(InputStream in, String separa return new StreamMatrixSource(new SymbolSeparatedStream(in, separator)); } + /** + * Creates a new matrix builder instance of given {@code factory}. + * + * @param factory the builder's factory + * + * @return a factorised matrix builder + */ + public static MatrixBuilder asBuilder(Factory factory) { + return new TerminalMatrixBuilder(factory); + } + /** * Makes a minimum matrix accumulator that accumulates the minimum of matrix elements. * diff --git a/src/main/java/org/la4j/matrix/builder/Array1DSourcedMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/Array1DSourcedMatrixBuilder.java new file mode 100644 index 00000000..1c615b31 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/Array1DSourcedMatrixBuilder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.matrix.Matrix; + +public class Array1DSourcedMatrixBuilder extends NonTerminalMatrixBuilder { + + private double array[]; + + public Array1DSourcedMatrixBuilder(UnderlyingMatrixBuilder underlying, double array[]) { + super(underlying); + this.array = array; + } + + @Override + public Matrix build() { + return underlying.build(array); + } + + @Override + public Matrix buildSymmetric() { + return underlying.buildSymmetric(array); + } + + @Override + public Matrix buildIdentity() { + return underlying.buildIdentity(array); + } + + @Override + public Matrix buildDiagonal() { + return underlying.buildDiagonal(array); + } + + @Override + public Matrix buildDiagonal(int rows, int columns) { + return underlying.buildDiagonal(rows, columns, array); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return underlying.buildIdentity(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(int rows, int columns) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix build(int rows, int columns) { + return underlying.build(rows, columns, array); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/Array2DSourcedMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/Array2DSourcedMatrixBuilder.java new file mode 100644 index 00000000..c1c2973c --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/Array2DSourcedMatrixBuilder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.matrix.Matrix; + +public class Array2DSourcedMatrixBuilder extends NonTerminalMatrixBuilder { + + private double array[][]; + + public Array2DSourcedMatrixBuilder(UnderlyingMatrixBuilder underlying, double array[][]) { + super(underlying); + this.array = array; + } + + @Override + public Matrix build() { + return underlying.build(array); + } + + @Override + public Matrix buildSymmetric() { + return underlying.buildSymmetric(array); + } + + @Override + public Matrix buildIdentity() { + return underlying.buildIdentity(array); + } + + @Override + public Matrix buildDiagonal() { + return underlying.buildDiagonal(array); + } + + @Override + public Matrix buildDiagonal(int rows, int columns) { + return underlying.buildDiagonal(rows, columns, array); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(int rows, int columns) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix build(int rows, int columns) { + return underlying.build(rows, columns, array); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/ConstantSourcedMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/ConstantSourcedMatrixBuilder.java new file mode 100644 index 00000000..015b3eb5 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/ConstantSourcedMatrixBuilder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.matrix.Matrix; + +public class ConstantSourcedMatrixBuilder extends NonTerminalMatrixBuilder { + + private double value; + + public ConstantSourcedMatrixBuilder(UnderlyingMatrixBuilder underlying, double value) { + super(underlying); + this.value = value; + } + + @Override + public Matrix build() { + return underlying.build(value); + } + + @Override + public Matrix buildSymmetric() { + return underlying.buildSymmetric(value); + } + + @Override + public Matrix buildIdentity() { + return underlying.buildIdentity(value); + } + + @Override + public Matrix buildDiagonal() { + return underlying.buildDiagonal(value); + } + + @Override + public Matrix buildDiagonal(int rows, int columns) { + return underlying.buildDiagonal(rows, columns, value); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return underlying.buildIdentity(rows, columns, value); + } + + @Override + public Matrix buildSymmetric(int rows, int columns) { + return underlying.buildSymmetric(rows, columns, value); + } + + @Override + public Matrix build(int rows, int columns) { + return underlying.build(rows, columns, value); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/MatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/MatrixBuilder.java new file mode 100644 index 00000000..5ec40969 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/MatrixBuilder.java @@ -0,0 +1,115 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.io.MatrixStream; +import org.la4j.matrix.Matrix; + +import java.util.Random; + +/** + * A matrix builder. + */ +public interface MatrixBuilder { + + /** + * Specifies the shape of a result matrix. + * + * @param rows the number of matrix rows + * @param columns the number of matrix columns + * + * @return a shaped matrix builder + */ + MatrixBuilder shape(int rows, int columns); + + /** + * Specifies the source of a result matrix. + * + * @param value the constant value source + * + * @return a sourced matrix builder + */ + MatrixBuilder source(double value); + + /** + * Specifies the source of a result matrix. + * + * @param array the 2D source array + * + * @return a sourced matrix builder + */ + MatrixBuilder source(double array[][]); + + /** + * Specifies the source of a result matrix. + * + * @param array the 1D source array + * + * @return a sourced matrix builder + */ + MatrixBuilder source(double array[]); + + /** + * Specifies the source of a result matrix. + * + * @param random the random generator instance + * + * @return a sourced matrix builder + */ + MatrixBuilder source(Random random); + + /** + * Specifies the source of a result matrix. + * + * @param stream the matrix stream instance + * + * @return a sourced matrix builder + */ + MatrixBuilder source(MatrixStream stream); + + /** + * Commits the building process and returns a simple matrix. + * + * @return a freshly built matrix + */ + Matrix build(); + + /** + * Commits the building process and returns a symmetric matrix. + * + * @return a freshly built matrix + */ + Matrix buildSymmetric(); + + /** + * Commits the building process and returns an identity matrix. + * + * @return a freshly built matrix + */ + Matrix buildIdentity(); + + /** + * Commits the building process and returns a diagonal matrix. + * + * @return a freshly built matrix + */ + Matrix buildDiagonal(); +} diff --git a/src/main/java/org/la4j/matrix/builder/NonTerminalMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/NonTerminalMatrixBuilder.java new file mode 100644 index 00000000..00a145f0 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/NonTerminalMatrixBuilder.java @@ -0,0 +1,255 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.io.MatrixStream; +import org.la4j.matrix.Matrix; + +import java.util.Random; + +public abstract class NonTerminalMatrixBuilder extends UnderlyingMatrixBuilder { + + protected UnderlyingMatrixBuilder underlying; + + public NonTerminalMatrixBuilder(UnderlyingMatrixBuilder underlying) { + this.underlying = underlying; + } + + @Override + public Matrix buildDiagonal(int rows, int columns, MatrixStream stream) { + return underlying.buildDiagonal(rows, columns, stream); + } + + @Override + public Matrix buildIdentity(int rows, int columns, MatrixStream stream) { + return underlying.buildIdentity(rows, columns, stream); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, MatrixStream stream) { + return underlying.buildSymmetric(rows, columns, stream); + } + + @Override + public Matrix build(int rows, int columns, MatrixStream stream) { + return underlying.build(rows, columns, stream); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, Random random) { + return underlying.buildDiagonal(rows, columns, random); + } + + @Override + public Matrix buildIdentity(int rows, int columns, Random random) { + return underlying.buildIdentity(rows, columns, random); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, Random random) { + return underlying.buildSymmetric(rows, columns, random); + } + + @Override + public Matrix build(int rows, int columns, Random random) { + return underlying.build(rows, columns, random); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, double[][] array) { + return underlying.buildDiagonal(rows, columns, array); + } + + @Override + public Matrix buildIdentity(int rows, int columns, double[][] array) { + return underlying.buildIdentity(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, double[][] array) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix build(int rows, int columns, double[][] array) { + return underlying.build(rows, columns, array); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, double[] array) { + return underlying.buildDiagonal(rows, columns, array); + } + + @Override + public Matrix buildIdentity(int rows, int columns, double[] array) { + return underlying.buildIdentity(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, double[] array) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix build(int rows, int columns, double[] array) { + return underlying.build(rows, columns, array); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, double value) { + return underlying.buildDiagonal(rows, columns, value); + } + + @Override + public Matrix buildIdentity(int rows, int columns, double value) { + return underlying.buildIdentity(rows, columns, value); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, double value) { + return underlying.buildSymmetric(rows, columns, value); + } + + @Override + public Matrix build(int rows, int columns, double value) { + return underlying.build(rows, columns, value); + } + + @Override + public Matrix buildDiagonal(MatrixStream stream) { + return null; + } + + @Override + public Matrix buildIdentity(MatrixStream stream) { + return underlying.buildIdentity(stream); + } + + @Override + public Matrix buildSymmetric(MatrixStream stream) { + return underlying.buildSymmetric(stream); + } + + @Override + public Matrix build(MatrixStream stream) { + return underlying.build(stream); + } + + @Override + public Matrix buildDiagonal(Random random) { + return underlying.buildDiagonal(random); + } + + @Override + public Matrix buildIdentity(Random random) { + return underlying.buildIdentity(random); + } + + @Override + public Matrix buildSymmetric(Random random) { + return underlying.buildSymmetric(random); + } + + @Override + public Matrix build(Random random) { + return underlying.build(random); + } + + @Override + public Matrix buildDiagonal(double[][] array) { + return underlying.buildDiagonal(array); + } + + @Override + public Matrix buildIdentity(double[][] array) { + return underlying.buildIdentity(array); + } + + @Override + public Matrix buildSymmetric(double[][] array) { + return underlying.buildSymmetric(array); + } + + @Override + public Matrix build(double[][] array) { + return underlying.build(array); + } + + @Override + public Matrix buildDiagonal(double[] array) { + return underlying.buildDiagonal(array); + } + + @Override + public Matrix buildIdentity(double[] array) { + return underlying.buildIdentity(array); + } + + @Override + public Matrix buildSymmetric(double[] array) { + return underlying.buildSymmetric(array); + } + + @Override + public Matrix build(double[] array) { + return underlying.build(array); + } + + @Override + public Matrix buildDiagonal(double value) { + return underlying.buildDiagonal(value); + } + + @Override + public Matrix buildIdentity(double value) { + return underlying.buildIdentity(value); + } + + @Override + public Matrix buildSymmetric(double value) { + return underlying.buildSymmetric(value); + } + + @Override + public Matrix build(double value) { + return underlying.build(value); + } + + @Override + public Matrix buildDiagonal(int rows, int columns) { + return underlying.buildDiagonal(rows, columns); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return underlying.buildIdentity(rows, columns); + } + + @Override + public Matrix buildSymmetric(int rows, int columns) { + return underlying.buildSymmetric(rows, columns); + } + + @Override + public Matrix build(int rows, int columns) { + return underlying.build(rows, columns); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/RandomSourcedMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/RandomSourcedMatrixBuilder.java new file mode 100644 index 00000000..be2b2acb --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/RandomSourcedMatrixBuilder.java @@ -0,0 +1,75 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.matrix.Matrix; + +import java.util.Random; + +public class RandomSourcedMatrixBuilder extends NonTerminalMatrixBuilder { + + private Random random; + + public RandomSourcedMatrixBuilder(UnderlyingMatrixBuilder underlying, Random random) { + super(underlying); + this.random = random; + } + + @Override + public Matrix build() { + return underlying.build(random); + } + + @Override + public Matrix buildSymmetric() { + return underlying.buildSymmetric(random); + } + + @Override + public Matrix buildIdentity() { + return underlying.buildIdentity(random); + } + + @Override + public Matrix buildDiagonal() { + return underlying.buildDiagonal(random); + } + + @Override + public Matrix buildDiagonal(int rows, int columns) { + return underlying.buildDiagonal(rows, columns, random); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return underlying.buildIdentity(rows, columns, random); + } + + @Override + public Matrix buildSymmetric(int rows, int columns) { + return underlying.buildSymmetric(rows, columns, random); + } + + @Override + public Matrix build(int rows, int columns) { + return underlying.build(rows, columns, random); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/ShapedMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/ShapedMatrixBuilder.java new file mode 100644 index 00000000..d0f7f887 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/ShapedMatrixBuilder.java @@ -0,0 +1,158 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.io.MatrixStream; +import org.la4j.matrix.Matrix; + +import java.util.Random; + +public class ShapedMatrixBuilder extends NonTerminalMatrixBuilder { + + private int rows; + private int columns; + + public ShapedMatrixBuilder(UnderlyingMatrixBuilder underlying, int rows, int columns) { + super(underlying); + this.rows = rows; + this.columns = columns; + } + + @Override + public Matrix build() { + return underlying.build(rows, columns); + } + + @Override + public Matrix buildSymmetric() { + return underlying.buildSymmetric(rows, columns); + } + + @Override + public Matrix buildIdentity() { + return underlying.buildIdentity(rows, columns); + } + + @Override + public Matrix buildDiagonal() { + return underlying.buildDiagonal(rows, columns); + } + + @Override + public Matrix buildDiagonal(MatrixStream stream) { + return underlying.buildDiagonal(rows, columns, stream); + } + + @Override + public Matrix buildIdentity(MatrixStream stream) { + return underlying.buildIdentity(rows, columns, stream); + } + + @Override + public Matrix buildSymmetric(MatrixStream stream) { + return underlying.buildSymmetric(rows, columns, stream); + } + + @Override + public Matrix build(MatrixStream stream) { + return underlying.build(rows, columns, stream); + } + + @Override + public Matrix buildDiagonal(Random random) { + return underlying.buildDiagonal(rows, columns, random); + } + + @Override + public Matrix buildIdentity(Random random) { + return underlying.buildIdentity(rows, columns, random); + } + + @Override + public Matrix buildSymmetric(Random random) { + return underlying.buildSymmetric(rows, columns, random); + } + + @Override + public Matrix build(Random random) { + return underlying.build(rows, columns, random); + } + + @Override + public Matrix buildDiagonal(double[][] array) { + return underlying.buildDiagonal(rows, columns, array); + } + + @Override + public Matrix buildIdentity(double[][] array) { + return underlying.buildIdentity(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(double[][] array) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix build(double[][] array) { + return underlying.build(rows, columns, array); + } + + @Override + public Matrix buildDiagonal(double[] array) { + return underlying.buildDiagonal(rows, columns, array); + } + + @Override + public Matrix buildIdentity(double[] array) { + return underlying.buildIdentity(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(double[] array) { + return underlying.buildSymmetric(rows, columns, array); + } + + @Override + public Matrix build(double[] array) { + return underlying.build(rows, columns, array); + } + + @Override + public Matrix buildDiagonal(double value) { + return underlying.buildDiagonal(rows, columns, value); + } + + @Override + public Matrix buildIdentity(double value) { + return underlying.buildIdentity(rows, columns, value); + } + + @Override + public Matrix buildSymmetric(double value) { + return underlying.buildSymmetric(rows, columns, value); + } + + @Override + public Matrix build(double value) { + return underlying.build(rows, columns, value); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/StreamSourcedMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/StreamSourcedMatrixBuilder.java new file mode 100644 index 00000000..7f0e5d83 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/StreamSourcedMatrixBuilder.java @@ -0,0 +1,74 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.io.MatrixStream; +import org.la4j.matrix.Matrix; + +public class StreamSourcedMatrixBuilder extends NonTerminalMatrixBuilder { + + private MatrixStream stream; + + public StreamSourcedMatrixBuilder(UnderlyingMatrixBuilder underlying, MatrixStream stream) { + super(underlying); + this.stream = stream; + } + + @Override + public Matrix build() { + return underlying.build(stream); + } + + @Override + public Matrix buildSymmetric() { + return underlying.buildSymmetric(stream); + } + + @Override + public Matrix buildIdentity() { + return underlying.buildIdentity(stream); + } + + @Override + public Matrix buildDiagonal() { + return underlying.buildDiagonal(stream); + } + + @Override + public Matrix buildDiagonal(int rows, int columns) { + return underlying.buildDiagonal(rows, columns, stream); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return underlying.buildIdentity(rows, columns, stream); + } + + @Override + public Matrix buildSymmetric(int rows, int columns) { + return underlying.buildSymmetric(rows, columns, stream); + } + + @Override + public Matrix build(int rows, int columns) { + return underlying.build(rows, columns, stream); + } +} diff --git a/src/main/java/org/la4j/matrix/builder/TerminalMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/TerminalMatrixBuilder.java new file mode 100644 index 00000000..5c817ff3 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/TerminalMatrixBuilder.java @@ -0,0 +1,229 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.factory.Factory; +import org.la4j.io.MatrixStream; +import org.la4j.matrix.Matrices; +import org.la4j.matrix.Matrix; +import org.la4j.matrix.source.MatrixSource; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Random; + +public class TerminalMatrixBuilder extends UnderlyingMatrixBuilder { + + private Factory factory; + + public TerminalMatrixBuilder(Factory factory) { + this.factory = factory; + } + + @Override + public Matrix build() { + return factory.createMatrix(); + } + + @Override + public Matrix build(int rows, int columns) { + return factory.createMatrix(rows, columns); + } + + @Override + public Matrix buildIdentity(int rows, int columns) { + return factory.createIdentityMatrix(Math.min(rows, columns)); + } + + @Override + public Matrix buildDiagonal(double[] array) { + return factory.createDiagonalMatrix(array); + } + + @Override + public Matrix build(double[][] array) { + return factory.createMatrix(array); + } + + @Override + public Matrix buildSymmetric(double[][] array) { + return symmetrized(Matrices.asArray2DSource(array)); + } + + @Override + public Matrix buildDiagonal(double[][] array) { + + int size = Math.min(array.length, array[0].length); + double diagonal[] = new double[size]; + + for (int i = 0; i < size; i++) { + diagonal[i] = array[i][i]; + } + + return buildDiagonal(diagonal); + } + + @Override + public Matrix buildIdentity(double[][] array) { + return buildIdentity(array.length, array[0].length); + } + + @Override + public Matrix build(MatrixStream stream) { + return streamedOrEmpty(stream); + } + + @Override + public Matrix buildSymmetric(MatrixStream stream) { + return symmetrized(Matrices.asMatrixSource(streamedOrEmpty(stream))); + } + + @Override + public Matrix buildIdentity(MatrixStream stream) { + Matrix source = streamedOrEmpty(stream); + int size = Math.min(source.rows(), source.columns()); + return buildIdentity(size, size); + } + + @Override + public Matrix buildDiagonal(MatrixStream stream) { + + Matrix source = streamedOrEmpty(stream); + int size = Math.min(source.rows(), source.columns()); + double diagonal[] = new double[size]; + + for (int i = 0; i < size; i++) { + diagonal[i] = source.get(i, i); + } + + return buildDiagonal(diagonal); + } + + @Override + public Matrix build(int rows, int columns, double value) { + return factory.createConstantMatrix(rows, columns, value); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, double value) { + int size = Math.min(rows, columns); + double diagonal[] = new double[size]; + Arrays.fill(diagonal, value); + return buildDiagonal(diagonal); + } + + @Override + public Matrix build(int rows, int columns, double[] array) { + return factory.createMatrix(rows, columns, array); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, double[] array) { + return symmetrized(Matrices.asArray1DSource(rows, columns, array)); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, double[] array) { + int size = Math.min(rows, columns); + return factory.createDiagonalMatrix(array).resize(size, size); + } + + @Override + public Matrix build(int rows, int columns, double[][] array) { + return factory.createMatrix(array).resize(rows, columns); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, double[][] array) { + int size = Math.min(rows, columns); + return buildSymmetric(array).resize(size, size); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, double[][] array) { + int size = Math.min(rows, columns); + return buildDiagonal(array).resize(size, size); + } + + @Override + public Matrix build(int rows, int columns, Random random) { + return factory.createRandomMatrix(rows, columns, random); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, Random random) { + return factory.createRandomSymmetricMatrix(Math.min(rows, columns), random); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, Random random) { + + int size = Math.min(rows, columns); + double diagonal[] = new double[size]; + + for (int i = 0; i < size; i++) { + diagonal[i] = random.nextDouble(); + } + + return buildDiagonal(diagonal); + } + + @Override + public Matrix build(int rows, int columns, MatrixStream stream) { + return build(stream).resize(rows, columns); + } + + @Override + public Matrix buildSymmetric(int rows, int columns, MatrixStream stream) { + int size = Math.min(rows, columns); + return buildSymmetric(stream).resize(size, size); + } + + @Override + public Matrix buildDiagonal(int rows, int columns, MatrixStream stream) { + int size = Math.min(rows, columns); + return buildDiagonal(stream).resize(size, size); + } + + private Matrix streamedOrEmpty(MatrixStream stream) { + try { + return stream.readMatrix(factory); + } catch (IOException ignored) { + return factory.createMatrix(); + } + } + + private Matrix symmetrized(MatrixSource source) { + + Matrix result = factory.createMatrix(source.rows(), source.columns()); + + for (int i = 0; i < source.rows(); i++) { + result.set(i, i, source.get(i, i)); + for (int j = i + 1; j < source.columns(); j++) { + double value = source.get(i, j); + result.set(i, j, value); + result.set(j, i, value); + } + } + + return result; + } +} diff --git a/src/main/java/org/la4j/matrix/builder/UnderlyingMatrixBuilder.java b/src/main/java/org/la4j/matrix/builder/UnderlyingMatrixBuilder.java new file mode 100644 index 00000000..31198b73 --- /dev/null +++ b/src/main/java/org/la4j/matrix/builder/UnderlyingMatrixBuilder.java @@ -0,0 +1,196 @@ +/* + * Copyright 2011-2014, by Vladimir Kostyukov and Contributors. + * + * This file is part of la4j project (http://la4j.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributor(s): - + */ + +package org.la4j.matrix.builder; + +import org.la4j.LinearAlgebra; +import org.la4j.io.MatrixStream; +import org.la4j.matrix.Matrix; + +import java.util.Random; + +public abstract class UnderlyingMatrixBuilder implements MatrixBuilder { + + @Override + public Matrix buildSymmetric() { + return build(); + } + + @Override + public Matrix buildIdentity() { + return build(); + } + + @Override + public Matrix buildDiagonal() { + return build(); + } + + // shape + public abstract Matrix build(int rows, int columns); + public abstract Matrix buildIdentity(int rows, int columns); + + public Matrix buildSymmetric(int rows, int columns) { + return build(rows, columns); + } + + public Matrix buildDiagonal(int rows, int columns) { + return build(rows, columns); + } + + // value source (ignored w/o shape) + public Matrix build(double value) { + return build(); + } + + public Matrix buildSymmetric(double value) { + return build(); + } + + public Matrix buildIdentity(double value) { + return build(); + } + + public Matrix buildDiagonal(double value) { + return build(); + } + + // 1D array source + public abstract Matrix buildDiagonal(double array[]); + + public Matrix build(double array[]) { + return build(); + } + + public Matrix buildSymmetric(double array[]) { + return build(); + } + + public Matrix buildIdentity(double array[]) { + return build(); + } + + // 2D array source + public abstract Matrix build(double array[][]); + public abstract Matrix buildSymmetric(double array[][]); + public abstract Matrix buildDiagonal(double array[][]); + public abstract Matrix buildIdentity(double array[][]); + + // random source (ignored w/o shape) + public Matrix build(Random random) { + return build(); + } + + public Matrix buildSymmetric(Random random) { + return build(); + } + + public Matrix buildIdentity(Random random) { + return build(); + } + + public Matrix buildDiagonal(Random random) { + return build(); + } + + // stream source + public abstract Matrix build(MatrixStream stream); + public abstract Matrix buildSymmetric(MatrixStream stream); + public abstract Matrix buildIdentity(MatrixStream stream); + public abstract Matrix buildDiagonal(MatrixStream stream); + + // shape + value source + public abstract Matrix build(int rows, int columns, double value); + public abstract Matrix buildDiagonal(int rows, int columns, double value); + + public Matrix buildIdentity(int rows, int columns, double value) { + return buildIdentity(rows, columns); + } + + public Matrix buildSymmetric(int rows, int columns, double value) { + return build(rows, columns, value); + } + + // shape + 1D array source + public abstract Matrix build(int rows, int columns, double array[]); + public abstract Matrix buildSymmetric(int rows, int columns, double array[]); + public abstract Matrix buildDiagonal(int rows, int columns, double array[]); + + public Matrix buildIdentity(int rows, int columns, double array[]) { + return buildIdentity(rows, columns); + } + + // shape + 2D array source + public abstract Matrix build(int rows, int columns, double array[][]); + public abstract Matrix buildSymmetric(int rows, int columns, double array[][]); + public abstract Matrix buildDiagonal(int rows, int columns, double array[][]); + + public Matrix buildIdentity(int rows, int columns, double array[][]) { + return buildIdentity(rows, columns); + } + + // shape + random source + public abstract Matrix build(int rows, int columns, Random random); + public abstract Matrix buildSymmetric(int rows, int columns, Random random); + public abstract Matrix buildDiagonal(int rows, int columns, Random random); + + public Matrix buildIdentity(int rows, int columns, Random random) { + return buildIdentity(rows, columns); + } + + // shape + stream source + public abstract Matrix build(int rows, int columns, MatrixStream stream); + public abstract Matrix buildSymmetric(int rows, int columns, MatrixStream stream); + public abstract Matrix buildDiagonal(int rows, int columns, MatrixStream stream); + + public Matrix buildIdentity(int rows, int columns, MatrixStream stream) { + return buildIdentity(rows, columns); + } + + @Override + public MatrixBuilder shape(int rows, int columns) { + return new ShapedMatrixBuilder(this, rows, columns); + } + + @Override + public MatrixBuilder source(double value) { + return new ConstantSourcedMatrixBuilder(this, value); + } + + @Override + public MatrixBuilder source(double[][] array) { + return new Array2DSourcedMatrixBuilder(this, array); + } + + @Override + public MatrixBuilder source(double[] array) { + return new Array1DSourcedMatrixBuilder(this, array); + } + + @Override + public MatrixBuilder source(Random random) { + return new RandomSourcedMatrixBuilder(this, random); + } + + @Override + public MatrixBuilder source(MatrixStream stream) { + return new StreamSourcedMatrixBuilder(this, stream); + } +} diff --git a/src/main/java/org/la4j/matrix/dense/Basic2DMatrix.java b/src/main/java/org/la4j/matrix/dense/Basic2DMatrix.java index 087385b9..82887771 100644 --- a/src/main/java/org/la4j/matrix/dense/Basic2DMatrix.java +++ b/src/main/java/org/la4j/matrix/dense/Basic2DMatrix.java @@ -60,6 +60,18 @@ public Basic2DMatrix(int rows, int columns) { this(new double[rows][columns]); } + public Basic2DMatrix(int rows, int columns, double array[]) { + this(rows, columns); + + // TODO: + // We suppose that 'array.length = rows * columns' for now. + // Probably, we should check this explicitly. + + for (int i = 0; i < rows; i++) { + System.arraycopy(array, i * columns, self[i], 0, columns); + } + } + public Basic2DMatrix(double array[][]) { super(LinearAlgebra.BASIC2D_FACTORY, array.length, array.length == 0 ? 0: array[0].length); this.self = array; diff --git a/src/main/java/org/la4j/matrix/sparse/CCSMatrix.java b/src/main/java/org/la4j/matrix/sparse/CCSMatrix.java index 8a382d91..6ec37e6a 100644 --- a/src/main/java/org/la4j/matrix/sparse/CCSMatrix.java +++ b/src/main/java/org/la4j/matrix/sparse/CCSMatrix.java @@ -60,6 +60,10 @@ public CCSMatrix(int rows, int columns) { this (rows, columns, 0); } + public CCSMatrix(int rows, int columns, double array[]) { + this (Matrices.asArray1DSource(rows, columns, array)); + } + public CCSMatrix(Matrix matrix) { this(Matrices.asMatrixSource(matrix)); } diff --git a/src/main/java/org/la4j/matrix/sparse/CRSMatrix.java b/src/main/java/org/la4j/matrix/sparse/CRSMatrix.java index cae0b69a..1fd3f4dd 100644 --- a/src/main/java/org/la4j/matrix/sparse/CRSMatrix.java +++ b/src/main/java/org/la4j/matrix/sparse/CRSMatrix.java @@ -60,6 +60,10 @@ public CRSMatrix(int rows, int columns) { this(rows, columns, 0); } + public CRSMatrix(int rows, int columns, double array[]) { + this(Matrices.asArray1DSource(rows, columns, array)); + } + public CRSMatrix(Matrix matrix) { this(Matrices.asMatrixSource(matrix)); }