calculus

This submodule collects all the basic functionality for calculus contained within this package. The central ingredient is the Complex type, which allows to work with rational-number-valued complex numbers. While not perfect, this is sufficient for many scenarios and much simpler to implement and maintain than irrational numbers and avoids (most) reliance on rounding.

class Complex(re: int | Fraction, im: int | Fraction = 0)

Bases: object

Rudimentary implementation of a rational-number-valued complex number.

Parameters:
  • re (int|Fraction) – Real part of the complex number.

  • im (int|Fraction, optional) – Imaginary part of the complex number, defaults to 0.

abs2() int | Fraction

Computes z*conjugate(z) of the complex number z.

Returns:

Absolute value squared of the current complex number.

Return type:

int|Fraction

conjugate()

Complex conjugation.

Returns:

Complex conjugate of the current complex number.

Return type:

Complex

property imag

Returns the imaginary part of the complex number to comply with standard Python number interface.

phase()

Assuming the complex number to be purely real or purely imaginary, this function returns the sign and (for a purely imaginary number) an imaginary unit.

Returns:

Sign and any overall imaginary unit.

Return type:

Complex

Raises:

AssertionError – If number is not purely real or purely imaginary.

property real

Returns the real part of the complex number to comply with standard Python number interface.

class Matrix(components: list[list[Complex]] = None)

Bases: object

Basic implementation of a matrix with complex rational coefficients.

Parameters:

components (list[list[Complex]], optional) – The rows and columns of the matrix. If None are given, yields an empty matrix which is a valid starting point to build your matrix via Matrix.extend.

COL = 1
property M

Returns the number of rows.

property N

Returns the number of columns.

ROW = 0
static ZERO(M: int, N: int)

Creates an \(M\times N\) matrix filled with zeros.

Parameters:
  • M (int) – Number of rows.

  • N (int) – Number of columns.

Returns:

An all zero matrix with the requested dimensions.

Return type:

Matrix

__getitem__(idx: int | tuple[int | slice, int | slice])

Danger

A slice ranging further than the matrix dimensions will not yield an error.

Raises:

TypeError – Indicates that the chosen indices are not in the expected format.

__setitem__(idx: int | tuple[int | slice, int | slice], value)

Danger

A slice ranging further than the matrix dimensions will not yield an error as long as the actual size available agrees with value.

Parameters:
Raises:
  • AssertionError – Indicates that there was a dimension mismatch.

  • TypeError – Indicates that the chosen indices are not in the expected format.

conjugate()

Charge conjugation.

Returns:

Charge conjugated matrix.

Return type:

Matrix

static diag(delems: list[Complex], M=None, N=None)

Generalisation of a standard diagonal square matrix.

The user can choose the dimensions freely as long as the diagonal entries fit into the matrix.

Parameters:
  • diag (list[Complex]) – Diagonal elements to fill the matrix with.

  • M (int, optional) – Requested number of rows. Defaults to None. Otherwise, must be at least the number of delems given.

  • N (int, optional) – Requested number of columns. Defaults to None. Otherwise, must be at least the number of delems given.

Returns:

New matrix with diagonal entries set to delems and otherwise zero.

Return type:

Matrix

extend(elems: list[Complex], dim=1)

Enlarge the matrix by adding a column or row to the existing matrix.

Parameters:
  • elems (list[Complex] | Matrix) – Vector to be added to the matrix.

  • dim (int, optional) – Selects whether to add the vector as a ROW or COL. Defaults to COL.

Raises:
inverse()

Inversion of the matrix.

Uses basic implementation of Gauss elimination for inverse of complex rational square matrix.

Returns:

The inverse of this matrix.

Return type:

Matrix

Raises:

AssertionError – Indicates that the matrix is not square and thus non-invertible.

rank(persistent=False)

This is an adaptation of solve omitting any RHS input but allowing for all-zero columns. If persistent is True the Matrix is changed into a generalised row-echelon form and any totally zero rows are being dropped. This way only the rows relevant for checks of linear-independence are being kept.

Parameters:

persistent (bool, optional) – Indicates whether to keep the generalised row-echelon form of the matrix as the new matrix at the end of the calculation. Defaults to False.

Returns:

Number of linearly independent rows.

Return type:

int

trace()

Computes the generalised trace.

Returns:

Sum of all the diagonal elements.

Return type:

Complex

transpose()

Exchanges rows and columns.

Returns:

Transposed matrix.

Return type:

Matrix

solve(A: Matrix, y: Matrix)

Solves the general linear system of equations via Gauss elimination

\[Ax = y\]
Parameters:
  • A (Matrix) – Matrix acting on the \(x\) that we are looking for.

  • y (Matrix) – Right-hand side answer.

Returns:

Result found for \(x\).

Return type:

Matrix