ts-quantum
Version:
TypeScript library for quantum mechanics calculations and utilities
165 lines • 6.08 kB
JavaScript
;
/**
* Advanced matrix function implementations for quantum operations
*
* Provides higher-level matrix functions building on core matrix operations.
* Implements functions like matrix logarithm, square root, and arbitrary
* function application using eigendecomposition.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.matrixCos = exports.matrixSin = exports.matrixPower = exports.matrixSquareRoot = exports.matrixLogarithm = exports.matrixFunction = void 0;
const matrixOperations_1 = require("./matrixOperations");
const math = __importStar(require("mathjs"));
/**
* Applies an arbitrary function to a diagonalizable matrix
*
* For a matrix A with eigendecomposition A = UDU†, computes f(A) = Uf(D)U†
* where f(D) applies the function to each eigenvalue on the diagonal.
*
* @param matrix Matrix to apply function to
* @param func Function to apply to eigenvalues
* @returns Matrix with function applied to eigenvalues
*/
function matrixFunction(matrix, func) {
const dim = matrix.length;
// For Hermitian matrices, use eigendecomposition
if ((0, matrixOperations_1.isHermitian)(matrix)) {
const { values, vectors } = (0, matrixOperations_1.eigenDecomposition)(matrix, { computeEigenvectors: true });
if (!vectors) {
throw new Error('Failed to compute eigenvectors');
}
// Apply function to eigenvalues
const funcValues = values.map(v => func(v));
// Construct the diagonal matrix of f(D)
const fD = Array(dim).fill(null).map((_, i) => Array(dim).fill(null).map((_, j) => i === j ? funcValues[i] : math.complex(0, 0)));
// The vectors are already in the correct format
const U = vectors;
// Calculate U†
const UDagger = (0, matrixOperations_1.adjoint)(U);
// Multiply: result = U * f(D) * U†
const temp = (0, matrixOperations_1.multiplyMatrices)(U, fD);
return (0, matrixOperations_1.multiplyMatrices)(temp, UDagger);
}
else {
// For non-Hermitian matrices, use Jordan normal form or other methods
throw new Error('Matrix function for non-Hermitian matrices not implemented');
}
}
exports.matrixFunction = matrixFunction;
/**
* Calculates the matrix logarithm
*
* For a positive definite matrix A, computes log(A) such that exp(log(A)) = A
*
* @param matrix Matrix to calculate logarithm of (must be positive definite)
* @returns The matrix logarithm
*/
function matrixLogarithm(matrix) {
return matrixFunction(matrix, (x) => {
// Natural logarithm of complex number
const r = Math.sqrt(x.re * x.re + x.im * x.im);
const theta = Math.atan2(x.im, x.re);
return math.complex(Math.log(r), theta);
});
}
exports.matrixLogarithm = matrixLogarithm;
/**
* Calculates the matrix square root
*
* For a matrix A, computes √A such that √A × √A = A
*
* @param matrix Matrix to calculate square root of
* @returns The matrix square root
*/
function matrixSquareRoot(matrix) {
// Verify matrix structure
matrix.forEach((row, i) => {
row.forEach((elem, j) => {
if (!math.isComplex(elem)) {
// console.log(`Invalid element at [${i}][${j}]:`, elem);
}
});
});
try {
const result = matrixFunction(matrix, x => math.sqrt(x));
// console.log('Square root calculation successful');
return result;
}
catch (e) {
console.error('Error in matrix square root:', e);
if (e instanceof Error) {
console.error('Stack:', e.stack);
}
throw e;
}
}
exports.matrixSquareRoot = matrixSquareRoot;
/**
* Calculates an arbitrary power of a matrix
*
* For a matrix A and a number p, computes A^p
*
* @param matrix Base matrix
* @param power Power to raise matrix to
* @returns The matrix raised to the specified power
*/
function matrixPower(matrix, power) {
return matrixFunction(matrix, (x) => {
// Complex number raised to real power
const r = Math.pow(Math.sqrt(x.re * x.re + x.im * x.im), power);
const theta = Math.atan2(x.im, x.re) * power;
return math.complex(r * Math.cos(theta), r * Math.sin(theta));
});
}
exports.matrixPower = matrixPower;
;
/**
* Calculates the matrix sine function
*
* @param matrix Input matrix
* @returns The matrix sine
*/
function matrixSin(matrix) {
return matrixFunction(matrix, (x) => {
// Sine of complex number
return math.complex(Math.sin(x.re) * Math.cosh(x.im), Math.cos(x.re) * Math.sinh(x.im));
});
}
exports.matrixSin = matrixSin;
/**
* Calculates the matrix cosine function
*
* @param matrix Input matrix
* @returns The matrix cosine
*/
function matrixCos(matrix) {
return matrixFunction(matrix, (x) => {
// Cosine of complex number
return math.complex(Math.cos(x.re) * Math.cosh(x.im), -Math.sin(x.re) * Math.sinh(x.im));
});
}
exports.matrixCos = matrixCos;
//# sourceMappingURL=matrixFunctions.js.map