To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at git://github.com/rmeddis/MAP.git .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Revision:

root / MAP / intpow.c @ 30:1a502830d462

History | View | Annotate | Download (2.55 KB)

1
/* intpow - Nick Clark - 3/5/2011
2
 *
3
 * Function that raises a double precision vector to an integer power 
4
 * vector. The following call to this mex function... 
5
 *
6
 * >>      z = intpow(x,y)
7
 * 
8
 * ... does the equivalent of the following in Matlab...
9
 *
10
 * >>      z = x .^ ceil(y)
11
 * 
12
 * NOTE: Under most circumstances, this function is slower than MATLAB's 
13
 * built in ^ operator, but for the (generally) small integer powers used 
14
 * in MAP we observe massive performance boosts using this C function.
15
 */
16

    
17
/*************************************************************************/
18
/* Header(s)                                                             */
19
/*************************************************************************/
20
#include "mex.h"
21

    
22
/*************************************************************************/
23
/* Input vars                                                            */
24
/*************************************************************************/
25
#define IN_x         prhs[0]
26
#define IN_y         prhs[1]
27

    
28
/*************************************************************************/
29
/* Output vars                                                           */
30
/*************************************************************************/
31
#define OUT_z         plhs[0]
32

    
33
/*************************************************************************/
34
/* Gateway function and error checking                                   */
35
/*************************************************************************/
36
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
37
{
38
    /* variable declarations */
39
    int numElements, xM, xN, yM, yN; 
40
    int nn, kk; 
41
    double *x, *y, *z;
42
     
43
    /*  check the number of input and output parameters  */  
44
    if(nrhs!=2)
45
        mexErrMsgTxt("intpow : Two input args expected");
46
    if(nlhs > 1)
47
        mexErrMsgTxt("intpow : Too many outputs");
48
    
49
    /* check that x and y have equal size */ 
50
    x = mxGetPr(IN_x);
51
    xM = mxGetM(IN_x);
52
    xN = mxGetN(IN_x);
53
    
54
    y = mxGetPr(IN_y);
55
    yM = mxGetM(IN_y);
56
    yN = mxGetN(IN_y);
57
    
58
    if  (xM != yM || xN != yN)
59
        mexErrMsgTxt("intpow : x and y must have equal size");
60
    
61
    /* find upper loop boundary */
62
    numElements = xM * xN; 
63
    
64
    /*  allocate memory and pointer for the output array */ 
65
    OUT_z = mxCreateDoubleMatrix(xM,xN,mxREAL);
66
    z = mxGetPr(OUT_z);
67
    
68
    /*  do stuff */ 
69
    for( nn = 0;  nn<numElements; ++nn )
70
    {
71
        z[nn] = 1.0;
72
        for (kk = 0; kk<y[nn]; ++kk)
73
        {
74
            z[nn] = z[nn]*x[nn];
75
        }
76
    }            
77
}