/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.tfcomplex;
import cern.colt.matrix.tfcomplex.impl.DenseFComplexMatrix2D;
import cern.colt.matrix.tfcomplex.impl.SparseFComplexMatrix2D;
/**
* Factory for convenient construction of 2-d matrices holding complex
* cells. Also provides convenient methods to compose (concatenate) and
* decompose (split) matrices from/to constituent blocks.
*
*
*
*
*
* Construction
* Use idioms like ComplexFactory2D.dense.make(4,4) to construct
* dense matrices, ComplexFactory2D.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 {@link #appendColumns(FComplexMatrix2D,FComplexMatrix2D)
* appendColumns}, {@link #appendColumns(FComplexMatrix2D,FComplexMatrix2D)
* appendRows} and {@link #repeat(FComplexMatrix2D,int,int) repeat} to append
* rows and columns.
*
*
* General block matrices
* Use methods {@link #compose(FComplexMatrix2D[][]) compose} and
* {@link #decompose(FComplexMatrix2D[][],FComplexMatrix2D) decompose} to work
* with general block matrices.
*
*
* Diagonal matrices
* Use methods {@link #diagonal(FComplexMatrix1D) diagonal(vector)},
* {@link #diagonal(FComplexMatrix2D) diagonal(matrix)} and
* {@link #identity(int) identity} to work with diagonal matrices.
*
*
* Diagonal block matrices
* Use method
* {@link #composeDiagonal(FComplexMatrix2D,FComplexMatrix2D,FComplexMatrix2D)
* composeDiagonal} to work with diagonal block matrices.
*
*
* Random
* Use methods {@link #random(int,int) random} and
* {@link #sample(int,int,float[],float) sample} to construct random matrices.
*
*
*
*
*
*
* If the factory is used frequently it might be useful to streamline the
* notation. For example by aliasing:
*
*
*
*
*
* ComplexFactory2D F = ComplexFactory2D.dense;
* F.make(4,4);
* F.random(4,4);
* ...
*
*
*
*
*
* @author Piotr Wendykier (piotr.wendykier@gmail.com)
*/
public class FComplexFactory2D extends cern.colt.PersistentObject {
private static final long serialVersionUID = 1L;
/**
* A factory producing dense matrices.
*/
public static final FComplexFactory2D dense = new FComplexFactory2D();
/**
* A factory producing sparse hash matrices.
*/
public static final FComplexFactory2D sparse = new FComplexFactory2D();
/**
* Makes this class non instantiable, but still let's others inherit from
* it.
*/
protected FComplexFactory2D() {
}
/**
* 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
*
*
*/
public FComplexMatrix2D appendColumns(FComplexMatrix2D A, FComplexMatrix2D B) {
// force both to have maximal shared number of rows.
if (B.rows() > A.rows())
B = B.viewPart(0, 0, A.rows(), B.columns());
else if (B.rows() < A.rows())
A = A.viewPart(0, 0, B.rows(), A.columns());
// concatenate
int ac = A.columns();
int bc = B.columns();
int r = A.rows();
FComplexMatrix2D matrix = make(r, ac + bc);
matrix.viewPart(0, 0, r, ac).assign(A);
matrix.viewPart(0, ac, r, bc).assign(B);
return matrix;
}
/**
* C = A||b; Constructs a new matrix which is the column-wise concatenation
* of two other matrices.
*
*
* 0 1 2
* 3 4 5
* appendColumn
* 6
* 8
* -->
* 0 1 2 6
* 3 4 5 8
*
*
*/
public FComplexMatrix2D appendColumn(FComplexMatrix2D A, FComplexMatrix1D b) {
// force both to have maximal shared number of rows.
if (b.size() > A.rows())
b = b.viewPart(0, A.rows());
else if (b.size() < A.rows())
A = A.viewPart(0, 0, (int) b.size(), A.columns());
// concatenate
int ac = A.columns();
int bc = 1;
int r = A.rows();
FComplexMatrix2D matrix = make(r, ac + bc);
matrix.viewPart(0, 0, r, ac).assign(A);
matrix.viewColumn(ac).assign(b);
return matrix;
}
/**
* C = A||B; Constructs a new matrix which is the row-wise concatenation of
* two other matrices.
*
*/
public FComplexMatrix2D appendRows(FComplexMatrix2D A, FComplexMatrix2D B) {
// force both to have maximal shared number of columns.
if (B.columns() > A.columns())
B = B.viewPart(0, 0, B.rows(), A.columns());
else if (B.columns() < A.columns())
A = A.viewPart(0, 0, A.rows(), B.columns());
// concatenate
int ar = A.rows();
int br = B.rows();
int c = A.columns();
FComplexMatrix2D matrix = make(ar + br, c);
matrix.viewPart(0, 0, ar, c).assign(A);
matrix.viewPart(ar, 0, br, c).assign(B);
return matrix;
}
/**
* C = A||b; Constructs a new matrix which is the row-wise concatenation of
* two other matrices.
*
*/
public FComplexMatrix2D appendRow(FComplexMatrix2D A, FComplexMatrix1D b) {
// force both to have maximal shared number of columns.
if (b.size() > A.columns())
b = b.viewPart(0, A.columns());
else if (b.size() < A.columns())
A = A.viewPart(0, 0, A.rows(), (int) b.size());
// concatenate
int ar = A.rows();
int br = 1;
int c = A.columns();
FComplexMatrix2D matrix = make(ar + br, c);
matrix.viewPart(0, 0, ar, c).assign(A);
matrix.viewRow(ar).assign(b);
return matrix;
}
/**
* Checks whether the given array is rectangular, that is, whether all rows
* have the same number of columns.
*
* @throws IllegalArgumentException
* if the array is not rectangular.
*/
protected static void checkRectangularShape(float[][] array) {
int columns = -1;
for (int r = 0; r < array.length; r++) {
if (array[r] != null) {
if (columns == -1)
columns = array[r].length;
if (array[r].length != columns)
throw new IllegalArgumentException("All rows of array must have same number of columns.");
}
}
}
/**
* Checks whether the given array is rectangular, that is, whether all rows
* have the same number of columns.
*
* @throws IllegalArgumentException
* if the array is not rectangular.
*/
protected static void checkRectangularShape(FComplexMatrix2D[][] array) {
int columns = -1;
for (int r = 0; r < array.length; r++) {
if (array[r] != null) {
if (columns == -1)
columns = array[r].length;
if (array[r].length != columns)
throw new IllegalArgumentException("All rows of array must have same number of columns.");
}
}
}
/**
* Constructs a block matrix made from the given parts. The inverse to
* method {@link #decompose(FComplexMatrix2D[][], FComplexMatrix2D)}.
*
* 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.
*
* @throws IllegalArgumentException
* subject to the conditions outlined above.
*/
public FComplexMatrix2D compose(FComplexMatrix2D[][] parts) {
checkRectangularShape(parts);
int rows = parts.length;
int columns = 0;
if (parts.length > 0)
columns = parts[0].length;
FComplexMatrix2D empty = make(0, 0);
if (rows == 0 || columns == 0)
return empty;
// determine maximum column width of each column
int[] maxWidths = new int[columns];
for (int c = 0; c < columns; c++) {
int maxWidth = 0;
for (int r = 0; r < rows; r++) {
FComplexMatrix2D part = parts[r][c];
if (part != null) {
int width = part.columns();
if (maxWidth > 0 && width > 0 && width != maxWidth)
throw new IllegalArgumentException("Different number of columns.");
maxWidth = Math.max(maxWidth, width);
}
}
maxWidths[c] = maxWidth;
}
// determine row height of each row
int[] maxHeights = new int[rows];
for (int r = 0; r < rows; r++) {
int maxHeight = 0;
for (int c = 0; c < columns; c++) {
FComplexMatrix2D part = parts[r][c];
if (part != null) {
int height = part.rows();
if (maxHeight > 0 && height > 0 && height != maxHeight)
throw new IllegalArgumentException("Different number of rows.");
maxHeight = Math.max(maxHeight, height);
}
}
maxHeights[r] = maxHeight;
}
// shape of result
int resultRows = 0;
for (int r = 0; r < rows; r++)
resultRows += maxHeights[r];
int resultCols = 0;
for (int c = 0; c < columns; c++)
resultCols += maxWidths[c];
FComplexMatrix2D matrix = make(resultRows, resultCols);
// copy
int idxr = 0;
for (int r = 0; r < rows; r++) {
int idxc = 0;
for (int c = 0; c < columns; c++) {
FComplexMatrix2D part = parts[r][c];
if (part != null) {
matrix.viewPart(idxr, idxc, part.rows(), part.columns()).assign(part);
}
idxc += maxWidths[c];
}
idxr += maxHeights[r];
}
return matrix;
}
/**
* Constructs a diagonal block matrix from the given parts (the direct
* sum of two matrices). That is the concatenation
*
*
* A 0
* 0 B
*
*
*
* (The direct sum has A.rows()+B.rows() rows and
* A.columns()+B.columns() columns). Cells are copied.
*
* @return a new matrix which is the direct sum.
*/
public FComplexMatrix2D composeDiagonal(FComplexMatrix2D A, FComplexMatrix2D B) {
int ar = A.rows();
int ac = A.columns();
int br = B.rows();
int bc = B.columns();
FComplexMatrix2D sum = make(ar + br, ac + bc);
sum.viewPart(0, 0, ar, ac).assign(A);
sum.viewPart(ar, ac, br, bc).assign(B);
return sum;
}
/**
* Constructs a diagonal block matrix from the given parts. The
* concatenation has the form
*
*
* A 0 0
* 0 B 0
* 0 0 C
*
*
*
* from the given parts. Cells are copied.
*/
public FComplexMatrix2D composeDiagonal(FComplexMatrix2D A, FComplexMatrix2D B, FComplexMatrix2D C) {
FComplexMatrix2D diag = make(A.rows() + B.rows() + C.rows(), A.columns() + B.columns() + C.columns());
diag.viewPart(0, 0, A.rows(), A.columns()).assign(A);
diag.viewPart(A.rows(), A.columns(), B.rows(), B.columns()).assign(B);
diag.viewPart(A.rows() + B.rows(), A.columns() + B.columns(), C.rows(), C.columns()).assign(C);
return diag;
}
/**
* Constructs a bidiagonal block matrix from the given parts.
*
* from the given parts. Cells are copied.
*/
public FComplexMatrix2D composeBidiagonal(FComplexMatrix2D A, FComplexMatrix2D B) {
int ar = A.rows();
int ac = A.columns();
int br = B.rows();
int bc = B.columns();
FComplexMatrix2D sum = make(ar + br - 1, ac + bc);
sum.viewPart(0, 0, ar, ac).assign(A);
sum.viewPart(ar - 1, ac, br, bc).assign(B);
return sum;
}
/**
* Splits a block matrix into its constituent blocks; Copies blocks of a
* matrix into the given parts. The inverse to method
* {@link #compose(FComplexMatrix2D[][])}.
*
* 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.
*
* @throws IllegalArgumentException
* subject to the conditions outlined above.
*/
public void decompose(FComplexMatrix2D[][] parts, FComplexMatrix2D matrix) {
checkRectangularShape(parts);
int rows = parts.length;
int columns = 0;
if (parts.length > 0)
columns = parts[0].length;
if (rows == 0 || columns == 0)
return;
// determine maximum column width of each column
int[] maxWidths = new int[columns];
for (int c = 0; c < columns; c++) {
int maxWidth = 0;
for (int r = 0; r < rows; r++) {
FComplexMatrix2D part = parts[r][c];
if (part != null) {
int width = part.columns();
if (maxWidth > 0 && width > 0 && width != maxWidth)
throw new IllegalArgumentException("Different number of columns.");
maxWidth = Math.max(maxWidth, width);
}
}
maxWidths[c] = maxWidth;
}
// determine row height of each row
int[] maxHeights = new int[rows];
for (int r = 0; r < rows; r++) {
int maxHeight = 0;
for (int c = 0; c < columns; c++) {
FComplexMatrix2D part = parts[r][c];
if (part != null) {
int height = part.rows();
if (maxHeight > 0 && height > 0 && height != maxHeight)
throw new IllegalArgumentException("Different number of rows.");
maxHeight = Math.max(maxHeight, height);
}
}
maxHeights[r] = maxHeight;
}
// shape of result parts
int resultRows = 0;
for (int r = 0; r < rows; r++)
resultRows += maxHeights[r];
int resultCols = 0;
for (int c = 0; c < columns; c++)
resultCols += maxWidths[c];
if (matrix.rows() < resultRows || matrix.columns() < resultCols)
throw new IllegalArgumentException("Parts larger than matrix.");
// copy
int idxr = 0;
for (int r = 0; r < rows; r++) {
int idxc = 0;
for (int c = 0; c < columns; c++) {
FComplexMatrix2D part = parts[r][c];
if (part != null) {
part.assign(matrix.viewPart(idxr, idxc, part.rows(), part.columns()));
}
idxc += maxWidths[c];
}
idxr += maxHeights[r];
}
}
/**
* Demonstrates usage of this class.
*/
public void demo1() {
System.out.println("\n\n");
FComplexMatrix2D[][] parts1 = { { null, make(2, 2, new float[] { 1, 2 }), null },
{ make(4, 4, new float[] { 3, 4 }), null, make(4, 3, new float[] { 5, 6 }) },
{ null, make(2, 2, new float[] { 7, 8 }), null } };
System.out.println("\n" + compose(parts1));
}
/**
* Demonstrates usage of this class.
*/
public void demo2() {
System.out.println("\n\n");
FComplexMatrix2D matrix;
FComplexMatrix2D A, B, C, D;
FComplexMatrix2D _ = null;
A = make(2, 2, new float[] { 1, 2 });
B = make(4, 4, new float[] { 3, 4 });
C = make(4, 3, new float[] { 5, 6 });
D = make(2, 2, new float[] { 7, 8 });
FComplexMatrix2D[][] parts1 = { { _, A, _ }, { B, _, C }, { _, D, _ } };
matrix = compose(parts1);
System.out.println("\n" + matrix);
A.assign(9, 9);
B.assign(9, 9);
C.assign(9, 9);
D.assign(9, 9);
decompose(parts1, matrix);
System.out.println(A);
System.out.println(B);
System.out.println(C);
System.out.println(D);
}
/**
* Constructs a new diagonal matrix whose diagonal elements are the elements
* of vector. Cells values are copied. The new matrix is not a
* view.
*
* @return a new matrix.
*/
public FComplexMatrix2D diagonal(FComplexMatrix1D vector) {
int size = (int) vector.size();
FComplexMatrix2D diag = make(size, size);
for (int i = 0; i < size; i++) {
diag.setQuick(i, i, vector.getQuick(i));
}
return diag;
}
/**
* Constructs a new vector consisting of the diagonal elements of A
* . Cells values are copied. The new vector is not a view.
*
* @param A
* the matrix, need not be square.
* @return a new vector.
*/
public FComplexMatrix1D diagonal(FComplexMatrix2D A) {
int min = Math.min(A.rows(), A.columns());
FComplexMatrix1D diag = make1D(min);
for (int i = 0; i < min; i++) {
diag.setQuick(i, A.getQuick(i, i));
}
return diag;
}
/**
* Constructs an identity matrix (having ones on the diagonal and zeros
* elsewhere).
*/
public FComplexMatrix2D identity(int rowsAndColumns) {
FComplexMatrix2D matrix = make(rowsAndColumns, rowsAndColumns);
float[] one = new float[] { 1, 0 };
for (int i = rowsAndColumns; --i >= 0;) {
matrix.setQuick(i, i, one);
}
return matrix;
}
/**
* 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.
*
* @param values
* The values to be filled into the new matrix.
* @throws IllegalArgumentException
* if
* for any 1 <= row < values.length: values[row].length != values[row-1].length
* .
*/
public FComplexMatrix2D make(float[][] values) {
if (this == sparse) {
return new SparseFComplexMatrix2D(values);
} else {
return new DenseFComplexMatrix2D(values);
}
}
/**
* Constructs a matrix with the given shape, each cell initialized with
* zero.
*/
public FComplexMatrix2D make(int rows, int columns) {
if (this == sparse)
return new SparseFComplexMatrix2D(rows, columns);
else
return new DenseFComplexMatrix2D(rows, columns);
}
/**
* Constructs a matrix with the given shape, each cell initialized with the
* given value.
*/
public FComplexMatrix2D make(int rows, int columns, float[] initialValue) {
if (initialValue[0] == 0 && initialValue[1] == 0)
return make(rows, columns);
return make(rows, columns).assign(initialValue);
}
/**
* Constructs a 1d matrix of the right dynamic type.
*/
protected FComplexMatrix1D make1D(int size) {
return make(0, 0).like1D(size);
}
/**
* Constructs a matrix with uniformly distributed values in (0,1)
* (exclusive).
*/
public FComplexMatrix2D random(int rows, int columns) {
return make(rows, columns).assign(cern.jet.math.tfcomplex.FComplexFunctions.random());
}
/**
* C = A||A||..||A; Constructs a new matrix which is duplicated both along
* the row and column dimension.
*/
public FComplexMatrix2D repeat(FComplexMatrix2D A, int rowRepeat, int columnRepeat) {
int r = A.rows();
int c = A.columns();
FComplexMatrix2D matrix = make(r * rowRepeat, c * columnRepeat);
for (int i = 0; i < rowRepeat; i++) {
for (int j = 0; j < columnRepeat; j++) {
matrix.viewPart(r * i, c * j, r, c).assign(A);
}
}
return matrix;
}
/**
* 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 IllegalArgumentException
* if nonZeroFraction < 0 || nonZeroFraction > 1.
* @see cern.jet.random.tfloat.sampling.FloatRandomSampler
*/
public FComplexMatrix2D sample(int rows, int columns, float[] value, float nonZeroFraction) {
FComplexMatrix2D matrix = make(rows, columns);
sample(matrix, value, nonZeroFraction);
return matrix;
}
/**
* 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 IllegalArgumentException
* if nonZeroFraction < 0 || nonZeroFraction > 1.
* @see cern.jet.random.tfloat.sampling.FloatRandomSampler
*/
public FComplexMatrix2D sample(FComplexMatrix2D matrix, float[] value, float nonZeroFraction) {
int rows = matrix.rows();
int columns = matrix.columns();
float epsilon = 1e-05f;
if (nonZeroFraction < 0 - epsilon || nonZeroFraction > 1 + epsilon)
throw new IllegalArgumentException();
if (nonZeroFraction < 0)
nonZeroFraction = 0;
if (nonZeroFraction > 1)
nonZeroFraction = 1;
matrix.assign(0, 0);
int size = rows * columns;
int n = Math.round(size * nonZeroFraction);
if (n == 0)
return matrix;
cern.jet.random.tfloat.sampling.FloatRandomSamplingAssistant sampler = new cern.jet.random.tfloat.sampling.FloatRandomSamplingAssistant(
n, size, new cern.jet.random.tfloat.engine.FloatMersenneTwister());
for (int i = 0; i < size; i++) {
if (sampler.sampleNextElement()) {
int row = (i / columns);
int column = (i % columns);
matrix.set(row, column, value);
}
}
return matrix;
}
}
Ads help maintain this website.