MatX For MATLAB/NumPy/Eigen Users#
MatX has many features directly inspired by MATLAB and Python (Numpy/Scipy) for translating these high-level languages into C++. The table below aims to give users of either of those tools a translation guide to writing efficient MatX code by learning the syntax mapping between the tools. Most of these conversions can also be found inside either the unit tests or the source code as well.
Due to its popularity in linear algebra applications, examples of common Eigen operations have been added to the below table. An example file is provided at examples/eigenExample.cu
with examples of common operations in Eigen and their MatX equivalent.
If you have Eigen installed on your system, you can build the examples with Eigen by setting the cmake variable eigen_DIR=/path/to/eigen/
.
A few key notes to be aware of when using MatX and Eigen in the same environment:
The below example are only valid for 2D data. Eigen and its API is primarily targeting 2D problems (without the unsupported/tensor library), so there is not a single pattern to follow for porting code with rank > 2 tensors from Eigen to MatX; each user’s solution for higher rank data will result in a unique mapping to MatX tensor memory.
When copying data between Eigen and MatX structures (most likely Eigen::MatrixXd to MatX tensors) keep in mind that the underlying data structure may or may not be available on the CPU. use the accessor functions () or a cudaMemcpy when applicable.
Eigen has column-major storage by default, so ensure you transpose any raw data copies between structures.
Overview#
Some general rules to keep in mind about these three tools:
The Python column assumes both Numpy and Scipy are being used for certain library calls.
Both Python and MATLAB use the term “multi-dimensional array”. MatX calls these tensors.
MATLAB uses 1-based indexing while Python and MatX use 0-based indexing.
MATLAB uses inclusive ranges on indexing, while Python and MatX use exclusive ranges.
MATLAB and Python may or may not make a copy of a tensor behind-the-scenes to improve performance. MatX makes this explicit. by never making a copy unless the function call mentions that it copies.
MATLAB uses column-major (FORTRAN) memory order, while Python and MatX use row-major (C). When converting optimized MATLAB scripts, it may be beneficial to transpose the dimension so the fastest changing dimension is the inner-most dimension.
Conversion Table#
Operation |
MATLAB |
Python |
Eigen |
MatX |
Notes |
---|---|---|---|---|---|
Basic indexing |
|
|
|
|
Retrieves the element in the first row and fifth column |
Tensor addition |
|
|
|
|
Adds two tensors element-wise |
Tensor subtraction |
|
|
|
|
Subtracts two tensors element-wise |
Tensor multiplication |
|
|
|
|
Multiplies two tensors element-wise |
Tensor division |
|
|
|
|
Divides two tensors element-wise |
Tensor slice (contiguous) |
|
|
|
|
Slices 4 elements of the outer dimension starting at 0, and 5 elements of the inner dimension, starting at the second element. |
Tensor slice (w/stride) |
|
|
|
|
Slices N elements of the outer dimension starting at the first element and picking every second element until the end. In the inner dimension, start at the first element and grab every third item, and stop at the 8th item. |
Cloning a dimension |
|
|
|
|
Takes a 4x4 2D tensor and makes it a 5x4x4 3D tensor where every outer dimension replicates the two inner inner dimensions |
Slice off a row or column |
|
|
|
|
Selects the fifth row and all columns from a 2D tensor, and returns a 1D tensor |
Permute dimensions |
|
|
|
|
Permutes the three axes into the opposite order In the inner dimension, start at the first element and grab every third item, and stop at the 8th item. In the inner dimension, start at the first element and grab every third item, and stop at the 8th item. |
Get real values |
|
|
|
|
Returns only the real values of the complex series |
Matrix multiply (GEMM) |
|
|
|
|
Computes the matrix multiplication of |
Compute matrix inverse |
|
|
|
|
Computes the inverse of matrix A using LU factorization |
1D FFT |
|
|
N/A |
|
Computes the 1D fast fourier transfor, (FFT) of rows of A |
1D IFFT |
|
|
N/A |
|
Computes the 1D inverse fast fourier transfor, (IFFT) of rows of A |
2D FFT |
|
|
N/A |
|
Computes the 2D fast fourier transfor, (FFT) of matrices in outer 2 dimensions of A |
2D IFFT |
|
|
N/A |
|
Computes the 2D inverse fast fourier transfor, (IFFT) of matrices in outer 2 dimensions of A |
Covariance |
|
|
N/A |
|
Computes the covariance on the rows of matrix A |