# HG changeset patch
# User Robert Marik <marik@mendelu.cz>
# Date 1267399874 -3600
# Node ID a1e99a90b1993d275c2235595ef452fed01c955f
# Parent 46b1ee5997ac7e7faafdd86751fe7d121b6c1385
#8390 and reindent.py
diff -r 46b1ee5997ac -r a1e99a90b199 sage/symbolic/expression.pyx
a
|
b
|
|
154 | 154 | def is_SymbolicEquation(x): |
155 | 155 | """ |
156 | 156 | Returns True if *x* is a symbolic equation. |
157 | | |
| 157 | |
158 | 158 | EXAMPLES: |
159 | 159 | |
160 | 160 | The following two examples are symbolic equations:: |
161 | | |
| 161 | |
162 | 162 | sage: from sage.symbolic.expression import is_SymbolicEquation |
163 | 163 | sage: is_SymbolicEquation(sin(x) == x) |
164 | 164 | True |
… |
… |
|
166 | 166 | True |
167 | 167 | sage: is_SymbolicEquation(x) |
168 | 168 | False |
169 | | |
| 169 | |
170 | 170 | This is not, since ``2==3`` evaluates to the boolean |
171 | 171 | ``False``:: |
172 | | |
| 172 | |
173 | 173 | sage: is_SymbolicEquation(2 == 3) |
174 | 174 | False |
175 | | |
| 175 | |
176 | 176 | However here since both 2 and 3 are coerced to be symbolic, we |
177 | 177 | obtain a symbolic equation:: |
178 | | |
| 178 | |
179 | 179 | sage: is_SymbolicEquation(SR(2) == SR(3)) |
180 | 180 | True |
181 | 181 | |
… |
… |
|
190 | 190 | value. Otherwise, a TypeError is raised. |
191 | 191 | |
192 | 192 | EXAMPLES:: |
193 | | |
| 193 | |
194 | 194 | sage: var('x') |
195 | 195 | x |
196 | 196 | sage: b = -17/3 |
… |
… |
|
210 | 210 | |
211 | 211 | def __init__(self, SR, x=0): |
212 | 212 | """ |
213 | | Nearly all expressions are created by calling new_Expression_from_*, |
214 | | but we need to make sure this at least doesn't leave self._gobj |
| 213 | Nearly all expressions are created by calling new_Expression_from_*, |
| 214 | but we need to make sure this at least doesn't leave self._gobj |
215 | 215 | uninitialized and segfault. |
216 | 216 | |
217 | 217 | TESTS:: |
… |
… |
|
250 | 250 | __setstate__(). |
251 | 251 | |
252 | 252 | In order to pickle Expression objects, we return a tuple containing |
253 | | |
| 253 | |
254 | 254 | * 0 - as pickle version number |
255 | 255 | in case we decide to change the pickle format in the feature |
256 | 256 | * names of symbols of this expression |
… |
… |
|
266 | 266 | (0, |
267 | 267 | ['x', 'y', 'z'], |
268 | 268 | ...) |
269 | | |
| 269 | |
270 | 270 | """ |
271 | 271 | cdef GArchive ar |
272 | 272 | ar.archive_ex(self._gobj, "sage_ex") |
… |
… |
|
318 | 318 | def _repr_(self, simplify=None): |
319 | 319 | """ |
320 | 320 | Return string representation of this symbolic expression. |
321 | | |
322 | | EXAMPLES:: |
323 | | |
| 321 | |
| 322 | EXAMPLES:: |
| 323 | |
324 | 324 | sage: var("x y") |
325 | 325 | (x, y) |
326 | 326 | sage: repr(x+y) |
327 | 327 | 'x + y' |
328 | 328 | |
329 | 329 | TESTS:: |
330 | | |
| 330 | |
331 | 331 | # printing of modular number equal to -1 as coefficient |
332 | 332 | sage: k.<a> = GF(9); k(2)*x |
333 | 333 | 2*x |
… |
… |
|
356 | 356 | -I |
357 | 357 | sage: y + 3*(x^(-1)) |
358 | 358 | y + 3/x |
359 | | |
| 359 | |
360 | 360 | Printing the exp function:: |
361 | | |
| 361 | |
362 | 362 | sage: x.parent(1).exp() |
363 | 363 | e |
364 | 364 | sage: x.exp() |
… |
… |
|
386 | 386 | (n + A/B)^(n + 1) |
387 | 387 | |
388 | 388 | Powers where the base or exponent is a Python object:: |
389 | | |
| 389 | |
390 | 390 | sage: (2/3)^x |
391 | 391 | (2/3)^x |
392 | 392 | sage: x^CDF(1,2) |
… |
… |
|
414 | 414 | |
415 | 415 | sage: f = sin(e + 2) |
416 | 416 | sage: f._interface_(sage.calculus.calculus.maxima) |
417 | | sin(%e+2) |
| 417 | sin(%e+2) |
418 | 418 | """ |
419 | 419 | if is_a_constant(self._gobj): |
420 | 420 | return self.pyobject()._interface_(I) |
… |
… |
|
455 | 455 | sage: f = pi + I*e |
456 | 456 | sage: f._pari_init_() |
457 | 457 | '(Pi)+((exp(1))*(I))' |
458 | | """ |
| 458 | """ |
459 | 459 | from sage.symbolic.expression_conversions import InterfaceInit |
460 | 460 | return InterfaceInit(I)(self) |
461 | | |
| 461 | |
462 | 462 | def _gap_init_(self): |
463 | 463 | """ |
464 | 464 | Conversion of symbolic object to GAP always results in a GAP |
465 | 465 | string. |
466 | | |
467 | | EXAMPLES:: |
468 | | |
| 466 | |
| 467 | EXAMPLES:: |
| 468 | |
469 | 469 | sage: gap(e + pi^2 + x^3) |
470 | 470 | pi^2 + x^3 + e |
471 | 471 | """ |
… |
… |
|
475 | 475 | """ |
476 | 476 | Conversion of a symbolic object to Singular always results in a |
477 | 477 | Singular string. |
478 | | |
479 | | EXAMPLES:: |
480 | | |
| 478 | |
| 479 | EXAMPLES:: |
| 480 | |
481 | 481 | sage: singular(e + pi^2 + x^3) |
482 | 482 | pi^2 + x^3 + e |
483 | 483 | """ |
… |
… |
|
486 | 486 | def _magma_init_(self, magma): |
487 | 487 | """ |
488 | 488 | Return string representation in Magma of this symbolic expression. |
489 | | |
| 489 | |
490 | 490 | Since Magma has no notation of symbolic calculus, this simply |
491 | 491 | returns something that evaluates in Magma to a a Magma string. |
492 | | |
493 | | EXAMPLES:: |
494 | | |
495 | | sage: x = var('x') |
| 492 | |
| 493 | EXAMPLES:: |
| 494 | |
| 495 | sage: x = var('x') |
496 | 496 | sage: f = sin(cos(x^2) + log(x)) |
497 | 497 | sage: f._magma_init_(magma) |
498 | 498 | '"sin(log(x) + cos(x^2))"' |
… |
… |
|
506 | 506 | def _latex_(self): |
507 | 507 | r""" |
508 | 508 | Return string representation of this symbolic expression. |
509 | | |
510 | | EXAMPLES: |
511 | | |
512 | | |
513 | | TESTS:: |
514 | | |
| 509 | |
| 510 | EXAMPLES: |
| 511 | |
| 512 | |
| 513 | TESTS:: |
| 514 | |
515 | 515 | sage: var('x,y,z') |
516 | 516 | (x, y, z) |
517 | 517 | sage: latex(y + 3*(x^(-1))) |
… |
… |
|
549 | 549 | {\left(A B\right)}^{n - 1} |
550 | 550 | |
551 | 551 | Powers where the base or exponent is a Python object:: |
552 | | |
| 552 | |
553 | 553 | sage: latex((2/3)^x) |
554 | 554 | \left(\frac{2}{3}\right)^{x} |
555 | 555 | sage: latex(x^CDF(1,2)) |
… |
… |
|
586 | 586 | def _mathml_(self): |
587 | 587 | """ |
588 | 588 | Returns a MathML representation of this object. |
589 | | |
| 589 | |
590 | 590 | EXAMPLES:: |
591 | 591 | |
592 | 592 | sage: mathml(pi) |
… |
… |
|
601 | 601 | except TypeError: |
602 | 602 | return mathml(repr(self)) |
603 | 603 | return mathml(obj) |
604 | | |
| 604 | |
605 | 605 | def _integer_(self, ZZ=None): |
606 | 606 | """ |
607 | 607 | EXAMPLES:: |
608 | | |
| 608 | |
609 | 609 | sage: f = x^3 + 17*x -3 |
610 | 610 | sage: ZZ(f.coeff(x^3)) |
611 | 611 | 1 |
… |
… |
|
617 | 617 | <type 'sage.rings.integer.Integer'> |
618 | 618 | |
619 | 619 | Coercion is done if necessary:: |
620 | | |
| 620 | |
621 | 621 | sage: f = x^3 + 17/1*x |
622 | 622 | sage: ZZ(f.coeff(x)) |
623 | 623 | 17 |
… |
… |
|
626 | 626 | |
627 | 627 | If the symbolic expression is just a wrapper around an integer, |
628 | 628 | that very same integer is returned:: |
629 | | |
| 629 | |
630 | 630 | sage: n = 17; SR(n)._integer_() is n |
631 | 631 | True |
632 | 632 | """ |
… |
… |
|
641 | 641 | def __int__(self): |
642 | 642 | """ |
643 | 643 | EXAMPLES:: |
644 | | |
| 644 | |
645 | 645 | sage: int(sin(2)*100) |
646 | 646 | 90 |
647 | 647 | sage: int(log(8)/log(2)) |
… |
… |
|
653 | 653 | def __long__(self): |
654 | 654 | """ |
655 | 655 | EXAMPLES:: |
656 | | |
| 656 | |
657 | 657 | sage: long(sin(2)*100) |
658 | 658 | 90L |
659 | 659 | """ |
… |
… |
|
662 | 662 | def _rational_(self): |
663 | 663 | """ |
664 | 664 | EXAMPLES:: |
665 | | |
| 665 | |
666 | 666 | sage: f = x^3 + 17/1*x - 3/8 |
667 | 667 | sage: QQ(f.coeff(x^2)) |
668 | 668 | 0 |
… |
… |
|
677 | 677 | |
678 | 678 | If the symbolic expression is just a wrapper around a rational, |
679 | 679 | that very same rational is returned:: |
680 | | |
| 680 | |
681 | 681 | sage: n = 17/1; SR(n)._rational_() is n |
682 | 682 | True |
683 | 683 | """ |
… |
… |
|
719 | 719 | only variables, and functions whose arguments contain a variable. |
720 | 720 | |
721 | 721 | EXAMPLES:: |
722 | | |
| 722 | |
723 | 723 | sage: f = sqrt(2) * cos(3); f |
724 | 724 | sqrt(2)*cos(3) |
725 | 725 | sage: f._convert(RDF) |
… |
… |
|
740 | 740 | log(10) |
741 | 741 | sage: t._convert(QQ) |
742 | 742 | 2.30258509299405 |
743 | | |
| 743 | |
744 | 744 | :: |
745 | 745 | |
746 | 746 | sage: (0.25 / (log(5.74 /x^0.9, 10))^2 / 4)._convert(QQ) |
747 | 747 | 0.331368631904900/log(287/50/x^0.900000000000000)^2 |
748 | 748 | sage: (0.25 / (log(5.74 /x^0.9, 10))^2 / 4)._convert(CC) |
749 | 749 | 0.331368631904900/log(5.74000000000000/x^0.900000000000000)^2 |
750 | | |
| 750 | |
751 | 751 | When converting to an exact domain, powers remain unevaluated:: |
752 | 752 | |
753 | 753 | sage: f = sqrt(2) * cos(3); f |
… |
… |
|
764 | 764 | |
765 | 765 | The precision of the approximation is determined by the precision of |
766 | 766 | the input R. |
767 | | |
| 767 | |
768 | 768 | EXAMPLES:: |
769 | 769 | |
770 | 770 | 0.090909090909090909090909090909090909090909090909090909090909 |
… |
… |
|
781 | 781 | def _real_mpfi_(self, R): |
782 | 782 | """ |
783 | 783 | Returns this expression as a real interval. |
784 | | |
| 784 | |
785 | 785 | EXAMPLES:: |
786 | 786 | |
787 | 787 | sage: RIF(sqrt(2)) |
… |
… |
|
795 | 795 | def _complex_mpfi_(self, R): |
796 | 796 | """ |
797 | 797 | Returns this expression as a complex interval. |
798 | | |
| 798 | |
799 | 799 | EXAMPLES:: |
800 | 800 | |
801 | 801 | sage: CIF(pi) |
… |
… |
|
822 | 822 | |
823 | 823 | The precision of the approximation is determined by the precision of |
824 | 824 | the input R. |
825 | | |
826 | | EXAMPLES:: |
827 | | |
828 | | sage: ComplexField(200)(SR(1/11)) |
| 825 | |
| 826 | EXAMPLES:: |
| 827 | |
| 828 | sage: ComplexField(200)(SR(1/11)) |
829 | 829 | 0.090909090909090909090909090909090909090909090909090909090909 |
830 | 830 | sage: zeta(x).subs(x=I)._complex_mpfr_field_(ComplexField(70)) |
831 | 831 | 0.0033002236853241028742 - 0.41815544914132167669*I |
… |
… |
|
856 | 856 | |
857 | 857 | EXAMPLES:: |
858 | 858 | |
859 | | sage: CDF(SR(1/11)) |
| 859 | sage: CDF(SR(1/11)) |
860 | 860 | 0.0909090909091 |
861 | 861 | sage: zeta(x).subs(x=I)._complex_double_(CDF) |
862 | 862 | 0.00330022368532 - 0.418155449141*I |
… |
… |
|
875 | 875 | Otherwise, raise a TypeError. |
876 | 876 | |
877 | 877 | OUTPUT: |
878 | | |
| 878 | |
879 | 879 | - float - double precision evaluation of self |
880 | | |
881 | | EXAMPLES:: |
882 | | |
| 880 | |
| 881 | EXAMPLES:: |
| 882 | |
883 | 883 | sage: float(SR(12)) |
884 | 884 | 12.0 |
885 | 885 | sage: float(SR(2/3)) |
… |
… |
|
905 | 905 | EXAMPLES:: |
906 | 906 | |
907 | 907 | sage: complex(I) |
908 | | 1j |
| 908 | 1j |
909 | 909 | sage: complex(erf(3*I)) |
910 | 910 | Traceback (most recent call last): |
911 | 911 | ... |
… |
… |
|
915 | 915 | return self._eval_self(complex) |
916 | 916 | except TypeError: |
917 | 917 | raise TypeError, "unable to simplify to complex approximation" |
918 | | |
| 918 | |
919 | 919 | def _sympy_(self): |
920 | 920 | """ |
921 | 921 | Returns a Sympy version of this object. |
… |
… |
|
930 | 930 | """ |
931 | 931 | from sage.symbolic.expression_conversions import sympy |
932 | 932 | return sympy(self) |
933 | | |
| 933 | |
934 | 934 | def _algebraic_(self, field): |
935 | 935 | """ |
936 | 936 | Convert a symbolic expression to an algebraic number. |
937 | | |
| 937 | |
938 | 938 | EXAMPLES:: |
939 | 939 | |
940 | 940 | sage: QQbar(sqrt(2) + sqrt(8)) |
… |
… |
|
947 | 947 | 1 + 1*I |
948 | 948 | sage: QQbar(e^(pi*I/3)) |
949 | 949 | 0.500000000000000? + 0.866025403784439?*I |
950 | | |
| 950 | |
951 | 951 | sage: QQbar(sqrt(2)) |
952 | 952 | 1.414213562373095? |
953 | 953 | sage: AA(abs(1+I)) |
… |
… |
|
964 | 964 | """ |
965 | 965 | from sage.symbolic.expression_conversions import algebraic |
966 | 966 | return algebraic(self, field) |
967 | | |
| 967 | |
968 | 968 | def __hash__(self): |
969 | 969 | """ |
970 | 970 | Return hash of this expression. |
971 | 971 | |
972 | 972 | EXAMPLES:: |
973 | | |
| 973 | |
974 | 974 | The hash of an object in Python or its coerced version into |
975 | 975 | the symbolic ring is the same:: |
976 | 976 | |
977 | | sage: hash(SR(3/1)) |
| 977 | sage: hash(SR(3/1)) |
978 | 978 | 3 |
979 | 979 | sage: hash(SR(19/23)) |
980 | 980 | 4 |
… |
… |
|
990 | 990 | <type 'int'> |
991 | 991 | sage: t = hash(x^y); type(t) |
992 | 992 | <type 'int'> |
993 | | sage: type(hash(x+y)) |
| 993 | sage: type(hash(x+y)) |
994 | 994 | <type 'int'> |
995 | 995 | sage: d = {x+y: 5} |
996 | 996 | sage: d |
… |
… |
|
998 | 998 | |
999 | 999 | In this example hashing is important otherwise the answer is |
1000 | 1000 | wrong:: |
1001 | | |
| 1001 | |
1002 | 1002 | sage: uniq([x-x, -x+x]) |
1003 | 1003 | [0] |
1004 | 1004 | |
… |
… |
|
1052 | 1052 | def __richcmp__(left, right, int op): |
1053 | 1053 | """ |
1054 | 1054 | Create a formal symbolic inequality or equality. |
1055 | | |
1056 | | EXAMPLES:: |
1057 | | |
| 1055 | |
| 1056 | EXAMPLES:: |
| 1057 | |
1058 | 1058 | sage: var('x, y') |
1059 | 1059 | (x, y) |
1060 | 1060 | sage: x + 2/3 < y^2 |
… |
… |
|
1097 | 1097 | elif op == Py_NE: |
1098 | 1098 | e = g_ne(l._gobj, r._gobj) |
1099 | 1099 | elif op == Py_GE: |
1100 | | e = g_ge(l._gobj, r._gobj) |
| 1100 | e = g_ge(l._gobj, r._gobj) |
1101 | 1101 | else: |
1102 | 1102 | raise TypeError |
1103 | 1103 | return new_Expression_from_GEx(l._parent, e) |
… |
… |
|
1106 | 1106 | r""" |
1107 | 1107 | Assume that this equation holds. This is relevant for symbolic |
1108 | 1108 | integration, among other things. |
1109 | | |
| 1109 | |
1110 | 1110 | EXAMPLES: We call the assume method to assume that `x>2`:: |
1111 | | |
| 1111 | |
1112 | 1112 | sage: (x > 2).assume() |
1113 | | |
| 1113 | |
1114 | 1114 | Bool returns True below if the inequality is *definitely* known to |
1115 | 1115 | be True. |
1116 | | |
1117 | | :: |
1118 | | |
| 1116 | |
| 1117 | :: |
| 1118 | |
1119 | 1119 | sage: bool(x > 0) |
1120 | 1120 | True |
1121 | 1121 | sage: bool(x < 0) |
1122 | 1122 | False |
1123 | | |
| 1123 | |
1124 | 1124 | This may or may not be True, so bool returns False:: |
1125 | | |
| 1125 | |
1126 | 1126 | sage: bool(x > 3) |
1127 | 1127 | False |
1128 | | |
| 1128 | |
1129 | 1129 | If you make inconsistent or meaningless assumptions, |
1130 | 1130 | Sage will let you know:: |
1131 | 1131 | |
… |
… |
|
1140 | 1140 | sage: forget() |
1141 | 1141 | |
1142 | 1142 | TESTS:: |
1143 | | |
| 1143 | |
1144 | 1144 | sage: v,c = var('v,c') |
1145 | 1145 | sage: assume(c != 0) |
1146 | 1146 | sage: integral((1+v^2/c^2)^3/(1-v^2/c^2)^(3/2),v) |
… |
… |
|
1161 | 1161 | def forget(self): |
1162 | 1162 | """ |
1163 | 1163 | Forget the given constraint. |
1164 | | |
1165 | | EXAMPLES:: |
1166 | | |
| 1164 | |
| 1165 | EXAMPLES:: |
| 1166 | |
1167 | 1167 | sage: var('x,y') |
1168 | 1168 | (x, y) |
1169 | 1169 | sage: forget() |
… |
… |
|
1189 | 1189 | """ |
1190 | 1190 | Return string that when evaluated in Maxima defines the assumption |
1191 | 1191 | determined by this expression. |
1192 | | |
| 1192 | |
1193 | 1193 | EXAMPLES:: |
1194 | 1194 | |
1195 | 1195 | sage: f = x+2 > sqrt(3) |
… |
… |
|
1400 | 1400 | Return True if self is a relational expression. |
1401 | 1401 | |
1402 | 1402 | EXAMPLES:: |
1403 | | |
| 1403 | |
1404 | 1404 | sage: x = var('x') |
1405 | 1405 | sage: eqn = (x-1)^2 == x^2 - 2*x + 3 |
1406 | 1406 | sage: eqn.is_relational() |
… |
… |
|
1416 | 1416 | of the relation. Otherwise, raise a ValueError. |
1417 | 1417 | |
1418 | 1418 | EXAMPLES:: |
1419 | | |
| 1419 | |
1420 | 1420 | sage: x = var('x') |
1421 | 1421 | sage: eqn = (x-1)^2 == x^2 - 2*x + 3 |
1422 | 1422 | sage: eqn.left_hand_side() |
… |
… |
|
1438 | 1438 | of the relation. Otherwise, raise a ValueError. |
1439 | 1439 | |
1440 | 1440 | EXAMPLES:: |
1441 | | |
| 1441 | |
1442 | 1442 | sage: x = var('x') |
1443 | 1443 | sage: eqn = (x-1)^2 <= x^2 - 2*x + 3 |
1444 | 1444 | sage: eqn.right_hand_side() |
… |
… |
|
1457 | 1457 | def __nonzero__(self): |
1458 | 1458 | """ |
1459 | 1459 | Return True unless this symbolic expression can be shown by Sage |
1460 | | to be zero. Note that deciding if an expression is zero is |
| 1460 | to be zero. Note that deciding if an expression is zero is |
1461 | 1461 | undecidable in general. |
1462 | 1462 | |
1463 | 1463 | EXAMPLES:: |
1464 | | |
| 1464 | |
1465 | 1465 | sage: x = var('x') |
1466 | 1466 | sage: forget() |
1467 | 1467 | sage: SR(0).__nonzero__() |
… |
… |
|
1479 | 1479 | sage: pol = 1/(k-1) - 1/k - 1/k/(k-1) |
1480 | 1480 | sage: pol.is_zero() |
1481 | 1481 | True |
1482 | | |
| 1482 | |
1483 | 1483 | sage: f = sin(x)^2 + cos(x)^2 - 1 |
1484 | 1484 | sage: f.is_zero() |
1485 | 1485 | True |
… |
… |
|
1488 | 1488 | |
1489 | 1489 | First, a bunch of tests of nonzero (which is called by bool) |
1490 | 1490 | for symbolic relations:: |
1491 | | |
| 1491 | |
1492 | 1492 | sage: x = var('x') |
1493 | 1493 | sage: bool((x-1)^2 == x^2 - 2*x + 1) |
1494 | 1494 | True |
… |
… |
|
1522 | 1522 | True |
1523 | 1523 | |
1524 | 1524 | Next, tests to ensure assumptions are correctly used:: |
1525 | | |
| 1525 | |
1526 | 1526 | sage: x, y, z = var('x, y, z') |
1527 | 1527 | sage: assume(x>=y,y>=z,z>=x) |
1528 | 1528 | sage: bool(x==z) |
… |
… |
|
1549 | 1549 | False |
1550 | 1550 | sage: bool(x != 0) |
1551 | 1551 | True |
1552 | | sage: bool(x == 1) |
| 1552 | sage: bool(x == 1) |
1553 | 1553 | False |
1554 | 1554 | |
1555 | 1555 | The following must be true, even though we don't |
1556 | 1556 | know for sure that x isn't 1, as symbolic comparisons |
1557 | | elsewhere rely on x!=y unless we are sure it is not |
| 1557 | elsewhere rely on x!=y unless we are sure it is not |
1558 | 1558 | true; there is no equivalent of Maxima's ``unknown``. |
1559 | 1559 | Since it is False that x==1, it is True that x != 1. |
1560 | 1560 | |
1561 | 1561 | :: |
1562 | 1562 | |
1563 | | sage: bool(x != 1) |
| 1563 | sage: bool(x != 1) |
1564 | 1564 | True |
1565 | 1565 | sage: forget() |
1566 | 1566 | sage: assume(x>y) |
… |
… |
|
1583 | 1583 | if res: |
1584 | 1584 | if self.operator() == operator.ne: # this hack is necessary to catch the case where the operator is != but is False because of assumptions made |
1585 | 1585 | m = self._maxima_() |
1586 | | s = m.parent()._eval_line('is (notequal(%s,%s))'%(repr(m.lhs()),repr(m.rhs()))) |
| 1586 | s = m.parent()._eval_line('is (notequal(%s,%s))'%(repr(m.lhs()),repr(m.rhs()))) |
1587 | 1587 | if s == 'false': |
1588 | 1588 | return False |
1589 | 1589 | else: |
… |
… |
|
1624 | 1624 | # lot of basic Sage objects can't be put into maxima. |
1625 | 1625 | from sage.symbolic.relation import test_relation_maxima |
1626 | 1626 | return test_relation_maxima(self) |
1627 | | |
| 1627 | |
1628 | 1628 | self_is_zero = self._gobj.is_zero() |
1629 | 1629 | if self_is_zero: |
1630 | 1630 | return False |
… |
… |
|
1635 | 1635 | """ |
1636 | 1636 | Test this relation at several random values, attempting to find |
1637 | 1637 | a contradiction. If this relation has no variables, it will also |
1638 | | test this relation after casting into the domain. |
1639 | | |
1640 | | Because the interval fields never return false positives, we can be |
1641 | | assured that if True or False is returned (and proof is False) then |
1642 | | the answer is correct. |
| 1638 | test this relation after casting into the domain. |
| 1639 | |
| 1640 | Because the interval fields never return false positives, we can be |
| 1641 | assured that if True or False is returned (and proof is False) then |
| 1642 | the answer is correct. |
1643 | 1643 | |
1644 | 1644 | INPUT:: |
1645 | | |
| 1645 | |
1646 | 1646 | ntests -- (default 20) the number of iterations to run |
1647 | 1647 | domain -- (optional) the domain from which to draw the random values |
1648 | | defaults to CIF for equality testing and RIF for |
| 1648 | defaults to CIF for equality testing and RIF for |
1649 | 1649 | order testing |
1650 | | proof -- (default True) if False and the domain is an interval field, |
| 1650 | proof -- (default True) if False and the domain is an interval field, |
1651 | 1651 | regard overlapping (potentially equal) intervals as equal, |
1652 | | and return True if all tests succeeded. |
| 1652 | and return True if all tests succeeded. |
1653 | 1653 | |
1654 | 1654 | OUTPUT:: |
1655 | 1655 | |
… |
… |
|
1658 | 1658 | NotImplemented - no contradiction found |
1659 | 1659 | |
1660 | 1660 | EXAMPLES:: |
1661 | | |
| 1661 | |
1662 | 1662 | sage: (3 < pi).test_relation() |
1663 | 1663 | True |
1664 | 1664 | sage: (0 >= pi).test_relation() |
… |
… |
|
1677 | 1677 | (x, y) |
1678 | 1678 | sage: (x < y).test_relation() |
1679 | 1679 | False |
1680 | | |
| 1680 | |
1681 | 1681 | TESTS:: |
1682 | 1682 | |
1683 | 1683 | sage: all_relations = [op for name, op in sorted(operator.__dict__.items()) if len(name) == 2] |
1684 | | sage: all_relations |
| 1684 | sage: all_relations |
1685 | 1685 | [<built-in function eq>, <built-in function ge>, <built-in function gt>, <built-in function le>, <built-in function lt>, <built-in function ne>] |
1686 | 1686 | sage: [op(3, pi).test_relation() for op in all_relations] |
1687 | 1687 | [False, False, False, True, True, True] |
… |
… |
|
1776 | 1776 | # Nothing failed, so it *may* be True, but this method doesn't wasn't |
1777 | 1777 | # able to find anything. |
1778 | 1778 | return NotImplemented |
1779 | | |
| 1779 | |
1780 | 1780 | def negation(self): |
1781 | 1781 | """ |
1782 | | Returns the negated version of self, that is the relation that is |
| 1782 | Returns the negated version of self, that is the relation that is |
1783 | 1783 | False iff self is True. |
1784 | | |
| 1784 | |
1785 | 1785 | EXAMPLES:: |
1786 | 1786 | |
1787 | 1787 | sage: (x < 5).negation() |
… |
… |
|
1807 | 1807 | elif op == greater_or_equal: |
1808 | 1808 | falsify = operator.lt |
1809 | 1809 | return falsify(self.lhs(), self.rhs()) |
1810 | | |
| 1810 | |
1811 | 1811 | def contradicts(self, soln): |
1812 | 1812 | """ |
1813 | 1813 | Returns ``True`` if this relation is violated by the given variable assignment(s). |
1814 | | |
1815 | | EXAMPLES:: |
1816 | | |
| 1814 | |
| 1815 | EXAMPLES:: |
| 1816 | |
1817 | 1817 | sage: (x<3).contradicts(x==0) |
1818 | 1818 | False |
1819 | 1819 | sage: (x<3).contradicts(x==3) |
… |
… |
|
1833 | 1833 | Return True if this expression is a unit of the symbolic ring. |
1834 | 1834 | |
1835 | 1835 | EXAMPLES:: |
1836 | | |
| 1836 | |
1837 | 1837 | sage: SR(1).is_unit() |
1838 | 1838 | True |
1839 | 1839 | sage: SR(-1).is_unit() |
… |
… |
|
1863 | 1863 | Add left and right. |
1864 | 1864 | |
1865 | 1865 | EXAMPLES:: |
1866 | | |
| 1866 | |
1867 | 1867 | sage: var("x y") |
1868 | 1868 | (x, y) |
1869 | 1869 | sage: x + y + y + x |
… |
… |
|
1877 | 1877 | 2*x + y > 2*x |
1878 | 1878 | |
1879 | 1879 | TESTS:: |
1880 | | |
| 1880 | |
1881 | 1881 | sage: x + ( (x+y) > x ) |
1882 | 1882 | 2*x + y > 2*x |
1883 | 1883 | |
… |
… |
|
1960 | 1960 | cpdef ModuleElement _sub_(left, ModuleElement right): |
1961 | 1961 | """ |
1962 | 1962 | EXAMPLES:: |
1963 | | |
| 1963 | |
1964 | 1964 | sage: var("x y") |
1965 | 1965 | (x, y) |
1966 | 1966 | sage: x - y |
… |
… |
|
1974 | 1974 | y > 0 |
1975 | 1975 | |
1976 | 1976 | TESTS:: |
1977 | | |
| 1977 | |
1978 | 1978 | sage: x - ( (x+y) > x ) |
1979 | 1979 | -y > 0 |
1980 | 1980 | |
… |
… |
|
2012 | 2012 | cpdef RingElement _mul_(left, RingElement right): |
2013 | 2013 | """ |
2014 | 2014 | Multiply left and right. |
2015 | | |
2016 | | EXAMPLES:: |
2017 | | |
| 2015 | |
| 2016 | EXAMPLES:: |
| 2017 | |
2018 | 2018 | sage: var("x y") |
2019 | 2019 | (x, y) |
2020 | 2020 | sage: x*y*y |
… |
… |
|
2031 | 2031 | -x - y > -x |
2032 | 2032 | |
2033 | 2033 | TESTS:: |
2034 | | |
| 2034 | |
2035 | 2035 | sage: x * ( (x+y) > x ) |
2036 | 2036 | (x + y)*x > x^2 |
2037 | 2037 | |
… |
… |
|
2136 | 2136 | Divide left and right. |
2137 | 2137 | |
2138 | 2138 | EXAMPLES:: |
2139 | | |
| 2139 | |
2140 | 2140 | sage: var("x y") |
2141 | 2141 | (x, y) |
2142 | 2142 | sage: x/y/y |
… |
… |
|
2153 | 2153 | -x - y > -x |
2154 | 2154 | |
2155 | 2155 | TESTS:: |
2156 | | |
| 2156 | |
2157 | 2157 | sage: x / ( (x+y) > x ) |
2158 | 2158 | x/(x + y) > 1 |
2159 | 2159 | |
… |
… |
|
2262 | 2262 | this function won't be called. |
2263 | 2263 | |
2264 | 2264 | EXAMPLES:: |
2265 | | |
| 2265 | |
2266 | 2266 | sage: x,y = var('x,y') |
2267 | 2267 | sage: x.__cmp__(y) |
2268 | 2268 | -1 |
… |
… |
|
2310 | 2310 | x^(sin(x)^cos(y)) |
2311 | 2311 | |
2312 | 2312 | TESTS:: |
2313 | | |
| 2313 | |
2314 | 2314 | sage: (Mod(2,7)*x^2 + Mod(2,7))^7 |
2315 | 2315 | (2*x^2 + 2)^7 |
2316 | 2316 | sage: k = GF(7) |
… |
… |
|
2331 | 2331 | Infinity |
2332 | 2332 | |
2333 | 2333 | Test powers of exp:: |
2334 | | |
| 2334 | |
2335 | 2335 | sage: exp(2)^5 |
2336 | 2336 | e^10 |
2337 | 2337 | sage: exp(x)^5 |
… |
… |
|
2347 | 2347 | (1+3j)^sqrt(2) |
2348 | 2348 | """ |
2349 | 2349 | cdef Expression base, nexp |
2350 | | |
| 2350 | |
2351 | 2351 | try: |
2352 | 2352 | # self is an Expression and exp might not be |
2353 | 2353 | base = self |
… |
… |
|
2379 | 2379 | :meth:`_derivative` |
2380 | 2380 | |
2381 | 2381 | EXAMPLES:: |
2382 | | |
| 2382 | |
2383 | 2383 | sage: var("x y") |
2384 | 2384 | (x, y) |
2385 | 2385 | sage: t = (x^2+y)^2 |
… |
… |
|
2401 | 2401 | (tan(x*y)^2 + 1)*x*sin(y^2 + x) + 2*y*cos(y^2 + x)*tan(x*y) |
2402 | 2402 | |
2403 | 2403 | :: |
2404 | | |
| 2404 | |
2405 | 2405 | sage: h = sin(x)/cos(x) |
2406 | 2406 | sage: derivative(h,x,x,x) |
2407 | 2407 | 6*sin(x)^4/cos(x)^4 + 8*sin(x)^2/cos(x)^2 + 2 |
… |
… |
|
2409 | 2409 | 6*sin(x)^4/cos(x)^4 + 8*sin(x)^2/cos(x)^2 + 2 |
2410 | 2410 | |
2411 | 2411 | :: |
2412 | | |
| 2412 | |
2413 | 2413 | sage: var('x, y') |
2414 | 2414 | (x, y) |
2415 | 2415 | sage: u = (sin(x) + cos(y))*(cos(x) - sin(y)) |
2416 | 2416 | sage: derivative(u,x,y) |
2417 | | sin(x)*sin(y) - cos(x)*cos(y) |
| 2417 | sin(x)*sin(y) - cos(x)*cos(y) |
2418 | 2418 | sage: f = ((x^2+1)/(x^2-1))^(1/4) |
2419 | 2419 | sage: g = derivative(f, x); g # this is a complex expression |
2420 | 2420 | 1/2*(x/(x^2 - 1) - (x^2 + 1)*x/(x^2 - 1)^2)/((x^2 + 1)/(x^2 - 1))^(3/4) |
… |
… |
|
2427 | 2427 | sage: f = y^(sin(x)) |
2428 | 2428 | sage: derivative(f, x) |
2429 | 2429 | y^sin(x)*log(y)*cos(x) |
2430 | | |
2431 | | :: |
2432 | | |
| 2430 | |
| 2431 | :: |
| 2432 | |
2433 | 2433 | sage: g(x) = sqrt(5-2*x) |
2434 | 2434 | sage: g_3 = derivative(g, x, 3); g_3(2) |
2435 | 2435 | -3 |
2436 | | |
2437 | | :: |
2438 | | |
| 2436 | |
| 2437 | :: |
| 2438 | |
2439 | 2439 | sage: f = x*e^(-x) |
2440 | 2440 | sage: derivative(f, 100) |
2441 | 2441 | x*e^(-x) - 100*e^(-x) |
2442 | | |
2443 | | :: |
2444 | | |
| 2442 | |
| 2443 | :: |
| 2444 | |
2445 | 2445 | sage: g = 1/(sqrt((x^2-1)*(x+5)^6)) |
2446 | 2446 | sage: derivative(g, x) |
2447 | 2447 | -((x + 5)^6*x + 3*(x + 5)^5*(x^2 - 1))/((x + 5)^6*(x^2 - 1))^(3/2) |
2448 | 2448 | |
2449 | 2449 | TESTS:: |
2450 | | |
| 2450 | |
2451 | 2451 | sage: t.derivative() |
2452 | 2452 | Traceback (most recent call last): |
2453 | 2453 | ... |
… |
… |
|
2460 | 2460 | def _derivative(self, symb=None, deg=1): |
2461 | 2461 | """ |
2462 | 2462 | Return the deg-th (partial) derivative of self with respect to symb. |
2463 | | |
2464 | | EXAMPLES:: |
2465 | | |
| 2463 | |
| 2464 | EXAMPLES:: |
| 2465 | |
2466 | 2466 | sage: var("x y") |
2467 | 2467 | (x, y) |
2468 | 2468 | sage: b = (x+y)^5 |
… |
… |
|
2480 | 2480 | |
2481 | 2481 | Raise error if no variable is specified and there are multiple |
2482 | 2482 | variables:: |
2483 | | |
| 2483 | |
2484 | 2484 | sage: b._derivative() |
2485 | 2485 | Traceback (most recent call last): |
2486 | 2486 | ... |
… |
… |
|
2521 | 2521 | def gradient(self, variables=None): |
2522 | 2522 | r""" |
2523 | 2523 | Compute the gradient of a symbolic function. |
2524 | | |
| 2524 | |
2525 | 2525 | This function returns a vector whose components are the derivatives |
2526 | 2526 | of the original function with respect to the arguments of the |
2527 | 2527 | original function. Alternatively, you can specify the variables as |
2528 | 2528 | a list. |
2529 | | |
2530 | | EXAMPLES:: |
2531 | | |
| 2529 | |
| 2530 | EXAMPLES:: |
| 2531 | |
2532 | 2532 | sage: x,y = var('x y') |
2533 | 2533 | sage: f = x^2+y^2 |
2534 | 2534 | sage: f.gradient() |
… |
… |
|
2547 | 2547 | if variables is None: |
2548 | 2548 | variables = self.arguments() |
2549 | 2549 | return vector([self.derivative(x) for x in variables]) |
2550 | | |
| 2550 | |
2551 | 2551 | def hessian(self): |
2552 | 2552 | r""" |
2553 | 2553 | Compute the hessian of a function. This returns a matrix components |
2554 | 2554 | are the 2nd partial derivatives of the original function. |
2555 | | |
2556 | | EXAMPLES:: |
2557 | | |
| 2555 | |
| 2556 | EXAMPLES:: |
| 2557 | |
2558 | 2558 | sage: x,y = var('x y') |
2559 | 2559 | sage: f = x^2+y^2 |
2560 | 2560 | sage: f.hessian() |
… |
… |
|
2586 | 2586 | - a power series |
2587 | 2587 | |
2588 | 2588 | To truncate the power series and obtain a normal expression, use the |
2589 | | truncate command. |
2590 | | |
2591 | | EXAMPLES: |
2592 | | |
| 2589 | truncate command. |
| 2590 | |
| 2591 | EXAMPLES: |
| 2592 | |
2593 | 2593 | We expand a polynomial in `x` about 0, about `1`, and also truncate |
2594 | 2594 | it back to a polynomial:: |
2595 | | |
| 2595 | |
2596 | 2596 | sage: var('x,y') |
2597 | 2597 | (x, y) |
2598 | 2598 | sage: f = (x^3 - sin(y)*x^2 - 5*x + 3); f |
… |
… |
|
2609 | 2609 | x^3 - x^2*sin(y) - 5*x + 3 |
2610 | 2610 | |
2611 | 2611 | We computer another series expansion of an analytic function:: |
2612 | | |
| 2612 | |
2613 | 2613 | sage: f = sin(x)/x^2 |
2614 | 2614 | sage: f.series(x,7) |
2615 | 2615 | 1*x^(-1) + (-1/6)*x + 1/120*x^3 + (-1/5040)*x^5 + Order(x^7) |
… |
… |
|
2624 | 2624 | the fractions 1/5 and 1/239. |
2625 | 2625 | |
2626 | 2626 | :: |
2627 | | |
| 2627 | |
2628 | 2628 | sage: x = var('x') |
2629 | 2629 | sage: f = atan(x).series(x, 10); f |
2630 | 2630 | 1*x + (-1/3)*x^3 + 1/5*x^5 + (-1/7)*x^7 + 1/9*x^9 + Order(x^10) |
… |
… |
|
2641 | 2641 | r""" |
2642 | 2642 | Expands this symbolic expression in a truncated Taylor or |
2643 | 2643 | Laurent series in the variable `v` around the point `a`, |
2644 | | containing terms through `(x - a)^n`. Functions in more |
| 2644 | containing terms through `(x - a)^n`. Functions in more |
2645 | 2645 | variables is also supported. |
2646 | | |
2647 | | INPUT: |
2648 | | |
2649 | | - ``*args`` - the following notation is supported |
2650 | | |
| 2646 | |
| 2647 | INPUT: |
| 2648 | |
| 2649 | - ``*args`` - the following notation is supported |
| 2650 | |
2651 | 2651 | - ``x, a, n`` - variable, point, degree |
2652 | | |
| 2652 | |
2653 | 2653 | - ``(x, a), (y, b), n`` - variables with points, degree of polynomial |
2654 | | |
2655 | | EXAMPLES:: |
2656 | | |
| 2654 | |
| 2655 | EXAMPLES:: |
| 2656 | |
2657 | 2657 | sage: var('a, x, z') |
2658 | 2658 | (a, x, z) |
2659 | 2659 | sage: taylor(a*log(z), z, 2, 3) |
2660 | 2660 | 1/24*(z - 2)^3*a - 1/8*(z - 2)^2*a + 1/2*(z - 2)*a + a*log(2) |
2661 | 2661 | |
2662 | | :: |
| 2662 | :: |
2663 | 2663 | |
2664 | 2664 | sage: taylor(sqrt (sin(x) + a*x + 1), x, 0, 3) |
2665 | 2665 | 1/48*(3*a^3 + 9*a^2 + 9*a - 1)*x^3 - 1/8*(a^2 + 2*a + 1)*x^2 + 1/2*(a + 1)*x + 1 |
2666 | 2666 | |
2667 | | :: |
| 2667 | :: |
2668 | 2668 | |
2669 | 2669 | sage: taylor (sqrt (x + 1), x, 0, 5) |
2670 | 2670 | 7/256*x^5 - 5/128*x^4 + 1/16*x^3 - 1/8*x^2 + 1/2*x + 1 |
2671 | 2671 | |
2672 | | :: |
| 2672 | :: |
2673 | 2673 | |
2674 | 2674 | sage: taylor (1/log (x + 1), x, 0, 3) |
2675 | 2675 | -19/720*x^3 + 1/24*x^2 - 1/12*x + 1/x + 1/2 |
2676 | 2676 | |
2677 | | :: |
| 2677 | :: |
2678 | 2678 | |
2679 | 2679 | sage: taylor (cos(x) - sec(x), x, 0, 5) |
2680 | 2680 | -1/6*x^4 - x^2 |
2681 | 2681 | |
2682 | | :: |
| 2682 | :: |
2683 | 2683 | |
2684 | 2684 | sage: taylor ((cos(x) - sec(x))^3, x, 0, 9) |
2685 | 2685 | -1/2*x^8 - x^6 |
2686 | 2686 | |
2687 | | :: |
| 2687 | :: |
2688 | 2688 | |
2689 | 2689 | sage: taylor (1/(cos(x) - sec(x))^3, x, 0, 5) |
2690 | 2690 | -15377/7983360*x^4 - 6767/604800*x^2 + 11/120/x^2 + 1/2/x^4 - 1/x^6 - 347/15120 |
2691 | 2691 | |
2692 | | |
| 2692 | |
2693 | 2693 | Ticket #7472 fixed (Taylor polynomial in more variables) :: |
2694 | | |
2695 | | sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,1),4) |
| 2694 | |
| 2695 | sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,1),4) |
2696 | 2696 | (y - 1)^3*(x - 1) + (y - 1)^3 + 3*(y - 1)^2*(x - 1) + 3*(y - 1)^2 + 3*(y - 1)*(x - 1) + x + 3*y - 3 |
2697 | | sage: expand(_) |
2698 | | x*y^3 |
| 2697 | sage: expand(_) |
| 2698 | x*y^3 |
2699 | 2699 | |
2700 | 2700 | """ |
2701 | 2701 | from sage.all import SR, Integer |
… |
… |
|
2729 | 2729 | - expression |
2730 | 2730 | |
2731 | 2731 | EXAMPLES:: |
2732 | | |
| 2732 | |
2733 | 2733 | sage: f = sin(x)/x^2 |
2734 | 2734 | sage: f.truncate() |
2735 | 2735 | sin(x)/x^2 |
… |
… |
|
2750 | 2750 | sums are multiplied out, numerators of rational expressions which |
2751 | 2751 | are sums are split into their respective terms, and multiplications |
2752 | 2752 | are distributed over addition at all levels. |
2753 | | |
| 2753 | |
2754 | 2754 | EXAMPLES: |
2755 | 2755 | |
2756 | 2756 | We expand the expression `(x-y)^5` using both |
2757 | 2757 | method and functional notation. |
2758 | | |
2759 | | :: |
2760 | | |
| 2758 | |
| 2759 | :: |
| 2760 | |
2761 | 2761 | sage: x,y = var('x,y') |
2762 | 2762 | sage: a = (x-y)^5 |
2763 | 2763 | sage: a.expand() |
2764 | 2764 | x^5 - 5*x^4*y + 10*x^3*y^2 - 10*x^2*y^3 + 5*x*y^4 - y^5 |
2765 | 2765 | sage: expand(a) |
2766 | 2766 | x^5 - 5*x^4*y + 10*x^3*y^2 - 10*x^2*y^3 + 5*x*y^4 - y^5 |
2767 | | |
| 2767 | |
2768 | 2768 | We expand some other expressions:: |
2769 | | |
| 2769 | |
2770 | 2770 | sage: expand((x-1)^3/(y-1)) |
2771 | 2771 | x^3/(y - 1) - 3*x^2/(y - 1) + 3*x/(y - 1) - 1/(y - 1) |
2772 | 2772 | sage: expand((x+sin((x+y)^2))^2) |
… |
… |
|
2783 | 2783 | (16*x - 13)^2 == 9/2*x^2 + 15*x + 25/2 |
2784 | 2784 | |
2785 | 2785 | TESTS: |
2786 | | |
| 2786 | |
2787 | 2787 | sage: var('x,y') |
2788 | 2788 | (x, y) |
2789 | 2789 | sage: ((x + (2/3)*y)^3).expand() |
… |
… |
|
2804 | 2804 | return self.operator()(self.lhs(), self.rhs().expand()) |
2805 | 2805 | else: |
2806 | 2806 | raise ValueError, "side must be 'left', 'right', or None" |
2807 | | |
| 2807 | |
2808 | 2808 | _sig_on |
2809 | 2809 | cdef GEx x = self._gobj.expand(0) |
2810 | 2810 | _sig_off |
… |
… |
|
2817 | 2817 | Expands trigonometric and hyperbolic functions of sums of angles |
2818 | 2818 | and of multiple angles occurring in self. For best results, self |
2819 | 2819 | should already be expanded. |
2820 | | |
2821 | | INPUT: |
2822 | | |
| 2820 | |
| 2821 | INPUT: |
| 2822 | |
2823 | 2823 | - ``full`` - (default: False) To enhance user control |
2824 | 2824 | of simplification, this function expands only one level at a time |
2825 | 2825 | by default, expanding sums of angles or multiple angles. To obtain |
2826 | 2826 | full expansion into sines and cosines immediately, set the optional |
2827 | 2827 | parameter full to True. |
2828 | | |
| 2828 | |
2829 | 2829 | - ``half_angles`` - (default: False) If True, causes |
2830 | 2830 | half-angles to be simplified away. |
2831 | | |
| 2831 | |
2832 | 2832 | - ``plus`` - (default: True) Controls the sum rule; |
2833 | 2833 | expansion of sums (e.g. 'sin(x + y)') will take place only if plus |
2834 | 2834 | is True. |
2835 | | |
| 2835 | |
2836 | 2836 | - ``times`` - (default: True) Controls the product |
2837 | 2837 | rule, expansion of products (e.g. sin(2\*x)) will take place only |
2838 | 2838 | if times is True. |
2839 | | |
2840 | | |
| 2839 | |
| 2840 | |
2841 | 2841 | OUTPUT: a symbolic expression |
2842 | | |
2843 | | EXAMPLES:: |
2844 | | |
| 2842 | |
| 2843 | EXAMPLES:: |
| 2844 | |
2845 | 2845 | sage: sin(5*x).expand_trig() |
2846 | 2846 | sin(x)^5 - 10*sin(x)^3*cos(x)^2 + 5*sin(x)*cos(x)^4 |
2847 | 2847 | sage: cos(2*x + var('y')).expand_trig() |
2848 | 2848 | -sin(2*x)*sin(y) + cos(2*x)*cos(y) |
2849 | | |
| 2849 | |
2850 | 2850 | We illustrate various options to this function:: |
2851 | | |
| 2851 | |
2852 | 2852 | sage: f = sin(sin(3*cos(2*x))*x) |
2853 | 2853 | sage: f.expand_trig() |
2854 | 2854 | sin(-(sin(cos(2*x))^3 - 3*sin(cos(2*x))*cos(cos(2*x))^2)*x) |
… |
… |
|
2888 | 2888 | sin's and cos's of x into those of multiples of x. It also |
2889 | 2889 | tries to eliminate these functions when they occur in |
2890 | 2890 | denominators. |
2891 | | |
2892 | | INPUT: |
2893 | | |
2894 | | - ``self`` - a symbolic expression |
| 2891 | |
| 2892 | INPUT: |
| 2893 | |
| 2894 | - ``self`` - a symbolic expression |
2895 | 2895 | |
2896 | 2896 | - ``var`` - (default: None) the variable which is used for |
2897 | 2897 | these transformations. If not specified, all variables are |
2898 | 2898 | used. |
2899 | | |
| 2899 | |
2900 | 2900 | OUTPUT: a symbolic expression |
2901 | | |
| 2901 | |
2902 | 2902 | EXAMPLES:: |
2903 | 2903 | |
2904 | 2904 | sage: y=var('y') |
… |
… |
|
2932 | 2932 | Check if self matches the given pattern. |
2933 | 2933 | |
2934 | 2934 | INPUT: |
2935 | | |
| 2935 | |
2936 | 2936 | - ``pattern`` - a symbolic expression, possibly containing wildcards |
2937 | 2937 | to match for |
2938 | 2938 | |
… |
… |
|
2946 | 2946 | See also http://www.ginac.de/tutorial/Pattern-matching-and-advanced-substitutions.html |
2947 | 2947 | |
2948 | 2948 | EXAMPLES:: |
2949 | | |
| 2949 | |
2950 | 2950 | sage: var('x,y,z,a,b,c,d,e,f') |
2951 | 2951 | (x, y, z, a, b, c, d, e, f) |
2952 | 2952 | sage: w0 = SR.wild(0); w1 = SR.wild(1); w2 = SR.wild(2) |
… |
… |
|
3062 | 3062 | def has(self, pattern): |
3063 | 3063 | """ |
3064 | 3064 | EXAMPLES:: |
3065 | | |
| 3065 | |
3066 | 3066 | sage: var('x,y,a'); w0 = SR.wild(); w1 = SR.wild() |
3067 | 3067 | (x, y, a) |
3068 | 3068 | sage: (x*sin(x + y + 2*a)).has(y) |
… |
… |
|
3070 | 3070 | |
3071 | 3071 | Here "x+y" is not a subexpression of "x+y+2*a" (which has the |
3072 | 3072 | subexpressions "x", "y" and "2*a"):: |
3073 | | |
| 3073 | |
3074 | 3074 | sage: (x*sin(x + y + 2*a)).has(x+y) |
3075 | 3075 | False |
3076 | 3076 | sage: (x*sin(x + y + 2*a)).has(x + y + w0) |
… |
… |
|
3078 | 3078 | |
3079 | 3079 | The following fails because "2*(x+y)" automatically gets converted to |
3080 | 3080 | "2*x+2*y" of which "x+y" is not a subexpression:: |
3081 | | |
| 3081 | |
3082 | 3082 | sage: (x*sin(2*(x+y) + 2*a)).has(x+y) |
3083 | 3083 | False |
3084 | 3084 | |
3085 | | Although x^1==x and x^0==1, neither "x" nor "1" are actually of the |
| 3085 | Although x^1==x and x^0==1, neither "x" nor "1" are actually of the |
3086 | 3086 | form "x^something":: |
3087 | | |
| 3087 | |
3088 | 3088 | sage: (x+1).has(x^w0) |
3089 | 3089 | False |
3090 | 3090 | |
… |
… |
|
3094 | 3094 | coeff() function instead. |
3095 | 3095 | |
3096 | 3096 | :: |
3097 | | |
| 3097 | |
3098 | 3098 | sage: (4*x^2 - x + 3).has(w0*x) |
3099 | 3099 | True |
3100 | 3100 | sage: (4*x^2 + x + 3).has(w0*x) |
… |
… |
|
3112 | 3112 | def substitute(self, in_dict=None, **kwds): |
3113 | 3113 | """ |
3114 | 3114 | EXAMPLES:: |
3115 | | |
| 3115 | |
3116 | 3116 | sage: var('x,y,z,a,b,c,d,e,f') |
3117 | 3117 | (x, y, z, a, b, c, d, e, f) |
3118 | 3118 | sage: w0 = SR.wild(0); w1 = SR.wild(1) |
3119 | 3119 | sage: t = a^2 + b^2 + (x+y)^3 |
3120 | | |
| 3120 | |
3121 | 3121 | # substitute with keyword arguments (works only with symbols) |
3122 | 3122 | sage: t.subs(a=c) |
3123 | 3123 | (x + y)^3 + b^2 + c^2 |
3124 | | |
| 3124 | |
3125 | 3125 | # substitute with a dictionary argument |
3126 | 3126 | sage: t.subs({a^2: c}) |
3127 | 3127 | (x + y)^3 + b^2 + c |
… |
… |
|
3134 | 3134 | (x + y)^3 + a^3 + b^3 |
3135 | 3135 | |
3136 | 3136 | sage: t.subs(w0==w0^2) |
3137 | | (x^2 + y^2)^18 + a^16 + b^16 |
| 3137 | (x^2 + y^2)^18 + a^16 + b^16 |
3138 | 3138 | |
3139 | 3139 | # more than one keyword argument is accepted |
3140 | 3140 | sage: t.subs(a=b, b=c) |
… |
… |
|
3221 | 3221 | return new_Expression_from_GEx(self._parent, self._gobj.subs_map(smap)) |
3222 | 3222 | |
3223 | 3223 | subs = substitute |
3224 | | |
| 3224 | |
3225 | 3225 | cpdef Expression _subs_expr(self, expr): |
3226 | 3226 | """ |
3227 | 3227 | EXAMPLES:: |
3228 | | |
| 3228 | |
3229 | 3229 | sage: var('x,y,z,a,b,c,d,e,f') |
3230 | 3230 | (x, y, z, a, b, c, d, e, f) |
3231 | 3231 | sage: w0 = SR.wild(0); w1 = SR.wild(1) |
… |
… |
|
3273 | 3273 | of key for value in self. The substitutions can also be given |
3274 | 3274 | as a number of symbolic equalities key == value; see the |
3275 | 3275 | examples. |
3276 | | |
| 3276 | |
3277 | 3277 | .. warning:: |
3278 | 3278 | |
3279 | 3279 | This is a formal pattern substitution, which may or may not |
… |
… |
|
3281 | 3281 | present in Sage are determined by Maxima's subst |
3282 | 3282 | command. Sometimes patterns are not replaced even though |
3283 | 3283 | one would think they should be - see examples below. |
3284 | | |
3285 | | EXAMPLES:: |
3286 | | |
| 3284 | |
| 3285 | EXAMPLES:: |
| 3286 | |
3287 | 3287 | sage: f = x^2 + 1 |
3288 | 3288 | sage: f.subs_expr(x^2 == x) |
3289 | 3289 | x + 1 |
3290 | | |
3291 | | :: |
3292 | | |
| 3290 | |
| 3291 | :: |
| 3292 | |
3293 | 3293 | sage: var('x,y,z'); f = x^3 + y^2 + z |
3294 | 3294 | (x, y, z) |
3295 | 3295 | sage: f.subs_expr(x^3 == y^2, z == 1) |
3296 | 3296 | 2*y^2 + 1 |
3297 | 3297 | |
3298 | 3298 | Or the same thing giving the substitutions as a dictionary:: |
3299 | | |
| 3299 | |
3300 | 3300 | sage: f.subs_expr({x^3:y^2, z:1}) |
3301 | 3301 | 2*y^2 + 1 |
3302 | | |
| 3302 | |
3303 | 3303 | sage: f = x^2 + x^4 |
3304 | 3304 | sage: f.subs_expr(x^2 == x) |
3305 | 3305 | x^4 + x |
3306 | 3306 | sage: f = cos(x^2) + sin(x^2) |
3307 | 3307 | sage: f.subs_expr(x^2 == x) |
3308 | 3308 | sin(x) + cos(x) |
3309 | | |
3310 | | :: |
3311 | | |
| 3309 | |
| 3310 | :: |
| 3311 | |
3312 | 3312 | sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t |
3313 | 3313 | sage: f.subs_expr(y^2 == t) |
3314 | 3314 | (x, y, t) |--> x^2 + 2*t + sin(y) + cos(x) |
3315 | | |
| 3315 | |
3316 | 3316 | The following seems really weird, but it *is* what Maple does:: |
3317 | | |
| 3317 | |
3318 | 3318 | sage: f.subs_expr(x^2 + y^2 == t) |
3319 | 3319 | (x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x) |
3320 | 3320 | sage: maple.eval('subs(x^2 + y^2 = t, cos(x) + sin(y) + x^2 + y^2 + t)') # optional requires maple |
… |
… |
|
3322 | 3322 | sage: maxima.quit() |
3323 | 3323 | sage: maxima.eval('cos(x) + sin(y) + x^2 + y^2 + t, x^2 + y^2 = t') |
3324 | 3324 | 'sin(y)+y^2+cos(x)+x^2+t' |
3325 | | |
| 3325 | |
3326 | 3326 | Actually Mathematica does something that makes more sense:: |
3327 | | |
| 3327 | |
3328 | 3328 | sage: mathematica.eval('Cos[x] + Sin[y] + x^2 + y^2 + t /. x^2 + y^2 -> t') # optional -- requires mathematica |
3329 | 3329 | 2 t + Cos[x] + Sin[y] |
3330 | 3330 | """ |
… |
… |
|
3355 | 3355 | """ |
3356 | 3356 | from sage.symbolic.expression_conversions import SubstituteFunction |
3357 | 3357 | return SubstituteFunction(self, original, new)() |
3358 | | |
| 3358 | |
3359 | 3359 | def __call__(self, *args, **kwds): |
3360 | 3360 | """ |
3361 | 3361 | Calls the :meth:`subs` on this expression. |
3362 | 3362 | |
3363 | 3363 | EXAMPLES:: |
3364 | | |
| 3364 | |
3365 | 3365 | sage: var('x,y,z') |
3366 | 3366 | (x, y, z) |
3367 | 3367 | sage: (x+y)(x=z^2, y=x^y) |
… |
… |
|
3374 | 3374 | Return sorted tuple of variables that occur in this expression. |
3375 | 3375 | |
3376 | 3376 | EXAMPLES:: |
3377 | | |
| 3377 | |
3378 | 3378 | sage: (x,y,z) = var('x,y,z') |
3379 | 3379 | sage: (x+y).variables() |
3380 | 3380 | (x, y) |
… |
… |
|
3386 | 3386 | (x, y, z) |
3387 | 3387 | |
3388 | 3388 | """ |
3389 | | from sage.symbolic.ring import SR |
| 3389 | from sage.symbolic.ring import SR |
3390 | 3390 | cdef GExSet sym_set |
3391 | 3391 | g_list_symbols(self._gobj, sym_set) |
3392 | 3392 | res = [] |
… |
… |
|
3431 | 3431 | 1 |
3432 | 3432 | |
3433 | 3433 | :: |
3434 | | |
| 3434 | |
3435 | 3435 | sage: x,y,z = var('x,y,z') |
3436 | 3436 | sage: (x+y).number_of_arguments() |
3437 | 3437 | 2 |
… |
… |
|
3443 | 3443 | 3 |
3444 | 3444 | sage: (sin(x+y)).number_of_arguments() |
3445 | 3445 | 2 |
3446 | | |
3447 | | :: |
3448 | | |
| 3446 | |
| 3447 | :: |
| 3448 | |
3449 | 3449 | sage: ( 2^(8/9) - 2^(1/9) )(x-1) |
3450 | 3450 | Traceback (most recent call last): |
3451 | 3451 | ... |
… |
… |
|
3458 | 3458 | Returns the number of arguments of this expression. |
3459 | 3459 | |
3460 | 3460 | EXAMPLES:: |
3461 | | |
| 3461 | |
3462 | 3462 | sage: var('a,b,c,x,y') |
3463 | 3463 | (a, b, c, x, y) |
3464 | 3464 | sage: a.number_of_operands() |
… |
… |
|
3479 | 3479 | Returns the number of arguments of this expression. |
3480 | 3480 | |
3481 | 3481 | EXAMPLES:: |
3482 | | |
| 3482 | |
3483 | 3483 | sage: var('a,b,c,x,y') |
3484 | 3484 | (a, b, c, x, y) |
3485 | 3485 | sage: len(a) |
… |
… |
|
3498 | 3498 | Returns a list containing the operands of this expression. |
3499 | 3499 | |
3500 | 3500 | EXAMPLES:: |
3501 | | |
| 3501 | |
3502 | 3502 | sage: var('a,b,c,x,y') |
3503 | 3503 | (a, b, c, x, y) |
3504 | 3504 | sage: (a^2 + b^2 + (x+y)^2).operands() |
… |
… |
|
3517 | 3517 | Returns the topmost operator in this expression. |
3518 | 3518 | |
3519 | 3519 | EXAMPLES:: |
3520 | | |
| 3520 | |
3521 | 3521 | sage: x,y,z = var('x,y,z') |
3522 | 3522 | sage: (x+y).operator() |
3523 | 3523 | <built-in function add> |
… |
… |
|
3551 | 3551 | D[0](f)(x) |
3552 | 3552 | sage: a.operator() |
3553 | 3553 | D[0](f) |
3554 | | |
| 3554 | |
3555 | 3555 | TESTS: |
3556 | 3556 | sage: (x <= y).operator() |
3557 | 3557 | <built-in function le> |
… |
… |
|
3614 | 3614 | def __index__(self): |
3615 | 3615 | """ |
3616 | 3616 | EXAMPLES:: |
3617 | | |
| 3617 | |
3618 | 3618 | sage: a = range(10) |
3619 | 3619 | sage: a[:SR(5)] |
3620 | 3620 | [0, 1, 2, 3, 4] |
3621 | 3621 | """ |
3622 | | return int(self._integer_()) |
| 3622 | return int(self._integer_()) |
3623 | 3623 | |
3624 | 3624 | def iterator(self): |
3625 | 3625 | """ |
3626 | 3626 | Return an iterator over the arguments of this expression. |
3627 | 3627 | |
3628 | 3628 | EXAMPLES:: |
3629 | | |
| 3629 | |
3630 | 3630 | sage: x,y,z = var('x,y,z') |
3631 | 3631 | sage: list((x+y+z).iterator()) |
3632 | 3632 | [x, y, z] |
… |
… |
|
3640 | 3640 | ## def __getitem__(self, ind): |
3641 | 3641 | ## """ |
3642 | 3642 | ## EXAMPLES:: |
3643 | | |
| 3643 | |
3644 | 3644 | ## sage: x,y,z = var('x,y,z') |
3645 | 3645 | ## sage: e = x + x*y + z^y + 3*y*z; e |
3646 | 3646 | ## x*y + 3*y*z + z^y + x |
… |
… |
|
3727 | 3727 | TESTS: |
3728 | 3728 | |
3729 | 3729 | We test the evaluation of different infinities available in Pynac:: |
3730 | | |
| 3730 | |
3731 | 3731 | sage: t = x - oo; t |
3732 | 3732 | -Infinity |
3733 | 3733 | sage: t.n() |
… |
… |
|
3764 | 3764 | res = x.pyobject() |
3765 | 3765 | else: |
3766 | 3766 | raise TypeError, "cannot evaluate symbolic expresssion numerically" |
3767 | | |
| 3767 | |
3768 | 3768 | # Important -- the we get might not be a valid output for numerical_approx in |
3769 | 3769 | # the case when one gets infinity. |
3770 | 3770 | if isinstance(res, AnInfinity): |
… |
… |
|
3781 | 3781 | result. This may lead to misleading results. |
3782 | 3782 | |
3783 | 3783 | EXAMPLES:: |
3784 | | |
| 3784 | |
3785 | 3785 | sage: t = sqrt(Integer('1'*1000)).round(); t |
3786 | 3786 | 33333333333333330562872877837571095953933917311168389275365130348599729268937180234955282892214983624063325870179809380946753270304893256291223685906477377407805362767048122713900447143840625646189582982153140391592656164907437770144711391823061825859553426442610175266854303816282031207413258045024789468633297982051760989540057817134748449866609872506236671598268343900233203388804404405970909678015833968186200327087593697947284709714538428657648875107834928345435181446453242319048564666814955520 |
3787 | 3787 | |
… |
… |
|
3801 | 3801 | EXAMPLES: |
3802 | 3802 | |
3803 | 3803 | We will use several symbolic variables in the examples below:: |
3804 | | |
| 3804 | |
3805 | 3805 | sage: var('x, y, z, t, a, w, n') |
3806 | 3806 | (x, y, z, t, a, w, n) |
3807 | | |
3808 | | :: |
3809 | | |
| 3807 | |
| 3808 | :: |
| 3809 | |
3810 | 3810 | sage: u = sin(x) + x*cos(y) |
3811 | 3811 | sage: g = u.function(x,y) |
3812 | 3812 | sage: g(x,y) |
… |
… |
|
3815 | 3815 | t*cos(z) + sin(t) |
3816 | 3816 | sage: g(x^2, x^y) |
3817 | 3817 | x^2*cos(x^y) + sin(x^2) |
3818 | | |
3819 | | :: |
3820 | | |
| 3818 | |
| 3819 | :: |
| 3820 | |
3821 | 3821 | sage: f = (x^2 + sin(a*w)).function(a,x,w); f |
3822 | 3822 | (a, x, w) |--> x^2 + sin(a*w) |
3823 | 3823 | sage: f(1,2,3) |
3824 | 3824 | sin(3) + 4 |
3825 | | |
| 3825 | |
3826 | 3826 | Using the :meth:`function` method we can obtain the above function |
3827 | 3827 | `f`, but viewed as a function of different variables:: |
3828 | | |
| 3828 | |
3829 | 3829 | sage: h = f.function(w,a); h |
3830 | 3830 | (w, a) |--> x^2 + sin(a*w) |
3831 | | |
| 3831 | |
3832 | 3832 | This notation also works:: |
3833 | | |
| 3833 | |
3834 | 3834 | sage: h(w,a) = f |
3835 | 3835 | sage: h |
3836 | 3836 | (w, a) |--> x^2 + sin(a*w) |
3837 | | |
| 3837 | |
3838 | 3838 | You can even make a symbolic expression `f` into a function |
3839 | 3839 | by writing ``f(x,y) = f``:: |
3840 | | |
| 3840 | |
3841 | 3841 | sage: f = x^n + y^n; f |
3842 | 3842 | x^n + y^n |
3843 | 3843 | sage: f(x,y) = f |
… |
… |
|
3865 | 3865 | def coefficient(self, s, int n=1): |
3866 | 3866 | """ |
3867 | 3867 | Returns the coefficient of `s^n` in this symbolic expression. |
3868 | | |
| 3868 | |
3869 | 3869 | INPUT: |
3870 | 3870 | |
3871 | 3871 | - ``s`` - expression |
… |
… |
|
3880 | 3880 | is not done automatically. |
3881 | 3881 | |
3882 | 3882 | EXAMPLES:: |
3883 | | |
| 3883 | |
3884 | 3884 | sage: var('x,y,a') |
3885 | 3885 | (x, y, a) |
3886 | 3886 | sage: f = 100 + a*x + x^3*sin(x*y) + x*y + x/y + 2*sin(x*y)/x; f |
… |
… |
|
3906 | 3906 | |
3907 | 3907 | sage: var('a, x, y, z') |
3908 | 3908 | (a, x, y, z) |
3909 | | sage: f = (a*sqrt(2))*x^2 + sin(y)*x^(1/2) + z^z |
| 3909 | sage: f = (a*sqrt(2))*x^2 + sin(y)*x^(1/2) + z^z |
3910 | 3910 | sage: f.coefficient(sin(y)) |
3911 | | sqrt(x) |
| 3911 | sqrt(x) |
3912 | 3912 | sage: f.coefficient(x^2) |
3913 | 3913 | sqrt(2)*a |
3914 | 3914 | sage: f.coefficient(x^(1/2)) |
… |
… |
|
3927 | 3927 | def coefficients(self, x=None): |
3928 | 3928 | r""" |
3929 | 3929 | Coefficients of this symbolic expression as a polynomial in x. |
3930 | | |
3931 | | INPUT: |
3932 | | |
| 3930 | |
| 3931 | INPUT: |
| 3932 | |
3933 | 3933 | - ``x`` - optional variable |
3934 | | |
3935 | | OUTPUT: |
| 3934 | |
| 3935 | OUTPUT: |
3936 | 3936 | |
3937 | 3937 | - A list of pairs (expr, n), where expr is a symbolic |
3938 | 3938 | expression and n is a power. |
3939 | | |
3940 | | EXAMPLES:: |
3941 | | |
| 3939 | |
| 3940 | EXAMPLES:: |
| 3941 | |
3942 | 3942 | sage: var('x, y, a') |
3943 | 3943 | (x, y, a) |
3944 | 3944 | sage: p = x^3 - (x-3)*(x^2+x) + 1 |
… |
… |
|
3950 | 3950 | [[x^2 + x + 1, 0], [-2*sqrt(2)*x, 1], [2, 2]] |
3951 | 3951 | sage: p.coefficients(x) |
3952 | 3952 | [[2*a^2 + 1, 0], [-2*sqrt(2)*a + 1, 1], [1, 2]] |
3953 | | |
| 3953 | |
3954 | 3954 | A polynomial with wacky exponents:: |
3955 | | |
| 3955 | |
3956 | 3956 | sage: p = (17/3*a)*x^(3/2) + x*y + 1/x + x^x |
3957 | 3957 | sage: p.coefficients(x) |
3958 | 3958 | [[1, -1], [x^x, 0], [y, 1], [17/3*a, 3/2]] |
… |
… |
|
3969 | 3969 | return S[1:] |
3970 | 3970 | |
3971 | 3971 | coeffs = coefficients |
3972 | | |
| 3972 | |
3973 | 3973 | def leading_coefficient(self, s): |
3974 | 3974 | """ |
3975 | 3975 | Return the leading coefficient of s in self. |
3976 | | |
3977 | | EXAMPLES:: |
3978 | | |
| 3976 | |
| 3977 | EXAMPLES:: |
| 3978 | |
3979 | 3979 | sage: var('x,y,a') |
3980 | 3980 | (x, y, a) |
3981 | 3981 | sage: f = 100 + a*x + x^3*sin(x*y) + x*y + x/y + 2*sin(x*y)/x; f |
… |
… |
|
3991 | 3991 | return new_Expression_from_GEx(self._parent, self._gobj.lcoeff(ss._gobj)) |
3992 | 3992 | |
3993 | 3993 | leading_coeff = leading_coefficient |
3994 | | |
| 3994 | |
3995 | 3995 | def trailing_coefficient(self, s): |
3996 | 3996 | """ |
3997 | 3997 | Return the trailing coefficient of s in self, i.e., the coefficient |
3998 | 3998 | of the smallest power of s in self. |
3999 | | |
4000 | | EXAMPLES:: |
4001 | | |
| 3999 | |
| 4000 | EXAMPLES:: |
| 4001 | |
4002 | 4002 | sage: var('x,y,a') |
4003 | 4003 | (x, y, a) |
4004 | 4004 | sage: f = 100 + a*x + x^3*sin(x*y) + x*y + x/y + 2*sin(x*y)/x; f |
… |
… |
|
4014 | 4014 | return new_Expression_from_GEx(self._parent, self._gobj.tcoeff(ss._gobj)) |
4015 | 4015 | |
4016 | 4016 | trailing_coeff = trailing_coefficient |
4017 | | |
| 4017 | |
4018 | 4018 | def low_degree(self, s): |
4019 | 4019 | """ |
4020 | 4020 | Return the exponent of the lowest nonpositive power of s in self. |
… |
… |
|
4024 | 4024 | - an integer <= 0. |
4025 | 4025 | |
4026 | 4026 | EXAMPLES:: |
4027 | | |
| 4027 | |
4028 | 4028 | sage: var('x,y,a') |
4029 | 4029 | (x, y, a) |
4030 | 4030 | sage: f = 100 + a*x + x^3*sin(x*y) + x*y + x/y^10 + 2*sin(x*y)/x; f |
… |
… |
|
4050 | 4050 | - an integer >= 0. |
4051 | 4051 | |
4052 | 4052 | EXAMPLES:: |
4053 | | |
| 4053 | |
4054 | 4054 | sage: var('x,y,a') |
4055 | 4055 | (x, y, a) |
4056 | 4056 | sage: f = 100 + a*x + x^3*sin(x*y) + x*y + x/y^10 + 2*sin(x*y)/x; f |
… |
… |
|
4072 | 4072 | Express this symbolic expression as a polynomial in *x*. If |
4073 | 4073 | this is not a polynomial in *x*, then some coefficients may be |
4074 | 4074 | functions of *x*. |
4075 | | |
| 4075 | |
4076 | 4076 | .. warning:: |
4077 | 4077 | |
4078 | 4078 | This is different from :meth:`polynomial` which returns |
4079 | 4079 | a Sage polynomial over a given base ring. |
4080 | | |
4081 | | EXAMPLES:: |
4082 | | |
| 4080 | |
| 4081 | EXAMPLES:: |
| 4082 | |
4083 | 4083 | sage: var('a, x') |
4084 | 4084 | (a, x) |
4085 | 4085 | sage: p = expand((x-a*sqrt(2))^2 + x + 1); p |
… |
… |
|
4087 | 4087 | sage: p.poly(a) |
4088 | 4088 | -2*sqrt(2)*a*x + 2*a^2 + x^2 + x + 1 |
4089 | 4089 | sage: bool(p.poly(a) == (x-a*sqrt(2))^2 + x + 1) |
4090 | | True |
| 4090 | True |
4091 | 4091 | sage: p.poly(x) |
4092 | 4092 | -(2*sqrt(2)*a - 1)*x + 2*a^2 + x^2 + 1 |
4093 | 4093 | """ |
… |
… |
|
4121 | 4121 | r""" |
4122 | 4122 | Return this symbolic expression as an algebraic polynomial |
4123 | 4123 | over the given base ring, if possible. |
4124 | | |
| 4124 | |
4125 | 4125 | The point of this function is that it converts purely symbolic |
4126 | 4126 | polynomials into optimised algebraic polynomials over a given |
4127 | 4127 | base ring. |
4128 | | |
| 4128 | |
4129 | 4129 | .. warning:: |
4130 | 4130 | |
4131 | 4131 | This is different from meth:`poly` which is used to rewrite |
4132 | 4132 | self as a polynomial in terms of one of the variables. |
4133 | | |
4134 | | INPUT: |
4135 | | |
| 4133 | |
| 4134 | INPUT: |
| 4135 | |
4136 | 4136 | - ``base_ring`` - a ring |
4137 | | |
4138 | | EXAMPLES:: |
4139 | | |
| 4137 | |
| 4138 | EXAMPLES:: |
| 4139 | |
4140 | 4140 | sage: f = x^2 -2/3*x + 1 |
4141 | 4141 | sage: f.polynomial(QQ) |
4142 | 4142 | x^2 - 2/3*x + 1 |
4143 | 4143 | sage: f.polynomial(GF(19)) |
4144 | 4144 | x^2 + 12*x + 1 |
4145 | | |
| 4145 | |
4146 | 4146 | Polynomials can be useful for getting the coefficients of an |
4147 | 4147 | expression:: |
4148 | | |
| 4148 | |
4149 | 4149 | sage: g = 6*x^2 - 5 |
4150 | 4150 | sage: g.coefficients() |
4151 | 4151 | [[-5, 0], [6, 2]] |
… |
… |
|
4153 | 4153 | [-5, 0, 6] |
4154 | 4154 | sage: g.polynomial(QQ).dict() |
4155 | 4155 | {0: -5, 2: 6} |
4156 | | |
4157 | | :: |
4158 | | |
| 4156 | |
| 4157 | :: |
| 4158 | |
4159 | 4159 | sage: f = x^2*e + x + pi/e |
4160 | 4160 | sage: f.polynomial(RDF) |
4161 | 4161 | 2.71828182846*x^2 + x + 1.15572734979 |
4162 | 4162 | sage: g = f.polynomial(RR); g |
4163 | 4163 | 2.71828182845905*x^2 + x + 1.15572734979092 |
4164 | 4164 | sage: g.parent() |
4165 | | Univariate Polynomial Ring in x over Real Field with 53 bits of precision |
| 4165 | Univariate Polynomial Ring in x over Real Field with 53 bits of precision |
4166 | 4166 | sage: f.polynomial(RealField(100)) |
4167 | 4167 | 2.7182818284590452353602874714*x^2 + x + 1.1557273497909217179100931833 |
4168 | 4168 | sage: f.polynomial(CDF) |
4169 | 4169 | 2.71828182846*x^2 + x + 1.15572734979 |
4170 | 4170 | sage: f.polynomial(CC) |
4171 | 4171 | 2.71828182845905*x^2 + x + 1.15572734979092 |
4172 | | |
| 4172 | |
4173 | 4173 | We coerce a multivariate polynomial with complex symbolic |
4174 | 4174 | coefficients:: |
4175 | | |
| 4175 | |
4176 | 4176 | sage: x, y, n = var('x, y, n') |
4177 | 4177 | sage: f = pi^3*x - y^2*e - I; f |
4178 | 4178 | pi^3*x - y^2*e - I |
… |
… |
|
4182 | 4182 | (-2.71828182845905)*y^2 + 31.0062766802998*x - 1.00000000000000*I |
4183 | 4183 | sage: f.polynomial(ComplexField(70)) |
4184 | 4184 | (-2.7182818284590452354)*y^2 + 31.006276680299820175*x - 1.0000000000000000000*I |
4185 | | |
| 4185 | |
4186 | 4186 | Another polynomial:: |
4187 | | |
| 4187 | |
4188 | 4188 | sage: f = sum((e*I)^n*x^n for n in range(5)); f |
4189 | 4189 | x^4*e^4 - I*x^3*e^3 - x^2*e^2 + I*x*e + 1 |
4190 | 4190 | sage: f.polynomial(CDF) |
4191 | 4191 | 54.5981500331*x^4 - 20.0855369232*I*x^3 - 7.38905609893*x^2 + 2.71828182846*I*x + 1.0 |
4192 | 4192 | sage: f.polynomial(CC) |
4193 | 4193 | 54.5981500331442*x^4 - 20.0855369231877*I*x^3 - 7.38905609893065*x^2 + 2.71828182845905*I*x + 1.00000000000000 |
4194 | | |
| 4194 | |
4195 | 4195 | A multivariate polynomial over a finite field:: |
4196 | | |
| 4196 | |
4197 | 4197 | sage: f = (3*x^5 - 5*y^5)^7; f |
4198 | 4198 | (3*x^5 - 5*y^5)^7 |
4199 | 4199 | sage: g = f.polynomial(GF(7)); g |
… |
… |
|
4203 | 4203 | """ |
4204 | 4204 | from sage.symbolic.expression_conversions import polynomial |
4205 | 4205 | return polynomial(self, base_ring=base_ring, ring=ring) |
4206 | | |
| 4206 | |
4207 | 4207 | def _polynomial_(self, R): |
4208 | 4208 | """ |
4209 | 4209 | Coerce this symbolic expression to a polynomial in `R`. |
4210 | | |
4211 | | EXAMPLES:: |
4212 | | |
| 4210 | |
| 4211 | EXAMPLES:: |
| 4212 | |
4213 | 4213 | sage: var('x,y,z,w') |
4214 | 4214 | (x, y, z, w) |
4215 | | |
4216 | | :: |
4217 | | |
| 4215 | |
| 4216 | :: |
| 4217 | |
4218 | 4218 | sage: R = QQ[x,y,z] |
4219 | 4219 | sage: R(x^2 + y) |
4220 | 4220 | x^2 + y |
… |
… |
|
4224 | 4224 | sage: R = GF(7)[z] |
4225 | 4225 | sage: R(z^3 + 10*z) |
4226 | 4226 | z^3 + 3*z |
4227 | | |
| 4227 | |
4228 | 4228 | .. note:: |
4229 | 4229 | |
4230 | 4230 | If the base ring of the polynomial ring is the symbolic ring, |
4231 | 4231 | then a constant polynomial is always returned. |
4232 | | |
4233 | | :: |
4234 | | |
| 4232 | |
| 4233 | :: |
| 4234 | |
4235 | 4235 | sage: R = SR[x] |
4236 | 4236 | sage: a = R(sqrt(2) + x^3 + y) |
4237 | 4237 | sage: a |
… |
… |
|
4240 | 4240 | <class 'sage.rings.polynomial.polynomial_element_generic.Polynomial_generic_dense_field'> |
4241 | 4241 | sage: a.degree() |
4242 | 4242 | 0 |
4243 | | |
| 4243 | |
4244 | 4244 | We coerce to a double precision complex polynomial ring:: |
4245 | | |
| 4245 | |
4246 | 4246 | sage: f = e*x^3 + pi*y^3 + sqrt(2) + I; f |
4247 | 4247 | pi*y^3 + x^3*e + sqrt(2) + I |
4248 | 4248 | sage: R = CDF[x,y] |
4249 | 4249 | sage: R(f) |
4250 | 4250 | 2.71828182846*x^3 + 3.14159265359*y^3 + 1.41421356237 + 1.0*I |
4251 | | |
| 4251 | |
4252 | 4252 | We coerce to a higher-precision polynomial ring |
4253 | | |
4254 | | :: |
4255 | | |
| 4253 | |
| 4254 | :: |
| 4255 | |
4256 | 4256 | sage: R = ComplexField(100)[x,y] |
4257 | 4257 | sage: R(f) |
4258 | 4258 | 2.7182818284590452353602874714*x^3 + 3.1415926535897932384626433833*y^3 + 1.4142135623730950488016887242 + 1.0000000000000000000000000000*I |
… |
… |
|
4276 | 4276 | This shows that the issue at trac #4246 is fixed (attempting to |
4277 | 4277 | coerce an expression containing at least one variable that's not in |
4278 | 4278 | `R` raises an error):: |
4279 | | |
| 4279 | |
4280 | 4280 | sage: x, y = var('x y') |
4281 | 4281 | sage: S = PolynomialRing(Integers(4), 1, 'x') |
4282 | 4282 | sage: S(x) |
… |
… |
|
4296 | 4296 | """ |
4297 | 4297 | from sage.symbolic.all import SR |
4298 | 4298 | from sage.rings.all import is_MPolynomialRing |
4299 | | |
| 4299 | |
4300 | 4300 | base_ring = R.base_ring() |
4301 | 4301 | if base_ring == SR: |
4302 | 4302 | if is_MPolynomialRing(R): |
… |
… |
|
4304 | 4304 | else: |
4305 | 4305 | return R([self]) |
4306 | 4306 | return self.polynomial(None, ring=R) |
4307 | | |
| 4307 | |
4308 | 4308 | def power_series(self, base_ring): |
4309 | 4309 | """ |
4310 | 4310 | Return algebraic power series associated to this symbolic |
4311 | 4311 | expression, which must be a polynomial in one variable, with |
4312 | 4312 | coefficients coercible to the base ring. |
4313 | | |
| 4313 | |
4314 | 4314 | The power series is truncated one more than the degree. |
4315 | | |
4316 | | EXAMPLES:: |
4317 | | |
| 4315 | |
| 4316 | EXAMPLES:: |
| 4317 | |
4318 | 4318 | sage: theta = var('theta') |
4319 | 4319 | sage: f = theta^3 + (1/3)*theta - 17/3 |
4320 | 4320 | sage: g = f.power_series(QQ); g |
… |
… |
|
4341 | 4341 | http://trac.sagemath.org/sage_trac/ticket/694 on Ginac dies |
4342 | 4342 | after about 10 seconds. Singular easily does that GCD now. |
4343 | 4343 | Since Ginac only handles poly gcd over QQ, we should change |
4344 | | ginac itself to use Singular. |
4345 | | |
4346 | | EXAMPLES:: |
4347 | | |
| 4344 | ginac itself to use Singular. |
| 4345 | |
| 4346 | EXAMPLES:: |
| 4347 | |
4348 | 4348 | sage: var('x,y') |
4349 | 4349 | (x, y) |
4350 | 4350 | sage: SR(10).gcd(SR(15)) |
… |
… |
|
4379 | 4379 | OUTPUT: |
4380 | 4380 | |
4381 | 4381 | - expression |
4382 | | |
4383 | | EXAMPLES:: |
4384 | | |
| 4382 | |
| 4383 | EXAMPLES:: |
| 4384 | |
4385 | 4385 | sage: var('x,y,z') |
4386 | 4386 | (x, y, z) |
4387 | 4387 | sage: f = 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2 + x^2*y^2*z^2 |
… |
… |
|
4401 | 4401 | def collect_common_factors(self): |
4402 | 4402 | """ |
4403 | 4403 | EXAMPLES:: |
4404 | | |
| 4404 | |
4405 | 4405 | sage: var('x') |
4406 | 4406 | x |
4407 | 4407 | sage: (x/(x^2 + x)).collect_common_factors() |
… |
… |
|
4415 | 4415 | def __abs__(self): |
4416 | 4416 | """ |
4417 | 4417 | Return the absolute value of this expression. |
4418 | | |
4419 | | EXAMPLES:: |
4420 | | |
| 4418 | |
| 4419 | EXAMPLES:: |
| 4420 | |
4421 | 4421 | sage: var('x, y') |
4422 | 4422 | (x, y) |
4423 | 4423 | |
4424 | 4424 | The absolute value of a symbolic expression:: |
4425 | | |
| 4425 | |
4426 | 4426 | sage: abs(x^2+y^2) |
4427 | 4427 | abs(x^2 + y^2) |
4428 | 4428 | |
4429 | 4429 | The absolute value of a number in the symbolic ring:: |
4430 | | |
| 4430 | |
4431 | 4431 | sage: abs(SR(-5)) |
4432 | 4432 | 5 |
4433 | 4433 | sage: type(abs(SR(-5))) |
… |
… |
|
4439 | 4439 | """ |
4440 | 4440 | Return the value of the Heaviside step function, which is 0 for |
4441 | 4441 | negative x, 1/2 for 0, and 1 for positive x. |
4442 | | |
4443 | | EXAMPLES:: |
4444 | | |
| 4442 | |
| 4443 | EXAMPLES:: |
| 4444 | |
4445 | 4445 | sage: x = var('x') |
4446 | 4446 | sage: SR(1.5).step() |
4447 | 4447 | 1 |
… |
… |
|
4461 | 4461 | symbolic expression. |
4462 | 4462 | |
4463 | 4463 | It can be somewhat arbitrary when self is not real. |
4464 | | |
| 4464 | |
4465 | 4465 | EXAMPLES: |
4466 | 4466 | sage: x = var('x') |
4467 | 4467 | sage: SR(-2).csgn() |
… |
… |
|
4482 | 4482 | def conjugate(self): |
4483 | 4483 | """ |
4484 | 4484 | Return the complex conjugate of this symbolic expression. |
4485 | | |
4486 | | EXAMPLES:: |
4487 | | |
4488 | | sage: a = 1 + 2*I |
| 4485 | |
| 4486 | EXAMPLES:: |
| 4487 | |
| 4488 | sage: a = 1 + 2*I |
4489 | 4489 | sage: a.conjugate() |
4490 | 4490 | -2*I + 1 |
4491 | 4491 | sage: a = sqrt(2) + 3^(1/3)*I; a |
… |
… |
|
4512 | 4512 | r""" |
4513 | 4513 | The complex norm of this symbolic expression, i.e., |
4514 | 4514 | the expression times its complex conjugate. |
4515 | | |
4516 | | EXAMPLES:: |
4517 | | |
4518 | | sage: a = 1 + 2*I |
| 4515 | |
| 4516 | EXAMPLES:: |
| 4517 | |
| 4518 | sage: a = 1 + 2*I |
4519 | 4519 | sage: a.norm() |
4520 | 4520 | 5 |
4521 | 4521 | sage: a = sqrt(2) + 3^(1/3)*I; a |
… |
… |
|
4534 | 4534 | Return the real part of this symbolic expression. |
4535 | 4535 | |
4536 | 4536 | EXAMPLES:: |
4537 | | |
| 4537 | |
4538 | 4538 | sage: x = var('x') |
4539 | 4539 | sage: x.real_part() |
4540 | 4540 | real_part(x) |
… |
… |
|
4561 | 4561 | |
4562 | 4562 | sage: sqrt(-2).imag_part() |
4563 | 4563 | sqrt(2) |
4564 | | |
| 4564 | |
4565 | 4565 | We simplify `\ln(\exp(z))` to `z`. This should only |
4566 | 4566 | be for `-\pi<{\rm Im}(z)<=\pi`, but Maxima does not |
4567 | 4567 | have a symbolic imaginary part function, so we cannot |
… |
… |
|
4574 | 4574 | sage: f.simplify() |
4575 | 4575 | z |
4576 | 4576 | sage: forget() |
4577 | | |
| 4577 | |
4578 | 4578 | A more symbolic example:: |
4579 | | |
| 4579 | |
4580 | 4580 | sage: var('a, b') |
4581 | 4581 | (a, b) |
4582 | 4582 | sage: f = log(a + b*I) |
… |
… |
|
4584 | 4584 | arctan2(real_part(b) + imag_part(a), real_part(a) - imag_part(b)) |
4585 | 4585 | |
4586 | 4586 | TESTS:: |
4587 | | |
| 4587 | |
4588 | 4588 | sage: x = var('x') |
4589 | 4589 | sage: x.imag_part() |
4590 | 4590 | imag_part(x) |
… |
… |
|
4648 | 4648 | def cos(self): |
4649 | 4649 | """ |
4650 | 4650 | Return the cosine of self. |
4651 | | |
| 4651 | |
4652 | 4652 | EXAMPLES:: |
4653 | 4653 | |
4654 | 4654 | sage: var('x, y') |
… |
… |
|
4668 | 4668 | sage: SR(RR(1)).cos().n() |
4669 | 4669 | 0.540302305868140 |
4670 | 4670 | sage: SR(float(1)).cos().n() |
4671 | | 0.540302305868140 |
| 4671 | 0.540302305868140 |
4672 | 4672 | |
4673 | 4673 | TESTS:: |
4674 | 4674 | |
… |
… |
|
4723 | 4723 | """ |
4724 | 4724 | Return the arcsin of x, i.e., the number y between -pi and pi |
4725 | 4725 | such that sin(y) == x. |
4726 | | |
| 4726 | |
4727 | 4727 | EXAMPLES:: |
4728 | 4728 | |
4729 | 4729 | sage: x.arcsin() |
… |
… |
|
4816 | 4816 | def arctan2(self, x): |
4817 | 4817 | """ |
4818 | 4818 | Return the inverse of the 2-variable tan function on self and x. |
4819 | | |
| 4819 | |
4820 | 4820 | EXAMPLES:: |
4821 | 4821 | |
4822 | 4822 | sage: var('x,y') |
… |
… |
|
4862 | 4862 | 0.0 |
4863 | 4863 | sage: SR(0).arctan2(0) |
4864 | 4864 | 0 |
4865 | | |
| 4865 | |
4866 | 4866 | sage: SR(I).arctan2(1) |
4867 | 4867 | arctan2(I, 1) |
4868 | 4868 | sage: SR(CDF(0,1)).arctan2(1) |
… |
… |
|
4900 | 4900 | r""" |
4901 | 4901 | Return sinh of self. |
4902 | 4902 | |
4903 | | We have $\sinh(x) = (e^{x} - e^{-x})/2$. |
| 4903 | We have $\sinh(x) = (e^{x} - e^{-x})/2$. |
4904 | 4904 | |
4905 | 4905 | EXAMPLES:: |
4906 | 4906 | |
… |
… |
|
4938 | 4938 | r""" |
4939 | 4939 | Return cosh of self. |
4940 | 4940 | |
4941 | | We have $\cosh(x) = (e^{x} + e^{-x})/2$. |
| 4941 | We have $\cosh(x) = (e^{x} + e^{-x})/2$. |
4942 | 4942 | |
4943 | 4943 | EXAMPLES:: |
4944 | 4944 | |
… |
… |
|
4974 | 4974 | r""" |
4975 | 4975 | Return tanh of self. |
4976 | 4976 | |
4977 | | We have $\tanh(x) = \sinh(x) / \cosh(x)$. |
| 4977 | We have $\tanh(x) = \sinh(x) / \cosh(x)$. |
4978 | 4978 | |
4979 | 4979 | EXAMPLES:: |
4980 | 4980 | |
… |
… |
|
5006 | 5006 | def arcsinh(self): |
5007 | 5007 | """ |
5008 | 5008 | Return the inverse hyperbolic sine of self. |
5009 | | |
| 5009 | |
5010 | 5010 | EXAMPLES:: |
5011 | 5011 | |
5012 | 5012 | sage: x.arcsinh() |
… |
… |
|
5183 | 5183 | def zeta(self): |
5184 | 5184 | """ |
5185 | 5185 | EXAMPLES:: |
5186 | | |
| 5186 | |
5187 | 5187 | sage: x, y = var('x, y') |
5188 | 5188 | sage: (x/y).zeta() |
5189 | 5189 | zeta(x/y) |
… |
… |
|
5198 | 5198 | sage: plot(lambda x: SR(x).zeta(), -10,10).show(ymin=-3,ymax=3) |
5199 | 5199 | |
5200 | 5200 | TESTS:: |
5201 | | |
| 5201 | |
5202 | 5202 | sage: t = SR(1).zeta(); t |
5203 | 5203 | zeta(1) |
5204 | 5204 | sage: t.n() |
… |
… |
|
5208 | 5208 | cdef GEx x = g_zeta(self._gobj) |
5209 | 5209 | _sig_off |
5210 | 5210 | return new_Expression_from_GEx(self._parent, x) |
5211 | | |
| 5211 | |
5212 | 5212 | def factorial(self): |
5213 | 5213 | """ |
5214 | 5214 | Return the factorial of self. |
5215 | | |
| 5215 | |
5216 | 5216 | OUTPUT: |
5217 | 5217 | symbolic expression |
5218 | | |
| 5218 | |
5219 | 5219 | EXAMPLES: |
5220 | 5220 | sage: var('x, y') |
5221 | 5221 | (x, y) |
… |
… |
|
5234 | 5234 | def binomial(self, k): |
5235 | 5235 | """ |
5236 | 5236 | Return binomial coefficient "self choose k". |
5237 | | |
| 5237 | |
5238 | 5238 | OUTPUT: |
5239 | 5239 | symbolic expression |
5240 | | |
| 5240 | |
5241 | 5241 | EXAMPLES: |
5242 | 5242 | sage: var('x, y') |
5243 | 5243 | (x, y) |
… |
… |
|
5260 | 5260 | |
5261 | 5261 | OUTPUT: |
5262 | 5262 | symbolic expression |
5263 | | |
| 5263 | |
5264 | 5264 | EXAMPLES: |
5265 | 5265 | sage: n = var('n') |
5266 | 5266 | sage: t = (17*n^3).Order(); t |
… |
… |
|
5273 | 5273 | def gamma(self): |
5274 | 5274 | """ |
5275 | 5275 | Return the Gamma function evaluated at self. |
5276 | | |
| 5276 | |
5277 | 5277 | EXAMPLES: |
5278 | 5278 | sage: x = var('x') |
5279 | 5279 | sage: x.gamma() |
… |
… |
|
5322 | 5322 | This is the logarithm of gamma of self, where |
5323 | 5323 | gamma is a complex function such that gamma(n) |
5324 | 5324 | equals factorial(n-1). |
5325 | | |
| 5325 | |
5326 | 5326 | EXAMPLES:: |
5327 | 5327 | |
5328 | 5328 | sage: x = var('x') |
… |
… |
|
5349 | 5349 | Return the default variable, which is by definition the first |
5350 | 5350 | variable in self, or `x` is there are no variables in self. |
5351 | 5351 | The result is cached. |
5352 | | |
5353 | | EXAMPLES:: |
5354 | | |
| 5352 | |
| 5353 | EXAMPLES:: |
| 5354 | |
5355 | 5355 | sage: sqrt(2).default_variable() |
5356 | | x |
| 5356 | x |
5357 | 5357 | sage: x, theta, a = var('x, theta, a') |
5358 | 5358 | sage: f = x^2 + theta^3 - a^x |
5359 | 5359 | sage: f.default_variable() |
5360 | 5360 | a |
5361 | | |
| 5361 | |
5362 | 5362 | Note that this is the first *variable*, not the first *argument*:: |
5363 | | |
| 5363 | |
5364 | 5364 | sage: f(theta, a, x) = a + theta^3 |
5365 | 5365 | sage: f.default_variable() |
5366 | 5366 | a |
… |
… |
|
5380 | 5380 | Returns a simplified version of this symbolic expression |
5381 | 5381 | by combining all terms with the same denominator into a single |
5382 | 5382 | term. |
5383 | | |
5384 | | EXAMPLES:: |
5385 | | |
| 5383 | |
| 5384 | EXAMPLES:: |
| 5385 | |
5386 | 5386 | sage: var('x, y, a, b, c') |
5387 | 5387 | (x, y, a, b, c) |
5388 | 5388 | sage: f = x*(x-1)/(x^2 - 7) + y^2/(x^2-7) + 1/(x+1) + b/a + c/a; f |
… |
… |
|
5391 | 5391 | ((x - 1)*x + y^2)/(x^2 - 7) + (b + c)/a + 1/(x + 1) |
5392 | 5392 | """ |
5393 | 5393 | return self.parent()(self._maxima_().combine()) |
5394 | | |
| 5394 | |
5395 | 5395 | def numerator(self): |
5396 | 5396 | """ |
5397 | 5397 | Returns the numerator of this symbolic expression. If the |
5398 | 5398 | expression is not a quotient, then this will return the |
5399 | 5399 | expression itself. |
5400 | | |
5401 | | EXAMPLES:: |
5402 | | |
| 5400 | |
| 5401 | EXAMPLES:: |
| 5402 | |
5403 | 5403 | sage: a, x, y = var('a,x,y') |
5404 | 5404 | sage: f = x*(x-a)/((x^2 - y)*(x-a)); f |
5405 | 5405 | x/(x^2 - y) |
… |
… |
|
5423 | 5423 | """ |
5424 | 5424 | Returns the denominator of this symbolic expression. If the |
5425 | 5425 | expression is not a quotient, then this will just return 1. |
5426 | | |
5427 | | EXAMPLES:: |
5428 | | |
| 5426 | |
| 5427 | EXAMPLES:: |
| 5428 | |
5429 | 5429 | sage: x, y, z, theta = var('x, y, z, theta') |
5430 | 5430 | sage: f = (sqrt(x) + sqrt(y) + sqrt(z))/(x^10 - y^10 - sqrt(theta)) |
5431 | 5431 | sage: f.denominator() |
… |
… |
|
5445 | 5445 | r""" |
5446 | 5446 | Return the partial fraction expansion of ``self`` with |
5447 | 5447 | respect to the given variable. |
5448 | | |
5449 | | INPUT: |
5450 | | |
5451 | | |
| 5448 | |
| 5449 | INPUT: |
| 5450 | |
| 5451 | |
5452 | 5452 | - ``var`` - variable name or string (default: first |
5453 | 5453 | variable) |
5454 | | |
5455 | | |
| 5454 | |
| 5455 | |
5456 | 5456 | OUTPUT: Symbolic expression |
5457 | | |
5458 | | EXAMPLES:: |
5459 | | |
| 5457 | |
| 5458 | EXAMPLES:: |
| 5459 | |
5460 | 5460 | sage: f = x^2/(x+1)^3 |
5461 | 5461 | sage: f.partial_fraction() |
5462 | 5462 | 1/(x + 1) - 2/(x + 1)^2 + 1/(x + 1)^3 |
5463 | 5463 | sage: f.partial_fraction() |
5464 | 5464 | 1/(x + 1) - 2/(x + 1)^2 + 1/(x + 1)^3 |
5465 | | |
| 5465 | |
5466 | 5466 | Notice that the first variable in the expression is used by |
5467 | 5467 | default:: |
5468 | | |
| 5468 | |
5469 | 5469 | sage: y = var('y') |
5470 | 5470 | sage: f = y^2/(y+1)^3 |
5471 | 5471 | sage: f.partial_fraction() |
5472 | 5472 | 1/(y + 1) - 2/(y + 1)^2 + 1/(y + 1)^3 |
5473 | | |
| 5473 | |
5474 | 5474 | sage: f = y^2/(y+1)^3 + x/(x-1)^3 |
5475 | 5475 | sage: f.partial_fraction() |
5476 | 5476 | y^2/(y^3 + 3*y^2 + 3*y + 1) + 1/(x - 1)^2 + 1/(x - 1)^3 |
5477 | | |
| 5477 | |
5478 | 5478 | You can explicitly specify which variable is used:: |
5479 | | |
| 5479 | |
5480 | 5480 | sage: f.partial_fraction(y) |
5481 | 5481 | x/(x^3 - 3*x^2 + 3*x - 1) + 1/(y + 1) - 2/(y + 1)^2 + 1/(y + 1)^3 |
5482 | 5482 | """ |
… |
… |
|
5487 | 5487 | def simplify(self): |
5488 | 5488 | """ |
5489 | 5489 | Returns a simplified version of this symbolic expression. |
5490 | | |
| 5490 | |
5491 | 5491 | .. note:: |
5492 | 5492 | |
5493 | 5493 | Currently, this just sends the expression to Maxima |
… |
… |
|
5507 | 5507 | x^(-a + 1)*sin(2) |
5508 | 5508 | """ |
5509 | 5509 | return self._parent(self._maxima_()) |
5510 | | |
| 5510 | |
5511 | 5511 | def simplify_full(self): |
5512 | 5512 | """ |
5513 | 5513 | Applies simplify_factorial, simplify_trig, simplify_rational, and simplify_radical |
5514 | 5514 | to self (in that order). |
5515 | | |
| 5515 | |
5516 | 5516 | ALIAS: simplify_full and full_simplify are the same. |
5517 | | |
5518 | | EXAMPLES:: |
5519 | | |
| 5517 | |
| 5518 | EXAMPLES:: |
| 5519 | |
5520 | 5520 | sage: a = log(8)/log(2) |
5521 | 5521 | sage: a.simplify_full() |
5522 | 5522 | 3 |
5523 | | |
5524 | | :: |
5525 | | |
| 5523 | |
| 5524 | :: |
| 5525 | |
5526 | 5526 | sage: f = sin(x)^2 + cos(x)^2 |
5527 | 5527 | sage: f.simplify_full() |
5528 | 5528 | 1 |
5529 | | |
5530 | | :: |
5531 | | |
| 5529 | |
| 5530 | :: |
| 5531 | |
5532 | 5532 | sage: f = sin(x/(x^2 + x)) |
5533 | 5533 | sage: f.simplify_full() |
5534 | 5534 | sin(1/(x + 1)) |
… |
… |
|
5552 | 5552 | full_simplify = simplify_full |
5553 | 5553 | |
5554 | 5554 | def simplify_trig(self,expand=True): |
5555 | | r""" |
| 5555 | r""" |
5556 | 5556 | Optionally expands and then employs identities such as |
5557 | 5557 | `\sin(x)^2 + \cos(x)^2 = 1`, `\cosh(x)^2 - \sinh(x)^2 = 1`, |
5558 | 5558 | `\sin(x)\csc(x) = 1`, or `\tanh(x)=\sinh(x)/\cosh(x)` |
5559 | 5559 | to simplify expressions containing tan, sec, etc., to sin, |
5560 | 5560 | cos, sinh, cosh. |
5561 | | |
| 5561 | |
5562 | 5562 | INPUT: |
5563 | 5563 | |
5564 | 5564 | - ``self`` - symbolic expression |
… |
… |
|
5568 | 5568 | angles occurring in ``self`` first. For best results, |
5569 | 5569 | ``self`` should be expanded. See also :meth:`expand_trig` to |
5570 | 5570 | get more controls on this expansion. |
5571 | | |
| 5571 | |
5572 | 5572 | ALIAS: :meth:`trig_simplify` and :meth:`simplify_trig` are the same |
5573 | | |
5574 | | EXAMPLES:: |
5575 | | |
| 5573 | |
| 5574 | EXAMPLES:: |
| 5575 | |
5576 | 5576 | sage: f = sin(x)^2 + cos(x)^2; f |
5577 | 5577 | sin(x)^2 + cos(x)^2 |
5578 | 5578 | sage: f.simplify() |
… |
… |
|
5591 | 5591 | sage: f=tan(3*x) |
5592 | 5592 | sage: f.simplify_trig() |
5593 | 5593 | (4*cos(x)^2 - 1)*sin(x)/(4*cos(x)^3 - 3*cos(x)) |
5594 | | sage: f.simplify_trig(False) |
| 5594 | sage: f.simplify_trig(False) |
5595 | 5595 | sin(3*x)/cos(3*x) |
5596 | | |
| 5596 | |
5597 | 5597 | """ |
5598 | 5598 | # much better to expand first, since it often doesn't work |
5599 | 5599 | # right otherwise! |
… |
… |
|
5610 | 5610 | |
5611 | 5611 | INPUT: |
5612 | 5612 | |
5613 | | - ``self`` - symbolic expression |
| 5613 | - ``self`` - symbolic expression |
5614 | 5614 | |
5615 | 5615 | - ``method`` - (default: 'full') string which switches the |
5616 | | method for simplifications. Possible values are |
| 5616 | method for simplifications. Possible values are |
5617 | 5617 | |
5618 | 5618 | - 'simple' (simplify rational functions into quotient of two |
5619 | | polynomials), |
| 5619 | polynomials), |
5620 | 5620 | |
5621 | 5621 | - 'full' (apply repeatedly, if necessary) |
5622 | 5622 | |
… |
… |
|
5634 | 5634 | DETAILS: We call Maxima functions ratsimp, fullratsimp and |
5635 | 5635 | xthru. If each part of the expression has to be simplified |
5636 | 5636 | separately, we use Maxima function map. |
5637 | | |
5638 | | EXAMPLES:: |
5639 | | |
| 5637 | |
| 5638 | EXAMPLES:: |
| 5639 | |
5640 | 5640 | sage: f = sin(x/(x^2 + x)) |
5641 | 5641 | sage: f |
5642 | 5642 | sin(x/(x^2 + x)) |
5643 | 5643 | sage: f.simplify_rational() |
5644 | 5644 | sin(1/(x + 1)) |
5645 | | |
5646 | | :: |
5647 | | |
| 5645 | |
| 5646 | :: |
| 5647 | |
5648 | 5648 | sage: f = ((x - 1)^(3/2) - (x + 1)*sqrt(x - 1))/sqrt((x - 1)*(x + 1)); f |
5649 | 5649 | ((x - 1)^(3/2) - sqrt(x - 1)*(x + 1))/sqrt((x - 1)*(x + 1)) |
5650 | 5650 | sage: f.simplify_rational() |
… |
… |
|
5663 | 5663 | sage: f.simplify_rational(map=True) |
5664 | 5664 | x - log(x)/(x + 2) - 1 |
5665 | 5665 | |
5666 | | Here is an example from the Maxima documentation of where |
| 5666 | Here is an example from the Maxima documentation of where |
5667 | 5667 | ``method='simple'`` produces an (possibly useful) intermediate |
5668 | 5668 | step:: |
5669 | 5669 | |
… |
… |
|
5706 | 5706 | |
5707 | 5707 | def simplify_factorial(self): |
5708 | 5708 | """ |
5709 | | Simplify by combining expressions with factorials, and by |
| 5709 | Simplify by combining expressions with factorials, and by |
5710 | 5710 | expanding binomials into factorials. |
5711 | | |
| 5711 | |
5712 | 5712 | ALIAS: factorial_simplify and simplify_factorial are the same |
5713 | | |
| 5713 | |
5714 | 5714 | EXAMPLES: |
5715 | 5715 | |
5716 | 5716 | Some examples are relatively clear:: |
5717 | | |
| 5717 | |
5718 | 5718 | sage: var('n,k') |
5719 | 5719 | (n, k) |
5720 | 5720 | sage: f = factorial(n+1)/factorial(n); f |
… |
… |
|
5736 | 5736 | factorial(-k + n)*factorial(k)*binomial(n, k) |
5737 | 5737 | sage: f.simplify_factorial() |
5738 | 5738 | factorial(n) |
5739 | | |
| 5739 | |
5740 | 5740 | A more complicated example, which needs further processing:: |
5741 | 5741 | |
5742 | 5742 | sage: f = factorial(x)/factorial(x-2)/2 + factorial(x+1)/factorial(x)/2; f |
… |
… |
|
5756 | 5756 | exponentials, and radicals, by converting it into a form which is |
5757 | 5757 | canonical over a large class of expressions and a given ordering of |
5758 | 5758 | variables |
5759 | | |
| 5759 | |
5760 | 5760 | DETAILS: This uses the Maxima radcan() command. From the Maxima |
5761 | 5761 | documentation: "All functionally equivalent forms are mapped into a |
5762 | 5762 | unique form. For a somewhat larger class of expressions, produces a |
… |
… |
|
5766 | 5766 | time consuming. This is the cost of exploring certain relationships |
5767 | 5767 | among the components of the expression for simplifications based on |
5768 | 5768 | factoring and partial fraction expansions of exponents." |
5769 | | |
5770 | | ALIAS: radical_simplify, simplify_radical, exp_simplify, simplify_exp |
| 5769 | |
| 5770 | ALIAS: radical_simplify, simplify_radical, exp_simplify, simplify_exp |
5771 | 5771 | are all the same |
5772 | | |
5773 | | EXAMPLES:: |
5774 | | |
| 5772 | |
| 5773 | EXAMPLES:: |
| 5774 | |
5775 | 5775 | sage: var('x,y,a') |
5776 | 5776 | (x, y, a) |
5777 | | |
5778 | | :: |
5779 | | |
| 5777 | |
| 5778 | :: |
| 5779 | |
5780 | 5780 | sage: f = log(x*y) |
5781 | 5781 | sage: f.simplify_radical() |
5782 | 5782 | log(x) + log(y) |
5783 | | |
5784 | | :: |
5785 | | |
| 5783 | |
| 5784 | :: |
| 5785 | |
5786 | 5786 | sage: f = (log(x+x^2)-log(x))^a/log(1+x)^(a/2) |
5787 | 5787 | sage: f.simplify_radical() |
5788 | 5788 | log(x + 1)^(1/2*a) |
5789 | | |
5790 | | :: |
5791 | | |
| 5789 | |
| 5790 | :: |
| 5791 | |
5792 | 5792 | sage: f = (e^x-1)/(1+e^(x/2)) |
5793 | 5793 | sage: f.simplify_exp() |
5794 | 5794 | e^(1/2*x) - 1 |
… |
… |
|
5813 | 5813 | this transformation in optional parameter ``method``. |
5814 | 5814 | |
5815 | 5815 | INPUT: |
5816 | | |
| 5816 | |
5817 | 5817 | - ``self`` - expression to be simplified |
5818 | 5818 | |
5819 | 5819 | - ``method`` - (default: None) optional, governs the condition |
5820 | 5820 | on a1 and a2 which must be satisfied to contract expression |
5821 | | a1*log(b1) + a2*log(b2). Values are |
5822 | | |
5823 | | - None (use Maxima default, integers), |
5824 | | |
5825 | | - 'one' (1 and -1), |
5826 | | |
5827 | | - 'ratios' (integers and fractions of integers), |
5828 | | |
5829 | | - 'constants' (constants), |
5830 | | |
5831 | | - 'all' (all expressions). |
| 5821 | a1*log(b1) + a2*log(b2). Values are |
| 5822 | |
| 5823 | - None (use Maxima default, integers), |
| 5824 | |
| 5825 | - 'one' (1 and -1), |
| 5826 | |
| 5827 | - 'ratios' (integers and fractions of integers), |
| 5828 | |
| 5829 | - 'constants' (constants), |
| 5830 | |
| 5831 | - 'all' (all expressions). |
5832 | 5832 | |
5833 | 5833 | See also examples below. |
5834 | | |
| 5834 | |
5835 | 5835 | DETAILS: This uses the Maxima logcontract() command. From the |
5836 | 5836 | Maxima documentation: "Recursively scans the expression expr, |
5837 | 5837 | transforming subexpressions of the form a1*log(b1) + |
… |
… |
|
5842 | 5842 | logconcoeffp:'logconfun$ logconfun(m):=featurep(m,integer) or |
5843 | 5843 | ratnump(m)$ . Then logcontract(1/2*log(x)); will give |
5844 | 5844 | log(sqrt(x))." |
5845 | | |
| 5845 | |
5846 | 5846 | ALIAS: :meth:`log_simplify` and :meth:`simplify_log` are the |
5847 | 5847 | same |
5848 | 5848 | |
5849 | 5849 | EXAMPLES:: |
5850 | | |
| 5850 | |
5851 | 5851 | sage: x,y,t=var('x y t') |
5852 | | |
| 5852 | |
5853 | 5853 | Only two first terms are contracted in the following example , |
5854 | 5854 | the logarithm with coefficient 1/2 is not contracted:: |
5855 | | |
| 5855 | |
5856 | 5856 | sage: f = log(x)+2*log(y)+1/2*log(t) |
5857 | 5857 | sage: f.simplify_log() |
5858 | 5858 | log(x*y^2) + 1/2*log(t) |
5859 | | |
| 5859 | |
5860 | 5860 | To contract all terms in previous example use option ``method``:: |
5861 | | |
| 5861 | |
5862 | 5862 | sage: f.simplify_log(method='ratios') |
5863 | 5863 | log(sqrt(t)*x*y^2) |
5864 | 5864 | |
… |
… |
|
5866 | 5866 | has no influence to future calls (we changed some default |
5867 | 5867 | Maxima flag, and have to ensure that this flag has been |
5868 | 5868 | restored):: |
5869 | | |
| 5869 | |
5870 | 5870 | sage: f.simplify_log('one') |
5871 | 5871 | 1/2*log(t) + log(x) + 2*log(y) |
5872 | 5872 | |
5873 | 5873 | sage: f.simplify_log('ratios') |
5874 | | log(sqrt(t)*x*y^2) |
| 5874 | log(sqrt(t)*x*y^2) |
5875 | 5875 | |
5876 | 5876 | sage: f.simplify_log() |
5877 | | log(x*y^2) + 1/2*log(t) |
5878 | | |
5879 | | To contract terms with no coefficient (more precisely, with |
| 5877 | log(x*y^2) + 1/2*log(t) |
| 5878 | |
| 5879 | To contract terms with no coefficient (more precisely, with |
5880 | 5880 | coefficients 1 and -1) use option ``method``:: |
5881 | | |
5882 | | sage: f = log(x)+2*log(y)-log(t) |
5883 | | sage: f.simplify_log('one') |
| 5881 | |
| 5882 | sage: f = log(x)+2*log(y)-log(t) |
| 5883 | sage: f.simplify_log('one') |
5884 | 5884 | 2*log(y) + log(x/t) |
5885 | 5885 | |
5886 | 5886 | :: |
… |
… |
|
5892 | 5892 | sage: f.simplify_log('ratios') |
5893 | 5893 | log(x*y/(x + 1)^(1/3)) |
5894 | 5894 | |
5895 | | `\pi` is irrational number, to contract logarithms in the following example |
| 5895 | `\pi` is irrational number, to contract logarithms in the following example |
5896 | 5896 | we have to put ``method`` to ``constants`` or ``all``:: |
5897 | 5897 | |
5898 | | sage: f = log(x)+log(y)-pi*log((x+1)) |
| 5898 | sage: f = log(x)+log(y)-pi*log((x+1)) |
5899 | 5899 | sage: f.simplify_log('constants') |
5900 | 5900 | log(x*y/(x + 1)^pi) |
5901 | 5901 | |
… |
… |
|
5906 | 5906 | sage: (x*log(9)).simplify_log('all') |
5907 | 5907 | log(9^x) |
5908 | 5908 | |
5909 | | TESTS:: |
5910 | | |
5911 | | This shows that the issue at trac #7344 is fixed:: |
| 5909 | TESTS:: |
| 5910 | |
| 5911 | This shows that the issue at trac #7344 is fixed:: |
5912 | 5912 | |
5913 | 5913 | sage: (log(sqrt(2)-1)+log(sqrt(2)+1)).simplify_full() |
5914 | 5914 | 0 |
5915 | | |
| 5915 | |
5916 | 5916 | AUTHORS: |
5917 | | |
5918 | | - Robert Marik (11-2009) |
| 5917 | |
| 5918 | - Robert Marik (11-2009) |
5919 | 5919 | """ |
5920 | 5920 | from sage.calculus.calculus import maxima |
5921 | 5921 | maxima.eval('domain: real$ savelogexpand:logexpand$ logexpand:false$') |
… |
… |
|
5928 | 5928 | elif method == 'constants': |
5929 | 5929 | maxima.eval('logconfun(m):= constantp(m)$') |
5930 | 5930 | elif method == 'all': |
5931 | | maxima.eval('logconfun(m):= true$') |
| 5931 | maxima.eval('logconfun(m):= true$') |
5932 | 5932 | elif method is not None: |
5933 | 5933 | raise NotImplementedError, "unknown method, see the help for available methods" |
5934 | 5934 | res = self.parent()(self._maxima_().logcontract()) |
5935 | 5935 | maxima.eval('domain: complex$') |
5936 | 5936 | if method is not None: |
5937 | 5937 | maxima.eval('logconcoeffp:false$') |
5938 | | maxima.eval('logexpand:savelogexpand$') |
| 5938 | maxima.eval('logexpand:savelogexpand$') |
5939 | 5939 | return res |
5940 | 5940 | |
5941 | 5941 | log_simplify = simplify_log |
5942 | 5942 | |
5943 | 5943 | def expand_log(self,method='products'): |
5944 | | r""" |
5945 | | Simplifies symbolic expression, which can contain logs. |
| 5944 | r""" |
| 5945 | Simplifies symbolic expression, which can contain logs. |
5946 | 5946 | |
5947 | 5947 | Expands logarithms of powers, logarithms of products and |
5948 | 5948 | logarithms of quotients. The option ``method`` specifies |
5949 | 5949 | which expression types should be expanded. |
5950 | 5950 | |
5951 | 5951 | INPUT: |
5952 | | |
| 5952 | |
5953 | 5953 | - ``self`` - expression to be simplified |
5954 | 5954 | |
5955 | 5955 | - ``method`` - (default: 'products') optional, governs which |
5956 | | expression is expanded. Possible values are |
5957 | | |
5958 | | - 'nothing' (no expansion), |
5959 | | |
5960 | | - 'powers' (log(a^r) is expanded), |
5961 | | |
5962 | | - 'products' (like 'powers' and also log(a*b) are expanded), |
5963 | | |
5964 | | - 'all' (all possible expansion). |
5965 | | |
5966 | | See also examples below. |
5967 | | |
| 5956 | expression is expanded. Possible values are |
| 5957 | |
| 5958 | - 'nothing' (no expansion), |
| 5959 | |
| 5960 | - 'powers' (log(a^r) is expanded), |
| 5961 | |
| 5962 | - 'products' (like 'powers' and also log(a*b) are expanded), |
| 5963 | |
| 5964 | - 'all' (all possible expansion). |
| 5965 | |
| 5966 | See also examples below. |
| 5967 | |
5968 | 5968 | DETAILS: This uses the Maxima simplifier and sets |
5969 | 5969 | ``logexpand`` option for this simplifier. From the Maxima |
5970 | 5970 | documentation: "Logexpand:true causes log(a^b) to become |
… |
… |
|
5974 | 5974 | a#1. (log(1/b), for integer b, always simplifies.) If it is |
5975 | 5975 | set to false, all of these simplifications will be turned |
5976 | 5976 | off. " |
5977 | | |
| 5977 | |
5978 | 5978 | ALIAS: :meth:`log_expand` and :meth:`expand_log` are the same |
5979 | 5979 | |
5980 | 5980 | EXAMPLES:: |
5981 | 5981 | |
5982 | 5982 | By default powers and products (and quotients) are expanded, |
5983 | 5983 | but not quotients of integers:: |
5984 | | |
| 5984 | |
5985 | 5985 | sage: (log(3/4*x^pi)).log_expand() |
5986 | 5986 | pi*log(x) + log(3/4) |
5987 | 5987 | |
… |
… |
|
5998 | 5998 | The expression ``log((3*x)^6)`` is not expanded with |
5999 | 5999 | ``method='powers'``, since it is converted into product |
6000 | 6000 | first:: |
6001 | | |
| 6001 | |
6002 | 6002 | sage: (log((3*x)^6)).log_expand('powers') |
6003 | 6003 | log(729*x^6) |
6004 | 6004 | |
… |
… |
|
6006 | 6006 | has no influence to future calls (we changed some default |
6007 | 6007 | Maxima flag, and have to ensure that this flag has been |
6008 | 6008 | restored):: |
6009 | | |
| 6009 | |
6010 | 6010 | sage: (log(3/4*x^pi)).log_expand() |
6011 | 6011 | pi*log(x) + log(3/4) |
6012 | 6012 | |
… |
… |
|
6015 | 6015 | |
6016 | 6016 | sage: (log(3/4*x^pi)).log_expand() |
6017 | 6017 | pi*log(x) + log(3/4) |
6018 | | |
| 6018 | |
6019 | 6019 | AUTHORS: |
6020 | | |
6021 | | - Robert Marik (11-2009) |
| 6020 | |
| 6021 | - Robert Marik (11-2009) |
6022 | 6022 | """ |
6023 | 6023 | from sage.calculus.calculus import maxima |
6024 | 6024 | maxima.eval('domain: real$ savelogexpand:logexpand$') |
… |
… |
|
6035 | 6035 | maxima.eval('logexpand:%s'%maxima_method) |
6036 | 6036 | res = self._maxima_() |
6037 | 6037 | res = res.sage() |
6038 | | maxima.eval('domain: complex$ logexpand:savelogexpand$') |
6039 | | return res |
6040 | | |
6041 | | log_expand = expand_log |
| 6038 | maxima.eval('domain: complex$ logexpand:savelogexpand$') |
| 6039 | return res |
| 6040 | |
| 6041 | log_expand = expand_log |
6042 | 6042 | |
6043 | 6043 | |
6044 | 6044 | def factor(self, dontfactor=[]): |
6045 | 6045 | """ |
6046 | 6046 | Factors self, containing any number of variables or functions, into |
6047 | 6047 | factors irreducible over the integers. |
6048 | | |
6049 | | INPUT: |
6050 | | |
6051 | | |
| 6048 | |
| 6049 | INPUT: |
| 6050 | |
| 6051 | |
6052 | 6052 | - ``self`` - a symbolic expression |
6053 | | |
| 6053 | |
6054 | 6054 | - ``dontfactor`` - list (default: []), a list of |
6055 | 6055 | variables with respect to which factoring is not to occur. |
6056 | 6056 | Factoring also will not take place with respect to any variables |
6057 | 6057 | which are less important (using the variable ordering assumed for |
6058 | 6058 | CRE form) than those on the 'dontfactor' list. |
6059 | | |
6060 | | |
6061 | | EXAMPLES:: |
6062 | | |
| 6059 | |
| 6060 | |
| 6061 | EXAMPLES:: |
| 6062 | |
6063 | 6063 | sage: x,y,z = var('x, y, z') |
6064 | 6064 | sage: (x^3-y^3).factor() |
6065 | 6065 | (x - y)*(x^2 + x*y + y^2) |
… |
… |
|
6073 | 6073 | dontfactor is empty) the factorization is done using Singular |
6074 | 6074 | instead of Maxima, so the following is very fast instead of |
6075 | 6075 | dreadfully slow:: |
6076 | | |
| 6076 | |
6077 | 6077 | sage: var('x,y') |
6078 | 6078 | (x, y) |
6079 | 6079 | sage: (x^99 + y^99).factor() |
… |
… |
|
6099 | 6099 | """ |
6100 | 6100 | Returns a list of the factors of self, as computed by the |
6101 | 6101 | factor command. |
6102 | | |
6103 | | INPUT: |
6104 | | |
| 6102 | |
| 6103 | INPUT: |
| 6104 | |
6105 | 6105 | - ``self`` - a symbolic expression |
6106 | | |
| 6106 | |
6107 | 6107 | - ``dontfactor`` - see docs for :meth:`factor` |
6108 | | |
| 6108 | |
6109 | 6109 | .. note:: |
6110 | 6110 | |
6111 | 6111 | If you already have a factored expression and just want to |
6112 | 6112 | get at the individual factors, use :meth:`_factor_list` |
6113 | 6113 | instead. |
6114 | | |
6115 | | EXAMPLES:: |
6116 | | |
| 6114 | |
| 6115 | EXAMPLES:: |
| 6116 | |
6117 | 6117 | sage: var('x, y, z') |
6118 | 6118 | (x, y, z) |
6119 | 6119 | sage: f = x^3-y^3 |
6120 | 6120 | sage: f.factor() |
6121 | 6121 | (x - y)*(x^2 + x*y + y^2) |
6122 | | |
| 6122 | |
6123 | 6123 | Notice that the -1 factor is separated out:: |
6124 | | |
| 6124 | |
6125 | 6125 | sage: f.factor_list() |
6126 | 6126 | [(x - y, 1), (x^2 + x*y + y^2, 1)] |
6127 | | |
| 6127 | |
6128 | 6128 | We factor a fairly straightforward expression:: |
6129 | | |
| 6129 | |
6130 | 6130 | sage: factor(-8*y - 4*x + z^2*(2*y + x)).factor_list() |
6131 | 6131 | [(z - 2, 1), (z + 2, 1), (x + 2*y, 1)] |
6132 | 6132 | |
6133 | 6133 | A more complicated example:: |
6134 | | |
| 6134 | |
6135 | 6135 | sage: var('x, u, v') |
6136 | 6136 | (x, u, v) |
6137 | | sage: f = expand((2*u*v^2-v^2-4*u^3)^2 * (-u)^3 * (x-sin(x))^3) |
| 6137 | sage: f = expand((2*u*v^2-v^2-4*u^3)^2 * (-u)^3 * (x-sin(x))^3) |
6138 | 6138 | sage: f.factor() |
6139 | 6139 | -(x - sin(x))^3*(4*u^3 - 2*u*v^2 + v^2)^2*u^3 |
6140 | | sage: g = f.factor_list(); g |
| 6140 | sage: g = f.factor_list(); g |
6141 | 6141 | [(x - sin(x), 3), (4*u^3 - 2*u*v^2 + v^2, 2), (u, 3), (-1, 1)] |
6142 | 6142 | |
6143 | 6143 | This function also works for quotients:: |
6144 | | |
| 6144 | |
6145 | 6145 | sage: f = -1 - 2*x - x^2 + y^2 + 2*x*y^2 + x^2*y^2 |
6146 | 6146 | sage: g = f/(36*(1 + 2*y + y^2)); g |
6147 | 6147 | 1/36*(x^2*y^2 + 2*x*y^2 - x^2 + y^2 - 2*x - 1)/(y^2 + 2*y + 1) |
… |
… |
|
6149 | 6149 | 1/36*(y - 1)*(x^2 + 2*x + 1)/(y + 1) |
6150 | 6150 | sage: g.factor_list(dontfactor=[x]) |
6151 | 6151 | [(y - 1, 1), (y + 1, -1), (x^2 + 2*x + 1, 1), (1/36, 1)] |
6152 | | |
| 6152 | |
6153 | 6153 | This example also illustrates that the exponents do not have to be |
6154 | 6154 | integers:: |
6155 | | |
| 6155 | |
6156 | 6156 | sage: f = x^(2*sin(x)) * (x-1)^(sqrt(2)*x); f |
6157 | 6157 | (x - 1)^(sqrt(2)*x)*x^(2*sin(x)) |
6158 | 6158 | sage: f.factor_list() |
… |
… |
|
6164 | 6164 | r""" |
6165 | 6165 | Turn an expression already in factored form into a list of (prime, |
6166 | 6166 | power) pairs. |
6167 | | |
| 6167 | |
6168 | 6168 | This is used, e.g., internally by the :meth:`factor_list` |
6169 | 6169 | command. |
6170 | | |
6171 | | EXAMPLES:: |
6172 | | |
| 6170 | |
| 6171 | EXAMPLES:: |
| 6172 | |
6173 | 6173 | sage: g = factor(x^3 - 1); g |
6174 | 6174 | (x - 1)*(x^2 + x + 1) |
6175 | 6175 | sage: v = g._factor_list(); v |
… |
… |
|
6184 | 6184 | return [tuple(self.operands())] |
6185 | 6185 | else: |
6186 | 6186 | return [(self, 1)] |
6187 | | |
| 6187 | |
6188 | 6188 | ################################################################### |
6189 | 6189 | # solve |
6190 | 6190 | ################################################################### |
… |
… |
|
6193 | 6193 | Returns roots of ``self`` that can be found exactly, |
6194 | 6194 | possibly with multiplicities. Not all roots are guaranteed to |
6195 | 6195 | be found. |
6196 | | |
| 6196 | |
6197 | 6197 | .. warning:: |
6198 | 6198 | |
6199 | 6199 | This is *not* a numerical solver - use ``find_root`` to |
6200 | 6200 | solve for self == 0 numerically on an interval. |
6201 | | |
6202 | | INPUT: |
6203 | | |
| 6201 | |
| 6202 | INPUT: |
| 6203 | |
6204 | 6204 | - ``x`` - variable to view the function in terms of |
6205 | 6205 | (use default variable if not given) |
6206 | | |
| 6206 | |
6207 | 6207 | - ``explicit_solutions`` - bool (default True); require that |
6208 | 6208 | roots be explicit rather than implicit |
6209 | | |
| 6209 | |
6210 | 6210 | - ``multiplicities`` - bool (default True); when True, return |
6211 | 6211 | multiplicities |
6212 | | |
| 6212 | |
6213 | 6213 | - ``ring`` - a ring (default None): if not None, convert |
6214 | 6214 | self to a polynomial over ring and find roots over ring |
6215 | 6215 | |
6216 | | OUTPUT: |
6217 | | |
| 6216 | OUTPUT: |
| 6217 | |
6218 | 6218 | list of pairs (root, multiplicity) or list of roots |
6219 | | |
| 6219 | |
6220 | 6220 | If there are infinitely many roots, e.g., a function like |
6221 | 6221 | `\sin(x)`, only one is returned. |
6222 | | |
6223 | | EXAMPLES:: |
6224 | | |
| 6222 | |
| 6223 | EXAMPLES:: |
| 6224 | |
6225 | 6225 | sage: var('x, a') |
6226 | 6226 | (x, a) |
6227 | | |
| 6227 | |
6228 | 6228 | A simple example:: |
6229 | | |
| 6229 | |
6230 | 6230 | sage: ((x^2-1)^2).roots() |
6231 | 6231 | [(-1, 2), (1, 2)] |
6232 | 6232 | sage: ((x^2-1)^2).roots(multiplicities=False) |
6233 | 6233 | [-1, 1] |
6234 | | |
| 6234 | |
6235 | 6235 | A complicated example:: |
6236 | | |
| 6236 | |
6237 | 6237 | sage: f = expand((x^2 - 1)^3*(x^2 + 1)*(x-a)); f |
6238 | 6238 | -a*x^8 + x^9 + 2*a*x^6 - 2*x^7 - 2*a*x^2 + 2*x^3 + a - x |
6239 | | |
| 6239 | |
6240 | 6240 | The default variable is `a`, since it is the first in |
6241 | 6241 | alphabetical order:: |
6242 | | |
| 6242 | |
6243 | 6243 | sage: f.roots() |
6244 | 6244 | [(x, 1)] |
6245 | | |
| 6245 | |
6246 | 6246 | As a polynomial in `a`, `x` is indeed a root:: |
6247 | | |
| 6247 | |
6248 | 6248 | sage: f.poly(a) |
6249 | 6249 | x^9 - 2*x^7 + 2*x^3 - (x^8 - 2*x^6 + 2*x^2 - 1)*a - x |
6250 | 6250 | sage: f(a=x) |
6251 | 6251 | 0 |
6252 | | |
| 6252 | |
6253 | 6253 | The roots in terms of `x` are what we expect:: |
6254 | | |
| 6254 | |
6255 | 6255 | sage: f.roots(x) |
6256 | 6256 | [(a, 1), (-I, 1), (I, 1), (1, 3), (-1, 3)] |
6257 | | |
| 6257 | |
6258 | 6258 | Only one root of `\sin(x) = 0` is given:: |
6259 | | |
6260 | | sage: f = sin(x) |
| 6259 | |
| 6260 | sage: f = sin(x) |
6261 | 6261 | sage: f.roots(x) |
6262 | 6262 | [(0, 1)] |
6263 | | |
| 6263 | |
6264 | 6264 | .. note:: |
6265 | 6265 | |
6266 | 6266 | It is possible to solve a greater variety of equations |
6267 | | using ``solve()`` and the keyword ``to_poly_solve``, |
6268 | | but only at the price of possibly encountering |
| 6267 | using ``solve()`` and the keyword ``to_poly_solve``, |
| 6268 | but only at the price of possibly encountering |
6269 | 6269 | approximate solutions. See documentation for f.solve |
6270 | 6270 | for more details. |
6271 | 6271 | |
6272 | 6272 | We derive the roots of a general quadratic polynomial:: |
6273 | | |
| 6273 | |
6274 | 6274 | sage: var('a,b,c,x') |
6275 | 6275 | (a, b, c, x) |
6276 | 6276 | sage: (a*x^2 + b*x + c).roots(x) |
6277 | 6277 | [(-1/2*(b + sqrt(-4*a*c + b^2))/a, 1), (-1/2*(b - sqrt(-4*a*c + b^2))/a, 1)] |
6278 | 6278 | |
6279 | 6279 | By default, all the roots are required to be explicit rather than |
6280 | | implicit. To get implicit roots, pass ``explicit_solutions=False`` |
| 6280 | implicit. To get implicit roots, pass ``explicit_solutions=False`` |
6281 | 6281 | to ``.roots()`` :: |
6282 | | |
| 6282 | |
6283 | 6283 | sage: var('x') |
6284 | 6284 | x |
6285 | 6285 | sage: f = x^(1/9) + (2^(8/9) - 2^(1/9))*(x - 1) - x^(8/9) |
… |
… |
|
6292 | 6292 | |
6293 | 6293 | Another example, but involving a degree 5 poly whose roots don't |
6294 | 6294 | get computed explicitly:: |
6295 | | |
| 6295 | |
6296 | 6296 | sage: f = x^5 + x^3 + 17*x + 1 |
6297 | 6297 | sage: f.roots() |
6298 | 6298 | Traceback (most recent call last): |
… |
… |
|
6304 | 6304 | [x^5 + x^3 + 17*x + 1] |
6305 | 6305 | |
6306 | 6306 | Now let's find some roots over different rings:: |
6307 | | |
| 6307 | |
6308 | 6308 | sage: f.roots(ring=CC) |
6309 | 6309 | [(-0.0588115223184..., 1), (-1.331099917875... - 1.52241655183732*I, 1), (-1.331099917875... + 1.52241655183732*I, 1), (1.36050567903502 - 1.51880872209965*I, 1), (1.36050567903502 + 1.51880872209965*I, 1)] |
6310 | 6310 | sage: (2.5*f).roots(ring=RR) |
… |
… |
|
6317 | 6317 | [-0.05881152231844944?, -1.331099917875796? - 1.522416551837318?*I, -1.331099917875796? + 1.522416551837318?*I, 1.360505679035020? - 1.518808722099650?*I, 1.360505679035020? + 1.518808722099650?*I] |
6318 | 6318 | |
6319 | 6319 | Root finding over finite fields:: |
6320 | | |
| 6320 | |
6321 | 6321 | sage: f.roots(ring=GF(7^2, 'a')) |
6322 | 6322 | [(3, 1), (4*a + 6, 2), (3*a + 3, 2)] |
6323 | 6323 | |
6324 | 6324 | TESTS:: |
6325 | | |
| 6325 | |
6326 | 6326 | sage: (sqrt(3) * f).roots(ring=QQ) |
6327 | 6327 | Traceback (most recent call last): |
6328 | 6328 | ... |
… |
… |
|
6347 | 6347 | def solve(self, x, multiplicities=False, solution_dict=False, explicit_solutions=False, to_poly_solve=False): |
6348 | 6348 | r""" |
6349 | 6349 | Analytically solve the equation ``self == 0`` or an univarite |
6350 | | inequality for the variable `x`. |
6351 | | |
| 6350 | inequality for the variable `x`. |
| 6351 | |
6352 | 6352 | .. warning:: |
6353 | 6353 | |
6354 | 6354 | This is not a numerical solver - use ``find_root`` to solve |
6355 | 6355 | for self == 0 numerically on an interval. |
6356 | | |
6357 | | INPUT: |
6358 | | |
6359 | | |
| 6356 | |
| 6357 | INPUT: |
| 6358 | |
| 6359 | |
6360 | 6360 | - ``x`` - variable to solve for |
6361 | | |
| 6361 | |
6362 | 6362 | - ``multiplicities`` - bool (default: False); if True, |
6363 | 6363 | return corresponding multiplicities. This keyword is |
6364 | 6364 | incompatible with ``to_poly_solve=True`` and does not make |
6365 | | any sense when solving inequality. |
6366 | | |
| 6365 | any sense when solving inequality. |
| 6366 | |
6367 | 6367 | - ``solution_dict`` - bool (default: False); if True, |
6368 | 6368 | return a list of dictionaries containing solutions. Not used |
6369 | | when solving inequality. |
| 6369 | when solving inequality. |
6370 | 6370 | |
6371 | 6371 | - ``explicit_solutions`` - bool (default: False); require that |
6372 | 6372 | all roots be explicit rather than implicit. Not used |
6373 | | when solving inequality. |
6374 | | |
6375 | | - ``to_poly_solve`` - bool (default: False); use Maxima's |
6376 | | ``to_poly_solver`` package to search for more possible |
| 6373 | when solving inequality. |
| 6374 | |
| 6375 | - ``to_poly_solve`` - bool (default: False) or string; use Maxima's |
| 6376 | ``to_poly_solver`` package to search for more possible |
6377 | 6377 | solutions, but possibly encounter approximate solutions. |
6378 | 6378 | This keyword is incompatible with ``multiplicities=True`` |
6379 | | and is not used when solving inequality. |
6380 | | |
6381 | | EXAMPLES:: |
6382 | | |
| 6379 | and is not used when solving inequality. Setting ``to_poly_solve`` |
| 6380 | to 'force' (string) omits Maxima's solve command (usefull when |
| 6381 | some solution of trigonometric equations are lost). |
| 6382 | |
| 6383 | EXAMPLES:: |
| 6384 | |
6383 | 6385 | sage: z = var('z') |
6384 | 6386 | sage: (z^5 - 1).solve(z) |
6385 | 6387 | [z == e^(2/5*I*pi), z == e^(4/5*I*pi), z == e^(-4/5*I*pi), z == e^(-2/5*I*pi), z == 1] |
… |
… |
|
6389 | 6391 | |
6390 | 6392 | A simple example to show use of the keyword |
6391 | 6393 | ``multiplicities``:: |
6392 | | |
| 6394 | |
6393 | 6395 | sage: ((x^2-1)^2).solve(x) |
6394 | 6396 | [x == -1, x == 1] |
6395 | 6397 | sage: ((x^2-1)^2).solve(x,multiplicities=True) |
… |
… |
|
6422 | 6424 | sage: solve(Q*sqrt(Q^2 + 2) - 1, Q) |
6423 | 6425 | [Q == 1/sqrt(Q^2 + 2)] |
6424 | 6426 | sage: solve(Q*sqrt(Q^2 + 2) - 1, Q, to_poly_solve=True) |
6425 | | [Q == 1/sqrt(-sqrt(2) + 1), Q == 1/sqrt(sqrt(2) + 1)] |
| 6427 | [Q == 1/sqrt(-sqrt(2) + 1), Q == 1/sqrt(sqrt(2) + 1)] |
6426 | 6428 | |
6427 | 6429 | In some cases there may be infinitely many solutions indexed |
6428 | 6430 | by a dummy variable. If it begins with ``z``, it is implicitly |
… |
… |
|
6430 | 6432 | |
6431 | 6433 | sage: solve( sin(x)==cos(x), x, to_poly_solve=True) |
6432 | 6434 | [x == 1/4*pi + pi*z45] |
6433 | | |
| 6435 | |
6434 | 6436 | An effort is made to only return solutions that satisfy the current assumptions:: |
6435 | | |
| 6437 | |
6436 | 6438 | sage: solve(x^2==4, x) |
6437 | 6439 | [x == -2, x == 2] |
6438 | 6440 | sage: assume(x<0) |
… |
… |
|
6449 | 6451 | [x == -sqrt(-z + 2)] |
6450 | 6452 | sage: solve((x-z)^2==2, x) |
6451 | 6453 | [x == z - sqrt(2), x == z + sqrt(2)] |
6452 | | |
| 6454 | |
6453 | 6455 | There is still room for improvement:: |
6454 | | |
| 6456 | |
6455 | 6457 | sage: assume(x, 'integer') |
6456 | 6458 | sage: assume(z, 'integer') |
6457 | 6459 | sage: solve((x-z)^2==2, x) |
6458 | 6460 | [x == z - sqrt(2), x == z + sqrt(2)] |
6459 | | |
| 6461 | |
6460 | 6462 | sage: forget() |
6461 | 6463 | |
6462 | 6464 | In some cases it may be worthwhile to directly use to_poly_solve, |
… |
… |
|
6499 | 6501 | sage: (x^2>1).solve(x) |
6500 | 6502 | [[x < -1], [x > 1]] |
6501 | 6503 | |
6502 | | Catch error message from Maxima:: |
| 6504 | Catch error message from Maxima:: |
6503 | 6505 | |
6504 | 6506 | sage: solve(acot(x),x) |
6505 | 6507 | [] |
6506 | 6508 | |
6507 | | :: |
| 6509 | :: |
6508 | 6510 | |
6509 | 6511 | sage: solve(acot(x),x,to_poly_solve=True) |
6510 | 6512 | [] |
6511 | 6513 | |
6512 | | Trac #7491 fixed:: |
| 6514 | Trac #7491 fixed:: |
6513 | 6515 | |
6514 | 6516 | sage: y=var('y') |
6515 | 6517 | sage: solve(y==y,y) |
6516 | 6518 | [y == r1] |
6517 | 6519 | sage: solve(y==y,y,multiplicities=True) |
6518 | 6520 | ([y == r1], []) |
6519 | | |
| 6521 | |
6520 | 6522 | sage: from sage.symbolic.assumptions import GenericDeclaration |
6521 | 6523 | sage: GenericDeclaration(x, 'rational').assume() |
6522 | 6524 | sage: solve(x^2 == 2, x) |
6523 | 6525 | [] |
6524 | 6526 | sage: forget() |
6525 | 6527 | |
| 6528 | Trac #8390 fixed:: |
| 6529 | |
| 6530 | sage: solve(sin(x)==1/2,x) |
| 6531 | [x == 1/6*pi] |
| 6532 | |
| 6533 | :: |
| 6534 | |
| 6535 | sage: solve(sin(x)==1/2,x,to_poly_solve=True) |
| 6536 | [x == 1/6*pi] |
| 6537 | |
| 6538 | :: |
| 6539 | |
| 6540 | sage: solve(sin(x)==1/2,x,to_poly_solve='force') |
| 6541 | [x == 5/6*pi + 2*pi*z87, x == 1/6*pi + 2*pi*z85] |
6526 | 6542 | """ |
6527 | 6543 | import operator |
6528 | 6544 | cdef Expression ex |
… |
… |
|
6561 | 6577 | if explicit_solutions: |
6562 | 6578 | P.eval('solveexplicit: true') # switches Maxima to looking for only explicit solutions |
6563 | 6579 | try: |
6564 | | s = m.solve(x).str() |
| 6580 | if to_poly_solve != 'force': |
| 6581 | s = m.solve(x).str() |
| 6582 | else: # omit Maxima's solve command |
| 6583 | s = str([]) |
6565 | 6584 | except TypeError, mess: # if Maxima's solve has an error, we catch it |
6566 | 6585 | if "Error executing code in Maxima" in str(mess): |
6567 | 6586 | s = str([]) |
… |
… |
|
6574 | 6593 | if solution_dict: |
6575 | 6594 | ans = (dict([[x,self.parent().var('r1')]])) |
6576 | 6595 | else: |
6577 | | ans = ([x == self.parent().var('r1')]) |
6578 | | if multiplicities: |
| 6596 | ans = ([x == self.parent().var('r1')]) |
| 6597 | if multiplicities: |
6579 | 6598 | return ans,[] |
6580 | 6599 | else: |
6581 | 6600 | return ans |
… |
… |
|
6598 | 6617 | # but also allows for the possibility of approximate # |
6599 | 6618 | # solutions being returned. # |
6600 | 6619 | ######################################################## |
6601 | | if to_poly_solve and not multiplicities: |
| 6620 | if to_poly_solve and not multiplicities: |
6602 | 6621 | if len(X)==0: # if Maxima's solve gave no solutions, only try it |
6603 | 6622 | try: |
6604 | 6623 | s = m.to_poly_solve(x) |
… |
… |
|
6612 | 6631 | from sage.calculus.calculus import symbolic_expression_from_maxima_element |
6613 | 6632 | try: |
6614 | 6633 | Y = symbolic_expression_from_maxima_element((eq._maxima_()).to_poly_solve(x)) # try to solve it using to_poly_solve |
6615 | | X.remove(eq) |
| 6634 | X.remove(eq) |
6616 | 6635 | X.extend([y[0] for y in Y]) # replace with the new solutions |
6617 | 6636 | except TypeError, mess: |
6618 | 6637 | if "Error executing code in Maxima" in str(mess) or "unable to make sense of Maxima expression" in str(mess): |
… |
… |
|
6647 | 6666 | """ |
6648 | 6667 | Numerically find a root of self on the closed interval [a,b] (or |
6649 | 6668 | [b,a]) if possible, where self is a function in the one variable. |
6650 | | |
6651 | | INPUT: |
6652 | | |
| 6669 | |
| 6670 | INPUT: |
| 6671 | |
6653 | 6672 | - ``a, b`` - endpoints of the interval |
6654 | | |
| 6673 | |
6655 | 6674 | - ``var`` - optional variable |
6656 | | |
| 6675 | |
6657 | 6676 | - ``xtol, rtol`` - the routine converges when a root |
6658 | 6677 | is known to lie within xtol of the value return. Should be = 0. The |
6659 | 6678 | routine modifies this to take into account the relative precision |
6660 | 6679 | of doubles. |
6661 | | |
| 6680 | |
6662 | 6681 | - ``maxiter`` - integer; if convergence is not |
6663 | 6682 | achieved in maxiter iterations, an error is raised. Must be = 0. |
6664 | | |
| 6683 | |
6665 | 6684 | - ``full_output`` - bool (default: False), if True, |
6666 | 6685 | also return object that contains information about convergence. |
6667 | | |
6668 | | |
| 6686 | |
| 6687 | |
6669 | 6688 | EXAMPLES: |
6670 | 6689 | |
6671 | 6690 | Note that in this example both f(-2) and f(3) are positive, |
6672 | 6691 | yet we still find a root in that interval:: |
6673 | | |
| 6692 | |
6674 | 6693 | sage: f = x^2 - 1 |
6675 | 6694 | sage: f.find_root(-2, 3) |
6676 | 6695 | 1.0 |
… |
… |
|
6687 | 6706 | 10 |
6688 | 6707 | sage: result.root |
6689 | 6708 | 1.0 |
6690 | | |
| 6709 | |
6691 | 6710 | More examples:: |
6692 | | |
| 6711 | |
6693 | 6712 | sage: (sin(x) + exp(x)).find_root(-10, 10) |
6694 | 6713 | -0.588532743981862... |
6695 | 6714 | sage: sin(x).find_root(-1,1) |
6696 | 6715 | 0.0 |
6697 | 6716 | sage: (1/tan(x)).find_root(3,3.5) |
6698 | 6717 | 3.1415926535... |
6699 | | |
| 6718 | |
6700 | 6719 | An example with a square root:: |
6701 | | |
| 6720 | |
6702 | 6721 | sage: f = 1 + x + sqrt(x+2); f.find_root(-2,10) |
6703 | 6722 | -1.6180339887498949 |
6704 | | |
| 6723 | |
6705 | 6724 | Some examples that Ted Kosan came up with:: |
6706 | | |
| 6725 | |
6707 | 6726 | sage: t = var('t') |
6708 | 6727 | sage: v = 0.004*(9600*e^(-(1200*t)) - 2400*e^(-(300*t))) |
6709 | 6728 | sage: v.find_root(0, 0.002) |
6710 | 6729 | 0.001540327067911417... |
6711 | | |
6712 | | With this expression, we can see there is a |
| 6730 | |
| 6731 | With this expression, we can see there is a |
6713 | 6732 | zero very close to the origin:: |
6714 | 6733 | |
6715 | 6734 | sage: a = .004*(8*e^(-(300*t)) - 8*e^(-(1200*t)))*(720000*e^(-(300*t)) - 11520000*e^(-(1200*t))) +.004*(9600*e^(-(1200*t)) - 2400*e^(-(300*t)))^2 |
6716 | 6735 | sage: show(plot(a, 0, .002), xmin=0, xmax=.002) |
6717 | | |
| 6736 | |
6718 | 6737 | It is easy to approximate with ``find_root``:: |
6719 | 6738 | |
6720 | 6739 | sage: a.find_root(0,0.002) |
6721 | 6740 | 0.0004110514049349... |
6722 | | |
6723 | | Using solve takes more effort, and even then gives |
| 6741 | |
| 6742 | Using solve takes more effort, and even then gives |
6724 | 6743 | only a solution with free (integer) variables:: |
6725 | 6744 | |
6726 | 6745 | sage: a.solve(t) |
… |
… |
|
6730 | 6749 | sage: b.solve(t) |
6731 | 6750 | [] |
6732 | 6751 | sage: b.solve(t, to_poly_solve=True) |
6733 | | [t == 1/450*I*pi*z86 + 1/900*log(3/4*sqrt(41) + 25/4), t == 1/450*I*pi*z84 + 1/900*log(-3/4*sqrt(41) + 25/4)] |
| 6752 | [t == 1/450*I*pi*z99 + 1/900*log(3/4*sqrt(41) + 25/4), t == 1/450*I*pi*z97 + 1/900*log(-3/4*sqrt(41) + 25/4)] |
6734 | 6753 | sage: n(1/900*log(-3/4*sqrt(41) + 25/4)) |
6735 | 6754 | 0.000411051404934985 |
6736 | 6755 | |
6737 | 6756 | We illustrate that root finding is only implemented in one |
6738 | 6757 | dimension:: |
6739 | | |
| 6758 | |
6740 | 6759 | sage: x, y = var('x,y') |
6741 | 6760 | sage: (x-y).find_root(-2,2) |
6742 | 6761 | Traceback (most recent call last): |
6743 | 6762 | ... |
6744 | 6763 | NotImplementedError: root finding currently only implemented in 1 dimension. |
6745 | | |
6746 | | TESTS: |
6747 | | |
| 6764 | |
| 6765 | TESTS: |
| 6766 | |
6748 | 6767 | Test the special case that failed for the first attempt to fix |
6749 | 6768 | #3980:: |
6750 | | |
| 6769 | |
6751 | 6770 | sage: t = var('t') |
6752 | 6771 | sage: find_root(1/t - x,0,2) |
6753 | 6772 | Traceback (most recent call last): |
… |
… |
|
6760 | 6779 | if self.number_of_arguments() == 0: |
6761 | 6780 | if bool(self == 0): |
6762 | 6781 | return a |
6763 | | else: |
| 6782 | else: |
6764 | 6783 | raise RuntimeError, "no zero in the interval, since constant expression is not 0." |
6765 | 6784 | elif self.number_of_arguments() == 1: |
6766 | 6785 | f = self._fast_float_(self.default_variable()) |
… |
… |
|
6775 | 6794 | Numerically find the maximum of the expression ``self`` |
6776 | 6795 | on the interval [a,b] (or [b,a]) along with the point at which the |
6777 | 6796 | maximum is attained. |
6778 | | |
| 6797 | |
6779 | 6798 | See the documentation for |
6780 | 6799 | ``self.find_minimum_on_interval`` for more details. |
6781 | | |
6782 | | EXAMPLES:: |
6783 | | |
| 6800 | |
| 6801 | EXAMPLES:: |
| 6802 | |
6784 | 6803 | sage: f = x*cos(x) |
6785 | 6804 | sage: f.find_maximum_on_interval(0,5) |
6786 | 6805 | (0.5610963381910451, 0.8603335890...) |
… |
… |
|
6790 | 6809 | minval, x = (-self).find_minimum_on_interval(a, b, var=var, tol=tol, |
6791 | 6810 | maxfun=maxfun) |
6792 | 6811 | return -minval, x |
6793 | | |
| 6812 | |
6794 | 6813 | def find_minimum_on_interval(self, a, b, var=None, tol=1.48e-08, maxfun=500): |
6795 | 6814 | r""" |
6796 | 6815 | Numerically find the minimum of the expression ``self`` |
6797 | 6816 | on the interval [a,b] (or [b,a]) and the point at which it attains |
6798 | 6817 | that minimum. Note that ``self`` must be a function of |
6799 | 6818 | (at most) one variable. |
6800 | | |
6801 | | INPUT: |
6802 | | |
| 6819 | |
| 6820 | INPUT: |
| 6821 | |
6803 | 6822 | - ``var`` - variable (default: first variable in |
6804 | 6823 | self) |
6805 | | |
| 6824 | |
6806 | 6825 | - ``a,b`` - endpoints of interval on which to minimize |
6807 | 6826 | self. |
6808 | | |
| 6827 | |
6809 | 6828 | - ``tol`` - the convergence tolerance |
6810 | | |
| 6829 | |
6811 | 6830 | - ``maxfun`` - maximum function evaluations |
6812 | | |
6813 | | |
6814 | | OUTPUT: |
6815 | | |
| 6831 | |
| 6832 | |
| 6833 | OUTPUT: |
| 6834 | |
6816 | 6835 | - ``minval`` - (float) the minimum value that self takes on in the |
6817 | 6836 | interval [a,b] |
6818 | | |
| 6837 | |
6819 | 6838 | - ``x`` - (float) the point at which self takes on the minimum value |
6820 | | |
6821 | | EXAMPLES:: |
6822 | | |
| 6839 | |
| 6840 | EXAMPLES:: |
| 6841 | |
6823 | 6842 | sage: f = x*cos(x) |
6824 | 6843 | sage: f.find_minimum_on_interval(1, 5) |
6825 | 6844 | (-3.288371395590..., 3.4256184695...) |
… |
… |
|
6830 | 6849 | sage: show(f.plot(0, 20)) |
6831 | 6850 | sage: f.find_minimum_on_interval(1, 15) |
6832 | 6851 | (-9.477294259479..., 9.5293344109...) |
6833 | | |
| 6852 | |
6834 | 6853 | ALGORITHM: |
6835 | 6854 | |
6836 | 6855 | Uses ``scipy.optimize.fminbound`` which uses Brent's method. |
6837 | | |
| 6856 | |
6838 | 6857 | AUTHORS: |
6839 | 6858 | |
6840 | 6859 | - William Stein (2007-12-07) |
… |
… |
|
6853 | 6872 | """ |
6854 | 6873 | Returns an object which provides fast floating point |
6855 | 6874 | evaluation of this symbolic expression. |
6856 | | |
| 6875 | |
6857 | 6876 | See :mod:`sage.ext.fast_eval` for more information. |
6858 | 6877 | |
6859 | 6878 | EXAMPLES:: |
… |
… |
|
6884 | 6903 | """ |
6885 | 6904 | from sage.symbolic.expression_conversions import fast_callable |
6886 | 6905 | return fast_callable(self, etb) |
6887 | | |
| 6906 | |
6888 | 6907 | def show(self): |
6889 | 6908 | """ |
6890 | 6909 | Show this symbolic expression, i.e., typeset it nicely. |
6891 | | |
6892 | | EXAMPLES:: |
6893 | | |
| 6910 | |
| 6911 | EXAMPLES:: |
| 6912 | |
6894 | 6913 | sage: (x^2 + 1).show() |
6895 | 6914 | x^{2} + 1 |
6896 | 6915 | """ |
… |
… |
|
6900 | 6919 | def plot(self, *args, **kwds): |
6901 | 6920 | """ |
6902 | 6921 | Plot a symbolic expression. All arguments are passed onto the standard plot command. |
6903 | | |
| 6922 | |
6904 | 6923 | EXAMPLES: |
6905 | 6924 | |
6906 | 6925 | This displays a straight line:: |
6907 | | |
| 6926 | |
6908 | 6927 | sage: sin(2).plot((x,0,3)) |
6909 | | |
| 6928 | |
6910 | 6929 | This draws a red oscillatory curve:: |
6911 | | |
| 6930 | |
6912 | 6931 | sage: sin(x^2).plot((x,0,2*pi), rgbcolor=(1,0,0)) |
6913 | | |
| 6932 | |
6914 | 6933 | Another plot using the variable theta:: |
6915 | | |
| 6934 | |
6916 | 6935 | sage: var('theta') |
6917 | 6936 | theta |
6918 | 6937 | sage: (cos(theta) - erf(theta)).plot((theta,-2*pi,2*pi)) |
6919 | | |
| 6938 | |
6920 | 6939 | A very thick green plot with a frame:: |
6921 | | |
| 6940 | |
6922 | 6941 | sage: sin(x).plot((x,-4*pi, 4*pi), thickness=20, rgbcolor=(0,0.7,0)).show(frame=True) |
6923 | | |
| 6942 | |
6924 | 6943 | You can embed 2d plots in 3d space as follows:: |
6925 | | |
| 6944 | |
6926 | 6945 | sage: plot(sin(x^2), (x,-pi, pi), thickness=2).plot3d(z = 1) |
6927 | | |
| 6946 | |
6928 | 6947 | A more complicated family:: |
6929 | | |
| 6948 | |
6930 | 6949 | sage: G = sum([plot(sin(n*x), (x,-2*pi, 2*pi)).plot3d(z=n) for n in [0,0.1,..1]]) |
6931 | 6950 | sage: G.show(frame_aspect_ratio=[1,1,1/2]) |
6932 | | |
| 6951 | |
6933 | 6952 | A plot involving the floor function:: |
6934 | | |
| 6953 | |
6935 | 6954 | sage: plot(1.0 - x * floor(1/x), (x,0.00001,1.0)) |
6936 | | |
| 6955 | |
6937 | 6956 | Sage used to allow symbolic functions with "no arguments"; |
6938 | 6957 | this no longer works:: |
6939 | | |
| 6958 | |
6940 | 6959 | sage: plot(2*sin, -4, 4) |
6941 | 6960 | Traceback (most recent call last): |
6942 | 6961 | ... |
6943 | 6962 | TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.functions.trig.Function_sin'>' |
6944 | | |
| 6963 | |
6945 | 6964 | You should evaluate the function first:: |
6946 | 6965 | |
6947 | 6966 | sage: plot(2*sin(x), -4, 4) |
6948 | | |
6949 | | TESTS:: |
6950 | | |
| 6967 | |
| 6968 | TESTS:: |
| 6969 | |
6951 | 6970 | sage: f(x) = x*(1 - x) |
6952 | 6971 | sage: plot(f,0,1) |
6953 | 6972 | """ |
… |
… |
|
7027 | 7046 | # of an interface so we just use the fast_callable function. |
7028 | 7047 | return fast_callable(self, vars=vars) |
7029 | 7048 | |
7030 | | ############ |
| 7049 | ############ |
7031 | 7050 | # Calculus # |
7032 | 7051 | ############ |
7033 | 7052 | def sum(self, *args, **kwds): |
… |
… |
|
7195 | 7214 | return nintegral(self, *args, **kwds) |
7196 | 7215 | |
7197 | 7216 | nintegrate = nintegral |
7198 | | |
| 7217 | |
7199 | 7218 | def minpoly(self, *args, **kwds): |
7200 | 7219 | """ |
7201 | 7220 | Return the minimal polynomial of this symbolic expression. |
7202 | | |
| 7221 | |
7203 | 7222 | EXAMPLES:: |
7204 | 7223 | |
7205 | 7224 | sage: golden_ratio.minpoly() |
… |
… |
|
7305 | 7324 | is included for backward compatibility reasons only. |
7306 | 7325 | |
7307 | 7326 | EXAMPLES:: |
7308 | | |
| 7327 | |
7309 | 7328 | sage: var('x,y'); f = x + 3 < y - 2 |
7310 | 7329 | (x, y) |
7311 | 7330 | sage: f.multiply_both_sides(7) |
… |
… |
|
7320 | 7339 | Since the direction of the inequality never changes when doing |
7321 | 7340 | arithmetic with equations, you can multiply or divide the |
7322 | 7341 | equation by a quantity with unknown sign:: |
7323 | | |
| 7342 | |
7324 | 7343 | sage: f*(1+I) |
7325 | 7344 | (I + 1)*x + 3*I + 3 < (I + 1)*y - 2*I - 2 |
7326 | 7345 | sage: f = sqrt(2) + x == y^3 |
… |
… |
|
7328 | 7347 | I*x + I*sqrt(2) == I*y^3 |
7329 | 7348 | sage: f.multiply_both_sides(-1) |
7330 | 7349 | -x - sqrt(2) == -y^3 |
7331 | | |
| 7350 | |
7332 | 7351 | Note that the direction of the following inequalities is |
7333 | 7352 | not reversed:: |
7334 | | |
| 7353 | |
7335 | 7354 | sage: (x^3 + 1 > 2*sqrt(3)) * (-1) |
7336 | 7355 | -x^3 - 1 > -2*sqrt(3) |
7337 | 7356 | sage: (x^3 + 1 >= 2*sqrt(3)) * (-1) |
7338 | 7357 | -x^3 - 1 >= -2*sqrt(3) |
7339 | 7358 | sage: (x^3 + 1 <= 2*sqrt(3)) * (-1) |
7340 | | -x^3 - 1 <= -2*sqrt(3) |
| 7359 | -x^3 - 1 <= -2*sqrt(3) |
7341 | 7360 | """ |
7342 | 7361 | if not is_a_relational(self._gobj): |
7343 | 7362 | raise TypeError, "this expression must be a relation" |
7344 | 7363 | return self * x |
7345 | | |
| 7364 | |
7346 | 7365 | def divide_both_sides(self, x, checksign=None): |
7347 | 7366 | """ |
7348 | 7367 | Returns a relation obtained by dividing both sides of this |
… |
… |
|
7362 | 7381 | sage: eqn.divide_both_sides(theta) |
7363 | 7382 | (x^3 + theta)/theta < sin(theta*x)/theta |
7364 | 7383 | sage: eqn/theta |
7365 | | (x^3 + theta)/theta < sin(theta*x)/theta |
| 7384 | (x^3 + theta)/theta < sin(theta*x)/theta |
7366 | 7385 | """ |
7367 | 7386 | if not is_a_relational(self._gobj): |
7368 | 7387 | raise TypeError, "this expression must be a relation" |
… |
… |
|
7373 | 7392 | # implemented using a lot from cln, and I had to mostly delete |
7374 | 7393 | # their implementations. They are pretty specialized for |
7375 | 7394 | # physics apps, maybe. |
7376 | | # This doesn't work / isn't implemented yet / just segfaults. |
| 7395 | # This doesn't work / isn't implemented yet / just segfaults. |
7377 | 7396 | #def Li(self, x): |
7378 | 7397 | # """ |
7379 | 7398 | # """ |
… |
… |
|
7423 | 7442 | Return this iterator object itself. |
7424 | 7443 | |
7425 | 7444 | EXAMPLES:: |
7426 | | |
| 7445 | |
7427 | 7446 | sage: x,y,z = var('x,y,z') |
7428 | 7447 | sage: i = (x+y).iterator() |
7429 | 7448 | sage: iter(i) is i |
… |
… |
|
7436 | 7455 | Return the next component of the expression. |
7437 | 7456 | |
7438 | 7457 | EXAMPLES:: |
7439 | | |
| 7458 | |
7440 | 7459 | sage: x,y,z = var('x,y,z') |
7441 | 7460 | sage: i = (x+y).iterator() |
7442 | 7461 | sage: i.next() |
… |
… |
|
7454 | 7473 | Construct a new iterator over a symbolic expression. |
7455 | 7474 | |
7456 | 7475 | EXAMPLES:: |
7457 | | |
| 7476 | |
7458 | 7477 | sage: x,y,z = var('x,y,z') |
7459 | 7478 | sage: i = (x+y).iterator() #indirect doctest |
7460 | 7479 | """ |
… |
… |
|
7484 | 7503 | elif lop == not_equal or rop == not_equal: |
7485 | 7504 | raise TypeError, "incompatible relations" |
7486 | 7505 | elif lop == equal: |
7487 | | return rop |
| 7506 | return rop |
7488 | 7507 | elif rop == equal: |
7489 | | return lop |
| 7508 | return lop |
7490 | 7509 | elif lop in [less, less_or_equal] and rop in [less, less_or_equal]: |
7491 | | return less |
| 7510 | return less |
7492 | 7511 | elif lop in [greater, greater_or_equal] and rop in [greater, greater_or_equal]: |
7493 | | return greater |
| 7512 | return greater |
7494 | 7513 | else: |
7495 | 7514 | raise TypeError, "incompatible relations" |