Compare commits
5 Commits
9b61d89fc4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e690e88bed | |||
| 074b1edbb6 | |||
| 384544c5ae | |||
| 725ca99712 | |||
| 2494c237d7 |
18
Cargo.lock
generated
18
Cargo.lock
generated
@@ -2,30 +2,14 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "autocfg"
|
|
||||||
version = "1.5.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fractions"
|
name = "fractions"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git?branch=main#885bbfeebed047a62ef86eae5bcc44137e2ae127"
|
source = "git+https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git?branch=main#2fea33ca7080b4e05b6e4df0cad90d7e7c97f24f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matrix"
|
name = "matrix"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fractions",
|
"fractions",
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-traits"
|
|
||||||
version = "0.2.19"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -4,5 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num-traits = "0.2.19"
|
|
||||||
fractions = {git = "https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git", branch= "main" }
|
fractions = {git = "https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git", branch= "main" }
|
||||||
|
|||||||
77
src/lib.rs
77
src/lib.rs
@@ -1,6 +1,7 @@
|
|||||||
use fractions::{Fraction, FractionError};
|
pub use fractions::{Fraction, FractionError};
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::fmt::{Debug, Display};
|
use std::error::Error;
|
||||||
|
use std::fmt::{self, Debug, Display};
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use std::ops::Mul;
|
use std::ops::Mul;
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
@@ -27,6 +28,45 @@ impl From<FractionError> for MatrixError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for MatrixError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
MatrixError::IndexOutOfRange => write!(f, "Index out of range"),
|
||||||
|
MatrixError::RowOutOfRange => write!(f, "Row index out of range"),
|
||||||
|
MatrixError::ColumnOutOfRange => write!(f, "Column index out of range"),
|
||||||
|
MatrixError::NotSquared => write!(f, "Matrix must be square"),
|
||||||
|
MatrixError::InvalidDataSize => write!(f, "Invalid data size for matrix"),
|
||||||
|
MatrixError::InvalidSizeForAdd => {
|
||||||
|
write!(f, "Matrices must have same dimensions for addition")
|
||||||
|
}
|
||||||
|
MatrixError::InvalidSizeForSub => {
|
||||||
|
write!(f, "Matrices must have same dimensions for subtraction")
|
||||||
|
}
|
||||||
|
MatrixError::InvalidSizeForMul => {
|
||||||
|
write!(f, "Invalid matrix dimensions for multiplication")
|
||||||
|
}
|
||||||
|
MatrixError::ZeroSize => write!(f, "Matrix cannot have zero rows or columns"),
|
||||||
|
MatrixError::FailedGauss => {
|
||||||
|
write!(f, "Gaussian elimination failed (possibly singular matrix)")
|
||||||
|
}
|
||||||
|
MatrixError::FailedGaussJordan => write!(
|
||||||
|
f,
|
||||||
|
"Gauss-Jordan elimination failed (possibly singular matrix)"
|
||||||
|
),
|
||||||
|
MatrixError::FractionError(err) => write!(f, "Fraction error: {}", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for MatrixError {
|
||||||
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||||
|
match self {
|
||||||
|
MatrixError::FractionError(err) => Some(err),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||||
pub struct Matrix {
|
pub struct Matrix {
|
||||||
rows: usize,
|
rows: usize,
|
||||||
@@ -540,19 +580,30 @@ impl Mul for Matrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Matrix {
|
impl fmt::Display for Matrix {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut display = String::new();
|
let strings: Vec<String> = self.data.iter().map(|x| x.to_string()).collect();
|
||||||
let mut index = 0;
|
|
||||||
for _i in 0..self.columns {
|
let max_width = strings.iter().map(|s| s.len()).max().unwrap_or(0);
|
||||||
display += "{";
|
|
||||||
for _k in 0..self.rows {
|
for r in 0..self.rows {
|
||||||
display += &format!(" {},", self.data[index]);
|
write!(f, "{{ ")?;
|
||||||
index += 1;
|
|
||||||
|
for c in 0..self.columns {
|
||||||
|
let idx = r * self.columns + c;
|
||||||
|
let val = &strings[idx];
|
||||||
|
|
||||||
|
write!(f, "{:>width$}", val, width = max_width)?;
|
||||||
|
|
||||||
|
if c != self.columns - 1 {
|
||||||
|
write!(f, ", ")?;
|
||||||
}
|
}
|
||||||
display += " }\n";
|
|
||||||
}
|
}
|
||||||
write!(f, "{}", display)
|
|
||||||
|
writeln!(f, " }}")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user