linear-algebra/backends/default
API baseline for Luna-Flow/linear-algebra/backends/default in the current 0.4.7 repository state.
Purpose
backends/default provides owned wrapper types around the existing dense mutable and immut implementations. Because these wrappers are owned by the default backend package, the package can implement the public @algebra traits for them without violating MoonBit's foreign trait / foreign type rule.
DenseVector[T]
///|
test "DenseVector wraps a mutable vector backend" {
let vector : @default.DenseVector[Int] = @default.DenseVector::from_array([
1, 2, 3,
])
inspect(vector.length(), content="3")
inspect(vector[1], content="2")
}Owned wrapper for the default mutable dense vector backend.
Constructors And Accessors
DenseVector::from_backend(inner : @mutable.Vector[T]) -> DenseVector[T]wraps an existing mutable vector.DenseVector::from_array(data : Array[T]) -> DenseVector[T]builds a mutable dense vector from an array.DenseVector::make(length : Int, value : T) -> DenseVector[T]builds a vector filled withvalue.DenseVector::inner(self) -> @mutable.Vector[T]returns the wrapped mutable vector.DenseVector::length(self) -> Intreturns vector length.DenseVector::op_get(self, index : Int) -> Tsupports read indexing.
Backend Methods
DenseVector::scale(self, scalar : T) -> DenseVector[T]scales the vector element-wise and returns a new backend value.DenseVector::dot(self, other : DenseVector[T]) -> Tcomputes the scalar dot product.DenseVector::axpy(self, alpha : T, other : DenseVector[T]) -> DenseVector[T]computes the BLAS-style linear combinationalpha * self + other.
Trait Implementations
Add,Neg,Sub, andMulwith the matching element-level operation constraints.@algebra.VectorShapefor allT@algebra.AdditiveVectorwhenT : Add + Neg@algebra.VecMulVectorwhenT : Add + Neg + Mul
DenseMatrix[T]
///|
test "DenseMatrix wraps a mutable matrix backend" {
let matrix : @default.DenseMatrix[Int] = @default.DenseMatrix::from_2d_array([
[1, 2],
[3, 4],
])
inspect(matrix.row(), content="2")
inspect(matrix.col(), content="2")
}Owned wrapper for the default mutable dense matrix backend.
Constructors And Accessors
DenseMatrix::from_backend(inner : @mutable.Matrix[T]) -> DenseMatrix[T]wraps an existing mutable matrix.DenseMatrix::from_2d_array(data : Array[Array[T]]) -> DenseMatrix[T]builds a matrix from row-major nested arrays.DenseMatrix::new(row : Int, col : Int, value : T) -> DenseMatrix[T]builds a matrix filled withvalue.DenseMatrix::inner(self) -> @mutable.Matrix[T]returns the wrapped mutable matrix.DenseMatrix::row(self) -> Intreturns row count.DenseMatrix::col(self) -> Intreturns column count.
Backend Methods
DenseMatrix::matvec(self, vector : DenseVector[T]) -> DenseVector[T]multiplies the matrix by a default-backend dense vector and returns a new dense vector wrapper.
Trait Implementations
Add,Neg,Sub, andMulwith the matching element-level operation constraints.@algebra.MatrixShapeand@algebra.TransposeMatrixfor allT@algebra.AdditiveMatrixwhenT : Add + Neg@algebra.MatMulMatrixwhenT : Add + Neg + AddMonoid + Mul
ImmutableDenseVector[T]
///|
test "ImmutableDenseVector wraps an immutable vector backend" {
let vector : @default.ImmutableDenseVector[Int] = @default.ImmutableDenseVector::from_array([
1, 2, 3,
],
)
inspect(vector.length(), content="3")
inspect(vector[2], content="3")
}Owned wrapper for the default immutable dense vector backend.
Constructors And Accessors
ImmutableDenseVector::from_backend(inner : @immut.Vector[T])ImmutableDenseVector::from_array(data : Array[T])ImmutableDenseVector::make(length : Int, value : T)ImmutableDenseVector::inner(self) -> @immut.Vector[T]ImmutableDenseVector::length(self) -> IntImmutableDenseVector::op_get(self, index : Int) -> T
Backend Methods
ImmutableDenseVector::scale(self, scalar : T) -> ImmutableDenseVector[T]ImmutableDenseVector::dot(self, other : ImmutableDenseVector[T]) -> TImmutableDenseVector::axpy(self, alpha : T, other : ImmutableDenseVector[T]) -> ImmutableDenseVector[T]
Trait Implementations
Add,Neg,Sub, andMulwith the matching element-level operation constraints.@algebra.VectorShapefor allT@algebra.AdditiveVectorwhenT : Add + Neg@algebra.VecMulVectorwhenT : Add + Neg + Mul
ImmutableDenseMatrix[T]
///|
test "ImmutableDenseMatrix wraps an immutable matrix backend" {
let matrix : @default.ImmutableDenseMatrix[Int] = @default.ImmutableDenseMatrix::from_2d_array([
[1, 2],
[3, 4],
],
)
inspect(matrix.row(), content="2")
inspect(matrix.col(), content="2")
}Owned wrapper for the default immutable dense matrix backend.
Constructors And Accessors
ImmutableDenseMatrix::from_backend(inner : @immut.Matrix[T])ImmutableDenseMatrix::from_2d_array(data : Array[Array[T]])ImmutableDenseMatrix::new(row : Int, col : Int, value : T)ImmutableDenseMatrix::inner(self) -> @immut.Matrix[T]ImmutableDenseMatrix::row(self) -> IntImmutableDenseMatrix::col(self) -> Int
Backend Methods
ImmutableDenseMatrix::matvec(self, vector : ImmutableDenseVector[T]) -> ImmutableDenseVector[T]multiplies the matrix by an immutable dense vector and returns a new immutable dense vector wrapper.
Trait Implementations
Add,Neg,Sub, andMulwith the matching element-level operation constraints.@algebra.MatrixShapeand@algebra.TransposeMatrixfor allT@algebra.AdditiveMatrixwhenT : Add + Neg@algebra.MatMulMatrixwhenT : Add + Neg + Zero + Mul
Generic Helper Functions
shape_of[M : @algebra.MatrixShape](matrix : M) -> (Int, Int)returns an object's shape.matmul[M : @algebra.MatMulMatrix](left : M, right : M) -> Mdispatches matrix multiplication through the explicit multiplication capability.transpose[M : @algebra.TransposeMatrix](matrix : M) -> Mdispatches closed transpose through the algebra trait.
Boundary
This package implements outer algebra traits for the default backend wrapper types. It does not define new structure traits. Scalar-valued products and matrix-vector interactions described above remain backend methods rather than new @algebra traits, while norms, solves, and decompositions remain backend methods or future dedicated algorithm-layer APIs unless they can be represented as structure traits or closed operations.