131415161718192021
} @Test public void testIndexedDotProduct() { Vector v1=Vector.of(0,1,2,3,4,5,6,7,8,9); Vector v2=Vector.of(1,2,3); Index ix=Index.of (2,7,4); assertEquals((1*2)+(2*7)+(3*4),v1.dotProduct(v2,ix),0.0); }
298299300301302303304305306307308
Vectorz.fillGaussian(v); } int len=v.length(); int tlen=len+10; Index ix=Indexz.createRandomChoice(len, tlen); Vector tv=Vector.createLength(tlen); tv.addMultiple(v, ix, 2.0); assertEquals(v.elementSum()*2.0,tv.elementSum(),0.0001); }
314315316317318319320321322323324325326
v=v.exactClone(); int len=v.length(); Vector tv=Vector.createLength(len); Vectorz.fillGaussian(tv); Index ix=Indexz.createLength(len); for (int i=0; i<len; i++) { ix.set(i, Rand.r(len)); } v.addMultiple(tv, ix, 2.0); assertEquals(len+tv.elementSum()*2.0,v.elementSum(),0.0001); }
417418419420421422423424425426427428429430431432433434435436
if (n>0) { assertTrue(v.isRangeZero(0, nzi[0])); // check the leading part of the vector is all zero } Index ind=Index.of(nzi); assertEquals(n,nzi.length); for (int i=0; i<nzi.length; i++) { assertTrue(v.unsafeGet(nzi[i])!=0.0); } for (int i=0; i<len; i++) { if (v.unsafeGet(i)==0.0) { assertFalse(ind.containsSorted(i)); } else { assertTrue(ind.containsSorted(i)); } } }
67891011121314
import mikera.indexz.Index; public class TestIntArrays { @Test public void testCreate() { Index ix=Index.of(1,2,3,4); assertTrue(IntArrays.equals(ix.data, IntArrays.create(ix))); }
114115116117118119120121122123124
for (int i=0; i<SSIZE; i++) { double[] data=new double[DSIZE]; for (int j=0; j<DSIZE; j++) { data[j]=Rand.nextDouble(); } Index indy=Indexz.createRandomChoice(DSIZE, SSIZE); M.replaceRow(i,SparseIndexedVector.create(SSIZE, indy, data)); } Matrix D = Matrix.create(M); assertTrue(M.equals(D)); assertTrue(D.epsilonEquals(M, 0.1));
101102103104105106107108109110111
for (int i=0; i<SSIZE; i++) { double[] data=new double[DSIZE]; for (int j=0; j<DSIZE; j++) { data[j]=Rand.nextDouble(); } Index indy=Indexz.createRandomChoice(DSIZE, SSIZE); M.replaceColumn(i,SparseIndexedVector.create(SSIZE, indy, data)); } Matrix D = Matrix.create(M); assertTrue(M.equals(D)); assertTrue(D.epsilonEquals(M, 0.1));
22272228222922302231223222332234223522362237
* @return */ public Index nonSparseIndex(){ int n=(int)nonZeroCount(); int length=length(); Index ind=Index.createLength(n); int di=0; for (int i=0; i<length; i++) { double v=unsafeGet(i); if (v!=0.0) { ind.data[di++]=i;
4849505152535455
public static PermutationMatrix wrap(Index rowPermutations) { return new PermutationMatrix(rowPermutations); } public static PermutationMatrix create(int... rowPermutations) { Index index=Index.of(rowPermutations); return wrap(index); }
5859606162636465
public static PermutationMatrix wrap(int[] rowPermutations) { return wrap(Index.wrap(rowPermutations)); } public static PermutationMatrix createRandomPermutation(int length) { Index index=Indexz.createRandomPermutation(length); return new PermutationMatrix(index); }