You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by gs...@apache.org on 2009/11/23 16:14:38 UTC
svn commit: r883365 [42/47] - in /lucene/mahout/trunk: ./ examples/ matrix/
matrix/src/ matrix/src/main/ matrix/src/main/java/
matrix/src/main/java/org/ matrix/src/main/java/org/apache/
matrix/src/main/java/org/apache/mahout/ matrix/src/main/java/org/a...
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TestMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TestMatrix2D.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TestMatrix2D.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TestMatrix2D.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,1784 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
+is hereby granted without fee, provided that the above copyright notice appear in all copies and
+that both that copyright notice and this permission notice appear in supporting documentation.
+CERN makes no representations about the suitability of this software for any purpose.
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.colt.matrix.impl;
+
+import org.apache.mahout.colt.function.DoubleDoubleFunction;
+import org.apache.mahout.colt.function.DoubleFunction;
+import org.apache.mahout.colt.list.IntArrayList;
+import org.apache.mahout.colt.map.AbstractIntDoubleMap;
+import org.apache.mahout.colt.map.OpenIntDoubleHashMap;
+import org.apache.mahout.colt.matrix.DoubleFactory2D;
+import org.apache.mahout.colt.matrix.DoubleMatrix1D;
+import org.apache.mahout.colt.matrix.DoubleMatrix2D;
+import org.apache.mahout.colt.matrix.DoubleMatrix3D;
+import org.apache.mahout.colt.matrix.doublealgo.DoubleMatrix2DComparator;
+import org.apache.mahout.colt.matrix.linalg.Algebra;
+import org.apache.mahout.colt.matrix.linalg.LUDecompositionQuick;
+import org.apache.mahout.colt.matrix.linalg.SeqBlas;
+/**
+ * Quick and dirty tests.
+ *
+ * @author wolfgang.hoschek@cern.ch
+ * @version 1.0, 09/24/99
+ */
+class TestMatrix2D {
+ private static final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
+ private static final org.apache.mahout.colt.matrix.DoubleFactory2D Factory2D = org.apache.mahout.colt.matrix.DoubleFactory2D.dense;
+ private static final org.apache.mahout.colt.matrix.DoubleFactory1D Factory1D = org.apache.mahout.colt.matrix.DoubleFactory1D.dense;
+ private static final org.apache.mahout.colt.matrix.linalg.Algebra LinearAlgebra = org.apache.mahout.colt.matrix.linalg.Algebra.DEFAULT;
+ private static final org.apache.mahout.colt.matrix.doublealgo.Transform Transform = org.apache.mahout.colt.matrix.doublealgo.Transform.transform;
+ private static final org.apache.mahout.colt.matrix.linalg.Property Property = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT;
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected TestMatrix2D() {
+ throw new RuntimeException("Non instantiable");
+}
+/**
+ */
+public static void doubleTest() {
+int rows = 4;
+int columns = 5; // make a 4*5 matrix
+DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
+System.out.println(master);
+master.assign(1); // set all cells to 1
+System.out.println("\n"+master);
+master.viewPart(2,1,2,3).assign(2); // set [2,1] .. [3,3] to 2
+System.out.println("\n"+master);
+
+DoubleMatrix2D copyPart = master.viewPart(2,1,2,3).copy();
+copyPart.assign(3); // modify an independent copy
+copyPart.set(0,0,4);
+System.out.println("\n"+copyPart); // has changed
+System.out.println("\n"+master); // master has not changed
+
+DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
+DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
+System.out.println("\n"+view1);
+System.out.println("\n"+view2);
+}
+/**
+ */
+public static void doubleTest(int rows, int columns, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
+ DoubleMatrix2D matrix = new SparseDoubleMatrix2D(rows,columns,initialCapacity,minLoadFactor,maxLoadFactor);
+ System.out.println(matrix);
+
+ System.out.println("adding...");
+ int i=0;
+ for (int column=0; column < columns; column++) {
+ for (int row=0; row < rows; row++) {
+ //if (i%1000 == 0) {
+ matrix.set(row,column,i);
+ //}
+ i++;
+ }
+ }
+ System.out.println(matrix);
+
+ System.out.println("removing...");
+ for (int column=0; column < columns; column++) {
+ for (int row=0; row < rows; row++) {
+ //if (i%1000 == 0) {
+ matrix.set(row,column,0);
+ //}
+ }
+ }
+
+ System.out.println(matrix);
+ System.out.println("bye bye.");
+}
+/**
+ */
+public static void doubleTest10() {
+int rows = 6;
+int columns = 7; // make a 4*5 matrix
+//DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
+DoubleMatrix2D master = Factory2D.ascending(rows,columns);
+//Basic.ascending(master);
+//master.assign(1); // set all cells to 1
+Transform.mult(master,Math.sin(0.3));
+System.out.println("\n"+master);
+//master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
+//System.out.println("\n"+master);
+
+int[] rowIndexes = {0,1,2,3};
+int[] columnIndexes = {0,1,2,3};
+
+int[] rowIndexes2 = {3,0,3};
+int[] columnIndexes2 = {3,0,3};
+DoubleMatrix2D view1 = master.viewPart(1,1,4,5).viewSelection(rowIndexes,columnIndexes);
+System.out.println("\nview1="+view1);
+DoubleMatrix2D view9 = view1.viewStrides(2,2).viewStrides(2,1);
+System.out.println("\nview9="+view9);
+view1 = view1.viewSelection(rowIndexes2,columnIndexes2);
+System.out.println("\nview1="+view1);
+DoubleMatrix2D view2 = view1.viewPart(1,1,2,2);
+System.out.println("\nview2="+view2);
+DoubleMatrix2D view3 = view2.viewRowFlip();
+System.out.println("\nview3="+view3);
+view3.assign(Factory2D.ascending(view3.rows(),view3.columns()));
+//Basic.ascending(view3);
+System.out.println("\nview3="+view3);
+
+//view2.assign(-1);
+System.out.println("\nmaster replaced"+master);
+System.out.println("\nview1 replaced"+view1);
+System.out.println("\nview2 replaced"+view2);
+System.out.println("\nview3 replaced"+view3);
+
+}
+/**
+ */
+public static void doubleTest11() {
+int rows = 4;
+int columns = 5; // make a 1*1 matrix
+DoubleMatrix2D master = new DenseDoubleMatrix2D(1,1);
+master.assign(2);
+System.out.println("\n"+master);
+
+int[] rowIndexes = new int[rows];
+int[] columnIndexes = new int[columns];
+
+DoubleMatrix2D view1 = master.viewSelection(rowIndexes,columnIndexes);
+System.out.println(view1);
+
+master.assign(1);
+System.out.println("\n"+master);
+System.out.println(view1);
+}
+/**
+ */
+public static void doubleTest12() {
+DoubleMatrix2D A,B,C,D,E,F,G,H,I,J;
+A = Factory2D.make(2,3,9);
+B = Factory2D.make(4,3,8);
+C = Factory2D.appendRows(A,B);
+System.out.println("\nA="+A);
+System.out.println("\nB="+B);
+System.out.println("\nC="+C);
+D = Factory2D.make(3,2,7);
+E = Factory2D.make(3,4,6);
+F = Factory2D.appendColumns(D,E);
+System.out.println("\nD="+D);
+System.out.println("\nE="+E);
+System.out.println("\nF="+F);
+G = Factory2D.appendRows(C,F);
+System.out.println("\nG="+G);
+H = Factory2D.ascending(2,3);
+System.out.println("\nH="+H);
+I = Factory2D.repeat(H,2,3);
+System.out.println("\nI="+I);
+}
+/**
+ */
+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 org.apache.mahout.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 = org.apache.mahout.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 org.apache.mahout.jet.random.Poisson(5, org.apache.mahout.jet.random.Poisson.makeDefaultGenerator())));
+
+}
+/**
+ */
+public static void doubleTest14(int r1, int c, int r2) {
+double[] values = {0, 1, 2, 3};
+DoubleMatrix2D a = DoubleFactory2D.dense.ascending(r1,c);
+DoubleMatrix2D b = Transform.mult(DoubleFactory2D.dense.ascending(c,r2), -1);
+
+
+//System.out.println(a);
+//System.out.println(b);
+//System.out.println(Basic.product(a,b));
+a.assign(0);
+b.assign(0);
+
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+LinearAlgebra.mult(a,b);
+timer.stop().display();
+}
+/**
+ */
+public static void doubleTest15(int size, int runs) {
+System.out.println("\n\n");
+double[][] values =
+{
+ { 0, 5, 9 },
+ { 2, 6, 10 },
+ { 3, 7, 11 }
+};
+
+//DoubleMatrix2D A = Factory2D.make(values);
+DoubleMatrix2D A = Factory2D.make(size,size);
+double value = 5;
+for (int i=size; --i >= 0; ) {
+ A.setQuick(i,i, value);
+}
+A.viewRow(0).assign(value);
+
+//DoubleMatrix2D A = Factory2D.makeIdentity(size,size);
+
+
+//DoubleMatrix2D A = Factory2D.makeAscending(size,size).assign(new org.apache.mahout.jet.random.engine.MersenneTwister());
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+DoubleMatrix2D inv = null;
+for (int run=0; run<runs; run++) {
+ inv = LinearAlgebra.inverse(A);
+}
+timer.stop().display();
+
+/*
+timer.reset().start();
+for (int run=0; run<runs; run++) {
+ new Jama.Matrix(A.toArray()).inverse();
+}
+timer.stop().display();
+*/
+//System.out.println("A="+A);
+//System.out.println("inverse(A)="+inv);
+//System.out.println("formatted inverse(A)="+ new Jama.Matrix(inv.toArray()));
+
+/*
+-1.0000000000000018, 2.000000000000007, -1.0000000000000047
+2.000000000000007, -6.750000000000024, 4.500000000000016
+-1.000000000000004, 3.7500000000000133, -2.500000000000009
+*/
+}
+/**
+ */
+public static void doubleTest17(int size) {
+System.out.println("\n\n");
+
+//DoubleMatrix2D A = Factory2D.make(values);
+DoubleMatrix2D A = Factory2D.ascending(3,4);
+DoubleMatrix2D B = Factory2D.ascending(2,3);
+DoubleMatrix2D C = Factory2D.ascending(1,2);
+B.assign(F.plus(A.zSum()));
+C.assign(F.plus(B.zSum()));
+
+
+/*
+System.out.println("\n"+A);
+System.out.println("\n"+B);
+System.out.println("\n"+C);
+System.out.println("\n"+Factory2D.diag(A,B,C));
+*/
+
+//System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+//System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(B.toString()));
+//System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(C.toString()));
+//System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(Factory2D.diagonal(A,B,C).toString()));
+}
+/**
+ */
+public static void doubleTest18(int size) {
+System.out.println("\n\n");
+int s = 2;
+
+//DoubleMatrix2D A = Factory2D.make(values);
+DoubleMatrix2D A00, A01, A02, A10, A11, A12, A20, A21, A22, empty;
+empty = Factory2D.make(0,0);
+
+A00 = Factory2D.ascending(s,s);
+//A01 = empty;
+A01 = Factory2D.ascending(s,s).assign(F.plus(A00.getQuick(s-1,s-1)));
+A02 = Factory2D.ascending(s,s).assign(F.plus(A01.getQuick(s-1,s-1)));
+A10 = Factory2D.ascending(s,s).assign(F.plus(A02.getQuick(s-1,s-1)));
+A11 = null;
+//A11 = Factory2D.ascending(s,s).assign(F.plus(A10.getQuick(s-1,s-1)));
+A12 = Factory2D.ascending(s,s).assign(F.plus(A10.getQuick(s-1,s-1)));
+//A12 = Factory2D.ascending(s,s).assign(F.plus(A11.getQuick(s-1,s-1)));
+A20 = Factory2D.ascending(s,s).assign(F.plus(A12.getQuick(s-1,s-1)));
+A21 = empty;
+//A21 = Factory2D.ascending(s,s).assign(F.plus(A20.getQuick(s-1,s-1)));
+A22 = Factory2D.ascending(s,s).assign(F.plus(A20.getQuick(s-1,s-1)));
+//A22 = Factory2D.ascending(s,s).assign(F.plus(A21.getQuick(s-1,s-1)));
+
+
+//B.assign(F.plus(A.zSum()));
+//C.assign(F.plus(B.zSum()));
+
+
+
+
+System.out.println("\n"+A00);
+System.out.println("\n"+A01);
+System.out.println("\n"+A02);
+System.out.println("\n"+A10);
+System.out.println("\n"+A11);
+System.out.println("\n"+A12);
+System.out.println("\n"+A20);
+System.out.println("\n"+A21);
+System.out.println("\n"+A22);
+//System.out.println("\n"+Factory2D.make33(A00,A01,A02,A10,A11,A12,A20,A21,A22));
+
+
+/*
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A00.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A01.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A02.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A10.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A11.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A12.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A20.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A21.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A22.toString()));
+
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(Factory2D.make33(A00,A01,A02,A10,A11,A12,A20,A21,A22).toString()));
+*/
+}
+/**
+ */
+public static void doubleTest19() {
+System.out.println("\n\n");
+DoubleMatrix2D A;
+int k;
+int uk;
+int lk;
+
+double[][] values5 =
+{
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 }
+};
+A = Factory2D.make(values5);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+double[][] values4 =
+{
+ { 1, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 1 }
+};
+A = Factory2D.make(values4);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+double[][] values1 =
+{
+ { 1, 1, 0, 0 },
+ { 1, 1, 1, 0 },
+ { 0, 1, 1, 1 },
+ { 0, 0, 1, 1 }
+};
+A = Factory2D.make(values1);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+
+double[][] values6 =
+{
+ { 0, 1, 1, 1 },
+ { 0, 1, 1, 1 },
+ { 0, 0, 0, 1 },
+ { 0, 0, 0, 1 }
+};
+A = Factory2D.make(values6);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+double[][] values7 =
+{
+ { 0, 0, 0, 0 },
+ { 1, 1, 0, 0 },
+ { 1, 1, 0, 0 },
+ { 1, 1, 1, 1 }
+};
+A = Factory2D.make(values7);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+
+double[][] values2 =
+{
+ { 1, 1, 0, 0 },
+ { 0, 1, 1, 0 },
+ { 0, 1, 0, 1 },
+ { 1, 0, 1, 1 }
+};
+A = Factory2D.make(values2);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+double[][] values3 =
+{
+ { 1, 1, 1, 0 },
+ { 0, 1, 0, 0 },
+ { 1, 1, 0, 1 },
+ { 0, 0, 1, 1 }
+};
+A = Factory2D.make(values3);
+k = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.semiBandwidth(A);
+uk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.upperBandwidth(A);
+lk = org.apache.mahout.colt.matrix.linalg.Property.DEFAULT.lowerBandwidth(A);
+System.out.println("\n\nupperBandwidth="+uk);
+System.out.println("lowerBandwidth="+lk);
+System.out.println("bandwidth="+k+" "+A);
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+
+
+
+
+}
+/**
+ */
+public static void doubleTest19(int size) {
+System.out.println("\n\n");
+int s = 2;
+
+//DoubleMatrix2D A = Factory2D.make(values);
+DoubleMatrix2D A00, A01, A02, A10, A11, A12, A20, A21, A22, empty;
+empty = Factory2D.make(0,0);
+
+A00 = Factory2D.ascending(s,s);
+//A01 = empty;
+A01 = Factory2D.ascending(s,s).assign(F.plus(A00.getQuick(s-1,s-1)));
+A02 = Factory2D.ascending(s,s).assign(F.plus(A01.getQuick(s-1,s-1)));
+A10 = Factory2D.ascending(s,s).assign(F.plus(A02.getQuick(s-1,s-1)));
+A11 = null;
+//A11 = Factory2D.ascending(s,s).assign(F.plus(A10.getQuick(s-1,s-1)));
+A12 = Factory2D.ascending(s,s).assign(F.plus(A10.getQuick(s-1,s-1)));
+//A12 = Factory2D.ascending(s,s).assign(F.plus(A11.getQuick(s-1,s-1)));
+A20 = Factory2D.ascending(s,s).assign(F.plus(A12.getQuick(s-1,s-1)));
+A21 = empty;
+//A21 = Factory2D.ascending(s,s).assign(F.plus(A20.getQuick(s-1,s-1)));
+A22 = Factory2D.ascending(s,s).assign(F.plus(A20.getQuick(s-1,s-1)));
+//A22 = Factory2D.ascending(s,s).assign(F.plus(A21.getQuick(s-1,s-1)));
+
+
+//B.assign(F.plus(A.zSum()));
+//C.assign(F.plus(B.zSum()));
+
+
+
+
+System.out.println("\n"+A00);
+System.out.println("\n"+A01);
+System.out.println("\n"+A02);
+System.out.println("\n"+A10);
+System.out.println("\n"+A11);
+System.out.println("\n"+A12);
+System.out.println("\n"+A20);
+System.out.println("\n"+A21);
+System.out.println("\n"+A22);
+//System.out.println("\n"+Factory2D.make33(A00,A01,A02,A10,A11,A12,A20,A21,A22));
+
+
+/*
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A00.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A01.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A02.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A10.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A11.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A12.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A20.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A21.toString()));
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A22.toString()));
+
+System.out.println("\n"+org.apache.mahout.colt.matrixpattern.Converting.toHTML(Factory2D.make33(A00,A01,A02,A10,A11,A12,A20,A21,A22).toString()));
+*/
+}
+/**
+ */
+public static void doubleTest2() {
+// using a map
+int[] keys = {0 , 3 , 100000, 9 };
+double[] values = {100.0, 1000.0, 70.0 , 71.0};
+
+int size = keys.length;
+AbstractIntDoubleMap map = new OpenIntDoubleHashMap(size*2, 0.2, 0.5);
+
+for (int i=0; i<keys.length; i++) {
+ map.put(keys[i], (int)values[i]);
+}
+
+System.out.println(map.containsKey(3));
+System.out.println(map.get(3));
+
+System.out.println(map.containsKey(4));
+System.out.println(map.get(4));
+
+System.out.println(map.containsValue((int)71.0));
+System.out.println(map.keyOf((int)71.0));
+
+System.out.println(map);
+}
+/**
+ */
+public static void doubleTest20() {
+System.out.println("\n\n");
+DoubleMatrix2D A;
+int k;
+int uk;
+int lk;
+
+double[][] values1 =
+{
+ { 0, 1, 0, 0 },
+ { 3, 0, 2, 0 },
+ { 0, 2, 0, 3 },
+ { 0, 0, 1, 0 }
+};
+A = Factory2D.make(values1);
+
+System.out.println("\n\n"+LinearAlgebra.toVerboseString(A));
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+
+double[][] values2 =
+{
+ { 1.0000000000000167, -0.3623577544766736, -0.3623577544766736 },
+ { 0 , 0.9320390859672374, -0.3377315902755755 },
+ { 0 , 0 , 0.8686968577706282 },
+ { 0 , 0 , 0 },
+ { 0 , 0 , 0 }
+};
+
+A = Factory2D.make(values2);
+
+System.out.println("\n\n"+LinearAlgebra.toVerboseString(A));
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+double[][] values3 =
+{
+ { 611, 196, -192, 407, -8, -52, -49, 29 },
+ { 196, 899, 113, -192, -71, -43, -8, -44 },
+ {-192, 113, 899, 196, 61, 49, 8, 52 },
+ { 407, -192, 196, 611, 8, 44, 59, -23 },
+ { -8, -71, 61, 8, 411, -599, 208, 208 },
+ { -52, -43, 49, 44, -599, 411, 208, 208 },
+ { -49, -8, 8, 59, 208, 208, 99, -911 },
+ { 29, -44, 52, -23, 208, 208, -911, 99 }
+};
+
+
+A = Factory2D.make(values3);
+
+System.out.println("\n\n"+LinearAlgebra.toVerboseString(A));
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+
+//Exact eigenvalues from Westlake (1968), p.150 (ei'vectors given too):
+double a = Math.sqrt(10405); double b = Math.sqrt(26);
+double[] e = {-10*a, 0, 510-100*b, 1000, 1000, 510+100*b, 1020, 10*a};
+System.out.println(Factory1D.dense.make(e));
+
+
+}
+/**
+ */
+public static void doubleTest21() {
+System.out.println("\n\n");
+DoubleMatrix2D A;
+int k;
+int uk;
+int lk;
+
+double[][] values1 =
+{
+ { 1/3, 2/3, Math.PI, 0 },
+ { 3, 9, 0, 0 },
+ { 0, 2, 7, 0 },
+ { 0, 0, 3, 9 }
+};
+A = Factory2D.make(values1);
+System.out.println(A);
+System.out.println(new org.apache.mahout.colt.matrix.doublealgo.Formatter(null).toString(A));
+
+//System.out.println("\n\n"+LinearAlgebra.toVerboseString(A));
+//System.out.println(new LUDecomposition(A));
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+}
+/**
+ */
+public static void doubleTest22() {
+System.out.println("\n\n");
+DoubleMatrix2D A;
+int k;
+int uk;
+int lk;
+
+double[][] values1 =
+{
+ { 1/3, 2/3, Math.PI, 0 },
+ { 3, 9, 0, 0 },
+ { 0, 2, 7, 0 },
+ { 0, 0, 3, 9 }
+};
+A = Factory2D.make(values1);
+System.out.println(A);
+System.out.println(Property.isDiagonallyDominantByRow(A));
+System.out.println(Property.isDiagonallyDominantByColumn(A));
+Property.generateNonSingular(A);
+System.out.println(A);
+System.out.println(Property.isDiagonallyDominantByRow(A));
+System.out.println(Property.isDiagonallyDominantByColumn(A));
+
+//System.out.println("\n\n"+LinearAlgebra.toVerboseString(A));
+//System.out.println(new LUDecomposition(A));
+//System.out.println("\n\nbandwidth="+k+" "+org.apache.mahout.colt.matrixpattern.Converting.toHTML(A.toString()));
+}
+/**
+ */
+public static void doubleTest23(int runs, int size, double nonZeroFraction, boolean dense) {
+System.out.println("\n\n");
+System.out.println("initializing...");
+DoubleMatrix2D A, LU, I, Inv;
+DoubleMatrix1D b, solved;
+
+double mean = 5.0;
+double stdDev = 3.0;
+org.apache.mahout.jet.random.Normal random = new org.apache.mahout.jet.random.Normal(mean, stdDev, new org.apache.mahout.jet.random.engine.MersenneTwister());
+
+System.out.println("sampling...");
+double value = 2;
+if (dense)
+ A = Factory2D.dense.sample(size,size, value, nonZeroFraction);
+else
+ A = Factory2D.sparse.sample(size,size, value, nonZeroFraction);
+b = A.like1D(size).assign(1);
+
+//A.assign(random);
+//A.assign(F.rint); // round
+System.out.println("generating invertible matrix...");
+Property.generateNonSingular(A);
+
+//I = Factory2D.identity(size);
+
+LU = A.like();
+solved = b.like();
+//Inv = Factory2D.make(size,size);
+
+LUDecompositionQuick lu = new LUDecompositionQuick();
+
+System.out.println("benchmarking assignment...");
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+LU.assign(A);
+solved.assign(b);
+timer.stop().display();
+
+LU.assign(A);
+lu.decompose(LU);
+
+System.out.println("benchmarking LU...");
+timer.reset().start();
+for (int i=runs; --i >=0; ) {
+ solved.assign(b);
+ //Inv.assign(I);
+ //lu.decompose(LU);
+ lu.solve(solved);
+ //lu.solve(Inv);
+}
+timer.stop().display();
+
+//System.out.println("A="+A);
+//System.out.println("LU="+LU);
+//System.out.println("U="+lu.getU());
+//System.out.println("L="+lu.getL());
+System.out.println("done.");
+
+}
+/**
+ */
+public static void doubleTest24(int runs, int size, boolean dense) {
+System.out.println("\n\n");
+System.out.println("initializing...");
+DoubleMatrix2D A;
+DoubleFactory2D factory;
+if (dense)
+ factory = Factory2D.dense;
+else
+ factory = Factory2D.sparse;
+
+double value = 2;
+double omega = 1.25;
+final double alpha = omega * 0.25;
+final double beta = 1-omega;
+A = factory.make(size,size,value);
+
+org.apache.mahout.colt.function.Double9Function function = new org.apache.mahout.colt.function.Double9Function() {
+ public final double apply(double a00, double a01, double a02, double a10, double a11, double a12, double a20, double a21, double a22) {
+ return alpha*a11 + beta*(a01+a10+a12+a21);
+ }
+};
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+
+System.out.println("benchmarking stencil...");
+for (int i=0; i<runs; i++) {
+ A.zAssign8Neighbors(A,function);
+}
+//A.zSum4Neighbors(A,alpha,beta,runs);
+timer.stop().display();
+//System.out.println("A="+A);
+A=null;
+
+double[][] B = factory.make(size,size,value).toArray();
+timer.reset().start();
+
+System.out.println("benchmarking stencil scimark...");
+for (int i=0; i<runs; i++) {
+// jnt.scimark2.SOR.execute(omega, B, runs);
+}
+timer.stop().display();
+
+
+System.out.println("done.");
+
+}
+/**
+ */
+public static void doubleTest25(int size) {
+
+
+System.out.println("\n\n");
+System.out.println("initializing...");
+boolean dense = true;
+DoubleMatrix2D A;
+DoubleFactory2D factory;
+if (dense)
+ factory = Factory2D.dense;
+else
+ factory = Factory2D.sparse;
+
+double value = 0.5;
+A = factory.make(size,size,value);
+Property.generateNonSingular(A);
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+
+System.out.println(A);
+System.out.println(Algebra.ZERO.inverse(A));
+
+timer.stop().display();
+
+System.out.println("done.");
+
+}
+/**
+ */
+public static void doubleTest26(int size) {
+
+
+System.out.println("\n\n");
+System.out.println("initializing...");
+boolean dense = true;
+DoubleMatrix2D A;
+DoubleFactory2D factory;
+if (dense)
+ factory = Factory2D.dense;
+else
+ factory = Factory2D.sparse;
+
+double value = 0.5;
+A = factory.make(size,size,value);
+Property.generateNonSingular(A);
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+
+DoubleMatrix2DComparator fun = new DoubleMatrix2DComparator() {
+ public int compare(DoubleMatrix2D a, DoubleMatrix2D b) {
+ return a.zSum() == b.zSum() ? 1 : 0;
+ }
+};
+
+System.out.println(A);
+System.out.println(Algebra.ZERO.inverse(A));
+
+timer.stop().display();
+
+System.out.println("done.");
+
+}
+/**
+ */
+public static void doubleTest27() {
+
+
+System.out.println("\n\n");
+System.out.println("initializing...");
+
+int rows=51;
+int columns=10;
+double[][] trainingSet = new double[columns][rows];
+for (int i=columns; --i >= 0; ) trainingSet[i][i]=2.0;
+
+int patternIndex = 0;
+int unitIndex = 0;
+
+DoubleMatrix2D patternMatrix = null;
+DoubleMatrix2D transposeMatrix = null;
+DoubleMatrix2D QMatrix = null;
+DoubleMatrix2D inverseQMatrix = null;
+DoubleMatrix2D pseudoInverseMatrix = null;
+DoubleMatrix2D weightMatrix = null;
+
+// form a matrix with the columns as training vectors
+patternMatrix = DoubleFactory2D.dense.make (rows, columns);
+
+// copy the patterns into the matrix
+for (patternIndex=0;patternIndex<columns;patternIndex++) {
+ for (unitIndex=0;unitIndex<rows;unitIndex++) {
+ patternMatrix.setQuick (unitIndex, patternIndex, trainingSet[patternIndex][unitIndex]);
+ }
+}
+
+transposeMatrix = Algebra.DEFAULT.transpose (patternMatrix);
+QMatrix = Algebra.DEFAULT.mult (transposeMatrix,patternMatrix);
+inverseQMatrix = Algebra.DEFAULT.inverse (QMatrix);
+pseudoInverseMatrix = Algebra.DEFAULT.mult (inverseQMatrix,transposeMatrix);
+weightMatrix = Algebra.DEFAULT.mult (patternMatrix,pseudoInverseMatrix);
+System.out.println("done.");
+
+}
+/**
+ */
+public static void doubleTest28() {
+ double[] data={1,2,3,4,5,6};
+ double[][] arrMatrix =
+ {
+ { 1, 2, 3, 4, 5, 6},
+ { 2, 3, 4, 5, 6, 7}
+ };
+ DoubleFactory2D f = DoubleFactory2D.dense;
+ DoubleMatrix1D vector = new DenseDoubleMatrix1D(data);
+ DoubleMatrix2D matrix = f.make(arrMatrix);
+ DoubleMatrix1D res = vector.like(matrix.rows());
+
+ matrix.zMult(vector,res);
+
+ System.out.println(res);
+}
+/**
+ */
+public static void doubleTest28(DoubleFactory2D f) {
+ double[] data={1,2,3,4,5,6};
+ double[][] arrMatrix =
+ {
+ { 1, 2, 3, 4, 5, 6},
+ { 2, 3, 4, 5, 6, 7}
+ };
+
+ DoubleMatrix1D vector = new DenseDoubleMatrix1D(data);
+ DoubleMatrix2D matrix = f.make(arrMatrix);
+ DoubleMatrix1D res = vector.like(matrix.rows());
+
+ matrix.zMult(vector,res);
+
+ System.out.println(res);
+}
+/**
+ */
+public static void doubleTest29(int size) {
+/*
+
+System.out.println("\n\n");
+System.out.println("initializing...");
+boolean dense = false;
+DoubleMatrix2D A;
+DoubleFactory2D factory;
+if (dense)
+ factory = Factory2D.dense;
+else
+ factory = Factory2D.sparse;
+
+double value = 0.5;
+
+DoubleMatrix2D C = Factory2D.dense.sample(size,size,value,1);
+
+A = factory.make(size,size);
+System.out.print("A assign C... ");
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+A.assign(C);
+timer.stop().display();
+
+System.out.print("A getquick... ");
+timer.reset().start();
+double sum=0;
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ sum+=A.getQuick(i,j);
+ }
+}
+timer.stop().display();
+System.out.println(sum);
+//System.out.println(A);
+
+System.out.print("sci set3... ");
+JSci.maths.DoubleSparseMatrix B = new JSci.maths.DoubleSparseMatrix(size);
+timer.reset().start();
+//for (int i=size; --i>=0; ) {
+// for (int j=size; --j>=0; ) {
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ B.setElement3(i,j,C.getQuick(i,j));
+ }
+}
+//System.out.println(A);
+timer.stop().display();
+
+System.out.print("sci get3... ");
+timer.reset().start();
+sum=0;
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ sum+=B.getElement3(i,j);
+ }
+}
+System.out.println(sum);
+timer.stop().display();
+
+JSci.maths.DoubleVector vec = new JSci.maths.DoubleVector(size);
+
+System.out.print("sci mult3... ");
+timer.reset().start();
+B.multiply3(vec);
+timer.stop().display();
+
+
+System.out.println("done.");
+*/
+}
+/**
+ */
+public static void doubleTest29(int size,DoubleFactory2D f) {
+
+ DoubleMatrix2D x = new DenseDoubleMatrix2D(size,size).assign(0.5);
+ DoubleMatrix2D matrix = f.sample(size,size,0.5,0.001);
+
+ org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+ DoubleMatrix2D res = matrix.zMult(x,null);
+ timer.stop().display();
+
+ //System.out.println(res);
+}
+/**
+ */
+public static void doubleTest29(DoubleFactory2D f) {
+ double[][] data =
+ {
+ { 6, 5, 4 },
+ { 7, 6, 3 },
+ { 6, 5, 4 },
+ { 7, 6, 3 },
+ { 6, 5, 4 },
+ { 7, 6, 3 }
+ };
+
+ double[][] arrMatrix =
+ {
+ { 1, 2, 3, 4, 5, 6},
+ { 2, 3, 4, 5, 6, 7}
+ };
+
+ DoubleMatrix2D x = new DenseDoubleMatrix2D(data);
+ DoubleMatrix2D matrix = f.make(arrMatrix);
+
+ DoubleMatrix2D res = matrix.zMult(x,null);
+
+ System.out.println(res);
+}
+/**
+ */
+public static void doubleTest3() {
+int rows = 4;
+int columns = 5; // make a 4*5 matrix
+DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
+System.out.println(master);
+master.assign(1); // set all cells to 1
+System.out.println("\n"+master);
+master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
+System.out.println("\n"+master);
+
+DoubleMatrix2D flip1 = master.viewColumnFlip();
+System.out.println("flip around columns="+flip1);
+DoubleMatrix2D flip2 = flip1.viewRowFlip();
+System.out.println("further flip around rows="+flip2);
+
+flip2.viewPart(0,0,2,2).assign(3);
+System.out.println("master replaced"+master);
+System.out.println("flip1 replaced"+flip1);
+System.out.println("flip2 replaced"+flip2);
+
+
+/*
+DoubleMatrix2D copyPart = master.copyPart(2,1,2,3);
+copyPart.assign(3); // modify an independent copy
+copyPart.set(0,0,4);
+System.out.println("\n"+copyPart); // has changed
+System.out.println("\n"+master); // master has not changed
+
+DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
+DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
+System.out.println("\n"+view1);
+System.out.println("\n"+view2);
+*/
+}
+/**
+ */
+public static void doubleTest30() {
+ double[][] data =
+ {
+ { 6, 5 },
+ { 7, 6 },
+ };
+
+ double[] x = { 1, 2 };
+ double[] y = { 3, 4 };
+
+ DoubleMatrix2D A = new DenseDoubleMatrix2D(data);
+ SeqBlas.seqBlas.dger(1,new DenseDoubleMatrix1D(x), new DenseDoubleMatrix1D(y), A);
+
+ System.out.println(A);
+}
+/**
+ */
+public static void doubleTest30(int size) {
+
+int[] values = { 0, 2, 3, 5, 7};
+IntArrayList list = new IntArrayList(values);
+int val = 3;
+int sum=0;
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+for (int i=size; --i>=0; ) {
+ int k = list.binarySearchFromTo(val,0,values.length-1);
+ System.out.println(list+", "+val+" --> "+k);
+ sum+=k;
+}
+timer.stop().display();
+//System.out.println("sum = "+sum);
+
+
+/*
+System.out.println("\n\n");
+System.out.println("initializing...");
+boolean dense = false;
+DoubleMatrix2D A;
+DoubleFactory2D factory;
+if (dense)
+ factory = Factory2D.dense;
+else
+ factory = Factory2D.sparse;
+
+double value = 0.5;
+
+DoubleMatrix2D C = Factory2D.dense.sample(size,size,value,0.01);
+
+A = factory.make(size,size);
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+A.assign(C);
+timer.stop().display();
+
+timer.reset().start();
+double sum=0;
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ sum+=A.getQuick(i,j);
+ }
+}
+timer.stop().display();
+System.out.println(sum);
+//System.out.println(A);
+
+JSci.maths.DoubleSparseMatrix B = new JSci.maths.DoubleSparseMatrix(size);
+timer.reset().start();
+for (int i=size; --i>=0; ) {
+ for (int j=size; --j>=0; ) {
+//for (int i=0; i<size; i++) {
+// for (int j=0; j<size; j++ ) {
+ B.setElement2(i,j,C.getQuick(i,j));
+ }
+}
+//System.out.println(A);
+timer.stop().display();
+
+timer.reset().start();
+sum=0;
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ sum+=B.getElement2(i,j);
+ }
+}
+System.out.println(sum);
+timer.stop().display();
+
+System.out.println("done.");
+
+*/
+}
+/**
+ */
+public static void doubleTest30(int size, int val) {
+
+//int[] values = { 0, 2};
+int[] values = {2};
+IntArrayList list = new IntArrayList(values);
+int l = values.length-1;
+int sum=0;
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+for (int i=size; --i>=0; ) {
+ int k = org.apache.mahout.colt.Sorting.binarySearchFromTo(values,val,0,l);
+ //int k = list.binarySearchFromTo(val,0,l);
+ //System.out.println(list+", "+val+" --> i="+k+", -i-1="+(-k-1));
+ sum+=k;
+}
+timer.stop().display();
+System.out.println("sum = "+sum);
+
+
+/*
+System.out.println("\n\n");
+System.out.println("initializing...");
+boolean dense = false;
+DoubleMatrix2D A;
+DoubleFactory2D factory;
+if (dense)
+ factory = Factory2D.dense;
+else
+ factory = Factory2D.sparse;
+
+double value = 0.5;
+
+DoubleMatrix2D C = Factory2D.dense.sample(size,size,value,0.01);
+
+A = factory.make(size,size);
+org.apache.mahout.colt.Timer timer = new org.apache.mahout.colt.Timer().start();
+A.assign(C);
+timer.stop().display();
+
+timer.reset().start();
+double sum=0;
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ sum+=A.getQuick(i,j);
+ }
+}
+timer.stop().display();
+System.out.println(sum);
+//System.out.println(A);
+
+JSci.maths.DoubleSparseMatrix B = new JSci.maths.DoubleSparseMatrix(size);
+timer.reset().start();
+for (int i=size; --i>=0; ) {
+ for (int j=size; --j>=0; ) {
+//for (int i=0; i<size; i++) {
+// for (int j=0; j<size; j++ ) {
+ B.setElement2(i,j,C.getQuick(i,j));
+ }
+}
+//System.out.println(A);
+timer.stop().display();
+
+timer.reset().start();
+sum=0;
+for (int i=0; i<size; i++) {
+ for (int j=0; j<size; j++ ) {
+ sum+=B.getElement2(i,j);
+ }
+}
+System.out.println(sum);
+timer.stop().display();
+
+System.out.println("done.");
+
+*/
+}
+/**
+ */
+public static void doubleTest31(int size) {
+
+System.out.println("\ninit");
+DoubleMatrix1D a = Factory1D.dense.descending(size);
+DoubleMatrix1D b = new WrapperDoubleMatrix1D(a);
+DoubleMatrix1D c = b.viewPart(2,3);
+DoubleMatrix1D d = c.viewFlip();
+//DoubleMatrix1D c = b.viewFlip();
+//DoubleMatrix1D d = c.viewFlip();
+d.set(0,99);
+b = b.viewSorted();
+System.out.println("a = "+a);
+System.out.println("b = "+b);
+System.out.println("c = "+c);
+System.out.println("d = "+d);
+
+System.out.println("done");
+
+}
+/**
+ */
+public static void doubleTest32() {
+ double[][] data =
+ {
+ { 1, 4, 0 },
+ { 6, 2, 5 },
+ { 0, 7, 3 },
+ { 0, 0, 8 },
+ { 0, 0, 0 },
+ { 0, 0, 0 }
+ };
+
+ DoubleMatrix2D x = new TridiagonalDoubleMatrix2D(data);
+
+
+ System.out.println("\n\n\n"+x);
+ System.out.println("\n"+new DenseDoubleMatrix2D(data));
+}
+/**
+ */
+public static void doubleTest33() {
+ double nan = Double.NaN;
+ double inf = Double.POSITIVE_INFINITY;
+ double ninf = Double.NEGATIVE_INFINITY;
+
+ double[][] data =
+ {{ ninf, nan}};
+ /*
+ {
+ { 1, 4, 0 },
+ { 6, 2, 5 },
+ { 0, 7, 3 },
+ { 0, 0, 8 },
+ { 0, 0, 0 },
+ { 0, 0, 0 }
+ };
+ */
+
+ DoubleMatrix2D x = new DenseDoubleMatrix2D(data);
+
+ System.out.println("\n\n\n"+x);
+ System.out.println("\n"+ x.equals(ninf));
+}
+/**
+ */
+public static void doubleTest34() {
+ double[][] data =
+ {
+ { 3, 0, 0, 0 },
+ { 0, 4, 2, 0 },
+ { 0, 0, 0, 0 },
+ { 0, 0, 0, 0 },
+ };
+
+ DoubleMatrix2D A = new DenseDoubleMatrix2D(data);
+ Property.DEFAULT.generateNonSingular(A);
+ DoubleMatrix2D inv = Algebra.DEFAULT.inverse(A);
+
+
+ System.out.println("\n\n\n"+A);
+ System.out.println("\n"+inv);
+ DoubleMatrix2D B = A.zMult(inv,null);
+ System.out.println(B);
+ if (!(B.equals(DoubleFactory2D.dense.identity(A.rows)))) {
+ throw new InternalError();
+ }
+}
+/**
+ * Title: Aero3D<p>
+ * Description: A Program to analyse aeroelestic evects in transonic wings<p>
+ * Copyright: Copyright (c) 1998<p>
+ * Company: PIERSOL Engineering Inc.<p>
+ * @author John R. Piersol
+ * @version
+ */
+public static void doubleTest35() {
+ /*
+ final int DOF = 200;
+ final org.apache.mahout.jet.random.engine.MersenneTwister RANDOM = new org.apache.mahout.jet.random.engine.MersenneTwister();
+ final Algebra ALGEBRA = new Algebra();
+
+ System.out.println("\n\n\nStarting...");
+ double[][] k = randomMatrix(DOF, RANDOM);
+ DoubleMatrix2D kd = new DenseDoubleMatrix2D(k);
+ Jama.Matrix km = new Jama.Matrix(k);
+
+
+
+
+
+ DoubleMatrix2D coltL = new LUDecomposition(kd).getL();
+ DoubleMatrix2D coltU = new LUDecomposition(kd).getU();
+ Jama.Matrix jamaL = new Jama.LUDecomposition(km).getL();
+ Jama.Matrix jamaU = new Jama.LUDecomposition(km).getU();
+
+ System.out.println(coltL.equals(kd.like().assign(jamaL.getArrayCopy())));
+ System.out.println(coltL.aggregate(F.plus,F.abs));
+ double s = 0;
+ double[] temp2 = jamaL.getColumnPackedCopy();
+ for (int i = 0, n = temp2.length; i < n; ++i) s += Math.abs(temp2[i]);
+ System.out.println(s);
+
+ System.out.println(coltU.equals(kd.like().assign(jamaU.getArrayCopy())));
+ System.out.println(coltU.aggregate(F.plus,F.abs));
+ s = 0;
+ temp2 = jamaU.getColumnPackedCopy();
+ for (int i = 0, n = temp2.length; i < n; ++i) s += Math.abs(temp2[i]);
+ System.out.println(s);
+
+ //System.out.println("colt="+new LUDecomposition(kd).toString());
+ //System.out.println("jama="+new Jama.LUDecomposition(km).toString());
+
+
+
+ Jama.Matrix kmi = km.inverse();
+
+ DoubleMatrix2D kdi = Algebra.DEFAULT.inverse(kd);
+ DoubleMatrix2D checkColt = Algebra.DEFAULT.mult(kd, kdi);
+ System.out.println("Colt checksum = " + checkColt.aggregate(F.plus,F.abs) + ", correct = " + DOF);
+
+ Jama.Matrix checkJama = kmi.times(km);
+ double checksum = 0;
+ double[] temp = checkJama.getColumnPackedCopy();
+ for (int i = 0, n = temp.length; i < n; ++i) checksum += Math.abs(temp[i]);
+ System.out.println("Jama checksum = " + checksum + ", correct = " + DOF);
+
+ System.out.println("done\n");
+ */
+}
+/**
+ * Title: Aero3D<p>
+ * Description: A Program to analyse aeroelestic evects in transonic wings<p>
+ * Copyright: Copyright (c) 1998<p>
+ * Company: PIERSOL Engineering Inc.<p>
+ * @author John R. Piersol
+ * @version
+ */
+public static void doubleTest36() {
+ double[] testSort = new double[5];
+ testSort[0] = 5;
+ testSort[1] = Double.NaN;
+ testSort[2] = 2;
+ testSort[3] = Double.NaN;
+ testSort[4] = 1;
+ DoubleMatrix1D doubleDense = new DenseDoubleMatrix1D(testSort);
+ System.out.println("orig = "+doubleDense);
+ doubleDense = doubleDense.viewSorted();
+ doubleDense.toArray(testSort);
+ System.out.println("sort = "+doubleDense);
+ System.out.println("done\n");
+}
+/**
+ */
+public static void doubleTest4() {
+int rows = 4;
+int columns = 5; // make a 4*5 matrix
+DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
+System.out.println(master);
+master.assign(1); // set all cells to 1
+DoubleMatrix2D view = master.viewPart(2,0,2,3).assign(2);
+System.out.println("\n"+master);
+System.out.println("\n"+view);
+Transform.mult(view,3);
+System.out.println("\n"+master);
+System.out.println("\n"+view);
+
+
+/*
+DoubleMatrix2D copyPart = master.copyPart(2,1,2,3);
+copyPart.assign(3); // modify an independent copy
+copyPart.set(0,0,4);
+System.out.println("\n"+copyPart); // has changed
+System.out.println("\n"+master); // master has not changed
+
+DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
+DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
+System.out.println("\n"+view1);
+System.out.println("\n"+view2);
+*/
+}
+/**
+ */
+public static void doubleTest5() {
+ /*
+int rows = 4;
+int columns = 5; // make a 4*5 matrix
+DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
+System.out.println(master);
+master.assign(1); // set all cells to 1
+DoubleMatrix2D view = master.viewPart(2,0,2,3);
+view.assign(0);
+for (int i=0; i<rows; i++) {
+ for (int j=0; j<columns; j++) {
+ boolean hasIndex = view.hasIndex(master.index(i,j));
+ System.out.println("("+i+","+j+"):"+hasIndex);
+ }
+}
+System.out.println("\n"+master);
+System.out.println("\n"+view);
+IntArrayList rowList = new IntArrayList();
+IntArrayList columnList = new IntArrayList();
+DoubleArrayList valueList = new DoubleArrayList();
+master.getNonZeros(rowList,columnList,valueList);
+System.out.println(rowList);
+System.out.println(columnList);
+System.out.println(valueList);
+System.out.println(master.toStringSparse());
+*/
+}
+/**
+ */
+public static void doubleTest6() {
+int rows = 4;
+int columns = 5; // make a 4*5 matrix
+DoubleMatrix2D master = Factory2D.ascending(rows,columns);
+//master.assign(1); // set all cells to 1
+System.out.println("\n"+master);
+master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
+System.out.println("\n"+master);
+
+int[] indexes = {0,1,3,0,1,2};
+DoubleMatrix1D view1 = master.viewRow(0).viewSelection(indexes);
+System.out.println("view1="+view1);
+DoubleMatrix1D view2 = view1.viewPart(0,3);
+System.out.println("view2="+view2);
+
+view2.viewPart(0,2).assign(-1);
+System.out.println("master replaced"+master);
+System.out.println("flip1 replaced"+view1);
+System.out.println("flip2 replaced"+view2);
+
+
+/*
+DoubleMatrix2D copyPart = master.copyPart(2,1,2,3);
+copyPart.assign(3); // modify an independent copy
+copyPart.set(0,0,4);
+System.out.println("\n"+copyPart); // has changed
+System.out.println("\n"+master); // master has not changed
+
+DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
+DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
+System.out.println("\n"+view1);
+System.out.println("\n"+view2);
+*/
+}
+/**
+ */
+public static void doubleTest7() {
+int rows = 4;
+int columns = 5; // make a 4*5 matrix
+DoubleMatrix2D master = Factory2D.ascending(rows,columns);
+//master.assign(1); // set all cells to 1
+System.out.println("\n"+master);
+//master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
+//System.out.println("\n"+master);
+
+int[] rowIndexes = {0,1,3,0};
+int[] columnIndexes = {0,2};
+DoubleMatrix2D view1 = master.viewSelection(rowIndexes,columnIndexes);
+System.out.println("view1="+view1);
+DoubleMatrix2D view2 = view1.viewPart(0,0,2,2);
+System.out.println("view2="+view2);
+
+view2.assign(-1);
+System.out.println("master replaced"+master);
+System.out.println("flip1 replaced"+view1);
+System.out.println("flip2 replaced"+view2);
+
+}
+/**
+ */
+public static void doubleTest8() {
+int rows = 2;
+int columns = 3; // make a 4*5 matrix
+DoubleMatrix2D master = Factory2D.ascending(rows,columns);
+//master.assign(1); // set all cells to 1
+System.out.println("\n"+master);
+//master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
+//System.out.println("\n"+master);
+
+DoubleMatrix2D view1 = master.viewDice();
+System.out.println("view1="+view1);
+DoubleMatrix2D view2 = view1.viewDice();
+System.out.println("view2="+view2);
+
+view2.assign(-1);
+System.out.println("master replaced"+master);
+System.out.println("flip1 replaced"+view1);
+System.out.println("flip2 replaced"+view2);
+
+}
+/**
+ */
+public static void doubleTest9() {
+int rows = 2;
+int columns = 3; // make a 4*5 matrix
+DoubleMatrix2D master = Factory2D.ascending(rows,columns);
+//master.assign(1); // set all cells to 1
+System.out.println("\n"+master);
+//master.viewPart(2,0,2,3).assign(2); // set [2,1] .. [3,3] to 2
+//System.out.println("\n"+master);
+
+DoubleMatrix2D view1 = master.viewRowFlip();
+System.out.println("view1="+view1);
+DoubleMatrix2D view2 = view1.viewRowFlip();
+System.out.println("view2="+view2);
+
+view2.assign(-1);
+System.out.println("master replaced"+master);
+System.out.println("flip1 replaced"+view1);
+System.out.println("flip2 replaced"+view2);
+
+}
+public static void doubleTestQR() {
+// test case0...
+ double x0[] = { -6.221564, -9.002113, 2.678001, 6.483597, -7.934148 };
+ double y0[] = { -7.291898, -7.346928, 0.520158, 5.012548, -8.223725 };
+ double x1[] = { 1.185925, -2.523077, 0.135380 , 0.412556, -2.980280 };
+ double y1[] = {13.561087, -15.204410, 16.496829, 16.470860, 0.822198};
+
+ solve(x1.length, x1, y1);
+ solve(x0.length, x0, y0);
+}
+/**
+ */
+public static void main(String[] args) {
+ int runs = Integer.parseInt(args[0]);
+ int val = Integer.parseInt(args[1]);
+ doubleTest30(runs, val);
+ /*
+ int runs = Integer.parseInt(args[0]);
+ int size = Integer.parseInt(args[1]);
+ double nonZeroFraction = new Double(args[2]).doubleValue();
+ boolean dense = args[3].equals("dense");
+ //doubleTest23(runs, size, nonZeroFraction, dense);
+ doubleTest24(runs, size, dense);
+ */
+}
+public static double[][] randomMatrix(int dof, org.apache.mahout.jet.random.engine.MersenneTwister RANDOM) {
+ double[][] m = new double[dof][dof];
+ /*
+ for (int i = 0; i < dof; ++i) {
+ for (int j = i - 1, n = i + 1; j <= n; ++j) {
+ if (j < dof && j > -1)
+ m[i][j] = RANDOM.nextDouble();
+ }
+ }
+ */
+ for (int i = 0; i < dof; ++i) {
+ for (int j = 0; j < dof; j++) {
+ m[i][j] = 5;
+ }
+ }
+ // for (int i = 0; i < dof; ++i)
+ // for (int j = 0; j < dof; ++j) m[i][j] = RANDOM.nextDouble();
+ return m;
+}
+public static void solve(int numpnt, double x[], double y[]) {
+ /*
+ // create the matrix object
+ DoubleMatrix2D A = new DenseDoubleMatrix2D(numpnt, 5);
+ DoubleMatrix2D B = new DenseDoubleMatrix2D(numpnt, 1);
+ //fillout the matrix
+ for (int i = 0; i < numpnt; i++) {
+ A.setQuick(i, 0, x[i] * y[i]);
+ A.setQuick(i, 1, y[i] * y[i]);
+ A.setQuick(i, 2, x[i]);
+ A.setQuick(i, 3, y[i]);
+ A.setQuick(i, 4, 1.0);
+ B.setQuick(i, 0, -x[i] * x[i]);
+ }
+ System.out.println(A);
+ //test the matrix condition
+ SingularValueDecomposition svd = new SingularValueDecomposition(A);
+ System.out.println(svd);
+ // Using Algebra to solve the equation
+ Algebra alg = new Algebra();
+ DoubleMatrix2D resAlg = alg.solve(A.copy(), B.copy());
+ System.out.println("Using Algebra...");
+ System.out.println(resAlg);
+ // Using QRDecomposition to solve the problem..
+ QRDecomposition qrd = new QRDecomposition(A);
+ DoubleMatrix2D resQRD = qrd.solve(B);
+ System.out.println("Using QRDecomposition...");
+ System.out.println(resQRD);
+ // Using Jama.QRDecomposition to solve the problem..
+ Jama.QRDecomposition qrdJama = new Jama.QRDecomposition(new Jama.Matrix(A.toArray()));
+ resQRD = new DenseDoubleMatrix2D(qrdJama.solve(new Jama.Matrix(B.toArray())).getArrayCopy());
+ System.out.println("Using Jama.QRDecomposition...");
+ System.out.println(resQRD);
+ */
+}
+/**
+ */
+public static void testLU() {
+double[][] vals = {
+ {-0.074683, 0.321248,-0.014656, 0.286586,0},
+ {-0.344852, -0.16278 , 0.173711, 0.00064 ,0},
+ {-0.181924, -0.092926, 0.184153, 0.177966,1},
+ {-0.166829, -0.10321 , 0.582301, 0.142583,0},
+ { 0 , -0.112952,-0.04932 ,-0.700157,0},
+ { 0 , 0 ,0 ,0 ,0}
+};
+
+DoubleMatrix2D H = new DenseDoubleMatrix2D( vals ); // see values below...
+System.out.println("\nHplus="+H.viewDice().zMult(H,null));
+
+DoubleMatrix2D Hplus = Algebra.DEFAULT.inverse(H.viewDice().zMult( H,null )).zMult(H.viewDice(),null);
+Hplus.assign(org.apache.mahout.jet.math.Functions.round(1.0E-10));
+System.out.println("\nHplus="+Hplus);
+
+ /*
+DoubleMatrix2D HtH = new DenseDoubleMatrix2D( 5, 5 );
+DoubleMatrix2D Hplus = new DenseDoubleMatrix2D( 5, 6 );
+LUDecompositionQuick LUD = new LUDecompositionQuick();
+ //H.zMult( H, HtH, 1, 0, true, false );
+ //DoubleMatrix2D res = Algebra.DEFAULT.inverse(HtH).zMult(H,null,1,0,false,true);
+ LUD.decompose( HtH );
+ // first fill Hplus with the transpose of H...
+ for (int i = 0; i < 6; i++ ) {
+ for ( int j = 0; j < 5; j++ ) {
+ Hplus.set( j, i, H.get( i, j ) );
+ }
+ }
+ LUD.solve( Hplus );
+
+ DoubleMatrix2D perm = Algebra.DEFAULT.permute(Hplus, null,LUD.getPivot());
+ DoubleMatrix2D inv = Algebra.DEFAULT.inverse(HtH);//.zMult(H,null,1,0,false,true);
+ */
+
+ // in matlab...
+ // Hplus = inv(H' * H) * H'
+
+//System.out.println("\nLU="+LUD);
+//System.out.println("\nHplus="+Hplus);
+//System.out.println("\nperm="+perm);
+//System.out.println("\ninv="+inv);
+//System.out.println("\nres="+res);
+}
+/**
+ */
+public static void testMax() {
+ double[] temp = new double[2];
+
+ temp[0] = 8.9;
+ temp[1] = 1;
+
+ DenseDoubleMatrix1D d1Double = new DenseDoubleMatrix1D(temp);
+
+ // hep.aida.bin.DynamicBin1D d1ynamicBin = org.apache.mahout.colt.matrix.doublealgo.Statistic.bin(d1Double);
+
+ double max = 0;//d1ynamicBin.max();
+
+ System.out.println("max = "+ max);
+}
+}
\ No newline at end of file
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TestMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TridiagonalDoubleMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TridiagonalDoubleMatrix2D.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TridiagonalDoubleMatrix2D.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TridiagonalDoubleMatrix2D.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,510 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
+is hereby granted without fee, provided that the above copyright notice appear in all copies and
+that both that copyright notice and this permission notice appear in supporting documentation.
+CERN makes no representations about the suitability of this software for any purpose.
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.colt.matrix.impl;
+
+import org.apache.mahout.colt.matrix.DoubleMatrix1D;
+import org.apache.mahout.colt.matrix.DoubleMatrix2D;
+/**
+Tridiagonal 2-d matrix holding <tt>double</tt> elements.
+First see the <a href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the broad picture.
+<p>
+<b>Implementation:</b>
+TODO.
+
+@author wolfgang.hoschek@cern.ch
+@version 0.9, 04/14/2000
+*/
+class TridiagonalDoubleMatrix2D extends WrapperDoubleMatrix2D {
+ /*
+ * The non zero elements of the matrix: {lower, diagonal, upper}.
+ */
+ protected double[] values;
+
+ /*
+ * The startIndexes and number of non zeros: {lowerStart, diagonalStart, upperStart, values.length, lowerNonZeros, diagonalNonZeros, upperNonZeros}.
+ * lowerStart = 0
+ * diagonalStart = lowerStart + lower.length
+ * upperStart = diagonalStart + diagonal.length
+ */
+ protected int[] dims;
+
+ protected static final int NONZERO = 4;
+
+ //protected double diagonal[];
+ //protected double lower[];
+ //protected double upper[];
+
+ //protected int diagonalNonZeros;
+ //protected int lowerNonZeros;
+ //protected int upperNonZeros;
+ //protected int N;
+/**
+ * Constructs a matrix with a copy of the given values.
+ * <tt>values</tt> is required to have the form <tt>values[row][column]</tt>
+ * and have exactly the same number of columns in every row.
+ * <p>
+ * 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.
+ * @throws IllegalArgumentException if <tt>for any 1 <= row < values.length: values[row].length != values[row-1].length</tt>.
+ */
+public TridiagonalDoubleMatrix2D(double[][] values) {
+ this(values.length, values.length==0 ? 0: values[0].length);
+ assign(values);
+}
+/**
+ * Constructs a matrix with a given number of rows and columns.
+ * All entries are initially <tt>0</tt>.
+ * @param rows the number of rows the matrix shall have.
+ * @param columns the number of columns the matrix shall have.
+ * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt>.
+ */
+public TridiagonalDoubleMatrix2D(int rows, int columns) {
+ super(null);
+ setUp(rows, columns);
+
+ int d = Math.min(rows,columns);
+ int u = d-1;
+ int l = d-1;
+ if (rows>columns) l++;
+ if (rows<columns) u++;
+
+ values = new double[l+d+u]; // {lower, diagonal, upper}
+ int[] dimensions = { 0, l, l+d, l+d+u, 0, 0, 0}; // {lowerStart, diagonalStart, upperStart, values.length, lowerNonZeros, diagonalNonZeros, upperNonZeros}
+ dims = dimensions;
+
+ //diagonal = new double[d];
+ //lower = new double[l];
+ //upper = new double[u];
+
+ //diagonalNonZeros = 0;
+ //lowerNonZeros = 0;
+ //upperNonZeros = 0;
+}
+/**
+ * Sets all cells to the state specified by <tt>value</tt>.
+ * @param value the value to be filled into the cells.
+ * @return <tt>this</tt> (for convenience only).
+ */
+public DoubleMatrix2D assign(double value) {
+ // overriden for performance only
+ if (value==0) {
+ for (int i=values.length; --i >= 0; ) values[i]=0;
+ for (int i=dims.length; --i >= NONZERO; ) dims[i]=0;
+
+ //for (int i=diagonal.length; --i >= 0; ) diagonal[i]=0;
+ //for (int i=upper.length; --i >= 0; ) upper[i]=0;
+ //for (int i=lower.length; --i >= 0; ) lower[i]=0;
+
+ //diagonalNonZeros = 0;
+ //lowerNonZeros = 0;
+ //upperNonZeros = 0;
+ }
+ else super.assign(value);
+ return this;
+}
+public DoubleMatrix2D assign(final org.apache.mahout.colt.function.DoubleFunction function) {
+ if (function instanceof org.apache.mahout.jet.math.Mult) { // x[i] = mult*x[i]
+ final double alpha = ((org.apache.mahout.jet.math.Mult) function).multiplicator;
+ if (alpha==1) return this;
+ if (alpha==0) return assign(0);
+ if (alpha!=alpha) return assign(alpha); // the funny definition of isNaN(). This should better not happen.
+
+ /*
+ double[] vals = values.elements();
+ for (int j=values.size(); --j >= 0; ) {
+ vals[j] *= alpha;
+ }
+ */
+
+ forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ return function.apply(value);
+ }
+ }
+ );
+ }
+ else {
+ super.assign(function);
+ }
+ return this;
+}
+/**
+ * Replaces all cell values of the receiver with the values of another matrix.
+ * Both matrices must have the same number of 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>columns() != source.columns() || rows() != source.rows()</tt>
+ */
+public DoubleMatrix2D assign(DoubleMatrix2D source) {
+ // overriden for performance only
+ if (source==this) return this; // nothing to do
+ checkShape(source);
+
+ if (source instanceof TridiagonalDoubleMatrix2D) {
+ // quickest
+ TridiagonalDoubleMatrix2D other = (TridiagonalDoubleMatrix2D) source;
+
+ System.arraycopy(other.values, 0, this.values, 0, this.values.length);
+ System.arraycopy(other.dims, 0, this.dims, 0, this.dims.length);
+ return this;
+ }
+
+ if (source instanceof RCDoubleMatrix2D || source instanceof SparseDoubleMatrix2D) {
+ assign(0);
+ source.forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ setQuick(i,j,value);
+ return value;
+ }
+ }
+ );
+ return this;
+ }
+
+ return super.assign(source);
+}
+public DoubleMatrix2D assign(final DoubleMatrix2D y, org.apache.mahout.colt.function.DoubleDoubleFunction function) {
+ checkShape(y);
+
+ if (function instanceof org.apache.mahout.jet.math.PlusMult) { // x[i] = x[i] + alpha*y[i]
+ final double alpha = ((org.apache.mahout.jet.math.PlusMult) function).multiplicator;
+ if (alpha==0) return this; // nothing to do
+ y.forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ setQuick(i,j,getQuick(i,j) + alpha*value);
+ return value;
+ }
+ }
+ );
+ return this;
+ }
+
+ if (function== org.apache.mahout.jet.math.Functions.mult) { // x[i] = x[i] * y[i]
+ forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ setQuick(i,j,getQuick(i,j) * y.getQuick(i,j));
+ return value;
+ }
+ }
+ );
+ return this;
+ }
+
+ if (function== org.apache.mahout.jet.math.Functions.div) { // x[i] = x[i] / y[i]
+ forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ setQuick(i,j,getQuick(i,j) / y.getQuick(i,j));
+ return value;
+ }
+ }
+ );
+ return this;
+ }
+
+ return super.assign(y,function);
+}
+public DoubleMatrix2D forEachNonZero(final org.apache.mahout.colt.function.IntIntDoubleFunction function) {
+ for (int kind=0; kind<=2; kind++) {
+ int i=0,j=0;
+ switch (kind) {
+ case 0: { i=1; } // lower
+ // case 1: { } // diagonal
+ case 2: { j=1; } // upper
+ }
+ int low = dims[kind];
+ int high = dims[kind+1];
+
+ for (int k=low; k < high; k++, i++, j++) {
+ double value = values[k];
+ if (value!=0) {
+ double r = function.apply(i,j,value);
+ if (r!=value) {
+ if (r==0) dims[kind+NONZERO]++; // one non zero more
+ values[k] = r;
+ }
+ }
+ }
+ }
+ return this;
+}
+/**
+ * Returns the content of this matrix if it is a wrapper; or <tt>this</tt> otherwise.
+ * Override this method in wrappers.
+ */
+protected DoubleMatrix2D getContent() {
+ return this;
+}
+/**
+ * Returns the matrix cell value at coordinate <tt>[row,column]</tt>.
+ *
+ * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception.
+ * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b>
+ * Precondition (unchecked): <tt>0 <= column < columns() && 0 <= row < rows()</tt>.
+ *
+ * @param row the index of the row-coordinate.
+ * @param column the index of the column-coordinate.
+ * @return the value at the specified coordinate.
+ */
+public double getQuick(int row, int column) {
+ int i = row;
+ int j = column;
+
+ int k = j-i+1;
+ int q = i;
+ if (k==0) q=j; // lower diagonal
+
+ if (k>=0 && k<=2) {
+ return values[dims[k]+q];
+ }
+ return 0;
+
+
+
+
+
+ //int k = -1;
+ //int q = 0;
+
+ //if (i==j) { k=0; q=i; }
+ //if (i==j+1) { k=1; q=j; }
+ //if (i==j-1) { k=2; q=i; }
+
+ //if (k<0) return 0;
+ //return values[dims[k]+q];
+
+
+
+
+
+ //if (i==j) return diagonal[i];
+ //if (i==j+1) return lower[j];
+ //if (i==j-1) return upper[i];
+
+ //return 0;
+}
+/**
+ * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified number of rows and columns.
+ * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>,
+ * if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc.
+ * In general, the new matrix should have internal parametrization as similar as possible.
+ *
+ * @param rows the number of rows the matrix shall have.
+ * @param columns the number of columns the matrix shall have.
+ * @return a new empty matrix of the same dynamic type.
+ */
+public DoubleMatrix2D like(int rows, int columns) {
+ return new TridiagonalDoubleMatrix2D(rows,columns);
+}
+/**
+ * Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver.
+ * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must be of type <tt>DenseDoubleMatrix1D</tt>,
+ * if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix must be of type <tt>SparseDoubleMatrix1D</tt>, etc.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @return a new matrix of the corresponding dynamic type.
+ */
+public DoubleMatrix1D like1D(int size) {
+ return new SparseDoubleMatrix1D(size);
+}
+/**
+ * Sets the matrix cell at coordinate <tt>[row,column]</tt> to the specified value.
+ *
+ * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception.
+ * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b>
+ * Precondition (unchecked): <tt>0 <= column < columns() && 0 <= row < rows()</tt>.
+ *
+ * @param row the index of the row-coordinate.
+ * @param column the index of the column-coordinate.
+ * @param value the value to be filled into the specified cell.
+ */
+public void setQuick(int row, int column, double value) {
+ int i = row;
+ int j = column;
+
+ boolean isZero = (value==0);
+
+ int k = j-i+1;
+ int q = i;
+ if (k==0) q=j; // lower diagonal
+
+ if (k>=0 && k<=2) {
+ int index = dims[k]+q;
+ if (values[index]!=0) {
+ if (isZero) dims[k+NONZERO]--; // one nonZero less
+ }
+ else {
+ if (!isZero) dims[k+NONZERO]++; // one nonZero more
+ }
+ values[index] = value;
+ return;
+ }
+
+ if (!isZero) throw new IllegalArgumentException("Can't store non-zero value to non-tridiagonal coordinate: row="+row+", column="+column+", value="+value);
+
+ //int k = -1;
+ //int q = 0;
+
+ //if (i==j) { k=0; q=i; } // diagonal
+ //if (i==j+1) { k=1; q=j; } // lower diagonal
+ //if (i==j-1) { k=2; q=i; } // upper diagonal
+
+ //if (k>0) {
+ //int index = dims[k]+q;
+ //if (values[index]!=0) {
+ //if (isZero) dims[k+NONZERO]--; // one nonZero less
+ //}
+ //else {
+ //if (!isZero) dims[k+NONZERO]++; // one nonZero more
+ //}
+ //values[index] = value;
+ //return;
+ //}
+
+ //if (!isZero) throw new IllegalArgumentException("Can't store non-zero value to non-tridiagonal coordinate: row="+row+", column="+column+", value="+value);
+
+
+
+
+
+
+ //if (i==j) {
+ //if (diagonal[i]!=0) {
+ //if (isZero) diagonalNonZeros--;
+ //}
+ //else {
+ //if (!isZero) diagonalNonZeros++;
+ //}
+ //diagonal[i] = value;
+ //return;
+ //}
+
+ //if (i==j+1) {
+ //if (lower[j]!=0) {
+ //if (isZero) lowerNonZeros--;
+ //}
+ //else {
+ //if (!isZero) lowerNonZeros++;
+ //}
+ //lower[j] = value;
+ //return;
+ //}
+
+ //if (i==j-1) {
+ //if (upper[i]!=0) {
+ //if (isZero) upperNonZeros--;
+ //}
+ //else {
+ //if (!isZero) upperNonZeros++;
+ //}
+ //upper[i] = value;
+ //return;
+ //}
+
+ //if (!isZero) throw new IllegalArgumentException("Can't store non-zero value to non-tridiagonal coordinate: row="+row+", column="+column+", value="+value);
+}
+public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, final boolean transposeA) {
+ int m = rows;
+ int n = columns;
+ if (transposeA) {
+ m = columns;
+ n = rows;
+ }
+
+ boolean ignore = (z==null);
+ if (z==null) z = new DenseDoubleMatrix1D(m);
+
+ if (!(this.isNoView && y instanceof DenseDoubleMatrix1D && z instanceof DenseDoubleMatrix1D)) {
+ return super.zMult(y,z,alpha,beta,transposeA);
+ }
+
+ if (n != y.size() || m > z.size())
+ throw new IllegalArgumentException("Incompatible args: "+ ((transposeA ? viewDice() : this).toStringShort()) +", "+y.toStringShort()+", "+z.toStringShort());
+
+ if (!ignore) z.assign(org.apache.mahout.jet.math.Functions.mult(beta/alpha));
+
+ DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D) z;
+ final double[] zElements = zz.elements;
+ final int zStride = zz.stride;
+ final int zi = z.index(0);
+
+ DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D) y;
+ final double[] yElements = yy.elements;
+ final int yStride = yy.stride;
+ final int yi = y.index(0);
+
+ if (yElements==null || zElements==null) throw new InternalError();
+
+ forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ if (transposeA) { int tmp=i; i=j; j=tmp; }
+ zElements[zi + zStride*i] += value * yElements[yi + yStride*j];
+ //z.setQuick(row,z.getQuick(row) + value * y.getQuick(column));
+ //System.out.println("["+i+","+j+"]-->"+value);
+ return value;
+ }
+ }
+ );
+
+ if (alpha!=1) z.assign(org.apache.mahout.jet.math.Functions.mult(alpha));
+ return z;
+}
+public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, final double alpha, double beta, final boolean transposeA, boolean transposeB) {
+ if (transposeB) B = B.viewDice();
+ int m = rows;
+ int n = columns;
+ if (transposeA) {
+ m = columns;
+ n = rows;
+ }
+ int p = B.columns;
+ boolean ignore = (C==null);
+ if (C==null) C = new DenseDoubleMatrix2D(m,p);
+
+ if (B.rows != n)
+ throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort());
+ if (C.rows != m || C.columns != p)
+ throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort()+", "+C.toStringShort());
+ if (this == C || B == C)
+ throw new IllegalArgumentException("Matrices must not be identical");
+
+ if (!ignore) C.assign(org.apache.mahout.jet.math.Functions.mult(beta));
+
+ // cache views
+ final DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
+ for (int i=n; --i>=0; ) Brows[i] = B.viewRow(i);
+ final DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
+ for (int i=m; --i>=0; ) Crows[i] = C.viewRow(i);
+
+ final org.apache.mahout.jet.math.PlusMult fun = org.apache.mahout.jet.math.PlusMult.plusMult(0);
+
+ forEachNonZero(
+ new org.apache.mahout.colt.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ fun.multiplicator = value*alpha;
+ if (!transposeA)
+ Crows[i].assign(Brows[j],fun);
+ else
+ Crows[j].assign(Brows[i],fun);
+ return value;
+ }
+ }
+ );
+
+ return C;
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/TridiagonalDoubleMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/WrapperDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/WrapperDoubleMatrix1D.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/WrapperDoubleMatrix1D.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/WrapperDoubleMatrix1D.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,210 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
+is hereby granted without fee, provided that the above copyright notice appear in all copies and
+that both that copyright notice and this permission notice appear in supporting documentation.
+CERN makes no representations about the suitability of this software for any purpose.
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.colt.matrix.impl;
+
+import org.apache.mahout.colt.matrix.DoubleMatrix1D;
+import org.apache.mahout.colt.matrix.DoubleMatrix2D;
+/**
+1-d matrix holding <tt>double</tt> elements; either a view wrapping another matrix or a matrix whose views are wrappers.
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+class WrapperDoubleMatrix1D extends DoubleMatrix1D {
+ /*
+ * The elements of the matrix.
+ */
+ protected DoubleMatrix1D content;
+public WrapperDoubleMatrix1D(DoubleMatrix1D newContent) {
+ if (newContent != null) setUp(newContent.size());
+ this.content = newContent;
+}
+/**
+ * Returns the content of this matrix if it is a wrapper; or <tt>this</tt> otherwise.
+ * Override this method in wrappers.
+ */
+protected DoubleMatrix1D getContent() {
+ return this.content;
+}
+/**
+ * Returns the matrix cell value at coordinate <tt>index</tt>.
+ *
+ * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception.
+ * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b>
+ * Precondition (unchecked): <tt>index<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @return the value of the specified cell.
+ */
+public double getQuick(int index) {
+ return content.getQuick(index);
+}
+/**
+ * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified size.
+ * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix1D</tt>,
+ * if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix1D</tt>, etc.
+ * In general, the new matrix should have internal parametrization as similar as possible.
+ *
+ * @param size the number of cell the matrix shall have.
+ * @return a new empty matrix of the same dynamic type.
+ */
+public DoubleMatrix1D like(int size) {
+ return content.like(size);
+}
+/**
+ * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver.
+ * For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be of type <tt>DenseDoubleMatrix2D</tt>,
+ * if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new matrix must be of type <tt>SparseDoubleMatrix2D</tt>, etc.
+ *
+ * @param rows the number of rows the matrix shall have.
+ * @param columns the number of columns the matrix shall have.
+ * @return a new matrix of the corresponding dynamic type.
+ */
+public DoubleMatrix2D like2D(int rows, int columns) {
+ return content.like2D(rows,columns);
+}
+/**
+ * Sets the matrix cell at coordinate <tt>index</tt> to the specified value.
+ *
+ * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception.
+ * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b>
+ * Precondition (unchecked): <tt>index<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @param value the value to be filled into the specified cell.
+ */
+public void setQuick(int index, double value) {
+ content.setQuick(index, value);
+}
+/**
+Constructs and returns a new <i>flip view</i>.
+What used to be index <tt>0</tt> is now index <tt>size()-1</tt>, ..., what used to be index <tt>size()-1</tt> is now index <tt>0</tt>.
+The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
+
+@return a new flip view.
+*/
+public DoubleMatrix1D viewFlip() {
+ DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
+ public double getQuick(int index) {
+ return content.get(size-1-index);
+ }
+ public void setQuick(int index, double value) {
+ content.set(size-1-index,value);
+ }
+ };
+ return view;
+}
+/**
+Constructs and returns a new <i>sub-range view</i> that is a <tt>width</tt> sub matrix starting at <tt>index</tt>.
+
+Operations on the returned view can only be applied to the restricted range.
+Any attempt to access coordinates not contained in the view will throw an <tt>IndexOutOfBoundsException</tt>.
+<p>
+<b>Note that the view is really just a range restriction:</b>
+The returned matrix is backed by this matrix, so changes in the returned matrix are reflected in this matrix, and vice-versa.
+<p>
+The view contains the cells from <tt>index..index+width-1</tt>.
+and has <tt>view.size() == width</tt>.
+A view's legal coordinates are again zero based, as usual.
+In other words, legal coordinates of the view are <tt>0 .. view.size()-1==width-1</tt>.
+As usual, any attempt to access a cell at other coordinates will throw an <tt>IndexOutOfBoundsException</tt>.
+
+@param index The index of the first cell.
+@param width The width of the range.
+@throws IndexOutOfBoundsException if <tt>index<0 || width<0 || index+width>size()</tt>.
+@return the new view.
+
+*/
+public DoubleMatrix1D viewPart(final int index, int width) {
+ checkRange(index,width);
+ DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
+ public double getQuick(int i) {
+ return content.get(index+i);
+ }
+ public void setQuick(int i, double value) {
+ content.set(index+i,value);
+ }
+ };
+ view.size=width;
+ return view;
+}
+/**
+Constructs and returns a new <i>selection view</i> that is a matrix holding the indicated cells.
+There holds <tt>view.size() == indexes.length</tt> and <tt>view.get(i) == this.get(indexes[i])</tt>.
+Indexes can occur multiple times and can be in arbitrary order.
+<p>
+<b>Example:</b>
+<br>
+<pre>
+this = (0,0,8,0,7)
+indexes = (0,2,4,2)
+-->
+view = (0,8,7,8)
+</pre>
+Note that modifying <tt>indexes</tt> after this call has returned has no effect on the view.
+The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
+
+@param indexes The indexes of the cells that shall be visible in the new view. To indicate that <i>all</i> cells shall be visible, simply set this parameter to <tt>null</tt>.
+@return the new view.
+@throws IndexOutOfBoundsException if <tt>!(0 <= indexes[i] < size())</tt> for any <tt>i=0..indexes.length()-1</tt>.
+*/
+public DoubleMatrix1D viewSelection(int[] indexes) {
+ // check for "all"
+ if (indexes==null) {
+ indexes = new int[size];
+ for (int i=size; --i >= 0; ) indexes[i] = i;
+ }
+
+ checkIndexes(indexes);
+ final int[] idx = indexes;
+
+ DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
+ public double getQuick(int i) {
+ return content.get(idx[i]);
+ }
+ public void setQuick(int i, double value) {
+ content.set(idx[i],value);
+ }
+ };
+ view.size=indexes.length;
+ return view;
+}
+/**
+ * Construct and returns a new selection view.
+ *
+ * @param offsets the offsets of the visible elements.
+ * @return a new view.
+ */
+protected DoubleMatrix1D viewSelectionLike(int[] offsets) {
+ throw new InternalError(); // should never get called
+}
+/**
+Constructs and returns a new <i>stride view</i> which is a sub matrix consisting of every i-th cell.
+More specifically, the view has size <tt>this.size()/stride</tt> holding cells <tt>this.get(i*stride)</tt> for all <tt>i = 0..size()/stride - 1</tt>.
+
+@param stride the step factor.
+@throws IndexOutOfBoundsException if <tt>stride <= 0</tt>.
+@return the new view.
+
+*/
+public DoubleMatrix1D viewStrides(final int _stride) {
+ if (stride<=0) throw new IndexOutOfBoundsException("illegal stride: "+stride);
+ DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
+ public double getQuick(int index) {
+ return content.get(index*_stride);
+ }
+ public void setQuick(int index, double value) {
+ content.set(index*_stride,value);
+ }
+ };
+ view.size = size;
+ if (size!=0) view.size = (size-1)/_stride +1;
+ return view;
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/WrapperDoubleMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native