Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
SparseMatrix.py
Go to the documentation of this file.
1"""Sparse matrix wrapper used by low-level math utilities."""
2
3from pyhyperiso.phyperiso.pyhyperiso import math as mb
4
5
7 """Python wrapper around the C++ sparse matrix class.
8
9 The wrapper exposes only a minimal API: element assignment/access, inversion
10 on a selected index set, and inspection of diagonal entries. It is intended
11 for advanced users who need direct access to sparse matrix primitives in the
12 C++ math module.
13
14 Example:
15 >>> M = SparseMatrix()
16 >>> M[0, 1] = 2.0
17 >>> M[0, 1]
18 2.0
19 """
20
21 def __init__(self):
22 """Create an empty sparse matrix backed by C++."""
23 self._cpp_obj = mb.matrix.SparseMatrix()
24
25 def set(self, row, col, val):
26 """Set one matrix entry.
27
28 Args:
29 row: Row index.
30 col: Column index.
31 val: Numeric value assigned to ``(row, col)``.
32 """
33 self._cpp_obj.set_element(row, col, val)
34
35 def get(self, row, col):
36 """Return one matrix entry.
37
38 Args:
39 row: Row index.
40 col: Column index.
41
42 Returns:
43 float: Stored value returned by the C++ sparse matrix.
44 """
45 return self._cpp_obj.get_element(row, col)
46
47 def print(self, indices):
48 """Print a selected sparse submatrix using the C++ debug printer.
49
50 Args:
51 indices: Indices to print. The exact accepted container mirrors the
52 C++ binding.
53 """
54 self._cpp_obj.print(indices)
55
56 def invert(self, indices):
57 """Invert a selected sparse submatrix.
58
59 Args:
60 indices: Row/column indices defining the submatrix to invert.
61
62 Returns:
63 SparseMatrix: Python wrapper around the inverted C++ matrix.
64 """
65 inv_cpp = self._cpp_obj.invert(indices)
66 inv = SparseMatrix()
67 inv._cpp_obj = inv_cpp
68 return inv
69
70 def diagonal(self):
71 """Return the sparse matrix diagonal entries.
72
73 Returns:
74 Any: Container returned by the C++ binding.
75 """
76 return self._cpp_obj.get_diagonal_elements()
77
78 def __getitem__(self, key):
79 """Return ``self[row, col]``."""
80 row, col = key
81 return self.get(row, col)
82
83 def __setitem__(self, key, value):
84 """Set ``self[row, col] = value``."""
85 row, col = key
86 self.set(row, col, value)
87
88
89__all__ = ["SparseMatrix"]
set(self, row, col, val)