Skip to content

Commit bbe810d

Browse files
Volodymyr OrlovVolodymyr Orlov
authored andcommitted
feat: documents matrix methods
1 parent 1e3ed4c commit bbe810d

25 files changed

+570
-228
lines changed

src/algorithm/neighbour/bbd_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ mod tests {
337337

338338
#[test]
339339
fn fit_predict_iris() {
340-
let data = DenseMatrix::from_array(&[
340+
let data = DenseMatrix::from_2d_array(&[
341341
&[5.1, 3.5, 1.4, 0.2],
342342
&[4.9, 3.0, 1.4, 0.2],
343343
&[4.7, 3.2, 1.3, 0.2],

src/cluster/kmeans.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! use smartcore::cluster::kmeans::*;
2121
//!
2222
//! // Iris data
23-
//! let x = DenseMatrix::from_array(&[
23+
//! let x = DenseMatrix::from_2d_array(&[
2424
//! &[5.1, 3.5, 1.4, 0.2],
2525
//! &[4.9, 3.0, 1.4, 0.2],
2626
//! &[4.7, 3.2, 1.3, 0.2],
@@ -264,7 +264,7 @@ mod tests {
264264

265265
#[test]
266266
fn fit_predict_iris() {
267-
let x = DenseMatrix::from_array(&[
267+
let x = DenseMatrix::from_2d_array(&[
268268
&[5.1, 3.5, 1.4, 0.2],
269269
&[4.9, 3.0, 1.4, 0.2],
270270
&[4.7, 3.2, 1.3, 0.2],
@@ -298,7 +298,7 @@ mod tests {
298298

299299
#[test]
300300
fn serde() {
301-
let x = DenseMatrix::from_array(&[
301+
let x = DenseMatrix::from_2d_array(&[
302302
&[5.1, 3.5, 1.4, 0.2],
303303
&[4.9, 3.0, 1.4, 0.2],
304304
&[4.7, 3.2, 1.3, 0.2],

src/decomposition/pca.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! use smartcore::decomposition::pca::*;
1515
//!
1616
//! // Iris data
17-
//! let iris = DenseMatrix::from_array(&[
17+
//! let iris = DenseMatrix::from_2d_array(&[
1818
//! &[5.1, 3.5, 1.4, 0.2],
1919
//! &[4.9, 3.0, 1.4, 0.2],
2020
//! &[4.7, 3.2, 1.3, 0.2],
@@ -211,7 +211,7 @@ impl<T: RealNumber, M: Matrix<T>> PCA<T, M> {
211211
);
212212
}
213213

214-
let mut x_transformed = x.dot(&self.projection);
214+
let mut x_transformed = x.matmul(&self.projection);
215215
for r in 0..nrows {
216216
for c in 0..n_components {
217217
x_transformed.sub_element_mut(r, c, self.pmu[c]);
@@ -227,7 +227,7 @@ mod tests {
227227
use crate::linalg::naive::dense_matrix::*;
228228

229229
fn us_arrests_data() -> DenseMatrix<f64> {
230-
DenseMatrix::from_array(&[
230+
DenseMatrix::from_2d_array(&[
231231
&[13.2, 236.0, 58.0, 21.2],
232232
&[10.0, 263.0, 48.0, 44.5],
233233
&[8.1, 294.0, 80.0, 31.0],
@@ -285,7 +285,7 @@ mod tests {
285285
fn decompose_covariance() {
286286
let us_arrests = us_arrests_data();
287287

288-
let expected_eigenvectors = DenseMatrix::from_array(&[
288+
let expected_eigenvectors = DenseMatrix::from_2d_array(&[
289289
&[
290290
-0.0417043206282872,
291291
-0.0448216562696701,
@@ -312,7 +312,7 @@ mod tests {
312312
],
313313
]);
314314

315-
let expected_projection = DenseMatrix::from_array(&[
315+
let expected_projection = DenseMatrix::from_2d_array(&[
316316
&[-64.8022, -11.448, 2.4949, -2.4079],
317317
&[-92.8275, -17.9829, -20.1266, 4.094],
318318
&[-124.0682, 8.8304, 1.6874, 4.3537],
@@ -394,7 +394,7 @@ mod tests {
394394
fn decompose_correlation() {
395395
let us_arrests = us_arrests_data();
396396

397-
let expected_eigenvectors = DenseMatrix::from_array(&[
397+
let expected_eigenvectors = DenseMatrix::from_2d_array(&[
398398
&[
399399
0.124288601688222,
400400
-0.0969866877028367,
@@ -421,7 +421,7 @@ mod tests {
421421
],
422422
]);
423423

424-
let expected_projection = DenseMatrix::from_array(&[
424+
let expected_projection = DenseMatrix::from_2d_array(&[
425425
&[0.9856, -1.1334, 0.4443, -0.1563],
426426
&[1.9501, -1.0732, -2.04, 0.4386],
427427
&[1.7632, 0.746, -0.0548, 0.8347],
@@ -507,7 +507,7 @@ mod tests {
507507

508508
#[test]
509509
fn serde() {
510-
let iris = DenseMatrix::from_array(&[
510+
let iris = DenseMatrix::from_2d_array(&[
511511
&[5.1, 3.5, 1.4, 0.2],
512512
&[4.9, 3.0, 1.4, 0.2],
513513
&[4.7, 3.2, 1.3, 0.2],

src/ensemble/random_forest_classifier.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! use smartcore::ensemble::random_forest_classifier::*;
1313
//!
1414
//! // Iris dataset
15-
//! let x = DenseMatrix::from_array(&[
15+
//! let x = DenseMatrix::from_2d_array(&[
1616
//! &[5.1, 3.5, 1.4, 0.2],
1717
//! &[4.9, 3.0, 1.4, 0.2],
1818
//! &[4.7, 3.2, 1.3, 0.2],
@@ -226,7 +226,7 @@ mod tests {
226226

227227
#[test]
228228
fn fit_predict_iris() {
229-
let x = DenseMatrix::from_array(&[
229+
let x = DenseMatrix::from_2d_array(&[
230230
&[5.1, 3.5, 1.4, 0.2],
231231
&[4.9, 3.0, 1.4, 0.2],
232232
&[4.7, 3.2, 1.3, 0.2],
@@ -270,7 +270,7 @@ mod tests {
270270

271271
#[test]
272272
fn serde() {
273-
let x = DenseMatrix::from_array(&[
273+
let x = DenseMatrix::from_2d_array(&[
274274
&[5.1, 3.5, 1.4, 0.2],
275275
&[4.9, 3.0, 1.4, 0.2],
276276
&[4.7, 3.2, 1.3, 0.2],

src/ensemble/random_forest_regressor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! use smartcore::ensemble::random_forest_regressor::*;
1313
//!
1414
//! // Longley dataset (https://www.statsmodels.org/stable/datasets/generated/longley.html)
15-
//! let x = DenseMatrix::from_array(&[
15+
//! let x = DenseMatrix::from_2d_array(&[
1616
//! &[234.289, 235.6, 159., 107.608, 1947., 60.323],
1717
//! &[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
1818
//! &[258.054, 368.2, 161.6, 109.773, 1949., 60.171],
@@ -184,7 +184,7 @@ mod tests {
184184

185185
#[test]
186186
fn fit_longley() {
187-
let x = DenseMatrix::from_array(&[
187+
let x = DenseMatrix::from_2d_array(&[
188188
&[234.289, 235.6, 159., 107.608, 1947., 60.323],
189189
&[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
190190
&[258.054, 368.2, 161.6, 109.773, 1949., 60.171],
@@ -231,7 +231,7 @@ mod tests {
231231

232232
#[test]
233233
fn serde() {
234-
let x = DenseMatrix::from_array(&[
234+
let x = DenseMatrix::from_2d_array(&[
235235
&[234.289, 235.6, 159., 107.608, 1947., 60.323],
236236
&[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
237237
&[258.054, 368.2, 161.6, 109.773, 1949., 60.171],

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
//! use smartcore::math::distance::*;
4949
//!
5050
//! // Turn Rust vectors with samples into a matrix
51-
//! let x = DenseMatrix::from_array(&[
51+
//! let x = DenseMatrix::from_2d_array(&[
5252
//! &[1., 2.],
5353
//! &[3., 4.],
5454
//! &[5., 6.],

src/linalg/evd.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,15 @@ mod tests {
793793

794794
#[test]
795795
fn decompose_symmetric() {
796-
let A = DenseMatrix::from_array(&[
796+
let A = DenseMatrix::from_2d_array(&[
797797
&[0.9000, 0.4000, 0.7000],
798798
&[0.4000, 0.5000, 0.3000],
799799
&[0.7000, 0.3000, 0.8000],
800800
]);
801801

802802
let eigen_values: Vec<f64> = vec![1.7498382, 0.3165784, 0.1335834];
803803

804-
let eigen_vectors = DenseMatrix::from_array(&[
804+
let eigen_vectors = DenseMatrix::from_2d_array(&[
805805
&[0.6881997, -0.07121225, 0.7220180],
806806
&[0.3700456, 0.89044952, -0.2648886],
807807
&[0.6240573, -0.44947578, -0.6391588],
@@ -820,15 +820,15 @@ mod tests {
820820

821821
#[test]
822822
fn decompose_asymmetric() {
823-
let A = DenseMatrix::from_array(&[
823+
let A = DenseMatrix::from_2d_array(&[
824824
&[0.9000, 0.4000, 0.7000],
825825
&[0.4000, 0.5000, 0.3000],
826826
&[0.8000, 0.3000, 0.8000],
827827
]);
828828

829829
let eigen_values: Vec<f64> = vec![1.79171122, 0.31908143, 0.08920735];
830830

831-
let eigen_vectors = DenseMatrix::from_array(&[
831+
let eigen_vectors = DenseMatrix::from_2d_array(&[
832832
&[0.7178958, 0.05322098, 0.6812010],
833833
&[0.3837711, -0.84702111, -0.1494582],
834834
&[0.6952105, 0.43984484, -0.7036135],
@@ -847,7 +847,7 @@ mod tests {
847847

848848
#[test]
849849
fn decompose_complex() {
850-
let A = DenseMatrix::from_array(&[
850+
let A = DenseMatrix::from_2d_array(&[
851851
&[3.0, -2.0, 1.0, 1.0],
852852
&[4.0, -1.0, 1.0, 1.0],
853853
&[1.0, 1.0, 3.0, -2.0],
@@ -857,7 +857,7 @@ mod tests {
857857
let eigen_values_d: Vec<f64> = vec![0.0, 2.0, 2.0, 0.0];
858858
let eigen_values_e: Vec<f64> = vec![2.2361, 0.9999, -0.9999, -2.2361];
859859

860-
let eigen_vectors = DenseMatrix::from_array(&[
860+
let eigen_vectors = DenseMatrix::from_2d_array(&[
861861
&[-0.9159, -0.1378, 0.3816, -0.0806],
862862
&[-0.6707, 0.1059, 0.901, 0.6289],
863863
&[0.9159, -0.1378, 0.3816, 0.0806],

src/linalg/lu.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,13 @@ mod tests {
225225

226226
#[test]
227227
fn decompose() {
228-
let a = DenseMatrix::from_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]);
229-
let expected_L = DenseMatrix::from_array(&[&[1., 0., 0.], &[0., 1., 0.], &[0.2, 0.8, 1.]]);
230-
let expected_U = DenseMatrix::from_array(&[&[5., 6., 0.], &[0., 1., 5.], &[0., 0., -1.]]);
228+
let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]);
229+
let expected_L =
230+
DenseMatrix::from_2d_array(&[&[1., 0., 0.], &[0., 1., 0.], &[0.2, 0.8, 1.]]);
231+
let expected_U =
232+
DenseMatrix::from_2d_array(&[&[5., 6., 0.], &[0., 1., 5.], &[0., 0., -1.]]);
231233
let expected_pivot =
232-
DenseMatrix::from_array(&[&[0., 0., 1.], &[0., 1., 0.], &[1., 0., 0.]]);
234+
DenseMatrix::from_2d_array(&[&[0., 0., 1.], &[0., 1., 0.], &[1., 0., 0.]]);
233235
let lu = a.lu();
234236
assert!(lu.L().approximate_eq(&expected_L, 1e-4));
235237
assert!(lu.U().approximate_eq(&expected_U, 1e-4));
@@ -238,9 +240,9 @@ mod tests {
238240

239241
#[test]
240242
fn inverse() {
241-
let a = DenseMatrix::from_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]);
243+
let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]);
242244
let expected =
243-
DenseMatrix::from_array(&[&[-6.0, 3.6, 1.4], &[5.0, -3.0, -1.0], &[-1.0, 0.8, 0.2]]);
245+
DenseMatrix::from_2d_array(&[&[-6.0, 3.6, 1.4], &[5.0, -3.0, -1.0], &[-1.0, 0.8, 0.2]]);
244246
let a_inv = a.lu().inverse();
245247
println!("{}", a_inv);
246248
assert!(a_inv.approximate_eq(&expected, 1e-4));

0 commit comments

Comments
 (0)