The solve function solves a linear matrix equation Ax = b, where A is a square coefficient matrix and b is either a single vector (1D) or a matrix of multiple right-hand sides (2D).
Returns:NDArray<Double, D> (or Float/Complex) — the solution vector or matrix x.
Example — single right-hand side
Solve the system:
2x + y = 5
x + 3y = 7
val a = mk.ndarray(mk[mk[2.0, 1.0], mk[1.0, 3.0]])
val b = mk.ndarray(mk[5.0, 7.0])
val x = mk.linalg.solve(a, b)
// x ≈ [1.6, 1.8]
// Verify: a dot x ≈ b
Example — multiple right-hand sides
Solve Ax = B where B has multiple columns:
val a = mk.ndarray(mk[mk[1.0, 2.0], mk[3.0, 4.0]])
val b = mk.ndarray(mk[mk[5.0, 6.0], mk[7.0, 8.0]])
val x = mk.linalg.solve(a, b)
// x: 2×2 matrix, each column is the solution for the corresponding column of b
Pitfalls
The coefficient matrix A must be square and non-singular (invertible). A singular matrix will cause an error.
Numeric overloads return Double results, even if the inputs are Int or Long.
The pure Kotlin engine supports solve for numeric types. Complex types require the OpenBLAS engine.