summaryrefslogtreecommitdiff
path: root/src/stk_mex.h
blob: 101e04d54d0f864481b86483ec16515c0cd36df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*****************************************************************************
 *                                                                           *
 *                  Small (Matlab/Octave) Toolbox for Kriging                *
 *                                                                           *
 * Copyright Notice                                                          *
 *                                                                           *
 *    Copyright (C) 2015 CentraleSupelec                                     *
 *    Copyright (C) 2012, 2013 SUPELEC                                       *
 *                                                                           *
 *    Author:  Julien Bect  <julien.bect@centralesupelec.fr>                 *
 *                                                                           *
 * Copying Permission Statement                                              *
 *                                                                           *
 *    This file is part of                                                   *
 *                                                                           *
 *            STK: a Small (Matlab/Octave) Toolbox for Kriging               *
 *               (https://github.com/stk-kriging/stk/)                   *
 *                                                                           *
 *    STK is free software: you can redistribute it and/or modify it under   *
 *    the terms of the GNU General Public License as published by the Free   *
 *    Software Foundation,  either version 3  of the License, or  (at your   *
 *    option) any later version.                                             *
 *                                                                           *
 *    STK is distributed  in the hope that it will  be useful, but WITHOUT   *
 *    ANY WARRANTY;  without even the implied  warranty of MERCHANTABILITY   *
 *    or FITNESS  FOR A  PARTICULAR PURPOSE.  See  the GNU  General Public   *
 *    License for more details.                                              *
 *                                                                           *
 *    You should  have received a copy  of the GNU  General Public License   *
 *    along with STK.  If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                           *
 ****************************************************************************/

#ifndef ___STK_MEX_H___
#define ___STK_MEX_H___

#include <math.h>
#include "mex.h"

#if (defined __STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
#   include <stdbool.h>
#else
#   ifndef bool
#      define bool int
#   endif
#   ifndef true
#      define true 1
#   endif
#   ifndef false
#      define false 0
#   endif
#endif

int stk_is_realmatrix(const mxArray* x)
{
  if (mxIsComplex(x) || !mxIsDouble(x))
    return 0;

  if (mxGetNumberOfDimensions(x) != 2)
    return 0;

  return 1;
}

int mxIsDoubleVector (const mxArray* x)
{
  size_t m, n;

  if (! stk_is_realmatrix (x))
    return 0;
  
  m = mxGetM (x);
  n = mxGetN (x);

  return ((m == 1) && (n > 0)) || ((n == 1) && (m > 0));
}  

void mxReplaceField
(mxArray* S, mwIndex index, const char* fieldname, const mxArray* value)
{
  mxArray *tmp, *value_copy;

  tmp = mxGetField(S, index, fieldname);
  if (tmp != NULL)
    mxDestroyArray(tmp);

  value_copy = mxDuplicateArray(value);
  if (value_copy == NULL)
    mexErrMsgTxt("mxDuplicateArray: not enough free heap "
		 "space to create the mxArray");
  
  mxSetField(S, index, fieldname, value_copy);
}

#define  STK_OK                0
#define  STK_ERROR            -1
#define  STK_ERROR_DOMAIN      1
#define  STK_ERROR_OOM         2
#define  STK_ERROR_SANITY      3

int mxReadScalar_int(const mxArray* x, int* n)
{
  double t;

  if (mxGetNumberOfElements(x) != 1)
    return STK_ERROR;
  
  if (mxIsComplex(x) || !mxIsDouble(x))
    return STK_ERROR;

  t = mxGetScalar(x);
  *n = (int) t;

  return ((((double) *n) == t) ? STK_OK : STK_ERROR);
}

#endif