Search in sources :

Example 1 with DoubleMatrix3D

use of cern.colt.matrix.DoubleMatrix3D in project tdq-studio-se by Talend.

the class DenseDoubleMatrix3D method assign.

/**
 * Replaces all cell values of the receiver with the values of another matrix.
 * Both matrices must have the same number of slices, rows and columns.
 * If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>.
 *
 * @param     source   the source matrix to copy from (may be identical to the receiver).
 * @return <tt>this</tt> (for convenience only).
 * @throws	IllegalArgumentException if <tt>slices() != source.slices() || rows() != source.rows() || columns() != source.columns()</tt>
 */
public DoubleMatrix3D assign(DoubleMatrix3D source) {
    // overriden for performance only
    if (!(source instanceof DenseDoubleMatrix3D)) {
        return super.assign(source);
    }
    DenseDoubleMatrix3D other = (DenseDoubleMatrix3D) source;
    if (other == this)
        return this;
    checkShape(other);
    if (haveSharedCells(other)) {
        DoubleMatrix3D c = other.copy();
        if (!(c instanceof DenseDoubleMatrix3D)) {
            // should not happen
            return super.assign(source);
        }
        other = (DenseDoubleMatrix3D) c;
    }
    if (this.isNoView && other.isNoView) {
        // quickest
        System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
        return this;
    }
    return super.assign(other);
}
Also used : DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D)

Example 2 with DoubleMatrix3D

use of cern.colt.matrix.DoubleMatrix3D in project tdq-studio-se by Talend.

the class Sorting method zdemo2.

/**
 * Demonstrates advanced sorting.
 * Sorts by sum of slice.
 */
public static void zdemo2() {
    Sorting sort = quickSort;
    DoubleMatrix3D matrix = DoubleFactory3D.dense.descending(4, 3, 2);
    DoubleMatrix2DComparator comp = new DoubleMatrix2DComparator() {

        public int compare(DoubleMatrix2D a, DoubleMatrix2D b) {
            double as = a.zSum();
            double bs = b.zSum();
            return as < bs ? -1 : as == bs ? 0 : 1;
        }
    };
    System.out.println("unsorted:" + matrix);
    System.out.println("sorted  :" + sort.sort(matrix, comp));
}
Also used : DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 3 with DoubleMatrix3D

use of cern.colt.matrix.DoubleMatrix3D in project tdq-studio-se by Talend.

the class Stencil method stencil27.

/**
 *27 point stencil operation.
 *Applies a function to a moving <tt>3 x 3 x 3</tt> window.
 *@param A the matrix to operate on.
 *@param function the function to be applied to each window.
 *@param maxIterations the maximum number of times the stencil shall be applied to the matrix.
 *	Should be a multiple of 2 because two iterations are always done in one atomic step.
 *@param hasConverged Convergence condition; will return before maxIterations are done when <tt>hasConverged.apply(A)==true</tt>.
 *	Set this parameter to <tt>null</tt> to indicate that no convergence checks shall be made.
 *@param convergenceIterations the number of iterations to pass between each convergence check.
 *	(Since a convergence may be expensive, you may want to do it only every 2,4 or 8 iterations.)
 *@return the number of iterations actually executed.
 */
public static int stencil27(DoubleMatrix3D A, cern.colt.function.Double27Function function, int maxIterations, DoubleMatrix3DProcedure hasConverged, int convergenceIterations) {
    DoubleMatrix3D B = A.copy();
    if (convergenceIterations <= 1)
        convergenceIterations = 2;
    // odd -> make it even
    if (convergenceIterations % 2 != 0)
        convergenceIterations++;
    int i = 0;
    while (i < maxIterations) {
        // do two steps at a time for efficiency
        A.zAssign27Neighbors(B, function);
        B.zAssign27Neighbors(A, function);
        i = i + 2;
        if (i % convergenceIterations == 0 && hasConverged != null) {
            if (hasConverged.apply(A))
                return i;
        }
    }
    return i;
}
Also used : DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D)

Example 4 with DoubleMatrix3D

use of cern.colt.matrix.DoubleMatrix3D in project tdq-studio-se by Talend.

the class BenchmarkMatrix method run.

/**
 * Executes procedure repeatadly until more than minSeconds have elapsed.
 */
protected static void run(double minSeconds, String title, Double2DProcedure function, String[] types, int[] sizes, double[] densities) {
    // int[] sizes = {33,500,1000};
    // double[] densities = {0.001,0.01,0.99};
    // int[] sizes = {3,5,7,9,30,45,60,61,100,200,300,500,800,1000};
    // double[] densities = {0.001,0.01,0.1,0.999};
    // int[] sizes = {3};
    // double[] densities = {0.1};
    DoubleMatrix3D timings = DoubleFactory3D.dense.make(types.length, sizes.length, densities.length);
    cern.colt.Timer runTime = new cern.colt.Timer().start();
    for (int k = 0; k < types.length; k++) {
        // DoubleFactory2D factory = (k==0 ? DoubleFactory2D.dense : k==1 ? DoubleFactory2D.sparse : DoubleFactory2D.rowCompressed);
        // DoubleFactory2D factory = (k==0 ? DoubleFactory2D.dense : k==1 ? DoubleFactory2D.sparse : k==2 ? DoubleFactory2D.rowCompressed : DoubleFactory2D.rowCompressedModified);
        DoubleFactory2D factory = getFactory(types[k]);
        System.out.print("\n@");
        for (int i = 0; i < sizes.length; i++) {
            int size = sizes[i];
            System.out.print("x");
            for (int j = 0; j < densities.length; j++) {
                final double density = densities[j];
                System.out.print(".");
                // System.out.println("   doing density="+density+"...");
                float opsPerSec;
                // if (!((k==1 && density >= 0.1 && size >=100) || (size>5000 && (k==0 || density>1.0E-4) ))) {
                if (!((k > 0 && density >= 0.1 && size >= 500))) {
                    double val = 0.5;
                    // --> help gc before allocating new mem
                    function.A = null;
                    // --> help gc before allocating new mem
                    function.B = null;
                    // --> help gc before allocating new mem
                    function.C = null;
                    // --> help gc before allocating new mem
                    function.D = null;
                    DoubleMatrix2D A = factory.sample(size, size, val, density);
                    DoubleMatrix2D B = factory.sample(size, size, val, density);
                    function.setParameters(A, B);
                    // help gc
                    A = null;
                    // help gc
                    B = null;
                    double ops = function.operations();
                    double secs = BenchmarkKernel.run(minSeconds, function);
                    opsPerSec = (float) (ops / secs);
                } else {
                    // skip this parameter combination (not used in practice & would take a lot of memory and time)
                    opsPerSec = Float.NaN;
                }
                timings.set(k, i, j, opsPerSec);
            // System.out.println(secs);
            // System.out.println(opsPerSec+" Mops/sec\n");
            }
        }
    }
    runTime.stop();
    String sliceAxisName = "type";
    String rowAxisName = "size";
    // "density";
    String colAxisName = "d";
    // String[] sliceNames = {"dense", "sparse"};
    // String[] sliceNames = {"dense", "sparse", "rowCompressed"};
    String[] sliceNames = types;
    hep.aida.bin.BinFunctions1D F = hep.aida.bin.BinFunctions1D.functions;
    // {F.mean, F.median, F.sum};
    hep.aida.bin.BinFunction1D[] aggr = null;
    String[] rowNames = new String[sizes.length];
    String[] colNames = new String[densities.length];
    for (int i = sizes.length; --i >= 0; ) rowNames[i] = Integer.toString(sizes[i]);
    for (int j = densities.length; --j >= 0; ) colNames[j] = Double.toString(densities[j]);
    System.out.println("*");
    // show transposed
    String tmp = rowAxisName;
    rowAxisName = colAxisName;
    colAxisName = tmp;
    String[] tmp2 = rowNames;
    rowNames = colNames;
    colNames = tmp2;
    timings = timings.viewDice(0, 2, 1);
    System.out.println(new cern.colt.matrix.doublealgo.Formatter("%1.3G").toTitleString(timings, sliceNames, rowNames, colNames, sliceAxisName, rowAxisName, colAxisName, "Performance of " + title, aggr));
    /*
	title = "Speedup of dense over sparse";
	DoubleMatrix2D speedup = cern.colt.matrix.doublealgo.Transform.div(timings.viewSlice(0).copy(),timings.viewSlice(1));
	System.out.println("\n"+new cern.colt.matrix.doublealgo.Formatter("%1.3G").toTitleString(speedup,rowNames,colNames,rowAxisName,colAxisName,title,aggr));
	*/
    System.out.println("Run took a total of " + runTime + ". End of run.");
}
Also used : DoubleFactory2D(cern.colt.matrix.DoubleFactory2D) DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 5 with DoubleMatrix3D

use of cern.colt.matrix.DoubleMatrix3D in project tdq-studio-se by Talend.

the class TestMatrix2D method doubleTest13.

/**
 */
public static void doubleTest13() {
    double[] values = { 0, 1, 2, 3 };
    DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
    System.out.println(matrix);
    // Sum( x[i]*x[i] )
    System.out.println(matrix.viewSelection(new cern.colt.function.DoubleProcedure() {

        public final boolean apply(double a) {
            return a % 2 == 0;
        }
    }));
    // --> 14
    // Sum( x[i]*x[i] )
    System.out.println(matrix.aggregate(F.plus, F.square));
    // --> 14
    // Sum( x[i]*x[i]*x[i] )
    System.out.println(matrix.aggregate(F.plus, F.pow(3)));
    // --> 36
    // Sum( x[i] )
    System.out.println(matrix.aggregate(F.plus, F.identity));
    // --> 6
    // Min( x[i] )
    System.out.println(matrix.aggregate(F.min, F.identity));
    // --> 0
    // Max( Sqrt(x[i]) / 2 )
    System.out.println(matrix.aggregate(F.max, F.chain(F.div(2), F.sqrt)));
    // --> 0.8660254037844386
    // Number of all cells with 0 <= value <= 2
    System.out.println(matrix.aggregate(F.plus, F.between(0, 2)));
    // --> 3
    // Number of all cells with 0.8 <= Log2(value) <= 1.2
    System.out.println(matrix.aggregate(F.plus, F.chain(F.between(0.8, 1.2), F.log2)));
    // --> 1
    // Product( x[i] )
    System.out.println(matrix.aggregate(F.mult, F.identity));
    // --> 0
    // Product( x[i] ) of all x[i] > limit
    final double limit = 1;
    DoubleFunction f = new DoubleFunction() {

        public final double apply(double a) {
            return a > limit ? a : 1;
        }
    };
    System.out.println(matrix.aggregate(F.mult, f));
    // --> 6
    // Sum( (x[i]+y[i])^2 )
    DoubleMatrix1D otherMatrix1D = matrix.copy();
    System.out.println(matrix.aggregate(otherMatrix1D, F.plus, F.chain(F.square, F.plus)));
    // --> 56
    matrix.assign(F.plus(1));
    otherMatrix1D = matrix.copy();
    // otherMatrix1D.zMult(3);
    System.out.println(matrix);
    System.out.println(otherMatrix1D);
    // Sum(Math.PI * Math.log(otherMatrix1D[i] / matrix[i]))
    System.out.println(matrix.aggregate(otherMatrix1D, F.plus, F.chain(F.mult(Math.PI), F.chain(F.log, F.swapArgs(F.div)))));
    // or, perhaps less error prone and more readable:
    System.out.println(matrix.aggregate(otherMatrix1D, F.plus, new DoubleDoubleFunction() {

        public double apply(double a, double b) {
            return Math.PI * Math.log(b / a);
        }
    }));
    DoubleMatrix3D x = cern.colt.matrix.DoubleFactory3D.dense.ascending(2, 2, 2);
    System.out.println(x);
    // Sum( x[slice,row,col]*x[slice,row,col] )
    System.out.println(x.aggregate(F.plus, F.square));
    // --> 140
    DoubleMatrix3D y = x.copy();
    // Sum( (x[i]+y[i])^2 )
    System.out.println(x.aggregate(y, F.plus, F.chain(F.square, F.plus)));
    // --> 560
    System.out.println(matrix.assign(F.random()));
    System.out.println(matrix.assign(new cern.jet.random.Poisson(5, cern.jet.random.Poisson.makeDefaultGenerator())));
}
Also used : DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D) DoubleFunction(cern.colt.function.DoubleFunction) DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction) DoubleMatrix1D(cern.colt.matrix.DoubleMatrix1D) DoubleDoubleFunction(cern.colt.function.DoubleDoubleFunction)

Aggregations

DoubleMatrix3D (cern.colt.matrix.DoubleMatrix3D)5 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)2 DoubleDoubleFunction (cern.colt.function.DoubleDoubleFunction)1 DoubleFunction (cern.colt.function.DoubleFunction)1 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)1 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)1