-
Notifications
You must be signed in to change notification settings - Fork 0
MatrixArray構造体を実装し、行列の生成と要素取得メソッドを追加 #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9c243d7
MatrixArray構造体を実装し、行列の生成と要素取得メソッドを追加
SanaeProject 84dcd4a
MatrixArray構造体のメイン関数を更新し、行列の生成と表示機能を追加
SanaeProject f3cafda
MatrixArray構造体のメソッドにエラーハンドリングを追加し、行列の要素取得時に範囲外アクセスを防止
SanaeProject 0f9a7ec
MatrixLayoutトレイトを実装し、行列のインデックス取得とストライド計算のメソッドを追加
SanaeProject e5d3720
.gitignoreファイルを追加し、targetディレクトリを無視するように設定。不要なターゲットファイルを削除。
SanaeProject 2463702
.gitignoreファイルのtargetディレクトリ設定を修正
SanaeProject 562ebff
MatrixLayoutトレイトのコメントから不要な引数説明を削除
SanaeProject 7256351
MatrixArrayモジュールを削除し、Matrixモジュールを追加して行列の実装を統一
SanaeProject f85669b
MatrixLayoutトレイトのメソッドをOption型に変更し、行列のインデックス取得とストライド計算でのエラーハンドリングを追加
SanaeProject File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pub mod matrix_layout; | ||
| pub mod matrix; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| use NeuralNetwork::matrix; | ||
| use NeuralNetwork::matrix_layout::{ColumnMajor, RowMajor}; | ||
|
SanaeProject marked this conversation as resolved.
SanaeProject marked this conversation as resolved.
|
||
|
|
||
| fn main() { | ||
| println!("Hello, world!"); | ||
| } | ||
| let mtx = matrix::Matrix::<i32, RowMajor>::with_size(3, 4); | ||
| println!("{}", mtx); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| use core::fmt; | ||
| use crate::matrix_layout::{ RowMajor, MatrixLayout}; | ||
|
|
||
| pub struct Matrix<T, L: MatrixLayout = RowMajor> { | ||
| data: Vec<T>, | ||
| row: usize, | ||
| col: usize, | ||
| _marker: std::marker::PhantomData<L>, | ||
| } | ||
|
|
||
| impl<T, L: MatrixLayout> Matrix<T, L> { | ||
| pub fn with_size(row: usize, col: usize) -> Self | ||
| where | ||
| T: Default + Clone, | ||
| { | ||
| Self { | ||
| data: vec![T::default(); row * col], | ||
|
SanaeProject marked this conversation as resolved.
SanaeProject marked this conversation as resolved.
SanaeProject marked this conversation as resolved.
SanaeProject marked this conversation as resolved.
|
||
| row, | ||
| col, | ||
| _marker: std::marker::PhantomData, | ||
| } | ||
| } | ||
|
|
||
| pub fn get(&self, row: usize, col: usize) -> Option<&T> { | ||
| if row >= self.row || col >= self.col { return None; } | ||
|
|
||
| let idx = L::get_index(row, col, self.row, self.col)?; | ||
| self.data.get(idx) | ||
|
SanaeProject marked this conversation as resolved.
|
||
| } | ||
|
Comment on lines
+24
to
+29
|
||
|
|
||
| pub fn get_row(&self, row: usize) -> Option<impl Iterator<Item = &T>> { | ||
| let (start, step) = L::row_stride(row, self.row, self.col)?; | ||
| Some(self.data.iter().skip(start).step_by(step).take(self.col)) | ||
| } | ||
|
|
||
| pub fn get_column(&self, col: usize) -> Option<impl Iterator<Item = &T>> { | ||
| let (start, step) = L::col_stride(col, self.row, self.col)?; | ||
| Some(self.data.iter().skip(start).step_by(step).take(self.row)) | ||
| } | ||
| } | ||
|
|
||
| impl<T: std::fmt::Display, L: MatrixLayout> std::fmt::Display for Matrix<T, L> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| (0..self.row).for_each(|r| { | ||
| if let Some(row_iter) = self.get_row(r) { | ||
| row_iter.for_each(|c| { | ||
| _ = write!(f, "{}\t", c); | ||
| }); | ||
| } | ||
| _ = write!(f, "\n"); | ||
| }); | ||
|
SanaeProject marked this conversation as resolved.
SanaeProject marked this conversation as resolved.
SanaeProject marked this conversation as resolved.
|
||
|
|
||
| Ok(()) | ||
|
SanaeProject marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| pub trait MatrixLayout { | ||
| /// row, colから一次元配列のindexを取得する | ||
| /// # 引数 | ||
| /// - `row`: 行番号 | ||
| /// - `col`: 列番号 | ||
| /// - `matrix_row`: 行数 | ||
| /// - `matrix_col`: 列数 | ||
| /// # 戻り値 | ||
| /// - 一次元配列のindex | ||
| fn get_index(row: usize, col: usize, matrix_row: usize, matrix_col: usize) -> Option<usize>; | ||
|
|
||
| /// row, colから行・列のstrideを取得する | ||
| /// # 引数 | ||
| /// - `row`: 行番号 | ||
| /// - `matrix_row`: 行数 | ||
| /// - `matrix_col`: 列数 | ||
| /// # 戻り値 | ||
| /// - (開始位置, ステップ)のタプル | ||
| fn row_stride(row: usize, matrix_row: usize, matrix_col: usize) -> Option<(usize, usize)>; | ||
|
|
||
| /// row, colから行・列のstrideを取得する | ||
| /// # 引数 | ||
| /// - `col`: 列番号 | ||
| /// - `matrix_row`: 行数 | ||
| /// - `matrix_col`: 列数 | ||
| /// # 戻り値 | ||
| /// - (開始位置, ステップ)のタプル | ||
| fn col_stride(col: usize, matrix_row: usize, matrix_col: usize) -> Option<(usize, usize)>; | ||
| } | ||
|
|
||
| pub struct RowMajor; | ||
| impl MatrixLayout for RowMajor { | ||
| fn get_index(row: usize, col: usize, _m_row: usize, m_col: usize) -> Option<usize> { | ||
| if _m_row == 0 || m_col == 0 || row >= _m_row || col >= m_col { | ||
| return None; | ||
| } | ||
|
|
||
| Some(row * m_col + col) | ||
| } | ||
| fn row_stride(row: usize, _m_row: usize, m_col: usize) -> Option<(usize, usize)> { | ||
| if _m_row == 0 || m_col == 0 || row >= _m_row { | ||
| return None; | ||
| } | ||
|
|
||
| Some((row * m_col, 1)) | ||
| } | ||
| fn col_stride(col: usize, _m_row: usize, m_col: usize) -> Option<(usize, usize)> { | ||
| if _m_row == 0 || m_col == 0 || col >= m_col { | ||
| return None; | ||
| } | ||
|
|
||
| Some((col, m_col)) | ||
| } | ||
| } | ||
| pub struct ColumnMajor; | ||
| impl MatrixLayout for ColumnMajor { | ||
| fn get_index(row: usize, col: usize, m_row: usize, _m_col: usize) -> Option<usize> { | ||
| if m_row == 0 || _m_col == 0 || row >= m_row || col >= _m_col { | ||
| return None; | ||
| } | ||
| Some(col * m_row + row) | ||
| } | ||
| fn row_stride(row: usize, m_row: usize, _m_col: usize) -> Option<(usize, usize)> { | ||
| if m_row == 0 || _m_col == 0 || row >= m_row { | ||
| return None; | ||
| } | ||
| Some((row, m_row)) | ||
| } | ||
| fn col_stride(col: usize, m_row: usize, _m_col: usize) -> Option<(usize, usize)> { | ||
| if m_row == 0 || _m_col == 0 || col >= _m_col { | ||
| return None; | ||
| } | ||
| Some((col * m_row, 1)) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.