#endif
@@ -81,15 +83,16 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
HANDLE hOut;
#endif
+MathoMatic *mathomatic;
+
/*
* Display invocation usage info.
*/
void
-usage(fp)
-FILE *fp;
+usage(FILE *fp)
{
fprintf(fp, _("Mathomatic computer algebra system, version %s\n"), VERSION);
- fprintf(fp, _("Usage: %s [ options ] [ input_files or input ]\n\n"), prog_name);
+ fprintf(fp, _("Usage: %s [ options ] [ input_files or input ]\n\n"), mathomatic->prog_name);
fprintf(fp, _("Options:\n"));
fprintf(fp, _(" -a Enable alternative color mode.\n"));
fprintf(fp, _(" -b Enable bold color mode.\n"));
@@ -134,45 +137,46 @@ char **argv;
#if I18N
/* Initialize internationalization so that messages are in the right language. */
setlocale(LC_ALL, "");
- bindtextdomain(prog_name, LOCALEDIR);
- textdomain(prog_name);
+ bindtextdomain(mathomatic->prog_name, LOCALEDIR);
+ textdomain(mathomatic->prog_name);
#endif
#if CYGWIN || MINGW
dir_path = strdup(dirname_win(argv[0])); /* set dir_path to this executable's directory */
#endif
+ mathomatic = newtMathoMatic();
/* initialize the global variables */
- init_gvars();
- default_out = stdout; /* set default_out to any file you want to redirect output to */
- gfp = default_out;
- get_screen_size();
+ init_gvars(mathomatic);
+ mathomatic->default_out = stdout; /* set default_out to any file you want to redirect output to */
+ mathomatic->gfp = mathomatic->default_out;
+ get_screen_size(mathomatic);
/* process command line options */
while ((i = getopt(argc, argv, "s:abqrtdchuvwxm:e")) >= 0) {
switch (i) {
case 's':
if (optarg) {
- security_level = strtod(optarg, &cp);
+ mathomatic->security_level = strtod(optarg, &cp);
#if SECURE
- if (security_level != 4) {
- fprintf(stderr, _("%s: Already compiled for maximum security (level 4), therefore setting security level ignored.\n"), prog_name);
+ if (mathomatic->security_level != 4) {
+ fprintf(stderr, _("%s: Already compiled for maximum security (level 4), therefore setting security level ignored.\n"), mathomatic->prog_name);
}
#endif
}
if (optarg == NULL || cp == NULL || (cp == optarg && *cp != ':') || (*cp != '\0' && *cp != ':')) {
- fprintf(stderr, _("%s: Error in setting security level.\n"), prog_name);
+ fprintf(stderr, _("%s: Error in setting security level.\n"), mathomatic->prog_name);
exit(2);
}
if (*cp == ':') {
time_out_seconds = strtol(cp + 1, &cp, 10);
if (!(*cp == '\0' && time_out_seconds > 0)) {
- fprintf(stderr, _("%s: Error in setting time out seconds.\n"), prog_name);
+ fprintf(stderr, _("%s: Error in setting time out seconds.\n"), mathomatic->prog_name);
exit(2);
}
}
#if !SECURE
if (time_out_seconds > 0) {
- printf(_("Security level is %d, time out seconds is %d.\n"), security_level, time_out_seconds);
+ printf(_("Security level is %d, time out seconds is %d.\n"), mathomatic->security_level, time_out_seconds);
}
#endif
break;
@@ -189,34 +193,34 @@ char **argv;
coption++;
break;
case 'x':
- html_flag = 1;
+ mathomatic->html_flag = 1;
wide_flag = true;
break;
case 'm':
if (optarg)
new_size = strtod(optarg, &cp) * DEFAULT_N_TOKENS;
if (optarg == NULL || cp == NULL || *cp || new_size <= 0 || new_size >= (INT_MAX / sizeof(token_type))) {
- fprintf(stderr, _("%s: Invalid memory size multiplier specified.\n"), prog_name);
+ fprintf(stderr, _("%s: Invalid memory size multiplier specified.\n"), mathomatic->prog_name);
exit(2);
}
- n_tokens = (int) new_size;
+ mathomatic->n_tokens = (int) new_size;
break;
case 'q':
- quiet_mode = true;
+ mathomatic->quiet_mode = true;
break;
case 'r':
- readline_enabled = false;
+ mathomatic->readline_enabled = false;
break;
case 't':
- readline_enabled = false;
+ mathomatic->readline_enabled = false;
wide_flag = true;
- test_mode = true;
+ mathomatic->test_mode = true;
break;
case 'd':
- demo_mode = true;
+ mathomatic->demo_mode = true;
break;
case 'u':
- echo_input = true;
+ mathomatic->echo_input = true;
setbuf(stdout, NULL); /* make output unbuffered */
setbuf(stderr, NULL);
break;
@@ -228,30 +232,30 @@ char **argv;
printf(_("Mathomatic version %s\n"), VERSION);
exit(0);
case 'e':
- eoption = true;
- autoselect = false;
+ mathomatic->eoption = true;
+ mathomatic->autoselect = false;
break;
default:
usage(stdout);
exit(2);
}
}
- if (n_tokens < 100 || n_tokens >= (INT_MAX / sizeof(token_type))) {
- fprintf(stderr, _("%s: Standard expression array size %d out of range!\n"), prog_name, n_tokens);
+ if (mathomatic->n_tokens < 100 || mathomatic->n_tokens >= (INT_MAX / sizeof(token_type))) {
+ fprintf(stderr, _("%s: Standard expression array size %d out of range!\n"), mathomatic->prog_name, mathomatic->n_tokens);
}
- if (!init_mem()) {
- fprintf(stderr, _("%s: Not enough memory.\n"), prog_name);
+ if (!init_mem(mathomatic)) {
+ fprintf(stderr, _("%s: Not enough memory.\n"), mathomatic->prog_name);
exit(2);
}
#if READLINE
- if (readline_enabled) { /* readline_enabled flag must not change after this */
+ if (mathomatic->readline_enabled) { /* readline_enabled flag must not change after this */
if ((cp = getenv("HOME"))) {
#if MINGW
- snprintf(history_filename_storage, sizeof(history_filename_storage), "%s/matho_history", cp);
+ snprintf(mathomatic->history_filename_storage, sizeof(mathomatic->history_filename_storage), "%s/matho_history", cp);
#else
- snprintf(history_filename_storage, sizeof(history_filename_storage), "%s/.matho_history", cp);
+ snprintf(mathomatic->history_filename_storage, sizeof(mathomatic->history_filename_storage), "%s/.matho_history", cp);
#endif
- history_filename = history_filename_storage;
+ mathomatic->history_filename = mathomatic->history_filename_storage;
}
using_history(); /* initialize readline history */
rl_initialize(); /* initialize readline */
@@ -259,58 +263,58 @@ char **argv;
rl_inhibit_completion = true; /* turn off readline file name completion */
#if 0 /* not 100% tested and this might confuse the user with the -c toggle */
#if !WIN32_CONSOLE_COLORS
- if (!html_flag) { /* If doing ANSI color: */
- color_flag = (tigetnum("colors") >= 8); /* autoset color output mode. Requires ncurses. */
+ if (!mathomatic->html_flag) { /* If doing ANSI color: */
+ mathomatic->color_flag = (tigetnum("colors") >= 8); /* autoset color output mode. Requires ncurses. */
}
#endif
#endif
#if !SECURE
- if (security_level <= 3) {
- read_history(history_filename); /* restore readline history of previous session */
+ if (mathomatic->security_level <= 3) {
+ read_history(mathomatic->history_filename); /* restore readline history of previous session */
}
#endif
}
#endif
- if (html_flag) {
+ if (mathomatic->html_flag) {
printf("\n");
}
- if (!test_mode && !quiet_mode && !eoption) {
- display_startup_message(stdout);
+ if (!mathomatic->test_mode && !mathomatic->quiet_mode && !mathomatic->eoption) {
+ display_startup_message(mathomatic, stdout);
}
fflush(stdout);
#if !SECURE
/* read the user options initialization file */
- if (security_level <= 3 && !test_mode && !demo_mode && !load_rc(true, NULL)) {
- fprintf(stderr, _("%s: Error loading startup set options from \"%s\".\n"), prog_name, rc_file);
+ if (mathomatic->security_level <= 3 && !mathomatic->test_mode && !mathomatic->demo_mode && !load_rc(mathomatic, true, NULL)) {
+ fprintf(stderr, _("%s: Error loading startup set options from \"%s\".\n"), mathomatic->prog_name, mathomatic->rc_file);
fprintf(stderr, _("Use the \"set no save\" command to startup with the program defaults every time.\n\n"));
}
#endif
if (wide_flag) {
- screen_columns = 0;
- screen_rows = 0;
+ mathomatic->screen_columns = 0;
+ mathomatic->screen_rows = 0;
}
if (coption & 1) {
- color_flag = !color_flag;
+ mathomatic->color_flag = !mathomatic->color_flag;
}
if (boption) {
- color_flag = 1;
- bold_colors = 1;
+ mathomatic->color_flag = 1;
+ mathomatic->bold_colors = 1;
}
- if (color_flag && aoption) {
- color_flag = 2;
+ if (mathomatic->color_flag && aoption) {
+ mathomatic->color_flag = 2;
}
- if (test_mode) {
- color_flag = 0;
- } else if (!quiet_mode && !eoption) {
- if (color_flag) {
+ if (mathomatic->test_mode) {
+ mathomatic->color_flag = 0;
+ } else if (!mathomatic->quiet_mode && !mathomatic->eoption) {
+ if (mathomatic->color_flag) {
#if WIN32_CONSOLE_COLORS
- if (color_flag == 2) {
- printf(_("%s%s color mode enabled"), html_flag ? "HTML" : "ANSI", bold_colors ? " bold" : "");
+ if (mathomatic->color_flag == 2) {
+ printf(_("%s%s color mode enabled"), mathomatic->html_flag ? "HTML" : "ANSI", mathomatic->bold_colors ? " bold" : "");
} else {
- printf(_("%s%s color mode enabled"), html_flag ? "HTML" : "WIN32 CONSOLE", bold_colors ? " bold" : "");
+ printf(_("%s%s color mode enabled"), mathomatic->html_flag ? "HTML" : "WIN32 CONSOLE", mathomatic->bold_colors ? " bold" : "");
}
#else
- printf(_("%s%s color mode enabled"), html_flag ? "HTML" : "ANSI", bold_colors ? " bold" : "");
+ printf(_("%s%s color mode enabled"), mathomatic->html_flag ? "HTML" : "ANSI", mathomatic->bold_colors ? " bold" : "");
#endif
printf(_("; manage by typing \"help color\".\n"));
} else {
@@ -318,18 +322,18 @@ char **argv;
}
}
fflush(stdout);
- if ((i = setjmp(jmp_save)) != 0) {
+ if ((i = setjmp(mathomatic->jmp_save)) != 0) {
/* for error handling */
- clean_up();
+ clean_up(mathomatic);
switch (i) {
case 14:
- error(_("Expression too large."));
+ error(mathomatic, _("Expression too large."));
default:
printf(_("Operation aborted.\n"));
break;
}
- previous_return_value = 0;
- if (eoption)
+ mathomatic->previous_return_value = 0;
+ if (mathomatic->eoption)
exit_value = 1;
} else {
if ((rv = set_signals(time_out_seconds))) {
@@ -339,38 +343,38 @@ char **argv;
#endif
exit_value = 2;
}
- if (!f_to_fraction(0.5, &numerator, &denominator)
+ if (!f_to_fraction(mathomatic, 0.5, &numerator, &denominator)
|| numerator != 1.0 || denominator != 2.0
- || !f_to_fraction(1.0/3.0, &numerator, &denominator)
+ || !f_to_fraction(mathomatic, 1.0/3.0, &numerator, &denominator)
|| numerator != 1.0 || denominator != 3.0) {
- fprintf(stderr, _("%s: Cannot convert any floating point values to fractions!\n"), prog_name);
+ fprintf(stderr, _("%s: Cannot convert any floating point values to fractions!\n"), mathomatic->prog_name);
fprintf(stderr, _("Roots will not work properly.\n"));
exit_value = 2;
}
- if (max_memory_usage() <= 0) {
- fprintf(stderr, _("%s: Calculated maximum memory usage overflows a long integer!\n"), prog_name);
+ if (max_memory_usage(mathomatic) <= 0) {
+ fprintf(stderr, _("%s: Calculated maximum memory usage overflows a long integer!\n"), mathomatic->prog_name);
exit_value = 2;
}
- if (eoption) {
+ if (mathomatic->eoption) {
/* process expressions and commands from the command line */
for (i = optind; i < argc && argv[i]; i++) {
- if (!display_process(argv[i])) {
+ if (!display_process(mathomatic, argv[i])) {
exit_value = 1;
}
}
} else {
#if SECURE
- if (!quiet_mode && !test_mode)
+ if (!mathomatic->quiet_mode && !mathomatic->test_mode)
printf(_("Anything done here is temporary.\n"));
if (optind < argc) {
- warning(_("File arguments ignored in high security mode."));
+ warning(mathomatic, _("File arguments ignored in high security mode."));
}
#else
- if (!quiet_mode && !test_mode) {
+ if (!mathomatic->quiet_mode && !mathomatic->test_mode) {
if (optind < argc) {
printf(_("Reading in file%s specified on the command line...\n"), (optind == (argc - 1)) ? "" : "s");
} else {
- if (security_level >= 2) {
+ if (mathomatic->security_level >= 2) {
printf(_("Anything done here is temporary.\n"));
} else {
printf(_("Anything done here is temporary, unless it is saved or redirected.\n"));
@@ -381,7 +385,7 @@ char **argv;
for (i = optind; i < argc && argv[i]; i++) {
if (strcmp(argv[i], "-") == 0) {
main_io_loop();
- } else if (!read_file(argv[i])) {
+ } else if (!read_file(mathomatic, argv[i])) {
fflush(NULL); /* flush all output */
fprintf(stderr, _("Read of file \"%s\" failed.\n"), argv[i]);
exit_program(1);
@@ -390,7 +394,7 @@ char **argv;
#endif
}
}
- if (!eoption)
+ if (!mathomatic->eoption)
main_io_loop(); /* main input/output loop */
exit_program(exit_value); /* exit Mathomatic, doesn't return */
return(exit_value); /* so the compiler doesn't complain */
@@ -405,13 +409,13 @@ main_io_loop(void)
char *cp = NULL;
for (;;) {
- error_str = NULL;
- warning_str = NULL;
- default_color(false);
- snprintf(prompt_str, sizeof(prompt_str), "%d%s", cur_equation + 1, html_flag ? HTML_PROMPT_STR : PROMPT_STR);
- if ((cp = get_string((char *) tlhs, n_tokens * sizeof(token_type))) == NULL)
+ matho_set_error_str(mathomatic, NULL);
+ matho_set_warning_str(mathomatic, NULL);
+ default_color(mathomatic, false);
+ snprintf(mathomatic->prompt_str, sizeof(mathomatic->prompt_str), "%d%s", mathomatic->cur_equation + 1, mathomatic->html_flag ? HTML_PROMPT_STR : PROMPT_STR);
+ if ((cp = get_string(mathomatic, (char *) mathomatic->tlhs, mathomatic->n_tokens * sizeof(token_type))) == NULL)
break;
- process(cp);
+ process(mathomatic, cp);
}
}
@@ -466,7 +470,7 @@ fphandler(sig)
int sig;
{
#if DEBUG
- warning("Floating point exception.");
+ warning(mathomatic, "Floating point exception.");
#endif
}
@@ -479,8 +483,8 @@ void
inthandler(sig)
int sig;
{
- abort_flag++;
- switch (abort_flag) {
+ matho_inc_abort_flag(mathomatic);
+ switch (matho_get_abort_flag(mathomatic)) {
case 0:
case 1:
/* wait for graceful abort */
@@ -505,7 +509,7 @@ alarmhandler(sig)
int sig;
{
printf(_("\nTimeout, quitting...\n"));
- exit_program(1);
+ exit_program(mathomatic, 1);
}
#endif
@@ -513,8 +517,7 @@ int sig;
* Signal handler for proper exiting to the operating system.
*/
void
-exithandler(sig)
-int sig;
+exithandler(int sig)
{
exit_program(1);
}
@@ -524,11 +527,10 @@ int sig;
* Window resize signal handler.
*/
void
-resizehandler(sig)
-int sig;
+resizehandler(int sig)
{
- if (screen_columns)
- get_screen_size();
+ if (mathomatic->screen_columns)
+ get_screen_size(mathomatic);
}
#endif
@@ -536,19 +538,19 @@ int sig;
* Properly exit this program and return to the operating system.
*/
void
-exit_program(exit_value)
-int exit_value; /* zero if OK, non-zero indicates error return */
+exit_program(int exit_value)
+//int exit_value; /* zero if OK, non-zero indicates error return */
{
- reset_attr();
- if (html_flag) {
+ reset_attr(mathomatic);
+ if (mathomatic->html_flag) {
printf("\n");
}
#if READLINE && !SECURE
- if (readline_enabled && security_level <= 3) {
- write_history(history_filename); /* save readline history */
+ if (mathomatic->readline_enabled && mathomatic->security_level <= 3) {
+ write_history(mathomatic->history_filename); /* save readline history */
}
#endif
- if (exit_value == 0 && !quiet_mode && !eoption && !html_flag) {
+ if (exit_value == 0 && !mathomatic->quiet_mode && !mathomatic->eoption && !mathomatic->html_flag) {
printf(_("ByeBye!! from Mathomatic.\n"));
}
#if VALGRIND
@@ -556,6 +558,7 @@ int exit_value; /* zero if OK, non-zero indicates error return */
printf("If you are not using valgrind, please compile without -DVALGRIND.\n");
free_mem(); /* Free all known memory buffers to check for memory leaks with something like valgrind(1). */
#endif
+ closetMathoMatic(mathomatic);
exit(exit_value);
}
#endif
diff --git a/parse.c b/parse.c
index aa9b98c..398f589 100644
--- a/parse.c
+++ b/parse.c
@@ -45,35 +45,34 @@ char *cp;
* followed by the error message, which must be a constant string.
*/
void
-put_up_arrow(cnt, cp)
-int cnt; /* position of error, relative to "input_column" */
-char *cp; /* error message (constant ASCII string) */
+put_up_arrow(MathoMatic* mathomatic, int cnt, char *cp)
+//int cnt; /* position of error, relative to "input_column" */
+//char *cp; /* error message (constant ASCII string) */
{
#if !SILENT && !LIBRARY
int i;
- cnt += input_column;
- if (!quiet_mode && point_flag && (screen_columns == 0 || cnt < screen_columns)) {
+ cnt += mathomatic->input_column;
+ if (!mathomatic->quiet_mode && mathomatic->point_flag && (mathomatic->screen_columns == 0 || cnt < mathomatic->screen_columns)) {
for (i = 0; i < cnt; i++) {
printf(" ");
}
printf("^ ");
}
#endif
- error(cp);
+ error(mathomatic, cp);
}
/*
* Return true if character is a valid starting variable character.
*/
int
-isvarchar(ch)
-int ch;
+isvarchar(MathoMatic* mathomatic, int ch)
{
if (isdigit(ch)) { /* variable names can never start with a digit */
return false;
}
- return(ch == '_' || (ch && strchr(special_variable_characters, ch)) || isalpha(ch));
+ return(ch == '_' || (ch && strchr(mathomatic->special_variable_characters, ch)) || isalpha(ch));
}
/*
@@ -130,7 +129,7 @@ int i; /* location of operator to parenthesize in expression */
#if DEBUG
if (i >= (n - 1) || (n & 1) != 1 || (i & 1) != 1 || p1[i].kind != OPERATOR) {
- error_bug("Internal error in arguments to binary_parenthesize().");
+ error_bug(mathomatic, "Internal error in arguments to binary_parenthesize().");
}
#endif
skip_negate = (p1[i].token.operatr != NEGATE);
@@ -186,9 +185,9 @@ int *np;
* organize() should be called after this to remove unneeded parentheses.
*/
void
-give_priority(equation, np)
-token_type *equation; /* pointer to expression */
-int *np; /* pointer to expression length */
+give_priority(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* pointer to expression */
+//int *np; /* pointer to expression length */
{
int i;
@@ -199,7 +198,7 @@ int *np; /* pointer to expression length */
}
}
- if (right_associative_power) {
+ if (mathomatic->right_associative_power) {
for (i = *np - 2; i >= 1; i -= 2) { /* count down (for right to left evaluation) */
if (equation[i].token.operatr == POWER) {
binary_parenthesize(equation, *np, i);
@@ -236,11 +235,11 @@ int *np; /* pointer to expression length */
* Returns the new string position, or NULL if error.
*/
char *
-parse_section(equation, np, cp, allow_space)
-token_type *equation; /* where the parsed expression is stored (equation side) */
-int *np; /* pointer to the returned parsed expression length */
-char *cp; /* string to parse */
-int allow_space; /* if false, any space characters terminate parsing */
+parse_section(MathoMatic* mathomatic, token_type *equation, int *np, char *cp, int allow_space)
+//token_type *equation; /* where the parsed expression is stored (equation side) */
+//int *np; /* pointer to the returned parsed expression length */
+//char *cp; /* string to parse */
+//int allow_space; /* if false, any space characters terminate parsing */
{
int i;
int n = 0, old_n; /* position in equation[] */
@@ -255,8 +254,8 @@ int allow_space; /* if false, any space characters terminate parsing */
return(NULL);
cp_start = cp;
for (;; cp++) {
- if (n > (n_tokens - 10)) {
- error_huge();
+ if (n > (mathomatic->n_tokens - 10)) {
+ error_huge(mathomatic);
}
switch (*cp) {
case '(':
@@ -279,7 +278,7 @@ int allow_space; /* if false, any space characters terminate parsing */
case '}':
cur_level--;
if (cur_level <= 0 || (abs_count > 0 && cur_level < abs_array[abs_count-1])) {
- put_up_arrow(cp - cp_start, _("Unmatched parenthesis: too many )"));
+ put_up_arrow(mathomatic, cp - cp_start, _("Unmatched parenthesis: too many )"));
return(NULL);
}
if (!operand) {
@@ -301,7 +300,7 @@ int allow_space; /* if false, any space characters terminate parsing */
continue;
case '\033':
if (cp[1] == '[' || cp[1] == 'O') {
- error(_("Cursor or function key string encountered, unable to interpret."));
+ error(mathomatic, _("Cursor or function key string encountered, unable to interpret."));
return(NULL);
}
continue;
@@ -311,7 +310,7 @@ int allow_space; /* if false, any space characters terminate parsing */
case '|':
if (operand) {
if (abs_count >= ARR_CNT(abs_array)) {
- error(_("Too many nested absolute values."));
+ error(mathomatic, _("Too many nested absolute values."));
return(NULL);
}
cur_level += 3;
@@ -347,7 +346,7 @@ int allow_space; /* if false, any space characters terminate parsing */
goto syntax_error;
}
if (cp[1] == '!' && cp[2] != '!') {
- warning(_("Multifactorial not implemented, using x!! = (x!)!"));
+ warning(mathomatic, _("Multifactorial not implemented, using x!! = (x!)!"));
}
equation[n].level = cur_level;
equation[n].kind = OPERATOR;
@@ -425,7 +424,7 @@ parse_power:
if (strncmp(cp, "+/-", 3) == 0) {
equation[n].level = cur_level;
equation[n].kind = VARIABLE;
- next_sign(&equation[n].token.variable);
+ next_sign(mathomatic, &equation[n].token.variable);
n++;
equation[n].level = cur_level;
equation[n].kind = OPERATOR;
@@ -475,7 +474,7 @@ parse_power:
goto syntax_error;
}
if (errno) {
- put_up_arrow(cp1 - cp_start, _("Constant out of range."));
+ put_up_arrow(mathomatic, cp1 - cp_start, _("Constant out of range."));
return(NULL);
}
equation[n].kind = CONSTANT;
@@ -494,42 +493,42 @@ parse_power:
case '+':
case '-':
i = strtol(cp, &cp1, 10);
- i = cur_equation + i;
+ i = mathomatic->cur_equation + i;
break;
default:
i = strtol(cp, &cp1, 10) - 1;
break;
}
if (cp1 == NULL || cp == cp1) {
- put_up_arrow(cp - cp_start, _("Error parsing equation space number after #."));
+ put_up_arrow(mathomatic, cp - cp_start, _("Error parsing equation space number after #."));
return NULL;
}
- if (empty_equation_space(i)) {
- put_up_arrow(cp - cp_start, _("No expression available in # specified equation space."));
+ if (empty_equation_space(mathomatic, i)) {
+ put_up_arrow(mathomatic, cp - cp_start, _("No expression available in # specified equation space."));
return NULL;
}
cp = cp1 - 1;
old_n = n;
- if (n_rhs[i]) {
- n += n_rhs[i];
- if (n > n_tokens) {
- error_huge();
+ if (mathomatic->n_rhs[i]) {
+ n += mathomatic->n_rhs[i];
+ if (n > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
- blt(&equation[old_n], rhs[i], n_rhs[i] * sizeof(token_type));
+ blt(&equation[old_n], mathomatic->rhs[i], mathomatic->n_rhs[i] * sizeof(token_type));
} else {
- n += n_lhs[i];
- if (n > n_tokens) {
- error_huge();
+ n += mathomatic->n_lhs[i];
+ if (n > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
- blt(&equation[old_n], lhs[i], n_lhs[i] * sizeof(token_type));
+ blt(&equation[old_n], mathomatic->lhs[i], mathomatic->n_lhs[i] * sizeof(token_type));
}
for (; old_n < n; old_n++) {
equation[old_n].level += cur_level;
}
break;
default:
- if (!isvarchar(*cp)) {
- put_up_arrow(cp - cp_start, _("Unrecognized character."));
+ if (!isvarchar(mathomatic, *cp)) {
+ put_up_arrow(mathomatic, cp - cp_start, _("Unrecognized character."));
return(NULL);
}
if (!operand) {
@@ -541,18 +540,18 @@ parse_power:
}
cp1 = cp;
if (strncasecmp(cp, "inf", strlen("inf")) == 0
- && !isvarchar(cp[strlen("inf")])) {
+ && !isvarchar(mathomatic, cp[strlen("inf")])) {
equation[n].kind = CONSTANT;
equation[n].token.constant = INFINITY; /* the infinity constant */
cp += strlen("inf");
} else if (strncasecmp(cp, INFINITY_NAME, strlen(INFINITY_NAME)) == 0
- && !isvarchar(cp[strlen(INFINITY_NAME)])) {
+ && !isvarchar(mathomatic, cp[strlen(INFINITY_NAME)])) {
equation[n].kind = CONSTANT;
equation[n].token.constant = INFINITY; /* the infinity constant */
cp += strlen(INFINITY_NAME);
} else {
equation[n].kind = VARIABLE;
- cp = parse_var(&equation[n].token.variable, cp);
+ cp = parse_var(mathomatic, &equation[n].token.variable, cp);
if (cp == NULL) {
return(NULL);
}
@@ -560,9 +559,9 @@ parse_power:
if (*cp == '(') {
/* Named functions currently not implemented, except when using m4. */
#if LIBRARY
- put_up_arrow(cp1 - cp_start, _("Unknown function."));
+ put_up_arrow(mathomatic, cp1 - cp_start, _("Unknown function."));
#else
- put_up_arrow(cp1 - cp_start, _("Unknown function; try using rmath, which allows basic functions."));
+ put_up_arrow(mathomatic, cp1 - cp_start, _("Unknown function; try using rmath, which allows basic functions."));
#endif
return(NULL);
}
@@ -577,21 +576,21 @@ p_out:
goto syntax_error;
}
if (cur_level != 1) {
- put_up_arrow(cp - cp_start, _("Unmatched parenthesis: missing )"));
+ put_up_arrow(mathomatic, cp - cp_start, _("Unmatched parenthesis: missing )"));
return(NULL);
}
while (*cp == '=')
cp++;
*np = n;
if (n) {
- give_priority(equation, np);
- organize(equation, np);
+ give_priority(mathomatic, equation, np);
+ organize(mathomatic, equation, np);
}
- input_column += (cp - cp_start);
+ mathomatic->input_column += (cp - cp_start);
return cp;
syntax_error:
- put_up_arrow(cp - cp_start, _("Syntax error."));
+ put_up_arrow(mathomatic, cp - cp_start, _("Syntax error."));
return(NULL);
}
@@ -604,18 +603,18 @@ syntax_error:
* Currently, there can be no more to parse in the string when this returns.
*/
char *
-parse_equation(n, cp)
-int n; /* equation space number */
-char *cp; /* pointer to the beginning of the equation character string */
+parse_equation(MathoMatic* mathomatic, int n, char *cp)
+//int n; /* equation space number */
+//char *cp; /* pointer to the beginning of the equation character string */
{
- if ((cp = parse_expr(lhs[n], &n_lhs[n], cp, true)) != NULL) {
- if ((cp = parse_expr(rhs[n], &n_rhs[n], cp, true)) != NULL) {
- if (!extra_characters(cp))
+ if ((cp = parse_expr(mathomatic, mathomatic->lhs[n], &mathomatic->n_lhs[n], cp, true)) != NULL) {
+ if ((cp = parse_expr(mathomatic, mathomatic->rhs[n], &mathomatic->n_rhs[n], cp, true)) != NULL) {
+ if (!extra_characters(mathomatic, cp))
return cp;
}
}
- n_lhs[n] = 0;
- n_rhs[n] = 0;
+ mathomatic->n_lhs[n] = 0;
+ mathomatic->n_rhs[n] = 0;
return NULL;
}
@@ -626,18 +625,18 @@ char *cp; /* pointer to the beginning of the equation character string */
* Returns the new string position, or NULL if error.
*/
char *
-parse_expr(equation, np, cp, allow_space)
-token_type *equation; /* where the parsed expression is stored (equation side) */
-int *np; /* pointer to the returned parsed expression length */
-char *cp; /* string to parse */
-int allow_space; /* if true, allow and ignore space characters; if false, space means terminate parsing */
+parse_expr(MathoMatic* mathomatic, token_type *equation, int *np, char *cp, int allow_space)
+//token_type *equation; /* where the parsed expression is stored (equation side) */
+//int *np; /* pointer to the returned parsed expression length */
+//char *cp; /* string to parse */
+//int allow_space; /* if true, allow and ignore space characters; if false, space means terminate parsing */
{
if (cp == NULL)
return NULL;
- if (!case_sensitive_flag) {
+ if (!mathomatic->case_sensitive_flag) {
str_tolower(cp);
}
- cp = parse_section(equation, np, cp, allow_space);
+ cp = parse_section(mathomatic, equation, np, cp, allow_space);
return cp;
}
@@ -651,9 +650,7 @@ int allow_space; /* if true, allow and ignore space characters; if false, space
* Display error message and return NULL on failure.
*/
char *
-parse_var(vp, cp)
-long *vp;
-char *cp;
+parse_var(MathoMatic* mathomatic, long *vp, char *cp)
{
int i, j;
long vtmp;
@@ -663,17 +660,17 @@ char *cp;
int level; /* parentheses level */
int (*strcmpfunc)();
- if (case_sensitive_flag) {
+ if (mathomatic->case_sensitive_flag) {
strcmpfunc = strcmp;
} else {
strcmpfunc = strcasecmp;
}
- if (!isvarchar(*cp) || paren_increment(*cp) < 0) {
- error(_("Invalid variable."));
+ if (!isvarchar(mathomatic, *cp) || paren_increment(*cp) < 0) {
+ error(mathomatic, _("Invalid variable."));
return(NULL); /* variable name must start with a valid variable character */
}
for (level = 0, cp1 = cp, i = 0; *cp1;) {
- if (level <= 0 && !isvarchar(*cp1)) {
+ if (level <= 0 && !isvarchar(mathomatic, *cp1)) {
break;
}
j = paren_increment(*cp1);
@@ -681,7 +678,7 @@ char *cp;
if (level < 0)
break;
if (i >= MAX_VAR_LEN) {
- error(_("Variable name too long."));
+ error(mathomatic, _("Variable name too long."));
return(NULL);
}
buf[i++] = *cp1++;
@@ -690,15 +687,15 @@ char *cp;
}
buf[i] = '\0';
if (level > 0) {
- error(_("Unmatched parenthesis: missing )"));
+ error(mathomatic, _("Unmatched parenthesis: missing )"));
return(NULL);
}
if (strcasecmp(buf, NAN_NAME) == 0) {
- warning(_("Attempt to enter NaN (Not a Number); Converted to variable."));
+ warning(mathomatic, _("Attempt to enter NaN (Not a Number); Converted to variable."));
}
if (strcasecmp(buf, "inf") == 0 || strcasecmp(buf, INFINITY_NAME) == 0) {
- error(_("Infinity cannot be used as a variable."));
+ error(mathomatic, _("Infinity cannot be used as a variable."));
return(NULL);
} else if ((*strcmpfunc)(buf, "sign") == 0) {
vtmp = SIGN;
@@ -716,7 +713,7 @@ char *cp;
return(cp + 3);
}
for (level = 0, cp1 = cp, i = 0; *cp1;) {
- if (level <= 0 && !isvarchar(*cp1) && !isdigit(*cp1)) {
+ if (level <= 0 && !isvarchar(mathomatic, *cp1) && !isdigit(*cp1)) {
break;
}
j = paren_increment(*cp1);
@@ -724,7 +721,7 @@ char *cp;
if (level < 0)
break;
if (i >= MAX_VAR_LEN) {
- error(_("Variable name too long."));
+ error(mathomatic, _("Variable name too long."));
return(NULL);
}
buf[i++] = *cp1++;
@@ -732,12 +729,12 @@ char *cp;
break;
}
if (i <= 0) {
- error(_("Empty variable name parsed!"));
+ error(mathomatic, _("Empty variable name parsed!"));
return(NULL);
}
buf[i] = '\0';
if (level > 0) {
- error(_("Unmatched parenthesis: missing )"));
+ error(mathomatic, _("Unmatched parenthesis: missing )"));
return(NULL);
}
if ((*strcmpfunc)(buf, "i") == 0) {
@@ -753,33 +750,33 @@ char *cp;
return(cp1);
}
if (is_all(buf)) {
- error(_("\"all\" is a reserved word and may not be used as a variable name."));
+ error(mathomatic, _("\"all\" is a reserved word and may not be used as a variable name."));
return(NULL);
}
vtmp = 0;
- for (i = 0; var_names[i]; i++) {
- if ((*strcmpfunc)(buf, var_names[i]) == 0) {
+ for (i = 0; mathomatic->var_names[i]; i++) {
+ if ((*strcmpfunc)(buf, mathomatic->var_names[i]) == 0) {
vtmp = i + VAR_OFFSET;
break;
}
}
if (vtmp == 0) {
if (i >= (MAX_VAR_NAMES - 1)) {
- error(_("Maximum number of variable names reached."));
+ error(mathomatic, _("Maximum number of variable names reached."));
#if !SILENT
printf(_("Please restart or use \"clear all\".\n"));
#endif
return(NULL);
}
len = strlen(buf) + 1;
- var_names[i] = (char *) malloc(len);
- if (var_names[i] == NULL) {
- error(_("Out of memory (can't malloc(3) variable name)."));
+ mathomatic->var_names[i] = (char *) malloc(len);
+ if (mathomatic->var_names[i] == NULL) {
+ error(mathomatic, _("Out of memory (can't malloc(3) variable name)."));
return(NULL);
}
- blt(var_names[i], buf, len);
+ blt(mathomatic->var_names[i], buf, len);
vtmp = i + VAR_OFFSET;
- var_names[i+1] = NULL;
+ mathomatic->var_names[i+1] = NULL;
}
*vp = vtmp;
return cp1;
@@ -788,15 +785,15 @@ char *cp;
if (isdigit(*cp1)) {
j = strtol(cp1, &cp1, 10);
if (j < 0 || j > MAX_SUBSCRIPT) {
- error(_("Maximum subscript exceeded in special variable name."));
+ error(mathomatic, _("Maximum subscript exceeded in special variable name."));
return(NULL);
}
if (vtmp == SIGN) {
- sign_array[j+1] = true;
+ mathomatic->sign_array[j+1] = true;
}
vtmp += ((long) (j + 1)) << VAR_SHIFT;
} else if (vtmp == SIGN) {
- sign_array[0] = true;
+ mathomatic->sign_array[0] = true;
}
*vp = vtmp;
return cp1;
@@ -806,8 +803,7 @@ char *cp;
* Remove trailing spaces from a string.
*/
void
-remove_trailing_spaces(cp)
-char *cp;
+remove_trailing_spaces(char *cp)
{
int i;
@@ -824,20 +820,20 @@ char *cp;
* Truncate string to the actual content.
*/
void
-set_error_level(cp)
-char *cp; /* input string */
+set_error_level(MathoMatic* mathomatic, char *cp)
+//char *cp; /* input string */
{
char *cp1;
int len;
- point_flag = true;
+ mathomatic->point_flag = true;
/* handle comments, line breaks, and the DOS EOF character (control-Z) by truncating the string where found */
cp1 = cp;
while ((cp1 = strpbrk(cp1, ";\n\r\032"))) {
if (cp1 > cp && *(cp1 - 1) == '\\') {
if (*cp1 == ';') {
/* skip backslash escaped semicolon */
- point_flag = false;
+ mathomatic->point_flag = false;
len = strlen(cp1);
blt(cp1 - 1, cp1, len + 1);
continue;
@@ -852,7 +848,7 @@ char *cp; /* input string */
/* set point_flag to false if non-printable characters encountered */
for (cp1 = cp; *cp1; cp1++) {
if (!isprint(*cp1)) {
- point_flag = false;
+ mathomatic->point_flag = false;
}
}
}
diff --git a/poly.c b/poly.c
index aaf0996..3f8600f 100644
--- a/poly.c
+++ b/poly.c
@@ -41,7 +41,7 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
#include "includes.h"
-#define REMAINDER_IS_ZERO() (n_trhs == 1 && trhs[0].kind == CONSTANT && trhs[0].token.constant == 0.0)
+#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
@@ -58,14 +58,14 @@ 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(token_type *equation, int *np, int loc, int level, int do_repeat);
-static int pf_sub(token_type *equation, int *np, int loc, int len, int level, int do_repeat);
-static int save_factors(token_type *equation, int *np, int loc1, int len, int level);
-static int do_gcd(long *vp);
-static int mod_recurse(token_type *equation, int *np, int loc, int level);
-static int polydiv_recurse(token_type *equation, int *np, int loc, int level);
-static int pdiv_recurse(token_type *equation, int *np, int loc, int level, int code);
-static int poly_div_sub(token_type *d1, int len1, token_type *d2, int len2, long *vp);
+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);
/*
@@ -92,17 +92,17 @@ sort_type *p1, *p2;
* as long as it doesn't contain the variable v.
*/
int
-poly_in_v_sub(p1, n, v, allow_divides)
-token_type *p1; /* expression pointer */
-int n; /* expression length */
-long v; /* Mathomatic variable */
-int allow_divides; /* if true, allow division by variable */
+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(p1, n);
+ 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++;
@@ -147,18 +147,18 @@ int allow_divides; /* if true, allow division by variable */
* The passed expression should be fully unfactored, for a proper determination.
*/
int
-poly_in_v(p1, n, v, 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 */
+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(&p1[j], i - j, v, allow_divides)) {
+ if (!poly_in_v_sub(mathomatic, &p1[j], i - j, v, allow_divides)) {
return false;
}
j = i + 1;
@@ -182,19 +182,16 @@ int allow_divides; /* allow variable to be right of a divide (negative exponent
* Return true if equation side was modified (factored).
*/
int
-poly_factor(equation, np, 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 */
+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(equation, np, 0, 1, do_repeat);
+ return pf_recurse(mathomatic, equation, np, 0, 1, do_repeat);
}
static int
-pf_recurse(equation, np, loc, level, do_repeat)
-token_type *equation;
-int *np, loc, level;
-int do_repeat;
+pf_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int do_repeat)
{
int modified = false;
int i;
@@ -212,11 +209,11 @@ int do_repeat;
}
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(equation, np, loc, i - loc, level, do_repeat);
+ 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(equation, np, i, level + 1, do_repeat);
+ modified |= pf_recurse(mathomatic, equation, np, i, level + 1, do_repeat);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
@@ -235,13 +232,13 @@ int do_repeat;
* Return true if equation side was modified (factored).
*/
static int
-pf_sub(equation, np, loc, len, level, 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 */
+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;
@@ -258,30 +255,30 @@ int do_repeat; /* factor repeated factors flag */
double d;
int old_partial;
- debug_string(3, "Entering pf_sub().");
- old_partial = partial_flag;
+ 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, jmp_save, sizeof(jmp_save));
- if ((i = setjmp(jmp_save)) != 0) { /* trap errors */
- partial_flag = old_partial;
- blt(jmp_save, save_save, sizeof(jmp_save));
+ 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(jmp_save, i);
+ 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(trhs, &equation[loc1], len * sizeof(token_type));
- n_trhs = len;
- partial_flag = false;
- uf_simp(trhs, &n_trhs);
- partial_flag = old_partial;
- if (level1_plus_count(trhs, n_trhs) < 2) {
+ 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;
}
@@ -290,12 +287,12 @@ int do_repeat; /* factor repeated factors flag */
for (vc = 0; vc < ARR_CNT(va);) {
cnt = 0;
v1 = -1;
- for (i = 0; i < n_trhs; i += 2) {
- if (trhs[i].kind == VARIABLE && trhs[i].token.variable > last_v) {
- if (v1 == -1 || trhs[i].token.variable < v1) {
- v1 = trhs[i].token.variable;
+ 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 (trhs[i].token.variable == v1) {
+ } else if (mathomatic->trhs[i].token.variable == v1) {
cnt++;
}
}
@@ -307,12 +304,12 @@ int do_repeat; /* factor repeated factors flag */
va[vc].count = cnt;
vc++;
}
- side_debug(3, &equation[loc1], len);
- side_debug(3, trhs, n_trhs);
+ 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(trhs, n_trhs, v, true)) {
+ if (vc > 1 && !poly_in_v(mathomatic, mathomatic->trhs, mathomatic->n_trhs, v, true)) {
v = 0;
}
}
@@ -321,7 +318,7 @@ int do_repeat; /* factor repeated factors flag */
continue;
}
if (v == 0) {
- if (poly_in_v(trhs, n_trhs, va[i].v, true)) {
+ if (poly_in_v(mathomatic, mathomatic->trhs, mathomatic->n_trhs, va[i].v, true)) {
v = va[i].v;
}
}
@@ -341,9 +338,9 @@ int do_repeat; /* factor repeated factors flag */
continue;
}
v = va[i].v;
- blt(tlhs, trhs, n_trhs * sizeof(token_type));
- n_tlhs = n_trhs;
- if (differentiate(tlhs, &n_tlhs, 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;
@@ -353,48 +350,48 @@ int do_repeat; /* factor repeated factors flag */
}
#endif
} else {
- blt(tlhs, trhs, n_trhs * sizeof(token_type));
- n_tlhs = n_trhs;
- if (!differentiate(tlhs, &n_tlhs, 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;
}
}
#if !SILENT
- if (debug_level >= 3) {
- list_var(v, 0);
- fprintf(gfp, _("Differentiation successful using variable %s.\n"), var_str);
+ 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(tlhs, &n_tlhs);
- if ((n_tlhs + 2) > min(DIVISOR_SIZE, n_tokens))
+ simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ if ((mathomatic->n_tlhs + 2) > min(DIVISOR_SIZE, mathomatic->n_tokens))
break;
- for (i = 0; i < n_tlhs; i++)
- tlhs[i].level++;
- tlhs[n_tlhs].kind = OPERATOR;
- tlhs[n_tlhs].level = 1;
- tlhs[n_tlhs].token.operatr = TIMES;
- n_tlhs++;
- tlhs[n_tlhs].kind = VARIABLE;
- tlhs[n_tlhs].level = 1;
- tlhs[n_tlhs].token.variable = v;
- n_tlhs++;
- uf_simp(tlhs, &n_tlhs);
- if (poly_gcd(&equation[loc1], len, tlhs, n_tlhs, v) <= 0)
+ 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(tlhs, n_tlhs) == 0)
+ if (level1_plus_count(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs) == 0)
break;
- if (!save_factors(equation, np, loc1, len, level))
+ if (!save_factors(mathomatic, equation, np, loc1, len, level))
break;
- loc1 += n_tlhs + 1;
- len = n_trhs;
+ loc1 += mathomatic->n_tlhs + 1;
+ len = mathomatic->n_trhs;
switch (count) {
case 1:
- debug_string(1, "Polynomial with repeated factor factored.");
- len_first = n_tlhs;
+ debug_string(mathomatic, 1, "Polynomial with repeated factor factored.");
+ len_first = mathomatic->n_tlhs;
loc2 = loc1;
break;
case 2:
- len2 = n_tlhs;
+ len2 = mathomatic->n_tlhs;
break;
}
modified = true;
@@ -404,10 +401,10 @@ int do_repeat; /* factor repeated factors flag */
last_v = 0;
next_v:
p1 = &equation[loc1];
- blt(trhs, p1, len * sizeof(token_type));
- n_trhs = len;
- uf_simp_no_repeat(trhs, &n_trhs);
- if (level1_plus_count(trhs, n_trhs) < 2) {
+ 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;
}
@@ -425,49 +422,49 @@ next_v:
}
last_v = v;
/* make sure there is more than one "v" raised to the highest power: */
- if (find_greatest_power(trhs, n_trhs, &v, &d, &j, &k, &div_flag) <= 1) {
+ if (find_greatest_power(mathomatic->trhs, mathomatic->n_trhs, &v, &d, &j, &k, &div_flag) <= 1) {
continue;
}
- blt(tlhs, trhs, n_trhs * sizeof(token_type));
- n_tlhs = n_trhs;
+ blt(mathomatic->tlhs, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
+ mathomatic->n_tlhs = mathomatic->n_trhs;
/* do the grouping: */
- while (factor_plus(tlhs, &n_tlhs, v, 0.0)) {
- simp_loop(tlhs, &n_tlhs);
+ 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(tlhs, n_tlhs, &v, &d, &j, &k, &div_flag) != 1) {
+ if (find_greatest_power(mathomatic->tlhs, mathomatic->n_tlhs, &v, &d, &j, &k, &div_flag) != 1) {
continue;
}
if (j) {
- blt(tlhs, &tlhs[j], k * sizeof(token_type));
+ blt(mathomatic->tlhs, &mathomatic->tlhs[j], k * sizeof(token_type));
}
- n_tlhs = k;
+ mathomatic->n_tlhs = k;
#if !SILENT
- if (debug_level >= 3) {
- fprintf(gfp, _("Trying factor: "));
- list_proc(tlhs, n_tlhs, false);
- fprintf(gfp, "\n");
+ 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(&equation[loc1], len, tlhs, n_tlhs, 0L) <= 0)
+ if (poly_gcd(mathomatic, &equation[loc1], len, mathomatic->tlhs, mathomatic->n_tlhs, 0L) <= 0)
goto next_v;
- if (level1_plus_count(tlhs, n_tlhs) == 0)
+ if (level1_plus_count(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs) == 0)
goto next_v;
if (!symbolic_modified) {
- debug_string(1, "Symbolic polynomial factored.");
+ debug_string(mathomatic, 1, "Symbolic polynomial factored.");
} else {
- debug_string(1, "Found another symbolic factor.");
+ debug_string(mathomatic, 1, "Found another symbolic factor.");
}
- if (!save_factors(equation, np, loc1, len, level))
+ if (!save_factors(mathomatic, equation, np, loc1, len, level))
break;
- len = n_tlhs;
+ len = mathomatic->n_tlhs;
symbolic_modified = true;
last_v = 0;
goto next_v;
}
}
skip_factor:
- blt(jmp_save, save_save, sizeof(jmp_save));
+ 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. */
@@ -478,18 +475,18 @@ skip_factor:
loct = loc;
lent = len_first;
}
- if (poly_gcd(&equation[loc1], len, &equation[loct], lent, v) > 0) {
- if (save_factors(equation, np, loc1, len, level)) {
- loc1 += n_tlhs + 1;
- len = n_trhs;
+ 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(&equation[loc], len_first, &equation[loc1], len, 0L) > 0) {
- save_factors(equation, np, loc, len_first, level);
+ 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) {
@@ -497,42 +494,40 @@ skip_factor:
;
#if DEBUG
if ((i & 1) != 1) {
- error_bug("Error in result of pf_sub().");
+ error_bug(mathomatic, "Error in result of pf_sub().");
}
#endif
- debug_string(1, "Resulting factors of pf_sub():");
- side_debug(1, &equation[loc], i - loc);
+ 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(equation, np, loc1, len, level)
-token_type *equation;
-int *np, loc1, len, level;
+save_factors(MathoMatic* mathomatic, token_type *equation, int *np, int loc1, int len, int level)
{
int i, j;
- i = n_tlhs + 1 + n_trhs;
+ i = mathomatic->n_tlhs + 1 + mathomatic->n_trhs;
if (i > (len * 3))
goto rejected;
- if ((*np + (i - len)) > n_tokens)
+ 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], tlhs, n_tlhs * sizeof(token_type));
- i = loc1 + n_tlhs;
+ 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], trhs, n_trhs * sizeof(token_type));
- i += n_trhs;
+ 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(1, "Polynomial factor rejected because too large.");
+ debug_string(mathomatic, 1, "Polynomial factor rejected because too large.");
return false;
}
@@ -544,37 +539,37 @@ rejected:
* If this returns false, nothing is removed.
*/
int
-remove_factors(void)
+remove_factors(MathoMatic* mathomatic)
{
int i, j, k;
int plus_flag = false, divide_flag = false;
int op;
- debug_string(3, "Entering remove_factors() with: ");
- side_debug(3, tlhs, n_tlhs);
+ debug_string(mathomatic, 3, "Entering remove_factors() with: ");
+ side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
do {
- simp_ssub(tlhs, &n_tlhs, 0L, 1.0, false, true, 4);
- } while (uf_power(tlhs, &n_tlhs));
+ 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 >= n_tlhs) {
+ if (i >= mathomatic->n_tlhs) {
if (plus_flag && !divide_flag) {
if (k > 0)
j--;
- blt(&scratch[k], &tlhs[j], (i - j) * sizeof(token_type));
+ blt(&mathomatic->scratch[k], &mathomatic->tlhs[j], (i - j) * sizeof(token_type));
k += i - j;
}
if (k <= 0) {
- debug_string(3, "Leaving remove_factors() with false return and no change.");
+ debug_string(mathomatic, 3, "Leaving remove_factors() with false return and no change.");
return false;
}
- blt(tlhs, scratch, k * sizeof(token_type));
- n_tlhs = k;
- debug_string(3, "Leaving remove_factors() with success and: ");
- side_debug(3, tlhs, n_tlhs);
+ 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 = tlhs[i].token.operatr;
- switch (tlhs[i].level) {
+ op = mathomatic->tlhs[i].token.operatr;
+ switch (mathomatic->tlhs[i].level) {
case 1:
switch (op) {
case PLUS:
@@ -585,13 +580,13 @@ remove_factors(void)
case DIVIDE:
break;
default:
- debug_string(3, "Leaving remove_factors() with false return and no change.");
+ 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(&scratch[k], &tlhs[j], (i - j) * sizeof(token_type));
+ blt(&mathomatic->scratch[k], &mathomatic->tlhs[j], (i - j) * sizeof(token_type));
k += i - j;
}
plus_flag = false;
@@ -619,31 +614,31 @@ remove_factors(void)
* If a negative number, switching operands won't work either.
*/
static int
-do_gcd(vp)
-long *vp; /* polynomial base variable pointer */
+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(trhs, n_trhs, gcd_divisor, len_d, vp)) {
+ switch (poly_div(mathomatic, mathomatic->trhs, mathomatic->n_trhs, gcd_divisor, len_d, vp)) {
case 0:
/* divide failed */
return(1 - count);
case 2:
/* Total success! Remainder is zero. */
- debug_string(2, "Found raw polynomial GCD:");
- side_debug(2, gcd_divisor, len_d);
+ debug_string(mathomatic, 2, "Found raw polynomial GCD:");
+ side_debug(mathomatic, 2, gcd_divisor, len_d);
return count;
}
/* Do the Euclidean shuffle: swap trhs[] (remainder) and gcd_divisor[] */
- if (len_d > n_tokens || n_trhs > DIVISOR_SIZE)
+ if (len_d > mathomatic->n_tokens || mathomatic->n_trhs > DIVISOR_SIZE)
return 0;
- blt(scratch, trhs, n_trhs * sizeof(token_type));
- blt(trhs, gcd_divisor, len_d * sizeof(token_type));
- blt(gcd_divisor, scratch, n_trhs * sizeof(token_type));
- i = n_trhs;
- n_trhs = len_d;
+ blt(mathomatic->scratch, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
+ blt(mathomatic->trhs, gcd_divisor, len_d * sizeof(token_type));
+ blt(gcd_divisor, mathomatic->scratch, mathomatic->n_trhs * sizeof(token_type));
+ i = mathomatic->n_trhs;
+ mathomatic->n_trhs = len_d;
len_d = i;
}
return 0;
@@ -660,60 +655,60 @@ long *vp; /* polynomial base variable pointer */
* The results are unfactored and simplified.
*/
int
-poly_gcd(larger, llen, smaller, slen, 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 */
+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(3, "Entering poly_gcd():");
- side_debug(3, larger, llen);
- side_debug(3, smaller, slen);
- if (llen > n_tokens || slen > min(ARR_CNT(gcd_divisor), n_tokens))
+ 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(gcd_divisor), mathomatic->n_tokens))
return 0;
- if (trhs != larger) {
- blt(trhs, larger, llen * sizeof(token_type));
+ if (mathomatic->trhs != larger) {
+ blt(mathomatic->trhs, larger, llen * sizeof(token_type));
}
- n_trhs = llen;
- if (tlhs != smaller) {
- blt(tlhs, smaller, slen * sizeof(token_type));
+ mathomatic->n_trhs = llen;
+ if (mathomatic->tlhs != smaller) {
+ blt(mathomatic->tlhs, smaller, slen * sizeof(token_type));
}
- n_tlhs = slen;
- if (!remove_factors())
+ mathomatic->n_tlhs = slen;
+ if (!remove_factors(mathomatic))
return 0;
- if (n_tlhs > ARR_CNT(gcd_divisor))
+ if (mathomatic->n_tlhs > ARR_CNT(gcd_divisor))
return 0;
- blt(gcd_divisor, tlhs, n_tlhs * sizeof(token_type));
- len_d = n_tlhs;
- count = do_gcd(&v);
+ blt(gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ len_d = mathomatic->n_tlhs;
+ count = do_gcd(mathomatic, &v);
if (count <= 0)
return 0;
if (count > 1) {
- if (len_d > n_tokens)
+ if (len_d > mathomatic->n_tokens)
return 0;
- blt(tlhs, gcd_divisor, len_d * sizeof(token_type));
- n_tlhs = len_d;
- if (!remove_factors())
+ blt(mathomatic->tlhs, gcd_divisor, len_d * sizeof(token_type));
+ mathomatic->n_tlhs = len_d;
+ if (!remove_factors(mathomatic))
return 0;
- if (n_tlhs > ARR_CNT(gcd_divisor))
+ if (mathomatic->n_tlhs > ARR_CNT(gcd_divisor))
return 0;
- blt(gcd_divisor, tlhs, n_tlhs * sizeof(token_type));
- len_d = n_tlhs;
- if (poly_div(larger, llen, gcd_divisor, len_d, &v) != 2) {
- debug_string(1, "Polynomial GCD found, but larger divide failed in poly_gcd().");
+ blt(gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ len_d = mathomatic->n_tlhs;
+ if (poly_div(mathomatic, larger, llen, gcd_divisor, len_d, &v) != 2) {
+ debug_string(mathomatic, 1, "Polynomial GCD found, but larger divide failed in poly_gcd().");
return 0;
}
}
- if (len_d > n_tokens)
+ if (len_d > mathomatic->n_tokens)
return 0;
- blt(trhs, gcd_divisor, len_d * sizeof(token_type));
- n_trhs = len_d;
- uf_simp(tlhs, &n_tlhs);
- uf_simp(trhs, &n_trhs);
- debug_string(3, "poly_gcd() successful.");
+ blt(mathomatic->trhs, gcd_divisor, len_d * sizeof(token_type));
+ mathomatic->n_trhs = 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);
}
@@ -726,13 +721,13 @@ long v; /* polynomial base variable */
* Return smaller/GCD in trhs[].
*/
int
-poly2_gcd(larger, llen, smaller, slen, v, 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 */
+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;
@@ -760,65 +755,65 @@ int require_additive; /* require the GCD to contain addition or subtraction */
if (count == 0 /* || count > 200 */)
return 0;
}
- debug_string(3, "Entering poly2_gcd():");
- side_debug(3, larger, llen);
- side_debug(3, smaller, slen);
- if (llen > n_tokens || slen > min(ARR_CNT(gcd_divisor), n_tokens))
+ 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(gcd_divisor), mathomatic->n_tokens))
return 0;
- blt(trhs, larger, llen * sizeof(token_type));
- n_trhs = llen;
- blt(tlhs, smaller, slen * sizeof(token_type));
- n_tlhs = slen;
+ 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, jmp_save, sizeof(jmp_save));
- if ((i = setjmp(jmp_save)) != 0) { /* trap errors */
- blt(jmp_save, save_save, sizeof(jmp_save));
+ 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(jmp_save, i);
+ longjmp(mathomatic->jmp_save, i);
}
return 0;
}
- uf_simp(tlhs, &n_tlhs);
- blt(jmp_save, save_save, sizeof(jmp_save));
- if (level1_plus_count(tlhs, n_tlhs) == 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 (n_tlhs > ARR_CNT(gcd_divisor))
+ if (mathomatic->n_tlhs > ARR_CNT(gcd_divisor))
return 0;
- blt(gcd_divisor, tlhs, n_tlhs * sizeof(token_type));
- len_d = n_tlhs;
- count = do_gcd(&v);
+ blt(gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ len_d = mathomatic->n_tlhs;
+ count = do_gcd(mathomatic, &v);
if (count <= 0)
return count;
if (count > 1) {
- if (require_additive && level1_plus_count(gcd_divisor, len_d) == 0)
+ if (require_additive && level1_plus_count(mathomatic, gcd_divisor, len_d) == 0)
return 0;
- if (poly_div(smaller, slen, gcd_divisor, len_d, &v) != 2) {
- debug_string(1, "Polynomial GCD found, but smaller divide failed in poly2_gcd().");
+ if (poly_div(mathomatic, smaller, slen, gcd_divisor, len_d, &v) != 2) {
+ debug_string(mathomatic, 1, "Polynomial GCD found, but smaller divide failed in poly2_gcd().");
return 0;
}
- blt(trhs, gcd_divisor, len_d * sizeof(token_type));
- n_trhs = len_d;
- if (n_tlhs > ARR_CNT(gcd_divisor))
+ blt(mathomatic->trhs, gcd_divisor, len_d * sizeof(token_type));
+ mathomatic->n_trhs = len_d;
+ if (mathomatic->n_tlhs > ARR_CNT(gcd_divisor))
return 0;
- blt(gcd_divisor, tlhs, n_tlhs * sizeof(token_type));
- len_d = n_tlhs;
- blt(tlhs, trhs, n_trhs * sizeof(token_type));
- n_tlhs = n_trhs;
- if (poly_div(larger, llen, tlhs, n_tlhs, &v) != 2) {
- debug_string(1, "Polynomial GCD found, but larger divide failed in poly2_gcd().");
+ blt(gcd_divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ 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(trhs, gcd_divisor, len_d * sizeof(token_type));
- n_trhs = len_d;
+ blt(mathomatic->trhs, gcd_divisor, len_d * sizeof(token_type));
+ mathomatic->n_trhs = len_d;
} else {
- n_trhs = 1;
- trhs[0] = one_token;
+ mathomatic->n_trhs = 1;
+ mathomatic->trhs[0] = mathomatic->one_token;
}
- debug_string(3, "poly2_gcd() successful.");
+ debug_string(mathomatic, 3, "poly2_gcd() successful.");
return count;
}
@@ -829,19 +824,18 @@ int require_additive; /* require the GCD to contain addition or subtraction */
* Integer variable names start with "integer".
*/
int
-is_integer_var(v)
-long v;
+is_integer_var(MathoMatic* mathomatic, long v)
{
char *cp;
int (*strncmpfunc)();
- if (case_sensitive_flag) {
+ if (mathomatic->case_sensitive_flag) {
strncmpfunc = strncmp;
} else {
strncmpfunc = strncasecmp;
}
- cp = var_name(v);
+ cp = var_name(mathomatic, v);
if (cp && strncmpfunc(cp, V_INTEGER_PREFIX, strlen(V_INTEGER_PREFIX)) == 0)
return true;
else
@@ -857,16 +851,16 @@ long v;
* Should first be unfactored with uf_pplus() for a proper determination.
*/
int
-is_integer_expr(p1, n)
-token_type *p1; /* expression pointer */
-int n; /* length of expression */
+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("(p1 == NULL || n < 1) in is_integer_expr().");
+ error_bug(mathomatic, "(p1 == NULL || n < 1) in is_integer_expr().");
}
#endif
for (i = 0; i < n; i++) {
@@ -881,7 +875,7 @@ int n; /* length of expression */
break;
case VARIABLE:
v = labs(p1[i].token.variable);
- if (!is_integer_var(v) && (v & VAR_MASK) != SIGN)
+ if (!is_integer_var(mathomatic, v) && (v & VAR_MASK) != SIGN)
return false;
break;
}
@@ -897,17 +891,15 @@ int n; /* length of expression */
* Return true if equation side was modified.
*/
int
-mod_simp(equation, np)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of the equation side */
+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(equation, np, 0, 1);
+ return mod_recurse(mathomatic, equation, np, 0, 1);
}
static int
-mod_recurse(equation, np, loc, level)
-token_type *equation;
-int *np, loc, level;
+mod_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level)
{
int modified = false;
int i, j, k;
@@ -918,7 +910,7 @@ int *np, loc, level;
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
- modified |= mod_recurse(equation, np, i, level + 1);
+ modified |= mod_recurse(mathomatic, equation, np, i, level + 1);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
@@ -958,14 +950,14 @@ int *np, loc, level;
case MODULUS:
/* simplify (x%n)%n to x%n */
len3 = k - (i1 + 1);
- if (se_compare(&equation[i+1], len1, &equation[i1+1], len3, &diff_sign)) {
+ 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(&equation[j], len2))
+ 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) {
@@ -974,7 +966,7 @@ int *np, loc, level;
if (equation[i2].level == (level + 2)
&& equation[i2].token.operatr == MODULUS) {
len3 = i1 - (i2 + 1);
- if (se_compare(&equation[i+1], len1, &equation[i2+1], len3, &diff_sign)) {
+ 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;
@@ -997,7 +989,7 @@ int *np, loc, level;
switch (equation[i2].token.operatr) {
case MODULUS:
len3 = i1 - (i2 + 1);
- if (se_compare(&equation[i+1], len1, &equation[i2+1], len3, &diff_sign)) {
+ 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;
@@ -1005,7 +997,7 @@ int *np, loc, level;
break;
case TIMES:
i2 = i1 - 2;
- if (!is_integer_expr(&equation[i3+1], i1 - (i3 + 1))) {
+ if (!is_integer_expr(mathomatic, &equation[i3+1], i1 - (i3 + 1))) {
break;
}
for (i4 = i3 + 2; i4 < i1; i4 += 2) {
@@ -1014,7 +1006,7 @@ int *np, loc, level;
for (i5 = i4 + 2; i5 < i1 && equation[i5].level > (level + 3); i5 += 2)
;
len3 = i5 - (i4 + 1);
- if (se_compare(&equation[i+1], len1, &equation[i4+1], len3, &diff_sign)) {
+ if (se_compare(mathomatic, &equation[i+1], len1, &equation[i4+1], len3, &diff_sign)) {
blt(&equation[i4], &equation[i5], (*np - i5) * sizeof(token_type));
*np -= len3 + 1;
return true;
@@ -1036,19 +1028,19 @@ int *np, loc, level;
/* Remove integer*n multiples in x for x%n by doing */
/* polynomial division x/n and replacing with remainder%n. */
/* Globals tlhs[] and trhs[] are wiped out by the polynomial division here. */
- if (poly_div(&equation[j], len2, &equation[i+1], len1, NULL)) {
- uf_pplus(tlhs, &n_tlhs); /* so integer%(integer^integer) isn't simplified to 0 */
- if (is_integer_expr(tlhs, n_tlhs)) {
- if (n_trhs < len2 || REMAINDER_IS_ZERO()) { /* if it will make it smaller */
- if ((*np + (n_trhs - len2)) > n_tokens)
- error_huge();
- for (k = 0; k < n_trhs; k++)
- trhs[k].level += level;
- blt(&equation[j+n_trhs], &equation[j+len2], (*np - (j + len2)) * sizeof(token_type));
- *np += n_trhs - len2;
- blt(&equation[j], trhs, n_trhs * sizeof(token_type));
- debug_string(2, "Polynomial division successful in modulus simplification. The result is:");
- side_debug(2, equation, *np);
+ if (poly_div(mathomatic, &equation[j], len2, &equation[i+1], len1, NULL)) {
+ uf_pplus(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs); /* so integer%(integer^integer) isn't simplified to 0 */
+ if (is_integer_expr(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs)) {
+ if (mathomatic->n_trhs < len2 || REMAINDER_IS_ZERO()) { /* if it will make it smaller */
+ if ((*np + (mathomatic->n_trhs - len2)) > mathomatic->n_tokens)
+ error_huge(mathomatic);
+ for (k = 0; k < mathomatic->n_trhs; k++)
+ mathomatic->trhs[k].level += level;
+ blt(&equation[j+mathomatic->n_trhs], &equation[j+len2], (*np - (j + len2)) * sizeof(token_type));
+ *np += mathomatic->n_trhs - len2;
+ blt(&equation[j], mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
+ debug_string(mathomatic, 2, "Polynomial division successful in modulus simplification. The result is:");
+ side_debug(mathomatic, 2, equation, *np);
return true;
}
}
@@ -1069,17 +1061,13 @@ int *np, loc, level;
* and denominator by the GCD.
*/
int
-poly_gcd_simp(equation, np)
-token_type *equation;
-int *np;
+poly_gcd_simp(MathoMatic* mathomatic, token_type *equation, int *np)
{
- return polydiv_recurse(equation, np, 0, 1);
+ return polydiv_recurse(mathomatic, equation, np, 0, 1);
}
static int
-polydiv_recurse(equation, np, loc, level)
-token_type *equation;
-int *np, loc, level;
+polydiv_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level)
{
int modified = false;
int i, j, k;
@@ -1089,7 +1077,7 @@ int *np, loc, level;
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
- modified |= polydiv_recurse(equation, np, i, level + 1);
+ modified |= polydiv_recurse(mathomatic, equation, np, i, level + 1);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
@@ -1101,7 +1089,7 @@ start:
for (i = loc + 1; i < *np && equation[i].level >= level; i += 2) {
#if DEBUG
if (equation[i].kind != OPERATOR)
- error_bug("Bug in poly_gcd_simp().");
+ error_bug(mathomatic, "Bug in poly_gcd_simp().");
#endif
if (equation[i].level == level && equation[i].token.operatr == DIVIDE) {
for (k = i + 2;; k += 2) {
@@ -1122,7 +1110,7 @@ start:
case TIMES:
break;
default:
- error_bug("Expression is corrupt in poly_gcd_simp().");
+ error_bug(mathomatic, "Expression is corrupt in poly_gcd_simp().");
}
last_op2 = DIVIDE;
for (k = j + 1;; k += 2) {
@@ -1130,28 +1118,28 @@ start:
break;
}
len2 = k - j;
- if ((rv = poly2_gcd(&equation[i+1], len1, &equation[j], len2, 0L, true)) > 0) {
+ if ((rv = poly2_gcd(mathomatic, &equation[i+1], len1, &equation[j], len2, 0L, true)) > 0) {
store_code:
- for (k = 0; k < n_tlhs; k++)
- tlhs[k].level += level;
- for (k = 0; k < n_trhs; k++)
- trhs[k].level += level;
- if (((*np + (n_trhs - len2)) > n_tokens)
- || ((*np + (n_trhs - len2) + (n_tlhs - len1)) > n_tokens))
- error_huge();
- blt(&equation[j+n_trhs], &equation[j+len2], (*np - (j + len2)) * sizeof(token_type));
- *np += n_trhs - len2;
+ for (k = 0; k < mathomatic->n_tlhs; k++)
+ mathomatic->tlhs[k].level += level;
+ for (k = 0; k < mathomatic->n_trhs; k++)
+ mathomatic->trhs[k].level += level;
+ if (((*np + (mathomatic->n_trhs - len2)) > mathomatic->n_tokens)
+ || ((*np + (mathomatic->n_trhs - len2) + (mathomatic->n_tlhs - len1)) > mathomatic->n_tokens))
+ error_huge(mathomatic);
+ blt(&equation[j+mathomatic->n_trhs], &equation[j+len2], (*np - (j + len2)) * sizeof(token_type));
+ *np += mathomatic->n_trhs - len2;
if (i > j)
- i += n_trhs - len2;
- blt(&equation[j], trhs, n_trhs * sizeof(token_type));
- blt(&equation[i+n_tlhs+1], &equation[i+1+len1], (*np - (i + 1 + len1)) * sizeof(token_type));
- *np += n_tlhs - len1;
- blt(&equation[i+1], tlhs, n_tlhs * sizeof(token_type));
- debug_string(1, _("Division simplified with polynomial GCD."));
+ i += mathomatic->n_trhs - len2;
+ blt(&equation[j], mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
+ blt(&equation[i+mathomatic->n_tlhs+1], &equation[i+1+len1], (*np - (i + 1 + len1)) * sizeof(token_type));
+ *np += mathomatic->n_tlhs - len1;
+ blt(&equation[i+1], mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ debug_string(mathomatic, 1, _("Division simplified with polynomial GCD."));
modified = true;
goto start;
}
- if (rv == 0 && poly2_gcd(&equation[j], len2, &equation[i+1], len1, 0L, true) > 0) {
+ if (rv == 0 && poly2_gcd(mathomatic, &equation[j], len2, &equation[i+1], len1, 0L, true) > 0) {
k = j - 1;
j = i + 1;
i = k;
@@ -1179,28 +1167,24 @@ store_code:
* Return true if expression was simplified.
*/
int
-div_remainder(equation, np, poly_flag, quick_flag)
-token_type *equation;
-int *np;
-int poly_flag; /* if true, try polynomial division first, then smart division */
-int quick_flag; /* if true, keep algebraic fractions simpler */
+div_remainder(MathoMatic* mathomatic, token_type *equation, int *np, int poly_flag, int quick_flag)
+//int poly_flag; /* if true, try polynomial division first, then smart division */
+//int quick_flag; /* if true, keep algebraic fractions simpler */
{
int rv = false;
- debug_string(3, "Entering div_remainder().");
+ debug_string(mathomatic, 3, "Entering div_remainder().");
if (quick_flag)
- group_proc(equation, np);
- rv = pdiv_recurse(equation, np, 0, 1, poly_flag);
+ group_proc(mathomatic, equation, np);
+ rv = pdiv_recurse(mathomatic, equation, np, 0, 1, poly_flag);
if (quick_flag)
- organize(equation, np);
- debug_string(3, "Leaving div_remainder().");
+ organize(mathomatic, equation, np);
+ debug_string(mathomatic, 3, "Leaving div_remainder().");
return rv;
}
static int
-pdiv_recurse(equation, np, loc, level, code)
-token_type *equation;
-int *np, loc, level, code;
+pdiv_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int code)
{
int modified = false;
int i, j, k;
@@ -1264,51 +1248,51 @@ next_thingy:
len1 = real_len1;
}
if (flag || power_flag) {
- rv = poly_div(&equation[j], len2, &equation[i+1], len1, NULL);
+ rv = poly_div(mathomatic, &equation[j], len2, &equation[i+1], len1, NULL);
} else {
- rv = smart_div(&equation[j], len2, &equation[i+1], len1);
+ rv = smart_div(mathomatic, &equation[j], len2, &equation[i+1], len1);
}
zero_remainder = (rv > 0 && REMAINDER_IS_ZERO());
if (power_flag && !zero_remainder) {
rv = 0;
}
if (rv > 0) { /* if successful and the result is smaller than the original: */
- if ((n_tlhs + 2 + n_trhs + len1) > n_tokens)
- error_huge();
- for (k = 0; k < n_tlhs; k++)
- tlhs[k].level++;
- tlhs[n_tlhs].level = 1;
- tlhs[n_tlhs].kind = OPERATOR;
- tlhs[n_tlhs].token.operatr = PLUS;
- n_tlhs++;
- for (k = 0; k < n_trhs; k++)
- trhs[k].level += 2;
- blt(&tlhs[n_tlhs], trhs, n_trhs * sizeof(token_type));
- n_tlhs += n_trhs;
- tlhs[n_tlhs].level = 2;
- tlhs[n_tlhs].kind = OPERATOR;
- tlhs[n_tlhs].token.operatr = DIVIDE;
- n_tlhs++;
- k = n_tlhs;
- blt(&tlhs[n_tlhs], &equation[i+1], len1 * sizeof(token_type));
- n_tlhs += len1;
- for (; k < n_tlhs; k++)
- tlhs[k].level += 2;
- side_debug(3, &equation[j], len2);
- side_debug(3, &equation[i+1], len1);
- simpb_side(tlhs, &n_tlhs, false, true, 3); /* parameters should match what's used in simpa_side() */
- side_debug(3, tlhs, n_tlhs);
+ if ((mathomatic->n_tlhs + 2 + mathomatic->n_trhs + len1) > mathomatic->n_tokens)
+ error_huge(mathomatic);
+ for (k = 0; k < mathomatic->n_tlhs; k++)
+ mathomatic->tlhs[k].level++;
+ mathomatic->tlhs[mathomatic->n_tlhs].level = 1;
+ mathomatic->tlhs[mathomatic->n_tlhs].kind = OPERATOR;
+ mathomatic->tlhs[mathomatic->n_tlhs].token.operatr = PLUS;
+ mathomatic->n_tlhs++;
+ for (k = 0; k < mathomatic->n_trhs; k++)
+ mathomatic->trhs[k].level += 2;
+ blt(&mathomatic->tlhs[mathomatic->n_tlhs], mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
+ mathomatic->n_tlhs += mathomatic->n_trhs;
+ mathomatic->tlhs[mathomatic->n_tlhs].level = 2;
+ mathomatic->tlhs[mathomatic->n_tlhs].kind = OPERATOR;
+ mathomatic->tlhs[mathomatic->n_tlhs].token.operatr = DIVIDE;
+ mathomatic->n_tlhs++;
+ k = mathomatic->n_tlhs;
+ blt(&mathomatic->tlhs[mathomatic->n_tlhs], &equation[i+1], len1 * sizeof(token_type));
+ mathomatic->n_tlhs += len1;
+ for (; k < mathomatic->n_tlhs; k++)
+ mathomatic->tlhs[k].level += 2;
+ side_debug(mathomatic, 3, &equation[j], len2);
+ side_debug(mathomatic, 3, &equation[i+1], len1);
+ simpb_side(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, false, true, 3); /* parameters should match what's used in simpa_side() */
+ side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
if (power_flag) {
- k = (var_count(tlhs, n_tlhs) <= var_count(&equation[j], len2));
+ k = (var_count(mathomatic->tlhs, mathomatic->n_tlhs) <= var_count(&equation[j], len2));
} else {
- k = ((var_count(tlhs, n_tlhs) + (n_tlhs >= len1 + 1 + len2)) <= (var_count(&equation[j], len2) + var_count(&equation[i+1], len1)));
+ k = ((var_count(mathomatic->tlhs, mathomatic->n_tlhs) + (mathomatic->n_tlhs >= len1 + 1 + len2)) <= (var_count(&equation[j], len2) + var_count(&equation[i+1], len1)));
}
if (k) {
- for (k = 0; k < n_tlhs; k++)
- tlhs[k].level += level;
+ for (k = 0; k < mathomatic->n_tlhs; k++)
+ mathomatic->tlhs[k].level += level;
if (power_flag) {
- if ((*np - len2 + n_tlhs + 2) > n_tokens)
- error_huge();
+ if ((*np - len2 + mathomatic->n_tlhs + 2) > mathomatic->n_tokens)
+ error_huge(mathomatic);
for (k = i + 2 + len1; k <= i + real_len1; k++) {
equation[k].level++;
}
@@ -1325,23 +1309,23 @@ next_thingy:
j += 2;
}
} else {
- if ((*np - (len1 + 1 + len2) + n_tlhs) > n_tokens)
- error_huge();
+ if ((*np - (len1 + 1 + len2) + mathomatic->n_tlhs) > mathomatic->n_tokens)
+ error_huge(mathomatic);
blt(&equation[i], &equation[i+1+len1], (*np - (i + 1 + len1)) * sizeof(token_type));
*np -= len1 + 1;
if (i < j) {
j -= len1 + 1;
}
}
- blt(&equation[j+n_tlhs], &equation[j+len2], (*np - (j + len2)) * sizeof(token_type));
- *np -= len2 - n_tlhs;
- blt(&equation[j], tlhs, n_tlhs * sizeof(token_type));
+ blt(&equation[j+mathomatic->n_tlhs], &equation[j+len2], (*np - (j + len2)) * sizeof(token_type));
+ *np -= len2 - mathomatic->n_tlhs;
+ blt(&equation[j], mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
if (flag || power_flag) {
- debug_string(1, _("Polynomial division successful."));
+ debug_string(mathomatic, 1, _("Polynomial division successful."));
} else {
- debug_string(1, _("Smart division successful."));
+ debug_string(mathomatic, 1, _("Smart division successful."));
}
- side_debug(3, equation, *np);
+ side_debug(mathomatic, 3, equation, *np);
return true;
}
}
@@ -1357,7 +1341,7 @@ next_thingy:
}
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
- modified |= pdiv_recurse(equation, np, i, level + 1, code);
+ modified |= pdiv_recurse(mathomatic, equation, np, i, level + 1, code);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
@@ -1381,32 +1365,32 @@ next_thingy:
* If *vp is 0, automatically select the best polynomial base variable and return it in *vp.
*/
int
-poly_div(d1, len1, d2, len2, vp)
-token_type *d1; /* pointer to dividend */
-int len1; /* length of dividend */
-token_type *d2; /* pointer to divisor */
-int len2; /* length of divisor */
-long *vp; /* pointer to polynomial base variable */
+poly_div(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2, long *vp)
+//token_type *d1; /* pointer to dividend */
+//int len1; /* length of dividend */
+//token_type *d2; /* pointer to divisor */
+//int len2; /* length of divisor */
+//long *vp; /* pointer to polynomial base variable */
{
int i;
int rv;
int old_partial;
jmp_buf save_save;
- old_partial = partial_flag;
- partial_flag = false; /* We want full unfactoring during polynomial division. */
- blt(save_save, jmp_save, sizeof(jmp_save));
- if ((i = setjmp(jmp_save)) != 0) { /* Trap errors so we almost always return normally. */
- blt(jmp_save, save_save, sizeof(jmp_save));
- partial_flag = old_partial;
+ old_partial = mathomatic->partial_flag;
+ mathomatic->partial_flag = false; /* We want full unfactoring during polynomial division. */
+ blt(save_save, mathomatic->jmp_save, sizeof(mathomatic->jmp_save));
+ if ((i = setjmp(mathomatic->jmp_save)) != 0) { /* Trap errors so we almost always return normally. */
+ blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
+ mathomatic->partial_flag = old_partial;
if (i == 13) { /* critical error code */
- longjmp(jmp_save, i);
+ longjmp(mathomatic->jmp_save, i);
}
return false;
}
- rv = poly_div_sub(d1, len1, d2, len2, vp);
- blt(jmp_save, save_save, sizeof(jmp_save));
- partial_flag = old_partial;
+ rv = poly_div_sub(mathomatic, d1, len1, d2, len2, vp);
+ blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
+ mathomatic->partial_flag = old_partial;
return rv;
}
@@ -1417,12 +1401,7 @@ long *vp; /* pointer to polynomial base variable */
* generalized, polynomial long division algorithm.
*/
static int
-poly_div_sub(d1, len1, d2, len2, vp)
-token_type *d1;
-int len1;
-token_type *d2;
-int len2;
-long *vp;
+poly_div_sub(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2, long *vp)
{
int i;
int t1, len_t1;
@@ -1437,42 +1416,42 @@ long *vp;
if (vp == NULL)
vp = &tmp_v;
- if (len1 > n_tokens || len2 > n_tokens)
+ if (len1 > mathomatic->n_tokens || len2 > mathomatic->n_tokens)
return false;
/* Copy the source polynomials to where we want them (tlhs and trhs). */
- if (trhs != d1) {
- blt(trhs, d1, len1 * sizeof(token_type));
+ if (mathomatic->trhs != d1) {
+ blt(mathomatic->trhs, d1, len1 * sizeof(token_type));
}
- n_trhs = len1;
- if (tlhs != d2) {
- blt(tlhs, d2, len2 * sizeof(token_type));
+ mathomatic->n_trhs = len1;
+ if (mathomatic->tlhs != d2) {
+ blt(mathomatic->tlhs, d2, len2 * sizeof(token_type));
}
- n_tlhs = len2;
+ mathomatic->n_tlhs = len2;
/* Do the basic unfactoring and simplification of the dividend and divisor. */
- uf_simp(trhs, &n_trhs);
- uf_simp(tlhs, &n_tlhs);
+ uf_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ uf_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
if (*vp == 0) {
/* Select the best polynomial base variable. */
- if (!find_highest_count(trhs, n_trhs, tlhs, n_tlhs, vp))
+ if (!find_highest_count(mathomatic->trhs, mathomatic->n_trhs, mathomatic->tlhs, mathomatic->n_tlhs, vp))
return false;
}
#if !SILENT
/* Display debugging info. */
- if (debug_level >= 3) {
- list_var(*vp, 0);
- fprintf(gfp, _("poly_div() starts using base variable %s:\n"), var_str);
- side_debug(3, trhs, n_trhs);
- side_debug(3, tlhs, n_tlhs);
+ if (mathomatic->debug_level >= 3) {
+ list_var(mathomatic, *vp, 0);
+ fprintf(mathomatic->gfp, _("poly_div() starts using base variable %s:\n"), mathomatic->var_str);
+ side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
+ side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
}
#endif
/* Determine divide_flag and if the polynomials can be divided. */
divide_flag = 2;
- last_count = find_greatest_power(trhs, n_trhs, vp, &last_power, &t1, &len_t1, ÷_flag);
- divisor_count = find_greatest_power(tlhs, n_tlhs, vp, &divisor_power, &t2, &len_t2, ÷_flag);
+ last_count = find_greatest_power(mathomatic->trhs, mathomatic->n_trhs, vp, &last_power, &t1, &len_t1, ÷_flag);
+ divisor_count = find_greatest_power(mathomatic->tlhs, mathomatic->n_tlhs, vp, &divisor_power, &t2, &len_t2, ÷_flag);
if (divisor_power <= 0 || last_power < divisor_power) {
divide_flag = !divide_flag;
- last_count = find_greatest_power(trhs, n_trhs, vp, &last_power, &t1, &len_t1, ÷_flag);
- divisor_count = find_greatest_power(tlhs, n_tlhs, vp, &divisor_power, &t2, &len_t2, ÷_flag);
+ last_count = find_greatest_power(mathomatic->trhs, mathomatic->n_trhs, vp, &last_power, &t1, &len_t1, ÷_flag);
+ divisor_count = find_greatest_power(mathomatic->tlhs, mathomatic->n_tlhs, vp, &divisor_power, &t2, &len_t2, ÷_flag);
if (divisor_power <= 0 || last_power < divisor_power) {
return false;
}
@@ -1482,16 +1461,16 @@ long *vp;
/* Initialize the quotient. */
n_quotient = 1;
- quotient[0] = zero_token;
+ quotient[0] = mathomatic->zero_token;
/* Store the divisor. */
- if (n_tlhs > ARR_CNT(divisor))
+ if (mathomatic->n_tlhs > ARR_CNT(divisor))
return false;
- blt(divisor, tlhs, n_tlhs * sizeof(token_type));
- n_divisor = n_tlhs;
- sum_size = n_trhs + n_quotient;
+ blt(divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ n_divisor = mathomatic->n_tlhs;
+ sum_size = mathomatic->n_trhs + n_quotient;
/* Loop until polynomial division is finished. */
for (;;) {
- if (t1 > 0 && trhs[t1-1].token.operatr == MINUS)
+ if (t1 > 0 && mathomatic->trhs[t1-1].token.operatr == MINUS)
sign = MINUS;
else
sign = PLUS;
@@ -1501,83 +1480,83 @@ long *vp;
else
sign = MINUS;
}
- if ((len_t1 + len_t2 + 1) > n_tokens)
+ if ((len_t1 + len_t2 + 1) > mathomatic->n_tokens)
return false;
- blt(tlhs, &trhs[t1], len_t1 * sizeof(token_type));
- n_tlhs = len_t1;
- for (i = 0; i < n_tlhs; i++)
- tlhs[i].level++;
- tlhs[n_tlhs].level = 1;
- tlhs[n_tlhs].kind = OPERATOR;
- tlhs[n_tlhs].token.operatr = DIVIDE;
- n_tlhs++;
- blt(&tlhs[n_tlhs], &divisor[t2], len_t2 * sizeof(token_type));
- i = n_tlhs;
- n_tlhs += len_t2;
- for (; i < n_tlhs; i++)
- tlhs[i].level++;
- if (!simp_loop(tlhs, &n_tlhs))
+ blt(mathomatic->tlhs, &mathomatic->trhs[t1], len_t1 * sizeof(token_type));
+ mathomatic->n_tlhs = len_t1;
+ for (i = 0; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
+ mathomatic->tlhs[mathomatic->n_tlhs].level = 1;
+ mathomatic->tlhs[mathomatic->n_tlhs].kind = OPERATOR;
+ mathomatic->tlhs[mathomatic->n_tlhs].token.operatr = DIVIDE;
+ mathomatic->n_tlhs++;
+ blt(&mathomatic->tlhs[mathomatic->n_tlhs], &divisor[t2], len_t2 * sizeof(token_type));
+ i = mathomatic->n_tlhs;
+ mathomatic->n_tlhs += len_t2;
+ for (; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
+ if (!simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs))
return false;
- if ((n_quotient + 1 + n_tlhs) > min(ARR_CNT(quotient), n_tokens))
+ if ((n_quotient + 1 + mathomatic->n_tlhs) > min(ARR_CNT(quotient), mathomatic->n_tokens))
return false;
- for (i = 0; i < n_tlhs; i++)
- tlhs[i].level++;
+ for (i = 0; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
quotient[n_quotient].level = 1;
quotient[n_quotient].kind = OPERATOR;
quotient[n_quotient].token.operatr = sign;
n_quotient++;
- blt("ient[n_quotient], tlhs, n_tlhs * sizeof(token_type));
- n_quotient += n_tlhs;
- if ((n_trhs + n_tlhs + n_divisor + 2) > n_tokens)
+ blt("ient[n_quotient], mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ n_quotient += mathomatic->n_tlhs;
+ if ((mathomatic->n_trhs + mathomatic->n_tlhs + n_divisor + 2) > mathomatic->n_tokens)
return false;
- blt(&trhs[t1+1], &trhs[t1+len_t1], (n_trhs - (t1 + len_t1)) * sizeof(token_type));
- n_trhs -= len_t1 - 1;
- trhs[t1] = zero_token;
- for (i = 0; i < n_trhs; i++)
- trhs[i].level++;
- trhs[n_trhs].level = 1;
- trhs[n_trhs].kind = OPERATOR;
+ blt(&mathomatic->trhs[t1+1], &mathomatic->trhs[t1+len_t1], (mathomatic->n_trhs - (t1 + len_t1)) * sizeof(token_type));
+ mathomatic->n_trhs -= len_t1 - 1;
+ mathomatic->trhs[t1] = mathomatic->zero_token;
+ for (i = 0; i < mathomatic->n_trhs; i++)
+ mathomatic->trhs[i].level++;
+ mathomatic->trhs[mathomatic->n_trhs].level = 1;
+ mathomatic->trhs[mathomatic->n_trhs].kind = OPERATOR;
if (sign == PLUS)
- trhs[n_trhs].token.operatr = MINUS;
+ mathomatic->trhs[mathomatic->n_trhs].token.operatr = MINUS;
else
- trhs[n_trhs].token.operatr = PLUS;
- n_trhs++;
- blt(&trhs[n_trhs], tlhs, n_tlhs * sizeof(token_type));
- i = n_trhs;
- n_trhs += n_tlhs;
- for (; i < n_trhs; i++)
- trhs[i].level++;
- trhs[n_trhs].level = 2;
- trhs[n_trhs].kind = OPERATOR;
- trhs[n_trhs].token.operatr = TIMES;
- n_trhs++;
- i = n_trhs;
- blt(&trhs[n_trhs], divisor, t2 * sizeof(token_type));
- n_trhs += t2;
- trhs[n_trhs] = zero_token;
- n_trhs++;
- blt(&trhs[n_trhs], &divisor[t2+len_t2], (n_divisor - (t2 + len_t2)) * sizeof(token_type));
- n_trhs += (n_divisor - (t2 + len_t2));
- for (; i < n_trhs; i++)
- trhs[i].level += 2;
- side_debug(3, trhs, n_trhs);
- uf_repeat(trhs, &n_trhs);
- uf_tsimp(trhs, &n_trhs);
- side_debug(4, trhs, n_trhs);
- count = find_greatest_power(trhs, n_trhs, vp, &d, &t1, &len_t1, ÷_flag);
+ mathomatic->trhs[mathomatic->n_trhs].token.operatr = PLUS;
+ mathomatic->n_trhs++;
+ blt(&mathomatic->trhs[mathomatic->n_trhs], mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ i = mathomatic->n_trhs;
+ mathomatic->n_trhs += mathomatic->n_tlhs;
+ for (; i < mathomatic->n_trhs; i++)
+ mathomatic->trhs[i].level++;
+ mathomatic->trhs[mathomatic->n_trhs].level = 2;
+ mathomatic->trhs[mathomatic->n_trhs].kind = OPERATOR;
+ mathomatic->trhs[mathomatic->n_trhs].token.operatr = TIMES;
+ mathomatic->n_trhs++;
+ i = mathomatic->n_trhs;
+ blt(&mathomatic->trhs[mathomatic->n_trhs], divisor, t2 * sizeof(token_type));
+ mathomatic->n_trhs += t2;
+ mathomatic->trhs[mathomatic->n_trhs] = mathomatic->zero_token;
+ mathomatic->n_trhs++;
+ blt(&mathomatic->trhs[mathomatic->n_trhs], &divisor[t2+len_t2], (n_divisor - (t2 + len_t2)) * sizeof(token_type));
+ mathomatic->n_trhs += (n_divisor - (t2 + len_t2));
+ for (; i < mathomatic->n_trhs; i++)
+ mathomatic->trhs[i].level += 2;
+ side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
+ uf_repeat(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ uf_tsimp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ side_debug(mathomatic, 4, mathomatic->trhs, mathomatic->n_trhs);
+ count = find_greatest_power(mathomatic->trhs, mathomatic->n_trhs, vp, &d, &t1, &len_t1, ÷_flag);
if (d < divisor_power) {
/* Success! Polynomial division ends here. */
- debug_string(3, "Successful polynomial division!");
- blt(tlhs, quotient, n_quotient * sizeof(token_type));
- n_tlhs = n_quotient;
- debug_string(3, "Quotient:");
- side_debug(3, tlhs, n_tlhs);
- debug_string(3, "Remainder:");
- side_debug(3, trhs, n_trhs);
+ debug_string(mathomatic, 3, "Successful polynomial division!");
+ blt(mathomatic->tlhs, quotient, n_quotient * sizeof(token_type));
+ mathomatic->n_tlhs = n_quotient;
+ debug_string(mathomatic, 3, "Quotient:");
+ side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
+ debug_string(mathomatic, 3, "Remainder:");
+ side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
if (REMAINDER_IS_ZERO())
return 2;
- if ((n_trhs + n_quotient) >= (sum_size /* - (sum_size / 10) */)) {
- if ((n_trhs + 1) > sum_size && n_trhs > n_divisor)
+ if ((mathomatic->n_trhs + n_quotient) >= (sum_size /* - (sum_size / 10) */)) {
+ if ((mathomatic->n_trhs + 1) > sum_size && mathomatic->n_trhs > n_divisor)
return -2;
else
return -1;
@@ -1615,11 +1594,11 @@ long *vp;
* Quotient is returned in tlhs[] and remainder in trhs[].
*/
int
-smart_div(d1, len1, d2, len2)
-token_type *d1; /* pointer to dividend */
-int len1; /* length of dividend */
-token_type *d2; /* pointer to divisor */
-int len2; /* length of divisor */
+smart_div(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2)
+//token_type *d1; /* pointer to dividend */
+//int len1; /* length of dividend */
+//token_type *d2; /* pointer to divisor */
+//int len2; /* length of divisor */
{
int i, j, k;
int t1, len_t1;
@@ -1637,52 +1616,52 @@ int len2; /* length of divisor */
int dcount = 0; /* divisor term count */
int flag;
- blt(trhs, d1, len1 * sizeof(token_type));
- n_trhs = len1;
- blt(tlhs, d2, len2 * sizeof(token_type));
- n_tlhs = len2;
+ blt(mathomatic->trhs, d1, len1 * sizeof(token_type));
+ mathomatic->n_trhs = len1;
+ blt(mathomatic->tlhs, d2, len2 * sizeof(token_type));
+ mathomatic->n_tlhs = len2;
/* Do the basic unfactoring and simplification of the dividend and divisor. */
- uf_simp_no_repeat(trhs, &n_trhs);
- uf_simp_no_repeat(tlhs, &n_tlhs);
+ uf_simp_no_repeat(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ uf_simp_no_repeat(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
/* Display debugging info. */
- debug_string(3, "smart_div() starts:");
- side_debug(3, trhs, n_trhs);
- side_debug(3, tlhs, n_tlhs);
+ debug_string(mathomatic, 3, "smart_div() starts:");
+ side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
+ side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
/* Find which divisor term to use. */
for (i = 0, j = 0, k = 0, flag = false;; i++) {
- if (i >= n_tlhs || (tlhs[i].kind == OPERATOR && tlhs[i].level == 1
- && (tlhs[i].token.operatr == PLUS || tlhs[i].token.operatr == MINUS))) {
+ if (i >= mathomatic->n_tlhs || (mathomatic->tlhs[i].kind == OPERATOR && mathomatic->tlhs[i].level == 1
+ && (mathomatic->tlhs[i].token.operatr == PLUS || mathomatic->tlhs[i].token.operatr == MINUS))) {
dcount++;
if (flag) {
- if (len_t2 == 0 || var_count(&tlhs[j], i - j) < k) {
+ if (len_t2 == 0 || var_count(&mathomatic->tlhs[j], i - j) < k) {
len_t2 = i - j;
t2 = j;
- k = var_count(&tlhs[t2], len_t2);
+ k = var_count(&mathomatic->tlhs[t2], len_t2);
}
}
flag = false;
j = i + 1;
- } else if (tlhs[i].kind == VARIABLE && tlhs[i].token.variable != IMAGINARY) {
+ } else if (mathomatic->tlhs[i].kind == VARIABLE && mathomatic->tlhs[i].token.variable != IMAGINARY) {
flag = true;
}
- if (i >= n_tlhs)
+ if (i >= mathomatic->n_tlhs)
break;
}
if (len_t2 <= 0)
return false;
/* Initialize the quotient. */
n_quotient = 1;
- quotient[0] = zero_token;
- if (n_tlhs > ARR_CNT(divisor))
+ quotient[0] = mathomatic->zero_token;
+ if (mathomatic->n_tlhs > ARR_CNT(divisor))
return false;
- blt(divisor, tlhs, n_tlhs * sizeof(token_type));
- n_divisor = n_tlhs;
+ blt(divisor, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ n_divisor = mathomatic->n_tlhs;
try_one:
- trhs_size = n_trhs;
+ trhs_size = mathomatic->n_trhs;
for (skip_count = 0, count = 0;;) {
- sum_size = n_trhs + n_quotient;
+ sum_size = mathomatic->n_trhs + n_quotient;
for (term_count = 1, q_size = 0;; term_count++) {
- if (!get_term(trhs, n_trhs, term_count, &t1, &len_t1))
+ if (!get_term(mathomatic->trhs, mathomatic->n_trhs, term_count, &t1, &len_t1))
break;
flag = false;
for (i = 0; i < skip_count; i++) {
@@ -1693,25 +1672,25 @@ try_one:
}
if (flag)
continue;
- if ((len_t1 + len_t2 + 1) > n_tokens)
+ if ((len_t1 + len_t2 + 1) > mathomatic->n_tokens)
return false;
- blt(tlhs, &trhs[t1], len_t1 * sizeof(token_type));
- n_tlhs = len_t1;
- for (i = 0; i < n_tlhs; i++)
- tlhs[i].level++;
- tlhs[n_tlhs].level = 1;
- tlhs[n_tlhs].kind = OPERATOR;
- tlhs[n_tlhs].token.operatr = DIVIDE;
- n_tlhs++;
- blt(&tlhs[n_tlhs], &divisor[t2], len_t2 * sizeof(token_type));
- i = n_tlhs;
- n_tlhs += len_t2;
- for (; i < n_tlhs; i++)
- tlhs[i].level++;
- if (!simp_loop(tlhs, &n_tlhs))
+ blt(mathomatic->tlhs, &mathomatic->trhs[t1], len_t1 * sizeof(token_type));
+ mathomatic->n_tlhs = len_t1;
+ for (i = 0; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
+ mathomatic->tlhs[mathomatic->n_tlhs].level = 1;
+ mathomatic->tlhs[mathomatic->n_tlhs].kind = OPERATOR;
+ mathomatic->tlhs[mathomatic->n_tlhs].token.operatr = DIVIDE;
+ mathomatic->n_tlhs++;
+ blt(&mathomatic->tlhs[mathomatic->n_tlhs], &divisor[t2], len_t2 * sizeof(token_type));
+ i = mathomatic->n_tlhs;
+ mathomatic->n_tlhs += len_t2;
+ for (; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
+ if (!simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs))
continue;
- if (basic_size(tlhs, n_tlhs) <= basic_size(&trhs[t1], len_t1)) {
- q_size = n_tlhs;
+ if (basic_size(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs) <= basic_size(mathomatic, &mathomatic->trhs[t1], len_t1)) {
+ q_size = mathomatic->n_tlhs;
term_pos = t1;
term_size = len_t1;
break;
@@ -1729,20 +1708,20 @@ try_one:
}
end_div:
if (dcount > 1) {
- if (n_quotient + n_trhs >= trhs_size + 1) {
+ if (n_quotient + mathomatic->n_trhs >= trhs_size + 1) {
return false;
}
}
end_div2:
- blt(tlhs, quotient, n_quotient * sizeof(token_type));
- n_tlhs = n_quotient;
- side_debug(3, tlhs, n_tlhs);
- side_debug(3, trhs, n_trhs);
+ blt(mathomatic->tlhs, quotient, n_quotient * sizeof(token_type));
+ mathomatic->n_tlhs = n_quotient;
+ side_debug(mathomatic, 3, mathomatic->tlhs, mathomatic->n_tlhs);
+ side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
return true; /* Success! */
}
t1 = term_pos;
len_t1 = term_size;
- if (t1 > 0 && trhs[t1-1].token.operatr == MINUS)
+ if (t1 > 0 && mathomatic->trhs[t1-1].token.operatr == MINUS)
sign = MINUS;
else
sign = PLUS;
@@ -1752,96 +1731,96 @@ end_div2:
else
sign = MINUS;
}
- if ((len_t1 + len_t2 + 1) > n_tokens)
+ if ((len_t1 + len_t2 + 1) > mathomatic->n_tokens)
return false;
- blt(tlhs, &trhs[t1], len_t1 * sizeof(token_type));
- n_tlhs = len_t1;
- for (i = 0; i < n_tlhs; i++)
- tlhs[i].level++;
- tlhs[n_tlhs].level = 1;
- tlhs[n_tlhs].kind = OPERATOR;
- tlhs[n_tlhs].token.operatr = DIVIDE;
- n_tlhs++;
- blt(&tlhs[n_tlhs], &divisor[t2], len_t2 * sizeof(token_type));
- i = n_tlhs;
- n_tlhs += len_t2;
- for (; i < n_tlhs; i++)
- tlhs[i].level++;
- simp_loop(tlhs, &n_tlhs);
- if ((n_quotient + 1 + n_tlhs) > min(ARR_CNT(quotient), n_tokens))
+ blt(mathomatic->tlhs, &mathomatic->trhs[t1], len_t1 * sizeof(token_type));
+ mathomatic->n_tlhs = len_t1;
+ for (i = 0; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
+ mathomatic->tlhs[mathomatic->n_tlhs].level = 1;
+ mathomatic->tlhs[mathomatic->n_tlhs].kind = OPERATOR;
+ mathomatic->tlhs[mathomatic->n_tlhs].token.operatr = DIVIDE;
+ mathomatic->n_tlhs++;
+ blt(&mathomatic->tlhs[mathomatic->n_tlhs], &divisor[t2], len_t2 * sizeof(token_type));
+ i = mathomatic->n_tlhs;
+ mathomatic->n_tlhs += len_t2;
+ for (; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
+ simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ if ((n_quotient + 1 + mathomatic->n_tlhs) > min(ARR_CNT(quotient), mathomatic->n_tokens))
return false;
- for (i = 0; i < n_tlhs; i++)
- tlhs[i].level++;
+ for (i = 0; i < mathomatic->n_tlhs; i++)
+ mathomatic->tlhs[i].level++;
old_n_quotient = n_quotient;
quotient[n_quotient].level = 1;
quotient[n_quotient].kind = OPERATOR;
quotient[n_quotient].token.operatr = sign;
n_quotient++;
qp = "ient[n_quotient];
- q_size = n_tlhs;
- blt("ient[n_quotient], tlhs, n_tlhs * sizeof(token_type));
- n_quotient += n_tlhs;
- if ((n_trhs + q_size + n_divisor + 2) > n_tokens)
+ q_size = mathomatic->n_tlhs;
+ blt("ient[n_quotient], mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ n_quotient += mathomatic->n_tlhs;
+ if ((mathomatic->n_trhs + q_size + n_divisor + 2) > mathomatic->n_tokens)
return false;
- blt(tlhs, trhs, n_trhs * sizeof(token_type));
- n_tlhs = n_trhs;
- blt(&trhs[t1+1], &trhs[t1+len_t1], (n_trhs - (t1 + len_t1)) * sizeof(token_type));
- n_trhs -= len_t1 - 1;
- trhs[t1] = zero_token;
- for (i = 0; i < n_trhs; i++)
- trhs[i].level++;
- trhs[n_trhs].level = 1;
- trhs[n_trhs].kind = OPERATOR;
+ blt(mathomatic->tlhs, mathomatic->trhs, mathomatic->n_trhs * sizeof(token_type));
+ mathomatic->n_tlhs = mathomatic->n_trhs;
+ blt(&mathomatic->trhs[t1+1], &mathomatic->trhs[t1+len_t1], (mathomatic->n_trhs - (t1 + len_t1)) * sizeof(token_type));
+ mathomatic->n_trhs -= len_t1 - 1;
+ mathomatic->trhs[t1] = mathomatic->zero_token;
+ for (i = 0; i < mathomatic->n_trhs; i++)
+ mathomatic->trhs[i].level++;
+ mathomatic->trhs[mathomatic->n_trhs].level = 1;
+ mathomatic->trhs[mathomatic->n_trhs].kind = OPERATOR;
if (sign == PLUS)
- trhs[n_trhs].token.operatr = MINUS;
+ mathomatic->trhs[mathomatic->n_trhs].token.operatr = MINUS;
else
- trhs[n_trhs].token.operatr = PLUS;
- n_trhs++;
- blt(&trhs[n_trhs], qp, q_size * sizeof(token_type));
- i = n_trhs;
- n_trhs += q_size;
- for (; i < n_trhs; i++)
- trhs[i].level++;
- trhs[n_trhs].level = 2;
- trhs[n_trhs].kind = OPERATOR;
- trhs[n_trhs].token.operatr = TIMES;
- n_trhs++;
- i = n_trhs;
- blt(&trhs[n_trhs], divisor, t2 * sizeof(token_type));
- n_trhs += t2;
- trhs[n_trhs] = zero_token;
- n_trhs++;
- blt(&trhs[n_trhs], &divisor[t2+len_t2], (n_divisor - (t2 + len_t2)) * sizeof(token_type));
- n_trhs += (n_divisor - (t2 + len_t2));
- for (; i < n_trhs; i++)
- trhs[i].level += 2;
- side_debug(3, trhs, n_trhs);
- uf_tsimp(trhs, &n_trhs);
- side_debug(4, trhs, n_trhs);
+ mathomatic->trhs[mathomatic->n_trhs].token.operatr = PLUS;
+ mathomatic->n_trhs++;
+ blt(&mathomatic->trhs[mathomatic->n_trhs], qp, q_size * sizeof(token_type));
+ i = mathomatic->n_trhs;
+ mathomatic->n_trhs += q_size;
+ for (; i < mathomatic->n_trhs; i++)
+ mathomatic->trhs[i].level++;
+ mathomatic->trhs[mathomatic->n_trhs].level = 2;
+ mathomatic->trhs[mathomatic->n_trhs].kind = OPERATOR;
+ mathomatic->trhs[mathomatic->n_trhs].token.operatr = TIMES;
+ mathomatic->n_trhs++;
+ i = mathomatic->n_trhs;
+ blt(&mathomatic->trhs[mathomatic->n_trhs], divisor, t2 * sizeof(token_type));
+ mathomatic->n_trhs += t2;
+ mathomatic->trhs[mathomatic->n_trhs] = mathomatic->zero_token;
+ mathomatic->n_trhs++;
+ blt(&mathomatic->trhs[mathomatic->n_trhs], &divisor[t2+len_t2], (n_divisor - (t2 + len_t2)) * sizeof(token_type));
+ mathomatic->n_trhs += (n_divisor - (t2 + len_t2));
+ for (; i < mathomatic->n_trhs; i++)
+ mathomatic->trhs[i].level += 2;
+ side_debug(mathomatic, 3, mathomatic->trhs, mathomatic->n_trhs);
+ uf_tsimp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ side_debug(mathomatic, 4, mathomatic->trhs, mathomatic->n_trhs);
if (REMAINDER_IS_ZERO())
goto end_div2;
if (dcount > 1) {
- if ((n_trhs + n_quotient) >= sum_size) {
+ if ((mathomatic->n_trhs + n_quotient) >= sum_size) {
if (skip_count >= ARR_CNT(skip_terms)) {
if (count == 0) {
return false;
} else {
n_quotient = old_n_quotient;
- blt(trhs, tlhs, n_tlhs * sizeof(token_type));
- n_trhs = n_tlhs;
+ blt(mathomatic->trhs, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ mathomatic->n_trhs = mathomatic->n_tlhs;
goto end_div;
}
}
skip_terms[skip_count] = term_pos;
skip_count++;
n_quotient = old_n_quotient;
- blt(trhs, tlhs, n_tlhs * sizeof(token_type));
- n_trhs = n_tlhs;
- debug_string(3, "Skipping last operation.");
+ blt(mathomatic->trhs, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
+ mathomatic->n_trhs = mathomatic->n_tlhs;
+ debug_string(mathomatic, 3, "Skipping last operation.");
continue;
}
}
- if (n_trhs == 1 && trhs[0].kind == CONSTANT)
+ if (mathomatic->n_trhs == 1 && mathomatic->trhs[0].kind == CONSTANT)
goto end_div;
skip_count = 0;
count++;
@@ -1853,9 +1832,7 @@ end_div2:
* minus any constant multiplier.
*/
int
-basic_size(p1, len)
-token_type *p1;
-int len;
+basic_size(MathoMatic* mathomatic, token_type *p1, int len)
{
int i, j;
int level;
@@ -1863,7 +1840,7 @@ int len;
int constant_flag = true;
rv = len;
- level = min_level(p1, len);
+ level = min_level(mathomatic, p1, len);
for (i = 0, j = -1; i < len; i++) {
if (p1[i].kind == OPERATOR) {
if (p1[i].level == level
diff --git a/proto.h b/proto.h
index a00987c..df9c0d0 100644
--- a/proto.h
+++ b/proto.h
@@ -3,47 +3,47 @@
/* This file is required to compile Mathomatic quietly with the -Wall compiler option. */
/* am.c */
-void display_startup_message(FILE *fp);
-void error(const char *str);
-void reset_error(void);
-void warning(const char *str);
-void error_huge(void);
-void error_bug(const char *str);
-void check_err(void);
-int get_screen_size(void);
-int malloc_vscreen(void);
-int init_mem(void);
+void display_startup_message(MathoMatic* mathomatic, FILE *fp);
+void error(MathoMatic* mathomatic, const char *str);
+void reset_error(MathoMatic* mathomatic);
+void warning(MathoMatic* mathomatic, const char *str);
+void error_huge(MathoMatic* mathomatic);
+void error_bug(MathoMatic* mathomatic, const char *str);
+void check_err(MathoMatic* mathomatic);
+int get_screen_size(MathoMatic* mathomatic);
+int malloc_vscreen(MathoMatic* mathomatic);
+int init_mem(MathoMatic* mathomatic);
int check_gvars(void);
-void init_gvars(void);
-void clean_up(void);
-void set_sign_array(void);
-int next_sign(long *vp);
-void clear_all(void);
-int alloc_espace(int i);
-int alloc_to_espace(int en);
-int alloc_next_espace(void);
-int next_espace(void);
-void copy_espace(int src, int dest);
-int solved_equation(int i);
+void init_gvars(MathoMatic* mathomatic);
+void clean_up(MathoMatic* mathomatic);
+void set_sign_array(MathoMatic* mathomatic);
+int next_sign(MathoMatic* mathomatic, long *vp);
+void clear_all(MathoMatic* mathomatic);
+int alloc_espace(MathoMatic* mathomatic, int i);
+int alloc_to_espace(MathoMatic* mathomatic, int en);
+int alloc_next_espace(MathoMatic* mathomatic);
+int next_espace(MathoMatic* mathomatic);
+void copy_espace(MathoMatic* mathomatic, int src, int dest);
+int solved_equation(MathoMatic* mathomatic, int i);
int found_var(token_type *p1, int n, long v);
-int var_in_equation(int i, long v);
-int search_all_for_var(long v, int forward_direction);
-void rename_var_in_es(int en, long from_v, long to_v);
-int subst_var_with_exp(token_type *equation, int *np, token_type *expression, int len, long v);
-int min_level(token_type *expression, int n);
-int get_default_en(char *cp);
-int get_expr(token_type *equation, int *np);
-int prompt_var(long *vp);
-int not_defined(int i);
-int current_not_defined(void);
-char *get_string(char *string, int n);
-int get_yes_no(void);
-int return_result(int en);
-void free_result_str(void);
+int var_in_equation(MathoMatic* mathomatic, int i, long v);
+int search_all_for_var(MathoMatic* mathomatic, long v, int forward_direction);
+void rename_var_in_es(MathoMatic* mathomatic, int en, long from_v, long to_v);
+int subst_var_with_exp(MathoMatic* mathomatic, token_type *equation, int *np, token_type *expression, int len, long v);
+int min_level(MathoMatic* mathomatic, token_type *expression, int n);
+int get_default_en(MathoMatic* mathomatic, char *cp);
+int get_expr(MathoMatic* mathomatic, token_type *equation, int *np);
+int prompt_var(MathoMatic* mathomatic, long *vp);
+int not_defined(MathoMatic* mathomatic, int i);
+int current_not_defined(MathoMatic* mathomatic);
+char *get_string(MathoMatic* mathomatic, char *string, int n);
+int get_yes_no(MathoMatic* mathomatic);
+int return_result(MathoMatic* mathomatic, int en);
+void free_result_str(MathoMatic* mathomatic);
int is_all(char *cp);
-int get_range(char **cpp, int *ip, int *jp);
-int extra_characters(char *cp);
-int get_range_eol(char **cpp, int *ip, int *jp);
+int get_range(MathoMatic* mathomatic, char **cpp, int *ip, int *jp);
+int extra_characters(MathoMatic* mathomatic, char *cp);
+int get_range_eol(MathoMatic* mathomatic, char **cpp, int *ip, int *jp);
char *skip_space(char *cp);
char *skip_comma_space(char *cp);
long decstrtol(char *cp, char **cpp);
@@ -51,71 +51,71 @@ int isdelimiter(int ch);
char *skip_param(char *cp);
int strcmp_tospace(char *cp1, char *cp2);
int level_plus_count(token_type *p1, int n1, int level);
-int level1_plus_count(token_type *p1, int n1);
+int level1_plus_count(MathoMatic* mathomatic, token_type *p1, int n1);
int var_count(token_type *p1, int n1);
int no_vars(token_type *source, int n, long *vp);
int exp_contains_infinity(token_type *p1, int n1);
int exp_contains_nan(token_type *p1, int n1);
int exp_is_numeric(token_type *p1, int n1);
int exp_is_absolute(token_type *p1, int n1);
-int check_divide_by_zero(double denominator);
-int load_rc(int return_true_if_no_file, FILE *ofp);
+int check_divide_by_zero(MathoMatic* mathomatic, double denominator);
+int load_rc(MathoMatic* mathomatic, int return_true_if_no_file, FILE *ofp);
/* cmds.c */
-int plot_cmd(char *cp);
-int version_cmd(char *cp);
-long max_memory_usage(void);
+int plot_cmd(MathoMatic* mathomatic, char *cp);
+int version_cmd(MathoMatic* mathomatic, char *cp);
+long max_memory_usage(MathoMatic* mathomatic);
int show_status(FILE *ofp);
-int version_report(void);
-int solve_cmd(char *cp);
-int sum_cmd(char *cp);
-int product_cmd(char *cp);
-int for_cmd(char *cp);
-int optimize_cmd(char *cp);
-int output_current_directory(FILE *ofp);
+int version_report(MathoMatic* mathomatic);
+int solve_cmd(MathoMatic* mathomatic, char *cp);
+int sum_cmd(MathoMatic* mathomatic, char *cp);
+int product_cmd(MathoMatic* mathomatic, char *cp);
+int for_cmd(MathoMatic* mathomatic, char *cp);
+int optimize_cmd(MathoMatic* mathomatic, char *cp);
+int output_current_directory(MathoMatic* mathomatic, FILE *ofp);
int fprintf_escaped(FILE *ofp, char *cp);
-void output_options(FILE *ofp, int all_set_options);
+void output_options(MathoMatic* mathomatic, FILE *ofp, int all_set_options);
int skip_no(char **cpp);
-int save_set_options(char *cp);
-int set_options(char *cp, int loading_startup_file);
-int set_cmd(char *cp);
-int echo_cmd(char *cp);
-int pause_cmd(char *cp);
-int copy_cmd(char *cp);
-int real_cmd(char *cp);
-int imaginary_cmd(char *cp);
-int tally_cmd(char *cp);
-int calculate_cmd(char *cp);
-int clear_cmd(char *cp);
-int compare_es(int i, int j);
-int compare_cmd(char *cp);
-int display_fraction(double value);
-int divide_cmd(char *cp);
-int eliminate_cmd(char *cp);
-int display_cmd(char *cp);
-int list_cmd(char *cp);
-int code_cmd(char *cp);
-int variables_cmd(char *cp);
-int approximate_cmd(char *cp);
-int replace_cmd(char *cp);
-int simplify_cmd(char *cp);
-int factor_cmd(char *cp);
-int display_term_count(int en);
-int unfactor_cmd(char *cp);
-int div_loc_find(token_type *expression, int n);
-int fraction_cmd(char *cp);
-int quit_cmd(char *cp);
-int read_cmd(char *cp);
-int read_file(char *cp);
-int read_sub(FILE *fp, char *filename);
-int edit_cmd(char *cp);
-int save_cmd(char *cp);
+int save_set_options(MathoMatic* mathomatic, char *cp);
+int set_options(MathoMatic* mathomatic, char *cp, int loading_startup_file);
+int set_cmd(MathoMatic* mathomatic, char *cp);
+int echo_cmd(MathoMatic* mathomatic, char *cp);
+int pause_cmd(MathoMatic* mathomatic, char *cp);
+int copy_cmd(MathoMatic* mathomatic, char *cp);
+int real_cmd(MathoMatic* mathomatic, char *cp);
+int imaginary_cmd(MathoMatic* mathomatic, char *cp);
+int tally_cmd(MathoMatic* mathomatic, char *cp);
+int calculate_cmd(MathoMatic* mathomatic, char *cp);
+int clear_cmd(MathoMatic* mathomatic, char *cp);
+int compare_es(MathoMatic* mathomatic, int i, int j);
+int compare_cmd(MathoMatic* mathomatic, char *cp);
+int display_fraction(MathoMatic* mathomatic, double value);
+int divide_cmd(MathoMatic* mathomatic, char *cp);
+int eliminate_cmd(MathoMatic* mathomatic, char *cp);
+int display_cmd(MathoMatic* mathomatic, char *cp);
+int list_cmd(MathoMatic* mathomatic, char *cp);
+int code_cmd(MathoMatic* mathomatic, char *cp);
+int variables_cmd(MathoMatic* mathomatic, char *cp);
+int approximate_cmd(MathoMatic* mathomatic, char *cp);
+int replace_cmd(MathoMatic* mathomatic, char *cp);
+int simplify_cmd(MathoMatic* mathomatic, char *cp);
+int factor_cmd(MathoMatic* mathomatic, char *cp);
+int display_term_count(MathoMatic* mathomatic, int en);
+int unfactor_cmd(MathoMatic* mathomatic, char *cp);
+int div_loc_find(MathoMatic* mathomatic, token_type *expression, int n);
+int fraction_cmd(MathoMatic* mathomatic, char *cp);
+int quit_cmd(MathoMatic* mathomatic, char *cp);
+int read_cmd(MathoMatic* mathomatic, char *cp);
+int read_file(MathoMatic* mathomatic, char *cp);
+int read_sub(MathoMatic* mathomatic, FILE *fp, char *filename);
+int edit_cmd(MathoMatic* mathomatic, char *cp);
+int save_cmd(MathoMatic* mathomatic, char *cp);
/* complex.c */
void rect_to_polar(double x, double y, double *radiusp, double *thetap);
-int roots_cmd(char *cp);
-int complex_root_simp(token_type *equation, int *np);
-int approximate_complex_roots(token_type *equation, int *np);
-int get_constant(token_type *p1, int n, double *dp);
-int parse_complex(token_type *p1, int n, complexs *cp);
+int roots_cmd(MathoMatic * mathomatic, char *cp);
+int complex_root_simp(MathoMatic* mathomatic, token_type *equation, int *np);
+int approximate_complex_roots(MathoMatic* mathomatic, token_type *equation, int *np);
+int get_constant(MathoMatic* mathomatic, token_type *p1, int n, double *dp);
+int parse_complex(MathoMatic* mathomatic, token_type *p1, int n, complexs *cp);
/* complex_lib.c */
int complex_fixup(complexs *ap);
complexs complex_add(complexs a, complexs b);
@@ -126,76 +126,76 @@ complexs complex_log(complexs a);
complexs complex_exp(complexs a);
complexs complex_pow(complexs a, complexs b);
/* diff.c */
-int differentiate(token_type *equation, int *np, long v);
-int derivative_cmd(char *cp);
-int extrema_cmd(char *cp);
-int taylor_cmd(char *cp);
-int limit_cmd(char *cp);
+int differentiate(MathoMatic* mathomatic, token_type *equation, int *np, long v);
+int derivative_cmd(MathoMatic* mathomatic, char *cp);
+int extrema_cmd(MathoMatic* mathomatic, char *cp);
+int taylor_cmd(MathoMatic* mathomatic, char *cp);
+int limit_cmd(MathoMatic* mathomatic, char *cp);
/* factor.c */
-int factor_divide(token_type *equation, int *np, long v, double d);
-int subtract_itself(token_type *equation, int *np);
-int factor_plus(token_type *equation, int *np, long v, double d);
-int factor_times(token_type *equation, int *np);
-int factor_power(token_type *equation, int *np);
+int factor_divide(MathoMatic* mathomatic, token_type *equation, int *np, long v, double d);
+int subtract_itself(MathoMatic* mathomatic, token_type *equation, int *np);
+int factor_plus(MathoMatic* mathomatic, token_type *equation, int *np, long v, double d);
+int factor_times(MathoMatic* mathomatic, token_type *equation, int *np);
+int factor_power(MathoMatic* mathomatic, token_type *equation, int *np);
/* factor_int.c */
-int factor_one(double value);
-double multiply_out_unique(void);
-int display_unique(void);
-int is_prime(void);
-int factor_int(token_type *equation, int *np);
-int factor_int_equation(int n);
-int list_factor(token_type *equation, int *np, int factor_flag);
-int factor_constants(token_type *equation, int *np, int level_code);
+int factor_one(MathoMatic* mathomatic, double value);
+double multiply_out_unique(MathoMatic* mathomatic);
+int display_unique(MathoMatic* mathomatic);
+int is_prime(MathoMatic* mathomatic);
+int factor_int(MathoMatic* mathomatic, token_type *equation, int *np);
+int factor_int_equation(MathoMatic* mathomatic, int n);
+int list_factor(MathoMatic* mathomatic, token_type *equation, int *np, int factor_flag);
+int factor_constants(MathoMatic* mathomatic, token_type *equation, int *np, int level_code);
/* gcd.c */
-double gcd(double d1, double d2);
-double gcd_verified(double d1, double d2);
+double gcd(MathoMatic* mathomatic, double d1, double d2);
+double gcd_verified(MathoMatic* mathomatic, double d1, double d2);
double my_round(double d1);
-int f_to_fraction(double d, double *numeratorp, double *denominatorp);
-int make_fractions(token_type *equation, int *np);
-int make_simple_fractions(token_type *equation, int *np);
-int make_mixed_fractions(token_type *equation, int *np);
+int f_to_fraction(MathoMatic* mathomatic, double d, double *numeratorp, double *denominatorp);
+int make_fractions(MathoMatic* mathomatic, token_type *equation, int *np);
+int make_simple_fractions(MathoMatic* mathomatic, token_type *equation, int *np);
+int make_mixed_fractions(MathoMatic* mathomatic, token_type *equation, int *np);
/* globals.c */
/* help.c */
-int parse(int n, char *cp);
-int process_parse(int n, char *cp);
-int process(char *cp);
-int process_rv(char *cp);
-int display_process(char *cp);
-int shell_out(char *cp);
-char *parse_var2(long *vp, char *cp);
-int display_usage(char *pstr, int i);
-int display_command(int i);
-int display_repeat_command(void);
-int read_examples(char **cpp);
-void underline_title(int count);
-int help_cmd(char *cp);
+int parse(MathoMatic* mathomatic, int n, char *cp);
+int process_parse(MathoMatic* mathomatic, int n, char *cp);
+int process(MathoMatic* mathomatic, char *cp);
+int process_rv(MathoMatic* mathomatic, char *cp);
+int display_process(MathoMatic* mathomatic, char *cp);
+int shell_out(MathoMatic* mathomatic, char *cp);
+char *parse_var2(MathoMatic* mathomatic, long *vp, char *cp);
+int display_usage(MathoMatic* mathomatic, char *pstr, int i);
+int display_command(MathoMatic* mathomatic, int i);
+int display_repeat_command(MathoMatic* mathomatic);
+int read_examples(MathoMatic* mathomatic, char **cpp);
+void underline_title(MathoMatic* mathomatic, int count);
+int help_cmd(MathoMatic* mathomatic, char *cp);
/* integrate.c */
-void make_powers(token_type *equation, int *np, long v);
-int int_dispatch(token_type *equation, int *np, long v, int (*func)(token_type *equation, int *np, int loc, int eloc, long v));
-int integrate_cmd(char *cp);
-int laplace_cmd(char *cp);
-int nintegrate_cmd(char *cp);
+void make_powers(MathoMatic* mathomatic, token_type *equation, int *np, long v);
+int int_dispatch(MathoMatic* mathomatic, token_type *equation, int *np, long v, int (*func)(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int eloc, long v));
+int integrate_cmd(MathoMatic* mathomatic, char *cp);
+int laplace_cmd(MathoMatic* mathomatic, char *cp);
+int nintegrate_cmd(MathoMatic* mathomatic, char *cp);
/* list.c */
-void reset_attr(void);
-int set_color(int color);
-void default_color(int set_no_color_flag);
-int display_all_colors(void);
-int list1_sub(int n, int export_flag);
-int list_sub(int n);
-void list_debug(int level, token_type *p1, int n1, token_type *p2, int n2);
-char *var_name(long v);
-int list_var(long v, int lang_code);
-int list_proc(token_type *p1, int n, int export_flag);
-char *list_equation(int n, int export_flag);
-char *list_expression(token_type *p1, int n, int export_flag);
-int list_string(token_type *p1, int n, char *string, int export_flag);
-int list_string_sub(token_type *p1, int n, int outflag, char *string, int export_flag);
+void reset_attr(MathoMatic* mathomatic);
+int set_color(MathoMatic* mathomatic, int color);
+void default_color(MathoMatic* mathomatic, int set_no_color_flag);
+int display_all_colors(MathoMatic* mathomatic);
+int list1_sub(MathoMatic* mathomatic, int n, int export_flag);
+int list_sub(MathoMatic* mathomatic, int n);
+void list_debug(MathoMatic* mathomatic, int level, token_type *p1, int n1, token_type *p2, int n2);
+char *var_name(MathoMatic* mathomatic, long v);
+int list_var(MathoMatic* mathomatic, long v, int lang_code);
+int list_proc(MathoMatic* mathomatic, token_type *p1, int n, int export_flag);
+char *list_equation(MathoMatic* mathomatic, int n, int export_flag);
+char *list_expression(MathoMatic* mathomatic, token_type *p1, int n, int export_flag);
+int list_string(MathoMatic* mathomatic, token_type *p1, int n, char *string, int export_flag);
+int list_string_sub(MathoMatic* mathomatic, token_type *p1, int n, int outflag, char *string, int export_flag);
int int_expr(token_type *p1, int n);
-int list_code_equation(int en, enum language_list language, int int_flag);
-char *string_code_equation(int en, enum language_list language, int int_flag);
-int list_code(token_type *equation, int *np, int outflag, char *string, enum language_list language, int int_flag);
-char *flist_equation_string(int n);
-int flist_equation(int n);
+int list_code_equation(MathoMatic* mathomatic, int en, enum language_list language, int int_flag);
+char *string_code_equation(MathoMatic* mathomatic, int en, enum language_list language, int int_flag);
+int list_code(MathoMatic* mathomatic, token_type *equation, int *np, int outflag, char *string, enum language_list language, int int_flag);
+char *flist_equation_string(MathoMatic* mathomatic, int n);
+int flist_equation(MathoMatic* mathomatic, int n);
/* main.c */
void usage(FILE *fp);
int main(int argc, char **argv);
@@ -208,97 +208,97 @@ void resizehandler(int sig);
void exit_program(int exit_value);
/* parse.c */
void str_tolower(char *cp);
-void put_up_arrow(int cnt, char *cp);
-int isvarchar(int ch);
+void put_up_arrow(MathoMatic* mathomatic, int cnt, char *cp);
+int isvarchar(MathoMatic* mathomatic, int ch);
int paren_increment(int ch);
int is_mathomatic_operator(int ch);
void binary_parenthesize(token_type *p1, int n, int i);
void handle_negate(token_type *equation, int *np);
-void give_priority(token_type *equation, int *np);
-char *parse_section(token_type *equation, int *np, char *cp, int allow_space);
-char *parse_equation(int n, char *cp);
-char *parse_expr(token_type *equation, int *np, char *cp, int allow_space);
-char *parse_var(long *vp, char *cp);
+void give_priority(MathoMatic* mathomatic, token_type *equation, int *np);
+char *parse_section(MathoMatic* mathomatic, token_type *equation, int *np, char *cp, int allow_space);
+char *parse_equation(MathoMatic* mathomatic, int n, char *cp);
+char *parse_expr(MathoMatic* mathomatic, token_type *equation, int *np, char *cp, int allow_space);
+char *parse_var(MathoMatic* mathomatic, long *vp, char *cp);
void remove_trailing_spaces(char *cp);
-void set_error_level(char *cp);
+void set_error_level(MathoMatic* mathomatic, char *cp);
int var_is_const(long v, double *dp);
int subst_constants(token_type *equation, int *np);
int my_strlcpy(char *dest, char *src, int n);
/* poly.c */
-int poly_in_v_sub(token_type *p1, int n, long v, int allow_divides);
-int poly_in_v(token_type *p1, int n, long v, int allow_divides);
-int poly_factor(token_type *equation, int *np, int do_repeat);
-int remove_factors(void);
-int poly_gcd(token_type *larger, int llen, token_type *smaller, int slen, long v);
-int poly2_gcd(token_type *larger, int llen, token_type *smaller, int slen, long v, int require_additive);
-int is_integer_var(long v);
-int is_integer_expr(token_type *p1, int n);
-int mod_simp(token_type *equation, int *np);
-int poly_gcd_simp(token_type *equation, int *np);
-int div_remainder(token_type *equation, int *np, int poly_flag, int quick_flag);
-int poly_div(token_type *d1, int len1, token_type *d2, int len2, long *vp);
-int smart_div(token_type *d1, int len1, token_type *d2, int len2);
-int basic_size(token_type *p1, int len);
+int poly_in_v_sub(MathoMatic* mathomatic, token_type *p1, int n, long v, int allow_divides);
+int poly_in_v(MathoMatic* mathomatic, token_type *p1, int n, long v, int allow_divides);
+int poly_factor(MathoMatic* mathomatic, token_type *equation, int *np, int do_repeat);
+int remove_factors(MathoMatic* mathomatic);
+int poly_gcd(MathoMatic* mathomatic, token_type *larger, int llen, token_type *smaller, int slen, long v);
+int poly2_gcd(MathoMatic* mathomatic, token_type *larger, int llen, token_type *smaller, int slen, long v, int require_additive);
+int is_integer_var(MathoMatic* mathomatic, long v);
+int is_integer_expr(MathoMatic* mathomatic, token_type *p1, int n);
+int mod_simp(MathoMatic* mathomatic, token_type *equation, int *np);
+int poly_gcd_simp(MathoMatic* mathomatic, token_type *equation, int *np);
+int div_remainder(MathoMatic* mathomatic, token_type *equation, int *np, int poly_flag, int quick_flag);
+int poly_div(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2, long *vp);
+int smart_div(MathoMatic* mathomatic, token_type *d1, int len1, token_type *d2, int len2);
+int basic_size(MathoMatic* mathomatic, token_type *p1, int len);
int get_term(token_type *p1, int n1, int count, int *tp1, int *lentp1);
void term_value(double *dp, token_type *p1, int n1, int loc);
int find_greatest_power(token_type *p1, int n1, long *vp1, double *pp1, int *tp1, int *lentp1, int *dcodep);
/* simplify.c */
-void organize(token_type *equation, int *np);
-void elim_loop(token_type *equation, int *np);
-void simp_ssub(token_type *equation, int *np, long v, double d, int power_flag, int times_flag, int fc_level);
-void simp_equation(int n);
-void mid_simp_side(token_type *equation, int *np);
-void mid_simp_equation(int n);
-void simps_side(token_type *equation, int *np, int zsolve);
-void simpv_side(token_type *equation, int *np, long v);
-void simpv_equation(int n, long v);
-int factor_imaginary(token_type *equation, int *np);
-void factorv(token_type *equation, int *np, long v);
-void calc_simp(token_type *equation, int *np);
-void approximate(token_type *equation, int *np);
-int simp_i(token_type *equation, int *np);
-void simp_divide(token_type *equation, int *np);
-void simp2_divide(token_type *equation, int *np, long v, int fc_level);
-void simpb_side(token_type *equation, int *np, int uf_power_flag, int power_flag, int fc_level);
-void simple_frac_side(token_type *equation, int *np);
-void simpa_side(token_type *equation, int *np, int quick_flag, int frac_flag);
-void simpa_repeat_side(token_type *equation, int *np, int quick_flag, int frac_flag);
-void simpa_repeat(int n, int quick_flag, int frac_flag);
-void simple_frac_repeat_side(token_type *equation, int *np);
-int simp_loop(token_type *equation, int *np);
-int simp_pp(token_type *equation, int *np);
-int integer_root_simp(token_type *equation, int *np);
-int simp_constant_power(token_type *equation, int *np);
-int simp2_power(token_type *equation, int *np);
+void organize(MathoMatic* mathomatic, token_type *equation, int *np);
+void elim_loop(MathoMatic* mathomatic, token_type *equation, int *np);
+void simp_ssub(MathoMatic* mathomatic, token_type *equation, int *np, long v, double d, int power_flag, int times_flag, int fc_level);
+void simp_equation(MathoMatic* mathomatic, int n);
+void mid_simp_side(MathoMatic* mathomatic, token_type *equation, int *np);
+void mid_simp_equation(MathoMatic* mathomatic, int n);
+void simps_side(MathoMatic* mathomatic, token_type *equation, int *np, int zsolve);
+void simpv_side(MathoMatic* mathomatic, token_type *equation, int *np, long v);
+void simpv_equation(MathoMatic* mathomatic, int n, long v);
+int factor_imaginary(MathoMatic* mathomatic, token_type *equation, int *np);
+void factorv(MathoMatic* mathomatic, token_type *equation, int *np, long v);
+void calc_simp(MathoMatic* mathomatic, token_type *equation, int *np);
+void approximate(MathoMatic* mathomatic, token_type *equation, int *np);
+int simp_i(MathoMatic* mathomatic, token_type *equation, int *np);
+void simp_divide(MathoMatic* mathomatic, token_type *equation, int *np);
+void simp2_divide(MathoMatic* mathomatic, token_type *equation, int *np, long v, int fc_level);
+void simpb_side(MathoMatic* mathomatic, token_type *equation, int *np, int uf_power_flag, int power_flag, int fc_level);
+void simple_frac_side(MathoMatic* mathomatic, token_type *equation, int *np);
+void simpa_side(MathoMatic* mathomatic, token_type *equation, int *np, int quick_flag, int frac_flag);
+void simpa_repeat_side(MathoMatic* mathomatic, token_type *equation, int *np, int quick_flag, int frac_flag);
+void simpa_repeat(MathoMatic* mathomatic, int n, int quick_flag, int frac_flag);
+void simple_frac_repeat_side(MathoMatic* mathomatic, token_type *equation, int *np);
+int simp_loop(MathoMatic* mathomatic, token_type *equation, int *np);
+int simp_pp(MathoMatic* mathomatic, token_type *equation, int *np);
+int integer_root_simp(MathoMatic* mathomatic, token_type *equation, int *np);
+int simp_constant_power(MathoMatic* mathomatic, token_type *equation, int *np);
+int simp2_power(MathoMatic* mathomatic, token_type *equation, int *np);
double fixed_fmod(double k1, double k2);
-int combine_constants(token_type *equation, int *np, int iflag);
-int calc(int *op1p, double *k1p, int op2, double k2);
-int elim_k(token_type *equation, int *np);
-int se_compare(token_type *p1, int n1, token_type *p2, int n2, int *diff_signp);
-int elim_sign(token_type *equation, int *np);
-int div_imaginary(token_type *equation, int *np);
-int reorder(token_type *equation, int *np);
-int rationalize(token_type *equation, int *np);
+int combine_constants(MathoMatic* mathomatic, token_type *equation, int *np, int iflag);
+int calc(MathoMatic* mathomatic, int *op1p, double *k1p, int op2, double k2);
+int elim_k(MathoMatic* mathomatic, token_type *equation, int *np);
+int se_compare(MathoMatic* mathomatic, token_type *p1, int n1, token_type *p2, int n2, int *diff_signp);
+int elim_sign(MathoMatic* mathomatic, token_type *equation, int *np);
+int div_imaginary(MathoMatic* mathomatic, token_type *equation, int *np);
+int reorder(MathoMatic* mathomatic, token_type *equation, int *np);
+int rationalize(MathoMatic* mathomatic, token_type *equation, int *np);
/* solve.c */
-int solve_espace(int want, int have);
-int solve_sub(token_type *wantp, int wantn, token_type *leftp, int *leftnp, token_type *rightp, int *rightnp);
+int solve_espace(MathoMatic* mathomatic, int want, int have);
+int solve_sub(MathoMatic* mathomatic, token_type *wantp, int wantn, token_type *leftp, int *leftnp, token_type *rightp, int *rightnp);
/* super.c */
-void group_proc(token_type *equation, int *np);
-int fractions_and_group(token_type *equation, int *np);
-int make_fractions_and_group(int n);
-int super_factor(token_type *equation, int *np, int start_flag);
+void group_proc(MathoMatic* mathomatic, token_type *equation, int *np);
+int fractions_and_group(MathoMatic* mathomatic, token_type *equation, int *np);
+int make_fractions_and_group(MathoMatic* mathomatic, int n);
+int super_factor(MathoMatic* mathomatic, token_type *equation, int *np, int start_flag);
/* unfactor.c */
-int uf_tsimp(token_type *equation, int *np);
-int uf_power(token_type *equation, int *np);
-int uf_pplus(token_type *equation, int *np);
-void uf_allpower(token_type *equation, int *np);
-void uf_repeat(token_type *equation, int *np);
-void uf_repeat_always(token_type *equation, int *np);
-void uf_simp(token_type *equation, int *np);
-void uf_simp_no_repeat(token_type *equation, int *np);
-int ufactor(token_type *equation, int *np);
-int uf_times(token_type *equation, int *np);
-int sub_ufactor(token_type *equation, int *np, int ii);
-int unsimp_power(token_type *equation, int *np);
-void uf_neg_help(token_type *equation, int *np);
-int patch_root_div(token_type *equation, int *np);
+int uf_tsimp(MathoMatic* mathomatic, token_type *equation, int *np);
+int uf_power(MathoMatic* mathomatic, token_type *equation, int *np);
+int uf_pplus(MathoMatic* mathomatic, token_type *equation, int *np);
+void uf_allpower(MathoMatic* mathomatic, token_type *equation, int *np);
+void uf_repeat(MathoMatic* mathomatic, token_type *equation, int *np);
+void uf_repeat_always(MathoMatic* mathomatic, token_type *equation, int *np);
+void uf_simp(MathoMatic* mathomatic, token_type *equation, int *np);
+void uf_simp_no_repeat(MathoMatic* mathomatic, token_type *equation, int *np);
+int ufactor(MathoMatic* mathomatic, token_type *equation, int *np);
+int uf_times(MathoMatic* mathomatic, token_type *equation, int *np);
+int sub_ufactor(MathoMatic* mathomatic, token_type *equation, int *np, int ii);
+int unsimp_power(MathoMatic* mathomatic, token_type *equation, int *np);
+void uf_neg_help(token_type *equation, int *np, MathoMatic* mathomatic);
+int patch_root_div(MathoMatic* mathomatic, token_type *equation, int *np);
diff --git a/simplify.c b/simplify.c
index b8d1be3..ffb2a50 100644
--- a/simplify.c
+++ b/simplify.c
@@ -33,10 +33,10 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/
#define MAX_COMPARE_TERMS (DEFAULT_N_TOKENS / 6)
-static int org_recurse(token_type *equation, int *np, int loc, int level, int *elocp);
-static int const_recurse(token_type *equation, int *np, int loc, int level, int iflag);
-static int compare_recurse(token_type *p1, int n1, int l1, token_type *p2, int n2, int l2, int *diff_signp);
-static int order_recurse(token_type *equation, int *np, int loc, int level);
+static int org_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int *elocp);
+static int const_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int iflag);
+static int compare_recurse(MathoMatic* mathomatic, token_type *p1, int n1, int l1, token_type *p2, int n2, int l2, int *diff_signp);
+static int order_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level);
/*
* Fix up levels of parentheses in an equation side.
@@ -45,29 +45,27 @@ static int order_recurse(token_type *equation, int *np, int loc, int level);
* This is the inner-most loop in Mathomatic, make it fast.
*/
void
-organize(equation, np)
-token_type *equation; /* equation side pointer */
-int *np; /* pointer to length of equation side */
+organize(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* equation side pointer */
+//int *np; /* pointer to length of equation side */
{
#if DEBUG
if (equation == NULL || np == NULL) {
- error_bug("NULL pointer passed to organize().");
+ error_bug(mathomatic, "NULL pointer passed to organize().");
}
#endif
if (*np <= 0 || (*np & 1) == 0) {
printf("Bad expression size = %d.\n", *np);
- error_bug("Internal error: organize() called with bad expression size.");
+ error_bug(mathomatic, "Internal error: organize() called with bad expression size.");
}
- if (*np > n_tokens) {
- error_bug("Internal error: expression array overflow detected in organize().");
+ if (*np > mathomatic->n_tokens) {
+ error_bug(mathomatic, "Internal error: expression array overflow detected in organize().");
}
- org_recurse(equation, np, 0, 1, NULL);
+ org_recurse(mathomatic, equation, np, 0, 1, NULL);
}
static inline void
-org_up_level(bp, ep, level, invert)
-token_type *bp, *ep;
-int level, invert;
+org_up_level(token_type *bp, token_type *ep, int level, int invert)
{
if (invert) {
for (; bp <= ep; bp++) {
@@ -101,10 +99,9 @@ int level, invert;
* moving up levels to "level" of parentheses.
*/
static int
-org_recurse(equation, np, loc, level, elocp)
-token_type *equation; /* equation side pointer */
-int *np; /* pointer to length of equation side */
-int loc, level, *elocp;
+org_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int *elocp)
+//token_type *equation; /* equation side pointer */
+//int *np; /* pointer to length of equation side */
{
token_type *p1, *bp, *ep;
int op, sub_op;
@@ -145,7 +142,7 @@ int loc, level, *elocp;
}
for (i = loc; i <= eloc; i += 2) {
if (equation[i].level > level) {
- sub_op = org_recurse(equation, np, i, level + 1, &sub_eloc);
+ sub_op = org_recurse(mathomatic, equation, np, i, level + 1, &sub_eloc);
switch (sub_op) {
case PLUS:
case MINUS:
@@ -173,55 +170,55 @@ int loc, level, *elocp;
* Just does constant simplification.
*/
void
-elim_loop(equation, np)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of equation side */
+elim_loop(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 equation side */
{
- if (abort_flag) {
+ if (mathomatic->abort_flag) {
/* Control-C pressed, gracefully return to main prompt and leave unsimplified */
- abort_flag = false;
+ mathomatic->abort_flag = false;
#if DEBUG && !SILENT
char *cp, buf[100];
- my_strlcpy(prompt_str, _("Enter debug level, or an empty line to abort the current operation: "), sizeof(prompt_str));
- if ((cp = get_string(buf, sizeof(buf))) == NULL || *cp == '\0') {
- longjmp(jmp_save, 13);
+ my_strlcpy(mathomatic->prompt_str, _("Enter debug level, or an empty line to abort the current operation: "), sizeof(mathomatic->prompt_str));
+ if ((cp = get_string(mathomatic, buf, sizeof(buf))) == NULL || *cp == '\0') {
+ longjmp(mathomatic->jmp_save, 13);
} else {
- debug_level = decstrtol(cp, NULL);
- printf(_("Debug level set to %d.\n"), debug_level);
+ mathomatic->debug_level = decstrtol(cp, NULL);
+ printf(_("Debug level set to %d.\n"), mathomatic->debug_level);
}
#else
- longjmp(jmp_save, 13);
+ longjmp(mathomatic->jmp_save, 13);
#endif
}
- side_debug(6, equation, *np);
+ side_debug(mathomatic, 6, equation, *np);
do {
do {
do {
- organize(equation, np);
- } while (combine_constants(equation, np, true));
- } while (elim_k(equation, np));
- } while (simp_pp(equation, np));
- if (reorder(equation, np)) {
+ organize(mathomatic, equation, np);
+ } while (combine_constants(mathomatic, equation, np, true));
+ } while (elim_k(mathomatic, equation, np));
+ } while (simp_pp(mathomatic, equation, np));
+ if (reorder(mathomatic, equation, np)) {
do {
- organize(equation, np);
- } while (elim_k(equation, np));
+ organize(mathomatic, equation, np);
+ } while (elim_k(mathomatic, equation, np));
}
- side_debug(5, equation, *np);
+ side_debug(mathomatic, 5, equation, *np);
}
/*
* Configurable high level simplify routine.
*/
void
-simp_ssub(equation, np, v, d, power_flag, times_flag, fc_level)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of equation side */
-long v; /* variable to factor, 0L or MATCH_ANY to factor all variables */
-double d; /* factor expressions raised to the power of this if v */
-int power_flag; /* factor_power() flag */
-int times_flag; /* factor_times() flag */
-int fc_level; /* factor constants code, passed to factor_constants() */
+simp_ssub(MathoMatic* mathomatic, token_type *equation, int *np, long v, double d, int power_flag, int times_flag, int fc_level)
+//token_type *equation; /* pointer to the beginning of equation side to simplify */
+//int *np; /* pointer to length of equation side */
+//long v; /* variable to factor, 0L or MATCH_ANY to factor all variables */
+//double d; /* factor expressions raised to the power of this if v */
+//int power_flag; /* factor_power() flag */
+//int times_flag; /* factor_times() flag */
+//int fc_level; /* factor constants code, passed to factor_constants() */
{
do {
do {
@@ -231,15 +228,15 @@ int fc_level; /* factor constants code, passed to factor_constants() */
do {
do {
do {
- elim_loop(equation, np);
- } while (simp2_power(equation, np));
- } while (times_flag && factor_times(equation, np));
- } while (elim_sign(equation, np));
- } while (subtract_itself(equation, np));
- } while (factor_constants(equation, np, fc_level));
- } while (factor_divide(equation, np, v, d));
- } while (factor_plus(equation, np, v, d));
- } while (power_flag && factor_power(equation, np));
+ elim_loop(mathomatic, equation, np);
+ } while (simp2_power(mathomatic, equation, np));
+ } while (times_flag && factor_times(mathomatic, equation, np));
+ } while (elim_sign(mathomatic, equation, np));
+ } while (subtract_itself(mathomatic, equation, np));
+ } while (factor_constants(mathomatic, equation, np, fc_level));
+ } while (factor_divide(mathomatic, equation, np, v, d));
+ } while (factor_plus(mathomatic, equation, np, v, d));
+ } while (power_flag && factor_power(mathomatic, equation, np));
}
/*
@@ -247,14 +244,14 @@ int fc_level; /* factor constants code, passed to factor_constants() */
* No factoring is done.
*/
void
-simp_equation(n)
-int n; /* equation space number to simplify */
+simp_equation(MathoMatic* mathomatic, int n)
+//int n; /* equation space number to simplify */
{
- if (empty_equation_space(n))
+ if (empty_equation_space(mathomatic, n))
return;
- simp_loop(lhs[n], &n_lhs[n]);
- if (n_rhs[n] > 0) {
- simp_loop(rhs[n], &n_rhs[n]);
+ simp_loop(mathomatic, mathomatic->lhs[n], &mathomatic->n_lhs[n]);
+ if (mathomatic->n_rhs[n] > 0) {
+ simp_loop(mathomatic, mathomatic->rhs[n], &mathomatic->n_rhs[n]);
}
}
@@ -263,11 +260,11 @@ int n; /* equation space number to simplify */
* Trivial factoring is done.
*/
void
-mid_simp_side(equation, np)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of equation side */
+mid_simp_side(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 equation side */
{
- simp_ssub(equation, np, 0L, 1.0, true, true, 6);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, true, true, 6);
}
/*
@@ -275,14 +272,14 @@ int *np; /* pointer to length of equation side */
* Trivial factoring is done.
*/
void
-mid_simp_equation(n)
-int n; /* equation space number to simplify */
+mid_simp_equation(MathoMatic* mathomatic, int n)
+//int n; /* equation space number to simplify */
{
- if (empty_equation_space(n))
+ if (empty_equation_space(mathomatic, n))
return;
- mid_simp_side(lhs[n], &n_lhs[n]);
- if (n_rhs[n] > 0) {
- mid_simp_side(rhs[n], &n_rhs[n]);
+ mid_simp_side(mathomatic, mathomatic->lhs[n], &mathomatic->n_lhs[n]);
+ if (mathomatic->n_rhs[n] > 0) {
+ mid_simp_side(mathomatic, mathomatic->rhs[n], &mathomatic->n_rhs[n]);
}
}
@@ -290,28 +287,28 @@ int n; /* equation space number to simplify */
* This function is the mid-range simplifier used by the solver.
*/
void
-simps_side(equation, np, zsolve)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of equation side */
-int zsolve; /* true for solving for zero */
+simps_side(MathoMatic* mathomatic, token_type *equation, int *np, int zsolve)
+//token_type *equation; /* pointer to the beginning of equation side to simplify */
+//int *np; /* pointer to length of equation side */
+//int zsolve; /* true for solving for zero */
{
- elim_loop(equation, np);
- simp_constant_power(equation, np);
+ elim_loop(mathomatic, equation, np);
+ simp_constant_power(mathomatic, equation, np);
do {
- simp_ssub(equation, np, 0L, 0.0, !zsolve, true, 6);
- } while (super_factor(equation, np, 0));
+ simp_ssub(mathomatic, equation, np, 0L, 0.0, !zsolve, true, 6);
+ } while (super_factor(mathomatic, equation, np, 0));
}
/*
* This function is used by the factor command.
*/
void
-simpv_side(equation, np, v)
-token_type *equation; /* pointer to the beginning of equation side to factor */
-int *np; /* pointer to length of equation side */
-long v; /* variable to factor, 0 for all variables */
+simpv_side(MathoMatic* mathomatic, token_type *equation, int *np, long v)
+//token_type *equation; /* pointer to the beginning of equation side to factor */
+//int *np; /* pointer to length of equation side */
+//long v; /* variable to factor, 0 for all variables */
{
- simp_ssub(equation, np, v, 0.0, v == 0, true, 6);
+ simp_ssub(mathomatic, equation, np, v, 0.0, v == 0, true, 6);
}
/*
@@ -319,15 +316,15 @@ long v; /* variable to factor, 0 for all variables */
* Trivial factoring is done.
*/
void
-simpv_equation(n, v)
-int n; /* equation space number to simplify */
-long v; /* Mathomatic variable to factor or 0 */
+simpv_equation(MathoMatic* mathomatic, int n, long v)
+//int n; /* equation space number to simplify */
+//long v; /* Mathomatic variable to factor or 0 */
{
- if (empty_equation_space(n))
+ if (empty_equation_space(mathomatic, n))
return;
- simpv_side(lhs[n], &n_lhs[n], v);
- if (n_rhs[n] > 0) {
- simpv_side(rhs[n], &n_rhs[n], v);
+ simpv_side(mathomatic, mathomatic->lhs[n], &mathomatic->n_lhs[n], v);
+ if (mathomatic->n_rhs[n] > 0) {
+ simpv_side(mathomatic, mathomatic->rhs[n], &mathomatic->n_rhs[n], v);
}
}
@@ -338,14 +335,14 @@ long v; /* Mathomatic variable to factor or 0 */
* Return true if anything was approximated.
*/
int
-factor_imaginary(equation, np)
-token_type *equation; /* pointer to the beginning of equation side */
-int *np; /* pointer to equation side length */
+factor_imaginary(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* pointer to the beginning of equation side */
+//int *np; /* pointer to equation side length */
{
int rv;
- rv = approximate_complex_roots(equation, np);
- factorv(equation, np, IMAGINARY);
+ rv = approximate_complex_roots(mathomatic, equation, np);
+ factorv(mathomatic, equation, np, IMAGINARY);
return rv;
}
@@ -357,16 +354,16 @@ int *np; /* pointer to equation side length */
* not in the divisor.
*/
void
-factorv(equation, np, v)
-token_type *equation; /* pointer to the beginning of equation side to factor */
-int *np; /* pointer to equation side length */
-long v; /* Mathomatic variable to factor */
+factorv(MathoMatic* mathomatic, token_type *equation, int *np, long v)
+//token_type *equation; /* pointer to the beginning of equation side to factor */
+//int *np; /* pointer to equation side length */
+//long v; /* Mathomatic variable to factor */
{
do {
do {
- simp_loop(equation, np);
- } while (factor_plus(equation, np, v, 0.0));
- } while (v == IMAGINARY && div_imaginary(equation, np));
+ simp_loop(mathomatic, equation, np);
+ } while (factor_plus(mathomatic, equation, np, v, 0.0));
+ } while (v == IMAGINARY && div_imaginary(mathomatic, equation, np));
}
/*
@@ -374,41 +371,37 @@ long v; /* Mathomatic variable to factor */
* Includes complex number simplification.
*/
void
-calc_simp(equation, np)
-token_type *equation;
-int *np;
+calc_simp(MathoMatic* mathomatic, token_type *equation, int *np)
{
- approximate_roots = true;
+ mathomatic->approximate_roots = true;
subst_constants(equation, np);
- mid_simp_side(equation, np);
- factor_imaginary(equation, np);
- ufactor(equation, np);
- factor_imaginary(equation, np);
- uf_simp(equation, np);
- factor_imaginary(equation, np);
- mid_simp_side(equation, np);
- make_simple_fractions(equation, np);
- uf_tsimp(equation, np);
- approximate_roots = false;
+ mid_simp_side(mathomatic, equation, np);
+ factor_imaginary(mathomatic, equation, np);
+ ufactor(mathomatic, equation, np);
+ factor_imaginary(mathomatic, equation, np);
+ uf_simp(mathomatic, equation, np);
+ factor_imaginary(mathomatic, equation, np);
+ mid_simp_side(mathomatic, equation, np);
+ make_simple_fractions(mathomatic, equation, np);
+ uf_tsimp(mathomatic, equation, np);
+ mathomatic->approximate_roots = false;
}
/*
* Approximate an equation side for the approximate command.
*/
void
-approximate(equation, np)
-token_type *equation;
-int *np;
+approximate(MathoMatic* mathomatic, token_type *equation, int *np)
{
- if (repeat_flag) {
+ if (mathomatic->repeat_flag) {
/* do more */
- calc_simp(equation, np);
+ calc_simp(mathomatic, equation, np);
} else {
subst_constants(equation, np);
- approximate_roots = true;
- simp_loop(equation, np);
- factor_imaginary(equation, np);
- approximate_roots = false;
+ mathomatic->approximate_roots = true;
+ simp_loop(mathomatic, equation, np);
+ factor_imaginary(mathomatic, equation, np);
+ mathomatic->approximate_roots = false;
}
}
@@ -419,21 +412,19 @@ int *np;
* Return true if any imaginary units where substituted.
*/
int
-simp_i(equation, np)
-token_type *equation;
-int *np;
+simp_i(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i;
int level;
int rv = false;
- simp_loop(equation, np);
+ simp_loop(mathomatic, equation, np);
for (i = 0; i < *np; i++) {
switch (equation[i].kind) {
case VARIABLE:
if (equation[i].token.variable == IMAGINARY) {
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
level = equation[i].level + 1;
blt(&equation[i+2], &equation[i], (*np - i) * sizeof(token_type));
@@ -472,14 +463,14 @@ int *np;
do {
do {
do {
- organize(equation, np);
- } while (combine_constants(equation, np, false));
- } while (elim_k(equation, np));
- } while (simp_pp(equation, np));
+ organize(mathomatic, equation, np);
+ } while (combine_constants(mathomatic, equation, np, false));
+ } while (elim_k(mathomatic, equation, np));
+ } while (simp_pp(mathomatic, equation, np));
/* The following factor_power() messes up absolute values and complex numbers sometimes: */
- } while (factor_power(equation, np));
- } while (factor_times(equation, np));
- simp_loop(equation, np);
+ } while (factor_power(mathomatic, equation, np));
+ } while (factor_times(mathomatic, equation, np));
+ simp_loop(mathomatic, equation, np);
return rv;
}
@@ -487,15 +478,13 @@ int *np;
* Combine all like denominators.
*/
void
-simp_divide(equation, np)
-token_type *equation;
-int *np;
+simp_divide(MathoMatic* mathomatic, token_type *equation, int *np)
{
do {
do {
- simp_loop(equation, np);
- } while (factor_constants(equation, np, 1));
- } while (factor_divide(equation, np, 0L, 0.0));
+ simp_loop(mathomatic, equation, np);
+ } while (factor_constants(mathomatic, equation, np, 1));
+ } while (factor_divide(mathomatic, equation, np, 0L, 0.0));
}
/*
@@ -505,23 +494,19 @@ int *np;
* For beauty simplifier simpb_side() below.
*/
void
-simp2_divide(equation, np, v, fc_level)
-token_type *equation;
-int *np;
-long v;
-int fc_level;
+simp2_divide(MathoMatic* mathomatic, token_type *equation, int *np, long v, int fc_level)
{
do {
do {
do {
do {
do {
- elim_loop(equation, np);
- } while (simp2_power(equation, np));
- } while (elim_sign(equation, np));
- } while (subtract_itself(equation, np));
- } while (factor_constants(equation, np, fc_level));
- } while (factor_divide(equation, np, v, 0.0));
+ elim_loop(mathomatic, equation, np);
+ } while (simp2_power(mathomatic, equation, np));
+ } while (elim_sign(mathomatic, equation, np));
+ } while (subtract_itself(mathomatic, equation, np));
+ } while (factor_constants(mathomatic, equation, np, fc_level));
+ } while (factor_divide(mathomatic, equation, np, v, 0.0));
}
/*
@@ -555,21 +540,21 @@ sort_type *p1, *p2;
* Factors variables in order: "sign" variables first, then by frequency.
*/
void
-simpb_side(equation, np, uf_power_flag, power_flag, fc_level)
-token_type *equation; /* pointer to the beginning of equation side */
-int *np; /* pointer to length of equation side */
-int uf_power_flag; /* uf_allpower() flag */
-int power_flag; /* factor_power() flag */
-int fc_level; /* factor constants code, passed to factor_constants() */
+simpb_side(MathoMatic* mathomatic, token_type *equation, int *np, int uf_power_flag, int power_flag, int fc_level)
+//token_type *equation; /* pointer to the beginning of equation side */
+//int *np; /* pointer to length of equation side */
+//int uf_power_flag; /* uf_allpower() flag */
+//int power_flag; /* factor_power() flag */
+//int fc_level; /* factor constants code, passed to factor_constants() */
{
int i;
int vc, cnt; /* counts */
long v1, last_v; /* Mathomatic variables */
sort_type va[MAX_VARS]; /* array of all real variables found in equation side */
- elim_loop(equation, np);
+ elim_loop(mathomatic, equation, np);
if (uf_power_flag) {
- uf_allpower(equation, np);
+ uf_allpower(mathomatic, equation, np);
}
last_v = 0;
for (vc = 0; vc < ARR_CNT(va);) {
@@ -598,35 +583,35 @@ int fc_level; /* factor constants code, passed to factor_constants() */
/* sort variable array va[] */
qsort((char *) va, vc, sizeof(*va), simpb_vcmp);
/* factor division by most frequently occurring variables first */
- simp2_divide(equation, np, va[0].v, fc_level);
+ simp2_divide(mathomatic, equation, np, va[0].v, fc_level);
for (i = 1; i < vc; i++) {
- if (factor_divide(equation, np, va[i].v, 0.0))
- simp2_divide(equation, np, va[i].v, fc_level);
+ if (factor_divide(mathomatic, equation, np, va[i].v, 0.0))
+ simp2_divide(mathomatic, equation, np, va[i].v, fc_level);
}
- simp2_divide(equation, np, 0L, fc_level);
+ simp2_divide(mathomatic, equation, np, 0L, fc_level);
/* factor all sub-expressions in order of most frequently occurring variables */
for (i = 0; i < vc; i++) {
- while (factor_plus(equation, np, va[i].v, 0.0)) {
- simp2_divide(equation, np, 0L, fc_level);
+ while (factor_plus(mathomatic, equation, np, va[i].v, 0.0)) {
+ simp2_divide(mathomatic, equation, np, 0L, fc_level);
}
}
}
#if 0 /* Messes up support/gcd.in and tests/quartic.in */
- make_simple_fractions(equation, np);
+ make_simple_fractions(mathomatic, equation, np);
#endif
/* make sure equation side is completely factored */
- while (factor_divide(equation, np, MATCH_ANY, 0.0)) {
- simp2_divide(equation, np, MATCH_ANY, fc_level);
+ while (factor_divide(mathomatic, equation, np, MATCH_ANY, 0.0)) {
+ simp2_divide(mathomatic, equation, np, MATCH_ANY, fc_level);
}
- while (factor_plus(equation, np, MATCH_ANY, 0.0)) {
- simp2_divide(equation, np, 0L, fc_level);
+ while (factor_plus(mathomatic, equation, np, MATCH_ANY, 0.0)) {
+ simp2_divide(mathomatic, equation, np, 0L, fc_level);
}
- simp_ssub(equation, np, MATCH_ANY, 0.0, power_flag, true, fc_level);
+ simp_ssub(mathomatic, equation, np, MATCH_ANY, 0.0, power_flag, true, fc_level);
#if 0 /* Seems to complicate and change constants slightly when enabled. */
if (power_flag) {
- make_simple_fractions(equation, np);
- factor_power(equation, np);
- simp_ssub(equation, np, MATCH_ANY, 0.0, power_flag, true, fc_level);
+ make_simple_fractions(mathomatic, equation, np);
+ factor_power(mathomatic, equation, np);
+ simp_ssub(mathomatic, equation, np, MATCH_ANY, 0.0, power_flag, true, fc_level);
}
#endif
}
@@ -638,38 +623,38 @@ int fc_level; /* factor constants code, passed to factor_constants() */
* Globals tlhs[] and trhs[] are wiped out.
*/
void
-simple_frac_side(equation, np)
-token_type *equation; /* pointer to the beginning of equation side */
-int *np; /* pointer to length of equation side */
+simple_frac_side(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* pointer to the beginning of equation side */
+//int *np; /* pointer to length of equation side */
{
if (*np == 1) {
- make_simple_fractions(equation, np);
- fractions_and_group(equation, np);
+ make_simple_fractions(mathomatic, equation, np);
+ fractions_and_group(mathomatic, equation, np);
return;
}
- simp_loop(equation, np);
- poly_factor(equation, np, true);
+ simp_loop(mathomatic, equation, np);
+ poly_factor(mathomatic, equation, np, true);
do {
do {
do {
- simp_ssub(equation, np, 0L, 0.0, false, true, 5);
- } while (poly_gcd_simp(equation, np));
- } while (uf_power(equation, np));
- } while (super_factor(equation, np, 3));
- side_debug(2, equation, *np);
+ simp_ssub(mathomatic, equation, np, 0L, 0.0, false, true, 5);
+ } while (poly_gcd_simp(mathomatic, equation, np));
+ } while (uf_power(mathomatic, equation, np));
+ } while (super_factor(mathomatic, equation, np, 3));
+ side_debug(mathomatic, 2, equation, *np);
- make_simple_fractions(equation, np);
- uf_tsimp(equation, np);
+ make_simple_fractions(mathomatic, equation, np);
+ uf_tsimp(mathomatic, equation, np);
#if 0 /* Not really needed, but makes it end up the same as the simplify command. */
- make_simple_fractions(equation, np);
- uf_power(equation, np);
- integer_root_simp(equation, np);
- simpb_side(equation, np, true, true, 3);
+ make_simple_fractions(mathomatic, equation, np);
+ uf_power(mathomatic, equation, np);
+ integer_root_simp(mathomatic, equation, np);
+ simpb_side(mathomatic, equation, np, true, true, 3);
#endif
- poly_factor(equation, np, true);
- simpb_side(equation, np, true, false, 2);
- simpb_side(equation, np, true, false, 2); /* Added for thoroughness, making sure everything is uf_power()ed. */
- fractions_and_group(equation, np);
+ poly_factor(mathomatic, equation, np, true);
+ simpb_side(mathomatic, equation, np, true, false, 2);
+ simpb_side(mathomatic, equation, np, true, false, 2); /* Added for thoroughness, making sure everything is uf_power()ed. */
+ fractions_and_group(mathomatic, equation, np);
}
/*
@@ -680,155 +665,155 @@ int *np; /* pointer to length of equation side */
* Globals tlhs[] and trhs[] are wiped out.
*/
void
-simpa_side(equation, np, quick_flag, frac_flag)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of the equation side */
-int quick_flag; /* "simplify quick" option, simpler with no (x+1)^2 expansion */
-int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polynomials */
+simpa_side(MathoMatic* mathomatic, token_type *equation, int *np, int quick_flag, int frac_flag)
+//token_type *equation; /* pointer to the beginning of equation side to simplify */
+//int *np; /* pointer to length of the equation side */
+//int quick_flag; /* "simplify quick" option, simpler with no (x+1)^2 expansion */
+//int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polynomials */
{
int i;
int flag, poly_flag = true;
jmp_buf save_save;
if (*np == 1) { /* no need to full simplify a single constant or variable */
- make_simple_fractions(equation, np);
- simpb_side(equation, np, true, !frac_flag, 2);
+ make_simple_fractions(mathomatic, equation, np);
+ simpb_side(mathomatic, equation, np, true, !frac_flag, 2);
return;
}
- debug_string(2, "Simplify input:");
- side_debug(2, equation, *np);
- simp_loop(equation, np);
+ debug_string(mathomatic, 2, "Simplify input:");
+ side_debug(mathomatic, 2, equation, *np);
+ simp_loop(mathomatic, equation, np);
#if 1
do {
- simp_ssub(equation, np, 0L, 1.0, false, true, 5);
- } while (uf_power(equation, np));
- while (factor_power(equation, np)) {
- simp_loop(equation, np);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, false, true, 5);
+ } while (uf_power(mathomatic, equation, np));
+ while (factor_power(mathomatic, equation, np)) {
+ simp_loop(mathomatic, equation, np);
}
#else
- simpb_side(equation, np, false, true, 5);
+ simpb_side(mathomatic, equation, np, false, true, 5);
#endif
- if (rationalize_denominators) {
- rationalize(equation, np);
+ if (mathomatic->rationalize_denominators) {
+ rationalize(mathomatic, equation, np);
}
- unsimp_power(equation, np);
- uf_tsimp(equation, np);
+ unsimp_power(mathomatic, equation, np);
+ uf_tsimp(mathomatic, equation, np);
/* Here is the only place in Mathomatic that we do complete modulus (%) simplification: */
- uf_pplus(equation, np);
- uf_repeat(equation, np);
+ uf_pplus(mathomatic, equation, np);
+ uf_repeat(mathomatic, equation, np);
do {
- elim_loop(equation, np);
- } while (mod_simp(equation, np));
+ elim_loop(mathomatic, equation, np);
+ } while (mod_simp(mathomatic, equation, np));
/* Here we try to simplify out unnecessary negative constants and imaginary numbers: */
- simp_i(equation, np);
- unsimp_power(equation, np);
- uf_times(equation, np);
- simp_ssub(equation, np, 0L, 1.0, true, true, 5);
- unsimp_power(equation, np);
- uf_neg_help(equation, np);
- uf_tsimp(equation, np);
+ simp_i(mathomatic, equation, np);
+ unsimp_power(mathomatic, equation, np);
+ uf_times(mathomatic, equation, np);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, true, true, 5);
+ unsimp_power(mathomatic, equation, np);
+ uf_neg_help(equation, np, mathomatic);
+ uf_tsimp(mathomatic, equation, np);
do {
do {
- simp_ssub(equation, np, 0L, 1.0, false, true, 6);
- } while (uf_power(equation, np));
- } while (!quick_flag && super_factor(equation, np, 2));
- if (poly_gcd_simp(equation, np)) {
- simp_ssub(equation, np, 0L, 1.0, false, true, 6);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, false, true, 6);
+ } while (uf_power(mathomatic, equation, np));
+ } while (!quick_flag && super_factor(mathomatic, equation, np, 2));
+ if (poly_gcd_simp(mathomatic, equation, np)) {
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, false, true, 6);
}
- side_debug(2, equation, *np);
- unsimp_power(equation, np);
- uf_times(equation, np);
- factorv(equation, np, IMAGINARY);
- uf_pplus(equation, np);
- simp_ssub(equation, np, 0L, 1.0, true, false, 5);
- if (poly_gcd_simp(equation, np)) {
- factorv(equation, np, IMAGINARY);
- uf_pplus(equation, np);
- simp_ssub(equation, np, 0L, 1.0, true, false, 5);
+ side_debug(mathomatic, 2, equation, *np);
+ unsimp_power(mathomatic, equation, np);
+ uf_times(mathomatic, equation, np);
+ factorv(mathomatic, equation, np, IMAGINARY);
+ uf_pplus(mathomatic, equation, np);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, true, false, 5);
+ if (poly_gcd_simp(mathomatic, equation, np)) {
+ factorv(mathomatic, equation, np, IMAGINARY);
+ uf_pplus(mathomatic, equation, np);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, true, false, 5);
}
- uf_times(equation, np);
- uf_pplus(equation, np);
- factor_imaginary(equation, np);
- uf_power(equation, np);
+ uf_times(mathomatic, equation, np);
+ uf_pplus(mathomatic, equation, np);
+ factor_imaginary(mathomatic, equation, np);
+ uf_power(mathomatic, equation, np);
do {
do {
- simp_ssub(equation, np, 0L, 1.0, false, true, 6);
- } while (uf_power(equation, np));
- } while (!quick_flag && super_factor(equation, np, 2));
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, false, true, 6);
+ } while (uf_power(mathomatic, equation, np));
+ } while (!quick_flag && super_factor(mathomatic, equation, np, 2));
/* Here we do the greatest expansion; if it fails, do less expansion. */
- partial_flag = frac_flag;
- n_tlhs = *np;
- blt(tlhs, equation, n_tlhs * sizeof(token_type));
- blt(save_save, jmp_save, sizeof(jmp_save));
- if ((i = setjmp(jmp_save)) != 0) { /* trap errors */
- blt(jmp_save, save_save, sizeof(jmp_save));
+ mathomatic->partial_flag = frac_flag;
+ mathomatic->n_tlhs = *np;
+ blt(mathomatic->tlhs, equation, mathomatic->n_tlhs * sizeof(token_type));
+ 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(jmp_save, i);
+ longjmp(mathomatic->jmp_save, i);
}
/* an error occurred, restore the original expression */
- *np = n_tlhs;
- blt(equation, tlhs, n_tlhs * sizeof(token_type));
+ *np = mathomatic->n_tlhs;
+ blt(equation, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(token_type));
if (i == 14) {
- debug_string(1, "Simplify not expanding fully, due to oversized expression.");
+ debug_string(mathomatic, 1, "Simplify not expanding fully, due to oversized expression.");
} else {
- debug_string(0, "Simplify not expanding fully, due to some error.");
+ debug_string(mathomatic, 0, "Simplify not expanding fully, due to some error.");
}
- partial_flag = true; /* expand less */
- uf_tsimp(equation, np);
+ mathomatic->partial_flag = true; /* expand less */
+ uf_tsimp(mathomatic, equation, np);
} else {
if (quick_flag) {
- uf_tsimp(equation, np);
+ uf_tsimp(mathomatic, equation, np);
} else {
/* expand powers of 2 and higher, might result in error_huge() trap */
do {
- uf_power(equation, np);
- uf_repeat(equation, np);
- } while (uf_tsimp(equation, np));
+ uf_power(mathomatic, equation, np);
+ uf_repeat(mathomatic, equation, np);
+ } while (uf_tsimp(mathomatic, equation, np));
}
- blt(jmp_save, save_save, sizeof(jmp_save));
+ blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
}
- partial_flag = true;
+ mathomatic->partial_flag = true;
- simpb_side(equation, np, true, true, 2);
- debug_string(1, "Simplify result before applying polynomial operations:");
- side_debug(1, equation, *np);
+ simpb_side(mathomatic, equation, np, true, true, 2);
+ debug_string(mathomatic, 1, "Simplify result before applying polynomial operations:");
+ side_debug(mathomatic, 1, equation, *np);
for (flag = false;;) {
/* divide top and bottom of fractions by any polynomial GCD found */
- if (poly_gcd_simp(equation, np)) {
+ if (poly_gcd_simp(mathomatic, equation, np)) {
flag = false;
- simpb_side(equation, np, false, true, 3);
+ simpb_side(mathomatic, equation, np, false, true, 3);
}
/* factor polynomials */
- if (!flag && poly_factor(equation, np, true)) {
+ if (!flag && poly_factor(mathomatic, equation, np, true)) {
flag = true;
- simpb_side(equation, np, false, true, 3);
+ simpb_side(mathomatic, equation, np, false, true, 3);
continue;
}
/* simplify algebraic fractions with polynomial and smart division */
- if (!frac_flag && div_remainder(equation, np, poly_flag, quick_flag)) {
+ if (!frac_flag && div_remainder(mathomatic, equation, np, poly_flag, quick_flag)) {
flag = false;
- simpb_side(equation, np, false, true, 3);
+ simpb_side(mathomatic, equation, np, false, true, 3);
continue;
}
break;
}
- debug_string(2, "Raw simplify result after applying polynomial operations:");
- side_debug(2, equation, *np);
- simp_constant_power(equation, np);
- simp_ssub(equation, np, 0L, 1.0, true, true, 5);
- unsimp_power(equation, np);
- make_simple_fractions(equation, np);
- factor_power(equation, np);
- uf_tsimp(equation, np);
- make_simple_fractions(equation, np);
- uf_power(equation, np);
- integer_root_simp(equation, np);
- simpb_side(equation, np, true, true, 3);
- poly_factor(equation, np, true);
- simpb_side(equation, np, true, !frac_flag, 2);
+ debug_string(mathomatic, 2, "Raw simplify result after applying polynomial operations:");
+ side_debug(mathomatic, 2, equation, *np);
+ simp_constant_power(mathomatic, equation, np);
+ simp_ssub(mathomatic, equation, np, 0L, 1.0, true, true, 5);
+ unsimp_power(mathomatic, equation, np);
+ make_simple_fractions(mathomatic, equation, np);
+ factor_power(mathomatic, equation, np);
+ uf_tsimp(mathomatic, equation, np);
+ make_simple_fractions(mathomatic, equation, np);
+ uf_power(mathomatic, equation, np);
+ integer_root_simp(mathomatic, equation, np);
+ simpb_side(mathomatic, equation, np, true, true, 3);
+ poly_factor(mathomatic, equation, np, true);
+ simpb_side(mathomatic, equation, np, true, !frac_flag, 2);
}
/*
@@ -840,24 +825,24 @@ int frac_flag; /* "simplify fraction" option, simplify to the ratio of two poly
* Globals tes[], tlhs[], and trhs[] are wiped out.
*/
void
-simpa_repeat_side(equation, np, quick_flag, frac_flag)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of the equation side */
-int quick_flag; /* "simplify quick" option, simpler fractions with no (x+1)^2 expansion */
-int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polynomials */
+simpa_repeat_side(MathoMatic* mathomatic, token_type *equation, int *np, int quick_flag, int frac_flag)
+//token_type *equation; /* pointer to the beginning of equation side to simplify */
+//int *np; /* pointer to length of the equation side */
+//int quick_flag; /* "simplify quick" option, simpler fractions with no (x+1)^2 expansion */
+//int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polynomials */
{
if (*np <= 0)
return;
- simpa_side(equation, np, quick_flag, frac_flag);
- if (repeat_flag && *np > 1) {
+ simpa_side(mathomatic, equation, np, quick_flag, frac_flag);
+ if (mathomatic->repeat_flag && *np > 1) {
do {
- n_tes = *np;
- blt(tes, equation, n_tes * sizeof(token_type));
- simpa_side(equation, np, quick_flag, frac_flag);
- } while (*np < n_tes);
- if (*np != n_tes) {
- *np = n_tes;
- blt(equation, tes, n_tes * sizeof(token_type));
+ mathomatic->n_tes = *np;
+ blt(mathomatic->tes, equation, mathomatic->n_tes * sizeof(token_type));
+ simpa_side(mathomatic, equation, np, quick_flag, frac_flag);
+ } while (*np < mathomatic->n_tes);
+ if (*np != mathomatic->n_tes) {
+ *np = mathomatic->n_tes;
+ blt(equation, mathomatic->tes, mathomatic->n_tes * sizeof(token_type));
}
}
}
@@ -868,16 +853,16 @@ int frac_flag; /* "simplify fraction" option, simplify to the ratio of two poly
* Globals tes[], tlhs[], and trhs[] are clobbered.
*/
void
-simpa_repeat(n, quick_flag, frac_flag)
-int n; /* equation space number to simplify */
-int quick_flag; /* "simplify quick" option, simpler fractions with no (x+1)^2 expansion */
-int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polynomials */
+simpa_repeat(MathoMatic* mathomatic, int n, int quick_flag, int frac_flag)
+//int n; /* equation space number to simplify */
+//int quick_flag; /* "simplify quick" option, simpler fractions with no (x+1)^2 expansion */
+//int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polynomials */
{
- if (empty_equation_space(n))
+ if (empty_equation_space(mathomatic, n))
return;
- simpa_repeat_side(lhs[n], &n_lhs[n], quick_flag, frac_flag);
- if (n_rhs[n] > 0) {
- simpa_repeat_side(rhs[n], &n_rhs[n], quick_flag, frac_flag);
+ simpa_repeat_side(mathomatic, mathomatic->lhs[n], &mathomatic->n_lhs[n], quick_flag, frac_flag);
+ if (mathomatic->n_rhs[n] > 0) {
+ simpa_repeat_side(mathomatic, mathomatic->rhs[n], &mathomatic->n_rhs[n], quick_flag, frac_flag);
}
}
@@ -889,22 +874,22 @@ int frac_flag; /* "simplify fraction" option, simplify to the ratio of two polyn
* Globals tes[], tlhs[], and trhs[] are wiped out.
*/
void
-simple_frac_repeat_side(equation, np)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of the equation side */
+simple_frac_repeat_side(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 */
{
if (*np <= 0)
return;
- simple_frac_side(equation, np);
- if (repeat_flag) {
+ simple_frac_side(mathomatic, equation, np);
+ if (mathomatic->repeat_flag) {
do {
- n_tes = *np;
- blt(tes, equation, n_tes * sizeof(token_type));
- simple_frac_side(equation, np);
- } while (*np < n_tes);
- if (*np != n_tes) {
- *np = n_tes;
- blt(equation, tes, n_tes * sizeof(token_type));
+ mathomatic->n_tes = *np;
+ blt(mathomatic->tes, equation, mathomatic->n_tes * sizeof(token_type));
+ simple_frac_side(mathomatic, equation, np);
+ } while (*np < mathomatic->n_tes);
+ if (*np != mathomatic->n_tes) {
+ *np = mathomatic->n_tes;
+ blt(equation, mathomatic->tes, mathomatic->n_tes * sizeof(token_type));
}
}
}
@@ -915,9 +900,9 @@ int *np; /* pointer to length of the equation side */
* Return true if factor_times() did something.
*/
int
-simp_loop(equation, np)
-token_type *equation; /* pointer to the beginning of equation side to simplify */
-int *np; /* pointer to length of equation side */
+simp_loop(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 equation side */
{
int i;
int rv = false;
@@ -926,14 +911,14 @@ int *np; /* pointer to length of equation side */
do {
do {
do {
- elim_loop(equation, np);
- } while (simp2_power(equation, np));
- i = factor_times(equation, np);
+ elim_loop(mathomatic, equation, np);
+ } while (simp2_power(mathomatic, equation, np));
+ i = factor_times(mathomatic, equation, np);
if (i)
rv = true;
} while (i);
- } while (elim_sign(equation, np));
- } while (subtract_itself(equation, np));
+ } while (elim_sign(mathomatic, equation, np));
+ } while (subtract_itself(mathomatic, equation, np));
return rv;
}
@@ -949,9 +934,7 @@ int *np; /* pointer to length of equation side */
* Return true if equation side was modified.
*/
int
-simp_pp(equation, np)
-token_type *equation;
-int *np;
+simp_pp(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, j, k;
int ilevel, jlevel;
@@ -961,7 +944,7 @@ int *np;
for (i = 1; i < *np; i += 2) {
#if DEBUG
if (equation[i].kind != OPERATOR) {
- error_bug("Bug found in simp_pp(), operators are misplaced.");
+ error_bug(mathomatic, "Bug found in simp_pp(), operators are misplaced.");
}
#endif
if (equation[i].token.operatr != POWER)
@@ -970,12 +953,12 @@ int *np;
for (j = i + 2; j < *np; j += 2) {
jlevel = equation[j].level;
if (jlevel == ilevel - 1 && equation[j].token.operatr == POWER) {
- if (!symb_flag && (equation[i-1].level != ilevel || equation[i-1].kind != CONSTANT || equation[i-1].token.constant < 0)) {
+ if (!mathomatic->symb_flag && (equation[i-1].level != ilevel || equation[i-1].kind != CONSTANT || equation[i-1].token.constant < 0)) {
if (jlevel == equation[j+1].level && equation[j+1].kind == CONSTANT) {
- f_to_fraction(equation[j+1].token.constant, &numerator, &denominator);
+ f_to_fraction(mathomatic, equation[j+1].token.constant, &numerator, &denominator);
if (fmod(denominator, 2.0) == 0.0) {
if ((i + 2) == j && equation[i+1].kind == CONSTANT) {
- f_to_fraction(equation[i+1].token.constant, &numerator, &denominator);
+ f_to_fraction(mathomatic, equation[i+1].token.constant, &numerator, &denominator);
if (fmod(numerator, 2.0) == 0.0) {
break;
}
@@ -984,7 +967,7 @@ int *np;
}
} else {
if ((i + 2) == j && equation[i+1].kind == CONSTANT) {
- f_to_fraction(equation[i+1].token.constant, &numerator, &denominator);
+ f_to_fraction(mathomatic, equation[i+1].token.constant, &numerator, &denominator);
if (fmod(numerator, 2.0) == 0.0) {
break;
}
@@ -1015,9 +998,7 @@ int *np;
* Return true if equation side was modified.
*/
int
-integer_root_simp(equation, np)
-token_type *equation;
-int *np;
+integer_root_simp(MathoMatic* mathomatic, token_type *equation, int *np)
{
int modified = false;
int i, j;
@@ -1027,7 +1008,7 @@ int *np;
for (i = 1; (i + 3) < *np; i += 2) {
#if DEBUG
if (equation[i].kind != OPERATOR) {
- error_bug("Bug found in integer_root_simp(), operators are misplaced.");
+ error_bug(mathomatic, "Bug found in integer_root_simp(), operators are misplaced.");
}
#endif
if (equation[i].token.operatr == POWER) {
@@ -1053,24 +1034,24 @@ int *np;
if (errno) {
continue;
}
- if (!factor_one(d2))
+ if (!factor_one(mathomatic, d2))
continue;
d1 = 1.0;
- for (j = 0; j < uno; j++) {
- if (unique[j] > 0.0) {
- while (ucnt[j] >= denominator) {
- d1 *= unique[j];
- ucnt[j] -= denominator;
+ for (j = 0; j < mathomatic->uno; j++) {
+ if (mathomatic->unique[j] > 0.0) {
+ while (mathomatic->ucnt[j] >= denominator) {
+ d1 *= mathomatic->unique[j];
+ mathomatic->ucnt[j] -= denominator;
}
}
}
if (d1 == 1.0)
continue;
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
equation[i+1].token.constant = 1.0;
- equation[i-1].token.constant = multiply_out_unique();
+ equation[i-1].token.constant = multiply_out_unique(mathomatic);
for (j = i - 1; j < (i + 4); j++) {
equation[j].level++;
}
@@ -1098,9 +1079,7 @@ int *np;
* Return true if equation side was modified.
*/
int
-simp_constant_power(equation, np)
-token_type *equation;
-int *np;
+simp_constant_power(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, j;
int level;
@@ -1113,7 +1092,7 @@ int *np;
level = equation[i].level;
if (equation[i-1].level != level || equation[i-1].kind != CONSTANT)
continue;
- if (equation[i-1].token.constant < 0 && !symb_flag)
+ if (equation[i-1].token.constant < 0 && !mathomatic->symb_flag)
continue;
if (equation[i+1].level != level + 1 || equation[i+1].kind != CONSTANT
|| equation[i+1].token.constant == 1.0)
@@ -1125,8 +1104,8 @@ int *np;
case TIMES:
break;
case DIVIDE:
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[j+2], &equation[j], (*np - j) * sizeof(token_type));
*np += 2;
@@ -1152,9 +1131,7 @@ int *np;
* Return true if equation side was modified.
*/
int
-simp2_power(equation, np)
-token_type *equation;
-int *np;
+simp2_power(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, i1, j, k;
int level;
@@ -1183,8 +1160,8 @@ int *np;
case 0:
case TIMES:
case DIVIDE:
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
equation[k].token.constant = -equation[k].token.constant;
for (k = i - 2;; k--) {
@@ -1237,12 +1214,12 @@ double k1, k2;
* Return true if equation side was modified.
*/
int
-combine_constants(equation, np, iflag)
-token_type *equation; /* pointer to the beginning of equation side */
-int *np; /* pointer to length of equation side */
-int iflag; /* produce imaginary numbers flag */
+combine_constants(MathoMatic* mathomatic, token_type *equation, int *np, int iflag)
+//token_type *equation; /* pointer to the beginning of equation side */
+//int *np; /* pointer to length of equation side */
+//int iflag; /* produce imaginary numbers flag */
{
- return const_recurse(equation, np, 0, 1, iflag);
+ return const_recurse(mathomatic, equation, np, 0, 1, iflag);
}
/*
@@ -1252,11 +1229,11 @@ int iflag; /* produce imaginary numbers flag */
* domain_check must be set to false after this.
*/
int
-calc(op1p, k1p, op2, k2)
-int *op1p; /* Pointer to operator 1, which comes immediately before operand 1 if this operator exists. */
-double *k1p; /* Pointer to operand 1 and where to store the result of the calculation. */
-int op2; /* Operator 2; always exists and comes immediately before operand 2, and usually after operand 1. */
-double k2; /* Operand 2; ignored for unary operators. */
+calc(MathoMatic* mathomatic, int *op1p, double *k1p, int op2, double k2)
+//int *op1p; /* Pointer to operator 1, which comes immediately before operand 1 if this operator exists. */
+//double *k1p; /* Pointer to operand 1 and where to store the result of the calculation. */
+//int op2; /* Operator 2; always exists and comes immediately before operand 2, and usually after operand 1. */
+//double k2; /* Operand 2; ignored for unary operators. */
{
#if _REENTRANT
int sign = 1;
@@ -1264,7 +1241,7 @@ double k2; /* Operand 2; ignored for unary operators. */
int op1;
double d, d1, d2;
- domain_check = false;
+ mathomatic->domain_check = false;
errno = 0;
if (op1p) {
op1 = *op1p;
@@ -1278,7 +1255,7 @@ double k2; /* Operand 2; ignored for unary operators. */
d = -(*k1p);
else
d = *k1p;
- d1 = fabs(d) * epsilon;
+ d1 = fabs(d) * mathomatic->epsilon;
if (op2 == PLUS) {
d += k2;
} else {
@@ -1306,28 +1283,28 @@ double k2; /* Operand 2; ignored for unary operators. */
*k1p *= k2;
} else {
if (op1 == DIVIDE) {
- check_divide_by_zero(*k1p);
+ check_divide_by_zero(mathomatic, *k1p);
*k1p = k2 / *k1p;
*op1p = TIMES;
} else if (op2 == DIVIDE) {
- check_divide_by_zero(k2);
+ check_divide_by_zero(mathomatic, k2);
*k1p = *k1p / k2;
}
}
break;
case IDIVIDE:
- check_divide_by_zero(k2);
+ check_divide_by_zero(mathomatic, k2);
modf(*k1p / k2, k1p);
break;
case MODULUS:
if (k2 == 0) {
- warning(_("Modulo 0 encountered."));
+ warning(mathomatic, _("Modulo 0 encountered."));
}
*k1p = fixed_fmod(*k1p, k2);
- if (modulus_mode && *k1p < 0.0) {
+ if (mathomatic->modulus_mode && *k1p < 0.0) {
*k1p += fabs(k2); /* make positive */
}
- if (modulus_mode == 1 && k2 < 0.0 && *k1p > 0.0) {
+ if (mathomatic->modulus_mode == 1 && k2 < 0.0 && *k1p > 0.0) {
*k1p += k2; /* make negative */
}
/* modulus_mode == 0 result same sign as dividend,
@@ -1339,19 +1316,19 @@ double k2; /* Operand 2; ignored for unary operators. */
/* it's probably imaginary; pow() will give a domain error, so skip these calculations */
break;
}
- domain_check = true;
+ mathomatic->domain_check = true;
if (*k1p == 0.0 && k2 == 0.0) {
- warning(_("0^0 encountered, might be considered indeterminate."));
+ warning(mathomatic, _("0^0 encountered, might be considered indeterminate."));
d = 1.0; /* 0^0 = 1 */
} else if (*k1p == 0 && k2 < 0.0) {
- warning(_("Divide by zero (0 raised to negative power)."));
+ warning(mathomatic, _("Divide by zero (0 raised to negative power)."));
d = INFINITY;
} else {
d = pow(*k1p, k2);
- if (preserve_surds && !approximate_roots) {
- if (isfinite(k2) && fmod(k2, 1.0) != 0.0 && f_to_fraction(*k1p, &d1, &d2)) {
- if (!f_to_fraction(d, &d1, &d2)) {
- domain_check = false;
+ if (mathomatic->preserve_surds && !mathomatic->approximate_roots) {
+ if (isfinite(k2) && fmod(k2, 1.0) != 0.0 && f_to_fraction(mathomatic, *k1p, &d1, &d2)) {
+ if (!f_to_fraction(mathomatic, d, &d1, &d2)) {
+ mathomatic->domain_check = false;
return false;
}
}
@@ -1359,12 +1336,12 @@ double k2; /* Operand 2; ignored for unary operators. */
}
#if 1
if (errno == ERANGE) { /* preserve overflowed powers rather than aborting with an error message */
- domain_check = false;
+ mathomatic->domain_check = false;
return false;
}
#endif
- check_err();
- if (domain_check)
+ check_err(mathomatic);
+ if (mathomatic->domain_check)
*k1p = d;
break;
case FACTORIAL:
@@ -1410,9 +1387,7 @@ double k2; /* Operand 2; ignored for unary operators. */
}
static int
-const_recurse(equation, np, loc, level, iflag)
-token_type *equation;
-int *np, loc, level, iflag;
+const_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int iflag)
{
int loc1, old_loc;
int const_count = 0;
@@ -1430,7 +1405,7 @@ beginning:
return modified;
}
if (equation[loc].level > level) {
- modified |= const_recurse(equation, np, loc, level + 1, iflag);
+ modified |= const_recurse(mathomatic, equation, np, loc, level + 1, iflag);
for (; loc < *np && equation[loc].level > level; loc++)
;
goto beginning;
@@ -1444,18 +1419,18 @@ beginning:
op = equation[loc-1].token.operatr;
d1 = equation[loc1].token.constant;
d2 = equation[loc].token.constant;
- if (calc((loc1 <= old_loc) ? NULL : &equation[loc1-1].token.operatr, &d1, op, d2)) {
- if (op == POWER && !domain_check) {
- if (!f_to_fraction(d2, &numerator, &denominator)) { /* if irrational power */
- if (!iflag || (preserve_surds && !approximate_roots))
+ if (calc(mathomatic, (loc1 <= old_loc) ? NULL : &equation[loc1-1].token.operatr, &d1, op, d2)) {
+ if (op == POWER && !mathomatic->domain_check) {
+ if (!f_to_fraction(mathomatic, d2, &numerator, &denominator)) { /* if irrational power */
+ if (!iflag || (mathomatic->preserve_surds && !mathomatic->approximate_roots))
return modified;
cv.re = d1;
cv.im = 0.0;
p.re = d2;
p.im = 0.0;
cv = complex_pow(cv, p);
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[loc1+2], &equation[loc1], (*np - loc1) * sizeof(token_type));
*np += 2;
@@ -1482,10 +1457,10 @@ beginning:
}
errno = 0;
d3 = pow(-d1, d2);
- check_err();
+ check_err(mathomatic);
if (!always_positive(denominator)) {
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[loc1+2], &equation[loc1], (*np - loc1) * sizeof(token_type));
*np += 2;
@@ -1515,8 +1490,8 @@ beginning:
}
if (!iflag)
return modified;
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[loc1+2], &equation[loc1], (*np - loc1) * sizeof(token_type));
*np += 2;
@@ -1564,13 +1539,13 @@ beginning:
} else {
equation[loc1].token.constant = d1;
modified = true;
- domain_check = false;
+ mathomatic->domain_check = false;
blt(&equation[loc-1], &equation[loc+1], (*np - (loc + 1)) * sizeof(token_type));
*np -= 2;
loc -= 2;
}
} else {
- domain_check = false;
+ mathomatic->domain_check = false;
}
}
}
@@ -1582,9 +1557,9 @@ beginning:
* Return true if equation side was significantly modified.
*/
int
-elim_k(equation, np)
-token_type *equation; /* equation side pointer */
-int *np; /* pointer to length of equation side */
+elim_k(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* equation side pointer */
+//int *np; /* pointer to length of equation side */
{
token_type *p1, *p2, *p3, *p4;
token_type *ep; /* end pointer */
@@ -1702,7 +1677,7 @@ int *np; /* pointer to length of equation side */
modified = true;
continue;
}
- if (fabs(p2->token.constant - 1.0) <= epsilon) {
+ if (fabs(p2->token.constant - 1.0) <= mathomatic->epsilon) {
/* Replace 1*x with x. */
blt(p2, p1 + 1, (char *) ep - (char *) (p1 + 1));
*np -= 2;
@@ -1751,8 +1726,8 @@ int *np; /* pointer to length of equation side */
p2 = p1 + 1;
if (p2->level == level && p2->kind == CONSTANT) {
/* Replace division by a constant with times its reciprocal. */
- f_to_fraction(p2->token.constant, &numerator, &denominator);
- check_divide_by_zero(numerator);
+ f_to_fraction(mathomatic, p2->token.constant, &numerator, &denominator);
+ check_divide_by_zero(mathomatic, numerator);
p2->token.constant = denominator / numerator;
p1->token.operatr = TIMES;
continue;
@@ -1779,12 +1754,12 @@ int *np; /* pointer to length of equation side */
if (p1->token.operatr == MODULUS) {
if ((p1 + 1)->level == level && (p1 + 1)->kind == CONSTANT) {
d = fabs((p1 + 1)->token.constant);
- if (d > epsilon && fmod(1.0 / d, 1.0) == 0.0) {
+ if (d > mathomatic->epsilon && fmod(1.0 / d, 1.0) == 0.0) {
for (p2 = p1 - 1; p2 > equation; p2--) {
if ((p2 - 1)->level < level)
break;
}
- if (is_integer_expr(p2, p1 - p2)) {
+ if (is_integer_expr(mathomatic, p2, p1 - p2)) {
blt(p2, p1 + 1, (char *) ep - (char *) (p1 + 1));
*np -= (p1 + 1) - p2;
p2->token.constant = 0.0;
@@ -1829,7 +1804,7 @@ int *np; /* pointer to length of equation side */
modified = true;
continue;
}
- if (fabs(p2->token.constant - 1.0) <= epsilon) {
+ if (fabs(p2->token.constant - 1.0) <= mathomatic->epsilon) {
/* Replace x^1 with x. */
blt(p1, p1 + 2, (char *) ep - (char *) (p1 + 2));
*np -= 2;
@@ -1853,12 +1828,12 @@ int *np; /* pointer to length of equation side */
* Otherwise return false.
*/
int
-se_compare(p1, n1, p2, n2, diff_signp)
-token_type *p1; /* first sub-expression pointer */
-int n1; /* first sub-expression length */
-token_type *p2; /* second sub-expression pointer */
-int n2; /* second sub-expression length */
-int *diff_signp; /* different sign flag pointer */
+se_compare(MathoMatic* mathomatic, token_type *p1, int n1, token_type *p2, int n2, int *diff_signp)
+//token_type *p1; /* first sub-expression pointer */
+//int n1; /* first sub-expression length */
+//token_type *p2; /* second sub-expression pointer */
+//int n2; /* second sub-expression length */
+//int *diff_signp; /* different sign flag pointer */
{
int l1, l2;
int rv;
@@ -1866,7 +1841,7 @@ int *diff_signp; /* different sign flag pointer */
int rv_should_be_false = false;
if (n1 < 1 || n2 < 1 || (n1 & 1) != 1 || (n2 & 1) != 1 || diff_signp == NULL || p1 == NULL || p2 == NULL) {
- error_bug("Programming error in call to se_compare().");
+ error_bug(mathomatic, "Programming error in call to se_compare().");
}
#endif
if (((n1 > n2) ? ((n1 + 1) / (n2 + 1)) : ((n2 + 1) / (n1 + 1))) > 3) {
@@ -1879,12 +1854,12 @@ int *diff_signp; /* different sign flag pointer */
#endif
}
/* Find the proper ground levels of parentheses for the two sub-expressions: */
- l1 = min_level(p1, n1);
- l2 = min_level(p2, n2);
- rv = compare_recurse(p1, n1, l1, p2, n2, l2, diff_signp);
+ l1 = min_level(mathomatic, p1, n1);
+ l2 = min_level(mathomatic, p2, n2);
+ rv = compare_recurse(mathomatic, p1, n1, l1, p2, n2, l2, diff_signp);
#if DEBUG
if (rv && rv_should_be_false) {
- error_bug("Expression compare optimization failed in se_compare().");
+ error_bug(mathomatic, "Expression compare optimization failed in se_compare().");
}
#endif
return rv;
@@ -1896,13 +1871,9 @@ int *diff_signp; /* different sign flag pointer */
* Optimizing or streamlining this would greatly speed things up.
*/
static int
-compare_recurse(p1, n1, l1, p2, n2, l2, diff_signp)
-token_type *p1;
-int n1, l1;
-token_type *p2;
-int n2, l2, *diff_signp;
+compare_recurse(MathoMatic* mathomatic, token_type *p1, int n1, int l1, token_type *p2, int n2, int l2, int *diff_signp)
{
-#define compare_epsilon epsilon
+#define compare_epsilon mathomatic->epsilon
token_type *pv1, *ep1, *ep2;
int i, j;
@@ -1922,7 +1893,7 @@ int n2, l2, *diff_signp;
}
switch (p1->kind) {
case VARIABLE:
- if (sign_cmp_flag && (p1->token.variable & VAR_MASK) == SIGN) {
+ if (mathomatic->sign_cmp_flag && (p1->token.variable & VAR_MASK) == SIGN) {
return((p2->token.variable & VAR_MASK) == SIGN);
} else {
return(p1->token.variable == p2->token.variable);
@@ -1947,14 +1918,14 @@ int n2, l2, *diff_signp;
}
break;
case OPERATOR:
- error_bug("Programming error in call to compare_recurse().");
+ error_bug(mathomatic, "Programming error in call to compare_recurse().");
break;
}
return false;
}
#if DEBUG
if (n1 < 1 || n2 < 1 || (n1 & 1) != 1 || (n2 & 1) != 1) {
- error_bug("Programming error in call to compare_recurse().");
+ error_bug(mathomatic, "Programming error in call to compare_recurse().");
}
#endif
ep1 = &p1[n1];
@@ -1999,7 +1970,7 @@ int n2, l2, *diff_signp;
if (diff_op) {
if (p1->kind == CONSTANT && p1->level == l1 && op1 == TIMES) {
if (fabs(fabs(p1->token.constant) - 1.0) <= compare_epsilon) {
- if (!compare_recurse(p1 + 2, n1 - 2, min_level(p1 + 2, n1 - 2), p2, n2, l2, diff_signp)) {
+ if (!compare_recurse(mathomatic, p1 + 2, n1 - 2, min_level(mathomatic, p1 + 2, n1 - 2), p2, n2, l2, diff_signp)) {
return false;
}
if (p1->token.constant < 0.0) {
@@ -2010,7 +1981,7 @@ int n2, l2, *diff_signp;
}
if (p2->kind == CONSTANT && p2->level == l2 && op2 == TIMES) {
if (fabs(fabs(p2->token.constant) - 1.0) <= compare_epsilon) {
- if (!compare_recurse(p1, n1, l1, p2 + 2, n2 - 2, min_level(p2 + 2, n2 - 2), diff_signp)) {
+ if (!compare_recurse(mathomatic, p1, n1, l1, p2 + 2, n2 - 2, min_level(mathomatic, p2 + 2, n2 - 2), diff_signp)) {
return false;
}
if (p2->token.constant < 0.0) {
@@ -2030,7 +2001,7 @@ no_op2:
opa2[oc2] = pv1 + 1;
used[oc2] = false;
if (++oc2 >= ARR_CNT(opa2)) {
- debug_string(1, "Expression too big to compare, because MAX_COMPARE_TERMS exceeded.");
+ debug_string(mathomatic, 1, "Expression too big to compare, because MAX_COMPARE_TERMS exceeded.");
return false;
}
}
@@ -2073,7 +2044,7 @@ no_op2:
return false;
break;
}
- if (compare_recurse(pv1, len, (pv1->level <= l1) ? l1 : (l1 + 1),
+ if (compare_recurse(mathomatic, pv1, len, (pv1->level <= l1) ? l1 : (l1 + 1),
opa2[i], (opa2[i+1] - opa2[i]) - 1, (opa2[i]->level <= l2) ? l2 : (l2 + 1), &j)) {
switch (op1) {
case 0:
@@ -2137,9 +2108,7 @@ Also (x-y)^5 should compare identical with different sign to (y-x)^5.
* Return true if equation side was modified.
*/
int
-elim_sign(equation, np)
-token_type *equation;
-int *np;
+elim_sign(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, j, k;
int level;
@@ -2151,7 +2120,7 @@ int *np;
for (i = 1; i < *np; i += 2) {
#if DEBUG
if (equation[i].kind != OPERATOR) {
- error_bug("Error in elim_sign().");
+ error_bug(mathomatic, "Error in elim_sign().");
}
#endif
level = equation[i].level;
@@ -2166,10 +2135,10 @@ int *np;
;
if (k <= i + 2)
continue;
- if (!is_integer_expr(&equation[i+3], k - (i + 3)))
+ if (!is_integer_expr(mathomatic, &equation[i+3], k - (i + 3)))
continue;
}
- f_to_fraction(equation[i+1].token.constant, &numerator, &denominator);
+ f_to_fraction(mathomatic, equation[i+1].token.constant, &numerator, &denominator);
if (always_positive(numerator)) {
if (equation[i-1].level == level
&& equation[i-1].kind == VARIABLE
@@ -2250,9 +2219,7 @@ int *np;
* Return true if equation side was modified.
*/
int
-div_imaginary(equation, np)
-token_type *equation;
-int *np;
+div_imaginary(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, j, k;
int n;
@@ -2266,7 +2233,7 @@ int *np;
for (i = 1; i < *np; i += 2) {
#if DEBUG
if (equation[i].kind != OPERATOR) {
- error_bug("Error in div_imaginary().");
+ error_bug(mathomatic, "Error in div_imaginary().");
}
#endif
if (equation[i].token.operatr == DIVIDE) {
@@ -2276,8 +2243,8 @@ int *np;
if (equation[i+1].level == level
&& equation[i+1].kind == VARIABLE
&& equation[i+1].token.variable == IMAGINARY) {
- if (*np + 2 > n_tokens) {
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[i+2], &equation[i], (*np - i) * sizeof(token_type));
*np += 2;
@@ -2338,41 +2305,41 @@ int *np;
}
}
}
- if ((eloc - (i + 1)) + 5 + (eiloc - biloc) + *np + 2 > n_tokens)
- error_huge();
+ if ((eloc - (i + 1)) + 5 + (eiloc - biloc) + *np + 2 > mathomatic->n_tokens)
+ error_huge(mathomatic);
n = eloc - (i + 1);
- blt(scratch, &equation[i+1], n * sizeof(token_type));
- scratch[iloc-(i+1)].kind = CONSTANT;
- scratch[iloc-(i+1)].token.constant = 0.0;
+ blt(mathomatic->scratch, &equation[i+1], n * sizeof(token_type));
+ mathomatic->scratch[iloc-(i+1)].kind = CONSTANT;
+ mathomatic->scratch[iloc-(i+1)].token.constant = 0.0;
for (j = 0; j < n; j++)
- scratch[j].level += 2;
- scratch[n].level = level + 2;
- scratch[n].kind = OPERATOR;
- scratch[n].token.operatr = POWER;
+ mathomatic->scratch[j].level += 2;
+ mathomatic->scratch[n].level = level + 2;
+ mathomatic->scratch[n].kind = OPERATOR;
+ mathomatic->scratch[n].token.operatr = POWER;
n++;
- scratch[n].level = level + 2;
- scratch[n].kind = CONSTANT;
- scratch[n].token.constant = 2.0;
+ mathomatic->scratch[n].level = level + 2;
+ mathomatic->scratch[n].kind = CONSTANT;
+ mathomatic->scratch[n].token.constant = 2.0;
n++;
- scratch[n].level = level + 1;
- scratch[n].kind = OPERATOR;
- scratch[n].token.operatr = PLUS;
+ mathomatic->scratch[n].level = level + 1;
+ mathomatic->scratch[n].kind = OPERATOR;
+ mathomatic->scratch[n].token.operatr = PLUS;
n++;
- blt(&scratch[n], &equation[biloc], (eiloc - biloc) * sizeof(token_type));
+ blt(&mathomatic->scratch[n], &equation[biloc], (eiloc - biloc) * sizeof(token_type));
j = n;
n += (eiloc - biloc);
for (k = j; k < n; k++)
- scratch[k].level += 2;
- scratch[n].level = level + 2;
- scratch[n].kind = OPERATOR;
- scratch[n].token.operatr = POWER;
+ mathomatic->scratch[k].level += 2;
+ mathomatic->scratch[n].level = level + 2;
+ mathomatic->scratch[n].kind = OPERATOR;
+ mathomatic->scratch[n].token.operatr = POWER;
n++;
- scratch[n].level = level + 2;
- scratch[n].kind = CONSTANT;
- scratch[n].token.constant = 2.0;
+ mathomatic->scratch[n].level = level + 2;
+ mathomatic->scratch[n].kind = CONSTANT;
+ mathomatic->scratch[n].token.constant = 2.0;
n++;
- scratch[j+(iloc-biloc)].kind = CONSTANT;
- scratch[j+(iloc-biloc)].token.constant = 1.0;
+ mathomatic->scratch[j+(iloc-biloc)].kind = CONSTANT;
+ mathomatic->scratch[j+(iloc-biloc)].token.constant = 1.0;
blt(&equation[iloc+2], &equation[iloc], (*np - iloc) * sizeof(token_type));
*np += 2;
ilevel++;
@@ -2387,7 +2354,7 @@ int *np;
equation[iloc].level = ilevel;
blt(&equation[i+1+n], &equation[i], (*np - i) * sizeof(token_type));
*np += n + 1;
- blt(&equation[i+1], scratch, n * sizeof(token_type));
+ blt(&equation[i+1], mathomatic->scratch, n * sizeof(token_type));
i += n + 1;
equation[i].token.operatr = TIMES;
modified = true;
@@ -2405,19 +2372,13 @@ int *np;
* This routine also checks the validity of the equation side.
*/
int
-reorder(equation, np)
-token_type *equation;
-int *np;
+reorder(MathoMatic* mathomatic, token_type *equation, int *np)
{
- return(order_recurse(equation, np, 0, 1));
+ return(order_recurse(mathomatic, equation, np, 0, 1));
}
static void
-swap(equation, np, level, i1, i2)
-token_type *equation;
-int *np;
-int level;
-int i1, i2;
+swap(MathoMatic* mathomatic, token_type *equation, int *np, int level, int i1, int i2)
{
int e1, e2;
int n1, n2;
@@ -2432,12 +2393,12 @@ int i1, i2;
}
n1 = e1 - i1;
n2 = e2 - i2;
- blt(scratch, &equation[i1], (e2 - i1) * sizeof(token_type));
+ blt(mathomatic->scratch, &equation[i1], (e2 - i1) * sizeof(token_type));
if ((i1 + n2) != e1) {
blt(&equation[i1+n2], &equation[e1], (i2 - e1) * sizeof(token_type));
}
- blt(&equation[i1], &scratch[i2-i1], n2 * sizeof(token_type));
- blt(&equation[e2-n1], scratch, n1 * sizeof(token_type));
+ blt(&equation[i1], &mathomatic->scratch[i2-i1], n2 * sizeof(token_type));
+ blt(&equation[e2-n1], mathomatic->scratch, n1 * sizeof(token_type));
}
/*
@@ -2445,9 +2406,7 @@ int i1, i2;
* which starts at "loc" in an equation side.
*/
static int
-order_recurse(equation, np, loc, level)
-token_type *equation;
-int *np, loc, level;
+order_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level)
{
int i, j, k, n;
int op = 0;
@@ -2465,7 +2424,7 @@ int *np, loc, level;
break;
}
if (equation[i].level > level) {
- modified |= order_recurse(equation, np, i, level + 1);
+ modified |= order_recurse(mathomatic, equation, np, i, level + 1);
i++;
for (; i < *np && equation[i].level > level; i++)
;
@@ -2511,7 +2470,7 @@ int *np, loc, level;
&& (equation[loc+1].token.operatr == TIMES || equation[loc+1].token.operatr == DIVIDE))) {
for (j = loc + 1; j < i; j += 2) {
if (equation[j].level == level && equation[j].token.operatr == PLUS) {
- swap(equation, np, level, loc, j + 1);
+ swap(mathomatic, equation, np, level, loc, j + 1);
modified = true;
break;
}
@@ -2532,9 +2491,9 @@ int *np, loc, level;
for (n = k + 2; n < i && equation[n].level > level; n += 2)
;
n -= k;
- blt(scratch, &equation[k], n * sizeof(token_type));
+ blt(mathomatic->scratch, &equation[k], n * sizeof(token_type));
blt(&equation[j+n], &equation[j], (k - j) * sizeof(token_type));
- blt(&equation[j], scratch, n * sizeof(token_type));
+ blt(&equation[j], mathomatic->scratch, n * sizeof(token_type));
j += n;
k += n;
modified = true;
@@ -2546,7 +2505,7 @@ int *np, loc, level;
}
return modified;
corrupt:
- error_bug("Internal representation of expression is corrupt!");
+ error_bug(mathomatic, "Internal representation of expression is corrupt!");
return modified; /* not reached */
}
@@ -2559,9 +2518,7 @@ corrupt:
* Unfactoring needs to be done immediately, if this returns true.
*/
int
-rationalize(equation, np)
-token_type *equation;
-int *np;
+rationalize(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, j, k;
int i1, k1;
@@ -2577,7 +2534,7 @@ be_thorough:
break;
#if DEBUG
if (equation[i].kind != OPERATOR) {
- error_bug("Bug in rationalize().");
+ error_bug(mathomatic, "Bug in rationalize().");
}
#endif
if (equation[i].token.operatr == DIVIDE) {
@@ -2648,21 +2605,21 @@ do_again:
}
neg_one_loc = i1 + 1;
k = i1 - i;
- blt(scratch, &equation[i+1], k * sizeof(token_type));
- scratch[k].level = div_level + 2;
- scratch[k].kind = CONSTANT;
- scratch[k].token.constant = -1.0;
+ blt(mathomatic->scratch, &equation[i+1], k * sizeof(token_type));
+ mathomatic->scratch[k].level = div_level + 2;
+ mathomatic->scratch[k].kind = CONSTANT;
+ mathomatic->scratch[k].token.constant = -1.0;
k++;
- scratch[k].level = div_level + 2;
- scratch[k].kind = OPERATOR;
- scratch[k].token.operatr = TIMES;
+ mathomatic->scratch[k].level = div_level + 2;
+ mathomatic->scratch[k].kind = OPERATOR;
+ mathomatic->scratch[k].token.operatr = TIMES;
k++;
- blt(&scratch[k], &equation[neg_one_loc], (end_loc - neg_one_loc) * sizeof(token_type));
+ blt(&mathomatic->scratch[k], &equation[neg_one_loc], (end_loc - neg_one_loc) * sizeof(token_type));
for (k1 = 0; k1 < (j - neg_one_loc); k1++, k++)
- scratch[k].level++;
+ mathomatic->scratch[k].level++;
k = end_loc - (i + 1) + 2;
- if (*np + 2 * (k + 1) > n_tokens) {
- error_huge();
+ if (*np + 2 * (k + 1) > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[end_loc+(2*(k+1))], &equation[end_loc], (*np - end_loc) * sizeof(token_type));
*np += 2 * (k + 1);
@@ -2671,16 +2628,16 @@ do_again:
equation[k1].kind = OPERATOR;
equation[k1].token.operatr = TIMES;
k1++;
- blt(&equation[k1], scratch, k * sizeof(token_type));
+ blt(&equation[k1], mathomatic->scratch, k * sizeof(token_type));
k1 += k;
equation[k1].level = div_level;
equation[k1].kind = OPERATOR;
equation[k1].token.operatr = DIVIDE;
k1++;
- blt(&equation[k1], scratch, k * sizeof(token_type));
+ blt(&equation[k1], mathomatic->scratch, k * sizeof(token_type));
k1 += k;
- debug_string(1, "Square roots in denominator rationalized:");
- side_debug(1, &equation[i+1], k1 - (i + 1));
+ debug_string(mathomatic, 1, "Square roots in denominator rationalized:");
+ side_debug(mathomatic, 1, &equation[i+1], k1 - (i + 1));
i = k1;
modified = true;
goto be_thorough;
diff --git a/solve.c b/solve.c
index c142a9d..48154c9 100644
--- a/solve.c
+++ b/solve.c
@@ -26,15 +26,11 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
#define MAX_RAISE_POWER 20 /* Maximum number of times to increase power in solve function. */
-static int increase(double d, long v);
-static int poly_solve(long v);
-static int g_of_f(int op, token_type *operandp, token_type *side1p, int *side1np, token_type *side2p, int *side2np);
-static int flip(token_type *side1p, int *side1np, token_type *side2p, int *side2np);
+static int increase(MathoMatic* mathomatic, double d, long v);
+static int poly_solve(MathoMatic* mathomatic, long v);
+static int g_of_f(MathoMatic* mathomatic, int op, token_type *operandp, token_type *side1p, int *side1np, token_type *side2p, int *side2np);
+static int flip(MathoMatic* mathomatic, token_type *side1p, int *side1np, token_type *side2p, int *side2np);
-static int repeat_count;
-static int prev_n1, prev_n2;
-
-static int last_int_var = 0;
/*
* Solve using equation spaces. Almost always displays a message.
@@ -44,50 +40,50 @@ static int last_int_var = 0;
* preferably with "simplify quick". Just plain "simplify" expands too much sometimes.
*/
int
-solve_espace(want, have)
-int want; /* equation number containing what to solve for */
-int have; /* equation number to solve */
+solve_espace(MathoMatic* mathomatic, int want, int have)
+//int want; /* equation number containing what to solve for */
+//int have; /* equation number to solve */
{
int i;
jmp_buf save_save;
int rv = 0; /* solve_sub() return value */
- if (want == have || !equation_space_is_equation(have)) {
+ if (want == have || !equation_space_is_equation(mathomatic, have)) {
#if LIBRARY || !HELP
- error(_("Solving requires an equation."));
+ error(mathomatic, _("Solving requires an equation."));
#else
- error(_("Please enter an equation to solve, or a command like \"help\"."));
+ error(mathomatic, _("Please enter an equation to solve, or a command like \"help\"."));
#endif
printf(_("Solve failed for equation space #%d.\n"), have + 1);
return false;
}
- blt(save_save, jmp_save, sizeof(jmp_save));
- if ((i = setjmp(jmp_save)) != 0) { /* trap errors */
- clean_up();
+ blt(save_save, mathomatic->jmp_save, sizeof(mathomatic->jmp_save));
+ if ((i = setjmp(mathomatic->jmp_save)) != 0) { /* trap errors */
+ clean_up(mathomatic);
if (i == 14) {
- error(_("Expression too large."));
+ error(mathomatic, _("Expression too large."));
}
rv = 0;
} else {
- if (n_lhs[want]) {
- if (n_rhs[want]) {
+ if (mathomatic->n_lhs[want]) {
+ if (mathomatic->n_rhs[want]) {
/* Something in the LHS and RHS of equation number "want". */
- error(_("Can only solve for a single variable or for 0, possibly raised to a power."));
+ error(mathomatic, _("Can only solve for a single variable or for 0, possibly raised to a power."));
rv = 0;
} else {
/* Normal solve: */
- rv = solve_sub(lhs[want], n_lhs[want], lhs[have], &n_lhs[have], rhs[have], &n_rhs[have]);
+ rv = solve_sub(mathomatic, mathomatic->lhs[want], mathomatic->n_lhs[want], mathomatic->lhs[have], &mathomatic->n_lhs[have], mathomatic->rhs[have], &mathomatic->n_rhs[have]);
}
} else {
/* Solve variable was preceded by an equals sign, solve using reversed equation sides: */
- rv = solve_sub(rhs[want], n_rhs[want], rhs[have], &n_rhs[have], lhs[have], &n_lhs[have]);
+ rv = solve_sub(mathomatic, mathomatic->rhs[want], mathomatic->n_rhs[want], mathomatic->rhs[have], &mathomatic->n_rhs[have], mathomatic->lhs[have], &mathomatic->n_lhs[have]);
}
}
- blt(jmp_save, save_save, sizeof(jmp_save));
+ blt(mathomatic->jmp_save, save_save, sizeof(mathomatic->jmp_save));
if (rv <= 0) {
printf(_("Solve failed for equation space #%d.\n"), have + 1);
} else {
- debug_string(0, _("Solve successful:"));
+ debug_string(mathomatic, 0, _("Solve successful:"));
}
return(rv > 0);
}
@@ -109,13 +105,13 @@ int have; /* equation number to solve */
* Returns -2 if unsolvable in all realms.
*/
int
-solve_sub(wantp, wantn, leftp, leftnp, rightp, rightnp)
-token_type *wantp; /* expression to solve for */
-int wantn; /* length of expression to solve for */
-token_type *leftp; /* LHS of equation */
-int *leftnp; /* pointer to length of LHS */
-token_type *rightp; /* RHS of equation */
-int *rightnp; /* pointer to length of RHS */
+solve_sub(MathoMatic* mathomatic, token_type *wantp, int wantn, token_type *leftp, int *leftnp, token_type *rightp, int *rightnp)
+//token_type *wantp; /* expression to solve for */
+//int wantn; /* length of expression to solve for */
+//token_type *leftp; /* LHS of equation */
+//int *leftnp; /* pointer to length of LHS */
+//token_type *rightp; /* RHS of equation */
+//int *rightnp; /* pointer to length of RHS */
{
int i, j;
int found, found_count;
@@ -134,14 +130,14 @@ int *rightnp; /* pointer to length of RHS */
double numerator, denominator;
int success = 1;
- repeat_count = 0;
- prev_n1 = 0;
- prev_n2 = 0;
+ mathomatic->repeat_count = 0;
+ mathomatic->prev_n1 = 0;
+ mathomatic->prev_n2 = 0;
if (*leftnp <= 0 || *rightnp <= 0) {
#if LIBRARY || !HELP
- error(_("Solving requires an equation."));
+ error(mathomatic, _("Solving requires an equation."));
#else
- error(_("Please enter an equation to solve, or a command like \"help\"."));
+ error(mathomatic, _("Please enter an equation to solve, or a command like \"help\"."));
#endif
return false;
}
@@ -156,87 +152,87 @@ int *rightnp; /* pointer to length of RHS */
if (wantp[0].kind == VARIABLE) {
v = wantp[0].token.variable;
}
- if (solve_sub(&zero_token, 1, rightp, rightnp, leftp, leftnp) <= 0)
+ if (solve_sub(mathomatic, &mathomatic->zero_token, 1, rightp, rightnp, leftp, leftnp) <= 0)
return false;
- n_tlhs = *leftnp;
- blt(tlhs, leftp, n_tlhs * sizeof(*leftp));
- n_trhs = *rightnp;
- blt(trhs, rightp, n_trhs * sizeof(*rightp));
- uf_simp(tlhs, &n_tlhs);
- if (increase(1 / wantp[2].token.constant, v) != true) {
- error(_("Unable to isolate root."));
+ mathomatic->n_tlhs = *leftnp;
+ blt(mathomatic->tlhs, leftp, mathomatic->n_tlhs * sizeof(*leftp));
+ mathomatic->n_trhs = *rightnp;
+ blt(mathomatic->trhs, rightp, mathomatic->n_trhs * sizeof(*rightp));
+ uf_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ if (increase(mathomatic, 1 / wantp[2].token.constant, v) != true) {
+ error(mathomatic, _("Unable to isolate root."));
return false;
}
- list_tdebug(2);
- mid_simp_side(tlhs, &n_tlhs);
- simp_loop(trhs, &n_trhs);
- uf_simp(trhs, &n_trhs);
- list_tdebug(1);
+ list_tdebug(mathomatic, 2);
+ mid_simp_side(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ simp_loop(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ uf_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ list_tdebug(mathomatic, 1);
- blt(leftp, tlhs, n_tlhs * sizeof(*leftp));
- *leftnp = n_tlhs;
- blt(rightp, trhs, n_trhs * sizeof(*rightp));
- *rightnp = n_trhs;
+ blt(leftp, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(*leftp));
+ *leftnp = mathomatic->n_tlhs;
+ blt(rightp, mathomatic->trhs, mathomatic->n_trhs * sizeof(*rightp));
+ *rightnp = mathomatic->n_trhs;
return true;
}
- error(_("Can only solve for a single variable or for 0, possibly raised to a power."));
+ error(mathomatic, _("Can only solve for a single variable or for 0, possibly raised to a power."));
return false;
}
/* copy the equation to temporary storage where it will be manipulated */
- n_tlhs = *leftnp;
- blt(tlhs, leftp, n_tlhs * sizeof(*leftp));
- n_trhs = *rightnp;
- blt(trhs, rightp, n_trhs * sizeof(*rightp));
+ mathomatic->n_tlhs = *leftnp;
+ blt(mathomatic->tlhs, leftp, mathomatic->n_tlhs * sizeof(*leftp));
+ mathomatic->n_trhs = *rightnp;
+ blt(mathomatic->trhs, rightp, mathomatic->n_trhs * sizeof(*rightp));
if (wantp->kind == VARIABLE) {
v = wantp->token.variable;
- if (!found_var(trhs, n_trhs, v) && !found_var(tlhs, n_tlhs, v)) {
+ if (!found_var(mathomatic->trhs, mathomatic->n_trhs, v) && !found_var(mathomatic->tlhs, mathomatic->n_tlhs, v)) {
/* variable v is 0 or not found */
- error(_("Solve variable not found."));
+ error(mathomatic, _("Solve variable not found."));
return false;
}
zsolve = false;
} else {
v = 0;
if (wantp->kind != CONSTANT || wantp->token.constant != 0.0) {
- error(_("Can only solve for a single variable or for 0, possibly raised to a power."));
+ error(mathomatic, _("Can only solve for a single variable or for 0, possibly raised to a power."));
return false;
}
- debug_string(1, _("Solving for zero..."));
+ debug_string(mathomatic, 1, _("Solving for zero..."));
zsolve = true;
}
- uf_power(tlhs, &n_tlhs);
- uf_power(trhs, &n_trhs);
+ uf_power(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ uf_power(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
simp_again:
/* Make sure equation is a bit simplified. */
- list_tdebug(2);
- simps_side(tlhs, &n_tlhs, zsolve);
+ list_tdebug(mathomatic, 2);
+ simps_side(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, zsolve);
if (uf_flag) {
- simp_loop(trhs, &n_trhs);
- uf_simp(trhs, &n_trhs);
- factorv(trhs, &n_trhs, v);
+ simp_loop(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ uf_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ factorv(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, v);
} else {
- simps_side(trhs, &n_trhs, zsolve);
+ simps_side(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, zsolve);
}
- list_tdebug(1);
+ list_tdebug(mathomatic, 1);
no_simp:
/* First selectively move sub-expressions from the RHS to the LHS. */
op = 0;
- ep = &trhs[n_trhs];
+ ep = &mathomatic->trhs[mathomatic->n_trhs];
if (zsolve) {
- for (b1 = p1 = trhs; p1 < ep; p1++) {
+ for (b1 = p1 = mathomatic->trhs; p1 < ep; p1++) {
if (p1->level == 1 && p1->kind == OPERATOR) {
op = p1->token.operatr;
b1 = p1 + 1;
if (op == DIVIDE) {
- if (!g_of_f(op, b1, trhs, &n_trhs, tlhs, &n_tlhs))
+ if (!g_of_f(mathomatic, op, b1, mathomatic->trhs, &mathomatic->n_trhs, mathomatic->tlhs, &mathomatic->n_tlhs))
return false;
goto simp_again;
}
}
}
} else {
- for (b1 = p1 = trhs; p1 < ep; p1++) {
+ for (b1 = p1 = mathomatic->trhs; p1 < ep; p1++) {
if (p1->kind == VARIABLE && v == p1->token.variable) {
if (op == 0) {
for (p1++;; p1++) {
@@ -266,13 +262,13 @@ no_simp:
case TIMES:
case DIVIDE:
case POWER:
- b1 = trhs;
+ b1 = mathomatic->trhs;
op = PLUS;
for (p1 = b1; p1 < ep; p1++)
p1->level++;
break;
}
- if (!g_of_f(op, b1, trhs, &n_trhs, tlhs, &n_tlhs))
+ if (!g_of_f(mathomatic, op, b1, mathomatic->trhs, &mathomatic->n_trhs, mathomatic->tlhs, &mathomatic->n_tlhs))
return false;
goto simp_again;
} else if (p1->level == 1 && p1->kind == OPERATOR) {
@@ -282,33 +278,33 @@ no_simp:
}
}
if (uf_flag) {
- simps_side(trhs, &n_trhs, zsolve);
+ simps_side(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, zsolve);
}
left_again:
worked = true;
uf_flag = false;
see_work:
- if (found_var(trhs, n_trhs, v)) {
+ if (found_var(mathomatic->trhs, mathomatic->n_trhs, v)) {
/* solve variable in RHS */
- debug_string(1, _("Solve variable moved back to RHS, quitting solve routine."));
+ debug_string(mathomatic, 1, _("Solve variable moved back to RHS, quitting solve routine."));
return false;
}
/* See if we have solved the equation. */
- if (se_compare(wantp, wantn, tlhs, n_tlhs, &diff_sign) && !diff_sign) {
+ if (se_compare(mathomatic, wantp, wantn, mathomatic->tlhs, mathomatic->n_tlhs, &diff_sign) && !diff_sign) {
if (zsolve) {
- debug_string(1, "Simplifying the zero solve until there are no more divides:");
+ debug_string(mathomatic, 1, "Simplifying the zero solve until there are no more divides:");
zero_simp:
- list_tdebug(2);
- uf_power(trhs, &n_trhs);
+ list_tdebug(mathomatic, 2);
+ uf_power(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
do {
do {
- simp_ssub(trhs, &n_trhs, 0L, 0.0, false, true, 4);
- } while (uf_power(trhs, &n_trhs));
- } while (super_factor(trhs, &n_trhs, 1));
- list_tdebug(1);
- ep = &trhs[n_trhs];
+ simp_ssub(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, 0L, 0.0, false, true, 4);
+ } while (uf_power(mathomatic, mathomatic->trhs, &mathomatic->n_trhs));
+ } while (super_factor(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, 1));
+ list_tdebug(mathomatic, 1);
+ ep = &mathomatic->trhs[mathomatic->n_trhs];
op = 0;
- for (p1 = trhs + 1; p1 < ep; p1 += 2) {
+ for (p1 = mathomatic->trhs + 1; p1 < ep; p1 += 2) {
if (p1->level == 1) {
op = p1->token.operatr;
if (op == DIVIDE) {
@@ -321,13 +317,13 @@ zero_simp:
}
switch (op) {
case TIMES:
- for (p1 = trhs; p1 < ep;) {
+ for (p1 = mathomatic->trhs; p1 < ep;) {
b1 = p1;
for (;; p1++) {
if (p1 >= ep || (p1->kind == OPERATOR && p1->level == 1)) {
blt(b1 + 1, p1, (char *) ep - (char *) p1);
- n_trhs -= p1 - (b1 + 1);
- *b1 = one_token;
+ mathomatic->n_trhs -= p1 - (b1 + 1);
+ *b1 = mathomatic->one_token;
goto zero_simp;
}
if (p1->kind != CONSTANT && p1->kind != OPERATOR
@@ -340,7 +336,7 @@ zero_simp:
;
#if DEBUG
if (p1 != ep && (p1->kind != OPERATOR || p1->token.operatr != TIMES)) {
- error_bug("Operator mix up in zero_simp.");
+ error_bug(mathomatic, "Operator mix up in zero_simp.");
}
#endif
if ((p1 - 2) > b1) {
@@ -363,21 +359,21 @@ zero_simp:
/* if ((p1 - 2) <= trhs || (p1 - 2)->token.operatr != POWER || (p1 - 2)->level != 2) { */
p1++;
if (p1->level == 1 && p1->kind == CONSTANT && p1->token.constant > 0.0) {
- n_trhs -= 2;
+ mathomatic->n_trhs -= 2;
goto zero_simp;
}
/* } */
break;
}
- debug_string(1, _("Solve for zero completed:"));
+ debug_string(mathomatic, 1, _("Solve for zero completed:"));
} else {
- debug_string(1, _("Solve completed:"));
+ debug_string(mathomatic, 1, _("Solve completed:"));
}
- list_tdebug(1);
- blt(leftp, tlhs, n_tlhs * sizeof(*leftp));
- *leftnp = n_tlhs;
- blt(rightp, trhs, n_trhs * sizeof(*rightp));
- *rightnp = n_trhs;
+ list_tdebug(mathomatic, 1);
+ blt(leftp, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(*leftp));
+ *leftnp = mathomatic->n_tlhs;
+ blt(rightp, mathomatic->trhs, mathomatic->n_trhs * sizeof(*rightp));
+ *rightnp = mathomatic->n_trhs;
return success;
}
/* Move what we don't want in the LHS to the RHS. */
@@ -385,11 +381,11 @@ zero_simp:
need_flip = 0;
found = 0;
op = 0;
- ep = &tlhs[n_tlhs];
- for (b1 = p1 = tlhs;; p1++) {
+ ep = &mathomatic->tlhs[mathomatic->n_tlhs];
+ for (b1 = p1 = mathomatic->tlhs;; p1++) {
if (p1 >= ep || (p1->level == 1 && p1->kind == OPERATOR)) {
if (!found) {
- if ((p1 < ep || found_count || zsolve || n_tlhs > 1 || tlhs[0].kind != CONSTANT)
+ if ((p1 < ep || found_count || zsolve || mathomatic->n_tlhs > 1 || mathomatic->tlhs[0].kind != CONSTANT)
&& (p1 - b1 != 1 || b1->kind != CONSTANT || b1->token.constant != 1.0
|| p1 >= ep || p1->token.operatr != DIVIDE)) {
if (op == 0) {
@@ -428,23 +424,23 @@ zero_simp:
}
} else {
if (op != DIVIDE) {
- b1 = tlhs;
+ b1 = mathomatic->tlhs;
op = PLUS;
for (p1 = b1; p1 < ep; p1++)
p1->level++;
}
}
}
- if (!g_of_f(op, b1, tlhs, &n_tlhs, trhs, &n_trhs))
+ if (!g_of_f(mathomatic, op, b1, mathomatic->tlhs, &mathomatic->n_tlhs, mathomatic->trhs, &mathomatic->n_trhs))
return false;
- list_tdebug(2);
+ list_tdebug(mathomatic, 2);
if (uf_flag) {
- simp_loop(tlhs, &n_tlhs);
+ simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
} else {
- simps_side(tlhs, &n_tlhs, zsolve);
+ simps_side(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, zsolve);
}
- simps_side(trhs, &n_trhs, zsolve);
- list_tdebug(1);
+ simps_side(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, zsolve);
+ list_tdebug(mathomatic, 1);
goto see_work;
}
} else if (op == DIVIDE) {
@@ -452,74 +448,74 @@ zero_simp:
}
if (p1 >= ep) {
if (found_count == 0) { /* if solve variable no longer in LHS */
- if (found_var(trhs, n_trhs, v)) {
+ if (found_var(mathomatic->trhs, mathomatic->n_trhs, v)) {
/* solve variable in RHS */
- debug_string(1, _("Solve variable moved back to RHS, quitting solve routine."));
+ debug_string(mathomatic, 1, _("Solve variable moved back to RHS, quitting solve routine."));
return false;
}
/* The following code determines if we have an identity: */
- calc_simp(tlhs, &n_tlhs);
- calc_simp(trhs, &n_trhs);
- if (se_compare(tlhs, n_tlhs, trhs, n_trhs, &diff_sign) && !diff_sign) {
- error(_("This equation is an identity."));
- debug_string(0, _("That is, the LHS is identical to the RHS."));
+ calc_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ calc_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ if (se_compare(mathomatic, mathomatic->tlhs, mathomatic->n_tlhs, mathomatic->trhs, mathomatic->n_trhs, &diff_sign) && !diff_sign) {
+ error(mathomatic, _("This equation is an identity."));
+ debug_string(mathomatic, 0, _("That is, the LHS is identical to the RHS."));
return -1;
}
found = false;
- for (i = 0; i < n_tlhs; i += 2) {
- if (tlhs[i].kind == VARIABLE && tlhs[i].token.variable > IMAGINARY) {
+ for (i = 0; i < mathomatic->n_tlhs; i += 2) {
+ if (mathomatic->tlhs[i].kind == VARIABLE && mathomatic->tlhs[i].token.variable > IMAGINARY) {
found = true;
break;
}
}
- for (i = 0; i < n_trhs; i += 2) {
- if (trhs[i].kind == VARIABLE && trhs[i].token.variable > IMAGINARY) {
+ for (i = 0; i < mathomatic->n_trhs; i += 2) {
+ if (mathomatic->trhs[i].kind == VARIABLE && mathomatic->trhs[i].token.variable > IMAGINARY) {
found = true;
break;
}
}
if (found) {
- error(_("This equation is independent of the solve variable."));
+ error(mathomatic, _("This equation is independent of the solve variable."));
} else {
- error(_("There are no possible values for the solve variable."));
+ error(mathomatic, _("There are no possible values for the solve variable."));
}
return -2;
}
- zflag = (n_trhs == 1 && trhs[0].kind == CONSTANT && trhs[0].token.constant == 0.0);
+ zflag = (mathomatic->n_trhs == 1 && mathomatic->trhs[0].kind == CONSTANT && mathomatic->trhs[0].token.constant == 0.0);
if (zflag) {
/* overwrite -0.0 */
- trhs[0].token.constant = 0.0;
+ mathomatic->trhs[0].token.constant = 0.0;
}
if (need_flip >= found_count) {
- if (!flip(tlhs, &n_tlhs, trhs, &n_trhs))
+ if (!flip(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, mathomatic->trhs, &mathomatic->n_trhs))
return false;
- list_tdebug(2);
- simps_side(tlhs, &n_tlhs, zsolve);
- simps_side(trhs, &n_trhs, zsolve);
- list_tdebug(1);
+ list_tdebug(mathomatic, 2);
+ simps_side(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, zsolve);
+ simps_side(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, zsolve);
+ list_tdebug(mathomatic, 1);
goto left_again;
}
if (worked && !uf_flag) {
worked = false;
- debug_string(1, _("Unfactoring..."));
- partial_flag = false;
- uf_simp(tlhs, &n_tlhs);
- partial_flag = true;
- factorv(tlhs, &n_tlhs, v);
- list_tdebug(1);
+ debug_string(mathomatic, 1, _("Unfactoring..."));
+ mathomatic->partial_flag = false;
+ uf_simp(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ mathomatic->partial_flag = true;
+ factorv(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, v);
+ list_tdebug(mathomatic, 1);
uf_flag = true;
goto see_work;
}
if (uf_flag) {
- simps_side(tlhs, &n_tlhs, zsolve);
+ simps_side(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, zsolve);
uf_flag = false;
goto see_work;
}
op = 0;
- b1 = tlhs;
- for (i = 1; i < n_tlhs; i += 2) {
- if (tlhs[i].level == 1) {
- op_kind = tlhs[i].token.operatr;
+ b1 = mathomatic->tlhs;
+ for (i = 1; i < mathomatic->n_tlhs; i += 2) {
+ if (mathomatic->tlhs[i].level == 1) {
+ op_kind = mathomatic->tlhs[i].token.operatr;
if (op_kind == TIMES || op_kind == DIVIDE) {
if (op == 0) {
op = TIMES;
@@ -530,23 +526,23 @@ zero_simp:
}
if (zflag) {
if (op_kind == DIVIDE
- || (tlhs[i+1].kind == VARIABLE && tlhs[i+1].token.variable == v
- && (tlhs[i+1].level == 1
- || (tlhs[i+1].level == 2 && tlhs[i+2].token.operatr == POWER
- && tlhs[i+3].level == 2 && tlhs[i+3].kind == CONSTANT && tlhs[i+3].token.constant > 0.0)))) {
+ || (mathomatic->tlhs[i+1].kind == VARIABLE && mathomatic->tlhs[i+1].token.variable == v
+ && (mathomatic->tlhs[i+1].level == 1
+ || (mathomatic->tlhs[i+1].level == 2 && mathomatic->tlhs[i+2].token.operatr == POWER
+ && mathomatic->tlhs[i+3].level == 2 && mathomatic->tlhs[i+3].kind == CONSTANT && mathomatic->tlhs[i+3].token.constant > 0.0)))) {
op = op_kind;
- b1 = &tlhs[i+1];
+ b1 = &mathomatic->tlhs[i+1];
if (op_kind == DIVIDE)
break;
}
} else {
if (op_kind == DIVIDE) {
- for (j = i + 2; j < n_tlhs && tlhs[j].level > 1; j += 2) {
- if (tlhs[j].level == 2) {
- op_kind = tlhs[j].token.operatr;
+ for (j = i + 2; j < mathomatic->n_tlhs && mathomatic->tlhs[j].level > 1; j += 2) {
+ if (mathomatic->tlhs[j].level == 2) {
+ op_kind = mathomatic->tlhs[j].token.operatr;
if (op_kind == PLUS || op_kind == MINUS) {
op = DIVIDE;
- b1 = &tlhs[i+1];
+ b1 = &mathomatic->tlhs[i+1];
}
break;
}
@@ -564,36 +560,36 @@ zero_simp:
qtries = 0; /* might be quadratic after removing solution */
success = 2;
#if !SILENT
- fprintf(gfp, _("Removing possible solution: \""));
- list_proc(b1, 1, false);
- fprintf(gfp, " = 0\".\n");
+ fprintf(mathomatic->gfp, _("Removing possible solution: \""));
+ list_proc(mathomatic, b1, 1, false);
+ fprintf(mathomatic->gfp, " = 0\".\n");
#endif
} else {
- debug_string(1, _("Juggling..."));
+ debug_string(mathomatic, 1, _("Juggling..."));
uf_flag = true;
}
- if (!g_of_f(op, b1, tlhs, &n_tlhs, trhs, &n_trhs))
+ if (!g_of_f(mathomatic, op, b1, mathomatic->tlhs, &mathomatic->n_tlhs, mathomatic->trhs, &mathomatic->n_trhs))
return false;
goto simp_again;
}
b1 = NULL;
- for (i = 1; i < n_tlhs; i += 2) {
- if (tlhs[i].token.operatr == POWER
- && tlhs[i+1].level == tlhs[i].level
- && tlhs[i+1].kind == CONSTANT
- && fabs(tlhs[i+1].token.constant) < 1.0) {
- if (!f_to_fraction(tlhs[i+1].token.constant, &numerator, &denominator)
+ for (i = 1; i < mathomatic->n_tlhs; i += 2) {
+ if (mathomatic->tlhs[i].token.operatr == POWER
+ && mathomatic->tlhs[i+1].level == mathomatic->tlhs[i].level
+ && mathomatic->tlhs[i+1].kind == CONSTANT
+ && fabs(mathomatic->tlhs[i+1].token.constant) < 1.0) {
+ if (!f_to_fraction(mathomatic, mathomatic->tlhs[i+1].token.constant, &numerator, &denominator)
|| fabs(numerator) != 1.0 || denominator < 2.0) {
continue;
}
- for (j = i - 1; j >= 0 && tlhs[j].level >= tlhs[i].level; j--) {
- if (tlhs[j].kind == VARIABLE && tlhs[j].token.variable == v) {
+ for (j = i - 1; j >= 0 && mathomatic->tlhs[j].level >= mathomatic->tlhs[i].level; j--) {
+ if (mathomatic->tlhs[j].kind == VARIABLE && mathomatic->tlhs[j].token.variable == v) {
if (b1) {
- if (fabs(b1->token.constant) < fabs(tlhs[i+1].token.constant)) {
- b1 = &tlhs[i+1];
+ if (fabs(b1->token.constant) < fabs(mathomatic->tlhs[i+1].token.constant)) {
+ b1 = &mathomatic->tlhs[i+1];
}
} else {
- b1 = &tlhs[i+1];
+ b1 = &mathomatic->tlhs[i+1];
}
break;
}
@@ -606,7 +602,7 @@ zero_simp:
return false;
zero_solved = false;
qtries = 0;
- if (!increase(b1->token.constant, v)) {
+ if (!increase(mathomatic, b1->token.constant, v)) {
return false;
}
uf_flag = true;
@@ -615,17 +611,17 @@ zero_simp:
if (qtries) {
return false;
}
- *leftnp = n_tlhs;
- blt(leftp, tlhs, n_tlhs * sizeof(*leftp));
- *rightnp = n_trhs;
- blt(rightp, trhs, n_trhs * sizeof(*rightp));
- if (solve_sub(&zero_token, 1, leftp, leftnp, rightp, rightnp) <= 0)
+ *leftnp = mathomatic->n_tlhs;
+ blt(leftp, mathomatic->tlhs, mathomatic->n_tlhs * sizeof(*leftp));
+ *rightnp = mathomatic->n_trhs;
+ blt(rightp, mathomatic->trhs, mathomatic->n_trhs * sizeof(*rightp));
+ if (solve_sub(mathomatic, &mathomatic->zero_token, 1, leftp, leftnp, rightp, rightnp) <= 0)
return false;
if (zero_solved) {
qtries++;
}
zero_solved = true;
- if (poly_solve(v)) {
+ if (poly_solve(mathomatic, v)) {
goto left_again;
} else {
goto simp_again;
@@ -652,9 +648,7 @@ fin1:
* Return true if successful.
*/
static int
-increase(d, v)
-double d;
-long v;
+increase(MathoMatic* mathomatic, double d, long v)
{
int flag, foundp, found2;
int len1, len2;
@@ -663,25 +657,25 @@ long v;
token_type *ep;
#if !SILENT
- if (debug_level >= 0) {
- fprintf(gfp, _("Raising both equation sides to the power of %.*g and expanding...\n"), precision, 1.0 / d);
+ if (mathomatic->debug_level >= 0) {
+ fprintf(mathomatic->gfp, _("Raising both equation sides to the power of %.*g and expanding...\n"), mathomatic->precision, 1.0 / d);
}
#endif
- list_tdebug(2);
- partial_flag = false;
- ufactor(tlhs, &n_tlhs);
- partial_flag = true;
+ list_tdebug(mathomatic, 2);
+ mathomatic->partial_flag = false;
+ ufactor(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ mathomatic->partial_flag = true;
/* symb_flag = symblify; */
- simp_ssub(tlhs, &n_tlhs, v, d, true, false, 2);
- simp_ssub(tlhs, &n_tlhs, 0L, 1.0, true, true, 2);
+ simp_ssub(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, v, d, true, false, 2);
+ simp_ssub(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, 0L, 1.0, true, true, 2);
/* symb_flag = false; */
- list_tdebug(1);
+ list_tdebug(mathomatic, 1);
isolate:
- ep = &tlhs[n_tlhs];
+ ep = &mathomatic->tlhs[mathomatic->n_tlhs];
len2 = len1 = 0;
foundp = false;
- for (p1 = tlhs + 1;; p1 += 2) {
+ for (p1 = mathomatic->tlhs + 1;; p1 += 2) {
if (p1 >= ep) {
return 2; /* power not found */
}
@@ -701,7 +695,7 @@ isolate:
if (b1->kind == VARIABLE && b1->token.variable == v) {
flag = true;
}
- if (b1 == tlhs)
+ if (b1 == mathomatic->tlhs)
break;
}
if (flag || v == 0) {
@@ -728,7 +722,7 @@ isolate:
if (b1->kind == VARIABLE && b1->token.variable == v) {
flag = true;
}
- if (b1 == tlhs)
+ if (b1 == mathomatic->tlhs)
break;
}
if (flag || v == 0) {
@@ -744,13 +738,13 @@ isolate:
b1 = p1 + 1;
op = p1->token.operatr;
if (op == POWER && b1->level == 1 && b1->kind == CONSTANT && b1->token.constant == d) {
- return(g_of_f(POWER, b1, tlhs, &n_tlhs, trhs, &n_trhs));
+ return(g_of_f(mathomatic, POWER, b1, mathomatic->tlhs, &mathomatic->n_tlhs, mathomatic->trhs, &mathomatic->n_trhs));
}
if (!foundp) {
- b1 = tlhs;
+ b1 = mathomatic->tlhs;
if (p1 - b1 == 1 && p1->token.operatr == DIVIDE
&& b1->kind == CONSTANT && b1->token.constant == 1.0) {
- if (!flip(tlhs, &n_tlhs, trhs, &n_trhs))
+ if (!flip(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs, mathomatic->trhs, &mathomatic->n_trhs))
return false;
goto end;
}
@@ -768,13 +762,13 @@ isolate:
break;
}
}
- if (!g_of_f(op, b1, tlhs, &n_tlhs, trhs, &n_trhs))
+ if (!g_of_f(mathomatic, op, b1, mathomatic->tlhs, &mathomatic->n_tlhs, mathomatic->trhs, &mathomatic->n_trhs))
return false;
end:
- list_tdebug(2);
- simp_loop(tlhs, &n_tlhs);
- simp_loop(trhs, &n_trhs);
- list_tdebug(1);
+ list_tdebug(mathomatic, 2);
+ simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ simp_loop(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ list_tdebug(mathomatic, 1);
goto isolate;
}
@@ -789,8 +783,8 @@ end:
* Return true if successful, with solved equation in tlhs and trhs.
*/
static int
-poly_solve(v)
-long v; /* solve variable */
+poly_solve(MathoMatic* mathomatic, long v)
+//long v; /* solve variable */
{
int i, j, k;
token_type *p1, *p2, *ep;
@@ -804,22 +798,22 @@ long v; /* solve variable */
int len, alen, blen, aloc, nx1;
double high_power = 0.0;
- debug_string(1, _("Checking if equation is a polynomial equation:"));
+ debug_string(mathomatic, 1, _("Checking if equation is a polynomial equation:"));
#if DEBUG
- if (n_tlhs != 1 || tlhs[0].kind != CONSTANT || tlhs[0].token.constant != 0.0) {
- error_bug("poly_solve() called without a zero-solved equation!");
+ if (mathomatic->n_tlhs != 1 || mathomatic->tlhs[0].kind != CONSTANT || mathomatic->tlhs[0].token.constant != 0.0) {
+ error_bug(mathomatic, "poly_solve() called without a zero-solved equation!");
}
#endif
- uf_simp(trhs, &n_trhs);
- while (factor_plus(trhs, &n_trhs, v, 0.0)) {
- simp_loop(trhs, &n_trhs);
+ uf_simp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ while (factor_plus(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, v, 0.0)) {
+ simp_loop(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
}
- list_tdebug(1);
+ list_tdebug(mathomatic, 1);
found = false;
op = 0;
- ep = &trhs[n_trhs];
- for (x1tp = p1 = trhs;; p1++) {
+ ep = &mathomatic->trhs[mathomatic->n_trhs];
+ for (x1tp = p1 = mathomatic->trhs;; p1++) {
if (p1 >= ep || (p1->level == 1 && p1->kind == OPERATOR)) {
if (p1 < ep) {
switch (p1->token.operatr) {
@@ -891,12 +885,12 @@ long v; /* solve variable */
if (high_power == 0.0)
return false;
#if !SILENT
- if (debug_level >= 0) {
- list_var(v, 0);
- fprintf(gfp, _("Equation is a degree %.*g polynomial equation in %s.\n"), precision, high_power, var_str);
+ if (mathomatic->debug_level >= 0) {
+ list_var(mathomatic, v, 0);
+ fprintf(mathomatic->gfp, _("Equation is a degree %.*g polynomial equation in %s.\n"), mathomatic->precision, high_power, mathomatic->var_str);
}
#endif
- if (a1p > trhs && (a1p - 1)->token.operatr == MINUS)
+ if (a1p > mathomatic->trhs && (a1p - 1)->token.operatr == MINUS)
opx1 = MINUS;
else
opx1 = PLUS;
@@ -914,9 +908,9 @@ long v; /* solve variable */
}
opx2 = 0;
op = 0;
- for (x2p = p1 = trhs;; p1++) {
+ for (x2p = p1 = mathomatic->trhs;; p1++) {
if (p1 >= ep || (p1->level == 1 && p1->kind == OPERATOR)) {
- if (se_compare(x1_storage, nx1, x2p, p1 - x2p, &diff_sign)) {
+ if (se_compare(mathomatic, x1_storage, nx1, x2p, p1 - x2p, &diff_sign)) {
b1p = x2p;
b2p = p1;
b2ep = b2p;
@@ -927,7 +921,7 @@ long v; /* solve variable */
for (b1p = p2 = x2p;; p2++) {
if (p2 >= p1 || (p2->level == 2 && p2->kind == OPERATOR)) {
if (op2 == 0 || op2 == TIMES) {
- if (se_compare(x1_storage, nx1, x2p, p2 - x2p, &diff_sign)) {
+ if (se_compare(mathomatic, x1_storage, nx1, x2p, p2 - x2p, &diff_sign)) {
b2p = p2;
b2ep = p1;
goto big_bbreak;
@@ -972,76 +966,76 @@ big_bbreak:
default:
return false;
}
- blt(scratch, b1p, (char *) x2p - (char *) b1p);
+ blt(mathomatic->scratch, b1p, (char *) x2p - (char *) b1p);
len = x2p - b1p;
- scratch[len].level = 7;
- scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].level = 7;
+ mathomatic->scratch[len].kind = CONSTANT;
if (opx2 == MINUS)
- scratch[len].token.constant = -1.0;
+ mathomatic->scratch[len].token.constant = -1.0;
else
- scratch[len].token.constant = 1.0;
+ mathomatic->scratch[len].token.constant = 1.0;
len++;
- blt(&scratch[len], b2p, (char *) b2ep - (char *) b2p);
+ blt(&mathomatic->scratch[len], b2p, (char *) b2ep - (char *) b2p);
len += (b2ep - b2p);
blen = len;
- j = min_level(scratch, len);
+ j = min_level(mathomatic, mathomatic->scratch, len);
j = 7 - j;
for (i = 0; i < len; i++)
- scratch[i].level += j;
- scratch[len].level = 6;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = POWER;
+ mathomatic->scratch[i].level += j;
+ mathomatic->scratch[len].level = 6;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = POWER;
len++;
- scratch[len].level = 6;
- scratch[len].kind = CONSTANT;
- scratch[len].token.constant = 2.0;
+ mathomatic->scratch[len].level = 6;
+ mathomatic->scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].token.constant = 2.0;
len++;
- scratch[len].level = 5;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = MINUS;
+ mathomatic->scratch[len].level = 5;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = MINUS;
len++;
- scratch[len].level = 6;
- scratch[len].kind = CONSTANT;
- scratch[len].token.constant = 4.0;
+ mathomatic->scratch[len].level = 6;
+ mathomatic->scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].token.constant = 4.0;
len++;
- scratch[len].level = 6;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = 6;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
aloc = len;
- blt(&scratch[len], a1p, (char *) x1p - (char *) a1p);
+ blt(&mathomatic->scratch[len], a1p, (char *) x1p - (char *) a1p);
len += (x1p - a1p);
- scratch[len].level = 7;
- scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].level = 7;
+ mathomatic->scratch[len].kind = CONSTANT;
if (opx1 == MINUS)
- scratch[len].token.constant = -1.0;
+ mathomatic->scratch[len].token.constant = -1.0;
else
- scratch[len].token.constant = 1.0;
+ mathomatic->scratch[len].token.constant = 1.0;
len++;
- blt(&scratch[len], a2p, (char *) a2ep - (char *) a2p);
+ blt(&mathomatic->scratch[len], a2p, (char *) a2ep - (char *) a2p);
len += (a2ep - a2p);
alen = len - aloc;
- j = min_level(&scratch[aloc], len - aloc);
+ j = min_level(mathomatic, &mathomatic->scratch[aloc], len - aloc);
j = 7 - j;
for (i = aloc; i < len; i++)
- scratch[i].level += j;
- scratch[len].level = 6;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[i].level += j;
+ mathomatic->scratch[len].level = 6;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
k = len;
- scratch[len] = zero_token;
+ mathomatic->scratch[len] = mathomatic->zero_token;
len++;
- for (p2 = p1 = trhs;; p1++) {
+ for (p2 = p1 = mathomatic->trhs;; p1++) {
if (p1 >= ep || (p1->level == 1 && p1->kind == OPERATOR)) {
if (!((p2 <= x1p && p1 > x1p) || (p2 <= x2p && p1 > x2p))) {
- if (p2 == trhs) {
- scratch[len].level = 1;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = PLUS;
+ if (p2 == mathomatic->trhs) {
+ mathomatic->scratch[len].level = 1;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = PLUS;
len++;
}
- blt(&scratch[len], p2, (char *) p1 - (char *) p2);
+ blt(&mathomatic->scratch[len], p2, (char *) p1 - (char *) p2);
len += (p1 - p2);
}
if (p1 >= ep)
@@ -1051,59 +1045,59 @@ big_bbreak:
}
}
for (i = k; i < len; i++)
- scratch[i].level += 6;
- scratch[len].level = 4;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = POWER;
+ mathomatic->scratch[i].level += 6;
+ mathomatic->scratch[len].level = 4;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = POWER;
len++;
- scratch[len].level = 4;
- scratch[len].kind = CONSTANT;
- scratch[len].token.constant = 0.5;
+ mathomatic->scratch[len].level = 4;
+ mathomatic->scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].token.constant = 0.5;
len++;
- scratch[len].level = 3;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = 3;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- scratch[len].level = 3;
- scratch[len].kind = VARIABLE;
- next_sign(&scratch[len].token.variable);
+ mathomatic->scratch[len].level = 3;
+ mathomatic->scratch[len].kind = VARIABLE;
+ next_sign(mathomatic, &mathomatic->scratch[len].token.variable);
len++;
- scratch[len].level = 2;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = MINUS;
+ mathomatic->scratch[len].level = 2;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = MINUS;
len++;
- if (len + blen + 3 + alen > n_tokens) {
- error_huge();
+ if (len + blen + 3 + alen > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
- blt(&scratch[len], scratch, blen * sizeof(token_type));
+ blt(&mathomatic->scratch[len], mathomatic->scratch, blen * sizeof(token_type));
len += blen;
- scratch[len].level = 1;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = DIVIDE;
+ mathomatic->scratch[len].level = 1;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = DIVIDE;
len++;
- scratch[len].level = 2;
- scratch[len].kind = CONSTANT;
- scratch[len].token.constant = 2.0;
+ mathomatic->scratch[len].level = 2;
+ mathomatic->scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].token.constant = 2.0;
len++;
- scratch[len].level = 2;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = 2;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- blt(&scratch[len], &scratch[aloc], alen * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &mathomatic->scratch[aloc], alen * sizeof(token_type));
len += alen;
- if (found_var(scratch, len, v))
+ if (found_var(mathomatic->scratch, len, v))
return false;
- blt(tlhs, x1_storage, nx1 * sizeof(token_type));
- n_tlhs = nx1;
- simp_loop(tlhs, &n_tlhs);
- blt(trhs, scratch, len * sizeof(token_type));
- n_trhs = len;
- simp_loop(trhs, &n_trhs);
- list_tdebug(2);
- uf_tsimp(trhs, &n_trhs); /* don't unfactor result so much, just unfactor what will be unfactored anyway */
- simps_side(trhs, &n_trhs, false);
- list_tdebug(1);
- debug_string(0, _("Equation was solved with the quadratic formula."));
+ blt(mathomatic->tlhs, x1_storage, nx1 * sizeof(token_type));
+ mathomatic->n_tlhs = nx1;
+ simp_loop(mathomatic, mathomatic->tlhs, &mathomatic->n_tlhs);
+ blt(mathomatic->trhs, mathomatic->scratch, len * sizeof(token_type));
+ mathomatic->n_trhs = len;
+ simp_loop(mathomatic, mathomatic->trhs, &mathomatic->n_trhs);
+ list_tdebug(mathomatic, 2);
+ uf_tsimp(mathomatic, mathomatic->trhs, &mathomatic->n_trhs); /* don't unfactor result so much, just unfactor what will be unfactored anyway */
+ simps_side(mathomatic, mathomatic->trhs, &mathomatic->n_trhs, false);
+ list_tdebug(mathomatic, 1);
+ debug_string(mathomatic, 0, _("Equation was solved with the quadratic formula."));
return true;
}
@@ -1129,13 +1123,13 @@ big_bbreak:
* Return true unless something is wrong.
*/
static int
-g_of_f(op, operandp, side1p, side1np, side2p, side2np)
-int op; /* current operator */
-token_type *operandp; /* operand pointer */
-token_type *side1p; /* equation side pointer */
-int *side1np; /* pointer to the length of "side1p" */
-token_type *side2p; /* equation side pointer */
-int *side2np; /* pointer to the length of "side2p" */
+g_of_f(MathoMatic* mathomatic, int op, token_type *operandp, token_type *side1p, int *side1np, token_type *side2p, int *side2np)
+//int op; /* current operator */
+//token_type *operandp; /* operand pointer */
+//token_type *side1p; /* equation side pointer */
+//int *side1np; /* pointer to the length of "side1p" */
+//token_type *side2p; /* equation side pointer */
+//int *side2np; /* pointer to the length of "side2p" */
{
token_type *p1, *p2, *ep;
int oldn, operandn;
@@ -1147,17 +1141,17 @@ int *side2np; /* pointer to the length of "side2p" */
oldn = *side1np;
ep = &side1p[oldn];
if (operandp < side1p || operandp >= ep) {
- error_bug("g_of_f() called with invalid operandp.");
+ error_bug(mathomatic, "g_of_f() called with invalid operandp.");
}
- if (*side1np == prev_n1 && *side2np == prev_n2) {
- if (++repeat_count >= 4) {
- debug_string(1, _("Infinite loop aborted in solve routine."));
+ if (*side1np == mathomatic->prev_n1 && *side2np == mathomatic->prev_n2) {
+ if (++mathomatic->repeat_count >= 4) {
+ debug_string(mathomatic, 1, _("Infinite loop aborted in solve routine."));
return false;
}
} else {
- prev_n1 = *side1np;
- prev_n2 = *side2np;
- repeat_count = 0;
+ mathomatic->prev_n1 = *side1np;
+ mathomatic->prev_n2 = *side2np;
+ mathomatic->repeat_count = 0;
}
switch (op) {
case PLUS:
@@ -1185,14 +1179,14 @@ int *side2np; /* pointer to the length of "side2p" */
}
operandn = p1 - operandp;
if (op == POWER && operandp == side1p) {
- if (!parse_complex(side2p, *side2np, &c1))
+ if (!parse_complex(mathomatic, side2p, *side2np, &c1))
return false;
- if (!parse_complex(operandp, operandn, &c2))
+ if (!parse_complex(mathomatic, operandp, operandn, &c2))
return false;
- debug_string(1, _("Taking logarithm of both equation sides:"));
+ debug_string(mathomatic, 1, _("Taking logarithm of both equation sides:"));
errno = 0;
c1 = complex_div(complex_log(c1), complex_log(c2));
- check_err();
+ check_err(mathomatic);
*side2np = 0;
side2p[*side2np].level = 1;
side2p[*side2np].kind = CONSTANT;
@@ -1220,68 +1214,68 @@ int *side2np; /* pointer to the length of "side2p" */
return true;
}
if (op == MODULUS) {
- if (get_constant(side2p, *side2np, &d1) && get_constant(operandp, operandn, &d2)) {
+ if (get_constant(mathomatic, side2p, *side2np, &d1) && get_constant(mathomatic, operandp, operandn, &d2)) {
if (fabs(d1) >= fabs(d2)) {
- error(_("There are no possible solutions."));
+ error(mathomatic, _("There are no possible solutions."));
return false;
}
}
}
#if !SILENT
- if (debug_level > 0) {
+ if (mathomatic->debug_level > 0) {
switch (op) {
case PLUS:
- fprintf(gfp, _("Subtracting"));
+ fprintf(mathomatic->gfp, _("Subtracting"));
break;
case MINUS:
- fprintf(gfp, _("Adding"));
+ fprintf(mathomatic->gfp, _("Adding"));
break;
case TIMES:
- fprintf(gfp, _("Dividing both sides of the equation by"));
+ fprintf(mathomatic->gfp, _("Dividing both sides of the equation by"));
break;
case DIVIDE:
- fprintf(gfp, _("Multiplying both sides of the equation by"));
+ fprintf(mathomatic->gfp, _("Multiplying both sides of the equation by"));
break;
case POWER:
- fprintf(gfp, _("Raising both sides of the equation to the power of"));
+ fprintf(mathomatic->gfp, _("Raising both sides of the equation to the power of"));
break;
case MODULUS:
- fprintf(gfp, _("Applying inverse modulus of"));
+ fprintf(mathomatic->gfp, _("Applying inverse modulus of"));
break;
}
if (op == POWER && operandn == 1 && operandp->kind == CONSTANT) {
- fprintf(gfp, " %.*g:\n", precision, 1.0 / operandp->token.constant);
+ fprintf(mathomatic->gfp, " %.*g:\n", mathomatic->precision, 1.0 / operandp->token.constant);
} else {
- fprintf(gfp, " \"");
+ fprintf(mathomatic->gfp, " \"");
if (op == POWER)
- fprintf(gfp, "1/(");
- list_proc(operandp, operandn, false);
+ fprintf(mathomatic->gfp, "1/(");
+ list_proc(mathomatic, operandp, operandn, false);
switch (op) {
case PLUS:
- fprintf(gfp, _("\" from both sides of the equation:\n"));
+ fprintf(mathomatic->gfp, _("\" from both sides of the equation:\n"));
break;
case MINUS:
case MODULUS:
- fprintf(gfp, _("\" to both sides of the equation:\n"));
+ fprintf(mathomatic->gfp, _("\" to both sides of the equation:\n"));
break;
case POWER:
- fprintf(gfp, ")");
+ fprintf(mathomatic->gfp, ")");
default:
- fprintf(gfp, "\":\n");
+ fprintf(mathomatic->gfp, "\":\n");
break;
}
}
}
#endif
- if (*side1np + operandn + 3 > n_tokens || *side2np + operandn + 5 > n_tokens) {
- error_huge();
+ if (*side1np + operandn + 3 > mathomatic->n_tokens || *side2np + operandn + 5 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
- if (min_level(side1p, oldn) <= 1) {
+ if (min_level(mathomatic, side1p, oldn) <= 1) {
for (p2 = side1p; p2 < ep; p2++)
p2->level++;
}
ep = &side2p[*side2np];
- if (min_level(side2p, *side2np) <= 1) {
+ if (min_level(mathomatic, side2p, *side2np) <= 1) {
for (p2 = side2p; p2 < ep; p2++)
p2->level++;
}
@@ -1294,12 +1288,12 @@ int *side2np; /* pointer to the length of "side2p" */
p2++;
p2->level = 2;
p2->kind = VARIABLE;
- snprintf(var_name_buf, sizeof(var_name_buf), "%s_any%.0d", V_INTEGER_PREFIX, last_int_var);
- if (parse_var(&p2->token.variable, var_name_buf) == NULL)
+ snprintf(var_name_buf, sizeof(var_name_buf), "%s_any%.0d", V_INTEGER_PREFIX, mathomatic->last_int_var);
+ if (parse_var(mathomatic, &p2->token.variable, var_name_buf) == NULL)
return false;
- last_int_var++;
- if (last_int_var < 0) {
- last_int_var = 0;
+ mathomatic->last_int_var++;
+ if (mathomatic->last_int_var < 0) {
+ mathomatic->last_int_var = 0;
}
p2++;
p2->level = 2;
@@ -1361,7 +1355,7 @@ int *side2np; /* pointer to the length of "side2p" */
blt(&side2p[*side2np], &side1p[oldn], (*side1np - oldn) * sizeof(*side1p));
*side2np += *side1np - oldn;
if (op == POWER && operandn == 1 && operandp->kind == CONSTANT) {
- f_to_fraction(operandp->token.constant, &numerator, &denominator);
+ f_to_fraction(mathomatic, operandp->token.constant, &numerator, &denominator);
if (always_positive(numerator)) {
ep = &side2p[*side2np];
for (p2 = side2p; p2 < ep; p2++)
@@ -1372,7 +1366,7 @@ int *side2np; /* pointer to the length of "side2p" */
p2++;
p2->level = 1;
p2->kind = VARIABLE;
- next_sign(&p2->token.variable);
+ next_sign(mathomatic, &p2->token.variable);
*side2np += 2;
}
}
@@ -1388,17 +1382,17 @@ int *side2np; /* pointer to the length of "side2p" */
* Return true if successful.
*/
static int
-flip(side1p, side1np, side2p, side2np)
-token_type *side1p; /* equation side pointer */
-int *side1np; /* pointer to equation side length */
-token_type *side2p;
-int *side2np;
+flip(MathoMatic* mathomatic, token_type *side1p, int *side1np, token_type *side2p, int *side2np)
+//token_type *side1p; /* equation side pointer */
+//int *side1np; /* pointer to equation side length */
+//token_type *side2p;
+//int *side2np;
{
token_type *p1, *ep;
- debug_string(1, _("Taking the reciprocal of both sides of the equation..."));
- if (*side1np + 2 > n_tokens || *side2np + 2 > n_tokens) {
- error_huge();
+ debug_string(mathomatic, 1, _("Taking the reciprocal of both sides of the equation..."));
+ if (*side1np + 2 > mathomatic->n_tokens || *side2np + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
ep = &side1p[*side1np];
for (p1 = side1p; p1 < ep; p1++)
@@ -1411,13 +1405,13 @@ int *side2np;
blt(side2p + 2, side2p, *side2np * sizeof(*side2p));
*side2np += 2;
- *side1p = one_token;
+ *side1p = mathomatic->one_token;
side1p++;
side1p->level = 1;
side1p->kind = OPERATOR;
side1p->token.operatr = DIVIDE;
- *side2p = one_token;
+ *side2p = mathomatic->one_token;
side2p++;
side2p->level = 1;
side2p->kind = OPERATOR;
diff --git a/super.c b/super.c
index cc7ff87..d690fc2 100644
--- a/super.c
+++ b/super.c
@@ -24,15 +24,15 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
#include "includes.h"
-static int sf_recurse(token_type *equation, int *np, int loc, int level, int start_flag);
-static int sf_sub(token_type *equation, int *np, int loc, int i1, int n1, int i2, int n2, int level, int start_flag);
+static int sf_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int start_flag);
+static int sf_sub(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int i1, int n1, int i2, int n2, int level, int start_flag);
static void
-group_recurse(equation, np, loc, level)
-token_type *equation; /* equation side pointer */
-int *np; /* pointer to length of equation side */
-int loc; /* starting location within equation side */
-int level; /* current level of parentheses within the sub-expression starting at "loc" */
+group_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level)
+//token_type *equation; /* equation side pointer */
+//int *np; /* pointer to length of equation side */
+//int loc; /* starting location within equation side */
+//int level; /* current level of parentheses within the sub-expression starting at "loc" */
{
int i;
int len;
@@ -42,7 +42,7 @@ int level; /* current level of parentheses within the sub-expression starting
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
- group_recurse(equation, np, i, level + 1);
+ group_recurse(mathomatic, equation, np, i, level + 1);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
@@ -69,9 +69,9 @@ int level; /* current level of parentheses within the sub-expression starting
edi = i;
continue;
}
- blt(scratch, &equation[i], len * sizeof(token_type));
+ blt(mathomatic->scratch, &equation[i], len * sizeof(token_type));
blt(&equation[di+len], &equation[di], (i - di) * sizeof(token_type));
- blt(&equation[di], scratch, len * sizeof(token_type));
+ blt(&equation[di], mathomatic->scratch, len * sizeof(token_type));
edi += len;
i += len - 2;
} else {
@@ -85,7 +85,7 @@ int level; /* current level of parentheses within the sub-expression starting
if (equation[i].level == level && equation[i].kind == OPERATOR) {
#if DEBUG
if (equation[i].token.operatr != DIVIDE) {
- error_bug("Bug in group_recurse().");
+ error_bug(mathomatic, "Bug in group_recurse().");
}
#endif
equation[i].token.operatr = TIMES;
@@ -101,11 +101,11 @@ int level; /* current level of parentheses within the sub-expression starting
* Not guaranteed to put the grouped divisors last, reorder() puts divisors last.
*/
void
-group_proc(equation, np)
-token_type *equation; /* equation side pointer */
-int *np; /* pointer to length of equation side */
+group_proc(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* equation side pointer */
+//int *np; /* pointer to length of equation side */
{
- group_recurse(equation, np, 0, 1);
+ group_recurse(mathomatic, equation, np, 0, 1);
}
/*
@@ -117,17 +117,17 @@ int *np; /* pointer to length of equation side */
* Return true if any fractions were created.
*/
int
-fractions_and_group(equation, np)
-token_type *equation;
-int *np;
+fractions_and_group(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation;
+//int *np;
{
int rv = false;
- elim_loop(equation, np);
- if (fractions_display) {
- rv = make_fractions(equation, np);
+ elim_loop(mathomatic, equation, np);
+ if (mathomatic->fractions_display) {
+ rv = make_fractions(mathomatic, equation, np);
}
- group_proc(equation, np);
+ group_proc(mathomatic, equation, np);
return rv;
}
@@ -138,17 +138,17 @@ int *np;
* Return true if any fractions were created.
*/
int
-make_fractions_and_group(n)
-int n; /* equation space number */
+make_fractions_and_group(MathoMatic* mathomatic, int n)
+//int n; /* equation space number */
{
int rv = false;
- if (empty_equation_space(n))
+ if (empty_equation_space(mathomatic, n))
return false;
- if (fractions_and_group(lhs[n], &n_lhs[n]))
+ if (fractions_and_group(mathomatic, mathomatic->lhs[n], &mathomatic->n_lhs[n]))
rv = true;
- if (n_rhs[n] > 0) {
- if (fractions_and_group(rhs[n], &n_rhs[n]))
+ if (mathomatic->n_rhs[n] > 0) {
+ if (fractions_and_group(mathomatic, mathomatic->rhs[n], &mathomatic->n_rhs[n]))
rv = true;
}
return rv;
@@ -184,23 +184,21 @@ int n; /* equation space number */
* Return true if the equation side was modified.
*/
int
-super_factor(equation, np, start_flag)
-token_type *equation; /* pointer to the beginning of the equation side to process */
-int *np; /* pointer to the length of the equation side */
-int start_flag;
+super_factor(MathoMatic* mathomatic, token_type *equation, int *np, int start_flag)
+//token_type *equation; /* pointer to the beginning of the equation side to process */
+//int *np; /* pointer to the length of the equation side */
+//int start_flag;
{
int rv;
- group_proc(equation, np);
- rv = sf_recurse(equation, np, 0, 1, start_flag);
- organize(equation, np);
+ group_proc(mathomatic, equation, np);
+ rv = sf_recurse(mathomatic, equation, np, 0, 1, start_flag);
+ organize(mathomatic, equation, np);
return rv;
}
static int
-sf_recurse(equation, np, loc, level, start_flag)
-token_type *equation;
-int *np, loc, level, start_flag;
+sf_recurse(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int level, int start_flag)
{
int modified = false;
int i, j, k;
@@ -218,7 +216,7 @@ int *np, loc, level, start_flag;
op = 0;
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].level > level) {
- modified |= sf_recurse(equation, np, i, level + 1, start_flag);
+ modified |= sf_recurse(mathomatic, equation, np, i, level + 1, start_flag);
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
@@ -250,11 +248,11 @@ sf_again:
side_debug(0, &equation[i], len1);
side_debug(0, &equation[j], len2);
#endif
- if (sf_sub(equation, np, loc, i, len1, j, len2, level + 1, start_flag)) {
+ if (sf_sub(mathomatic, equation, np, loc, i, len1, j, len2, level + 1, start_flag)) {
#if 0
int junk;
printf("start_flag = %d\n", start_flag);
- debug_string(0, "super_factor() successful:");
+ debug_string(mathomatic, 0, "super_factor() successful:");
for (junk = 1; (loc + junk) < *np && equation[loc+junk].level > level; junk += 2)
;
side_debug(0, &equation[loc], junk);
@@ -267,9 +265,7 @@ sf_again:
}
static int
-sf_sub(equation, np, loc, i1, n1, i2, n2, level, start_flag)
-token_type *equation;
-int *np, loc, i1, n1, i2, n2, level, start_flag;
+sf_sub(MathoMatic* mathomatic, token_type *equation, int *np, int loc, int i1, int n1, int i2, int n2, int level, int start_flag)
{
int i, j, k;
int b1, b2;
@@ -339,30 +335,30 @@ int *np, loc, i1, n1, i2, n2, level, start_flag;
#endif
if (start_flag >= 2 && div_flag1 && div_flag2) {
#if DEBUG
- debug_string(1, "Trying to find a polynomial GCD between 2 denominators in sf_sub():");
- side_debug(1, &equation[b1], i - b1);
- side_debug(1, &equation[b2], j - b2);
+ debug_string(mathomatic, 1, "Trying to find a polynomial GCD between 2 denominators in sf_sub():");
+ side_debug(mathomatic, 1, &equation[b1], i - b1);
+ side_debug(mathomatic, 1, &equation[b2], j - b2);
#endif
- if ((rv = poly2_gcd(&equation[b1], i - b1, &equation[b2], j - b2, 0L, true)) > 0) {
- p1 = tlhs;
- np1 = n_tlhs;
- p2 = trhs;
- np2 = n_trhs;
+ if ((rv = poly2_gcd(mathomatic, &equation[b1], i - b1, &equation[b2], j - b2, 0L, true)) > 0) {
+ p1 = mathomatic->tlhs;
+ np1 = mathomatic->n_tlhs;
+ p2 = mathomatic->trhs;
+ np2 = mathomatic->n_trhs;
goto do_gcd_super;
}
- if (rv == 0 && poly2_gcd(&equation[b2], j - b2, &equation[b1], i - b1, 0L, true) > 0) {
- p1 = trhs;
- np1 = n_trhs;
- p2 = tlhs;
- np2 = n_tlhs;
+ if (rv == 0 && poly2_gcd(mathomatic, &equation[b2], j - b2, &equation[b1], i - b1, 0L, true) > 0) {
+ p1 = mathomatic->trhs;
+ np1 = mathomatic->n_trhs;
+ p2 = mathomatic->tlhs;
+ np2 = mathomatic->n_tlhs;
goto do_gcd_super;
}
#if DEBUG
- debug_string(1, "Done; polynomial GCD not found.");
+ debug_string(mathomatic, 1, "Done; polynomial GCD not found.");
#endif
}
- if (n1 + n2 + (i - b1) + (j - b2) + 8 > n_tokens) {
- error_huge();
+ if (n1 + n2 + (i - b1) + (j - b2) + 8 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
if (!div_flag1) {
for (k = i1; k < e1; k++)
@@ -373,76 +369,76 @@ int *np, loc, i1, n1, i2, n2, level, start_flag;
equation[k].level++;
}
len = (b1 - i1) - 1;
- blt(scratch, &equation[i1], len * sizeof(token_type));
+ blt(mathomatic->scratch, &equation[i1], len * sizeof(token_type));
if (op1 == MINUS) {
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- scratch[len].level = level;
- scratch[len].kind = CONSTANT;
- scratch[len].token.constant = -1.0;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].token.constant = -1.0;
len++;
}
if (div_flag1) {
- blt(&scratch[len], &equation[i], (e1 - i) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[i], (e1 - i) * sizeof(token_type));
len += e1 - i;
}
if (div_flag2) {
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- blt(&scratch[len], &equation[b2], (j - b2) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b2], (j - b2) * sizeof(token_type));
len += j - b2;
}
for (k = 0; k < len; k++)
- scratch[k].level += 2;
- scratch[len].level = level + 1;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = op2;
+ mathomatic->scratch[k].level += 2;
+ mathomatic->scratch[len].level = level + 1;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = op2;
len++;
k = len;
- blt(&scratch[len], &equation[i2], (b2 - i2 - 1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[i2], (b2 - i2 - 1) * sizeof(token_type));
len += b2 - i2 - 1;
if (div_flag2) {
- blt(&scratch[len], &equation[j], (e2 - j) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[j], (e2 - j) * sizeof(token_type));
len += e2 - j;
}
if (div_flag1) {
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- blt(&scratch[len], &equation[b1], (i - b1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b1], (i - b1) * sizeof(token_type));
len += i - b1;
}
for (; k < len; k++)
- scratch[k].level += 2;
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = DIVIDE;
+ mathomatic->scratch[k].level += 2;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = DIVIDE;
len++;
k = len;
if (div_flag1) {
- blt(&scratch[len], &equation[b1], (i - b1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b1], (i - b1) * sizeof(token_type));
len += i - b1;
}
if (div_flag1 && div_flag2) {
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
}
if (div_flag2) {
- blt(&scratch[len], &equation[b2], (j - b2) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b2], (j - b2) * sizeof(token_type));
len += j - b2;
}
for (; k < len; k++)
- scratch[k].level++;
+ mathomatic->scratch[k].level++;
end_mess:
- if (*np + len - n1 - (n2 + 1) > n_tokens) {
- error_huge();
+ if (*np + len - n1 - (n2 + 1) > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
if (op1 == MINUS) {
equation[i1-1].token.operatr = PLUS;
@@ -451,16 +447,16 @@ end_mess:
*np -= n2 + 1;
blt(&equation[i1+len], &equation[e1], (*np - e1) * sizeof(token_type));
*np += len - n1;
- blt(&equation[i1], scratch, len * sizeof(token_type));
+ blt(&equation[i1], mathomatic->scratch, len * sizeof(token_type));
return true;
do_gcd_super:
#if DEBUG
- debug_string(1, "Found a polynomial GCD between 2 denominators in sf_sub().");
+ debug_string(mathomatic, 1, "Found a polynomial GCD between 2 denominators in sf_sub().");
#endif
- if (5 - i1 + e1 + (2*np2) + b2 - i2 + e2 - j + np1 > n_tokens) {
- error_huge();
+ if (5 - i1 + e1 + (2*np2) + b2 - i2 + e2 - j + np1 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
for (k = 0; k < np1; k++) {
p1[k].level += level;
@@ -469,72 +465,72 @@ do_gcd_super:
p2[k].level += level;
}
len = (b1 - i1) - 1;
- blt(scratch, &equation[i1], len * sizeof(token_type));
+ blt(mathomatic->scratch, &equation[i1], len * sizeof(token_type));
if (op1 == MINUS) {
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- scratch[len].level = level;
- scratch[len].kind = CONSTANT;
- scratch[len].token.constant = -1.0;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = CONSTANT;
+ mathomatic->scratch[len].token.constant = -1.0;
len++;
}
/* if (div_flag1) { */
- blt(&scratch[len], &equation[i], (e1 - i) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[i], (e1 - i) * sizeof(token_type));
len += e1 - i;
/* } */
/* if (div_flag2) { */
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- blt(&scratch[len], p2, np2 * sizeof(token_type));
+ blt(&mathomatic->scratch[len], p2, np2 * sizeof(token_type));
len += np2;
/* } */
for (k = 0; k < len; k++)
- scratch[k].level += 2;
- scratch[len].level = level + 1;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = op2;
+ mathomatic->scratch[k].level += 2;
+ mathomatic->scratch[len].level = level + 1;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = op2;
len++;
k = len;
- blt(&scratch[len], &equation[i2], (b2 - i2 - 1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[i2], (b2 - i2 - 1) * sizeof(token_type));
len += b2 - i2 - 1;
/* if (div_flag2) { */
- blt(&scratch[len], &equation[j], (e2 - j) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[j], (e2 - j) * sizeof(token_type));
len += e2 - j;
/* } */
/* if (div_flag1) { */
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
- blt(&scratch[len], p1, np1 * sizeof(token_type));
+ blt(&mathomatic->scratch[len], p1, np1 * sizeof(token_type));
len += np1;
/* } */
for (; k < len; k++)
- scratch[k].level += 2;
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = DIVIDE;
+ mathomatic->scratch[k].level += 2;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = DIVIDE;
len++;
k = len;
/* if (div_flag1) { */
- blt(&scratch[len], &equation[b1], (i - b1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b1], (i - b1) * sizeof(token_type));
len += (i - b1);
/* } */
/* if (div_flag1 && div_flag2) { */
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].token.operatr = TIMES;
len++;
/* } */
/* if (div_flag2) { */
- blt(&scratch[len], p2, np2 * sizeof(token_type));
+ blt(&mathomatic->scratch[len], p2, np2 * sizeof(token_type));
len += np2;
/* } */
for (; k < len; k++)
- scratch[k].level++;
+ mathomatic->scratch[k].level++;
goto end_mess;
}
diff --git a/unfactor.c b/unfactor.c
index b572611..a9c8caa 100644
--- a/unfactor.c
+++ b/unfactor.c
@@ -24,7 +24,7 @@ George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
#include "includes.h"
-static int unf_sub(token_type *equation, int *np, int b1, int loc, int e1, int level, int ii);
+static int unf_sub(MathoMatic* mathomatic, token_type *equation, int *np, int b1, int loc, int e1, int level, int ii);
/*
* Unfactor times and divide only (products of sums) and simplify.
@@ -32,17 +32,15 @@ static int unf_sub(token_type *equation, int *np, int b1, int loc, int e1, int l
* Return true if equation side was unfactored.
*/
int
-uf_tsimp(equation, np)
-token_type *equation;
-int *np;
+uf_tsimp(MathoMatic* mathomatic, token_type *equation, int *np)
{
int rv;
- rv = uf_times(equation, np);
- simp_loop(equation, np);
- while (uf_times(equation, np)) {
+ rv = uf_times(mathomatic, equation, np);
+ simp_loop(mathomatic, equation, np);
+ while (uf_times(mathomatic, equation, np)) {
rv = true;
- simp_loop(equation, np);
+ simp_loop(mathomatic, equation, np);
}
return rv;
}
@@ -54,17 +52,15 @@ int *np;
* Return true if equation side was unfactored.
*/
int
-uf_power(equation, np)
-token_type *equation;
-int *np;
+uf_power(MathoMatic* mathomatic, token_type *equation, int *np)
{
int count = -1;
do {
- organize(equation, np);
+ organize(mathomatic, equation, np);
if (++count > 0)
break;
- } while (sub_ufactor(equation, np, 2));
+ } while (sub_ufactor(mathomatic, equation, np, 2));
return count;
}
@@ -75,17 +71,15 @@ int *np;
* Return true if equation side was unfactored.
*/
int
-uf_pplus(equation, np)
-token_type *equation;
-int *np;
+uf_pplus(MathoMatic* mathomatic, token_type *equation, int *np)
{
int count = -1;
do {
- organize(equation, np);
+ organize(mathomatic, equation, np);
if (++count > 0)
break;
- } while (sub_ufactor(equation, np, 4));
+ } while (sub_ufactor(mathomatic, equation, np, 4));
return count;
}
@@ -94,13 +88,11 @@ int *np;
* Same as a call to uf_pplus() and uf_power(), only faster.
*/
void
-uf_allpower(equation, np)
-token_type *equation;
-int *np;
+uf_allpower(MathoMatic* mathomatic, token_type *equation, int *np)
{
do {
- organize(equation, np);
- } while (sub_ufactor(equation, np, 0));
+ organize(mathomatic, equation, np);
+ } while (sub_ufactor(mathomatic, equation, np, 0));
}
/*
@@ -112,18 +104,16 @@ int *np;
* uf_times() is usually called after this to complete the expansion.
*/
void
-uf_repeat(equation, np)
-token_type *equation;
-int *np;
+uf_repeat(MathoMatic* mathomatic, token_type *equation, int *np)
{
int count = -1;
do {
- organize(equation, np);
+ organize(mathomatic, equation, np);
if (++count > 0)
break;
- } while (sub_ufactor(equation, np, 6));
- patch_root_div(equation, np);
+ } while (sub_ufactor(mathomatic, equation, np, 6));
+ patch_root_div(mathomatic, equation, np);
}
/*
@@ -132,31 +122,29 @@ int *np;
* Useful for removing all integer powers.
*/
void
-uf_repeat_always(equation, np)
-token_type *equation;
-int *np;
+uf_repeat_always(MathoMatic* mathomatic, token_type *equation, int *np)
{
int count = -1;
do {
- organize(equation, np);
+ organize(mathomatic, equation, np);
if (++count > 0)
break;
- } while (sub_ufactor(equation, np, 8));
+ } while (sub_ufactor(mathomatic, equation, np, 8));
}
/*
* Totally unfactor equation side and simplify.
*/
void
-uf_simp(equation, np)
-token_type *equation; /* pointer to beginning of equation side */
-int *np; /* pointer to length of equation side */
+uf_simp(MathoMatic* mathomatic, token_type *equation, int *np)
+//token_type *equation; /* pointer to beginning of equation side */
+//int *np; /* pointer to length of equation side */
{
- uf_tsimp(equation, np);
- uf_power(equation, np);
- uf_repeat(equation, np);
- uf_tsimp(equation, np);
+ uf_tsimp(mathomatic, equation, np);
+ uf_power(mathomatic, equation, np);
+ uf_repeat(mathomatic, equation, np);
+ uf_tsimp(mathomatic, equation, np);
}
/*
@@ -164,27 +152,23 @@ int *np; /* pointer to length of equation side */
* Don't call uf_repeat().
*/
void
-uf_simp_no_repeat(equation, np)
-token_type *equation;
-int *np;
+uf_simp_no_repeat(MathoMatic* mathomatic, token_type *equation, int *np)
{
- uf_power(equation, np);
- uf_tsimp(equation, np);
+ uf_power(mathomatic, equation, np);
+ uf_tsimp(mathomatic, equation, np);
}
/*
* Totally unfactor equation side with no simplification.
*/
int
-ufactor(equation, np)
-token_type *equation;
-int *np;
+ufactor(MathoMatic* mathomatic, token_type *equation, int *np)
{
int rv;
- uf_repeat(equation, np);
- rv = uf_times(equation, np);
- uf_allpower(equation, np);
+ uf_repeat(mathomatic, equation, np);
+ rv = uf_times(mathomatic, equation, np);
+ uf_allpower(mathomatic, equation, np);
return rv;
}
@@ -192,9 +176,7 @@ int *np;
* Increase the level of numerators by 2, so that the divide operator is not unfactored.
*/
static void
-no_divide(equation, np)
-token_type *equation;
-int *np;
+no_divide(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i, j;
int level;
@@ -219,26 +201,24 @@ int *np;
* Return true if equation side was unfactored.
*/
int
-uf_times(equation, np)
-token_type *equation;
-int *np;
+uf_times(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i;
int rv = false;
do {
- organize(equation, np);
- if (reorder(equation, np)) {
- organize(equation, np);
+ organize(mathomatic, equation, np);
+ if (reorder(mathomatic, equation, np)) {
+ organize(mathomatic, equation, np);
}
- group_proc(equation, np);
- if (partial_flag) {
- no_divide(equation, np);
+ group_proc(mathomatic, equation, np);
+ if (mathomatic->partial_flag) {
+ no_divide(mathomatic, equation, np);
}
- i = sub_ufactor(equation, np, 1);
+ i = sub_ufactor(mathomatic, equation, np, 1);
rv |= i;
} while (i);
- organize(equation, np);
+ organize(mathomatic, equation, np);
return rv;
}
@@ -250,10 +230,7 @@ int *np;
* Return true if equation side was modified.
*/
int
-sub_ufactor(equation, np, ii)
-token_type *equation;
-int *np;
-int ii;
+sub_ufactor(MathoMatic* mathomatic, token_type *equation, int *np, int ii)
{
int modified = false;
int i;
@@ -283,7 +260,7 @@ int ii;
if (equation[e1].level < level)
break;
}
- if (unf_sub(equation, np, b1, i, e1, level, ii)) {
+ if (unf_sub(mathomatic, equation, np, b1, i, e1, level, ii)) {
modified = true;
i = b1 - 1;
continue;
@@ -293,11 +270,7 @@ int ii;
}
static int
-unf_sub(equation, np, b1, loc, e1, level, ii)
-token_type *equation;
-int *np;
-int b1, loc, e1, level;
-int ii;
+unf_sub(MathoMatic* mathomatic, token_type *equation, int *np, int b1, int loc, int e1, int level, int ii)
{
int i, j, k;
int b2, eb1, be1;
@@ -332,24 +305,24 @@ int ii;
}
len = 0;
u_again:
- if (len + (eb1 - b1) + (i - b2) + (e1 - be1) + 1 > n_tokens) {
- error_huge();
+ if (len + (eb1 - b1) + (i - b2) + (e1 - be1) + 1 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
- blt(&scratch[len], &equation[b1], (eb1 - b1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b1], (eb1 - b1) * sizeof(token_type));
j = len;
len += (eb1 - b1);
for (; j < len; j++)
- scratch[j].level++;
- blt(&scratch[len], &equation[b2], (i - b2) * sizeof(token_type));
+ mathomatic->scratch[j].level++;
+ blt(&mathomatic->scratch[len], &equation[b2], (i - b2) * sizeof(token_type));
len += (i - b2);
- blt(&scratch[len], &equation[be1], (e1 - be1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[be1], (e1 - be1) * sizeof(token_type));
j = len;
len += (e1 - be1);
for (; j < len; j++)
- scratch[j].level++;
+ mathomatic->scratch[j].level++;
if (i < be1) {
- scratch[len] = equation[i];
- scratch[len].level--;
+ mathomatic->scratch[len] = equation[i];
+ mathomatic->scratch[len].level--;
len++;
b2 = i + 1;
for (i += 2; i < be1; i += 2) {
@@ -358,12 +331,12 @@ u_again:
}
goto u_again;
} else {
- if (*np - (e1 - b1) + len > n_tokens) {
- error_huge();
+ if (*np - (e1 - b1) + len > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[b1+len], &equation[e1], (*np - e1) * sizeof(token_type));
*np += len - (e1 - b1);
- blt(&equation[b1], scratch, len * sizeof(token_type));
+ blt(&equation[b1], mathomatic->scratch, len * sizeof(token_type));
return true;
}
}
@@ -396,19 +369,19 @@ u_again:
b2 = b1;
len = 0;
u1_again:
- if (len + (i - b2) + (e1 - loc) + 1 > n_tokens) {
- error_huge();
+ if (len + (i - b2) + (e1 - loc) + 1 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
- blt(&scratch[len], &equation[b2], (i - b2) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b2], (i - b2) * sizeof(token_type));
len += (i - b2);
- blt(&scratch[len], &equation[loc], (e1 - loc) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[loc], (e1 - loc) * sizeof(token_type));
j = len;
len += (e1 - loc);
for (; j < len; j++)
- scratch[j].level++;
+ mathomatic->scratch[j].level++;
if (i < loc) {
- scratch[len] = equation[i];
- scratch[len].level--;
+ mathomatic->scratch[len] = equation[i];
+ mathomatic->scratch[len].level--;
len++;
b2 = i + 1;
for (i += 2; i < loc; i += 2) {
@@ -417,12 +390,12 @@ u1_again:
}
goto u1_again;
} else {
- if (*np - (e1 - b1) + len > n_tokens) {
- error_huge();
+ if (*np - (e1 - b1) + len > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[b1+len], &equation[e1], (*np - e1) * sizeof(token_type));
*np += len - (e1 - b1);
- blt(&equation[b1], scratch, len * sizeof(token_type));
+ blt(&equation[b1], mathomatic->scratch, len * sizeof(token_type));
return true;
}
}
@@ -442,23 +415,23 @@ do_pplus:
b2 = loc + 1;
len = 0;
u2_again:
- if (len + (loc - b1) + (i - b2) + 2 > n_tokens) {
- error_huge();
+ if (len + (loc - b1) + (i - b2) + 2 > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
j = len;
- blt(&scratch[len], &equation[b1], (loc + 1 - b1) * sizeof(token_type));
+ blt(&mathomatic->scratch[len], &equation[b1], (loc + 1 - b1) * sizeof(token_type));
len += (loc + 1 - b1);
for (; j < len; j++)
- scratch[j].level++;
- blt(&scratch[len], &equation[b2], (i - b2) * sizeof(token_type));
+ mathomatic->scratch[j].level++;
+ blt(&mathomatic->scratch[len], &equation[b2], (i - b2) * sizeof(token_type));
len += (i - b2);
if (i < e1) {
- scratch[len].level = level;
- scratch[len].kind = OPERATOR;
+ mathomatic->scratch[len].level = level;
+ mathomatic->scratch[len].kind = OPERATOR;
if (equation[i].token.operatr == PLUS)
- scratch[len].token.operatr = TIMES;
+ mathomatic->scratch[len].token.operatr = TIMES;
else
- scratch[len].token.operatr = DIVIDE;
+ mathomatic->scratch[len].token.operatr = DIVIDE;
len++;
b2 = i + 1;
for (i += 2; i < e1; i += 2) {
@@ -467,12 +440,12 @@ u2_again:
}
goto u2_again;
} else {
- if (*np - (e1 - b1) + len > n_tokens) {
- error_huge();
+ if (*np - (e1 - b1) + len > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[b1+len], &equation[e1], (*np - e1) * sizeof(token_type));
*np += len - (e1 - b1);
- blt(&equation[b1], scratch, len * sizeof(token_type));
+ blt(&equation[b1], mathomatic->scratch, len * sizeof(token_type));
return true;
}
}
@@ -497,7 +470,7 @@ do_repeat:
}
d1 = ceil(d1) - 1.0;
d2 = d1 * ((i - b1) + 1.0);
- if ((*np + d2) > (n_tokens - 10)) {
+ if ((*np + d2) > (mathomatic->n_tokens - 10)) {
break; /* too big to expand, do nothing */
}
j = d1;
@@ -526,9 +499,7 @@ do_repeat:
}
static int
-usp_sub(equation, np, i)
-token_type *equation;
-int *np, i;
+usp_sub(MathoMatic* mathomatic, token_type *equation, int *np, int i)
{
int level;
int j;
@@ -544,8 +515,8 @@ int *np, i;
return false;
}
}
- if ((*np + 2) > n_tokens) {
- error_huge();
+ if ((*np + 2) > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
equation[j].token.operatr = TIMES;
for (j = i + 1;; j++) {
@@ -573,9 +544,7 @@ int *np, i;
* Return true if equation side is modified.
*/
int
-unsimp_power(equation, np)
-token_type *equation;
-int *np;
+unsimp_power(MathoMatic* mathomatic, token_type *equation, int *np)
{
int modified = false;
int i;
@@ -585,7 +554,7 @@ int *np;
if (equation[i].level == equation[i+1].level
&& equation[i+1].kind == CONSTANT)
continue;
- modified |= usp_sub(equation, np, i);
+ modified |= usp_sub(mathomatic, equation, np, i);
}
}
return modified;
@@ -599,9 +568,7 @@ int *np;
* Return true if equation side is modified.
*/
int
-unsimp2_power(equation, np)
-token_type *equation;
-int *np;
+unsimp2_power(token_type *equation, int *np)
{
int modified = false;
int i;
@@ -615,9 +582,7 @@ int *np;
}
int
-usp2_sub(equation, np, i)
-token_type *equation;
-int *np, i;
+usp2_sub(token_type *equation, int *np, int i)
{
int level;
int j, k;
@@ -636,8 +601,8 @@ int *np, i;
return false;
}
}
- if ((*np + 2) > n_tokens) {
- error_huge();
+ if ((*np + 2) > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
equation[j].token.operatr = TIMES;
j++;
@@ -664,9 +629,7 @@ int *np, i;
* attempted to be negated, possibly getting rid of unneeded times -1.
*/
void
-uf_neg_help(equation, np)
-token_type *equation;
-int *np;
+uf_neg_help(token_type *equation, int *np, MathoMatic* mathomatic)
{
int i;
int level;
@@ -678,8 +641,8 @@ int *np;
switch (equation[i+1].token.operatr) {
case TIMES:
case DIVIDE:
- if ((*np + 2) > n_tokens) {
- error_huge();
+ if ((*np + 2) > mathomatic->n_tokens) {
+ error_huge(mathomatic);
}
blt(&equation[i+3], &equation[i+1], (*np - (i + 1)) * sizeof(token_type));
*np += 2;
@@ -707,9 +670,7 @@ int *np;
* Return true if equation side was modified.
*/
int
-patch_root_div(equation, np)
-token_type *equation;
-int *np;
+patch_root_div(MathoMatic* mathomatic, token_type *equation, int *np)
{
int i;
int level;
@@ -724,14 +685,14 @@ int *np;
&& equation[i+3].level == level
&& equation[i+3].kind == CONSTANT) {
if (fmod(equation[i+1].token.constant, 1.0) == 0.0) {
- if (!rationalize_denominators
+ if (!mathomatic->rationalize_denominators
|| !isfinite(equation[i+3].token.constant)
|| equation[i+3].token.constant <= 0.0
|| equation[i+3].token.constant >= 1.0) {
continue;
}
- if (*np + 2 > n_tokens)
- error_huge();
+ if (*np + 2 > mathomatic->n_tokens)
+ error_huge(mathomatic);
equation[i+3].token.constant -= 1.0;
blt(&equation[i+2], &equation[i], (*np - i) * sizeof(token_type));
*np += 2;