Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "single_algebra"
version = "0.7.0"
version = "0.8.3"
edition = "2021"
license-file = "LICENSE.md"
description = "A linear algebra convenience library for the single-rust library. Can be used externally as well."
Expand Down Expand Up @@ -37,7 +37,7 @@ simba = { version = "0.9.0", optional = true }
smartcore = { version = "0.4", features = ["ndarray-bindings"], optional = true }
single-svdlib = { version = "1.0.4" }
rand = "0.9.0"
single-utilities = "0.6.0"
single-utilities = "0.7.0"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
66 changes: 50 additions & 16 deletions src/sparse/csc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use nalgebra_sparse::CscMatrix;
use num_traits::{Float, NumCast, PrimInt, Unsigned, Zero};
use single_utilities::types::Direction;
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
use std::iter::Sum;
use std::ops::Add;
use std::ops::AddAssign;

use crate::sparse::MatrixNTop;
use crate::utils::Normalize;

use super::{
Expand Down Expand Up @@ -359,8 +358,8 @@ where

fn var_col<I, T>(&self) -> anyhow::Result<Vec<T>>
where
I: PrimInt + Unsigned + Zero + AddAssign + Into<T>,
T: Float + NumCast + AddAssign + std::iter::Sum,
I: PrimInt + Unsigned + Zero + AddAssign + Into<T> + Send + Sync,
T: Float + NumCast + AddAssign + std::iter::Sum + Send + Sync,
Self::Item: NumCast,
{
let sum: Vec<T> = self.sum_col()?;
Expand All @@ -385,8 +384,8 @@ where

fn var_row<I, T>(&self) -> anyhow::Result<Vec<T>>
where
I: PrimInt + Unsigned + Zero + AddAssign + Into<T>,
T: Float + NumCast + AddAssign + std::iter::Sum,
I: PrimInt + Unsigned + Zero + AddAssign + Into<T> + Send + Sync,
T: Float + NumCast + AddAssign + std::iter::Sum + Send + Sync,
Self::Item: NumCast,
{
let sum: Vec<T> = self.sum_row()?;
Expand All @@ -410,8 +409,8 @@ where

fn var_col_chunk<I, T>(&self, reference: &mut [T]) -> anyhow::Result<()>
where
I: PrimInt + Unsigned + Zero + AddAssign + Into<T>,
T: Float + NumCast + AddAssign + std::iter::Sum,
I: PrimInt + Unsigned + Zero + AddAssign + Into<T> + Send + Sync,
T: Float + NumCast + AddAssign + std::iter::Sum + Send + Sync,
Self::Item: NumCast,
{
// Validate input slice length matches number of columns
Expand Down Expand Up @@ -449,8 +448,8 @@ where

fn var_row_chunk<I, T>(&self, reference: &mut [T]) -> anyhow::Result<()>
where
I: PrimInt + Unsigned + Zero + AddAssign + Into<T>,
T: Float + NumCast + AddAssign + std::iter::Sum,
I: PrimInt + Unsigned + Zero + AddAssign + Into<T> + Send + Sync,
T: Float + NumCast + AddAssign + std::iter::Sum + Send + Sync,
Self::Item: NumCast,
{
// Validate input slice length matches number of rows
Expand Down Expand Up @@ -488,8 +487,8 @@ where

fn var_col_masked<I, T>(&self, mask: &[bool]) -> anyhow::Result<Vec<T>>
where
I: PrimInt + Unsigned + Zero + AddAssign + Into<T>,
T: Float + NumCast + AddAssign + Sum,
I: PrimInt + Unsigned + Zero + AddAssign + Into<T> + Send + Sync,
T: Float + NumCast + AddAssign + Sum + Send + Sync,
{
// Validate mask length
if mask.len() < self.nrows() {
Expand Down Expand Up @@ -537,8 +536,8 @@ where

fn var_row_masked<I, T>(&self, mask: &[bool]) -> anyhow::Result<Vec<T>>
where
I: PrimInt + Unsigned + Zero + AddAssign + Into<T>,
T: Float + NumCast + AddAssign + Sum,
I: PrimInt + Unsigned + Zero + AddAssign + Into<T> + Send + Sync,
T: Float + NumCast + AddAssign + Sum + Send + Sync
{
// Validate mask length
if mask.len() < self.ncols() {
Expand Down Expand Up @@ -590,7 +589,7 @@ impl<M: NumCast + Copy + PartialOrd + NumericOps> MatrixMinMax for CscMatrix<M>

fn min_max_col<Item>(&self) -> anyhow::Result<(Vec<Item>, Vec<Item>)>
where
Item: NumCast + Copy + PartialOrd + NumericOps,
Item: NumCast + Copy + PartialOrd + NumericOps + Send + Sync,
{
let mut min: Vec<Item> = vec![Item::max_value(); self.ncols()];
let mut max: Vec<Item> = vec![Item::min_value(); self.ncols()];
Expand All @@ -601,7 +600,7 @@ impl<M: NumCast + Copy + PartialOrd + NumericOps> MatrixMinMax for CscMatrix<M>

fn min_max_row<Item>(&self) -> anyhow::Result<(Vec<Item>, Vec<Item>)>
where
Item: NumCast + Copy + PartialOrd + NumericOps,
Item: NumCast + Copy + PartialOrd + NumericOps + Send + Sync,
{
let mut min: Vec<Item> = vec![Item::max_value(); self.nrows()];
let mut max: Vec<Item> = vec![Item::min_value(); self.nrows()];
Expand Down Expand Up @@ -1027,6 +1026,41 @@ impl<M: NumericOps + NumCast> BatchMatrixMean for CscMatrix<M> {
}
}

impl<M: NumericOps + NumCast> MatrixNTop for CscMatrix<M> {
type Item = M;

fn sum_row_n_top<T>(&self, n: usize) -> anyhow::Result<Vec<T>>
where
T: Float + NumCast + AddAssign + Sum {
let mut result = vec![T::zero(); self.nrows()];

let mut row_values: Vec<Vec<T>> = vec![Vec::new(); self.nrows()];

for col_idx in 0..self.ncols() {
let col_start = self.col_offsets()[col_idx];
let col_end = self.col_offsets()[col_idx + 1];

for idx in col_start..col_end {
let row_idx = self.row_indices()[idx];
if let Some(val) = T::from(self.values()[idx]) {
row_values[row_idx].push(val);
}
}
}

for (row_idx, mut values) in row_values.into_iter().enumerate() {
if values.len() <= n {
result[row_idx] = values.into_iter().sum();
} else {
values.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
result[row_idx] = values.into_iter().take(n).sum();
}
}

Ok(result)
}
}

#[cfg(test)]
mod tests {
use Direction;
Expand Down
Loading
Loading