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|")
}