1
#include "scicos_block.h"
9
int isinf(double x) { return !finite(x) && x==x; }
14
arcsinh z = log (z+sqrt(1+z2))
16
double asinh(double x)
18
return log(x+sqrt(x*x+1));
21
double acosh(double x)
23
return log(x+sqrt(x*x-1));
29
* atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
33
* atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
37
double atanh(double x)
41
return 0.5*log((1+x)/(1-x));
45
return 0.5*log((2*x)+(2*x)*x/(1-x));
51
void evaluate_expr(scicos_block *block,int flag)
53
static double stack [1000];
54
static int count,bottom,nzcr,i,phase;
56
if (flag==1||flag==9){
57
phase=get_phase_simulation();
61
while (count<block->nipar-1){
63
switch (block->ipar[count]) {
72
stack[bottom]=block->inptr[block->ipar[count]-1][0];
74
j=block->ipar[count]-1;
75
if (j<block->insz[0]){
76
stack[bottom]=block->inptr[0][block->ipar[count]-1];
89
stack[bottom]=block->rpar[block->ipar[count]-1];
93
switch (block->ipar[count]) {
95
stack[bottom-1]=stack[bottom-1]+stack[bottom];
99
stack[bottom-1]=stack[bottom-1]-stack[bottom];
103
stack[bottom-1]=stack[bottom-1]*stack[bottom];
107
stack[bottom-1]=stack[bottom-1]/stack[bottom];
111
stack[bottom-1]=pow(stack[bottom-1],stack[bottom]);
114
case 16: /* case == */
115
if(block->ng>0) nzcr=nzcr+1;
117
block->g[nzcr]=stack[bottom-1]-stack[bottom];
119
block->mode[nzcr]=(stack[bottom-1]==stack[bottom]);
122
if(phase==1||block->ng==0){
123
i=(stack[bottom-1]==stack[bottom]);
127
stack[bottom-1]=(double)i;
132
if(block->ng>0) nzcr=nzcr+1;
134
block->g[nzcr]=stack[bottom-1]-stack[bottom];
136
block->mode[nzcr]=(stack[bottom-1]<stack[bottom]);
139
if(phase==1||block->ng==0){
140
i=(stack[bottom-1]<stack[bottom]);
144
stack[bottom-1]=(double)i;
148
if(block->ng>0) nzcr=nzcr+1;
150
block->g[nzcr]=stack[bottom-1]-stack[bottom];
152
block->mode[nzcr]=(stack[bottom-1]>stack[bottom]);
155
if(phase==1||block->ng==0){
156
i=(stack[bottom-1]>stack[bottom]);
160
stack[bottom-1]=(double)i;
164
if(block->ng>0) nzcr=nzcr+1;
166
block->g[nzcr]=stack[bottom-1]-stack[bottom];
168
block->mode[nzcr]=(stack[bottom-1]<=stack[bottom]);
171
if(phase==1||block->ng==0){
172
i=(stack[bottom-1]<=stack[bottom]);
176
stack[bottom-1]=(double)i;
180
if(block->ng>0) nzcr=nzcr+1;
182
block->g[nzcr]=stack[bottom-1]-stack[bottom];
184
block->mode[nzcr]=(stack[bottom-1]>=stack[bottom]);
187
if(phase==1||block->ng==0){
188
i=(stack[bottom-1]>=stack[bottom]);
192
stack[bottom-1]=(double)i;
196
if(block->ng>0) nzcr=nzcr+1;
198
block->g[nzcr]=stack[bottom-1]-stack[bottom];
200
block->mode[nzcr]=(stack[bottom-1]!=stack[bottom]);
203
if(phase==1||block->ng==0){
204
i=(stack[bottom-1]!=stack[bottom]);
208
stack[bottom-1]=(double)i;
212
if(block->ng>0) nzcr=nzcr+1;
214
block->g[nzcr]=stack[bottom-1]-stack[bottom];
216
block->mode[nzcr]=((int)stack[bottom-1]||(int)stack[bottom]);
219
if(phase==1||block->ng==0){
220
i=((int)stack[bottom-1]||(int)stack[bottom]);
224
stack[bottom-1]=(double)i;
228
if(block->ng>0) nzcr=nzcr+1;
230
block->g[nzcr]=stack[bottom-1]-stack[bottom];
232
block->mode[nzcr]=((int)stack[bottom-1]&&(int)stack[bottom]);
235
if(phase==1||block->ng==0){
236
i=((int)stack[bottom-1]&&(int)stack[bottom]);
240
stack[bottom-1]=(double)i;
246
block->g[nzcr]=stack[bottom];
248
block->mode[nzcr]=(0.0==stack[bottom]);
251
if(block->ng>0) nzcr=nzcr+1;
252
if(phase==1||block->ng==0){
253
i=(stack[bottom]==0.0);
264
stack[bottom]=-stack[bottom];
267
stack[bottom]=sin(stack[bottom]);
270
stack[bottom]=cos(stack[bottom]);
273
stack[bottom]=tan(stack[bottom]);
276
stack[bottom]=exp(stack[bottom]);
279
stack[bottom]=log(stack[bottom]);
282
stack[bottom]=sinh(stack[bottom]);
285
stack[bottom]=cosh(stack[bottom]);
288
stack[bottom]=tanh(stack[bottom]);
292
if(block->ng>0) nzcr=nzcr+1;
294
if (stack[bottom]>0) {
295
i=(int)floor(stack[bottom]);
297
i=(int)ceil(stack[bottom]);
300
block->g[nzcr]=(stack[bottom]-1)*(stack[bottom]+1);
302
block->g[nzcr]=(stack[bottom]-i-1.)*(stack[bottom]-i);
304
block->g[nzcr]=(stack[bottom]-i)*(stack[bottom]-i+1);
306
if(i%2) block->g[nzcr]=-block->g[nzcr];
307
if(phase==1) block->mode[nzcr]=i;
309
if(phase==1||block->ng==0){
310
if (stack[bottom]>0) {
311
stack[bottom]=floor(stack[bottom]);
313
stack[bottom]=ceil(stack[bottom]);
316
stack[bottom]=(double) block->mode[nzcr];
320
if (stack[bottom]>0) {
321
stack[bottom]=floor(stack[bottom]);
323
stack[bottom]=ceil(stack[bottom]);
327
if(block->ng>0) nzcr=nzcr+1;
329
if (stack[bottom]>0) {
330
i=(int)floor(stack[bottom]+.5);
332
i=(int)ceil(stack[bottom]-.5);
334
block->g[nzcr]=(stack[bottom]-i-.5)*(stack[bottom]-i+.5);
335
if(i%2) block->g[nzcr]=-block->g[nzcr];
336
if(phase==1) block->mode[nzcr]=i;
338
if(phase==1||block->ng==0){
339
if (stack[bottom]>0) {
340
stack[bottom]=floor(stack[bottom]+.5);
342
stack[bottom]=ceil(stack[bottom]-.5);
345
stack[bottom]=(double) block->mode[nzcr];
348
/* if (stack[bottom]>0) {
349
stack[bottom]=floor(stack[bottom]+.5);
351
stack[bottom]=ceil(stack[bottom]-.5);
354
if(block->ng>0) nzcr=nzcr+1;
356
i=(int)ceil(stack[bottom]);
357
block->g[nzcr]=(stack[bottom]-i)*(stack[bottom]-i+1);
358
if(i%2) block->g[nzcr]=-block->g[nzcr];
359
if(phase==1) block->mode[nzcr]=i;
361
if(phase==1||block->ng==0){
362
stack[bottom]=ceil(stack[bottom]);
364
stack[bottom]=(double) block->mode[nzcr];
368
if(block->ng>0) nzcr=nzcr+1;
370
i=(int)floor(stack[bottom]);
371
block->g[nzcr]=(stack[bottom]-i-1)*(stack[bottom]-i);
372
if(i%2) block->g[nzcr]=-block->g[nzcr];
373
if(phase==1) block->mode[nzcr]=i;
375
if(phase==1||block->ng==0){
376
stack[bottom]=floor(stack[bottom]);
378
stack[bottom]=(double) block->mode[nzcr];
382
if(block->ng>0) nzcr=nzcr+1;
384
if (stack[bottom]>0) {
386
}else if (stack[bottom]<0){
391
block->g[nzcr]=stack[bottom];
392
if(phase==1) block->mode[nzcr]=i;
394
if(phase==1||block->ng==0){
395
if (stack[bottom]>0) {
397
}else if(stack[bottom]<0){
403
stack[bottom]=(double) block->mode[nzcr];
406
/* if (stack[bottom]>0) {
408
}else if(stack[bottom]<0){
414
if(block->ng>0) nzcr=nzcr+1;
416
if (stack[bottom]>0) {
418
}else if (stack[bottom]<0){
423
block->g[nzcr]=stack[bottom];
424
if(phase==1) block->mode[nzcr]=i;
426
if(phase==1||block->ng==0){
427
if (stack[bottom]>0) {
428
stack[bottom]=stack[bottom];
430
stack[bottom]=-stack[bottom];
433
stack[bottom]=stack[bottom]*(block->mode[nzcr]);
436
/* if (stack[bottom]>0) {
437
stack[bottom]=stack[bottom];
439
stack[bottom]=-stack[bottom];
442
if(block->ng>0) nzcr=nzcr+1;
444
if (stack[bottom]>stack[bottom-1]) {
449
block->g[nzcr]=stack[bottom]-stack[bottom-1];
450
if(phase==1) block->mode[nzcr]=i;
452
if(phase==1||block->ng==0){
453
stack[bottom-1]=max(stack[bottom-1],stack[bottom]);
455
stack[bottom-1]=stack[bottom-block->mode[nzcr]];
460
if(block->ng>0) nzcr=nzcr+1;
462
if (stack[bottom]<stack[bottom-1]) {
467
block->g[nzcr]=stack[bottom]-stack[bottom-1];
468
if(phase==1) block->mode[nzcr]=i;
470
if(phase==1||block->ng==0){
471
stack[bottom-1]=min(stack[bottom-1],stack[bottom]);
473
stack[bottom-1]=stack[bottom-block->mode[nzcr]];
478
stack[bottom]=asin(stack[bottom]);
481
stack[bottom]=acos(stack[bottom]);
484
stack[bottom]=atan(stack[bottom]);
487
stack[bottom]=asinh(stack[bottom]);
490
stack[bottom]=acosh(stack[bottom]);
493
stack[bottom]=atanh(stack[bottom]);
496
stack[bottom-1]=atan2(stack[bottom-1],stack[bottom]);
501
stack[bottom]=log10(stack[bottom]);
507
if(!_finite(stack[bottom])||_isnan(stack[bottom])){
509
if(isinf(stack[bottom])||isnan(stack[bottom])){
514
block->outptr[0][0]=stack[bottom];