From ed1f8ce5ce6fb87308126a3e57cfc13f3290e89f Mon Sep 17 00:00:00 2001 From: OtonariS Date: Wed, 3 Jun 2026 23:22:46 +0900 Subject: [PATCH 1/4] =?UTF-8?q?matrix=5Falgorithm=E3=83=88=E3=83=AC?= =?UTF-8?q?=E3=82=A4=E3=83=88=E3=81=A8NaiveAlgorithm=E6=A7=8B=E9=80=A0?= =?UTF-8?q?=E4=BD=93=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97=E3=80=81=E8=A1=8C?= =?UTF-8?q?=E5=88=97=E3=81=AE=E5=8A=A0=E7=AE=97=E3=80=81=E6=B8=9B=E7=AE=97?= =?UTF-8?q?=E3=80=81Hadamard=E7=A9=8D=E3=80=81=E9=99=A4=E7=AE=97=E3=80=81?= =?UTF-8?q?=E8=A1=8C=E5=88=97=E4=B9=97=E7=AE=97=E3=81=AE=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/lib.rs | 1 + rust/src/matrix.rs | 155 +++++++++++++++++++++-------------- rust/src/matrix_algorithm.rs | 87 ++++++++++++++++++++ 3 files changed, 183 insertions(+), 60 deletions(-) create mode 100644 rust/src/matrix_algorithm.rs diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 3489657..1c13d99 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,2 +1,3 @@ pub mod matrix_layout; +pub mod matrix_algorithm; pub mod matrix; \ No newline at end of file diff --git a/rust/src/matrix.rs b/rust/src/matrix.rs index d84bd64..e0a96fd 100644 --- a/rust/src/matrix.rs +++ b/rust/src/matrix.rs @@ -1,6 +1,6 @@ use core::fmt; -use std::marker::PhantomData; -use crate::matrix_layout::{ RowMajor, MatrixLayout }; +use std::{marker::PhantomData, ops::Mul}; +use crate::{matrix_algorithm::{MatrixAlgorithm, NaiveAlgorithm}, matrix_layout::{ MatrixLayout, RowMajor }}; pub struct Matrix { data : Vec, @@ -158,6 +158,71 @@ impl Matrix { let (start, step) = L::col_stride(col, self.rows, self.cols)?; Some(self.data.iter_mut().skip(start).step_by(step).take(self.rows)) } + + pub fn add(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::AddAssign + Copy + { + NaiveAlgorithm::add(self, other) + } + pub fn add_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::AddAssign + Copy + { + Calc::add(self, other) + } + + pub fn sub(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::SubAssign + Copy + { + NaiveAlgorithm::sub(self, other) + } + pub fn sub_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::SubAssign + Copy + { + Calc::sub(self, other) + } + + pub fn mul(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::Mul + std::ops::AddAssign + Default + Copy + { + NaiveAlgorithm::mtx_mul(self, other).map(|result| *self = result) + } + pub fn mul_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::Mul + std::ops::AddAssign + Default + Copy + { + Calc::mtx_mul(self, other).map(|result| *self = result) + } + + pub fn hadamard_mul(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::MulAssign + Copy + { + NaiveAlgorithm::hadamard_mul(self, other) + } + pub fn hadamard_mul_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::MulAssign + Copy + { + Calc::hadamard_mul(self, other) + } + + pub fn div(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::DivAssign + Copy + { + NaiveAlgorithm::div(self, other) + } + pub fn div_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> + where + T: std::ops::DivAssign + Copy + { + Calc::div(self, other) + } } /// タプルインデックスで要素にアクセスできるようにします。 @@ -181,7 +246,7 @@ impl std::ops::Index<(usize, usize)> for Matrix { impl std::ops::Add> for Matrix where - T: std::ops::AddAssign + Clone + T: std::ops::AddAssign + Copy { type Output = Option>; @@ -201,18 +266,13 @@ where fn add(mut self, other: Matrix) -> Self::Output { if self.rows != other.rows || self.cols != other.cols { return None; } - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a += b.clone(); - }); - } - + NaiveAlgorithm::add(&mut self, &other).ok()?; Some(self) } } impl std::ops::AddAssign> for Matrix where - T: std::ops::AddAssign + Clone + T: std::ops::AddAssign + Copy { /// 同じサイズの行列同士の要素ごとの加算を行います。 /// * サイズが異なる行列同士の加算はパニックを引き起こします。 @@ -230,16 +290,12 @@ where fn add_assign(&mut self, other: Matrix){ assert!(self.rows == other.rows && self.cols == other.cols); - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a += b.clone(); - }); - } + NaiveAlgorithm::add(self, &other).ok(); } } impl std::ops::Sub> for Matrix where - T: std::ops::SubAssign + Clone + T: std::ops::SubAssign + Copy { type Output = Option>; @@ -259,18 +315,13 @@ where fn sub(mut self, other: Matrix) -> Self::Output { if self.rows != other.rows || self.cols != other.cols { return None; } - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a -= b.clone(); - }); - } - + NaiveAlgorithm::sub(&mut self, &other).ok()?; Some(self) } } impl std::ops::SubAssign> for Matrix where - T: std::ops::SubAssign + Clone + T: std::ops::SubAssign + Copy { /// 同じサイズの行列同士の要素ごとの減算を行います。 /// * サイズが異なる行列同士の減算はパニックを引き起こします。 @@ -288,16 +339,12 @@ where fn sub_assign(&mut self, other: Matrix){ assert!(self.rows == other.rows && self.cols == other.cols); - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a -= b.clone(); - }); - } + NaiveAlgorithm::sub(self, &other).ok(); } } impl std::ops::Mul> for Matrix where - T: std::ops::MulAssign + Clone + T: std::ops::MulAssign + Copy + PartialEq + Default + std::ops::Mul + std::ops::AddAssign { type Output = Option>; @@ -309,26 +356,24 @@ where /// 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(&5)); - /// assert_eq!(m3.unwrap().get(0, 1), Some(&12)); - /// assert_eq!(m3.unwrap().get(1, 0), Some(&21)); - /// assert_eq!(m3.unwrap().get(1, 1), Some(&32)); + /// 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)); /// ``` fn mul(mut self, other: Matrix) -> Self::Output { if self.rows != other.rows || self.cols != other.cols { return None; } - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a *= b.clone(); - }); - } + NaiveAlgorithm::mtx_mul(&self, &other).map(|result| { + self = result; + }).ok()?; Some(self) } } impl std::ops::MulAssign> for Matrix where - T: std::ops::MulAssign + Clone + T: std::ops::MulAssign + Copy + PartialEq + Default + std::ops::Mul + std::ops::AddAssign { /// 同じサイズの行列同士の要素ごとの乗算を行います。 /// * サイズが異なる行列同士の乗算はパニックを引き起こします。 @@ -338,24 +383,22 @@ where /// let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); /// let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); /// m1 *= m2; - /// 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.get(0, 0), Some(&19)); + /// assert_eq!(m1.get(0, 1), Some(&22)); + /// assert_eq!(m1.get(1, 0), Some(&43)); + /// assert_eq!(m1.get(1, 1), Some(&50)); /// ``` fn mul_assign(&mut self, other: Matrix){ assert!(self.rows == other.rows && self.cols == other.cols); - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a *= b.clone(); - }); - } + NaiveAlgorithm::mtx_mul(&self, &other).ok().map(|result| { + *self = result; + }); } } impl std::ops::Div> for Matrix where - T: std::ops::DivAssign + Clone + PartialEq + Default + T: std::ops::DivAssign + Copy + PartialEq + Default { type Output = Option>; @@ -376,18 +419,14 @@ where if self.rows != other.rows || self.cols != other.cols { return None; } if other.data.iter().any(|val| *val == T::default()) { return None; } - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a /= b.clone(); - }); - } + NaiveAlgorithm::div(&mut self, &other).ok()?; Some(self) } } impl std::ops::DivAssign> for Matrix where - T: std::ops::DivAssign + Clone + PartialEq + Default + T: std::ops::DivAssign + Copy + PartialEq + Default { /// 同じサイズの行列同士の要素ごとの除算を行います。 /// * サイズが異なる行列同士の除算はパニックを引き起こします。 @@ -407,11 +446,7 @@ where assert!(self.rows == other.rows && self.cols == other.cols); assert!(other.data.iter().all(|val| *val != T::default())); - for row in 0..self.rows { - self.get_row_mut(row).unwrap().zip(other.get_row(row).unwrap()).for_each(|(a, b)| { - *a /= b.clone(); - }); - } + NaiveAlgorithm::div(self, &other).ok(); } } diff --git a/rust/src/matrix_algorithm.rs b/rust/src/matrix_algorithm.rs new file mode 100644 index 0000000..e943278 --- /dev/null +++ b/rust/src/matrix_algorithm.rs @@ -0,0 +1,87 @@ +use crate::matrix_layout::MatrixLayout; +use crate::matrix::Matrix; + +pub trait MatrixAlgorithm { + fn add(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> where T: std::ops::AddAssign + Copy; + fn sub(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> where T: std::ops::SubAssign + Copy; + fn hadamard_mul(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> where T: std::ops::MulAssign + Copy; + fn div(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> where T: std::ops::DivAssign + Copy; + fn mtx_mul(mtx: &Matrix, other: &Matrix) -> Result, String> where T: std::ops::Mul + std::ops::AddAssign + Default + Copy; +} + +pub struct NaiveAlgorithm; +impl MatrixAlgorithm for NaiveAlgorithm { + fn add(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> + where T: std::ops::AddAssign + Copy + { + if mtx.rows() != other.rows() || mtx.cols() != other.cols() { + return Err("行列のサイズが一致しません".to_string()); + } + + for i in 0..mtx.rows() { + mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a += *b); + } + Ok(()) + } + + fn sub(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> + where T: std::ops::SubAssign + Copy + { + if mtx.rows() != other.rows() || mtx.cols() != other.cols() { + return Err("行列のサイズが一致しません".to_string()); + } + + for i in 0..mtx.rows() { + mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a -= *b); + } + Ok(()) + } + + fn hadamard_mul(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> + where T: std::ops::MulAssign + Copy + { + if mtx.rows() != other.rows() || mtx.cols() != other.cols() { + return Err("行列のサイズが一致しません".to_string()); + } + + for i in 0..mtx.rows() { + mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a *= *b); + } + Ok(()) + } + + fn div(mtx: &mut Matrix, other: &Matrix) -> Result<(), String> + where T: std::ops::DivAssign + Copy + { + if mtx.rows() != other.rows() || mtx.cols() != other.cols() { + return Err("行列のサイズが一致しません".to_string()); + } + + for i in 0..mtx.rows() { + mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a /= *b); + } + Ok(()) + } + + fn mtx_mul(mtx: &Matrix, other: &Matrix) -> Result, String> + where T: Default + Copy + std::ops::Mul + std::ops::AddAssign + { + if mtx.cols() != other.rows() { + return Err("行列のサイズが一致しません".to_string()); + } + + let mut result = Matrix::with_size(mtx.rows(), other.cols()); + for i in 0..mtx.rows() { + for j in 0..other.cols() { + let mut temp = T::default(); + + mtx.get_row(i).unwrap().zip(other.get_column(j).unwrap()).for_each(|(a, b)| { + temp += *a * *b; + }); + + *result.get_mut(i, j).unwrap() = temp; + } + } + Ok(result) + } +} \ No newline at end of file From f8173c058cd19f954839c47c9ffb71ea53be436b Mon Sep 17 00:00:00 2001 From: OtonariS Date: Thu, 4 Jun 2026 14:04:07 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=82=92=E5=A4=89?= =?UTF-8?q?=E6=9B=B4=E3=81=97=E3=81=BE=E3=81=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/src/matrix.rs | 247 ++++++----------------------------- rust/src/matrix_algorithm.rs | 10 +- 2 files changed, 45 insertions(+), 212 deletions(-) diff --git a/rust/src/matrix.rs b/rust/src/matrix.rs index e0a96fd..3f17c59 100644 --- a/rust/src/matrix.rs +++ b/rust/src/matrix.rs @@ -10,15 +10,7 @@ pub struct Matrix { } impl Matrix { - /// 二次元配列から行列を作成します。 - /// * TはDefaultとCopyトレイトを実装している必要があります。 - /// # Examples - /// use NeuralNetwork::matrix::Matrix; - /// assert_eq!(m.get(0, 0), Some(&1)); - /// assert_eq!(m.get(0, 1), Some(&2)); - /// assert_eq!(m.get(1, 0), Some(&3)); - /// assert_eq!(m.get(1, 1), Some(&4)); - /// ``` + #[doc = include_str!("../docs/matrix/new.md")] pub fn new(data: [[T; COLS]; ROWS]) -> Self where T: Default + Copy @@ -37,18 +29,7 @@ impl Matrix { Self { data: vec, rows: ROWS, cols: COLS, _marker: PhantomData } } - /// サイズを指定して行列を作成します。 - /// * 要素は全てTのデフォルト値で初期化されます。 - /// * TはDefaultとCopyトレイトを実装している必要があります。 - /// # Examples - /// ``` - /// use 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)); - /// assert_eq!(m.get(1, 0), Some(&0)); - /// assert_eq!(m.get(1, 1), Some(&0)); - /// ``` + #[doc = include_str!("../docs/matrix/with_size.md")] pub fn with_size(row: usize, col: usize) -> Self where T: Default + Copy @@ -56,31 +37,13 @@ impl Matrix { Self { data: vec![T::default(); row * col], rows: row, cols: col, _marker: std::marker::PhantomData } } - /// 行数を返します。 - /// # Examples - /// ``` - /// use 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); - /// ``` + #[doc = include_str!("../docs/matrix/rows.md")] pub fn rows(&self) -> usize { self.rows } - /// 列数を返します。 - /// # Examples - /// ``` - /// use 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); - /// ``` + #[doc = include_str!("../docs/matrix/cols.md")] pub fn cols(&self) -> usize { self.cols } - /// 指定した位置の要素への参照を返します。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let m: Matrix = Matrix::with_size(2, 2); - /// assert_eq!(m.get(0, 0), Some(&0)); - /// ``` + #[doc = include_str!("../docs/matrix/get.md")] pub fn get(&self, row: usize, col: usize) -> Option<&T> { if row >= self.rows || col >= self.cols { return None; } @@ -88,16 +51,7 @@ impl Matrix { self.data.get(idx) } - /// 指定した位置の要素への可変参照を返します。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m: Matrix = Matrix::with_size(2, 2); - /// if let Some(val) = m.get_mut(0, 0) { - /// *val = 42; - /// } - /// assert_eq!(m.get(0, 0), Some(&42)); - /// ``` + #[doc = include_str!("../docs/matrix/get_mut.md")] pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> { if row >= self.rows || col >= self.cols { return None; } @@ -105,66 +59,39 @@ impl Matrix { self.data.get_mut(idx) } - /// 指定した行の要素への参照を返します。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let m: Matrix = Matrix::with_size(2, 2); - /// assert_eq!(m.get_row(0).unwrap().collect::>(), vec![&0, &0]); - /// ``` - pub fn get_row(&self, row: usize) -> Option> { + #[doc = include_str!("../docs/matrix/row_iter.md")] + pub fn row_iter(&self, row: usize) -> Option> { let (start, step) = L::row_stride(row, self.rows, self.cols)?; Some(self.data.iter().skip(start).step_by(step).take(self.cols)) } - /// 指定した行の要素への可変参照を返します。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m: Matrix = Matrix::with_size(2, 2); - /// if let Some(row_iter) = m.get_row_mut(0) { - /// row_iter.for_each(|val| *val = 42); - /// } - /// assert_eq!(m.get_row(0).unwrap().collect::>(), vec![&42, &42]); - /// ``` - pub fn get_row_mut(&mut self, row: usize) -> Option> { + #[doc = include_str!("../docs/matrix/row_mut_iter.md")] + pub fn row_mut_iter(&mut self, row: usize) -> Option> { let (start, step) = L::row_stride(row, self.rows, self.cols)?; Some(self.data.iter_mut().skip(start).step_by(step).take(self.cols)) } - /// 指定した列の要素への参照を返します。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let m: Matrix = Matrix::with_size(2, 2); - /// assert_eq!(m.get_column(0).unwrap().collect::>(), vec![&0, &0]); - /// ``` - pub fn get_column(&self, col: usize) -> Option> { + #[doc = include_str!("../docs/matrix/col_iter.md")] + pub fn col_iter(&self, col: usize) -> Option> { let (start, step) = L::col_stride(col, self.rows, self.cols)?; Some(self.data.iter().skip(start).step_by(step).take(self.rows)) } - /// 指定した列の要素への可変参照を返します。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m: Matrix = Matrix::with_size(2, 2); - /// if let Some(col_iter) = m.get_column_mut(0) { - /// col_iter.for_each(|val| *val = 42); - /// } - /// assert_eq!(m.get_column(0).unwrap().collect::>(), vec![&42, &42]); - /// ``` - pub fn get_column_mut(&mut self, col: usize) -> Option> { + #[doc = include_str!("../docs/matrix/col_mut_iter.md")] + pub fn col_mut_iter(&mut self, col: usize) -> Option> { let (start, step) = L::col_stride(col, self.rows, self.cols)?; Some(self.data.iter_mut().skip(start).step_by(step).take(self.rows)) } + #[doc = include_str!("../docs/matrix/add.md")] pub fn add(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::AddAssign + Copy { NaiveAlgorithm::add(self, other) } + + #[doc = include_str!("../docs/matrix/add_with.md")] pub fn add_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::AddAssign + Copy @@ -172,12 +99,15 @@ impl Matrix { Calc::add(self, other) } + #[doc = include_str!("../docs/matrix/sub.md")] pub fn sub(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::SubAssign + Copy { NaiveAlgorithm::sub(self, other) } + + #[doc = include_str!("../docs/matrix/sub_with.md")] pub fn sub_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::SubAssign + Copy @@ -185,12 +115,15 @@ impl Matrix { Calc::sub(self, other) } + #[doc = include_str!("../docs/matrix/mul.md")] pub fn mul(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::Mul + std::ops::AddAssign + Default + Copy { NaiveAlgorithm::mtx_mul(self, other).map(|result| *self = result) } + + #[doc = include_str!("../docs/matrix/mul_with.md")] pub fn mul_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::Mul + std::ops::AddAssign + Default + Copy @@ -198,12 +131,15 @@ impl Matrix { Calc::mtx_mul(self, other).map(|result| *self = result) } + #[doc = include_str!("../docs/matrix/hadamard_mul.md")] pub fn hadamard_mul(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::MulAssign + Copy { NaiveAlgorithm::hadamard_mul(self, other) } + + #[doc = include_str!("../docs/matrix/hadamard_mul_with.md")] pub fn hadamard_mul_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::MulAssign + Copy @@ -211,12 +147,15 @@ impl Matrix { Calc::hadamard_mul(self, other) } + #[doc = include_str!("../docs/matrix/div.md")] pub fn div(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::DivAssign + Copy { NaiveAlgorithm::div(self, other) } + + #[doc = include_str!("../docs/matrix/div_with.md")] pub fn div_with, OL: MatrixLayout>(&mut self, other: &Matrix) -> Result<(), String> where T: std::ops::DivAssign + Copy @@ -225,20 +164,11 @@ impl Matrix { } } -/// タプルインデックスで要素にアクセスできるようにします。 +#[doc = include_str!("../docs/matrix/impl_index.md")] impl std::ops::Index<(usize, usize)> for Matrix { type Output = T; - /// タプルインデックスで要素にアクセスします。 - /// # Examples - /// ``` - /// use 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]); - /// ``` + #[doc = include_str!("../docs/matrix/index.md")] fn index(&self, (row, col): (usize, usize)) -> &Self::Output{ self.get(row, col).expect("Index out of bounds") } @@ -250,19 +180,7 @@ where { type Output = Option>; - /// 同じサイズの行列同士の要素ごとの加算を行います。 - /// * サイズが異なる行列同士の加算はNoneを返します。 - /// # Examples - /// ``` - /// use 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)); - /// ``` + #[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; } @@ -274,19 +192,7 @@ impl std::ops::AddAssign> fo where T: std::ops::AddAssign + Copy { - /// 同じサイズの行列同士の要素ごとの加算を行います。 - /// * サイズが異なる行列同士の加算はパニックを引き起こします。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); - /// let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); - /// m1 += m2; - /// assert_eq!(m1.get(0, 0), Some(&6)); - /// assert_eq!(m1.get(0, 1), Some(&8)); - /// assert_eq!(m1.get(1, 0), Some(&10)); - /// assert_eq!(m1.get(1, 1), Some(&12)); - /// ``` + #[doc = include_str!("../docs/matrix/add_assign.md")] fn add_assign(&mut self, other: Matrix){ assert!(self.rows == other.rows && self.cols == other.cols); @@ -299,19 +205,7 @@ where { type Output = Option>; - /// 同じサイズの行列同士の要素ごとの減算を行います。 - /// * サイズが異なる行列同士の減算はNoneを返します。 - /// # Examples - /// ``` - /// use 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)); - /// ``` + #[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; } @@ -323,19 +217,7 @@ impl std::ops::SubAssign> fo where T: std::ops::SubAssign + Copy { - /// 同じサイズの行列同士の要素ごとの減算を行います。 - /// * サイズが異なる行列同士の減算はパニックを引き起こします。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); - /// let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); - /// m1 -= m2; - /// assert_eq!(m1.get(0, 0), Some(&-4)); - /// assert_eq!(m1.get(0, 1), Some(&-4)); - /// assert_eq!(m1.get(1, 0), Some(&-4)); - /// assert_eq!(m1.get(1, 1), Some(&-4)); - /// ``` + #[doc = include_str!("../docs/matrix/sub_assign.md")] fn sub_assign(&mut self, other: Matrix){ assert!(self.rows == other.rows && self.cols == other.cols); @@ -348,19 +230,7 @@ where { type Output = Option>; - /// 同じサイズの行列同士の要素ごとの乗算を行います。 - /// * サイズが異なる行列同士の乗算はNoneを返します。 - /// # Examples - /// ``` - /// use 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)); - /// ``` + #[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; } @@ -375,19 +245,7 @@ impl std::ops::MulAssign> fo where T: std::ops::MulAssign + Copy + PartialEq + Default + std::ops::Mul + std::ops::AddAssign { - /// 同じサイズの行列同士の要素ごとの乗算を行います。 - /// * サイズが異なる行列同士の乗算はパニックを引き起こします。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); - /// let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); - /// m1 *= m2; - /// assert_eq!(m1.get(0, 0), Some(&19)); - /// assert_eq!(m1.get(0, 1), Some(&22)); - /// assert_eq!(m1.get(1, 0), Some(&43)); - /// assert_eq!(m1.get(1, 1), Some(&50)); - /// ``` + #[doc = include_str!("../docs/matrix/mul_assign.md")] fn mul_assign(&mut self, other: Matrix){ assert!(self.rows == other.rows && self.cols == other.cols); @@ -402,19 +260,7 @@ where { type Output = Option>; - /// 同じサイズの行列同士の要素ごとの除算を行います。 - /// * サイズが異なる行列同士の除算はNoneを返します。 - /// * 0で割る要素がある場合もNoneを返します。 - /// # Examples - /// ``` - /// use NeuralNetwork::matrix::Matrix; - /// 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)); - /// ``` + #[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; } @@ -428,20 +274,7 @@ impl std::ops::DivAssign> fo where T: std::ops::DivAssign + Copy + PartialEq + Default { - /// 同じサイズの行列同士の要素ごとの除算を行います。 - /// * サイズが異なる行列同士の除算はパニックを引き起こします。 - /// * 0で割る要素がある場合もパニックを引き起こします。 - /// # Examples - /// ``` - /// use matrix::Matrix; - /// let mut m1: Matrix = Matrix::new([[10, 20], [30, 40]]); - /// let m2: Matrix = Matrix::new([[1, 2], [3, 4]]); - /// m1 /= m2; - /// assert_eq!(m1.get(0, 0), Some(&10)); - /// assert_eq!(m1.get(0, 1), Some(&10)); - /// assert_eq!(m1.get(1, 0), Some(&10)); - /// assert_eq!(m1.get(1, 1), Some(&10)); - /// ``` + #[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())); @@ -453,7 +286,7 @@ where impl std::fmt::Display for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (0..self.rows).for_each(|r| { - if let Some(row_iter) = self.get_row(r) { + if let Some(row_iter) = self.row_iter(r) { row_iter.for_each(|c| { _ = write!(f, "{}\t", c); }); diff --git a/rust/src/matrix_algorithm.rs b/rust/src/matrix_algorithm.rs index e943278..7698b65 100644 --- a/rust/src/matrix_algorithm.rs +++ b/rust/src/matrix_algorithm.rs @@ -19,7 +19,7 @@ impl MatrixAlgorithm for NaiveAl } for i in 0..mtx.rows() { - mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a += *b); + mtx.row_mut_iter(i).unwrap().zip(other.row_iter(i).unwrap()).for_each(|(a, b)| *a += *b); } Ok(()) } @@ -32,7 +32,7 @@ impl MatrixAlgorithm for NaiveAl } for i in 0..mtx.rows() { - mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a -= *b); + mtx.row_mut_iter(i).unwrap().zip(other.row_iter(i).unwrap()).for_each(|(a, b)| *a -= *b); } Ok(()) } @@ -45,7 +45,7 @@ impl MatrixAlgorithm for NaiveAl } for i in 0..mtx.rows() { - mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a *= *b); + mtx.row_mut_iter(i).unwrap().zip(other.row_iter(i).unwrap()).for_each(|(a, b)| *a *= *b); } Ok(()) } @@ -58,7 +58,7 @@ impl MatrixAlgorithm for NaiveAl } for i in 0..mtx.rows() { - mtx.get_row_mut(i).unwrap().zip(other.get_row(i).unwrap()).for_each(|(a, b)| *a /= *b); + mtx.row_mut_iter(i).unwrap().zip(other.row_iter(i).unwrap()).for_each(|(a, b)| *a /= *b); } Ok(()) } @@ -75,7 +75,7 @@ impl MatrixAlgorithm for NaiveAl for j in 0..other.cols() { let mut temp = T::default(); - mtx.get_row(i).unwrap().zip(other.get_column(j).unwrap()).for_each(|(a, b)| { + mtx.row_iter(i).unwrap().zip(other.col_iter(j).unwrap()).for_each(|(a, b)| { temp += *a * *b; }); From 0f29793c9806e22377ecede7ec4d6e7c0f6160eb Mon Sep 17 00:00:00 2001 From: OtonariS Date: Thu, 4 Jun 2026 14:04:20 +0900 Subject: [PATCH 3/4] =?UTF-8?q?rustdoc=E3=82=92=E5=88=86=E9=9B=A2=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/docs/matrix/add.md | 13 +++++++++++++ rust/docs/matrix/add_assign.md | 13 +++++++++++++ rust/docs/matrix/add_trait.md | 13 +++++++++++++ rust/docs/matrix/add_with.md | 13 +++++++++++++ rust/docs/matrix/col_iter.md | 7 +++++++ rust/docs/matrix/col_mut_iter.md | 10 ++++++++++ rust/docs/matrix/cols.md | 7 +++++++ rust/docs/matrix/div.md | 14 ++++++++++++++ rust/docs/matrix/div_assign.md | 14 ++++++++++++++ rust/docs/matrix/div_trait.md | 13 +++++++++++++ rust/docs/matrix/div_with.md | 14 ++++++++++++++ rust/docs/matrix/get.md | 7 +++++++ rust/docs/matrix/get_mut.md | 10 ++++++++++ rust/docs/matrix/hadamard_mul.md | 13 +++++++++++++ rust/docs/matrix/hadamard_mul_with.md | 13 +++++++++++++ rust/docs/matrix/impl_index.md | 1 + rust/docs/matrix/index.md | 10 ++++++++++ rust/docs/matrix/mul.md | 13 +++++++++++++ rust/docs/matrix/mul_assign.md | 13 +++++++++++++ rust/docs/matrix/mul_trait.md | 13 +++++++++++++ rust/docs/matrix/mul_with.md | 13 +++++++++++++ rust/docs/matrix/new.md | 9 +++++++++ rust/docs/matrix/row_iter.md | 7 +++++++ rust/docs/matrix/row_mut_iter.md | 10 ++++++++++ rust/docs/matrix/rows.md | 7 +++++++ rust/docs/matrix/sub.md | 13 +++++++++++++ rust/docs/matrix/sub_assign.md | 13 +++++++++++++ rust/docs/matrix/sub_trait.md | 13 +++++++++++++ rust/docs/matrix/sub_with.md | 13 +++++++++++++ rust/docs/matrix/with_size.md | 12 ++++++++++++ 30 files changed, 334 insertions(+) create mode 100644 rust/docs/matrix/add.md create mode 100644 rust/docs/matrix/add_assign.md create mode 100644 rust/docs/matrix/add_trait.md create mode 100644 rust/docs/matrix/add_with.md create mode 100644 rust/docs/matrix/col_iter.md create mode 100644 rust/docs/matrix/col_mut_iter.md create mode 100644 rust/docs/matrix/cols.md create mode 100644 rust/docs/matrix/div.md create mode 100644 rust/docs/matrix/div_assign.md create mode 100644 rust/docs/matrix/div_trait.md create mode 100644 rust/docs/matrix/div_with.md create mode 100644 rust/docs/matrix/get.md create mode 100644 rust/docs/matrix/get_mut.md create mode 100644 rust/docs/matrix/hadamard_mul.md create mode 100644 rust/docs/matrix/hadamard_mul_with.md create mode 100644 rust/docs/matrix/impl_index.md create mode 100644 rust/docs/matrix/index.md create mode 100644 rust/docs/matrix/mul.md create mode 100644 rust/docs/matrix/mul_assign.md create mode 100644 rust/docs/matrix/mul_trait.md create mode 100644 rust/docs/matrix/mul_with.md create mode 100644 rust/docs/matrix/new.md create mode 100644 rust/docs/matrix/row_iter.md create mode 100644 rust/docs/matrix/row_mut_iter.md create mode 100644 rust/docs/matrix/rows.md create mode 100644 rust/docs/matrix/sub.md create mode 100644 rust/docs/matrix/sub_assign.md create mode 100644 rust/docs/matrix/sub_trait.md create mode 100644 rust/docs/matrix/sub_with.md create mode 100644 rust/docs/matrix/with_size.md diff --git a/rust/docs/matrix/add.md b/rust/docs/matrix/add.md new file mode 100644 index 0000000..689661a --- /dev/null +++ b/rust/docs/matrix/add.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの加算を行います。 +* サイズが異なる行列同士の加算はエラーを返します。 +# Examples +``` +use 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(); +assert_eq!(m1.get(0, 0), Some(&6)); +assert_eq!(m1.get(0, 1), Some(&8)); +assert_eq!(m1.get(1, 0), Some(&10)); +assert_eq!(m1.get(1, 1), Some(&12)); +``` diff --git a/rust/docs/matrix/add_assign.md b/rust/docs/matrix/add_assign.md new file mode 100644 index 0000000..9495337 --- /dev/null +++ b/rust/docs/matrix/add_assign.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの加算を行います。 +* サイズが異なる行列同士の加算はパニックを引き起こします。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); +let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); +m1 += m2; +assert_eq!(m1.get(0, 0), Some(&6)); +assert_eq!(m1.get(0, 1), Some(&8)); +assert_eq!(m1.get(1, 0), Some(&10)); +assert_eq!(m1.get(1, 1), Some(&12)); +``` diff --git a/rust/docs/matrix/add_trait.md b/rust/docs/matrix/add_trait.md new file mode 100644 index 0000000..29937e2 --- /dev/null +++ b/rust/docs/matrix/add_trait.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの加算を行います。 +* サイズが異なる行列同士の加算はNoneを返します。 +# Examples +``` +use 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)); +``` diff --git a/rust/docs/matrix/add_with.md b/rust/docs/matrix/add_with.md new file mode 100644 index 0000000..2962f80 --- /dev/null +++ b/rust/docs/matrix/add_with.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの加算を行います。 +* サイズが異なる行列同士の加算はエラーを返します。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); +let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); +m1.add_with::(&m2).unwrap(); +assert_eq!(m1.get(0, 0), Some(&6)); +assert_eq!(m1.get(0, 1), Some(&8)); +assert_eq!(m1.get(1, 0), Some(&10)); +assert_eq!(m1.get(1, 1), Some(&12)); +``` diff --git a/rust/docs/matrix/col_iter.md b/rust/docs/matrix/col_iter.md new file mode 100644 index 0000000..025c591 --- /dev/null +++ b/rust/docs/matrix/col_iter.md @@ -0,0 +1,7 @@ +指定した列の要素への参照を返します。 +# Examples +``` +use 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_mut_iter.md b/rust/docs/matrix/col_mut_iter.md new file mode 100644 index 0000000..3bffebd --- /dev/null +++ b/rust/docs/matrix/col_mut_iter.md @@ -0,0 +1,10 @@ +指定した列の要素への可変参照を返します。 +# Examples +``` +use matrix::Matrix; +let mut m: Matrix = Matrix::with_size(2, 2); +if let Some(col_iter) = m.col_mut_iter(0) { + col_iter.for_each(|val| *val = 42); +} +assert_eq!(m.col_iter(0).unwrap().collect::>(), vec![&42, &42]); +``` diff --git a/rust/docs/matrix/cols.md b/rust/docs/matrix/cols.md new file mode 100644 index 0000000..791bf2e --- /dev/null +++ b/rust/docs/matrix/cols.md @@ -0,0 +1,7 @@ +列数を返します。 +# Examples +``` +use 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 new file mode 100644 index 0000000..99c4f68 --- /dev/null +++ b/rust/docs/matrix/div.md @@ -0,0 +1,14 @@ +同じサイズの行列同士の要素ごとの除算を行います。 +* サイズが異なる行列同士の除算はエラーを返します。 +* 0で割る要素がある場合もエラーを返します。 +# Examples +``` +use 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(); +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)); +``` diff --git a/rust/docs/matrix/div_assign.md b/rust/docs/matrix/div_assign.md new file mode 100644 index 0000000..4990ab1 --- /dev/null +++ b/rust/docs/matrix/div_assign.md @@ -0,0 +1,14 @@ +同じサイズの行列同士の要素ごとの除算を行います。 +* サイズが異なる行列同士の除算はパニックを引き起こします。 +* 0で割る要素がある場合もパニックを引き起こします。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[10, 20], [30, 40]]); +let m2: Matrix = Matrix::new([[1, 2], [3, 4]]); +m1 /= m2; +assert_eq!(m1.get(0, 0), Some(&10)); +assert_eq!(m1.get(0, 1), Some(&10)); +assert_eq!(m1.get(1, 0), Some(&10)); +assert_eq!(m1.get(1, 1), Some(&10)); +``` diff --git a/rust/docs/matrix/div_trait.md b/rust/docs/matrix/div_trait.md new file mode 100644 index 0000000..5cb124f --- /dev/null +++ b/rust/docs/matrix/div_trait.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの除算を行います。 +* サイズが異なる行列同士の除算はNoneを返します。 +* 0で割る要素がある場合もNoneを返します。 +# Examples +``` +use NeuralNetwork::matrix::Matrix; +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)); +``` diff --git a/rust/docs/matrix/div_with.md b/rust/docs/matrix/div_with.md new file mode 100644 index 0000000..df0e115 --- /dev/null +++ b/rust/docs/matrix/div_with.md @@ -0,0 +1,14 @@ +/ 同じサイズの行列同士の要素ごとの除算を行います。 +* サイズが異なる行列同士の除算はエラーを返します。 +* 0で割る要素がある場合もエラーを返します。 +# Examples +``` +use matrix::Matrix; +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)); +``` diff --git a/rust/docs/matrix/get.md b/rust/docs/matrix/get.md new file mode 100644 index 0000000..012747b --- /dev/null +++ b/rust/docs/matrix/get.md @@ -0,0 +1,7 @@ +指定した位置の要素への参照を返します。 +# Examples +``` +use 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 new file mode 100644 index 0000000..34f36da --- /dev/null +++ b/rust/docs/matrix/get_mut.md @@ -0,0 +1,10 @@ +指定した位置の要素への可変参照を返します。 +# Examples +``` +use matrix::Matrix; +let mut m: Matrix = Matrix::with_size(2, 2); +if let Some(val) = m.get_mut(0, 0) { + *val = 42; +} +assert_eq!(m.get(0, 0), Some(&42)); +``` diff --git a/rust/docs/matrix/hadamard_mul.md b/rust/docs/matrix/hadamard_mul.md new file mode 100644 index 0000000..2ef9f65 --- /dev/null +++ b/rust/docs/matrix/hadamard_mul.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの乗算を行います。 +* サイズが異なる行列同士の乗算はエラーを返します。 +# Examples +``` +use 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(); +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)); +``` diff --git a/rust/docs/matrix/hadamard_mul_with.md b/rust/docs/matrix/hadamard_mul_with.md new file mode 100644 index 0000000..751a6c8 --- /dev/null +++ b/rust/docs/matrix/hadamard_mul_with.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの乗算を行います。 +* サイズが異なる行列同士の乗算はエラーを返します。 +# Examples +``` +use matrix::Matrix; +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)); +``` diff --git a/rust/docs/matrix/impl_index.md b/rust/docs/matrix/impl_index.md new file mode 100644 index 0000000..dc31c82 --- /dev/null +++ b/rust/docs/matrix/impl_index.md @@ -0,0 +1 @@ +タプルインデックスで要素にアクセスできるようにします。 diff --git a/rust/docs/matrix/index.md b/rust/docs/matrix/index.md new file mode 100644 index 0000000..d54f408 --- /dev/null +++ b/rust/docs/matrix/index.md @@ -0,0 +1,10 @@ +タプルインデックスで要素にアクセスします。 +# Examples +``` +use 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]); +``` diff --git a/rust/docs/matrix/mul.md b/rust/docs/matrix/mul.md new file mode 100644 index 0000000..07c2854 --- /dev/null +++ b/rust/docs/matrix/mul.md @@ -0,0 +1,13 @@ +行列積を行います。 +* 列数と行数が一致しない行列同士の乗算はエラーを返します。 +# Examples +``` +use 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(); +assert_eq!(m1.get(0, 0), Some(&19)); +assert_eq!(m1.get(0, 1), Some(&22)); +assert_eq!(m1.get(1, 0), Some(&43)); +assert_eq!(m1.get(1, 1), Some(&50)); +``` diff --git a/rust/docs/matrix/mul_assign.md b/rust/docs/matrix/mul_assign.md new file mode 100644 index 0000000..c645d5f --- /dev/null +++ b/rust/docs/matrix/mul_assign.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの乗算を行います。 +* サイズが異なる行列同士の乗算はパニックを引き起こします。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); +let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); +m1 *= m2; +assert_eq!(m1.get(0, 0), Some(&19)); +assert_eq!(m1.get(0, 1), Some(&22)); +assert_eq!(m1.get(1, 0), Some(&43)); +assert_eq!(m1.get(1, 1), Some(&50)); +``` diff --git a/rust/docs/matrix/mul_trait.md b/rust/docs/matrix/mul_trait.md new file mode 100644 index 0000000..3e11e85 --- /dev/null +++ b/rust/docs/matrix/mul_trait.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの乗算を行います。 +* サイズが異なる行列同士の乗算はNoneを返します。 +# Examples +``` +use 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)); +``` diff --git a/rust/docs/matrix/mul_with.md b/rust/docs/matrix/mul_with.md new file mode 100644 index 0000000..4556afb --- /dev/null +++ b/rust/docs/matrix/mul_with.md @@ -0,0 +1,13 @@ +行列積を行います。 +* 列数と行数が一致しない行列同士の乗算はエラーを返します。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); +let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); +m1.mul_with::(&m2).unwrap(); +assert_eq!(m1.get(0, 0), Some(&19)); +assert_eq!(m1.get(0, 1), Some(&22)); +assert_eq!(m1.get(1, 0), Some(&43)); +assert_eq!(m1.get(1, 1), Some(&50)); +``` diff --git a/rust/docs/matrix/new.md b/rust/docs/matrix/new.md new file mode 100644 index 0000000..3259a15 --- /dev/null +++ b/rust/docs/matrix/new.md @@ -0,0 +1,9 @@ +二次元配列から行列を作成します。 +* TはDefaultとCopyトレイトを実装している必要があります。 +# Examples +use NeuralNetwork::matrix::Matrix; +assert_eq!(m.get(0, 0), Some(&1)); +assert_eq!(m.get(0, 1), Some(&2)); +assert_eq!(m.get(1, 0), Some(&3)); +assert_eq!(m.get(1, 1), Some(&4)); +``` diff --git a/rust/docs/matrix/row_iter.md b/rust/docs/matrix/row_iter.md new file mode 100644 index 0000000..b372464 --- /dev/null +++ b/rust/docs/matrix/row_iter.md @@ -0,0 +1,7 @@ +指定した行の要素への参照を返します。 +# Examples +``` +use matrix::Matrix; +let m: Matrix = Matrix::with_size(2, 2); +assert_eq!(m.row_iter(0).unwrap().collect::>(), vec![&0, &0]); +``` diff --git a/rust/docs/matrix/row_mut_iter.md b/rust/docs/matrix/row_mut_iter.md new file mode 100644 index 0000000..4f4111e --- /dev/null +++ b/rust/docs/matrix/row_mut_iter.md @@ -0,0 +1,10 @@ +指定した行の要素への可変参照を返します。 +# Examples +``` +use matrix::Matrix; +let mut m: Matrix = Matrix::with_size(2, 2); +if let Some(row_iter) = m.row_mut_iter(0) { + row_iter.for_each(|val| *val = 42); +} +assert_eq!(m.row_mut_iter(0).unwrap().collect::>(), vec![&42, &42]); +``` diff --git a/rust/docs/matrix/rows.md b/rust/docs/matrix/rows.md new file mode 100644 index 0000000..11fee2f --- /dev/null +++ b/rust/docs/matrix/rows.md @@ -0,0 +1,7 @@ +行数を返します。 +# Examples +``` +use 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 new file mode 100644 index 0000000..5d00383 --- /dev/null +++ b/rust/docs/matrix/sub.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの減算を行います。 +* サイズが異なる行列同士の減算はエラーを返します。 +# Examples +``` +use 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(); +assert_eq!(m1.get(0, 0), Some(&-4)); +assert_eq!(m1.get(0, 1), Some(&-4)); +assert_eq!(m1.get(1, 0), Some(&-4)); +assert_eq!(m1.get(1, 1), Some(&-4)); +``` diff --git a/rust/docs/matrix/sub_assign.md b/rust/docs/matrix/sub_assign.md new file mode 100644 index 0000000..58c934a --- /dev/null +++ b/rust/docs/matrix/sub_assign.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの減算を行います。 +* サイズが異なる行列同士の減算はパニックを引き起こします。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); +let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); +m1 -= m2; +assert_eq!(m1.get(0, 0), Some(&-4)); +assert_eq!(m1.get(0, 1), Some(&-4)); +assert_eq!(m1.get(1, 0), Some(&-4)); +assert_eq!(m1.get(1, 1), Some(&-4)); +``` diff --git a/rust/docs/matrix/sub_trait.md b/rust/docs/matrix/sub_trait.md new file mode 100644 index 0000000..64b40ea --- /dev/null +++ b/rust/docs/matrix/sub_trait.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの減算を行います。 +* サイズが異なる行列同士の減算はNoneを返します。 +# Examples +``` +use 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)); +``` diff --git a/rust/docs/matrix/sub_with.md b/rust/docs/matrix/sub_with.md new file mode 100644 index 0000000..145b1b4 --- /dev/null +++ b/rust/docs/matrix/sub_with.md @@ -0,0 +1,13 @@ +同じサイズの行列同士の要素ごとの減算を行います。 +* サイズが異なる行列同士の減算はエラーを返します。 +# Examples +``` +use matrix::Matrix; +let mut m1: Matrix = Matrix::new([[1, 2], [3, 4]]); +let m2: Matrix = Matrix::new([[5, 6], [7, 8]]); +m1.sub_with::(&m2).unwrap(); +assert_eq!(m1.get(0, 0), Some(&-4)); +assert_eq!(m1.get(0, 1), Some(&-4)); +assert_eq!(m1.get(1, 0), Some(&-4)); +assert_eq!(m1.get(1, 1), Some(&-4)); +``` diff --git a/rust/docs/matrix/with_size.md b/rust/docs/matrix/with_size.md new file mode 100644 index 0000000..3aaacbb --- /dev/null +++ b/rust/docs/matrix/with_size.md @@ -0,0 +1,12 @@ +サイズを指定して行列を作成します。 +* 要素は全てTのデフォルト値で初期化されます。 +* TはDefaultとCopyトレイトを実装している必要があります。 +# Examples +``` +use 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)); +assert_eq!(m.get(1, 0), Some(&0)); +assert_eq!(m.get(1, 1), Some(&0)); +``` From 89a34d437612b5156242720029333a175bede294 Mon Sep 17 00:00:00 2001 From: OtonariS Date: Thu, 4 Jun 2026 14:37:57 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E3=83=89=E3=82=AD=E3=83=A5=E3=83=A1?= =?UTF-8?q?=E3=83=B3=E3=83=88=E3=81=AE=E4=BF=AE=E6=AD=A3=E3=82=92=E8=A1=8C?= =?UTF-8?q?=E3=81=84=E3=81=BE=E3=81=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rust/docs/matrix/div_trait.md | 2 +- rust/docs/matrix/div_with.md | 2 +- rust/docs/matrix/mul_assign.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/docs/matrix/div_trait.md b/rust/docs/matrix/div_trait.md index 5cb124f..ed74e37 100644 --- a/rust/docs/matrix/div_trait.md +++ b/rust/docs/matrix/div_trait.md @@ -3,7 +3,7 @@ * 0で割る要素がある場合もNoneを返します。 # Examples ``` -use NeuralNetwork::matrix::Matrix; +use matrix::Matrix; let m2: Matrix = Matrix::new([[1, 2], [3, 4]]); let m3 = m1 / m2; assert_eq!(m3.unwrap().get(0, 0), Some(&10)); diff --git a/rust/docs/matrix/div_with.md b/rust/docs/matrix/div_with.md index df0e115..5cb2fb1 100644 --- a/rust/docs/matrix/div_with.md +++ b/rust/docs/matrix/div_with.md @@ -1,4 +1,4 @@ -/ 同じサイズの行列同士の要素ごとの除算を行います。 +同じサイズの行列同士の要素ごとの除算を行います。 * サイズが異なる行列同士の除算はエラーを返します。 * 0で割る要素がある場合もエラーを返します。 # Examples diff --git a/rust/docs/matrix/mul_assign.md b/rust/docs/matrix/mul_assign.md index c645d5f..208201f 100644 --- a/rust/docs/matrix/mul_assign.md +++ b/rust/docs/matrix/mul_assign.md @@ -1,4 +1,4 @@ -同じサイズの行列同士の要素ごとの乗算を行います。 +行列積を行います。 * サイズが異なる行列同士の乗算はパニックを引き起こします。 # Examples ```