Skip to main content

A library for calculations in tropical algebra.

Project description

MPlusA

MPlusA is a Python library for calculations in tropical algebra (also known as (min, +) and (max, +) algebra). For the full list of the library's capabilities refer to the documentation.

Contributing

The library is open for all contributions. If you want to contribute to its development, please refer to the guidelines to verify the code standards before making a pull request. Developments from all areas of tropical mathematics are welcome.

Installation

The easiest way to install the library is to use pip.

pip install mplusa

The only automatically installed dependency is NumPy. Otherwise the library does make use of Matplotlib for visualisations but it is not a required dependency and needs to be installed separately in case the user wants to use these capabilities.

Examples

For the full list of the library's capabilities, refer to the documentation documentation. This section is only meant to highlight the core features of the library.

Basic operations

 # import maxplus instead of minplus to use the (max, +) semiring
from mplusa import minplus as mpa
import numpy as np

print(mpa.add(3, 5, 89))  # -> 3
print(mpa.mult(4, 8, 9))  # -> 21
print(mpa.power(10, 89))  # -> 890
print(mpa.modulo(10, 3))  # -> 1

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
])
B = np.array([
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1],
])

print(mpa.add_matrices(A, B))  # -> [[1, 2, 3], [4, 5, 4], [3, 2, 1]]
print(mpa.mult_matrices(A, B))  # -> [[6, 5, 4], [9, 8, 7], [12, 11, 10]]
print(mpa.power_matrix(A, 3))  # -> [[3, 4, 5], [6, 7, 8], [9, 10, 11]]
print(mpa.modulo_matrices(A, np.array([[1, 2, 3]]).T))  # -> [[0, 0, 0], [0, 1, 0], [1, 2, 0]]

Matrix-specific operations

 # import maxplus instead of minplus to use the (max, +) semiring
from mplusa import minplus as mpa
import numpy as np

A = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
])

print(mpa.kleene_star(A))  # -> [[0, 2, 3], [4, 0, 6], [7, 8, 0]]
print(mpa.kleene_star_series(A))  # -> all the matrices generated during kleene star
print(mpa.kleene_plus(A))  # -> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(mpa.kleene_plus_series(A))  # -> all the matrices generated during kleene plus

print(mpa.tdet(A))  # -> 15
print(mpa.is_matrix_singular(A))  # -> True
print(mpa.matrix_rank_tropical(A))  # -> 1

# This function takes an argument func which accepts the following methods: 
# 1. mpa.power_algorithm (the default value)
# 2. mpa.karp_algorihtm
print(mpa.eigenvalue(A))  # -> 1
# This function takes an argument func which accepts the following methods:
# 1. mpa.power_algorithm (the default value)
# 2. mpa.kleene_star_algorithm
# 3. mpa.modified_kleene_star_algorithm
print(mpa.eigenvector(A))  # -> [[0], [3], [6]]

Polynomials

 # import maxplus instead of minplus to use the (max, +) semiring
from mplusa import minplus as mpa
import numpy as np

p = mpa.MultivariatePolynomial(np.array([
    [1, np.inf, 15],
    [5, 0, np.inf],
    [5, 4, 3],
]))

print(p)  # -> (1) + (∞ * b) + (15 * b^2) + (5 * a) + (0 * a * b) + (∞ * a * b^2) + (5 * a^2) + (4 * a^2 * b) + (3 * a^2 * b^2)
print(p(8, 5))  # -> 1
print(p.get_linear_hyperplanes())  # -> [[1, 0, 0, 1], [15, 0, 2, 1], [5, 1, 0, 1], [0, 1, 1, 1], [5, 2, 0, 1], [4, 2, 1, 1], [3, 2, 2, 1]]

# A polynomial with one variable
p = mpa.Polynomial(5, 8, 9)

print(p)  # -> (5) + (8 * a) + (9 * a^2)
print(p(8))  # -> 5
print(p.get_linear_hyperplanes())  # -> [[5, 0, 1], [8, 1, 1], [9, 2, 1]]
print(p.get_line_intersections())  # -> [(-2.0, 5.0)]
print(p.get_roots())  # -> ([-2.0], [1]) where the first value is the root and the latter is its corresponding rank

Geometry and visualizations

 # import maxplus instead of minplus to use the (max, +) semiring
from mplusa import minplus as mpa
import numpy as np
# For visualizations matplotlib is required and is imported inside of the submodule
# By default drawing polytopes and hyperplanes won't display the figure and that is
# why it is imported here; it's not necessary if a figure is displayed using show=True
import matplotlib.pyplot as plt

# NOTE: the library uses the first element in a point for projections; it should usually be 0
p = mpa.geometry.Polytope2D((0, 1, 2), (0, 3, 3), (0, 4, 5))

print(p.vertices)  # -> [[0, 1, 2], [0, 3, 3], [0, 4, 5]]
print(p.pseudovertices)  # -> [[0, 2, 3], [0, 4, 4]]
print(p.edges)  # -> [[[0, 1, 2], [0, 3, 3]], [[0, 3, 3], [0, 4, 5]], [[0, 4, 5], [0, 1, 2]]]

print(p.get_all_line_segments())  # -> point by point edges (including pseudovertices)

mpa.visualize.hasse_diagram(p)
mpa.visualize.draw_polytope2D(p, show=True)

for hyperplane in p.get_hyperplanes():
    mpa.visualize.draw_hyperplane2d(hyperplane)
plt.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mplusa-0.0.5.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mplusa-0.0.5-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file mplusa-0.0.5.tar.gz.

File metadata

  • Download URL: mplusa-0.0.5.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for mplusa-0.0.5.tar.gz
Algorithm Hash digest
SHA256 e432599a4548dcce511d558c308152b8147cd5dde41511c7b4a46af3fa77718a
MD5 5696d5b803b6af3f372dbadae4a9aa28
BLAKE2b-256 9b9391d5156ef4a2efb67102da9c27074bbf2af74acefd57c345149ec80a6b05

See more details on using hashes here.

File details

Details for the file mplusa-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: mplusa-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for mplusa-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ac536d310b95b9cf9dce6b12c252303f50552209ebeffa972130bf981a97d767
MD5 6dcb16b96df084e5881a131b6352fe90
BLAKE2b-256 078ed2e135db29dccb7112d7c9b3c8f5d4f2b26574312b8224bee21aea5f6e18

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page