Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions rust/docs/matrix/invert_layout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
行列のレイアウトを反転させた新しい行列を返す

# Example
```rust
use NeuralNetwork::matrix::Matrix;
use NeuralNetwork::matrix_layout::{RowMajor, ColumnMajor};
let m: Matrix<i32, RowMajor> = 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);
```
16 changes: 16 additions & 0 deletions rust/docs/matrix/invert_layout_par.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
行列のレイアウトを反転させた新しい行列を返す

# Example
```rust
use NeuralNetwork::matrix::Matrix;
use NeuralNetwork::matrix_layout::{RowMajor, ColumnMajor};
let m: Matrix<i32, RowMajor> = 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);
```
17 changes: 17 additions & 0 deletions rust/docs/matrix/with_data.md
Original file line number Diff line number Diff line change
@@ -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::<i32>::with_data(data, 2, 3);
assert_eq!(matrix.get(0, 0), Some(&1));
```
23 changes: 23 additions & 0 deletions rust/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ impl<T, L: MatrixLayout> Matrix<T, L> {
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<T>, 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 }

Expand Down Expand Up @@ -231,6 +237,23 @@ impl<T, L: MatrixLayout> Matrix<T, L> {

result
}

#[doc = include_str!("../docs/matrix/invert_layout.md")]
pub fn invert_layout(&self) -> Matrix<T, L::InverseLayout>
where
T: MatrixElement,
L::InverseLayout: MatrixLayout
{
Matrix::<T, L::InverseLayout>::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<T, L::InverseLayout>
where
T: MatrixElement,
L::InverseLayout: MatrixLayout
{
Matrix::<T, L::InverseLayout>::with_data(self.transpose_par().data, self.rows(), self.cols())
}
}

#[doc = include_str!("../docs/matrix/impl_index.md")]
Expand Down
6 changes: 6 additions & 0 deletions rust/src/matrix_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub trait MatrixLayout: Sync + Send {
fn get_un_major_par_iter<T, L: MatrixLayout>(mtx: &Matrix<T, L>, i: usize) -> impl IndexedParallelIterator<Item = &T> where T: Sync;
#[doc = include_str!("../docs/matrix_layout/get_un_major_iter.md")]
fn get_un_major_par_iter_mut<T, L: MatrixLayout>(mtx: &mut Matrix<T, L>, i: usize) -> impl IndexedParallelIterator<Item = &mut T> where T: Send + Sync;

type InverseLayout;
}

pub struct RowMajor;
Expand Down Expand Up @@ -79,6 +81,8 @@ impl MatrixLayout for RowMajor {
fn get_un_major_par_iter_mut<T, L: MatrixLayout>(mtx: &mut Matrix<T, L>, i: usize) -> impl IndexedParallelIterator<Item = &mut T> where T: Send + Sync{
mtx.col_par_iter_mut(i).unwrap()
}

type InverseLayout = ColumnMajor;
}
pub struct ColumnMajor;
impl MatrixLayout for ColumnMajor {
Expand Down Expand Up @@ -124,4 +128,6 @@ impl MatrixLayout for ColumnMajor {
fn get_un_major_par_iter_mut<T, L: MatrixLayout>(mtx: &mut Matrix<T, L>, i: usize) -> impl IndexedParallelIterator<Item = &mut T> where T: Send + Sync {
mtx.row_par_iter_mut(i).unwrap()
}

type InverseLayout = RowMajor;
}
Loading