mplusa 0.0.1__tar.gz

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-0.0.1/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2025 Maksymilian Wiekiera
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
mplusa-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,84 @@
1
+ Metadata-Version: 2.2
2
+ Name: mplusa
3
+ Version: 0.0.1
4
+ Summary: A library for calculations in tropical and arctic semirings.
5
+ Author-email: "Maksymilian W." <maksymilian3563@gmail.com>
6
+ License: Copyright (c) 2025 Maksymilian Wiekiera
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+ Project-URL: homepage, https://github.com/Hadelekw/mplusa
26
+ Classifier: Programming Language :: Python :: 3
27
+ Classifier: Operating System :: OS Independent
28
+ Requires-Python: >=3.11
29
+ Description-Content-Type: text/markdown
30
+ License-File: LICENSE
31
+ Requires-Dist: numpy>=2.2.3
32
+
33
+ # MPlusA
34
+ ---
35
+ **MPlusA** is a small Python library for tropical algebra (also known as $(\min, +)$ and $(\max, +)$ algebra). It provides the definitions of basic operations on numbers and NumPy arrays, as well as a basic implementation of tropical polynomials.
36
+
37
+ Any improvements or fixes are always welcome.
38
+
39
+ ## How to use
40
+ After having installed the library one can import one of the two modules the package consists of (`minplus` and `maxplus`) and use the full array of its capabilities. The functions are essentially the same between the modules. The list below is a full list of the library's capabilities.
41
+
42
+ **`add(*args) -> Real`**
43
+ Tropical addition. Essentially an alias for Python's `min` function.
44
+ **`mult(*args) -> Real`**
45
+ Tropical multiplication. Essentially an alias for Python's `sum` function.
46
+ **`add_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
47
+ Tropical addition of NumPy arrays. The summed matrices have to be of the same shape.
48
+ **`mult_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
49
+ 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.
50
+ **`modulo(a : Real, t : int) -> Real`**
51
+ 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$.
52
+ **`modulo_matrices(A : np.ndarray, b : np.ndarray) -> np.ndarray`**
53
+ Tropical modulo operator for NumPy arrays. The input matrices should be of size MxN and Mx1. The result is an MxN matrix.
54
+ **`power(a : real, k : int) -> Real`**
55
+ Tropical power operator. Applies the multiplication k times.
56
+ **`power_matrix(A : np.ndarray, k : int) -> np.ndarray`**
57
+ Tropical power operator for NumPy arrays. It multiplies the matrix k times.
58
+ **`unit_matrix(width : int, height : int) -> np.ndarray`**
59
+ Creates a tropical unit matrix of given width and height.
60
+ **`star(A : np.ndarray) -> np.ndarray`**
61
+ 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.
62
+ **`Polynomial(*coefficients)`**
63
+ 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
+
65
+ ### Example code
66
+ ```
67
+ import numpy as np
68
+ from mplusa import minplus
69
+
70
+ # Basic operators
71
+ s = minplus.add(10, 6, 4, 13) # -> 4
72
+ p = minplus.mult(10, 5, 8) # -> 23
73
+ mod = minplus.modulo(p, s) # -> 3
74
+
75
+ # NumPy arrays
76
+ A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
77
+ B = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
78
+ S = minplus.add_matrices(A, B) # -> [[1, 2, 3], [4, 5, 4], [3, 2, 1]]
79
+ P = minplus.mult_matrices(A, B) # -> [[6, 5, 4], [9, 8, 7], [12, 11, 10]]
80
+ ```
81
+
82
+ ## Bibliography
83
+ 1. A. Obuchowicz, K. A. D'Souza, Z. A. Banaszak. *An Algebraic Approach to Modelling and Performance Optimisation of a Traffic Route System*. International Journal of Applied Mathematics and Computer Science, vol. 8, no. 2, pp. 335-365, June 1998.
84
+ 2. D. Speyer, B. Sturmfels. "*Tropical Mathematics*". Mathematics Magazine, vol. 82, no. 3, pp. 163-173, June 2009, doi: https://doi.org/10.4169%2F193009809x468760.
mplusa-0.0.1/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # MPlusA
2
+ ---
3
+ **MPlusA** is a small Python library for tropical algebra (also known as $(\min, +)$ and $(\max, +)$ algebra). It provides the definitions of basic operations on numbers and NumPy arrays, as well as a basic implementation of tropical polynomials.
4
+
5
+ Any improvements or fixes are always welcome.
6
+
7
+ ## How to use
8
+ After having installed the library one can import one of the two modules the package consists of (`minplus` and `maxplus`) and use the full array of its capabilities. The functions are essentially the same between the modules. The list below is a full list of the library's capabilities.
9
+
10
+ **`add(*args) -> Real`**
11
+ Tropical addition. Essentially an alias for Python's `min` function.
12
+ **`mult(*args) -> Real`**
13
+ Tropical multiplication. Essentially an alias for Python's `sum` function.
14
+ **`add_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
15
+ Tropical addition of NumPy arrays. The summed matrices have to be of the same shape.
16
+ **`mult_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
17
+ 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.
18
+ **`modulo(a : Real, t : int) -> Real`**
19
+ 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$.
20
+ **`modulo_matrices(A : np.ndarray, b : np.ndarray) -> np.ndarray`**
21
+ Tropical modulo operator for NumPy arrays. The input matrices should be of size MxN and Mx1. The result is an MxN matrix.
22
+ **`power(a : real, k : int) -> Real`**
23
+ Tropical power operator. Applies the multiplication k times.
24
+ **`power_matrix(A : np.ndarray, k : int) -> np.ndarray`**
25
+ Tropical power operator for NumPy arrays. It multiplies the matrix k times.
26
+ **`unit_matrix(width : int, height : int) -> np.ndarray`**
27
+ Creates a tropical unit matrix of given width and height.
28
+ **`star(A : np.ndarray) -> np.ndarray`**
29
+ 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.
30
+ **`Polynomial(*coefficients)`**
31
+ 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.
32
+
33
+ ### Example code
34
+ ```
35
+ import numpy as np
36
+ from mplusa import minplus
37
+
38
+ # Basic operators
39
+ s = minplus.add(10, 6, 4, 13) # -> 4
40
+ p = minplus.mult(10, 5, 8) # -> 23
41
+ mod = minplus.modulo(p, s) # -> 3
42
+
43
+ # NumPy arrays
44
+ A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
45
+ B = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
46
+ S = minplus.add_matrices(A, B) # -> [[1, 2, 3], [4, 5, 4], [3, 2, 1]]
47
+ P = minplus.mult_matrices(A, B) # -> [[6, 5, 4], [9, 8, 7], [12, 11, 10]]
48
+ ```
49
+
50
+ ## Bibliography
51
+ 1. A. Obuchowicz, K. A. D'Souza, Z. A. Banaszak. *An Algebraic Approach to Modelling and Performance Optimisation of a Traffic Route System*. International Journal of Applied Mathematics and Computer Science, vol. 8, no. 2, pp. 335-365, June 1998.
52
+ 2. D. Speyer, B. Sturmfels. "*Tropical Mathematics*". Mathematics Magazine, vol. 82, no. 3, pp. 163-173, June 2009, doi: https://doi.org/10.4169%2F193009809x468760.
@@ -0,0 +1,20 @@
1
+ [project]
2
+ name = "mplusa"
3
+ version = "0.0.1"
4
+ authors = [
5
+ {name="Maksymilian W.", email="maksymilian3563@gmail.com"}
6
+ ]
7
+ description = "A library for calculations in tropical and arctic semirings."
8
+ readme = "README.md"
9
+ requires-python = ">=3.11"
10
+ dependencies = [
11
+ "numpy>=2.2.3"
12
+ ]
13
+ classifiers = [
14
+ "Programming Language :: Python :: 3",
15
+ "Operating System :: OS Independent",
16
+ ]
17
+ license = {file="LICENSE"}
18
+
19
+ [project.urls]
20
+ homepage = "https://github.com/Hadelekw/mplusa"
mplusa-0.0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,2 @@
1
+ from . import maxplus
2
+ from . import minplus
@@ -0,0 +1,161 @@
1
+ import math
2
+ import numpy as np
3
+ from numbers import Real
4
+
5
+
6
+ def add(*args) -> Real:
7
+ return max(args)
8
+
9
+
10
+ def mult(*args) -> Real:
11
+ return sum(args) if -math.inf not in args else -math.inf
12
+
13
+
14
+ def add_matrices(A : np.ndarray,
15
+ B : np.ndarray) -> np.ndarray:
16
+ if A.shape != B.shape:
17
+ raise ValueError(
18
+ 'Maxplus.add_matrices: given matrices ' +\
19
+ 'are of different shape (A: {}, B: {}).'.format(A.shape, B.shape)
20
+ )
21
+ result = np.copy(A)
22
+ shape = A.shape
23
+ for i in range(shape[0]):
24
+ for j in range(shape[1]):
25
+ result[i, j] = add(result[i, j], B[i, j])
26
+ return result
27
+
28
+
29
+ def mult_matrices(A : np.ndarray,
30
+ B : np.ndarray) -> np.ndarray:
31
+ if A.shape[1] != B.shape[0]:
32
+ raise ValueError(
33
+ 'Maxplus.mult_matrices: given matrices ' +\
34
+ 'are of shapes not given as MxN and NxP (A: {}, B: {}).'.format(
35
+ A.shape, B.shape
36
+ )
37
+ )
38
+ result = np.zeros((A.shape[0], B.shape[1]))
39
+ for i in range(A.shape[0]):
40
+ for j in range(B.shape[1]):
41
+ result[i, j] = add(*[mult(A[i, k], B[k, j]) for k in range(A.shape[1])])
42
+ return result
43
+
44
+
45
+ def modulo(a : Real,
46
+ t : int) -> Real:
47
+ if a == -math.inf:
48
+ return -math.inf
49
+ if a == 0:
50
+ return 0
51
+ if t == -math.inf or t == 0:
52
+ return a
53
+ return a - (a // t) * t
54
+
55
+
56
+ def modulo_matrices(A : np.ndarray,
57
+ b : np.ndarray) -> np.ndarray:
58
+ if b.shape[1] != 1:
59
+ raise ValueError(
60
+ 'Maxplus.modulo_matrices: given matrix b ' +\
61
+ 'is not a properly formated vector (has shape of {}).'.format(
62
+ b.shape
63
+ )
64
+ )
65
+ if A.shape[0] != b.shape[0]:
66
+ raise ValueError(
67
+ 'Maxplus.modulo_matrices: given matrix b ' +\
68
+ 'does not have an Mx1 shape against MxN matrix A (A: {}, b: {}).'.format(
69
+ A.shape, b.shape
70
+ )
71
+ )
72
+ if np.any(A < 0) or np.any(b < 0):
73
+ raise ValueError(
74
+ 'Maxplus.modulo_matrices: matrices contain negative values.'
75
+ )
76
+ result = np.zeros(A.shape)
77
+ for i in range(A.shape[0]):
78
+ for j in range(A.shape[1]):
79
+ result[i, j] = modulo(A[i, j], b[i])
80
+ return result
81
+
82
+
83
+ def power(a : Real,
84
+ k : int) -> Real:
85
+ return mult(*[a for _ in range(k)])
86
+
87
+
88
+ def power_matrix(A : np.ndarray,
89
+ k : int) -> np.ndarray:
90
+ if np.any(np.diagonal(A) != 0):
91
+ raise ValueError(
92
+ 'Maxplus.power_matrix: matrix contains non-zero values on diagonal.'
93
+ )
94
+ if k == 0:
95
+ result = unit_matrix(A.shape[0], A.shape[1])
96
+ else:
97
+ result = A.copy()
98
+ for _ in range(k):
99
+ result = mult_matrices(A, result)
100
+ return result
101
+
102
+
103
+ def unit_matrix(width : int,
104
+ height : int) -> np.ndarray:
105
+ if width < 0 or height < 0:
106
+ raise ValueError(
107
+ 'Maxplus.unit_matrix: invalid width or height.'
108
+ )
109
+ result = np.eye(width, height)
110
+ result[result == 0] = -math.inf
111
+ result[result == 1] = 0
112
+ return result
113
+
114
+
115
+ def star(A : np.ndarray,
116
+ iterations : int = 1000,
117
+ eps : float = 0.001) -> np.ndarray:
118
+ if A.shape[0] != A.shape[1]:
119
+ raise ValueError(
120
+ 'Maxplus.star: matrix is not square.'
121
+ )
122
+ series = [
123
+ unit_matrix(A.shape[0], A.shape[1]),
124
+ A.copy()
125
+ ]
126
+ for i in range(2, iterations):
127
+ series.append(add_matrices(series[-1], series[-2]))
128
+ # Very basic check if the series is convergent.
129
+ if abs(np.max(series[-1] - series[-2])) < eps:
130
+ break
131
+ else:
132
+ raise ValueError(
133
+ 'Maxplus.star: the series for this matrix is not convergent ' +\
134
+ '(within the limits of iterations and decimal places).'
135
+ )
136
+ return series[-1]
137
+
138
+
139
+ class Polynomial:
140
+ """ A simple implementation of a single-variable arctic polynomial. """
141
+
142
+ def __init__(self, *coefficients) -> None:
143
+ for value in coefficients:
144
+ if not isinstance(value, Real) or value == math.inf:
145
+ raise ValueError(
146
+ 'Maxplus.Polynomial.__init__: coefficient value out of domain.'
147
+ )
148
+ self.coefficients = coefficients[::-1]
149
+
150
+ def __call__(self, x : float) -> float:
151
+ return add(*[mult(coefficient, power(x, i)) for i, coefficient in enumerate(self.coefficients)])
152
+
153
+ def get_hypersurface(self) -> list[float]:
154
+ result = []
155
+ candidates = [c2 - c1 for c1, c2 in zip(self.coefficients[1:], self.coefficients[:-1])]
156
+ for candidate in candidates:
157
+ if not isinstance(candidate, Real) or candidate == math.inf:
158
+ continue
159
+ if abs(self(candidate) - self(candidate - 1)) != abs(self(candidate) - self(candidate + 1)):
160
+ result.append(candidate)
161
+ return result
@@ -0,0 +1,161 @@
1
+ import math
2
+ import numpy as np
3
+ from numbers import Real
4
+
5
+
6
+ def add(*args) -> Real:
7
+ return min(args)
8
+
9
+
10
+ def mult(*args) -> Real:
11
+ return sum(args) if math.inf not in args else math.inf
12
+
13
+
14
+ def add_matrices(A : np.ndarray,
15
+ B : np.ndarray) -> np.ndarray:
16
+ if A.shape != B.shape:
17
+ raise ValueError(
18
+ 'Minplus.add_matrices: given matrices ' +\
19
+ 'are of different shape (A: {}, B: {}).'.format(A.shape, B.shape)
20
+ )
21
+ result = np.copy(A)
22
+ shape = A.shape
23
+ for i in range(shape[0]):
24
+ for j in range(shape[1]):
25
+ result[i, j] = add(result[i, j], B[i, j])
26
+ return result
27
+
28
+
29
+ def mult_matrices(A : np.ndarray,
30
+ B : np.ndarray) -> np.ndarray:
31
+ if A.shape[1] != B.shape[0]:
32
+ raise ValueError(
33
+ 'Minplus.mult_matrices: given matrices ' +\
34
+ 'are of shapes not given as MxN and NxP (A: {}, B: {}).'.format(
35
+ A.shape, B.shape
36
+ )
37
+ )
38
+ result = np.zeros((A.shape[0], B.shape[1]))
39
+ for i in range(A.shape[0]):
40
+ for j in range(B.shape[1]):
41
+ result[i, j] = add(*[mult(A[i, k], B[k, j]) for k in range(A.shape[1])])
42
+ return result
43
+
44
+
45
+ def modulo(a : Real,
46
+ t : int) -> Real:
47
+ if a == math.inf:
48
+ return math.inf
49
+ if a == 0:
50
+ return 0
51
+ if t == math.inf or t == 0:
52
+ return a
53
+ return a - (a // t) * t
54
+
55
+
56
+ def modulo_matrices(A : np.ndarray,
57
+ b : np.ndarray) -> np.ndarray:
58
+ if b.shape[1] != 1:
59
+ raise ValueError(
60
+ 'Minplus.modulo_matrices: given matrix b ' +\
61
+ 'is not a properly formated vector (has shape of {}).'.format(
62
+ b.shape
63
+ )
64
+ )
65
+ if A.shape[0] != b.shape[0]:
66
+ raise ValueError(
67
+ 'Minplus.modulo_matrices: given matrix b ' +\
68
+ 'does not have an Mx1 shape against MxN matrix A (A: {}, b: {}).'.format(
69
+ A.shape, b.shape
70
+ )
71
+ )
72
+ if np.any(A < 0) or np.any(b < 0):
73
+ raise ValueError(
74
+ 'Minplus.modulo_matrices: matrices contain negative values.'
75
+ )
76
+ result = np.zeros(A.shape)
77
+ for i in range(A.shape[0]):
78
+ for j in range(A.shape[1]):
79
+ result[i, j] = modulo(A[i, j], b[i])
80
+ return result
81
+
82
+
83
+ def power(a : Real,
84
+ k : int) -> Real:
85
+ return mult(*[a for _ in range(k)])
86
+
87
+
88
+ def power_matrix(A : np.ndarray,
89
+ k : int) -> np.ndarray:
90
+ if np.any(np.diagonal(A) != 0):
91
+ raise ValueError(
92
+ 'Minplus.power_matrix: matrix contains non-zero values on diagonal.'
93
+ )
94
+ if k == 0:
95
+ result = unit_matrix(A.shape[0], A.shape[1])
96
+ else:
97
+ result = A.copy()
98
+ for _ in range(k):
99
+ result = mult_matrices(A, result)
100
+ return result
101
+
102
+
103
+ def unit_matrix(width : int,
104
+ height : int) -> np.ndarray:
105
+ if width < 0 or height < 0:
106
+ raise ValueError(
107
+ 'Minplus.unit_matrix: invalid width or height.'
108
+ )
109
+ result = np.eye(width, height)
110
+ result[result == 0] = math.inf
111
+ result[result == 1] = 0
112
+ return result
113
+
114
+
115
+ def star(A : np.ndarray,
116
+ iterations : int = 1000,
117
+ eps : float = 0.001) -> np.ndarray:
118
+ if A.shape[0] != A.shape[1]:
119
+ raise ValueError(
120
+ 'Minplus.star: matrix is not square.'
121
+ )
122
+ series = [
123
+ unit_matrix(A.shape[0], A.shape[1]),
124
+ A.copy()
125
+ ]
126
+ for i in range(2, iterations):
127
+ series.append(add_matrices(series[-1], series[-2]))
128
+ # Very basic check if the series is convergent.
129
+ if abs(np.max(series[-1] - series[-2])) < eps:
130
+ break
131
+ else:
132
+ raise ValueError(
133
+ 'Minplus.star: the series for this matrix is not convergent ' +\
134
+ '(within the limits of iterations and decimal places).'
135
+ )
136
+ return series[-1]
137
+
138
+
139
+ class Polynomial:
140
+ """ A simple implementation of a single-variable tropical polynomial. """
141
+
142
+ def __init__(self, *coefficients) -> None:
143
+ for value in coefficients:
144
+ if not isinstance(value, Real) or value == -math.inf:
145
+ raise ValueError(
146
+ 'Minplus.Polynomial.__init__: coefficient value out of domain.'
147
+ )
148
+ self.coefficients = coefficients[::-1]
149
+
150
+ def __call__(self, x : float) -> float:
151
+ return add(*[mult(coefficient, power(x, i)) for i, coefficient in enumerate(self.coefficients)])
152
+
153
+ def get_hypersurface(self) -> list[float]:
154
+ result = []
155
+ candidates = [c2 - c1 for c1, c2 in zip(self.coefficients[1:], self.coefficients[:-1])]
156
+ for candidate in candidates:
157
+ if not isinstance(candidate, Real) or candidate == -math.inf:
158
+ continue
159
+ if abs(self(candidate) - self(candidate - 1)) != abs(self(candidate) - self(candidate + 1)):
160
+ result.append(candidate)
161
+ return result
@@ -0,0 +1,84 @@
1
+ Metadata-Version: 2.2
2
+ Name: mplusa
3
+ Version: 0.0.1
4
+ Summary: A library for calculations in tropical and arctic semirings.
5
+ Author-email: "Maksymilian W." <maksymilian3563@gmail.com>
6
+ License: Copyright (c) 2025 Maksymilian Wiekiera
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+ Project-URL: homepage, https://github.com/Hadelekw/mplusa
26
+ Classifier: Programming Language :: Python :: 3
27
+ Classifier: Operating System :: OS Independent
28
+ Requires-Python: >=3.11
29
+ Description-Content-Type: text/markdown
30
+ License-File: LICENSE
31
+ Requires-Dist: numpy>=2.2.3
32
+
33
+ # MPlusA
34
+ ---
35
+ **MPlusA** is a small Python library for tropical algebra (also known as $(\min, +)$ and $(\max, +)$ algebra). It provides the definitions of basic operations on numbers and NumPy arrays, as well as a basic implementation of tropical polynomials.
36
+
37
+ Any improvements or fixes are always welcome.
38
+
39
+ ## How to use
40
+ After having installed the library one can import one of the two modules the package consists of (`minplus` and `maxplus`) and use the full array of its capabilities. The functions are essentially the same between the modules. The list below is a full list of the library's capabilities.
41
+
42
+ **`add(*args) -> Real`**
43
+ Tropical addition. Essentially an alias for Python's `min` function.
44
+ **`mult(*args) -> Real`**
45
+ Tropical multiplication. Essentially an alias for Python's `sum` function.
46
+ **`add_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
47
+ Tropical addition of NumPy arrays. The summed matrices have to be of the same shape.
48
+ **`mult_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
49
+ 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.
50
+ **`modulo(a : Real, t : int) -> Real`**
51
+ 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$.
52
+ **`modulo_matrices(A : np.ndarray, b : np.ndarray) -> np.ndarray`**
53
+ Tropical modulo operator for NumPy arrays. The input matrices should be of size MxN and Mx1. The result is an MxN matrix.
54
+ **`power(a : real, k : int) -> Real`**
55
+ Tropical power operator. Applies the multiplication k times.
56
+ **`power_matrix(A : np.ndarray, k : int) -> np.ndarray`**
57
+ Tropical power operator for NumPy arrays. It multiplies the matrix k times.
58
+ **`unit_matrix(width : int, height : int) -> np.ndarray`**
59
+ Creates a tropical unit matrix of given width and height.
60
+ **`star(A : np.ndarray) -> np.ndarray`**
61
+ 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.
62
+ **`Polynomial(*coefficients)`**
63
+ 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
+
65
+ ### Example code
66
+ ```
67
+ import numpy as np
68
+ from mplusa import minplus
69
+
70
+ # Basic operators
71
+ s = minplus.add(10, 6, 4, 13) # -> 4
72
+ p = minplus.mult(10, 5, 8) # -> 23
73
+ mod = minplus.modulo(p, s) # -> 3
74
+
75
+ # NumPy arrays
76
+ A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
77
+ B = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
78
+ S = minplus.add_matrices(A, B) # -> [[1, 2, 3], [4, 5, 4], [3, 2, 1]]
79
+ P = minplus.mult_matrices(A, B) # -> [[6, 5, 4], [9, 8, 7], [12, 11, 10]]
80
+ ```
81
+
82
+ ## Bibliography
83
+ 1. A. Obuchowicz, K. A. D'Souza, Z. A. Banaszak. *An Algebraic Approach to Modelling and Performance Optimisation of a Traffic Route System*. International Journal of Applied Mathematics and Computer Science, vol. 8, no. 2, pp. 335-365, June 1998.
84
+ 2. D. Speyer, B. Sturmfels. "*Tropical Mathematics*". Mathematics Magazine, vol. 82, no. 3, pp. 163-173, June 2009, doi: https://doi.org/10.4169%2F193009809x468760.
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/mplusa/__init__.py
5
+ src/mplusa/maxplus.py
6
+ src/mplusa/minplus.py
7
+ src/mplusa.egg-info/PKG-INFO
8
+ src/mplusa.egg-info/SOURCES.txt
9
+ src/mplusa.egg-info/dependency_links.txt
10
+ src/mplusa.egg-info/requires.txt
11
+ src/mplusa.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ numpy>=2.2.3
@@ -0,0 +1 @@
1
+ mplusa