mikera.matrixx.impl.SparseRowMatrix 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import mikera.arrayz.INDArray;
import mikera.arrayz.ISparse;
import mikera.matrixx.AMatrix;
import mikera.matrixx.Matrixx;
import mikera.vectorz.AVector;
import mikera.vectorz.Vector;
import mikera.vectorz.Vectorz;
import mikera.vectorz.impl.IndexedElementVisitor;
import mikera.vectorz.impl.GrowableIndexedVector;
import mikera.vectorz.impl.SingleElementVector;
import mikera.vectorz.impl.SparseIndexedVector;
import mikera.vectorz.impl.ZeroVector;
import mikera.vectorz.util.ErrorMessages;
import mikera.vectorz.util.VectorzException;
/**
* Matrix stored as a collection of normally sparse row vectors.
*
* This format is especially efficient for:
* - innerProduct() with another matrix, especially one with efficient
* column access like SparseColumnMatrix
* - access via getRow() operation
* - transpose into SparseColumnMatrix
*
* @author Mike
*
*/
public class SparseRowMatrix extends ASparseRCMatrix implements ISparse, IFastRows {
private static final long serialVersionUID = 8646257152425415773L;
private static final long SPARSE_ELEMENT_THRESHOLD = 1000L;
private final AVector emptyRow;
protected SparseRowMatrix(int rowCount, int columnCount) {
this(new AVector[rowCount],rowCount,columnCount);
}
protected SparseRowMatrix(AVector[] data, int rowCount, int columnCount) {
super(rowCount,columnCount,data);
if (data.length != rowCount)
throw new IllegalArgumentException(ErrorMessages.incompatibleShapes(rowCount, data.length));
emptyRow=Vectorz.createZeroVector(columnCount);
}
protected SparseRowMatrix(AVector... vectors) {
this(vectors, vectors.length, vectors[0].length());
}
protected SparseRowMatrix(List data, int rowCount, int columnCount) {
this(data.toArray(new AVector[0]),rowCount,columnCount);
}
protected SparseRowMatrix(List data) {
this(data.toArray(new AVector[0]));
}
// protected SparseRowMatrix(HashMap data, int rowCount, int columnCount) {
// super(rowCount,columnCount,data);
// emptyColumn=Vectorz.createZeroVector(rowCount);
// }
public static SparseRowMatrix create(int rows, int cols) {
return new SparseRowMatrix(rows, cols);
}
public static SparseRowMatrix create(AVector[] data, int rows, int cols) {
return new SparseRowMatrix(data, rows, cols);
}
public static SparseRowMatrix create(AVector... vecs) {
return new SparseRowMatrix(vecs);
// don't validate; user can call validate() if they want it.
}
public static SparseRowMatrix create(List rows) {
return create(rows.toArray(new AVector[rows.size()]));
}
public static INDArray create(ArrayList slices, int rows, int cols) {
AVector[] vecs=new AVector[rows];
for (int i=0; i rows=source.getRows();
for (int i = 0; i < rc; i++) {
AVector row = rows.get(i);
if (!row.isZero()) {
data[i] = Vectorz.createSparse(row);
}
}
return SparseRowMatrix.wrap(data,rc,cc);
}
public static SparseRowMatrix wrap(List vecs) {
return create(vecs);
}
// public static SparseRowMatrix wrap(HashMap data, int rows, int cols) {
// return new SparseRowMatrix(data, rows, cols);
// }
@Override
public int componentCount() {
return rows;
}
@Override
public AVector getComponent(int k) {
AVector v=data[k];
if (v==null) return emptyRow;
return v;
}
@Override
protected int lineLength() {
return cols;
}
@Override
public double get(int i, int j) {
return getRow(i).get(j);
}
@Override
public void set(int i, int j, double value) {
checkIndex(i,j);
unsafeSet(i,j,value);
}
@Override
public double unsafeGet(int row, int column) {
return getRow(row).unsafeGet(column);
}
@Override
public void unsafeSet(int i, int j, double value) {
AVector v = unsafeGetVector(i);
if (v == null) {
if (value == 0.0)
return;
v = SingleElementVector.create(value, j, cols);
} else if (v.isFullyMutable()) {
v.set(j, value);
return;
} else {
v = v.sparseClone();
v.unsafeSet(j, value);
}
unsafeSetVec(i, v);
}
@Override
public void set(AMatrix a) {
checkSameShape(a);
List srows=a.getRows();
for (int i=0; i getColumns() {
// initial arraylist with nulls for each column
final ArrayList columns=new ArrayList();
for (int j=0; j getRows() {
ArrayList rowList = new ArrayList(rows);
for (int i = 0; i < rows; i++) {
AVector v = unsafeGetVector(i);
if (v == null) v=emptyRow;
rowList.add(v);
}
return rowList;
}
@Override
public AVector getRowView(int i) {
return ensureMutableRow(i);
}
@Override
public boolean isUpperTriangular() {
int rc=rowCount();
for (int i=1; i mrows=m.getRows();
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy