feat: adds elastic net

This commit is contained in:
Volodymyr Orlov
2020-12-11 18:55:07 -08:00
parent 2650416235
commit 78673b597f
8 changed files with 647 additions and 237 deletions
+3
View File
@@ -271,6 +271,9 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
fn std(&self) -> T {
self.var().sqrt()
}
/// Copies content of `other` vector.
fn copy_from(&mut self, other: &Self);
}
/// Generic matrix type.
+14
View File
@@ -176,6 +176,20 @@ impl<T: RealNumber> BaseVector<T> for Vec<T> {
result.dedup();
result
}
fn copy_from(&mut self, other: &Self) {
if self.len() != other.len() {
panic!(
"Can't copy vector of length {} into a vector of length {}.",
self.len(),
other.len()
);
}
for i in 0..self.len() {
self[i] = other[i];
}
}
}
/// Column-major, dense matrix. See [Simple Dense Matrix](../index.html).
+14
View File
@@ -181,6 +181,10 @@ impl<T: RealNumber + 'static> BaseVector<T> for MatrixMN<T, U1, Dynamic> {
result.dedup();
result
}
fn copy_from(&mut self, other: &Self) {
Matrix::copy_from(self, other);
}
}
impl<T: RealNumber + Scalar + AddAssign + SubAssign + MulAssign + DivAssign + Sum + 'static>
@@ -575,6 +579,16 @@ mod tests {
use crate::linear::linear_regression::*;
use nalgebra::{DMatrix, Matrix2x3, RowDVector};
#[test]
fn vec_copy_from() {
let mut v1 = RowDVector::from_vec(vec![1., 2., 3.]);
let mut v2 = RowDVector::from_vec(vec![4., 5., 6.]);
v1.copy_from(&v2);
assert_eq!(v2, v1);
v2[0] = 10.0;
assert_ne!(v2, v1);
}
#[test]
fn vec_len() {
let v = RowDVector::from_vec(vec![1., 2., 3.]);
+14
View File
@@ -176,6 +176,10 @@ impl<T: RealNumber + ScalarOperand> BaseVector<T> for ArrayBase<OwnedRepr<T>, Ix
result.dedup();
result
}
fn copy_from(&mut self, other: &Self) {
self.assign(&other);
}
}
impl<T: RealNumber + ScalarOperand + AddAssign + SubAssign + MulAssign + DivAssign + Sum>
@@ -537,6 +541,16 @@ mod tests {
assert_eq!(5., BaseVector::get(&result, 1));
}
#[test]
fn vec_copy_from() {
let mut v1 = arr1(&[1., 2., 3.]);
let mut v2 = arr1(&[4., 5., 6.]);
v1.copy_from(&v2);
assert_eq!(v1, v2);
v2[0] = 10.0;
assert_ne!(v1, v2);
}
#[test]
fn vec_len() {
let v = arr1(&[1., 2., 3.]);