Liking cljdoc? Tell your friends :D

org.soulspace.qclojure.domain.math.core

Facade for linear algebra operations with pluggable backends.

Goals:

  • Provide a single, meaningful API for matrix and vector operations.
  • Allow swapping implementations (pure Clojure Math, Fastmath, Neanderthal) without changing call sites.
  • Keep data shapes simple: matrices as vectors of row vectors; vectors as vectors of numbers.
  • Handle conversions between FastMath Vec2 format and backend-specific representations.

Default backend: :pure, implemented via org.soulspace.qclojure.domain.math.clojure-math.

Use set-backend! to change the global backend, or with-backend to override in a dynamic scope.

Facade for linear algebra operations with pluggable backends.

Goals:
 - Provide a single, meaningful API for matrix and vector operations.
 - Allow swapping implementations (pure Clojure Math, Fastmath, Neanderthal) without changing call sites.
 - Keep data shapes simple: matrices as vectors of row vectors; vectors as vectors of numbers.
 - Handle conversions between FastMath Vec2 format and backend-specific representations.
 
 Default backend: :pure, implemented via org.soulspace.qclojure.domain.math.clojure-math.
 
 Use `set-backend!` to change the global backend, or `with-backend` to override in a dynamic scope.
raw docstring

*backend*clj

Current math backend instance. Bind dynamically with with-backend. Defaults to a FastMathComplexBackend instance.

Current math backend instance. Bind dynamically with `with-backend`.
Defaults to a FastMathComplexBackend instance.
sourceraw docstring

*backend-key*clj

Current math backend key for informational purposes.

Current math backend key for informational purposes.
sourceraw docstring

*tolerance*clj

source

addclj

(add A B)

Perform matrix addition A + B.

Parameters:

  • A: First matrix
  • B: Second matrix with same dimensions as A

Returns: Matrix representing A + B

Perform matrix addition A + B.

Parameters:
- A: First matrix
- B: Second matrix with same dimensions as A

Returns:
Matrix representing A + B
sourceraw docstring

available-backendsclj

(available-backends)

Return the set of available backend keys.

Returns: Set of backend keywords that can be used with set-backend! or create-backend

Return the set of available backend keys.

Returns:
Set of backend keywords that can be used with set-backend! or create-backend
sourceraw docstring

cholesky-decompositionclj

(cholesky-decomposition A)

Compute the Cholesky decomposition A = L L†.

Parameters:

  • A: Positive semidefinite matrix

Returns: Map containing:

  • :L - Lower triangular matrix such that A = L L†

Note: May throw or return nil if A is not positive semidefinite

Compute the Cholesky decomposition A = L L†.

Parameters:
- A: Positive semidefinite matrix

Returns:
Map containing:
- :L - Lower triangular matrix such that A = L L†

Note:
May throw or return nil if A is not positive semidefinite
sourceraw docstring

complex-matrixclj

(complex-matrix real-m imag-m)

Create a complex matrix from real & imaginary part matrices.

Parameters:

  • real-m: matrix (vector of row vectors) of real parts
  • imag-m: matrix of imaginary parts (same shape)

Returns complex matrix as Vec2 format [[#vec2 [...], ...], ...].

Create a complex matrix from real & imaginary part matrices.

Parameters:
- real-m: matrix (vector of row vectors) of real parts
- imag-m: matrix of imaginary parts (same shape)

Returns complex matrix as Vec2 format [[#vec2 [...], ...], ...].
sourceraw docstring

complex-vectorclj

(complex-vector real-part imag-part)

Create a complex vector from parallel real and imaginary part collections.

Parameters:

  • real-part: sequence of real components
  • imag-part: sequence of imaginary components (same length)

Returns: Complex vector as Vec2 format [#vec2 [...], #vec2 [...], ...].

Example: (complex-vector [1 0] [0 1]) ; => [#vec2 [1.0, 0.0], #vec2 [0.0, 1.0]]

Create a complex vector from parallel real and imaginary part collections.

Parameters:
- real-part: sequence of real components
- imag-part: sequence of imaginary components (same length)

Returns:
Complex vector as Vec2 format [#vec2 [...], #vec2 [...], ...].

Example:
(complex-vector [1 0] [0 1])  ; => [#vec2 [1.0, 0.0], #vec2 [0.0, 1.0]]
sourceraw docstring

condition-numberclj

(condition-number A)

Compute the 2-norm condition number κ₂(A) = σ_max / σ_min.

Parameters:

  • A: Square matrix

Returns: Positive real number representing the condition number

Note: Higher values indicate numerical instability in linear solves

Compute the 2-norm condition number κ₂(A) = σ_max / σ_min.

Parameters:
- A: Square matrix

Returns:
Positive real number representing the condition number

Note:
Higher values indicate numerical instability in linear solves
sourceraw docstring

conjugate-transposeclj

(conjugate-transpose A)

Compute the conjugate transpose Aᴴ (Hermitian adjoint).

Parameters:

  • A: Matrix

Returns: Matrix representing the conjugate transpose of A

Note: For real matrices, this is equivalent to transpose

Compute the conjugate transpose Aᴴ (Hermitian adjoint).

Parameters:
- A: Matrix

Returns:
Matrix representing the conjugate transpose of A

Note:
For real matrices, this is equivalent to transpose
sourceraw docstring

create-backendclj

(create-backend backend-key)
(create-backend backend-key opts)

Create a backend instance for the given key and optional configuration.

Parameters:

  • backend-key: Keyword identifying the backend type (from available-backends)
  • opts: Optional map of backend-specific configuration options

Returns: Backend instance implementing the mathematical operation protocols

Create a backend instance for the given key and optional configuration.

Parameters:
- backend-key: Keyword identifying the backend type (from available-backends)
- opts: Optional map of backend-specific configuration options

Returns:
Backend instance implementing the mathematical operation protocols
sourceraw docstring

current-toleranceclj

(current-tolerance)

Return the effective tolerance used for approximate numeric comparisons.

This reads the dynamic var tolerance. Backend specific overrides (if any) should bind tolerance when performing computations so tests and client code can query a single source of truth.

Returns: Current tolerance value as a double

Return the effective tolerance used for approximate numeric comparisons.

This reads the dynamic var *tolerance*. Backend specific overrides (if any)
should bind *tolerance* when performing computations so tests and client
code can query a single source of truth.

Returns:
Current tolerance value as a double
sourceraw docstring

eigen-generalclj

(eigen-general A)

Compute eigenvalues for a general (possibly non-Hermitian) matrix.

Parameters:

  • A: Square matrix

Returns: Map containing:

  • :eigenvalues - Vector of eigenvalue approximations
  • :iterations - Number of iterations used in computation
Compute eigenvalues for a general (possibly non-Hermitian) matrix.

Parameters:
- A: Square matrix

Returns:
Map containing:
- :eigenvalues - Vector of eigenvalue approximations
- :iterations - Number of iterations used in computation
sourceraw docstring

eigen-hermitianclj

(eigen-hermitian A)

Compute the eigendecomposition of a Hermitian matrix.

Parameters:

  • A: Square Hermitian matrix

Returns: Map containing:

  • :eigenvalues - Vector of eigenvalues in ascending order
  • :eigenvectors - Vector of corresponding eigenvector columns [v0 v1 ...] (Both eigenvalues and eigenvectors returned in original format)
Compute the eigendecomposition of a Hermitian matrix.

Parameters:
- A: Square Hermitian matrix

Returns:
Map containing:
- :eigenvalues - Vector of eigenvalues in ascending order
- :eigenvectors - Vector of corresponding eigenvector columns [v0 v1 ...]
(Both eigenvalues and eigenvectors returned in original format)
sourceraw docstring

get-backendclj

(get-backend)

Return the current backend key.

Returns: Keyword identifying the currently active backend

Return the current backend key.

Returns:
Keyword identifying the currently active backend
sourceraw docstring

get-backend-instanceclj

(get-backend-instance)

Return the current backend instance implementing the real/complex protocols.

Returns: Backend instance that implements the mathematical operation protocols

Return the current backend instance implementing the real/complex protocols.

Returns:
Backend instance that implements the mathematical operation protocols
sourceraw docstring

hadamard-productclj

(hadamard-product A B)

Compute the element-wise (Hadamard) product A ⊙ B.

Parameters:

  • A: First matrix
  • B: Second matrix with same dimensions as A

Returns: Matrix with element-wise multiplication of A and B

Compute the element-wise (Hadamard) product A ⊙ B.

Parameters:
- A: First matrix
- B: Second matrix with same dimensions as A

Returns:
Matrix with element-wise multiplication of A and B
sourceraw docstring

hermitian?clj

(hermitian? A)
(hermitian? A eps)

Test if a matrix is Hermitian (A ≈ Aᴴ).

Parameters:

  • A: Square matrix
  • eps: Optional tolerance for approximate equality (uses current tolerance if not provided)

Returns: Boolean indicating whether A is Hermitian or real symmetric

Test if a matrix is Hermitian (A ≈ Aᴴ).

Parameters:
- A: Square matrix
- eps: Optional tolerance for approximate equality (uses current tolerance if not provided)

Returns:
Boolean indicating whether A is Hermitian or real symmetric
sourceraw docstring

inner-productclj

(inner-product x y)

Compute the vector inner product ⟨x|y⟩.

Parameters:

  • x: First vector
  • y: Second vector with same length as x

Returns: Scalar representing the inner product

Note: For complex vectors, computes ⟨x|y⟩ = Σ xᵢ* yᵢ (conjugate x)

Compute the vector inner product ⟨x|y⟩.

Parameters:
- x: First vector
- y: Second vector with same length as x

Returns:
Scalar representing the inner product

Note:
For complex vectors, computes ⟨x|y⟩ = Σ xᵢ* yᵢ (conjugate x)
sourceraw docstring

kronecker-productclj

(kronecker-product A B)

Compute the Kronecker (tensor) product A ⊗ B.

Parameters:

  • A: First matrix
  • B: Second matrix

Returns: Matrix representing the Kronecker product A ⊗ B

Note: Essential for quantum computing multi-qubit operations

Compute the Kronecker (tensor) product A ⊗ B.

Parameters:
- A: First matrix
- B: Second matrix
 
Returns:
Matrix representing the Kronecker product A ⊗ B

Note:
Essential for quantum computing multi-qubit operations
sourceraw docstring

lu-decompositionclj

(lu-decomposition A)

Compute the LU decomposition A = P L U.

Parameters:

  • A: Square matrix

Returns: Map containing:

  • :P - Row permutation matrix
  • :L - Lower triangular matrix
  • :U - Upper triangular matrix
Compute the LU decomposition A = P L U.

Parameters:
- A: Square matrix

Returns:
Map containing:
- :P - Row permutation matrix
- :L - Lower triangular matrix
- :U - Upper triangular matrix
sourceraw docstring

matrix-expclj

(matrix-exp A)

Compute the matrix exponential exp(A).

Parameters:

  • A: Square matrix

Returns: Matrix representing exp(A)

Note: Important for quantum time evolution operators

Compute the matrix exponential exp(A).

Parameters:
- A: Square matrix

Returns:
Matrix representing exp(A)

Note:
Important for quantum time evolution operators
sourceraw docstring

matrix-from-eigenclj

(matrix-from-eigen eigenvalues eigenvectors)

Reconstruct (approximate) Hermitian matrix from eigenvalues & eigenvectors.

Parameters:

  • eigenvalues vector [λ₀ … λₙ₋₁]
  • eigenvectors vector of eigenvector column vectors [v₀ …]

Returns: Matrix Σ λᵢ vᵢ vᵢᵀ (real case only for now) in Vec2 format. NOTE: Complex support can be added when complex eigenvectors are exposed.

Reconstruct (approximate) Hermitian matrix from eigenvalues & eigenvectors.

Parameters:
- eigenvalues   vector [λ₀ … λₙ₋₁]
- eigenvectors  vector of eigenvector column vectors [v₀ …]

Returns:
Matrix Σ λᵢ vᵢ vᵢᵀ (real case only for now) in Vec2 format.
NOTE: Complex support can be added when complex eigenvectors are exposed.
sourceraw docstring

matrix-inverseclj

(matrix-inverse A)

Compute the matrix inverse A⁻¹.

Parameters:

  • A: Square matrix

Returns: Inverse matrix A⁻¹

Throws: Exception if matrix is singular

Compute the matrix inverse A⁻¹.

Parameters:
- A: Square matrix

Returns:
Inverse matrix A⁻¹

Throws:
Exception if matrix is singular
sourceraw docstring

matrix-logclj

(matrix-log A)

Compute the principal matrix logarithm log(A).

Parameters:

  • A: Square matrix

Returns: Matrix representing the principal branch of log(A)

Compute the principal matrix logarithm log(A).

Parameters:
- A: Square matrix

Returns:
Matrix representing the principal branch of log(A)
sourceraw docstring

matrix-multiplyclj

(matrix-multiply A B)

Perform matrix multiplication A × B.

Parameters:

  • A: Left matrix
  • B: Right matrix with compatible dimensions

Returns: Matrix representing the product A × B

Perform matrix multiplication A × B.

Parameters:
- A: Left matrix
- B: Right matrix with compatible dimensions

Returns:
Matrix representing the product A × B
sourceraw docstring

matrix-sqrtclj

(matrix-sqrt A)

Compute the principal matrix square root √A.

Parameters:

  • A: Square matrix

Returns: Matrix representing the principal square root of A

Compute the principal matrix square root √A.

Parameters:
- A: Square matrix

Returns:
Matrix representing the principal square root of A
sourceraw docstring

matrix-vector-productclj

(matrix-vector-product A x)

Perform matrix–vector multiplication A × x.

Parameters:

  • A: Matrix
  • x: Vector with compatible dimensions

Returns: Vector representing the product A × x

Perform matrix–vector multiplication A × x.

Parameters:
- A: Matrix
- x: Vector with compatible dimensions

Returns:
Vector representing the product A × x
sourceraw docstring

negateclj

(negate A)

Compute the additive inverse -A.

Parameters:

  • A: Matrix

Returns: Matrix representing -A

Compute the additive inverse -A.

Parameters:
- A: Matrix

Returns:
Matrix representing -A
sourceraw docstring

norm2clj

(norm2 x)

Compute the Euclidean norm ||x||₂.

Parameters:

  • x: Vector

Returns: Non-negative real number representing the Euclidean norm

Compute the Euclidean norm ||x||₂.

Parameters:
- x: Vector

Returns:
Non-negative real number representing the Euclidean norm
sourceraw docstring

outer-productclj

(outer-product x y)

Compute the outer product x ⊗ y†.

Parameters:

  • x: First vector
  • y: Second vector

Returns: Matrix representing the outer product x ⊗ y† in Vec2 format (no conjugation of x)

Note: For quantum states, this creates projector-like matrices

Compute the outer product x ⊗ y†.

Parameters:
- x: First vector
- y: Second vector

Returns:
Matrix representing the outer product x ⊗ y† in Vec2 format (no conjugation of x)

Note:
For quantum states, this creates projector-like matrices
sourceraw docstring

positive-semidefinite?clj

(positive-semidefinite? A)

Test if a matrix is positive semidefinite.

Parameters:

  • A: Square Hermitian matrix

Returns: Boolean indicating whether all eigenvalues of A are ≥ -eps

Note: Matrix must be Hermitian for meaningful results

Test if a matrix is positive semidefinite.

Parameters:
- A: Square Hermitian matrix

Returns:
Boolean indicating whether all eigenvalues of A are ≥ -eps

Note:
Matrix must be Hermitian for meaningful results
sourceraw docstring

qr-decompositionclj

(qr-decomposition A)

Compute the QR decomposition A = Q R.

Parameters:

  • A: Matrix

Returns: Map containing:

  • :Q - Orthogonal/unitary matrix
  • :R - Upper triangular matrix
Compute the QR decomposition A = Q R.

Parameters:
- A: Matrix

Returns:
Map containing:
- :Q - Orthogonal/unitary matrix
- :R - Upper triangular matrix
sourceraw docstring

scaleclj

(scale A alpha)

Perform scalar multiplication α · A.

Parameters:

  • A: Matrix
  • alpha: Scalar value

Returns: Matrix representing α · A

Perform scalar multiplication α · A.

Parameters:
- A: Matrix
- alpha: Scalar value

Returns:
Matrix representing α · A
sourceraw docstring

set-backend!clj

(set-backend! backend-key)

Set the global backend by key.

Parameters:

  • backend-key: Keyword identifying the backend (use keys from available-backends)

Returns: The backend key that was set

Note: Prefer with-backend for scoped overrides instead of global mutation

Set the global backend by key.

Parameters:
- backend-key: Keyword identifying the backend (use keys from available-backends)

Returns:
The backend key that was set

Note:
Prefer with-backend for scoped overrides instead of global mutation
sourceraw docstring

shapeclj

(shape A)

Return the shape of a matrix.

Parameters:

  • A: Matrix

Returns: Vector [rows cols] indicating the matrix dimensions

Return the shape of a matrix.

Parameters:
- A: Matrix

Returns:
Vector [rows cols] indicating the matrix dimensions
sourceraw docstring

solve-linear-systemclj

(solve-linear-system A b)

Solve the linear system Ax = b.

Parameters:

  • A: Matrix (square)
  • b: Vector or matrix

Returns: Solution vector(s) x such that Ax = b

Solve the linear system Ax = b.

Parameters:
- A: Matrix (square)
- b: Vector or matrix

Returns:
Solution vector(s) x such that Ax = b
sourceraw docstring

spectral-normclj

(spectral-norm A)

Compute the spectral norm ||A||₂ (largest singular value).

Parameters:

  • A: Matrix

Returns: Non-negative real number representing the largest singular value

Compute the spectral norm ||A||₂ (largest singular value).

Parameters:
- A: Matrix

Returns:
Non-negative real number representing the largest singular value
sourceraw docstring

subtractclj

(subtract A B)

Perform matrix subtraction A - B.

Parameters:

  • A: First matrix
  • B: Second matrix with same dimensions as A

Returns: Matrix representing A - B

Perform matrix subtraction A - B.

Parameters:
- A: First matrix
- B: Second matrix with same dimensions as A

Returns:
Matrix representing A - B
sourceraw docstring

svdclj

(svd A)

Compute the Singular Value Decomposition of a matrix.

Parameters:

  • A: Matrix

Returns: Map containing:

  • :U - Left singular vectors matrix
  • :singular-values - Vector of singular values [σ0≥σ1≥...] in descending order
  • :Vt - Right singular vectors transpose matrix

Note: Renames backend keys (:S :V†) for consistency

Compute the Singular Value Decomposition of a matrix.

Parameters:
- A: Matrix

Returns:
Map containing:
- :U - Left singular vectors matrix
- :singular-values - Vector of singular values [σ0≥σ1≥...] in descending order
- :Vt - Right singular vectors transpose matrix

Note:
Renames backend keys (:S :V†) for consistency
sourceraw docstring

traceclj

(trace A)

Compute the trace Tr(A) = Σ aᵢᵢ.

Parameters:

  • A: Square matrix

Returns: Scalar representing the sum of diagonal elements

Compute the trace Tr(A) = Σ aᵢᵢ.

Parameters:
- A: Square matrix

Returns:
Scalar representing the sum of diagonal elements
sourceraw docstring

transposeclj

(transpose A)

Compute the transpose Aᵀ.

Parameters:

  • A: Matrix

Returns: Matrix representing the transpose of A

Compute the transpose Aᵀ.

Parameters:
- A: Matrix

Returns:
Matrix representing the transpose of A
sourceraw docstring

unitary?clj

(unitary? U)
(unitary? U eps)

Test if a matrix is unitary (Uᴴ U ≈ I).

Parameters:

  • U: Square matrix
  • eps: Optional tolerance for approximate equality (uses current tolerance if not provided)

Returns: Boolean indicating whether U is unitary (or orthogonal for real matrices)

Test if a matrix is unitary (Uᴴ U ≈ I).

Parameters:
- U: Square matrix
- eps: Optional tolerance for approximate equality (uses current tolerance if not provided)

Returns:
Boolean indicating whether U is unitary (or orthogonal for real matrices)
sourceraw docstring

with-backendcljmacro

(with-backend backend & body)

Evaluate body with the backend temporarily bound to the specified backend.

Parameters:

  • backend: Either a backend keyword or a backend instance
  • body: Forms to evaluate with the temporary backend binding

Returns: Result of evaluating body with the temporary backend

Evaluate body with the backend temporarily bound to the specified backend.

Parameters:
- backend: Either a backend keyword or a backend instance
- body: Forms to evaluate with the temporary backend binding

Returns:
Result of evaluating body with the temporary backend
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close