-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot.cc
17377 lines (16807 loc) · 618 KB
/
plot.cc
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode:C++ ; compile-command: "g++ -I. -I.. -I../include -g -c plot.cc -Wall -DIN_GIAC -DHAVE_CONFIG_H -DGIAC_GENERIC_CONSTANTS " -*- */
// NB: Using gnuplot optimally requires patching and recompiling gnuplot
// If you use the -DGNUPLOT_IO compile flag, you
// MUST compile gnuplot with interactive mode enabled, file src/plot.c
// line 448
/*
diff plot.c plot.c~
448c448
< interactive = TRUE; // isatty(fileno(stdin));
---
> interactive = isatty(fileno(stdin));
*/
/*
* Copyright (C) 2000/14 B. Parisse, Institut Fourier, 38402 St Martin d'Heres
* implicitplot3d code adapted from
* http://astronomy.swin.edu.au/~pbourke/modelling/polygonise
* by Paul Bourke and Cory Gene Bloyd
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "giacPCH.h"
using namespace std;
#if !defined NSPIRE && !defined FXCG && !defined KHICAS
#ifdef VISUALC13
#undef clock
#undef clock_t
#endif
#include <iomanip>
#endif
#if !defined GIAC_HAS_STO_38 && !defined NSPIRE && !defined FXCG
#include <fstream>
#endif
#include "vector.h"
#include <algorithm>
#include <cmath>
#if 0
#include<ext/stdio_filebuf.h>
typedef std::basic_ofstream<char>::__filebuf_type buffer_t;
typedef __gnu_cxx::stdio_filebuf<char> io_buffer_t;
FILE* cfile_impl(buffer_t* const fb){
return (static_cast<io_buffer_t* const>(fb))->file(); //type std::__c_file
}
FILE* cfile(std::ofstream const& ofs){return cfile_impl(ofs.rdbuf());}
FILE* cfile(std::ifstream const& ifs){return cfile_impl(ifs.rdbuf());}
#endif
// C headers
#include <stdio.h>
#ifndef __VISUALC__
#if !defined RTOS_THREADX && !defined BESTA_OS && !defined FREERTOS && !defined FXCG
#include <fcntl.h>
#endif
#endif // __VISUALC__
#ifdef FXCG
extern "C" {
#include <fxcg/keyboard.h>
#include <fxcg/display.h>
}
#endif
// Giac headers
#include "gen.h"
#include "usual.h"
#include "prog.h"
#include "rpn.h"
#include "identificateur.h"
#include "subst.h"
#include "symbolic.h"
#include "derive.h"
#include "solve.h"
#include "intg.h"
#include "path.h"
#include "sym2poly.h"
#include "input_parser.h"
#include "input_lexer.h"
#include "ti89.h"
#include "isom.h"
#include "unary.h"
#include "plot.h"
#include "plot3d.h"
#include "ifactor.h"
#include "gauss.h"
#include "misc.h"
#include "lin.h"
#include "quater.h"
#include "giacintl.h"
#include "signalprocessing.h"
#ifdef USE_GMP_REPLACEMENTS
#undef HAVE_GMPXX_H
#undef HAVE_LIBMPFR
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#ifndef BESTA_OS
#define clock_t int
//#define CLOCK() 0
#endif
#endif
#ifndef HAVE_NO_SYS_RESOURCE_WAIT_H
#ifndef __MINGW_H
#include <sys/resource.h>
#endif
#include <sys/wait.h>
#endif
#if defined GIAC_HAS_STO_38 || defined NSPIRE || defined NSPIRE_NEWLIB || defined FXCG || defined GIAC_GGB || defined USE_GMP_REPLACEMENTS || defined KHICAS
inline bool is_graphe(const giac::gen &g){ return false; }
inline giac::gen _graph_vertices(const giac::gen &g,const giac::context *){ return g;}
inline giac::gen _is_planar(const giac::gen &g,const giac::context *){ return g;}
#else
#include "signalprocessing.h"
#include "graphtheory.h"
#endif
extern const int BUFFER_SIZE;
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
#ifdef IPAQ
int LEGENDE_SIZE=40;
int COORD_SIZE=26;
int PARAM_STEP=18;
#else // IPAQ
int LEGENDE_SIZE=100;
int COORD_SIZE=30;
int PARAM_STEP=20;
#endif // IPAQ
#ifdef GNUWINCE
ofstream * outptr=0;
#endif
// int plot_instructionsh=300,plot_instructionsw=300;
const int _GROUP__VECT_subtype[]={_GROUP__VECT,_LINE__VECT,_HALFLINE__VECT,_VECTOR__VECT,_POLYEDRE__VECT,_POINT__VECT,0};
#ifdef IPAQ
double gnuplot_xmin=-4,gnuplot_xmax=4,gnuplot_ymin=-4,gnuplot_ymax=4,gnuplot_zmin=-5,gnuplot_zmax=5,gnuplot_tmin=-6,gnuplot_tmax=6,gnuplot_tstep=0.3;
#else
double gnuplot_xmin=-5,gnuplot_xmax=5,gnuplot_ymin=-5,gnuplot_ymax=5,gnuplot_zmin=-5,gnuplot_zmax=5,gnuplot_tmin=-6,gnuplot_tmax=6,gnuplot_tstep=0.1;
#endif
double global_window_xmin(gnuplot_xmin),global_window_xmax(gnuplot_xmax),global_window_ymin(gnuplot_ymin),global_window_ymax(gnuplot_ymax);
double x_tick(1.0),y_tick(1.0);
double class_minimum(0.0),class_size(1.0);
#if defined RTOS_THREADX || defined EMCC || defined EMCC2 || defined KHICAS
int gnuplot_pixels_per_eval=128;
#else
int gnuplot_pixels_per_eval=401;
#endif
bool autoscale=true;
bool has_gnuplot=true;
bool fastcurveprint=false;
static gen rand_complex(){
int i=std_rand(),j=std_rand();
i=i/(RAND_MAX/10)-5;
j=j/(RAND_MAX/10)-5;
return i+j*cst_i;
}
gen abs_norm(const gen & g,GIAC_CONTEXT){
if (g.type==_VECT)
return sqrt(dotvecteur(*g._VECTptr,*g._VECTptr),contextptr);
else
return abs(g,contextptr);
}
gen abs_norm(const gen & a,const gen & b,GIAC_CONTEXT){
if (a.type==_VECT)
return abs_norm(b-a,contextptr);
gen ax,ay,bx,by;
reim(a,ax,ay,contextptr);
reim(b,bx,by,contextptr);
bx -= ax;
by -= ay;
return sqrt(bx*bx+by*by,contextptr);
}
gen abs_norm2(const gen & g,GIAC_CONTEXT){
if (g.type==_VECT)
return dotvecteur(*g._VECTptr,*g._VECTptr);
else
return ratnormal(_lin(g*conj(g,contextptr),contextptr),contextptr);
}
gen dotvecteur(const gen & a,const gen & b,GIAC_CONTEXT){
return scalar_product(a,b,contextptr);
/*
if (a.type==_VECT)
return dotvecteur(*a._VECTptr,*b._VECTptr);
else
return -im(a*conj(cst_i*b,contextptr),contextptr);
*/
}
static gen set3derr(GIAC_CONTEXT){
return gensizeerr(contextptr); // 2-d instruction
}
static void in_autoname_plus_plus(string & autoname){
int labs=int(autoname.size());
if (!labs){
autoname="A";
labs=1;
}
#ifdef GIAC_HAS_STO_38
int labsmin=1;
#else
int labsmin=0;
#endif
for (;labs>labsmin;--labs){
char c=autoname[labs-1];
if (c!='Z' && c!='z'){
autoname[labs-1]=char(c+1);
return;
}
autoname[labs-1]='A';
}
if (labs==labsmin)
autoname += 'A';
}
void autoname_plus_plus(string & autoname){
#ifdef GIAC_HAS_STO_38
if (autoname.size()==2 && autoname[0]=='G' && autoname[1]<'Z'){
if (autoname[1]=='E')
autoname[1]='G';
else {
if (autoname[1]=='Z')
autoname[1]='a';
else
++autoname[1];
}
return;
}
#endif
for (;;){
in_autoname_plus_plus(autoname);
if (gen(autoname,context0).type==_IDNT) // FIXME GIAC_CONTEXT
break;
}
}
#ifdef WITH_GNUPLOT
string PICTautoname("A");
void PICTautoname_plus_plus(){
autoname_plus_plus(PICTautoname);
}
#endif
// find last at_erase in history, return next position (or 0 if not found)
int erase_pos(GIAC_CONTEXT){
int s=int(history_out(contextptr).size());
if (!s)
return s;
gen e;
for (int i=s-1;i>=0;--i){
e=history_out(contextptr)[i];
if ( (e.type==_SYMB && (e._SYMBptr->sommet==at_erase
// || equalposcomp(implicittex_plot_sommets,e._SYMBptr->sommet)
)) ||
(e.type==_FUNC && (*e._FUNCptr==at_erase
// || equalposcomp(implicittex_plot_sommets,e._SYMBptr->sommet)
) ) )
return i+1;
}
return 0;
// int i=history_out(contextptr).find_last_of(at_erase);
}
int erase_pos(int current,GIAC_CONTEXT){
int s=int(history_out(contextptr).size());
if (current>=s)
current=s-1;
if (!s)
return s;
gen e;
for (int i=current;i>=0;--i){
e=history_out(contextptr)[i];
if ( (e.type==_SYMB && (e._SYMBptr->sommet==at_erase
// || equalposcomp(implicittex_plot_sommets,e._SYMBptr->sommet)
)) ||
(e.type==_FUNC && (*e._FUNCptr==at_erase
// || equalposcomp(implicittex_plot_sommets,e._SYMBptr->sommet)
) ) )
return i+1;
}
return 0;
// int i=history_out(contextptr).find_last_of(at_erase);
}
gen remove_at_pnt(const gen & e){
int t=e.type;
if (t==_VECT && e.subtype==_GGB__VECT){
if (e._VECTptr->size()==2)
return e._VECTptr->front()+cst_i*e._VECTptr->back();
if (e._VECTptr->size()==3)
return change_subtype(e,_POINT__VECT);
}
if (t==_SYMB && e._SYMBptr->sommet==at_pnt){
const gen & f = e._SYMBptr->feuille;
#if 1
if (f.type==_VECT){
const vecteur & v = *f._VECTptr;
if (!v.empty())
return v.front();
}
#else
if (f.type==_VECT){
vecteur & v = *f._VECTptr;
/*
int s=int(v.size());
if (s){
if (s>1 && v[1].type==_VECT){
gen & v1=v[1];
if (v1.type==_VECT && v1._VECTptr->size()>1){
gen v12=(*v1._VECTptr)[1];
if (v12.type<=_CPLX)
return v12;
if (v12.type==_STRNG){
string s=*v12._STRNGptr;
if (!s.empty() && s[s.size()-1]==' '){
gen g(s,context0);
if (g.is_symb_of_sommet(at_equal))
return g._SYMBptr->feuille._VECTptr->back();
}
}
}
}
*/
return v.front();
}
}
#endif
return gensizeerr("Bad pnt argument");
}
return e;
}
/*
static gen unsubtype(const gen & g){
return (g.type==_VECT)?gen(*g._VECTptr):g;
}
*/
gen remove_sto(const gen & e){
if ( (e.type==_SYMB) && (e._SYMBptr->sommet==at_sto))
return e._SYMBptr->feuille._VECTptr->back();
else
return e;
}
vecteur selection2vecteur(const vector<int> & selected,GIAC_CONTEXT){
int i=erase_pos(contextptr);
vecteur res;
vector<int>::const_iterator it=selected.begin(),itend=selected.end();
for (;it!=itend;++it){
gen e=history_in(contextptr)[i+*it];
if ( (e.type==_SYMB) && (e._SYMBptr->sommet==at_sto))
res.push_back(e._SYMBptr->feuille._VECTptr->back());
else
res.push_back(e);
}
return res;
}
vecteur selection2vecteureval(const vector<int> & selected,GIAC_CONTEXT){
int i=erase_pos(contextptr);
vecteur res;
vector<int>::const_iterator it=selected.begin(),itend=selected.end();
for (;it!=itend;++it){
gen e=history_out(contextptr)[i+*it];
res.push_back(e);
}
return res;
}
bool is_segment(const gen & e){
gen f=remove_at_pnt(e);
if ( (f.type!=_VECT) || (f._VECTptr->size()!=2))
return false;
return true;
}
int findfirstpoint(const vecteur & v){
const_iterateur it=v.begin(),itend=v.end();
gen p;
for (;it!=itend;++it){
p=*it;
if ( (p.type==_SYMB) && (p._SYMBptr->sommet==at_pnt)){
p=p._SYMBptr->feuille._VECTptr->front();
if ( (p.type!=_VECT || p.subtype==_POINT__VECT) && ( (p.type!=_SYMB) || (!equalposcomp(not_point_sommets,p._SYMBptr->sommet))) )
return int(it-v.begin());
}
}
return -1;
}
int findfirstcercle(const vecteur & v){
const_iterateur it=v.begin(),itend=v.end();
gen p;
for (;it!=itend;++it){
p=*it;
if ( (p.type==_SYMB) && (p._SYMBptr->sommet==at_pnt)){
p=p._SYMBptr->feuille._VECTptr->front();
if ( (p.type==_SYMB) && (p._SYMBptr->sommet==at_cercle) )
return int(it-v.begin());
}
}
return -1;
}
symbolic symb_curve(const gen & source,const gen & plot){
return symbolic(at_curve,gen(makevecteur(source,plot),_GROUP__VECT));
}
static symbolic symb_perpendiculaire(const gen & aa){
gen a(aa);
if (a.type==_VECT) a.subtype=_SEQ__VECT;
return symbolic(at_perpendiculaire,a);
}
static symbolic symb_milieu(const gen & aa){
gen a(aa);
if (a.type==_VECT) a.subtype=_SEQ__VECT;
return symbolic(at_milieu,a);
}
static symbolic symb_plotfunc(const gen & a,const gen & b){
return symbolic(at_plotfunc,makesequence(a,b));
}
static gen setplotfuncerr(){
return gensizeerr(gettext("Plotfunc: bad variable name"));
}
// find best int in selected (and modify selected)
// p is the pointed mouse point, eps the precision
// try_perp=-1 if no try of perp line, =an history_position otherwise
bool find_best(vector<int> & selected,const gen & p,double eps,int try_perp_history_pos,int & pnt_pos,int & history_position,gen & res,GIAC_CONTEXT){
vecteur v=selection2vecteur(selected,contextptr),w=selection2vecteureval(selected,contextptr);
pnt_pos=findfirstpoint(w);
if (pnt_pos<0){ // no point nearby, try to make one
// try intersection or midpoint
int nsegments=0,n1=-1,n2=-1;
const_iterateur it=w.begin(),itend=w.end();
for (;it!=itend;++it){
if (is_segment(*it)){
++nsegments;
n2=int(it-w.begin());
if (n1<0)
n1=n2;
}
}
if (nsegments>=2){
history_position=erase_pos(contextptr)+selected[n1];
gen l1=remove_sto(history_in(contextptr)[history_position]);
history_position=erase_pos(contextptr)+selected[n2];
gen l2=remove_sto(history_in(contextptr)[history_position]);
res = symbolic(at_head,symbolic(at_inter,makesequence(l1,l2)));
selected=vector<int>(2);
selected[0]=n1;
selected[1]=n2;
return true;
}
if (nsegments){ // near middle point?
gen m=remove_at_pnt(_milieu(w[n1],0));
if (!is_undef(m) && is_greater(eps,abs(p-m,contextptr),contextptr)){
history_position=erase_pos(contextptr)+selected[n1];
gen l1=remove_sto(history_in(contextptr)[history_position]);
res=symb_milieu(l1);
selected=vector<int>(1,n1);
return true;
}
// near perp?
if (try_perp_history_pos>=0){
m=remove_at_pnt(history_out(contextptr)[try_perp_history_pos]);
gen pm=p-m;
gen wv=remove_at_pnt(w[n1]);
wv=wv._VECTptr->front()-wv._VECTptr->back();
if (is_greater(eps*abs(pm,contextptr)*abs(wv,contextptr),scalar_product(pm,wv,contextptr),contextptr)){
history_position=erase_pos(contextptr)+selected[n1];
gen l1=remove_sto(history_in(contextptr)[history_position]);
res=symb_perpendiculaire(makevecteur(remove_sto(history_in(contextptr)[try_perp_history_pos]),l1));
res=symbolic(at_head,symbolic(at_inter,makesequence(l1,res)));
selected=vector<int>(1,n1);
return true;
}
}
}
// try a cercle
pnt_pos=findfirstcercle(w);
if (pnt_pos<0)
return false;
history_position=erase_pos(contextptr)+selected[pnt_pos];
res=remove_sto(history_in(contextptr)[history_position]);
return true;
}
else {
history_position=erase_pos(contextptr)+selected[pnt_pos];
res=remove_sto(history_in(contextptr)[history_position]);
return true;
}
}
void streamcopy(FILE * source,FILE * target){
char c;
for (;!feof(source);){
c=fgetc(source);
if (!feof(source))
fputc(c,target);
}
fclose(source);
}
#if !defined VISUALC && ! defined __MINGW_H && !defined BESTA_OS && !defined FXCG
int set_nonblock_flag (int desc, int value){
int oldflags = fcntl (desc, F_GETFL, 0);
/* If reading the flags failed, return error indication now. */
if (oldflags == -1)
return -1;
/* Set just the flag we want to set. */
if (value != 0)
oldflags |= O_NONBLOCK;
else
oldflags &= ~O_NONBLOCK;
/* Store modified flag word in the descriptor. */
return fcntl (desc, F_SETFL, oldflags);
}
int set_cloexec_flag (int desc, int value){
int oldflags = fcntl (desc, F_GETFD, 0);
/* If reading the flags failed, return error indication now.
if (oldflags < 0)
return oldflags;
// Set just the flag we want to set. */
if (value != 0)
oldflags |= FD_CLOEXEC;
else
oldflags &= ~FD_CLOEXEC;
/* Store modified flag word in the descriptor. */
return fcntl (desc, F_SETFD, oldflags);
}
#endif
bool gnuplot_opengl=false;
#ifdef WITH_GNUPLOT
vecteur plot_instructions;
#ifdef WIN32
static bool win9x=true; // For win95/98/Me we can not pipe to gnuplot
#else
static bool win9x=false;
#endif
static pid_t gnuplot_pid=0;
string gnuplot_name(giac_gnuplot_location);
int gnuplot_fileno=0;
bool gnuplot_do_splot=true;
int gnuplot_in_pipe[2]={-1,-1},gnuplot_out_pipe[2]={-1,-1};
FILE * gnuplot_out_readstream=0;
#ifdef IPAQ
bool gnuplot_hidden3d=false;
#else
bool gnuplot_hidden3d=true;
#endif
bool gnuplot_pm3d=false;
static int gnuplot_wait_times=5;
string gnuplot_filename("session3d");
bool ck_gnuplot(string & s){
if (!access(s.c_str(),R_OK))
return true;
if (!access("/usr/local/bin/gnuplot",R_OK)){
s="/usr/local/bin/gnuplot";
return true;
}
if (!access("/usr/bin/gnuplot",R_OK)){
s="/usr/bin/gnuplot";
return true;
}
if (!access((xcasroot()+"gnuplot").c_str(),R_OK)){
s=xcasroot()+"gnuplot";
return true;
}
return false;
}
int run_gnuplot(int & r){
#ifdef WIN32
has_gnuplot=false;
return -1;
#ifdef GNUWINCE
#endif
gnuplot_name=xcasroot()+"pgnuplot.exe";
#endif //WIN32
if (!gnuplot_pid){ // start a gnuplot process
if (!ck_gnuplot(gnuplot_name))
{
has_gnuplot=false;
return -1;
}
if (pipe(gnuplot_in_pipe) || pipe(gnuplot_out_pipe))
throw(std::runtime_error("Plot error: Unable to create pipe"));
gnuplot_pid=fork();
if (gnuplot_pid<(pid_t) 0)
throw(std::runtime_error("Plot error: Unable to fork"));
if (!gnuplot_pid){ // child process, redirect input/output
#ifndef GNUPLOT_IO
int gnuplot_out=open("gnuplot.out",O_WRONLY | O_CREAT);
dup2(gnuplot_out,STDERR_FILENO);
close(gnuplot_out);
#else
dup2(gnuplot_out_pipe[1],STDERR_FILENO);
close(gnuplot_out_pipe[1]);
set_cloexec_flag(STDERR_FILENO,0);
#endif
dup2(gnuplot_in_pipe[0],STDIN_FILENO);
close(gnuplot_in_pipe[0]);
// cout << errno << '\n';
#ifdef IPAQ
execlp("gnuplot","gnuplot -geometry 235x300 -noraise",0);
#else
execlp("gnuplot","gnuplot -geometry 500x700 -noraise",0); // start gnuplot
#endif
return -1; // in case gnuplot is not found
}
else {
usleep(10000);
bool clrplot=false;
FILE * stream = open_gnuplot(clrplot,gnuplot_out_readstream,r);
gnuplot_out_readstream=fdopen(r,"r");
gnuplot_wait(r,gnuplot_out_readstream,3);
// close(gnuplot_out_pipe[1]);
// close(gnuplot_in_pipe[0]);
}
}
r=gnuplot_out_pipe[0];
return gnuplot_in_pipe[1];
return 0;
}
FILE * open_gnuplot(bool & clrplot,FILE * & gnuplot_out_r,int & r){
if (win9x){
r=-1;
#ifdef WIN32
gnuplot_name="'"+xcasroot()+"wgnuplot.exe'"; // was xcasroot()+"wgnuplot.exe",
#ifdef GNUWINCE
gnuplot_name="/gnuplot.exe";
#endif
#endif
FILE * stream= fopen("gnuplot.txt","a");
if (!stream)
stream=fopen("gnuplot.txt","w");
if (!stream)
setsizeerr(gettext("Gnuplot write error"));
return stream;
}
FILE * res=fdopen(run_gnuplot(r),"w");
if (!res)
setsizeerr(gettext("Gnuplot write error"));
#ifdef __APPLE__
if (debug_infolevel)
printf("set terminal aqua\n");
fprintf(res,"set terminal aqua\n");
#endif
// CERR << errno << '\n';
gnuplot_out_r = gnuplot_out_readstream;
// CERR << r << " " << errno << '\n';
return res;
}
void terminal_stream_replot(const char * terminal,FILE * stream,const char * filename){
#ifdef IPAQ
if (string(terminal)=="png")
return;
#endif
#ifdef __APPLE__
if (string(terminal)=="png")
return;
#endif
fprintf(stream,"set terminal %s\nset output \"%s\"\nreplot\nset output\n",terminal,filename);
if (debug_infolevel)
printf("set terminal %s\nset output \"%s\"\nreplot\nset output\n",terminal,filename);
#ifdef WIN32
if (debug_infolevel)
printf("set terminal windows\n");
fprintf(stream,"set terminal windows\n");
#else
#ifdef __APPLE__
if (debug_infolevel)
printf("set terminal aqua\n");
fprintf(stream,"set terminal aqua\n");
#else
if (debug_infolevel)
printf("set terminal x11\n");
fprintf(stream,"set terminal x11\n");
#endif
#endif
fflush(stream);
}
void terminal_stream_replot(const char * terminal,FILE * stream,int i,const char * file_extension){
terminal_stream_replot(terminal,stream,(gnuplot_filename+print_INT_(i)+"."+string(file_extension)).c_str());
}
void gnuplot_wait(int handle,FILE * gnuplot_out_readstream,int ngwait){
#ifndef GNUPLOT_IO
usleep(200000);
return;
#endif
// CERR << "gnuplot_wait " << handle << " " << gnuplot_out_readstream << " " << ngwait << '\n';
// wait for gnuplot to be quiet
if (handle>0 && gnuplot_out_readstream){
int i,ntry=500;
for (;ntry;){
usleep(10000);
set_nonblock_flag(handle,1);
i=fgetc(gnuplot_out_readstream);
// CERR << char(i) ;
if (char(i)=='>')
--ngwait;
set_nonblock_flag(handle,0);
if (i==EOF){
if (ngwait<=0)
break;
usleep(10000);
--ntry;
}
set_nonblock_flag(handle,1);
while (i!=EOF){
i=fgetc(gnuplot_out_readstream);
// CERR << char(i);
if (char(i)=='>')
--ngwait;
}
set_nonblock_flag(handle,0);
}
// fclose(gnuplot_out_readstream);
usleep(10000);
// CERR << "gnuplot_wait end " << getpid() << " " <<CLOCK() << '\n';
}
else
;// CERR << "gnuplot wait no input" << '\n';
}
bool terminal_replot(const char * terminal,int i,const char * file_extension){
if (!has_gnuplot)
return 0;
if (win9x){
FILE * stream2 =fopen("gnuplot.gp","w");
FILE * stream =fopen("gnuplot.txt","r");
streamcopy(stream,stream2);
terminal_stream_replot(terminal,stream2,i,file_extension);
system_no_deprecation((gnuplot_name+" gnuplot.gp").c_str());
}
else {
bool clrplot;
int r;
FILE * gnuplot_out_readstream,* stream = open_gnuplot(clrplot,gnuplot_out_readstream,r);
terminal_stream_replot(terminal,stream,i,file_extension);
gnuplot_wait(r,gnuplot_out_readstream,gnuplot_wait_times);
}
return true;
}
bool terminal_replot(const char * terminal,const string & s){
if (!has_gnuplot)
return 0;
#ifdef IPAQ
if (string(terminal)=="png")
return 0;
#endif
#ifdef __APPLE__
if (string(terminal)=="png")
return 0;
#endif
if (win9x){
FILE * stream = fopen("gnuplot.txt","r");
FILE * stream2 =fopen("gnuplot.gp","w");
streamcopy(stream,stream2);
terminal_stream_replot(terminal,stream2,s.c_str());
system_no_deprecation((gnuplot_name+" gnuplot.gp").c_str());
}
else {
bool clrplot;
int r;
FILE * gnuplot_out_readstream,* stream = open_gnuplot(clrplot,gnuplot_out_readstream,r);
terminal_stream_replot(terminal,stream,s.c_str());
gnuplot_wait(r,gnuplot_out_readstream,gnuplot_wait_times);
}
return true;
}
void win9x_gnuplot(FILE * stream){
if (!win9x){
#ifndef IPAQ
#ifndef __APPLE__
latex_replot(stream,(gnuplot_filename+print_INT_(gnuplot_fileno)+".tex").c_str());
terminal_stream_replot("png",stream,gnuplot_fileno,"png");
#endif
#endif
fflush(stream);
return;
}
fflush(stream);
fclose(stream);
#ifdef GNUWINCE
(*outptr) << "// <plot> </plot>" << '\n';
return ;
#endif
// Copy to a temporary gnuplot
unlink("gnuplot.gp");
FILE * stream2 = fopen("gnuplot.gp","w");
stream = fopen("gnuplot.txt","r");
streamcopy(stream,stream2);
fprintf(stream2,"pause -1 \"Press RETURN when OK\"\n");
latex_replot(stream2,(gnuplot_filename+print_INT_(gnuplot_fileno)+".tex").c_str());
terminal_stream_replot("png",stream2,gnuplot_fileno,"png");
fflush(stream2);
fclose(stream2);
system_no_deprecation((gnuplot_name+" gnuplot.gp").c_str());
}
void kill_gnuplot(){
if (gnuplot_pid){
FILE *stream;
stream = fdopen (gnuplot_in_pipe[1], "w");
fprintf(stream,"\n\nq\n");
if (fclose(stream))
CERR << "Error closing gnuplot" << '\n';
}
}
bool png_replot(int i){
return terminal_replot("png",i,"png");
}
bool png_replot(const string & s){
return terminal_replot("png",s);
}
string gnuplot_traduit(const gen & g){
string s(evalf(g,1,0).print());
string f_s(s),ff_s;
string::iterator it=f_s.begin(),itend=f_s.end();
for (;it!=itend;++it){
if (*it=='^')
ff_s += "**";
else
ff_s += *it;
}
return ff_s;
}
#endif // WITH_GNUPLOT
static void plotpreprocess(gen & g,vecteur & quoted,GIAC_CONTEXT){
gen tmp=eval(g,contextptr);
if (tmp.type==_IDNT){
g=tmp;
quoted=vecteur(1,tmp);
return;
}
if (tmp.type==_VECT){
bool done=true;
const_iterateur it=tmp._VECTptr->begin(),itend=tmp._VECTptr->end();
if (it!=itend){
for (;it!=itend;++it){
if (it->type!=_IDNT && !it->is_symb_of_sommet(at_at))
break;
}
if (it==itend){
g=tmp;
quoted=*tmp._VECTptr;
}
else
done=false;
}
else
done=false;
if (!done){
if (g.type==_VECT)
quoted=*g._VECTptr;
else
quoted=vecteur(1,g);
}
}
else {
quoted=vecteur(1,g);
}
}
vecteur quote_eval(const vecteur & v,const vecteur & quoted,GIAC_CONTEXT){
/*
vecteur l(quoted);
lidnt(v,l);
int qs=quoted.size();
l=vecteur(l.begin()+qs,l.end());
vecteur lnew=*eval(l,1,contextptr)._VECTptr;
vecteur w=subst(v,l,lnew,true,contextptr);
return w;
*/
const_iterateur it=quoted.begin(),itend=quoted.end();
vector<int> save;
for (;it!=itend;++it){
gen tmp=*it;
if (is_equal(tmp))
tmp=tmp._SYMBptr->feuille._VECTptr->front();
if (tmp.type!=_IDNT)
save.push_back(-1);
else {
if (contextptr && contextptr->quoted_global_vars){
gen ckassume=tmp._IDNTptr->eval(1,tmp,contextptr);
if (ckassume.type==_VECT && ckassume.subtype==_ASSUME__VECT)
save.push_back(-1);
else {
contextptr->quoted_global_vars->push_back(tmp);
save.push_back(0);
}
}
else {
if (tmp._IDNTptr->quoted){
save.push_back(*tmp._IDNTptr->quoted);
*tmp._IDNTptr->quoted=1;
}
else
save.push_back(0);
}
}
}
vecteur res(v);
int s=int(v.size());
for (int i=0;i<s;++i){
#ifndef NO_STDEXCEPT
try {
#endif
bool done=false;
if (v[i].is_symb_of_sommet(at_prod) && v[i]._SYMBptr->feuille.type==_VECT){ // hack for polarplot using re(rho)
vecteur vi = *v[i]._SYMBptr->feuille._VECTptr;
if (!vi.empty() && vi.front().is_symb_of_sommet(at_re)){
vi.front()=vi.front()._SYMBptr->feuille;
gen tmp=eval(vi,contextptr);
if (tmp.type==_VECT){
vi=*tmp._VECTptr;
vi.front()=symbolic(at_re,vi.front());
res[i]=_prod(vi,contextptr);
done=true;
}
}
}
if (!done){
#if 0
vecteur lv=lidnt(v[i]);
vecteur lw(lv);
for (int j=0;j<int(lw.size());++j){
gen g=eval(lv[j],1,contextptr);
g=ifte2when(g,contextptr);
lw[j]=g;
}
res[i]=quotesubst(v[i],lv,lw,contextptr); // otherwise plots with if else fails (error not catched with emscripten)
#endif
res[i]=eval(v[i],contextptr);
}
#ifndef NO_STDEXCEPT
} catch (std::runtime_error & ){
last_evaled_argptr(contextptr)=NULL;
// *logptr(contextptr) << e.what() << '\n';
}
#endif
}
it=quoted.begin();
for (int i=0;it!=itend;++it,++i){
if (save[i]>=0){