mikera.vectorz.impl.ASparseIndexedVector 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.vectorz.impl;
import java.util.Arrays;
import mikera.indexz.Index;
import mikera.matrixx.AMatrix;
import mikera.vectorz.AVector;
import mikera.vectorz.util.DoubleArrays;
import mikera.vectorz.util.IntArrays;
import mikera.vectorz.util.VectorzException;
/**
* Base class containing common implementations for sparse indexed vectors
* @author Mike
*
*/
public abstract class ASparseIndexedVector extends ASparseVector {
private static final long serialVersionUID = -8106136233328863653L;
public ASparseIndexedVector(int length) {
super(length);
}
abstract double[] internalData();
abstract Index internalIndex();
int[] internalIndexArray() {
return internalIndex().data;
}
@Override
public boolean includesIndex(int i) {
return internalIndex().indexPosition(i)>=0;
}
@Override
public boolean isZero() {
return DoubleArrays.isZero(internalData());
}
@Override
public boolean isRangeZero(int start, int length) {
int end=start+length;
Index index=internalIndex();
double[] data=internalData();
int si=index.seekPosition(start);
int di=index.seekPosition(end);
for (int i=si; i=0)&&(offset+length<=this.length));
double[] data=internalData();
Index index=internalIndex();
int start=index.seekPosition(offset);
int[] ixs=index.data;
int dataLength=data.length;
for (int j=start; j=length) return;
array[arrayOffset+di]+=data[j];
}
}
@Override
public void addToArray(double[] dest, int offset) {
double[] data=internalData();
int[] ixs=internalIndexArray();
int dataLength=data.length;
for (int i=0; i=(offset+length)) return;
array[di+aOffset]+=factor*data[i];
}
}
@Override
public void addProductToArray(double factor, int offset, AVector other,int otherOffset, double[] array, int arrayOffset, int length) {
if (other instanceof ADenseArrayVector) {
addProductToArray(factor,offset,(ADenseArrayVector)other,otherOffset,array,arrayOffset,length);
return;
}
assert(offset>=0);
assert(offset+length<=length());
double[] data=internalData();
Index index=internalIndex();
int[] ixs=index.data;
int dataLength=data.length;
for (int j=index.seekPosition(offset); j=length) return;
array[i+arrayOffset]+=factor*data[j]*other.get(i+otherOffset);
}
}
@Override
public void addProductToArray(double factor, int offset, ADenseArrayVector other,int otherOffset, double[] array, int arrayOffset, int length) {
assert(offset>=0);
assert(offset+length<=length());
double[] otherArray=other.getArray();
otherOffset+=other.getArrayOffset();
double[] data=internalData();
Index index=internalIndex();
int[] ixs=index.data;
int dataLength=data.length;
for (int j=index.seekPosition(offset); j=length) return;
array[i+arrayOffset]+=factor*data[j]*otherArray[i+otherOffset];
}
}
@Override
public boolean equalsArray(double[] ds, int offset) {
double[] data=internalData();
int[] ixs=internalIndexArray();
int n=data.length;
if (n==0) return DoubleArrays.isZero(ds, offset, length);
int di=0;
int i=0;
while (di=data.length) break;
}
}
return SparseIndexedVector.wrap(length, nixs, ndata);
}
@Override
public final void getElements(double[] array, int offset) {
Arrays.fill(array,offset,offset+length,0.0);
copySparseValuesTo(array,offset);
}
@Override public final void copyTo(AVector v, int offset) {
if (v instanceof ADenseArrayVector) {
ADenseArrayVector av=(ADenseArrayVector)v;
getElements(av.getArray(),av.getArrayOffset()+offset);
}
v.fillRange(offset,length,0.0);
double[] data=internalData();
int[] ixs=internalIndexArray();
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy