MPlusA
A small Python library for calculations in tropical algebra.
Installation
MPlusA was initially created in Python 3.11 and was developed to be independent from any operating system specifics. It is believed that the library would work for previous versions of Python3 but it was not tested.
To install MPlusA in your Python virtual environment use pip:
$ pip install mplusa
Function list
add(*args)
An implementation of the addition operation in tropical algebra. In the minplus package it is defined as: $$ \forall a, b \in \mathbb{R}_{\min}: a \oplus b := \min\{a, b\} $$
- Parameters
-
- *args : float: numerical arguments within the domain.
- Returns
-
- result : float: the minimum.
mult(*args)
An implementation of the multiplication operation in tropical algebra.
$$ \forall a, b \in \mathbb{R}_{\min}: a \otimes b := a + b $$- Parameters
-
- *args : float: numerical arguments within the domain.
- Returns
-
- result : float: the sum.
power(a, k)
An implementation of the power operation in tropical algebra.
$$ \forall a \in \mathbb{R}_{\min}, k \in \mathbb{N}: a_{\otimes}^{k} := \bigotimes_{i=1}^{k}a $$- Parameters
-
- a : float: a number within the domain.
- k : int: an integer value of the exponent.
- Returns
-
- result : float: the product.
modulo(a, t)
An implementation of the modulo operation in tropical algebra. Based on the definition from [1]:
$$\forall a,t \in [0, \infty) \exists b \in [0, \infty): a \diamond t = b \iff a = b \otimes t_{\otimes}^{k}$$ $$\forall a \in [0, \infty): a \diamond \infty = a; \quad \forall a \in [e, \infty): \infty \diamond a = \infty$$- Parameters
-
- a : float: a positive number within the domain.
- t : int: an positive integer value of the exponent.
- Returns
-
- result : float: the remainder.
add_matrices(A, B)
An implementation of parallel composition (sum) of matrices of the same size.
$$ \left(\mathbf{A} \oplus \mathbf{B}\right)_{ij} := \mathbf{A}_{ij} \oplus \mathbf{B}_{ij} $$- Parameters
-
- A : numpy.ndarray: a NumPy array.
- B : numpy.ndarray: a NumPy array of the same size as A.
- Returns
-
- result : numpy.ndarray: a NumPy array with minimal entries.
mult_matrices(A, B)
An implementation of series composition (product) of matrices of sizes MxN and NxP.
$$ \left(\mathbf{A} \otimes \mathbf{B}\right)_{ij} := \bigoplus_{k=1}^{N}\left(\mathbf{A}_{ik} \otimes \mathbf{B}_{kj}\right) $$- Parameters
-
- A : numpy.ndarray: a NumPy array of size MxN.
- B : numpy.ndarray: a NumPy array of size NxP.
- Returns
-
- result : numpy.ndarray: a NumPy array of size MxP.
power_matrix(A, k)
An implementation of multiple multiplication on a matrix. By definition from [1]:
$$ \exists k_0 \forall k \geq k_0: \mathbf{A}_{\otimes}^{k + 1} := \mathbf{A}_{\otimes}^{k} $$ $$ \mathbf{A}_{\otimes}^{k} = \mathbf{A} \otimes \mathbf{A}_{\otimes}^{k - 1}, \quad \mathbf{A}_{\otimes}^{0} = \mathbf{E} $$with E being a tropical unit matrix. It applies if A is a matrix with non-negative entries and its diagonal elements are equal to the tropical identity element.
- Parameters
-
- A : numpy.ndarray: a NumPy array.
- k : int: an integer value of the exponent.
- Returns
-
- result : numpy.ndarray: a NumPy array.
modulo_matrices(A, b)
An implementation of the modulo operation for matrices according to [1]. If A is an MxN matrix with non-negative entries and b is an m-element vector with non-negative entries, then B is an MxN matrix defined by:
$$ \mathbf{B}_{ij} := \mathbf{A}_{ij} \diamond \mathbf{b}_{i} $$- Parameters
-
- A : numpy.ndarray: a NumPy array.
- b : numpy.ndarray: a vertical NumPy array of size Mx1.
- Returns
-
- result : numpy.ndarray: a NumPy array of remainders.
unit_matrix(width, height)
Generates a two dimensional tropical unit matrix. In the minplus package it is defined as:
$$ \mathbf{E}_{ij} := \begin{cases} 0, &i = j\\ \infty, &i \neq j \end{cases}$$- Parameters
-
- width : int: the width of the unit matrix.
- height : int: the height of the unit matrix.
- Returns
-
- result : numpy.ndarray: a NumPy array tropical unit matrix.
kleene_star(A, iterations)
An implementation of Kleene star operator for tropical algebra.
$$ \mathbf{A}^{*} := \bigoplus_{k=1}^{\infty} \mathbf{A}_{\otimes}^{k}$$As of version 0.0.3 this function does not check the convergence of the series.
- Parameters
-
- A : numpy.ndarray: a square NumPy array.
- iterations : int: an arbitrary amount of iterations to calculate the series (default 1000).
- Returns
-
- result : numpy.ndarray: a NumPy array.
Class list
MultivariatePolynomial(coefficients)
An implementation of a multivariate tropical polynomial.
- Constructor parameters
-
- coefficients : numpy.ndarray: a NumPy array representing coefficients with one dimension being a new variable, i.e. a 2-dimensional 3x3 array would represent a polynomial of 2 variables and their combinations depending on their position in the array (the indices represent the powers in the polynomial).
- Functions
-
MultivariatePolynomial.__call__(*variables)
- Parameters
-
- *variables : float: a sequence of values within the domain to calculate the value of the polynomial for.
- Returns
-
- result : float: the value of the polynomial at given point.
MultivariatePolynomial.__str__()
- Returns
-
- result : str: a written notation of the polynomial.
MultivariatePolynomial.get_hyperplanes()
- Returns
-
- result : list: a list of linear coefficients for the hyperplanes building the polynomial.
Polynomial(*coefficients)
Inherits from MultivariatePolynomial
An implementation of a single variable tropical polynomial.
- Constructor parameters
-
- *coefficients : float: values to be used as coefficients.
- Functions
-
Polynomial.get_line_intersections()
- Returns
-
- result : list: a list of points where the lines building the polynomial intersect.
Polynomial.get_roots()
- Returns
-
- roots : list: a list of the roots' values.
- ranks : list: a list of the corresponding ranks (amount of monomials attaining the value).
Example code
import mplusa.minplus as mp
import numpy as np
A, B = numpy.random.rand(5, 5), numpy.random.rand(5, 5)
C = mp.add_matrices(A, B)
Bibliography
[1] Obuchowicz, A., D'Souza, K. A., Banaszak, Z. A. (1998). An Algebraic Approach to Modelling and Performance Optimisation of a Traffic Route System. International Journal of Applied Mathematics and Computer Science, 8(2), 335-365.
[2] Speyer, D., Sturmfels, B. (2009). Tropical Mathematics. Mathematics Magazine, 82(3), 163-173.
[3] Carnia, E., Wilopo, R., Napitupulu, H., Anggriani, N., Supriatna, A. K. (2023). Modified Kleene Star Algorithm Using Max-Plus Algebra and Its Application in the Railroad Scheduling Graphical User Interface. Computation, 11(1), 11.