Mython

Some code to support fundamental maths ideas in Python, using finite sets.

As far as possible items are named for what they are. For example, the class
defining a set is named Set. As consequence, only a few docstrings are needed.

Inheritance is not always used. For example a natural number is a set, but we do
not make NaturalNumber extend Set, because there is little point.

We define natural numbers, integers and rationals in terms of sets. But we also use
Python ints where it seems simpler.

Classes

NaturalNumber
Rational
Set
Relation
Function
BinaryOp
Group
Z

 class BinaryOp(Function) A binary operation Method resolution order: BinaryOp Function Relation Set Methods defined here: __init__(self, domain, values)values should be a list of triples [x,y,z] such that  x•y = z __str__(self) allHaveInverse(self)Return true iff all elements have inverses apply(self, x, y)return x◦y identity(self)Returns left identity, or None inverse(self, x) isAssoc(self)Return true iff this is associative Methods inherited from Function: isInjective(self) isSurjective(self) Methods inherited from Relation: image(self)Also known as range isAntiSymmetric(self) isEquivalence(self) isOrder(self) isReflexive(self) isSymmetric(self) isTransitive(self) Methods inherited from Set: __eq__(self, other) card(self)Cardinality cartesianProduct(self, other) difference(self, other) getElements(self) hasMember(self, y)Is y an element of this set intersection(self, other) isSubset(self, other) powerSet(self) symDiff(self, other)symmetric difference union(self, other)

 class Function(Relation) Method resolution order: Function Relation Set Methods defined here: __init__(self, domain, codomain, pairs, name) isInjective(self) isSurjective(self) Methods inherited from Relation: image(self)Also known as range isAntiSymmetric(self) isEquivalence(self) isOrder(self) isReflexive(self) isSymmetric(self) isTransitive(self) Methods inherited from Set: __eq__(self, other) __str__(self) card(self)Cardinality cartesianProduct(self, other) difference(self, other) getElements(self) hasMember(self, y)Is y an element of this set intersection(self, other) isSubset(self, other) powerSet(self) symDiff(self, other)symmetric difference union(self, other)

 class Group(BinaryOp) Method resolution order: Group BinaryOp Function Relation Set Methods defined here: __init__(self, domain, values)values should be a list of triples [x,y,z] such that  x•y = z Methods inherited from BinaryOp: __str__(self) allHaveInverse(self)Return true iff all elements have inverses apply(self, x, y)return x◦y identity(self)Returns left identity, or None inverse(self, x) isAssoc(self)Return true iff this is associative Methods inherited from Function: isInjective(self) isSurjective(self) Methods inherited from Relation: image(self)Also known as range isAntiSymmetric(self) isEquivalence(self) isOrder(self) isReflexive(self) isSymmetric(self) isTransitive(self) Methods inherited from Set: __eq__(self, other) card(self)Cardinality cartesianProduct(self, other) difference(self, other) getElements(self) hasMember(self, y)Is y an element of this set intersection(self, other) isSubset(self, other) powerSet(self) symDiff(self, other)symmetric difference union(self, other)

 class NaturalNumber Methods defined here: __add__(self, other) __init__(self, n) pred(self) succ(self)

 class Rational A rational number class Methods defined here: __add__(self, other) __eq__(self, other) __init__(self, a, b) __mul__(self, other) __str__(self) __sub__(self, other) __truediv__(self, other)

 class Relation(Set) Methods defined here: __init__(self, domain, codomain, arg3, name) image(self)Also known as range isAntiSymmetric(self) isEquivalence(self) isOrder(self) isReflexive(self) isSymmetric(self) isTransitive(self) Methods inherited from Set: __eq__(self, other) __str__(self) card(self)Cardinality cartesianProduct(self, other) difference(self, other) getElements(self) hasMember(self, y)Is y an element of this set intersection(self, other) isSubset(self, other) powerSet(self) symDiff(self, other)symmetric difference union(self, other)

 class Set Methods defined here: __eq__(self, other) __init__(self, arg1, arg2=None, arg3=None)The set can be defined with a given list, or as a formula to calulate the nth term, and a range. A name is optional. Examples: A = Set([1, 2, 3, 2, 1], "A") gets name 'A'. Duplicates ignored A = Set([1, 2, 3]) set name is the stringified list A = Set(range(1, 100), "3*n") multiples of 3 from 3 to 300 A = Set(range(1, 10), "1/n") 1, 0.5, 0.333, 0.25..0.1 __str__(self) card(self)Cardinality cartesianProduct(self, other) difference(self, other) getElements(self) hasMember(self, y)Is y an element of this set intersection(self, other) isSubset(self, other) powerSet(self) symDiff(self, other)symmetric difference union(self, other)

 class Z A signed integer type Methods defined here: __add__(self, other) __eq__(self, other) __init__(self, a, b) __mul__(self, other) __str__(self) __sub__(self, other)

 Functions gcd(a, b)

 Data A = b = nullSet = t = [[0, 0, 0], [0, 1, 1], [0, 2, 2], [1, 0, 1], [1, 1, 2], [1, 2, 0], [2, 0, 2], [2, 1, 0], [2, 2, 1]] x = 2 y = 2 z = 2