Skip to content

Commit 78673b5

Browse files
Volodymyr OrlovVolodymyr Orlov
authored andcommitted
feat: adds elastic net
1 parent 2650416 commit 78673b5

File tree

8 files changed

+647
-237
lines changed

8 files changed

+647
-237
lines changed

src/linalg/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
271271
fn std(&self) -> T {
272272
self.var().sqrt()
273273
}
274+
275+
/// Copies content of `other` vector.
276+
fn copy_from(&mut self, other: &Self);
274277
}
275278

276279
/// Generic matrix type.

src/linalg/naive/dense_matrix.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ impl<T: RealNumber> BaseVector<T> for Vec<T> {
176176
result.dedup();
177177
result
178178
}
179+
180+
fn copy_from(&mut self, other: &Self) {
181+
if self.len() != other.len() {
182+
panic!(
183+
"Can't copy vector of length {} into a vector of length {}.",
184+
self.len(),
185+
other.len()
186+
);
187+
}
188+
189+
for i in 0..self.len() {
190+
self[i] = other[i];
191+
}
192+
}
179193
}
180194

181195
/// Column-major, dense matrix. See [Simple Dense Matrix](../index.html).

src/linalg/nalgebra_bindings.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ impl<T: RealNumber + 'static> BaseVector<T> for MatrixMN<T, U1, Dynamic> {
181181
result.dedup();
182182
result
183183
}
184+
185+
fn copy_from(&mut self, other: &Self) {
186+
Matrix::copy_from(self, other);
187+
}
184188
}
185189

186190
impl<T: RealNumber + Scalar + AddAssign + SubAssign + MulAssign + DivAssign + Sum + 'static>
@@ -575,6 +579,16 @@ mod tests {
575579
use crate::linear::linear_regression::*;
576580
use nalgebra::{DMatrix, Matrix2x3, RowDVector};
577581

582+
#[test]
583+
fn vec_copy_from() {
584+
let mut v1 = RowDVector::from_vec(vec![1., 2., 3.]);
585+
let mut v2 = RowDVector::from_vec(vec![4., 5., 6.]);
586+
v1.copy_from(&v2);
587+
assert_eq!(v2, v1);
588+
v2[0] = 10.0;
589+
assert_ne!(v2, v1);
590+
}
591+
578592
#[test]
579593
fn vec_len() {
580594
let v = RowDVector::from_vec(vec![1., 2., 3.]);

src/linalg/ndarray_bindings.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ impl<T: RealNumber + ScalarOperand> BaseVector<T> for ArrayBase<OwnedRepr<T>, Ix
176176
result.dedup();
177177
result
178178
}
179+
180+
fn copy_from(&mut self, other: &Self) {
181+
self.assign(&other);
182+
}
179183
}
180184

181185
impl<T: RealNumber + ScalarOperand + AddAssign + SubAssign + MulAssign + DivAssign + Sum>
@@ -537,6 +541,16 @@ mod tests {
537541
assert_eq!(5., BaseVector::get(&result, 1));
538542
}
539543

544+
#[test]
545+
fn vec_copy_from() {
546+
let mut v1 = arr1(&[1., 2., 3.]);
547+
let mut v2 = arr1(&[4., 5., 6.]);
548+
v1.copy_from(&v2);
549+
assert_eq!(v1, v2);
550+
v2[0] = 10.0;
551+
assert_ne!(v1, v2);
552+
}
553+
540554
#[test]
541555
fn vec_len() {
542556
let v = arr1(&[1., 2., 3.]);

0 commit comments

Comments
 (0)