This submission defines a generic class of matrix-like objects called MatrixObj and a subclass called DataObj. Objects of the class are capable of behaving as matrices, but whose math operators (+,-,*,\,.*,<,>,etc...) and other methods can be defined/redefined from within any Mfile or even from the command line. This removes the restriction of writing a dedicated classdef file or class directory for every new matrix-type object that a user might wish to create.
The class works by storing function handles to the various matrix operator functions (plus, minus, mtimes, mldivide, etc...) in a property of MatrixObj called Ops, which is a structure variable. Hence, one can set the matrix operators as desired simply by setting the fields of Ops to an appropriate function handle.
MatrixObj objects are particularly useful when an object needs to be endowed with just a few matrix-like capabilities that are very quickly expressed using anonymous functions or a few short nested functions. This is illustrated in the examples below that deal with creating an efficient version of a DFT matrix. Another advantage of MatrixObj objects is that it is not necessary to issue a "clear classes" command when their Ops methods need to be edited or redefined.
The DataObj subclass is a specialized version of MatrixObj well-suited for mimicking/modifying the behavior of existing MATLAB numeric data types. Its Ops property contains default methods appropriate to existing data types, but which can be selectively overwritten. Example 4 below illustrates its use.
EXAMPLE 1: Implementing fft() in operator form. As is well-known, the operation fft(x) can be represented as a matrix-vector multiplication. If you have the Signal Processing Toolbox, the relevant matrix can be generated using the DFTMTX function. Otherwise, it can be generated as follows,
Q=fft(eye(d)); %DFT matrix - 2500x2500
The operation fft(x) is equivalent to Q*x, but this is a slow way to perform the operation,
tic; y0=Q*x; toc %Elapsed time is 3.595847 seconds.
However, using the MatrixObj class, we can quickly create an object Qobj which can transform x using the same matrix multiplication syntax, Qobj*x, but which uses fft() under the hood, with all of its advantages in speed,
Qobj.Ops.mtimes=@(obj,z) fft(z); %set the mtimes method in 1 line!!
tic; y1=Qobj*x; toc %Elapsed time is 0.212282 seconds.
tic; y2=fft(x); toc %Elapsed time is 0.212496 seconds.
isequal(y1,y2); % =1
And of of course, the memory footprint of Qobj is far less than for the full matrix Q
>>whos Q Qobj
Name Size Bytes Class Attributes
Q 2500x2500 100000000 double complex
Qobj 1x1 4412 MatrixObj
EXAMPLE 2: Continuing with Example 1, suppose I now decide that I still want Qobj to represent an fft() operation, but that it be normalized to satisfy Parseval's theorem. A simple on-the-fly redefinition of mtimes() can accomplish this.
TestParseval=[norm(x), norm(Qobj*x)], % =[28.2807, 28.2807]
EXAMPLE 3: Continuing with Example 2, let us now look at how to give Qobj a ctranspose method so that Qobj' is defined. Because Qobj satisfies Parseval's theorem, Qobj' is its inverse. A one-line definition can be made using the Trans property,
Qobj.Trans.mtimes=@(obj,z) sqrt(numel(z))*ifft(z) ;
The code below verifies that the ctranpose operation has various anticipated properties,
TestParseval=[norm(x), norm(Qobj'*x)], % =[28.2807, 28.2807]
AdjointOfAdjoint=isequal(Qobj*x, (Qobj')'*x), % =1
InversionErrorLeft=norm(x- Qobj'*(Qobj*x)), % =8.4315e-015
InversionErrorRight=norm(x- Qobj*(Qobj'*x)), % =7.9086e-015
EXAMPLE 4:The following is an example of the DataObj subclass. Here, we use it to create a specialized array type which invokes bsxfun() for certain operations. This can be a useful way of circumventing bsxfun's lengthy functional syntax. Other operations like mtimes have default implementations.
P.Ops.plus= @(A,B) bsxfun(@plus,A,B);
S=P*Q, %This uses a natural default
Matt J (2021). On-The-Fly Definition of Custom Matrix Objects (https://www.mathworks.com/matlabcentral/fileexchange/26611-on-the-fly-definition-of-custom-matrix-objects), MATLAB Central File Exchange. Retrieved .
Yes, the main application IMO is simplifying syntax, creating a sort of mini-language. Another good use would be creating a matrix that self-validates on any operations, for instance a "positive-definite matrix" that raises an error if this condition is not verified. This kind of use is what motivated my question earlier :)
Thanks Joao. For operating on small data sizes, native code could definitely reduce overhead. In situations like those, the class would be best used just for simplifying syntax. For large data sizes, of course, the processing of the data become the primary bottleneck. In those cases, overhead from the class interface can be negligible, as for instance the FFT operator (Example 1 above) showed.
Nice and useful! One question, do you think a native code implementation would minimize the overhead significantly? Or the bottleneck here is the call to an anonymous function?
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!