You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2009/11/25 04:41:31 UTC
svn commit: r883974 [19/20] - in
/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix:
./ bench/ doublealgo/ impl/ linalg/ objectalgo/
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SingularValueDecomposition.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SingularValueDecomposition.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SingularValueDecomposition.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SingularValueDecomposition.java Wed Nov 25 03:41:28 2009
@@ -27,23 +27,23 @@
*/
@Deprecated
public class SingularValueDecomposition implements java.io.Serializable {
- static final long serialVersionUID = 1020;
- /** Arrays for internal storage of U and V.
- @serial internal storage of U.
- @serial internal storage of V.
- */
- private double[][] U, V;
-
- /** Array for internal storage of singular values.
- @serial internal storage of singular values.
- */
- private double[] s;
-
- /** Row and column dimensions.
- @serial row dimension.
- @serial column dimension.
- */
- private int m, n;
+ static final long serialVersionUID = 1020;
+ /** Arrays for internal storage of U and V.
+ @serial internal storage of U.
+ @serial internal storage of V.
+ */
+ private double[][] U, V;
+
+ /** Array for internal storage of singular values.
+ @serial internal storage of singular values.
+ */
+ private double[] s;
+
+ /** Row and column dimensions.
+ @serial row dimension.
+ @serial column dimension.
+ */
+ private int m, n;
/**
Constructs and returns a new singular value decomposition object;
The decomposed matrices can be retrieved via instance methods of the returned decomposition object.
@@ -52,480 +52,480 @@
@throws IllegalArgumentException if <tt>A.rows() < A.columns()</tt>.
*/
public SingularValueDecomposition(DoubleMatrix2D Arg) {
- Property.DEFAULT.checkRectangular(Arg);
+ Property.DEFAULT.checkRectangular(Arg);
- // Derived from LINPACK code.
- // Initialize.
- double[][] A = Arg.toArray();
- m = Arg.rows();
- n = Arg.columns();
- int nu = Math.min(m,n);
- s = new double [Math.min(m+1,n)];
- U = new double [m][nu];
- V = new double [n][n];
- double[] e = new double [n];
- double[] work = new double [m];
- boolean wantu = true;
- boolean wantv = true;
-
- // Reduce A to bidiagonal form, storing the diagonal elements
- // in s and the super-diagonal elements in e.
-
- int nct = Math.min(m-1,n);
- int nrt = Math.max(0,Math.min(n-2,m));
- for (int k = 0; k < Math.max(nct,nrt); k++) {
- if (k < nct) {
-
- // Compute the transformation for the k-th column and
- // place the k-th diagonal in s[k].
- // Compute 2-norm of k-th column without under/overflow.
- s[k] = 0;
- for (int i = k; i < m; i++) {
- s[k] = Algebra.hypot(s[k],A[i][k]);
- }
- if (s[k] != 0.0) {
- if (A[k][k] < 0.0) {
- s[k] = -s[k];
- }
- for (int i = k; i < m; i++) {
- A[i][k] /= s[k];
- }
- A[k][k] += 1.0;
- }
- s[k] = -s[k];
- }
- for (int j = k+1; j < n; j++) {
- if ((k < nct) & (s[k] != 0.0)) {
-
- // Apply the transformation.
-
- double t = 0;
- for (int i = k; i < m; i++) {
- t += A[i][k]*A[i][j];
- }
- t = -t/A[k][k];
- for (int i = k; i < m; i++) {
- A[i][j] += t*A[i][k];
- }
- }
-
- // Place the k-th row of A into e for the
- // subsequent calculation of the row transformation.
-
- e[j] = A[k][j];
- }
- if (wantu & (k < nct)) {
-
- // Place the transformation in U for subsequent back
- // multiplication.
-
- for (int i = k; i < m; i++) {
- U[i][k] = A[i][k];
- }
- }
- if (k < nrt) {
-
- // Compute the k-th row transformation and place the
- // k-th super-diagonal in e[k].
- // Compute 2-norm without under/overflow.
- e[k] = 0;
- for (int i = k+1; i < n; i++) {
- e[k] = Algebra.hypot(e[k],e[i]);
- }
- if (e[k] != 0.0) {
- if (e[k+1] < 0.0) {
- e[k] = -e[k];
- }
- for (int i = k+1; i < n; i++) {
- e[i] /= e[k];
- }
- e[k+1] += 1.0;
- }
- e[k] = -e[k];
- if ((k+1 < m) & (e[k] != 0.0)) {
-
- // Apply the transformation.
-
- for (int i = k+1; i < m; i++) {
- work[i] = 0.0;
- }
- for (int j = k+1; j < n; j++) {
- for (int i = k+1; i < m; i++) {
- work[i] += e[j]*A[i][j];
- }
- }
- for (int j = k+1; j < n; j++) {
- double t = -e[j]/e[k+1];
- for (int i = k+1; i < m; i++) {
- A[i][j] += t*work[i];
- }
- }
- }
- if (wantv) {
-
- // Place the transformation in V for subsequent
- // back multiplication.
-
- for (int i = k+1; i < n; i++) {
- V[i][k] = e[i];
- }
- }
- }
- }
-
- // Set up the final bidiagonal matrix or order p.
-
- int p = Math.min(n,m+1);
- if (nct < n) {
- s[nct] = A[nct][nct];
- }
- if (m < p) {
- s[p-1] = 0.0;
- }
- if (nrt+1 < p) {
- e[nrt] = A[nrt][p-1];
- }
- e[p-1] = 0.0;
-
- // If required, generate U.
-
- if (wantu) {
- for (int j = nct; j < nu; j++) {
- for (int i = 0; i < m; i++) {
- U[i][j] = 0.0;
- }
- U[j][j] = 1.0;
- }
- for (int k = nct-1; k >= 0; k--) {
- if (s[k] != 0.0) {
- for (int j = k+1; j < nu; j++) {
- double t = 0;
- for (int i = k; i < m; i++) {
- t += U[i][k]*U[i][j];
- }
- t = -t/U[k][k];
- for (int i = k; i < m; i++) {
- U[i][j] += t*U[i][k];
- }
- }
- for (int i = k; i < m; i++ ) {
- U[i][k] = -U[i][k];
- }
- U[k][k] = 1.0 + U[k][k];
- for (int i = 0; i < k-1; i++) {
- U[i][k] = 0.0;
- }
- } else {
- for (int i = 0; i < m; i++) {
- U[i][k] = 0.0;
- }
- U[k][k] = 1.0;
- }
- }
- }
-
- // If required, generate V.
-
- if (wantv) {
- for (int k = n-1; k >= 0; k--) {
- if ((k < nrt) & (e[k] != 0.0)) {
- for (int j = k+1; j < nu; j++) {
- double t = 0;
- for (int i = k+1; i < n; i++) {
- t += V[i][k]*V[i][j];
- }
- t = -t/V[k+1][k];
- for (int i = k+1; i < n; i++) {
- V[i][j] += t*V[i][k];
- }
- }
- }
- for (int i = 0; i < n; i++) {
- V[i][k] = 0.0;
- }
- V[k][k] = 1.0;
- }
- }
-
- // Main iteration loop for the singular values.
-
- int pp = p-1;
- int iter = 0;
- double eps = Math.pow(2.0,-52.0);
- while (p > 0) {
- int k,kase;
-
- // Here is where a test for too many iterations would go.
-
- // This section of the program inspects for
- // negligible elements in the s and e arrays. On
- // completion the variables kase and k are set as follows.
-
- // kase = 1 if s(p) and e[k-1] are negligible and k<p
- // kase = 2 if s(k) is negligible and k<p
- // kase = 3 if e[k-1] is negligible, k<p, and
- // s(k), ..., s(p) are not negligible (qr step).
- // kase = 4 if e(p-1) is negligible (convergence).
-
- for (k = p-2; k >= -1; k--) {
- if (k == -1) {
- break;
- }
- if (Math.abs(e[k]) <= eps*(Math.abs(s[k]) + Math.abs(s[k+1]))) {
- e[k] = 0.0;
- break;
- }
- }
- if (k == p-2) {
- kase = 4;
- } else {
- int ks;
- for (ks = p-1; ks >= k; ks--) {
- if (ks == k) {
- break;
- }
- double t = (ks != p ? Math.abs(e[ks]) : 0.) +
- (ks != k+1 ? Math.abs(e[ks-1]) : 0.);
- if (Math.abs(s[ks]) <= eps*t) {
- s[ks] = 0.0;
- break;
- }
- }
- if (ks == k) {
- kase = 3;
- } else if (ks == p-1) {
- kase = 1;
- } else {
- kase = 2;
- k = ks;
- }
- }
- k++;
-
- // Perform the task indicated by kase.
-
- switch (kase) {
-
- // Deflate negligible s(p).
-
- case 1: {
- double f = e[p-2];
- e[p-2] = 0.0;
- for (int j = p-2; j >= k; j--) {
- double t = Algebra.hypot(s[j],f);
- double cs = s[j]/t;
- double sn = f/t;
- s[j] = t;
- if (j != k) {
- f = -sn*e[j-1];
- e[j-1] = cs*e[j-1];
- }
- if (wantv) {
- for (int i = 0; i < n; i++) {
- t = cs*V[i][j] + sn*V[i][p-1];
- V[i][p-1] = -sn*V[i][j] + cs*V[i][p-1];
- V[i][j] = t;
- }
- }
- }
- }
- break;
-
- // Split at negligible s(k).
-
- case 2: {
- double f = e[k-1];
- e[k-1] = 0.0;
- for (int j = k; j < p; j++) {
- double t = Algebra.hypot(s[j],f);
- double cs = s[j]/t;
- double sn = f/t;
- s[j] = t;
- f = -sn*e[j];
- e[j] = cs*e[j];
- if (wantu) {
- for (int i = 0; i < m; i++) {
- t = cs*U[i][j] + sn*U[i][k-1];
- U[i][k-1] = -sn*U[i][j] + cs*U[i][k-1];
- U[i][j] = t;
- }
- }
- }
- }
- break;
+ // Derived from LINPACK code.
+ // Initialize.
+ double[][] A = Arg.toArray();
+ m = Arg.rows();
+ n = Arg.columns();
+ int nu = Math.min(m,n);
+ s = new double [Math.min(m+1,n)];
+ U = new double [m][nu];
+ V = new double [n][n];
+ double[] e = new double [n];
+ double[] work = new double [m];
+ boolean wantu = true;
+ boolean wantv = true;
+
+ // Reduce A to bidiagonal form, storing the diagonal elements
+ // in s and the super-diagonal elements in e.
+
+ int nct = Math.min(m-1,n);
+ int nrt = Math.max(0,Math.min(n-2,m));
+ for (int k = 0; k < Math.max(nct,nrt); k++) {
+ if (k < nct) {
+
+ // Compute the transformation for the k-th column and
+ // place the k-th diagonal in s[k].
+ // Compute 2-norm of k-th column without under/overflow.
+ s[k] = 0;
+ for (int i = k; i < m; i++) {
+ s[k] = Algebra.hypot(s[k],A[i][k]);
+ }
+ if (s[k] != 0.0) {
+ if (A[k][k] < 0.0) {
+ s[k] = -s[k];
+ }
+ for (int i = k; i < m; i++) {
+ A[i][k] /= s[k];
+ }
+ A[k][k] += 1.0;
+ }
+ s[k] = -s[k];
+ }
+ for (int j = k+1; j < n; j++) {
+ if ((k < nct) & (s[k] != 0.0)) {
+
+ // Apply the transformation.
+
+ double t = 0;
+ for (int i = k; i < m; i++) {
+ t += A[i][k]*A[i][j];
+ }
+ t = -t/A[k][k];
+ for (int i = k; i < m; i++) {
+ A[i][j] += t*A[i][k];
+ }
+ }
+
+ // Place the k-th row of A into e for the
+ // subsequent calculation of the row transformation.
+
+ e[j] = A[k][j];
+ }
+ if (wantu & (k < nct)) {
+
+ // Place the transformation in U for subsequent back
+ // multiplication.
+
+ for (int i = k; i < m; i++) {
+ U[i][k] = A[i][k];
+ }
+ }
+ if (k < nrt) {
+
+ // Compute the k-th row transformation and place the
+ // k-th super-diagonal in e[k].
+ // Compute 2-norm without under/overflow.
+ e[k] = 0;
+ for (int i = k+1; i < n; i++) {
+ e[k] = Algebra.hypot(e[k],e[i]);
+ }
+ if (e[k] != 0.0) {
+ if (e[k+1] < 0.0) {
+ e[k] = -e[k];
+ }
+ for (int i = k+1; i < n; i++) {
+ e[i] /= e[k];
+ }
+ e[k+1] += 1.0;
+ }
+ e[k] = -e[k];
+ if ((k+1 < m) & (e[k] != 0.0)) {
+
+ // Apply the transformation.
+
+ for (int i = k+1; i < m; i++) {
+ work[i] = 0.0;
+ }
+ for (int j = k+1; j < n; j++) {
+ for (int i = k+1; i < m; i++) {
+ work[i] += e[j]*A[i][j];
+ }
+ }
+ for (int j = k+1; j < n; j++) {
+ double t = -e[j]/e[k+1];
+ for (int i = k+1; i < m; i++) {
+ A[i][j] += t*work[i];
+ }
+ }
+ }
+ if (wantv) {
+
+ // Place the transformation in V for subsequent
+ // back multiplication.
+
+ for (int i = k+1; i < n; i++) {
+ V[i][k] = e[i];
+ }
+ }
+ }
+ }
+
+ // Set up the final bidiagonal matrix or order p.
+
+ int p = Math.min(n,m+1);
+ if (nct < n) {
+ s[nct] = A[nct][nct];
+ }
+ if (m < p) {
+ s[p-1] = 0.0;
+ }
+ if (nrt+1 < p) {
+ e[nrt] = A[nrt][p-1];
+ }
+ e[p-1] = 0.0;
+
+ // If required, generate U.
+
+ if (wantu) {
+ for (int j = nct; j < nu; j++) {
+ for (int i = 0; i < m; i++) {
+ U[i][j] = 0.0;
+ }
+ U[j][j] = 1.0;
+ }
+ for (int k = nct-1; k >= 0; k--) {
+ if (s[k] != 0.0) {
+ for (int j = k+1; j < nu; j++) {
+ double t = 0;
+ for (int i = k; i < m; i++) {
+ t += U[i][k]*U[i][j];
+ }
+ t = -t/U[k][k];
+ for (int i = k; i < m; i++) {
+ U[i][j] += t*U[i][k];
+ }
+ }
+ for (int i = k; i < m; i++ ) {
+ U[i][k] = -U[i][k];
+ }
+ U[k][k] = 1.0 + U[k][k];
+ for (int i = 0; i < k-1; i++) {
+ U[i][k] = 0.0;
+ }
+ } else {
+ for (int i = 0; i < m; i++) {
+ U[i][k] = 0.0;
+ }
+ U[k][k] = 1.0;
+ }
+ }
+ }
+
+ // If required, generate V.
+
+ if (wantv) {
+ for (int k = n-1; k >= 0; k--) {
+ if ((k < nrt) & (e[k] != 0.0)) {
+ for (int j = k+1; j < nu; j++) {
+ double t = 0;
+ for (int i = k+1; i < n; i++) {
+ t += V[i][k]*V[i][j];
+ }
+ t = -t/V[k+1][k];
+ for (int i = k+1; i < n; i++) {
+ V[i][j] += t*V[i][k];
+ }
+ }
+ }
+ for (int i = 0; i < n; i++) {
+ V[i][k] = 0.0;
+ }
+ V[k][k] = 1.0;
+ }
+ }
+
+ // Main iteration loop for the singular values.
+
+ int pp = p-1;
+ int iter = 0;
+ double eps = Math.pow(2.0,-52.0);
+ while (p > 0) {
+ int k,kase;
+
+ // Here is where a test for too many iterations would go.
+
+ // This section of the program inspects for
+ // negligible elements in the s and e arrays. On
+ // completion the variables kase and k are set as follows.
+
+ // kase = 1 if s(p) and e[k-1] are negligible and k<p
+ // kase = 2 if s(k) is negligible and k<p
+ // kase = 3 if e[k-1] is negligible, k<p, and
+ // s(k), ..., s(p) are not negligible (qr step).
+ // kase = 4 if e(p-1) is negligible (convergence).
+
+ for (k = p-2; k >= -1; k--) {
+ if (k == -1) {
+ break;
+ }
+ if (Math.abs(e[k]) <= eps*(Math.abs(s[k]) + Math.abs(s[k+1]))) {
+ e[k] = 0.0;
+ break;
+ }
+ }
+ if (k == p-2) {
+ kase = 4;
+ } else {
+ int ks;
+ for (ks = p-1; ks >= k; ks--) {
+ if (ks == k) {
+ break;
+ }
+ double t = (ks != p ? Math.abs(e[ks]) : 0.) +
+ (ks != k+1 ? Math.abs(e[ks-1]) : 0.);
+ if (Math.abs(s[ks]) <= eps*t) {
+ s[ks] = 0.0;
+ break;
+ }
+ }
+ if (ks == k) {
+ kase = 3;
+ } else if (ks == p-1) {
+ kase = 1;
+ } else {
+ kase = 2;
+ k = ks;
+ }
+ }
+ k++;
+
+ // Perform the task indicated by kase.
+
+ switch (kase) {
+
+ // Deflate negligible s(p).
+
+ case 1: {
+ double f = e[p-2];
+ e[p-2] = 0.0;
+ for (int j = p-2; j >= k; j--) {
+ double t = Algebra.hypot(s[j],f);
+ double cs = s[j]/t;
+ double sn = f/t;
+ s[j] = t;
+ if (j != k) {
+ f = -sn*e[j-1];
+ e[j-1] = cs*e[j-1];
+ }
+ if (wantv) {
+ for (int i = 0; i < n; i++) {
+ t = cs*V[i][j] + sn*V[i][p-1];
+ V[i][p-1] = -sn*V[i][j] + cs*V[i][p-1];
+ V[i][j] = t;
+ }
+ }
+ }
+ }
+ break;
+
+ // Split at negligible s(k).
+
+ case 2: {
+ double f = e[k-1];
+ e[k-1] = 0.0;
+ for (int j = k; j < p; j++) {
+ double t = Algebra.hypot(s[j],f);
+ double cs = s[j]/t;
+ double sn = f/t;
+ s[j] = t;
+ f = -sn*e[j];
+ e[j] = cs*e[j];
+ if (wantu) {
+ for (int i = 0; i < m; i++) {
+ t = cs*U[i][j] + sn*U[i][k-1];
+ U[i][k-1] = -sn*U[i][j] + cs*U[i][k-1];
+ U[i][j] = t;
+ }
+ }
+ }
+ }
+ break;
- // Perform one qr step.
+ // Perform one qr step.
- case 3: {
+ case 3: {
- // Calculate the shift.
+ // Calculate the shift.
- double scale = Math.max(Math.max(Math.max(Math.max(
- Math.abs(s[p-1]),Math.abs(s[p-2])),Math.abs(e[p-2])),
- Math.abs(s[k])),Math.abs(e[k]));
- double sp = s[p-1]/scale;
- double spm1 = s[p-2]/scale;
- double epm1 = e[p-2]/scale;
- double sk = s[k]/scale;
- double ek = e[k]/scale;
- double b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/2.0;
- double c = (sp*epm1)*(sp*epm1);
- double shift = 0.0;
- if ((b != 0.0) | (c != 0.0)) {
- shift = Math.sqrt(b*b + c);
- if (b < 0.0) {
- shift = -shift;
- }
- shift = c/(b + shift);
- }
- double f = (sk + sp)*(sk - sp) + shift;
- double g = sk*ek;
+ double scale = Math.max(Math.max(Math.max(Math.max(
+ Math.abs(s[p-1]),Math.abs(s[p-2])),Math.abs(e[p-2])),
+ Math.abs(s[k])),Math.abs(e[k]));
+ double sp = s[p-1]/scale;
+ double spm1 = s[p-2]/scale;
+ double epm1 = e[p-2]/scale;
+ double sk = s[k]/scale;
+ double ek = e[k]/scale;
+ double b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/2.0;
+ double c = (sp*epm1)*(sp*epm1);
+ double shift = 0.0;
+ if ((b != 0.0) | (c != 0.0)) {
+ shift = Math.sqrt(b*b + c);
+ if (b < 0.0) {
+ shift = -shift;
+ }
+ shift = c/(b + shift);
+ }
+ double f = (sk + sp)*(sk - sp) + shift;
+ double g = sk*ek;
- // Chase zeros.
+ // Chase zeros.
- for (int j = k; j < p-1; j++) {
- double t = Algebra.hypot(f,g);
- double cs = f/t;
- double sn = g/t;
- if (j != k) {
- e[j-1] = t;
- }
- f = cs*s[j] + sn*e[j];
- e[j] = cs*e[j] - sn*s[j];
- g = sn*s[j+1];
- s[j+1] = cs*s[j+1];
- if (wantv) {
- for (int i = 0; i < n; i++) {
- t = cs*V[i][j] + sn*V[i][j+1];
- V[i][j+1] = -sn*V[i][j] + cs*V[i][j+1];
- V[i][j] = t;
- }
- }
- t = Algebra.hypot(f,g);
- cs = f/t;
- sn = g/t;
- s[j] = t;
- f = cs*e[j] + sn*s[j+1];
- s[j+1] = -sn*e[j] + cs*s[j+1];
- g = sn*e[j+1];
- e[j+1] = cs*e[j+1];
- if (wantu && (j < m-1)) {
- for (int i = 0; i < m; i++) {
- t = cs*U[i][j] + sn*U[i][j+1];
- U[i][j+1] = -sn*U[i][j] + cs*U[i][j+1];
- U[i][j] = t;
- }
- }
- }
- e[p-2] = f;
- iter = iter + 1;
- }
- break;
+ for (int j = k; j < p-1; j++) {
+ double t = Algebra.hypot(f,g);
+ double cs = f/t;
+ double sn = g/t;
+ if (j != k) {
+ e[j-1] = t;
+ }
+ f = cs*s[j] + sn*e[j];
+ e[j] = cs*e[j] - sn*s[j];
+ g = sn*s[j+1];
+ s[j+1] = cs*s[j+1];
+ if (wantv) {
+ for (int i = 0; i < n; i++) {
+ t = cs*V[i][j] + sn*V[i][j+1];
+ V[i][j+1] = -sn*V[i][j] + cs*V[i][j+1];
+ V[i][j] = t;
+ }
+ }
+ t = Algebra.hypot(f,g);
+ cs = f/t;
+ sn = g/t;
+ s[j] = t;
+ f = cs*e[j] + sn*s[j+1];
+ s[j+1] = -sn*e[j] + cs*s[j+1];
+ g = sn*e[j+1];
+ e[j+1] = cs*e[j+1];
+ if (wantu && (j < m-1)) {
+ for (int i = 0; i < m; i++) {
+ t = cs*U[i][j] + sn*U[i][j+1];
+ U[i][j+1] = -sn*U[i][j] + cs*U[i][j+1];
+ U[i][j] = t;
+ }
+ }
+ }
+ e[p-2] = f;
+ iter = iter + 1;
+ }
+ break;
- // Convergence.
+ // Convergence.
- case 4: {
+ case 4: {
- // Make the singular values positive.
+ // Make the singular values positive.
- if (s[k] <= 0.0) {
- s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
- if (wantv) {
- for (int i = 0; i <= pp; i++) {
- V[i][k] = -V[i][k];
- }
- }
- }
+ if (s[k] <= 0.0) {
+ s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
+ if (wantv) {
+ for (int i = 0; i <= pp; i++) {
+ V[i][k] = -V[i][k];
+ }
+ }
+ }
- // Order the singular values.
+ // Order the singular values.
- while (k < pp) {
- if (s[k] >= s[k+1]) {
- break;
- }
- double t = s[k];
- s[k] = s[k+1];
- s[k+1] = t;
- if (wantv && (k < n-1)) {
- for (int i = 0; i < n; i++) {
- t = V[i][k+1]; V[i][k+1] = V[i][k]; V[i][k] = t;
- }
- }
- if (wantu && (k < m-1)) {
- for (int i = 0; i < m; i++) {
- t = U[i][k+1]; U[i][k+1] = U[i][k]; U[i][k] = t;
- }
- }
- k++;
- }
- iter = 0;
- p--;
- }
- break;
- }
- }
+ while (k < pp) {
+ if (s[k] >= s[k+1]) {
+ break;
+ }
+ double t = s[k];
+ s[k] = s[k+1];
+ s[k+1] = t;
+ if (wantv && (k < n-1)) {
+ for (int i = 0; i < n; i++) {
+ t = V[i][k+1]; V[i][k+1] = V[i][k]; V[i][k] = t;
+ }
+ }
+ if (wantu && (k < m-1)) {
+ for (int i = 0; i < m; i++) {
+ t = U[i][k+1]; U[i][k+1] = U[i][k]; U[i][k] = t;
+ }
+ }
+ k++;
+ }
+ iter = 0;
+ p--;
+ }
+ break;
+ }
+ }
}
/**
Returns the two norm condition number, which is <tt>max(S) / min(S)</tt>.
*/
public double cond() {
- return s[0]/s[Math.min(m,n)-1];
+ return s[0]/s[Math.min(m,n)-1];
}
/**
Returns the diagonal matrix of singular values.
@return S
*/
public DoubleMatrix2D getS() {
- double[][] S = new double[n][n];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- S[i][j] = 0.0;
- }
- S[i][i] = this.s[i];
- }
- return DoubleFactory2D.dense.make(S);
+ double[][] S = new double[n][n];
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < n; j++) {
+ S[i][j] = 0.0;
+ }
+ S[i][i] = this.s[i];
+ }
+ return DoubleFactory2D.dense.make(S);
}
/**
Returns the diagonal of <tt>S</tt>, which is a one-dimensional array of singular values
@return diagonal of <tt>S</tt>.
*/
public double[] getSingularValues() {
- return s;
+ return s;
}
/**
Returns the left singular vectors <tt>U</tt>.
@return <tt>U</tt>
*/
public DoubleMatrix2D getU() {
- //return new DoubleMatrix2D(U,m,Math.min(m+1,n));
- return DoubleFactory2D.dense.make(U).viewPart(0,0,m,Math.min(m+1,n));
+ //return new DoubleMatrix2D(U,m,Math.min(m+1,n));
+ return DoubleFactory2D.dense.make(U).viewPart(0,0,m,Math.min(m+1,n));
}
/**
Returns the right singular vectors <tt>V</tt>.
@return <tt>V</tt>
*/
public DoubleMatrix2D getV() {
- return DoubleFactory2D.dense.make(V);
+ return DoubleFactory2D.dense.make(V);
}
/**
Returns the two norm, which is <tt>max(S)</tt>.
*/
public double norm2() {
- return s[0];
+ return s[0];
}
/**
Returns the effective numerical matrix rank, which is the number of nonnegligible singular values.
*/
public int rank() {
- double eps = Math.pow(2.0,-52.0);
- double tol = Math.max(m,n)*s[0]*eps;
- int r = 0;
- for (int i = 0; i < s.length; i++) {
- if (s[i] > tol) {
- r++;
- }
- }
- return r;
+ double eps = Math.pow(2.0,-52.0);
+ double tol = Math.max(m,n)*s[0]*eps;
+ int r = 0;
+ for (int i = 0; i < s.length; i++) {
+ if (s[i] > tol) {
+ r++;
+ }
+ }
+ return r;
}
/**
Returns a String with (propertyName, propertyValue) pairs.
@@ -537,37 +537,37 @@
</pre>
*/
public String toString() {
- StringBuffer buf = new StringBuffer();
- String unknown = "Illegal operation or error: ";
+ StringBuffer buf = new StringBuffer();
+ String unknown = "Illegal operation or error: ";
- buf.append("---------------------------------------------------------------------\n");
- buf.append("SingularValueDecomposition(A) --> cond(A), rank(A), norm2(A), U, S, V\n");
- buf.append("---------------------------------------------------------------------\n");
-
- buf.append("cond = ");
- try { buf.append(String.valueOf(this.cond()));}
- catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
-
- buf.append("\nrank = ");
- try { buf.append(String.valueOf(this.rank()));}
- catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
-
- buf.append("\nnorm2 = ");
- try { buf.append(String.valueOf(this.norm2()));}
- catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
-
- buf.append("\n\nU = ");
- try { buf.append(String.valueOf(this.getU()));}
- catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
-
- buf.append("\n\nS = ");
- try { buf.append(String.valueOf(this.getS()));}
- catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
-
- buf.append("\n\nV = ");
- try { buf.append(String.valueOf(this.getV()));}
- catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
-
- return buf.toString();
+ buf.append("---------------------------------------------------------------------\n");
+ buf.append("SingularValueDecomposition(A) --> cond(A), rank(A), norm2(A), U, S, V\n");
+ buf.append("---------------------------------------------------------------------\n");
+
+ buf.append("cond = ");
+ try { buf.append(String.valueOf(this.cond()));}
+ catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
+
+ buf.append("\nrank = ");
+ try { buf.append(String.valueOf(this.rank()));}
+ catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
+
+ buf.append("\nnorm2 = ");
+ try { buf.append(String.valueOf(this.norm2()));}
+ catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
+
+ buf.append("\n\nU = ");
+ try { buf.append(String.valueOf(this.getU()));}
+ catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
+
+ buf.append("\n\nS = ");
+ try { buf.append(String.valueOf(this.getS()));}
+ catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
+
+ buf.append("\n\nV = ");
+ try { buf.append(String.valueOf(this.getV()));}
+ catch (IllegalArgumentException exc) { buf.append(unknown+exc.getMessage()); }
+
+ return buf.toString();
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/Smp.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/Smp.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/Smp.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/Smp.java Wed Nov 25 03:41:28 2009
@@ -14,182 +14,182 @@
/*
*/
class Smp {
- protected FJTaskRunnerGroup taskGroup; // a very efficient and light weight thread pool
+ protected FJTaskRunnerGroup taskGroup; // a very efficient and light weight thread pool
- protected int maxThreads;
+ protected int maxThreads;
/**
Constructs a new Smp using a maximum of <tt>maxThreads<tt> threads.
*/
protected Smp(int maxThreads) {
- maxThreads = Math.max(1,maxThreads);
- this.maxThreads = maxThreads;
- if (maxThreads>1) {
- this.taskGroup = new FJTaskRunnerGroup(maxThreads);
- }
- else { // avoid parallel overhead
- this.taskGroup = null;
- }
+ maxThreads = Math.max(1,maxThreads);
+ this.maxThreads = maxThreads;
+ if (maxThreads>1) {
+ this.taskGroup = new FJTaskRunnerGroup(maxThreads);
+ }
+ else { // avoid parallel overhead
+ this.taskGroup = null;
+ }
}
/**
* Clean up deamon threads, if necessary.
*/
public void finalize() {
- if (this.taskGroup!=null) this.taskGroup.interruptAll();
+ if (this.taskGroup!=null) this.taskGroup.interruptAll();
}
protected void run(final DoubleMatrix2D[] blocksA, final DoubleMatrix2D[] blocksB, final double[] results, final Matrix2DMatrix2DFunction function) {
- final FJTask[] subTasks = new FJTask[blocksA.length];
- for (int i=0; i<blocksA.length; i++) {
- final int k = i;
- subTasks[i] = new FJTask() {
- public void run() {
- double result = function.apply(blocksA[k],blocksB != null ? blocksB[k] : null);
- if (results!=null) results[k] = result;
- //System.out.print(".");
- }
- };
- }
-
- // run tasks and wait for completion
- try {
- this.taskGroup.invoke(
- new FJTask() {
- public void run() {
- coInvoke(subTasks);
- }
- }
- );
- } catch (InterruptedException exc) {}
+ final FJTask[] subTasks = new FJTask[blocksA.length];
+ for (int i=0; i<blocksA.length; i++) {
+ final int k = i;
+ subTasks[i] = new FJTask() {
+ public void run() {
+ double result = function.apply(blocksA[k],blocksB != null ? blocksB[k] : null);
+ if (results!=null) results[k] = result;
+ //System.out.print(".");
+ }
+ };
+ }
+
+ // run tasks and wait for completion
+ try {
+ this.taskGroup.invoke(
+ new FJTask() {
+ public void run() {
+ coInvoke(subTasks);
+ }
+ }
+ );
+ } catch (InterruptedException exc) {}
}
protected DoubleMatrix2D[] splitBlockedNN(DoubleMatrix2D A, int threshold, long flops) {
- /*
- determine how to split and parallelize best into blocks
- if more B.columns than tasks --> split B.columns, as follows:
-
- xx|xx|xxx B
- xx|xx|xxx
- xx|xx|xxx
- A
- xxx xx|xx|xxx C
- xxx xx|xx|xxx
- xxx xx|xx|xxx
- xxx xx|xx|xxx
- xxx xx|xx|xxx
-
- if less B.columns than tasks --> split A.rows, as follows:
-
- xxxxxxx B
- xxxxxxx
- xxxxxxx
- A
- xxx xxxxxxx C
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
-
- */
- //long flops = 2L*A.rows()*A.columns()*A.columns();
- int noOfTasks = (int) Math.min(flops / threshold, this.maxThreads); // each thread should process at least 30000 flops
- boolean splitHoriz = (A.columns() < noOfTasks);
- //boolean splitHoriz = (A.columns() >= noOfTasks);
- int p = splitHoriz ? A.rows() : A.columns();
- noOfTasks = Math.min(p,noOfTasks);
-
- if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
- return null;
- }
-
- // set up concurrent tasks
- int span = p/noOfTasks;
- final DoubleMatrix2D[] blocks = new DoubleMatrix2D[noOfTasks];
- for (int i=0; i<noOfTasks; i++) {
- final int offset = i*span;
- if (i==noOfTasks-1) span = p - span*i; // last span may be a bit larger
-
- final DoubleMatrix2D AA,BB,CC;
- if (!splitHoriz) { // split B along columns into blocks
- blocks[i] = A.viewPart(0,offset, A.rows(), span);
- }
- else { // split A along rows into blocks
- blocks[i] = A.viewPart(offset,0,span,A.columns());
- }
- }
- return blocks;
+ /*
+ determine how to split and parallelize best into blocks
+ if more B.columns than tasks --> split B.columns, as follows:
+
+ xx|xx|xxx B
+ xx|xx|xxx
+ xx|xx|xxx
+ A
+ xxx xx|xx|xxx C
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+
+ if less B.columns than tasks --> split A.rows, as follows:
+
+ xxxxxxx B
+ xxxxxxx
+ xxxxxxx
+ A
+ xxx xxxxxxx C
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+
+ */
+ //long flops = 2L*A.rows()*A.columns()*A.columns();
+ int noOfTasks = (int) Math.min(flops / threshold, this.maxThreads); // each thread should process at least 30000 flops
+ boolean splitHoriz = (A.columns() < noOfTasks);
+ //boolean splitHoriz = (A.columns() >= noOfTasks);
+ int p = splitHoriz ? A.rows() : A.columns();
+ noOfTasks = Math.min(p,noOfTasks);
+
+ if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
+ return null;
+ }
+
+ // set up concurrent tasks
+ int span = p/noOfTasks;
+ final DoubleMatrix2D[] blocks = new DoubleMatrix2D[noOfTasks];
+ for (int i=0; i<noOfTasks; i++) {
+ final int offset = i*span;
+ if (i==noOfTasks-1) span = p - span*i; // last span may be a bit larger
+
+ final DoubleMatrix2D AA,BB,CC;
+ if (!splitHoriz) { // split B along columns into blocks
+ blocks[i] = A.viewPart(0,offset, A.rows(), span);
+ }
+ else { // split A along rows into blocks
+ blocks[i] = A.viewPart(offset,0,span,A.columns());
+ }
+ }
+ return blocks;
}
protected DoubleMatrix2D[][] splitBlockedNN(DoubleMatrix2D A, DoubleMatrix2D B, int threshold, long flops) {
- DoubleMatrix2D[] blocksA = splitBlockedNN(A,threshold, flops);
- if (blocksA==null) return null;
- DoubleMatrix2D[] blocksB = splitBlockedNN(B,threshold, flops);
- if (blocksB==null) return null;
- DoubleMatrix2D[][] blocks = {blocksA,blocksB};
- return blocks;
+ DoubleMatrix2D[] blocksA = splitBlockedNN(A,threshold, flops);
+ if (blocksA==null) return null;
+ DoubleMatrix2D[] blocksB = splitBlockedNN(B,threshold, flops);
+ if (blocksB==null) return null;
+ DoubleMatrix2D[][] blocks = {blocksA,blocksB};
+ return blocks;
}
protected DoubleMatrix2D[] splitStridedNN(DoubleMatrix2D A, int threshold, long flops) {
- /*
- determine how to split and parallelize best into blocks
- if more B.columns than tasks --> split B.columns, as follows:
-
- xx|xx|xxx B
- xx|xx|xxx
- xx|xx|xxx
- A
- xxx xx|xx|xxx C
- xxx xx|xx|xxx
- xxx xx|xx|xxx
- xxx xx|xx|xxx
- xxx xx|xx|xxx
-
- if less B.columns than tasks --> split A.rows, as follows:
-
- xxxxxxx B
- xxxxxxx
- xxxxxxx
- A
- xxx xxxxxxx C
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
-
- */
- //long flops = 2L*A.rows()*A.columns()*A.columns();
- int noOfTasks = (int) Math.min(flops / threshold, this.maxThreads); // each thread should process at least 30000 flops
- boolean splitHoriz = (A.columns() < noOfTasks);
- //boolean splitHoriz = (A.columns() >= noOfTasks);
- int p = splitHoriz ? A.rows() : A.columns();
- noOfTasks = Math.min(p,noOfTasks);
-
- if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
- return null;
- }
-
- // set up concurrent tasks
- int span = p/noOfTasks;
- final DoubleMatrix2D[] blocks = new DoubleMatrix2D[noOfTasks];
- for (int i=0; i<noOfTasks; i++) {
- final int offset = i*span;
- if (i==noOfTasks-1) span = p - span*i; // last span may be a bit larger
-
- final DoubleMatrix2D AA,BB,CC;
- if (!splitHoriz) {
- // split B along columns into blocks
- blocks[i] = A.viewPart(0,i,A.rows(),A.columns()-i).viewStrides(1,noOfTasks);
- }
- else {
- // split A along rows into blocks
- blocks[i] = A.viewPart(i,0,A.rows()-i,A.columns()).viewStrides(noOfTasks,1);
- }
- }
- return blocks;
+ /*
+ determine how to split and parallelize best into blocks
+ if more B.columns than tasks --> split B.columns, as follows:
+
+ xx|xx|xxx B
+ xx|xx|xxx
+ xx|xx|xxx
+ A
+ xxx xx|xx|xxx C
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+
+ if less B.columns than tasks --> split A.rows, as follows:
+
+ xxxxxxx B
+ xxxxxxx
+ xxxxxxx
+ A
+ xxx xxxxxxx C
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+
+ */
+ //long flops = 2L*A.rows()*A.columns()*A.columns();
+ int noOfTasks = (int) Math.min(flops / threshold, this.maxThreads); // each thread should process at least 30000 flops
+ boolean splitHoriz = (A.columns() < noOfTasks);
+ //boolean splitHoriz = (A.columns() >= noOfTasks);
+ int p = splitHoriz ? A.rows() : A.columns();
+ noOfTasks = Math.min(p,noOfTasks);
+
+ if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
+ return null;
+ }
+
+ // set up concurrent tasks
+ int span = p/noOfTasks;
+ final DoubleMatrix2D[] blocks = new DoubleMatrix2D[noOfTasks];
+ for (int i=0; i<noOfTasks; i++) {
+ final int offset = i*span;
+ if (i==noOfTasks-1) span = p - span*i; // last span may be a bit larger
+
+ final DoubleMatrix2D AA,BB,CC;
+ if (!splitHoriz) {
+ // split B along columns into blocks
+ blocks[i] = A.viewPart(0,i,A.rows(),A.columns()-i).viewStrides(1,noOfTasks);
+ }
+ else {
+ // split A along rows into blocks
+ blocks[i] = A.viewPart(i,0,A.rows()-i,A.columns()).viewStrides(noOfTasks,1);
+ }
+ }
+ return blocks;
}
/**
* Prints various snapshot statistics to System.out; Simply delegates to {@link EDU.oswego.cs.dl.util.concurrent.FJTaskRunnerGroup#stats}.
*/
public void stats() {
- if (this.taskGroup!=null) this.taskGroup.stats();
+ if (this.taskGroup!=null) this.taskGroup.stats();
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SmpBlas.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SmpBlas.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SmpBlas.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/SmpBlas.java Wed Nov 25 03:41:28 2009
@@ -61,27 +61,27 @@
*/
@Deprecated
public class SmpBlas implements Blas {
- /**
- * The public global parallel blas; initialized via {@link #allocateBlas}.
- * Do not modify this variable via other means (it is public).
- */
- public static Blas smpBlas = SeqBlas.seqBlas;
-
- protected Blas seqBlas; // blocks are operated on in parallel; for each block this seq algo is used.
-
- protected Smp smp;
-
- protected int maxThreads;
+ /**
+ * The public global parallel blas; initialized via {@link #allocateBlas}.
+ * Do not modify this variable via other means (it is public).
+ */
+ public static Blas smpBlas = SeqBlas.seqBlas;
+
+ protected Blas seqBlas; // blocks are operated on in parallel; for each block this seq algo is used.
+
+ protected Smp smp;
+
+ protected int maxThreads;
- protected static int NN_THRESHOLD = 30000;
+ protected static int NN_THRESHOLD = 30000;
/**
Constructs a blas using a maximum of <tt>maxThreads<tt> threads; each executing the given sequential algos.
*/
protected SmpBlas(int maxThreads, Blas seqBlas) {
- this.seqBlas = seqBlas;
- this.maxThreads = maxThreads;
- this.smp = new Smp(maxThreads);
- //Smp.smp = new Smp(maxThreads);
+ this.seqBlas = seqBlas;
+ this.maxThreads = maxThreads;
+ this.smp = new Smp(maxThreads);
+ //Smp.smp = new Smp(maxThreads);
}
/**
Sets the public global variable <tt>SmpBlas.smpBlas</tt> to a blas using a maximum of <tt>maxThreads</tt> threads, each executing the given sequential algorithm; <tt>maxThreads</tt> is normally the number of CPUs.
@@ -91,302 +91,302 @@
@param seqBlas the sequential blas algorithms to be used on concurrently processed matrix blocks.
*/
public static void allocateBlas(int maxThreads, Blas seqBlas) {
- if (smpBlas instanceof SmpBlas) { // no need to change anything?
- SmpBlas s = (SmpBlas) smpBlas;
- if (s.maxThreads == maxThreads && s.seqBlas == seqBlas) return;
- }
-
- if (maxThreads<=1)
- smpBlas = seqBlas;
- else {
- smpBlas = new SmpBlas(maxThreads, seqBlas);
- }
+ if (smpBlas instanceof SmpBlas) { // no need to change anything?
+ SmpBlas s = (SmpBlas) smpBlas;
+ if (s.maxThreads == maxThreads && s.seqBlas == seqBlas) return;
+ }
+
+ if (maxThreads<=1)
+ smpBlas = seqBlas;
+ else {
+ smpBlas = new SmpBlas(maxThreads, seqBlas);
+ }
}
public void assign(DoubleMatrix2D A, final org.apache.mahout.matrix.function.DoubleFunction function) {
- run(A,false,
- new Matrix2DMatrix2DFunction() {
- public double apply(DoubleMatrix2D AA, DoubleMatrix2D BB) {
- seqBlas.assign(AA,function);
- return 0;
- }
- }
- );
+ run(A,false,
+ new Matrix2DMatrix2DFunction() {
+ public double apply(DoubleMatrix2D AA, DoubleMatrix2D BB) {
+ seqBlas.assign(AA,function);
+ return 0;
+ }
+ }
+ );
}
public void assign(DoubleMatrix2D A, DoubleMatrix2D B, final org.apache.mahout.matrix.function.DoubleDoubleFunction function) {
- run(A,B,false,
- new Matrix2DMatrix2DFunction() {
- public double apply(DoubleMatrix2D AA, DoubleMatrix2D BB) {
- seqBlas.assign(AA,BB,function);
- return 0;
- }
- }
- );
+ run(A,B,false,
+ new Matrix2DMatrix2DFunction() {
+ public double apply(DoubleMatrix2D AA, DoubleMatrix2D BB) {
+ seqBlas.assign(AA,BB,function);
+ return 0;
+ }
+ }
+ );
}
public double dasum(DoubleMatrix1D x) {
- return seqBlas.dasum(x);
+ return seqBlas.dasum(x);
}
public void daxpy(double alpha, DoubleMatrix1D x, DoubleMatrix1D y) {
- seqBlas.daxpy(alpha,x,y);
+ seqBlas.daxpy(alpha,x,y);
}
public void daxpy(double alpha, DoubleMatrix2D A, DoubleMatrix2D B) {
- seqBlas.daxpy(alpha,A,B);
+ seqBlas.daxpy(alpha,A,B);
}
public void dcopy(DoubleMatrix1D x, DoubleMatrix1D y) {
- seqBlas.dcopy(x,y);
+ seqBlas.dcopy(x,y);
}
public void dcopy(DoubleMatrix2D A, DoubleMatrix2D B) {
- seqBlas.dcopy(A, B);
+ seqBlas.dcopy(A, B);
}
public double ddot(DoubleMatrix1D x, DoubleMatrix1D y) {
- return seqBlas.ddot(x,y);
+ return seqBlas.ddot(x,y);
}
public void dgemm(final boolean transposeA, final boolean transposeB, final double alpha, final DoubleMatrix2D A, final DoubleMatrix2D B, final double beta, final DoubleMatrix2D C) {
- /*
- determine how to split and parallelize best into blocks
- if more B.columns than tasks --> split B.columns, as follows:
-
- xx|xx|xxx B
- xx|xx|xxx
- xx|xx|xxx
- A
- xxx xx|xx|xxx C
- xxx xx|xx|xxx
- xxx xx|xx|xxx
- xxx xx|xx|xxx
- xxx xx|xx|xxx
-
- if less B.columns than tasks --> split A.rows, as follows:
-
- xxxxxxx B
- xxxxxxx
- xxxxxxx
- A
- xxx xxxxxxx C
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
-
- */
- if (transposeA) {
- dgemm(false, transposeB, alpha, A.viewDice(), B, beta, C);
- return;
- }
- if (transposeB) {
- dgemm(transposeA, false, alpha, A, B.viewDice(), beta, C);
- return;
- }
- int m = A.rows();
- int n = A.columns();
- int p = B.columns();
-
- if (B.rows() != n)
- throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+A.toStringShort()+", "+B.toStringShort());
- if (C.rows() != m || C.columns() != p)
- throw new IllegalArgumentException("Incompatibel result matrix: "+A.toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
- if (A == C || B == C)
- throw new IllegalArgumentException("Matrices must not be identical");
-
- long flops = 2L*m*n*p;
- int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads); // each thread should process at least 30000 flops
- boolean splitB = (p >= noOfTasks);
- int width = splitB ? p : m;
- noOfTasks = Math.min(width,noOfTasks);
-
- if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
- seqBlas.dgemm(transposeA, transposeB, alpha, A, B, beta, C);
- return;
- }
-
- // set up concurrent tasks
- int span = width/noOfTasks;
- final FJTask[] subTasks = new FJTask[noOfTasks];
- for (int i=0; i<noOfTasks; i++) {
- final int offset = i*span;
- if (i==noOfTasks-1) span = width - span*i; // last span may be a bit larger
-
- final DoubleMatrix2D AA,BB,CC;
- if (splitB) {
- // split B along columns into blocks
- AA = A;
- BB = B.viewPart(0,offset,n,span);
- CC = C.viewPart(0,offset,m,span);
- }
- else {
- // split A along rows into blocks
- AA = A.viewPart(offset,0,span,n);
- BB = B;
- CC = C.viewPart(offset,0,span,p);
- }
-
- subTasks[i] = new FJTask() {
- public void run() {
- seqBlas.dgemm(transposeA,transposeB,alpha,AA,BB,beta,CC);
- //System.out.println("Hello "+offset);
- }
- };
- }
-
- // run tasks and wait for completion
- try {
- this.smp.taskGroup.invoke(
- new FJTask() {
- public void run() {
- coInvoke(subTasks);
- }
- }
- );
- } catch (InterruptedException exc) {}
+ /*
+ determine how to split and parallelize best into blocks
+ if more B.columns than tasks --> split B.columns, as follows:
+
+ xx|xx|xxx B
+ xx|xx|xxx
+ xx|xx|xxx
+ A
+ xxx xx|xx|xxx C
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+ xxx xx|xx|xxx
+
+ if less B.columns than tasks --> split A.rows, as follows:
+
+ xxxxxxx B
+ xxxxxxx
+ xxxxxxx
+ A
+ xxx xxxxxxx C
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+
+ */
+ if (transposeA) {
+ dgemm(false, transposeB, alpha, A.viewDice(), B, beta, C);
+ return;
+ }
+ if (transposeB) {
+ dgemm(transposeA, false, alpha, A, B.viewDice(), beta, C);
+ return;
+ }
+ int m = A.rows();
+ int n = A.columns();
+ int p = B.columns();
+
+ if (B.rows() != n)
+ throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+A.toStringShort()+", "+B.toStringShort());
+ if (C.rows() != m || C.columns() != p)
+ throw new IllegalArgumentException("Incompatibel result matrix: "+A.toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
+ if (A == C || B == C)
+ throw new IllegalArgumentException("Matrices must not be identical");
+
+ long flops = 2L*m*n*p;
+ int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads); // each thread should process at least 30000 flops
+ boolean splitB = (p >= noOfTasks);
+ int width = splitB ? p : m;
+ noOfTasks = Math.min(width,noOfTasks);
+
+ if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
+ seqBlas.dgemm(transposeA, transposeB, alpha, A, B, beta, C);
+ return;
+ }
+
+ // set up concurrent tasks
+ int span = width/noOfTasks;
+ final FJTask[] subTasks = new FJTask[noOfTasks];
+ for (int i=0; i<noOfTasks; i++) {
+ final int offset = i*span;
+ if (i==noOfTasks-1) span = width - span*i; // last span may be a bit larger
+
+ final DoubleMatrix2D AA,BB,CC;
+ if (splitB) {
+ // split B along columns into blocks
+ AA = A;
+ BB = B.viewPart(0,offset,n,span);
+ CC = C.viewPart(0,offset,m,span);
+ }
+ else {
+ // split A along rows into blocks
+ AA = A.viewPart(offset,0,span,n);
+ BB = B;
+ CC = C.viewPart(offset,0,span,p);
+ }
+
+ subTasks[i] = new FJTask() {
+ public void run() {
+ seqBlas.dgemm(transposeA,transposeB,alpha,AA,BB,beta,CC);
+ //System.out.println("Hello "+offset);
+ }
+ };
+ }
+
+ // run tasks and wait for completion
+ try {
+ this.smp.taskGroup.invoke(
+ new FJTask() {
+ public void run() {
+ coInvoke(subTasks);
+ }
+ }
+ );
+ } catch (InterruptedException exc) {}
}
public void dgemv(final boolean transposeA, final double alpha, DoubleMatrix2D A, final DoubleMatrix1D x, final double beta, DoubleMatrix1D y) {
- /*
- split A, as follows:
-
- x x
- x
- x
- A
- xxx x y
- xxx x
- --- -
- xxx x
- xxx x
- --- -
- xxx x
-
- */
- if (transposeA) {
- dgemv(false, alpha, A.viewDice(), x, beta, y);
- return;
- }
- int m = A.rows();
- int n = A.columns();
- long flops = 2L*m*n;
- int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads); // each thread should process at least 30000 flops
- int width = A.rows();
- noOfTasks = Math.min(width,noOfTasks);
-
- if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
- seqBlas.dgemv(transposeA, alpha, A, x, beta, y);
- return;
- }
-
- // set up concurrent tasks
- int span = width/noOfTasks;
- final FJTask[] subTasks = new FJTask[noOfTasks];
- for (int i=0; i<noOfTasks; i++) {
- final int offset = i*span;
- if (i==noOfTasks-1) span = width - span*i; // last span may be a bit larger
-
- // split A along rows into blocks
- final DoubleMatrix2D AA = A.viewPart(offset,0,span,n);
- final DoubleMatrix1D yy = y.viewPart(offset,span);
-
- subTasks[i] = new FJTask() {
- public void run() {
- seqBlas.dgemv(transposeA,alpha,AA,x,beta,yy);
- //System.out.println("Hello "+offset);
- }
- };
- }
-
- // run tasks and wait for completion
- try {
- this.smp.taskGroup.invoke(
- new FJTask() {
- public void run() {
- coInvoke(subTasks);
- }
- }
- );
- } catch (InterruptedException exc) {}
+ /*
+ split A, as follows:
+
+ x x
+ x
+ x
+ A
+ xxx x y
+ xxx x
+ --- -
+ xxx x
+ xxx x
+ --- -
+ xxx x
+
+ */
+ if (transposeA) {
+ dgemv(false, alpha, A.viewDice(), x, beta, y);
+ return;
+ }
+ int m = A.rows();
+ int n = A.columns();
+ long flops = 2L*m*n;
+ int noOfTasks = (int) Math.min(flops / 30000, this.maxThreads); // each thread should process at least 30000 flops
+ int width = A.rows();
+ noOfTasks = Math.min(width,noOfTasks);
+
+ if (noOfTasks < 2) { // parallelization doesn't pay off (too much start up overhead)
+ seqBlas.dgemv(transposeA, alpha, A, x, beta, y);
+ return;
+ }
+
+ // set up concurrent tasks
+ int span = width/noOfTasks;
+ final FJTask[] subTasks = new FJTask[noOfTasks];
+ for (int i=0; i<noOfTasks; i++) {
+ final int offset = i*span;
+ if (i==noOfTasks-1) span = width - span*i; // last span may be a bit larger
+
+ // split A along rows into blocks
+ final DoubleMatrix2D AA = A.viewPart(offset,0,span,n);
+ final DoubleMatrix1D yy = y.viewPart(offset,span);
+
+ subTasks[i] = new FJTask() {
+ public void run() {
+ seqBlas.dgemv(transposeA,alpha,AA,x,beta,yy);
+ //System.out.println("Hello "+offset);
+ }
+ };
+ }
+
+ // run tasks and wait for completion
+ try {
+ this.smp.taskGroup.invoke(
+ new FJTask() {
+ public void run() {
+ coInvoke(subTasks);
+ }
+ }
+ );
+ } catch (InterruptedException exc) {}
}
public void dger(double alpha, DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix2D A) {
- seqBlas.dger(alpha,x,y,A);
+ seqBlas.dger(alpha,x,y,A);
}
public double dnrm2(DoubleMatrix1D x) {
- return seqBlas.dnrm2(x);
+ return seqBlas.dnrm2(x);
}
public void drot(DoubleMatrix1D x, DoubleMatrix1D y, double c, double s) {
- seqBlas.drot(x,y,c,s);
+ seqBlas.drot(x,y,c,s);
}
public void drotg(double a, double b, double rotvec[]) {
- seqBlas.drotg(a,b,rotvec);
+ seqBlas.drotg(a,b,rotvec);
}
public void dscal(double alpha, DoubleMatrix1D x) {
- seqBlas.dscal(alpha,x);
+ seqBlas.dscal(alpha,x);
}
public void dscal(double alpha, DoubleMatrix2D A) {
- seqBlas.dscal(alpha, A);
+ seqBlas.dscal(alpha, A);
}
public void dswap(DoubleMatrix1D x, DoubleMatrix1D y) {
- seqBlas.dswap(x,y);
+ seqBlas.dswap(x,y);
}
public void dswap(DoubleMatrix2D A, DoubleMatrix2D B) {
- seqBlas.dswap(A,B);
+ seqBlas.dswap(A,B);
}
public void dsymv(boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y) {
- seqBlas.dsymv(isUpperTriangular, alpha, A, x, beta, y);
+ seqBlas.dsymv(isUpperTriangular, alpha, A, x, beta, y);
}
public void dtrmv(boolean isUpperTriangular, boolean transposeA, boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) {
- seqBlas.dtrmv(isUpperTriangular, transposeA, isUnitTriangular, A, x);
+ seqBlas.dtrmv(isUpperTriangular, transposeA, isUnitTriangular, A, x);
}
public int idamax(DoubleMatrix1D x) {
- return seqBlas.idamax(x);
+ return seqBlas.idamax(x);
}
protected double[] run(DoubleMatrix2D A, DoubleMatrix2D B, boolean collectResults, Matrix2DMatrix2DFunction fun) {
- DoubleMatrix2D[][] blocks;
- blocks = this.smp.splitBlockedNN(A, B, NN_THRESHOLD, A.rows()*A.columns());
- //blocks = this.smp.splitStridedNN(A, B, NN_THRESHOLD, A.rows()*A.columns());
- int b = blocks!=null ? blocks[0].length : 1;
- double[] results = collectResults ? new double[b] : null;
-
- if (blocks==null) { // too small --> sequential
- double result = fun.apply(A,B);
- if (collectResults) results[0] = result;
- return results;
- }
- else { // parallel
- this.smp.run(blocks[0],blocks[1],results,fun);
- }
- return results;
+ DoubleMatrix2D[][] blocks;
+ blocks = this.smp.splitBlockedNN(A, B, NN_THRESHOLD, A.rows()*A.columns());
+ //blocks = this.smp.splitStridedNN(A, B, NN_THRESHOLD, A.rows()*A.columns());
+ int b = blocks!=null ? blocks[0].length : 1;
+ double[] results = collectResults ? new double[b] : null;
+
+ if (blocks==null) { // too small --> sequential
+ double result = fun.apply(A,B);
+ if (collectResults) results[0] = result;
+ return results;
+ }
+ else { // parallel
+ this.smp.run(blocks[0],blocks[1],results,fun);
+ }
+ return results;
}
protected double[] run(DoubleMatrix2D A, boolean collectResults, Matrix2DMatrix2DFunction fun) {
- DoubleMatrix2D[] blocks;
- blocks = this.smp.splitBlockedNN(A, NN_THRESHOLD, A.rows()*A.columns());
- //blocks = this.smp.splitStridedNN(A, NN_THRESHOLD, A.rows()*A.columns());
- int b = blocks!=null ? blocks.length : 1;
- double[] results = collectResults ? new double[b] : null;
-
- if (blocks==null) { // too small -> sequential
- double result = fun.apply(A,null);
- if (collectResults) results[0] = result;
- return results;
- }
- else { // parallel
- this.smp.run(blocks,null,results,fun);
- }
- return results;
+ DoubleMatrix2D[] blocks;
+ blocks = this.smp.splitBlockedNN(A, NN_THRESHOLD, A.rows()*A.columns());
+ //blocks = this.smp.splitStridedNN(A, NN_THRESHOLD, A.rows()*A.columns());
+ int b = blocks!=null ? blocks.length : 1;
+ double[] results = collectResults ? new double[b] : null;
+
+ if (blocks==null) { // too small -> sequential
+ double result = fun.apply(A,null);
+ if (collectResults) results[0] = result;
+ return results;
+ }
+ else { // parallel
+ this.smp.run(blocks,null,results,fun);
+ }
+ return results;
}
/**
* Prints various snapshot statistics to System.out; Simply delegates to {@link EDU.oswego.cs.dl.util.concurrent.FJTaskRunnerGroup#stats}.
*/
public void stats() {
- if (this.smp!=null) this.smp.stats();
+ if (this.smp!=null) this.smp.stats();
}
private double xsum(DoubleMatrix2D A) {
- double[] sums = run(A,true,
- new Matrix2DMatrix2DFunction() {
- public double apply(DoubleMatrix2D AA, DoubleMatrix2D BB) {
- return AA.zSum();
- }
- }
- );
-
- double sum = 0;
- for (int i = sums.length; --i >= 0; ) sum += sums[i];
- return sum;
+ double[] sums = run(A,true,
+ new Matrix2DMatrix2DFunction() {
+ public double apply(DoubleMatrix2D AA, DoubleMatrix2D BB) {
+ return AA.zSum();
+ }
+ }
+ );
+
+ double sum = 0;
+ for (int i = sums.length; --i >= 0; ) sum += sums[i];
+ return sum;
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/package.html
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/package.html?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/package.html (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/linalg/package.html Wed Nov 25 03:41:28 2009
@@ -1,12 +1,12 @@
<HTML>
<BODY>
-<p>Linear Algebraic matrix computations operating on {@link cern.colt.matrix.DoubleMatrix2D}
- and {@link cern.colt.matrix.DoubleMatrix1D}. </p>
+<p>Linear Algebraic matrix computations operating on {@link org.apache.mahout.matrix.matrix.DoubleMatrix2D}
+ and {@link org.apache.mahout.matrix.matrix.DoubleMatrix1D}. </p>
<h1><a name="Overview"></a>Overview</h1>
<p>The linalg package provides easy and performant access to compute intensive
- Linear Algebra. Much functionality is concentrated in class {@link cern.colt.matrix.linalg.Algebra}.
+ Linear Algebra. Much functionality is concentrated in class {@link org.apache.mahout.matrix.matrix.linalg.Algebra}.
Five fundamental matrix decompositions, which consist of pairs or triples of
matrices, permutation vectors, and the like, produce results in five decomposition
classes. These decompositions are accessed by the <tt>Algebra</tt> class
@@ -31,8 +31,8 @@
<h2>Design Issues</h2>
-<p> Jama matrices are of type <tt>Jama.Matrix</tt>, Colt matrices of type <tt>cern.colt.matrix.DoubleMatrix1D</tt>,
- <tt>cern.colt.matrix.DoubleMatrix2D</tt> and <tt>cern.colt.matrix.DoubleMatrix3D</tt>.
+<p> Jama matrices are of type <tt>Jama.Matrix</tt>, Colt matrices of type <tt>org.apache.mahout.matrix.matrix.DoubleMatrix1D</tt>,
+ <tt>org.apache.mahout.matrix.matrix.DoubleMatrix2D</tt> and <tt>org.apache.mahout.matrix.matrix.DoubleMatrix3D</tt>.
<p><tt>Jama.Matrix</tt> is not a general-purpose array class. It is designed for
a single special purpose: Linear algebra. Because of its limited scope, Jama
@@ -115,4 +115,4 @@
Contact <a href="mailto:wolfgang.hoschek@cern.ch">wolfgang.hoschek@cern.ch</a>
for more info.</i></p>
</BODY>
-</HTML>
\ No newline at end of file
+</HTML>
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/Formatter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/Formatter.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/Formatter.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/Formatter.java Wed Nov 25 03:41:28 2009
@@ -32,123 +32,123 @@
* Constructs and returns a matrix formatter with alignment <tt>LEFT</tt>.
*/
public Formatter() {
- this(LEFT);
+ this(LEFT);
}
/**
* Constructs and returns a matrix formatter.
* @param alignment the given alignment used to align a column.
*/
public Formatter(String alignment) {
- setAlignment(alignment);
+ setAlignment(alignment);
}
/**
* Converts a given cell to a String; no alignment considered.
*/
protected String form(AbstractMatrix1D matrix, int index, Former formatter) {
- return this.form((ObjectMatrix1D) matrix, index, formatter);
+ return this.form((ObjectMatrix1D) matrix, index, formatter);
}
/**
* Converts a given cell to a String; no alignment considered.
*/
protected String form(ObjectMatrix1D matrix, int index, Former formatter) {
- Object value = matrix.get(index);
- if (value==null) return "";
- return String.valueOf(value);
+ Object value = matrix.get(index);
+ if (value==null) return "";
+ return String.valueOf(value);
}
/**
* Returns a string representations of all cells; no alignment considered.
*/
protected String[][] format(AbstractMatrix2D matrix) {
- return this.format((ObjectMatrix2D) matrix);
+ return this.format((ObjectMatrix2D) matrix);
}
/**
* Returns a string representations of all cells; no alignment considered.
*/
protected String[][] format(ObjectMatrix2D matrix) {
- String[][] strings = new String[matrix.rows()][matrix.columns()];
- for (int row=matrix.rows(); --row >= 0; ) strings[row] = formatRow(matrix.viewRow(row));
- return strings;
+ String[][] strings = new String[matrix.rows()][matrix.columns()];
+ for (int row=matrix.rows(); --row >= 0; ) strings[row] = formatRow(matrix.viewRow(row));
+ return strings;
}
/**
* Returns a string <tt>s</tt> such that <tt>Object[] m = s</tt> is a legal Java statement.
* @param matrix the matrix to format.
*/
public String toSourceCode(ObjectMatrix1D matrix) {
- Formatter copy = (Formatter) this.clone();
- copy.setPrintShape(false);
- copy.setColumnSeparator(", ");
- String lead = "{";
- String trail = "};";
- return lead + copy.toString(matrix) + trail;
+ Formatter copy = (Formatter) this.clone();
+ copy.setPrintShape(false);
+ copy.setColumnSeparator(", ");
+ String lead = "{";
+ String trail = "};";
+ return lead + copy.toString(matrix) + trail;
}
/**
* Returns a string <tt>s</tt> such that <tt>Object[] m = s</tt> is a legal Java statement.
* @param matrix the matrix to format.
*/
public String toSourceCode(ObjectMatrix2D matrix) {
- Formatter copy = (Formatter) this.clone();
- String b3 = blanks(3);
- copy.setPrintShape(false);
- copy.setColumnSeparator(", ");
- copy.setRowSeparator("},\n"+b3+"{");
- String lead = "{\n"+b3+"{";
- String trail = "}\n};";
- return lead + copy.toString(matrix) + trail;
+ Formatter copy = (Formatter) this.clone();
+ String b3 = blanks(3);
+ copy.setPrintShape(false);
+ copy.setColumnSeparator(", ");
+ copy.setRowSeparator("},\n"+b3+"{");
+ String lead = "{\n"+b3+"{";
+ String trail = "}\n};";
+ return lead + copy.toString(matrix) + trail;
}
/**
* Returns a string <tt>s</tt> such that <tt>Object[] m = s</tt> is a legal Java statement.
* @param matrix the matrix to format.
*/
public String toSourceCode(ObjectMatrix3D matrix) {
- Formatter copy = (Formatter) this.clone();
- String b3 = blanks(3);
- String b6 = blanks(6);
- copy.setPrintShape(false);
- copy.setColumnSeparator(", ");
- copy.setRowSeparator("},\n"+b6+"{");
- copy.setSliceSeparator("}\n"+b3+"},\n"+b3+"{\n"+b6+"{");
- String lead = "{\n"+b3+"{\n"+b6+"{";
- String trail = "}\n"+b3+"}\n}";
- return lead + copy.toString(matrix) + trail;
+ Formatter copy = (Formatter) this.clone();
+ String b3 = blanks(3);
+ String b6 = blanks(6);
+ copy.setPrintShape(false);
+ copy.setColumnSeparator(", ");
+ copy.setRowSeparator("},\n"+b6+"{");
+ copy.setSliceSeparator("}\n"+b3+"},\n"+b3+"{\n"+b6+"{");
+ String lead = "{\n"+b3+"{\n"+b6+"{";
+ String trail = "}\n"+b3+"}\n}";
+ return lead + copy.toString(matrix) + trail;
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
protected String toString(AbstractMatrix2D matrix) {
- return this.toString((ObjectMatrix2D) matrix);
+ return this.toString((ObjectMatrix2D) matrix);
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(ObjectMatrix1D matrix) {
- ObjectMatrix2D easy = matrix.like2D(1,matrix.size());
- easy.viewRow(0).assign(matrix);
- return toString(easy);
+ ObjectMatrix2D easy = matrix.like2D(1,matrix.size());
+ easy.viewRow(0).assign(matrix);
+ return toString(easy);
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(ObjectMatrix2D matrix) {
- return super.toString(matrix);
+ return super.toString(matrix);
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(ObjectMatrix3D matrix) {
- StringBuffer buf = new StringBuffer();
- boolean oldPrintShape = this.printShape;
- this.printShape = false;
- for (int slice=0; slice < matrix.slices(); slice++) {
- if (slice!=0) buf.append(sliceSeparator);
- buf.append(toString(matrix.viewSlice(slice)));
- }
- this.printShape = oldPrintShape;
- if (printShape) buf.insert(0,shape(matrix) + "\n");
- return buf.toString();
+ StringBuffer buf = new StringBuffer();
+ boolean oldPrintShape = this.printShape;
+ this.printShape = false;
+ for (int slice=0; slice < matrix.slices(); slice++) {
+ if (slice!=0) buf.append(sliceSeparator);
+ buf.append(toString(matrix.viewSlice(slice)));
+ }
+ this.printShape = oldPrintShape;
+ if (printShape) buf.insert(0,shape(matrix) + "\n");
+ return buf.toString();
}
/**
Returns a string representation of the given matrix with axis as well as rows and columns labeled.
@@ -163,78 +163,78 @@
@return the matrix converted to a string.
*/
public String toTitleString(ObjectMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title) {
- if (matrix.size()==0) return "Empty matrix";
- String oldFormat = this.format;
- this.format = LEFT;
-
- int rows = matrix.rows();
- int columns = matrix.columns();
-
- // determine how many rows and columns are needed
- int r=0;
- int c=0;
- r += (columnNames==null ? 0 : 1);
- c += (rowNames==null ? 0 : 1);
- c += (rowAxisName==null ? 0 : 1);
- c += (rowNames!=null || rowAxisName!=null ? 1 : 0);
-
- int height = r + Math.max(rows, rowAxisName==null ? 0: rowAxisName.length());
- int width = c + columns;
-
- // make larger matrix holding original matrix and naming strings
- org.apache.mahout.matrix.matrix.ObjectMatrix2D titleMatrix = matrix.like(height, width);
-
- // insert original matrix into larger matrix
- titleMatrix.viewPart(r,c,rows,columns).assign(matrix);
-
- // insert column axis name in leading row
- if (r>0) titleMatrix.viewRow(0).viewPart(c,columns).assign(columnNames);
-
- // insert row axis name in leading column
- if (rowAxisName!=null) {
- String[] rowAxisStrings = new String[rowAxisName.length()];
- for (int i=rowAxisName.length(); --i >= 0; ) rowAxisStrings[i] = rowAxisName.substring(i,i+1);
- titleMatrix.viewColumn(0).viewPart(r,rowAxisName.length()).assign(rowAxisStrings);
- }
- // insert row names in next leading columns
- if (rowNames!=null) titleMatrix.viewColumn(c-2).viewPart(r,rows).assign(rowNames);
-
- // insert vertical "---------" separator line in next leading column
- if (c>0) titleMatrix.viewColumn(c-2+1).viewPart(0,rows+r).assign("|");
-
- // convert the large matrix to a string
- boolean oldPrintShape = this.printShape;
- this.printShape = false;
- String str = toString(titleMatrix);
- this.printShape = oldPrintShape;
-
- // insert horizontal "--------------" separator line
- StringBuffer total = new StringBuffer(str);
- if (columnNames != null) {
- int i = str.indexOf(rowSeparator);
- total.insert(i+1,repeat('-',i)+rowSeparator);
- }
- else if (columnAxisName != null) {
- int i = str.indexOf(rowSeparator);
- total.insert(0,repeat('-',i)+rowSeparator);
- }
-
- // insert line for column axis name
- if (columnAxisName != null) {
- int j=0;
- if (c>0) j = str.indexOf('|');
- String s = blanks(j);
- if (c>0) s = s + "| ";
- s = s+columnAxisName+"\n";
- total.insert(0,s);
- }
-
- // insert title
- if (title!=null) total.insert(0,title+"\n");
-
- this.format = oldFormat;
-
- return total.toString();
+ if (matrix.size()==0) return "Empty matrix";
+ String oldFormat = this.format;
+ this.format = LEFT;
+
+ int rows = matrix.rows();
+ int columns = matrix.columns();
+
+ // determine how many rows and columns are needed
+ int r=0;
+ int c=0;
+ r += (columnNames==null ? 0 : 1);
+ c += (rowNames==null ? 0 : 1);
+ c += (rowAxisName==null ? 0 : 1);
+ c += (rowNames!=null || rowAxisName!=null ? 1 : 0);
+
+ int height = r + Math.max(rows, rowAxisName==null ? 0: rowAxisName.length());
+ int width = c + columns;
+
+ // make larger matrix holding original matrix and naming strings
+ org.apache.mahout.matrix.matrix.ObjectMatrix2D titleMatrix = matrix.like(height, width);
+
+ // insert original matrix into larger matrix
+ titleMatrix.viewPart(r,c,rows,columns).assign(matrix);
+
+ // insert column axis name in leading row
+ if (r>0) titleMatrix.viewRow(0).viewPart(c,columns).assign(columnNames);
+
+ // insert row axis name in leading column
+ if (rowAxisName!=null) {
+ String[] rowAxisStrings = new String[rowAxisName.length()];
+ for (int i=rowAxisName.length(); --i >= 0; ) rowAxisStrings[i] = rowAxisName.substring(i,i+1);
+ titleMatrix.viewColumn(0).viewPart(r,rowAxisName.length()).assign(rowAxisStrings);
+ }
+ // insert row names in next leading columns
+ if (rowNames!=null) titleMatrix.viewColumn(c-2).viewPart(r,rows).assign(rowNames);
+
+ // insert vertical "---------" separator line in next leading column
+ if (c>0) titleMatrix.viewColumn(c-2+1).viewPart(0,rows+r).assign("|");
+
+ // convert the large matrix to a string
+ boolean oldPrintShape = this.printShape;
+ this.printShape = false;
+ String str = toString(titleMatrix);
+ this.printShape = oldPrintShape;
+
+ // insert horizontal "--------------" separator line
+ StringBuffer total = new StringBuffer(str);
+ if (columnNames != null) {
+ int i = str.indexOf(rowSeparator);
+ total.insert(i+1,repeat('-',i)+rowSeparator);
+ }
+ else if (columnAxisName != null) {
+ int i = str.indexOf(rowSeparator);
+ total.insert(0,repeat('-',i)+rowSeparator);
+ }
+
+ // insert line for column axis name
+ if (columnAxisName != null) {
+ int j=0;
+ if (c>0) j = str.indexOf('|');
+ String s = blanks(j);
+ if (c>0) s = s + "| ";
+ s = s+columnAxisName+"\n";
+ total.insert(0,s);
+ }
+
+ // insert title
+ if (title!=null) total.insert(0,title+"\n");
+
+ this.format = oldFormat;
+
+ return total.toString();
}
/**
Returns a string representation of the given matrix with axis as well as rows and columns labeled.
@@ -251,12 +251,12 @@
@return the matrix converted to a string.
*/
public String toTitleString(ObjectMatrix3D matrix, String[] sliceNames, String[] rowNames, String[] columnNames, String sliceAxisName, String rowAxisName, String columnAxisName, String title) {
- if (matrix.size()==0) return "Empty matrix";
- StringBuffer buf = new StringBuffer();
- for (int i=0; i<matrix.slices(); i++) {
- if (i!=0) buf.append(sliceSeparator);
- buf.append(toTitleString(matrix.viewSlice(i),rowNames,columnNames,rowAxisName,columnAxisName,title+"\n"+sliceAxisName+"="+sliceNames[i]));
- }
- return buf.toString();
+ if (matrix.size()==0) return "Empty matrix";
+ StringBuffer buf = new StringBuffer();
+ for (int i=0; i<matrix.slices(); i++) {
+ if (i!=0) buf.append(sliceSeparator);
+ buf.append(toTitleString(matrix.viewSlice(i),rowNames,columnNames,rowAxisName,columnAxisName,title+"\n"+sliceAxisName+"="+sliceNames[i]));
+ }
+ return buf.toString();
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix1DComparator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix1DComparator.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix1DComparator.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix1DComparator.java Wed Nov 25 03:41:28 2009
@@ -20,8 +20,6 @@
* order for the data structure to serialize successfully, the comparator (if
* provided) must implement <tt>Serializable</tt>.<p>
*
- * @author wolfgang.hoschek@cern.ch
- * @version 1.0, 09/24/99
* @see java.util.Comparator
* @see org.apache.mahout.colt
* @see org.apache.mahout.matrix.Sorting
@@ -51,8 +49,8 @@
*
*
* @return a negative integer, zero, or a positive integer as the
- * first argument is less than, equal to, or greater than the
- * second.
+ * first argument is less than, equal to, or greater than the
+ * second.
*/
int compare(ObjectMatrix1D o1, ObjectMatrix1D o2);
/**
@@ -73,8 +71,8 @@
*
* @param obj the reference object with which to compare.
* @return <code>true</code> only if the specified object is also
- * a comparator and it imposes the same ordering as this
- * comparator.
+ * a comparator and it imposes the same ordering as this
+ * comparator.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.Object#hashCode()
*/
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix2DComparator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix2DComparator.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix2DComparator.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/objectalgo/ObjectMatrix2DComparator.java Wed Nov 25 03:41:28 2009
@@ -20,8 +20,6 @@
* order for the data structure to serialize successfully, the comparator (if
* provided) must implement <tt>Serializable</tt>.<p>
*
- * @author wolfgang.hoschek@cern.ch
- * @version 1.0, 09/24/99
* @see java.util.Comparator
* @see org.apache.mahout.colt
* @see org.apache.mahout.matrix.Sorting
@@ -51,8 +49,8 @@
*
*
* @return a negative integer, zero, or a positive integer as the
- * first argument is less than, equal to, or greater than the
- * second.
+ * first argument is less than, equal to, or greater than the
+ * second.
*/
int compare(ObjectMatrix2D o1, ObjectMatrix2D o2);
/**
@@ -73,8 +71,8 @@
*
* @param obj the reference object with which to compare.
* @return <code>true</code> only if the specified object is also
- * a comparator and it imposes the same ordering as this
- * comparator.
+ * a comparator and it imposes the same ordering as this
+ * comparator.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.Object#hashCode()
*/