Skip to main content
logo

Variants of the Kaczmarz algorithm for solving linear systems

PyPI Version Supported Python Versions Build Status Code Coverage Code style: black

Installation

To install Kaczmarz Algorithms, run this command in your terminal:

$ pip install -U kaczmarz-algorithms

The -U argument is optional. It specifies that the most package should be upgraded to the most recent version if it is already installed.

If you don't have pip installed, these installation instructions can guide you through the process.

Usage

Import the package

>>> import kaczmarz

Define your system of equations (e.g. 3 * x0 + x1 = 9 and x0 + 2 * x1 = 8)

>>> A = [[3, 1],
...      [1, 2]]
>>> b = [9, 8]

Solve the system of equations using the Kaczmarz algorithm with a cyclic selection rule

>>> x = kaczmarz.Cyclic.solve(A, b)
>>> x
array([2., 3.])

Similarly, to solve the same system of equations using the max-distance selection rule

>>> x = kaczmarz.MaxDistance.solve(A, b)

For a complete list of selection strategies, check the docs. If your desired selection strategy is not provided, please open an issue with your suggestion!

Inspect the iterates

To access the iterates of the Kaczmarz algorithm use kaczmarz.SelectionStrategy.iterates(). For example,

>>> A = [[1, 0, 0],
...      [0, 1, 0],
...      [0, 0, 1]]
>>> b = [1, 1, 1]
>>> x0 = [0, 0, 0]  # Initial iterate
>>> for xk in kaczmarz.Cyclic.iterates(A, b, x0):
...     xk
array([0., 0., 0.])
array([1., 0., 0.])
array([1., 1., 0.])
array([1., 1., 1.])

Inspect the rows/equations used

You can access the row index used at each iteration as iterates.ik in the following example.

>>> iterates = kaczmarz.Cyclic.iterates(A, b, x0)
>>> for xk in iterates:
...     print("Row used:", iterates.ik)
Row used: -1
Row used: 0
Row used: 1
Row used: 2

The initial value of iterates.ik is -1, since no projections have been performed yet at the start of the algorithm.

Optional arguments

The solve() and iterates() functions take optional arguments of maxiter and tol to specify a limit on the number of iterations and the desired accuracy of the solution respectively.

Creating your own selection strategy

To implement a selection strategy of your own, inherit from kaczmarz.Base and implement the _select_row_index() method. For example, to implement a strategy which uses of the equations of your system in reverse cyclic order:

>>> class ReverseCyclic(kaczmarz.Base):
...     def __init__(self, A, *args, **kwargs):
...         super().__init__(A, *args, **kwargs)
...         self.n_rows = len(A)
...         self.row_index = None
...
...     def _select_row_index(self, xk):
...         if self.row_index is None:
...             self.row_index = self.n_rows
...         self.row_index = (self.row_index - 1) % self.n_rows
...         return self.row_index

Your new class will inherit solve() and iterates() class methods which work the same way as kaczmarz.SelectionStrategy.solve() and kaczmarz.SelectionStrategy.iterates() described above.

>>> iterates = ReverseCyclic.iterates(A, b, x0)
>>> for xk in iterates:
...     print("Row used:", iterates.ik)
...     print("Iterate:", xk)
Row used: -1
Iterate: [0. 0. 0.]
Row used: 2
Iterate: [0. 0. 1.]
Row used: 1
Iterate: [0. 1. 1.]
Row used: 0
Iterate: [1. 1. 1.]

Citing

If you use our code in an academic setting, please consider citing our code. You can find the appropriate DOI for whichever version you are using on zenodo.org.

Development

See CONTRIBUTING.md for information related to developing the code.

Metadata

Release files for kaczmarz-algorithms 0.8.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 kaczmarz-algorithms 0.8.1
File Size Uploaded Provenance Actions
kaczmarz-algorithms-0.8.1.tar.gz 66.4 kB Without provenance Details for kaczmarz-algorithms-0.8.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kaczmarz-algorithms 0.8.1
File Interpreter ABI Platform Actions
kaczmarz_algorithms-0.8.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for kaczmarz_algorithms-0.8.1-py2.py3-none-any.whl

Total release size: 78.2 kB

Release files / kaczmarz-algorithms-0.8.1.tar.gz

Download URL kaczmarz-algorithms-0.8.1.tar.gz
Size 66.4 kB
Tags Source
SHA-256 checksum
How to use checksums
30d30ee13cfc6958bec040ba8f130e0aef01ef629a4daf35df0350b206784cf6
BLAKE2b-256 checksum
How to use checksums
f01c9cc3eb559fdacd45ac21a2df19b20d2acbf1ee249efbf0cdac4246b982d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Release files / kaczmarz_algorithms-0.8.1-py2.py3-none-any.whl

Download URL kaczmarz_algorithms-0.8.1-py2.py3-none-any.whl
Size 11.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
77acc832d45b93aa097ecbc7fecc3b191165dd5c6e3e99acd45e419e2f30ec44
BLAKE2b-256 checksum
How to use checksums
a72f249829446f859319d53adaeaf16c36aac071439dcfa5e7bb508a918edeb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Security for kaczmarz-algorithms 0.8.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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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