mikera.matrixx.impl.AStridedMatrix 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.Iterator;
import mikera.arrayz.impl.IStridedArray;
import mikera.matrixx.AMatrix;
import mikera.matrixx.Matrix;
import mikera.matrixx.Matrixx;
import mikera.vectorz.AVector;
import mikera.vectorz.Op;
import mikera.vectorz.Op2;
import mikera.vectorz.Vectorz;
import mikera.vectorz.impl.AStridedVector;
import mikera.vectorz.util.ErrorMessages;
/**
* Abstract base class for arbitrary strided matrices
*
* @author Mike
*/
public abstract class AStridedMatrix extends AArrayMatrix implements IStridedArray {
private static final long serialVersionUID = -8908577438753599161L;
protected AStridedMatrix(double[] data, int rows, int cols) {
super(data, rows, cols);
}
@Override
public abstract int getArrayOffset();
/**
* Gets the row stride for this strided matrix. Each row has this offset in the underlying data array
* vs. the previous row. May be positive, zero or negative.
*/
public abstract int rowStride();
/**
* Gets the column stride for this strided matrix. Each column has this offset in the underlying data array
* vs. the previous column. May be positive, zero or negative.
*/
public abstract int columnStride();
@Override
public AStridedMatrix subMatrix(int rowStart, int rowCount, int colStart, int colCount) {
if ((rowStart<0)||(rowStart>=this.rows)||(colStart<0)||(colStart>=this.cols)) throw new IndexOutOfBoundsException(ErrorMessages.position(rowStart,colStart));
if ((rowStart+rowCount>this.rows)||(colStart+colCount>this.cols)) throw new IndexOutOfBoundsException(ErrorMessages.position(rowStart+rowCount,colStart+colCount));
int rowStride=rowStride();
int colStride=columnStride();
int offset=getArrayOffset();
return StridedMatrix.wrap(data, rowCount, colCount, offset+rowStart*rowStride+colStart*colStride, rowStride, colStride);
}
@Override
public AStridedVector getRow(int i) {
return Vectorz.wrapStrided(data, getArrayOffset()+i*rowStride(), cols, columnStride());
}
@Override
public AStridedVector getColumn(int i) {
return Vectorz.wrapStrided(data, getArrayOffset()+i*columnStride(), rows, rowStride());
}
@Override
public AStridedVector getRowView(int i) {
return getRow(i);
}
@Override
public final AStridedVector getColumnView(int i) {
return getColumn(i);
}
@Override
public double diagonalProduct() {
int n=Math.min(rowCount(), columnCount());
int offset=getArrayOffset();
int st=rowStride()+columnStride();
double[] data=getArray();
double result=1.0;
for (int i=0; icols)||(i<-rows)) throw new IndexOutOfBoundsException(ErrorMessages.invalidBand(this, i));
return Vectorz.wrapStrided(data, getArrayOffset()+bandStartColumn(i)*cs+bandStartRow(i)*rs, bandLength(i), rs+cs);
}
@Override
public void add(AVector v) {
checkColumnCount(v.length());
int offset=getArrayOffset();
int colStride=columnStride();
int rowStride=rowStride();
for (int i=0; i elementIterator() {
return new StridedElementIterator(this);
}
@Override
public AStridedMatrix getTranspose() {
return getTransposeView();
}
@Override
public AStridedMatrix getTransposeView() {
return Matrixx.wrapStrided(getArray(),columnCount(),rowCount(),getArrayOffset(),columnStride(),rowStride());
}
@Override
public boolean isPackedArray() {
return (getArrayOffset()==0)&&(columnStride()==1)&&(rowStride()==columnCount())&&(getArray().length==elementCount());
}
@Override
public double[] asDoubleArray() {
if (isPackedArray()) return getArray();
return null;
}
@Override
public boolean isZero() {
// select row or column iteration in the most cache-friendly manner
if (rowStride()>columnStride()) {
int rc=rowCount();
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy