jmath package

Subpackages

Submodules

jmath.autodiff module

Automatic Differentiation

Examples

>>> from jmath.autodiff import x, y
>>> f = 6*x*y*2 + y + 9
>>> f_prime = f.d(x)
>>> f_prime(x = 2, y = 1)
6
>>> from jmath.autodiff import x, y
>>> f = x*y
>>> grad_f = f.d(x, y)
>>> grad_f(x = 3, y = 2)
Vector(2, 3)
class jmath.autodiff.Function(func, derivatives)

Bases: object

Automatic Differentiation Function Object

Parameters
  • func (Callable) – Represented function.

  • derivatives (Tuple[Callable]) – Tuple of partial derivatives of the function with respect to function variables. Note that derivatives may be numeric values or functions with built-in operations and Functions.

d(*wrt)

Differentiates the function with respect to a variable.

Parameters

wrt (Union[jmath.autodiff.Variable, str]) – The variable to differentiate with respect to.

Return type

Union[jmath.autodiff.Function, jmath.linearalgebra.vectors.Vector]

differentiate(*wrt)

Differentiates the function with respect to a variable.

Parameters

wrt (Union[jmath.autodiff.Variable, str]) – The variable to differentiate with respect to.

Return type

Union[jmath.autodiff.Function, jmath.linearalgebra.vectors.Vector]

register(*inputs)

Registers inputs to the function.

Parameters

inputs (jmath.autodiff.Function) – Args, the functions to register as inputs.

class jmath.autodiff.Variable(id=None)

Bases: jmath.autodiff.Function

Variables for function differentiation.

Parameters

id (str) – Unique identifier string.

differentiate(wrt)

Differentiates the function with respect to a variable.

Parameters

wrt (jmath.autodiff.Variable) – The variable to differentiate with respect to.

Return type

int

jmath.autodiff.analyse(f)

Automatically analyses the given function and produces a Function object.

Parameters

f (Callable) – The function to analyse

Returns

A differentiable function object representing the given function.

Return type

Function

jmath.complex module

Complex form of points.

class jmath.complex.Complex(real, imaginary)

Bases: jmath.linearalgebra.vectors.Point

Complex numbers based on vectors.

Parameters
  • real (float) – Real component of a complex number

  • imaginary (float) – Imaginary component of a complex number

argument()

Computes the argument of the complex number in radians.

Return type

float

imaginary()

Returns the imaginary component of the complex number.

modulus()

Rename of magnitude.

Return type

float

quadrant()

Returns which quadrant the number is in. Returns None if on a quadrant border.

Return type

Optional[int]

real()

Returns the real component of the complex number.

jmath.discrete module

jmath/discrete.py

Discrete mathematical tools.

class jmath.discrete.Graph

Bases: object

Graph object defined by a set of nodes.

add_nodes(*nodes)

Adds node/s to the graph

Parameters

*nodes – Graph nodes to be added.

get_node(id)

Returns a node object in the graph based upon its ID. Returns None if not found.

Parameters

id – The ID of the node

Return type

Optional[jmath.discrete.Node]

intersections()

Returns a list of nodes that have more than one connection

Return type

List[jmath.discrete.Node]

loops()

Finds loops in the graph

Return type

List[jmath.discrete.Loop]

relationships()

Prints a human readable description of the relationship between all nodes

Return type

str

walk(start, stop=None, neighbour=0, default_neighbour=0)

“Walks” a loop around the graph, intended for generating loops for self.loops()

Parameters
  • start (jmath.discrete.Node) – Node the walk starts at.

  • stop (Optional[jmath.discrete.Node]) – Node the walk stops at.

  • neighbour (int) – Initial neighbour to move to.

  • default_neighbour (int) – Neighbour to move to on consequential moves.

Return type

jmath.discrete.Loop

class jmath.discrete.Loop(nodes)

Bases: jmath.discrete.Graph

A sub-graph structure generated by Graph.loops(), represents a distinct path around the graph.

Parameters

nodes (List[jmath.discrete.Node]) – List of node objects that define the loop.

relationships()

Prints a human readable representation of the relationship between nodes

Return type

str

reorder(node)

Reorders the loop to start at the specified node.

Parameters

node (jmath.discrete.Node) – Node to reconfigure to start at.

class jmath.discrete.Node(id, weight=1)

Bases: object

Node of a graph.

Parameters
  • id (str) – Unique ID string describing the object.

  • weight (float) – Weighting of node.

add_neighbour(neighbour, weight=1, two_way=False)

Adds a neighbouring node

Parameters
  • neighbour (jmath.discrete.Node) – Node object describing the neighbour.

  • weight (float) – The weighting of the relationship.

  • two_way (bool) – Whether the relationship goes both ways.

neighbours_list()

Returns the IDs of the neighbouring nodes

Return type

list

relationships()

Returns human readable string of relationships between node and neighbours.

Return type

str

remove_neighbour(neighbour)

Removes the relationship between the nodes.

Parameters

neighbour (jmath.discrete.Node) – Node object describing the neighbour node

Notes

Only removes the neighbourship from THIS NODE.

jmath.exceptions module

Defines exceptions for jmath.

exception jmath.exceptions.NoConversion(from_unit, to_unit, message='')

Bases: Exception

Exception thrown if there is no way to convert between two given units.

Parameters
  • from_unit (Unit) – The unit to convert from.

  • to_unit (Unit) – The unit to convert to.

  • message (str) – Appended additional message

exception jmath.exceptions.OutOfRange(num_input, lower_bound, upper_bound, message='')

Bases: Exception

Exception thrown for values that are not within expected bounds.

Parameters
  • num_input (float) – The input number

  • lower_bound (float) – The lower boundary

  • upper_bound (float) – The upper boundary

  • message (str) – Appended additional message

exception jmath.exceptions.VectorsNotSameSize(message='Operation invalid for vectors of different sizes.')

Bases: Exception

Exception thrown for operations on vectors of different sizes

exception jmath.exceptions.ZeroDistance(message='Invalid operation! Zero distance between objects.')

Bases: Exception

Exception thrown for calculations with zero distance between objects.

jmath.modular module

Tools for modular arithmetic

jmath.modular.extended_gcd(a, b)

Recursive extended euclidean algorithm to find the greatest common denominator and its linear combination Returns g, m, n such that gcd(a, b) = g = m*a + n*b

Parameters
  • a (int) – Number to find the greatest common denominator of with b

  • b (int) – Number to find the greatest common denominator of with a

Return type

Tuple[int, int, int]

jmath.modular.modular_inverse(a, set_size)

Finds the modular inverse of a number in a set.

Parameters
  • a (int) – The number to find the modular inverse of

  • set_size (int) – The size of the set to find the inverse in

Return type

Optional[int]

jmath.uncertainties module

Provides a class to deal with values with associated uncertainty.

class jmath.uncertainties.Uncertainty(value, uncertainty)

Bases: object

A value with an associated uncertainty.

Parameters
  • value (float) – The value associated with an uncertainty

  • uncertainty (float) – Uncertainty associated with the value

Examples

>>> Uncertainty(3, 1) + Uncertainty(4, 2)
Uncertainty(7, 3)
>>> Uncertainty(10, 1) - Uncertainty(2, 1)
Uncertainty(8, 2)
>>> 2 * Uncertainty(2, 1)
Uncertainty(4, 2.0)
>>> Uncertainty(2, 1) + Uncertainty(4, 1)
Uncertainty(8, 6.0)
>>> Uncertainty(20, 2) / Uncertainty(2, 0.5)
Uncertainty(10.0, 3.5)
abs_uncertainty()

Returns the absolute uncertainty.

Return type

float

apply(func, *args)

Applies a mathematical function to the value using the bruteforce method to calculate the new uncertainty.

Parameters
  • func (Callable[[float], float]) – The function to apply.

  • args – Additional arguments to send to the function.

Return type

jmath.uncertainties.Uncertainty

rel_uncertainty()

Returns the relative uncertainty as proportion.

Return type

float

jmath.uncertainties.half_range(data)

Calculates the absolute uncertainty associated with a set of data.

Parameters

data (List[float]) – List/tuple of the data

Return type

float

jmath.uncertainties.mean(data)

Calculates the mean of a set of data and it’s uncertainty by the half range rule.

Parameters

data (List[float]) – List/tuple of data to take the mean of.

Return type

jmath.uncertainties.Uncertainty

Module contents

The jmath top level package. Provides a set of default and optional sub-packages for doing maths in Python.

Testing

Testing can be performed upon the entire jmath codebase using

$ pytest

when in the jmath root directory.

Documentation

Upon addition of a new sub-module the documentation must be updated with

$ sphinx-apidoc -o ./docs/source/ ./jmath –force

when in the jmath root directory.

Documentation is automatically built by a workflow in github and published to https://jmath.jordanhay.com/