mikera.matrixx.impl.UpperTriangularMatrix Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vectorz Show documentation
Show all versions of vectorz Show documentation
Fast double-precision vector and matrix maths library for Java, supporting N-dimensional numeric arrays.
package mikera.matrixx.impl;
import mikera.matrixx.AMatrix;
import mikera.vectorz.AVector;
import mikera.vectorz.Vectorz;
import mikera.vectorz.impl.ArraySubVector;
import mikera.vectorz.impl.IndexedArrayVector;
/**
* Class for an upper triangular matrix packed densely by columns.
*
* Mutable only in the upper triangular elements
*
* Mostly useful for space efficiency when storing triangular matrices, but is also optimised
* for certain common operations on triangular matrices.
*
* @author Mike
*
*/
public final class UpperTriangularMatrix extends ATriangularMatrix implements IFastColumns {
private static final long serialVersionUID = 4438118586237354484L;
private UpperTriangularMatrix(double[] data, int rows, int cols) {
super(data, rows, cols);
}
private UpperTriangularMatrix(int rows, int cols) {
this (new double[(cols*(cols+1))>>1],rows,cols);
}
static UpperTriangularMatrix wrap(double[] data, int rows, int cols) {
return new UpperTriangularMatrix(data,rows,cols);
}
public static UpperTriangularMatrix createFrom(AMatrix m) {
int rc=m.rowCount();
int cc=m.columnCount();
UpperTriangularMatrix r = new UpperTriangularMatrix(rc,cc);
for (int i=0; i>1);
}
@Override
public double get(int i, int j) {
checkIndex(i,j);
if (i>j) return 0.0;
return data[internalIndex(i,j)];
}
@Override
public double unsafeGet(int i, int j) {
if (i>j) return 0.0;
return data[internalIndex(i,j)];
}
@Override
public void unsafeSet(int i, int j, double value) {
data[internalIndex(i,j)]=value;
}
@Override
public AVector getColumnView(int j) {
int end=Math.min(j+1, rows);
return ArraySubVector.wrap(data, internalIndex(0,j), end).join(Vectorz.createZeroVector(rows-end));
}
@Override
public LowerTriangularMatrix getTranspose() {
return LowerTriangularMatrix.wrap(data, cols, rows);
}
@Override
public boolean equals(AMatrix a) {
if (a==this) return true;
if (!isSameShape(a)) return false;
if (a instanceof ADenseArrayMatrix) {
ADenseArrayMatrix da=(ADenseArrayMatrix)a;
return equalsArray(da.getArray(),da.getArrayOffset());
}
for (int j = 0; j < cols; j++) {
int end=Math.min(j,rows-1);
for (int i = 0; i <= end; i++) {
if (data[internalIndex(i, j)] != a.unsafeGet(i, j)) return false;
}
// TODO: factor out using isRangeZero on rows / cols of a?
for (int i = j+1; i < rows; i++) {
if (a.unsafeGet(i, j)!=0.0) return false;
}
}
return true;
}
@Override
public AMatrix exactClone() {
return new UpperTriangularMatrix(data.clone(),rows,cols);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy