From 2aef609d073f947ec125508af7b0a36268fac6d4 Mon Sep 17 00:00:00 2001 From: OtonariS Date: Sun, 7 Jun 2026 18:34:34 +0900 Subject: [PATCH 1/2] =?UTF-8?q?with=5Fdata=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=99?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/docs/matrix/with_data.md | 17 +++++++++++++++++ rust/src/matrix.rs | 6 ++++++ 2 files changed, 23 insertions(+) create mode 100644 rust/docs/matrix/with_data.md diff --git a/rust/docs/matrix/with_data.md b/rust/docs/matrix/with_data.md new file mode 100644 index 0000000..870f6ae --- /dev/null +++ b/rust/docs/matrix/with_data.md @@ -0,0 +1,17 @@ +データを指定して行列を作成します。データの長さは行列のサイズと一致する必要があります。 + +# Panics +データの長さが行列のサイズと一致しない場合にパニックが発生します。 + +# Arguments +* `data` - 行列の要素を格納するベクタ +* `row` - 行列の行数 +* `col` - 行列の列数 + +# Example +```rust +use NeuralNetwork::matrix::Matrix; +let data = vec![1, 2, 3, 4, 5, 6]; +let matrix = Matrix::::with_data(data, 2, 3); +assert_eq!(matrix.get(0, 0), Some(&1)); +``` diff --git a/rust/src/matrix.rs b/rust/src/matrix.rs index 1cddb95..a1134b7 100644 --- a/rust/src/matrix.rs +++ b/rust/src/matrix.rs @@ -38,6 +38,12 @@ impl Matrix { Self { data: vec![T::default(); row * col], rows: row, cols: col, _marker: std::marker::PhantomData } } + #[doc = include_str!("../docs/matrix/with_data.md")] + pub fn with_data(data: Vec, row: usize, col: usize) -> Self { + assert!(data.len() == row * col, "Data length must match matrix dimensions"); + Self { data, rows: row, cols: col, _marker: std::marker::PhantomData } + } + #[doc = include_str!("../docs/matrix/rows.md")] pub fn rows(&self) -> usize { self.rows } From 10ad3023676f5b2b6af4495426a8d7a1b6705abd Mon Sep 17 00:00:00 2001 From: OtonariS Date: Sun, 7 Jun 2026 20:01:19 +0900 Subject: [PATCH 2/2] =?UTF-8?q?with=5Fdata=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE=E3=81=99?= =?UTF-8?q?=E3=80=82=20=E3=83=AC=E3=82=A4=E3=82=A2=E3=82=A6=E3=83=88?= =?UTF-8?q?=E3=82=92=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=BE?= =?UTF-8?q?=E3=81=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/docs/matrix/invert_layout.md | 16 ++++++++++++++++ rust/docs/matrix/invert_layout_par.md | 16 ++++++++++++++++ rust/src/matrix.rs | 17 +++++++++++++++++ rust/src/matrix_layout.rs | 6 ++++++ 4 files changed, 55 insertions(+) create mode 100644 rust/docs/matrix/invert_layout.md create mode 100644 rust/docs/matrix/invert_layout_par.md diff --git a/rust/docs/matrix/invert_layout.md b/rust/docs/matrix/invert_layout.md new file mode 100644 index 0000000..6b318d6 --- /dev/null +++ b/rust/docs/matrix/invert_layout.md @@ -0,0 +1,16 @@ +行列のレイアウトを反転させた新しい行列を返す + +# Example +```rust +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_layout::{RowMajor, ColumnMajor}; +let m: Matrix = Matrix::new([[1, 2], [3, 4]]); +let inverted = m.invert_layout(); + +// 反転後の行列はColumnMajorレイアウトになる。 +// アクセス時のインデックスは同じだが、内部的なデータの配置が異なる。 +assert_eq!(inverted[(0, 0)], 1); +assert_eq!(inverted[(0, 1)], 2); +assert_eq!(inverted[(1, 0)], 3); +assert_eq!(inverted[(1, 1)], 4); +``` diff --git a/rust/docs/matrix/invert_layout_par.md b/rust/docs/matrix/invert_layout_par.md new file mode 100644 index 0000000..d4650ca --- /dev/null +++ b/rust/docs/matrix/invert_layout_par.md @@ -0,0 +1,16 @@ +行列のレイアウトを反転させた新しい行列を返す + +# Example +```rust +use NeuralNetwork::matrix::Matrix; +use NeuralNetwork::matrix_layout::{RowMajor, ColumnMajor}; +let m: Matrix = Matrix::new([[1, 2], [3, 4]]); +let inverted = m.invert_layout_par(); + +// 反転後の行列はColumnMajorレイアウトになる。 +// アクセス時のインデックスは同じだが、内部的なデータの配置が異なる。 +assert_eq!(inverted[(0, 0)], 1); +assert_eq!(inverted[(0, 1)], 2); +assert_eq!(inverted[(1, 0)], 3); +assert_eq!(inverted[(1, 1)], 4); +``` diff --git a/rust/src/matrix.rs b/rust/src/matrix.rs index a1134b7..8c7dbb2 100644 --- a/rust/src/matrix.rs +++ b/rust/src/matrix.rs @@ -237,6 +237,23 @@ impl Matrix { result } + + #[doc = include_str!("../docs/matrix/invert_layout.md")] + pub fn invert_layout(&self) -> Matrix + where + T: MatrixElement, + L::InverseLayout: MatrixLayout + { + Matrix::::with_data(self.transpose().data, self.rows(), self.cols()) + } + #[doc = include_str!("../docs/matrix/invert_layout_par.md")] + pub fn invert_layout_par(&self) -> Matrix + where + T: MatrixElement, + L::InverseLayout: MatrixLayout + { + Matrix::::with_data(self.transpose_par().data, self.rows(), self.cols()) + } } #[doc = include_str!("../docs/matrix/impl_index.md")] diff --git a/rust/src/matrix_layout.rs b/rust/src/matrix_layout.rs index ded771f..19a1627 100644 --- a/rust/src/matrix_layout.rs +++ b/rust/src/matrix_layout.rs @@ -29,6 +29,8 @@ pub trait MatrixLayout: Sync + Send { 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; + + type InverseLayout; } pub struct RowMajor; @@ -79,6 +81,8 @@ impl MatrixLayout for RowMajor { 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() } + + type InverseLayout = ColumnMajor; } pub struct ColumnMajor; impl MatrixLayout for ColumnMajor { @@ -124,4 +128,6 @@ impl MatrixLayout for ColumnMajor { 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() } + + type InverseLayout = RowMajor; } \ No newline at end of file