container/adapters API

Luna-Flow/linear-algebra/container/adapters 为本仓库的每种向量和矩阵类型提供现成的 container 操作字典:包括 @immut 和 @mutable 类型、可变的行视图、列视图和转置视图,以及四个 backends/default 包装类型。每个工厂都是返回全新字典的泛型函数,本身不带状态。

源码:src/container/adapters。适配器为何单独成包见 adapters 设计。

导入

///|
import {
  "Luna-Flow/linear-algebra/container",
  "Luna-Flow/linear-algebra/container/adapters" @container_adapters,
}

能力矩阵

类型读取构建持久化编辑可变编辑
@immut.Vector[T]是是是否
@immut.Matrix[T]是是是否
@mutable.Vector[T]是是否是
@mutable.Matrix[T]是是否是
@mutable.RowView[T], @mutable.ColView[T]是否否是
@mutable.Transpose[T]是否否是
@default.DenseVector[T], @default.DenseMatrix[T]是是否是
@default.ImmutableDenseVector[T], @default.ImmutableDenseMatrix[T]是是是否

每个字典都遵循 container API 的共享约定:读取和编辑会先检查索引,并在不触及存储的情况下返回 IndexOutOfBounds;构建函数在形状为负时返回 NegativeDimension,且不会调用初始化函数。所有工厂都不对 T 施加约束。

@immut.Vector

immutable_vector_read_ops

immutable_vector_read_ops 返回 @immut.Vector 的读取字典。

pub fn[T] immutable_vector_read_ops() -> @container.VectorReadOps[@immut.Vector[T], T]

get 在边界检查后读取 v[i]。

immutable_vector_build_ops

immutable_vector_build_ops 返回 @immut.Vector 的构建字典。

pub fn[T] immutable_vector_build_ops() -> @container.VectorBuildOps[@immut.Vector[T], T]

tabulate(n, f) 即 @immut.Vector::makei(n, f)。

immutable_vector_persistent_edit_ops

immutable_vector_persistent_edit_ops 返回 @immut.Vector 的持久化编辑字典。

pub fn[T] immutable_vector_persistent_edit_ops() -> @container.VectorPersistentEditOps[@immut.Vector[T], T]

set 返回 v.set(i, x);v 保持不变。

@immut.Matrix

immutable_matrix_read_ops

immutable_matrix_read_ops 返回 @immut.Matrix 的读取字典。

pub fn[T] immutable_matrix_read_ops() -> @container.MatrixReadOps[@immut.Matrix[T], T]

shape 为 (m.row(), m.col());get 在边界检查后读取 m[r][c]。

immutable_matrix_build_ops

immutable_matrix_build_ops 返回 @immut.Matrix 的构建字典。

pub fn[T] immutable_matrix_build_ops() -> @container.MatrixBuildOps[@immut.Matrix[T], T]

tabulate(r, c, f) 即 @immut.Matrix::make(r, c, f)。

immutable_matrix_persistent_edit_ops

immutable_matrix_persistent_edit_ops 返回 @immut.Matrix 的持久化编辑字典。

pub fn[T] immutable_matrix_persistent_edit_ops() -> @container.MatrixPersistentEditOps[@immut.Matrix[T], T]

set 返回 m.set(r, c, x);m 保持不变。

@mutable.Vector

mutable_vector_read_ops

mutable_vector_read_ops 返回 @mutable.Vector 的读取字典。

pub fn[T] mutable_vector_read_ops() -> @container.VectorReadOps[@mutable.Vector[T], T]

get 在边界检查后读取 v[i]。

mutable_vector_build_ops

mutable_vector_build_ops 返回 @mutable.Vector 的构建字典。

pub fn[T] mutable_vector_build_ops() -> @container.VectorBuildOps[@mutable.Vector[T], T]

tabulate(n, f) 即 @mutable.Vector::makei(n, f)。

mutable_vector_mutable_edit_ops

mutable_vector_mutable_edit_ops 返回 @mutable.Vector 的可变编辑字典。

pub fn[T] mutable_vector_mutable_edit_ops() -> @container.VectorMutableEditOps[@mutable.Vector[T], T]

set 在边界检查后执行 v[i] = x。

@mutable.Matrix

mutable_matrix_read_ops

mutable_matrix_read_ops 返回 @mutable.Matrix 的读取字典。

pub fn[T] mutable_matrix_read_ops() -> @container.MatrixReadOps[@mutable.Matrix[T], T]

get 即边界检查后的 m.get(r, c)。

mutable_matrix_build_ops

mutable_matrix_build_ops 返回 @mutable.Matrix 的构建字典。

pub fn[T] mutable_matrix_build_ops() -> @container.MatrixBuildOps[@mutable.Matrix[T], T]

tabulate 填充一个全新的行主序数组,并按行主序调用初始化函数。

mutable_matrix_mutable_edit_ops

mutable_matrix_mutable_edit_ops 返回 @mutable.Matrix 的可变编辑字典。

pub fn[T] mutable_matrix_mutable_edit_ops() -> @container.MatrixMutableEditOps[@mutable.Matrix[T], T]

set 即边界检查后的 m.set(r, c, x)。

@mutable.RowView

mutable_row_view_read_ops

mutable_row_view_read_ops 返回 @mutable.RowView 的读取字典。

pub fn[T] mutable_row_view_read_ops() -> @container.VectorReadOps[@mutable.RowView[T], T]

length 为底层矩阵的列数;get(i) 读取元素 (row, i)。

mutable_row_view_mutable_edit_ops

mutable_row_view_mutable_edit_ops 返回 @mutable.RowView 的可变编辑字典。

pub fn[T] mutable_row_view_mutable_edit_ops() -> @container.VectorMutableEditOps[@mutable.RowView[T], T]

set(i, x) 写入底层矩阵的元素 (row, i)。

@mutable.ColView

mutable_col_view_read_ops

mutable_col_view_read_ops 返回 @mutable.ColView 的读取字典。

pub fn[T] mutable_col_view_read_ops() -> @container.VectorReadOps[@mutable.ColView[T], T]

length 为底层矩阵的行数;get(i) 读取元素 (i, col)。

mutable_col_view_mutable_edit_ops

mutable_col_view_mutable_edit_ops 返回 @mutable.ColView 的可变编辑字典。

pub fn[T] mutable_col_view_mutable_edit_ops() -> @container.VectorMutableEditOps[@mutable.ColView[T], T]

set(i, x) 写入底层矩阵的元素 (i, col)。

@mutable.Transpose

mutable_transpose_read_ops

mutable_transpose_read_ops 返回 @mutable.Transpose 的读取字典。

pub fn[T] mutable_transpose_read_ops() -> @container.MatrixReadOps[@mutable.Transpose[T], T]

shape 为转置后的形状;get(r, c) 读取底层矩阵的元素 (c, r)。

mutable_transpose_mutable_edit_ops

mutable_transpose_mutable_edit_ops 返回 @mutable.Transpose 的可变编辑字典。

pub fn[T] mutable_transpose_mutable_edit_ops() -> @container.MatrixMutableEditOps[@mutable.Transpose[T], T]

set(r, c, x) 写入底层矩阵的元素 (c, r)。

@default.DenseVector

dense_vector_read_ops

dense_vector_read_ops 返回 @default.DenseVector 的读取字典。

pub fn[T] dense_vector_read_ops() -> @container.VectorReadOps[@default.DenseVector[T], T]

get 读取被包装的 @mutable.Vector。

dense_vector_build_ops

dense_vector_build_ops 返回 @default.DenseVector 的构建字典。

pub fn[T] dense_vector_build_ops() -> @container.VectorBuildOps[@default.DenseVector[T], T]

tabulate 构建一个新的 @mutable.Vector 并将其包装。

dense_vector_mutable_edit_ops

dense_vector_mutable_edit_ops 返回 @default.DenseVector 的可变编辑字典。

pub fn[T] dense_vector_mutable_edit_ops() -> @container.VectorMutableEditOps[@default.DenseVector[T], T]

set 写入被包装的向量,该向量由包装对象的所有副本共享。

@default.DenseMatrix

dense_matrix_read_ops

dense_matrix_read_ops 返回 @default.DenseMatrix 的读取字典。

pub fn[T] dense_matrix_read_ops() -> @container.MatrixReadOps[@default.DenseMatrix[T], T]

get 读取被包装的 @mutable.Matrix。

dense_matrix_build_ops

dense_matrix_build_ops 返回 @default.DenseMatrix 的构建字典。

pub fn[T] dense_matrix_build_ops() -> @container.MatrixBuildOps[@default.DenseMatrix[T], T]

tabulate 构建一个新的行主序 @mutable.Matrix 并将其包装。

dense_matrix_mutable_edit_ops

dense_matrix_mutable_edit_ops 返回 @default.DenseMatrix 的可变编辑字典。

pub fn[T] dense_matrix_mutable_edit_ops() -> @container.MatrixMutableEditOps[@default.DenseMatrix[T], T]

set 写入被包装的矩阵。

@default.ImmutableDenseVector

immutable_dense_vector_read_ops

immutable_dense_vector_read_ops 返回 @default.ImmutableDenseVector 的读取字典。

pub fn[T] immutable_dense_vector_read_ops() -> @container.VectorReadOps[@default.ImmutableDenseVector[T], T]

get 读取被包装的 @immut.Vector。

immutable_dense_vector_build_ops

immutable_dense_vector_build_ops 返回 @default.ImmutableDenseVector 的构建字典。

pub fn[T] immutable_dense_vector_build_ops() -> @container.VectorBuildOps[@default.ImmutableDenseVector[T], T]

tabulate 构建一个新的 @immut.Vector 并将其包装。

immutable_dense_vector_persistent_edit_ops

immutable_dense_vector_persistent_edit_ops 返回 @default.ImmutableDenseVector 的持久化编辑字典。

pub fn[T] immutable_dense_vector_persistent_edit_ops() -> @container.VectorPersistentEditOps[@default.ImmutableDenseVector[T], T]

set 返回一个包裹 inner.set(i, x) 的新包装对象。

@default.ImmutableDenseMatrix

immutable_dense_matrix_read_ops

immutable_dense_matrix_read_ops 返回 @default.ImmutableDenseMatrix 的读取字典。

pub fn[T] immutable_dense_matrix_read_ops() -> @container.MatrixReadOps[@default.ImmutableDenseMatrix[T], T]

get 读取被包装的 @immut.Matrix。

immutable_dense_matrix_build_ops

immutable_dense_matrix_build_ops 返回 @default.ImmutableDenseMatrix 的构建字典。

pub fn[T] immutable_dense_matrix_build_ops() -> @container.MatrixBuildOps[@default.ImmutableDenseMatrix[T], T]

tabulate 构建一个新的 @immut.Matrix 并将其包装。

immutable_dense_matrix_persistent_edit_ops

immutable_dense_matrix_persistent_edit_ops 返回 @default.ImmutableDenseMatrix 的持久化编辑字典。

pub fn[T] immutable_dense_matrix_persistent_edit_ops() -> @container.MatrixPersistentEditOps[@default.ImmutableDenseMatrix[T], T]

set 返回一个包裹 inner.set(r, c, x) 的新包装对象。

示例

一个源、三个目标,以及一次通过视图进行的受检编辑:

///|
test "adapters connect every representation" {
  let m = @mutable.Matrix::from_2d_array([[1, 2], [3, 4]])
  let read = @container_adapters.mutable_matrix_read_ops()
  let dense : @default.DenseMatrix[Int] = @container.matrix_convert(
    m,
    read,
    @container_adapters.dense_matrix_build_ops(),
  ).unwrap()
  let frozen : @default.ImmutableDenseMatrix[Int] = @container.matrix_convert(
    m,
    read,
    @container_adapters.immutable_dense_matrix_build_ops(),
  ).unwrap()
  debug_inspect(@algebra.MatrixShape::shape(dense), content="(2, 2)")
  inspect(frozen.inner(), content="|1, 2|\n|3, 4|")
  let t = m.to_transpose()
  let t_read = @container_adapters.mutable_transpose_read_ops()
  inspect((t_read.get)(t, 0, 1).unwrap(), content="3")
  let col = m.col_view(1)
  (@container_adapters.mutable_col_view_mutable_edit_ops().set)(col, 0, 20).unwrap()
  inspect(m, content="|1, 20|\n|3, 4|")
}