Skip to content

Commit d44c2c1

Browse files
committed
wx, fix: 修补错误问题
1 parent 851c5d6 commit d44c2c1

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

examples/matrix.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use anyhow::{Ok, Result};
2+
3+
fn main() -> Result<()> {
4+
println!("f64 default: {}", f64::default());
5+
Ok(())
6+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod matrix;
2+
3+
pub use matrix::{multiply, Matrix};

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
fn main() {
1+
use anyhow::Result;
2+
3+
fn main() -> Result<()> {
24
println!("Hello, world!");
5+
6+
Ok(())
37
}

src/matrix.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use anyhow::{anyhow, Result};
2+
use std::{
3+
fmt,
4+
ops::{Add, AddAssign, Mul},
5+
};
6+
7+
// 声明一个矩阵的结构
8+
9+
pub struct Matrix<T> {
10+
data: Vec<T>,
11+
row: usize,
12+
col: usize,
13+
}
14+
15+
pub fn multiply<T>(a: &Matrix<T>, b: &Matrix<T>) -> Result<Matrix<T>>
16+
where
17+
T: Copy + Default + Add<Output = T> + AddAssign + Mul<Output = T>,
18+
{
19+
if a.col != b.row {
20+
return Err(anyhow!("Matrix multiply error: a.col != b.row"));
21+
}
22+
23+
// 让数据有缺省值
24+
let mut data = vec![T::default(); a.row * b.col];
25+
for i in 0..a.row {
26+
for j in 0..b.col {
27+
for k in 0..a.col {
28+
data[i * b.col + j] += a.data[i * a.col + k] * b.data[k * b.col + j]
29+
}
30+
}
31+
}
32+
Ok(Matrix {
33+
data,
34+
row: a.row,
35+
col: b.col,
36+
})
37+
}
38+
39+
impl<T: fmt::Debug> Matrix<T> {
40+
pub fn new(data: impl Into<Vec<T>>, row: usize, col: usize) -> Self {
41+
Self {
42+
data: data.into(),
43+
row,
44+
col,
45+
}
46+
}
47+
}
48+
49+
impl<T> fmt::Display for Matrix<T>
50+
where
51+
T: fmt::Display,
52+
{
53+
// display a 2x3 as {1 2 3, 4 5 6}, 3x2 as {1 2, 3 4, 5 6}
54+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55+
write!(f, "{{")?;
56+
for i in 0..self.row {
57+
for j in 0..self.col {
58+
write!(f, "{}", self.data[i * self.col + j])?;
59+
if j != self.col - 1 {
60+
write!(f, " ")?;
61+
}
62+
}
63+
64+
if i != self.row - 1 {
65+
write!(f, ", ")?;
66+
}
67+
}
68+
write!(f, "}}")?;
69+
Ok(())
70+
}
71+
}
72+
73+
impl<T> fmt::Debug for Matrix<T>
74+
where
75+
T: fmt::Display,
76+
{
77+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78+
write!(f, "Matrix(row={}, col={}, {})", self.row, self.col, self)
79+
}
80+
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
#[test]
87+
fn test_matrix_multiply() -> Result<()> {
88+
let a = Matrix::new([1, 2, 3, 4, 5, 6], 2, 3);
89+
let b = Matrix::new([1, 2, 3, 4, 5, 6], 3, 2);
90+
let c = multiply(&a, &b)?;
91+
assert_eq!(c.col, 2);
92+
assert_eq!(c.row, 2);
93+
assert_eq!(c.data, vec![22, 28, 49, 64]);
94+
assert_eq!(format!("{:?}", c), "Matrix(row=2, col=2, {22 28, 49 64})");
95+
96+
Ok(())
97+
}
98+
}

0 commit comments

Comments
 (0)