cern.colt.matrix.impl.DenseDoubleMatrix1D Java Examples

The following examples show how to use cern.colt.matrix.impl.DenseDoubleMatrix1D. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Sorting.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Demonstrates applying functions.
 */
protected static void zdemo4() {
	double[] values1 = {0, 1, 2, 3};
	double[] values2 = {0, 2, 4, 6};
	DoubleMatrix1D matrix1 = new DenseDoubleMatrix1D(values1);
	DoubleMatrix1D matrix2 = new DenseDoubleMatrix1D(values2);
	System.out.println("m1:"+matrix1);
	System.out.println("m2:"+matrix2);
	
	matrix1.assign(matrix2, cern.jet.math.Functions.pow);
	
	/*
	matrix1.assign(matrix2,
		new cern.colt.function.DoubleDoubleFunction() {
			public double apply(double x, double y) { return Math.pow(x,y); }
		}
	);
	*/
	
	System.out.println("applied:"+matrix1);
}
 
Example #2
Source File: Sorting.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Demonstrates applying functions.
 */
protected static void zdemo4() {
	double[] values1 = {0, 1, 2, 3};
	double[] values2 = {0, 2, 4, 6};
	DoubleMatrix1D matrix1 = new DenseDoubleMatrix1D(values1);
	DoubleMatrix1D matrix2 = new DenseDoubleMatrix1D(values2);
	System.out.println("m1:"+matrix1);
	System.out.println("m2:"+matrix2);
	
	matrix1.assign(matrix2, cern.jet.math.Functions.pow);
	
	/*
	matrix1.assign(matrix2,
		new cern.colt.function.DoubleDoubleFunction() {
			public double apply(double x, double y) { return Math.pow(x,y); }
		}
	);
	*/
	
	System.out.println("applied:"+matrix1);
}
 
Example #3
Source File: BucketsDB.java    From first-stories-twitter with MIT License 6 votes vote down vote up
/**
     * Create random vector of given dimensions.  using norm1 vector.
     * @param dimensions
     * @return A normal unit vector
     */
    private DenseDoubleMatrix1D createRandomVector(int dims) {
        DenseDoubleMatrix1D randomVect = new DenseDoubleMatrix1D(dims);
//        Random r = new Random();
        double norm = 0, g;
        int i;
        for (i = 0; i < dims; i++) {
            g = r.nextGaussian();
            randomVect.setQuick(i, g);
            norm += Math.abs(g);
        }

        for (i = 0; i < dims; i++) {
            randomVect.setQuick(i, (randomVect.getQuick(i) / norm));
        }

        return randomVect;
    }
 
Example #4
Source File: Formatter.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Demonstrates how to use this class.
 */
public static void demo2() {
// parameters
double[] values = {
	//5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MIN_VALUE, Double.MAX_VALUE
	5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.MIN_VALUE, Double.MAX_VALUE , Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
	//Double.MIN_VALUE, Double.MAX_VALUE //, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
};
//String[] formats =         {"%G", "%1.10G", "%f", "%1.2f", "%0.2e"};
String[] formats =         {"%G", "%1.19G"};


// now the processing
int size = formats.length;
DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);

String[] strings = new String[size];
//String[] javaStrings = new String[size];

for (int i=0; i<size; i++) {
	String format = formats[i];
	strings[i] = new Formatter(format).toString(matrix);
	for (int j=0; j<matrix.size(); j++) {
		System.out.println(String.valueOf(matrix.get(j)));
	}
}

System.out.println("original:\n"+new Formatter().toString(matrix));

for (int i=0; i<size; i++) {
	System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
}

}
 
Example #5
Source File: Sorting.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Demonstrates advanced sorting.
 * Sorts by sinus of cell values.
 */
public static void zdemo3() {
	Sorting sort = quickSort;
	double[] values = {0.5, 1.5, 2.5, 3.5};
	DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
	cern.colt.function.DoubleComparator comp = new cern.colt.function.DoubleComparator() {
		public int compare(double a, double b) {
			double as = Math.sin(a); double bs = Math.sin(b);
			return as < bs ? -1 : as == bs ? 0 : 1;
		}
	};
	System.out.println("unsorted:"+matrix);
	
	DoubleMatrix1D sorted = sort.sort(matrix,comp);
	System.out.println("sorted  :"+sorted);

	// check whether it is really sorted
	sorted.assign(cern.jet.math.Functions.sin);
	/*
	sorted.assign(
		new cern.colt.function.DoubleFunction() {
			public double apply(double arg) { return Math.sin(arg); }
		}
	);
	*/
	System.out.println("sined  :"+sorted);
}
 
Example #6
Source File: Formatter.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Demonstrates how to use this class.
 */
public static void demo2() {
// parameters
double[] values = {
	//5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MIN_VALUE, Double.MAX_VALUE
	5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.MIN_VALUE, Double.MAX_VALUE , Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
	//Double.MIN_VALUE, Double.MAX_VALUE //, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
};
//String[] formats =         {"%G", "%1.10G", "%f", "%1.2f", "%0.2e"};
String[] formats =         {"%G", "%1.19G"};


// now the processing
int size = formats.length;
DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);

String[] strings = new String[size];
//String[] javaStrings = new String[size];

for (int i=0; i<size; i++) {
	String format = formats[i];
	strings[i] = new Formatter(format).toString(matrix);
	for (int j=0; j<matrix.size(); j++) {
		System.out.println(String.valueOf(matrix.get(j)));
	}
}

System.out.println("original:\n"+new Formatter().toString(matrix));

for (int i=0; i<size; i++) {
	System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
}

}
 
Example #7
Source File: Sorting.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Demonstrates advanced sorting.
 * Sorts by sinus of cell values.
 */
public static void zdemo3() {
	Sorting sort = quickSort;
	double[] values = {0.5, 1.5, 2.5, 3.5};
	DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
	cern.colt.function.DoubleComparator comp = new cern.colt.function.DoubleComparator() {
		public int compare(double a, double b) {
			double as = Math.sin(a); double bs = Math.sin(b);
			return as < bs ? -1 : as == bs ? 0 : 1;
		}
	};
	System.out.println("unsorted:"+matrix);
	
	DoubleMatrix1D sorted = sort.sort(matrix,comp);
	System.out.println("sorted  :"+sorted);

	// check whether it is really sorted
	sorted.assign(cern.jet.math.Functions.sin);
	/*
	sorted.assign(
		new cern.colt.function.DoubleFunction() {
			public double apply(double arg) { return Math.sin(arg); }
		}
	);
	*/
	System.out.println("sined  :"+sorted);
}
 
Example #8
Source File: BucketsDB.java    From first-stories-twitter with MIT License 5 votes vote down vote up
public BucketsDB(int partialL, int k, int queueSize){
	this.partialL = partialL;
	this.k = k;
	this.queueSize = queueSize;
	
	 r = new Random();
	//initialize Random Vectors
       bucketRandVectors = new ArrayList<DenseDoubleMatrix1D[]>();
       //in case not serializable
       int inputDims = 0;

       //compute the random vectors for each bucket
       for (int bckts = 0; bckts < partialL; bckts++) {
           DenseDoubleMatrix1D[] randVectList = new DenseDoubleMatrix1D[k];
           for (int dim = 0; dim < k; dim++) {
               randVectList[dim] = createRandomVector(inputDims);
           }
           bucketRandVectors.add(randVectList);
       }

       //initialize Buckets
       bucketList = new ArrayList<Bucket>(partialL);

       for (int bckt = 0; bckt < partialL; bckt++) {
           bucketList.add(new Bucket(queueSize, (int) Math.pow(2, k)));
       }
     
	
}
 
Example #9
Source File: BucketsDB.java    From first-stories-twitter with MIT License 5 votes vote down vote up
/**
 * Iterates over the bucket list and returns all near neighbours that share the same hash as
 * the input tweet. 
 * 
 * @param tw The input tweet
 * @return A list of colliding tweets out of all buckets.
 */
public ArrayList<Tweet> getPossibleNeighbors(Tweet tw){
	ArrayList<Tweet> possibleNeighbours = new ArrayList<Tweet>();
	int rBcktCounter = 0;
	
	for (Bucket bck : bucketList) {
           SparseVector sp = tw.getSparseVector();

           int smallHash = 0;
           for (int i = 0; i < k; i++) {

               DenseDoubleMatrix1D randomV = bucketRandVectors.get(rBcktCounter)[i];
               IntArrayList nonZeroIndeces = new IntArrayList(sp.cardinality());
               sp.getNonZeros(nonZeroIndeces, null);
               double dotProductValue = randomV.zDotProduct(sp, 0, sp.size(), nonZeroIndeces);
               if (dotProductValue >= 0) {
                   smallHash = smallHash | (1 << i);
               }

           }
           rBcktCounter++;
           //its partial possible neighbours because its per bucket colliding neighbors
               ArrayList<Tweet> partialPossibleNeighbours = findPossibleNeighbours(smallHash, bck);
               if (!partialPossibleNeighbours.isEmpty())
                   possibleNeighbours.addAll(partialPossibleNeighbours);

           //insert the tweet into the right bucket. no prob to insert it since the possible neighbours have
           //already been stored in possibleNeigh hashmap
           bck.insertIntoBucket(smallHash, tw);

       }//end of buckets
	
	return possibleNeighbours;
}
 
Example #10
Source File: BucketsDB.java    From first-stories-twitter with MIT License 5 votes vote down vote up
/**
 * Increase the size of the random vectors by a given number of dimensions
 * 
 * @param byNumberOfDims The number of dimensions to increase.
 */
public void updateRandomVectors(int byNumberOfDims) {

    if (byNumberOfDims <= 0) {
        return;
    }

   
    for (int bckt = 0; bckt < partialL; bckt++) {
        for (int i = 0; i < k; i++) {
            DenseDoubleMatrix1D oldVect = bucketRandVectors.get(bckt)[i];
            DenseDoubleMatrix1D biggerVect = new DenseDoubleMatrix1D(oldVect.size() + byNumberOfDims);

            //copy each value of old Vect to new bigger one
            int w;
            for (w = 0; w < oldVect.size(); w++) {
                biggerVect.set(w, oldVect.get(w));
            }
            //for the remaining dimensions put gaussian values
            //since w==oldVect.size
            for (int index = 0; index < byNumberOfDims; index++) {
                biggerVect.set(w + index, r.nextGaussian());
            }

            bucketRandVectors.get(bckt)[i] = biggerVect;
        }
    }
}
 
Example #11
Source File: BucketsDBTest.java    From first-stories-twitter with MIT License 5 votes vote down vote up
public void testCreateRandomVector() throws Exception{
	Method method = b.getClass().getDeclaredMethod("createRandomVector",int.class);
	method.setAccessible(true);
	
	DenseDoubleMatrix1D d = (DenseDoubleMatrix1D) method.invoke(b, 2);
	assertEquals(d.cardinality(), 2);
}
 
Example #12
Source File: DoubleFactory1D.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a matrix with the given shape, each cell initialized with zero.
 */
public DoubleMatrix1D make(int size) {
	if (this==sparse) return new SparseDoubleMatrix1D(size);
	return new DenseDoubleMatrix1D(size);
}
 
Example #13
Source File: DoubleFactory1D.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Constructs a matrix with the given shape, each cell initialized with zero.
 */
public DoubleMatrix1D make(int size) {
	if (this==sparse) return new SparseDoubleMatrix1D(size);
	return new DenseDoubleMatrix1D(size);
}
 
Example #14
Source File: BucketsDB.java    From first-stories-twitter with MIT License 4 votes vote down vote up
public ArrayList<DenseDoubleMatrix1D[]> getRandomVectors(){
	return bucketRandVectors;
}
 
Example #15
Source File: DoubleFactory1D.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Constructs a matrix with the given cell values.
 * The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa.
 *
 * @param values The values to be filled into the new matrix.
 */
public DoubleMatrix1D make(double[] values) {
	if (this==sparse) return new SparseDoubleMatrix1D(values);
	else return new DenseDoubleMatrix1D(values);
}
 
Example #16
Source File: DoubleFactory1D.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a matrix with the given cell values.
 * The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa.
 *
 * @param values The values to be filled into the new matrix.
 */
public DoubleMatrix1D make(double[] values) {
	if (this==sparse) return new SparseDoubleMatrix1D(values);
	else return new DenseDoubleMatrix1D(values);
}