feat:node-modules

This commit is contained in:
houjunxiang
2025-11-24 10:26:18 +08:00
parent 753766893b
commit 8a3e48d856
8825 changed files with 567399 additions and 1 deletions

View File

@@ -0,0 +1,698 @@
import { clone } from '../../../utils/object.js';
export function createComplexEigs(_ref) {
var {
addScalar,
subtract,
flatten,
multiply,
multiplyScalar,
divideScalar,
sqrt,
abs,
bignumber,
diag,
size,
reshape,
inv,
qr,
usolve,
usolveAll,
equal,
complex,
larger,
smaller,
matrixFromColumns,
dot
} = _ref;
/**
* @param {number[][]} arr the matrix to find eigenvalues of
* @param {number} N size of the matrix
* @param {number|BigNumber} prec precision, anything lower will be considered zero
* @param {'number'|'BigNumber'|'Complex'} type
* @param {boolean} findVectors should we find eigenvectors?
*
* @returns {{ values: number[], vectors: number[][] }}
*/
function complexEigs(arr, N, prec, type) {
var findVectors = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
// TODO check if any row/col are zero except the diagonal
// make sure corresponding rows and columns have similar magnitude
// important because of numerical stability
// MODIFIES arr by side effect!
var R = balance(arr, N, prec, type, findVectors);
// R is the row transformation matrix
// arr = A' = R A R^-1, A is the original matrix
// (if findVectors is false, R is undefined)
// (And so to return to original matrix: A = R^-1 arr R)
// TODO if magnitudes of elements vary over many orders,
// move greatest elements to the top left corner
// using similarity transformations, reduce the matrix
// to Hessenberg form (upper triangular plus one subdiagonal row)
// updates the transformation matrix R with new row operationsq
// MODIFIES arr by side effect!
reduceToHessenberg(arr, N, prec, type, findVectors, R);
// still true that original A = R^-1 arr R)
// find eigenvalues
var {
values,
C
} = iterateUntilTriangular(arr, N, prec, type, findVectors);
// values is the list of eigenvalues, C is the column
// transformation matrix that transforms arr, the hessenberg
// matrix, to upper triangular
// (So U = C^-1 arr C and the relationship between current arr
// and original A is unchanged.)
if (findVectors) {
var eigenvectors = findEigenvectors(arr, N, C, R, values, prec, type);
return {
values,
eigenvectors
};
}
return {
values
};
}
/**
* @param {number[][]} arr
* @param {number} N
* @param {number} prec
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {number[][]}
*/
function balance(arr, N, prec, type, findVectors) {
var big = type === 'BigNumber';
var cplx = type === 'Complex';
var realzero = big ? bignumber(0) : 0;
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
var realone = big ? bignumber(1) : 1;
// base of the floating-point arithmetic
var radix = big ? bignumber(10) : 2;
var radixSq = multiplyScalar(radix, radix);
// the diagonal transformation matrix R
var Rdiag;
if (findVectors) {
Rdiag = Array(N).fill(one);
}
// this isn't the only time we loop thru the matrix...
var last = false;
while (!last) {
// ...haha I'm joking! unless...
last = true;
for (var i = 0; i < N; i++) {
// compute the taxicab norm of i-th column and row
// TODO optimize for complex numbers
var colNorm = realzero;
var rowNorm = realzero;
for (var j = 0; j < N; j++) {
if (i === j) continue;
colNorm = addScalar(colNorm, abs(arr[j][i]));
rowNorm = addScalar(rowNorm, abs(arr[i][j]));
}
if (!equal(colNorm, 0) && !equal(rowNorm, 0)) {
// find integer power closest to balancing the matrix
// (we want to scale only by integer powers of radix,
// so that we don't lose any precision due to round-off)
var f = realone;
var c = colNorm;
var rowDivRadix = divideScalar(rowNorm, radix);
var rowMulRadix = multiplyScalar(rowNorm, radix);
while (smaller(c, rowDivRadix)) {
c = multiplyScalar(c, radixSq);
f = multiplyScalar(f, radix);
}
while (larger(c, rowMulRadix)) {
c = divideScalar(c, radixSq);
f = divideScalar(f, radix);
}
// check whether balancing is needed
// condition = (c + rowNorm) / f < 0.95 * (colNorm + rowNorm)
var condition = smaller(divideScalar(addScalar(c, rowNorm), f), multiplyScalar(addScalar(colNorm, rowNorm), 0.95));
// apply balancing similarity transformation
if (condition) {
// we should loop once again to check whether
// another rebalancing is needed
last = false;
var g = divideScalar(1, f);
for (var _j = 0; _j < N; _j++) {
if (i === _j) {
continue;
}
arr[i][_j] = multiplyScalar(arr[i][_j], g);
arr[_j][i] = multiplyScalar(arr[_j][i], f);
}
// keep track of transformations
if (findVectors) {
Rdiag[i] = multiplyScalar(Rdiag[i], g);
}
}
}
}
}
// return the diagonal row transformation matrix
return findVectors ? diag(Rdiag) : null;
}
/**
* @param {number[][]} arr
* @param {number} N
* @param {number} prec
* @param {'number'|'BigNumber'|'Complex'} type
* @param {boolean} findVectors
* @param {number[][]} R the row transformation matrix that will be modified
*/
function reduceToHessenberg(arr, N, prec, type, findVectors, R) {
var big = type === 'BigNumber';
var cplx = type === 'Complex';
var zero = big ? bignumber(0) : cplx ? complex(0) : 0;
if (big) {
prec = bignumber(prec);
}
for (var i = 0; i < N - 2; i++) {
// Find the largest subdiag element in the i-th col
var maxIndex = 0;
var max = zero;
for (var j = i + 1; j < N; j++) {
var el = arr[j][i];
if (smaller(abs(max), abs(el))) {
max = el;
maxIndex = j;
}
}
// This col is pivoted, no need to do anything
if (smaller(abs(max), prec)) {
continue;
}
if (maxIndex !== i + 1) {
// Interchange maxIndex-th and (i+1)-th row
var tmp1 = arr[maxIndex];
arr[maxIndex] = arr[i + 1];
arr[i + 1] = tmp1;
// Interchange maxIndex-th and (i+1)-th column
for (var _j2 = 0; _j2 < N; _j2++) {
var tmp2 = arr[_j2][maxIndex];
arr[_j2][maxIndex] = arr[_j2][i + 1];
arr[_j2][i + 1] = tmp2;
}
// keep track of transformations
if (findVectors) {
var tmp3 = R[maxIndex];
R[maxIndex] = R[i + 1];
R[i + 1] = tmp3;
}
}
// Reduce following rows and columns
for (var _j3 = i + 2; _j3 < N; _j3++) {
var n = divideScalar(arr[_j3][i], max);
if (n === 0) {
continue;
}
// from j-th row subtract n-times (i+1)th row
for (var k = 0; k < N; k++) {
arr[_j3][k] = subtract(arr[_j3][k], multiplyScalar(n, arr[i + 1][k]));
}
// to (i+1)th column add n-times j-th column
for (var _k = 0; _k < N; _k++) {
arr[_k][i + 1] = addScalar(arr[_k][i + 1], multiplyScalar(n, arr[_k][_j3]));
}
// keep track of transformations
if (findVectors) {
for (var _k2 = 0; _k2 < N; _k2++) {
R[_j3][_k2] = subtract(R[_j3][_k2], multiplyScalar(n, R[i + 1][_k2]));
}
}
}
}
return R;
}
/**
* @returns {{values: values, C: Matrix}}
* @see Press, Wiliams: Numerical recipes in Fortran 77
* @see https://en.wikipedia.org/wiki/QR_algorithm
*/
function iterateUntilTriangular(A, N, prec, type, findVectors) {
var big = type === 'BigNumber';
var cplx = type === 'Complex';
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
if (big) {
prec = bignumber(prec);
}
// The Francis Algorithm
// The core idea of this algorithm is that doing successive
// A' = QtAQ transformations will eventually converge to block-
// upper-triangular with diagonal blocks either 1x1 or 2x2.
// The Q here is the one from the QR decomposition, A = QR.
// Since the eigenvalues of a block-upper-triangular matrix are
// the eigenvalues of its diagonal blocks and we know how to find
// eigenvalues of a 2x2 matrix, we know the eigenvalues of A.
var arr = clone(A);
// the list of converged eigenvalues
var lambdas = [];
// size of arr, which will get smaller as eigenvalues converge
var n = N;
// the diagonal of the block-diagonal matrix that turns
// converged 2x2 matrices into upper triangular matrices
var Sdiag = [];
// N×N matrix describing the overall transformation done during the QR algorithm
var Qtotal = findVectors ? diag(Array(N).fill(one)) : undefined;
// nxn matrix describing the QR transformations done since last convergence
var Qpartial = findVectors ? diag(Array(n).fill(one)) : undefined;
// last eigenvalue converged before this many steps
var lastConvergenceBefore = 0;
while (lastConvergenceBefore <= 100) {
lastConvergenceBefore += 1;
// TODO if the convergence is slow, do something clever
// Perform the factorization
var k = arr[n - 1][n - 1]; // TODO this is apparently a somewhat
// old-fashioned choice; ideally set close to an eigenvalue, or
// perhaps better yet switch to the implicit QR version that is sometimes
// specifically called the "Francis algorithm" that is alluded to
// in the following TODO. (Or perhaps we switch to an independently
// optimized third-party package for the linear algebra operations...)
for (var i = 0; i < n; i++) {
arr[i][i] = subtract(arr[i][i], k);
}
// TODO do an implicit QR transformation
var {
Q,
R
} = qr(arr);
arr = multiply(R, Q);
for (var _i = 0; _i < n; _i++) {
arr[_i][_i] = addScalar(arr[_i][_i], k);
}
// keep track of transformations
if (findVectors) {
Qpartial = multiply(Qpartial, Q);
}
// The rightmost diagonal element converged to an eigenvalue
if (n === 1 || smaller(abs(arr[n - 1][n - 2]), prec)) {
lastConvergenceBefore = 0;
lambdas.push(arr[n - 1][n - 1]);
// keep track of transformations
if (findVectors) {
Sdiag.unshift([[1]]);
inflateMatrix(Qpartial, N);
Qtotal = multiply(Qtotal, Qpartial);
if (n > 1) {
Qpartial = diag(Array(n - 1).fill(one));
}
}
// reduce the matrix size
n -= 1;
arr.pop();
for (var _i2 = 0; _i2 < n; _i2++) {
arr[_i2].pop();
}
// The rightmost diagonal 2x2 block converged
} else if (n === 2 || smaller(abs(arr[n - 2][n - 3]), prec)) {
lastConvergenceBefore = 0;
var ll = eigenvalues2x2(arr[n - 2][n - 2], arr[n - 2][n - 1], arr[n - 1][n - 2], arr[n - 1][n - 1]);
lambdas.push(...ll);
// keep track of transformations
if (findVectors) {
Sdiag.unshift(jordanBase2x2(arr[n - 2][n - 2], arr[n - 2][n - 1], arr[n - 1][n - 2], arr[n - 1][n - 1], ll[0], ll[1], prec, type));
inflateMatrix(Qpartial, N);
Qtotal = multiply(Qtotal, Qpartial);
if (n > 2) {
Qpartial = diag(Array(n - 2).fill(one));
}
}
// reduce the matrix size
n -= 2;
arr.pop();
arr.pop();
for (var _i3 = 0; _i3 < n; _i3++) {
arr[_i3].pop();
arr[_i3].pop();
}
}
if (n === 0) {
break;
}
}
// standard sorting
lambdas.sort((a, b) => +subtract(abs(a), abs(b)));
// the algorithm didn't converge
if (lastConvergenceBefore > 100) {
var err = Error('The eigenvalues failed to converge. Only found these eigenvalues: ' + lambdas.join(', '));
err.values = lambdas;
err.vectors = [];
throw err;
}
// combine the overall QR transformation Qtotal with the subsequent
// transformation S that turns the diagonal 2x2 blocks to upper triangular
var C = findVectors ? multiply(Qtotal, blockDiag(Sdiag, N)) : undefined;
return {
values: lambdas,
C
};
}
/**
* @param {Matrix} A hessenberg-form matrix
* @param {number} N size of A
* @param {Matrix} C column transformation matrix that turns A into upper triangular
* @param {Matrix} R similarity that turns original matrix into A
* @param {number[]} values array of eigenvalues of A
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {number[][]} eigenvalues
*/
function findEigenvectors(A, N, C, R, values, prec, type) {
var Cinv = inv(C);
var U = multiply(Cinv, A, C);
var big = type === 'BigNumber';
var cplx = type === 'Complex';
var zero = big ? bignumber(0) : cplx ? complex(0) : 0;
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
// turn values into a kind of "multiset"
// this way it is easier to find eigenvectors
var uniqueValues = [];
var multiplicities = [];
for (var lambda of values) {
var i = indexOf(uniqueValues, lambda, equal);
if (i === -1) {
uniqueValues.push(lambda);
multiplicities.push(1);
} else {
multiplicities[i] += 1;
}
}
// find eigenvectors by solving U lambdaE = 0
// TODO replace with an iterative eigenvector algorithm
// (this one might fail for imprecise eigenvalues)
var vectors = [];
var len = uniqueValues.length;
var b = Array(N).fill(zero);
var E = diag(Array(N).fill(one));
var _loop = function _loop() {
var lambda = uniqueValues[_i4];
var S = subtract(U, multiply(lambda, E)); // the characteristic matrix
var solutions = usolveAll(S, b);
solutions.shift(); // ignore the null vector
// looks like we missed something, try inverse iteration
// But if that fails, just presume that the original matrix truly
// was defective.
while (solutions.length < multiplicities[_i4]) {
var approxVec = inverseIterate(S, N, solutions, prec, type);
if (approxVec === null) {
break;
} // no more vectors were found
solutions.push(approxVec);
}
// Transform back into original array coordinates
var correction = multiply(inv(R), C);
solutions = solutions.map(v => multiply(correction, v));
vectors.push(...solutions.map(v => ({
value: lambda,
vector: flatten(v)
})));
};
for (var _i4 = 0; _i4 < len; _i4++) {
_loop();
}
return vectors;
}
/**
* Compute the eigenvalues of an 2x2 matrix
* @return {[number,number]}
*/
function eigenvalues2x2(a, b, c, d) {
// lambda_+- = 1/2 trA +- 1/2 sqrt( tr^2 A - 4 detA )
var trA = addScalar(a, d);
var detA = subtract(multiplyScalar(a, d), multiplyScalar(b, c));
var x = multiplyScalar(trA, 0.5);
var y = multiplyScalar(sqrt(subtract(multiplyScalar(trA, trA), multiplyScalar(4, detA))), 0.5);
return [addScalar(x, y), subtract(x, y)];
}
/**
* For an 2x2 matrix compute the transformation matrix S,
* so that SAS^-1 is an upper triangular matrix
* @return {[[number,number],[number,number]]}
* @see https://math.berkeley.edu/~ogus/old/Math_54-05/webfoils/jordan.pdf
* @see http://people.math.harvard.edu/~knill/teaching/math21b2004/exhibits/2dmatrices/index.html
*/
function jordanBase2x2(a, b, c, d, l1, l2, prec, type) {
var big = type === 'BigNumber';
var cplx = type === 'Complex';
var zero = big ? bignumber(0) : cplx ? complex(0) : 0;
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
// matrix is already upper triangular
// return an identity matrix
if (smaller(abs(c), prec)) {
return [[one, zero], [zero, one]];
}
// matrix is diagonalizable
// return its eigenvectors as columns
if (larger(abs(subtract(l1, l2)), prec)) {
return [[subtract(l1, d), subtract(l2, d)], [c, c]];
}
// matrix is not diagonalizable
// compute diagonal elements of N = A - lambdaI
var na = subtract(a, l1);
var nd = subtract(d, l1);
// col(N,2) = 0 implies S = ( col(N,1), e_1 )
// col(N,2) != 0 implies S = ( col(N,2), e_2 )
if (smaller(abs(b), prec) && smaller(abs(nd), prec)) {
return [[na, one], [c, zero]];
} else {
return [[b, zero], [nd, one]];
}
}
/**
* Enlarge the matrix from nxn to NxN, setting the new
* elements to 1 on diagonal and 0 elsewhere
*/
function inflateMatrix(arr, N) {
// add columns
for (var i = 0; i < arr.length; i++) {
arr[i].push(...Array(N - arr[i].length).fill(0));
}
// add rows
for (var _i5 = arr.length; _i5 < N; _i5++) {
arr.push(Array(N).fill(0));
arr[_i5][_i5] = 1;
}
return arr;
}
/**
* Create a block-diagonal matrix with the given square matrices on the diagonal
* @param {Matrix[] | number[][][]} arr array of matrices to be placed on the diagonal
* @param {number} N the size of the resulting matrix
*/
function blockDiag(arr, N) {
var M = [];
for (var i = 0; i < N; i++) {
M[i] = Array(N).fill(0);
}
var I = 0;
for (var sub of arr) {
var n = sub.length;
for (var _i6 = 0; _i6 < n; _i6++) {
for (var j = 0; j < n; j++) {
M[I + _i6][I + j] = sub[_i6][j];
}
}
I += n;
}
return M;
}
/**
* Finds the index of an element in an array using a custom equality function
* @template T
* @param {Array<T>} arr array in which to search
* @param {T} el the element to find
* @param {function(T, T): boolean} fn the equality function, first argument is an element of `arr`, the second is always `el`
* @returns {number} the index of `el`, or -1 when it's not in `arr`
*/
function indexOf(arr, el, fn) {
for (var i = 0; i < arr.length; i++) {
if (fn(arr[i], el)) {
return i;
}
}
return -1;
}
/**
* Provided a near-singular upper-triangular matrix A and a list of vectors,
* finds an eigenvector of A with the smallest eigenvalue, which is orthogonal
* to each vector in the list
* @template T
* @param {T[][]} A near-singular square matrix
* @param {number} N dimension
* @param {T[][]} orthog list of vectors
* @param {number} prec epsilon
* @param {'number'|'BigNumber'|'Complex'} type
* @return {T[] | null} eigenvector
*
* @see Numerical Recipes for Fortran 77 11.7 Eigenvalues or Eigenvectors by Inverse Iteration
*/
function inverseIterate(A, N, orthog, prec, type) {
var largeNum = type === 'BigNumber' ? bignumber(1000) : 1000;
var b; // the vector
// you better choose a random vector before I count to five
var i = 0;
for (; i < 5; ++i) {
b = randomOrthogonalVector(N, orthog, type);
try {
b = usolve(A, b);
} catch (_unused) {
// That direction didn't work, likely because the original matrix
// was defective. But still make the full number of tries...
continue;
}
if (larger(norm(b), largeNum)) {
break;
}
}
if (i >= 5) {
return null; // couldn't find any orthogonal vector in the image
}
// you better converge before I count to ten
i = 0;
while (true) {
var c = usolve(A, b);
if (smaller(norm(orthogonalComplement(b, [c])), prec)) {
break;
}
if (++i >= 10) {
return null;
}
b = normalize(c);
}
return b;
}
/**
* Generates a random unit vector of dimension N, orthogonal to each vector in the list
* @template T
* @param {number} N dimension
* @param {T[][]} orthog list of vectors
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {T[]} random vector
*/
function randomOrthogonalVector(N, orthog, type) {
var big = type === 'BigNumber';
var cplx = type === 'Complex';
// generate random vector with the correct type
var v = Array(N).fill(0).map(_ => 2 * Math.random() - 1);
if (big) {
v = v.map(n => bignumber(n));
}
if (cplx) {
v = v.map(n => complex(n));
}
// project to orthogonal complement
v = orthogonalComplement(v, orthog);
// normalize
return normalize(v, type);
}
/**
* Project vector v to the orthogonal complement of an array of vectors
*/
function orthogonalComplement(v, orthog) {
var vectorShape = size(v);
for (var w of orthog) {
w = reshape(w, vectorShape); // make sure this is just a vector computation
// v := v (w, v)/|w|^2 w
v = subtract(v, multiply(divideScalar(dot(w, v), dot(w, w)), w));
}
return v;
}
/**
* Calculate the norm of a vector.
* We can't use math.norm because factory can't handle circular dependency.
* Seriously, I'm really fed up with factory.
*/
function norm(v) {
return abs(sqrt(dot(v, v)));
}
/**
* Normalize a vector
* @template T
* @param {T[]} v
* @param {'number'|'BigNumber'|'Complex'} type
* @returns {T[]} normalized vec
*/
function normalize(v, type) {
var big = type === 'BigNumber';
var cplx = type === 'Complex';
var one = big ? bignumber(1) : cplx ? complex(1) : 1;
return multiply(divideScalar(one, norm(v)), v);
}
return complexEigs;
}

View File

@@ -0,0 +1,297 @@
import { clone } from '../../../utils/object.js';
export function createRealSymmetric(_ref) {
var {
config,
addScalar,
subtract,
abs,
atan,
cos,
sin,
multiplyScalar,
inv,
bignumber,
multiply,
add
} = _ref;
/**
* @param {number[] | BigNumber[]} arr
* @param {number} N
* @param {number} prec
* @param {'number' | 'BigNumber'} type
*/
function main(arr, N) {
var prec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : config.relTol;
var type = arguments.length > 3 ? arguments[3] : undefined;
var computeVectors = arguments.length > 4 ? arguments[4] : undefined;
if (type === 'number') {
return diag(arr, prec, computeVectors);
}
if (type === 'BigNumber') {
return diagBig(arr, prec, computeVectors);
}
throw TypeError('Unsupported data type: ' + type);
}
// diagonalization implementation for number (efficient)
function diag(x, precision, computeVectors) {
var N = x.length;
var e0 = Math.abs(precision / N);
var psi;
var Sij;
if (computeVectors) {
Sij = new Array(N);
// Sij is Identity Matrix
for (var i = 0; i < N; i++) {
Sij[i] = Array(N).fill(0);
Sij[i][i] = 1.0;
}
}
// initial error
var Vab = getAij(x);
while (Math.abs(Vab[1]) >= Math.abs(e0)) {
var _i = Vab[0][0];
var j = Vab[0][1];
psi = getTheta(x[_i][_i], x[j][j], x[_i][j]);
x = x1(x, psi, _i, j);
if (computeVectors) Sij = Sij1(Sij, psi, _i, j);
Vab = getAij(x);
}
var Ei = Array(N).fill(0); // eigenvalues
for (var _i2 = 0; _i2 < N; _i2++) {
Ei[_i2] = x[_i2][_i2];
}
return sorting(clone(Ei), Sij, computeVectors);
}
// diagonalization implementation for bigNumber
function diagBig(x, precision, computeVectors) {
var N = x.length;
var e0 = abs(precision / N);
var psi;
var Sij;
if (computeVectors) {
Sij = new Array(N);
// Sij is Identity Matrix
for (var i = 0; i < N; i++) {
Sij[i] = Array(N).fill(0);
Sij[i][i] = 1.0;
}
}
// initial error
var Vab = getAijBig(x);
while (abs(Vab[1]) >= abs(e0)) {
var _i3 = Vab[0][0];
var j = Vab[0][1];
psi = getThetaBig(x[_i3][_i3], x[j][j], x[_i3][j]);
x = x1Big(x, psi, _i3, j);
if (computeVectors) Sij = Sij1Big(Sij, psi, _i3, j);
Vab = getAijBig(x);
}
var Ei = Array(N).fill(0); // eigenvalues
for (var _i4 = 0; _i4 < N; _i4++) {
Ei[_i4] = x[_i4][_i4];
}
// return [clone(Ei), clone(Sij)]
return sorting(clone(Ei), Sij, computeVectors);
}
// get angle
function getTheta(aii, ajj, aij) {
var denom = ajj - aii;
if (Math.abs(denom) <= config.relTol) {
return Math.PI / 4.0;
} else {
return 0.5 * Math.atan(2.0 * aij / (ajj - aii));
}
}
// get angle
function getThetaBig(aii, ajj, aij) {
var denom = subtract(ajj, aii);
if (abs(denom) <= config.relTol) {
return bignumber(-1).acos().div(4);
} else {
return multiplyScalar(0.5, atan(multiply(2.0, aij, inv(denom))));
}
}
// update eigvec
function Sij1(Sij, theta, i, j) {
var N = Sij.length;
var c = Math.cos(theta);
var s = Math.sin(theta);
var Ski = Array(N).fill(0);
var Skj = Array(N).fill(0);
for (var k = 0; k < N; k++) {
Ski[k] = c * Sij[k][i] - s * Sij[k][j];
Skj[k] = s * Sij[k][i] + c * Sij[k][j];
}
for (var _k = 0; _k < N; _k++) {
Sij[_k][i] = Ski[_k];
Sij[_k][j] = Skj[_k];
}
return Sij;
}
// update eigvec for overlap
function Sij1Big(Sij, theta, i, j) {
var N = Sij.length;
var c = cos(theta);
var s = sin(theta);
var Ski = Array(N).fill(bignumber(0));
var Skj = Array(N).fill(bignumber(0));
for (var k = 0; k < N; k++) {
Ski[k] = subtract(multiplyScalar(c, Sij[k][i]), multiplyScalar(s, Sij[k][j]));
Skj[k] = addScalar(multiplyScalar(s, Sij[k][i]), multiplyScalar(c, Sij[k][j]));
}
for (var _k2 = 0; _k2 < N; _k2++) {
Sij[_k2][i] = Ski[_k2];
Sij[_k2][j] = Skj[_k2];
}
return Sij;
}
// update matrix
function x1Big(Hij, theta, i, j) {
var N = Hij.length;
var c = bignumber(cos(theta));
var s = bignumber(sin(theta));
var c2 = multiplyScalar(c, c);
var s2 = multiplyScalar(s, s);
var Aki = Array(N).fill(bignumber(0));
var Akj = Array(N).fill(bignumber(0));
// 2cs Hij
var csHij = multiply(bignumber(2), c, s, Hij[i][j]);
// Aii
var Aii = addScalar(subtract(multiplyScalar(c2, Hij[i][i]), csHij), multiplyScalar(s2, Hij[j][j]));
var Ajj = add(multiplyScalar(s2, Hij[i][i]), csHij, multiplyScalar(c2, Hij[j][j]));
// 0 to i
for (var k = 0; k < N; k++) {
Aki[k] = subtract(multiplyScalar(c, Hij[i][k]), multiplyScalar(s, Hij[j][k]));
Akj[k] = addScalar(multiplyScalar(s, Hij[i][k]), multiplyScalar(c, Hij[j][k]));
}
// Modify Hij
Hij[i][i] = Aii;
Hij[j][j] = Ajj;
Hij[i][j] = bignumber(0);
Hij[j][i] = bignumber(0);
// 0 to i
for (var _k3 = 0; _k3 < N; _k3++) {
if (_k3 !== i && _k3 !== j) {
Hij[i][_k3] = Aki[_k3];
Hij[_k3][i] = Aki[_k3];
Hij[j][_k3] = Akj[_k3];
Hij[_k3][j] = Akj[_k3];
}
}
return Hij;
}
// update matrix
function x1(Hij, theta, i, j) {
var N = Hij.length;
var c = Math.cos(theta);
var s = Math.sin(theta);
var c2 = c * c;
var s2 = s * s;
var Aki = Array(N).fill(0);
var Akj = Array(N).fill(0);
// Aii
var Aii = c2 * Hij[i][i] - 2 * c * s * Hij[i][j] + s2 * Hij[j][j];
var Ajj = s2 * Hij[i][i] + 2 * c * s * Hij[i][j] + c2 * Hij[j][j];
// 0 to i
for (var k = 0; k < N; k++) {
Aki[k] = c * Hij[i][k] - s * Hij[j][k];
Akj[k] = s * Hij[i][k] + c * Hij[j][k];
}
// Modify Hij
Hij[i][i] = Aii;
Hij[j][j] = Ajj;
Hij[i][j] = 0;
Hij[j][i] = 0;
// 0 to i
for (var _k4 = 0; _k4 < N; _k4++) {
if (_k4 !== i && _k4 !== j) {
Hij[i][_k4] = Aki[_k4];
Hij[_k4][i] = Aki[_k4];
Hij[j][_k4] = Akj[_k4];
Hij[_k4][j] = Akj[_k4];
}
}
return Hij;
}
// get max off-diagonal value from Upper Diagonal
function getAij(Mij) {
var N = Mij.length;
var maxMij = 0;
var maxIJ = [0, 1];
for (var i = 0; i < N; i++) {
for (var j = i + 1; j < N; j++) {
if (Math.abs(maxMij) < Math.abs(Mij[i][j])) {
maxMij = Math.abs(Mij[i][j]);
maxIJ = [i, j];
}
}
}
return [maxIJ, maxMij];
}
// get max off-diagonal value from Upper Diagonal
function getAijBig(Mij) {
var N = Mij.length;
var maxMij = 0;
var maxIJ = [0, 1];
for (var i = 0; i < N; i++) {
for (var j = i + 1; j < N; j++) {
if (abs(maxMij) < abs(Mij[i][j])) {
maxMij = abs(Mij[i][j]);
maxIJ = [i, j];
}
}
}
return [maxIJ, maxMij];
}
// sort results
function sorting(E, S, computeVectors) {
var N = E.length;
var values = Array(N);
var vecs;
if (computeVectors) {
vecs = Array(N);
for (var k = 0; k < N; k++) {
vecs[k] = Array(N);
}
}
for (var i = 0; i < N; i++) {
var minID = 0;
var minE = E[0];
for (var j = 0; j < E.length; j++) {
if (abs(E[j]) < abs(minE)) {
minID = j;
minE = E[minID];
}
}
values[i] = E.splice(minID, 1)[0];
if (computeVectors) {
for (var _k5 = 0; _k5 < N; _k5++) {
vecs[i][_k5] = S[_k5][minID];
S[_k5].splice(minID, 1);
}
}
}
if (!computeVectors) return {
values
};
var eigenvectors = vecs.map((vector, i) => ({
value: values[i],
vector
}));
return {
values,
eigenvectors
};
}
return main;
}