symbolic.cc
53.7 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
// -*- mode:C++ ; compile-command: "g++-3.4 -I. -I.. -I../include -g -c symbolic.cc -Wall -DIN_GIAC -DHAVE_CONFIG_H" -*-
#include "giacPCH.h"
/*
* Copyright (C) 2000, 2014 B. Parisse, Institut Fourier, 38402 St Martin d'Heres
*
* 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/>.
*/
using namespace std;
#include "symbolic.h"
#include "identificateur.h"
#include "usual.h"
#include "prog.h"
#include "rpn.h"
#include "plot.h"
#include "giacintl.h"
#include "global.h"
// NB: the operator in the symbolic (sommet) can not be replace by a pointer
// to a unary_function_ptr in the current code. Indeed, symbolic are created
// in the parser from temporary gen objects of type _FUNC which contains
// copies of unary_function_ptr, these copies are deleted once the parser
// gen object is deleted -> segfault.
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
// unary_function_ptr quoted_op[]={at_of,at_for,at_bloc,at_local,at_program,at_rpn_prog,at_ifte,at_try_catch,at_print,at_signal,at_as_function_of,at_lieu,at_legende,at_debug,at_sst,at_sst_in,at_cont,at_kill,at_halt,at_watch,at_rmwatch,at_breakpoint,at_maple2mupad,at_mupad2maple,at_maple2xcas,at_mupad2xcas,at_purge,0};
#if defined(GIAC_GENERIC_CONSTANTS) || defined(VISUALC) || defined(x86_64)
const unary_function_ptr * archive_function_tab(){
static const unary_function_ptr archive_function_tab_ptr[]={*at_plus,*at_neg,*at_binary_minus,*at_prod,*at_division,*at_inv,*at_pow,*at_exp,*at_ln,*at_abs,*at_arg,*at_pnt,*at_point,*at_segment,*at_sto,*at_sin,
*at_cos,*at_tan,*at_asin,*at_acos,*at_atan,*at_sinh,*at_cosh,*at_tanh,*at_asinh,*at_acosh,*at_atanh,*at_interval,*at_union,*at_minus,*at_intersect,*at_not,
*at_and,*at_ou,*at_inferieur_strict,*at_inferieur_egal,*at_superieur_strict,*at_superieur_egal,*at_different,*at_equal,*at_rpn_prog,*at_local,*at_return,*at_Dialog,*at_double_deux_points,*at_pointprod,*at_pointdivision,*at_pointpow,*at_hash,*at_pourcent,*at_tilocal,*at_break,*at_continue,*at_ampersand_times,*at_maple_lib,*at_unit,*at_plot_style,*at_xor,*at_check_type,*at_quote_pow,*at_case,*at_dollar,*at_IFTE,*at_RPN_CASE,*at_RPN_LOCAL,*at_RPN_FOR,*at_RPN_WHILE,*at_NOP,*at_unit,*at_ifte,*at_for,*at_bloc,*at_program,*at_same,*at_increment,*at_decrement,*at_multcrement,*at_divcrement,*at_sq,*at_display,*at_of,*at_at,*at_normalmod,*at_equal2,*at_pointplus,*at_pointminus,0};
archive_function_tab_length=sizeof(archive_function_tab_ptr)/sizeof(const unary_function_ptr *);
return archive_function_tab_ptr;
}
int archive_function_tab_length=0;
#else
static const size_t archive_function_tab_alias[]={
// index = 2
alias_at_plus,alias_at_neg,alias_at_binary_minus,alias_at_prod,alias_at_division,
// 12
alias_at_inv,alias_at_pow,alias_at_exp,alias_at_ln,alias_at_abs,
// 22
alias_at_arg,alias_at_pnt,alias_at_point,alias_at_segment,alias_at_sto,
// 32
alias_at_sin,alias_at_cos,alias_at_tan,alias_at_asin,alias_at_acos,
// 42
alias_at_atan,alias_at_sinh,alias_at_cosh,alias_at_tanh,alias_at_asinh,
// 52
alias_at_acosh,alias_at_atanh,alias_at_interval,alias_at_union,alias_at_minus,
// 62
alias_at_intersect,alias_at_not,alias_at_and,alias_at_ou,alias_at_inferieur_strict,
// 72
alias_at_inferieur_egal,alias_at_superieur_strict,alias_at_superieur_egal,alias_at_different,alias_at_equal,
// 82
alias_at_rpn_prog,alias_at_local,alias_at_return,alias_at_Dialog,alias_at_double_deux_points,
// 92
alias_at_pointprod,alias_at_pointdivision,alias_at_pointpow,alias_at_hash,alias_at_pourcent,
// 102
alias_at_tilocal,alias_at_break,alias_at_continue,alias_at_ampersand_times,alias_at_maple_lib,
// 112
alias_at_unit,alias_at_plot_style,alias_at_xor,alias_at_check_type,alias_at_quote_pow,
// 122
alias_at_case,alias_at_dollar,alias_at_IFTE,alias_at_RPN_CASE,alias_at_RPN_LOCAL,
// 132
alias_at_RPN_FOR,alias_at_RPN_WHILE,alias_at_NOP,alias_at_unit,alias_at_ifte,
// 142
alias_at_for,alias_at_bloc,alias_at_program,alias_at_same,alias_at_increment,
// 152
alias_at_decrement,alias_at_multcrement,alias_at_divcrement,alias_at_sq,alias_at_display,
// 162
alias_at_of,alias_at_at,alias_at_normalmod,alias_at_equal2,alias_at_pointplus,
// 172
alias_at_pointminus,
0
};
const unary_function_ptr * _archive_function_tab = (const unary_function_ptr *) &archive_function_tab_alias;
const unary_function_ptr * archive_function_tab(){
return _archive_function_tab;
}
int archive_function_tab_length=sizeof(archive_function_tab_alias)/sizeof(size_t);
#endif
int archive_function_index(const unary_function_ptr & f){
return f.ptr()?(f.ptr()->index_quoted_function >> 1):0;
// return equalposcomp(archive_function_tab(),f);
}
int equalposcomp(const unary_function_ptr tab[],const unary_function_ptr & f){
for (int i=1;*((size_t *)tab)!=0;++tab,++i){
if (*tab==f)
return i;
}
return 0;
}
int equalposcomp(const vector<const unary_function_ptr * > & tab,const unary_function_ptr * f){
vector<const unary_function_ptr * >::const_iterator it=tab.begin(),itend=tab.end();
for (int i=1;it!=itend;++it,++i){
if (**it==*f)
return i;
}
return 0;
}
int equalposcomp(const const_unary_function_ptr_ptr tab[],const unary_function_ptr & f){
for (int i=1;*((size_t *)tab)!=0;++tab,++i){
if (**tab==f)
return i;
}
return 0;
}
symbolic::symbolic(const symbolic & mys,const gen & e): sommet(mys.sommet){
vecteur tmp;
if (mys.feuille.type==_VECT){
tmp = *mys.feuille._VECTptr;
tmp.push_back(e);
}
else {
tmp.push_back(mys.feuille);
tmp.push_back(e);
}
feuille = gen(tmp,_SEQ__VECT);
};
symbolic::symbolic(const gen & a,const unary_function_ptr & o,const gen & b):sommet(o) {
if (b.type==_VECT)
feuille=gen(mergevecteur(vecteur(1,a),*b._VECTptr),b.subtype);
else
feuille=gen(makevecteur(a,b),_SEQ__VECT);
};
symbolic::symbolic(const gen & a,const unary_function_ptr * o,const gen & b):sommet(*o) {
if (b.type==_VECT)
feuille=gen(mergevecteur(vecteur(1,a),*b._VECTptr),b.subtype);
else
feuille=gen(makevecteur(a,b),_SEQ__VECT);
};
int symbolic::size() const {
if (feuille.type==_SYMB)
return 1+feuille._SYMBptr->size();
if (feuille.type!=_VECT)
return 2;
int s=1;
iterateur it=feuille._VECTptr->begin(),itend=feuille._VECTptr->end();
for (;it!=itend;++it){
if (it->type==_SYMB)
s += it->_SYMBptr->size();
else
++s;
}
return s;
}
bool print_rewrite_prod_inv=false;
static string & add_print_plus(string & s,const symbolic & g,GIAC_CONTEXT){
if (is_inf(g.feuille)
// && calc_mode(contextptr)!=1
&& abs_calc_mode(contextptr)==38
){
s+="โ";
return s;
}
if (g.feuille.type!=_VECT){
s += '+';
return add_print(s,g.feuille,contextptr);
}
vecteur & v = *g.feuille._VECTptr;
if (v.empty())
return s;
int l=int(v.size());
if (l==1)
s += '+';
add_print(s, v.front(),contextptr);
for (int i=1;i<l;++i){
s += '+';
add_print(s,v[i],contextptr);
}
return s;
}
static string & add_print_neg(string & s,const gen & feuille,GIAC_CONTEXT){
int l = int(s.size());
// +- and -- -> - and +
if (l && s[l-1]=='+')
s[l-1]='-';
else {
if (l && s[l-1]=='-')
s[l-1]='+';
else
s += calc_mode(contextptr)==38?"โ":"-";
}
if (feuille.type!=_CPLX){
if(feuille.type!=_SYMB || (feuille._SYMBptr->sommet==at_inv || feuille._SYMBptr->sommet==at_prod) || !need_parenthesis(feuille)){
return add_print(s,feuille,contextptr);
}
}
s += "(";
add_print(s,feuille,contextptr);
s += ")";
return s;
}
static string & add_print_inv(string & s,const gen & feuille,GIAC_CONTEXT){
gen f = feuille;
bool isneg=false;
bool calc38=calc_mode(contextptr)==38;
if (f.is_symb_of_sommet(at_neg) && f._SYMBptr->feuille.type!=_VECT){
f = f._SYMBptr->feuille;
isneg=true;
}
if (f.type<_CPLX && is_positive(-f,contextptr)){
f=-f;
isneg=!isneg;
}
int l=int(s.size());
if (isneg && l && s[l-1]=='-'){
if (l==1)
s.clear();
else
s[l-1]='+';
isneg=false;
}
if (isneg && l && s[l-1]=='+'){
s[l-1]='-';
isneg=false;
}
bool bt=f.type==_SYMB && (f._SYMBptr->sommet==at_plus || f._SYMBptr->sommet==at_inv || f._SYMBptr->sommet==at_prod || need_parenthesis(f._SYMBptr->sommet));
if ( !bt && (f.type!=_CPLX) && (f.type!=_MOD)){
s += (isneg?(calc38?"โ1/":"-1/"):"1/");
return add_print(s,f,contextptr);
}
else {
s += (isneg?(calc38?"โ1/(":"-1"):"1/(");
add_print(s,f,contextptr);
s += ")";
return s;
}
}
static string & add_print_prod(string & s,const symbolic & g,GIAC_CONTEXT){
gen n0,d0;
if (print_rewrite_prod_inv &&
rewrite_prod_inv(g.feuille,n0,d0)
){
if (n0.type<_CPLX || n0.type==_IDNT){
add_print(s,n0,contextptr);
s += "/";
}
else {
s +="(";
add_print(s,n0,contextptr);
s += ")/";
}
if (d0.type<_CPLX || d0.type==_IDNT)
add_print(s,d0,contextptr); // s += d0.print(contextptr);
else {
s += "(";
add_print(s,d0,contextptr); s += ")"; // s += "("+d0.print(contextptr)+")";
}
return s;
}
if (g.feuille.type!=_VECT)
return add_print(s,g.feuille,contextptr);
vecteur & v = *g.feuille._VECTptr;
int l=int(v.size());
for (int i=0;i<l;++i){
gen e(v[i]);
if (e.type!=_SYMB){
if (i)
s += '*';
if ( (e.type==_CPLX
&& need_parenthesis(e)
) || e.type==_MOD )
s += "("+e.print(contextptr)+")";
else
add_print(s,e,contextptr); // s +=e.print(contextptr);
}
else {
if (e._SYMBptr->sommet==at_inv){
gen f(e._SYMBptr->feuille);
if (i){
if ( (f.type==_CPLX) || (f.type==_MOD) ||
((f.type==_SYMB) &&
( (f._SYMBptr->sommet==at_plus) || (f._SYMBptr->sommet==at_prod) || need_parenthesis(f._SYMBptr->sommet) || f._SYMBptr->sommet==at_inv || (f._SYMBptr->sommet==at_pow && f._SYMBptr->feuille[0].is_symb_of_sommet(at_inv))))
){
s += "/(";
add_print(s,f,contextptr);
s += ")"; // s += ("/("+f.print(contextptr) + ")");
}
else {
s += "/";
add_print(s,f,contextptr);
// s += ("/"+f.print(contextptr));
}
}
else
add_print(s,e,contextptr); // s += e.print(contextptr);
} // end if e._SYMBptr->sommet==at_inv
else {
if (i)
s += '*';
if ( e._SYMBptr->sommet==at_plus || e._SYMBptr->sommet==at_neg || is_inequation(e) ){
s += "(";
add_print(s,e,contextptr);
s += ")";
// s += ("("+e.print(contextptr)+")");
}
else
add_print(s,e,contextptr); // s += e.print(contextptr);
}
}
} // end_for
return s;
}
static string & add_print_pow(string & s,const symbolic & g,GIAC_CONTEXT){
if (g.feuille.type!=_VECT || g.feuille._VECTptr->size()!=2)
return add_print(s,g.feuille,contextptr);
gen pui=g.feuille._VECTptr->back();
gen arg=g.feuille._VECTptr->front();
#ifndef GIAC_HAS_STO_38
if (__pow.printsommet==&cprintaspow){
s+="pow(";
add_print(s,arg,contextptr);
s+=",";
add_print(s,pui,contextptr);
s+=')';
return s;
}
#endif
bool argpar = ( (arg.type>_CPLX && arg.type!=_FLOAT_) || !is_positive(arg,contextptr)) && arg.type!=_IDNT ;
#ifdef EMCC
bool need=need_parenthesis(arg) || arg.type==_SYMB;
if (pui==plus_one_half){
s += (need?"โ(":"โ");
add_print(s,arg,contextptr);
s += (need?")":"");
return s;
}
#endif
if (abs_calc_mode(contextptr)==38){
bool need=need_parenthesis(arg) || arg.type==_SYMB;
if (pui==plus_one_half){
s += (need?"โ(":"โ");
add_print(s,arg,contextptr);
s += (need?")":"");
return s;
}
if ( pui==minus_one_half || pui==fraction(minus_one,plus_two) ){
s += (need?"1/โ(":"1/โ");
add_print(s,arg,contextptr);
s += (need?")":"");
return s;
}
if (pui==minus_one){
s += (need?"(":"");
add_print(s,arg,contextptr);
s += (need?")๎":"๎");
return s;
// return s += (need?")\xe2\x81\xb2":"\xe2\x81\xb2");
}
#if 0
if (pui==plus_two){
s += (argpar?"(":"");
add_print(s,arg,contextptr);
s += (argpar?")ยฒ":"ยฒ");
return s;
}
#endif
}
if (pui.type>_REAL && pui==plus_one_half){
s += "sqrt(";
add_print(s,arg,contextptr);
s += ')';
return s;
}
if ( pui.type>_REAL && (pui==minus_one_half || pui==fraction(minus_one,plus_two) )){
s += "1/sqrt(";
add_print(s,arg,contextptr);
s += ')';
return s;
}
if (arg.type==_IDNT || (arg.type==_SYMB && arg._SYMBptr->sommet!=at_neg && (arg._SYMBptr->sommet!=at_exp || calc_mode(contextptr)!=1) && !arg._SYMBptr->sommet.ptr()->printsommet)){
argpar=arg.is_symb_of_sommet(at_inv) || (arg.is_symb_of_sommet(at_exp) && abs_calc_mode(contextptr)==38);
if (argpar) s +='(';
if (pui.type==_SYMB || pui.type==_FRAC || pui.type==_CPLX){
add_print(s,arg,contextptr);
if (argpar) s +=')';
#ifdef GIAC_HAS_STO_38
s += '^';
#else
s += __pow.s;
#endif
s += '(';
add_print(s,pui,contextptr);
s += ')';
return s;
}
else {
add_print(s,arg,contextptr);
if (argpar) s +=')';
#ifdef GIAC_HAS_STO_38
s += '^';
#else
s += __pow.s;
#endif
return add_print(s,pui,contextptr);
}
}
if (argpar)
s += '(';
add_print(s,arg,contextptr);
if (argpar)
s += ')';
#ifdef GIAC_HAS_STO_38
s += '^';
#else
s += __pow.s;
#endif
bool puipar = pui.type==_SYMB || pui.type==_FRAC || pui.type==_CPLX || (pui.type==_VECT && pui.subtype==_SEQ__VECT);
if (puipar)
s += '(';
add_print(s,pui,contextptr);
if (puipar)
s += ')';
return s ;
}
static string & add_print_int(string & s,int i,GIAC_CONTEXT){
char ch[32];
int l=int(s.size());
if (i<0){
if (l && s[l-1]=='+')
s[l-1]='-';
else {
if (l && s[l-1]=='-'){
if (l==1)
s.clear();
else
s[l-1]='+';
}
else
s += (calc_mode(contextptr)==38)?"โ":"-"; // add a minus
}
i=-i;
}
switch (integer_format(contextptr)){
case 16:
my_sprintf(ch,"0x%X",i);
case 8:
my_sprintf(ch,"0o%o",i);
default:
my_sprintf(ch,"%d",i);
}
s += ch;
return s;
}
static string & add_print_symbolic(string & s,const symbolic & g,GIAC_CONTEXT){
if (!g.sommet.ptr()){
s+="NULL(";
s+=g.feuille.print();
s+=")";
return s;
}
if ( g.sommet.ptr()->printsommet && g.sommet!=at_plus && g.sommet!=at_prod && g.sommet!=at_pow){
s += g.sommet.ptr()->printsommet(g.feuille,g.sommet.ptr()->s,contextptr);
return s;
}
if ( g.feuille.type==_VECT && g.feuille._VECTptr->empty() ){
s += g.sommet.ptr()->print(contextptr);
s += "(NULL)";
return s;
}
if (g.sommet==at_prod)
return add_print_prod(s,g,contextptr);
if (g.sommet==at_plus)
return add_print_plus(s,g,contextptr);
if (g.sommet==at_pow)
return add_print_pow(s,g,contextptr);
if (g.sommet==at_neg)
return add_print_neg(s,g.feuille,contextptr);
if (g.sommet==at_inv)
return add_print_inv(s,g.feuille,contextptr);
if (g.sommet==at_exp
#ifndef EMCC
&& (calc_mode(contextptr)==1 || abs_calc_mode(contextptr)==38)
#endif
){
s += printasexp(g.feuille,0,contextptr);
return s;
}
if ( g.feuille.type!=_VECT || ( g.sommet!=at_prod && g.feuille._VECTptr->front().type==_VECT ) ){
s += g.sommet.ptr()->print(contextptr);
s += '(';
add_print(s,g.feuille,contextptr);
s += ')';
return s;
}
int l=int(g.feuille._VECTptr->size());
s += g.sommet.ptr()->print(contextptr);
s += '(';
if (g.feuille.subtype!=_SEQ__VECT)
s += begin_VECT_string(g.feuille.subtype,false,contextptr);
for (int i=0;;++i){
add_print(s,(*(g.feuille._VECTptr))[i],contextptr); // s += (*(feuille._VECTptr))[i].print(contextptr);
if (i==l-1){
break;
}
s += ',';
}
if (g.feuille.subtype!=_SEQ__VECT)
s += end_VECT_string(g.feuille.subtype,false,contextptr);
s += ')';
return s;
}
string & add_print(string & s,const gen & g,GIAC_CONTEXT){
if (g.type==_IDNT){
if (calc_mode(contextptr)==1 && (is_inf(g) || is_undef(g)))
s += "?";
else
(s += g._IDNTptr->print(contextptr));
return s;
}
int l=int(s.size());
if (g.type==_INT_ && g.subtype==0)
return add_print_int(s,g.val,contextptr);
if (g.type==_VECT && g.subtype==0){
s += calc_mode(contextptr)==1?'{':'[';
add_printinner_VECT(s,*g._VECTptr,0,contextptr);
s += calc_mode(contextptr)==1?'}':']';
return s;
}
if (g.type==_FRAC && g._FRACptr->num.type==_INT_ && g._FRACptr->den.type==_INT_){
add_print(s,g._FRACptr->num,contextptr);
s += "/";
add_print(s,g._FRACptr->den,contextptr);
return s;
}
if (g.type==_SYMB){
if (g.subtype==_SPREAD__SYMB)
s += "=";
return add_print_symbolic(s,*g._SYMBptr,contextptr);
}
#ifdef EMCC
const string tmp=g.print(contextptr);
#else
const string & tmp=g.print(contextptr);
#endif
// check +- -> - and -- -> +
if (l && s[l-1]=='+' ){
if (!tmp.empty() && tmp[0]=='-'){
s = s.substr(0,l-1);
s += tmp;
return s;
}
if (tmp.size()>3 && (unsigned char)tmp[0]==226 && (unsigned char)tmp[1]==136 && (unsigned char) tmp[2]==146 ) { // -30, -120, -110
s[l-1]='-';
s += tmp.substr(3,tmp.size()-3);
return s;
}
}
if (l && s[l-1]=='-' ){
if (!tmp.empty() && tmp[0]=='-'){
s[l-1]='+';
s += tmp.substr(1,tmp.size()-1);
return s;
}
if (tmp.size()>3 && (unsigned char)tmp[0]==226 && (unsigned char)tmp[1]==136 && (unsigned char) tmp[2]==146 ) { // -30, -120, -110
s[l-1]='+';
s += tmp.substr(3,tmp.size()-3);
return s;
}
}
s += tmp;
return s;
}
string symbolic::print(GIAC_CONTEXT) const{
string s;
add_print_symbolic(s,*this,contextptr);
return s;
}
/* EVAL without system stack */
static void eval_sto_pnt_symb(const gen & feuille,gen & e,GIAC_CONTEXT){
if (e.type==_SYMB && e.ref_count()==1 && e._SYMBptr->feuille.type==_VECT && e._SYMBptr->feuille.ref_count()==1){
vecteur & v=*e._SYMBptr->feuille._VECTptr;
// legende not converted to string to avoid malloc ->faster
v.push_back(feuille._VECTptr->back());
// v=makevecteur(v.front(),v.back(),string2gen(feuille._VECTptr->back().print(contextptr),false));
e._SYMBptr->feuille.subtype=_PNT__VECT;
}
else
e=new_ref_symbolic(symbolic(at_pnt,gen(makevecteur(e._SYMBptr->feuille._VECTptr->front(),e._SYMBptr->feuille._VECTptr->back(),string2gen(feuille._VECTptr->back().print(contextptr),false)),_PNT__VECT)));
e.subtype=gnuplot_show_pnt(*e._SYMBptr,contextptr);
}
static void eval_sto_pnt_vect(const gen & feuilleback,gen & e,GIAC_CONTEXT){
vecteur v=*e._VECTptr;
iterateur it=v.begin(),itend=v.end();
gen legende;
for (int pos=0;it!=itend;++pos,++it){
if ( (it->type==_SYMB) && (it->_SYMBptr->sommet==at_pnt) && (it->_SYMBptr->feuille._VECTptr->size()==2)){
if (feuilleback.type==_VECT && pos<int(feuilleback._VECTptr->size()))
legende=(*feuilleback._VECTptr)[pos];
else
legende=feuilleback;
if (legende.type==_IDNT && abs_calc_mode(contextptr)==38){
// HP: if legende is an IDNT, keep it as legende
// this save alloc (2 alloc instead of 3 alloc + 1 print
*it=new_ref_symbolic(symbolic(at_pnt,gen(makevecteur(it->_SYMBptr->feuille._VECTptr->front(),it->_SYMBptr->feuille._VECTptr->back(),legende),_PNT__VECT)));
}
else {
*it=new_ref_symbolic(symbolic(at_pnt,gen(makevecteur(it->_SYMBptr->feuille._VECTptr->front(),it->_SYMBptr->feuille._VECTptr->back(),string2gen(legende.print(contextptr),false)),_PNT__VECT)));
}
it->subtype=gnuplot_show_pnt(*it->_SYMBptr,contextptr);
}
}
e=gen(v,e.subtype);
}
static gen eval_sto(const gen & feuille,std::vector<const char *> & last,int level,GIAC_CONTEXT){ // autoname function
// detect vector/matrix addressing with () parsed as function definition
// e.g. M(j,k):=j+k+1 parsed as M:=(j,k)->j+k+1
// these affectations are marked by a subtype==1 by the parser
// if destination is a matrix
if (feuille.type==_VECT && feuille.subtype==_SORTED__VECT && feuille._VECTptr->size()==2 && feuille._VECTptr->front().is_symb_of_sommet(at_program)){
gen prog=feuille._VECTptr->front()._SYMBptr->feuille;
if (prog.type==_VECT && prog._VECTptr->size()==3 && (prog._VECTptr->front()!=_VECT || prog._VECTptr->front()._VECTptr->size()==2)){
gen val=prog._VECTptr->back();
if (feuille._VECTptr->back().type==_IDNT && feuille._VECTptr->back()._IDNTptr->eval(1,feuille._VECTptr->back(),contextptr).type==_VECT){
prog=symbolic(at_of,makesequence(feuille._VECTptr->back(),prog._VECTptr->front()));
return eval_sto(gen(makevecteur(val,prog),_SORTED__VECT),last,level,contextptr);
}
}
}
gen ans;
gen & feuilleback=feuille._VECTptr->back();
vecteur & lastarg=last_evaled_arg(contextptr);
if ( feuilleback.type==_SYMB && (feuilleback._SYMBptr->sommet==at_unquote || feuilleback._SYMBptr->sommet==at_hash ) ){
ans=_sto(feuille.eval(level,contextptr),contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
bool b=show_point(contextptr),quotearg=false;
if (b)
show_point(false,contextptr);
gen e=feuille._VECTptr->front();
#ifdef GIAC_HAS_STO_38 // quote STO> E, STO> F, STO>R, STO> X, STO>Y
if (feuilleback.type==_IDNT){
const char * ch = feuilleback._IDNTptr->id_name;
if (strlen(ch)==2 && ch[1]>='0' && ch[1]<='9' && (ch[0]=='E' || ch[0]=='F' || ch[0]=='X' || ch[0]=='Y'))
quotearg=true;
}
#endif
if (!quotearg)
e=e.eval(level,contextptr);
if (b)
show_point(b,contextptr);
if (e.type==_SYMB && e._SYMBptr->sommet==at_pnt && e._SYMBptr->feuille.type==_VECT && e._SYMBptr->feuille._VECTptr->size()==2 && (contextptr?!contextptr->previous:!protection_level) )
eval_sto_pnt_symb(feuille,e,contextptr);
if ( e.type==_VECT && !e._VECTptr->empty() && e._VECTptr->back().type==_SYMB && e._VECTptr->back()._SYMBptr->sommet==at_pnt && (contextptr?!contextptr->previous:!protection_level))
eval_sto_pnt_vect(feuilleback,e,contextptr);
ans=sto(e,feuilleback,contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
} // end sommet==at_sto
// http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-34.html#%_sec_5.4
enum {
nr_eval_dispatch=1,
nr_eval_vect=2,
nr_eval_of=18,
nr_eval_op=19, // below this increment destination
nr_eval_prog=21, // above this do not increment destination
nr_eval_sto=22,
nr_eval_local=23,
nr_eval_bloc=24,
nr_eval_if_cond=25,
nr_eval_if_true=26,
nr_eval_if_false=27,
nr_eval_for_init=28,
nr_eval_for_cond=29,
nr_eval_for_loop=30,
nr_eval_for_incr=31,
nr_eval_for_in=32,
nr_eval_catch=34,
};
#define NR_INIT_STACK_SIZE 200
struct nr_pointers_t{
gen * itbeg;
unsigned curpos,endpos;
unsigned arglpos;
gen old;
vecteur argl;
signed char state;
nr_pointers_t(unsigned _arglpos,gen * _itbeg,gen * _it,gen * _itend,gen _old,int argls,signed char _state):itbeg(_itbeg),curpos(unsigned(_it-_itbeg)),endpos(unsigned(_itend-_itbeg)),arglpos(_arglpos),old(_old),argl(argls),state(_state) {};
nr_pointers_t():itbeg(0),curpos(0),endpos(0),arglpos(0),old(0),argl(0),state(0){}
};
inline void fromto_restore(unsigned & arglpos,gen * & itbeg,gen * & it,gen * & itend,gen & old,vecteur & argl,signed char & state,vector<nr_pointers_t> & fromto_stack){
vector<nr_pointers_t>::iterator ptr=fromto_stack.end()-1;
arglpos=ptr->arglpos;
itbeg=ptr->itbeg;
it=ptr->itbeg+ptr->curpos;
itend=ptr->itbeg+ptr->endpos;
old=ptr->old;
argl.swap(ptr->argl);
state=ptr->state;
fromto_stack.pop_back();
}
struct nr_prog {
context * contextptr;
gen save_debug_info,vars;
bool save_sst_mode;
int protect;
nr_prog(context * _contextptr,const gen & _save_debug_info,const gen & _vars,bool _save_sst_mode,int _protect):contextptr(_contextptr),save_debug_info(_save_debug_info),vars(_vars),save_sst_mode(_save_sst_mode),protect(_protect) {};
nr_prog() : contextptr(0),save_debug_info(0),vars(0),save_sst_mode(false),protect(0) {}
};
// name changed: BESTA compiler see this as repeated code with same sig as passing the itor itself
// - compiler whinges like a wounded pig --
static void increment_instruction_ptr(const gen * it0,const gen * itend,GIAC_CONTEXT){
const gen * it=it0;
for (;it!=itend;++it)
increment_instruction(*it,contextptr);
}
// nr_eval is slower than usual eval because it saves what is usually saved
// on stack on the heap. It does not use stack for recursive calls
// Should be used if we are near the bottom of the stack
gen nr_eval(const gen & g,int level,const context * ct){
context * contextptr=(context *) ct;
gen *itbeg, * it, *itend; // source, begin, current and end of evaluation
gen * destination; // destination of evaluation result
gen res; // the final answer
gen tmp;
unsigned arglpos;
vector<nr_pointers_t> fromto_stack; // save/restore destination, source, source end pointers here
fromto_stack.reserve(NR_INIT_STACK_SIZE);
signed char state=nr_eval_dispatch;
gen old=0; // destination
gen progname=undef,label=undef,forlast,lasterr;
vecteur argl(1); // current evaled vecteur
vector<nr_prog> nr_prog_stack;
vector<int> nr_eval_for_stack;
nr_prog_stack.reserve(NR_INIT_STACK_SIZE);
gen prog;
itbeg=it=(gen *) &g; itend=it+1; destination=(gen *)&argl.front();
while (1){
#ifndef NO_STDEXCEPT
try {
#endif
if (lasterr.type==_STRNG || (destination->type==_STRNG && destination->subtype==-1)){
// error!
debug_ptr(contextptr)->current_instruction += int(itend-it);
it=itend;
if (lasterr.type!=_STRNG)
lasterr=*destination;
}
if (it!=itend){
if (state>=nr_eval_prog){
if (destination->type==_SYMB && (destination->_SYMBptr->sommet==at_return || destination->_SYMBptr->sommet==at_break || (state!=nr_eval_for_incr && destination->_SYMBptr->sommet==at_continue) ) ){
++it; // do not eval anymore until end of function or loop
++debug_ptr(contextptr)->current_instruction;
continue;
}
// goto
if (!is_undef(label)){
if (it->is_symb_of_sommet(at_label) && label==it->_SYMBptr->feuille){
*destination=*it;
label=undef;
}
++it;
if (it==itend){
debug_ptr(contextptr)->current_instruction -= int(itend-itbeg);
for (it=itbeg;it!=itend;++it){
if (it->is_symb_of_sommet(at_label) && label==it->_SYMBptr->feuille){
*destination=*it;
label=undef;
break;
}
++debug_ptr(contextptr)->current_instruction;
}
}
else
++debug_ptr(contextptr)->current_instruction;
continue;
}
if (state!=nr_eval_if_cond && state!=nr_eval_for_init && state!=nr_eval_for_cond && state!=nr_eval_for_incr && state!=nr_eval_sto){
if (state==nr_eval_for_loop && it==itbeg+3 && it->is_symb_of_sommet(at_bloc))
;
else {
++debug_ptr(contextptr)->current_instruction;
if (debug_ptr(contextptr)->debug_mode){
debug_loop(res,contextptr);
if (is_undef(res)) return res;
}
}
}
}
switch (it->type){
case _VECT:
if (it->subtype==_SPREAD__VECT){
makespreadsheetmatrice(*it->_VECTptr,contextptr);
spread_eval(*it->_VECTptr,contextptr);
*destination=*it;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
if (it->subtype==_FOLDER__VECT || it->subtype==_RGBA__VECT){
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
fromto_stack.push_back(nr_pointers_t(unsigned(destination-(gen *) &argl.front()),itbeg,it,itend,old,1,state));
argl.swap(fromto_stack.back().argl);
// vecteur beginning by comment?
tmp=*it;
if (it->subtype==_SEQ__VECT && !it->_VECTptr->empty() && it->_VECTptr->front().type==_SYMB
&& (it->_VECTptr->front()._SYMBptr->sommet==at_comment)
&& (it->_VECTptr->back().is_symb_of_sommet(at_return))
){
itend=(gen *) &it->_VECTptr->back()+1;
it=(gen *) &it->_VECTptr->front();
for (;it!=itend;++it){
if ( (it->type!=_SYMB) || (it->_SYMBptr->sommet!=at_comment) )
break;
}
if (it+1==itend){
*destination=*it;
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *) &argl.front()+arglpos;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
if (it==itend){
*destination=zero;
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *) &argl.front()+arglpos;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
}
else {
itend=(gen *)&it->_VECTptr->back()+1;
it=(gen *)&it->_VECTptr->front();
}
itbeg=it;
old=tmp;
// normal vector evaluation
state=nr_eval_vect;
argl.resize(itend-it);
// vecteur_stack.push_back(argl);
// argl=vecteur(itend-it);
destination=(gen *)&argl.front();
continue;
case _SYMB: {
// special cases
unary_function_ptr & u =it->_SYMBptr->sommet;
gen f=it->_SYMBptr->feuille;
if (u==at_quote){
*destination=quote(f,contextptr);
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
if (u==at_program) {
*destination=quote_program(it->_SYMBptr->feuille,contextptr);
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
int specop=0;
if (u==at_goto)
label=f;
if (u==at_ifte || u==at_when)
specop=nr_eval_if_cond;
if (u==at_for){
if (state==nr_eval_for_cond){ // for in for_cond is for the syntax for x in l
*destination=*it;
++it;
continue;
}
nr_eval_for_stack.push_back(0); // index for for var in list/string
nr_eval_for_stack.push_back(debug_ptr(contextptr)->current_instruction);
specop=nr_eval_for_init;
}
if (u==at_bloc)
specop=nr_eval_bloc;
if (u==at_local)
specop=nr_eval_local;
if (u==at_sto)
specop=nr_eval_sto;
if (u==at_of)
specop=nr_eval_of;
if (u==at_try_catch)
specop=nr_eval_catch;
if (!specop && u.quoted() && !f.is_symb_of_sommet(at_unquote)){
// check for hash
if (f.is_symb_of_sommet(at_hash) && f._SYMBptr->feuille.type==_STRNG)
f=gen(*f._SYMBptr->feuille._STRNGptr,contextptr);
*destination=u(f,contextptr);
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
}
// save pointers and state, eval args
fromto_stack.push_back(nr_pointers_t(unsigned(destination-(gen *)&argl.front()),itbeg,it,itend,old,1,state));
argl.swap(fromto_stack.back().argl);
state=specop?specop:nr_eval_op;
old=*it;
destination=(gen *)&argl.front();
if ( f.type==_VECT &&
(
(specop==nr_eval_sto && f._VECTptr->size()==2) ||
(specop==nr_eval_if_cond && f._VECTptr->size()>=3) ||
(specop==nr_eval_for_init && f._VECTptr->size()>=3) ||
(specop==nr_eval_catch && f._VECTptr->size()>=3)
) ){
// sto: eval first arg, but not the second arg
// if: eval first arg, but not the second third arg
// for: eval first arg [init]
// try ... catch: eval first arg
itbeg=it=&f._VECTptr->front();
itend=it+1;
continue;
}
if (specop==nr_eval_local){
int protect=0;
gen vars,values;
context * save_contextptr=contextptr;
// Bind local var
prog=f;
vars=prog._VECTptr->front();
if (vars.type==_VECT && vars._VECTptr->size()==2 && vars._VECTptr->front().type!=_IDNT)
vars = vars._VECTptr->front();
if (vars.type!=_VECT)
vars=makevecteur(vars);
values=gen(vecteur(vars._VECTptr->size()));
for (unsigned i=0;i<vars._VECTptr->size();++i){
tmp=(*vars._VECTptr)[i];
if (tmp.is_symb_of_sommet(at_sto) || is_equal(tmp)){
(*values._VECTptr)[i]=tmp._SYMBptr->feuille._VECTptr->back();
(*vars._VECTptr)[i]=tmp._SYMBptr->feuille._VECTptr->front().eval(1,contextptr);
}
}
prog=prog._VECTptr->back();
protect=giac::bind(*values._VECTptr,*vars._VECTptr,contextptr);
if (protect==-RAND_MAX){
gensizeerr(res,contextptr);
return res;
}
// save previous state
nr_prog_stack.push_back(nr_prog(save_contextptr,0,vars,false,protect));
if (prog.type==_VECT && prog.subtype==0){
itbeg=it=(gen *)&prog._VECTptr->front();
itend=(gen *)&prog._VECTptr->back()+1;
}
else {
itbeg=it=&prog;
itend=it+1;
}
continue;
}
// if (specop==nr_eval_of || specof==nr_eval_bloc){
// state=specop; // program name is in old, eval args normally
// }
// normal symbolic
if (f.type==_VECT && (f.subtype==_SEQ__VECT || (specop==nr_eval_of || specop==nr_eval_bloc))){
itend=(gen *)&f._VECTptr->back()+1;
itbeg=it=(gen *)&f._VECTptr->front();
argl.resize(itend-it);
destination=(gen *)&argl.front();
}
else {
itbeg=it=&it->_SYMBptr->feuille; // do not use &f here!
itend=it+1;
}
continue;
}
case _IDNT:
tmp=eval(*it,level,contextptr);
if (tmp.type!=_VECT || tmp.subtype!=_SEQ__VECT || (state!=nr_eval_vect && !(state==nr_eval_op && old.type==_SYMB && old._SYMBptr->feuille.type==_VECT)))
*destination=tmp;
else {
// tmp is a sequence inside a vector,
// enlarge argl and copy tmp._VECTptr in argl
int pos=int(destination-(gen *)&argl.front());
for (unsigned i=1;i<tmp._VECTptr->size();++i)
argl.push_back(0);
destination=(gen *)&argl.front()+pos;
for (unsigned i=0;;){
*destination=(*tmp._VECTptr)[i];
++i;
if (i==tmp._VECTptr->size())
break;
++destination;
}
}
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
default:
*destination=*it;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
} // end switch on it->type
}
// it==itend
// end eval of current vector
signed char oldsubtype=old.type==_VECT?old.subtype:0;
if (fromto_stack.empty()){
return argl.front(); // end evaluation
}
// dispatch depending on current mode
switch (state){
case nr_eval_dispatch:
continue;
case nr_eval_vect:
// end eval of vecteur, store value in res
if (oldsubtype==_SET__VECT && !argl.empty()){
// remove multiple occurences
islesscomplexthanf_sort(argl.begin(),argl.end());
vecteur tmp;
tmp.reserve(argl.size());
tmp.push_back(argl.front());
for (iterateur jt=argl.begin()+1;jt!=argl.end();++jt){
if (*jt!=tmp.back())
tmp.push_back(*jt);
}
tmp.swap(argl);
res=gen(argl,oldsubtype);
}
else {
// FIXME: make a faster == check (may returns false for large == vecteurs)
if (old.type==_VECT && old._VECTptr->size()<17 && *old._VECTptr==argl)
res=old;
else
res=gen(argl,oldsubtype);
}
// restore previous state, old, pointers and argl
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *)&argl.front()+arglpos;
*destination=res;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue; // end state==nr_eval_vect
case nr_eval_for_init:
forlast=undef;
++itend;
state=nr_eval_for_cond;
continue;
case nr_eval_for_cond:
res=*destination;
if ( nr_eval_for_stack.size()>=2 && (res.is_symb_of_sommet(at_for) || res.is_symb_of_sommet(at_pour)) && res._SYMBptr->feuille.type==_VECT && res._SYMBptr->feuille._VECTptr->size()==2){
// for var in list/string
res=eval(res._SYMBptr->feuille._VECTptr->back(),1,contextptr);
if (res.type==_VECT){
if (int(res._VECTptr->size())>nr_eval_for_stack[nr_eval_for_stack.size()-2]){
res=res[nr_eval_for_stack[nr_eval_for_stack.size()-2]];
sto(res,destination->_SYMBptr->feuille._VECTptr->front(),contextptr);
res=1;
}
else
res=0;
}
else {
if (res.type==_STRNG){
if (int(res._STRNGptr->size())>nr_eval_for_stack[nr_eval_for_stack.size()-2]){
res=string2gen(string(1,(*res._STRNGptr)[nr_eval_for_stack[nr_eval_for_stack.size()-2]]),false);
sto(res,destination->_SYMBptr->feuille._VECTptr->front(),contextptr);
res=1;
}
else
res=0;
}
else
res=0;
}
++nr_eval_for_stack[nr_eval_for_stack.size()-2];
}
res=equaltosame(res).eval(eval_level(contextptr),contextptr);
if (!is_integer(res)){
res=res.evalf_double(eval_level(contextptr),contextptr);
if ( res.type!=_DOUBLE_ && res.type!=_CPLX ){
if (old._SYMBptr->sommet==at_for){
if (!nr_eval_for_stack.empty()) // pop back instruction and for in index
nr_eval_for_stack.pop_back();
if (!nr_eval_for_stack.empty())
nr_eval_for_stack.pop_back();
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *)&argl.front()+arglpos;
gensizeerr(gettext("For: Unable to check test"),*destination);
res=*destination;
it=itend;
continue;
}
else
res=old;
}
}
if (!is_zero(res)){ // TRUE
it++; itend+=2;
state=nr_eval_for_loop;
}
else {
increment_instruction_ptr(it+1,it+3,contextptr);
if (!nr_eval_for_stack.empty()) // pop back instruction and for in index
nr_eval_for_stack.pop_back();
if (!nr_eval_for_stack.empty())
nr_eval_for_stack.pop_back();
// restore state and pointers
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *)&argl.front()+arglpos;
*destination=forlast;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
}
continue;
case nr_eval_for_loop:
if (destination->type==_SYMB && (destination->_SYMBptr->sommet==at_break || destination->_SYMBptr->sommet==at_return)){
if (destination->_SYMBptr->sommet==at_break)
*destination=undef;
if (!nr_eval_for_stack.empty()) // pop back instruction and for in index
nr_eval_for_stack.pop_back();
if (!nr_eval_for_stack.empty())
nr_eval_for_stack.pop_back();
state=nr_eval_bloc;
}
else {
it -= 2; --itend;
state=nr_eval_for_incr;
forlast=*destination;
}
continue;
case nr_eval_for_incr:
if (!nr_eval_for_stack.empty())
debug_ptr(contextptr)->current_instruction=nr_eval_for_stack.back();
if (debug_ptr(contextptr)->debug_mode){
debug_loop(res,contextptr);
if (is_undef(res)) return res;
}
state=nr_eval_for_cond;
it -= 2;
itend -= 1;
continue;
case nr_eval_catch:
// catch lasterr, FIXME for debug
state=nr_eval_if_false;
if (lasterr.type==_STRNG){
lasterr.subtype=0;
sto(lasterr,*it,contextptr);
++it;
itend += 2;
lasterr=*destination=res=0;
continue;
}
increment_instruction(*(it+1),contextptr);
if (old._SYMBptr->feuille._VECTptr->size()==4){
it += 2;
itend += 3;
}
continue;
case nr_eval_if_cond:
res=*destination;
res=equaltosame(res).eval(eval_level(contextptr),contextptr);
if (!is_integer(res)){
res=res.evalf_double(eval_level(contextptr),contextptr);
if ( res.type!=_DOUBLE_ && res.type!=_CPLX ){
if (old._SYMBptr->sommet==at_ifte){
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *)&argl.front()+arglpos;
gensizeerr(gettext("Ifte: Unable to check test"),*destination);
res=*destination;
it=itend;
continue;
}
else
res=old;
}
}
if (!is_zero(res)){ // TRUE
state=nr_eval_if_true;
++itend;
}
else { //FALSE
if (old._SYMBptr->sommet==at_ifte){
increment_instruction(*it,contextptr);
}
state=nr_eval_if_false;
++it; itend+=2;
}
continue;
case nr_eval_of:
if (argl.size()==2 && argl.front().is_symb_of_sommet(at_program)){
debug_struct * dbgptr=debug_ptr(contextptr);
int protect=0;
bool save_sst_mode=dbgptr->sst_mode;
gen vars,values;
context * save_contextptr=contextptr;
dbgptr->sst_at_stack.push_back(dbgptr->sst_at);
dbgptr->sst_at.clear();
progname=old._SYMBptr->feuille._VECTptr->front();
if (progname.is_symb_of_sommet(at_program))
progname=undef;
if (progname.type==_IDNT)
adjust_sst_at(progname,contextptr);
dbgptr->current_instruction_stack.push_back(dbgptr->current_instruction);
dbgptr->current_instruction=0;
if (dbgptr->sst_in_mode){
dbgptr->sst_in_mode=false;
dbgptr->sst_mode=true;
}
else
dbgptr->sst_mode=false;
// Bind local var
prog=argl.front()._SYMBptr->feuille;
vars=prog._VECTptr->front();
values=argl[1];
prog=prog._VECTptr->back();
if (vars.type!=_VECT)
vars=gen(makevecteur(vars));
if (values.type!=_VECT || values.subtype!=_SEQ__VECT || (vars._VECTptr->size()==1 && values._VECTptr->size()!=1))
values=gen(makevecteur(values));
// *logptr(contextptr) << vars << " " << values << endl;
// removed sst test so that when a breakpoint is evaled
// the correct info is displayed
(*dbgptr->fast_debug_info_ptr)=prog;
(*dbgptr->debug_info_ptr)=prog;
if (!vars._VECTptr->empty())
protect=giac::bind(*values._VECTptr,*vars._VECTptr,contextptr);
if (protect==-RAND_MAX){
program_leave(*dbgptr->debug_info_ptr,save_sst_mode,dbgptr);
gensizeerr(res,contextptr);
return res;
}
// save previous state
nr_prog_stack.push_back(nr_prog(save_contextptr,*dbgptr->debug_info_ptr,vars,save_sst_mode,protect));
dbgptr->args_stack.push_back(gen(mergevecteur(vecteur(1,progname),*values._VECTptr)));
if (prog.type==_VECT && prog.subtype==0){
itbeg=it=(gen *)&prog._VECTptr->front();
itend=(gen *)&prog._VECTptr->back()+1;
}
else {
itbeg=it=&prog;
itend=it+1;
}
state=nr_eval_prog;
}
else
state=nr_eval_op; // like a goto nr_eval_op: below
continue;
case nr_eval_if_true:
if (old._SYMBptr->sommet==at_ifte)
increment_instruction(*it,contextptr); // no break here
case nr_eval_if_false:
res=*destination; // no break here
case nr_eval_op: case nr_eval_sto:
// eval operator
if (state==nr_eval_sto)
res=sto(*destination,old._SYMBptr->feuille._VECTptr->back(),contextptr);
else {
if (state==nr_eval_op){
if (old._SYMBptr->feuille.type==_VECT && old._SYMBptr->feuille.subtype==_SEQ__VECT)
res=gen(argl,_SEQ__VECT);
else
res=*destination;
res=(*old._SYMBptr->sommet.ptr())(res,contextptr);
}
}
// no break here -> restore
case nr_eval_bloc:
// restore state and pointers
if (state==nr_eval_bloc)
res=*destination;
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *)&argl.front()+arglpos;
*destination=res;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
case nr_eval_prog: case nr_eval_local:
// end of program reached, restore context
res=*destination;
if (state==nr_eval_prog && res.is_symb_of_sommet(at_return))
res=res._SYMBptr->feuille;
if (!nr_prog_stack.back().vars._VECTptr->empty())
leave(nr_prog_stack.back().protect,*nr_prog_stack.back().vars._VECTptr,contextptr);
if (state==nr_eval_prog)
program_leave(nr_prog_stack.back().save_debug_info,nr_prog_stack.back().save_sst_mode,debug_ptr(contextptr));
contextptr=nr_prog_stack.back().contextptr;
nr_prog_stack.pop_back();
// restore state and pointers
fromto_restore(arglpos,itbeg,it,itend,old,argl,state,fromto_stack);
destination=(gen *)&argl.front()+arglpos;
*destination=res;
++it;
if (it!=itend && state<=nr_eval_op)
++destination;
continue;
default:
gensizeerr(gettext("Bad state"),res);
return res;
} // end switch
#ifndef NO_STDEXCEPT
}
catch (std::runtime_error & e) {
res=string2gen(e.what(),false);
res.subtype=-1;
*destination=res;
}
#endif
} // end while(1)
return argl.front();
}
gen symbolic::eval(int level,const context * contextptr) const {
if (level==0 || !sommet.ptr())
return *this;
gen ans;
// FIXME test should be removed later, it's here for tests. See global.cc DEFAULT_EVAL_LEVEL
int & elevel=eval_level(contextptr);
if (elevel==26)
return nr_eval(*this,level,contextptr);
std::vector<const char *> & last =last_evaled_function_name(contextptr);
last.push_back(sommet.ptr()->s);
vecteur & lastarg=last_evaled_arg(contextptr);
lastarg.push_back(feuille);
if (sommet==at_sto && feuille.type==_VECT)
return eval_sto(feuille,last,level,contextptr);
if (sommet.quoted()){
#ifndef RTOS_THREADX
if (feuille.type==_SYMB){
unary_function_ptr & u=feuille._SYMBptr->sommet;
if (u==at_unquote){
ans=sommet(feuille.eval(level,contextptr),contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
if (u==at_hash){
ans=sommet(gen(*feuille._SYMBptr->feuille._STRNGptr,contextptr),contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
}
#endif
int save_level=elevel;
elevel=level;
#ifdef NO_STDEXCEPT
ans=sommet(feuille,contextptr);
#else
try {
ans=sommet(feuille,contextptr);
}
catch (std::runtime_error & err){
elevel=save_level;
throw(err);
}
#endif
elevel=save_level;
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
} // if (sommet.quoted())
else {
// pnt check required because pnt name might be the identifier->recursion
if (sommet==at_pnt && feuille.type==_VECT && feuille._VECTptr->size()==3){
ans=(*sommet.ptr())(gen(vecteur(feuille._VECTptr->begin(),feuille._VECTptr->begin()+2),feuille.subtype).in_eval(level,ans,contextptr)?ans:feuille,contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
if ((sommet==at_neg) && (feuille.type==_IDNT) && !strcmp(feuille._IDNTptr->id_name,string_infinity)){
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return minus_inf;
}
if (sommet==at_quote){
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return quote(feuille,contextptr);
}
ans=(*sommet.ptr())(feuille.in_eval(level,ans,contextptr)?ans:feuille,contextptr);
/*
if (feuille.in_eval(level,ans,contextptr))
ans=(*sommet.ptr())(ans,contextptr);
else
ans=(*sommet.ptr())(feuille,contextptr);
*/
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
}
bool rewrite_prod_inv(const gen & arg,gen & n,gen & d){
n=1; d=1;
if (arg.type==_VECT && !arg._VECTptr->empty() && arg._VECTptr->back().is_symb_of_sommet(at_inv)) {
vecteur & uv=*arg._VECTptr;
int tmps=int(uv.size()),invbegin;
vecteur den(1,uv.back()._SYMBptr->feuille);
// group all inv from the end to the beginning for the denominator
for (invbegin=tmps-2;invbegin>=0;--invbegin){
if (!uv[invbegin].is_symb_of_sommet(at_inv))
break;
den.push_back(uv[invbegin]._SYMBptr->feuille);
}
vecteur num;
for (int i=0;i<=invbegin;++i){
if (uv[i].is_symb_of_sommet(at_inv) && uv[i]._SYMBptr->feuille.type<_POLY)
d=d*uv[i]._SYMBptr->feuille;
else
num.push_back(uv[i]);
}
if (!is_one(d))
den.insert(den.begin(),d);
if (den.size()>1)
d=new_ref_symbolic(symbolic(at_prod,den));
else
d=den.front();
if (!num.empty()){
if (num.size()==1)
n=num.front();
else
n=new_ref_symbolic(symbolic(at_prod,num));
}
return true;
}
// Group scalar denominators (warning, do not use for matrices!)
vecteur num,den;
prod2frac(new_ref_symbolic(symbolic(at_prod,arg)),num,den);
if (!den.empty()){
if (num.empty())
n=plus_one;
else {
if (num.size()==1)
n=num.front();
else
n=new_ref_symbolic(symbolic(at_prod,num));
}
/* code that does not work with matrices
if (den.size()==1)
d=den.front();
else
d=ref_symbolic(symbolic(at_prod,den);)
*/
if (den.size()==1 && den.front().type<_IDNT){
d=den.front();
return true;
}
}
return false;
}
gen symbolic::evalf(int level,const context * contextptr) const {
if (level==0)
return *this;
std::vector<const char *> & last =last_evaled_function_name(contextptr);
last.push_back(sommet.ptr()->s);
vecteur & lastarg=last_evaled_arg(contextptr);
lastarg.push_back(feuille);
if (sommet==at_sto){ // autoname function
gen e=feuille._VECTptr->front().evalf(level,contextptr);
if ((e.type==_SYMB) && (e._SYMBptr->sommet==at_pnt) && (e._SYMBptr->feuille.type==_VECT) && (e._SYMBptr->feuille._VECTptr->size()==2))
e=new_ref_symbolic(symbolic(at_pnt,gen(makevecteur(e._SYMBptr->feuille._VECTptr->front(),e._SYMBptr->feuille._VECTptr->back(),string2gen(feuille._VECTptr->back().print(contextptr),false)),_PNT__VECT)));
if ( (e.type==_VECT) && (e._VECTptr->size()) && (e._VECTptr->back().type==_SYMB) && (e._VECTptr->back()._SYMBptr->sommet==at_pnt)){
vecteur v=*e._VECTptr;
iterateur it=v.begin(),itend=v.end();
for (;it!=itend;++it){
if ( (it->type==_SYMB) && (it->_SYMBptr->sommet==at_pnt) && (it->_SYMBptr->feuille._VECTptr->size()==2))
*it=new_ref_symbolic(symbolic(at_pnt,gen(makevecteur(it->_SYMBptr->feuille._VECTptr->front(),it->_SYMBptr->feuille._VECTptr->back(),string2gen(feuille._VECTptr->back().print(contextptr),false)),_PNT__VECT)));
}
e=v;
}
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return sto(e,feuille._VECTptr->back(),contextptr);
}
gen ans;
if (sommet==at_plus){
if (feuille.type!=_VECT){
if (feuille.type==_IDNT && !strcmp(feuille._IDNTptr->id_name,string_infinity))
ans=plus_inf;
else
ans=feuille.evalf(level,contextptr);
}
else {
const_iterateur it=feuille._VECTptr->begin(),itend=feuille._VECTptr->end();
for (;it!=itend;++it){
ans=ans+it->evalf(level,contextptr);
}
}
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
if (sommet==at_prod){
if (feuille.type!=_VECT)
ans=feuille.evalf(level,contextptr);
else {
ans=1;
const_iterateur it=feuille._VECTptr->begin(),itend=feuille._VECTptr->end();
for (;it!=itend;++it){
ans=operator_times(ans,it->evalf(level,contextptr),contextptr);
}
}
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
if (sommet.quoted() && sommet!=at_and && !equalposcomp(plot_sommets,sommet) ){
ans=sommet(feuille,contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
else {
if ((sommet==at_neg) && (feuille.type==_IDNT) && !strcmp(feuille._IDNTptr->id_name,string_infinity)){
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return minus_inf;
}
if (sommet==at_quote){
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return quote(feuille,contextptr);
}
if (sommet==at_and || (sommet!=at_cercle && equalposcomp(plot_sommets,sommet))){
// bool save_is_inevalf=is_inevalf;
// is_inevalf=true;
ans=new_ref_symbolic(symbolic(sommet,feuille.evalf(1,contextptr)));
// is_inevalf=save_is_inevalf;
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
ans=(*sommet.ptr())(feuille.evalf(level,contextptr),contextptr);
if (!last.empty())
last.pop_back();
if (!lastarg.empty())
lastarg.pop_back();
return ans;
}
}
unsigned taille(const gen & g,unsigned max){
if (g.type<=_IDNT)
return 1;
if (g.type==_FRAC)
return 1+taille(g._FRACptr->num,max)+taille(g._FRACptr->den,max);
if (g.type==_SYMB){
if (g.is_symb_of_sommet(at_curve))
return 10;
return 1+taille(g._SYMBptr->feuille,max);
}
if (g.type==_VECT){
unsigned res=0;
const_iterateur it=g._VECTptr->begin(),itend=g._VECTptr->end();
for (;it!=itend;++it){
res += taille(*it,max);
if (max && res>max)
return res;
}
return res;
}
return 2;
}
int print_max_depth=100;
unsigned depth(const gen & g,unsigned add,unsigned max){
gen g_(g);
for (;g_.type==_SYMB;++add){
g_=g_._SYMBptr->feuille;
}
if (add>=max)
return add;
if (g_.type==_VECT){
unsigned res=add;
const_iterateur it=g_._VECTptr->begin(),itend=g_._VECTptr->end();
for (;it!=itend;++it){
unsigned cur=depth(*it,add,max);
if (max && cur>max)
return res;
if (cur>res)
res=cur;
}
return res;
}
return add;
}
#ifdef NSPIRE
template<class T> nio::ios_base<T> & operator << (nio::ios_base<T> & os,const symbolic & s) { return os << s.print(context0); }
#else
ostream & operator << (ostream & os,const symbolic & s) { return os << s.print(context0); }
#endif
#ifndef NO_NAMESPACE_GIAC
} // namespace giac
#endif // ndef NO_NAMESPACE_GIAC