mplusa 0.0.1__tar.gz → 0.0.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mplusa
3
- Version: 0.0.1
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
 
@@ -9,24 +9,34 @@ After having installed the library one can import one of the two modules the pac
9
9
 
10
10
  **`add(*args) -> Real`**
11
11
  Tropical addition. Essentially an alias for Python's `min` function.
12
+
12
13
  **`mult(*args) -> Real`**
13
14
  Tropical multiplication. Essentially an alias for Python's `sum` function.
15
+
14
16
  **`add_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
15
17
  Tropical addition of NumPy arrays. The summed matrices have to be of the same shape.
18
+
16
19
  **`mult_matrices(A : np.ndarray, B : np.ndarray) -> np.ndarray`**
17
20
  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.
21
+
18
22
  **`modulo(a : Real, t : int) -> Real`**
19
23
  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$.
24
+
20
25
  **`modulo_matrices(A : np.ndarray, b : np.ndarray) -> np.ndarray`**
21
26
  Tropical modulo operator for NumPy arrays. The input matrices should be of size MxN and Mx1. The result is an MxN matrix.
27
+
22
28
  **`power(a : real, k : int) -> Real`**
23
29
  Tropical power operator. Applies the multiplication k times.
30
+
24
31
  **`power_matrix(A : np.ndarray, k : int) -> np.ndarray`**
25
32
  Tropical power operator for NumPy arrays. It multiplies the matrix k times.
33
+
26
34
  **`unit_matrix(width : int, height : int) -> np.ndarray`**
27
35
  Creates a tropical unit matrix of given width and height.
36
+
28
37
  **`star(A : np.ndarray) -> np.ndarray`**
29
38
  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.
39
+
30
40
  **`Polynomial(*coefficients)`**
31
41
  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
42
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "mplusa"
3
- version = "0.0.1"
3
+ version = "0.0.2"
4
4
  authors = [
5
5
  {name="Maksymilian W.", email="maksymilian3563@gmail.com"}
6
6
  ]
@@ -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 properly formated vector (has shape of {}).'.format(
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
- 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
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))
@@ -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 properly formated vector (has shape of {}).'.format(
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
- 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
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.1
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
 
File without changes
File without changes
File without changes