Skip to content

Commit

Permalink
Add new matrix methods
Browse files Browse the repository at this point in the history
- Add nsolve_any that yields any valid solution to an underdetermined linear system
- Add augmentation and splitting methods
- Add multi-line matrix formatting
  • Loading branch information
benruijl committed Dec 19, 2024
1 parent a57ec0f commit f924c8d
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 160 deletions.
93 changes: 93 additions & 0 deletions src/api/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,7 @@ impl PythonTransformer {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},)
);
}
Expand Down Expand Up @@ -2878,6 +2879,7 @@ impl PythonExpression {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
)
))
Expand Down Expand Up @@ -5571,6 +5573,7 @@ impl PythonSeries {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new()
)
Expand Down Expand Up @@ -6028,6 +6031,7 @@ impl PythonPolynomial {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new(),
))
Expand Down Expand Up @@ -6846,6 +6850,7 @@ impl PythonFiniteFieldPolynomial {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new(),
))
Expand Down Expand Up @@ -7426,6 +7431,7 @@ impl PythonPrimeTwoPolynomial {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new(),
))
Expand Down Expand Up @@ -7966,6 +7972,7 @@ impl PythonGaloisFieldPrimeTwoPolynomial {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new(),
))
Expand Down Expand Up @@ -8510,6 +8517,7 @@ impl PythonGaloisFieldPolynomial {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new(),
))
Expand Down Expand Up @@ -9055,6 +9063,7 @@ impl PythonNumberFieldPolynomial {
num_exp_as_superscript,
latex,
precision,
pretty_matrix: false,
},
PrintState::new(),
))
Expand Down Expand Up @@ -10675,6 +10684,47 @@ impl PythonMatrix {
})
}

/// Solve `A * x = b` for `x`, where `A` is the current matrix and return any solution if the
/// system is underdetermined.
pub fn solve_any(&self, b: PythonMatrix) -> PyResult<PythonMatrix> {
let (new_self, new_rhs) = self.unify(&b);
Ok(PythonMatrix {
matrix: new_self
.matrix
.solve_any(&new_rhs.matrix)
.map_err(|e| exceptions::PyValueError::new_err(format!("{}", e)))?,
})
}

/// Augment the matrix with another matrix, e.g. create `[A B]` from matrix `A` and `B`.
///
/// Returns an error when the matrices do not have the same number of rows.
pub fn row_reduce(&mut self, max_col: u32) -> usize {
self.matrix.row_reduce(max_col)
}

/// Solve `A * x = b` for `x`, where `A` is the current matrix.
pub fn augment(&self, b: PythonMatrix) -> PyResult<PythonMatrix> {
let (a, b) = self.unify(&b);

Ok(PythonMatrix {
matrix: a
.matrix
.augment(&b.matrix)
.map_err(|e| exceptions::PyValueError::new_err(format!("{}", e)))?,
})
}

/// Solve `A * x = b` for `x`, where `A` is the current matrix.
pub fn split_col(&self, index: u32) -> PyResult<(PythonMatrix, PythonMatrix)> {
let (a, b) = self
.matrix
.split_col(index)
.map_err(|e| exceptions::PyValueError::new_err(format!("{}", e)))?;

Ok((PythonMatrix { matrix: a }, PythonMatrix { matrix: b }))
}

/// Get the content of the matrix, i.e. the gcd of all entries.
pub fn content(&self) -> PythonRationalPolynomial {
PythonRationalPolynomial {
Expand Down Expand Up @@ -10737,6 +10787,49 @@ impl PythonMatrix {
})
}

/// Convert the matrix into a human-readable string, with tunable settings.
#[pyo3(signature =
(pretty_matrix = true,
number_thousands_separator = None,
multiplication_operator = '*',
double_star_for_exponentiation = false,
square_brackets_for_function = false,
num_exp_as_superscript = true,
latex = false,
precision = None)
)]
pub fn format(
&self,
pretty_matrix: bool,
number_thousands_separator: Option<char>,
multiplication_operator: char,
double_star_for_exponentiation: bool,
square_brackets_for_function: bool,
num_exp_as_superscript: bool,
latex: bool,
precision: Option<usize>,
) -> String {
self.matrix.format_string(
&PrintOptions {
terms_on_new_line: false,
color_top_level_sum: false,
color_builtin_symbols: false,
print_finite_field: false,
symmetric_representation_for_finite_field: false,
explicit_rational_polynomial: false,
number_thousands_separator,
multiplication_operator,
double_star_for_exponentiation,
square_brackets_for_function,
num_exp_as_superscript,
latex,
precision,
pretty_matrix,
},
PrintState::default(),
)
}

/// Convert the matrix into a LaTeX string.
pub fn to_latex(&self) -> PyResult<String> {
Ok(format!(
Expand Down
5 changes: 5 additions & 0 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct PrintOptions {
pub num_exp_as_superscript: bool,
pub latex: bool,
pub precision: Option<usize>,
pub pretty_matrix: bool,
}

impl PrintOptions {
Expand All @@ -43,6 +44,7 @@ impl PrintOptions {
num_exp_as_superscript: false,
latex: false,
precision: None,
pretty_matrix: false,
}
}

Expand All @@ -62,6 +64,7 @@ impl PrintOptions {
num_exp_as_superscript: false,
latex: true,
precision: None,
pretty_matrix: false,
}
}

Expand All @@ -81,6 +84,7 @@ impl PrintOptions {
num_exp_as_superscript: false,
latex: false,
precision: None,
pretty_matrix: false,
}
}

Expand Down Expand Up @@ -121,6 +125,7 @@ impl Default for PrintOptions {
num_exp_as_superscript: false,
latex: false,
precision: None,
pretty_matrix: false,
}
}
}
Expand Down
Loading

0 comments on commit f924c8d

Please sign in to comment.