-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.c
268 lines (238 loc) · 5.94 KB
/
utils.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* utils.c
*
* C utility functions for yao
*
* This file contains a number of utility functions, coded in C to gain
* execution time. It addresses functionalities that are missing in
* yorick, mostly concerning 2D image processing.
*
* This file is part of the yao package, an adaptive optics simulation tool.
*
* Copyright (c) 2002-2017, Francois Rigaut
*
* This program 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 2 of the License, or (at your
* option) any later version.
*
* This program 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 (to receive a copy of the GNU
* General Public License, write to the Free Software Foundation, Inc., 675
* Mass Ave, Cambridge, MA 02139, USA).
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "ydata.h"
#include "yapi.h"
#include "pstdlib.h"
/************************************************************************
* noop. For testing and timing. with parameter passing *
************************************************************************/
int _mynoop2(float *in, int nx, int ny, float *out, int fx, int fy, int binfact)
{
return(0);
}
void Y_usleep(int nArgs)
{
long milliseconds = YGetInteger(sp-nArgs+1);
// useconds_t us;
// us = (useconds_t)(milliseconds*1000l);
usleep(milliseconds*1000l);
}
int _cosf(float *x, long n)
{
long i = 0;
for (i=0;i<n;++i) {
x[i]=cos(x[i]);
}
return (0);
}
int _sinf(float *x, long n)
{
long i = 0;
for (i=0;i<n;++i) {
x[i]=sin(x[i]);
}
return (0);
}
// #ifdef __APPLE__
/* This is supposed to patch the issue of linking to a dynamic lib
in OsX. */
void ran1init()
{
long seed=0;
srandom(seed); /* WARNING! this might be platform specific */
}
float ran1()
{
float norm;
norm = 2147483647.f;
return random()/norm;
}
void _poidev(float *xmv, long n)
/* all floats -> doubles on June 2010 to avoid SIGFPE
for too large input values */
{
double gammln(double xx);
/* float ran1(long *idum);*/
static double sq,alxm,g,oldm=(-1.0);
double xm,em,t,y;
long i;
for (i=0;i<n;i++) {
xm = (double)xmv[i];
if (xm == 0.0f) continue;
if (xm < 20.0) { /* Use direct method. */
if (xm != oldm) {
oldm=xm;
g=exp(-xm); /* If xm is new, compute the exponential. */
}
em = -1;
t=1.0;
do {
++em;
t *= ran1();
} while (t > g);
} else { /* Use rejection method. */
if (xm != oldm) {
oldm=xm;
sq=sqrt(2.0*xm);
alxm=log(xm);
g=xm*alxm-gammln(xm+1.0);
}
do {
do {
y=tan(3.1415926535897932384626433832*ran1());
em=sq*y+xm;
} while (em < 0.0);
em=floor(em);
t=0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g);
} while (ran1() > t);
}
xmv[i] = (float)em;
}
}
double gammln(double xx)
{
/* Returns the value ln[?(xx)] for xx>0. */
double x,y,tmp,ser;
static double cof[6]={76.18009172947146,-86.50532032941677,
24.01409824083091,-1.231739572450155,
0.1208650973866179e-2,-0.5395239384953e-5};
int j;
y=x=xx;
tmp=x+5.5;
tmp -= (x+0.5)*log(tmp);
ser=1.000000000190015;
for (j=0;j<=5;j++) ser += cof[j]/++y;
return -tmp+log(2.5066282746310005*ser/x);
}
void _gaussdev(float *xmv, long n)
{
/* Returns a normally distributed deviate with zero mean and unit variance,
using ran1() as the source of uniform deviates. */
/* float ran1(long *idum); */
static int iset=0;
static float gset;
float fac,rsq,v1,v2;
long i;
for (i=0;i<n;i++) {
if (iset == 0) {
do {
v1=2.0*ran1()-1.0;
v2=2.0*ran1()-1.0;
rsq=v1*v1+v2*v2;
} while (rsq >= 1.0 || rsq == 0.0);
fac=sqrt(-2.0*log(rsq)/rsq);
gset=v1*fac;
iset=1;
xmv[i] = v2*fac;
} else {
iset=0;
xmv[i] = gset;
}
}
}
/************************************************************************
* Function _eclat *
* Returns results identical to roll(), but faster, as it is dedicated *
* to swapping quadrants, for use with FFTs. *
* Warning: In-place swapping.
* Last modified: December 15, 2003. *
* Author: F.Rigaut *
************************************************************************/
void _eclat_long(long *ar, int nx, int ny)
{
int i,j,k1,k2;
long a;
for ( i=0 ; i<(nx/2) ; ++i ) {
for ( j=0 ; j<(ny/2) ; ++j ) {
k1 = i+j*nx;
k2 = (i+nx/2)+(j+ny/2)*nx;
a = ar[k1];
ar[k1] = ar[k2];
ar[k2] = a;
}
}
for ( i=(nx/2) ; i<nx ; ++i ) {
for ( j=0 ; j<(ny/2) ; ++j ) {
k1 = i+j*nx;
k2 = (i-nx/2)+(j+ny/2)*nx;
a = ar[k1];
ar[k1] = ar[k2];
ar[k2] = a;
}
}
}
void _eclat_float(float *ar, int nx, int ny)
{
int i,j,k1,k2;
float a;
for ( i=0 ; i<(nx/2) ; ++i ) {
for ( j=0 ; j<(ny/2) ; ++j ) {
k1 = i+j*nx;
k2 = (i+nx/2)+(j+ny/2)*nx;
a = ar[k1];
ar[k1] = ar[k2];
ar[k2] = a;
}
}
for ( i=(nx/2) ; i<nx ; ++i ) {
for ( j=0 ; j<(ny/2) ; ++j ) {
k1 = i+j*nx;
k2 = (i-nx/2)+(j+ny/2)*nx;
a = ar[k1];
ar[k1] = ar[k2];
ar[k2] = a;
}
}
}
void _eclat_double(double *ar, int nx, int ny)
{
int i,j,k1,k2;
double a;
for ( i=0 ; i<(nx/2) ; ++i ) {
for ( j=0 ; j<(ny/2) ; ++j ) {
k1 = i+j*nx;
k2 = (i+nx/2)+(j+ny/2)*nx;
a = ar[k1];
ar[k1] = ar[k2];
ar[k2] = a;
}
}
for ( i=(nx/2) ; i<nx ; ++i ) {
for ( j=0 ; j<(ny/2) ; ++j ) {
k1 = i+j*nx;
k2 = (i-nx/2)+(j+ny/2)*nx;
a = ar[k1];
ar[k1] = ar[k2];
ar[k2] = a;
}
}
}
// #endif