-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathget_PHI_INV_chart.cpp
82 lines (72 loc) · 2.08 KB
/
get_PHI_INV_chart.cpp
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
extern "C"
{
#include <stdlib.h>
#include <assert.h>
#include <string.h>
}
#include <iostream>
#include <fstream>
#include <cmath>
#include <mex.h>
static const double ALFA=-0.4527;
static const double BETA= 0.0218;
static const double GAMA= 0.86;
static const double PI= 3.1415926535897932384626433832795;
double Phi(double x)
{
//assert x>0
assert(x>=0);
if(x<=10) {
return exp(ALFA*pow(x,GAMA)+BETA);
}
else if(x>10) {
return sqrt(PI/x)*exp(-x/4)*(1+1/(14*x)-3/(2*x));
}
}
/*
double Phi_inv(double y, double phi_inv_in)
{
assert(y>=0);
double * pItem;
int index;
pItem = (double*) bsearch (&y, phi_inv_in, PHI_SAMPLE_LENGTH, sizeof (double), compare);
index = pItem-phi_inv_in;
return (pItem!=NULL) ? (phi_inv_out[index]): PHI_SAMPLE_RANGE;
}
*/
/*
int compare (const void * kp, const void * vp)
{
//key is the item I want to search
double key = *(const double*)kp;
const double *vec = (const double*)vp;
//////////////////////////////////////////////////
// vec[-1] vec[0] vec[1]
// | | |
// | |
// (vec[-1]+vec[0])/2.0 (vec[0]+vec[1])/2.0
//////////////////////////////////////////////////
if (key > (vec[0]+vec[1])/2.0)
return 1;
else if (key < (vec[-1]+vec[0])/2.0)
return -1;
else
return 0;
}
*/
void mexFunction(int nlhs,mxArray *plhs[], int nrhs, const mxArray * prhs[])
{
//parameters are PHI_SAMPLE_LENGTH, PHI_SAMPLE_RANGE
int PHI_SAMPLE_LENGTH = mxGetScalar(prhs[0]);
double PHI_SAMPLE_RANGE = mxGetScalar(prhs[1]);
double PHI_SAMPLE_STEP = PHI_SAMPLE_RANGE/PHI_SAMPLE_LENGTH;
plhs[0]=mxCreateDoubleMatrix(1,PHI_SAMPLE_LENGTH,mxREAL);
plhs[1]=mxCreateDoubleMatrix(1,PHI_SAMPLE_LENGTH,mxREAL);
double *phi_inv_in=mxGetPr(plhs[0]);
double *phi_inv_out=mxGetPr(plhs[1]);
for (int i=0;(PHI_SAMPLE_RANGE-i*PHI_SAMPLE_STEP)>0;i++)
{
phi_inv_out[i]=PHI_SAMPLE_RANGE-i*PHI_SAMPLE_STEP;
phi_inv_in[i]=Phi(phi_inv_out[i]);
}
}