pub struct SparseVec {
pub indices: Vec<usize>,
pub values: Vec<f64>,
pub len: usize,
}Expand description
疎ベクトル(インデックス・値のペアリスト、インデックスで昇順ソート済み)
ゼロでない要素のみをインデックスと値のペアで保持する。
indices は常に昇順にソートされており、二分探索による O(log n) アクセスが可能。
ゼロ近傍の値(絶対値が ZERO_TOL 以下)は自動的に除去される。
Fields§
§indices: Vec<usize>非ゼロ要素のインデックス(昇順ソート済み)
values: Vec<f64>非ゼロ要素の値(indices と同じ順序)
len: usize論理的な長さ(ゼロ要素を含む全体の次元数)
Implementations§
Source§impl SparseVec
impl SparseVec
Sourcepub fn from_dense(dense: &[f64]) -> Self
pub fn from_dense(dense: &[f64]) -> Self
密ベクトルから疎ベクトルを生成する
絶対値が ZERO_TOL(1e-12)を超える要素のみを保持し、残りは捨てる。 インデックスは元の配列の位置順(昇順)で格納される。
§引数
dense: 変換元の密ベクトル(スライス)
Sourcepub fn to_dense(&self) -> Vec<f64>
pub fn to_dense(&self) -> Vec<f64>
疎ベクトルを密ベクトルに変換する
非ゼロ要素を対応するインデックスに配置し、残りはゼロで埋める。
返却ベクトルの長さは self.len と等しい。
Sourcepub fn to_dense_into(&self, buf: &mut [f64])
pub fn to_dense_into(&self, buf: &mut [f64])
事前確保済みバッファに密ベクトルを書き込む
buf を一旦ゼロクリアしてから非ゼロ要素を書き込む。
ヒープ割り当てを行わないため、反復ループ内での再利用に適する。
§引数
buf: 書き込み先バッファ(長さ >=self.lenであること)
Sourcepub fn get(&self, idx: usize) -> f64
pub fn get(&self, idx: usize) -> f64
指定インデックスの値を取得する
インデックスが非ゼロ要素として存在しない場合は 0.0 を返す。
内部では二分探索を使用するため O(log n) で動作する。
§引数
idx: 取得するインデックス
Sourcepub fn set(&mut self, idx: usize, val: f64)
pub fn set(&mut self, idx: usize, val: f64)
指定インデックスに値をセットする
val の絶対値が ZERO_TOL 以下の場合、そのインデックスを非ゼロリストから削除する
(ゼロとみなす)。既存のエントリがない場合は挿入し、ある場合は上書きする。
ソート順を維持するため、挿入位置は二分探索で決定する。
§引数
idx: セットするインデックスval: セットする値(ZERO_TOL 以下なら削除)
Sourcepub fn axpy(&mut self, alpha: f64, other: &SparseVec)
pub fn axpy(&mut self, alpha: f64, other: &SparseVec)
AXPY 演算: self += alpha * other
両ベクトルのインデックスリストをtwo-pointer mergeで走査し、 O(nnz_a + nnz_b) で演算する。ZERO_TOL 以下の結果はドロップする。
§引数
alpha: スカラー倍率other: 加算する疎ベクトル
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SparseVec
impl RefUnwindSafe for SparseVec
impl Send for SparseVec
impl Sync for SparseVec
impl Unpin for SparseVec
impl UnsafeUnpin for SparseVec
impl UnwindSafe for SparseVec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more