Best way of handling NaN in MATLAB/MEX - matlab

I am using solvers of LAPACK libraries in a MATLAB MEX file for solving linear system of equations. For some of the cases, the system that I solve is singular. For example, the system is as follows for one of my cases:
A =
0.00000000 0.00000000 0.00000000
0.00000000 0.00000000 0.00000000
0.00000000 0.00000000 77.31867171
b:
-0.00000000 -0.00000000 -0.00000000
What would be the best approach to label the solution of Ax=b of the above system as NaN similar to MATLAB?

Here is an example to create a numeric vector filled with NaNs from a MEX-function:
test_nan.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
plhs[0] = mxCreateDoubleMatrix(3, 1, mxREAL);
double *x = mxGetPr(plhs[0]);
double nanVal = mxGetNaN();
for (int i=0; i<3; ++i) {
x[i] = nanVal;
}
}
MATLAB
>> mex -largeArrayDims test_nan.cpp
>> x = test_nan()
x =
NaN
NaN
NaN

Related

How to dynamically create an array of mxArray in a MATLAB mex file

I am trying to dynamically declare an array of mxArray in a MATLAB mex file.
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
#define PRHS_NLEVELS prhs[0]
double *NLevel = mxGetPr( PRHS_NLEVELS );
int nLevel = (int) NLevel [0];
mxArray *Ain = (mxArray *) mxMalloc( nLevel * sizeof(mxArray) );
}
An image will be input to this mex function and Ain will store its pyramidal images at different levels. The compilation error I get is as follows:
mymex.cpp(59) : error C2027: use of undefined type 'mxArray_tag'
c:\program files\matlab\r2012b\extern\include\matrix.h(299) : see declaration of 'mxArray_tag'
I think you just want an array of mxArray*s, which is straightforward with new or malloc. You can't have an array of mxArrays, only pointers to them.
The mxArray comes from typedef struct mxArray_tag mxArray;, where mxArray_tag is not defined, hidden in the MathWorks implementation. Thus, you can't even do something as simple as mxArray x[3]; because it is an incomplete type. You can only deal with pointers to the object.
testMxArrayMEX.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int numLevels = 3;
//mxArray **matLevels = (mxArray **) malloc(numLevels * sizeof(mxArray**));
mxArray **matLevels = new mxArray*[numLevels];
matLevels[0] = mxCreateDoubleMatrix(2, 2, mxREAL);
matLevels[1] = mxCreateString("second");
matLevels[2] = mxCreateCellMatrix(1, 2);
mxSetCell(matLevels[2], 0, mxCreateString("third"));
mexPrintf("First:\n");
mexCallMATLAB(0, NULL, 1, matLevels, "disp");
mexPrintf("\nSecond:\n");
mexCallMATLAB(0, NULL, 1, &matLevels[1], "disp");
mexPrintf("\nThird:\n");
mexCallMATLAB(0, NULL, 1, matLevels+2, "disp");
// free(matLevels); // with malloc
delete[] matLevels; // with new
}
Output
>> testMxArrayMEX
First:
0 0
0 0
Second:
second
Third:
'third' []
You could create a cell array of matrices, and return that from the MEX-function.
Example:
test_cell_array.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
plhs[0] = mxCreateCellMatrix(1, 5);
for (mwIndex c=0; c<5; c++) {
mxArray *arr = mxCreateDoubleMatrix(3, 3, mxREAL);
double *x = mxGetPr(arr);
for (mwIndex i=0; i<9; i++) {
x[i] = c;
}
mxSetCell(plhs[0], c, arr);
}
}
MATLAB
>> c = test_cell_array()
c =
[3x3 double] [3x3 double] [3x3 double] [3x3 double] [3x3 double]
>> c{3}
ans =
2 2 2
2 2 2
2 2 2
In your case, each cell will contain the image blurred and resized at different levels to create the pyramid of images, and the number of levels would be specified as function input.

Most Efficient Way of Using mexCallMATLAB in Converting Double* to mxArray*

I am writing a MEX code in which I need to use pinv function. I am trying to find a way to pass the array of type double to pinv using mexCallMATLAB in the most efficient way. Let's for the sake of example say the array is named G and its size is 100.
double *G = (double*) mxMalloc( 100 * sizeof(double) );
where
G[0] = G11; G[1] = G12;
G[2] = G21; G[3] = G22;
Which means every four consecutive elements of G is a 2×2 matrix. G stores 25 different values of this 2×2 matrix.
I should note that these 2×2 matrices are not well-conditioned and they may contain all zero in their element. How can I use pinv function to calculate the pseudoinverse in the elements of G? For example, how can I pass the array to mexCallMATLAB in order to calculate the pseudoinverse of the first 2×2 matrix in G?
I thought of the following approach:
mxArray *G_PINV_input = mxCreateDoubleMatrix(2, 2, mxREAL);
mxArray *G_PINV_output = mxCreateDoubleMatrix(2, 2, mxREAL);
double *G_PINV_input_ptr = mxGetPr(G_PINV_input);
memcpy( G_PINV_input_ptr, &G[0], 4 * sizeof(double));
mexCallMATLAB(1, G_PINV_output, 1, G_PINV_input, "pinv");
I am not sure how good this approach is. Copying the values is not economical at all because the total number of elements in G in my actual application is large. Is there anyway to skip this copying?
Here is my implementation of the MEX-function:
my_pinv.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
// validate arguments
if (nrhs!=1 || nlhs>1)
mexErrMsgIdAndTxt("mex:error", "Wrong number of arguments");
if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || mxIsSparse(prhs[0]))
mexErrMsgIdAndTxt("mex:error", "Input isnt real dense double array");
if (mxGetNumberOfElements(prhs[0]) != 100)
mexErrMsgIdAndTxt("mex:error", "numel() != 100");
// create necessary arrays
mxArray *rhs[1], *lhs[1];
plhs[0] = mxCreateDoubleMatrix(100, 1, mxREAL);
rhs[0] = mxCreateDoubleMatrix(2, 2, mxREAL);
double *in = mxGetPr(prhs[0]);
double *out = mxGetPr(plhs[0]);
double *x = mxGetPr(rhs[0]), *y;
// for each 2x2 matrix
for (mwIndex i=0; i<100; i+=4) {
// copy 2x2 matrix into rhs
x[0] = in[i+0];
x[2] = in[i+1];
x[1] = in[i+2];
x[3] = in[i+3];
// lhs = pinv(rhs)
mexCallMATLAB(1, lhs, 1, rhs, "pinv");
// copy 2x2 matrix from lhs
y = mxGetPr(lhs[0]);
out[i+0] = y[0];
out[i+1] = y[1];
out[i+2] = y[2];
out[i+3] = y[3];
// free array
mxDestroyArray(lhs[0]);
}
// cleanup
mxDestroyArray(rhs[0]);
}
Here is a baseline implementation in MATLAB so that we can verify the results are correct:
my_pinv0.m
function y = my_pinv0(x)
y = zeros(size(x));
for i=1:4:numel(x)
y(i:i+3) = pinv(x([0 1; 2 3]+i));
end
end
Now we test the MEX-function:
% some vector
x = randn(100,1);
% MEX vs. MATLAB function
y = my_pinv0(x);
yy = my_pinv(x);
% compare
assert(isequal(y,yy))
EDIT:
Here is an another implementation:
my_pinv2.cpp
#include "mex.h"
inline void call_pinv(const double &a, const double &b, const double &c,
const double &d, double *out)
{
mxArray *rhs[1], *lhs[1];
// create input matrix [a b; c d]
rhs[0] = mxCreateDoubleMatrix(2, 2, mxREAL);
double *x = mxGetPr(rhs[0]);
x[0] = a;
x[1] = c;
x[2] = b;
x[3] = d;
// lhs = pinv(rhs)
mexCallMATLAB(1, lhs, 1, rhs, "pinv");
// get values from output matrix
const double *y = mxGetPr(lhs[0]);
out[0] = y[0];
out[1] = y[1];
out[2] = y[2];
out[3] = y[3];
// cleanup
mxDestroyArray(lhs[0]);
mxDestroyArray(rhs[0]);
}
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
// validate arguments
if (nrhs!=1 || nlhs>1)
mexErrMsgIdAndTxt("mex:error", "Wrong number of arguments");
if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || mxIsSparse(prhs[0]))
mexErrMsgIdAndTxt("mex:error", "Input isnt real dense double array");
if (mxGetNumberOfElements(prhs[0]) != 100)
mexErrMsgIdAndTxt("mex:error", "numel() != 100");
// allocate output
plhs[0] = mxCreateDoubleMatrix(100, 1, mxREAL);
double *out = mxGetPr(plhs[0]);
const double *in = mxGetPr(prhs[0]);
// for each 2x2 matrix
for (mwIndex i=0; i<100; i+=4) {
// 2x2 input matrix [a b; c d], and its determinant
const double a = in[i+0];
const double b = in[i+1];
const double c = in[i+2];
const double d = in[i+3];
const double det = (a*d - b*c);
if (det != 0) {
// inverse of 2x2 matrix [d -b; -c a]/det
out[i+0] = d/det;
out[i+1] = -c/det;
out[i+2] = -b/det;
out[i+3] = a/det;
}
else {
// singular matrix, fallback to pseudo-inverse
call_pinv(a, b, c, d, &out[i]);
}
}
}
This time we compute the determinant of the 2x2 matrix, if is non-zero, we calculate the inverse ourselves according to:
Otherwise we fallback to invoking PINV from MATLAB for the pseudo-inverse.
Here is quick benchmark:
% 100x1 vector
x = randn(100,1); % average case, with normal 2x2 matrices
% running time
funcs = {#my_pinv0, #my_pinv1, #my_pinv2};
t = cellfun(#(f) timeit(#() f(x)), funcs, 'Uniform',true);
% compare results
y = cellfun(#(f) f(x), funcs, 'Uniform',false);
assert(isequal(y{1},y{2}))
I get the following timings:
>> fprintf('%.6f\n', t);
0.002111 % MATLAB function
0.001498 % first MEX-file with mexCallMATLAB
0.000010 % second MEX-file with "unrolled" matrix inverse (+ PINV as fallback)
The error is acceptable and within machine precision:
>> norm(y{1}-y{3})
ans =
2.1198e-14
You could also test the worst case, when many of the 2x2 matrices are singular:
x = randi([0 1], [100 1]);
You don't need to allocate the output. Just make the pointer and let pinv create the mxArray automatically.
mxArray *lhs;
Then just use & like,
mexCallMATLAB(1, &lhs, 1, &rhs, "pinv");

Can a MATLAB Mex function accept both single and doubles?

I have a mex function that accepts double matrices as the input, but I just realized that the code this function is used for, can have single precision matrices as well. Is it possible to allow the function to accept either?
Or if not, what is an alternative way to fix this issue?
The simple solution would be to convert the inputs in MATLAB to a consistent type (presumably double), but if you would like to have your MEX function handle multiple types, here is one way.
Check the input type with mxIsSingle and mxIsDouble (or mxIsClass) and handle it accordingly. You might have an ifstatement in mexFunction that sets up the inputs and outputs and then calls a template function. See the example below, which thresholds all the values in an array using the C++ standard library function template std::min<T> without requiring any data conversion.
flexibleFunction.cpp
#include "mex.h"
#include <algorithm> // std::min
template <typename T>
void threshArrayLT(T *out, const T *arr, mwSize n, T c)
{ // you allocate out
for (mwSize i = 0; i < n; ++i)
out[i] = std::min<T>(arr[i], c);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nlhs > 1 || nrhs != 2)
mexErrMsgTxt("Syntax:\n\tH = flexibleFunction(arr,c)");
if (!mxIsDouble(prhs[0]) && !mxIsSingle(prhs[0]))
mexErrMsgTxt("Array must be double or single.");
if ((mxIsDouble(prhs[0]) && !mxIsDouble(prhs[1])) ||
(mxIsSingle(prhs[0]) && !mxIsSingle(prhs[1])))
mexErrMsgTxt("Arguments must have same type.");
const mwSize* dims = mxGetDimensions(prhs[0]);
int ndims = static_cast<int>(mxGetNumberOfDimensions(prhs[0]));
size_t numel = mxGetNumberOfElements(prhs[0]);
if (mxIsDouble(prhs[0])) {
double *arr = mxGetPr(prhs[0]);
double c = mxGetScalar(prhs[1]);
plhs[0] = mxCreateNumericArray(ndims,dims,mxDOUBLE_CLASS,mxREAL);
threshArrayLT(mxGetPr(plhs[0]),arr,numel,c);
// In reality, if it's this simple, use std::transform with lambda or bind:
//std::transform(arr, arr+numel, mxGetPr(plhs[0]),
// [&](double s){ return std::min(s,c); });
} else if (mxIsSingle(prhs[0])) {
float *arr = (float*)mxGetData(prhs[0]);
float c = static_cast<float>(mxGetScalar(prhs[1]));
plhs[0] = mxCreateNumericArray(ndims,dims,mxSINGLE_CLASS,mxREAL);
threshArrayLT((float*)mxGetData(plhs[0]),arr,numel,c);
}
}
You can also use function overloading in C++ (same name, different argument types).
Example
>> v = rand(1,8); c = 0.5;
>> whos v c
Name Size Bytes Class Attributes
c 1x1 8 double
v 1x8 64 double
>> flexibleFunction(v,c)
ans =
0.2760 0.5000 0.5000 0.1626 0.1190 0.4984 0.5000 0.3404
>> flexibleFunction(single(v),single(c))
ans =
0.2760 0.5000 0.5000 0.1626 0.1190 0.4984 0.5000 0.3404

how to create a single float sparse matrix in mex files

This Creating sparse matrix in MEX has a good example on mxCreateSparse. But this function return a double sparse matrix instead of single. If I want to return a single sparse matrix, what should I do ? Thanks !
As #horchler suggested, you could use the undocumented function mxCreateSparseNumericMatrix. Example:
singlesparse.c
#include "mex.h"
#include <string.h> /* memcpy */
/* undocumented function prototype */
EXTERN_C mxArray *mxCreateSparseNumericMatrix(mwSize m, mwSize n,
mwSize nzmax, mxClassID classid, mxComplexity ComplexFlag);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const float pr[] = {1.0, 7.0, 5.0, 3.0, 4.0, 2.0, 6.0};
const mwIndex ir[] = {0, 2, 4, 2, 3, 0, 4};
const mwIndex jc[] = {0, 3, 5, 5, 7};
const mwSize nzmax = 10;
const mwSize m = 5;
const mwSize n = 4;
plhs[0] = mxCreateSparseNumericMatrix(m, n, nzmax, mxSINGLE_CLASS, mxREAL);
memcpy((void*)mxGetPr(plhs[0]), (const void*)pr, sizeof(pr));
memcpy((void*)mxGetIr(plhs[0]), (const void*)ir, sizeof(ir));
memcpy((void*)mxGetJc(plhs[0]), (const void*)jc, sizeof(jc));
}
Usage:
>> mex -largeArrayDims singlesparse.c
>> s = singlesparse()
s =
(1,1) 1
(3,1) 7
(5,1) 5
(3,2) 3
(4,2) 4
(1,4) 2
(5,4) 6
>> ss = double(s);
>> whos s ss
Name Size Bytes Class Attributes
s 5x4 160 single sparse
ss 5x4 152 double sparse
>> f = full(s)
One or more output arguments not assigned during call to "full".
>> f = full(ss)
f =
1 0 0 2
0 0 0 0
7 3 0 0
0 4 0 0
5 0 0 6
>> s + s;
Undefined function 'plus' for input arguments of type 'single' and attributes 'sparse 2d real'.
>> ss + ss;
>> 2 * s;
Error using *
Undefined function 'times' for input arguments of type 'single' and attributes 'sparse 2d real'.
>> 2 * ss;
>> s * s';
Error using *
MTIMES is not supported for one sparse input and one single input.
>> ss * ss';
>> nnz(s)
ans =
7
>> nzmax(s)
ans =
10
>> dmperm(s)
Undefined function 'dmperm' for input arguments of type 'single'.
>> dmperm(ss)
ans =
1 3 0 5
>> svds(s)
Error using horzcat
The following error occurred converting from double to single:
Error using single
Attempt to convert to unimplemented sparse type
Error in svds (line 64)
B = [sparse(m,m) A; A' sparse(n,n)];
>> svds(ss)
ans =
9.9249
5.5807
3.2176
0.0000
>> % abs(s), cos(s), sin(s), s.^2, s.*s, etc.. all give errors
As you can see, the sparse single array was created successfully, however many functions expect the array to be of type double, so there is a lot of missing functionality...
Another restriction is that you cannot create multi-dimensional sparse arrays in MATLAB, they have to be 2D matrices..
Bottom line is: stick with double sparse 2D matrices in MATLAB!

How to return a float value from a mex function, and how to retrieve it from m-file?

I understand that all the returned values of a mex function are stored in plhs array of type mxArray*. I want to return a value of type float. How can I do it?
Some code examples on returning it from the mex function and retrieving it from the m-file is much appreciated.
The MATLAB class name for float type data is "single".
In the MEX-file you could write:
void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
// Create a 2-by-3 real float
plhs[0] = mxCreateNumericMatrix(2, 3, mxSINGLE_CLASS, mxREAL);
// fill in plhs[0] to contain the same as single([1 2 3; 4 5 6]);
float * data = (float *) mxGetData(plhs[0]);
data[0] = 1; data[1] = 4; data[2] = 2;
data[3] = 5; data[4] = 3; data[5] = 6;
}
Retrieving it from the M-file is pretty much like calling any other function. If you named the MEX-function foo, you'd call it like this:
>> x = foo;
Now x would contain the single-precision value equivalent to single([1 2 3; 4 5 6]) that was stored in plhs[0].