Python Mathematics of Arrays (MOA)
Mathematics of Arrays (MOA)
MOA is a mathematically rigorous approach to dealing with arrays that was developed by Lenore Mullins. MOA is guided by the following principles.
Everything is an array and has a shape. Scalars. Vectors. NDArray.
What is the shape of the computation at each step of the calculation?
Answering this guarentees no out of bounds indexing and a valid running program.
- What are the indicies and operations required to produce a given index in the result?
Once we have solved this step we have a minimal representation of the computation that has the Church Rosser property. Allowing us to truely compare algorithms, analyze algorithms, and finally map to algorithm to a low level implementation. For further questions see the documentation. The documentation provides the theory, implementation details, and a guide.
Important questions that will guide development:
- [X] Is a simple implementation of moa possible with only knowing the dimension?
- [X] Can we represent complex operations and einsum math: requires
- [ ] What is the interface for arrays? (shape, indexing function)
- [ ] How does one wrap pre-existing numerical routines?
pip install python-moa
Documentation is available on
documentation provides the theory, implementation details, and a
guide for development and usage of
A few well maintained jupyter notebooks are available for experimentation with binder
Python Frontend AST Generation
from moa.frontend import LazyArray A = LazyArray(name='A', shape=(2, 3)) B = LazyArray(name='B', shape=(2, 3)) expression = ((A + B).T) expression.visualize(as_text=True)
psi(Ψ) ├── Array _a2: <1> (0) └── transpose(Ø) └── + ├── Array A: <2 3> └── Array B: <2 3>
psi(Ψ): <2> ├── Array _a2: <1> (0) └── transpose(Ø): <3 2> └── +: <2 3> ├── Array A: <2 3> └── Array B: <2 3>
Reduction to DNF
+: <2> ├── psi(Ψ): <2> │ ├── Array _a6: <2> (_i3 0) │ └── Array A: <2 3> └── psi(Ψ): <2> ├── Array _a6: <2> (_i3 0) └── Array B: <2 3>
Reduction to ONF
function: <2> (A B) -> _a17 ├── if (not ((len(B.shape) == 2) and (len(A.shape) == 2))) │ └── error arguments have invalid dimension ├── if (not ((3 == B.shape) and ((2 == B.shape) and ((3 == A.shape) and (2 == A.shape))))) │ └── error arguments have invalid shape ├── initialize: <2> _a17 └── loop: <2> _i3 └── assign: <2> ├── psi(Ψ): <2> │ ├── Array _a18: <1> (_i3) │ └── Array _a17: <2> └── +: <2> ├── psi(Ψ): <2> │ ├── Array _a6: <2> (_i3 0) │ └── Array A: <2 3> └── psi(Ψ): <2> ├── Array _a6: <2> (_i3 0) └── Array B: <2 3>
Generate Python Source
@numba.jit def f(A, B): if (not ((len(B.shape) == 2) and (len(A.shape) == 2))): raise Exception('arguments have invalid dimension') if (not ((3 == B.shape) and ((2 == B.shape) and ((3 == A.shape) and (2 == A.shape))))): raise Exception('arguments have invalid shape') _a17 = numpy.zeros((2,)) for _i3 in range(0, 2): _a17[(_i3,)] = (A[(_i3, 0)] + B[(_i3, 0)]) return _a17
Download nix. No other dependencies and all builds will be identical on Linux and OSX.
nix-shell dev.nix -A jupyter-shell
nix-shell dev.nix -A ipython-shell
nix-build dev.nix -A python-moa
To include benchmarks (numba, numpy, pytorch, tensorflow)
nix-build dev.nix -A python-moa --arg benchmark true
nix-build dev.nix -A docs firefox result/index.html
nix-build moa.nix -A docker docker load < result
This is a proof of concept which should be guided by assumptions and goals.
Assumes that dimension is each operation is known. This condition with not much work can be relaxed to knowing an upper bound.
The MOA compiler is designed to be modular with clear separations: parsing, shape calculation, dnf reduction, onf reduction, and code generation.
All code is written with the idea that the logic can ported to any low level language (C for example). This means no object oriented design and using simple data structures. Dictionaries should be the highest level data structure used.
Performance is not a huge concern instead readability should be preferred. The goal of this code is to serve as documentation for beginners in MOA. Remember that tests are often great forms of documentation as well.
Runtime dependencies should be avoided. Testing (pytest, hypothesis) and Visualization (graphviz) are examples of suitable exceptions.
Contributions are welcome! For bug reports or requests please submit an issue.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size python-moa-0.5.1.tar.gz (33.7 kB)||File type Source||Python version None||Upload date||Hashes View|