Skip to content

Commit a37039a

Browse files
colescottjmbrapplebybraised-babbage
authored
Magicl NG (#63)
* Add initial version of magicl high-level rewrite * Add *derive-function-types* to asdf project * Add random, fix multiplication, add lapack, make things work * Fix idenetity typo * Fix empty and deye documentation and fix assert-square-matrix printing whole matrix * Remove FASL in high-level dir (Why was that there?) * Fix indentation in matrix.lisp and clean up trace * Add macro to generate blas mult boilerplate * Remove old version of matrix/double-float * Add lapack macros to make mult work * Fix from-*-major-index and add tests for util file * Move lapack method definitions into macros for ease of use * Add square restriction to qr/ql/rq/lq * Remove magicl from :use on packages to avoid shadowing errors * Add support for calling PPRINT-MATRIX within FORMAT * Add distribution as function to RAND and add tests * Add lapack inverse to high level and finish updating examples to new interface * Remove debug message from lapack-inv * Update TEST-RANDOM-UNITARY and TEST-LOGM to new interface * Fix TEST-KRON * Gave it some throught. It should work. * Add p-norm to vector type * Add slice to ABSTRACT-TENSOR - Slice implemented for abstract-tensor - Docstrings cleaned up - TODOs TODONE * Remove TODO regarding if it makes sense. It makes sense. * Fix tests not calling all the tests * Fix constant redefinition in magicl-tests caused by incorrect test predicate * Extract function for type inferring in tensor constructors * Clean up TODOs in high-level * Reduce oddity in abstract-tensor-tests and include dim=1 case * Fix TEST-TENSOR-SHAPE and TEST-TENSOR-RANK with more advanced loops * Remove unneeded multiplication of shape in FROM-DIAG constructor * Update high-level docstrings to be more uniform and descriptive and add order parameters to map and into * Implement VALID-INDEX-P properly and use in high-level * Fix teh typo and bad indentation in high-level-tests * Fix indentation in INFER-TENSOR-TYPE * Fix VALID-INDEX-P to allow for not specifying shape * Make map and into functions more readable * Change high-level constructors to functions from generic functions * Make DEFVECTOR/DEFMATRIX macros more expandable * Add HERMITIAN-EIG to new high-level interface * Fix appleby's nitpicks * Add policy-cond for performance when speed > safety and directly construct classes * Switch from CLOS to structs for tensor objects This gives a significant performance improvement due to avoiding generics at the cost of some flexibility and my sanity. * Remove freeze on tensor struct causing type error * Fix pretty printing for matrix and tensor types and remove old methods * Remove accidentally committed log files * Add (SETF SHAPE) and optimize matrix TREF * Remove warnings from unused arguments and load order * Add basic documentation for high-level interface * Fix bug in transpose on matrix with dimension of 1 * Add integer printing to matrix and vector pretty printers * Change from dynamic to static symbols in INFER-TENSOR-TYPE and move to appropriate file * Add CAST, a replacement for CHANGE-CLASS on tensors * Clean up util and comments * Add subtypes and straggling symbols to package * Add reduced svd interface and tests (#65) * Add CSD-2X2-BASIC (#67) * Use use-expectations in the expected way * Change ABSTRACT-TENSOR to not allow for construction or copying * Move magicl high-level into module in ASDF * Clean up documentation for ABSTRACT-TENSOR * Replace POLICY-IF with WITH-EXPECTATIONS in constructors * Fix mistake in abstract-tensor docstring * Use COPY-SEQ for copying tensor storage and add copiers to vector * Remove funky comma in ASSERT-SQUARE-SHAPE * Move lapack bindings to lapack-bindings.lisp and switch to functions to generate lapack code * Update docstrings for DEFTENSOR, DEFMATRIX, DEFVECTOR, and DEFCOMPATIBLE * Fix other library equivalents table errors Co-Authored-By: Juan M. Bello-Rivas <jbellorivas@rigetti.com> * Change def- to generate- in lapack-templates to reflect change from macro to func * Add numpy rq to high-level doc * Change terminology regarding indexing and number of dimensions of tensors ORDER is now LAYOUT RANK is now ORDER * Remove LAYOUT arg from INTO! and FOREACH in abstract tensor interface These fields do not make sense to have on a method which is calling a function independent of the ordering of the elements * Add ZEROS and ONES constructors for convenience * Remove square requirement from DEYE constructor * Remove ORDER 2 requirement from DEYE * Rename DEYE to EYE * Add TRIL and TRIU synonyms and change DAGGER(!) to synonyms * Add TRIL/TRIU to package * Add issue number to ZUNCSD shim * Replace = with := in LOOPs * Allow for FIXNUM in EYE shape and remove SHAPE requirement from FROM-DIAG * Update tests to refelect change in FROM-DIAG interface * Add BINARY-OPERATOR method and add '.' prefix to element-wise operators * Update documentation for elementwise operators * Remove unwanted argument on FROM-DIAG * Fix typo, represening how bad I am at speling * Fix RAND docstring to be more documenting * Allow for multiple tensors in every, any, notevery, notany * Fix bug in hermitian-eig caused by copying/resetting layout slot on return and export method * Export slice and mult from high-level * Fix expm returning incorrect layout for result * Bump version to 0.7.0 * Symbol case invariance. Drop unused declarations (#74) * Add optimizations for MATRIX class Add matrix-{row,column}-major-index Add specialized INTO! for MATRIX class Add make-storage for matrix class * Update documentation to be more correct Apply suggestions from code review Co-Authored-By: appleby <mappleby@rigetti.com> * Change INVERSE -> INV * Change POLICY-IF to POLICY-COND herminitan-matrix-p has a strict epsilon added to avoid test failures * Fix LAYOUT->ORDER in high-level docs * Fix indentation in high-level docs * Fix deye->eye in high-level docs * Add check for valid index to tref and setf tref in vector and tensor classes * Change default tensor type to var to allow for rebinding * Remove unneeded variable in hermitian-eig * Force square shape for upper and lower triangular of matrix * Update element-wise exponentiation in high-level docs to be consistent * Update examples.lisp Co-Authored-By: Erik Davis <erik@rigetti.com> * Remove layout from ARANGE constructor layout is not needed because it returns a vector * Fix tensor equality errors and add corresponding tests * Add equality for float vectors and fix error in high-level tests * Overhaul p-norm for vectors and add tests * Update documentation for numpy hermitian transpose * Fix p-norm for infinite norm of negative single element vectors * Change float comparison thresholds to variables * Change *-MATRIX-P to functions * Fix incorrect assertion in p-norm of vector * Apply suggestions from code review Co-Authored-By: Erik Davis <erik@rigetti.com> * Remove ORTHONORMALIZE methods for matrices (just use QR) * Add check for singular matrices in INV folllowing call to LU * Remove old note about LU requried for INV. INV now calls the LU LAPACK routine directly and does not require LU to be defined. * Add P-NORM-TYPE and modify vector norm to use type and to disallow negative infinity Co-authored-by: Juan M. Bello-Rivas <jmbr@superadditive.com> Co-authored-by: appleby <mappleby@rigetti.com> Co-authored-by: Erik Davis <erik@cadlag.org>
1 parent 122969c commit a37039a

37 files changed

+3275
-1388
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ You can run the MAGICL tests from your Lisp REPL with:
3939
(asdf:test-system :magicl)
4040
```
4141

42+
## High-level Interface
43+
44+
See [high-level doc](doc/high-level.md).
45+
4246
## Showing Available Functions
4347

4448
Some distributions of a library don't actually provide all of the functions of the reference BLAS and LAPACK. One can look at a summary of available and unavailable functions with the function `magicl:print-availability-report`. By default, it will show all functions and their availability. There are three arguments to fine-tune this behavior:

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"0.6.5"
1+
"0.7.0"

doc/high-level.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# High Level Bindings
2+
3+
The purpose of the high-level magicl bindings is to allow for MATLAB-like multidimensional arrays in lisp.
4+
5+
## Constructors
6+
7+
The construction of tensors can be done with any of the given constructors. The constructors take a shape and arguments for method of construction.
8+
9+
Tensors are specialized on both the shape and the element type. The class of a tensor will be of the form `$CLASS/$TYPE` (e.g. `MATRIX/DOUBLE-FLOAT`).
10+
11+
| Number of dimensions | Tensor Class |
12+
|----------------------|-----------------|
13+
| 1 | `VECTOR` |
14+
| 2 | `MATRIX` |
15+
| * | `TENSOR` |
16+
17+
| Element Type | Class Suffix |
18+
|--------------------------|------------------------|
19+
| `SINGLE-FLOAT` | `SINGLE-FLOAT` |
20+
| `DOUBLE-FLOAT` | `DOUBLE-FLOAT` |
21+
| `(COMPLEX SINGLE-FLOAT)` | `COMPLEX-SINGLE-FLOAT` |
22+
| `(COMPLEX DOUBLE-FLOAT)` | `COMPLEX-DOUBLE-FLOAT` |
23+
| `(SIGNED-BYTE 32)` | `INT32` |
24+
25+
The type of the elements of the tensor can be specified with the `:type` keyword, or the constructor will attempt to find an appropriate type from the given arguments. The default element type for a tensor is `DOUBLE-FLOAT`.
26+
27+
The layout of the tensor (column-major or row-major) can be specified with the `:layout` keyword. This affects the underlying storage of the tensor and will affect how it carries out operations with LAPACK.
28+
29+
30+
## Other Library Equivalents
31+
32+
This table was adapted largely from the [NumPy Equivalents Table](https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html#linear-algebra-equivalents).
33+
34+
### Basic Accessors
35+
36+
| MAGICL | MATLAB | NumPy | Description |
37+
|----------------|------------|-------------------------|---------------------------------------------------------------|
38+
| `(order a)` | `ndims(a)` | `ndim(a)` or `a.ndim` | Get the number of dimensions of the array. |
39+
| `(size a)` | `numel(a)` | `size(a)` or `a.size` | Get the number of elements of the array. |
40+
| `(shape a)` | `size(a)` | `shape(a)` or `a.shape` | Get the shape of the array. |
41+
| `(tref a 1 4)` | `a(2,5)` | `a[1, 4]` | Get the element in the second row, fifth column of the array. |
42+
43+
### Constructors
44+
45+
| MAGICL | MATLAB | NumPy | Description |
46+
|-------------------------------------------------|--------------------|-----------------------------------|--------------------------------------------------------------------------------------|
47+
| `(from-list '(1d0 2d0 3d0 4d0 5d0 6d0) '(2 3))` | `[ 1 2 3; 4 5 6 ]` | `array([[1.,2.,3.], [4.,5.,6.]])` | Create a 2x3 matrix from given elements. |
48+
| `(zeros '(2 3 4))` or `(const 0d0 '(2 3 4))` | `zeros(2,3,4)` | `zeros((2,3,4))` | Create a 2x3x4 dimensional array of zeroes of double-float element type. |
49+
| `(ones '(3 4)` or `(const 1d0 '(3 4))` | `ones(3,4)` | `ones((3,4))` | Create a 3x4 dimensional array of ones of double-float element type. |
50+
| `(eye 1d0 '(3 3))` | `eye(3)` | `eye(3)` | Create a 3x3 identity array of double-float element type. |
51+
| `(from-diag a)` | `diag(a)` | `diag(a)` | Create a square matrix from the diagonal entries in `a` with zeroes everywhere else. |
52+
| `(rand '(3 4))` | `rand(3,4)` | `random.rand(3,4)` | Create a random 3x4 array. |
53+
54+
### Basic Operations
55+
56+
| MAGICL | MATLAB | NumPy | Description |
57+
|------------|----------|------------------|-----------------------------|
58+
| `(@ a b)` | `a * b` | `a @ b` | Matrix multiplication |
59+
| `(.+ a b)` | `a + b` | `a + b` | Element-wise add |
60+
| `(.- a b)` | `a - b` | `a - b` | Element-wise subtract |
61+
| `(.* a b)` | `a .* b` | `a * b` | Element-wise multiply |
62+
| `(./ a b)` | `a./b` | `a/b` | Element-wise divide |
63+
| `(.^ a b)` | `a.^b` | `np.power(a,b)` | Element-wise exponentiation |
64+
65+
### Linear Algebra
66+
67+
| MAGICL | MATLAB | NumPy | Description |
68+
|---------------------------------------------------------|-------------------|----------------------------------------------------------------|--------------------------------------------|
69+
| `(det a)` | `det(a)` | `linalg.det(a)` | Determinant of matrix |
70+
| `(trace a)` | `trace(a)` | `trace(a)` | Trace (sum of diagonal elements) of matrix |
71+
| `(upper-triangular a)` | `triu(a)` | `triu(a)` | Upper triangular part of matrix |
72+
| `(lower-triangular a)` | `tril(a)` | `tril(a)` | Lower triangular part of matrix |
73+
| `(transpose a)` | `a.'` | `a.transpose()` or `a.T` | Transpose of matrix |
74+
| `(conjugate-transpose a)` or `(dagger a)` | `a'` | `a.conj().transpose()` or `a.H` | Conjugate transpose of matrix |
75+
| `(inv a)` | `inv(a)` | `linalg.inv(a)` | Inverse of matrix |
76+
| `(svd a)` (Returns `(VALUES U SIGMA Vt)`) | `[U,S,V]=svd(a)` | `U, S, Vh = linalg.svd(a), V = Vh.T` | Singular value decomposition of matrix |
77+
| `(eig a)` (Returns `(VALUES EIGENVALUES EIGENVECTORS)`) | `[V,D]=eig(a)` | `D,V = linalg.eig(a)` | Eigenvalues and eigenvectors of matrix |
78+
| `(qr a)` (Returns `(VALUES Q R)`) | `[Q,R,P]=qr(a,0)` | `Q,R = scipy.linalg.qr(a)` | QR factorization of matrix |
79+
| `(ql a)` (Returns `(VALUES Q L)`) | | | QL factorization of matrix |
80+
| `(rq a)` (Returns `(VALUES R Q)`) | | `R,Q = scipy.linalg.rq(a)` | RQ factorization of matrix |
81+
| `(lq a)` (Returns `(VALUES L Q)`) | | | LQ factorization of matrix |
82+
| `(lu a)` (Returns `(VALUES LU IPIV)`) | `[L,U,P]=lu(a)` | `L,U = scipy.linalg.lu(a)` or `LU,P=scipy.linalg.lu_factor(a)` | LU decomposition of matrix |
83+
| `(csd a)` (Returns `(VALUES U SIGMA VT)`) | | | Cosine-sine decomposition of matrix |

0 commit comments

Comments
 (0)