Skip to content

Latest commit

 

History

History
65 lines (45 loc) · 2.14 KB

File metadata and controls

65 lines (45 loc) · 2.14 KB

FactoredMatrices

CI codecov Aqua QA

Store and multiply matrices in factored form M = U*V without forming the full dense matrix. When U is m×k and V is k×n, a product A*B costs O(mk²n) instead of O(m²kn) — a significant saving when k is small relative to m and n.

Installation

using Pkg
Pkg.add("FactoredMatrices")

Usage

Basic construction and multiplication

using FactoredMatrices

U = randn(100, 5)   # 100×5
V = randn(5, 80)    # 5×80
A = FactoredMatrix(U, V)   # represents the 100×80 matrix U*V

x = randn(80)
y = A * x           # 100-element result, no 100×80 matrix formed

B = randn(80, 3)
C = A * B           # 100×3 result

FactoredMatrix supports adjoints, transposes, mul!, dot, and conversion to a dense Matrix.

Allocation-free repeated multiplication with Workspace

For tight loops where allocations matter, pre-allocate a Workspace and pass it via the cache keyword:

using LinearAlgebra

ws = FactoredMatrices.Workspace(A, size(B, 2))   # scratch buffers sized for A*B products

C = similar(A * B)              # pre-allocate output
for _ in 1:1000
    mul!(C, A, B; cache=ws)     # no allocation
end

The same Workspace can be reused across calls (including adjoint/transpose products) as long as the operand sizes do not change. Create one Workspace per thread when multiplying concurrently against the same A.

Related packages

One can achieve some of this functionality using other, more general packages. In particular, see: