📄 maxx.c
字号:
# include <math.h># include "mex.h"/*********************************************************************** * maxx.c - * * Function that takes two scalars x and y, and calculates: * * maxx_out = max(x,y) + ln(1 + exp(-abs(x-y))); * * This is a MEX-file for MATLAB. * * Author: MVe * Date: 12.08.2002 * ***********************************************************************//********************** C subroutine 'maxx'***********************/void maxx(double *maxx_out, double x, double y){ double max_xy,exp_xy = 0.0; /* <math.h> provides functions: * double exp(double x) -- Compute exponential of x * double log(double x) -- Compute (base-e logarithm) log(x) * double fabs(double x) -- Compute absolute value of x */ /* max_xy = max(x,y) */ max_xy = (x>y) ? x : y; /* exp_xy = exp(-abs(x-y)) */ exp_xy = exp(-fabs(x-y)); /* Final result */ *maxx_out = max_xy + log(1+exp_xy); }/****************************** The MATLAB gateway routine *******************************/void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ /* Pointer to output */ double *maxx_out; /* Input variables */ double x,y; /* Check for proper number of arguments. */ /* NOTE: You do not need an else statement when using mexErrMsgTxt within an if statement. It will never get to the else statement if mexErrMsgTxt is executed. (mexErrMsgTxt breaks you out of the MEX-file.) */ if(nrhs!=2) mexErrMsgTxt("Two inputs required."); if(nlhs>1) mexErrMsgTxt("Too many outputs."); /* Check to make sure the that the input arguments are scalars. */ if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || mxGetN(prhs[0])*mxGetM(prhs[0])!=1 || !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || mxGetN(prhs[1])*mxGetM(prhs[1])!=1) { mexErrMsgTxt("Inputs x and y must be a scalars."); } /* Get the scalar input x. */ x = mxGetScalar(prhs[0]); /* Get the scalar input y. */ y = mxGetScalar(prhs[1]); /* Set the output pointer to the output. */ plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); /* Create a C pointer to a copy of the output. */ maxx_out = mxGetPr(plhs[0]); /* Call the C subroutine. */ maxx(maxx_out,x,y);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -