Class LongFactory2D
- java.lang.Object
-
- cern.colt.PersistentObject
-
- cern.colt.matrix.tlong.LongFactory2D
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable
public class LongFactory2D extends PersistentObject
Factory for convenient construction of 2-d matrices holding int cells. Also provides convenient methods to compose (concatenate) and decompose (split) matrices from/to constituent blocks.Construction Use idioms like LongFactory2D.dense.make(4,4) to construct dense matrices, LongFactory2D.sparse.make(4,4) to construct sparse matrices. Construction with initial values Use other make methods to construct matrices with given initial values. Appending rows and columns Use methods appendColumns,appendRowsandrepeatto append rows and columns.General block matrices Use methods composeanddecomposeto work with general block matrices.Diagonal matrices Use methods diagonal(vector),diagonal(matrix)andidentityto work with diagonal matrices.Diagonal block matrices Use method composeDiagonalto work with diagonal block matrices.Random Use methods randomandsampleto construct random matrices.If the factory is used frequently it might be useful to streamline the notation. For example by aliasing:
LongFactory2D F = LongFactory2D.dense; F.make(4,4); F.descending(10,20); F.random(4,4); ...
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static LongFactory2DdenseA factory producing dense matrices.static LongFactory2DrowCompressedA factory producing sparse row compressed matrices.static LongFactory2DsparseA factory producing sparse hash matrices.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description LongMatrix2DappendColumn(LongMatrix2D A, LongMatrix1D b)LongMatrix2DappendColumns(LongMatrix2D A, LongMatrix2D B)C = A||B; Constructs a new matrix which is the column-wise concatenation of two other matrices.LongMatrix2DappendRow(LongMatrix2D A, LongMatrix1D b)LongMatrix2DappendRows(LongMatrix2D A, LongMatrix2D B)C = A||B; Constructs a new matrix which is the row-wise concatenation of two other matrices.LongMatrix2Dascending(int rows, int columns)Constructs a matrix with cells having ascending values.LongMatrix2Dcompose(LongMatrix2D[][] parts)Constructs a block matrix made from the given parts.LongMatrix2DcomposeBidiagonal(LongMatrix2D A, LongMatrix2D B)LongMatrix2DcomposeDiagonal(LongMatrix2D A, LongMatrix2D B)Constructs a diagonal block matrix from the given parts (the direct sum of two matrices).LongMatrix2DcomposeDiagonal(LongMatrix2D A, LongMatrix2D B, LongMatrix2D C)Constructs a diagonal block matrix from the given parts.voiddecompose(LongMatrix2D[][] parts, LongMatrix2D matrix)Splits a block matrix into its constituent blocks; Copies blocks of a matrix into the given parts.voiddemo1()Demonstrates usage of this class.voiddemo2()Demonstrates usage of this class.LongMatrix2Ddescending(int rows, int columns)Constructs a matrix with cells having descending values.LongMatrix2Ddiagonal(int[] vector)Constructs a new diagonal matrix whose diagonal elements are the elements of vector.LongMatrix2Ddiagonal(LongMatrix1D vector)Constructs a new diagonal matrix whose diagonal elements are the elements of vector.LongMatrix1Ddiagonal(LongMatrix2D A)Constructs a new vector consisting of the diagonal elements of A .LongMatrix2Didentity(int rowsAndColumns)Constructs an identity matrix (having ones on the diagonal and zeros elsewhere).LongMatrix2Dmake(int[] values, int rows)Construct a matrix from a one-dimensional column-major packed array, ala Fortran.LongMatrix2Dmake(int rows, int columns)Constructs a matrix with the given shape, each cell initialized with zero.LongMatrix2Dmake(int rows, int columns, long initialValue)Constructs a matrix with the given shape, each cell initialized with the given value.LongMatrix2Dmake(long[][] values)Constructs a matrix with the given cell values.LongMatrix2Drandom(int rows, int columns)Constructs a matrix with uniformly distributed values in (0,1) (exclusive).LongMatrix2Drepeat(LongMatrix2D A, int rowRepeat, int columnRepeat)C = A||A||..||A; Constructs a new matrix which is duplicated both along the row and column dimension.LongMatrix2Dreshape(LongMatrix1D a, int rows, int columns)LongMatrix2Dsample(int rows, int columns, int value, int nonZeroFraction)Constructs a randomly sampled matrix with the given shape.LongMatrix2Dsample(LongMatrix2D matrix, int value, int nonZeroFraction)Modifies the given matrix to be a randomly sampled matrix.-
Methods inherited from class cern.colt.PersistentObject
clone
-
-
-
-
Field Detail
-
dense
public static final LongFactory2D dense
A factory producing dense matrices.
-
sparse
public static final LongFactory2D sparse
A factory producing sparse hash matrices.
-
rowCompressed
public static final LongFactory2D rowCompressed
A factory producing sparse row compressed matrices.
-
-
Method Detail
-
appendColumns
public LongMatrix2D appendColumns(LongMatrix2D A, LongMatrix2D B)
C = A||B; Constructs a new matrix which is the column-wise concatenation of two other matrices.0 1 2 3 4 5 appendColumns 6 7 8 9 --> 0 1 2 6 7 3 4 5 8 9
-
appendColumn
public LongMatrix2D appendColumn(LongMatrix2D A, LongMatrix1D b)
-
appendRows
public LongMatrix2D appendRows(LongMatrix2D A, LongMatrix2D B)
C = A||B; Constructs a new matrix which is the row-wise concatenation of two other matrices.0 1 2 3 4 5 appendRows 6 7 8 9 --> 0 1 2 3 4 5 6 7 8 9
-
appendRow
public LongMatrix2D appendRow(LongMatrix2D A, LongMatrix1D b)
-
ascending
public LongMatrix2D ascending(int rows, int columns)
Constructs a matrix with cells having ascending values. For debugging purposes. Example:0 1 2 3 4 5
-
reshape
public LongMatrix2D reshape(LongMatrix1D a, int rows, int columns)
-
compose
public LongMatrix2D compose(LongMatrix2D[][] parts)
Constructs a block matrix made from the given parts. The inverse to methoddecompose(LongMatrix2D[][], LongMatrix2D).All matrices of a given column within parts must have the same number of columns. All matrices of a given row within parts must have the same number of rows. Otherwise an IllegalArgumentException is thrown. Note that nulls within parts[row,col] are an exception to this rule: they are ignored. Cells are copied. Example:
Code Result LongMatrix2D[][] parts1 = { { null, make(2, 2, 1), null }, { make(4, 4, 2), null, make(4, 3, 3) }, { null, make(2, 2, 4), null } }; System.out.println(compose(parts1));8 x 9 matrix
0 0 0 0 1 1 0 0 0
0 0 0 0 1 1 0 0 0
2 2 2 2 0 0 3 3 3
2 2 2 2 0 0 3 3 3
2 2 2 2 0 0 3 3 3
2 2 2 2 0 0 3 3 3
0 0 0 0 4 4 0 0 0
0 0 0 0 4 4 0 0 0LongMatrix2D[][] parts3 = { { identity(3), null, }, { null, identity(3).viewColumnFlip() }, { identity(3).viewRowFlip(), null } }; System.out.println("\n" + make(parts3));9 x 6 matrix
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 0
1 0 0 0 0 0LongMatrix2D A = ascending(2, 2); LongMatrix2D B = descending(2, 2); LongMatrix2D _ = null; LongMatrix2D[][] parts4 = { { A, _, A, _ }, { _, A, _, B } }; System.out.println("\n" + make(parts4));4 x 8 matrix
1 2 0 0 1 2 0 0
3 4 0 0 3 4 0 0
0 0 1 2 0 0 3 2
0 0 3 4 0 0 1 0LongMatrix2D[][] parts2 = { { null, make(2, 2, 1), null }, { make(4, 4, 2), null, make(4, 3, 3) }, { null, make(2, 3, 4), null } }; System.out.println("\n" + Factory2D.make(parts2));IllegalArgumentException
A[0,1].columns != A[2,1].columns
(2 != 3)- Throws:
java.lang.IllegalArgumentException- subject to the conditions outlined above.
-
composeDiagonal
public LongMatrix2D composeDiagonal(LongMatrix2D A, LongMatrix2D B)
Constructs a diagonal block matrix from the given parts (the direct sum of two matrices). That is the concatenationA 0 0 B
(The direct sum has A.rows()+B.rows() rows and A.columns()+B.columns() columns). Cells are copied.- Returns:
- a new matrix which is the direct sum.
-
composeDiagonal
public LongMatrix2D composeDiagonal(LongMatrix2D A, LongMatrix2D B, LongMatrix2D C)
Constructs a diagonal block matrix from the given parts. The concatenation has the formA 0 0 0 B 0 0 0 C
from the given parts. Cells are copied.
-
composeBidiagonal
public LongMatrix2D composeBidiagonal(LongMatrix2D A, LongMatrix2D B)
-
decompose
public void decompose(LongMatrix2D[][] parts, LongMatrix2D matrix)
Splits a block matrix into its constituent blocks; Copies blocks of a matrix into the given parts. The inverse to methodcompose(LongMatrix2D[][]).All matrices of a given column within parts must have the same number of columns. All matrices of a given row within parts must have the same number of rows. Otherwise an IllegalArgumentException is thrown. Note that nulls within parts[row,col] are an exception to this rule: they are ignored. Cells are copied. Example:
Code matrix --> parts LongMatrix2D matrix = ... ; LongMatrix2D _ = null; LongMatrix2D A,B,C,D; A = make(2,2); B = make (4,4); C = make(4,3); D = make (2,2); LongMatrix2D[][] parts = { { _, A, _ }, { B, _, C }, { _, D, _ } }; decompose(parts,matrix); System.out.println("\nA = "+A); System.out.println("\nB = "+B); System.out.println("\nC = "+C); System.out.println("\nD = "+D);8 x 9 matrix
9 9 9 9 1 1 9 9 9
9 9 9 9 1 1 9 9 9
2 2 2 2 9 9 3 3 3
2 2 2 2 9 9 3 3 3
2 2 2 2 9 9 3 3 3
2 2 2 2 9 9 3 3 3
9 9 9 9 4 4 9 9 9
9 9 9 9 4 4 9 9 9A = 2 x 2 matrix
1 1
1 1B = 4 x 4 matrix
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2C = 4 x 3 matrix
3 3 3
3 3 3
3 3 3
3 3 3D = 2 x 2 matrix
4 4
4 4- Throws:
java.lang.IllegalArgumentException- subject to the conditions outlined above.
-
demo1
public void demo1()
Demonstrates usage of this class.
-
demo2
public void demo2()
Demonstrates usage of this class.
-
descending
public LongMatrix2D descending(int rows, int columns)
Constructs a matrix with cells having descending values. For debugging purposes. Example:5 4 3 2 1 0
-
diagonal
public LongMatrix2D diagonal(LongMatrix1D vector)
Constructs a new diagonal matrix whose diagonal elements are the elements of vector. Cells values are copied. The new matrix is not a view. Example:5 4 3 --> 5 0 0 0 4 0 0 0 3
- Returns:
- a new matrix.
-
diagonal
public LongMatrix2D diagonal(int[] vector)
Constructs a new diagonal matrix whose diagonal elements are the elements of vector. Cells values are copied. The new matrix is not a view. Example:5 4 3 --> 5 0 0 0 4 0 0 0 3
- Returns:
- a new matrix.
-
diagonal
public LongMatrix1D diagonal(LongMatrix2D A)
Constructs a new vector consisting of the diagonal elements of A . Cells values are copied. The new vector is not a view. Example:5 0 0 9 0 4 0 9 0 0 3 9 --> 5 4 3
- Parameters:
A- the matrix, need not be square.- Returns:
- a new vector.
-
identity
public LongMatrix2D identity(int rowsAndColumns)
Constructs an identity matrix (having ones on the diagonal and zeros elsewhere).
-
make
public LongMatrix2D make(long[][] values)
Constructs a matrix with the given cell values. values is required to have the form values[row][column] and have exactly the same number of columns in every row.The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa.
- Parameters:
values- The values to be filled into the new matrix.- Throws:
java.lang.IllegalArgumentException- if for any 1 <= row < values.length: values[row].length != values[row-1].length .
-
make
public LongMatrix2D make(int[] values, int rows)
Construct a matrix from a one-dimensional column-major packed array, ala Fortran. Has the form matrix.get(row,column) == values[row + column*rows]. The values are copied.- Parameters:
values- One-dimensional array of doubles, packed by columns (ala Fortran).rows- the number of rows.- Throws:
java.lang.IllegalArgumentException- values.length must be a multiple of rows .
-
make
public LongMatrix2D make(int rows, int columns)
Constructs a matrix with the given shape, each cell initialized with zero.
-
make
public LongMatrix2D make(int rows, int columns, long initialValue)
Constructs a matrix with the given shape, each cell initialized with the given value.
-
random
public LongMatrix2D random(int rows, int columns)
Constructs a matrix with uniformly distributed values in (0,1) (exclusive).
-
repeat
public LongMatrix2D repeat(LongMatrix2D A, int rowRepeat, int columnRepeat)
C = A||A||..||A; Constructs a new matrix which is duplicated both along the row and column dimension. Example:0 1 2 3 repeat(2,3) --> 0 1 0 1 0 1 2 3 2 3 2 3 0 1 0 1 0 1 2 3 2 3 2 3
-
sample
public LongMatrix2D sample(int rows, int columns, int value, int nonZeroFraction)
Constructs a randomly sampled matrix with the given shape. Randomly picks exactly Math.round(rows*columns*nonZeroFraction) cells and initializes them to value, all the rest will be initialized to zero. Note that this is not the same as setting each cell with probability nonZeroFraction to value. Note: The random seed is a constant.- Throws:
java.lang.IllegalArgumentException- if nonZeroFraction < 0 || nonZeroFraction > 1.- See Also:
DoubleRandomSamplingAssistant
-
sample
public LongMatrix2D sample(LongMatrix2D matrix, int value, int nonZeroFraction)
Modifies the given matrix to be a randomly sampled matrix. Randomly picks exactly Math.round(rows*columns*nonZeroFraction) cells and initializes them to value, all the rest will be initialized to zero. Note that this is not the same as setting each cell with probability nonZeroFraction to value. Note: The random seed is a constant.- Throws:
java.lang.IllegalArgumentException- if nonZeroFraction < 0 || nonZeroFraction > 1.- See Also:
DoubleRandomSamplingAssistant
-
-
DMelt 3.0 © DataMelt by jWork.ORG