Removes DenseVector

This commit is contained in:
Volodymyr Orlov
2019-12-18 10:28:15 -08:00
parent 4411b57219
commit 2425419d10
9 changed files with 376 additions and 590 deletions
+14 -118
View File
@@ -3,10 +3,16 @@ use std::fmt::Debug;
pub mod naive;
pub trait Matrix: Into<Vec<f64>> + Clone + Debug{
pub trait Matrix: Clone + Debug {
fn from_array(nrows: usize, ncols: usize, values: &[f64]) -> Self;
fn from_vec(nrows: usize, ncols: usize, values: Vec<f64>) -> Self;
fn get(&self, row: usize, col: usize) -> f64;
fn set(&mut self, row: usize, col: usize, x: f64);
fn qr_solve_mut(&mut self, b: Self) -> Self;
fn svd_solve_mut(&mut self, b: Self) -> Self;
@@ -15,7 +21,7 @@ pub trait Matrix: Into<Vec<f64>> + Clone + Debug{
fn ones(nrows: usize, ncols: usize) -> Self;
fn from_vector<V:Vector>(v: &V, nrows: usize, ncols: usize) -> Self;
fn to_raw_vector(&self) -> Vec<f64>;
fn fill(nrows: usize, ncols: usize, value: f64) -> Self;
@@ -27,7 +33,9 @@ pub trait Matrix: Into<Vec<f64>> + Clone + Debug{
fn dot(&self, other: &Self) -> Self;
fn slice(&self, rows: Range<usize>, cols: Range<usize>) -> Self;
fn vector_dot(&self, other: &Self) -> f64;
fn slice(&self, rows: Range<usize>, cols: Range<usize>) -> Self;
fn approximate_eq(&self, other: &Self, error: f64) -> bool;
@@ -139,120 +147,8 @@ pub trait Matrix: Into<Vec<f64>> + Clone + Debug{
result
}
fn argmax(&self) -> Vec<usize>;
}
pub trait Vector: Into<Vec<f64>> + Clone + Debug {
fn from_array(values: &[f64]) -> Self;
fn from_vec(values: &Vec<f64>) -> Self;
fn get(&self, i: usize) -> f64;
fn set(&mut self, i: usize, value: f64);
fn zeros(size: usize) -> Self;
fn ones(size: usize) -> Self;
fn fill(size: usize, value: f64) -> Self;
fn shape(&self) -> (usize, usize);
fn norm2(&self) -> f64;
fn norm(&self, p:f64) -> f64;
fn negative_mut(&mut self) -> &Self;
fn negative(&self) -> Self;
fn add_mut(&mut self, other: &Self) -> &Self;
fn sub_mut(&mut self, other: &Self) -> &Self;
fn mul_mut(&mut self, other: &Self) -> &Self;
fn div_mut(&mut self, other: &Self) -> &Self;
fn add(&self, other: &Self) -> Self {
let mut r = self.clone();
r.add_mut(other);
r
}
fn sub(&self, other: &Self) -> Self {
let mut r = self.clone();
r.sub_mut(other);
r
}
fn mul(&self, other: &Self) -> Self {
let mut r = self.clone();
r.mul_mut(other);
r
}
fn div(&self, other: &Self) -> Self {
let mut r = self.clone();
r.div_mut(other);
r
}
fn add_scalar_mut(&mut self, scalar: f64) -> &Self;
fn sub_scalar_mut(&mut self, scalar: f64) -> &Self;
fn mul_scalar_mut(&mut self, scalar: f64) -> &Self;
fn div_scalar_mut(&mut self, scalar: f64) -> &Self;
fn add_scalar(&self, scalar: f64) -> Self{
let mut r = self.clone();
r.add_scalar_mut(scalar);
r
}
fn sub_scalar(&self, scalar: f64) -> Self{
let mut r = self.clone();
r.sub_scalar_mut(scalar);
r
}
fn mul_scalar(&self, scalar: f64) -> Self{
let mut r = self.clone();
r.mul_scalar_mut(scalar);
r
}
fn div_scalar(&self, scalar: f64) -> Self{
let mut r = self.clone();
r.div_scalar_mut(scalar);
r
}
fn dot(&self, other: &Self) -> f64;
fn copy_from(&mut self, other: &Self);
fn abs_mut(&mut self) -> &Self;
fn pow_mut(&mut self, p: f64) -> &Self;
fn sum(&self) -> f64;
fn abs(&self) -> Self{
let mut r = self.clone();
r.abs_mut();
r
}
fn max_diff(&self, other: &Self) -> f64;
fn argmax(&self) -> Vec<usize>;
fn softmax_mut(&mut self);
fn unique(&self) -> Vec<f64>;
fn unique(&self) -> Vec<f64>;
}
}
+58 -39
View File
@@ -1,5 +1,5 @@
use std::ops::Range;
use crate::linalg::{Matrix, Vector};
use crate::linalg::{Matrix};
use crate::math;
use rand::prelude::*;
@@ -12,7 +12,7 @@ pub struct DenseMatrix {
}
impl DenseMatrix {
impl DenseMatrix {
pub fn from_2d_array(values: &[&[f64]]) -> DenseMatrix {
DenseMatrix::from_2d_vec(&values.into_iter().map(|row| Vec::from(*row)).collect())
@@ -32,19 +32,7 @@ impl DenseMatrix {
}
}
m
}
pub fn from_array(nrows: usize, ncols: usize, values: &[f64]) -> DenseMatrix {
DenseMatrix::from_vec(nrows, ncols, Vec::from(values))
}
pub fn from_vec(nrows: usize, ncols: usize, values: Vec<f64>) -> DenseMatrix {
DenseMatrix {
ncols: ncols,
nrows: nrows,
values: values
}
}
}
pub fn vector_from_array(values: &[f64]) -> DenseMatrix {
DenseMatrix::vector_from_vec(Vec::from(values))
@@ -66,10 +54,10 @@ impl DenseMatrix {
for i in 0..self.values.len() {
self.values[i] /= b.values[i];
}
}
}
pub fn set(&mut self, row: usize, col: usize, x: f64) {
self.values[col*self.nrows + row] = x;
pub fn get_raw_values(&self) -> &Vec<f64> {
&self.values
}
fn div_element_mut(&mut self, row: usize, col: usize, x: f64) {
@@ -86,7 +74,7 @@ impl DenseMatrix {
fn sub_element_mut(&mut self, row: usize, col: usize, x: f64) {
self.values[col*self.nrows + row] -= x;
}
}
}
@@ -119,38 +107,46 @@ impl Into<Vec<f64>> for DenseMatrix {
}
}
impl Matrix for DenseMatrix {
impl Matrix for DenseMatrix {
fn from_array(nrows: usize, ncols: usize, values: &[f64]) -> DenseMatrix {
DenseMatrix::from_vec(nrows, ncols, Vec::from(values))
}
fn from_vec(nrows: usize, ncols: usize, values: Vec<f64>) -> DenseMatrix {
DenseMatrix {
ncols: ncols,
nrows: nrows,
values: values
}
}
fn get(&self, row: usize, col: usize) -> f64 {
self.values[col*self.nrows + row]
}
fn set(&mut self, row: usize, col: usize, x: f64) {
self.values[col*self.nrows + row] = x;
}
fn zeros(nrows: usize, ncols: usize) -> DenseMatrix {
DenseMatrix::fill(nrows, ncols, 0f64)
}
fn ones(nrows: usize, ncols: usize) -> DenseMatrix {
DenseMatrix::fill(nrows, ncols, 1f64)
}
}
fn from_vector<V:Vector>(v: &V, nrows: usize, ncols: usize) -> Self {
let (_, v_size) = v.shape();
if nrows * ncols != v_size {
panic!("Can't reshape {}-long vector into {}x{} matrix.", v_size, nrows, ncols);
fn to_raw_vector(&self) -> Vec<f64>{
let mut v = vec![0.; self.nrows * self.ncols];
for r in 0..self.nrows{
for c in 0..self.ncols {
v[r * self.ncols + c] = self.get(r, c);
}
}
let mut dst = DenseMatrix::zeros(nrows, ncols);
let mut dst_r = 0;
let mut dst_c = 0;
for i in 0..v_size {
dst.set(dst_r, dst_c, v.get(i));
if dst_c + 1 >= ncols {
dst_c = 0;
dst_r += 1;
} else {
dst_c += 1;
}
}
dst
v
}
fn shape(&self) -> (usize, usize) {
@@ -212,6 +208,22 @@ impl Matrix for DenseMatrix {
result
}
fn vector_dot(&self, other: &Self) -> f64 {
if (self.nrows != 1 || self.nrows != 1) && (other.nrows != 1 || other.ncols != 1) {
panic!("A and B should both be 1-dimentional vectors.");
}
if self.nrows * self.ncols != other.nrows * other.ncols {
panic!("A and B should have the same size");
}
let mut result = 0f64;
for i in 0..(self.nrows * self.ncols) {
result += self.values[i] * other.values[i];
}
result
}
fn slice(&self, rows: Range<usize>, cols: Range<usize>) -> DenseMatrix {
let ncols = cols.len();
@@ -226,7 +238,7 @@ impl Matrix for DenseMatrix {
}
m
}
}
fn qr_solve_mut(&mut self, mut b: DenseMatrix) -> DenseMatrix {
let m = self.nrows;
@@ -943,6 +955,13 @@ impl Matrix for DenseMatrix {
}
fn unique(&self) -> Vec<f64> {
let mut result = self.values.clone();
result.sort_by(|a, b| a.partial_cmp(b).unwrap());
result.dedup();
result
}
}
#[cfg(test)]
-304
View File
@@ -1,304 +0,0 @@
use crate::linalg::{Vector, Matrix};
use crate::math;
use crate::linalg::naive::dense_matrix::DenseMatrix;
#[derive(Debug, Clone)]
pub struct DenseVector {
size: usize,
values: Vec<f64>
}
impl Into<Vec<f64>> for DenseVector {
fn into(self) -> Vec<f64> {
self.values
}
}
impl PartialEq for DenseVector {
fn eq(&self, other: &Self) -> bool {
if self.size != other.size {
return false
}
let len = self.values.len();
let other_len = other.values.len();
if len != other_len {
return false;
}
for i in 0..len {
if (self.values[i] - other.values[i]).abs() > math::EPSILON {
return false;
}
}
true
}
}
impl Vector for DenseVector {
fn from_array(values: &[f64]) -> Self {
DenseVector::from_vec(&Vec::from(values))
}
fn from_vec(values: &Vec<f64>) -> Self {
DenseVector {
size: values.len(),
values: values.clone()
}
}
fn get(&self, i: usize) -> f64 {
self.values[i]
}
fn set(&mut self, i: usize, value: f64) {
self.values[i] = value;
}
fn zeros(size: usize) -> Self {
DenseVector::fill(size, 0f64)
}
fn ones(size: usize) -> Self {
DenseVector::fill(size, 1f64)
}
fn fill(size: usize, value: f64) -> Self {
DenseVector::from_vec(&vec![value; size])
}
fn shape(&self) -> (usize, usize) {
(1, self.size)
}
fn add_mut(&mut self, other: &Self) -> &Self {
if self.size != other.size {
panic!("A and B should have the same shape");
}
for i in 0..self.size {
self.values[i] += other.values[i];
}
self
}
fn mul_mut(&mut self, other: &Self) -> &Self {
if self.size != other.size {
panic!("A and B should have the same shape");
}
for i in 0..self.size {
self.values[i] *= other.values[i];
}
self
}
fn sub_mut(&mut self, other: &Self) -> &Self {
if self.size != other.size {
panic!("A and B should have the same shape");
}
for i in 0..self.size {
self.values[i] -= other.values[i];
}
self
}
fn div_mut(&mut self, other: &Self) -> &Self {
if self.size != other.size {
panic!("A and B should have the same shape");
}
for i in 0..self.size {
self.values[i] /= other.values[i];
}
self
}
fn dot(&self, other: &Self) -> f64 {
if self.size != other.size {
panic!("A and B should be of the same size");
}
let mut result = 0f64;
for i in 0..self.size {
result += self.get(i) * other.get(i);
}
result
}
fn norm2(&self) -> f64 {
let mut norm = 0f64;
for xi in self.values.iter() {
norm += xi * xi;
}
norm.sqrt()
}
fn norm(&self, p:f64) -> f64 {
if p.is_infinite() && p.is_sign_positive() {
self.values.iter().map(|x| x.abs()).fold(std::f64::NEG_INFINITY, |a, b| a.max(b))
} else if p.is_infinite() && p.is_sign_negative() {
self.values.iter().map(|x| x.abs()).fold(std::f64::INFINITY, |a, b| a.min(b))
} else {
let mut norm = 0f64;
for xi in self.values.iter() {
norm += xi.abs().powf(p);
}
norm.powf(1.0/p)
}
}
fn add_scalar_mut(&mut self, scalar: f64) -> &Self {
for i in 0..self.values.len() {
self.values[i] += scalar;
}
self
}
fn sub_scalar_mut(&mut self, scalar: f64) -> &Self {
for i in 0..self.values.len() {
self.values[i] -= scalar;
}
self
}
fn mul_scalar_mut(&mut self, scalar: f64) -> &Self {
for i in 0..self.values.len() {
self.values[i] *= scalar;
}
self
}
fn div_scalar_mut(&mut self, scalar: f64) -> &Self {
for i in 0..self.values.len() {
self.values[i] /= scalar;
}
self
}
fn negative_mut(&mut self) -> &Self {
for i in 0..self.values.len() {
self.values[i] = -self.values[i];
}
self
}
fn abs_mut(&mut self) -> &Self{
for i in 0..self.values.len() {
self.values[i] = self.values[i].abs();
}
self
}
fn pow_mut(&mut self, p: f64) -> &Self{
for i in 0..self.values.len() {
self.values[i] = self.values[i].powf(p);
}
self
}
fn sum(&self) -> f64 {
let mut sum = 0.;
for i in 0..self.values.len() {
sum += self.values[i];
}
sum
}
fn negative(&self) -> Self {
let mut result = DenseVector {
size: self.size,
values: self.values.clone()
};
for i in 0..self.values.len() {
result.values[i] = -self.values[i];
}
result
}
fn copy_from(&mut self, other: &Self) {
for i in 0..self.values.len() {
self.values[i] = other.values[i];
}
}
fn max_diff(&self, other: &Self) -> f64{
let mut max_diff = 0f64;
for i in 0..self.values.len() {
max_diff = max_diff.max((self.values[i] - other.values[i]).abs());
}
max_diff
}
fn softmax_mut(&mut self) {
let max = self.values.iter().map(|x| x.abs()).fold(std::f64::NEG_INFINITY, |a, b| a.max(b));
let mut z = 0.;
for i in 0..self.size {
let p = (self.values[i] - max).exp();
self.values[i] = p;
z += p;
}
for i in 0..self.size {
self.values[i] /= z;
}
}
fn unique(&self) -> Vec<f64> {
let mut result = self.values.clone();
result.sort_by(|a, b| a.partial_cmp(b).unwrap());
result.dedup();
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn qr_solve_mut() {
let v = DenseVector::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 copy_from() {
let mut a = DenseVector::from_array(&[0., 0., 0.]);
let b = DenseVector::from_array(&[-1., 0., 2.]);
a.copy_from(&b);
assert_eq!(a.get(0), b.get(0));
assert_eq!(a.get(1), b.get(1));
assert_eq!(a.get(2), b.get(2));
}
#[test]
fn softmax_mut() {
let mut prob = DenseVector::from_array(&[1., 2., 3.]);
prob.softmax_mut();
assert!((prob.get(0) - 0.09).abs() < 0.01);
assert!((prob.get(1) - 0.24).abs() < 0.01);
assert!((prob.get(2) - 0.66).abs() < 0.01);
}
}
+1 -2
View File
@@ -1,2 +1 @@
pub mod dense_matrix;
pub mod dense_vector;
pub mod dense_matrix;