-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcmds.c
4789 lines (4639 loc) · 111 KB
/
cmds.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 commands that don't belong anywhere else.
*
* 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 OPT_MIN_SIZE 7 /* Minimum size (in tokens) of repeated expressions to find in optimize command. */
enum spf_function {
SUM_COMMAND,
PRODUCT_COMMAND,
FOR_COMMAND
};
static int sum_product(char *cp, enum spf_function current_function);
static int complex_func(char *cp, int imag_flag);
static int elim_sub(int i, long v);
/* Global variables for the optimize command. */
static int opt_en[N_EQUATIONS+1];
static int last_temp_var = 0;
#if SHELL_OUT
/*
* The plot command.
*
* All command functions like this return true if successful, or false for failure.
*/
int
plot_cmd(cp)
char *cp; /* the command-line argument */
{
#define APPEND(str) { if (strlen(str) + cl1_len < sizeof(cl1)) { strcpy(&cl1[cl1_len], str); cl1_len += strlen(str); } else warning(_("Expression too large to plot; omitted.")); }
int i1, i2;
int start, stop;
int first_time = true;
int cl1_len = 0, cl2_len = 0, len;
char *cp1, cl[16384+MAX_CMD_LEN], cl1[16384], cl2[MAX_CMD_LEN], *exp_str;
long v, vx; /* Mathomatic variables */
token_type *equation;
int *np;
int ev; /* exit value */
int cur_equation_flag = false;
if (security_level > 0) {
show_usage = false;
error(_("Command disabled by security level."));
return false;
}
if (!parse_var(&vx, "x")) {
return false;
}
do {
cp1 = cp;
if (!get_range(&cp, &start, &stop)) {
reset_error();
break;
}
if (cp != cp1 || first_time) {
if (cp == cp1)
cur_equation_flag = (!empty_equation_space(cur_equation));
for (i1 = start; i1 <= stop; i1++) {
if (i1 != cur_equation) {
cur_equation_flag = false;
}
if (n_lhs[i1] > 0) {
v = 0;
if (n_rhs[i1]) {
equation = rhs[i1];
np = &n_rhs[i1];
} else {
equation = lhs[i1];
np = &n_lhs[i1];
}
if (!no_vars(equation, *np, &v) && v) {
if (strcmp(var_name(v), "x") && strcmp(var_name(v), "t")) {
list_var(v, 0);
fprintf(gfp, _("#%d: Renaming variable %s to x for gnuplot.\n"), i1 + 1, var_str);
rename_var_in_es(i1, v, vx);
}
}
if (n_rhs[i1] && !solved_equation(i1)) {
warning(_("Not a normally solved equation, plotting the RHS only."));
}
for (i2 = 0; i2 < *np; i2 += 2) {
if (equation[i2].kind == VARIABLE && (equation[i2].token.variable & VAR_MASK) == SIGN) {
warning(_("Plot expression contains sign variables; try \"simplify sign\" before plotting."));
break;
}
}
exp_str = list_expression(equation, *np, 3);
if (exp_str == NULL)
return false;
if (cl1_len) {
APPEND(", ");
}
APPEND(exp_str);
free(exp_str);
}
}
}
first_time = false;
} while (*cp && cp != cp1);
cl1[cl1_len] = '\0';
if (cl1_len == 0 && *cp == '\0') {
error(_("No plot expression specified."));
return false;
}
for (cl2_len = 0, i2 = 0; cp[i2]; i2++) {
if ((cl2_len + 2) >= sizeof(cl2)) {
error(_("Command-line too long."));
return false;
}
switch (cp[i2]) {
case '^':
cl2[cl2_len] = '*';
cl2_len++;
cl2[cl2_len] = '*';
cl2_len++;
break;
default:
cl2[cl2_len] = cp[i2];
cl2_len++;
break;
}
}
cl2[cl2_len] = '\0';
if (cl1_len && cl2_len && cl2[cl2_len-1] != ',') {
if (cur_equation_flag) {
snprintf(prompt_str, sizeof(prompt_str), _("Do you want to plot the current equation, too (y/n)? "));
if (!get_yes_no()) {
cl1_len = 0;
cl1[cl1_len] = '\0';
}
}
if (cl1_len) {
snprintf(prompt_str, sizeof(prompt_str), _("Does the plot command-line consist of any expressions (y/n)? "));
if (get_yes_no()) {
fprintf(gfp, _("Appending a comma to the command-line.\n"));
if ((cl2_len + 2) >= sizeof(cl2)) {
error(_("Command-line too long."));
return false;
}
cl2[cl2_len++] = ',';
cl2[cl2_len] = '\0';
}
}
}
if (strchr(cl2, 'y') || strchr(cl1, 'y')) {
fprintf(gfp, _("Performing 3D surface plot...\n"));
#if MINGW
len = snprintf(cl, sizeof(cl), "gnuplot -persist -e \"%s; splot %s %s\"", plot_prefix, cl2, cl1);
#else
len = snprintf(cl, sizeof(cl), "echo '%s; splot %s %s'|gnuplot -persist", plot_prefix, cl2, cl1);
#endif
} else {
fprintf(gfp, _("Performing 2D plot...\n"));
#if MINGW
len = snprintf(cl, sizeof(cl), "gnuplot -persist -e \"%s; plot %s %s\"", plot_prefix, cl2, cl1);
#else
len = snprintf(cl, sizeof(cl), "echo '%s; plot %s %s'|gnuplot -persist", plot_prefix, cl2, cl1);
#endif
}
if (len >= sizeof(cl)) {
error(_("gnuplot command-line too long."));
return false;
}
ev = shell_out(cl);
if (ev) {
error(_("Possible error running gnuplot."));
printf(_("Decimal exit value = %d\n"), ev);
if (ev == 256 || ev == 1) {
printf(_("Try separating each expression with a comma.\n"));
}
printf(_("Shell command-line = %s\n"), cl);
}
return true;
}
#endif
/*
* The version command.
*
* All commands return true if successful.
*/
int
version_cmd(cp)
char *cp; /* the command-line argument */
{
int rv = true; /* return value */
int status_flag = false;
if (strncasecmp(cp, "status", 4) == 0) {
status_flag = true;
cp = skip_param(cp);
}
if (extra_characters(cp)) /* Make sure nothing else is on the command-line. */
return false;
#if LIBRARY
free_result_str();
result_str = strdup(VERSION);
#endif
if (status_flag) {
rv = version_report();
} else {
#if !SILENT || !LIBRARY
fprintf(gfp, _("Mathomatic version %s\n"), VERSION);
#endif
}
if (status_flag) {
debug_string(0, "\nMathomatic is GNU LGPL version 2.1 licensed software,\n"
"meaning it is free software that comes with no warranty.\n"
"Type \"help license\" for the copyright and license.");
EP(_("\nFor all new stuff, visit the Mathomatic website: www.mathomatic.org"));
}
#if LIBRARY
return(rv && result_str != NULL);
#else
return rv;
#endif
}
/*
* Return the maximum amount of memory (in bytes) that this program will use.
*/
long
max_memory_usage(void)
{
return((long) (N_EQUATIONS + 3L) * (long) n_tokens * sizeof(token_type) * 2L);
}
/*
* Try the function getrusage(2).
*
* Return true if successful.
*/
int
show_status(ofp)
FILE *ofp;
{
#if SHOW_RESOURCES
struct rusage usage_local;
if (getrusage(RUSAGE_SELF, &usage_local) == 0) {
fprintf(ofp, _("Total CPU usage, user time: %g seconds, system time: %g seconds.\n"),
(double) usage_local.ru_utime.tv_sec + ((double) usage_local.ru_utime.tv_usec / 1000000.0),
(double) usage_local.ru_stime.tv_sec + ((double) usage_local.ru_stime.tv_usec / 1000000.0));
if (usage_local.ru_ixrss == 0 && usage_local.ru_idrss == 0 && usage_local.ru_isrss == 0) {
if (usage_local.ru_maxrss)
fprintf(ofp, _("Total RSS size: %ld kilobytes.\n"), usage_local.ru_maxrss);
} else {
fprintf(ofp, _("Total RSS size: %ld kbytes; shared text memory size: %ld kbytes*ticks;\n"), usage_local.ru_maxrss, usage_local.ru_ixrss);
fprintf(ofp, _("Unshared data size: %ld kbytes*ticks; unshared stack size: %ld kbytes*ticks.\n"), usage_local.ru_idrss, usage_local.ru_isrss);
fprintf(ofp, _("Number of times Mathomatic was swapped out: %ld; signals received: %ld.\n"), usage_local.ru_nswap, usage_local.ru_nsignals);
}
return true;
}
#endif
return false;
}
/*
* Display version and status info.
*/
int
version_report(void)
{
long l;
fprintf(gfp, _("Mathomatic version %s\n"), VERSION);
fprintf(gfp, _("The last main prompt return value is %d (meaning "), previous_return_value);
switch (previous_return_value) {
case 0:
fprintf(gfp, _("failure).\n"));
break;
default:
fprintf(gfp, _("success).\n"));
break;
}
show_status(gfp);
fprintf(gfp, _("\nCompile-time defines used: "));
#if linux
fprintf(gfp, "linux ");
#endif
#if sun
fprintf(gfp, "sun ");
#endif
#if UNIX
fprintf(gfp, "UNIX ");
#endif
#if CYGWIN
fprintf(gfp, "CYGWIN ");
#endif
#if MINGW
fprintf(gfp, "MINGW ");
#endif
#if HANDHELD
fprintf(gfp, "HANDHELD ");
#endif
#if EDITLINE
fprintf(gfp, "EDITLINE ");
#endif
#if READLINE
fprintf(gfp, "READLINE ");
#endif
#if SILENT
fprintf(gfp, "SILENT ");
#endif
#if LIBRARY
fprintf(gfp, "LIBRARY ");
#endif
#if SECURE
fprintf(gfp, "SECURE ");
#endif
#if TIMEOUT_SECONDS
fprintf(gfp, "TIMEOUT_SECONDS=%d ", TIMEOUT_SECONDS);
#endif
#if I18N
fprintf(gfp, "I18N ");
#endif
#if NO_COLOR
fprintf(gfp, "NO_COLOR ");
#endif
#if BOLD_COLOR
fprintf(gfp, "BOLD_COLOR ");
#endif
#if WIN32_CONSOLE_COLORS
fprintf(gfp, "WIN32_CONSOLE_COLORS ");
#endif
#if NOGAMMA
fprintf(gfp, "NOGAMMA ");
#endif
#if USE_TGAMMA
fprintf(gfp, "USE_TGAMMA ");
#endif
#if DEBUG
fprintf(gfp, "DEBUG ");
#endif
#if VALGRIND
fprintf(gfp, "VALGRIND ");
#endif
#if SHOW_RESOURCES
fprintf(gfp, "SHOW_RESOURCES ");
#endif
fprintf(gfp, "\nsizeof(int) = %u bytes, sizeof(long) = %u bytes.\n", (unsigned) sizeof(int), (unsigned) sizeof(long));
fprintf(gfp, "sizeof(double) = %u bytes, maximum double precision = %d decimal digits.\n", (unsigned) sizeof(double), DBL_DIG);
#ifdef __VERSION__
#ifdef __GNUC__
fprintf(gfp, "GNU ");
#endif
fprintf(gfp, _("C Compiler version: %s\n"), __VERSION__);
#endif
fprintf(gfp, _("\n%d equation spaces currently allocated.\n"), n_equations);
fprintf(gfp, _("The current expression array size is %d tokens,\n"), n_tokens);
l = max_memory_usage() / 1000L;
if (l >= 10000L) {
fprintf(gfp, _("making the maximum memory usage approximately %ld megabytes.\n"), l / 1000L);
} else {
fprintf(gfp, _("making the maximum memory usage approximately %ld kilobytes.\n"), l);
}
#if SECURE
fprintf(gfp, _("Compiled for maximum security.\n"));
#else
fprintf(gfp, _("The current security level is %d"), security_level);
switch (security_level) {
case -1:
fprintf(gfp, _(", meaning you are running m4 Mathomatic.\n"));
break;
case 0:
fprintf(gfp, _(", no security, meaning users are unrestricted.\n"));
break;
case 1:
case 2:
fprintf(gfp, _(", some security.\n"));
break;
case 3:
fprintf(gfp, _(", high security.\n"));
break;
case 4:
fprintf(gfp, _(", maximum security.\n"));
break;
default:
fprintf(gfp, _(", unknown meaning.\n"));
break;
}
#endif
#if READLINE || EDITLINE
#if READLINE
fprintf(gfp, _("\nreadline is compiled in and "));
#else
fprintf(gfp, _("\neditline is compiled in and "));
#endif
if (readline_enabled) {
fprintf(gfp, _("activated.\n"));
} else {
fprintf(gfp, _("deactivated.\n"));
}
#elif !LIBRARY && !HANDHELD
#if MINGW
SP(_("\nreadline is not compiled in, however some of its functionality"));
SP(_("already exists in the Windows console for any"));
EP(_("Windows console program (like Mathomatic)."));
#else
SP(_("\nreadline is not compiled in."));
SP(_("Please notify the package maintainer that readline"));
EP(_("should be compiled into Mathomatic, with \"make READLINE=1\"."));
#endif
#endif
return true;
}
/*
* The solve command.
*
* Return 0 on solve failure for any equation which a solve was requested, or something was not verifiable,
* with the "solve verifiable" option.
* Return 1 on total success (all requested solves completed successfully),
* or 2 if partial success (all solved, but some solutions didn't verify when doing "solve verify",
* or the result contains infinity or NaN).
*/
int
solve_cmd(cp)
char *cp;
{
int i, j, k;
int start, stop;
char buf[MAX_CMD_LEN];
int diff_sign;
int verify_flag = 0, plural_flag, once_through, contains_infinity, did_something = false, last_solve_successful = false;
char *cp1, *cp_start;
token_type want;
int rv = 1;
long pre_v; /* Mathomatic variable */
cp_start = cp;
if (strcmp_tospace(cp, "verify") == 0) {
verify_flag = 1;
cp = skip_param(cp);
} else if (strcmp_tospace(cp, "verifiable") == 0) {
verify_flag = 2;
cp = skip_param(cp);
}
if (!get_range(&cp, &start, &stop)) {
warning(_("No equations to solve."));
return false;
}
i = next_espace();
repeat_flag = true;
if (strcmp_tospace(cp, "verify") == 0) {
verify_flag = 1;
cp = skip_param(cp);
} else if (strcmp_tospace(cp, "verifiable") == 0) {
verify_flag = 2;
cp = skip_param(cp);
}
if (strcmp_tospace(cp, "for") == 0) {
cp1 = skip_param(cp);
if (*cp1) {
cp = cp1;
}
}
if (*cp == '\0') {
my_strlcpy(prompt_str, _("Enter variable to solve for: "), sizeof(prompt_str));
if ((cp = get_string(buf, sizeof(buf))) == NULL) {
return false;
}
cp_start = cp;
}
input_column += (cp - cp_start);
if ((cp = parse_equation(i, cp)) == NULL) {
return false;
}
if (verify_flag) {
if (n_lhs[i] != 1 || n_rhs[i] != 0 || (lhs[i][0].kind != VARIABLE
&& (lhs[i][0].kind != CONSTANT || lhs[i][0].token.constant != 0.0))) {
error(_("Can only verify for a single solve variable or identities after solving for 0."));
goto fail;
}
want = lhs[i][0];
}
show_usage = false;
for (k = start; k <= stop; k++) {
if (k == i || n_lhs[k] <= 0 || n_rhs[k] <= 0) {
continue;
}
last_solve_successful = false;
cur_equation = k;
did_something = true;
if (verify_flag) {
pre_v = 0;
fprintf(gfp, _("Solving equation #%d for "), cur_equation + 1);
list_proc(&want, 1, false);
if (verify_flag == 2) {
fprintf(gfp, " with required ");
} else {
fprintf(gfp, " with ");
}
if (want.kind == VARIABLE) {
fprintf(gfp, "verification...\n");
if (solved_equation(cur_equation)) {
pre_v = lhs[cur_equation][0].token.variable;
}
} else {
fprintf(gfp, "identity verification...\n");
}
copy_espace(cur_equation, i);
if (solve_sub(&want, 1, lhs[cur_equation], &n_lhs[cur_equation], rhs[cur_equation], &n_rhs[cur_equation]) > 0) {
simpa_repeat(cur_equation, true, false); /* Solve result should be quick simplified. */
last_solve_successful = true;
debug_string(0, _("Solve and \"repeat simplify quick\" successful:"));
if (!return_result(cur_equation)) { /* Display the simplified solve result. */
goto fail;
}
if (want.kind == VARIABLE) {
if (!solved_equation(cur_equation) || lhs[cur_equation][0].token.variable != want.token.variable) {
error(_("Result not a properly solved equation, so cannot verify."));
continue;
}
if (pre_v && pre_v == want.token.variable) {
warning(_("Equation was already solved, so no need to verify solutions."));
continue;
}
} else {
copy_espace(cur_equation, i);
}
plural_flag = false;
for (j = 0; j < n_rhs[cur_equation]; j += 2) {
if (rhs[cur_equation][j].kind == VARIABLE && (rhs[cur_equation][j].token.variable & VAR_MASK) == SIGN) {
plural_flag = true;
break;
}
}
if (want.kind == VARIABLE) {
subst_var_with_exp(lhs[i], &n_lhs[i], rhs[cur_equation], n_rhs[cur_equation], want.token.variable);
subst_var_with_exp(rhs[i], &n_rhs[i], rhs[cur_equation], n_rhs[cur_equation], want.token.variable);
}
once_through = false;
calc_simp(lhs[i], &n_lhs[i]);
calc_simp(rhs[i], &n_rhs[i]);
check_result:
contains_infinity = (exp_contains_infinity(lhs[i], n_lhs[i])
|| exp_contains_infinity(rhs[i], n_rhs[i]));
if (se_compare(lhs[i], n_lhs[i], rhs[i], n_rhs[i], &diff_sign) && (want.kind != VARIABLE || !diff_sign)) {
if (want.kind != VARIABLE) {
fprintf(gfp, _("This equation is an identity.\n"));
} else if (plural_flag)
fprintf(gfp, _("All solutions verified.\n"));
else
fprintf(gfp, _("Solution verified.\n"));
if (contains_infinity) {
error(_("Solution might be incorrect because it contains infinity or NaN."));
if (rv)
rv = 2;
}
} else {
if (!contains_infinity && once_through < 2) {
symb_flag = symblify;
simpa_repeat(i, once_through ? false : true, once_through ? true : false); /* Simplify to compare equation sides. */
symb_flag = false;
once_through++;
goto check_result;
}
if (contains_infinity) {
error(_("Solution might be incorrect because it contains infinity or NaN."));
} else {
if (want.kind != VARIABLE) {
error(_("This equation is NOT an identity."));
} else if (plural_flag)
error(_("Unable to verify all solutions."));
else
error(_("Unable to verify solution."));
}
if (verify_flag == 2)
rv = 0;
else if (rv)
rv = 2;
}
} else {
printf(_("Solve failed for equation space #%d.\n"), cur_equation + 1);
rv = 0;
}
} else {
if (solve_espace(i, cur_equation)) {
last_solve_successful = true;
if (!return_result(cur_equation)) {
goto fail;
}
} else {
rv = 0;
}
}
}
if (did_something) {
if (last_solve_successful && verify_flag) {
debug_string(1, "Verification identity:");
list_esdebug(1, i);
}
} else {
printf(_("No work done.\n"));
}
n_lhs[i] = 0;
n_rhs[i] = 0;
return rv;
fail:
n_lhs[i] = 0;
n_rhs[i] = 0;
return 0;
}
/*
* The sum command.
*/
int
sum_cmd(cp)
char *cp;
{
return sum_product(cp, SUM_COMMAND);
}
/*
* The product command.
*/
int
product_cmd(cp)
char *cp;
{
return sum_product(cp, PRODUCT_COMMAND);
}
/*
* The for command.
*/
int
for_cmd(cp)
char *cp;
{
return sum_product(cp, FOR_COMMAND);
}
/*
* Common function for the sum and product commands.
*/
static int
sum_product(cp, current_function)
char *cp; /* the command-line */
enum spf_function current_function;
{
int i;
long v = 0; /* Mathomatic variable */
double start, end, step = 1.0;
int result_equation;
int n, ns;
token_type *dest, *source;
int count_down; /* if true, count down, otherwise count up */
char *cp1, buf[MAX_CMD_LEN];
if (current_not_defined()) {
return false;
}
result_equation = next_espace();
if (n_rhs[cur_equation]) {
ns = n_rhs[cur_equation];
source = rhs[cur_equation];
dest = rhs[result_equation];
} else {
ns = n_lhs[cur_equation];
source = lhs[cur_equation];
dest = lhs[result_equation];
}
if (*cp) {
cp = parse_var2(&v, cp);
if (cp == NULL) {
return false;
}
}
if (no_vars(source, ns, &v)) {
error(_("Current expression contains no variables."));
return false;
}
if (v == 0) {
if (!prompt_var(&v)) {
return false;
}
}
if (!found_var(source, ns, v)) {
error(_("Specified variable not found."));
return false;
}
if (*cp) {
if (*cp == '=') {
cp++;
}
cp1 = cp;
} else {
list_var(v, 0);
snprintf(prompt_str, sizeof(prompt_str), "%s = ", var_str);
if ((cp1 = get_string(buf, sizeof(buf))) == NULL)
return false;
}
start = strtod(cp1, &cp);
if (cp1 == cp || !isfinite(start)) {
error(_("Number expected."));
return false;
}
if (fabs(start) >= MAX_K_INTEGER) {
error(_("Number too large."));
return false;
}
cp = skip_comma_space(cp);
if (strcmp_tospace(cp, "to") == 0) {
cp = skip_param(cp);
}
if (*cp) {
cp1 = cp;
} else {
my_strlcpy(prompt_str, _("To: "), sizeof(prompt_str));
if ((cp1 = get_string(buf, sizeof(buf))) == NULL)
return false;
}
end = strtod(cp1, &cp);
if (cp1 == cp || !isfinite(end)) {
error(_("Number expected."));
return false;
}
if (fabs(end) >= MAX_K_INTEGER) {
error(_("Number too large."));
return false;
}
cp = skip_comma_space(cp);
if (strcmp_tospace(cp, "step") == 0) {
cp = skip_param(cp);
}
if (*cp) {
cp1 = cp;
step = fabs(strtod(cp1, &cp));
if (cp1 == cp || !isfinite(step) || step <= 0.0 || step >= MAX_K_INTEGER) {
error(_("Invalid step."));
return false;
}
}
if (extra_characters(cp))
return false;
count_down = (end < start);
if (fmod(fabs(start - end) / step, 1.0) != 0.0) {
warning(_("End value not reached."));
}
if (current_function == PRODUCT_COMMAND) {
dest[0] = one_token;
} else {
dest[0] = zero_token;
}
n = 1;
for (; count_down ? (start >= end) : (start <= end); count_down ? (start -= step) : (start += step)) {
if (n + 1 + ns > n_tokens) {
error_huge();
}
blt(tlhs, source, ns * sizeof(token_type));
n_tlhs = ns;
for (i = 0; i < n_tlhs; i += 2) {
if (tlhs[i].kind == VARIABLE && tlhs[i].token.variable == v) {
tlhs[i].kind = CONSTANT;
tlhs[i].token.constant = start;
}
}
if (current_function != FOR_COMMAND) {
for (i = 0; i < n_tlhs; i++) {
tlhs[i].level++;
}
for (i = 0; i < n; i++) {
dest[i].level++;
}
dest[n].kind = OPERATOR;
dest[n].level = 1;
}
switch (current_function) {
case PRODUCT_COMMAND:
dest[n].token.operatr = TIMES;
n++;
break;
case SUM_COMMAND:
dest[n].token.operatr = PLUS;
n++;
break;
case FOR_COMMAND:
n = 0;
break;
}
blt(&dest[n], tlhs, n_tlhs * sizeof(token_type));
n += n_tlhs;
calc_simp(dest, &n);
if (current_function == FOR_COMMAND) {
list_var(v, 0);
fprintf(gfp, "%s = %.*g: ", var_str, precision, start);
list_factor(dest, &n, false);
fprintf(gfp, "\n");
} else {
side_debug(1, dest, n);
}
}
if (current_function == FOR_COMMAND) {
return true;
} else {
if (n_rhs[cur_equation]) {
n_rhs[result_equation] = n;
blt(lhs[result_equation], lhs[cur_equation], n_lhs[cur_equation] * sizeof(token_type));
n_lhs[result_equation] = n_lhs[cur_equation];
} else {
n_lhs[result_equation] = n;
}
return return_result(result_equation);
}
}
/*
* This function is for the "optimize" command.
* It finds and substitutes all occurrences of the RHS of "en" in "equation".
* It should be called repeatedly until it returns false.
*/
static int
find_more(equation, np, en)
token_type *equation; /* expression to search */
int *np; /* pointer to length of expression */
int en; /* equation space number */
{
int i, j, k;
int level;
int diff_sign;
int found_se; /* found sub-expression flag */
if (*np <= 0 || !solved_equation(en)) {
return false;
}
for (level = 1, found_se = true; found_se; level++) {
for (i = 1, found_se = false; i < *np; i = j + 2) {
for (j = i; j < *np && equation[j].level >= level; j += 2)
;
if (j == i) {
continue;
}
found_se = true;
k = i - 1;
if (se_compare(&equation[k], j - k, rhs[en], n_rhs[en], &diff_sign)) {
if (diff_sign) {
blt(&equation[i+2], &equation[j], (*np - j) * sizeof(token_type));
*np -= (j - (i + 2));
level++;
equation[k].level = level;
equation[k].kind = CONSTANT;
equation[k].token.constant = -1.0;
k++;
equation[k].level = level;
equation[k].kind = OPERATOR;
equation[k].token.operatr = TIMES;
k++;
} else {
blt(&equation[i], &equation[j], (*np - j) * sizeof(token_type));
*np -= (j - i);
}
equation[k].level = level;
equation[k].kind = VARIABLE;
equation[k].token.variable = lhs[en][0].token.variable;
return true;
}
}
}
return false;
}
/*
* This function is for the "optimize" command.
* It finds and replaces all repeated expressions in "equation" with temporary variables.
* It also creates a new equation for each temporary variable.
* It should be called repeatedly until it returns false.
*/
static int
opt_es(equation, np)
token_type *equation;
int *np;
{
int i, j, k, i1, i2, jj1, k1;
int level, level1;
int diff_sign;
int found_se, found_se1; /* found sub-expression flags */
long v; /* Mathomatic variable */
char var_name_buf[MAX_VAR_LEN];
if (*np <= 0) {
return false;
}
for (level = 1, found_se = true; found_se; level++) {
for (i = 1, found_se = false; i < *np; i = j + 2) {
for (j = i; j < *np && equation[j].level > level; j += 2)
;
if (j == i) {
continue;
}
found_se = true;
k = i - 1;
if ((j - k) < OPT_MIN_SIZE) {
continue;
}
found_se1 = true;
for (level1 = 1; found_se1; level1++) {
for (i1 = 1, found_se1 = false; i1 < *np; i1 = jj1 + 2) {
for (jj1 = i1; jj1 < *np && equation[jj1].level > level1; jj1 += 2) {
}
if (jj1 == i1) {
continue;
}
found_se1 = true;
if (i1 <= j)
continue;
k1 = i1 - 1;
if ((jj1 - k1) >= OPT_MIN_SIZE
&& se_compare(&equation[k], j - k, &equation[k1], jj1 - k1, &diff_sign)) {
snprintf(var_name_buf, sizeof(var_name_buf), "temp%.0d", last_temp_var);
if (parse_var(&v, var_name_buf) == NULL) {
return false; /* can't create "temp" variable */
}
last_temp_var++;
if (last_temp_var < 0) {
last_temp_var = 0;
}
i2 = next_espace();
lhs[i2][0].level = 1;
lhs[i2][0].kind = VARIABLE;
lhs[i2][0].token.variable = v;
n_lhs[i2] = 1;
blt(rhs[i2], &equation[k], (j - k) * sizeof(token_type));
n_rhs[i2] = j - k;
if (diff_sign) {
blt(&equation[i1+2], &equation[jj1], (*np - jj1) * sizeof(token_type));
*np -= (jj1 - (i1 + 2));
level1++;
equation[k1].level = level1;
equation[k1].kind = CONSTANT;
equation[k1].token.constant = -1.0;
k1++;
equation[k1].level = level1;
equation[k1].kind = OPERATOR;
equation[k1].token.operatr = TIMES;
k1++;
} else {
blt(&equation[i1], &equation[jj1], (*np - jj1) * sizeof(token_type));
*np -= (jj1 - i1);
}
equation[k1].level = level1;
equation[k1].kind = VARIABLE;
equation[k1].token.variable = v;
blt(&equation[i], &equation[j], (*np - j) * sizeof(token_type));
*np -= j - i;
equation[k].level = level;
equation[k].kind = VARIABLE;
equation[k].token.variable = v;
while (find_more(equation, np, i2))
;
simp_loop(rhs[i2], &n_rhs[i2]);
simp_loop(equation, np);
for (i = 0;; i++) {
if (i >= N_EQUATIONS) {
error_bug("Too many optimized equations.");
}
if (opt_en[i] < 0)
break;
}
opt_en[i] = i2;
opt_en[i+1] = -1;
return true;
}
}
}
}
}
return false;