mplusa 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mplusa/maxplus.py +27 -9
- mplusa/minplus.py +27 -9
- {mplusa-0.0.1.dist-info → mplusa-0.0.2.dist-info}/METADATA +11 -1
- mplusa-0.0.2.dist-info/RECORD +8 -0
- {mplusa-0.0.1.dist-info → mplusa-0.0.2.dist-info}/WHEEL +1 -1
- mplusa-0.0.1.dist-info/RECORD +0 -8
- {mplusa-0.0.1.dist-info → mplusa-0.0.2.dist-info}/LICENSE +0 -0
- {mplusa-0.0.1.dist-info → mplusa-0.0.2.dist-info}/top_level.txt +0 -0
mplusa/maxplus.py
CHANGED
|
@@ -4,10 +4,18 @@ from numbers import Real
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
def add(*args) -> Real:
|
|
7
|
+
if math.inf in args:
|
|
8
|
+
raise ValueError(
|
|
9
|
+
'Maxplus.add: value out of domain.'
|
|
10
|
+
)
|
|
7
11
|
return max(args)
|
|
8
12
|
|
|
9
13
|
|
|
10
14
|
def mult(*args) -> Real:
|
|
15
|
+
if math.inf in args:
|
|
16
|
+
raise ValueError(
|
|
17
|
+
'Maxplus.add: value out of domain.'
|
|
18
|
+
)
|
|
11
19
|
return sum(args) if -math.inf not in args else -math.inf
|
|
12
20
|
|
|
13
21
|
|
|
@@ -44,6 +52,10 @@ def mult_matrices(A : np.ndarray,
|
|
|
44
52
|
|
|
45
53
|
def modulo(a : Real,
|
|
46
54
|
t : int) -> Real:
|
|
55
|
+
if a < 0 or t < 0:
|
|
56
|
+
raise ValueError(
|
|
57
|
+
'Maxplus.modulo: modulo operation is only defined for positive numbers.'
|
|
58
|
+
)
|
|
47
59
|
if a == -math.inf:
|
|
48
60
|
return -math.inf
|
|
49
61
|
if a == 0:
|
|
@@ -58,7 +70,7 @@ def modulo_matrices(A : np.ndarray,
|
|
|
58
70
|
if b.shape[1] != 1:
|
|
59
71
|
raise ValueError(
|
|
60
72
|
'Maxplus.modulo_matrices: given matrix b ' +\
|
|
61
|
-
'is not a
|
|
73
|
+
'is not a vertical vector of shape Mx1 (has shape of {}).'.format(
|
|
62
74
|
b.shape
|
|
63
75
|
)
|
|
64
76
|
)
|
|
@@ -89,7 +101,7 @@ def power_matrix(A : np.ndarray,
|
|
|
89
101
|
k : int) -> np.ndarray:
|
|
90
102
|
if np.any(np.diagonal(A) != 0):
|
|
91
103
|
raise ValueError(
|
|
92
|
-
'Maxplus.power_matrix: matrix contains non-zero values on diagonal.'
|
|
104
|
+
'Maxplus.power_matrix: matrix contains non-zero values on the diagonal.'
|
|
93
105
|
)
|
|
94
106
|
if k == 0:
|
|
95
107
|
result = unit_matrix(A.shape[0], A.shape[1])
|
|
@@ -150,12 +162,18 @@ class Polynomial:
|
|
|
150
162
|
def __call__(self, x : float) -> float:
|
|
151
163
|
return add(*[mult(coefficient, power(x, i)) for i, coefficient in enumerate(self.coefficients)])
|
|
152
164
|
|
|
165
|
+
def get_lines(self) -> list[tuple[float]]:
|
|
166
|
+
""" Returns the a and b values of standard linear functions building the polynomial in form of y = ax + b. """
|
|
167
|
+
return [(a, b) for a, b in enumerate(self.coefficients) if b > -math.inf]
|
|
168
|
+
|
|
153
169
|
def get_hypersurface(self) -> list[float]:
|
|
170
|
+
lines = self.get_lines()
|
|
154
171
|
result = []
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
172
|
+
for (a, c) in lines:
|
|
173
|
+
for (b, d) in lines:
|
|
174
|
+
if a == b or c == d:
|
|
175
|
+
continue
|
|
176
|
+
x = (d - c) / (a - b)
|
|
177
|
+
if a * x + c == self(x):
|
|
178
|
+
result.append(x)
|
|
179
|
+
return list(set(result))
|
mplusa/minplus.py
CHANGED
|
@@ -4,10 +4,18 @@ from numbers import Real
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
def add(*args) -> Real:
|
|
7
|
+
if -math.inf in args:
|
|
8
|
+
raise ValueError(
|
|
9
|
+
'Minplus.add: value out of domain.'
|
|
10
|
+
)
|
|
7
11
|
return min(args)
|
|
8
12
|
|
|
9
13
|
|
|
10
14
|
def mult(*args) -> Real:
|
|
15
|
+
if -math.inf in args:
|
|
16
|
+
raise ValueError(
|
|
17
|
+
'Minplus.mult: value out of domain.'
|
|
18
|
+
)
|
|
11
19
|
return sum(args) if math.inf not in args else math.inf
|
|
12
20
|
|
|
13
21
|
|
|
@@ -44,6 +52,10 @@ def mult_matrices(A : np.ndarray,
|
|
|
44
52
|
|
|
45
53
|
def modulo(a : Real,
|
|
46
54
|
t : int) -> Real:
|
|
55
|
+
if a < 0 or t < 0:
|
|
56
|
+
raise ValueError(
|
|
57
|
+
'Minplus.modulo: modulo operation is only defined for positive numbers.'
|
|
58
|
+
)
|
|
47
59
|
if a == math.inf:
|
|
48
60
|
return math.inf
|
|
49
61
|
if a == 0:
|
|
@@ -58,7 +70,7 @@ def modulo_matrices(A : np.ndarray,
|
|
|
58
70
|
if b.shape[1] != 1:
|
|
59
71
|
raise ValueError(
|
|
60
72
|
'Minplus.modulo_matrices: given matrix b ' +\
|
|
61
|
-
'is not a
|
|
73
|
+
'is not a vertical vector of shape Mx1 (has shape of {}).'.format(
|
|
62
74
|
b.shape
|
|
63
75
|
)
|
|
64
76
|
)
|
|
@@ -89,7 +101,7 @@ def power_matrix(A : np.ndarray,
|
|
|
89
101
|
k : int) -> np.ndarray:
|
|
90
102
|
if np.any(np.diagonal(A) != 0):
|
|
91
103
|
raise ValueError(
|
|
92
|
-
'Minplus.power_matrix: matrix contains non-zero values on diagonal.'
|
|
104
|
+
'Minplus.power_matrix: matrix contains non-zero values on the diagonal.'
|
|
93
105
|
)
|
|
94
106
|
if k == 0:
|
|
95
107
|
result = unit_matrix(A.shape[0], A.shape[1])
|
|
@@ -150,12 +162,18 @@ class Polynomial:
|
|
|
150
162
|
def __call__(self, x : float) -> float:
|
|
151
163
|
return add(*[mult(coefficient, power(x, i)) for i, coefficient in enumerate(self.coefficients)])
|
|
152
164
|
|
|
165
|
+
def get_lines(self) -> list[tuple[float]]:
|
|
166
|
+
""" Returns the a and b values of standard linear functions building the polynomial in form of y = ax + b. """
|
|
167
|
+
return [(a, b) for a, b in enumerate(self.coefficients) if b < math.inf]
|
|
168
|
+
|
|
153
169
|
def get_hypersurface(self) -> list[float]:
|
|
170
|
+
lines = self.get_lines()
|
|
154
171
|
result = []
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
172
|
+
for (a, c) in lines:
|
|
173
|
+
for (b, d) in lines:
|
|
174
|
+
if a == b or c == d:
|
|
175
|
+
continue
|
|
176
|
+
x = (d - c) / (a - b)
|
|
177
|
+
if a * x + c == self(x):
|
|
178
|
+
result.append(x)
|
|
179
|
+
return list(set(result))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.2
|
|
2
2
|
Name: mplusa
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.2
|
|
4
4
|
Summary: A library for calculations in tropical and arctic semirings.
|
|
5
5
|
Author-email: "Maksymilian W." <maksymilian3563@gmail.com>
|
|
6
6
|
License: Copyright (c) 2025 Maksymilian Wiekiera
|
|
@@ -41,24 +41,34 @@ After having installed the library one can import one of the two modules the pac
|
|
|
41
41
|
|
|
42
42
|
**`add(*args) -> Real`**
|
|
43
43
|
Tropical addition. Essentially an alias for Python's `min` function.
|
|
44
|
+
|
|
44
45
|
**`mult(*args) -> Real`**
|
|
45
46
|
Tropical multiplication. Essentially an alias for Python's `sum` function.
|
|
47
|
+
|
|
46
48
|
**`add_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
|
|
47
49
|
Tropical addition of NumPy arrays. The summed matrices have to be of the same shape.
|
|
50
|
+
|
|
48
51
|
**`mult_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
|
|
49
52
|
Tropical multiplication of NumPy arrays. The multiplied matrices have to be of sizes MxN and NxP and their order matters. The result is of shape MxP.
|
|
53
|
+
|
|
50
54
|
**`modulo(a : Real, t : int) -> Real`**
|
|
51
55
|
Tropical modulo operator. It can be understood as the difference between the number $a$ and $t^k$ where $k$ is the largest integer that satisfies $a \geq t^k$.
|
|
56
|
+
|
|
52
57
|
**`modulo_matrices(A : np.ndarray, b : np.ndarray) -> np.ndarray`**
|
|
53
58
|
Tropical modulo operator for NumPy arrays. The input matrices should be of size MxN and Mx1. The result is an MxN matrix.
|
|
59
|
+
|
|
54
60
|
**`power(a : real, k : int) -> Real`**
|
|
55
61
|
Tropical power operator. Applies the multiplication k times.
|
|
62
|
+
|
|
56
63
|
**`power_matrix(A : np.ndarray, k : int) -> np.ndarray`**
|
|
57
64
|
Tropical power operator for NumPy arrays. It multiplies the matrix k times.
|
|
65
|
+
|
|
58
66
|
**`unit_matrix(width : int, height : int) -> np.ndarray`**
|
|
59
67
|
Creates a tropical unit matrix of given width and height.
|
|
68
|
+
|
|
60
69
|
**`star(A : np.ndarray) -> np.ndarray`**
|
|
61
70
|
Definition of a unique operator of tropical algebra, usually denoted as $\mathbf{A}^*$. It returns the value to which an infinite recursive sum of matrices converges. The input matrix has to be square and the series created in the process of calculating the value needs to be convergent.
|
|
71
|
+
|
|
62
72
|
**`Polynomial(*coefficients)`**
|
|
63
73
|
This is a class that implements basic single-variable tropical polynomials. Calling an object of this class allows to take a value the polynomial takes at a given point, it also implements function `get_hypersurface` which returns a list of its roots.
|
|
64
74
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
mplusa/__init__.py,sha256=8D1JvAZfjHkQ1DOqrxLyXWMEfROgGwf8Y41-rSX51cY,44
|
|
2
|
+
mplusa/maxplus.py,sha256=UnFh0lfbvarUfDHH7JQpQY5KC0_IP9uzgYYnbOkd3CY,5461
|
|
3
|
+
mplusa/minplus.py,sha256=uNsvTNjjlMO4_zuFI5S2z2HP07AKWXnCWOItw2fE5Js,5460
|
|
4
|
+
mplusa-0.0.2.dist-info/LICENSE,sha256=x1S-x_tM1tAmndGsdQKT4DU9LnstfQRgS4befak4XdA,1063
|
|
5
|
+
mplusa-0.0.2.dist-info/METADATA,sha256=2JhnKWBWTaO2dDThFq7DdlUfbCsLMObmVr2O49qqlsE,5100
|
|
6
|
+
mplusa-0.0.2.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
|
|
7
|
+
mplusa-0.0.2.dist-info/top_level.txt,sha256=W5b7P8CkZ-DB3-2K0Rcf0T0tpEuWLCbZBaVgrd7FEcM,7
|
|
8
|
+
mplusa-0.0.2.dist-info/RECORD,,
|
mplusa-0.0.1.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
mplusa/__init__.py,sha256=8D1JvAZfjHkQ1DOqrxLyXWMEfROgGwf8Y41-rSX51cY,44
|
|
2
|
-
mplusa/maxplus.py,sha256=q2GOM-VDGNCXEL-Gy7QXZPgU8ZyPUh89XEB8o2QNUdA,4931
|
|
3
|
-
mplusa/minplus.py,sha256=QvYJ-Zg3pC134k9Qiq6qQ1SnXdE5CgjA9odcbo55uY8,4929
|
|
4
|
-
mplusa-0.0.1.dist-info/LICENSE,sha256=x1S-x_tM1tAmndGsdQKT4DU9LnstfQRgS4befak4XdA,1063
|
|
5
|
-
mplusa-0.0.1.dist-info/METADATA,sha256=zS3zcKjEyYylkN6VbcTf_awLCIDrxMrNYcAU31yt29c,5090
|
|
6
|
-
mplusa-0.0.1.dist-info/WHEEL,sha256=EaM1zKIUYa7rQnxGiOCGhzJABRwy4WO57rWMR3_tj4I,91
|
|
7
|
-
mplusa-0.0.1.dist-info/top_level.txt,sha256=W5b7P8CkZ-DB3-2K0Rcf0T0tpEuWLCbZBaVgrd7FEcM,7
|
|
8
|
-
mplusa-0.0.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|