Public
Edited
Oct 23, 2023
Importers
48 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Matrixf = Eigen.Matrixf
Insert cell
Insert cell
Matrixf.zeros([2, 2], invalidation).show()
Insert cell
Matrixf.identity([2, 2], invalidation).show()
Insert cell
Matrixf.random([2, 4], invalidation).show()
Insert cell
Insert cell
Matrixf.identity([3, 3]).show()
Insert cell
Insert cell
Insert cell
Insert cell
data = new Float32Array(new Array(m * m).fill(0).map((d, i) => i))
Insert cell
// BAD! This leaks memory since the promise never resolves, triggering cleanup!!
Matrixf.fromArray(data, [m, m], new Promise(() => {})).show()
Insert cell
Insert cell
Insert cell
Insert cell
viewof A = Matrixf.from(
(i, j) => [-1, 2, -1][j - i + 1] || 0,
[n, n],
invalidation
).show('A')
Insert cell
viewof b = Matrixf.random([n, 1], invalidation).show('b')
Insert cell
viewof x = A.inverse(invalidation)
.mul(b, invalidation)
.show('x = A^{-1} b')
Insert cell
Insert cell
Eigen.ensure(invalidation, () =>
A.inverse()
.mul(b)
.transpose()
).show()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
LU = A.partialPivLU(invalidation)
Insert cell
Insert cell
LU.solve(b, invalidation).show('b')
Insert cell
Insert cell
Insert cell
fullPivLU = A.fullPivLU(invalidation)
Insert cell
Insert cell
fullPivLU.solve(b, invalidation).show('b')
Insert cell
Insert cell
Insert cell
LLT = A.llt(invalidation)
Insert cell
Insert cell
LLT.solve(b, invalidation).show('x')
Insert cell
Insert cell
Insert cell
LDLT = A.ldlt(invalidation)
Insert cell
Insert cell
LDLT.solve(b, invalidation).show('x')
Insert cell
Insert cell
Insert cell
Insert cell
QR = A.householderQR(invalidation)
Insert cell
QR.solve(b, invalidation).show('x')
Insert cell
Insert cell
Insert cell
colPivQR = A.colPivHouseholderQR(invalidation)
Insert cell
colPivQR.solve(b, invalidation).show('x')
Insert cell
Insert cell
Insert cell
fullPivQR = A.fullPivHouseholderQR(invalidation)
Insert cell
fullPivQR.solve(b, invalidation).show('x')
Insert cell
Insert cell
Insert cell
svd = A.bdcSVD(Eigen.ComputeThinU | Eigen.ComputeThinV, invalidation)
Insert cell
Insert cell
Insert cell
svd.solve(b, invalidation).show('x')
Insert cell
Insert cell
eig = Eigen.ensure(invalidation, () =>
A.selfAdjointEigenSolver(Eigen.ComputeEigenvectors)
)
Insert cell
viewof eigenvalues = eig.eigenvalues(invalidation).show('diga(\\Lambda)')
Insert cell
viewof eigenvectors = eig.eigenvectors(invalidation).show()
Insert cell
Insert cell
Insert cell
({
buffer: A.buffer.slice(),
shape: A.shape,
stride: A.stride,
offset: A.offset
})
Insert cell
Insert cell
Insert cell
ndarray(A.buffer, A.shape, A.offset, A.stride)
Insert cell
Insert cell
Insert cell
stdlib.array(A.buffer, {
shape: A.shape,
stride: A.stride,
offset: A.offset,
order: 'column-major'
})
Insert cell
Insert cell
ndarray(A.buffer.slice(), A.shape, A.stride, A.offset)
Insert cell
Insert cell
Insert cell
A.toString()
Insert cell
Insert cell
Insert cell
Insert cell
showMatrix(A)
Insert cell
Insert cell
Insert cell
Insert cell
Eigen.ensure(invalidation, () => {
var A = Matrixf.zeros([8, 8]);

// Set diagonal bands:
A.diagonal(3).setConstant(-5);
A.diagonal(0).setConstant(20);
A.diagonal(-3).assign(Matrixf.columnVector([12, 13, 14, 15, 16]));

return A.show();
})
Insert cell
Insert cell
Insert cell
Eigen.ensure(invalidation, () => {
var B = Matrixf.zeros([5, 5]);

B.block(0, 1, [2, 2]).setConstant(25);

B.block(1, 3, [2, 2]).assign(Matrixf.fromArray([10, 11, 12, 13], [2, 2]));

B.bottomLeftCorner([1, 2]).setConstant(35);
B.bottomRightCorner([2, 1]).setConstant(42);
B.topRightCorner([1, 1]).setConstant(57);

return B.show();
})
Insert cell
Insert cell
Eigen.ensure(invalidation, () => {
// Construct a large matrix:
var B = Matrixf.random([20, 20]);

// Define a block which references the original:
var submatrix = B.block(5, 2, [3, 2]);

// Construct and return a new matrix:
return Matrixf.fromBlock(submatrix).show();
})
Insert cell
Insert cell
Insert cell
Eigen.ensure(invalidation, () => {
// Construct a large matrix:
var A = Matrixf.zeros([4, 4]);
A.col(0).setConstant(5);
A.row(2).setConstant(4);
A.col(3).assign(Matrixf.columnVector([1, 2, 3, 4]));

return A.show();
})
Insert cell
Insert cell
A.map((row, col, value) => value + 2, invalidation).show()
Insert cell
Insert cell
A.squaredNorm()
Insert cell
A.norm() // (square root of .squaredNorm())
Insert cell
A.l1Norm()
Insert cell
A.lInfNorm()
Insert cell
Insert cell
Eigen.ensure(invalidation, tmp =>
svd
.matrixU(tmp)
.mul(Matrixf.fromDiagonal(svd.singularValues(tmp), tmp), tmp)
.mul(svd.matrixV(tmp).transpose(tmp), tmp)
.map((i, j, value) => Math.round(value))
).show('A = U \\Sigma V^T')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more