Skip to main content

SparseVec

Struct SparseVec 

Source
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

Source

pub fn new(len: usize) -> Self

指定した論理長の空疎ベクトルを生成する

非ゼロ要素は含まない(すべてゼロ)状態で初期化される。

§引数
  • len: ベクトルの論理的な長さ(次元数)
Source

pub fn from_dense(dense: &[f64]) -> Self

密ベクトルから疎ベクトルを生成する

絶対値が ZERO_TOL(1e-12)を超える要素のみを保持し、残りは捨てる。 インデックスは元の配列の位置順(昇順)で格納される。

§引数
  • dense: 変換元の密ベクトル(スライス)
Source

pub fn to_dense(&self) -> Vec<f64>

疎ベクトルを密ベクトルに変換する

非ゼロ要素を対応するインデックスに配置し、残りはゼロで埋める。 返却ベクトルの長さは self.len と等しい。

Source

pub fn to_dense_into(&self, buf: &mut [f64])

事前確保済みバッファに密ベクトルを書き込む

buf を一旦ゼロクリアしてから非ゼロ要素を書き込む。 ヒープ割り当てを行わないため、反復ループ内での再利用に適する。

§引数
  • buf: 書き込み先バッファ(長さ >= self.len であること)
Source

pub fn get(&self, idx: usize) -> f64

指定インデックスの値を取得する

インデックスが非ゼロ要素として存在しない場合は 0.0 を返す。 内部では二分探索を使用するため O(log n) で動作する。

§引数
  • idx: 取得するインデックス
Source

pub fn set(&mut self, idx: usize, val: f64)

指定インデックスに値をセットする

val の絶対値が ZERO_TOL 以下の場合、そのインデックスを非ゼロリストから削除する (ゼロとみなす)。既存のエントリがない場合は挿入し、ある場合は上書きする。 ソート順を維持するため、挿入位置は二分探索で決定する。

§引数
  • idx: セットするインデックス
  • val: セットする値(ZERO_TOL 以下なら削除)
Source

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: 加算する疎ベクトル
Source

pub fn dot(&self, other: &SparseVec) -> f64

別の疎ベクトルとの内積を計算する

両ベクトルのインデックスリストをマージソート的に走査し、 一致するインデックスの積を加算する。計算量は O(nnz_a + nnz_b)。

§引数
  • other: 内積を取る相手の疎ベクトル
Source

pub fn dot_dense(&self, dense: &[f64]) -> f64

密ベクトルとの内積を計算する

疎ベクトルの非ゼロ要素のインデックスのみを参照するため、 密ベクトルとの積でも O(nnz) で動作する。

§引数
  • dense: 内積を取る相手の密ベクトル(スライス)

Trait Implementations§

Source§

impl Clone for SparseVec

Source§

fn clone(&self) -> SparseVec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SparseVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,