Skip to main content

FMatx

中文 | English

FMatx, as f(ull accuraty) mat(rix), is a library written in pure python determined to resolve Linear algebra and Linear programming problems in a fast and easy way with keeping high quality.

Features

  • Implemented in Pure Python with no third parties requires
  • Use simple global context manager to handle actions
  • Support more than API for users including determinant, RREF, linear programming (also ilp), simplex method, and more

Installation

Stable version:

pip install -U fmatx

Development version:

pip install -U git+https://github.com/hibays/fmatx.git

Prerequisites: FMatx requires Python 3.8 or newer

Basic usage

Import basic core class.

>>> from fmatx import Mat

Import global context manager.

>>> from fmatx import mst

Matrix Create

>>> Mat([9, 7, 3]) # vectors

>>> Mat().tolist()
[]

>>> Mat(3).tolist()
[[], [], []]

>>> Mat([[1, 2], [3, 4]])
Mat(
┌ 1 2 ┐
└ 1 2 ┘)

>>> Mat([(1, 2), (3, 4)])
Mat(
┌ 1 2 ┐
└ 1 2 ┘)

>>> Mat(((1, 2), (3, 4))) # to use it all elements have to be tuple
Mat(
┌ 1 2 ┐
└ 1 2 ┘)

Convenient advanced functions are available for creating various standard matrices, see zeros, ones, diag, eye, randmat and hilbert.

>>> eye(5)
Mat(
┌ 1  0  0  0  0 ┐
│ 0  1  0  0  0 │
│ 0  0  1  0  0 │
│ 0  0  0  1  0 │
└ 0  0  0  0  1 ┘)

Matrix operators

>>> a = Mat([[1, 2, 2,  2], [2, 4, 6,  8], [3, 6, 8, 10]])
>>> a
Mat(
┌ 1  2  2   2 ┐
│ 2  4  6   8 │
└ 3  6  8  10 ┘)
>>> 6*a/5 + 1 - 4 # full accuraty calculation
Mat(
┌ -9/5  -3/5  -3/5  -3/5 ┐
│ -3/5   9/5  21/5  33/5 │
└  3/5  21/5  33/5     9 ┘)

also calculates between matrix and matrix

>>> a + a
Mat(
┌ 2   4   4   4 ┐
│ 4   8  12  16 │
└ 6  12  16  20 ┘)

You can raise powers of square matrices.

>>> A = Mat([[7, 9], [8, 5]])
>>> A**2
Mat(
┌ 121  108 ┐
└  96   97 ┘)

Negative powers will calculate the inverse.

>>> A**-1
Mat(
┌ -5/37   9/37 ┐
└  8/37  -7/37 ┘)
>>> A * A**-1
Mat(
┌ 1  0 ┐
└ 0  1 ┘)

Matrix transposition is straightforward. Also conjugate transposition via Mat.H.

>>> A = ones(2, 3)
>>> A
Mat(
┌ 1  1  1 ┐
└ 1  1  1 ┘)
>>> A.T
Mat(
┌ 1  1 ┐
│ 1  1 │
└ 1  1 ┘)
>>> A.H
Mat(
┌ 1  1 ┐
│ 1  1 │
└ 1  1 ┘)

Linear Algebra

RREF return tuple(rref of matrix, pivot_cols)

>>> a.rref()
(Mat(
┌ 1  2  0  -2 ┐
│ 0  0  1   2 │
└ 0  0  0   0 ┘), (0, 2))

To compute determinant.

>>> a.det()
0

Note: Bareiss algorithm is used to compute determinant internally. So following operation is ok.

>>> mst.accuracy_protect = None # don't use full accuracy to compute
	
>>> Mat(
... [[ 67,  68,  -3,  71],
...  [ 75,  27, -21,  30],
...  [104, -34,  46, 163],
...  [165, 110, 144,  25]])
Mat(
┌  67   68   -3   71 ┐
│  75   27  -21   30 │
│ 104  -34   46  163 │
└ 165  110  144   25 ┘)

>>> _.det()
139971819.0

To compute Nullspace and column space.

>>> a.nullspace()
[Mat(
┌ -2 ┐
│  1 │
│  0 │
└  0 ┘), Mat(
┌  2 ┐
│  0 │
│ -2 │
└  1 ┘)]

>>> a.columnspace()
[Mat(
┌ 1 ┐
│ 0 │
└ 0 ┘), Mat(
┌ 0 ┐
│ 1 │
└ 0 ┘)]

To compute Rank (using Gauss Method).

>>> a.rank()
2

Linear Programming (Also ILP)

It provided several methods that can solve LP, MLP, and ILP problems in full accuracy.

Example 1: solving following LP question using simplex method.

max z = 2x + y
	subject to
		 5y <= 15
	6x + 2y <= 24
	 x +  y <= 5
		x,y >= 0

>>> C = Mat([2, 1])
>>> A = Mat([[0, 5], [6, 2], [1, 1]])
>>> b = Mat([15, 24, 5])

>>> C.simplex(A, b, maximize=True)
(Rational(17, 2), {1: Rational(7, 2), 2: Rational(3, 2)})

Example 2: solving following ILP question using branch and bound method.

max z = 2x + y
	subject to
		 5y <= 15
	6x + 2y <= 24
	 x +  y <= 5
		x,y >= 0
		x,y ∈ Integers
		
>>> C = Mat([2, 1])
>>> A = Mat([[0, 5], [6, 2], [1, 1]])
>>> b = Mat([15, 24, 5])

>>> C.branch_bound(A, b, maximize=True)
(Rational(8, 1), {1: Rational(4, 1), 2: Rational(0, 1)})

Context manager

Testing

Note: To run testsuite, you must ensure your python had mpmath and SymPy installed.

python -m pytest ./testing -v

Metadata

Release files for fmatx 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fmatx 0.2.1
File Size Uploaded Provenance
fmatx-0.2.1.tar.gz 65.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fmatx 0.2.1
File Interpreter ABI Platform
fmatx-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 113.6 kB

Release files / fmatx-0.2.1.tar.gz

Download URL fmatx-0.2.1.tar.gz
Size 65.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ec70e2f8df37f02172b74edf7da4ead04b24c6032f0fc7b906f9a1421542f553
BLAKE2b-256 checksum
How to use checksums
8665494e451609a0efb30c6be3d3a323334b2dc9f045e1178a7a97cce2ae55b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / fmatx-0.2.1-py3-none-any.whl

Download URL fmatx-0.2.1-py3-none-any.whl
Size 48.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dbc16eb62ccb2fa78a6894bf339f0d684dd9bd036777fe41949deb99130ca4e9
BLAKE2b-256 checksum
How to use checksums
abe3f26f7e55c64b074c3a413354d7ce7b1bb53b0f9d3ff81246a5928f8add34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Security for fmatx 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page