/
rootseq.c
2069 lines (2015 loc) · 55 KB
/
rootseq.c
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
//
// This file is part of Alpertron Calculators.
//
// Copyright 2019-2023 Dario Alejandro Alpern
//
// Alpertron Calculators 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.
//
// Alpertron Calculators 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 Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
//
#include <string.h>
#include <math.h>
#include <assert.h>
#include "rootseq.h"
#include "expression.h"
#define NBR_COEFF 6
BigInteger Quintic;
BigInteger Quartic;
BigInteger Cubic;
BigInteger Quadratic;
BigInteger Linear;
BigInteger Independent;
BigInteger discr;
BigInteger commonDenom;
BigInteger tmp0;
BigInteger tmp1;
BigInteger tmp2;
BigInteger tmp3;
BigInteger tmp4;
BigInteger tmp5;
BigInteger tmp6;
BigInteger tmp7;
BigRational RatQuartic;
BigRational RatCubic;
BigRational RatQuadratic;
BigRational RatLinear;
BigRational RatIndependent;
BigRational RatDeprCubic;
BigRational RatDeprQuadratic;
BigRational RatDeprLinear;
BigRational RatDeprIndependent;
BigRational RatDiscr;
BigRational RatDelta0;
BigRational RatDelta1;
BigRational RatD;
BigRational Rat1;
BigRational Rat2;
BigRational Rat3;
BigRational Rat4;
BigRational Rat5;
BigRational RatS;
int indexRoot;
const char *ptrMinus;
const char *ptrTimes;
const char *ptrSin;
const char *ptrCos;
const char* ptrACos;
const char* ptrTimesPi;
const char *ptrPi;
const char *ptrI;
static int totients[(2 * MAX_DEGREE) + 1];
extern char* ptrOutput;
enum toShow
{
SHOW_REAL = 0,
SHOW_IMAG,
};
struct cosine12
{
char* radicands;
int denominator;
};
static struct cosine12 aCosine12[] =
{ // S = sqrt next digit, [ = start sqrt, ] = end sqrt
{"(S6*[5+S5]+S5-1)", 8}, // cos 12°
{"(S6*[5-S5]+S5+1)", 8}, // cos 24°
{"(S5+1)", 4}, // cos 36°
{"(S6*[5+S5]-S5+1)", 8}, // cos 48°
{"1", 2}, // cos 60°
{"(S5-1)", 4}, // cos 72°
{"(S6*[5-S5]-S5-1)", 8}, // cos 84°
};
static struct cosine12 a2Plus2Cosine12[] =
{ // S = sqrt next digits, [ = start sqrt, ] = end sqrt
{"7+S6*[5+S5]+S5", 4}, // 2 + 2*cos 12°
{"9+S6*[5-S5]+S5", 4}, // 2 + 2*cos 24°
{"10+2*S5", 4}, // 2 + 2*cos 36°
{"9+S6*[5+S5]-S5", 4}, // 2 + 2*cos 48°
{"3", 2}, // 2 + 2*cos 60°
{"6-2*S5", 4}, // 2 + 2*cos 72°
{"7+S6*[5-S5]-S5", 4}, // 2 + 2*cos 84°
};
static struct cosine12 a2Minus2Cosine12[] =
{ // S = sqrt next digits, [ = start sqrt, ] = end sqrt
{"9-S6*[5+S5]-S5", 4}, // 2 - 2*cos 12°
{"7-S6*[5-S5]-S5", 4}, // 2 - 2*cos 24°
{"6-2*S5", 4}, // 2 - 2*cos 36°
{"7-S6*[5+S5]+S5", 4}, // 2 - 2*cos 48°
{"1", 2}, // 2 - 2*cos 60°
{"10-2*S5", 4}, // 2 - 2*cos 72°
{"9-S6*[5-S5]+S5", 4}, // 2 - 2*cos 84°
};
#if 0
-cos(Pi/17) + (1/16)*(1-sqrt(17)+sqrt(34-2*sqrt(17))+2*sqrt(17+3*sqrt(17)+sqrt(170+38*sqrt(17))))
-cos(2*Pi/17) + (1/16)*(-1+sqrt(17)+sqrt(34-2*sqrt(17))+2*sqrt(17+3*sqrt(17)-sqrt(170+38*sqrt(17))))
-cos(3*Pi/17) + (1/16)*(1+sqrt(17)+sqrt(34+2*sqrt(17))+2*sqrt(17-3*sqrt(17)-sqrt(170-38*sqrt(17))))
-cos(4*Pi/17) + (1/16)*(-1+sqrt(17)-sqrt(34-2*sqrt(17))+2*sqrt(17+3*sqrt(17)+sqrt(170+38*sqrt(17))))
-cos(5*Pi/17) + (1/16)*(1+sqrt(17)+sqrt(34+2*sqrt(17))-2*sqrt(17-3*sqrt(17)-sqrt(170-38*sqrt(17))))
-cos(6*Pi/17) + (1/16)*(-1-sqrt(17)+sqrt(34+2*sqrt(17))+2*sqrt(17-3*sqrt(17)+sqrt(170-38*sqrt(17))))
-cos(7*Pi/17) + (1/16)*(1+sqrt(17)-sqrt(34+2*sqrt(17))+2*sqrt(17-3*sqrt(17)+sqrt(170-38*sqrt(17))))
-cos(8*Pi/17) + (1/16)*(-1+sqrt(17)+sqrt(34-2*sqrt(17))-2*sqrt(17+3*sqrt(17)-sqrt(170+38*sqrt(17))))
#endif
static struct cosine12 aCosine17[] =
{ // S = sqrt next digits, [ = start sqrt, ] = end sqrt
{"(1-S17+[34-2*S17]+2*[17+3*S17+[170+38*S17]])", 16}, // cos(pi/17)
{"(-1+S17+[34-2*S17]+2*[17+3*S17-[170+38*S17]])", 16}, // cos(2*pi/17)
{"(1+S17+[34+2*S17]+2*[17-3*S17-[170-38*S17]])", 16}, // cos(3*pi/17)
{"(-1+S17-[34-2*S17]+2*[17+3*S17+[170+38*S17]])", 16}, // cos(4*pi/17)
{"(1+S17+[34+2*S17]-2*[17-3*S17-[170-38*S17]])", 16}, // cos(5*pi/17)
{"(-1-S17+[34+2*S17]+2*[17-3*S17+[170-38*S17]])", 16}, // cos(6*pi/17)
{"(1+S17-[34+2*S17]+2*[17-3*S17+[170-38*S17]])", 16}, // cos(7*pi/17)
{"(-1+S17+[34-2*S17]-2*[17+3*S17-[170+38*S17]])", 16}, // cos(8*pi/17)
};
static struct cosine12 a2PM2Cosine17[] =
{ // S = sqrt next digits, [ = start sqrt, ] = end sqrt
{"34-2*S17+2*[34-2*S17]+4*[17+3*S17+[170+38*S17]]", 1}, // 2+2*cos(pi/17)
{"34-2*S17-2*[34-2*S17]-4*[17+3*S17-[170+38*S17]]", 1}, // 2-2*cos(2*pi/17)
{"34+2*S17+2*[34+2*S17]+4*[17-3*S17-[170-38*S17]]", 1}, // 2+2*cos(3*pi/17)
{"34-2*S17+2*[34-2*S17]-4*[17+3*S17+[170+38*S17]]", 1}, // 2-2*cos(4*pi/17)
{"34+2*S17+2*[34+2*S17]-4*[17-3*S17-[170-38*S17]]", 1}, // 2+2*cos(5*pi/17)
{"34+2*S17-2*[34+2*S17]-4*[17-3*S17+[170-38*S17]]", 1}, // 2-2*cos(6*pi/17)
{"34+2*S17-2*[34+2*S17]+4*[17-3*S17+[170-38*S17]]", 1}, // 2+2*cos(7*pi/17)
{"34-2*S17-2*[34-2*S17]+4*[17+3*S17-[170+38*S17]]", 1}, // 2-2*cos(8*pi/17)
};
void startLine(void)
{
showText("<li>");
if (pretty == TEX)
{
showText("\\bullet\\,\\,");
}
}
void endLine(void)
{
if (pretty == TEX)
{
showText("\\\\");
}
showText("</li>");
}
void showVariable(char **pptrOutput, char letter)
{
char* ptrOut = *pptrOutput;
if (pretty == PRETTY_PRINT)
{
copyStr(&ptrOut, "<var>");
*ptrOut = letter;
ptrOut++;
copyStr(&ptrOut, "</var>");
}
else
{
*ptrOut = letter;
ptrOut++;
}
*pptrOutput = ptrOut;
}
void showVarIndex(char letter, int index)
{
if (pretty == PRETTY_PRINT)
{
showText("<var>");
*ptrOutput = letter;
ptrOutput++;
showText("</var><sub>");
int2dec(&ptrOutput, index);
showText("</sub>");
}
else if (pretty == TEX)
{
*ptrOutput = letter;
ptrOutput++;
showText("_{");
int2dec(&ptrOutput, index);
showText("}");
}
else
{
*ptrOutput = letter;
ptrOutput++;
int2dec(&ptrOutput, index);
}
}
static void showXindex(int index)
{
showVarIndex('x', index);
}
void showX(int multiplicity)
{
if (teach)
{
showText("<div class=\"outerbox\"><div class=\"box\"><p>");
}
else
{
startLine();
}
if (multiplicity > 2)
{
showXindex(indexRoot);
showText(lang ? " a " : " to ");
indexRoot += multiplicity;
showXindex(indexRoot - 1);
showText(" = ");
}
else
{ // Multiplicity 1 or 2.
for (int ctr = 0; ctr < multiplicity; ctr++)
{
showXindex(indexRoot);
indexRoot++;
showText(" = ");
}
}
}
void endShowX(void)
{
if (teach)
{
showText("</p></div></div>");
}
else
{
endLine();
}
}
void startSqrt(void)
{
if (pretty == PRETTY_PRINT)
{
showText("<r-2><r-a>");
}
else if (pretty == TEX)
{
showText("\\sqrt{");
}
else
{
showText("(");
}
}
void endSqrt(void)
{
if (pretty == PRETTY_PRINT)
{
showText("</r-a></r-2>");
}
else if (pretty == TEX)
{
showText("}");
}
else
{
showText(")^(1/2)");
}
}
void startCbrt(void)
{
if (pretty == PRETTY_PRINT)
{
showText("<r-t><span class=\"befrad\" aria-hidden=\"true\">3</span><span class=\"radicand3\">");
}
else if (pretty == TEX)
{
showText("\\sqrt[3]{");
}
else
{
showText("(");
}
}
void endCbrt(void)
{
if (pretty == PRETTY_PRINT)
{
showText("</span></r-t>");
}
else if (pretty == TEX)
{
showText("}");
}
else
{
showText(")^(1/3)");
}
}
void startParen(void)
{
if (pretty == PRETTY_PRINT)
{
showText("<o-p><p-p>");
}
else if (pretty == TEX)
{
showText("\\left(");
}
else
{
*ptrOutput ='(';
ptrOutput++;
}
}
void endParen(void)
{
if (pretty == PRETTY_PRINT)
{
showText("</p-p></o-p>");
}
else if (pretty == TEX)
{
showText("\\right)");
}
else
{
*ptrOutput =')';
ptrOutput++;
}
}
void showPlusSignOn(bool condPlus, int type)
{
if ((type & TYPE_PM_SPACE_BEFORE) != 0)
{
*ptrOutput = ' ';
ptrOutput++;
}
if (condPlus)
{
*ptrOutput = '+';
ptrOutput++;
}
else
{
showText(ptrMinus);
}
if ((type & TYPE_PM_SPACE_AFTER) != 0)
{
*ptrOutput = ' ';
ptrOutput++;
}
}
void showRatConstants(const char* numerator, const char* denominator)
{
if (pretty == PRETTY_PRINT)
{
showText("<f-f><f-n>");
showText(numerator);
showText("</f-n><f-d>");
showText(denominator);
showText("</f-d></f-f>");
}
else if (pretty == TEX)
{
showText("\\frac{");
showText(numerator);
showText("}{");
showText(denominator);
showText("}");
}
else
{
showText("(");
showText(numerator);
showText("/");
showText(denominator);
showText(")");
}
}
void showTimesPi(char** pptrString)
{
char* ptrString = *pptrString;
if (pretty == PARI_GP)
{
copyStr(&ptrString, "*Pi");
}
else if (pretty == PRETTY_PRINT)
{
copyStr(&ptrString, ptrTimesPi);
}
else
{ // TEX
copyStr(&ptrString, ptrTimes);
*ptrString = ' ';
ptrString++;
copyStr(&ptrString, ptrPi);
}
*pptrString = ptrString;
}
static int gcd(int first, int second)
{
int a = first;
int b = second;
while (b != 0)
{
int c = a % b;
a = b;
b = c;
}
return a;
}
// When gcd(first, second) = 1, find Mult1st and Mult2nd such that:
// first * Mult1st + second * Mult2nd = 1.
static void ExtendedGCD(int first, int second, int* pMult1st, int* pMult2nd)
{
int a = first;
int b = second;
int mult1stOld = 1;
int mult1st = 0;
int mult2ndOld = 0;
int mult2nd = 1;
while (b != 0)
{
int quot = a / b;
int temp = b;
b = a - (quot * b);
a = temp;
temp = mult1st;
mult1st = mult1stOld - (quot * mult1st);
mult1stOld = temp;
temp = mult2nd;
mult2nd = mult2ndOld - (quot * mult2nd);
mult2ndOld = temp;
}
*pMult1st = mult1stOld;
*pMult2nd = mult2ndOld;
}
void showRatString(const char* num, const char* den)
{
if (pretty != PARI_GP)
{
showRatConstants(num, den);
}
else
{
showText("(");
showText(num);
showText("/");
showText(den);
showText(")");
}
}
static void ParseExpression(const char* expr)
{
const char* ptrExpr = expr;
while (*ptrExpr != 0)
{
if (*ptrExpr == 'S')
{ // Square root of next digits.
if (pretty != PARI_GP)
{
startSqrt();
}
for (;;)
{
ptrExpr++;
char c = *ptrExpr;
if ((c < '0') || (c > '9'))
{
break;
}
*ptrOutput = c;
ptrOutput++;
}
ptrExpr--;
if (pretty != PARI_GP)
{
endSqrt();
}
else
{
showText("^(1/2)");
}
}
else if (*ptrExpr == '[')
{ // Start of square root.
startSqrt();
}
else if (*ptrExpr == ']')
{ // End of square root.
endSqrt();
}
else if (*ptrExpr == '-')
{
showText(ptrMinus);
}
else if (*ptrExpr == '*')
{
showText(ptrTimes);
}
else if (*ptrExpr == '(')
{
startParen();
}
else if (*ptrExpr == ')')
{
endParen();
}
else
{
*ptrOutput = *ptrExpr;
ptrOutput++;
}
ptrExpr++;
}
}
// Show cos(num*pi/den) as radicals.
// The output is the sign and the value of the denominator, or zero
// if the result is zero.
static int showRadicals(int numerator, int denominator, int multipl,
int powerOf2, const char *times)
{
int multiple = multipl;
int power2 = powerOf2;
int num = numerator;
int den = denominator;
int arraySigns[20];
int indexSigns = 0;
int exprDen;
int den2 = 2 * den;
int angle;
const char* ptrExpr;
int sign;
int mult;
int result;
char denom[15];
num = num % den2; // Convert to range 0 to 360 degrees.
if (num < 0)
{
num += den2;
}
if (num == 0)
{
return 1;
}
if (num == den)
{
return -1;
}
if (((num * 2) == den) || ((num*2) == (den*3)))
{
return 0;
}
while (((num % 2) == 0) && ((den % 2) == 0))
{
num /= 2;
den /= 2;
power2--;
}
if (multiple == 1)
{
power2 -= 2;
multiple = 4;
angle = num % 8;
if ((angle == 1) || (angle == 7))
{ // 45 or 315 degrees.
sign = 1;
}
else
{
sign = -1;
}
if (power2 > 0)
{
if (sign > 0)
{
ptrExpr = "2+S2"; // 2+2*cos(Pi/4)
}
else
{
ptrExpr = "2-S2"; // 2-2*cos(Pi/4)
}
}
else
{
ptrExpr = "S2"; // cos(Pi/4)
}
exprDen = 2;
}
else
{ // Obtain cosine of num*pi/multiple.
int indexExpr;
angle = (2*num * 15 / multiple) % 60;
if (angle < 15)
{ // Angle between 0 and 90 degrees.
indexExpr = (angle - 1) / 2;
sign = 1;
}
else if (angle < 30)
{ // Angle between 90 and 180 degrees.
indexExpr = (30 - angle - 1) / 2;
sign = -1;
}
else if (angle < 45)
{ // Angle between 180 and 270 degrees.
indexExpr = (angle - 30 - 1) / 2;
sign = -1;
}
else
{ // Angle between 270 and 360 degrees.
indexExpr = (60 - angle - 1) / 2;
sign = 1;
}
if (power2 > 0)
{
if (sign > 0)
{
ptrExpr = a2Plus2Cosine12[indexExpr].radicands;
exprDen = a2Plus2Cosine12[indexExpr].denominator;
}
else
{
ptrExpr = a2Minus2Cosine12[indexExpr].radicands;
exprDen = a2Minus2Cosine12[indexExpr].denominator;
}
}
else
{
ptrExpr = aCosine12[indexExpr].radicands;
exprDen = aCosine12[indexExpr].denominator;
}
}
mult = 1;
for (indexSigns = power2 - 1; indexSigns >= 0; indexSigns--)
{
if (indexSigns >= ((int)sizeof(arraySigns) / (int)sizeof(arraySigns[0])))
{ // This cannot occur.
return 0;
}
arraySigns[indexSigns] = sign;
angle = num * 15 / multiple % (60*mult);
if ((angle < (15*mult)) || (angle > (45*mult)))
{ // Angle between 0 and 90 degrees.
// or from 270 to 360 degrees.
sign = 1;
}
else
{ // Angle between 90 and 270 degrees.
sign = -1;
}
mult *= 2;
}
for (indexSigns = 0; indexSigns < power2; indexSigns++)
{
if (indexSigns == (power2 - 1))
{
if ((indexSigns > 0) && (exprDen != 2))
{
char* ptrDenom = denom;
if (exprDen == 1)
{
*ptrOutput = '2';
ptrOutput++;
}
else
{
int2dec(&ptrDenom, exprDen / 2);
*ptrDenom = 0; // End of string.
showRatString("1", denom);
}
showText(ptrTimes);
}
startSqrt();
break;
}
startSqrt();
*ptrOutput = '2';
ptrOutput++;
if (arraySigns[indexSigns] < 0)
{
*ptrOutput = ' ';
ptrOutput++;
showText(ptrMinus);
*ptrOutput = ' ';
ptrOutput++;
}
else
{
showText(" + ");
}
}
// Interpret expression.
if ((power2 == 0) && (strcmp(ptrExpr, "1") == 0))
{
return sign * exprDen;
}
ParseExpression(ptrExpr);
for (indexSigns = 0; indexSigns < power2; indexSigns++)
{
endSqrt();
}
result = ((power2 > 1)? 2 : exprDen);
if (strcmp(ptrExpr, "1") && (result != 1))
{
showText(times);
}
return sign*result;
}
// Show cos(numerator34*Pi/34)
static int showRadicals17(int numerator34)
{
int index;
int angle2;
int angle = numerator34 % 68; // Convert to range 0 to 360 degrees.
if (angle < 0)
{
angle += 68;
}
assert((angle != 17) && (angle != 51)); // It cannot be 90 deg or 270 deg.
if ((angle % 2) == 0)
{
int sign;
angle /= 2; // Show cos(angle*Pi/17).
if (angle < 9)
{ // Range 0 to 90 degrees.
index = angle - 1;
sign = 1;
}
else if (angle < 17)
{ // Range 90 to 180 degrees.
index = 17 - angle - 1;
sign = -1;
}
else if (angle < 26)
{ // Range 180 to 270 degrees.
index = angle - 17 - 1;
sign = -1;
}
else
{ // Range 270 to 360 degrees.
index = 34 - angle - 1;
sign = 1;
}
ParseExpression(aCosine17[index].radicands);
return aCosine17[index].denominator * sign;
}
angle2 = angle % 34;
if (angle2 < 9)
{ // Range 0 to 90 degrees. Cosine positive.
index = angle2 - 1;
}
else if (angle2 < 17)
{ // Range 90 to 180 degrees. Cosine negative.
index = 17 - angle2 - 1;
}
else if (angle2 < 26)
{ // Range 180 to 270 degrees. Cosine negative.
index = angle2 - 17 - 1;
}
else
{ // Range 270 to 360 degrees. Cosine positive.
index = 34 - angle2 - 1;
}
startSqrt();
ParseExpression(a2PM2Cosine17[index].radicands);
endSqrt();
if ((angle < 17) || (angle > 51))
{
return 8;
}
return -8;
}
static void AdjustComponent(int denominator, char* ptrStart, enum toShow toShow,
int isFirst, const char *realRoot)
{
int denomin = denominator;
char beginning[1000];
char* ptrBeginning = beginning;
int lenBeginning;
*ptrBeginning = 0;
size_t diffPtrs;
if (denomin == 0)
{ // Discard all output if result is zero.
ptrOutput = ptrStart;
*ptrOutput = 0;
return;
}
if (denomin < 0)
{
copyStr(&ptrBeginning, (pretty == PRETTY_PRINT)? "−" : "-");
denomin = -denomin; // Make it positive.
}
else if ((toShow == SHOW_IMAG) || (isFirst == 0))
{
copyStr(&ptrBeginning, " + ");
}
else
{ // Nothing to do.
}
if (denomin == 1)
{
if (toShow == SHOW_IMAG)
{
if (pretty == PARI_GP)
{
*ptrBeginning = 'I';
ptrBeginning++;
}
else
{
*ptrBeginning = 'i';
ptrBeginning++;
*ptrBeginning = ' ';
ptrBeginning++;
}
copyStr(&ptrBeginning, ptrTimes);
}
}
else if (pretty != PARI_GP)
{
copyStr(&ptrBeginning, (pretty == TEX)? "\\frac{":
"<f-f><f-n>");
// Start numerator.
if (pretty == PARI_GP)
{
*ptrBeginning = ((toShow == SHOW_REAL)? '1' : 'I');
}
else
{
*ptrBeginning = ((toShow == SHOW_REAL)? '1' : 'i');
}
ptrBeginning++;
// End numerator.
copyStr(&ptrBeginning, (pretty == TEX)? "}{": "</f-n><f-d>");
// Start denominator.
int2dec(&ptrBeginning, denomin);
// End numerator.
copyStr(&ptrBeginning, (pretty == TEX)? "}": "</f-d></f-f> ");
copyStr(&ptrBeginning, ptrTimes);
}
else
{ // PARI-GP
*ptrBeginning = '(';
ptrBeginning++;
*ptrBeginning = ((toShow == SHOW_REAL)? '1' : 'I');
ptrBeginning++;
*ptrBeginning = '/';
ptrBeginning++;
int2dec(&ptrBeginning, denomin);
*ptrBeginning = ')';
ptrBeginning++;
*ptrBeginning = '*';
ptrBeginning++;
*ptrBeginning = 0; // Add terminator at end of string.
}
copyStr(&ptrBeginning, realRoot);
diffPtrs = ptrBeginning - &beginning[0];
lenBeginning = (int)diffPtrs;
if ((*realRoot != 0) && (lenBeginning != 0) && (*ptrStart != 0))
{
copyStr(&ptrBeginning, ptrTimes);
}
diffPtrs = ptrBeginning - &beginning[0];
lenBeginning = (int)diffPtrs;
(void)memmove(ptrStart + lenBeginning, ptrStart, strlen(ptrStart));
(void)memcpy(ptrStart, beginning, lenBeginning);
ptrOutput += lenBeginning;
*ptrOutput = 0;
}
// If den is multiple of 17, compute the extended GCD of 17 and den/17.
// Let the results be A and B respectively. so num*pi/den = num*A*pi/17 +
// num*B*pi/(den/17). Use the formula cos(a+b) = cos a * cos b - sin a * sin b.
static void showComponent(int num, int den, int multipl, int power2,
enum toShow toShow, const char *realRoot)
{
int numer = num;
int denom = den;
int multiple = multipl;
int denominCos;
int den2 = 2 * denom;
char * ptrStartRadicals = ptrOutput;
*ptrOutput = 0;
numer = numer % den2; // Convert to range 0 to 360 degrees.
if (numer < 0)
{
numer += den2;
}
if ((denom % 17) == 0)
{ // Denominator is multiple of 17.
int numerator34;
int numeratorDen;
int denominCos17;
denom /= 17;
multiple /= 17;
ExtendedGCD(denom, 17, &numerator34, &numeratorDen);
numerator34 *= 2*numer;
numeratorDen *= numer;
// The original angle is multDen*pi/den + mult17*pi/17 = A + B.
// Show cos(A) * cos(B) - sin(A) * sin(B).
// Show cos(A).
denominCos = showRadicals(numeratorDen, denom, multiple, power2, ptrTimes);
if (denominCos != 0)
{ // Show cos(B).
denominCos17 = showRadicals17(numerator34);
AdjustComponent(denominCos * denominCos17, ptrStartRadicals, toShow, 1, realRoot);
}
// Show sin(A).
ptrStartRadicals = ptrOutput;
*ptrOutput = 0;
if ((denom & 0x01) == 0x01)
{ // denom is odd.
denominCos = showRadicals(denom - (numeratorDen * 2), denom * 2, multiple, 1, ptrTimes);
}
else
{ // denom is even.
denominCos = showRadicals((denom / 2) - numeratorDen, denom, multiple, power2, ptrTimes);
}
if (denominCos != 0)
{ // Show sin(B).
denominCos17 = showRadicals17(17 - numerator34);
AdjustComponent(-denominCos * denominCos17, ptrStartRadicals, toShow, 0, realRoot);
}
return;
}
denominCos = showRadicals(numer, denom, multiple, power2, "");
AdjustComponent(denominCos, ptrStartRadicals, toShow, 1, realRoot);
}
// Try to find a radical expression for cos(num*pi/den) +
// i*sin(num*pi/den). If it is not possible, return with no output.
static void outputRadicandsForCosSin(int num, int den, const char *realRoot)
{
// den must be 3^e3 * 5^e5 * 17^e17 * 2^k, where the exponents can be 0 or 1.
// At this moment we do not consider other Fermat prime numbers.
int multiple = 1;
int power2 = 0;
int quot = den;
if ((quot % 3) == 0)
{
multiple *= 3;
quot /= 3;
}
if ((quot % 5) == 0)
{
multiple *= 5;
quot /= 5;
}
if ((quot % 17) == 0)
{
multiple *= 17;
quot /= 17;
}
// At this moment quot should be a power of 2.
while ((quot % 2) == 0)
{
power2++;
quot /= 2;
}