1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
use super::*;
/// A scalar or a matrix.
#[derive(FromPyObject)]
pub enum ScalarOrMatrix {
Scalar(ConvertibleToRationalPolynomial),
Matrix(PythonMatrix),
}
#[cfg(feature = "python_stubgen")]
impl_stub_type!(ScalarOrMatrix = ConvertibleToRationalPolynomial | PythonMatrix);
/// A Symbolica matrix with rational polynomial coefficients.
#[cfg_attr(feature = "python_stubgen", gen_stub_pyclass)]
#[pyclass(from_py_object, name = "Matrix", subclass, module = "symbolica.core")]
#[derive(Clone)]
pub struct PythonMatrix {
pub matrix: Matrix<RationalPolynomialField<IntegerRing, u16>>,
}
impl PythonMatrix {
fn unify(&self, rhs: &PythonMatrix) -> (PythonMatrix, PythonMatrix) {
let mut zero = self.matrix.field().zero();
let mut self_data = self.matrix.clone().into_vec();
let mut new_rhs_data = rhs.matrix.clone().into_vec();
for e in &mut self_data {
zero.unify_variables(e);
}
for e in &mut new_rhs_data {
zero.unify_variables(e);
}
(
PythonMatrix {
matrix: Matrix::from_linear(
self_data,
self.matrix.nrows() as u32,
self.matrix.ncols() as u32,
RationalPolynomialField::new(Z),
)
.unwrap(),
},
PythonMatrix {
matrix: Matrix::from_linear(
new_rhs_data,
rhs.matrix.nrows() as u32,
rhs.matrix.ncols() as u32,
RationalPolynomialField::new(Z),
)
.unwrap(),
},
)
}
fn unify_scalar(
&self,
rhs: &PythonRationalPolynomial,
) -> (PythonMatrix, PythonRationalPolynomial) {
let mut zero = self.matrix.field().zero();
let mut self_data = self.matrix.clone().into_vec();
for e in &mut self_data {
zero.unify_variables(e);
}
let mut new_rhs = rhs.poly.clone();
zero.unify_variables(&mut new_rhs);
(
PythonMatrix {
matrix: Matrix::from_linear(
self_data,
self.matrix.nrows() as u32,
self.matrix.ncols() as u32,
RationalPolynomialField::new(Z),
)
.unwrap(),
},
PythonRationalPolynomial { poly: new_rhs },
)
}
}
#[cfg_attr(feature = "python_stubgen", gen_stub_pymethods)]
#[cfg_attr(not(feature = "python_stubgen"), remove_gen_stub)]
#[pymethods]
impl PythonMatrix {
/// Create a new zeroed matrix with `nrows` rows and `ncols` columns.
#[new]
pub fn new(nrows: u32, ncols: u32) -> PyResult<PythonMatrix> {
if nrows == 0 || ncols == 0 {
return Err(exceptions::PyValueError::new_err(
"The matrix must have at least one row and one column",
));
}
Ok(PythonMatrix {
matrix: Matrix::new(nrows, ncols, RationalPolynomialField::new(Z)),
})
}
/// Create a new square matrix with `nrows` rows and ones on the main diagonal and zeroes elsewhere.
#[classmethod]
pub fn identity(_cls: &Bound<'_, PyType>, nrows: u32) -> PyResult<PythonMatrix> {
if nrows == 0 {
return Err(exceptions::PyValueError::new_err(
"The matrix must have at least one row and one column",
));
}
Ok(PythonMatrix {
matrix: Matrix::identity(nrows, RationalPolynomialField::new(Z)),
})
}
/// Create a new matrix with the scalars `diag` on the main diagonal and zeroes elsewhere.
#[classmethod]
pub fn eye(
_cls: &Bound<'_, PyType>,
diag: Vec<ConvertibleToRationalPolynomial>,
) -> PyResult<PythonMatrix> {
if diag.is_empty() {
return Err(exceptions::PyValueError::new_err(
"The diagonal must have at least one entry",
));
}
let mut diag: Vec<_> = diag
.into_iter()
.map(|x| Ok(x.to_rational_polynomial()?.poly.clone()))
.collect::<PyResult<_>>()?;
// unify the entries
let (first, rest) = diag.split_first_mut().unwrap();
for _ in 0..2 {
for x in &mut *rest {
first.unify_variables(x);
}
}
let field = RationalPolynomialField::new(Z);
Ok(PythonMatrix {
matrix: Matrix::eye(&diag, field),
})
}
/// Create a new column vector from a list of scalars.
#[classmethod]
pub fn vec(
_cls: &Bound<'_, PyType>,
entries: Vec<ConvertibleToRationalPolynomial>,
) -> PyResult<PythonMatrix> {
if entries.is_empty() {
return Err(exceptions::PyValueError::new_err(
"The matrix must have at least one row and one column",
));
}
let mut entries: Vec<_> = entries
.into_iter()
.map(|x| Ok(x.to_rational_polynomial()?.poly.clone()))
.collect::<PyResult<_>>()?;
// unify the entries
let (first, rest) = entries.split_first_mut().unwrap();
for _ in 0..2 {
for x in &mut *rest {
first.unify_variables(x);
}
}
let field = RationalPolynomialField::new(Z);
Ok(PythonMatrix {
matrix: Matrix::new_vec(entries, field),
})
}
/// Create a new zeroed matrix with `nrows` rows and `ncols` columns.
///
/// Parameters
/// ----------
/// nrows: int
/// The number of rows.
/// ncols: int
/// The number of columns.
#[classmethod]
pub fn from_linear(
_cls: &Bound<'_, PyType>,
nrows: u32,
ncols: u32,
entries: Vec<ConvertibleToRationalPolynomial>,
) -> PyResult<PythonMatrix> {
if entries.is_empty() {
return Err(exceptions::PyValueError::new_err(
"The matrix must have at least one row and one column",
));
}
let mut entries: Vec<_> = entries
.into_iter()
.map(|x| Ok(x.to_rational_polynomial()?.poly.clone()))
.collect::<PyResult<_>>()?;
// unify the entries
let (first, rest) = entries.split_first_mut().unwrap();
for _ in 0..2 {
for x in &mut *rest {
first.unify_variables(x);
}
}
let field = RationalPolynomialField::new(Z);
Ok(PythonMatrix {
matrix: Matrix::from_linear(entries, nrows, ncols, field)
.map_err(|e| exceptions::PyValueError::new_err(format!("Invalid matrix: {}", e)))?,
})
}
/// Create a new matrix from a 2-dimensional vector of scalars.
///
/// Parameters
/// ----------
/// entries: Sequence[Sequence[RationalPolynomial | Polynomial | Expression | int]]
/// The nested row entries of the matrix.
#[classmethod]
pub fn from_nested(
cls: &Bound<'_, PyType>,
entries: Vec<Vec<ConvertibleToRationalPolynomial>>,
) -> PyResult<PythonMatrix> {
if entries.is_empty() || entries.iter().any(|x| x.is_empty()) {
return Err(exceptions::PyValueError::new_err(
"The matrix must have at least one row and one column",
));
}
let nrows = entries.len() as u32;
let ncols = entries[0].len() as u32;
if entries.iter().any(|x| x.len() != ncols as usize) {
return Err(exceptions::PyValueError::new_err(
"The matrix is not rectangular",
));
}
let entries: Vec<_> = entries.into_iter().flatten().collect();
Self::from_linear(cls, nrows, ncols, entries)
}
/// Return the number of rows.
pub fn nrows(&self) -> usize {
self.matrix.nrows()
}
/// Return the number of columns.
pub fn ncols(&self) -> usize {
self.matrix.ncols()
}
/// Return true iff every entry in the matrix is zero.
pub fn is_zero(&self) -> bool {
self.matrix.is_zero()
}
/// Return true iff every non- main diagonal entry in the matrix is zero.
pub fn is_diagonal(&self) -> bool {
self.matrix.is_diagonal()
}
/// Return the transpose of the matrix.
pub fn transpose(&self) -> PythonMatrix {
PythonMatrix {
matrix: self.matrix.transpose(),
}
}
#[pyo3(signature = (row1, row2, start=0))]
pub fn swap_rows(&mut self, row1: u32, row2: u32, start: u32) -> PyResult<()> {
if row1 >= self.matrix.nrows() as u32 || row2 >= self.matrix.nrows() as u32 {
return Err(exceptions::PyIndexError::new_err("Row index out of bounds"));
}
if start >= self.matrix.ncols() as u32 {
return Err(exceptions::PyIndexError::new_err(
"Start index out of bounds",
));
}
self.matrix.swap_rows(row1, row2, start);
Ok(())
}
pub fn swap_cols(&mut self, col1: u32, col2: u32) -> PyResult<()> {
if col1 >= self.matrix.ncols() as u32 || col2 >= self.matrix.ncols() as u32 {
return Err(exceptions::PyIndexError::new_err(
"Column index out of bounds",
));
}
self.matrix.swap_cols(col1, col2);
Ok(())
}
/// Return the inverse of the matrix, if it exists.
pub fn inv(&self) -> PyResult<PythonMatrix> {
Ok(PythonMatrix {
matrix: self
.matrix
.inv()
.map_err(|e| exceptions::PyValueError::new_err(format!("{}", e)))?,
})
}
/// Return the determinant of the matrix.
pub fn det(&self) -> PyResult<PythonRationalPolynomial> {
Ok(PythonRationalPolynomial {
poly: self
.matrix
.det()
.map_err(|e| exceptions::PyValueError::new_err(format!("{}", e)))?,
})
}
/// Solve `A * x = b` for `x`, where `A` is the current matrix.
///
/// Parameters
/// ----------
/// b: Matrix
/// The right-hand-side matrix `b` in `A * x = b`.
pub fn solve(&self, b: PythonMatrix) -> PyResult<PythonMatrix> {
let (new_self, new_rhs) = self.unify(&b);
Ok(PythonMatrix {
matrix: new_self
.matrix
.solve(&new_rhs.matrix)
.map_err(|e| exceptions::PyValueError::new_err(format!("{}", e)))?,
})
}
/// Solve `A * x = b` for `x`, where `A` is the current matrix and return any solution if the
/// system is underdetermined.
///
/// Parameters
/// ----------
/// b: Matrix
/// The right-hand-side matrix `b` in `A * x = b`.
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)))?,
})
}
/// Row-reduce the first `max_col` columns of the matrix in-place using Gaussian elimination and return the rank.
///
/// Parameters
/// ----------
/// max_col: int
/// The highest column index included in row reduction.
pub fn row_reduce(&mut self, max_col: u32) -> usize {
self.matrix.row_reduce(max_col)
}
/// 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.
///
/// Parameters
/// ----------
/// b: Matrix
/// The matrix to append as additional columns.
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)))?,
})
}
/// Split the matrix into two matrices at column `index`.
///
/// Parameters
/// ----------
/// index: int
/// The column index at which to split the 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 {
poly: self.matrix.content(),
}
}
/// Construct the same matrix, but with the content removed.
pub fn primitive_part(&self) -> PythonMatrix {
PythonMatrix {
matrix: self.matrix.primitive_part(),
}
}
/// Apply a function `f` to every entry of the matrix.
///
/// Parameters
/// ----------
/// f: Callable[[RationalPolynomial], RationalPolynomial]
/// The callback or function to apply.
pub fn map(
&self,
#[gen_stub(override_type(
type_repr = "typing.Callable[[RationalPolynomial], RationalPolynomial]"
))]
f: Py<PyAny>,
) -> PyResult<PythonMatrix> {
let data = self
.matrix
.into_iter()
.map(|x| {
let expr = PythonRationalPolynomial { poly: x.clone() };
Python::attach(|py| {
Ok(f.call1(py, (expr,))?
.extract::<ConvertibleToRationalPolynomial>(py)?
.to_rational_polynomial()?
.poly
.clone())
})
})
.collect::<PyResult<_>>()?;
Ok(PythonMatrix {
matrix: Matrix::from_linear(
data,
self.matrix.nrows() as u32,
self.matrix.ncols() as u32,
self.matrix.field().clone(),
)
.unwrap(),
})
}
fn __getitem__(&self, mut idx: (isize, isize)) -> PyResult<PythonRationalPolynomial> {
if idx.0 < 0 {
idx.0 += self.matrix.nrows() as isize;
}
if idx.1 < 0 {
idx.1 += self.matrix.ncols() as isize;
}
if idx.0 as usize >= self.matrix.nrows() || idx.1 as usize >= self.matrix.ncols() {
return Err(exceptions::PyIndexError::new_err("Index out of bounds"));
}
Ok(PythonRationalPolynomial {
poly: self.matrix[(idx.0 as u32, idx.1 as u32)].clone(),
})
}
/// Convert the matrix into a human-readable string, with tunable settings.
///
/// Parameters
/// ----------
/// mode: PrintMode
/// The mode that controls how the input is interpreted or formatted.
/// max_line_length: int | None
/// The preferred maximum line length before wrapping.
/// indentation: int
/// The number of spaces used for wrapped lines.
/// fill_indented_lines: bool
/// Whether wrapped lines should be padded to the configured indentation.
/// pretty_matrix: bool
/// Whether matrices should be printed in the pretty multi-line layout.
/// number_thousands_separator: str | None
/// The separator inserted between groups of digits in printed integers.
/// multiplication_operator: str
/// The string used to print multiplication.
/// double_star_for_exponentiation: bool
/// Whether exponentiation should be printed as `**` instead of `^`.
/// function_brackets: tuple[str, str]
/// The opening and closing brackets used when printing function arguments.
/// num_exp_as_superscript: bool
/// Whether small integer exponents should be printed as superscripts.
/// precision: int | None
/// The decimal precision used when printing numeric coefficients.
/// show_namespaces: bool
/// Whether namespaces should be included in the formatted output.
/// hide_namespace: str | None
/// A namespace prefix to omit from printed symbol names.
/// include_attributes: bool
/// Whether symbol attributes should be included in the printed output.
/// max_terms: int | None
/// The maximum number of terms to print before truncating the output.
/// custom_print_mode: dict[str, int | str | dict[str | int, Any]] | None
/// Custom print data passed through to custom print callbacks.
#[pyo3(signature =
(mode = PythonPrintMode::Symbolica,
max_line_length = Some(80),
indentation = 4,
fill_indented_lines = true,
pretty_matrix = true,
number_thousands_separator = None,
multiplication_operator = '·',
double_star_for_exponentiation = false,
function_brackets = ('(',')'),
num_exp_as_superscript = true,
precision = None,
show_namespaces = false,
hide_namespace = None,
include_attributes = false,
max_terms = None,
custom_print_mode = None)
)]
pub fn format(
&self,
mode: PythonPrintMode,
max_line_length: Option<usize>,
indentation: usize,
fill_indented_lines: bool,
pretty_matrix: bool,
number_thousands_separator: Option<char>,
multiplication_operator: char,
double_star_for_exponentiation: bool,
function_brackets: (char, char),
num_exp_as_superscript: bool,
precision: Option<usize>,
show_namespaces: bool,
hide_namespace: Option<&str>,
include_attributes: bool,
max_terms: Option<usize>,
custom_print_mode: Option<HashMap<String, PythonPrintUserData>>,
) -> PyResult<String> {
Ok(self.matrix.format_string(
&PrintOptions {
max_line_length,
indentation,
fill_indented_lines,
terms_on_new_line: false,
color_mode: ColorMode::Auto,
color_top_level_sum: false,
color_builtin_symbols: false,
bracket_level_colors: None,
print_ring: false,
symmetric_representation_for_finite_field: false,
explicit_rational_polynomial: false,
number_thousands_separator,
multiplication_operator,
double_star_for_exponentiation,
function_brackets,
num_exp_as_superscript,
mode: mode.into(),
precision,
pretty_matrix,
hide_all_namespaces: !show_namespaces,
color_namespace: true,
hide_namespace: if show_namespaces {
hide_namespace.map(|x| std::borrow::Cow::Owned(x.to_owned()))
} else {
None
},
include_attributes,
max_terms,
custom_print_mode: custom_print_mode
.map(|m| m.into_iter().map(|(k, v)| (k, v.0)).collect())
.unwrap_or_default(),
},
PrintState::default(),
))
}
/// Convert the matrix into a LaTeX string.
pub fn to_latex(&self) -> PyResult<String> {
Ok(format!(
"$${}$$",
self.matrix
.format_string(&*LATEX_PRINT_OPTIONS, PrintState::new())
))
}
/// Compare two matrices.
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
match op {
CompareOp::Eq => Ok(self.matrix == other.matrix),
CompareOp::Ne => Ok(self.matrix != other.matrix),
_ => Err(exceptions::PyTypeError::new_err(
"Inequalities between matrices are not supported".to_string(),
)),
}
}
/// Copy the matrix.
pub fn __copy__(&self) -> Self {
Self {
matrix: self.matrix.clone(),
}
}
/// Convert the matrix into a portable string.
pub fn __repr__(&self) -> PyResult<String> {
Ok(self
.matrix
.format_string(&*PLAIN_PRINT_OPTIONS, PrintState::new()))
}
/// Convert the matrix into a human-readable string.
pub fn __str__(&self) -> PyResult<String> {
Ok(self
.matrix
.format_string(&*DEFAULT_PRINT_OPTIONS, PrintState::new()))
}
/// Convert the matrix into a plain string, useful for importing and exporting.
pub fn format_plain(&self) -> PyResult<String> {
Ok(self
.matrix
.format_string(&*PLAIN_PRINT_OPTIONS, PrintState::new()))
}
/// Convert the matrix into an HTML representation.
pub fn _repr_html_(&self) -> PyResult<String> {
let formatted = self.matrix.format_string(
&PrintOptions::new()
.max_line_length(Some(80))
.multiplication_operator('·')
.num_exp_as_superscript(true)
.max_terms(Some(100))
.color_mode(ColorMode::Always)
.pretty_matrix(true),
PrintState::new(),
);
Ok(crate::printer::AnsiHtmlFormatter::new(&formatted).to_string())
}
/// Convert the matrix into a LaTeX representation.
pub fn _repr_latex_(&self) -> PyResult<String> {
self.to_latex()
}
/// Convert the matrix into a pretty string representation.
pub fn _repr_pretty_(&self, pretty: &Bound<'_, PyAny>, cycle: bool) -> PyResult<()> {
let text = if cycle {
"...".to_string()
} else {
self.matrix.format_string(
&PrintOptions::new()
.max_line_length(Some(80))
.multiplication_operator('·')
.num_exp_as_superscript(true)
.max_terms(Some(100))
.color_mode(ColorMode::Always)
.pretty_matrix(true),
PrintState::new(),
)
};
pretty.call_method1("text", (text,))?;
Ok(())
}
/// Add two matrices `self` and `rhs`, returning the result.
///
/// Parameters
/// ----------
/// rhs: Matrix
/// The right-hand-side operand.
pub fn __add__(&self, rhs: PythonMatrix) -> PyResult<PythonMatrix> {
if self.matrix.nrows() != rhs.matrix.nrows() || self.matrix.ncols() != rhs.matrix.ncols() {
return Err(exceptions::PyValueError::new_err(format!(
"Cannot add matrices of different dimensions: ({},{}) vs ({},{})",
self.matrix.nrows(),
self.matrix.ncols(),
rhs.matrix.nrows(),
rhs.matrix.ncols()
)));
}
let (new_self, new_rhs) = self.unify(&rhs);
Ok(PythonMatrix {
matrix: &new_self.matrix + &new_rhs.matrix,
})
}
/// Subtract matrix `rhs` from `self`, returning the result.
///
/// Parameters
/// ----------
/// rhs: Matrix
/// The right-hand-side operand.
pub fn __sub__(&self, rhs: PythonMatrix) -> PyResult<PythonMatrix> {
self.__add__(rhs.__neg__())
}
/// Matrix multiply `self` and `rhs`, returning the result.
///
/// Parameters
/// ----------
/// rhs: Matrix | RationalPolynomial | Polynomial | Expression | int
/// The right-hand-side operand.
pub fn __mul__(&self, rhs: ScalarOrMatrix) -> PyResult<PythonMatrix> {
match rhs {
ScalarOrMatrix::Scalar(s) => {
let (new_self, new_rhs) = self.unify_scalar(&s.to_rational_polynomial()?);
Ok(Self {
matrix: new_self.matrix.mul_scalar(&new_rhs.poly),
})
}
ScalarOrMatrix::Matrix(m) => {
if self.matrix.ncols() != m.matrix.nrows() {
return Err(exceptions::PyValueError::new_err(format!(
"Cannot multiply matrices because of a dimension mismatch: ({},{}) vs ({},{})",
self.matrix.nrows(),
self.matrix.ncols(),
m.matrix.nrows(),
m.matrix.ncols()
)));
}
let (new_self, new_rhs) = self.unify(&m);
Ok(PythonMatrix {
matrix: &new_self.matrix * &new_rhs.matrix,
})
}
}
}
/// Matrix multiply `rhs` and `self`, returning the result.
///
/// Parameters
/// ----------
/// rhs: RationalPolynomial | Polynomial | Expression | int
/// The right-hand-side operand.
pub fn __rmul__(&self, rhs: ConvertibleToRationalPolynomial) -> PyResult<PythonMatrix> {
self.__mul__(ScalarOrMatrix::Scalar(rhs))
}
/// Matrix multiply `self` and `rhs`, returning the result.
///
/// Parameters
/// ----------
/// rhs: Matrix | RationalPolynomial | Polynomial | Expression | int
/// The right-hand-side operand.
pub fn __matmul__(&self, rhs: ScalarOrMatrix) -> PyResult<PythonMatrix> {
self.__mul__(rhs)
}
/// Matrix multiply `rhs` and `self`, returning the result.
///
/// Parameters
/// ----------
/// rhs: RationalPolynomial | Polynomial | Expression | int
/// The right-hand-side operand.
pub fn __rmatmul__(&self, rhs: ConvertibleToRationalPolynomial) -> PyResult<PythonMatrix> {
self.__mul__(ScalarOrMatrix::Scalar(rhs))
}
/// Divide this matrix by scalar `rhs` and return the result.
///
/// Parameters
/// ----------
/// rhs: RationalPolynomial | Polynomial | Expression | int
/// The right-hand-side operand.
pub fn __truediv__(&self, rhs: ConvertibleToRationalPolynomial) -> PyResult<PythonMatrix> {
let rhs = rhs.to_rational_polynomial()?;
if rhs.poly.is_zero() {
return Err(exceptions::PyZeroDivisionError::new_err(
"Cannot divide a matrix by zero",
));
}
Ok(PythonMatrix {
matrix: self.matrix.div_scalar(&rhs.poly),
})
}
/// Returns a warning that `**` should be used instead of `^` for taking a power.
pub fn __xor__(&self, _rhs: Py<PyAny>) -> PyResult<PythonMatrix> {
Err(exceptions::PyTypeError::new_err(
"Cannot xor a matrix. Did you mean to write a power? Use ** instead, i.e. x**2",
))
}
/// Returns a warning that `**` should be used instead of `^` for taking a power.
pub fn __rxor__(&self, _rhs: Py<PyAny>) -> PyResult<PythonMatrix> {
Err(exceptions::PyTypeError::new_err(
"Cannot xor a matrix. Did you mean to write a power? Use ** instead, i.e. x**2",
))
}
/// Negate the matrix, returning the result.
pub fn __neg__(&self) -> PythonMatrix {
PythonMatrix {
matrix: -self.matrix.clone(),
}
}
}