forked from mfillpot/mathomatic
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpoly.c
2214 lines (2153 loc) · 72.1 KB
/
poly.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
/*
* Mathomatic simplifying and general polynomial routines.
* Includes polynomial and smart division, polynomial factoring, etc.
* Globals tlhs[] and trhs[] are used and wiped out by most of these routines.
*
* The polynomial division and GCD routines here are not recursive,
* due to the global static expression storage areas.
* This limits the polynomial GCD routines to mostly univariate operation.
* This also does not allow their use during solving.
* So far, these limitations have been a good thing,
* making Mathomatic faster and more stable and reliable.
* Making polynomial GCD calculations partially recursive
* with one level of recursion would enable multivariate operation for most cases, I think.
*
* Mathomatic has proved it is practical and efficient to do
* generalized polynomial operations. By generalized, I mean that
* the coefficients of polynomials are not specially treated or stored in any way.
* They are not separated out from the main variable of the polynomial.
* Maximum simplification of all expressions is not possible unless everything is generalized.
*
* Copyright (C) 1987-2012 George Gesslein II.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
The chief copyright holder can be contacted at [email protected], or
George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/
#include "includes.h"
#define REMAINDER_IS_ZERO() (mathomatic->n_trhs == 1 && mathomatic->trhs[0].kind == CONSTANT && mathomatic->trhs[0].token.constant == 0.0)
/*
* The following static expression storage areas are of non-standard size
* and must only be used for temporary storage.
* Most Mathomatic expression manipulation and simplification routines should not be used
* on non-standard or constant size expression storage areas.
* Standard size expression storage areas that may be
* manipulated or simplified are the equation spaces, tlhs[], trhs[], and tes[] only.
*/
//token_type divisor[DIVISOR_SIZE]; /* static expression storage areas for polynomial and smart division */
//int n_divisor; /* length of expression in divisor[] */
//token_type quotient[DIVISOR_SIZE];
//int n_quotient; /* length of expression in quotient[] */
//token_type gcd_divisor[DIVISOR_SIZE]; /* static expression storage area for polynomial GCD routine */
//int len_d; /* length of expression in gcd_divisor[] */
static int pf_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int do_repeat);
static int pf_sub(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int len, int level, int do_repeat);
static int save_factors(MathoMatic* mathomatic, token_type *equation, int *np, int loc1, int len, int level);
static int do_gcd(MathoMatic* mathomatic, long *vp);
static int mod_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level);
static int polydiv_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level);
static int pdiv_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int code);
static int poly_div_sub(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2, long *vp);
static int find_highest_count(token_type *p1, int n1, token_type *p2, int n2, long *vp1);
#ifndef VCMP_DEFINED
#define VCMP_DEFINED /*there is a copy elsewhere */
/*
* Compare function for qsort(3).
*/
static int
vcmp(p1, p2)
sort_type *p1, *p2;
{
if (p2->count == p1->count) {
if (p1->v < p2->v)
return -1;
if (p1->v == p2->v)
return 0;
return 1;
}
return(p2->count - p1->count);
}
#endif /*VCMP_DEFINED*/
/*
* Return true if passed expression is strictly a single polynomial term in variable v.
* The general form of a polynomial term is c*(v^d)
* The coefficient (c) and exponent (d) may be any expression,
* as long as it doesn't contain the variable v.
*/
int
poly_in_v_sub(MathoMatic* mathomatic, token_type *p1, int n, long v, int allow_divides)
//token_type *p1; /* expression pointer */
//int n; /* expression length */
//long v; /* Mathomatic variable */
//int allow_divides; /* if true, allow division by variable */
{
int i, k;
int level, vlevel;
int count;
level = min_level(mathomatic, p1, n);
for (i = 0, count = 0; i < n; i += 2) {
if (p1[i].kind == VARIABLE && p1[i].token.variable == v) {
count++;
if (count > 1)
return false;
vlevel = p1[i].level;
if (vlevel == level || vlevel == (level + 1)) {
for (k = 1; k < n; k += 2) {
if (p1[k].level == level) {
switch (p1[k].token.operatr) {
case DIVIDE:
if (!allow_divides && k == (i - 1))
return false;
case TIMES:
continue;
case POWER:
if (k == (i + 1))
continue;
default:
return false;
}
}
}
if (vlevel == (level + 1)) {
if ((i + 1) < n && p1[i+1].level == vlevel
&& p1[i+1].token.operatr == POWER) {
continue;
}
} else {
continue;
}
}
return false;
}
}
return true;
}
/*
* Return true if passed expression is a polynomial in the given variable v.
* The coefficients and exponents may be anything, as long as they don't contain the variable v.
* The passed expression should be fully unfactored, for a proper determination.
*/
int
poly_in_v(MathoMatic* mathomatic, token_type *p1, int n, long v, int allow_divides)
//token_type *p1; /* expression pointer */
//int n; /* expression length */
//long v; /* Mathomatic variable */
//int allow_divides; /* allow variable to be right of a divide (negative exponents) as a polynomial term */
{
int i, j;
for (i = 1, j = 0;; i += 2) {
if (i >= n || (p1[i].level == 1
&& (p1[i].token.operatr == PLUS || p1[i].token.operatr == MINUS))) {
if (!poly_in_v_sub(mathomatic, &p1[j], i - j, v, allow_divides)) {
return false;
}
j = i + 1;
}
if (i >= n)
break;
}
return true;
}
/*
* Factor polynomials by calling pf_sub() for every additive sub-expression.
* Factors repeated factor polynomials (like (x+1)^5) if do_repeat is true.
* And always factors multivariate polynomials with symbolic factors (like (x+a)*(x+b)).
*
* Does not factor polynomials with different numeric factors (like (x+1)*(x+2)).
*
* Because of accumulated floating point round-off error,
* this routine does not succeed at factoring most large polynomials.
*
* Return true if equation side was modified (factored).
*/
int
poly_factor(MathoMatic* mathomatic, token_type *equation, int *np, int do_repeat)
//token_type *equation; /* pointer to the beginning of equation side */
//int *np; /* pointer to length of equation side */
//int do_repeat; /* factor repeated factors flag */
{
return pf_recurse(mathomatic, equation, np, 0, 1, do_repeat);
}
static int
pf_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int do_repeat)
{
int modified = false;
int i;
int count = 0, level_count = 0;
for (i = loc + 1; i < *np && equation[i].level >= level; i += 2) {
switch (equation[i].token.operatr) {
case PLUS:
case MINUS:
count++;
if (equation[i].level == level) {
level_count++;
}
}
}
if (level_count && count > 1) { /* so we don't factor expressions with only one additive operator */
/* try to factor the sub-expression */
modified = pf_sub(mathomatic, equation, np, loc, i - loc, level, do_repeat);
}
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
modified |= pf_recurse(mathomatic, equation, np, i, level + 1, do_repeat);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
continue;
}
i++;
}
return modified;
}
/*
* Polynomial factoring subroutine.
* It can't factor everything, but it usually can factor polynomials
* if it will make the expression size smaller.
*
* Return true if equation side was modified (factored).
*/
static int
pf_sub(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int len, int level, int do_repeat)
//token_type *equation; /* equation side holding the possible polynomial to factor */
//int *np, /* pointer to length of equation side */
// loc, /* index of start of polynomial in equation side */
// len; /* length of polynomial */
//int level; /* level of additive operators in polynomial */
//int do_repeat; /* factor repeated factors flag */
{
token_type *p1;
int modified = false, symbolic_modified = false;
int i, j, k;
long v = 0, v1, last_v;
int len_first = 0;
int loc1, loc2, len2 = 0;
int loct, lent;
int count;
jmp_buf save_save;
int div_flag = 3;
int vc, cnt;
sort_type va[MAX_VARS];
double d;
int old_partial;
debug_string(mathomatic, 3, "Entering pf_sub().");
old_partial = mathomatic->partial_flag;
loc2 = loc1 = loc;
find_greatest_power(&equation[loc1], len, &v, &d, &j, &k, &div_flag);
if (v == 0)
return false;
blt(save_save, mathomatic->jmp_save, sizeof(mathomatic->jmp_save));
if ((i = setjmp(mathomatic->jmp_save)) != 0) { /* trap errors */
mathomatic->partial_flag = old_partial;
blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
if (i == 13) { /* critical error code */
longjmp(mathomatic->jmp_save, i);
}
return(modified || symbolic_modified);
}
/* First factor polynomials with repeated factors */
/* using poly_gcd(polynomial, v * differentiate(polynomial, v)) to discover the factors: */
for (count = 1; do_repeat; count++) {
blt(mathomatic->trhs, &equation[loc1], len * sizeof(token_type));
mathomatic->n_trhs = len;
mathomatic->partial_flag = false;
uf_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
mathomatic->partial_flag = old_partial;
if (level1_plus_count(mathomatic, mathomatic->trhs, mathomatic->n_trhs) < 2) {
/* must be at least 2 level 1 additive operators to be factorable */
goto skip_factor;
}
/* create a variable list with counts of the number of times each variable occurs: */
last_v = 0;
for (vc = 0; vc < ARR_CNT(va);) {
cnt = 0;
v1 = -1;
for (i = 0; i < mathomatic->n_trhs; i += 2) {
if (mathomatic->trhs[i].kind == VARIABLE && mathomatic->trhs[i].token.variable > last_v) {
if (v1 == -1 || mathomatic->trhs[i].token.variable < v1) {
v1 = mathomatic->trhs[i].token.variable;
cnt = 1;
} else if (mathomatic->trhs[i].token.variable == v1) {
cnt++;
}
}
}
if (v1 == -1)
break;
last_v = v1;
va[vc].v = v1;
va[vc].count = cnt;
vc++;
}
side_debug(mathomatic, 3, &equation[loc1], len);
side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
/* Find a valid polynomial base variable "v": */
cnt = -1;
if (v) {
if (vc > 1 && !poly_in_v(mathomatic, mathomatic->trhs, mathomatic->n_trhs, v, true)) {
v = 0;
}
}
for (i = 0; i < vc; i++) {
if ((va[i].v & VAR_MASK) <= SIGN) {
continue;
}
if (v == 0) {
if (poly_in_v(mathomatic, mathomatic->trhs, mathomatic->n_trhs, va[i].v, true)) {
v = va[i].v;
}
}
if (cnt < 0 || va[i].count < cnt) {
cnt = va[i].count;
}
}
if (cnt <= 1) /* A polynomial requires 2 or more instances of every normal variable to be factorable. */
goto skip_factor;
if (v == 0) {
#if 1
goto skip_factor;
#else
/* no polynomial variables found, try differentiating with respect to each normal variable until one works */
for (i = 0; i < vc; i++) {
if ((va[i].v & VAR_MASK) <= SIGN) {
continue;
}
v = va[i].v;
blt(mathomatic->tlhs, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
mathomatic->n_tlhs = mathomatic->n_trhs;
if (differentiate(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, v)) {
break;
}
v = 0;
}
if (v == 0) {
goto skip_factor;
}
#endif
} else {
blt(mathomatic->tlhs, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
mathomatic->n_tlhs = mathomatic->n_trhs;
if (!differentiate(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, v)) {
break;
}
}
#if !SILENT
if (mathomatic->debug_level >= 3) {
list_var(mathomatic, v, 0);
fprintf(mathomatic->gfp, _("Differentiation successful using variable %s.\n"), mathomatic->var_str);
}
#endif
simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
if ((mathomatic->n_tlhs + 2) > min(DIVISOR_SIZE, mathomatic->n_tokens))
break;
for (i = 0; i < mathomatic->n_tlhs; i++)
mathomatic->tlhs[i].level++;
mathomatic->tlhs[mathomatic->n_tlhs].kind = OPERATOR;
mathomatic->tlhs[mathomatic->n_tlhs].level = 1;
mathomatic->tlhs[mathomatic->n_tlhs].token.operatr = TIMES;
mathomatic->n_tlhs++;
mathomatic->tlhs[mathomatic->n_tlhs].kind = VARIABLE;
mathomatic->tlhs[mathomatic->n_tlhs].level = 1;
mathomatic->tlhs[mathomatic->n_tlhs].token.variable = v;
mathomatic->n_tlhs++;
uf_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
if (poly_gcd(mathomatic, &equation[loc1], len, mathomatic->tlhs, mathomatic->n_tlhs, v) <= 0)
break;
if (level1_plus_count(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs) == 0)
break;
if (!save_factors(mathomatic, equation, np, loc1, len, level))
break;
loc1 += mathomatic->n_tlhs + 1;
len = mathomatic->n_trhs;
switch (count) {
case 1:
debug_string(mathomatic, 1, "Polynomial with repeated factor factored.");
len_first = mathomatic->n_tlhs;
loc2 = loc1;
break;
case 2:
len2 = mathomatic->n_tlhs;
break;
}
modified = true;
}
/* Now factor polynomials with symbolic factors by grouping: */
if (!modified) {
last_v = 0;
next_v:
p1 = &equation[loc1];
blt(mathomatic->trhs, p1, len * sizeof(token_type));
mathomatic->n_trhs = len;
uf_simp_no_repeat(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
if (level1_plus_count(mathomatic, mathomatic->trhs, mathomatic->n_trhs) < 2) {
/* must be at least 2 level 1 additive operators to be factorable */
goto skip_factor;
}
for (;;) {
v = -1;
for (i = 0; i < len; i += 2) {
if (p1[i].kind == VARIABLE && p1[i].token.variable > last_v) {
if (v == -1 || p1[i].token.variable < v) {
v = p1[i].token.variable;
}
}
}
if (v == -1) {
break;
}
last_v = v;
/* make sure there is more than one "v" raised to the highest power: */
if (find_greatest_power(mathomatic->trhs, mathomatic->n_trhs, &v, &d, &j, &k, &div_flag) <= 1) {
continue;
}
blt(mathomatic->tlhs, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
mathomatic->n_tlhs = mathomatic->n_trhs;
/* do the grouping: */
while (factor_plus(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, v, 0.0)) {
simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
}
/* extract the highest power group: */
if (find_greatest_power(mathomatic->tlhs, mathomatic->n_tlhs, &v, &d, &j, &k, &div_flag) != 1) {
continue;
}
if (j) {
blt(mathomatic->tlhs, &mathomatic->tlhs[j], k * sizeof(token_type));
}
mathomatic->n_tlhs = k;
#if !SILENT
if (mathomatic->debug_level >= 3) {
fprintf(mathomatic->gfp, _("Trying factor: "));
list_proc(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs, false);
fprintf(mathomatic->gfp, "\n");
}
#endif
if (poly_gcd(mathomatic, &equation[loc1], len, mathomatic->tlhs, mathomatic->n_tlhs, 0L) <= 0)
goto next_v;
if (level1_plus_count(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs) == 0)
goto next_v;
if (!symbolic_modified) {
debug_string(mathomatic, 1, "Symbolic polynomial factored.");
} else {
debug_string(mathomatic, 1, "Found another symbolic factor.");
}
if (!save_factors(mathomatic, equation, np, loc1, len, level))
break;
len = mathomatic->n_tlhs;
symbolic_modified = true;
last_v = 0;
goto next_v;
}
}
skip_factor:
blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
if (modified) {
/* Repeated factor was factored out. */
/* See if we can factor out more of the repeated factor. */
if (len2) {
loct = loc2;
lent = len2;
} else {
loct = loc;
lent = len_first;
}
if (poly_gcd(mathomatic, &equation[loc1], len, &equation[loct], lent, v) > 0) {
if (save_factors(mathomatic, equation, np, loc1, len, level)) {
loc1 += mathomatic->n_tlhs + 1;
len = mathomatic->n_trhs;
}
}
if (len2) {
loc1 = loc2;
len = len2;
}
if (poly_gcd(mathomatic, &equation[loc], len_first, &equation[loc1], len, 0L) > 0) {
save_factors(mathomatic, equation, np, loc, len_first, level);
}
}
if (modified || symbolic_modified) {
for (i = loc; i < *np && equation[i].level >= level; i++)
;
#if DEBUG
if ((i & 1) != 1) {
error_bug(mathomatic, "Error in result of pf_sub().");
}
#endif
debug_string(mathomatic, 1, "Resulting factors of pf_sub():");
side_debug(mathomatic, 1, &equation[loc], i - loc);
}
return(modified || symbolic_modified);
}
static int
save_factors(MathoMatic* mathomatic, token_type *equation, int *np, int loc1, int len, int level)
{
int i, j;
i = mathomatic->n_tlhs + 1 + mathomatic->n_trhs;
if (i > (len * 3))
goto rejected;
if ((*np + (i - len)) > mathomatic->n_tokens)
goto rejected;
blt(&equation[loc1+i], &equation[loc1+len], (*np - (loc1 + len)) * sizeof(token_type));
*np += i - len;
blt(&equation[loc1], mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
i = loc1 + mathomatic->n_tlhs;
equation[i].level = 0;
equation[i].kind = OPERATOR;
equation[i].token.operatr = TIMES;
i++;
blt(&equation[i], mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
i += mathomatic->n_trhs;
for (j = loc1; j < i; j++)
equation[j].level += level;
return true;
rejected:
debug_string(mathomatic, 1, "Polynomial factor rejected because too large.");
return false;
}
/*
* Remove level 1 trivial factors and divides from tlhs[].
* Makes it a simpler factor that looks a lot nicer and is easier to work with.
*
* Return true if result is a level 1 additive expression.
* If this returns false, nothing is removed.
*/
int
remove_factors(MathoMatic* mathomatic)
{
int i, j, k;
int plus_flag = false, divide_flag = false;
int op;
debug_string(mathomatic, 3, "Entering remove_factors() with: ");
side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
do {
simp_ssub(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, 0L, 1.0, false, true, 4);
} while (uf_power(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs));
for (i = 1, j = 0, k = 0;; i += 2) {
if (i >= mathomatic->n_tlhs) {
if (plus_flag && !divide_flag) {
if (k > 0)
j--;
blt(&mathomatic->scratch[k], &mathomatic->tlhs[j], (i - j) * sizeof(token_type));
k += i - j;
}
if (k <= 0) {
debug_string(mathomatic, 3, "Leaving remove_factors() with false return and no change.");
return false;
}
blt(mathomatic->tlhs, mathomatic->scratch, k * sizeof(token_type));
mathomatic->n_tlhs = k;
debug_string(mathomatic, 3, "Leaving remove_factors() with success and: ");
side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
return true;
}
op = mathomatic->tlhs[i].token.operatr;
switch (mathomatic->tlhs[i].level) {
case 1:
switch (op) {
case PLUS:
case MINUS:
plus_flag = true;
continue;
case TIMES:
case DIVIDE:
break;
default:
debug_string(mathomatic, 3, "Leaving remove_factors() with false return and no change.");
return false;
}
if (plus_flag && !divide_flag) {
if (k > 0)
j--;
blt(&mathomatic->scratch[k], &mathomatic->tlhs[j], (i - j) * sizeof(token_type));
k += i - j;
}
plus_flag = false;
divide_flag = (op == DIVIDE);
j = i + 1;
break;
case 2:
switch (op) {
case PLUS:
case MINUS:
plus_flag = true;
}
break;
}
}
}
/*
* This is the Euclidean GCD algorithm applied to polynomials.
* It needs to be made multivariate by making it recursive.
*
* Return the number of iterations (divisions), if successful,
* with the polynomial GCD result in gcd_divisor[] and len_d.
* Else return 0 or negative on failure. If 0, might work if operands switched,
* If a negative number, switching operands won't work either.
*/
static int
do_gcd(MathoMatic* mathomatic, long *vp)
//long *vp; /* polynomial base variable pointer */
{
int i;
int count;
for (count = 1; count < 50; count++) {
switch (poly_div(mathomatic, mathomatic->trhs, mathomatic->n_trhs, mathomatic->gcd_divisor, mathomatic->len_d, vp)) {
case 0:
/* divide failed */
return(1 - count);
case 2:
/* Total success! Remainder is zero. */
debug_string(mathomatic, 2, "Found raw polynomial GCD:");
side_debug(mathomatic, 2, mathomatic->gcd_divisor, mathomatic->len_d);
return count;
}
/* Do the Euclidean shuffle: swap trhs[] (remainder) and gcd_divisor[] */
if (mathomatic->len_d > mathomatic->n_tokens || mathomatic->n_trhs > DIVISOR_SIZE)
return 0;
blt(mathomatic->scratch, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
blt(mathomatic->trhs, mathomatic->gcd_divisor, mathomatic->len_d * sizeof(token_type));
blt(mathomatic->gcd_divisor, mathomatic->scratch, mathomatic->n_trhs * sizeof(token_type));
i = mathomatic->n_trhs;
mathomatic->n_trhs = mathomatic->len_d;
mathomatic->len_d = i;
}
return 0;
}
/*
* Compute the simplified and normalized polynomial Greatest Common Divisor
* of the expressions in "larger" and "smaller".
* This polynomial GCD routine is used for polynomial factoring, etc.
*
* Return a positive integer if successful.
* Return the GCD in trhs[].
* Return larger/GCD in tlhs[].
* The results are unfactored and simplified.
*/
int
poly_gcd(MathoMatic* mathomatic, token_type *larger, int llen, token_type *smaller, int slen, long v)
//token_type *larger; /* larger polynomial */
//int llen; /* larger polynomial length */
//token_type *smaller; /* smaller polynomial */
//int slen; /* smaller polynomial length */
//long v; /* polynomial base variable */
{
int count;
debug_string(mathomatic, 3, "Entering poly_gcd():");
side_debug(mathomatic, 3, larger, llen);
side_debug(mathomatic, 3, smaller, slen);
if (llen > mathomatic->n_tokens || slen > min(ARR_CNT(mathomatic->gcd_divisor), mathomatic->n_tokens))
return 0;
if (mathomatic->trhs != larger) {
blt(mathomatic->trhs, larger, llen * sizeof(token_type));
}
mathomatic->n_trhs = llen;
if (mathomatic->tlhs != smaller) {
blt(mathomatic->tlhs, smaller, slen * sizeof(token_type));
}
mathomatic->n_tlhs = slen;
if (!remove_factors(mathomatic))
return 0;
if (mathomatic->n_tlhs > ARR_CNT(mathomatic->gcd_divisor))
return 0;
blt(mathomatic->gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
mathomatic->len_d = mathomatic->n_tlhs;
count = do_gcd(mathomatic, &v);
if (count <= 0)
return 0;
if (count > 1) {
if (mathomatic->len_d > mathomatic->n_tokens)
return 0;
blt(mathomatic->tlhs, mathomatic->gcd_divisor, mathomatic->len_d * sizeof(token_type));
mathomatic->n_tlhs = mathomatic->len_d;
if (!remove_factors(mathomatic))
return 0;
if (mathomatic->n_tlhs > ARR_CNT(mathomatic->gcd_divisor))
return 0;
blt(mathomatic->gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
mathomatic->len_d = mathomatic->n_tlhs;
if (poly_div(mathomatic, larger, llen, mathomatic->gcd_divisor, mathomatic->len_d, &v) != 2) {
debug_string(mathomatic, 1, "Polynomial GCD found, but larger divide failed in poly_gcd().");
return 0;
}
}
if (mathomatic->len_d > mathomatic->n_tokens)
return 0;
blt(mathomatic->trhs, mathomatic->gcd_divisor, mathomatic->len_d * sizeof(token_type));
mathomatic->n_trhs = mathomatic->len_d;
uf_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
uf_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
debug_string(mathomatic, 3, "poly_gcd() successful.");
return(count);
}
/*
* Compute the polynomial Greatest Common Divisor of the expressions in "larger" and "smaller".
* This polynomial GCD routine is used by the division simplifiers.
*
* Return a positive integer if successful.
* Return larger/GCD in tlhs[].
* Return smaller/GCD in trhs[].
*/
int
poly2_gcd(MathoMatic* mathomatic, token_type *larger, int llen, token_type *smaller, int slen, long v, int require_additive)
//token_type *larger; /* larger polynomial */
//int llen; /* larger polynomial length */
//token_type *smaller; /* smaller polynomial */
//int slen; /* smaller polynomial length */
//long v; /* polynomial base variable */
//int require_additive; /* require the GCD to contain addition or subtraction */
{
int i;
int count;
#if 0
jmp_buf save_save;
#endif
if (require_additive) {
/* Require an additive operator in both polynomials to continue. */
count = 0;
for (i = 1; i < llen; i += 2) {
if (larger[i].token.operatr == PLUS || larger[i].token.operatr == MINUS) {
count++;
break;
}
}
if (count == 0)
return 0;
count = 0;
for (i = 1; i < slen; i += 2) {
if (smaller[i].token.operatr == PLUS || smaller[i].token.operatr == MINUS) {
count++;
}
}
if (count == 0 /* || count > 200 */)
return 0;
}
debug_string(mathomatic, 3, "Entering poly2_gcd():");
side_debug(mathomatic, 3, larger, llen);
side_debug(mathomatic, 3, smaller, slen);
if (llen > mathomatic->n_tokens || slen > min(ARR_CNT(mathomatic->gcd_divisor), mathomatic->n_tokens))
return 0;
blt(mathomatic->trhs, larger, llen * sizeof(token_type));
mathomatic->n_trhs = llen;
blt(mathomatic->tlhs, smaller, slen * sizeof(token_type));
mathomatic->n_tlhs = slen;
#if 0
if (require_additive) {
/* Require a level 1 additive operator in the divisor. */
blt(save_save, mathomatic->jmp_save, sizeof(mathomatic->jmp_save));
if ((i = setjmp(mathomatic->jmp_save)) != 0) { /* trap errors */
blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
if (i == 13) { /* critical error code */
longjmp(mathomatic->jmp_save, i);
}
return 0;
}
uf_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
if (level1_plus_count(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs) == 0)
return 0;
}
#endif
if (mathomatic->n_tlhs > ARR_CNT(mathomatic->gcd_divisor))
return 0;
blt(mathomatic->gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
mathomatic->len_d = mathomatic->n_tlhs;
count = do_gcd(mathomatic, &v);
if (count <= 0)
return count;
if (count > 1) {
if (require_additive && level1_plus_count(mathomatic, mathomatic->gcd_divisor, mathomatic->len_d) == 0)
return 0;
if (poly_div(mathomatic, smaller, slen, mathomatic->gcd_divisor, mathomatic->len_d, &v) != 2) {
debug_string(mathomatic, 1, "Polynomial GCD found, but smaller divide failed in poly2_gcd().");
return 0;
}
blt(mathomatic->trhs, mathomatic->gcd_divisor, mathomatic->len_d * sizeof(token_type));
mathomatic->n_trhs = mathomatic->len_d;
if (mathomatic->n_tlhs > ARR_CNT(mathomatic->gcd_divisor))
return 0;
blt(mathomatic->gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
mathomatic->len_d = mathomatic->n_tlhs;
blt(mathomatic->tlhs, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
mathomatic->n_tlhs = mathomatic->n_trhs;
if (poly_div(mathomatic, larger, llen, mathomatic->tlhs, mathomatic->n_tlhs, &v) != 2) {
debug_string(mathomatic, 1, "Polynomial GCD found, but larger divide failed in poly2_gcd().");
return 0;
}
blt(mathomatic->trhs, mathomatic->gcd_divisor, mathomatic->len_d * sizeof(token_type));
mathomatic->n_trhs = mathomatic->len_d;
} else {
mathomatic->n_trhs = 1;
mathomatic->trhs[0] = mathomatic->one_token;
}
debug_string(mathomatic, 3, "poly2_gcd() successful.");
return count;
}
/*
* This function returns true if the passed Mathomatic variable
* is of type integer.
*
* Integer variable names start with "integer".
*/
int
is_integer_var(MathoMatic* mathomatic, long v)
{
char *cp;
int (*strncmpfunc)();
if (mathomatic->case_sensitive_flag) {
strncmpfunc = strncmp;
} else {
strncmpfunc = strncasecmp;
}
cp = var_name(mathomatic, v);
if (cp && strncmpfunc(cp, V_INTEGER_PREFIX, strlen(V_INTEGER_PREFIX)) == 0)
return true;
else
return false;
}
/*
* This function is a strict test that
* returns true if passed expression is entirely integer and all variables
* are either "integer" or "sign".
*
* The result of evaluating the expression must be integer if this returns true.
* Should first be unfactored with uf_pplus() for a proper determination.
*/
int
is_integer_expr(MathoMatic* mathomatic, token_type *p1, int n)
//token_type *p1; /* expression pointer */
//int n; /* length of expression */
{
int i;
long v;
#if DEBUG
if (p1 == NULL || n < 1) {
error_bug(mathomatic, "(p1 == NULL || n < 1) in is_integer_expr().");
}
#endif
for (i = 0; i < n; i++) {
switch (p1[i].kind) {
case OPERATOR:
if (p1[i].token.operatr == DIVIDE)
return false;
break;
case CONSTANT:
if (fmod(p1[i].token.constant, 1.0) != 0.0)
return false;
break;
case VARIABLE:
v = labs(p1[i].token.variable);
if (!is_integer_var(mathomatic, v) && (v & VAR_MASK) != SIGN)
return false;
break;
}
}
return true;
}
/*
* This routine is the modulus operator (%) simplifier for equation sides.
* Using "integer" variables will allow more simplification here.
* Globals tlhs[] and trhs[] are wiped out by the polynomial division.
*
* Return true if equation side was modified.
*/
int
mod_simp(MathoMatic* mathomatic, token_type *equation, int *np)
//token_type *equation; /* pointer to the beginning of equation side to simplify */
//int *np; /* pointer to length of the equation side */
{
return mod_recurse(mathomatic, equation, np, 0, 1);
}
static int
mod_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level)
{
int modified = false;
int i, j, k;
int i1, i2, i3, i4, i5;
int op, last_op2;
int len1, len2, len3;
int diff_sign;
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
modified |= mod_recurse(mathomatic, equation, np, i, level + 1);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
continue;
}
i++;
}
if (modified) /* make sure the deepest levels are completed, first */
return true;
for (i = loc + 1; i < *np && equation[i].level >= level; i += 2) {
if (!(equation[i].level == level && equation[i].token.operatr == MODULUS))
continue;
for (k = i + 2;; k += 2) {
if (k >= *np || equation[k].level <= level)
break;
}
len1 = k - (i + 1);
last_op2 = 0;
for (j = loc; j < *np && equation[j].level >= level; j++) {
if (equation[j].level == level && equation[j].kind == OPERATOR) {
last_op2 = equation[j].token.operatr;
continue;
}
if (last_op2 == MODULUS) {
continue;
}
last_op2 = MODULUS;
op = 0;
for (i1 = k = j + 1; k < *np && equation[k].level > level; k += 2) {
if (equation[k].level == (level + 1)) {
op = equation[k].token.operatr;
i1 = k;
}
}
len2 = k - j;
switch (op) {
case MODULUS:
/* simplify (x%n)%n to x%n */
len3 = k - (i1 + 1);
if (se_compare(mathomatic, &equation[i+1], len1, &equation[i1+1], len3, &diff_sign)) {
blt(&equation[i1], &equation[k], (*np - k) * sizeof(token_type));
*np -= len3 + 1;
return true;
}
break;
case TIMES:
if (!is_integer_expr(mathomatic, &equation[j], len2))
break;
/* simplify (i%n*j)%n to (i*j)%n if j is integer */
for (i2 = i1 = j + 1;; i1 += 2) {
if (i1 >= k || equation[i1].level == (level + 1)) {
for (; i2 < i1; i2 += 2) {
if (equation[i2].level == (level + 2)
&& equation[i2].token.operatr == MODULUS) {
len3 = i1 - (i2 + 1);
if (se_compare(mathomatic, &equation[i+1], len1, &equation[i2+1], len3, &diff_sign)) {
blt(&equation[i2], &equation[i1], (*np - i1) * sizeof(token_type));
*np -= len3 + 1;
return true;
}
}
}
}
if (i1 >= k)
break;
}
break;
case PLUS:
case MINUS:
/* simplify (i%n+j)%n to (i+j)%n */
/* and ((i%n)*j+k)%n to (i*j+k)%n, when j is integer */
for (i2 = i1 = j + 1, i3 = j - 1;; i1 += 2) {
if (i1 >= k || equation[i1].level == (level + 1)) {
for (; i2 < i1; i2 += 2) {
if (equation[i2].level == (level + 2)) {
switch (equation[i2].token.operatr) {
case MODULUS:
len3 = i1 - (i2 + 1);
if (se_compare(mathomatic, &equation[i+1], len1, &equation[i2+1], len3, &diff_sign)) {
blt(&equation[i2], &equation[i1], (*np - i1) * sizeof(token_type));
*np -= len3 + 1;
return true;
}
break;
case TIMES:
i2 = i1 - 2;