core/matlab/examples/mx/mxisfinite.c

00001 /*=================================================================
00002  * mxisfinite.c 
00003  *
00004  * mxisfinite takes one input argument of type double. It returns one
00005  * output argument of type integer (32 bits) with equivalent values.
00006  * Values greater than the integer maximum or less than the integer
00007  * minimum are stored as INT_MAX/INT_MIN respectively.  NaNs in the
00008  * input argument are flagged as a warning and stored as zero.
00009  *
00010  * This is a MEX-file for MATLAB.  
00011  * Copyright 1984-2006 The MathWorks, Inc.
00012  * All rights reserved.
00013  *=================================================================*/
00014 /* $Revision: 1.1 $ */
00015 #include <limits.h>
00016 #include "mex.h"
00017 
00018 /* Function that converts double to int32 */
00019 static int dtoi32(double d)
00020 {
00021     int i=0;
00022     
00023     if(mxIsFinite(d)) {
00024     if(d < (double)INT_MAX && d > (double)INT_MIN) {
00025         i = (int) d;
00026     } else {
00027         i =  ((d > 0) ? INT_MAX : INT_MIN);
00028     }       
00029     } else if(mxIsInf(d)) {
00030     i = ( (d > 0) ? INT_MAX : INT_MIN);
00031     /* NOTE: Test for NaN is here for illustration only.  If a double is
00032        not finite and is not infinity, then it is a NaN */
00033     } else if(mxIsNaN(d)) {
00034     mexWarnMsgTxt("dtoi32: NaN detected.  Translating to 0.\n");
00035     i = 0;
00036     }
00037     return i;
00038 }
00039 
00040 void mexFunction(
00041          int nlhs,       mxArray *plhs[],
00042                  int nrhs, const mxArray *prhs[]
00043          )
00044 {
00045     mwSize i, n;
00046     double *pr, *pi;
00047     int *pri32, *pii32;
00048     
00049     
00050     /* Check for proper number of input and output arguments */    
00051     if (nrhs != 1) {
00052     mexErrMsgTxt("One input argument required.");
00053     } 
00054     if(nlhs > 1){
00055     mexErrMsgTxt("Too many output arguments.");
00056     }
00057     
00058     /* Check data type of input argument  */
00059     if (!(mxIsDouble(prhs[0]))){
00060     mexErrMsgTxt("Input argument must be of type double.");
00061     }   
00062 
00063     if(mxIsEmpty(prhs[0])) {
00064     mexWarnMsgTxt("Input argument is empty\n");
00065     }
00066 
00067     pr = mxGetPr(prhs[0]);
00068     pi = mxGetPi(prhs[0]);
00069     n = mxGetNumberOfElements(prhs[0]);
00070 
00071     /* Create numeric array of class mxINT32 */
00072     plhs[0] = mxCreateNumericArray(mxGetNumberOfDimensions(prhs[0]), mxGetDimensions(prhs[0]),
00073         mxINT32_CLASS, (mxIsComplex(prhs[0]) ? mxCOMPLEX : mxREAL));
00074     pri32 = mxGetData(plhs[0]);
00075     pii32 = mxGetImagData(plhs[0]);
00076 
00077     /* Convert each element of the real part of the input argument to
00078        a INT32 */
00079     for(i=0; i < n; i++) {
00080     pri32[i] = dtoi32(pr[i]);
00081     }
00082     
00083     /* If there is an imaginary part of the input, convert each
00084        element of the imaginary part of the input argument to a INT32. */
00085     if(pii32 != NULL) { 
00086     /* Initial assumption is that imaginary part might be empty */
00087     bool empty_image_data = true;
00088     for(i=0; i < n; i++) {
00089         pii32[i] = dtoi32(pi[i]);
00090         if(pii32[i] != 0) {
00091         empty_image_data = false;
00092         }
00093     }
00094     /* If imaginary part is empty, free the memory and set the
00095            data to NULL. */
00096     if (empty_image_data) {
00097         mxFree(pii32);
00098         mxSetImagData(plhs[0], NULL);
00099     }
00100     }
00101 }

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy