Create sparse arrays using VectorZ
Code: "SparseArrays.java". Programming language: Java DMelt Version 2.29. Last modified: 11/25/2018. License: Pro
https://datamelt.org/code/cache/SparseArrays_6284.java
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


import mikera.matrixx.AMatrix;
import mikera.matrixx.impl.SparseRowMatrix;
import mikera.util.Random;
import mikera.vectorz.impl.SparseIndexedVector;

public class SparseArrays {
	public static final int SIZE=1000000;
	private static Random r=new Random();

	public static SparseIndexedVector createRow() {
		SparseIndexedVector v=SparseIndexedVector.createLength(SIZE);
		
		for (int i=0; i<1000; i++) {
			v.set(r.nextInt(SIZE), r.nextDouble());
		}

		return v;
	}
	
	public static SparseRowMatrix createMatrix() {
		SparseRowMatrix sm=SparseRowMatrix.create(SIZE,SIZE);
		
		for (int i=0; i<1000; i++) {
			sm.replaceRow(r.nextInt(SIZE), createRow());
		}
		return sm;
	}
	
	public static void main(String[] args) {
		SparseRowMatrix sm=createMatrix();
		System.out.println(sm.nonZeroCount() +" elements are non-zero out of " + sm.elementCount()+" total elements");
		
		AMatrix smm=sm.innerProduct(sm);
		System.out.println(smm.nonZeroCount() +" elements are non-zero in the product.");
	}

}


Ads help maintain this website.