[Pkg-javascript-commits] [science.js] 45/87: Add eigendecomposition.
bhuvan krishna
bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:11:58 UTC 2016
This is an automated email from the git hooks/post-receive script.
bhuvan-guest pushed a commit to branch master
in repository science.js.
commit c836d96ca90e32477e1d307684afa76e693de4e8
Author: Jason Davies <jason at jasondavies.com>
Date: Sat Aug 27 18:02:33 2011 +0100
Add eigendecomposition.
---
Makefile | 3 +-
package.json | 6 +-
science.js | 767 ++++++++++++++++++++++++++++++++++++++++++
science.lin.min.js | 2 +-
science.min.js | 2 +-
science.stats.min.js | 2 +-
src/package.js | 6 +-
src/vector/decompose.js | 767 ++++++++++++++++++++++++++++++++++++++++++
test/vector/decompose-test.js | 23 ++
9 files changed, 1568 insertions(+), 10 deletions(-)
diff --git a/Makefile b/Makefile
index a65ed9d..02e4a05 100644
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ science.core.js: \
science.vector.js: \
src/vector/vector.js \
+ src/vector/decompose.js \
src/vector/cross.js \
src/vector/dot.js \
src/vector/length.js \
@@ -65,7 +66,7 @@ test: all
@rm -f $@
$(JS_COMPILER) < $< > $@
-package.json: science.js
+package.json: science.js src/package.js
node src/package.js > $@
science.js science%.js: Makefile
diff --git a/package.json b/package.json
index a8848b3..aa16674 100644
--- a/package.json
+++ b/package.json
@@ -16,8 +16,8 @@
"type": "git",
"url": "http://github.com/jasondavies/science.js.git"
},
- "dependencies": {
- "uglify-js": "1.0.6",
- "vows": "0.5.11"
+ "devDependencies": {
+ "uglify-js": "1.2.2",
+ "vows": "0.6.0"
}
}
diff --git a/science.js b/science.js
index 058aa85..76e1513 100644
--- a/science.js
+++ b/science.js
@@ -28,6 +28,773 @@ science.zeroes = function(n) {
return a;
};
science.vector = {};
+science.vector.decompose = function() {
+
+ function decompose(A) {
+ var n = A.length; // column dimension
+ var V = [],
+ d = [],
+ e = [];
+
+ for (var i = 0; i < n; i++) {
+ V[i] = [];
+ d[i] = [];
+ e[i] = [];
+ }
+
+ var symmetric = true;
+ for (var j = 0; j < n; j++) {
+ for (var i = 0; i < n; i++) {
+ if (A[i][j] !== A[j][i]) {
+ symmetric = false;
+ break;
+ }
+ }
+ }
+
+ if (symmetric) {
+ for (var i = 0; i < n; i++) V[i] = A[i].slice();
+
+ // Tridiagonalize.
+ science_vector_decomposeTred2(d, e, V);
+
+ // Diagonalize.
+ science_vector_decomposeTql2(d, e, V);
+ } else {
+ var H = [];
+ for (var j = 0; j < n; j++) H[i] = A.slice();
+
+ // Reduce to Hessenberg form.
+ science_vector_decomposeOrthes(H, V);
+
+ // Reduce Hessenberg to real Schur form.
+ science_vector_decomposeHqr2(d, e, H, V);
+ }
+
+ var D = [];
+ for (var i=0; i<n; i++) {
+ D[i] = [];
+ for (var j=0; j<n; j++) D[i][j] = 0;
+ D[i][i] = d[i];
+ if (e[i] > 0) D[i][i+1] = e[i];
+ else if (e[i] < 0) D[i][i-1] = e[i];
+ }
+ return {D: D, V: V};
+ }
+
+//
+// /** Return the real parts of the eigenvalues
+// @return real(diag(D))
+// */
+//
+// public double[] getRealEigenvalues () {
+// return d;
+// }
+//
+// /** Return the imaginary parts of the eigenvalues
+// @return imag(diag(D))
+// */
+//
+// public double[] getImagEigenvalues () {
+// return e;
+// }
+
+ /** Return the block diagonal eigenvalue matrix
+ @return D
+ */
+
+ /*
+ this.getD = function() {
+ var X = new Matrix(this.n,this.n);
+ var D = X.getArray();
+ for (var i = 0; i < this.n; i++) {
+ for (var j = 0; j < this.n; j++) {
+ D[i][j] = 0;
+ }
+ D[i][i] = this.d[i];
+ if (this.e[i] > 0) {
+ D[i][i+1] = this.e[i];
+ } else if (this.e[i] < 0) {
+ D[i][i-1] = this.e[i];
+ }
+ }
+ return X;
+ }
+ */
+
+ return decompose;
+};
+
+// Symmetric Householder reduction to tridiagonal form.
+function science_vector_decomposeTred2(d, e, V) {
+ // This is derived from the Algol procedures tred2 by
+ // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
+ // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ var n = V.length;
+
+ for (var j = 0; j < n; j++) { d[j] = V[n-1][j]; }
+
+ // Householder reduction to tridiagonal form.
+ for (var i = n-1; i > 0; i--) {
+ // Scale to avoid under/overflow.
+
+ var scale = 0,
+ h = 0;
+ for (var k = 0; k < i; k++) { scale = scale + Math.abs(d[k]); }
+ if (scale === 0) {
+ e[i] = d[i-1];
+ for (var j = 0; j < i; j++) {
+ d[j] = V[i-1][j];
+ V[i][j] = 0;
+ V[j][i] = 0;
+ }
+ } else {
+ // Generate Householder vector.
+ for (var k = 0; k < i; k++) {
+ d[k] /= scale;
+ h += d[k] * d[k];
+ }
+ var f = d[i-1];
+ var g = Math.sqrt(h);
+ if (f > 0) g = -g;
+ e[i] = scale * g;
+ h = h - f * g;
+ d[i-1] = f - g;
+ for (var j = 0; j < i; j++) e[j] = 0;
+
+ // Apply similarity transformation to remaining columns.
+
+ for (var j = 0; j < i; j++) {
+ f = d[j];
+ V[j][i] = f;
+ g = e[j] + V[j][j] * f;
+ for (var k = j+1; k <= i-1; k++) {
+ g += V[k][j] * d[k];
+ e[k] += V[k][j] * f;
+ }
+ e[j] = g;
+ }
+ f = 0;
+ for (var j = 0; j < i; j++) {
+ e[j] /= h;
+ f += e[j] * d[j];
+ }
+ var hh = f / (h + h);
+ for (var j = 0; j < i; j++) { e[j] -= hh * d[j]; }
+ for (var j = 0; j < i; j++) {
+ f = d[j];
+ g = e[j];
+ for (var k = j; k <= i-1; k++) { V[k][j] -= (f * e[k] + g * d[k]); }
+ d[j] = V[i-1][j];
+ V[i][j] = 0;
+ }
+ }
+ d[i] = h;
+ }
+
+ // Accumulate transformations.
+ for (var i = 0; i < n-1; i++) {
+ V[n-1][i] = V[i][i];
+ V[i][i] = 1.0;
+ var h = d[i+1];
+ if (h != 0) {
+ for (var k = 0; k <= i; k++) { d[k] = V[k][i+1] / h; }
+ for (var j = 0; j <= i; j++) {
+ var g = 0;
+ for (var k = 0; k <= i; k++) { g += V[k][i+1] * V[k][j]; }
+ for (var k = 0; k <= i; k++) { V[k][j] -= g * d[k]; }
+ }
+ }
+ for (var k = 0; k <= i; k++) { V[k][i+1] = 0; }
+ }
+ for (var j = 0; j < n; j++) {
+ d[j] = V[n-1][j];
+ V[n-1][j] = 0;
+ }
+ V[n-1][n-1] = 1;
+ e[0] = 0;
+}
+
+// Symmetric tridiagonal QL algorithm.
+function science_vector_decomposeTql2(d, e, V) {
+ // This is derived from the Algol procedures tql2, by
+ // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
+ // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ var n = V.length;
+
+ for (var i = 1; i < n; i++) { e[i-1] = e[i]; }
+ e[n-1] = 0;
+
+ var f = 0;
+ var tst1 = 0;
+ var eps = 1e-12;
+ for (var l = 0; l < n; l++) {
+ // Find small subdiagonal element
+ tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l]));
+ var m = l;
+ while (m < n) {
+ if (Math.abs(e[m]) <= eps*tst1) { break; }
+ m++;
+ }
+
+ // If m == l, d[l] is an eigenvalue,
+ // otherwise, iterate.
+ if (m > l) {
+ var iter = 0;
+ do {
+ iter++; // (Could check iteration count here.)
+
+ // Compute implicit shift
+ var g = d[l];
+ var p = (d[l+1] - g) / (2.0 * e[l]);
+ var r = science.hypot(p, 1);
+ if (p < 0) {
+ r = -r;
+ }
+ d[l] = e[l] / (p + r);
+ d[l+1] = e[l] * (p + r);
+ var dl1 = d[l+1];
+ var h = g - d[l];
+ for (var i = l+2; i < n; i++) {
+ d[i] -= h;
+ }
+ f = f + h;
+
+ // Implicit QL transformation.
+ p = d[m];
+ var c = 1.0;
+ var c2 = c;
+ var c3 = c;
+ var el1 = e[l+1];
+ var s = 0;
+ var s2 = 0;
+ for (var i = m-1; i >= l; i--) {
+ c3 = c2;
+ c2 = c;
+ s2 = s;
+ g = c * e[i];
+ h = c * p;
+ r = science.hypot(p,e[i]);
+ e[i+1] = s * r;
+ s = e[i] / r;
+ c = p / r;
+ p = c * d[i] - s * g;
+ d[i+1] = h + s * (c * g + s * d[i]);
+
+ // Accumulate transformation.
+ for (var k = 0; k < n; k++) {
+ h = V[k][i+1];
+ V[k][i+1] = s * V[k][i] + c * h;
+ V[k][i] = c * V[k][i] - s * h;
+ }
+ }
+ p = -s * s2 * c3 * el1 * e[l] / dl1;
+ e[l] = s * p;
+ d[l] = c * p;
+
+ // Check for convergence.
+ } while (Math.abs(e[l]) > eps*tst1);
+ }
+ d[l] = d[l] + f;
+ e[l] = 0;
+ }
+
+ // Sort eigenvalues and corresponding vectors.
+ for (var i = 0; i < n-1; i++) {
+ var k = i;
+ var p = d[i];
+ for (var j = i+1; j < n; j++) {
+ if (d[j] < p) {
+ k = j;
+ p = d[j];
+ }
+ }
+ if (k != i) {
+ d[k] = d[i];
+ d[i] = p;
+ for (var j = 0; j < n; j++) {
+ p = V[j][i];
+ V[j][i] = V[j][k];
+ V[j][k] = p;
+ }
+ }
+ }
+}
+
+// Nonsymmetric reduction to Hessenberg form.
+function science_vector_decomposeOrthes(H, V) {
+ // This is derived from the Algol procedures orthes and ortran,
+ // by Martin and Wilkinson, Handbook for Auto. Comp.,
+ // Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutines in EISPACK.
+
+ var n = H.length;
+ var ort = [];
+
+ var low = 0;
+ var high = n-1;
+
+ for (var m = low+1; m <= high-1; m++) {
+ // Scale column.
+ var scale = 0;
+ for (var i = m; i <= high; i++) { scale = scale + Math.abs(H[i][m-1]); }
+ if (scale !== 0) {
+
+ // Compute Householder transformation.
+ var h = 0;
+ for (var i = high; i >= m; i--) {
+ ort[i] = H[i][m-1]/scale;
+ h += ort[i] * ort[i];
+ }
+ var g = Math.sqrt(h);
+ if (ort[m] > 0) { g = -g; }
+ h = h - ort[m] * g;
+ ort[m] = ort[m] - g;
+
+ // Apply Householder similarity transformation
+ // H = (I-u*u'/h)*H*(I-u*u')/h)
+ for (var j = m; j < n; j++) {
+ var f = 0;
+ for (var i = high; i >= m; i--) { f += ort[i]*H[i][j]; }
+ f /= h;
+ for (var i = m; i <= high; i++) { H[i][j] -= f*ort[i]; }
+ }
+
+ for (var i = 0; i <= high; i++) {
+ var f = 0;
+ for (var j = high; j >= m; j--) { f += ort[j]*H[i][j]; }
+ f /= h;
+ for (var j = m; j <= high; j++) { H[i][j] -= f*ort[j]; }
+ }
+ ort[m] = scale*ort[m];
+ H[m][m-1] = scale*g;
+ }
+ }
+
+ // Accumulate transformations (Algol's ortran).
+ for (var i = 0; i < n; i++) {
+ for (var j = 0; j < n; j++) { V[i][j] = (i == j ? 1.0 : 0); }
+ }
+
+ for (var m = high-1; m >= low+1; m--) {
+ if (H[m][m-1] != 0) {
+ for (var i = m+1; i <= high; i++) { ort[i] = H[i][m-1]; }
+ for (var j = m; j <= high; j++) {
+ var g = 0;
+ for (var i = m; i <= high; i++) { g += ort[i] * V[i][j]; }
+ // Double division avoids possible underflow
+ g = (g / ort[m]) / H[m][m-1];
+ for (var i = m; i <= high; i++) { V[i][j] += g * ort[i]; }
+ }
+ }
+ }
+}
+
+// Nonsymmetric reduction from Hessenberg to real Schur form.
+function science_vector_decomposeHqr2(d, e, H, V) {
+ // This is derived from the Algol procedure hqr2,
+ // by Martin and Wilkinson, Handbook for Auto. Comp.,
+ // Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ // Initialize
+ var nn = H.length;
+ var n = nn-1;
+ var low = 0;
+ var high = nn-1;
+ var eps = 1e-12;
+ var exshift = 0;
+ var p=0,q=0,r=0,s=0,z=0,t,w,x,y;
+
+ // Store roots isolated by balanc and compute matrix norm
+ var norm = 0;
+ for (var i = 0; i < nn; i++) {
+ if (i < low | i > high) {
+ d[i] = H[i][i];
+ e[i] = 0;
+ }
+ for (var j = Math.max(i-1,0); j < nn; j++) { norm = norm + Math.abs(H[i][j]); }
+ }
+
+ // Outer loop over eigenvalue index
+
+ var iter = 0;
+ while (n >= low) {
+ // Look for single small sub-diagonal element
+ var l = n;
+ while (l > low) {
+ s = Math.abs(H[l-1][l-1]) + Math.abs(H[l][l]);
+ if (s == 0) {
+ s = norm;
+ }
+ if (Math.abs(H[l][l-1]) < eps * s) {
+ break;
+ }
+ l--;
+ }
+
+ // Check for convergence
+ // One root found
+ if (l == n) {
+ H[n][n] = H[n][n] + exshift;
+ d[n] = H[n][n];
+ e[n] = 0;
+ n--;
+ iter = 0;
+
+ // Two roots found
+ } else if (l == n-1) {
+ w = H[n][n-1] * H[n-1][n];
+ p = (H[n-1][n-1] - H[n][n]) / 2.0;
+ q = p * p + w;
+ z = Math.sqrt(Math.abs(q));
+ H[n][n] = H[n][n] + exshift;
+ H[n-1][n-1] = H[n-1][n-1] + exshift;
+ x = H[n][n];
+
+ // Real pair
+ if (q >= 0) {
+ if (p >= 0) {
+ z = p + z;
+ } else {
+ z = p - z;
+ }
+ d[n-1] = x + z;
+ d[n] = d[n-1];
+ if (z != 0) {
+ d[n] = x - w / z;
+ }
+ e[n-1] = 0;
+ e[n] = 0;
+ x = H[n][n-1];
+ s = Math.abs(x) + Math.abs(z);
+ p = x / s;
+ q = z / s;
+ r = Math.sqrt(p * p+q * q);
+ p = p / r;
+ q = q / r;
+
+ // Row modification
+ for (var j = n-1; j < nn; j++) {
+ z = H[n-1][j];
+ H[n-1][j] = q * z + p * H[n][j];
+ H[n][j] = q * H[n][j] - p * z;
+ }
+
+ // Column modification
+ for (var i = 0; i <= n; i++) {
+ z = H[i][n-1];
+ H[i][n-1] = q * z + p * H[i][n];
+ H[i][n] = q * H[i][n] - p * z;
+ }
+
+ // Accumulate transformations
+ for (var i = low; i <= high; i++) {
+ z = V[i][n-1];
+ V[i][n-1] = q * z + p * V[i][n];
+ V[i][n] = q * V[i][n] - p * z;
+ }
+
+ // Complex pair
+ } else {
+ d[n-1] = x + p;
+ d[n] = x + p;
+ e[n-1] = z;
+ e[n] = -z;
+ }
+ n = n - 2;
+ iter = 0;
+
+ // No convergence yet
+ } else {
+
+ // Form shift
+ x = H[n][n];
+ y = 0;
+ w = 0;
+ if (l < n) {
+ y = H[n-1][n-1];
+ w = H[n][n-1] * H[n-1][n];
+ }
+
+ // Wilkinson's original ad hoc shift
+ if (iter == 10) {
+ exshift += x;
+ for (var i = low; i <= n; i++) {
+ H[i][i] -= x;
+ }
+ s = Math.abs(H[n][n-1]) + Math.abs(H[n-1][n-2]);
+ x = y = 0.75 * s;
+ w = -0.4375 * s * s;
+ }
+
+ // MATLAB's new ad hoc shift
+ if (iter == 30) {
+ s = (y - x) / 2.0;
+ s = s * s + w;
+ if (s > 0) {
+ s = Math.sqrt(s);
+ if (y < x) {
+ s = -s;
+ }
+ s = x - w / ((y - x) / 2.0 + s);
+ for (var i = low; i <= n; i++) {
+ H[i][i] -= s;
+ }
+ exshift += s;
+ x = y = w = 0.964;
+ }
+ }
+
+ iter++; // (Could check iteration count here.)
+
+ // Look for two consecutive small sub-diagonal elements
+ var m = n-2;
+ while (m >= l) {
+ z = H[m][m];
+ r = x - z;
+ s = y - z;
+ p = (r * s - w) / H[m+1][m] + H[m][m+1];
+ q = H[m+1][m+1] - z - r - s;
+ r = H[m+2][m+1];
+ s = Math.abs(p) + Math.abs(q) + Math.abs(r);
+ p = p / s;
+ q = q / s;
+ r = r / s;
+ if (m == l) {
+ break;
+ }
+ if (Math.abs(H[m][m-1]) * (Math.abs(q) + Math.abs(r)) <
+ eps * (Math.abs(p) * (Math.abs(H[m-1][m-1]) + Math.abs(z) +
+ Math.abs(H[m+1][m+1])))) {
+ break;
+ }
+ m--;
+ }
+
+ for (var i = m+2; i <= n; i++) {
+ H[i][i-2] = 0;
+ if (i > m+2) { H[i][i-3] = 0; }
+ }
+
+ // Double QR step involving rows l:n and columns m:n
+ for (var k = m; k <= n-1; k++) {
+ var notlast = (k != n-1);
+ if (k != m) {
+ p = H[k][k-1];
+ q = H[k+1][k-1];
+ r = (notlast ? H[k+2][k-1] : 0);
+ x = Math.abs(p) + Math.abs(q) + Math.abs(r);
+ if (x != 0) {
+ p /= x;
+ q /= x;
+ r /= x;
+ }
+ }
+ if (x == 0) { break; }
+ s = Math.sqrt(p * p + q * q + r * r);
+ if (p < 0) { s = -s; }
+ if (s != 0) {
+ if (k != m) H[k][k-1] = -s * x;
+ else if (l != m) H[k][k-1] = -H[k][k-1];
+ p += s;
+ x = p / s;
+ y = q / s;
+ z = r / s;
+ q /= p;
+ r /= p;
+
+ // Row modification
+ for (var j = k; j < nn; j++) {
+ p = H[k][j] + q * H[k+1][j];
+ if (notlast) {
+ p = p + r * H[k+2][j];
+ H[k+2][j] = H[k+2][j] - p * z;
+ }
+ H[k][j] = H[k][j] - p * x;
+ H[k+1][j] = H[k+1][j] - p * y;
+ }
+
+ // Column modification
+ for (var i = 0; i <= Math.min(n,k+3); i++) {
+ p = x * H[i][k] + y * H[i][k+1];
+ if (notlast) {
+ p += z * H[i][k+2];
+ H[i][k+2] = H[i][k+2] - p * r;
+ }
+ H[i][k] = H[i][k] - p;
+ H[i][k+1] = H[i][k+1] - p * q;
+ }
+
+ // Accumulate transformations
+ for (var i = low; i <= high; i++) {
+ p = x * V[i][k] + y * V[i][k+1];
+ if (notlast) {
+ p = p + z * V[i][k+2];
+ V[i][k+2] = V[i][k+2] - p * r;
+ }
+ V[i][k] = V[i][k] - p;
+ V[i][k+1] = V[i][k+1] - p * q;
+ }
+ } // (s != 0)
+ } // k loop
+ } // check convergence
+ } // while (n >= low)
+
+ // Backsubstitute to find vectors of upper triangular form
+ if (norm == 0) { return; }
+
+ for (n = nn-1; n >= 0; n--) {
+ p = d[n];
+ q = e[n];
+
+ // Real vector
+ if (q == 0) {
+ var l = n;
+ H[n][n] = 1.0;
+ for (var i = n-1; i >= 0; i--) {
+ w = H[i][i] - p;
+ r = 0;
+ for (var j = l; j <= n; j++) { r = r + H[i][j] * H[j][n]; }
+ if (e[i] < 0) {
+ z = w;
+ s = r;
+ } else {
+ l = i;
+ if (e[i] == 0) {
+ if (w != 0) {
+ H[i][n] = -r / w;
+ } else {
+ H[i][n] = -r / (eps * norm);
+ }
+ } else {
+ // Solve real equations
+ x = H[i][i+1];
+ y = H[i+1][i];
+ q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
+ t = (x * s - z * r) / q;
+ H[i][n] = t;
+ if (Math.abs(x) > Math.abs(z)) {
+ H[i+1][n] = (-r - w * t) / x;
+ } else {
+ H[i+1][n] = (-s - y * t) / z;
+ }
+ }
+
+ // Overflow control
+ t = Math.abs(H[i][n]);
+ if ((eps * t) * t > 1) {
+ for (var j = i; j <= n; j++) {
+ H[j][n] = H[j][n] / t;
+ }
+ }
+ }
+ }
+ // Complex vector
+ } else if (q < 0) {
+ var l = n-1;
+
+ // Last vector component imaginary so matrix is triangular
+ if (Math.abs(H[n][n-1]) > Math.abs(H[n-1][n])) {
+ H[n-1][n-1] = q / H[n][n-1];
+ H[n-1][n] = -(H[n][n] - p) / H[n][n-1];
+ } else {
+ var zz = science_vector_decomposeCdiv(0,-H[n-1][n],H[n-1][n-1]-p,q);
+ H[n-1][n-1] = zz[0];
+ H[n-1][n] = zz[1];
+ }
+ H[n][n-1] = 0;
+ H[n][n] = 1.0;
+ for (var i = n-2; i >= 0; i--) {
+ var ra,sa,vr,vi;
+ ra = 0;
+ sa = 0;
+ for (var j = l; j <= n; j++) {
+ ra = ra + H[i][j] * H[j][n-1];
+ sa = sa + H[i][j] * H[j][n];
+ }
+ w = H[i][i] - p;
+
+ if (e[i] < 0) {
+ z = w;
+ r = ra;
+ s = sa;
+ } else {
+ l = i;
+ if (e[i] == 0) {
+ var zz = science_vector_decomposeCdiv(-ra,-sa,w,q);
+ H[i][n-1] = zz[0];
+ H[i][n] = zz[1];
+ } else {
+ // Solve complex equations
+ x = H[i][i+1];
+ y = H[i+1][i];
+ vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
+ vi = (d[i] - p) * 2.0 * q;
+ if (vr == 0 & vi == 0) {
+ vr = eps * norm * (Math.abs(w) + Math.abs(q) +
+ Math.abs(x) + Math.abs(y) + Math.abs(z));
+ }
+ var zz = science_vector_decomposeCdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
+ H[i][n-1] = zz[0];
+ H[i][n] = zz[1];
+ if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) {
+ H[i+1][n-1] = (-ra - w * H[i][n-1] + q * H[i][n]) / x;
+ H[i+1][n] = (-sa - w * H[i][n] - q * H[i][n-1]) / x;
+ } else {
+ var zz = science_vector_decomposeCdiv(-r-y*H[i][n-1],-s-y*H[i][n],z,q);
+ H[i+1][n-1] = zz[0];
+ H[i+1][n] = zz[1];
+ }
+ }
+
+ // Overflow control
+ t = Math.max(Math.abs(H[i][n-1]),Math.abs(H[i][n]));
+ if ((eps * t) * t > 1) {
+ for (var j = i; j <= n; j++) {
+ H[j][n-1] = H[j][n-1] / t;
+ H[j][n] = H[j][n] / t;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // Vectors of isolated roots
+ for (var i = 0; i < nn; i++) {
+ if (i < low | i > high) {
+ for (var j = i; j < nn; j++) { V[i][j] = H[i][j]; }
+ }
+ }
+
+ // Back transformation to get eigenvectors of original matrix
+ for (var j = nn-1; j >= low; j--) {
+ for (var i = low; i <= high; i++) {
+ z = 0;
+ for (var k = low; k <= Math.min(j,high); k++) { z = z + V[i][k] * H[k][j]; }
+ V[i][j] = z;
+ }
+ }
+}
+
+// Complex scalar division.
+function science_vector_decomposeCdiv(xr, xi, yr, yi) {
+ var r, d;
+ if (Math.abs(yr) > Math.abs(yi)) {
+ r = yi / yr;
+ d = yr + r * yi;
+ return [(xr + r * xi) / d, (xi - r * xr) / d];
+ } else {
+ r = yr / yi;
+ d = yi + r * yr;
+ return [(r * xr + xi) / d, (r * xi - xr) / d];
+ }
+}
science.vector.cross = function(a, b) {
// TODO how to handle non-3D vectors?
// TODO handle 7D vectors?
diff --git a/science.lin.min.js b/science.lin.min.js
index a470da4..734bfa6 100644
--- a/science.lin.min.js
+++ b/science.lin.min.js
@@ -1 +1 @@
-(function(){science.lin={},science.lin.tridag=function(a,b,c,d,e,f){var g,h;for(g=1;g<f;g++)h=a[g]/b[g-1],b[g]-=h*c[g-1],d[g]-=h*d[g-1];e[f-1]=d[f-1]/b[f-1];for(g=f-2;g>=0;g--)e[g]=(d[g]-c[g]*e[g+1])/b[g]}})()
\ No newline at end of file
+(function(){science.lin={},science.lin.tridag=function(a,b,c,d,e,f){var g,h;for(g=1;g<f;g++)h=a[g]/b[g-1],b[g]-=h*c[g-1],d[g]-=h*d[g-1];e[f-1]=d[f-1]/b[f-1];for(g=f-2;g>=0;g--)e[g]=(d[g]-c[g]*e[g+1])/b[g]}})();
\ No newline at end of file
diff --git a/science.min.js b/science.min.js
index a68af5b..eb9a6b5 100644
--- a/science.min.js
+++ b/science.min.js
@@ -1 +1 @@
-(function(){science={version:"1.5.0"},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.zeroes=function(a){var b=-1,c=[];if(arguments.length===1)while(++b<a)c[b]=0;else while(++b<a)c[b]=science.zeroes.apply(this,Array.prototype.slice.call(arguments,1));return c},science.vector={},science.vector.cross=function(a,b){return[a[1]*b [...]
\ No newline at end of file
+(function(){function a(a,b,c){var d=c.length;for(var e=0;e<d;e++)a[e]=c[d-1][e];for(var f=d-1;f>0;f--){var g=0,h=0;for(var i=0;i<f;i++)g+=Math.abs(a[i]);if(g===0){b[f]=a[f-1];for(var e=0;e<f;e++)a[e]=c[f-1][e],c[f][e]=0,c[e][f]=0}else{for(var i=0;i<f;i++)a[i]/=g,h+=a[i]*a[i];var j=a[f-1],k=Math.sqrt(h);j>0&&(k=-k),b[f]=g*k,h-=j*k,a[f-1]=j-k;for(var e=0;e<f;e++)b[e]=0;for(var e=0;e<f;e++){j=a[e],c[e][f]=j,k=b[e]+c[e][e]*j;for(var i=e+1;i<=f-1;i++)k+=c[i][e]*a[i],b[i]+=c[i][e]*j;b[e]=k}j=0 [...]
\ No newline at end of file
diff --git a/science.stats.min.js b/science.stats.min.js
index d086072..186c5f3 100644
--- a/science.stats.min.js
+++ b/science.stats.min.js
@@ -1 +1 @@
-(function(){function h(a,b){var c=b+1;while(c<a.length&&a[c]===0)c++;return c}function g(a,b,c,d){var e=d[0],f=d[1],g=h(b,f);if(g<a.length&&a[g]-a[c]<a[c]-a[e]){var i=h(b,e);d[0]=i,d[1]=g}}function f(a){return(a=1-a*a*a)*a*a}function e(a){var b=a.length,c=0;while(++c<b)if(a[c-1]>=a[c])return!1;return!0}function d(a){var b=a.length,c=-1;while(++c<b)if(!isFinite(a[c]))return!1;return!0}function c(a,b,c,d){var e=[],f=a+c,g=b.length,h=-1;while(++h<g)e[h]=(a*b[h]+c*d[h])/f;return e}function b [...]
\ No newline at end of file
+(function(){function a(a,b){if(!a||!b||a.length!==b.length)return!1;var c=a.length,d=-1;while(++d<c)if(a[d]!==b[d])return!1;return!0}function b(b,c){var d=c.length;if(b>d)return null;var e=[],f=[],g={},h=0,i=0,j,k,l;while(i<b){if(h===d)return null;var m=Math.floor(Math.random()*d);if(m in g)continue;g[m]=1,h++,k=c[m],l=!0;for(j=0;j<i;j++)if(a(k,e[j])){l=!1;break}l&&(e[i]=k,f[i]=m,i++)}return e}function c(a,b,c,d){var e=[],f=a+c,g=b.length,h=-1;while(++h<g)e[h]=(a*b[h]+c*d[h])/f;return e} [...]
\ No newline at end of file
diff --git a/src/package.js b/src/package.js
index 7d76651..414a18f 100644
--- a/src/package.js
+++ b/src/package.js
@@ -8,8 +8,8 @@ require("util").puts(JSON.stringify({
"homepage": "https://github.com/jasondavies/science.js",
"author": {"name": "Jason Davies", "url": "http://www.jasondavies.com/"},
"repository": {"type": "git", "url": "http://github.com/jasondavies/science.js.git"},
- "dependencies": {
- "uglify-js": "1.0.6",
- "vows": "0.5.10"
+ "devDependencies": {
+ "uglify-js": "1.2.2",
+ "vows": "0.6.0"
}
}, null, 2));
diff --git a/src/vector/decompose.js b/src/vector/decompose.js
new file mode 100644
index 0000000..c5af129
--- /dev/null
+++ b/src/vector/decompose.js
@@ -0,0 +1,767 @@
+science.vector.decompose = function() {
+
+ function decompose(A) {
+ var n = A.length; // column dimension
+ var V = [],
+ d = [],
+ e = [];
+
+ for (var i = 0; i < n; i++) {
+ V[i] = [];
+ d[i] = [];
+ e[i] = [];
+ }
+
+ var symmetric = true;
+ for (var j = 0; j < n; j++) {
+ for (var i = 0; i < n; i++) {
+ if (A[i][j] !== A[j][i]) {
+ symmetric = false;
+ break;
+ }
+ }
+ }
+
+ if (symmetric) {
+ for (var i = 0; i < n; i++) V[i] = A[i].slice();
+
+ // Tridiagonalize.
+ science_vector_decomposeTred2(d, e, V);
+
+ // Diagonalize.
+ science_vector_decomposeTql2(d, e, V);
+ } else {
+ var H = [];
+ for (var j = 0; j < n; j++) H[i] = A.slice();
+
+ // Reduce to Hessenberg form.
+ science_vector_decomposeOrthes(H, V);
+
+ // Reduce Hessenberg to real Schur form.
+ science_vector_decomposeHqr2(d, e, H, V);
+ }
+
+ var D = [];
+ for (var i=0; i<n; i++) {
+ D[i] = [];
+ for (var j=0; j<n; j++) D[i][j] = 0;
+ D[i][i] = d[i];
+ if (e[i] > 0) D[i][i+1] = e[i];
+ else if (e[i] < 0) D[i][i-1] = e[i];
+ }
+ return {D: D, V: V};
+ }
+
+//
+// /** Return the real parts of the eigenvalues
+// @return real(diag(D))
+// */
+//
+// public double[] getRealEigenvalues () {
+// return d;
+// }
+//
+// /** Return the imaginary parts of the eigenvalues
+// @return imag(diag(D))
+// */
+//
+// public double[] getImagEigenvalues () {
+// return e;
+// }
+
+ /** Return the block diagonal eigenvalue matrix
+ @return D
+ */
+
+ /*
+ this.getD = function() {
+ var X = new Matrix(this.n,this.n);
+ var D = X.getArray();
+ for (var i = 0; i < this.n; i++) {
+ for (var j = 0; j < this.n; j++) {
+ D[i][j] = 0;
+ }
+ D[i][i] = this.d[i];
+ if (this.e[i] > 0) {
+ D[i][i+1] = this.e[i];
+ } else if (this.e[i] < 0) {
+ D[i][i-1] = this.e[i];
+ }
+ }
+ return X;
+ }
+ */
+
+ return decompose;
+};
+
+// Symmetric Householder reduction to tridiagonal form.
+function science_vector_decomposeTred2(d, e, V) {
+ // This is derived from the Algol procedures tred2 by
+ // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
+ // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ var n = V.length;
+
+ for (var j = 0; j < n; j++) { d[j] = V[n-1][j]; }
+
+ // Householder reduction to tridiagonal form.
+ for (var i = n-1; i > 0; i--) {
+ // Scale to avoid under/overflow.
+
+ var scale = 0,
+ h = 0;
+ for (var k = 0; k < i; k++) { scale = scale + Math.abs(d[k]); }
+ if (scale === 0) {
+ e[i] = d[i-1];
+ for (var j = 0; j < i; j++) {
+ d[j] = V[i-1][j];
+ V[i][j] = 0;
+ V[j][i] = 0;
+ }
+ } else {
+ // Generate Householder vector.
+ for (var k = 0; k < i; k++) {
+ d[k] /= scale;
+ h += d[k] * d[k];
+ }
+ var f = d[i-1];
+ var g = Math.sqrt(h);
+ if (f > 0) g = -g;
+ e[i] = scale * g;
+ h = h - f * g;
+ d[i-1] = f - g;
+ for (var j = 0; j < i; j++) e[j] = 0;
+
+ // Apply similarity transformation to remaining columns.
+
+ for (var j = 0; j < i; j++) {
+ f = d[j];
+ V[j][i] = f;
+ g = e[j] + V[j][j] * f;
+ for (var k = j+1; k <= i-1; k++) {
+ g += V[k][j] * d[k];
+ e[k] += V[k][j] * f;
+ }
+ e[j] = g;
+ }
+ f = 0;
+ for (var j = 0; j < i; j++) {
+ e[j] /= h;
+ f += e[j] * d[j];
+ }
+ var hh = f / (h + h);
+ for (var j = 0; j < i; j++) { e[j] -= hh * d[j]; }
+ for (var j = 0; j < i; j++) {
+ f = d[j];
+ g = e[j];
+ for (var k = j; k <= i-1; k++) { V[k][j] -= (f * e[k] + g * d[k]); }
+ d[j] = V[i-1][j];
+ V[i][j] = 0;
+ }
+ }
+ d[i] = h;
+ }
+
+ // Accumulate transformations.
+ for (var i = 0; i < n-1; i++) {
+ V[n-1][i] = V[i][i];
+ V[i][i] = 1.0;
+ var h = d[i+1];
+ if (h != 0) {
+ for (var k = 0; k <= i; k++) { d[k] = V[k][i+1] / h; }
+ for (var j = 0; j <= i; j++) {
+ var g = 0;
+ for (var k = 0; k <= i; k++) { g += V[k][i+1] * V[k][j]; }
+ for (var k = 0; k <= i; k++) { V[k][j] -= g * d[k]; }
+ }
+ }
+ for (var k = 0; k <= i; k++) { V[k][i+1] = 0; }
+ }
+ for (var j = 0; j < n; j++) {
+ d[j] = V[n-1][j];
+ V[n-1][j] = 0;
+ }
+ V[n-1][n-1] = 1;
+ e[0] = 0;
+}
+
+// Symmetric tridiagonal QL algorithm.
+function science_vector_decomposeTql2(d, e, V) {
+ // This is derived from the Algol procedures tql2, by
+ // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
+ // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ var n = V.length;
+
+ for (var i = 1; i < n; i++) { e[i-1] = e[i]; }
+ e[n-1] = 0;
+
+ var f = 0;
+ var tst1 = 0;
+ var eps = 1e-12;
+ for (var l = 0; l < n; l++) {
+ // Find small subdiagonal element
+ tst1 = Math.max(tst1, Math.abs(d[l]) + Math.abs(e[l]));
+ var m = l;
+ while (m < n) {
+ if (Math.abs(e[m]) <= eps*tst1) { break; }
+ m++;
+ }
+
+ // If m == l, d[l] is an eigenvalue,
+ // otherwise, iterate.
+ if (m > l) {
+ var iter = 0;
+ do {
+ iter++; // (Could check iteration count here.)
+
+ // Compute implicit shift
+ var g = d[l];
+ var p = (d[l+1] - g) / (2.0 * e[l]);
+ var r = science.hypot(p, 1);
+ if (p < 0) {
+ r = -r;
+ }
+ d[l] = e[l] / (p + r);
+ d[l+1] = e[l] * (p + r);
+ var dl1 = d[l+1];
+ var h = g - d[l];
+ for (var i = l+2; i < n; i++) {
+ d[i] -= h;
+ }
+ f = f + h;
+
+ // Implicit QL transformation.
+ p = d[m];
+ var c = 1.0;
+ var c2 = c;
+ var c3 = c;
+ var el1 = e[l+1];
+ var s = 0;
+ var s2 = 0;
+ for (var i = m-1; i >= l; i--) {
+ c3 = c2;
+ c2 = c;
+ s2 = s;
+ g = c * e[i];
+ h = c * p;
+ r = science.hypot(p,e[i]);
+ e[i+1] = s * r;
+ s = e[i] / r;
+ c = p / r;
+ p = c * d[i] - s * g;
+ d[i+1] = h + s * (c * g + s * d[i]);
+
+ // Accumulate transformation.
+ for (var k = 0; k < n; k++) {
+ h = V[k][i+1];
+ V[k][i+1] = s * V[k][i] + c * h;
+ V[k][i] = c * V[k][i] - s * h;
+ }
+ }
+ p = -s * s2 * c3 * el1 * e[l] / dl1;
+ e[l] = s * p;
+ d[l] = c * p;
+
+ // Check for convergence.
+ } while (Math.abs(e[l]) > eps*tst1);
+ }
+ d[l] = d[l] + f;
+ e[l] = 0;
+ }
+
+ // Sort eigenvalues and corresponding vectors.
+ for (var i = 0; i < n-1; i++) {
+ var k = i;
+ var p = d[i];
+ for (var j = i+1; j < n; j++) {
+ if (d[j] < p) {
+ k = j;
+ p = d[j];
+ }
+ }
+ if (k != i) {
+ d[k] = d[i];
+ d[i] = p;
+ for (var j = 0; j < n; j++) {
+ p = V[j][i];
+ V[j][i] = V[j][k];
+ V[j][k] = p;
+ }
+ }
+ }
+}
+
+// Nonsymmetric reduction to Hessenberg form.
+function science_vector_decomposeOrthes(H, V) {
+ // This is derived from the Algol procedures orthes and ortran,
+ // by Martin and Wilkinson, Handbook for Auto. Comp.,
+ // Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutines in EISPACK.
+
+ var n = H.length;
+ var ort = [];
+
+ var low = 0;
+ var high = n-1;
+
+ for (var m = low+1; m <= high-1; m++) {
+ // Scale column.
+ var scale = 0;
+ for (var i = m; i <= high; i++) { scale = scale + Math.abs(H[i][m-1]); }
+ if (scale !== 0) {
+
+ // Compute Householder transformation.
+ var h = 0;
+ for (var i = high; i >= m; i--) {
+ ort[i] = H[i][m-1]/scale;
+ h += ort[i] * ort[i];
+ }
+ var g = Math.sqrt(h);
+ if (ort[m] > 0) { g = -g; }
+ h = h - ort[m] * g;
+ ort[m] = ort[m] - g;
+
+ // Apply Householder similarity transformation
+ // H = (I-u*u'/h)*H*(I-u*u')/h)
+ for (var j = m; j < n; j++) {
+ var f = 0;
+ for (var i = high; i >= m; i--) { f += ort[i]*H[i][j]; }
+ f /= h;
+ for (var i = m; i <= high; i++) { H[i][j] -= f*ort[i]; }
+ }
+
+ for (var i = 0; i <= high; i++) {
+ var f = 0;
+ for (var j = high; j >= m; j--) { f += ort[j]*H[i][j]; }
+ f /= h;
+ for (var j = m; j <= high; j++) { H[i][j] -= f*ort[j]; }
+ }
+ ort[m] = scale*ort[m];
+ H[m][m-1] = scale*g;
+ }
+ }
+
+ // Accumulate transformations (Algol's ortran).
+ for (var i = 0; i < n; i++) {
+ for (var j = 0; j < n; j++) { V[i][j] = (i == j ? 1.0 : 0); }
+ }
+
+ for (var m = high-1; m >= low+1; m--) {
+ if (H[m][m-1] != 0) {
+ for (var i = m+1; i <= high; i++) { ort[i] = H[i][m-1]; }
+ for (var j = m; j <= high; j++) {
+ var g = 0;
+ for (var i = m; i <= high; i++) { g += ort[i] * V[i][j]; }
+ // Double division avoids possible underflow
+ g = (g / ort[m]) / H[m][m-1];
+ for (var i = m; i <= high; i++) { V[i][j] += g * ort[i]; }
+ }
+ }
+ }
+}
+
+// Nonsymmetric reduction from Hessenberg to real Schur form.
+function science_vector_decomposeHqr2(d, e, H, V) {
+ // This is derived from the Algol procedure hqr2,
+ // by Martin and Wilkinson, Handbook for Auto. Comp.,
+ // Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ // Initialize
+ var nn = H.length;
+ var n = nn-1;
+ var low = 0;
+ var high = nn-1;
+ var eps = 1e-12;
+ var exshift = 0;
+ var p=0,q=0,r=0,s=0,z=0,t,w,x,y;
+
+ // Store roots isolated by balanc and compute matrix norm
+ var norm = 0;
+ for (var i = 0; i < nn; i++) {
+ if (i < low | i > high) {
+ d[i] = H[i][i];
+ e[i] = 0;
+ }
+ for (var j = Math.max(i-1,0); j < nn; j++) { norm = norm + Math.abs(H[i][j]); }
+ }
+
+ // Outer loop over eigenvalue index
+
+ var iter = 0;
+ while (n >= low) {
+ // Look for single small sub-diagonal element
+ var l = n;
+ while (l > low) {
+ s = Math.abs(H[l-1][l-1]) + Math.abs(H[l][l]);
+ if (s == 0) {
+ s = norm;
+ }
+ if (Math.abs(H[l][l-1]) < eps * s) {
+ break;
+ }
+ l--;
+ }
+
+ // Check for convergence
+ // One root found
+ if (l == n) {
+ H[n][n] = H[n][n] + exshift;
+ d[n] = H[n][n];
+ e[n] = 0;
+ n--;
+ iter = 0;
+
+ // Two roots found
+ } else if (l == n-1) {
+ w = H[n][n-1] * H[n-1][n];
+ p = (H[n-1][n-1] - H[n][n]) / 2.0;
+ q = p * p + w;
+ z = Math.sqrt(Math.abs(q));
+ H[n][n] = H[n][n] + exshift;
+ H[n-1][n-1] = H[n-1][n-1] + exshift;
+ x = H[n][n];
+
+ // Real pair
+ if (q >= 0) {
+ if (p >= 0) {
+ z = p + z;
+ } else {
+ z = p - z;
+ }
+ d[n-1] = x + z;
+ d[n] = d[n-1];
+ if (z != 0) {
+ d[n] = x - w / z;
+ }
+ e[n-1] = 0;
+ e[n] = 0;
+ x = H[n][n-1];
+ s = Math.abs(x) + Math.abs(z);
+ p = x / s;
+ q = z / s;
+ r = Math.sqrt(p * p+q * q);
+ p = p / r;
+ q = q / r;
+
+ // Row modification
+ for (var j = n-1; j < nn; j++) {
+ z = H[n-1][j];
+ H[n-1][j] = q * z + p * H[n][j];
+ H[n][j] = q * H[n][j] - p * z;
+ }
+
+ // Column modification
+ for (var i = 0; i <= n; i++) {
+ z = H[i][n-1];
+ H[i][n-1] = q * z + p * H[i][n];
+ H[i][n] = q * H[i][n] - p * z;
+ }
+
+ // Accumulate transformations
+ for (var i = low; i <= high; i++) {
+ z = V[i][n-1];
+ V[i][n-1] = q * z + p * V[i][n];
+ V[i][n] = q * V[i][n] - p * z;
+ }
+
+ // Complex pair
+ } else {
+ d[n-1] = x + p;
+ d[n] = x + p;
+ e[n-1] = z;
+ e[n] = -z;
+ }
+ n = n - 2;
+ iter = 0;
+
+ // No convergence yet
+ } else {
+
+ // Form shift
+ x = H[n][n];
+ y = 0;
+ w = 0;
+ if (l < n) {
+ y = H[n-1][n-1];
+ w = H[n][n-1] * H[n-1][n];
+ }
+
+ // Wilkinson's original ad hoc shift
+ if (iter == 10) {
+ exshift += x;
+ for (var i = low; i <= n; i++) {
+ H[i][i] -= x;
+ }
+ s = Math.abs(H[n][n-1]) + Math.abs(H[n-1][n-2]);
+ x = y = 0.75 * s;
+ w = -0.4375 * s * s;
+ }
+
+ // MATLAB's new ad hoc shift
+ if (iter == 30) {
+ s = (y - x) / 2.0;
+ s = s * s + w;
+ if (s > 0) {
+ s = Math.sqrt(s);
+ if (y < x) {
+ s = -s;
+ }
+ s = x - w / ((y - x) / 2.0 + s);
+ for (var i = low; i <= n; i++) {
+ H[i][i] -= s;
+ }
+ exshift += s;
+ x = y = w = 0.964;
+ }
+ }
+
+ iter++; // (Could check iteration count here.)
+
+ // Look for two consecutive small sub-diagonal elements
+ var m = n-2;
+ while (m >= l) {
+ z = H[m][m];
+ r = x - z;
+ s = y - z;
+ p = (r * s - w) / H[m+1][m] + H[m][m+1];
+ q = H[m+1][m+1] - z - r - s;
+ r = H[m+2][m+1];
+ s = Math.abs(p) + Math.abs(q) + Math.abs(r);
+ p = p / s;
+ q = q / s;
+ r = r / s;
+ if (m == l) {
+ break;
+ }
+ if (Math.abs(H[m][m-1]) * (Math.abs(q) + Math.abs(r)) <
+ eps * (Math.abs(p) * (Math.abs(H[m-1][m-1]) + Math.abs(z) +
+ Math.abs(H[m+1][m+1])))) {
+ break;
+ }
+ m--;
+ }
+
+ for (var i = m+2; i <= n; i++) {
+ H[i][i-2] = 0;
+ if (i > m+2) { H[i][i-3] = 0; }
+ }
+
+ // Double QR step involving rows l:n and columns m:n
+ for (var k = m; k <= n-1; k++) {
+ var notlast = (k != n-1);
+ if (k != m) {
+ p = H[k][k-1];
+ q = H[k+1][k-1];
+ r = (notlast ? H[k+2][k-1] : 0);
+ x = Math.abs(p) + Math.abs(q) + Math.abs(r);
+ if (x != 0) {
+ p /= x;
+ q /= x;
+ r /= x;
+ }
+ }
+ if (x == 0) { break; }
+ s = Math.sqrt(p * p + q * q + r * r);
+ if (p < 0) { s = -s; }
+ if (s != 0) {
+ if (k != m) H[k][k-1] = -s * x;
+ else if (l != m) H[k][k-1] = -H[k][k-1];
+ p += s;
+ x = p / s;
+ y = q / s;
+ z = r / s;
+ q /= p;
+ r /= p;
+
+ // Row modification
+ for (var j = k; j < nn; j++) {
+ p = H[k][j] + q * H[k+1][j];
+ if (notlast) {
+ p = p + r * H[k+2][j];
+ H[k+2][j] = H[k+2][j] - p * z;
+ }
+ H[k][j] = H[k][j] - p * x;
+ H[k+1][j] = H[k+1][j] - p * y;
+ }
+
+ // Column modification
+ for (var i = 0; i <= Math.min(n,k+3); i++) {
+ p = x * H[i][k] + y * H[i][k+1];
+ if (notlast) {
+ p += z * H[i][k+2];
+ H[i][k+2] = H[i][k+2] - p * r;
+ }
+ H[i][k] = H[i][k] - p;
+ H[i][k+1] = H[i][k+1] - p * q;
+ }
+
+ // Accumulate transformations
+ for (var i = low; i <= high; i++) {
+ p = x * V[i][k] + y * V[i][k+1];
+ if (notlast) {
+ p = p + z * V[i][k+2];
+ V[i][k+2] = V[i][k+2] - p * r;
+ }
+ V[i][k] = V[i][k] - p;
+ V[i][k+1] = V[i][k+1] - p * q;
+ }
+ } // (s != 0)
+ } // k loop
+ } // check convergence
+ } // while (n >= low)
+
+ // Backsubstitute to find vectors of upper triangular form
+ if (norm == 0) { return; }
+
+ for (n = nn-1; n >= 0; n--) {
+ p = d[n];
+ q = e[n];
+
+ // Real vector
+ if (q == 0) {
+ var l = n;
+ H[n][n] = 1.0;
+ for (var i = n-1; i >= 0; i--) {
+ w = H[i][i] - p;
+ r = 0;
+ for (var j = l; j <= n; j++) { r = r + H[i][j] * H[j][n]; }
+ if (e[i] < 0) {
+ z = w;
+ s = r;
+ } else {
+ l = i;
+ if (e[i] == 0) {
+ if (w != 0) {
+ H[i][n] = -r / w;
+ } else {
+ H[i][n] = -r / (eps * norm);
+ }
+ } else {
+ // Solve real equations
+ x = H[i][i+1];
+ y = H[i+1][i];
+ q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
+ t = (x * s - z * r) / q;
+ H[i][n] = t;
+ if (Math.abs(x) > Math.abs(z)) {
+ H[i+1][n] = (-r - w * t) / x;
+ } else {
+ H[i+1][n] = (-s - y * t) / z;
+ }
+ }
+
+ // Overflow control
+ t = Math.abs(H[i][n]);
+ if ((eps * t) * t > 1) {
+ for (var j = i; j <= n; j++) {
+ H[j][n] = H[j][n] / t;
+ }
+ }
+ }
+ }
+ // Complex vector
+ } else if (q < 0) {
+ var l = n-1;
+
+ // Last vector component imaginary so matrix is triangular
+ if (Math.abs(H[n][n-1]) > Math.abs(H[n-1][n])) {
+ H[n-1][n-1] = q / H[n][n-1];
+ H[n-1][n] = -(H[n][n] - p) / H[n][n-1];
+ } else {
+ var zz = science_vector_decomposeCdiv(0,-H[n-1][n],H[n-1][n-1]-p,q);
+ H[n-1][n-1] = zz[0];
+ H[n-1][n] = zz[1];
+ }
+ H[n][n-1] = 0;
+ H[n][n] = 1.0;
+ for (var i = n-2; i >= 0; i--) {
+ var ra,sa,vr,vi;
+ ra = 0;
+ sa = 0;
+ for (var j = l; j <= n; j++) {
+ ra = ra + H[i][j] * H[j][n-1];
+ sa = sa + H[i][j] * H[j][n];
+ }
+ w = H[i][i] - p;
+
+ if (e[i] < 0) {
+ z = w;
+ r = ra;
+ s = sa;
+ } else {
+ l = i;
+ if (e[i] == 0) {
+ var zz = science_vector_decomposeCdiv(-ra,-sa,w,q);
+ H[i][n-1] = zz[0];
+ H[i][n] = zz[1];
+ } else {
+ // Solve complex equations
+ x = H[i][i+1];
+ y = H[i+1][i];
+ vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
+ vi = (d[i] - p) * 2.0 * q;
+ if (vr == 0 & vi == 0) {
+ vr = eps * norm * (Math.abs(w) + Math.abs(q) +
+ Math.abs(x) + Math.abs(y) + Math.abs(z));
+ }
+ var zz = science_vector_decomposeCdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
+ H[i][n-1] = zz[0];
+ H[i][n] = zz[1];
+ if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) {
+ H[i+1][n-1] = (-ra - w * H[i][n-1] + q * H[i][n]) / x;
+ H[i+1][n] = (-sa - w * H[i][n] - q * H[i][n-1]) / x;
+ } else {
+ var zz = science_vector_decomposeCdiv(-r-y*H[i][n-1],-s-y*H[i][n],z,q);
+ H[i+1][n-1] = zz[0];
+ H[i+1][n] = zz[1];
+ }
+ }
+
+ // Overflow control
+ t = Math.max(Math.abs(H[i][n-1]),Math.abs(H[i][n]));
+ if ((eps * t) * t > 1) {
+ for (var j = i; j <= n; j++) {
+ H[j][n-1] = H[j][n-1] / t;
+ H[j][n] = H[j][n] / t;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // Vectors of isolated roots
+ for (var i = 0; i < nn; i++) {
+ if (i < low | i > high) {
+ for (var j = i; j < nn; j++) { V[i][j] = H[i][j]; }
+ }
+ }
+
+ // Back transformation to get eigenvectors of original matrix
+ for (var j = nn-1; j >= low; j--) {
+ for (var i = low; i <= high; i++) {
+ z = 0;
+ for (var k = low; k <= Math.min(j,high); k++) { z = z + V[i][k] * H[k][j]; }
+ V[i][j] = z;
+ }
+ }
+}
+
+// Complex scalar division.
+function science_vector_decomposeCdiv(xr, xi, yr, yi) {
+ var r, d;
+ if (Math.abs(yr) > Math.abs(yi)) {
+ r = yi / yr;
+ d = yr + r * yi;
+ return [(xr + r * xi) / d, (xi - r * xr) / d];
+ } else {
+ r = yr / yi;
+ d = yi + r * yr;
+ return [(r * xr + xi) / d, (r * xi - xr) / d];
+ }
+}
diff --git a/test/vector/decompose-test.js b/test/vector/decompose-test.js
new file mode 100644
index 0000000..334da3d
--- /dev/null
+++ b/test/vector/decompose-test.js
@@ -0,0 +1,23 @@
+require("../../science");
+
+var vows = require("vows"),
+ assert = require("assert");
+
+var suite = vows.describe("science.vector.decompose");
+
+suite.addBatch({
+ "decompose": {
+ "symmetric": function() {
+ var decompose = science.vector.decompose();
+ var A = [[1, 1, 1], [1, 2, 3], [1, 3, 6]];
+ var result = decompose(A);
+ assert.inDelta(
+ science.vector.multiply(A, result.V),
+ science.vector.multiply(result.V, result.D),
+ 1e-6
+ );
+ }
+ }
+});
+
+suite.export(module);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/science.js.git
More information about the Pkg-javascript-commits
mailing list