1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
|
/*
* Mathomatic symbolic differentiation routines and related commands.
*
* Copyright (C) 1987-2010 George Gesslein II.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "includes.h"
static int d_recurse();
/*
* Compute the derivative of an equation side, with respect to variable "v",
* using the fast, rule-based transform method.
* This is done by recursively applying the proper rule of differentiation
* for each operator encountered.
*
* Return true if successful.
* The result must be simplified by the caller.
*/
int
differentiate(equation, np, v)
token_type *equation; /* pointer to source and destination equation side */
int *np; /* pointer to the length of the equation side */
long v; /* differentiation variable */
{
int i;
organize(equation, np);
/* First put every times and divide on a level by itself. */
for (i = 1; i < *np; i += 2) {
switch (equation[i].token.operatr) {
case TIMES:
case DIVIDE:
binary_parenthesize(equation, *np, i);
}
}
return d_recurse(equation, np, 0, 1, v);
}
/*
* Recursive differentiation routine.
*
* Symbolically differentiate expression in "equation"
* (which is a standard equation side) starting at "loc".
* The current level of parentheses is "level" and
* do the differentiation with respect to variable "v".
*
* Return true if successful.
* Return false if it is beyond this program's capabilities or an error was encountered.
*/
static int
d_recurse(equation, np, loc, level, v)
token_type *equation;
int *np, loc, level;
long v;
{
int i, j;
int n;
int op;
int oploc, endloc;
complexs c;
if (equation[loc].level < level) {
/* First differentiate if it is a single variable or constant. */
/* If it is the specified variable, change it to the constant 1, */
/* otherwise change it to the constant 0. */
if (equation[loc].kind == VARIABLE
&& ((v == MATCH_ANY && (equation[loc].token.variable & VAR_MASK) > SIGN)
|| equation[loc].token.variable == v)) {
equation[loc].kind = CONSTANT;
equation[loc].token.constant = 1.0;
} else {
equation[loc].kind = CONSTANT;
equation[loc].token.constant = 0.0;
}
return true;
}
for (op = 0, oploc = endloc = loc + 1; endloc < *np && equation[endloc].level >= level; endloc += 2) {
if (equation[endloc].level == level) {
switch (op) {
case 0:
case PLUS:
case MINUS:
break;
default:
/* Oops. More than one operator on the same level in this expression. */
error_bug("Internal error in d_recurse(): differentiating with unparenthesized operators is not allowed.");
return false;
}
op = equation[endloc].token.operatr;
oploc = endloc;
}
}
switch (op) {
case 0:
case PLUS:
case MINUS:
break;
case TIMES:
goto d_times;
case DIVIDE:
goto d_divide;
case POWER:
goto d_power;
default:
/* Differentiate an unsupported operator. */
/* This is possible if the expression doesn't contain the specified variable. */
/* In that case, the expression is replaced with "0", otherwise return false. */
for (i = loc; i < endloc; i += 2) {
if (equation[i].kind == VARIABLE
&& ((v == MATCH_ANY && (equation[i].token.variable & VAR_MASK) > SIGN)
|| equation[i].token.variable == v)) {
return false;
}
}
blt(&equation[loc+1], &equation[endloc], (*np - endloc) * sizeof(token_type));
*np -= (endloc - (loc + 1));
equation[loc].level = level;
equation[loc].kind = CONSTANT;
equation[loc].token.constant = 0.0;
return true;
}
/* Differentiate PLUS and MINUS operators. */
/* Use addition rule: d(u+v) = d(u) + d(v), */
/* where "d()" is the derivative function */
/* and "u" and "v" are expressions. */
for (i = loc; i < *np && equation[i].level >= level;) {
if (equation[i].kind != OPERATOR) {
if (!d_recurse(equation, np, i, level + 1, v))
return false;
i++;
for (; i < *np && equation[i].level > level; i += 2)
;
continue;
}
i++;
}
return true;
d_times:
/* Differentiate TIMES operator. */
/* Use product rule: d(u*v) = u*d(v) + v*d(u). */
if (*np + 1 + (endloc - loc) > n_tokens) {
error_huge();
}
for (i = loc; i < endloc; i++)
equation[i].level++;
blt(&equation[endloc+1], &equation[loc], (*np - loc) * sizeof(token_type));
*np += 1 + (endloc - loc);
equation[endloc].level = level;
equation[endloc].kind = OPERATOR;
equation[endloc].token.operatr = PLUS;
if (!d_recurse(equation, np, endloc + (oploc - loc) + 2, level + 2, v))
return false;
return(d_recurse(equation, np, loc, level + 2, v));
d_divide:
/* Differentiate DIVIDE operator. */
/* Use quotient rule: d(u/v) = (v*d(u) - u*d(v))/v^2. */
if (*np + 3 + (endloc - loc) + (endloc - oploc) > n_tokens) {
error_huge();
}
for (i = loc; i < endloc; i++)
equation[i].level += 2;
equation[oploc].token.operatr = TIMES;
j = 1 + (endloc - loc);
blt(&equation[endloc+1], &equation[loc], (*np - loc) * sizeof(token_type));
*np += j;
equation[endloc].level = level + 1;
equation[endloc].kind = OPERATOR;
equation[endloc].token.operatr = MINUS;
j += endloc;
blt(&equation[j+2+(endloc-oploc)], &equation[j], (*np - j) * sizeof(token_type));
*np += 2 + (endloc - oploc);
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = DIVIDE;
blt(&equation[j+1], &equation[oploc+1], (endloc - (oploc + 1)) * sizeof(token_type));
j += endloc - oploc;
equation[j].level = level + 1;
equation[j].kind = OPERATOR;
equation[j].token.operatr = POWER;
j++;
equation[j].level = level + 1;
equation[j].kind = CONSTANT;
equation[j].token.constant = 2.0;
if (!d_recurse(equation, np, endloc + (oploc - loc) + 2, level + 3, v))
return false;
return(d_recurse(equation, np, loc, level + 3, v));
d_power:
/* Differentiate POWER operator. */
/* Since we don't have symbolic logarithms, do all we can without them. */
for (i = oploc; i < endloc; i++) {
if (equation[i].kind == VARIABLE
&& ((v == MATCH_ANY && (equation[i].token.variable & VAR_MASK) > SIGN)
|| equation[i].token.variable == v)) {
if (parse_complex(&equation[loc], oploc - loc, &c)) {
c = complex_log(c);
n = (endloc - oploc) + 6;
if (*np + n > n_tokens) {
error_huge();
}
blt(&equation[endloc+n], &equation[endloc], (*np - endloc) * sizeof(token_type));
*np += n;
n = endloc;
equation[n].level = level;
equation[n].kind = OPERATOR;
equation[n].token.operatr = TIMES;
n++;
equation[n].level = level + 1;
equation[n].kind = CONSTANT;
equation[n].token.constant = c.re;
n++;
equation[n].level = level + 1;
equation[n].kind = OPERATOR;
equation[n].token.operatr = PLUS;
n++;
equation[n].level = level + 2;
equation[n].kind = CONSTANT;
equation[n].token.constant = c.im;
n++;
equation[n].level = level + 2;
equation[n].kind = OPERATOR;
equation[n].token.operatr = TIMES;
n++;
equation[n].level = level + 2;
equation[n].kind = VARIABLE;
equation[n].token.variable = IMAGINARY;
n++;
equation[n].level = level;
equation[n].kind = OPERATOR;
equation[n].token.operatr = TIMES;
n++;
blt(&equation[n], &equation[oploc+1], (endloc - (oploc + 1)) * sizeof(token_type));
for (i = loc; i < endloc; i++) {
equation[i].level++;
}
return(d_recurse(equation, np, n, level + 1, v));
}
return false;
}
}
blt(scratch, &equation[oploc+1], (endloc - (oploc + 1)) * sizeof(token_type));
n = endloc - (oploc + 1);
scratch[n].level = level;
scratch[n].kind = OPERATOR;
scratch[n].token.operatr = TIMES;
n++;
if (n + (endloc - loc) + 2 > n_tokens) {
error_huge();
}
blt(&scratch[n], &equation[loc], (endloc - loc) * sizeof(token_type));
i = n;
n += oploc + 1 - loc;
for (; i < n; i++)
scratch[i].level++;
n += endloc - (oploc + 1);
for (; i < n; i++)
scratch[i].level += 2;
scratch[n].level = level + 2;
scratch[n].kind = OPERATOR;
scratch[n].token.operatr = MINUS;
n++;
scratch[n].level = level + 2;
scratch[n].kind = CONSTANT;
scratch[n].token.constant = 1.0;
n++;
if (n + (oploc - loc) + 1 > n_tokens) {
error_huge();
}
scratch[n].level = level;
scratch[n].kind = OPERATOR;
scratch[n].token.operatr = TIMES;
n++;
j = n;
blt(&scratch[n], &equation[loc], (oploc - loc) * sizeof(token_type));
n += oploc - loc;
if (*np - (endloc - loc) + n > n_tokens) {
error_huge();
}
blt(&equation[loc+n], &equation[endloc], (*np - endloc) * sizeof(token_type));
*np += loc + n - endloc;
blt(&equation[loc], scratch, n * sizeof(token_type));
return(d_recurse(equation, np, loc + j, level + 1, v));
}
/*
* The derivative command.
*/
int
derivative_cmd(cp)
char *cp;
{
int i, len;
long v = 0; /* Mathomatic variable */
long l1, order = 1;
token_type *source, *dest;
int n1, *nps, *np;
int simplify_flag = true, solved;
if (current_not_defined()) {
return false;
}
solved = solved_equation(cur_equation);
if (strcmp_tospace(cp, "nosimplify") == 0) {
simplify_flag = false;
cp = skip_param(cp);
}
i = next_espace();
if (n_rhs[cur_equation]) {
if (!solved) {
warning(_("Not a solved equation. Only the RHS will be differentiated."));
}
source = rhs[cur_equation];
nps = &n_rhs[cur_equation];
dest = rhs[i];
np = &n_rhs[i];
} else {
source = lhs[cur_equation];
nps = &n_lhs[cur_equation];
dest = lhs[i];
np = &n_lhs[i];
}
/* parse the command line or prompt: */
if (*cp) {
if (is_all(cp)) {
cp = skip_param(cp);
v = MATCH_ANY;
} else {
if (isvarchar(*cp)) {
cp = parse_var2(&v, cp);
if (cp == NULL) {
return false;
}
}
}
if (*cp) {
order = decstrtol(cp, &cp);
}
if (*cp || order <= 0) {
error(_("The order must be a positive integer."));
return false;
}
}
if (no_vars(source, *nps, &v)) {
#if !SILENT
error(_("Current expression contains no variables; the derivative would be zero."));
return false;
#endif
}
if (v == 0) {
if (!prompt_var(&v)) {
return false;
}
}
#if !SILENT
if (v != MATCH_ANY && !found_var(source, *nps, v)) {
error(_("Variable not found; the derivative would be zero."));
return false;
}
if (debug_level >= 0) {
list_var(v, 0);
if (n_rhs[cur_equation]) {
printf(_("Differentiating the RHS with respect to (%s)"), var_str);
} else {
printf(_("Differentiating with respect to (%s)"), var_str);
}
if (order != 1) {
printf(_(" %ld times"), order);
}
if (simplify_flag) {
printf(_(" and simplifying...\n"));
} else {
printf("...\n");
}
}
#endif
blt(dest, source, *nps * sizeof(token_type));
n1 = *nps;
/* do the actual differentiating and simplifying: */
for (l1 = 0; l1 < order; l1++) {
if (!differentiate(dest, &n1, v)) {
error(_("Differentiation failed."));
return false;
}
if (simplify_flag) {
simpa_side(dest, &n1, true, false);
} else {
elim_loop(dest, &n1);
}
}
*np = n1;
if (n_rhs[cur_equation]) {
blt(lhs[i], lhs[cur_equation], n_lhs[cur_equation] * sizeof(token_type));
n_lhs[i] = n_lhs[cur_equation];
if (solved && isvarchar('\'')) {
len = list_var(lhs[i][0].token.variable, 0);
for (l1 = 0; l1 < order && len < (MAX_VAR_LEN - 1); l1++) {
var_str[len++] = '\'';
}
var_str[len] = '\0';
parse_var(&lhs[i][0].token.variable, var_str);
}
}
cur_equation = i;
return return_result(cur_equation);
}
/*
* The extrema command.
*/
int
extrema_cmd(cp)
char *cp;
{
int i;
long v = 0; /* Mathomatic variable */
long l1, order = 1;
token_type want;
token_type *source;
int n;
if (current_not_defined()) {
return false;
}
i = next_espace();
if (n_rhs[cur_equation]) {
if (!solved_equation(cur_equation)) {
error(_("The current equation is not solved for a variable."));
return false;
}
source = rhs[cur_equation];
n = n_rhs[cur_equation];
} else {
source = lhs[cur_equation];
n = n_lhs[cur_equation];
}
if (*cp) {
if (isvarchar(*cp)) {
cp = parse_var2(&v, cp);
if (cp == NULL) {
return false;
}
}
if (*cp) {
order = decstrtol(cp, &cp);
}
if (*cp || order <= 0) {
error(_("The order must be a positive integer."));
return false;
}
}
if (no_vars(source, n, &v)) {
error(_("Current expression contains no variables."));
return false;
}
if (v == 0) {
if (!prompt_var(&v)) {
return false;
}
}
if (!found_var(source, n, v)) {
error(_("Variable not found; the derivative would be zero."));
return false;
}
blt(rhs[i], source, n * sizeof(token_type));
/* take derivatives with respect to the specified variable and simplify: */
for (l1 = 0; l1 < order; l1++) {
if (!differentiate(rhs[i], &n, v)) {
error(_("Differentiation failed."));
return false;
}
simpa_side(rhs[i], &n, true, false);
}
if (!found_var(rhs[i], n, v)) {
error(_("There are no solutions."));
return false;
}
n_rhs[i] = n;
/* set equal to zero: */
n_lhs[i] = 1;
lhs[i][0] = zero_token;
cur_equation = i;
/* lastly, solve for the specified variable and simplify: */
want.level = 1;
want.kind = VARIABLE;
want.token.variable = v;
if (solve_sub(&want, 1, lhs[i], &n_lhs[i], rhs[i], &n_rhs[i]) <= 0) {
error(_("Solve failed."));
return false;
}
simpa_side(rhs[i], &n_rhs[i], false, false);
return return_result(cur_equation);
}
/*
* The taylor command.
*/
int
taylor_cmd(cp)
char *cp;
{
long v = 0; /* Mathomatic variable */
int i, j, k, i1;
int level;
long l1, n, order = -1L;
double d;
char *cp_start, *cp1, buf[MAX_CMD_LEN];
int our;
int our_nlhs, our_nrhs;
token_type *ep, *source, *dest;
int n1, *nps, *np;
int simplify_flag = true;
cp_start = cp;
if (current_not_defined()) {
return false;
}
if (strcmp_tospace(cp, "nosimplify") == 0) {
simplify_flag = false;
cp = skip_param(cp);
}
i = next_espace();
blt(lhs[i], lhs[cur_equation], n_lhs[cur_equation] * sizeof(token_type));
n_lhs[i] = n_lhs[cur_equation];
n_rhs[i] = 0;
our = alloc_next_espace();
n_lhs[i] = 0;
if (our < 0) {
error(_("Out of free equation spaces."));
return false;
}
if (n_rhs[cur_equation]) {
source = rhs[cur_equation];
nps = &n_rhs[cur_equation];
dest = rhs[i];
np = &n_rhs[i];
} else {
source = lhs[cur_equation];
nps = &n_lhs[cur_equation];
dest = lhs[i];
np = &n_lhs[i];
}
if (*cp && isvarchar(*cp)) {
cp = parse_var2(&v, cp);
if (cp == NULL) {
return false;
}
}
if (*cp) {
order = decstrtol(cp, &cp1);
if (cp1 != skip_param(cp) || order < 0) {
error(_("Positive integer required for order."));
return false;
}
cp = cp1;
}
no_vars(source, *nps, &v);
if (v == 0) {
if (!prompt_var(&v)) {
return false;
}
}
if (!found_var(source, *nps, v)) {
warning(_("Variable not found."));
}
blt(rhs[our], source, *nps * sizeof(token_type));
our_nrhs = *nps;
/* Simplify and take the first derivative: */
uf_simp(rhs[our], &our_nrhs);
if (!differentiate(rhs[our], &our_nrhs, v)) {
error(_("Differentiation failed."));
return false;
}
if (*cp) {
input_column += (cp - cp_start);
if ((cp = parse_expr(lhs[our], &our_nlhs, cp)) == NULL || our_nlhs <= 0) {
return false;
}
} else {
#if !SILENT
list_var(v, 0);
printf(_("Taylor series expansion around %s = point.\n"), var_str);
#endif
my_strlcpy(prompt_str, _("Enter point: "), sizeof(prompt_str));
if (!get_expr(lhs[our], &our_nlhs)) {
return false;
}
}
if (order < 0) {
my_strlcpy(prompt_str, _("Enter order (number of derivatives to take): "), sizeof(prompt_str));
if ((cp1 = get_string(buf, sizeof(buf))) == NULL)
return false;
if (*cp1) {
order = decstrtol(cp1, &cp);
if (*cp || order < 0) {
error(_("Positive integer required for order."));
return false;
}
} else {
order = LONG_MAX - 1L;
#if !SILENT
printf(_("Derivatives will be taken until they reach zero...\n"));
#endif
}
}
#if !SILENT
printf(_("Computing the Taylor series"));
if (n_rhs[cur_equation]) {
printf(_(" of the RHS"));
}
if (simplify_flag) {
printf(_(" and simplifying"));
}
printf("...\n");
#endif
n = 0;
i1 = 0;
blt(dest, source, *nps * sizeof(token_type));
n1 = *nps;
loop_again:
for (k = i1; k < n1; k += 2) {
if (dest[k].kind == VARIABLE && dest[k].token.variable == v) {
level = dest[k].level;
if ((n1 + our_nlhs - 1) > n_tokens)
error_huge();
blt(&dest[k+our_nlhs], &dest[k+1], (n1 - (k + 1)) * sizeof(token_type));
n1 += our_nlhs - 1;
j = k;
blt(&dest[k], lhs[our], our_nlhs * sizeof(token_type));
k += our_nlhs;
for (; j < k; j++)
dest[j].level += level;
k--;
}
}
if ((n1 + our_nlhs + 7) > n_tokens)
error_huge();
for (k = i1; k < n1; k++)
dest[k].level++;
ep = &dest[n1];
ep->level = 1;
ep->kind = OPERATOR;
ep->token.operatr = TIMES;
ep++;
ep->level = 3;
ep->kind = VARIABLE;
ep->token.variable = v;
ep++;
ep->level = 3;
ep->kind = OPERATOR;
ep->token.operatr = MINUS;
n1 += 3;
j = n1;
blt(&dest[n1], lhs[our], our_nlhs * sizeof(token_type));
n1 += our_nlhs;
for (; j < n1; j++)
dest[j].level += 3;
ep = &dest[n1];
ep->level = 2;
ep->kind = OPERATOR;
ep->token.operatr = POWER;
ep++;
ep->level = 2;
ep->kind = CONSTANT;
ep->token.constant = n;
ep++;
ep->level = 1;
ep->kind = OPERATOR;
ep->token.operatr = DIVIDE;
ep++;
for (d = 1.0, l1 = 2; l1 <= n; l1++)
d *= l1;
ep->level = 1;
ep->kind = CONSTANT;
ep->token.constant = d;
n1 += 4;
for (; i1 < n1; i1++)
dest[i1].level++;
if (simplify_flag) {
uf_simp(dest, &n1);
}
side_debug(1, dest, n1);
if (exp_contains_infinity(dest, n1)) {
error(_("Result invalid because it contains infinity or NaN."));
return false;
}
if (n < order) {
if (n > 0) {
if (!differentiate(rhs[our], &our_nrhs, v)) {
error(_("Differentiation failed."));
return false;
}
}
symb_flag = symblify;
simpa_side(rhs[our], &our_nrhs, true, true);
symb_flag = false;
if (our_nrhs != 1 || rhs[our][0].kind != CONSTANT || rhs[our][0].token.constant != 0.0) {
i1 = n1;
if ((i1 + 1 + our_nrhs) > n_tokens)
error_huge();
for (j = 0; j < i1; j++)
dest[j].level++;
dest[i1].level = 1;
dest[i1].kind = OPERATOR;
dest[i1].token.operatr = PLUS;
i1++;
blt(&dest[i1], rhs[our], our_nrhs * sizeof(token_type));
n1 = i1 + our_nrhs;
n++;
goto loop_again;
}
}
#if !SILENT
printf(_("%ld derivative%s applied.\n"), n, (n == 1) ? "" : "s");
#endif
if (n_rhs[cur_equation]) {
n_lhs[i] = n_lhs[cur_equation];
}
*np = n1;
cur_equation = i;
return return_result(cur_equation);
}
/*
* The limit command.
*/
int
limit_cmd(cp)
char *cp;
{
int i;
long v = 0; /* Mathomatic variable */
token_type solved_v, want;
char *cp_start;
cp_start = cp;
if (current_not_defined()) {
return false;
}
i = next_espace();
if (n_rhs[cur_equation] == 0) {
/* make expression into an equation: */
blt(rhs[cur_equation], lhs[cur_equation], n_lhs[cur_equation] * sizeof(token_type));
n_rhs[cur_equation] = n_lhs[cur_equation];
n_lhs[cur_equation] = 1;
lhs[cur_equation][0].level = 1;
lhs[cur_equation][0].kind = VARIABLE;
parse_var(&lhs[cur_equation][0].token.variable, "answer");
}
if (!solved_equation(cur_equation)) {
error(_("The current equation is not solved for a variable."));
return false;
}
solved_v = lhs[cur_equation][0];
/* parse the command line or prompt: */
if (*cp) {
cp = parse_var2(&v, cp);
if (cp == NULL) {
return false;
}
}
if (no_vars(rhs[cur_equation], n_rhs[cur_equation], &v)) {
error(_("Current expression contains no variables."));
return false;
}
if (v == 0) {
if (!prompt_var(&v)) {
return false;
}
}
if (!found_var(rhs[cur_equation], n_rhs[cur_equation], v)) {
error(_("Variable not found."));
return false;
}
if (*cp == '=') {
cp = skip_space(cp + 1);
}
if (*cp) {
input_column += (cp - cp_start);
if ((cp = parse_expr(tes, &n_tes, cp)) == NULL || n_tes <= 0) {
return false;
}
} else {
list_var(v, 0);
snprintf(prompt_str, sizeof(prompt_str), _("as (%s) goes to: "), var_str);
if (!get_expr(tes, &n_tes)) {
return false;
}
}
/* copy the current equation to a new equation space, then simplify and work on the copy: */
copy_espace(cur_equation, i);
simpa_side(rhs[i], &n_rhs[i], false, false);
/* see if the limit expression is positive infinity: */
simp_loop(tes, &n_tes);
if (n_tes == 1 && tes[0].kind == CONSTANT && tes[0].token.constant == INFINITY) {
/* To take the limit to positive infinity, */
/* replace infinity with zero and replace the limit variable with its reciprocal: */
n_tes = 1;
tes[0] = zero_token;
tlhs[0] = one_token;
tlhs[1].level = 1;
tlhs[1].kind = OPERATOR;
tlhs[1].token.operatr = DIVIDE;
tlhs[2].level = 1;
tlhs[2].kind = VARIABLE;
tlhs[2].token.variable = v;
n_tlhs = 3;
subst_var_with_exp(rhs[i], &n_rhs[i], tlhs, n_tlhs, v);
}
/* General limit taking, solve for the limit variable: */
debug_string(0, "Solving...");
want.level = 1;
want.kind = VARIABLE;
want.token.variable = v;
if (solve_sub(&want, 1, lhs[i], &n_lhs[i], rhs[i], &n_rhs[i]) <= 0) {
error(_("Can't take the limit because solve failed."));
return false;
}
/* replace the limit variable (LHS) with the limit expression: */
blt(lhs[i], tes, n_tes * sizeof(token_type));
n_lhs[i] = n_tes;
/* simplify the RHS: */
symb_flag = symblify;
simpa_side(rhs[i], &n_rhs[i], false, false);
symb_flag = false;
if (exp_contains_nan(rhs[i], n_rhs[i])) {
error(_("Unable to take limit; result contains NaN (Not a Number)."));
return false;
}
/* solve back for the original variable: */
if (solve_sub(&solved_v, 1, lhs[i], &n_lhs[i], rhs[i], &n_rhs[i]) <= 0) {
error(_("Can't take the limit because solve failed."));
return false;
}
/* simplify before returning the result: */
simpa_side(rhs[i], &n_rhs[i], false, false);
if (exp_contains_nan(rhs[i], n_rhs[i])) {
error(_("Unable to take limit; result contains NaN (Not a Number)."));
return false;
}
return return_result(i);
}
|