Skip to main content

A Python package for abstract linear algebra

Project description

ablina

Documentation

https://ablina.readthedocs.io/en/latest

Installation

Ablina can be installed using pip:

pip install ablina

or by directly cloning the git repository:

git clone https://github.com/daniyal1249/ablina.git

and running the following in the cloned repo:

pip install .

Overview

>>> from ablina import *

Define a Vector Space

To define a subspace of $ℝ^n$ or $ℂ^n$, use fn

>>> V = fn('V', R, 3)
>>> print(V)
V (Subspace of R^3)
-------------------
Field      R
Identity   [0, 0, 0]
Basis      [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Dimension  3
Vector     [c0, c1, c2]

You can provide a list of constraints

>>> U = fn('U', R, 3, constraints=['v0 == 0', '2*v1 == v2'])
>>> print(U)
U (Subspace of R^3)
-------------------
Field      R
Identity   [0, 0, 0]
Basis      [[0, 1, 2]]
Dimension  1
Vector     [0, c0, 2*c0]

Or specify a basis

>>> W = fn('W', R, 3, basis=[[1, 0, 0], [0, 1, 0]])
>>> print(W)
W (Subspace of R^3)
-------------------
Field      R
Identity   [0, 0, 0]
Basis      [[1, 0, 0], [0, 1, 0]]
Dimension  2
Vector     [c0, c1, 0]

Operations with Vectors

Check whether a vector is an element of a vector space

>>> [1, 2, 0] in W
True
>>> [1, 2, 1] in W
False

Generate a random vector from a vector space

>>> U.vector()
[0, 2, 4]
>>> U.vector(arbitrary=True)
[0, c0, 2*c0]

Find the coordinate vector representation of a vector

>>> W.to_coordinate([1, 2, 0])
[1, 2]
>>> W.from_coordinate([1, 2])
[1, 2, 0]
>>> W.to_coordinate([1, 2, 0], basis=[[1, 1, 0], [1, -1, 0]])
[3/2, -1/2]

Check whether a list of vectors is linearly independent

>>> V.are_independent([1, 1, 0], [1, 0, 0])
True
>>> V.are_independent([1, 2, 3], [2, 4, 6])
False

Operations on Vector Spaces

Check for equality of two vector spaces

>>> U == W
False

Check whether a vector space is a subspace of another

>>> U.is_subspace(V)
True
>>> V.is_subspace(U)
False

Take the sum of two vector spaces

>>> X = U.sum(W)
>>> print(X)
U + W (Subspace of R^3)
-----------------------
Field      R
Identity   [0, 0, 0]
Basis      [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Dimension  3
Vector     [c0, c1, c2]

Take the intersection of two vector spaces

>>> X = U.intersection(W)
>>> print(X)
U ∩ W (Subspace of R^3)
-----------------------
Field      R
Identity   [0, 0, 0]
Basis      []
Dimension  0
Vector     [0, 0, 0]

Take the span of a list of vectors

>>> S = V.span('S', [1, 2, 3], [4, 5, 6])
>>> print(S)
S (Subspace of R^3)
-------------------
Field      R
Identity   [0, 0, 0]
Basis      [[1, 0, -1], [0, 1, 2]]
Dimension  2
Vector     [c0, c1, -c0 + 2*c1]

Define a Linear Map

>>> def mapping(vec):
>>>     return [vec[0], vec[1], 0]
>>>
>>> T = LinearMap('T', domain=V, codomain=W, mapping=mapping)
>>> print(T)
T : V -> W
----------
Field        R
Rank         2
Nullity      1
Injective?   False
Surjective?  True
Bijective?   False
Matrix       [[1, 0, 0], [0, 1, 0]]
>>> T([0, 0, 0])
[0, 0, 0]
>>> T([1, 2, 3])
[1, 2, 0]

Operations with Linear Maps

Find the image of a linear map

>>> im = T.image()
>>> print(im)
im(T) (Subspace of R^3)
-----------------------
Field      R
Identity   [0, 0, 0]
Basis      [[1, 0, 0], [0, 1, 0]]
Dimension  2
Vector     [c0, c1, 0]

Find the kernel of a linear map

>>> ker = T.kernel()
>>> print(ker)
ker(T) (Subspace of R^3)
------------------------
Field      R
Identity   [0, 0, 0]
Basis      [[0, 0, 1]]
Dimension  1
Vector     [0, 0, c0]

Define an Inner Product

Here we define the standard dot product

>>> def mapping(vec1, vec2):
>>>     return sum(i * j for i, j in zip(vec1, vec2))
>>>
>>> dot = InnerProduct('dot', vectorspace=V, mapping=mapping)
>>> print(dot)
dot : V x V -> R
----------------
Orthonormal Basis  [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Matrix             [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
>>> dot([1, 2, 3], [1, 2, 3])
14

Operations with Inner Products

Compute the norm of a vector

>>> dot.norm([1, 2, 3])
sqrt(14)

Check whether two vectors are orthogonal

>>> dot.are_orthogonal([1, 2, 3], [4, 5, 6])
False
>>> dot.are_orthogonal([0, 0, 0], [1, 2, 3])
True

Check whether a list of vectors is orthonormal

>>> dot.are_orthonormal([1, 0, 0], [0, 1, 0], [0, 0, 1])
True

Take the orthogonal complement of a vector space

>>> X = dot.ortho_complement(U)
>>> print(X)
perp(U) (Subspace of R^3)
-------------------------
Field      R
Identity   [0, 0, 0]
Basis      [[1, 0, 0], [0, 1, -1/2]]
Dimension  2
Vector     [c0, c1, -c1/2]

Define a Linear Operator

>>> def mapping(vec):
>>>     return [vec[0], 2*vec[1], 3*vec[2]]
>>>
>>> T = LinearOperator('T', vectorspace=V, mapping=mapping)
>>> print(T)
T : V -> V
----------
Field        R
Rank         3
Nullity      0
Injective?   True
Surjective?  True
Bijective?   True
Matrix       [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
>>> T([1, 1, 1])
[1, 2, 3]

Operations with Linear Operators

Given an inner product, check whether a linear operator

>>> T.is_symmetric(dot)
True
>>> T.is_hermitian(dot)
True
>>> T.is_orthogonal(dot)
False
>>> T.is_unitary(dot)
False
>>> T.is_normal(dot)
True

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

ablina-0.4.0.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

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

ablina-0.4.0-py3-none-any.whl (27.3 kB view details)

Uploaded Python 3

File details

Details for the file ablina-0.4.0.tar.gz.

File metadata

  • Download URL: ablina-0.4.0.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.5

File hashes

Hashes for ablina-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0fcb96a47f53f58905871d05cc1c17eded91389666b7ace9252b6c1ca7580770
MD5 d2e485dababacf0adf590713242af8b5
BLAKE2b-256 fb1b92b00d9076df4a5491a2649252f55c712a139f7bfa8e834a2cbc6de30645

See more details on using hashes here.

File details

Details for the file ablina-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: ablina-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.5

File hashes

Hashes for ablina-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3461d16bde8193b7f4a0d9f2e49e2aff1b5e1e775654b23ea448b88948eee179
MD5 2793590fba8166c38886468180312b2e
BLAKE2b-256 896201ad68df5d1831de36d6358338ee568c3e81ee7fac9dd9473520b66fb0f4

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