|
Hi James Tursa,
Thanks so much!
I believe that I have solve the current problem on myself, although without finishing the code.
--------------My Purpose -------------------------------------
solve X from A*X = B;
where A and B is supposed to be a page of ND-Array.
--------------My "Collected" Code --------------------------
#include "mex.h"
#include "matrix.h"
#include <stdlib.h>
#include "blas.h"
#include <math.h>
#include <string.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *lhs[1];
mxArray *x[2];
double *A, *B, *C, one = 1.0, zero = 0.0;
mwSize numDimsA, numDimsB, numDimsC, numPagesA=1, numPagesB =1;
const mwSize *dimsA=NULL, *dimsB=NULL;
mwSize *dimsC;
mwSize strideA, strideB, strideC;
mwSize m, n, p, pB;
//ptrdiff_t m, n, p, pB;
// mwSignedIndex m, n, p, pB;
char *chn = "N";
int i=0, PagesFlagA=100,PagesFlagB = 100;
numDimsA = mxGetNumberOfDimensions(prhs[0]);
numDimsB = mxGetNumberOfDimensions(prhs[1]);
dimsA = mxGetDimensions(prhs[0]);
dimsB = mxGetDimensions(prhs[1]);
m = dimsA[0];
p = dimsA[1];
pB = dimsB[0];
n = dimsB[1];
//Calculate number of pages of A
if(numDimsA == 2){
numPagesA = 1;
PagesFlagA = 0; // '0' stands for A is 2-D
}else if(numDimsA > 2){
for(i=2; i<numDimsA; i++){
numPagesA *= dimsA[i];
}
}else{
mexErrMsgTxt("input A and B should not be sclars");
} //end of if
//Calculate number of pages of B
if(numDimsB == 2){
numPagesB = 1;
PagesFlagB = 0; // '0' stands for A is 2-D
}
else if(numDimsB > 2){
for(i=2; i<numDimsB; i++){
numPagesB *= dimsB[i];
}
};
/*for double check*/
/*mexPrintf("A is %d\t by %d\t by %d \n",dimsA[0], dimsA[1],dimsA[2]);
mexPrintf("B is %d\t by %d\t by %d \n",dimsB[0], dimsB[1],dimsB[2]);*/
if ( p != pB){
mexErrMsgTxt("Inner dimensions of matrix multiply do not match");
}
if ( m != p){
mexErrMsgTxt("A is un-invertable!");
}
strideA = m*p;
strideB = p*n;
strideC = m*n;
m = (ptrdiff_t)(m);
p = (ptrdiff_t)(p);
n = (ptrdiff_t)(n);
x[0] = mxCreateDoubleMatrix(m, m, mxREAL);
x[1] = mxCreateDoubleMatrix(m, n, mxREAL);
x[0] = (mxArray*)prhs[0];
x[1] = (mxArray*)prhs[1];
mexCallMATLAB(1, lhs, 2, x, "mldivide");
} // End mexFunction
--------------I have modified code as fellowing --------------------------
x[0] = mxCreateDoubleMatrix(m, m, mxREAL); // add the code today
x[1] = mxCreateDoubleMatrix(m, n, mxREAL); // add the code today
x[0] = (mxArray*)prhs[0];
x[1] = (mxArray*)prhs[1];
The code I add to mexFunction correct my last error.
------------- What I will do in the further --------------------------
I confused with How "C" allocation of memory as a starter.
I will carry out my plan by call function in LAPACK/BLAS. Such as DGETRF and DLACN2.
Thanks so much for your kindness.
Lin Renwen
|