-
Notifications
You must be signed in to change notification settings - Fork 2
/
series.cc
3692 lines (3589 loc) · 120 KB
/
series.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.. -g -c series.cc -DIN_GIAC -DHAVE_CONFIG_H " -*-
#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 "derive.h"
#include "subst.h"
#include "series.h"
#include "symbolic.h"
#include "unary.h"
#include "usual.h"
#include "poly.h"
#include "sym2poly.h"
#include "tex.h"
#include "prog.h"
#include "misc.h"
#include "intg.h"
#include "maple.h"
#include "lin.h"
#include "plot.h"
#include "giacintl.h"
#ifndef NO_NAMESPACE_GIAC
namespace giac {
#endif // ndef NO_NAMESPACE_GIAC
static int mrv_begin_order=2;
static bool taylor_(const gen & f_x,const gen & x,const gen & lim_point,int ordre,vecteur & v,GIAC_CONTEXT){
gen current_derf(f_x),value,factorielle(1);
for (int i=0;;++i){
value=subst(current_derf,x,lim_point,false,contextptr);
if (is_undef(value)){
// if (x.type==_IDNT) value=limit(current_derf,*x._IDNTptr,lim_point,0,contextptr);
if (is_undef(value))
return false;
}
v.push_back(ratnormal(rdiv(value,factorielle,contextptr),contextptr));
if (i==ordre){
v.push_back(undef);
return true;
}
factorielle = factorielle * gen(i+1);
current_derf=ratnormal(derive(current_derf,x,contextptr),contextptr);
if (is_undef(current_derf))
return false;
}
v.dbgprint();
return false;
}
bool taylor(const gen & f_x,const gen & x,const gen & lim_point,int ordre,vecteur & v,GIAC_CONTEXT){
int i=series_flags(contextptr);
series_flags(contextptr)=series_flags(contextptr) | (1<<7) ;
bool b=taylor_(f_x,x,lim_point,ordre,v,contextptr);
series_flags(i,contextptr);
return b;
}
// direction is always ignored for taylor, but might not
// for generic series_expansion
// shift coeff =0 for taylor
gen taylor(const gen & lim_point,int ordre,const unary_function_ptr & f,int direction,gen & shift_coeff,GIAC_CONTEXT){
// Special handling for sin/cos expansion inside limit
if ( is_inf(lim_point) && ( (f==at_cos) || (f==at_sin) ) ){
gen g=bounded_function(contextptr);
/*
int i=sincosinf.size();
sincosinf.push_back(gen(" sincosinf"+print_INT_(i)));
gen g=sincosinf.back();
if (!g._IDNTptr->value){
vecteur minusone_one(2);
minusone_one[0]=minus_one;
minusone_one[1]=plus_one;
gen v(vecteur(1,gen(minusone_one,_LINE__VECT)));
gen d(_DOUBLE_);
d.subtype=_INT_TYPE;
gen aa(makevecteur(d,v,vecteur(0)),_ASSUME__VECT);
g._IDNTptr->value=new gen(aa);
}
*/
return vecteur(1,g);
}
// if preprocessing is needed for f, series_expansion for ordre==-1 should
// push back in a global vector f and it's substitution
if (ordre<0)
return 0;
shift_coeff=0;
if (is_undef(lim_point) || is_inf(lim_point)){
invalidserieserr(gettext("non tractable function ")+(f.ptr()->print(contextptr)+(" at "+lim_point.print(contextptr))));
return undef;
}
identificateur x(" ");
vecteur v;
gen fx=f(x,contextptr);
if (taylor(fx,x,lim_point,ordre,v,contextptr))
return v;
else
return undef;
}
gen porder(const sparse_poly1 & a){
if (a.empty())
return plus_inf;
sparse_poly1::const_iterator a_end=a.end()-1;
if (is_undef(a_end->coeff))
return a_end->exponent;
else
return plus_inf;
}
bool sparse_poly12vecteur(const sparse_poly1 & p,vecteur & v,int & shift){
sparse_poly1::const_iterator it=p.begin(),itend=p.end();
v.clear();
if (p.empty())
return true;
if (p.back().exponent.type!=_INT_)
return false;
int n1=p.front().exponent.val,n2=p.back().exponent.val;
if (n1>n2 || (n2-n1)+1<0) // if n==RAND_MAX, n+1<0
return false;
if (n1<0)
shift=n1;
else
shift=n1=0;
v.resize(n2-n1+1);
for (;it!=itend;++it){
if (it->exponent.type!=_INT_)
return false;
int m=it->exponent.val;
if (m<n1 || m>n2)
return false;
v[m-n1]=it->coeff;
}
reverse(v.begin(),v.end());
return true;
}
void vecteur2sparse_poly1(const vecteur & v,sparse_poly1 & p){
p.clear();
vecteur::const_iterator it=v.begin(),itend=v.end();
p.reserve(itend-it);
for (int i=0;it!=itend;++i,++it){
if (!is_zero(*it))
p.push_back(monome(*it,i));
}
}
sparse_poly1 gen2spol1(const gen &g){
if (g.type!=_VECT)
return sparse_poly1(1,monome(g,0));
sparse_poly1 p;
vecteur2sparse_poly1(*g._VECTptr,p);
return p;
}
sparse_poly1 vecteur2sparse_poly1(const vecteur & v){
sparse_poly1 p;
vecteur2sparse_poly1(v,p);
return p;
}
gen spol12gen(const sparse_poly1 & p,GIAC_CONTEXT){
string t;
t = t+series_variable_name(contextptr);
identificateur tt(t);
gen T(tt),remains;
gen g=sparse_poly12gen(p,T,remains,false);
if (!is_zero(remains))
g += remains*order_size(T,contextptr);
return g;
}
static gen spol12gen(const gen & coeff,const gen & x){
if (coeff.type==_VECT){
vecteur v=*coeff._VECTptr;
int s=int(v.size());
for (int i=0;i<s;++i){
v[i]=spol12gen(v[i],x);
}
return gen(v,coeff.subtype);
}
if (coeff.type==_SPOL1){
gen remains=0;
return sparse_poly12gen(*coeff._SPOL1ptr,x,remains,true)+remains;
}
if (coeff.type!=_SYMB)
return coeff;
return symbolic(coeff._SYMBptr->sommet,spol12gen(coeff._SYMBptr->feuille,x));
}
gen sparse_poly12gen(const sparse_poly1 & p,const gen & x,gen & remains,bool with_order_size){
gen res;
remains=0;
sparse_poly1::const_iterator it=p.begin(),itend=p.end();
for (;it!=itend;++it){
gen coeff=it->coeff;
if (is_undef(coeff)){
remains=pow(x,it->exponent,context0); // ok
if (with_order_size)
return res+remains*order_size(x,context0);
else
return res;
}
coeff=spol12gen(coeff,x);
res = res + coeff * pow(x,it->exponent,context0); // ok
}
return res;
}
bool ptruncate(sparse_poly1 & p,const gen & ordre,GIAC_CONTEXT){
if ( (series_flags(contextptr) & 0x2) || p.empty() ){
sparse_poly1::iterator it=p.begin(),itend=p.end();
gen first=it->exponent;
for (;it!=itend;++it){
if (is_undef(it->coeff))
return true;
if (ck_is_strictly_greater(it->exponent-first,ordre,contextptr)){
it->coeff=undef;
p.erase(it+1,itend);
return true;
}
}
}
return true;
}
void poly_truncate(sparse_poly1 & p,int ordre,GIAC_CONTEXT){
if ( (series_flags(contextptr) & 0x2) || p.empty() ){
sparse_poly1::iterator it=p.begin(),itend=p.end();
for (;it!=itend;++it){
if (is_undef(it->coeff))
return ;
if (ck_is_strictly_greater(it->exponent,ordre,contextptr)){
it->coeff=undef;
p.erase(it+1,itend);
return ;
}
}
}
return ;
}
static gen remove_lnexp(const gen & e,GIAC_CONTEXT);
bool padd(const sparse_poly1 & a,const sparse_poly1 &b, sparse_poly1 & res,GIAC_CONTEXT){
// Series addition
if (a.empty()){
if (&b!=&res)
res=b;
return true;
}
if (b.empty()){
if (&a!=&res)
res=a;
return true;
}
sparse_poly1::const_iterator a_cur,a_end,b_cur,b_end;
sparse_poly1 temp_a,temp_b;
if (&res==&a){ // must make a copy of a
temp_a=a;
a_cur=temp_a.begin();
a_end=temp_a.end();
}
else {
a_cur=a.begin();
a_end=a.end();
}
if (&res==&b){ // must make a copy of b
temp_b=b;
b_cur=temp_b.begin();
b_end=temp_b.end();
}
else {
b_cur=b.begin();
b_end=b.end();
}
res.clear();
res.reserve((a_end-a_cur)+(b_end-b_cur));
for (;(a_cur!=a_end) && (b_cur!=b_end) ;) {
gen a_pow=a_cur->exponent;
gen b_pow=b_cur->exponent;
// a and b are non-empty, compare powers
if (ck_is_strictly_greater(b_pow,a_pow,contextptr)) {
// get coefficient from a
res.push_back(*a_cur);
if (is_undef(a_cur->coeff)){
return true;
}
++a_cur;
continue;
}
if (ck_is_strictly_greater(a_pow,b_pow,contextptr)) {
// get coefficient from b
res.push_back(*b_cur);
if (is_undef(b_cur->coeff)){
return true;
}
++b_cur;
continue;
}
// Add coefficient of a and b
gen sum=a_cur->coeff+b_cur->coeff;
if (sum.type>_POLY && sum.type!=_FRAC &&(res.empty() || (series_flags(contextptr) & 0x1) ) ){
//cerr << sum << " ";
sum=recursive_normal(remove_lnexp(sum,contextptr),contextptr);
//cerr << sum << '\n';
}
// gen sum=(a_cur->coeff+b_cur->coeff);
if (!is_zero(sum))
res.push_back(monome(sum,a_pow));
if (is_undef(sum)){
return true;
}
++a_cur;
++b_cur;
}
for (;a_cur!=a_end;++a_cur)
res.push_back(*a_cur);
for (;b_cur!=b_end;++b_cur)
res.push_back(*b_cur);
return true;
}
sparse_poly1 spadd(const sparse_poly1 & a,const sparse_poly1 &b,GIAC_CONTEXT){
sparse_poly1 res;
padd(a,b,res,contextptr);
return res;
}
sparse_poly1 spsub(const sparse_poly1 & a,const sparse_poly1 &b,GIAC_CONTEXT){
sparse_poly1 res(b);
pneg(b,res,contextptr);
padd(a,res,res,contextptr);
return res;
}
bool pmul(const sparse_poly1 & a,const gen & b_orig, sparse_poly1 & res,GIAC_CONTEXT){
gen b(b_orig);
if (&a==&res){
if (is_one(b_orig)) return true;
sparse_poly1::iterator it=res.begin(),itend=res.end();
for (;it!=itend;++it){
gen g=it->coeff * b;
if (g.type>_POLY && g.type!=_FRAC)
g=ratnormal(g,contextptr) ;
it->coeff = g;
}
return true;
}
sparse_poly1::const_iterator it=a.begin(),itend=a.end();
res.clear();
res.reserve(itend-it);
for (;it!=itend;++it)
res.push_back(monome(ratnormal(it->coeff * b,contextptr), it->exponent));
return true;
}
bool pmul(const gen & b, const sparse_poly1 & a,sparse_poly1 & res,GIAC_CONTEXT){
return pmul(a,b,res,contextptr);
}
sparse_poly1 spmul(const sparse_poly1 & a,const gen &b,GIAC_CONTEXT){
sparse_poly1 res;
if (!pmul(a,b,res,contextptr))
res=sparse_poly1(1,monome(1,undef));
return res;
}
sparse_poly1 spmul(const gen & a,const sparse_poly1 &b,GIAC_CONTEXT){
sparse_poly1 res;
if (!pmul(a,b,res,contextptr))
res=sparse_poly1(1,monome(1,undef));
return res;
}
struct monome_less {
monome_less() {}
bool operator () (const monome & a,const monome & b){
return ck_is_strictly_greater(b.exponent,a.exponent,context0);
}
};
struct symb_size_less_t {
symb_size_less_t() {}
bool operator () (const gen &a,const gen &b){
return symb_size_less(a,b);
}
};
bool pmul(const sparse_poly1 & celuici,const sparse_poly1 &other, sparse_poly1 & final_seq,bool n_truncate,const gen & n_valuation,GIAC_CONTEXT){
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
int asize=int(celuici.size());
int bsize=int(other.size());
if ( (!asize) || (!bsize) ) {
final_seq.clear();
return true;
}
if (asize==1){
gen temp(celuici.front().coeff);
pshift(other,celuici.front().exponent,final_seq,contextptr);
// COUT << other << "Shifted" << final_seq << '\n';
return pmul(final_seq,temp,final_seq,contextptr);
// COUT << other << "Multiplied" << final_seq << '\n';
}
if (bsize==1){
gen temp(other.front().coeff);
pshift(celuici,other.front().exponent,final_seq,contextptr);
return pmul(final_seq,temp,final_seq,contextptr);
}
sparse_poly1 new_seq;
new_seq.reserve(asize*bsize);
// General sparse series multiplication: complexity is N*M*ln(N*M)
// Storage capacity 2*N*M expair
// That's much more than O(N+M) for dense poly *but*
// it works for non integer powers
// COUT << celuici << "pmul" << other << '\n';
// First find the order product
gen a_max = porder(celuici);
gen b_max = porder(other);
gen a_min = celuici.front().exponent;
gen b_min = other.front().exponent;
gen c_min = normal(a_min + b_min,contextptr);
gen c_max = min(normal(a_min + b_max,contextptr),normal(b_min + a_max,contextptr),contextptr);
if (c_max.type==_SYMB && c_max._SYMBptr->sommet==at_max)
return false; // setsizeerr(gettext("series.cc/pmul"));
// compute all products term by term, with optimization for dense poly
// (coeff are sorted for dense poly)
sparse_poly1::const_iterator itb = other.begin(),itbend = other.end();
sparse_poly1::const_iterator ita = celuici.begin(),ita_end=celuici.end();
sparse_poly1::const_iterator itabegin = ita;
gen old_pow=normal(ita->exponent+itb->exponent,contextptr);
gen res(0);
for ( ; ita!=ita_end; ++ita ){
sparse_poly1::const_iterator itacur=ita;
sparse_poly1::const_iterator itbcur=itb;
for (;;) {
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
gen cur_pow=normal(itacur->exponent+itbcur->exponent,contextptr);
if ((n_truncate && ck_is_strictly_greater(n_valuation,cur_pow,contextptr)) || ck_is_greater(c_max,cur_pow,contextptr)){
if (cur_pow!=old_pow){
new_seq.push_back( monome(res,old_pow ));
res=itacur->coeff * itbcur->coeff;
old_pow=cur_pow;
}
else
res=res+ itacur->coeff * itbcur->coeff;
}
if (itacur==itabegin)
break;
--itacur;
++itbcur;
if (itbcur==itbend)
break;
}
}
--ita;
++itb;
for ( ; itb!=itbend;++itb){
sparse_poly1::const_iterator itacur=ita;
sparse_poly1::const_iterator itbcur=itb;
for (;;) {
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
gen cur_pow=normal(itacur->exponent + itbcur->exponent,contextptr);
if ((n_truncate && ck_is_strictly_greater(n_valuation,cur_pow,contextptr)) || ck_is_greater(c_max,cur_pow,contextptr)){
if (cur_pow!=old_pow){
new_seq.push_back( monome(res ,old_pow ));
res= itacur->coeff * itbcur->coeff ;
old_pow=cur_pow;
}
else
res=res+ itacur->coeff * itbcur->coeff ;
}
if (itacur==itabegin)
break;
--itacur;
++itbcur;
if (itbcur==itbend)
break;
}
}
new_seq.push_back( monome(res ,old_pow ));
final_seq.clear();
sparse_poly1::const_iterator it=new_seq.begin();
sparse_poly1::const_iterator itend=new_seq.end();
const int MAXS=512;
int m=MAXS,M=-MAXS,N=itend-it;
final_seq.reserve(N);
#ifdef FXCG
if (N<MAXS){
for (;it!=itend;++it){
if (it->exponent.type!=_INT_)
break;
int cur=it->exponent.val;
if (cur<m)
m=cur;
if (cur>M)
M=cur;
}
if (it==itend){
gen tab[M-m+1];
memset(tab,sizeof(tab),0);
for (it=new_seq.begin();it!=itend;++it){
tab[it->exponent.val-m] += it->coeff;
}
for (int i=m;i<=M;++i){
gen res=tab[i-m];
if (is_zero(res))
continue;
if (is_undef(res)){
final_seq.push_back(monome(res,i));
return true;
}
if (series_flags(contextptr) & 0x1)
res=recursive_normal(res,contextptr);
if (!is_zero(res))
final_seq.push_back(monome(res,i));
}
return true;
}
}
#endif
// COUT << new_seq << '\n';
// sort by asc. power
sort( new_seq.begin(),new_seq.end(),monome_less());
// COUT << "Sorted" << new_seq << '\n';
// add terms with same power
it=new_seq.begin();
// itend=new_seq.end();
while (it!=itend){
gen res=it->coeff;
gen pow=it->exponent;
if (is_undef(res)){
final_seq.push_back(*it);
return true;
}
++it;
while ( (it!=itend) && (it->exponent==pow)){
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
if (is_undef(it->coeff)){
final_seq.push_back(*it);
return true;
}
res=res+it->coeff;
++it;
}
if (series_flags(contextptr) & 0x1)
res=recursive_normal(res,contextptr);
if (!is_zero(res))
final_seq.push_back(monome(res, pow));
}
if (c_max!=plus_inf)
final_seq.push_back(monome(undef, c_max));
return true;
//COUT << final_seq.back().coeff << '\n';
//return true;
}
sparse_poly1 spmul(const sparse_poly1 & a,const sparse_poly1 &b,GIAC_CONTEXT){
sparse_poly1 res;
if (!pmul(a,b,res,false,0,contextptr))
res=sparse_poly1(1,monome(1,undef));
return res;
}
bool pneg(const sparse_poly1 & a,sparse_poly1 & res,GIAC_CONTEXT){
if (&a==&res){
sparse_poly1::iterator it=res.begin(),itend=res.end();
for (;it!=itend;++it)
it->coeff=-it->coeff;
return true;
}
sparse_poly1::const_iterator it=a.begin(),itend=a.end();
res.clear();
res.reserve(itend-it);
for (;it!=itend;++it)
res.push_back(monome(-it->coeff, it->exponent));
return true;
}
sparse_poly1 spneg(const sparse_poly1 & a,GIAC_CONTEXT){
sparse_poly1 res;
pneg(a,res,contextptr);
return res;
}
bool pshift(const sparse_poly1 & a,const gen & b_orig, sparse_poly1 & res,GIAC_CONTEXT){
if (is_zero(b_orig)){
if (&a!=&res)
res=a;
return true;
}
gen b(b_orig);
if (&a==&res){
sparse_poly1::iterator it=res.begin(),itend=res.end();
for (;it!=itend;++it)
it->exponent = normal(it->exponent + b,contextptr);
return true;
}
sparse_poly1::const_iterator it=a.begin(),itend=a.end();
res.clear();
res.reserve(itend-it);
for (;it!=itend;++it)
res.push_back(monome(it->coeff , normal(it->exponent +b,contextptr)));
return true;
}
// ascending order division
bool pdiv(const sparse_poly1 & a,const sparse_poly1 &b_orig, sparse_poly1 & res,int ordre_orig,GIAC_CONTEXT){
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
//if (debug_infolevel) CERR << CLOCK()*1e-6 << " pdiv begin" <<'\n';
sparse_poly1 b(b_orig);
ptruncate(b,ordre_orig,contextptr);
if (b.empty()){
// divisionby0err(a);
return false;
}
gen b0=b.front().coeff;
if (is_undef(b0)){
if (&b!=&res)
res=b;
return true;
}
if (b.size()==1){
pshift(a,-b.front().exponent,res,contextptr);
return pdiv(res,b0,res,contextptr);
}
// COUT << a << "/" << b << '\n';
if (&res==&b){
// setsizeerr(gettext("series.cc/pdiv"));
return false;
}
gen e0=b.front().exponent;
gen ordre=min(min(porder(a),porder(b)-e0,contextptr),ordre_orig,contextptr);
if (ordre==plus_inf)
ordre=series_default_order(contextptr);
// COUT << ordre << '\n';
if (ordre.type==_SYMB && ordre._SYMBptr->sommet==at_max)
return false; // setsizeerr(gettext("series.cc/pdiv"));
sparse_poly1 rem(a);
res.clear();
sparse_poly1 bshift;
gen q_cur,e_cur; // current quotient, current exponent
for (;;){
if (is_undef(rem.front().coeff)){
res.push_back(monome(undef,rem.front().exponent-e0));
// COUT << "=" << res << '\n';
return true;
}
q_cur=rdiv(rem.front().coeff,b0,contextptr);
e_cur=rem.front().exponent-e0;
res.push_back(monome(q_cur,e_cur));
pshift(b,e_cur,bshift,contextptr);
sparse_poly1::iterator it=bshift.begin(),itend=bshift.end();
for (;it!=itend;++it){
if (is_undef(it->coeff))
break;
if (ck_is_strictly_greater(it->exponent,ordre,contextptr)){
it->coeff=undef;
bshift.erase(it+1,itend);
break;
}
}
if (!pmul(-q_cur,bshift,bshift,contextptr))
return false;
padd(rem,bshift,rem,contextptr);
// COUT << rem.front().exponent << " " << e0+ordre << '\n';
if (ck_is_strictly_greater(rem.front().exponent,a.front().exponent+ordre,contextptr)){
res.push_back(monome(undef,a.front().exponent+ordre+1-e0));
return true;
}
}
return true;
}
sparse_poly1 spdiv(const sparse_poly1 & a,const sparse_poly1 &b,GIAC_CONTEXT){
sparse_poly1 res;
gen og=min(porder(a),porder(b),contextptr);
int o=series_default_order(contextptr);
if (og.type==_INT_)
o=og.val;
if (!pdiv(a,b,res,o,contextptr))
res=sparse_poly1(1,monome(1,undef));
return res;
}
bool pdiv(const sparse_poly1 & a,const gen & b_orig, sparse_poly1 & res,GIAC_CONTEXT){
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
if (is_zero(b_orig))
return false; // divisionby0err(a);
if (is_one(b_orig)){
if (&a!=&res)
res=a;
return true;
}
gen b(b_orig);
if (&a==&res){
sparse_poly1::iterator it=res.begin(),itend=res.end();
for (;it!=itend;++it){
it->coeff=rdiv(it->coeff, b,contextptr);
if (series_flags(contextptr) & 0x1)
it->coeff=normal(it->coeff,contextptr);
}
// it->coeff=rdiv(it->coeff, b,contextptr);
return true;
}
sparse_poly1::const_iterator it=a.begin(),itend=a.end();
res.clear();
res.reserve(itend-it);
gen tmp;
for (;it!=itend;++it){
tmp=rdiv(it->coeff,b,contextptr);
if (series_flags(contextptr) & 0x1)
tmp=normal(tmp,contextptr);
res.push_back(monome(tmp , it->exponent));
}
// res.push_back(monome(rdiv(it->coeff,b,contextptr) , it->exponent));
return true;
}
sparse_poly1 spdiv(const sparse_poly1 & a,const gen &b,GIAC_CONTEXT){
sparse_poly1 res;
if (!pdiv(a,b,res,contextptr))
res=sparse_poly1(1,undef);
return res;
}
// v is replaced by e*v where e*v has no denominator
void lcmdeno(vecteur &v,gen & e,GIAC_CONTEXT){
if (v.empty()){
e=1;
return;
}
if (is_undef(v.front())){
v.erase(v.begin());
lcmdeno(v,e,contextptr);
v.insert(v.begin(),undef);
return;
}
vecteur l;
lvar(v,l);
//int l_size(l.size());
vecteur w;
w.reserve(2*v.size());
gen common=1,f,num,den;
// compute lcm of denominators in common
vecteur::iterator it=v.begin(),itend=v.end();
for (;it!=itend;++it){
if (is_integer(*it)){
num=*it; den=1;
}
else {
if (it->type==_FRAC && is_integer(it->_FRACptr->num) && is_integer(it->_FRACptr->den))
f=*it;
else
f=e2r(*it,l,contextptr);
fxnd(f,num,den);
}
w.push_back(num);
w.push_back(den);
// replace common by lcm of common and den
#if !defined USE_GMP_REPLACEMENTS && !defined BF2GMP_H
if (common.type==_ZINT && common.ref_count()==1 && is_integer(den)){
if (den.type==_ZINT)
mpz_lcm(*common._ZINTptr,*common._ZINTptr,*den._ZINTptr);
else
mpz_lcm_ui(*common._ZINTptr,*common._ZINTptr,absint(den.val));
}
else
common = lcm(common,den);
#else
common = lcm(common,den);
#endif
}
// compute e and recompute v
e=r2sym(common,l,contextptr);
it=v.begin();
for (int i=0;it!=itend;++it,i=i+2){
if (it->type==_FRAC && is_integer(it->_FRACptr->num) && is_integer(it->_FRACptr->den) && is_integer(common))
*it=w[i]*rdiv(common,w[i+1],contextptr);
else
*it=r2sym(w[i]*rdiv(common,w[i+1],contextptr),l,contextptr);
}
}
void lcmdeno_converted(vecteur &v,gen & e,GIAC_CONTEXT){
if (v.empty()){
e=1;
return;
}
if (is_undef(v.front())){
v.erase(v.begin());
lcmdeno_converted(v,e,contextptr);
v.insert(v.begin(),undef);
return;
}
vecteur w;
w.reserve(2*v.size());
gen common=1,f,num,den;
// compute lcm of denominators in common
vecteur::iterator it=v.begin(),itend=v.end();
for (;it!=itend;++it){
fxnd(*it,num,den);
w.push_back(num);
w.push_back(den);
// replace common by lcm of common and den
common = lcm(common,den);
}
// compute e and recompute v
e=common;
it=v.begin();
for (int i=0;it!=itend;++it,i=i+2)
*it=w[i]*rdiv(common,w[i+1],contextptr);
}
void lcmdeno(sparse_poly1 &v,gen & e,GIAC_CONTEXT){
if (v.empty()){
e=1;
return;
}
if (is_undef(v.back().coeff)){
monome last=v.back();
v.pop_back();
lcmdeno(v,e,contextptr);
v.push_back(last);
return;
}
vecteur l;
lvar(v,l);
int l_size(int(l.size()));
vector<gen> w;
w.reserve(2*l_size);
gen common=1,num,den,f;
// compute lcm of denominators in common
sparse_poly1::iterator it=v.begin(),itend=v.end();
for (;it!=itend;++it){
f=e2r(it->coeff,l,contextptr);
fxnd(f,num,den);
w.push_back(num);
w.push_back(den);
common=lcm(common,den);
}
// compute e and recompute v
e=r2sym(common,l,contextptr);
it=v.begin();
for (int i=0;it!=itend;++it,i=i+2){
it->coeff=r2sym(w[i]*rdiv(common,w[i+1],contextptr),l,contextptr);
}
}
void vreverse(iterateur a,iterateur aend){
iterateur b=aend-1;
for (;a<b;++a,--b){
swapgen(*a,*b);
}
}
bool pcompose(const vecteur & v,const sparse_poly1 & p, sparse_poly1 & res,GIAC_CONTEXT){
#ifdef TIMEOUT
control_c();
#endif
if (ctrl_c || interrupted) {
interrupted=ctrl_c=true;
return false;
}
if (v.empty()){
res.clear();
return true;
}
if ( p.empty() ){
res.clear();
if (!is_zero(v.front()))
res.push_back(monome(v.front(),0));
return true;
}
// Conversion of p and v to "internal" polynomial form
vecteur l; // will contain the list of variables common to v and p
alg_lvar(v,l);
alg_lvar(p,l);
// int l_size(l.size());
gen plcm=plus_one,vlcm=plus_one,f,num,den;
// compute lcm of denominators of p in plcm
sparse_poly1::const_iterator its=p.begin(),itsend=p.end();
vecteur ptemp;
ptemp.reserve(2*(itsend-its));
for (;its!=itsend;++its){
f=e2r(its->coeff,l,contextptr);
fxnd(f,num,den);
ptemp.push_back(num);
ptemp.push_back(den);
plcm=lcm(den,plcm);
}
// compute pcopy such that pcopy/plcm=p
its=p.begin();
sparse_poly1 pcopy;
pcopy.reserve(itsend-its);
for (int i=0;its!=itsend;++its,i=i+2){
num=ptemp[i]*rdiv(plcm,ptemp[i+1],contextptr);
pcopy.push_back(monome(num,its->exponent));
}
// do the same thing on v
vecteur w;
// compute lcm of denominators in common
vecteur::const_iterator it=v.begin(),itend=v.end();
w.reserve(2*(itend-it));
for (;it!=itend;++it){
f=e2r(*it,l,contextptr);
fxnd(f,num,den);
w.push_back(num);
w.push_back(den);
vlcm=lcm(vlcm,den);
}
// compute vcopy
it=v.begin();
vecteur vcopy;
vcopy.reserve(itend-it);
for (int i=0;it!=itend;++it,i=i+2)
vcopy.push_back(w[i]*rdiv(vlcm,w[i+1],contextptr));
vreverse(vcopy.begin(),vcopy.end());
if (vcopy.empty() ){
res=sparse_poly1(1,monome(undef,minus_inf));
return true;
}
// COUT << "compose " << vcopy << " with " << pcopy << '\n';
it=vcopy.begin(),itend=vcopy.end();
int n=int(itend-it)-1;
bool n_truncate=false;
gen n_valuation;
if (is_undef(*it)){
++it;
n_truncate=true;
n_valuation=gen(n)*p.front().exponent;
// add undef order term
gen cur_ordre=porder(p);
// compare cur_ordre with n*valuation(pcopy)
if ( (cur_ordre==plus_inf) || (ck_is_strictly_greater(cur_ordre,n_valuation,contextptr)) ){
// remove greater order terms from pcopy
for (;!pcopy.empty();){
if (ck_is_strictly_greater(pcopy.back().exponent,n_valuation,contextptr))
pcopy.pop_back();
else
break;
}
// insert undef
if (pcopy.empty() || (!is_undef(pcopy.back().coeff)) )
pcopy.push_back(monome(undef,n_valuation));