/
usual.cc
11374 lines (11071 loc) · 423 KB
/
usual.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++-3.4 -I.. -I../include -g -c usual.cc -Wall -D_I386_ -DHAVE_CONFIG_H -DIN_GIAC -msse" -*-
#include "giacPCH.h"
/*
* Copyright (C) 2000,14 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 <stdexcept>
#include <cmath>
#include <cstdlib>
#if !defined GIAC_HAS_STO_38 && !defined NSPIRE && !defined FXCG
#include <fstream>
#endif
#include "gen.h"
#include "identificateur.h"
#include "symbolic.h"
#include "poly.h"
#include "usual.h"
#include "series.h"
#include "modpoly.h"
#include "sym2poly.h"
#include "moyal.h"
#include "subst.h"
#include "gausspol.h"
#include "identificateur.h"
#include "ifactor.h"
#include "prog.h"
#include "rpn.h"
#include "plot.h"
#include "pari.h"
#include "tex.h"
#include "unary.h"
#include "intg.h"
#include "ti89.h"
#include "solve.h"
#include "alg_ext.h"
#include "lin.h"
#include "derive.h"
#include "series.h"
#include "misc.h"
#include "sparse.h"
#include "input_parser.h"
#include "giacintl.h"
#ifdef VISUALC
#include <float.h>
#endif
#ifdef HAVE_LIBGSL
#include <gsl/gsl_math.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_sf_psi.h>
#include <gsl/gsl_sf_zeta.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf_erf.h>
#include <gsl/gsl_sf_expint.h>
#endif
//#ifdef TARGET_OS_IPHONE
//#include "psi.h"
//#endif
#ifdef USE_GMP_REPLACEMENTS
#undef HAVE_GMPXX_H
#undef HAVE_LIBMPFR
#undef HAVE_LIBPARI
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
// must be declared before any function declaration with special handling
vector<const unary_function_ptr *> & limit_tractable_functions(){
static vector<const unary_function_ptr *> * ans = 0;
if (!ans) ans=new vector<const unary_function_ptr *>;
return * ans;
}
vector<gen_op_context> & limit_tractable_replace(){
static vector<gen_op_context> * ans = 0;
if (!ans) ans=new vector<gen_op_context>;
return * ans;
}
#ifdef HAVE_SIGNAL_H_OLD
string messages_to_print ;
#endif
gen frac_neg_out(const gen & g,GIAC_CONTEXT){
if ( (is_integer(g) && is_strictly_positive(-g,contextptr)) || (g.type==_FRAC && (g._FRACptr->num.type<=_DOUBLE_ || g._FRACptr->num.type==_FLOAT_) && is_strictly_positive(-g._FRACptr->num,contextptr)) )
return symbolic(at_neg,-g);
if (g.is_symb_of_sommet(at_prod)){
// count neg
gen f=g._SYMBptr->feuille;
vecteur fv(gen2vecteur(f));
int count=0,fvs=int(fv.size());
for (int i=0;i<fvs;++i){
gen & fvi = fv[i];
fvi=frac_neg_out(fvi,contextptr);
if (fvi.is_symb_of_sommet(at_neg)){
++count;
fvi=fvi._SYMBptr->feuille;
}
}
if (fvs==1)
f=fv[0];
else {
if (f.type==_VECT && *f._VECTptr==fv) // nothing changed
f=g;
else
f=symbolic(at_prod,fv);
}
if (count%2)
return symbolic(at_neg,f);
else
return f;
}
return g;
}
// utilities for trig functions
enum { trig_deno=24 };
static bool is_multiple_of_12(const gen & k0,int & l){
if (!k0.is_integer())
return false;
gen k=smod(k0,trig_deno);
if (k.type!=_INT_)
return false;
l=k.val+trig_deno/2;
return true;
}
//grad
static bool is_multiple_of_pi_over_12(const gen & a,int & l,GIAC_CONTEXT){
if (is_zero(a,contextptr)){
l=0;
return true;
}
gen k;
if (angle_radian(contextptr)){
if (!contains(a,cst_pi))
return false;
k=derive(a,cst_pi,contextptr);
if (is_undef(k) || !is_constant_wrt(k,cst_pi,contextptr) || !is_zero(ratnormal(a-k*cst_pi,contextptr)))
return false;
k=(trig_deno/2)*k;
if (k.type==_SYMB)
k=ratnormal(k,contextptr);
/*
gen k1=normal(rdiv(a*gen(trig_deno/2),cst_pi),contextptr);
if (k!=k1)
setsizeerr();
*/
}
else if(angle_degree(contextptr))
k=rdiv(a,15,context0);
//grad
else
k=rdiv(a,rdiv(50,3),context0); //50/3 grads, due to 200/12
return is_multiple_of_12(k,l);
}
static bool is_rational(const gen & a,int &n,int &d){
gen num,den;
fxnd(a,num,den);
if (num.type!=_INT_ || den.type!=_INT_)
return false;
n=num.val;
d=den.val;
return true;
}
// checking utility
static bool check_2d_vecteur(const gen & args) {
if (args.type!=_VECT)
return false; // settypeerr(gettext("check_2d_vecteur"));
if (args._VECTptr->size()!=2)
return false; // setsizeerr(gettext("check_2d_vecteur"));
return true;
}
// zero arg
/*
unary_function_constant __1(1);
unary_function_ptr at_one (&__1);
unary_function_constant __0(0);
unary_function_ptr at_zero (&__0);
*/
gen _constant_one(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
return 1;
}
static const char _constant_one_s []="1";
static define_unary_function_eval (__constant_one,&_constant_one,_constant_one_s);
define_unary_function_ptr( at_one ,alias_at_one ,&__constant_one);
gen _constant_zero(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
return 0;
}
static const char _constant_zero_s []="0";
static define_unary_function_eval (__constant_zero,&_constant_zero,_constant_zero_s);
define_unary_function_ptr( at_zero ,alias_at_zero ,&__constant_zero);
gen _rm_a_z(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
#if 0 // !defined RTOS_THREADX && !defined BESTA_OS && !defined FREERTOS && !defined FXCG
if (variables_are_files(contextptr)){
char a_effacer[]="a.cas";
for (;a_effacer[0]<='z';++a_effacer[0]){
unlink(a_effacer);
}
}
#endif
for (char c='a';c<='z';c++){
if (c=='e' || c=='i') continue; // skip exp(1) and sqrt(-1)
purgenoassume(gen(string(1,c),contextptr),contextptr);
}
return args;
}
static const char _rm_a_z_s []="rm_a_z";
static define_unary_function_eval (__rm_a_z,&_rm_a_z,_rm_a_z_s);
define_unary_function_ptr5( at_rm_a_z ,alias_at_rm_a_z,&__rm_a_z,0,true);
gen _rm_all_vars(const gen & args,const context * contextptr){
if ( args.type==_STRNG && args.subtype==-1) return args;
gen g=_VARS(args,contextptr);
if (g.type!=_VECT)
return g;
vecteur & v=*g._VECTptr;
const_iterateur it=v.begin(),itend=v.end();
for (;it!=itend;++it){
gen tmp=*it;
if (tmp.is_symb_of_sommet(at_sto))
tmp=tmp._SYMBptr->feuille[1];
if (tmp.type==_IDNT && (tmp!=cst_pi) )
purgenoassume(tmp,contextptr);
}
return g;
}
static const char _rm_all_vars_s []="rm_all_vars";
static define_unary_function_eval (__rm_all_vars,&_rm_all_vars,_rm_all_vars_s);
define_unary_function_ptr5( at_rm_all_vars ,alias_at_rm_all_vars,&__rm_all_vars,0,true);
bool is_equal(const gen & g){
return (g.type==_SYMB) && (g._SYMBptr->sommet==at_equal || g._SYMBptr->sommet==at_equal2);
}
gen apply_to_equal(const gen & g,const gen_op & f){
if (g.type!=_SYMB || (g._SYMBptr->sommet!=at_equal && g._SYMBptr->sommet!=at_equal2) || g._SYMBptr->feuille.type!=_VECT)
return f(g);
vecteur & v=*g._SYMBptr->feuille._VECTptr;
if (v.empty())
return gensizeerr(gettext("apply_to_equal"));
return symbolic(g._SYMBptr->sommet,gen(makevecteur(f(v.front()),f(v.back())),_SEQ__VECT));
}
gen apply_to_equal(const gen & g,gen (* f) (const gen &, GIAC_CONTEXT),GIAC_CONTEXT){
if (g.type!=_SYMB || (g._SYMBptr->sommet!=at_equal && g._SYMBptr->sommet!=at_equal2) || g._SYMBptr->feuille.type!=_VECT)
return f(g,contextptr);
vecteur & v=*g._SYMBptr->feuille._VECTptr;
if (v.empty())
return gensizeerr(contextptr);
return symbolic(g._SYMBptr->sommet,gen(makevecteur(f(v.front(),contextptr),f(v.back(),contextptr)),_SEQ__VECT));
}
// one arg
gen _id(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
return args;
}
define_partial_derivative_onearg_genop(D_at_id,"D_at_id",_constant_one);
static const char _id_s []="id";
#ifdef GIAC_HAS_STO_38
static define_unary_function_eval3 (__id,&_id,(size_t)&D_at_idunary_function_ptr,_id_s);
#else
static define_unary_function_eval3 (__id,&_id,D_at_id,_id_s);
#endif
define_unary_function_ptr5( at_id ,alias_at_id,&__id,0,true);
static string printasnot(const gen & g,const char * s,GIAC_CONTEXT){
if (abs_calc_mode(contextptr)==38){
if (is_inequation(g) || g.is_symb_of_sommet(at_and) ||g.is_symb_of_sommet(at_ou))
return "NOT("+g.print(contextptr)+")";
else
return "NOT "+g.print(contextptr);
}
else
return "not("+g.print(contextptr)+")";
}
symbolic symb_not(const gen & args){
return symbolic(at_not,args);
}
gen _not(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
if (args.type==_VECT || args.type==_MAP){
if (python_compat(contextptr)){
if (args.type==_VECT && args._VECTptr->empty())
return 1;
if (args.type==_MAP && args._MAPptr->empty())
return 1;
}
return apply(args,_not,contextptr);
}
return !equaltosame(args);
}
static const char _not_s []="not";
static define_unary_function_eval2_index (64,__not,&_not,_not_s,&printasnot);
define_unary_function_ptr( at_not ,alias_at_not ,&__not);
// static symbolic symb_neg(const gen & args){ return symbolic(at_neg,args); }
gen _neg(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
return -args;
}
define_partial_derivative_onearg_genop( D_at_neg,"D_at_neg",_neg);
static const char _neg_s []="-";
#ifdef GIAC_HAS_STO_38
static define_unary_function_eval3_index (4,__neg,&_neg,(size_t)&D_at_negunary_function_ptr,_neg_s);
#else
static define_unary_function_eval3_index (4,__neg,&_neg,D_at_neg,_neg_s);
#endif
define_unary_function_ptr( at_neg ,alias_at_neg ,&__neg);
symbolic symb_inv(const gen & a){
return symbolic(at_inv,a);
}
gen _inv(const gen & args,GIAC_CONTEXT){
if ( args.type==_STRNG && args.subtype==-1) return args;
if ((args.type!=_VECT) || ckmatrix(args))
return inv(args,contextptr);
if (args.subtype==_SEQ__VECT){
iterateur it=args._VECTptr->begin(), itend=args._VECTptr->end();
gen prod(1);
for (;it!=itend;++it)
prod = prod * (*it);
return inv(prod,contextptr);
}
return apply(args,_inv,contextptr);
}
static const char _inv_s []="inv";
static define_unary_function_eval_index (12,__inv,&_inv,_inv_s);
define_unary_function_ptr5( at_inv ,alias_at_inv,&__inv,0,true);
symbolic symb_ln(const gen & e){
return symbolic(at_ln,e);
}
gen ln(const gen & e,GIAC_CONTEXT){
// if (abs_calc_mode(contextptr)==38 && do_lnabs(contextptr) && !complex_mode(contextptr) && (e.type<=_POLY || e.type==_FLOAT_) && !is_positive(e,contextptr)) return gensizeerr(contextptr);
if (!escape_real(contextptr) && !complex_mode(contextptr) && (e.type<=_POLY || e.type==_FLOAT_) && !is_positive(e,contextptr)) return gensizeerr(contextptr);
if (e.type==_FLOAT_){
#ifdef BCD
if (!is_positive(e,contextptr))
return fln(-e._FLOAT_val)+cst_i*cst_pi;
return fln(e._FLOAT_val);
#else
return ln(get_double(e._FLOAT_val),contextptr);
#endif
}
if (e.type==_DOUBLE_){
if (e._DOUBLE_val==0)
return minus_inf;
if (e._DOUBLE_val>0){
#ifdef _SOFTMATH_H
return std::giac_gnuwince_log(e._DOUBLE_val);
#else
return std::log(e._DOUBLE_val);
#endif
}
else {
if (!escape_real(contextptr) && !complex_mode(contextptr))
*logptr(contextptr) << "Taking ln of negative real " << e << '\n';
#ifdef _SOFTMATH_H
return M_PI*cst_i+std::giac_gnuwince_log(-e._DOUBLE_val);
#else
return M_PI*cst_i+std::log(-e._DOUBLE_val);
#endif
}
}
if (e.type==_SPOL1){
gen expo=e._SPOL1ptr->empty()?undef:e._SPOL1ptr->front().exponent;
if (is_zero(expo))
return series(*e._SPOL1ptr,*at_ln,0,contextptr);
}
if (e.type==_REAL){
if (is_positive(e,contextptr))
return e._REALptr->log();
else {
if (!escape_real(contextptr) && !complex_mode(contextptr))
*logptr(contextptr) << "Taking ln of negative real " << e << '\n';
return (-e)._REALptr->log()+cst_pi*cst_i;
}
}
if (e.type==_CPLX){
if (e.subtype){
#ifdef _SOFTMATH_H
return std::giac_gnuwince_log(gen2complex_d(e));
#else
return std::log(gen2complex_d(e));
#endif
}
if (e._CPLXptr->type==_REAL || e._CPLXptr->type==_FLOAT_){
//grad
int mode=get_mode_set_radian(contextptr);
gen res=ln(abs(e,contextptr),contextptr)+cst_i*arg(e,contextptr);
angle_mode(mode,contextptr);
return res;
}
if (is_zero(*e._CPLXptr,contextptr)){
if (is_one(*(e._CPLXptr+1)))
return cst_i*cst_pi_over_2;
if (is_minus_one(*(e._CPLXptr+1)))
return -cst_i*cst_pi_over_2;
}
}
if (is_squarematrix(e))
return analytic_apply(at_ln,*e._VECTptr,contextptr);
if (e.type==_VECT){
#ifdef NSPIRE
if (e.subtype==_SEQ__VECT && e._VECTptr->size()==2)
return _logb(e,contextptr);
#endif
return apply(e,ln,contextptr);
}
if (is_zero(e,contextptr))
return minus_inf; // calc_mode(contextptr)==1?unsigned_inf:minus_inf;
if (is_one(e))
return 0;
if (is_minus_one(e))
return cst_i*cst_pi;
if (is_integer(e) && is_strictly_positive(-e,contextptr))
return cst_i*cst_pi+ln(-e,contextptr);
if (is_undef(e))
return e;
if ( (e==unsigned_inf) || (e==plus_inf))
return e;
if (e==minus_inf)
return unsigned_inf;
if (is_equal(e))
return apply_to_equal(e,ln,contextptr);
if (e.type==_SYMB){
if (e._SYMBptr->sommet==at_inv && e._SYMBptr->feuille.type!=_VECT)
return -ln(e._SYMBptr->feuille,contextptr);
if (e._SYMBptr->sommet==at_exp){
if (is_real(e._SYMBptr->feuille,contextptr) )
return e._SYMBptr->feuille;
}
}
if (e.type==_FRAC && e._FRACptr->num==1)
return -ln(e._FRACptr->den,contextptr);
gen a,b;
if (is_algebraic_program(e,a,b))
return symbolic(at_program,gen(makevecteur(a,0,ln(b,contextptr)),_SEQ__VECT));
if (e.is_symb_of_sommet(at_pow) && e._SYMBptr->feuille.type==_VECT && e._SYMBptr->feuille._VECTptr->size()==2){
gen a=e._SYMBptr->feuille._VECTptr->front();
gen b=e._SYMBptr->feuille._VECTptr->back();
// ln(a^b)
if (is_positive(a,contextptr))
return b*ln(a,contextptr);
}
return symb_ln(e);
}
gen log(const gen & e,GIAC_CONTEXT){
return ln(e,contextptr);
}
static const char _ln_s []="ln"; // Using C notation, log works also for natural
static gen d_ln(const gen & args,GIAC_CONTEXT){
return inv(args,contextptr);
}
define_partial_derivative_onearg_genop( D_at_ln,"D_at_ln",&d_ln);
#ifdef GIAC_HAS_STO_38
static define_unary_function_eval3_index (18,__ln,&ln,(size_t)&D_at_lnunary_function_ptr,_ln_s);
#else
static define_unary_function_eval3_index (18,__ln,&ln,D_at_ln,_ln_s);
#endif
define_unary_function_ptr5( at_ln ,alias_at_ln,&__ln,0,true);
gen log10(const gen & e,GIAC_CONTEXT){
if (e.type==_FLOAT_) {
if (is_positive(e,contextptr)){
#ifdef BCD
return flog10(e._FLOAT_val);
#else
return log10(get_double(e._FLOAT_val),contextptr);
#endif
}
return ln(e,contextptr)/ln(10,contextptr);
}
if (e.type==_DOUBLE_ && e._DOUBLE_val>=0 ){
#ifdef _SOFTMATH_H
return std::giac_gnuwince_log10(e._DOUBLE_val);
#else
return std::log10(e._DOUBLE_val);
#endif
}
if ( e.type==_DOUBLE_ || (e.type==_CPLX && e.subtype)){
#ifdef _SOFTMATH_H
return std::giac_gnuwince_log(gen2complex_d(e))/std::log(10.0);
#else
return std::log(gen2complex_d(e))/std::log(10.0);
#endif
}
if (e.type==_CPLX && (e._CPLXptr->type==_REAL || e._CPLXptr->type==_FLOAT_)){
return (ln(abs(e,contextptr),contextptr)+cst_i*arg(e,contextptr))/ln(10,contextptr);
}
if (is_squarematrix(e))
return analytic_apply(at_log10,*e._VECTptr,contextptr);
if (e.type==_VECT){
#ifdef NSPIRE
if (e.subtype==_SEQ__VECT && e._VECTptr->size()==2)
return _logb(e,contextptr);
#endif
return apply(e,log10,contextptr);
}
gen a,b;
// if (abs_calc_mode(contextptr)==38 && has_evalf(e,a,1,contextptr)) return log10(a,contextptr);
if (is_algebraic_program(e,a,b))
return symbolic(at_program,gen(makevecteur(a,0,log10(b,contextptr)),_SEQ__VECT));
int n=0; gen e1(e),q;
if (is_integer(e1) && !is_zero(e1)){
while (is_zero(irem(e1,10,q))){
if (q.type==_ZINT)
e1=*q._ZINTptr;
else
e1=q;
++n;
}
}
return rdiv(ln(e1,contextptr),ln(10,contextptr),contextptr)+n;
}
static const char _log10_s []="log10"; // Using C notation, log for natural
static gen d_log10(const gen & args,GIAC_CONTEXT){
return inv(args*ln(10,contextptr),contextptr);
}
define_partial_derivative_onearg_genop(D_at_log10,"D_at_log10",&d_log10);
#ifdef GIAC_HAS_STO_38
static define_unary_function_eval3 (__log10,&log10,(size_t)&D_at_log10unary_function_ptr,_log10_s);
#else
static define_unary_function_eval3 (__log10,&log10,D_at_log10,_log10_s);
#endif
define_unary_function_ptr5( at_log10 ,alias_at_log10,&__log10,0,true);
gen alog10(const gen & e,GIAC_CONTEXT){
#ifdef BCD
if (e.type==_FLOAT_)
return falog10(e._FLOAT_val);
#endif
if (is_squarematrix(e))
return analytic_apply(at_alog10,*e._VECTptr,0);
if (e.type==_VECT)
return apply(e,contextptr,alog10);
if (is_equal(e))
return apply_to_equal(e,alog10,contextptr);
gen a,b;
if (is_algebraic_program(e,a,b))
return symbolic(at_program,gen(makevecteur(a,0,alog10(b,contextptr)),_SEQ__VECT));
return pow(gen(10),e,contextptr);
}
static const char _alog10_s []="alog10";
static define_unary_function_eval (__alog10,&alog10,_alog10_s);
define_unary_function_ptr5( at_alog10 ,alias_at_alog10,&__alog10,0,true);
symbolic symb_atan(const gen & e){
return symbolic(at_atan,e);
}
static gen atanasln(const gen & e,GIAC_CONTEXT){
return plus_one_half*cst_i*ln(rdiv(cst_i+e,cst_i-e,contextptr),contextptr);
}
gen atan(const gen & e0,GIAC_CONTEXT){
if (e0.type==_FLOAT_)
#ifdef BCD
return fatan(e0._FLOAT_val,angle_mode(contextptr));
#else
return atan(get_double(e0._FLOAT_val),contextptr);
#endif
gen e=frac_neg_out(e0,contextptr);
if (e.type==_DOUBLE_){
#ifdef _SOFTMATH_H
double res=std::giac_gnuwince_atan(e._DOUBLE_val);
#else
double res=std::atan(e._DOUBLE_val);
#endif
if (angle_radian(contextptr))
return res;
else if(angle_degree(contextptr))
return res*rad2deg_d;
else
return res*rad2grad_d;
}
if (e.type==_SPOL1){
gen expo=e._SPOL1ptr->empty()?undef:e._SPOL1ptr->front().exponent;
if (is_positive(expo,contextptr))
return series(*e._SPOL1ptr,*at_atan,0,contextptr);
}
if (e.type==_REAL){
if (angle_radian(contextptr))
return e._REALptr->atan();
else if(angle_degree(contextptr))
return 180*e._REALptr->atan()/cst_pi;
//grad
else
return 200*e._REALptr->atan()/cst_pi;
}
if ( (e.type==_CPLX) && (e.subtype || e._CPLXptr->type==_REAL || e._CPLXptr->type==_FLOAT_)){
if (angle_radian(contextptr))
return no_context_evalf(atanasln(e,contextptr));
else if(angle_degree(contextptr))
return no_context_evalf(atanasln(e,contextptr))*gen(rad2deg_d);
//grad
else
return no_context_evalf(atanasln(e, contextptr))*gen(rad2grad_d);
}
if (is_squarematrix(e))
return analytic_apply(at_atan,*e._VECTptr,contextptr);
if (e.type==_VECT)
return apply(e,atan,contextptr);
if (is_zero(e,contextptr))
return e;
if (is_one(e)){
if (angle_radian(contextptr))
return rdiv(cst_pi,4,contextptr);
else if(angle_degree(contextptr))
return 45;
//grad
else
return 50;
}
if (is_minus_one(e)){
if (angle_radian(contextptr))
return rdiv(-cst_pi,4,contextptr);
else if(angle_degree(contextptr))
return -45;
//grad
else
return -50;
}
if (e==plus_sqrt3_3){
if (angle_radian(contextptr))
return rdiv(cst_pi,6,contextptr);
else if(angle_degree(contextptr))
return 30;
//grad
else
return rdiv(100,3); //100/3 grads
}
if (e==plus_sqrt3){
if (angle_radian(contextptr))
return rdiv(cst_pi,3,contextptr);
else if(angle_degree(contextptr))
return 60;
//grad
else
return rdiv(200,3); //200/3 grads
}
if (e==plus_inf){
if (angle_radian(contextptr))
return cst_pi_over_2;
else if(angle_degree(contextptr))
return 90;
//grad
else
return 100;
}
if (e==minus_inf){
if (angle_radian(contextptr))
return -cst_pi_over_2;
else if(angle_degree(contextptr))
return -90;
//grad
else
return -100;
}
if (is_undef(e))
return e;
if (e==unsigned_inf)
return undef;
gen a,b;
if (is_algebraic_program(e,a,b))
return symbolic(at_program,gen(makevecteur(a,0,atan(b,contextptr)),_SEQ__VECT));
gen tmp=evalf_double(e,0,contextptr);
if (tmp.type==_DOUBLE_){
double ed=tmp._DOUBLE_val;
// detect if atan is a multiples of pi/10
gen edh=horner(makevecteur(-5,60,-126,60,-5),tmp*tmp);
if (absdouble(edh._DOUBLE_val)<1e-7 &&
normal(horner(makevecteur(-5,60,-126,60,-5),e*e),contextptr)==0){
int res=int(std::floor(std::atan(absdouble(ed))*10/M_PI+.5));
if (res%2)
return (ed>0?res:-res)*(angle_radian(contextptr)?cst_pi/10:(angle_degree(contextptr)?gen(18):gen(20))); //grad
else
return (ed>0?res/2:-res/2)*(angle_radian(contextptr)?cst_pi/5:(angle_degree(contextptr)?gen(36):gen(40))); //grad
}
edh=horner(makevecteur(-3,55,-198,198,-55,3),tmp*tmp);
if (absdouble(edh._DOUBLE_val)<1e-7){
int res=int(std::floor(std::atan(absdouble(ed))*12/M_PI+.5));
int den=12;
int g=gcd(res,den);
res /=g; den /=g;
return (ed>0?res:-res)*(angle_radian(contextptr)?cst_pi/den:(angle_degree(contextptr)?gen(15*g):rdiv(50,3)*gen(g))); //grad 50/3*g grads
}
edh=horner(makevecteur(1,-6,1),ed*ed);
if (absdouble(edh._DOUBLE_val)<1e-7 &&
normal(horner(makevecteur(1,-6,1),e*e),contextptr)==0){
int res=int(std::floor(std::atan(absdouble(ed))*8/M_PI+.5));
return (ed>0?res:-res)*(angle_radian(contextptr)?cst_pi/8:(angle_degree(contextptr)?gen(45)/2:gen(25))); //grad
}
}
if ((e.type==_SYMB) && (e._SYMBptr->sommet==at_neg))
return -atan(e._SYMBptr->feuille,contextptr);
if ( (e.type==_INT_) && (e.val<0) )
return -atan(-e,contextptr);
if (is_equal(e))
return apply_to_equal(e,atan,contextptr);
vecteur v1(loptab(e,sincostan_tab));
if ((series_flags(contextptr)&8)==0 && v1.size()>1){
gen e1=ratnormal(_trigtan(e,contextptr),contextptr);
if (loptab(e1,sincostan_tab).size()<=1)
return atan(e1,contextptr);
}
// if (e.is_symb_of_sommet(at_inv)) return sign(e._SYMBptr->feuille,contextptr)*cst_pi/2-atan(e._SYMBptr->feuille,contextptr);
if (e.is_symb_of_sommet(at_tan)){
if (atan_tan_no_floor(contextptr))
return e._SYMBptr->feuille;
gen tmp=cst_pi;
if(!angle_radian(contextptr))
{
if(angle_degree(contextptr))
tmp=180;
//grad
else
tmp = 200;
}
gen tmp2=evalf_double(e._SYMBptr->feuille,0,contextptr);
if (tmp2.type<_IDNT)
tmp2=_floor(tmp2/tmp+plus_one_half,contextptr);
else
tmp2=_floor(e._SYMBptr->feuille/tmp+plus_one_half,contextptr);
if (tmp2.type==_FLOAT_)
tmp2=get_int(tmp2._FLOAT_val);
return operator_minus(e._SYMBptr->feuille,tmp2*tmp,contextptr);
}
vecteur ve=lvar(e);
if (ve.size()==1){
// atan((1+t)/(1-t))=atan((tan(pi/4)+t)/(1-tan(pi/4+t)))=atan(tan(pi/4+atan(t)))
gen t=ve.front();
gen test=(1+t)/(1-t);
test=ratnormal(e/test,contextptr);
if (is_one(test))
return atan(symbolic(at_tan,cst_pi/4+atan(t,contextptr)),contextptr);
if (is_minus_one(test))
return -atan(symbolic(at_tan,cst_pi/4+atan(t,contextptr)),contextptr);
test=(-1+t)/(1+t);
test=ratnormal(e/test,contextptr);
if (is_one(test))
return atan(symbolic(at_tan,-cst_pi/4+atan(t,contextptr)),contextptr);
if (is_minus_one(test))
return -atan(symbolic(at_tan,-cst_pi/4+atan(t,contextptr)),contextptr);
}
return symb_atan(e);
}
static gen d_atan(const gen & args,GIAC_CONTEXT){
gen g=inv(1+pow(args,2),contextptr);
if (angle_radian(contextptr))
return g;
else if(angle_degree(contextptr))
return g*rad2deg_e;
//grad
else
return g*rad2grad_e;
}
define_partial_derivative_onearg_genop( D_at_atan," D_at_atan",&d_atan);
static gen taylor_atan (const gen & lim_point,const int ordre,const unary_function_ptr & f, int direction,gen & shift_coeff,GIAC_CONTEXT){
if (ordre<0)
return 0; // no symbolic preprocessing
shift_coeff=0;
if (!is_inf(lim_point))
return taylor(lim_point,ordre,f,0,shift_coeff,contextptr);
vecteur v;
identificateur x(" ");
taylor(atan(x,contextptr),x,0,ordre,v,contextptr);
v=negvecteur(v);
v.front()=atan(lim_point,contextptr);
return v;
}
static const char _atan_s []="atan";
#ifdef GIAC_HAS_STO_38
static define_unary_function_eval_taylor_index (42,__atan,&atan,(size_t)&D_at_atanunary_function_ptr,&taylor_atan,_atan_s);
#else
static define_unary_function_eval_taylor_index (42,__atan,&atan,D_at_atan,&taylor_atan,_atan_s);
#endif
define_unary_function_ptr5( at_atan ,alias_at_atan,&__atan,0,true);
symbolic symb_exp(const gen & e){
return symbolic(at_exp,e);
}
static gen numeric_matrix_exp(const gen & e,double eps,GIAC_CONTEXT){
gen res=midn(int(e._VECTptr->size()));
gen eee(e);
for (double j=2;j<max_numexp && linfnorm(eee,contextptr)._DOUBLE_val>eps;++j){
res = res + eee;
eee = gen(1/j) * eee * e ;
}
return res;
}
gen exp(const gen & e0,GIAC_CONTEXT){
if (e0.type==_FLOAT_){
#ifdef BCD
return fexp(e0._FLOAT_val);
#else
return exp(get_double(e0._FLOAT_val),contextptr);
#endif
}
if (is_integer(e0) && is_strictly_greater(0,e0,contextptr))
return symb_inv(symb_exp(-e0));
gen e=frac_neg_out(e0,contextptr);
if (e.type==_SPOL1){
gen expo=e._SPOL1ptr->empty()?undef:e._SPOL1ptr->front().exponent;
if (is_positive(expo,contextptr))
return series(*e._SPOL1ptr,*at_exp,0,contextptr);
}
if (e.type==_DOUBLE_){
#ifdef _SOFTMATH_H
return std::giac_gnuwince_exp(e._DOUBLE_val);
#else
return std::exp(e._DOUBLE_val);
#endif
}
if (e.type==_REAL)
return e._REALptr->exp();
if (e.type==_CPLX){
if (e.subtype){
#ifdef _SOFTMATH_H
return std::giac_gnuwince_exp(gen2complex_d(e));
#else
return std::exp(gen2complex_d(e));
#endif
}
if (e._CPLXptr->type==_REAL || e._CPLXptr->type==_FLOAT_){
//grad
int mode=get_mode_set_radian(contextptr);
gen res=exp(*e._CPLXptr,contextptr)*(cos(*(e._CPLXptr+1),contextptr)+cst_i*sin(*(e._CPLXptr+1),contextptr));
angle_mode(mode,contextptr);
return res;
}
}
if (e.type==_VECT){
if (is_squarematrix(e)){
// check for numeric entries -> numeric exp
if (is_fully_numeric(e))
return numeric_matrix_exp(e,epsilon(contextptr),contextptr);
return analytic_apply(at_exp,*e._VECTptr,contextptr);
}
return apply(e,contextptr,exp);
}
if (is_zero(e,contextptr))
return 1;
if (is_undef(e) || e==plus_inf)
return e;
if (e==unsigned_inf)
return undef;
if (e==minus_inf)
return 0;
if (e.type==_SYMB && e._SYMBptr->sommet==at_ln)
return e._SYMBptr->feuille;
if (e.type==_SYMB && e._SYMBptr->sommet==at_neg && e._SYMBptr->feuille.type==_SYMB && e._SYMBptr->feuille._SYMBptr->sommet==at_ln)
return inv(e._SYMBptr->feuille._SYMBptr->feuille,contextptr);
gen a,b;
if (is_algebraic_program(e,a,b))
return symbolic(at_program,gen(makevecteur(a,0,exp(b,contextptr)),_SEQ__VECT));
int k;
if (simplify_sincosexp_pi && contains(e,cst_pi)){ // if (!approx_mode(contextptr))
gen a,b;
if (is_linear_wrt(e,cst_pi,a,b,contextptr) && !is_zero(a)){
if (is_multiple_of_12(a*cst_i*gen(trig_deno/2),k))
return (*table_cos[k]+cst_i*(*table_cos[(k+6)%24]))*exp(b,contextptr);
else {
gen kk;
kk=normal(a*cst_i,contextptr);
if (is_assumed_integer(kk,contextptr)){
if (is_assumed_integer(normal(rdiv(kk,plus_two,contextptr),contextptr),contextptr))
return exp(b,contextptr);
else
return pow(minus_one,kk,contextptr)*exp(b,contextptr);
}
int n,d,q,r;
if (is_rational(kk,n,d)){
if (b==0 && (d==5 || d==10) && calc_mode(contextptr)!=1)
return cos(kk*cst_pi,contextptr)-cst_i*sin(kk*cst_pi,contextptr);
if (d<7){
q=-n/d;
r=-n%d;
if (q%2)
q=-1;
else
q=1;
if (d<0){ r=-r; d=-d; }
if (r<0) r += 2*d;
if (abs_calc_mode(contextptr)==38 || calc_mode(contextptr)==1)
return q*symb_exp(r*(cst_pi*cst_i/d));
// exp(r*i*pi/d) -> use rootof([1,..,0],cyclotomic(2*d))
vecteur vr(r+1);
vr[0]=1;
vecteur vc(cyclotomic(2*d));
if (!is_undef(vc))
return q*symb_rootof(vr,vc,contextptr)*exp(b,contextptr);
// initially it was return q*symb_exp(r*(cst_pi*cst_i/d));
}
}
} // end else multiple of pi/12
} // end is_linear_wrt
} // end if contains(e,_IDNT_pi)
if (is_equal(e))
return apply_to_equal(e,exp,contextptr);
return symb_exp(e);
}
define_partial_derivative_onearg_genop( D_at_exp,"D_at_exp",exp);
static gen taylor_exp (const gen & lim_point,const int ordre,const unary_function_ptr & f,int direction,gen & shift_coeff,GIAC_CONTEXT){
if (ordre<0)
return 0; // no symbolic preprocessing
shift_coeff=0;
gen image=f(lim_point,contextptr); // should simplify if contains i*pi
vecteur v(1,image);
if (is_undef(image))
return v;
gen factorielle(1);
for (int i=1;i<=ordre;++i,factorielle=factorielle*gen(i))
v.push_back(rdiv(image,factorielle,contextptr));
v.push_back(undef);
return v;
}
static const char _exp_s []="exp";
string printasexp(const gen & g,const char * s,GIAC_CONTEXT){
if (
calc_mode(contextptr)==1 || abs_calc_mode(contextptr)==38
// xcas_mode(contextptr)==0
){
if (is_one(g))
return calc_mode(contextptr)==1?"ℯ":"e";
if (g.type>_REAL && g.type!=_IDNT)
return (calc_mode(contextptr)==1?"ℯ^(":"e^(")+g.print(contextptr)+")";
return (calc_mode(contextptr)==1?"ℯ^":"e^")+g.print(contextptr);
}
else
return "exp("+g.print(contextptr)+")";
}
static string texprintasexp(const gen & g,const char * s,GIAC_CONTEXT){
return "e^{"+gen2tex(g,contextptr)+"}";
}
#ifdef GIAC_HAS_STO_38
static define_unary_function_eval_taylor2_index(16,__exp,&exp,(size_t)&D_at_expunary_function_ptr,&taylor_exp,_exp_s,0,&texprintasexp);
#else
static define_unary_function_eval_taylor2_index(16,__exp,&exp,D_at_exp,&taylor_exp,_exp_s,0,&texprintasexp);
#endif
define_unary_function_ptr5( at_exp ,alias_at_exp,&__exp,0,true);
// static symbolic symb_sqrt(const gen & e){ return symbolic(at_sqrt,e); }
void zint2simpldoublpos(const gen & e,gen & simpl,gen & doubl,bool & pos,int d,GIAC_CONTEXT){
simpl=1;
doubl=1;
if (!is_integer(e)){
pos=true;
simpl=e;
return;
}
if (is_zero(e)){
simpl=e;
return;
}
gen e_copy;
pos=ck_is_positive(e,context0); // ok
if (!pos)
e_copy=-e;
else
e_copy=e;
vecteur u;
#if defined USE_GMP_REPLACEMENTS || defined BF2GMP_H
bool trial=true;
#else
bool trial=false;
if (e_copy.type==_ZINT && mpz_sizeinbase(*e_copy._ZINTptr,2)>128){
// detect perfect square
if (mpz_perfect_power_p(*e_copy._ZINTptr)){
int nbits=mpz_sizeinbase(*e_copy._ZINTptr,2);
gen h=accurate_evalf(e_copy,nbits);