fix: cargo fmt
This commit is contained in:
+245
-259
@@ -1,26 +1,26 @@
|
||||
extern crate num;
|
||||
use std::ops::Range;
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Range;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::ser::{Serializer, SerializeStruct};
|
||||
use serde::de::{Deserializer, Visitor, SeqAccess, MapAccess};
|
||||
use serde::de::{Deserializer, MapAccess, SeqAccess, Visitor};
|
||||
use serde::ser::{SerializeStruct, Serializer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::linalg::evd::EVDDecomposableMatrix;
|
||||
use crate::linalg::lu::LUDecomposableMatrix;
|
||||
use crate::linalg::qr::QRDecomposableMatrix;
|
||||
use crate::linalg::svd::SVDDecomposableMatrix;
|
||||
use crate::linalg::Matrix;
|
||||
pub use crate::linalg::{BaseMatrix, BaseVector};
|
||||
use crate::linalg::svd::SVDDecomposableMatrix;
|
||||
use crate::linalg::evd::EVDDecomposableMatrix;
|
||||
use crate::linalg::qr::QRDecomposableMatrix;
|
||||
use crate::linalg::lu::LUDecomposableMatrix;
|
||||
use crate::math::num::FloatExt;
|
||||
|
||||
impl<T: FloatExt> BaseVector<T> for Vec<T> {
|
||||
fn get(&self, i: usize) -> T {
|
||||
self[i]
|
||||
}
|
||||
fn set(&mut self, i: usize, x: T){
|
||||
fn set(&mut self, i: usize, x: T) {
|
||||
self[i] = x
|
||||
}
|
||||
|
||||
@@ -31,32 +31,34 @@ impl<T: FloatExt> BaseVector<T> for Vec<T> {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DenseMatrix<T: FloatExt> {
|
||||
|
||||
ncols: usize,
|
||||
nrows: usize,
|
||||
values: Vec<T>
|
||||
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T: FloatExt> fmt::Display for DenseMatrix<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut rows: Vec<Vec<f64>> = Vec::new();
|
||||
for r in 0..self.nrows {
|
||||
rows.push(self.get_row_as_vec(r).iter().map(|x| (x.to_f64().unwrap() * 1e4).round() / 1e4 ).collect());
|
||||
}
|
||||
rows.push(
|
||||
self.get_row_as_vec(r)
|
||||
.iter()
|
||||
.map(|x| (x.to_f64().unwrap() * 1e4).round() / 1e4)
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
write!(f, "{:?}", rows)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FloatExt> DenseMatrix<T> {
|
||||
|
||||
impl<T: FloatExt> DenseMatrix<T> {
|
||||
fn new(nrows: usize, ncols: usize, values: Vec<T>) -> Self {
|
||||
DenseMatrix {
|
||||
ncols: ncols,
|
||||
nrows: nrows,
|
||||
values: values
|
||||
values: values,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_array(values: &[&[T]]) -> Self {
|
||||
DenseMatrix::from_vec(&values.into_iter().map(|row| Vec::from(*row)).collect())
|
||||
@@ -64,11 +66,14 @@ impl<T: FloatExt> DenseMatrix<T> {
|
||||
|
||||
pub fn from_vec(values: &Vec<Vec<T>>) -> DenseMatrix<T> {
|
||||
let nrows = values.len();
|
||||
let ncols = values.first().unwrap_or_else(|| panic!("Cannot create 2d matrix from an empty vector")).len();
|
||||
let ncols = values
|
||||
.first()
|
||||
.unwrap_or_else(|| panic!("Cannot create 2d matrix from an empty vector"))
|
||||
.len();
|
||||
let mut m = DenseMatrix {
|
||||
ncols: ncols,
|
||||
nrows: nrows,
|
||||
values: vec![T::zero(); ncols*nrows]
|
||||
values: vec![T::zero(); ncols * nrows],
|
||||
};
|
||||
for row in 0..nrows {
|
||||
for col in 0..ncols {
|
||||
@@ -76,17 +81,17 @@ impl<T: FloatExt> DenseMatrix<T> {
|
||||
}
|
||||
}
|
||||
m
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vector_from_array(values: &[T]) -> Self {
|
||||
DenseMatrix::vector_from_vec(Vec::from(values))
|
||||
}
|
||||
DenseMatrix::vector_from_vec(Vec::from(values))
|
||||
}
|
||||
|
||||
pub fn vector_from_vec(values: Vec<T>) -> Self {
|
||||
DenseMatrix {
|
||||
ncols: values.len(),
|
||||
nrows: 1,
|
||||
values: values
|
||||
values: values,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,12 +103,11 @@ impl<T: FloatExt> DenseMatrix<T> {
|
||||
for i in 0..self.values.len() {
|
||||
self.values[i] = self.values[i] / b.values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_raw_values(&self) -> &Vec<T> {
|
||||
&self.values
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T: FloatExt + fmt::Debug + Deserialize<'de>> Deserialize<'de> for DenseMatrix<T> {
|
||||
@@ -111,31 +115,37 @@ impl<'de, T: FloatExt + fmt::Debug + Deserialize<'de>> Deserialize<'de> for Dens
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(field_identifier, rename_all = "lowercase")]
|
||||
enum Field { NRows, NCols, Values }
|
||||
|
||||
struct DenseMatrixVisitor<T: FloatExt + fmt::Debug>{
|
||||
t: PhantomData<T>
|
||||
enum Field {
|
||||
NRows,
|
||||
NCols,
|
||||
Values,
|
||||
}
|
||||
|
||||
|
||||
struct DenseMatrixVisitor<T: FloatExt + fmt::Debug> {
|
||||
t: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, T: FloatExt + fmt::Debug + Deserialize<'a>> Visitor<'a> for DenseMatrixVisitor<T> {
|
||||
type Value = DenseMatrix<T>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("struct DenseMatrix")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn visit_seq<V>(self, mut seq: V) -> Result<DenseMatrix<T>, V::Error>
|
||||
where
|
||||
V: SeqAccess<'a>,
|
||||
{
|
||||
let nrows = seq.next_element()?
|
||||
let nrows = seq
|
||||
.next_element()?
|
||||
.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
|
||||
let ncols = seq.next_element()?
|
||||
let ncols = seq
|
||||
.next_element()?
|
||||
.ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
|
||||
let values = seq.next_element()?
|
||||
let values = seq
|
||||
.next_element()?
|
||||
.ok_or_else(|| serde::de::Error::invalid_length(2, &self))?;
|
||||
Ok(DenseMatrix::new(nrows, ncols, values))
|
||||
}
|
||||
@@ -176,23 +186,26 @@ impl<'de, T: FloatExt + fmt::Debug + Deserialize<'de>> Deserialize<'de> for Dens
|
||||
}
|
||||
}
|
||||
|
||||
const FIELDS: &'static [&'static str] = &["nrows", "ncols", "values"];
|
||||
deserializer.deserialize_struct("DenseMatrix", FIELDS, DenseMatrixVisitor {
|
||||
t: PhantomData
|
||||
})
|
||||
const FIELDS: &'static [&'static str] = &["nrows", "ncols", "values"];
|
||||
deserializer.deserialize_struct(
|
||||
"DenseMatrix",
|
||||
FIELDS,
|
||||
DenseMatrixVisitor { t: PhantomData },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FloatExt + fmt::Debug + Serialize> Serialize for DenseMatrix<T> {
|
||||
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
|
||||
S: Serializer {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let (nrows, ncols) = self.shape();
|
||||
let mut state = serializer.serialize_struct("DenseMatrix", 3)?;
|
||||
state.serialize_field("nrows", &nrows)?;
|
||||
state.serialize_field("ncols", &ncols)?;
|
||||
state.serialize_field("ncols", &ncols)?;
|
||||
state.serialize_field("values", &self.values)?;
|
||||
state.end()
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +222,7 @@ impl<T: FloatExt> Matrix<T> for DenseMatrix<T> {}
|
||||
impl<T: FloatExt> PartialEq for DenseMatrix<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
if self.ncols != other.ncols || self.nrows != other.nrows {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
let len = self.values.len();
|
||||
@@ -235,26 +248,28 @@ impl<T: FloatExt> Into<Vec<T>> for DenseMatrix<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
|
||||
impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
type RowVector = Vec<T>;
|
||||
|
||||
fn from_row_vector(vec: Self::RowVector) -> Self{
|
||||
fn from_row_vector(vec: Self::RowVector) -> Self {
|
||||
DenseMatrix::new(1, vec.len(), vec)
|
||||
}
|
||||
|
||||
fn to_row_vector(self) -> Self::RowVector{
|
||||
fn to_row_vector(self) -> Self::RowVector {
|
||||
self.to_raw_vector()
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&self, row: usize, col: usize) -> T {
|
||||
if row >= self.nrows || col >= self.ncols {
|
||||
panic!("Invalid index ({},{}) for {}x{} matrix", row, col, self.nrows, self.ncols);
|
||||
panic!(
|
||||
"Invalid index ({},{}) for {}x{} matrix",
|
||||
row, col, self.nrows, self.ncols
|
||||
);
|
||||
}
|
||||
self.values[col*self.nrows + row]
|
||||
self.values[col * self.nrows + row]
|
||||
}
|
||||
|
||||
fn get_row_as_vec(&self, row: usize) -> Vec<T>{
|
||||
fn get_row_as_vec(&self, row: usize) -> Vec<T> {
|
||||
let mut result = vec![T::zero(); self.ncols];
|
||||
for c in 0..self.ncols {
|
||||
result[c] = self.get(row, c);
|
||||
@@ -262,16 +277,16 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
result
|
||||
}
|
||||
|
||||
fn get_col_as_vec(&self, col: usize) -> Vec<T>{
|
||||
fn get_col_as_vec(&self, col: usize) -> Vec<T> {
|
||||
let mut result = vec![T::zero(); self.nrows];
|
||||
for r in 0..self.nrows {
|
||||
result[r] = self.get(r, col);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn set(&mut self, row: usize, col: usize, x: T) {
|
||||
self.values[col*self.nrows + row] = x;
|
||||
self.values[col * self.nrows + row] = x;
|
||||
}
|
||||
|
||||
fn zeros(nrows: usize, ncols: usize) -> Self {
|
||||
@@ -280,7 +295,7 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
|
||||
fn ones(nrows: usize, ncols: usize) -> Self {
|
||||
DenseMatrix::fill(nrows, ncols, T::one())
|
||||
}
|
||||
}
|
||||
|
||||
fn eye(size: usize) -> Self {
|
||||
let mut matrix = Self::zeros(size, size);
|
||||
@@ -292,15 +307,15 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
return matrix;
|
||||
}
|
||||
|
||||
fn to_raw_vector(&self) -> Vec<T>{
|
||||
fn to_raw_vector(&self) -> Vec<T> {
|
||||
let mut v = vec![T::zero(); self.nrows * self.ncols];
|
||||
|
||||
for r in 0..self.nrows{
|
||||
for r in 0..self.nrows {
|
||||
for c in 0..self.ncols {
|
||||
v[r * self.ncols + c] = self.get(r, c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
v
|
||||
}
|
||||
|
||||
@@ -314,25 +329,25 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
let mut result = Self::zeros(self.nrows + other.nrows, self.ncols);
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows+other.nrows {
|
||||
if r < self.nrows {
|
||||
for r in 0..self.nrows + other.nrows {
|
||||
if r < self.nrows {
|
||||
result.set(r, c, self.get(r, c));
|
||||
} else {
|
||||
result.set(r, c, other.get(r - self.nrows, c));
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn v_stack(&self, other: &Self) -> Self{
|
||||
fn v_stack(&self, other: &Self) -> Self {
|
||||
if self.nrows != other.nrows {
|
||||
panic!("Number of rows in both matrices should be equal");
|
||||
}
|
||||
let mut result = Self::zeros(self.nrows, self.ncols + other.ncols);
|
||||
for r in 0..self.nrows {
|
||||
for c in 0..self.ncols+other.ncols {
|
||||
if c < self.ncols {
|
||||
for c in 0..self.ncols + other.ncols {
|
||||
if c < self.ncols {
|
||||
result.set(r, c, self.get(r, c));
|
||||
} else {
|
||||
result.set(r, c, other.get(r, c - self.ncols));
|
||||
@@ -343,7 +358,6 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
fn dot(&self, other: &Self) -> Self {
|
||||
|
||||
if self.ncols != other.nrows {
|
||||
panic!("Number of rows of A should equal number of columns of B");
|
||||
}
|
||||
@@ -361,7 +375,7 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn vector_dot(&self, other: &Self) -> T {
|
||||
if (self.nrows != 1 || self.nrows != 1) && (other.nrows != 1 || other.ncols != 1) {
|
||||
@@ -369,18 +383,17 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
if self.nrows * self.ncols != other.nrows * other.ncols {
|
||||
panic!("A and B should have the same size");
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = T::zero();
|
||||
for i in 0..(self.nrows * self.ncols) {
|
||||
result = result + self.values[i] * other.values[i];
|
||||
result = result + self.values[i] * other.values[i];
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn slice(&self, rows: Range<usize>, cols: Range<usize>) -> Self {
|
||||
|
||||
let ncols = cols.len();
|
||||
let nrows = rows.len();
|
||||
|
||||
@@ -388,22 +401,22 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
|
||||
for r in rows.start..rows.end {
|
||||
for c in cols.start..cols.end {
|
||||
m.set(r-rows.start, c-cols.start, self.get(r, c));
|
||||
m.set(r - rows.start, c - cols.start, self.get(r, c));
|
||||
}
|
||||
}
|
||||
|
||||
m
|
||||
}
|
||||
}
|
||||
|
||||
fn approximate_eq(&self, other: &Self, error: T) -> bool {
|
||||
if self.ncols != other.ncols || self.nrows != other.nrows {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows {
|
||||
if (self.get(r, c) - other.get(r, c)).abs() > error {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -418,7 +431,7 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
fn add_mut(&mut self, other: &Self) -> &Self {
|
||||
if self.ncols != other.ncols || self.nrows != other.nrows {
|
||||
panic!("A and B should have the same shape");
|
||||
}
|
||||
}
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows {
|
||||
self.add_element_mut(r, c, other.get(r, c));
|
||||
@@ -431,7 +444,7 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
fn sub_mut(&mut self, other: &Self) -> &Self {
|
||||
if self.ncols != other.ncols || self.nrows != other.nrows {
|
||||
panic!("A and B should have the same shape");
|
||||
}
|
||||
}
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows {
|
||||
self.sub_element_mut(r, c, other.get(r, c));
|
||||
@@ -444,7 +457,7 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
fn mul_mut(&mut self, other: &Self) -> &Self {
|
||||
if self.ncols != other.ncols || self.nrows != other.nrows {
|
||||
panic!("A and B should have the same shape");
|
||||
}
|
||||
}
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows {
|
||||
self.mul_element_mut(r, c, other.get(r, c));
|
||||
@@ -457,7 +470,7 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
fn div_mut(&mut self, other: &Self) -> &Self {
|
||||
if self.ncols != other.ncols || self.nrows != other.nrows {
|
||||
panic!("A and B should have the same shape");
|
||||
}
|
||||
}
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows {
|
||||
self.div_element_mut(r, c, other.get(r, c));
|
||||
@@ -468,26 +481,26 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
fn div_element_mut(&mut self, row: usize, col: usize, x: T) {
|
||||
self.values[col*self.nrows + row] = self.values[col*self.nrows + row] / x;
|
||||
self.values[col * self.nrows + row] = self.values[col * self.nrows + row] / x;
|
||||
}
|
||||
|
||||
fn mul_element_mut(&mut self, row: usize, col: usize, x: T) {
|
||||
self.values[col*self.nrows + row] = self.values[col*self.nrows + row] * x;
|
||||
self.values[col * self.nrows + row] = self.values[col * self.nrows + row] * x;
|
||||
}
|
||||
|
||||
fn add_element_mut(&mut self, row: usize, col: usize, x: T) {
|
||||
self.values[col*self.nrows + row] = self.values[col*self.nrows + row] + x
|
||||
self.values[col * self.nrows + row] = self.values[col * self.nrows + row] + x
|
||||
}
|
||||
|
||||
fn sub_element_mut(&mut self, row: usize, col: usize, x: T) {
|
||||
self.values[col*self.nrows + row] = self.values[col*self.nrows + row] - x;
|
||||
}
|
||||
self.values[col * self.nrows + row] = self.values[col * self.nrows + row] - x;
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Self {
|
||||
let mut m = DenseMatrix {
|
||||
ncols: self.nrows,
|
||||
nrows: self.ncols,
|
||||
values: vec![T::zero(); self.ncols * self.nrows]
|
||||
values: vec![T::zero(); self.ncols * self.nrows],
|
||||
};
|
||||
for c in 0..self.ncols {
|
||||
for r in 0..self.nrows {
|
||||
@@ -495,19 +508,16 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
}
|
||||
m
|
||||
|
||||
}
|
||||
|
||||
fn rand(nrows: usize, ncols: usize) -> Self {
|
||||
let values: Vec<T> = (0..nrows*ncols).map(|_| {
|
||||
T::rand()
|
||||
}).collect();
|
||||
fn rand(nrows: usize, ncols: usize) -> Self {
|
||||
let values: Vec<T> = (0..nrows * ncols).map(|_| T::rand()).collect();
|
||||
DenseMatrix {
|
||||
ncols: ncols,
|
||||
nrows: nrows,
|
||||
values: values
|
||||
values: values,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn norm2(&self) -> T {
|
||||
let mut norm = T::zero();
|
||||
@@ -519,21 +529,25 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
norm.sqrt()
|
||||
}
|
||||
|
||||
fn norm(&self, p:T) -> T {
|
||||
|
||||
fn norm(&self, p: T) -> T {
|
||||
if p.is_infinite() && p.is_sign_positive() {
|
||||
self.values.iter().map(|x| x.abs()).fold(T::neg_infinity(), |a, b| a.max(b))
|
||||
self.values
|
||||
.iter()
|
||||
.map(|x| x.abs())
|
||||
.fold(T::neg_infinity(), |a, b| a.max(b))
|
||||
} else if p.is_infinite() && p.is_sign_negative() {
|
||||
self.values.iter().map(|x| x.abs()).fold(T::infinity(), |a, b| a.min(b))
|
||||
self.values
|
||||
.iter()
|
||||
.map(|x| x.abs())
|
||||
.fold(T::infinity(), |a, b| a.min(b))
|
||||
} else {
|
||||
|
||||
let mut norm = T::zero();
|
||||
|
||||
for xi in self.values.iter() {
|
||||
norm = norm + xi.abs().powf(p);
|
||||
}
|
||||
|
||||
norm.powf(T::one()/p)
|
||||
norm.powf(T::one() / p)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -585,11 +599,14 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
for i in 0..self.values.len() {
|
||||
self.values[i] = -self.values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reshape(&self, nrows: usize, ncols: usize) -> Self {
|
||||
if self.nrows * self.ncols != nrows * ncols {
|
||||
panic!("Can't reshape {}x{} matrix into {}x{}.", self.nrows, self.ncols, nrows, ncols);
|
||||
panic!(
|
||||
"Can't reshape {}x{} matrix into {}x{}.",
|
||||
self.nrows, self.ncols, nrows, ncols
|
||||
);
|
||||
}
|
||||
let mut dst = DenseMatrix::zeros(nrows, ncols);
|
||||
let mut dst_r = 0;
|
||||
@@ -609,9 +626,11 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
fn copy_from(&mut self, other: &Self) {
|
||||
|
||||
if self.nrows != other.nrows || self.ncols != other.ncols {
|
||||
panic!("Can't copy {}x{} matrix into {}x{}.", self.nrows, self.ncols, other.nrows, other.ncols);
|
||||
panic!(
|
||||
"Can't copy {}x{} matrix into {}x{}.",
|
||||
self.nrows, self.ncols, other.nrows, other.ncols
|
||||
);
|
||||
}
|
||||
|
||||
for i in 0..self.values.len() {
|
||||
@@ -619,20 +638,19 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn abs_mut(&mut self) -> &Self{
|
||||
fn abs_mut(&mut self) -> &Self {
|
||||
for i in 0..self.values.len() {
|
||||
self.values[i] = self.values[i].abs();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
fn max_diff(&self, other: &Self) -> T{
|
||||
fn max_diff(&self, other: &Self) -> T {
|
||||
let mut max_diff = T::zero();
|
||||
for i in 0..self.values.len() {
|
||||
max_diff = max_diff.max((self.values[i] - other.values[i]).abs());
|
||||
}
|
||||
max_diff
|
||||
|
||||
}
|
||||
|
||||
fn sum(&self) -> T {
|
||||
@@ -644,7 +662,11 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
fn softmax_mut(&mut self) {
|
||||
let max = self.values.iter().map(|x| x.abs()).fold(T::neg_infinity(), |a, b| a.max(b));
|
||||
let max = self
|
||||
.values
|
||||
.iter()
|
||||
.map(|x| x.abs())
|
||||
.fold(T::neg_infinity(), |a, b| a.max(b));
|
||||
let mut z = T::zero();
|
||||
for r in 0..self.nrows {
|
||||
for c in 0..self.ncols {
|
||||
@@ -668,7 +690,6 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
fn argmax(&self) -> Vec<usize> {
|
||||
|
||||
let mut res = vec![0usize; self.nrows];
|
||||
|
||||
for r in 0..self.nrows {
|
||||
@@ -676,16 +697,15 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
let mut max_pos = 0usize;
|
||||
for c in 0..self.ncols {
|
||||
let v = self.get(r, c);
|
||||
if max < v{
|
||||
if max < v {
|
||||
max = v;
|
||||
max_pos = c;
|
||||
max_pos = c;
|
||||
}
|
||||
}
|
||||
res[r] = max_pos;
|
||||
}
|
||||
|
||||
res
|
||||
|
||||
}
|
||||
|
||||
fn unique(&self) -> Vec<T> {
|
||||
@@ -696,17 +716,16 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
}
|
||||
|
||||
fn cov(&self) -> Self {
|
||||
|
||||
let (m, n) = self.shape();
|
||||
|
||||
let mu = self.column_mean();
|
||||
let mu = self.column_mean();
|
||||
|
||||
let mut cov = Self::zeros(n, n);
|
||||
|
||||
for k in 0..m {
|
||||
for i in 0..n {
|
||||
for j in 0..=i {
|
||||
cov.add_element_mut(i, j, (self.get(k, i) - mu[i]) * (self.get(k, j) - mu[j]));
|
||||
cov.add_element_mut(i, j, (self.get(k, i) - mu[i]) * (self.get(k, j) - mu[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -715,127 +734,88 @@ impl<T: FloatExt> BaseMatrix<T> for DenseMatrix<T> {
|
||||
|
||||
for i in 0..n {
|
||||
for j in 0..=i {
|
||||
cov.div_element_mut(i, j, m_t);
|
||||
cov.div_element_mut(i, j, m_t);
|
||||
cov.set(j, i, cov.get(i, j));
|
||||
}
|
||||
}
|
||||
|
||||
cov
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn from_to_row_vec() {
|
||||
|
||||
let vec = vec![ 1., 2., 3.];
|
||||
assert_eq!(DenseMatrix::from_row_vector(vec.clone()), DenseMatrix::new(1, 3, vec![1., 2., 3.]));
|
||||
assert_eq!(DenseMatrix::from_row_vector(vec.clone()).to_row_vector(), vec![1., 2., 3.]);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn h_stack() {
|
||||
|
||||
let a = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.],
|
||||
&[7., 8., 9.]]);
|
||||
let b = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.],
|
||||
&[7., 8., 9.],
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.]]);
|
||||
let result = a.h_stack(&b);
|
||||
assert_eq!(result, expected);
|
||||
fn from_to_row_vec() {
|
||||
let vec = vec![1., 2., 3.];
|
||||
assert_eq!(
|
||||
DenseMatrix::from_row_vector(vec.clone()),
|
||||
DenseMatrix::new(1, 3, vec![1., 2., 3.])
|
||||
);
|
||||
assert_eq!(
|
||||
DenseMatrix::from_row_vector(vec.clone()).to_row_vector(),
|
||||
vec![1., 2., 3.]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_stack() {
|
||||
|
||||
let a = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.],
|
||||
&[7., 8., 9.]]);
|
||||
let b = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2.],
|
||||
&[3., 4.],
|
||||
&[5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3., 1., 2.],
|
||||
&[4., 5., 6., 3., 4.],
|
||||
&[7., 8., 9., 5., 6.]]);
|
||||
let result = a.v_stack(&b);
|
||||
assert_eq!(result, expected);
|
||||
fn h_stack() {
|
||||
let a = DenseMatrix::from_array(&[&[1., 2., 3.], &[4., 5., 6.], &[7., 8., 9.]]);
|
||||
let b = DenseMatrix::from_array(&[&[1., 2., 3.], &[4., 5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.],
|
||||
&[7., 8., 9.],
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.],
|
||||
]);
|
||||
let result = a.h_stack(&b);
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dot() {
|
||||
|
||||
let a = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.]]);
|
||||
let b = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2.],
|
||||
&[3., 4.],
|
||||
&[5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(
|
||||
&[
|
||||
&[22., 28.],
|
||||
&[49., 64.]]);
|
||||
let result = a.dot(&b);
|
||||
assert_eq!(result, expected);
|
||||
fn v_stack() {
|
||||
let a = DenseMatrix::from_array(&[&[1., 2., 3.], &[4., 5., 6.], &[7., 8., 9.]]);
|
||||
let b = DenseMatrix::from_array(&[&[1., 2.], &[3., 4.], &[5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(&[
|
||||
&[1., 2., 3., 1., 2.],
|
||||
&[4., 5., 6., 3., 4.],
|
||||
&[7., 8., 9., 5., 6.],
|
||||
]);
|
||||
let result = a.v_stack(&b);
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice() {
|
||||
|
||||
let m = DenseMatrix::from_array(
|
||||
&[
|
||||
&[1., 2., 3., 1., 2.],
|
||||
&[4., 5., 6., 3., 4.],
|
||||
&[7., 8., 9., 5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(
|
||||
&[
|
||||
&[2., 3.],
|
||||
&[5., 6.]]);
|
||||
let result = m.slice(0..2, 1..3);
|
||||
assert_eq!(result, expected);
|
||||
fn dot() {
|
||||
let a = DenseMatrix::from_array(&[&[1., 2., 3.], &[4., 5., 6.]]);
|
||||
let b = DenseMatrix::from_array(&[&[1., 2.], &[3., 4.], &[5., 6.]]);
|
||||
let expected = DenseMatrix::from_array(&[&[22., 28.], &[49., 64.]]);
|
||||
let result = a.dot(&b);
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn approximate_eq() {
|
||||
let m = DenseMatrix::from_array(
|
||||
&[
|
||||
&[2., 3.],
|
||||
&[5., 6.]]);
|
||||
let m_eq = DenseMatrix::from_array(
|
||||
&[
|
||||
&[2.5, 3.0],
|
||||
&[5., 5.5]]);
|
||||
let m_neq = DenseMatrix::from_array(
|
||||
&[
|
||||
&[3.0, 3.0],
|
||||
&[5., 6.5]]);
|
||||
assert!(m.approximate_eq(&m_eq, 0.5));
|
||||
assert!(!m.approximate_eq(&m_neq, 0.5));
|
||||
fn slice() {
|
||||
let m = DenseMatrix::from_array(&[
|
||||
&[1., 2., 3., 1., 2.],
|
||||
&[4., 5., 6., 3., 4.],
|
||||
&[7., 8., 9., 5., 6.],
|
||||
]);
|
||||
let expected = DenseMatrix::from_array(&[&[2., 3.], &[5., 6.]]);
|
||||
let result = m.slice(0..2, 1..3);
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn approximate_eq() {
|
||||
let m = DenseMatrix::from_array(&[&[2., 3.], &[5., 6.]]);
|
||||
let m_eq = DenseMatrix::from_array(&[&[2.5, 3.0], &[5., 5.5]]);
|
||||
let m_neq = DenseMatrix::from_array(&[&[3.0, 3.0], &[5., 6.5]]);
|
||||
assert!(m.approximate_eq(&m_eq, 0.5));
|
||||
assert!(!m.approximate_eq(&m_neq, 0.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -864,7 +844,7 @@ mod tests {
|
||||
fn reshape() {
|
||||
let m_orig = DenseMatrix::vector_from_array(&[1., 2., 3., 4., 5., 6.]);
|
||||
let m_2_by_3 = m_orig.reshape(2, 3);
|
||||
let m_result = m_2_by_3.reshape(1, 6);
|
||||
let m_result = m_2_by_3.reshape(1, 6);
|
||||
assert_eq!(m_2_by_3.shape(), (2, 3));
|
||||
assert_eq!(m_2_by_3.get(1, 1), 5.);
|
||||
assert_eq!(m_result.get(0, 1), 2.);
|
||||
@@ -872,70 +852,76 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn norm() {
|
||||
|
||||
let v = DenseMatrix::vector_from_array(&[3., -2., 6.]);
|
||||
assert_eq!(v.norm(1.), 11.);
|
||||
assert_eq!(v.norm(2.), 7.);
|
||||
assert_eq!(v.norm(std::f64::INFINITY), 6.);
|
||||
assert_eq!(v.norm(std::f64::NEG_INFINITY), 2.);
|
||||
fn norm() {
|
||||
let v = DenseMatrix::vector_from_array(&[3., -2., 6.]);
|
||||
assert_eq!(v.norm(1.), 11.);
|
||||
assert_eq!(v.norm(2.), 7.);
|
||||
assert_eq!(v.norm(std::f64::INFINITY), 6.);
|
||||
assert_eq!(v.norm(std::f64::NEG_INFINITY), 2.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn softmax_mut() {
|
||||
fn softmax_mut() {
|
||||
let mut prob: DenseMatrix<f64> = DenseMatrix::vector_from_array(&[1., 2., 3.]);
|
||||
prob.softmax_mut();
|
||||
assert!((prob.get(0, 0) - 0.09).abs() < 0.01);
|
||||
assert!((prob.get(0, 1) - 0.24).abs() < 0.01);
|
||||
assert!((prob.get(0, 2) - 0.66).abs() < 0.01);
|
||||
}
|
||||
|
||||
let mut prob: DenseMatrix<f64> = DenseMatrix::vector_from_array(&[1., 2., 3.]);
|
||||
prob.softmax_mut();
|
||||
assert!((prob.get(0, 0) - 0.09).abs() < 0.01);
|
||||
assert!((prob.get(0, 1) - 0.24).abs() < 0.01);
|
||||
assert!((prob.get(0, 2) - 0.66).abs() < 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn col_mean(){
|
||||
let a = DenseMatrix::from_array(&[
|
||||
&[1., 2., 3.],
|
||||
&[4., 5., 6.],
|
||||
&[7., 8., 9.]]);
|
||||
fn col_mean() {
|
||||
let a = DenseMatrix::from_array(&[&[1., 2., 3.], &[4., 5., 6.], &[7., 8., 9.]]);
|
||||
let res = a.column_mean();
|
||||
assert_eq!(res, vec![4., 5., 6.]);
|
||||
assert_eq!(res, vec![4., 5., 6.]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eye(){
|
||||
let a = DenseMatrix::from_array(&[
|
||||
&[1., 0., 0.],
|
||||
&[0., 1., 0.],
|
||||
&[0., 0., 1.]]);
|
||||
fn eye() {
|
||||
let a = DenseMatrix::from_array(&[&[1., 0., 0.], &[0., 1., 0.], &[0., 0., 1.]]);
|
||||
let res = DenseMatrix::eye(3);
|
||||
assert_eq!(res, a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_from_json() {
|
||||
let a = DenseMatrix::from_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
|
||||
let deserialized_a: DenseMatrix<f64> = serde_json::from_str(&serde_json::to_string(&a).unwrap()).unwrap();
|
||||
assert_eq!(a, deserialized_a);
|
||||
fn to_from_json() {
|
||||
let a = DenseMatrix::from_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
|
||||
let deserialized_a: DenseMatrix<f64> =
|
||||
serde_json::from_str(&serde_json::to_string(&a).unwrap()).unwrap();
|
||||
assert_eq!(a, deserialized_a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_from_bincode() {
|
||||
let a = DenseMatrix::from_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
|
||||
let deserialized_a: DenseMatrix<f64> = bincode::deserialize(&bincode::serialize(&a).unwrap()).unwrap();
|
||||
assert_eq!(a, deserialized_a);
|
||||
fn to_from_bincode() {
|
||||
let a = DenseMatrix::from_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
|
||||
let deserialized_a: DenseMatrix<f64> =
|
||||
bincode::deserialize(&bincode::serialize(&a).unwrap()).unwrap();
|
||||
assert_eq!(a, deserialized_a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
let a = DenseMatrix::from_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
|
||||
assert_eq!(format!("{}", a), "[[0.9, 0.4, 0.7], [0.4, 0.5, 0.3], [0.7, 0.3, 0.8]]");
|
||||
fn to_string() {
|
||||
let a = DenseMatrix::from_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
|
||||
assert_eq!(
|
||||
format!("{}", a),
|
||||
"[[0.9, 0.4, 0.7], [0.4, 0.5, 0.3], [0.7, 0.3, 0.8]]"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cov() {
|
||||
let a = DenseMatrix::from_array(&[&[64.0, 580.0, 29.0], &[66.0, 570.0, 33.0], &[68.0, 590.0, 37.0], &[69.0, 660.0, 46.0], &[73.0, 600.0, 55.0]]);
|
||||
let expected = DenseMatrix::from_array(&[&[11.5, 50.0, 34.75], &[50.0, 1250.0, 205.0], &[34.75, 205.0, 110.0]]);
|
||||
assert_eq!(a.cov(), expected);
|
||||
fn cov() {
|
||||
let a = DenseMatrix::from_array(&[
|
||||
&[64.0, 580.0, 29.0],
|
||||
&[66.0, 570.0, 33.0],
|
||||
&[68.0, 590.0, 37.0],
|
||||
&[69.0, 660.0, 46.0],
|
||||
&[73.0, 600.0, 55.0],
|
||||
]);
|
||||
let expected = DenseMatrix::from_array(&[
|
||||
&[11.5, 50.0, 34.75],
|
||||
&[50.0, 1250.0, 205.0],
|
||||
&[34.75, 205.0, 110.0],
|
||||
]);
|
||||
assert_eq!(a.cov(), expected);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user