diff --git a/rust/docs/matrix/add.md b/rust/docs/matrix/add.md index 533f445..f090af9 100644 --- a/rust/docs/matrix/add.md +++ b/rust/docs/matrix/add.md @@ -4,7 +4,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.add(&m2).unwrap(); diff --git a/rust/docs/matrix/add_assign.md b/rust/docs/matrix/add_assign.md index c18048b..80c4919 100644 --- a/rust/docs/matrix/add_assign.md +++ b/rust/docs/matrix/add_assign.md @@ -4,7 +4,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1 += m2; diff --git a/rust/docs/matrix/add_trait.md b/rust/docs/matrix/add_trait.md index 122bc24..1a60bfe 100644 --- a/rust/docs/matrix/add_trait.md +++ b/rust/docs/matrix/add_trait.md @@ -1,15 +1,15 @@ 同じサイズの行列同士の要素ごとの加算を行います。 -* サイズが異なる行列同士の加算はNoneを返します。 +* サイズが異なる行列同士の加算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); let m3 = m1 + m2; -assert_eq!(m3.unwrap().get(0, 0), Some(&6)); -assert_eq!(m3.unwrap().get(0, 1), Some(&8)); -assert_eq!(m3.unwrap().get(1, 0), Some(&10)); -assert_eq!(m3.unwrap().get(1, 1), Some(&12)); +assert_eq!(m3[(0, 0)], 6); +assert_eq!(m3[(0, 1)], 8); +assert_eq!(m3[(1, 0)], 10); +assert_eq!(m3[(1, 1)], 12); ``` diff --git a/rust/docs/matrix/add_with.md b/rust/docs/matrix/add_with.md index 2a35e17..24a26ed 100644 --- a/rust/docs/matrix/add_with.md +++ b/rust/docs/matrix/add_with.md @@ -1,10 +1,11 @@ 同じサイズの行列同士の要素ごとの加算を行います。 -* サイズが異なる行列同士の加算はエラーを返します。 +* サイズが異なる行列同士の加算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_algorithm::*; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.add_with::(&m2).unwrap(); diff --git a/rust/docs/matrix/col_iter.md b/rust/docs/matrix/col_iter.md index 0df5566..1e4a228 100644 --- a/rust/docs/matrix/col_iter.md +++ b/rust/docs/matrix/col_iter.md @@ -3,7 +3,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::with_size(2, 2); assert_eq!(m.col_iter(0).unwrap().collect::>(), vec![&0, &0]); ``` diff --git a/rust/docs/matrix/col_iter_mut.md b/rust/docs/matrix/col_iter_mut.md index 4446070..1cd8be5 100644 --- a/rust/docs/matrix/col_iter_mut.md +++ b/rust/docs/matrix/col_iter_mut.md @@ -3,7 +3,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m: Matrix = Matrix::with_size(2, 2); if let Some(col_iter) = m.col_iter_mut(0) { col_iter.for_each(|val| *val = 42); diff --git a/rust/docs/matrix/col_par_iter.md b/rust/docs/matrix/col_par_iter.md index 357c926..71ad14b 100644 --- a/rust/docs/matrix/col_par_iter.md +++ b/rust/docs/matrix/col_par_iter.md @@ -4,10 +4,11 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use rayon::prelude::*; let mut m: Matrix = Matrix::with_size(2, 2); if let Some(col_iter) = m.col_par_iter_mut(0) { col_iter.for_each(|val| *val = 42); } assert_eq!(m.col_iter(0).unwrap().collect::>(), vec![&42, &42]); -``` \ No newline at end of file +``` diff --git a/rust/docs/matrix/col_par_iter_mut.md b/rust/docs/matrix/col_par_iter_mut.md index a57d106..8e6f947 100644 --- a/rust/docs/matrix/col_par_iter_mut.md +++ b/rust/docs/matrix/col_par_iter_mut.md @@ -4,9 +4,10 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use rayon::prelude::*; let mut m: Matrix = Matrix::new([[1, 2], [3, 4]]); m.col_par_iter_mut(0).unwrap().for_each(|x| *x *= 2); assert_eq!(m[(0, 0)], 2); assert_eq!(m[(1, 0)], 6); -``` \ No newline at end of file +``` diff --git a/rust/docs/matrix/cols.md b/rust/docs/matrix/cols.md index ff2c20c..1086077 100644 --- a/rust/docs/matrix/cols.md +++ b/rust/docs/matrix/cols.md @@ -3,7 +3,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::with_size(3, 4); // [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] assert_eq!(m.cols(), 4); ``` diff --git a/rust/docs/matrix/div.md b/rust/docs/matrix/div.md index 8158768..6d6ae7b 100644 --- a/rust/docs/matrix/div.md +++ b/rust/docs/matrix/div.md @@ -1,11 +1,11 @@ 同じサイズの行列同士の要素ごとの除算を行います。 -* サイズが異なる行列同士の除算はエラーを返します。 -* 0で割る要素がある場合もエラーを返します。 +* サイズが異なる行列同士の除算はパニックを引き起こします。 +* 0で割る要素がある場合もパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.div(&m2).unwrap(); diff --git a/rust/docs/matrix/div_assign.md b/rust/docs/matrix/div_assign.md index 35f124b..a4de860 100644 --- a/rust/docs/matrix/div_assign.md +++ b/rust/docs/matrix/div_assign.md @@ -5,7 +5,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[10, 20], [30, 40]]); let m2: Matrix = Matrix::new([[1, 2], [3, 4]]); m1 /= m2; diff --git a/rust/docs/matrix/div_trait.md b/rust/docs/matrix/div_trait.md index 64f6287..099775e 100644 --- a/rust/docs/matrix/div_trait.md +++ b/rust/docs/matrix/div_trait.md @@ -1,15 +1,16 @@ 同じサイズの行列同士の要素ごとの除算を行います。 -* サイズが異なる行列同士の除算はNoneを返します。 -* 0で割る要素がある場合もNoneを返します。 +* サイズが異なる行列同士の除算はパニックを引き起こします。 +* 0で割る要素がある場合もパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +let m1: Matrix = Matrix::new([[10, 20], [30, 40]]); let m2: Matrix = Matrix::new([[1, 2], [3, 4]]); let m3 = m1 / m2; -assert_eq!(m3.unwrap().get(0, 0), Some(&10)); -assert_eq!(m3.unwrap().get(0, 1), Some(&10)); -assert_eq!(m3.unwrap().get(1, 0), Some(&10)); -assert_eq!(m3.unwrap().get(1, 1), Some(&10)); +assert_eq!(m3[(0, 0)], 10); +assert_eq!(m3[(0, 1)], 10); +assert_eq!(m3[(1, 0)], 10); +assert_eq!(m3[(1, 1)], 10); ``` diff --git a/rust/docs/matrix/div_with.md b/rust/docs/matrix/div_with.md index 65a936b..5522525 100644 --- a/rust/docs/matrix/div_with.md +++ b/rust/docs/matrix/div_with.md @@ -1,16 +1,17 @@ 同じサイズの行列同士の要素ごとの除算を行います。 -* サイズが異なる行列同士の除算はエラーを返します。 -* 0で割る要素がある場合もエラーを返します。 +* サイズが異なる行列同士の除算はパニックを引き起こします。 +* 0で割る要素がある場合もパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_algorithm::*; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.div_with::(&m2).unwrap(); -assert_eq!(m1.get(0, 0), Some(&0)); -assert_eq!(m1.get(0, 1), Some(&0)); -assert_eq!(m1.get(1, 0), Some(&0)); -assert_eq!(m1.get(1, 1), Some(&0)); +assert_eq!(m1[(0, 0)], 0); +assert_eq!(m1[(0, 1)], 0); +assert_eq!(m1[(1, 0)], 0); +assert_eq!(m1[(1, 1)], 0); ``` diff --git a/rust/docs/matrix/get.md b/rust/docs/matrix/get.md index 90061e3..6ca2d44 100644 --- a/rust/docs/matrix/get.md +++ b/rust/docs/matrix/get.md @@ -3,7 +3,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::with_size(2, 2); assert_eq!(m.get(0, 0), Some(&0)); ``` diff --git a/rust/docs/matrix/get_mut.md b/rust/docs/matrix/get_mut.md index 3b2134a..c93e64e 100644 --- a/rust/docs/matrix/get_mut.md +++ b/rust/docs/matrix/get_mut.md @@ -3,7 +3,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m: Matrix = Matrix::with_size(2, 2); if let Some(val) = m.get_mut(0, 0) { *val = 42; diff --git a/rust/docs/matrix/hadamard_mul.md b/rust/docs/matrix/hadamard_mul.md index 53b80e2..21d83b8 100644 --- a/rust/docs/matrix/hadamard_mul.md +++ b/rust/docs/matrix/hadamard_mul.md @@ -4,7 +4,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.hadamard_mul(&m2).unwrap(); diff --git a/rust/docs/matrix/hadamard_mul_with.md b/rust/docs/matrix/hadamard_mul_with.md index 58311ab..d0d644c 100644 --- a/rust/docs/matrix/hadamard_mul_with.md +++ b/rust/docs/matrix/hadamard_mul_with.md @@ -4,12 +4,13 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_algorithm::*; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.hadamard_mul_with::(&m2).unwrap(); -assert_eq!(m1.get(0, 0), Some(&5)); -assert_eq!(m1.get(0, 1), Some(&12)); -assert_eq!(m1.get(1, 0), Some(&21)); -assert_eq!(m1.get(1, 1), Some(&32)); +assert_eq!(m1[(0, 0)], 5); +assert_eq!(m1[(0, 1)], 12); +assert_eq!(m1[(1, 0)], 21); +assert_eq!(m1[(1, 1)], 32); ``` diff --git a/rust/docs/matrix/index.md b/rust/docs/matrix/index.md index 787ab31..a50e1d4 100644 --- a/rust/docs/matrix/index.md +++ b/rust/docs/matrix/index.md @@ -3,10 +3,10 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::new([[1, 2], [3, 4]]); assert_eq!(m[(0, 0)], 1); assert_eq!(m[(0, 1)], 2); assert_eq!(m[(1, 0)], 3); -assert_eq!(m[(1, 1)], 4]); +assert_eq!(m[(1, 1)], 4); ``` diff --git a/rust/docs/matrix/mul.md b/rust/docs/matrix/mul.md index 0fd6728..6a824ce 100644 --- a/rust/docs/matrix/mul.md +++ b/rust/docs/matrix/mul.md @@ -1,10 +1,10 @@ 行列積を行います。 -* 列数と行数が一致しない行列同士の乗算はエラーを返します。 +* 列数と行数が一致しない行列同士の乗算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.mul(&m2).unwrap(); diff --git a/rust/docs/matrix/mul_assign.md b/rust/docs/matrix/mul_assign.md index e970eb4..cb9ffde 100644 --- a/rust/docs/matrix/mul_assign.md +++ b/rust/docs/matrix/mul_assign.md @@ -4,7 +4,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1 *= m2; diff --git a/rust/docs/matrix/mul_trait.md b/rust/docs/matrix/mul_trait.md index a161fa6..4433682 100644 --- a/rust/docs/matrix/mul_trait.md +++ b/rust/docs/matrix/mul_trait.md @@ -1,15 +1,15 @@ 同じサイズの行列同士の要素ごとの乗算を行います。 -* サイズが異なる行列同士の乗算はNoneを返します。 +* サイズが異なる行列同士の乗算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); let m3 = m1 * m2; -assert_eq!(m3.unwrap().get(0, 0), Some(&19)); -assert_eq!(m3.unwrap().get(0, 1), Some(&22)); -assert_eq!(m3.unwrap().get(1, 0), Some(&43)); -assert_eq!(m3.unwrap().get(1, 1), Some(&50)); +assert_eq!(m3[(0, 0)], 19); +assert_eq!(m3[(0, 1)], 22); +assert_eq!(m3[(1, 0)], 43); +assert_eq!(m3[(1, 1)], 50); ``` diff --git a/rust/docs/matrix/mul_with.md b/rust/docs/matrix/mul_with.md index f50437b..da47c89 100644 --- a/rust/docs/matrix/mul_with.md +++ b/rust/docs/matrix/mul_with.md @@ -1,10 +1,11 @@ 行列積を行います。 -* 列数と行数が一致しない行列同士の乗算はエラーを返します。 +* 列数と行数が一致しない行列同士の乗算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_algorithm::*; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.mul_with::(&m2).unwrap(); diff --git a/rust/docs/matrix/row_iter.md b/rust/docs/matrix/row_iter.md index e64f6c0..58b394a 100644 --- a/rust/docs/matrix/row_iter.md +++ b/rust/docs/matrix/row_iter.md @@ -3,7 +3,10 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::with_size(2, 2); -assert_eq!(m.row_iter(0).unwrap().collect::>(), vec![&0, &0]); +assert_eq!( + m.row_iter(0).unwrap().collect::>(), + vec![&0, &0] +); ``` diff --git a/rust/docs/matrix/row_iter_mut.md b/rust/docs/matrix/row_iter_mut.md index 8f4b252..b1da493 100644 --- a/rust/docs/matrix/row_iter_mut.md +++ b/rust/docs/matrix/row_iter_mut.md @@ -3,10 +3,13 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; + let mut m: Matrix = Matrix::with_size(2, 2); -if let Some(row_iter) = m.row_mut_iter(0) { +if let Some(row_iter) = m.row_iter_mut(0) { row_iter.for_each(|val| *val = 42); } -assert_eq!(m.row_mut_iter(0).unwrap().collect::>(), vec![&42, &42]); + +assert_eq!(m[(0, 0)], 42); +assert_eq!(m[(0, 1)], 42); ``` diff --git a/rust/docs/matrix/row_par_iter.md b/rust/docs/matrix/row_par_iter.md index 2b0879f..ba256b9 100644 --- a/rust/docs/matrix/row_par_iter.md +++ b/rust/docs/matrix/row_par_iter.md @@ -4,8 +4,9 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use rayon::prelude::*; let m: Matrix = Matrix::new([[1, 2], [3, 4]]); let sum: i32 = m.row_par_iter(0).unwrap().cloned().sum(); assert_eq!(sum, 3); -``` \ No newline at end of file +``` diff --git a/rust/docs/matrix/row_par_iter_mut.md b/rust/docs/matrix/row_par_iter_mut.md index 00b53af..06c0aa3 100644 --- a/rust/docs/matrix/row_par_iter_mut.md +++ b/rust/docs/matrix/row_par_iter_mut.md @@ -4,8 +4,10 @@ # Examples ```rust -use matrix::Matrix; -let m: Matrix = Matrix::new([[1, 2], [3, 4]]); +use NeuralNetwork::matrix::Matrix; +use rayon::prelude::*; + +let mut m: Matrix = Matrix::new([[1, 2], [3, 4]]); m.row_par_iter_mut(0).unwrap().for_each(|x| *x *= 2); assert_eq!(m[(0, 0)], 2); -``` \ No newline at end of file +``` diff --git a/rust/docs/matrix/rows.md b/rust/docs/matrix/rows.md index 5041f53..ad3abdf 100644 --- a/rust/docs/matrix/rows.md +++ b/rust/docs/matrix/rows.md @@ -3,7 +3,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::with_size(3, 4); // [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] assert_eq!(m.rows(), 3); ``` diff --git a/rust/docs/matrix/sub.md b/rust/docs/matrix/sub.md index 64254a6..aad87cf 100644 --- a/rust/docs/matrix/sub.md +++ b/rust/docs/matrix/sub.md @@ -1,10 +1,10 @@ 同じサイズの行列同士の要素ごとの減算を行います。 -* サイズが異なる行列同士の減算はエラーを返します。 +* サイズが異なる行列同士の減算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.sub(&m2).unwrap(); diff --git a/rust/docs/matrix/sub_assign.md b/rust/docs/matrix/sub_assign.md index c671e33..ce5c0a9 100644 --- a/rust/docs/matrix/sub_assign.md +++ b/rust/docs/matrix/sub_assign.md @@ -4,7 +4,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1 -= m2; diff --git a/rust/docs/matrix/sub_trait.md b/rust/docs/matrix/sub_trait.md index 6693bf1..eb95ffe 100644 --- a/rust/docs/matrix/sub_trait.md +++ b/rust/docs/matrix/sub_trait.md @@ -1,15 +1,15 @@ 同じサイズの行列同士の要素ごとの減算を行います。 -* サイズが異なる行列同士の減算はNoneを返します。 +* サイズが異なる行列同士の減算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); let m3 = m1 - m2; -assert_eq!(m3.unwrap().get(0, 0), Some(&-4)); -assert_eq!(m3.unwrap().get(0, 1), Some(&-4)); -assert_eq!(m3.unwrap().get(1, 0), Some(&-4)); -assert_eq!(m3.unwrap().get(1, 1), Some(&-4)); +assert_eq!(m3[(0, 0)], -4); +assert_eq!(m3[(0, 1)], -4); +assert_eq!(m3[(1, 0)], -4); +assert_eq!(m3[(1, 1)], -4); ``` diff --git a/rust/docs/matrix/sub_with.md b/rust/docs/matrix/sub_with.md index 45c67e9..fa179d0 100644 --- a/rust/docs/matrix/sub_with.md +++ b/rust/docs/matrix/sub_with.md @@ -1,10 +1,11 @@ 同じサイズの行列同士の要素ごとの減算を行います。 -* サイズが異なる行列同士の減算はエラーを返します。 +* サイズが異なる行列同士の減算はパニックを引き起こします。 # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_algorithm::*; let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); m1.sub_with::(&m2).unwrap(); diff --git a/rust/docs/matrix/transpose.md b/rust/docs/matrix/transpose.md new file mode 100644 index 0000000..7dac94f --- /dev/null +++ b/rust/docs/matrix/transpose.md @@ -0,0 +1,11 @@ +転置を計算するメソッドです。 + +```rust +use NeuralNetwork::matrix::Matrix; +let m: Matrix = Matrix::new([[1, 2], [3, 4]]); +let transposed = m.transpose(); +assert_eq!(transposed[(0, 0)], 1); +assert_eq!(transposed[(0, 1)], 3); +assert_eq!(transposed[(1, 0)], 2); +assert_eq!(transposed[(1, 1)], 4); +``` diff --git a/rust/docs/matrix/transpose_par.md b/rust/docs/matrix/transpose_par.md new file mode 100644 index 0000000..8bd014a --- /dev/null +++ b/rust/docs/matrix/transpose_par.md @@ -0,0 +1,13 @@ +転置を並列で計算するメソッドです。 + +```rust +use NeuralNetwork::matrix::Matrix; +let m: Matrix = Matrix::new([[1, 2, 3], [4, 5, 6]]); +let transposed = m.transpose_par(); +assert_eq!(transposed[(0, 0)], 1); +assert_eq!(transposed[(0, 1)], 4); +assert_eq!(transposed[(1, 0)], 2); +assert_eq!(transposed[(1, 1)], 5); +assert_eq!(transposed[(2, 0)], 3); +assert_eq!(transposed[(2, 1)], 6); +``` diff --git a/rust/docs/matrix/with_size.md b/rust/docs/matrix/with_size.md index 607f6ec..db48a85 100644 --- a/rust/docs/matrix/with_size.md +++ b/rust/docs/matrix/with_size.md @@ -5,7 +5,7 @@ # Examples ```rust -use matrix::Matrix; +use NeuralNetwork::matrix::Matrix; let m: Matrix = Matrix::with_size(2, 2); assert_eq!(m.get(0, 0), Some(&0)); assert_eq!(m.get(0, 1), Some(&0)); diff --git a/rust/docs/matrix_layout/col_stride.md b/rust/docs/matrix_layout/col_stride.md new file mode 100644 index 0000000..0e71547 --- /dev/null +++ b/rust/docs/matrix_layout/col_stride.md @@ -0,0 +1,7 @@ +colから列のstrideを取得する +# 引数 +- `col`: 列番号 +- `matrix_row`: 行数 +- `matrix_col`: 列数 +# 戻り値 +- (開始位置, ステップ)のタプル diff --git a/rust/docs/matrix_layout/get_index.md b/rust/docs/matrix_layout/get_index.md new file mode 100644 index 0000000..e3c912a --- /dev/null +++ b/rust/docs/matrix_layout/get_index.md @@ -0,0 +1,10 @@ +row, colから一次元配列のindexを取得する + +# 引数 +- `row`: 行番号 +- `col`: 列番号 +- `matrix_row`: 行数 +- `matrix_col`: 列数 + +# 戻り値 +- 一次元配列のindex diff --git a/rust/docs/matrix_layout/get_un_major_iter.md b/rust/docs/matrix_layout/get_un_major_iter.md new file mode 100644 index 0000000..851083d --- /dev/null +++ b/rust/docs/matrix_layout/get_un_major_iter.md @@ -0,0 +1,8 @@ +メジャー方向とは逆の方向のイテレータを取得する +# 引数 +- `mtx`: 行列 +- `i`: 行番号または列番号 +# 戻り値 +メジャー方向とは逆の方向のイテレータ +RowMajorの場合、行方向がメジャーなので、列方向のイテレータを返す +ColumnMajorの場合、列方向がメジャーなので、行方向のイテレータを返す diff --git a/rust/docs/matrix_layout/major_dir_iter.md b/rust/docs/matrix_layout/major_dir_iter.md new file mode 100644 index 0000000..93a27c5 --- /dev/null +++ b/rust/docs/matrix_layout/major_dir_iter.md @@ -0,0 +1,10 @@ +メジャー方向のイテレータを取得する +# 引数 +- `mtx`: 行列のデータを格納するベクタ +- `matrix_row`: 行数 +- `matrix_col`: 列数 +# 戻り値 +メジャー方向のイテレータ +このイテレータは、行列のデータを格納するベクタを、メジャー方向のチャンクに分割して返す +RowMajorの場合、行方向がメジャーなので、行ごとにチャンクに分割して返す +ColumnMajorの場合、列方向がメジャーなので、列ごとにチャンクに分割して返す diff --git a/rust/docs/matrix_layout/major_dir_par_iter.md b/rust/docs/matrix_layout/major_dir_par_iter.md new file mode 100644 index 0000000..91bdf96 --- /dev/null +++ b/rust/docs/matrix_layout/major_dir_par_iter.md @@ -0,0 +1,10 @@ +メジャー方向のParallelイテレータを取得する +# 引数 +- `mtx`: 行列のデータを格納するベクタ +- `matrix_row`: 行数 +- `matrix_col`: 列数 +# 戻り値 +メジャー方向のイテレータ +このイテレータは、行列のデータを格納するベクタを、メジャー方向のチャンクに分割して返す +RowMajorの場合、行方向がメジャーなので、行ごとにチャンクに分割して返す +ColumnMajorの場合、列方向がメジャーなので、列ごとにチャンクに分割して返す diff --git a/rust/docs/matrix_layout/row_stride.md b/rust/docs/matrix_layout/row_stride.md new file mode 100644 index 0000000..240e3d0 --- /dev/null +++ b/rust/docs/matrix_layout/row_stride.md @@ -0,0 +1,9 @@ +row, colから行・列のstrideを取得する + +# 引数 +- `row`: 行番号 +- `matrix_row`: 行数 +- `matrix_col`: 列数 + +# 戻り値 +- (開始位置, ステップ)のタプル diff --git a/rust/src/main.rs b/rust/src/main.rs index 93c124a..fb82dcc 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,7 +1,13 @@ use NeuralNetwork::matrix; +use NeuralNetwork::matrix::Matrix; use NeuralNetwork::matrix_layout::{ColumnMajor, RowMajor}; +use rayon::iter::ParallelIterator; fn main() { - let mtx = matrix::Matrix::::with_size(3, 4); - println!("{}", mtx); + use NeuralNetwork::matrix::Matrix; + let m: Matrix = Matrix::new([[1, 2], [3, 4]]); + assert_eq!(m[(0, 0)], 1); + assert_eq!(m[(0, 1)], 2); + assert_eq!(m[(1, 0)], 3); + assert_eq!(m[(1, 1)], 4); } \ No newline at end of file diff --git a/rust/src/matrix.rs b/rust/src/matrix.rs index dd26b9f..1cddb95 100644 --- a/rust/src/matrix.rs +++ b/rust/src/matrix.rs @@ -197,6 +197,40 @@ impl Matrix { { Calc::div(self, other) } + + #[doc = include_str!("../docs/matrix/transpose.md")] + pub fn transpose(&self) -> Self + where T: MatrixElement + { + let (r_rows, r_cols) = (self.cols(), self.rows()); + let mut result = Self::with_size(r_rows, r_cols); + + L::major_dir_iter_mut(&mut result.data, r_rows, r_cols) + .enumerate() + .for_each(|(r_row, row)| { + row.iter_mut().zip(L::get_un_major_iter(&self, r_row)) + .for_each(|(a, b)| *a = *b); + }); + + result + } + + #[doc = include_str!("../docs/matrix/transpose_par.md")] + pub fn transpose_par(&self) -> Self + where T: MatrixElement + { + let (r_rows, r_cols) = (self.cols(), self.rows()); + let mut result = Self::with_size(r_rows, r_cols); + + L::major_dir_par_iter_mut(&mut result.data, r_rows, r_cols) + .enumerate() + .for_each(|(r_row, row)| { + row.par_iter_mut().zip(L::get_un_major_par_iter(&self, r_row)) + .for_each(|(a, b)| *a = *b); + }); + + result + } } #[doc = include_str!("../docs/matrix/impl_index.md")] @@ -213,14 +247,13 @@ impl std::ops::Add> for Matr where T: std::ops::AddAssign + MatrixElement { - type Output = Option>; + type Output = Matrix; #[doc = include_str!("../docs/matrix/add_trait.md")] fn add(mut self, other: Matrix) -> Self::Output { - if self.rows != other.rows || self.cols != other.cols { return None; } - - NaiveAlgorithm::add(&mut self, &other).ok()?; - Some(self) + assert!(self.rows == other.rows && self.cols == other.cols, "Matrix dimensions must match for addition"); + assert_eq!(NaiveAlgorithm::add(&mut self, &other), Ok(()), "Matrix addition failed"); + self } } impl std::ops::AddAssign> for Matrix @@ -229,23 +262,23 @@ where { #[doc = include_str!("../docs/matrix/add_assign.md")] fn add_assign(&mut self, other: Matrix){ - assert!(self.rows == other.rows && self.cols == other.cols); + assert!(self.rows == other.rows && self.cols == other.cols, "Matrix dimensions must match for addition"); - NaiveAlgorithm::add(self, &other).ok(); + assert_eq!(NaiveAlgorithm::add(self, &other), Ok(()), "Matrix addition failed"); } } impl std::ops::Sub> for Matrix where T: std::ops::SubAssign + MatrixElement { - type Output = Option>; + type Output = Matrix; #[doc = include_str!("../docs/matrix/sub_trait.md")] fn sub(mut self, other: Matrix) -> Self::Output { - if self.rows != other.rows || self.cols != other.cols { return None; } + assert!(self.rows == other.rows && self.cols == other.cols, "Matrix dimensions must match for subtraction"); + assert_eq!(NaiveAlgorithm::sub(&mut self, &other), Ok(()), "Matrix subtraction failed"); - NaiveAlgorithm::sub(&mut self, &other).ok()?; - Some(self) + self } } impl std::ops::SubAssign> for Matrix @@ -254,26 +287,24 @@ where { #[doc = include_str!("../docs/matrix/sub_assign.md")] fn sub_assign(&mut self, other: Matrix){ - assert!(self.rows == other.rows && self.cols == other.cols); - - NaiveAlgorithm::sub(self, &other).ok(); + assert!(self.rows == other.rows && self.cols == other.cols, "Matrix dimensions must match for subtraction"); + assert_eq!(NaiveAlgorithm::sub(self, &other), Ok(()), "Matrix subtraction failed"); } } impl std::ops::Mul> for Matrix where T: std::ops::MulAssign + MatrixElement + PartialEq + std::ops::Mul + std::ops::AddAssign + std::iter::Sum { - type Output = Option>; + type Output = Matrix; #[doc = include_str!("../docs/matrix/mul_trait.md")] fn mul(mut self, other: Matrix) -> Self::Output { - if self.rows != other.rows || self.cols != other.cols { return None; } - - NaiveAlgorithm::mtx_mul(&self, &other).map(|result| { + assert!(self.cols == other.rows, "Incompatible matrix dimensions for multiplication"); + assert_eq!(NaiveAlgorithm::mtx_mul(&self, &other).map(|result| { self = result; - }).ok()?; + }), Ok(()), "Matrix multiplication failed"); - Some(self) + self } } impl std::ops::MulAssign> for Matrix @@ -282,27 +313,26 @@ where { #[doc = include_str!("../docs/matrix/mul_assign.md")] fn mul_assign(&mut self, other: Matrix){ - assert!(self.rows == other.rows && self.cols == other.cols); + assert!(self.cols == other.rows, "Incompatible matrix dimensions for multiplication"); - NaiveAlgorithm::mtx_mul(&self, &other).ok().map(|result| { + assert_eq!(NaiveAlgorithm::mtx_mul(&self, &other).map(|result| { *self = result; - }); + }), Ok(()), "Matrix multiplication failed"); } } impl std::ops::Div> for Matrix where T: std::ops::DivAssign + MatrixElement + PartialEq { - type Output = Option>; + type Output = Matrix; #[doc = include_str!("../docs/matrix/div_trait.md")] fn div(mut self, other: Matrix) -> Self::Output { - if self.rows != other.rows || self.cols != other.cols { return None; } - if other.data.iter().any(|val| *val == T::default()) { return None; } + assert!(self.rows == other.rows && self.cols == other.cols, "Matrix dimensions must match for division"); + assert!(other.data.iter().all(|val| *val != T::default()), "Division by zero is not allowed"); + assert_eq!(NaiveAlgorithm::div(&mut self, &other), Ok(()), "Matrix division failed"); - NaiveAlgorithm::div(&mut self, &other).ok()?; - - Some(self) + self } } impl std::ops::DivAssign> for Matrix @@ -311,10 +341,9 @@ where { #[doc = include_str!("../docs/matrix/div_assign.md")] fn div_assign(&mut self, other: Matrix){ - assert!(self.rows == other.rows && self.cols == other.cols); - assert!(other.data.iter().all(|val| *val != T::default())); - - NaiveAlgorithm::div(self, &other).ok(); + assert!(self.rows == other.rows && self.cols == other.cols, "Matrix dimensions must match for division"); + assert!(other.data.iter().all(|val| *val != T::default()), "Division by zero is not allowed"); + assert_eq!(NaiveAlgorithm::div(self, &other), Ok(()), "Matrix division failed"); } } diff --git a/rust/src/matrix_element.rs b/rust/src/matrix_element.rs index 8023112..89a0617 100644 --- a/rust/src/matrix_element.rs +++ b/rust/src/matrix_element.rs @@ -1 +1,13 @@ -pub trait MatrixElement: Default + Copy + Sync + Send {} \ No newline at end of file +pub trait MatrixElement: Default + Copy + Sync + Send {} +impl MatrixElement for i8 {} +impl MatrixElement for i16 {} +impl MatrixElement for i32 {} +impl MatrixElement for i64 {} +impl MatrixElement for i128 {} +impl MatrixElement for u8 {} +impl MatrixElement for u16 {} +impl MatrixElement for u32 {} +impl MatrixElement for u64 {} +impl MatrixElement for u128 {} +impl MatrixElement for f32 {} +impl MatrixElement for f64 {} \ No newline at end of file diff --git a/rust/src/matrix_layout.rs b/rust/src/matrix_layout.rs index 90a264b..ded771f 100644 --- a/rust/src/matrix_layout.rs +++ b/rust/src/matrix_layout.rs @@ -1,31 +1,34 @@ +use rayon::prelude::*; +use crate::{matrix::Matrix, matrix_element::MatrixElement}; + pub trait MatrixLayout: Sync + Send { - /// row, colから一次元配列のindexを取得する - /// # 引数 - /// - `row`: 行番号 - /// - `col`: 列番号 - /// - `matrix_row`: 行数 - /// - `matrix_col`: 列数 - /// # 戻り値 - /// - 一次元配列のindex + #[doc = include_str!("../docs/matrix_layout/get_index.md")] fn get_index(row: usize, col: usize, matrix_row: usize, matrix_col: usize) -> Option; - /// row, colから行・列のstrideを取得する - /// # 引数 - /// - `row`: 行番号 - /// - `matrix_row`: 行数 - /// - `matrix_col`: 列数 - /// # 戻り値 - /// - (開始位置, ステップ)のタプル + #[doc = include_str!("../docs/matrix_layout/row_stride.md")] fn row_stride(row: usize, matrix_row: usize, matrix_col: usize) -> Option<(usize, usize)>; - /// colから列のstrideを取得する - /// # 引数 - /// - `col`: 列番号 - /// - `matrix_row`: 行数 - /// - `matrix_col`: 列数 - /// # 戻り値 - /// - (開始位置, ステップ)のタプル + #[doc = include_str!("../docs/matrix_layout/col_stride.md")] fn col_stride(col: usize, matrix_row: usize, matrix_col: usize) -> Option<(usize, usize)>; + + #[doc = include_str!("../docs/matrix_layout/major_dir_iter.md")] + fn major_dir_iter(mtx: &Vec, matrix_row: usize, matrix_col: usize) -> impl Iterator; + + #[doc = include_str!("../docs/matrix_layout/major_dir_iter.md")] + fn major_dir_iter_mut(mtx: &mut Vec, matrix_row: usize, matrix_col: usize) -> impl Iterator; + #[doc = include_str!("../docs/matrix_layout/major_dir_par_iter.md")] + fn major_dir_par_iter(mtx: &Vec, matrix_row: usize, matrix_col: usize) -> impl IndexedParallelIterator where T: Sync; + #[doc = include_str!("../docs/matrix_layout/major_dir_par_iter.md")] + fn major_dir_par_iter_mut(mtx: &mut Vec, matrix_row: usize, matrix_col: usize) -> impl IndexedParallelIterator where T: Send; + + #[doc = include_str!("../docs/matrix_layout/get_un_major_iter.md")] + fn get_un_major_iter(mtx: &Matrix, i: usize) -> impl Iterator; + #[doc = include_str!("../docs/matrix_layout/get_un_major_iter.md")] + fn get_un_major_iter_mut(mtx: &mut Matrix, i: usize) -> impl Iterator; + #[doc = include_str!("../docs/matrix_layout/get_un_major_iter.md")] + fn get_un_major_par_iter(mtx: &Matrix, i: usize) -> impl IndexedParallelIterator where T: Sync; + #[doc = include_str!("../docs/matrix_layout/get_un_major_iter.md")] + fn get_un_major_par_iter_mut(mtx: &mut Matrix, i: usize) -> impl IndexedParallelIterator where T: Send + Sync; } pub struct RowMajor; @@ -51,6 +54,31 @@ impl MatrixLayout for RowMajor { Some((col, m_col)) } + + fn major_dir_iter(mtx: &Vec, _matrix_row: usize, matrix_col: usize) -> impl Iterator { + mtx.chunks(matrix_col) + } + fn major_dir_iter_mut(mtx: &mut Vec, _matrix_row: usize, matrix_col: usize) -> impl Iterator { + mtx.chunks_mut(matrix_col) + } + fn major_dir_par_iter(mtx: &Vec, _matrix_row: usize, matrix_col: usize) -> impl IndexedParallelIterator where T: Sync { + mtx.par_chunks(matrix_col) + } + fn major_dir_par_iter_mut(mtx: &mut Vec, _matrix_row: usize, matrix_col: usize) -> impl IndexedParallelIterator where T: Send { + mtx.par_chunks_mut(matrix_col) + } + fn get_un_major_iter(mtx: &Matrix, i: usize) -> impl Iterator { + mtx.col_iter(i).unwrap() + } + fn get_un_major_iter_mut(mtx: &mut Matrix, i: usize) -> impl Iterator { + mtx.col_iter_mut(i).unwrap() + } + fn get_un_major_par_iter(mtx: &Matrix, i: usize) -> impl IndexedParallelIterator where T: Sync { + mtx.col_par_iter(i).unwrap() + } + fn get_un_major_par_iter_mut(mtx: &mut Matrix, i: usize) -> impl IndexedParallelIterator where T: Send + Sync{ + mtx.col_par_iter_mut(i).unwrap() + } } pub struct ColumnMajor; impl MatrixLayout for ColumnMajor { @@ -72,4 +100,28 @@ impl MatrixLayout for ColumnMajor { } Some((col * m_row, 1)) } + fn major_dir_iter(mtx: &Vec, matrix_row: usize, _matrix_col: usize) -> impl Iterator { + mtx.chunks(matrix_row) + } + fn major_dir_iter_mut(mtx: &mut Vec, matrix_row: usize, _matrix_col: usize) -> impl Iterator { + mtx.chunks_mut(matrix_row) + } + fn major_dir_par_iter(mtx: &Vec, matrix_row: usize, _matrix_col: usize) -> impl IndexedParallelIterator where T: Sync { + mtx.par_chunks(matrix_row) + } + fn major_dir_par_iter_mut(mtx: &mut Vec, matrix_row: usize, _matrix_col: usize) -> impl IndexedParallelIterator where T: Send { + mtx.par_chunks_mut(matrix_row) + } + fn get_un_major_iter(mtx: &Matrix, i: usize) -> impl Iterator { + mtx.row_iter(i).unwrap() + } + fn get_un_major_iter_mut(mtx: &mut Matrix, i: usize) -> impl Iterator { + mtx.row_iter_mut(i).unwrap() + } + fn get_un_major_par_iter(mtx: &Matrix, i: usize) -> impl IndexedParallelIterator where T: Sync { + mtx.row_par_iter(i).unwrap() + } + fn get_un_major_par_iter_mut(mtx: &mut Matrix, i: usize) -> impl IndexedParallelIterator where T: Send + Sync { + mtx.row_par_iter_mut(i).unwrap() + } } \ No newline at end of file