# HG changeset patch
# User Volker Braun <vbraun@stp.dias.ie>
# Date 1308366050 25200
# Node ID d78cf5ad0709bfedb67054eff2242d5c15a90f65
# Parent 787875a79bb28246097f5bc2241d570ff93b71e6
Trac #9880: Fix doctests in expression.pyx and random_tests.py
diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
a
|
b
|
|
15 | 15 | :: |
16 | 16 | |
17 | 17 | sage: eqn^2 |
18 | | (x - 1)^4 <= (x^2 - 2*x + 3)^2 |
| 18 | (x - 1)^4 <= (-x^2 + 2*x - 3)^2 |
19 | 19 | sage: eqn.expand() |
20 | 20 | x^2 - 2*x + 1 <= x^2 - 2*x + 3 |
21 | 21 | |
… |
… |
|
78 | 78 | sage: var('a,b,c') |
79 | 79 | (a, b, c) |
80 | 80 | sage: expand((u + v + a + b + c)^2) |
81 | | a^2 + 2*a*b + 2*a*c + 2*a*u + 2*a*v + b^2 + 2*b*c + 2*b*u + 2*b*v + c^2 + 2*c*u + 2*c*v + u^2 + 2*u*v + v^2 |
| 81 | a^2 + b^2 + 2*a*b + c^2 + 2*a*c + 2*b*c + u^2 + 2*a*u + 2*b*u + 2*c*u + v^2 + 2*a*v + 2*b*v + 2*c*v + 2*u*v |
82 | 82 | |
83 | 83 | TESTS: |
84 | 84 | |
… |
… |
|
108 | 108 | sage: t*u |
109 | 109 | 1 |
110 | 110 | sage: t + u |
111 | | e^sqrt(x) + e^(-sqrt(x)) |
| 111 | e^(-sqrt(x)) + e^sqrt(x) |
112 | 112 | sage: t |
113 | 113 | e^sqrt(x) |
114 | 114 | |
… |
… |
|
207 | 207 | |
208 | 208 | OUTPUT: |
209 | 209 | |
210 | | The python object corresponding to this expression, assuming |
211 | | this expression is a single numerical value. Otherwise, a |
212 | | TypeError is raised. |
| 210 | The Python object corresponding to this expression, assuming |
| 211 | this expression is a single numerical value or an infinity |
| 212 | representable in Python. Otherwise, a ``TypeError`` is raised. |
213 | 213 | |
214 | 214 | EXAMPLES:: |
215 | 215 | |
… |
… |
|
233 | 233 | sage: SR(I*oo).pyobject() |
234 | 234 | Traceback (most recent call last): |
235 | 235 | ... |
236 | | ValueError: Python infinity cannot have complex phase. |
| 236 | TypeError: Python infinity cannot have complex phase. |
237 | 237 | """ |
238 | 238 | cdef GConstant* c |
239 | 239 | if is_a_constant(self._gobj): |
… |
… |
|
328 | 328 | sage: t = 2*x*y^z+3 |
329 | 329 | sage: u = loads(dumps(t)) # indirect doctest |
330 | 330 | sage: u |
331 | | 2*y^z*x + 3 |
| 331 | 2*x*y^z + 3 |
332 | 332 | sage: bool(t == u) |
333 | 333 | True |
334 | 334 | sage: u.subs(x=z) |
… |
… |
|
370 | 370 | |
371 | 371 | EXAMPLES:: |
372 | 372 | |
373 | | sage: (1+x)._dbgprint() |
| 373 | sage: (1+x)._dbgprint() # random output |
374 | 374 | x + 1 |
375 | 375 | """ |
376 | 376 | self._gobj.dbgprint(); |
… |
… |
|
557 | 557 | EXAMPLES:: |
558 | 558 | |
559 | 559 | sage: gap(e + pi^2 + x^3) |
560 | | pi^2 + x^3 + e |
| 560 | x^3 + pi^2 + e |
561 | 561 | """ |
562 | 562 | return '"%s"'%repr(self) |
563 | 563 | |
… |
… |
|
569 | 569 | EXAMPLES:: |
570 | 570 | |
571 | 571 | sage: singular(e + pi^2 + x^3) |
572 | | pi^2 + x^3 + e |
| 572 | x^3 + pi^2 + e |
573 | 573 | """ |
574 | 574 | return '"%s"'%repr(self) |
575 | 575 | |
… |
… |
|
585 | 585 | sage: x = var('x') |
586 | 586 | sage: f = sin(cos(x^2) + log(x)) |
587 | 587 | sage: f._magma_init_(magma) |
588 | | '"sin(log(x) + cos(x^2))"' |
| 588 | '"sin(cos(x^2) + log(x))"' |
589 | 589 | sage: magma(f) # optional - magma |
590 | 590 | sin(log(x) + cos(x^2)) |
591 | 591 | sage: magma(f).Type() # optional - magma |
… |
… |
|
604 | 604 | sage: latex(y + 3*(x^(-1))) |
605 | 605 | y + \frac{3}{x} |
606 | 606 | sage: latex(x^(y+z^(1/y))) |
607 | | x^{z^{\left(\frac{1}{y}\right)} + y} |
| 607 | x^{y + z^{\left(\frac{1}{y}\right)}} |
608 | 608 | sage: latex(1/sqrt(x+y)) |
609 | 609 | \frac{1}{\sqrt{x + y}} |
610 | 610 | sage: latex(sin(x*(z+y)^x)) |
611 | | \sin\left({\left(y + z\right)}^{x} x\right) |
| 611 | \sin\left(x {\left(y + z\right)}^{x}\right) |
612 | 612 | sage: latex(3/2*(x+y)/z/y) |
613 | 613 | \frac{3 \, {\left(x + y\right)}}{2 \, y z} |
614 | 614 | sage: latex((2^(x^y))) |
… |
… |
|
618 | 618 | sage: latex((x*y).conjugate()) |
619 | 619 | \overline{x} \overline{y} |
620 | 620 | sage: latex(x*(1/(x^2)+sqrt(x^7))) |
621 | | {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} x |
| 621 | x {\left(\sqrt{x^{7}} + \frac{1}{x^{2}}\right)} |
622 | 622 | |
623 | 623 | Check spacing of coefficients of mul expressions (#3202):: |
624 | 624 | |
… |
… |
|
683 | 683 | sage: latex((x+2)*(x+1)/(x^3+1)) |
684 | 684 | \frac{{\left(x + 1\right)} {\left(x + 2\right)}}{x^{3} + 1} |
685 | 685 | sage: latex((x+2)/(x^3+1)/(x+1)) |
686 | | \frac{x + 2}{{\left(x + 1\right)} {\left(x^{3} + 1\right)}} |
| 686 | \frac{x + 2}{{\left(x^{3} + 1\right)} {\left(x + 1\right)}} |
687 | 687 | |
688 | 688 | Check that the sign is correct (#9086):: |
689 | 689 | |
… |
… |
|
1210 | 1210 | sage: x^3 -y == y + x |
1211 | 1211 | x^3 - y == x + y |
1212 | 1212 | sage: x^3 - y^10 >= y + x^10 |
1213 | | x^3 - y^10 >= x^10 + y |
| 1213 | -y^10 + x^3 >= x^10 + y |
1214 | 1214 | sage: x^2 > x |
1215 | 1215 | x^2 > x |
1216 | 1216 | """ |
… |
… |
|
1290 | 1290 | sage: v,c = var('v,c') |
1291 | 1291 | sage: assume(c != 0) |
1292 | 1292 | sage: integral((1+v^2/c^2)^3/(1-v^2/c^2)^(3/2),v) |
1293 | | -75/8*sqrt(c^2)*arcsin(sqrt(c^2)*v/c^2) - 17/8*v^3/(sqrt(-v^2/c^2 + 1)*c^2) - 1/4*v^5/(sqrt(-v^2/c^2 + 1)*c^4) + 83/8*v/sqrt(-v^2/c^2 + 1) |
| 1293 | -75/8*sqrt(c^2)*arcsin(sqrt(c^2)*v/c^2) - 1/4*v^5/(c^4*sqrt(-v^2/c^2 + 1)) - 17/8*v^3/(c^2*sqrt(-v^2/c^2 + 1)) + 83/8*v/sqrt(-v^2/c^2 + 1) |
1294 | 1294 | sage: forget() |
1295 | 1295 | """ |
1296 | 1296 | from sage.symbolic.assumptions import _assumptions |
… |
… |
|
2225 | 2225 | sage: nsr(oo) - nsr(oo) |
2226 | 2226 | Traceback (most recent call last): |
2227 | 2227 | ... |
2228 | | RuntimeError: indeterminate expression: Infinity - Infinity encountered. |
| 2228 | RuntimeError: indeterminate expression: infinity - infinity encountered. |
2229 | 2229 | sage: nsr(-oo) - nsr(-oo) |
2230 | 2230 | Traceback (most recent call last): |
2231 | 2231 | ... |
2232 | | RuntimeError: indeterminate expression: Infinity - Infinity encountered. |
| 2232 | RuntimeError: indeterminate expression: infinity - infinity encountered. |
2233 | 2233 | |
2234 | 2234 | sage: nsr(unsigned_infinity) + nsr(oo) |
2235 | 2235 | Traceback (most recent call last): |
2236 | 2236 | ... |
2237 | | RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. |
| 2237 | RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. |
2238 | 2238 | sage: nsr(unsigned_infinity) - nsr(oo) |
2239 | 2239 | Traceback (most recent call last): |
2240 | 2240 | ... |
2241 | | RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. |
| 2241 | RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. |
2242 | 2242 | sage: nsr(oo) + nsr(unsigned_infinity) |
2243 | 2243 | Traceback (most recent call last): |
2244 | 2244 | ... |
2245 | | RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. |
| 2245 | RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. |
2246 | 2246 | sage: nsr(oo) - nsr(unsigned_infinity) |
2247 | 2247 | Traceback (most recent call last): |
2248 | 2248 | ... |
2249 | | RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. |
| 2249 | RuntimeError: indeterminate expression: unsigned_infinity +- infinity encountered. |
2250 | 2250 | sage: nsr(unsigned_infinity) + nsr(unsigned_infinity) |
2251 | | Traceback (most recent call last): |
2252 | | ... |
2253 | | RuntimeError: indeterminate expression: unsigned_infinity + x where x is Infinity, -Infinity or unsigned infinity encountered. |
2254 | | |
2255 | | |
| 2251 | Infinity |
2256 | 2252 | """ |
2257 | 2253 | cdef GEx x |
2258 | 2254 | cdef Expression _right = <Expression>right |
… |
… |
|
2408 | 2404 | :: |
2409 | 2405 | |
2410 | 2406 | sage: x*oo |
2411 | | +Infinity |
2412 | | sage: -x*oo |
2413 | | -Infinity |
| 2407 | Traceback (most recent call last): |
| 2408 | ... |
| 2409 | ArithmeticError: indeterminate expression: infinity * f(x) encountered. |
2414 | 2410 | sage: x*unsigned_infinity |
2415 | 2411 | Traceback (most recent call last): |
2416 | 2412 | ... |
… |
… |
|
2483 | 2479 | sage: x/oo |
2484 | 2480 | 0 |
2485 | 2481 | sage: oo/x |
2486 | | +Infinity |
| 2482 | Traceback (most recent call last): |
| 2483 | ... |
| 2484 | RuntimeError: indeterminate expression: infinity * f(x) encountered. |
2487 | 2485 | |
2488 | 2486 | sage: SR(oo)/SR(oo) |
2489 | 2487 | Traceback (most recent call last): |
2490 | 2488 | ... |
2491 | | RuntimeError: indeterminate expression: 0*infinity encountered. |
| 2489 | RuntimeError: indeterminate expression: 0 * infinity encountered. |
2492 | 2490 | |
2493 | 2491 | sage: SR(-oo)/SR(oo) |
2494 | 2492 | Traceback (most recent call last): |
2495 | 2493 | ... |
2496 | | RuntimeError: indeterminate expression: 0*infinity encountered. |
| 2494 | RuntimeError: indeterminate expression: 0 * infinity encountered. |
2497 | 2495 | |
2498 | 2496 | sage: SR(oo)/SR(-oo) |
2499 | 2497 | Traceback (most recent call last): |
2500 | 2498 | ... |
2501 | | RuntimeError: indeterminate expression: 0*infinity encountered. |
| 2499 | RuntimeError: indeterminate expression: 0 * infinity encountered. |
2502 | 2500 | |
2503 | 2501 | sage: SR(oo)/SR(unsigned_infinity) |
2504 | 2502 | Traceback (most recent call last): |
2505 | 2503 | ... |
2506 | | RuntimeError: indeterminate expression: 0*infinity encountered. |
| 2504 | RuntimeError: indeterminate expression: 0 * infinity encountered. |
2507 | 2505 | |
2508 | 2506 | sage: SR(unsigned_infinity)/SR(oo) |
2509 | 2507 | Traceback (most recent call last): |
2510 | 2508 | ... |
2511 | | RuntimeError: indeterminate expression: 0*infinity encountered. |
| 2509 | RuntimeError: indeterminate expression: 0 * infinity encountered. |
2512 | 2510 | |
2513 | 2511 | sage: SR(0)/SR(oo) |
2514 | 2512 | 0 |
… |
… |
|
2621 | 2619 | sage: a = sqrt(3) |
2622 | 2620 | sage: b = x^2+1 |
2623 | 2621 | sage: a.__cmp__(b) # indirect doctest |
2624 | | -1 |
| 2622 | 1 |
2625 | 2623 | """ |
2626 | 2624 | return print_order_compare(left._gobj, (<Expression>right)._gobj) |
2627 | 2625 | |
… |
… |
|
2727 | 2725 | sage: x^oo |
2728 | 2726 | Traceback (most recent call last): |
2729 | 2727 | ... |
2730 | | RuntimeError: power::eval(): pow(x, Infinity) for non numeric x is not defined. |
| 2728 | RuntimeError: power::eval(): pow(f(x), infinity) is not defined. |
2731 | 2729 | sage: SR(oo)^2 |
2732 | 2730 | +Infinity |
2733 | 2731 | sage: SR(-oo)^2 |
… |
… |
|
2821 | 2819 | (x, y) |
2822 | 2820 | sage: u = (sin(x) + cos(y))*(cos(x) - sin(y)) |
2823 | 2821 | sage: derivative(u,x,y) |
2824 | | sin(x)*sin(y) - cos(x)*cos(y) |
| 2822 | -cos(x)*cos(y) + sin(x)*sin(y) |
2825 | 2823 | sage: f = ((x^2+1)/(x^2-1))^(1/4) |
2826 | 2824 | sage: g = derivative(f, x); g # this is a complex expression |
2827 | 2825 | 1/2*(x/(x^2 - 1) - (x^2 + 1)*x/(x^2 - 1)^2)/((x^2 + 1)/(x^2 - 1))^(3/4) |
… |
… |
|
2833 | 2831 | sage: y = var('y') |
2834 | 2832 | sage: f = y^(sin(x)) |
2835 | 2833 | sage: derivative(f, x) |
2836 | | y^sin(x)*log(y)*cos(x) |
| 2834 | y^sin(x)*cos(x)*log(y) |
2837 | 2835 | |
2838 | 2836 | :: |
2839 | 2837 | |
… |
… |
|
2851 | 2849 | |
2852 | 2850 | sage: g = 1/(sqrt((x^2-1)*(x+5)^6)) |
2853 | 2851 | sage: derivative(g, x) |
2854 | | -((x + 5)^6*x + 3*(x + 5)^5*(x^2 - 1))/((x + 5)^6*(x^2 - 1))^(3/2) |
| 2852 | -((x + 5)^6*x + 3*(x^2 - 1)*(x + 5)^5)/((x^2 - 1)*(x + 5)^6)^(3/2) |
2855 | 2853 | |
2856 | 2854 | TESTS:: |
2857 | 2855 | |
… |
… |
|
3025 | 3023 | sage: g = f.series(x==1, 4); g |
3026 | 3024 | (-sin(y) - 1) + (-2*sin(y) - 2)*(x - 1) + (-sin(y) + 3)*(x - 1)^2 + 1*(x - 1)^3 |
3027 | 3025 | sage: h = g.truncate(); h |
3028 | | -(sin(y) - 3)*(x - 1)^2 + (x - 1)^3 - 2*(sin(y) + 1)*(x - 1) - sin(y) - 1 |
| 3026 | (x - 1)^3 - (x - 1)^2*(-sin(y) - 3) - 2*(x - 1)*(-2*sin(y) + 1) - sin(y) - 1 |
3029 | 3027 | sage: h.expand() |
3030 | 3028 | x^3 - x^2*sin(y) - 5*x + 3 |
3031 | 3029 | |
… |
… |
|
3035 | 3033 | sage: f.series(x,7) |
3036 | 3034 | 1*x^(-1) + (-1/6)*x + 1/120*x^3 + (-1/5040)*x^5 + Order(x^7) |
3037 | 3035 | sage: f.series(x==1,3) |
3038 | | (sin(1)) + (-2*sin(1) + cos(1))*(x - 1) + (5/2*sin(1) - 2*cos(1))*(x - 1)^2 + Order((x - 1)^3) |
| 3036 | (sin(1)) + (cos(1) - 2*sin(1))*(x - 1) + (-2*cos(1) + 5/2*sin(1))*(x - 1)^2 + Order((x - 1)^3) |
3039 | 3037 | sage: f.series(x==1,3).truncate().expand() |
3040 | | 5/2*x^2*sin(1) - 2*x^2*cos(1) - 7*x*sin(1) + 5*x*cos(1) + 11/2*sin(1) - 3*cos(1) |
| 3038 | -2*x^2*cos(1) + 5/2*x^2*sin(1) + 5*x*cos(1) - 7*x*sin(1) - 3*cos(1) + 11/2*sin(1) |
3041 | 3039 | |
3042 | 3040 | Following the GiNaC tutorial, we use John Machin's amazing |
3043 | 3041 | formula `\pi = 16 \tan^{-1}(1/5) - 4 \tan^{-1}(1/239)` to compute |
… |
… |
|
3088 | 3086 | sage: var('a, x, z') |
3089 | 3087 | (a, x, z) |
3090 | 3088 | sage: taylor(a*log(z), z, 2, 3) |
3091 | | 1/24*(z - 2)^3*a - 1/8*(z - 2)^2*a + 1/2*(z - 2)*a + a*log(2) |
| 3089 | 1/24*a*(z - 2)^3 - 1/8*a*(z - 2)^2 + 1/2*a*(z - 2) + a*log(2) |
3092 | 3090 | |
3093 | 3091 | :: |
3094 | 3092 | |
… |
… |
|
3124 | 3122 | Ticket #7472 fixed (Taylor polynomial in more variables) :: |
3125 | 3123 | |
3126 | 3124 | sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,1),4) |
3127 | | (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 |
| 3125 | (x - 1)*(y - 1)^3 + (y - 1)^3 + 3*(x - 1)*(y - 1)^2 + 3*(y - 1)^2 + 3*(x - 1)*(y - 1) + x + 3*y - 3 |
3128 | 3126 | sage: expand(_) |
3129 | 3127 | x*y^3 |
3130 | 3128 | |
… |
… |
|
3169 | 3167 | sage: f.series(x,7).truncate() |
3170 | 3168 | -1/5040*x^5 + 1/120*x^3 - 1/6*x + 1/x |
3171 | 3169 | sage: f.series(x==1,3).truncate().expand() |
3172 | | 5/2*x^2*sin(1) - 2*x^2*cos(1) - 7*x*sin(1) + 5*x*cos(1) + 11/2*sin(1) - 3*cos(1) |
| 3170 | -2*x^2*cos(1) + 5/2*x^2*sin(1) + 5*x*cos(1) - 7*x*sin(1) - 3*cos(1) + 11/2*sin(1) |
3173 | 3171 | """ |
3174 | 3172 | if not is_a_series(self._gobj): |
3175 | 3173 | return self |
… |
… |
|
3192 | 3190 | sage: x,y = var('x,y') |
3193 | 3191 | sage: a = (x-y)^5 |
3194 | 3192 | sage: a.expand() |
3195 | | x^5 - 5*x^4*y + 10*x^3*y^2 - 10*x^2*y^3 + 5*x*y^4 - y^5 |
| 3193 | x^5 - y^5 + 5*x*y^4 - 10*x^2*y^3 + 10*x^3*y^2 - 5*x^4*y |
3196 | 3194 | sage: expand(a) |
3197 | | x^5 - 5*x^4*y + 10*x^3*y^2 - 10*x^2*y^3 + 5*x*y^4 - y^5 |
| 3195 | x^5 - y^5 + 5*x*y^4 - 10*x^2*y^3 + 10*x^3*y^2 - 5*x^4*y |
3198 | 3196 | |
3199 | 3197 | We expand some other expressions:: |
3200 | 3198 | |
3201 | 3199 | sage: expand((x-1)^3/(y-1)) |
3202 | 3200 | x^3/(y - 1) - 3*x^2/(y - 1) + 3*x/(y - 1) - 1/(y - 1) |
3203 | 3201 | sage: expand((x+sin((x+y)^2))^2) |
3204 | | x^2 + 2*x*sin((x + y)^2) + sin((x + y)^2)^2 |
| 3202 | x^2 + sin((x + y)^2)^2 + 2*x*sin((x + y)^2) |
3205 | 3203 | |
3206 | 3204 | We can expand individual sides of a relation:: |
3207 | 3205 | |
… |
… |
|
3218 | 3216 | sage: var('x,y') |
3219 | 3217 | (x, y) |
3220 | 3218 | sage: ((x + (2/3)*y)^3).expand() |
3221 | | x^3 + 2*x^2*y + 4/3*x*y^2 + 8/27*y^3 |
| 3219 | x^3 + 8/27*y^3 + 4/3*x*y^2 + 2*x^2*y |
3222 | 3220 | sage: expand( (x*sin(x) - cos(y)/x)^2 ) |
3223 | | x^2*sin(x)^2 - 2*sin(x)*cos(y) + cos(y)^2/x^2 |
| 3221 | x^2*sin(x)^2 - 2*cos(y)*sin(x) + cos(y)^2/x^2 |
3224 | 3222 | sage: f = (x-y)*(x+y); f |
3225 | 3223 | (x - y)*(x + y) |
3226 | 3224 | sage: f.expand() |
… |
… |
|
3279 | 3277 | EXAMPLES:: |
3280 | 3278 | |
3281 | 3279 | sage: sin(5*x).expand_trig() |
3282 | | sin(x)^5 - 10*sin(x)^3*cos(x)^2 + 5*sin(x)*cos(x)^4 |
| 3280 | sin(x)^5 - 10*cos(x)^2*sin(x)^3 + 5*cos(x)^4*sin(x) |
3283 | 3281 | sage: cos(2*x + var('y')).expand_trig() |
3284 | | -sin(2*x)*sin(y) + cos(2*x)*cos(y) |
| 3282 | cos(2*x)*cos(y) - sin(2*x)*sin(y) |
3285 | 3283 | |
3286 | 3284 | We illustrate various options to this function:: |
3287 | 3285 | |
3288 | 3286 | sage: f = sin(sin(3*cos(2*x))*x) |
3289 | 3287 | sage: f.expand_trig() |
3290 | | sin(-(sin(cos(2*x))^3 - 3*sin(cos(2*x))*cos(cos(2*x))^2)*x) |
| 3288 | sin(-(sin(cos(2*x))^3 - 3*cos(cos(2*x))^2*sin(cos(2*x)))*x) |
3291 | 3289 | sage: f.expand_trig(full=True) |
3292 | | sin(((sin(sin(x)^2)*cos(cos(x)^2) - sin(cos(x)^2)*cos(sin(x)^2))^3 - 3*(sin(sin(x)^2)*cos(cos(x)^2) - sin(cos(x)^2)*cos(sin(x)^2))*(sin(sin(x)^2)*sin(cos(x)^2) + cos(sin(x)^2)*cos(cos(x)^2))^2)*x) |
| 3290 | sin(-((cos(sin(x)^2)*sin(cos(x)^2) - cos(cos(x)^2)*sin(sin(x)^2))^3 - 3*(cos(cos(x)^2)*cos(sin(x)^2) + sin(cos(x)^2)*sin(sin(x)^2))^2*(cos(sin(x)^2)*sin(cos(x)^2) - cos(cos(x)^2)*sin(sin(x)^2)))*x) |
3293 | 3291 | sage: sin(2*x).expand_trig(times=False) |
3294 | 3292 | sin(2*x) |
3295 | 3293 | sage: sin(2*x).expand_trig(times=True) |
3296 | | 2*sin(x)*cos(x) |
| 3294 | 2*cos(x)*sin(x) |
3297 | 3295 | sage: sin(2 + x).expand_trig(plus=False) |
3298 | 3296 | sin(x + 2) |
3299 | 3297 | sage: sin(2 + x).expand_trig(plus=True) |
3300 | | sin(2)*cos(x) + sin(x)*cos(2) |
| 3298 | cos(x)*sin(2) + cos(2)*sin(x) |
3301 | 3299 | sage: sin(x/2).expand_trig(half_angles=False) |
3302 | 3300 | sin(1/2*x) |
3303 | 3301 | sage: sin(x/2).expand_trig(half_angles=True) |
3304 | | 1/2*sqrt(-cos(x) + 1)*sqrt(2)*(-1)^floor(1/2*x/pi) |
| 3302 | 1/2*sqrt(2)*(-1)^floor(1/2*x/pi)*sqrt(-cos(x) + 1) |
3305 | 3303 | |
3306 | 3304 | ALIASES: |
3307 | 3305 | |
… |
… |
|
3342 | 3340 | sage: y=var('y') |
3343 | 3341 | sage: f=sin(x)*cos(x)^3+sin(y)^2 |
3344 | 3342 | sage: f.reduce_trig() |
3345 | | 1/4*sin(2*x) + 1/8*sin(4*x) - 1/2*cos(2*y) + 1/2 |
| 3343 | -1/2*cos(2*y) + 1/4*sin(2*x) + 1/8*sin(4*x) + 1/2 |
3346 | 3344 | |
3347 | 3345 | To reduce only the expressions involving x we use optional parameter:: |
3348 | 3346 | |
… |
… |
|
3403 | 3401 | {$0: x + y} |
3404 | 3402 | sage: t = ((a+b)*(a+c)).match((a+w0)*(a+w1)) |
3405 | 3403 | sage: t[w0], t[w1] |
3406 | | (b, c) |
| 3404 | (c, b) |
3407 | 3405 | sage: ((a+b)*(a+c)).match((w0+b)*(w0+c)) |
3408 | 3406 | {$0: a} |
3409 | | sage: print ((a+b)*(a+c)).match((w0+w1)*(w0+w2)) # surprising? |
3410 | | None |
| 3407 | sage: print ((a+b)*(a+c)).match((w0+w1)*(w0+w2)) |
| 3408 | {$2: b, $0: a, $1: c} |
3411 | 3409 | sage: t = (a*(x+y)+a*z+b).match(a*w0+w1) |
3412 | 3410 | sage: t[w0], t[w1] |
3413 | | (x + y, a*z + b) |
| 3411 | (z, a*(x + y) + b) |
3414 | 3412 | sage: print (a+b+c+d+e+f).match(c) |
3415 | 3413 | None |
3416 | 3414 | sage: (a+b+c+d+e+f).has(c) |
… |
… |
|
3567 | 3565 | (x + y)^3 + b^2 + c |
3568 | 3566 | |
3569 | 3567 | sage: t.subs({w0^2: w0^3}) |
3570 | | (x + y)^3 + a^3 + b^3 |
| 3568 | a^3 + b^3 + (x + y)^3 |
3571 | 3569 | |
3572 | 3570 | # substitute with a relational expression |
3573 | 3571 | sage: t.subs(w0^2 == w0^3) |
3574 | | (x + y)^3 + a^3 + b^3 |
| 3572 | a^3 + b^3 + (x + y)^3 |
3575 | 3573 | |
3576 | 3574 | sage: t.subs(w0==w0^2) |
3577 | 3575 | (x^2 + y^2)^18 + a^16 + b^16 |
… |
… |
|
3611 | 3609 | sage: (x/y).subs(y=oo) |
3612 | 3610 | 0 |
3613 | 3611 | sage: (x/y).subs(x=oo) |
3614 | | +Infinity |
3615 | | sage: (x*y).subs(x=oo) |
3616 | | +Infinity |
| 3612 | Traceback (most recent call last): |
| 3613 | ... |
| 3614 | RuntimeError: indeterminate expression: infinity * f(x) encountered. |
3617 | 3615 | sage: (x^y).subs(x=oo) |
3618 | 3616 | Traceback (most recent call last): |
3619 | 3617 | ... |
3620 | | RuntimeError: power::eval(): pow(Infinity, x) for non numeric x is not defined. |
| 3618 | RuntimeError: power::eval(): pow(Infinity, f(x)) is not defined. |
3621 | 3619 | sage: (x^y).subs(y=oo) |
3622 | 3620 | Traceback (most recent call last): |
3623 | 3621 | ... |
3624 | | RuntimeError: power::eval(): pow(x, Infinity) for non numeric x is not defined. |
| 3622 | RuntimeError: power::eval(): pow(f(x), infinity) is not defined. |
3625 | 3623 | sage: (x+y).subs(x=oo) |
3626 | 3624 | +Infinity |
3627 | 3625 | sage: (x-y).subs(y=oo) |
… |
… |
|
3676 | 3674 | (x, y, z, a, b, c, d, e, f) |
3677 | 3675 | sage: w0 = SR.wild(0); w1 = SR.wild(1) |
3678 | 3676 | sage: (a^2 + b^2 + (x+y)^2)._subs_expr(w0^2 == w0^3) |
3679 | | (x + y)^3 + a^3 + b^3 |
| 3677 | a^3 + b^3 + (x + y)^3 |
3680 | 3678 | sage: (a^4 + b^4 + (x+y)^4)._subs_expr(w0^2 == w0^3) |
3681 | | (x + y)^4 + a^4 + b^4 |
| 3679 | a^4 + b^4 + (x + y)^4 |
3682 | 3680 | sage: (a^2 + b^4 + (x+y)^4)._subs_expr(w0^2 == w0^3) |
3683 | | (x + y)^4 + a^3 + b^4 |
| 3681 | b^4 + (x + y)^4 + a^3 |
3684 | 3682 | sage: ((a+b+c)^2)._subs_expr(a+b == x) |
3685 | 3683 | (a + b + c)^2 |
3686 | 3684 | sage: ((a+b+c)^2)._subs_expr(a+b+w0 == x+w0) |
… |
… |
|
3704 | 3702 | sage: (sin(x)^2 + cos(x)^2)._subs_expr(sin(w0)^2+cos(w0)^2==1) |
3705 | 3703 | 1 |
3706 | 3704 | sage: (1 + sin(x)^2 + cos(x)^2)._subs_expr(sin(w0)^2+cos(w0)^2==1) |
3707 | | sin(x)^2 + cos(x)^2 + 1 |
| 3705 | cos(x)^2 + sin(x)^2 + 1 |
3708 | 3706 | sage: (17*x + sin(x)^2 + cos(x)^2)._subs_expr(w1 + sin(w0)^2+cos(w0)^2 == w1 + 1) |
3709 | 3707 | 17*x + 1 |
3710 | 3708 | sage: ((x-1)*(sin(x)^2 + cos(x)^2)^2)._subs_expr(sin(w0)^2+cos(w0)^2 == 1) |
… |
… |
|
3751 | 3749 | x^4 + x |
3752 | 3750 | sage: f = cos(x^2) + sin(x^2) |
3753 | 3751 | sage: f.subs_expr(x^2 == x) |
3754 | | sin(x) + cos(x) |
| 3752 | cos(x) + sin(x) |
3755 | 3753 | |
3756 | 3754 | :: |
3757 | 3755 | |
3758 | 3756 | sage: f(x,y,t) = cos(x) + sin(y) + x^2 + y^2 + t |
3759 | 3757 | sage: f.subs_expr(y^2 == t) |
3760 | | (x, y, t) |--> x^2 + 2*t + sin(y) + cos(x) |
| 3758 | (x, y, t) |--> x^2 + 2*t + cos(x) + sin(y) |
3761 | 3759 | |
3762 | 3760 | The following seems really weird, but it *is* what Maple does:: |
3763 | 3761 | |
3764 | 3762 | sage: f.subs_expr(x^2 + y^2 == t) |
3765 | | (x, y, t) |--> x^2 + y^2 + t + sin(y) + cos(x) |
| 3763 | (x, y, t) |--> x^2 + y^2 + t + cos(x) + sin(y) |
3766 | 3764 | sage: maple.eval('subs(x^2 + y^2 = t, cos(x) + sin(y) + x^2 + y^2 + t)') # optional requires maple |
3767 | 3765 | 'cos(x)+sin(y)+x^2+y^2+t' |
3768 | 3766 | sage: maxima.quit() |
… |
… |
|
3811 | 3809 | sage: var('x,y,z') |
3812 | 3810 | (x, y, z) |
3813 | 3811 | sage: (x+y)(x=z^2, y=x^y) |
3814 | | x^y + z^2 |
| 3812 | z^2 + x^y |
3815 | 3813 | """ |
3816 | 3814 | return self._parent._call_element_(self, *args, **kwds) |
3817 | 3815 | |
… |
… |
|
3949 | 3947 | sage: var('a,b,c,x,y') |
3950 | 3948 | (a, b, c, x, y) |
3951 | 3949 | sage: (a^2 + b^2 + (x+y)^2).operands() |
3952 | | [(x + y)^2, a^2, b^2] |
| 3950 | [a^2, b^2, (x + y)^2] |
3953 | 3951 | sage: (a^2).operands() |
3954 | 3952 | [a, 2] |
3955 | 3953 | sage: (a*b^2*c).operands() |
… |
… |
|
4355 | 4353 | sage: x.add(x, hold=True) |
4356 | 4354 | x + x |
4357 | 4355 | sage: x.add(x, (2+x), hold=True) |
4358 | | x + x + (x + 2) |
| 4356 | (x + 2) + x + x |
4359 | 4357 | sage: x.add(x, (2+x), x, hold=True) |
4360 | | x + x + (x + 2) + x |
| 4358 | (x + 2) + x + x + x |
4361 | 4359 | sage: x.add(x, (2+x), x, 2*x, hold=True) |
4362 | | x + x + (x + 2) + x + 2*x |
| 4360 | (x + 2) + 2*x + x + x + x |
4363 | 4361 | |
4364 | 4362 | To then evaluate again, we currently must use Maxima via |
4365 | 4363 | :meth:`simplify`:: |
… |
… |
|
4388 | 4386 | sage: x.mul(x, hold=True) |
4389 | 4387 | x*x |
4390 | 4388 | sage: x.mul(x, (2+x), hold=True) |
4391 | | x*x*(x + 2) |
| 4389 | (x + 2)*x*x |
4392 | 4390 | sage: x.mul(x, (2+x), x, hold=True) |
4393 | | x*x*(x + 2)*x |
| 4391 | (x + 2)*x*x*x |
4394 | 4392 | sage: x.mul(x, (2+x), x, 2*x, hold=True) |
4395 | | x*x*(x + 2)*x*(2*x) |
| 4393 | (2*x)*(x + 2)*x*x*x |
4396 | 4394 | |
4397 | 4395 | To then evaluate again, we currently must use Maxima via |
4398 | 4396 | :meth:`simplify`:: |
… |
… |
|
4452 | 4450 | sage: f.coefficient(sin(x*y)) |
4453 | 4451 | x^3 + 2/x |
4454 | 4452 | sage: f.collect(sin(x*y)) |
4455 | | (x^3 + 2/x)*sin(x*y) + a*x + x*y + x/y + 100 |
| 4453 | a*x + x*y + (x^3 + 2/x)*sin(x*y) + x/y + 100 |
4456 | 4454 | |
4457 | 4455 | sage: var('a, x, y, z') |
4458 | 4456 | (a, x, y, z) |
… |
… |
|
4639 | 4637 | sage: bool(p.poly(a) == (x-a*sqrt(2))^2 + x + 1) |
4640 | 4638 | True |
4641 | 4639 | sage: p.poly(x) |
4642 | | -(2*sqrt(2)*a - 1)*x + 2*a^2 + x^2 + 1 |
| 4640 | 2*a^2 + x^2 - (-2*sqrt(2)*a - 1)*x + 1 |
4643 | 4641 | """ |
4644 | 4642 | from sage.symbolic.ring import SR |
4645 | 4643 | f = self._maxima_() |
… |
… |
|
4785 | 4783 | sage: R = SR[x] |
4786 | 4784 | sage: a = R(sqrt(2) + x^3 + y) |
4787 | 4785 | sage: a |
4788 | | y + sqrt(2) + x^3 |
| 4786 | x^3 + y + sqrt(2) |
4789 | 4787 | sage: type(a) |
4790 | 4788 | <class 'sage.rings.polynomial.polynomial_element_generic.Polynomial_generic_dense_field'> |
4791 | 4789 | sage: a.degree() |
… |
… |
|
4939 | 4937 | (x, y, z) |
4940 | 4938 | sage: f = 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2 + x^2*y^2*z^2 |
4941 | 4939 | sage: f.collect(x) |
4942 | | x^2*y^2*z^2 + (4*y + z)*x + 20*y^2 + 21*y*z + 4*z^2 |
| 4940 | x^2*y^2*z^2 + 20*y^2 + x*(4*y + z) + 4*z^2 + 21*y*z |
4943 | 4941 | sage: f.collect(y) |
4944 | | (x^2*z^2 + 20)*y^2 + (4*x + 21*z)*y + x*z + 4*z^2 |
| 4942 | (x^2*z^2 + 20)*y^2 + (4*x + 21*z)*y + 4*z^2 + x*z |
4945 | 4943 | sage: f.collect(z) |
4946 | | (x^2*y^2 + 4)*z^2 + (x + 21*y)*z + 4*x*y + 20*y^2 |
| 4944 | (x^2*y^2 + 4)*z^2 + 20*y^2 + 4*x*y + (x + 21*y)*z |
4947 | 4945 | """ |
4948 | 4946 | cdef Expression s0 = self.coerce_in(s) |
4949 | 4947 | cdef GEx x |
… |
… |
|
5276 | 5274 | (a, b) |
5277 | 5275 | sage: f = log(a + b*I) |
5278 | 5276 | sage: f.imag_part() |
5279 | | arctan2(real_part(b) + imag_part(a), real_part(a) - imag_part(b)) |
| 5277 | arctan2(imag_part(a) + real_part(b), -imag_part(b) + real_part(a)) |
5280 | 5278 | |
5281 | 5279 | Using the ``hold`` parameter it is possible to prevent automatic |
5282 | 5280 | evaluation:: |
… |
… |
|
5753 | 5751 | sage: SR(1).arctan2(CDF(0,1)) |
5754 | 5752 | arctan2(1, 1.0*I) |
5755 | 5753 | |
| 5754 | sage: arctan2(0,oo) |
| 5755 | 0 |
5756 | 5756 | sage: SR(oo).arctan2(oo) |
5757 | | Traceback (most recent call last): |
5758 | | ... |
5759 | | RuntimeError: arctan2_eval(): arctan2(infinity, infinity) encountered |
| 5757 | 1/4*pi |
5760 | 5758 | sage: SR(oo).arctan2(0) |
5761 | | 0 |
| 5759 | 1/2*pi |
5762 | 5760 | sage: SR(-oo).arctan2(0) |
| 5761 | -1/2*pi |
| 5762 | sage: SR(-oo).arctan2(-2) |
5763 | 5763 | pi |
5764 | | sage: SR(-oo).arctan2(-2) |
5765 | | -pi |
5766 | 5764 | sage: SR(unsigned_infinity).arctan2(2) |
5767 | 5765 | Traceback (most recent call last): |
5768 | 5766 | ... |
5769 | | RuntimeError: arctan2_eval(): arctan2(unsigned_infinity, x) encountered |
| 5767 | RuntimeError: arctan2_eval(): arctan2(x, unsigned_infinity) encountered |
5770 | 5768 | sage: SR(2).arctan2(oo) |
5771 | 5769 | 1/2*pi |
5772 | 5770 | sage: SR(2).arctan2(-oo) |
… |
… |
|
5774 | 5772 | sage: SR(2).arctan2(SR(unsigned_infinity)) |
5775 | 5773 | Traceback (most recent call last): |
5776 | 5774 | ... |
5777 | | RuntimeError: arctan2_eval(): arctan2(x, unsigned_infinity) encountered |
| 5775 | RuntimeError: arctan2_eval(): arctan2(unsigned_infinity, x) encountered |
5778 | 5776 | """ |
5779 | 5777 | cdef Expression nexp = self.coerce_in(x) |
5780 | 5778 | return new_Expression_from_GEx(self._parent, |
… |
… |
|
6296 | 6294 | sage: x.factorial() |
6297 | 6295 | factorial(x) |
6298 | 6296 | sage: (x^2+y^3).factorial() |
6299 | | factorial(x^2 + y^3) |
| 6297 | factorial(y^3 + x^2) |
6300 | 6298 | |
6301 | 6299 | To prevent automatic evaluation use the ``hold`` argument:: |
6302 | 6300 | |
… |
… |
|
6967 | 6965 | EXAMPLES:: |
6968 | 6966 | |
6969 | 6967 | sage: f = sin(x)^2 + cos(x)^2; f |
6970 | | sin(x)^2 + cos(x)^2 |
| 6968 | cos(x)^2 + sin(x)^2 |
6971 | 6969 | sage: f.simplify() |
6972 | | sin(x)^2 + cos(x)^2 |
| 6970 | cos(x)^2 + sin(x)^2 |
6973 | 6971 | sage: f.simplify_trig() |
6974 | 6972 | 1 |
6975 | 6973 | sage: h = sin(x)*csc(x) |
… |
… |
|
6983 | 6981 | |
6984 | 6982 | sage: f=tan(3*x) |
6985 | 6983 | sage: f.simplify_trig() |
6986 | | (4*cos(x)^2 - 1)*sin(x)/(4*cos(x)^3 - 3*cos(x)) |
| 6984 | -(4*cos(x)^2 - 1)*sin(x)/(-4*cos(x)^3 + 3*cos(x)) |
6987 | 6985 | sage: f.simplify_trig(False) |
6988 | 6986 | sin(3*x)/cos(3*x) |
6989 | 6987 | |
… |
… |
|
7064 | 7062 | sage: y = var('y') |
7065 | 7063 | sage: g = (x^(y/2) + 1)^2*(x^(y/2) - 1)^2/(x^y - 1) |
7066 | 7064 | sage: g.simplify_rational(algorithm='simple') |
7067 | | -(2*x^y - x^(2*y) - 1)/(x^y - 1) |
| 7065 | (x^(2*y) - 2*x^y + 1)/(x^y - 1) |
7068 | 7066 | sage: g.simplify_rational() |
7069 | 7067 | x^y - 1 |
7070 | 7068 | |
… |
… |
|
7075 | 7073 | sage: f.simplify_rational() |
7076 | 7074 | (2*x^2 + 5*x + 4)/(x^3 + 5*x^2 + 8*x + 4) |
7077 | 7075 | sage: f.simplify_rational(algorithm='noexpand') |
7078 | | ((x + 1)*x + (x + 2)^2)/((x + 1)*(x + 2)^2) |
7079 | | |
| 7076 | ((x + 2)^2 + (x + 1)*x)/((x + 1)*(x + 2)^2) |
7080 | 7077 | """ |
7081 | 7078 | self_m = self._maxima_() |
7082 | 7079 | if algorithm == 'full': |
… |
… |
|
7128 | 7125 | :: |
7129 | 7126 | |
7130 | 7127 | sage: f = binomial(n, k)*factorial(k)*factorial(n-k); f |
7131 | | factorial(-k + n)*factorial(k)*binomial(n, k) |
| 7128 | binomial(n, k)*factorial(-k + n)*factorial(k) |
7132 | 7129 | sage: f.simplify_factorial() |
7133 | 7130 | factorial(n) |
7134 | 7131 | |
… |
… |
|
7323 | 7320 | |
7324 | 7321 | sage: f = log(x)+log(y)-1/3*log((x+1)) |
7325 | 7322 | sage: f.simplify_log() |
7326 | | -1/3*log(x + 1) + log(x*y) |
| 7323 | log(x*y) - 1/3*log(x + 1) |
7327 | 7324 | |
7328 | 7325 | sage: f.simplify_log('ratios') |
7329 | 7326 | log(x*y/(x + 1)^(1/3)) |
… |
… |
|
7505 | 7502 | |
7506 | 7503 | sage: x,y,z = var('x, y, z') |
7507 | 7504 | sage: (x^3-y^3).factor() |
7508 | | (x - y)*(x^2 + x*y + y^2) |
| 7505 | (x^2 + y^2 + x*y)*(x - y) |
7509 | 7506 | sage: factor(-8*y - 4*x + z^2*(2*y + x)) |
7510 | | (z - 2)*(z + 2)*(x + 2*y) |
| 7507 | (x + 2*y)*(z - 2)*(z + 2) |
7511 | 7508 | sage: f = -1 - 2*x - x^2 + y^2 + 2*x*y^2 + x^2*y^2 |
7512 | 7509 | sage: F = factor(f/(36*(1 + 2*y + y^2)), dontfactor=[x]); F |
7513 | | 1/36*(y - 1)*(x^2 + 2*x + 1)/(y + 1) |
| 7510 | 1/36*(x^2 + 2*x + 1)*(y - 1)/(y + 1) |
7514 | 7511 | |
7515 | 7512 | If you are factoring a polynomial with rational coefficients (and |
7516 | 7513 | dontfactor is empty) the factorization is done using Singular |
… |
… |
|
7520 | 7517 | sage: var('x,y') |
7521 | 7518 | (x, y) |
7522 | 7519 | sage: (x^99 + y^99).factor() |
7523 | | (x + y)*(x^2 - x*y + y^2)*(x^6 - x^3*y^3 + y^6)*... |
| 7520 | (x^60 + y^60 + x^3*y^57 - x^9*y^51 - x^12*y^48 + x^18*y^42 |
| 7521 | + x^21*y^39 - x^27*y^33 - x^30*y^30 - x^33*y^27 + x^39*y^21 |
| 7522 | + x^42*y^18 - x^48*y^12 - x^51*y^9 + x^57*y^3)*(x^20 + y^20 |
| 7523 | + x*y^19 - x^3*y^17 - x^4*y^16 + x^6*y^14 + x^7*y^13 - x^9*y^11 |
| 7524 | - x^10*y^10 - x^11*y^9 + x^13*y^7 + x^14*y^6 - x^16*y^4 |
| 7525 | - x^17*y^3 + x^19*y)*(x^10 + y^10 - x*y^9 + x^2*y^8 - x^3*y^7 |
| 7526 | + x^4*y^6 - x^5*y^5 + x^6*y^4 - x^7*y^3 + x^8*y^2 - x^9*y)*(x^6 |
| 7527 | + y^6 - x^3*y^3)*(x^2 + y^2 - x*y)*(x + y) |
7524 | 7528 | """ |
7525 | 7529 | from sage.calculus.calculus import symbolic_expression_from_maxima_string, symbolic_expression_from_string |
7526 | 7530 | if len(dontfactor) > 0: |
… |
… |
|
7561 | 7565 | (x, y, z) |
7562 | 7566 | sage: f = x^3-y^3 |
7563 | 7567 | sage: f.factor() |
7564 | | (x - y)*(x^2 + x*y + y^2) |
| 7568 | (x^2 + y^2 + x*y)*(x - y) |
7565 | 7569 | |
7566 | 7570 | Notice that the -1 factor is separated out:: |
7567 | 7571 | |
7568 | 7572 | sage: f.factor_list() |
7569 | | [(x - y, 1), (x^2 + x*y + y^2, 1)] |
| 7573 | [(x^2 + y^2 + x*y, 1), (x - y, 1)] |
7570 | 7574 | |
7571 | 7575 | We factor a fairly straightforward expression:: |
7572 | 7576 | |
7573 | 7577 | sage: factor(-8*y - 4*x + z^2*(2*y + x)).factor_list() |
7574 | | [(z - 2, 1), (z + 2, 1), (x + 2*y, 1)] |
| 7578 | [(x + 2*y, 1), (z - 2, 1), (z + 2, 1)] |
7575 | 7579 | |
7576 | 7580 | A more complicated example:: |
7577 | 7581 | |
… |
… |
|
7579 | 7583 | (x, u, v) |
7580 | 7584 | sage: f = expand((2*u*v^2-v^2-4*u^3)^2 * (-u)^3 * (x-sin(x))^3) |
7581 | 7585 | sage: f.factor() |
7582 | | -(x - sin(x))^3*(4*u^3 - 2*u*v^2 + v^2)^2*u^3 |
| 7586 | (-4*u^3 + 2*u*v^2 - v^2)^2*u^3*(-x + sin(x))^3 |
7583 | 7587 | sage: g = f.factor_list(); g |
7584 | | [(x - sin(x), 3), (4*u^3 - 2*u*v^2 + v^2, 2), (u, 3), (-1, 1)] |
| 7588 | [(4*u^3 - 2*u*v^2 + v^2, 2), (u, 3), (x - sin(x), 3), (-1, 1)] |
7585 | 7589 | |
7586 | 7590 | This function also works for quotients:: |
7587 | 7591 | |
… |
… |
|
7589 | 7593 | sage: g = f/(36*(1 + 2*y + y^2)); g |
7590 | 7594 | 1/36*(x^2*y^2 + 2*x*y^2 - x^2 + y^2 - 2*x - 1)/(y^2 + 2*y + 1) |
7591 | 7595 | sage: g.factor(dontfactor=[x]) |
7592 | | 1/36*(y - 1)*(x^2 + 2*x + 1)/(y + 1) |
| 7596 | 1/36*(x^2 + 2*x + 1)*(y - 1)/(y + 1) |
7593 | 7597 | sage: g.factor_list(dontfactor=[x]) |
7594 | | [(y - 1, 1), (y + 1, -1), (x^2 + 2*x + 1, 1), (1/36, 1)] |
| 7598 | [(x^2 + 2*x + 1, 1), (y - 1, 1), (y + 1, -1), (1/36, 1)] |
7595 | 7599 | |
7596 | 7600 | This example also illustrates that the exponents do not have to be |
7597 | 7601 | integers:: |
… |
… |
|
7614 | 7618 | EXAMPLES:: |
7615 | 7619 | |
7616 | 7620 | sage: g = factor(x^3 - 1); g |
7617 | | (x - 1)*(x^2 + x + 1) |
| 7621 | (x^2 + x + 1)*(x - 1) |
7618 | 7622 | sage: v = g._factor_list(); v |
7619 | | [(x - 1, 1), (x^2 + x + 1, 1)] |
| 7623 | [(x^2 + x + 1, 1), (x - 1, 1)] |
7620 | 7624 | sage: type(v) |
7621 | 7625 | <type 'list'> |
7622 | 7626 | """ |
… |
… |
|
7759 | 7763 | A complicated example:: |
7760 | 7764 | |
7761 | 7765 | sage: f = expand((x^2 - 1)^3*(x^2 + 1)*(x-a)); f |
7762 | | -a*x^8 + x^9 + 2*a*x^6 - 2*x^7 - 2*a*x^2 + 2*x^3 + a - x |
| 7766 | x^9 - a*x^8 - 2*x^7 + 2*a*x^6 + 2*x^3 - 2*a*x^2 + a - x |
7763 | 7767 | |
7764 | 7768 | The default variable is `a`, since it is the first in |
7765 | 7769 | alphabetical order:: |
… |
… |
|
7798 | 7802 | sage: var('a,b,c,x') |
7799 | 7803 | (a, b, c, x) |
7800 | 7804 | sage: (a*x^2 + b*x + c).roots(x) |
7801 | | [(-1/2*(b + sqrt(-4*a*c + b^2))/a, 1), (-1/2*(b - sqrt(-4*a*c + b^2))/a, 1)] |
| 7805 | [(-1/2*(b + sqrt(b^2 - 4*a*c))/a, 1), (-1/2*(b - sqrt(b^2 - 4*a*c))/a, 1)] |
7802 | 7806 | |
7803 | 7807 | By default, all the roots are required to be explicit rather than |
7804 | 7808 | implicit. To get implicit roots, pass ``explicit_solutions=False`` |
… |
… |
|
7812 | 7816 | ... |
7813 | 7817 | RuntimeError: no explicit roots found |
7814 | 7818 | sage: f.roots(explicit_solutions=False) |
7815 | | [((2^(8/9) - 2^(1/9) + x^(8/9) - x^(1/9))/(2^(8/9) - 2^(1/9)), 1)] |
| 7819 | [((2^(8/9) + x^(8/9) - 2^(1/9) - x^(1/9))/(2^(8/9) - 2^(1/9)), 1)] |
7816 | 7820 | |
7817 | 7821 | Another example, but involving a degree 5 poly whose roots don't |
7818 | 7822 | get computed explicitly:: |
… |
… |
|
7858 | 7862 | (f6, f5, f4, x) |
7859 | 7863 | sage: e=15*f6*x^2 + 5*f5*x + f4 |
7860 | 7864 | sage: res = e.roots(x); res |
7861 | | [(-1/30*(sqrt(-12*f4*f6 + 5*f5^2)*sqrt(5) + 5*f5)/f6, 1), (1/30*(sqrt(-12*f4*f6 + 5*f5^2)*sqrt(5) - 5*f5)/f6, 1)] |
| 7865 | [(-1/30*(sqrt(5)*sqrt(5*f5^2 - 12*f4*f6) + 5*f5)/f6, 1), |
| 7866 | (1/30*(sqrt(5)*sqrt(5*f5^2 - 12*f4*f6) - 5*f5)/f6, 1)] |
7862 | 7867 | sage: e.subs(x=res[0][0]).is_zero() |
7863 | 7868 | True |
7864 | 7869 | """ |
… |
… |
|
8668 | 8673 | :: |
8669 | 8674 | |
8670 | 8675 | sage: (k * binomial(n, k)).sum(k, 1, n) |
8671 | | n*2^(n - 1) |
| 8676 | 2^(n - 1)*n |
8672 | 8677 | |
8673 | 8678 | :: |
8674 | 8679 | |
… |
… |
|
8886 | 8891 | sage: f*(-2/3) |
8887 | 8892 | -2/3*x - 2 < -2/3*y + 4/3 |
8888 | 8893 | sage: f*(-pi) |
8889 | | -(x + 3)*pi < -(y - 2)*pi |
| 8894 | -pi*(x + 3) < -pi*(y - 2) |
8890 | 8895 | |
8891 | 8896 | Since the direction of the inequality never changes when doing |
8892 | 8897 | arithmetic with equations, you can multiply or divide the |
diff --git a/sage/symbolic/random_tests.py b/sage/symbolic/random_tests.py
a
|
b
|
|
260 | 260 | sage: from sage.symbolic.random_tests import * |
261 | 261 | sage: set_random_seed(2) |
262 | 262 | sage: random_expr(50, nvars=3, coeff_generator=CDF.random_element) |
263 | | (euler_gamma - v3^(-e) + (v2 - factorial(-e/v2))^(((2.85879036573 - 1.18163393202*I)*v2 + (2.85879036573 - 1.18163393202*I)*v3)*pi - 0.247786879678 + 0.931826724898*I)*arccsc((0.891138386848 - 0.0936820840629*I)/v1) - (0.553423153995 - 0.5481180572*I)*v3 + 0.149683576515 - 0.155746451854*I)*v1 + arccsch(pi + e)*elliptic_f(khinchin*v2, 1.4656989704 + 0.863754357069*I) |
| 263 | (euler_gamma + (v2 - factorial(-e/v2))^(pi*((2.85879036573 - 1.18163393202*I)*v2 + (2.85879036573 - 1.18163393202*I)*v3) - 0.247786879678 + 0.931826724898*I)*arccsc((0.891138386848 - 0.0936820840629*I)/v1) - (0.553423153995 - 0.5481180572*I)*v3 - v3^(-e) + 0.149683576515 - 0.155746451854*I)*v1 + arccsch(pi + e)*elliptic_f(khinchin*v2, 1.4656989704 + 0.863754357069*I) |
264 | 264 | sage: random_expr(5, verbose=True) |
265 | 265 | About to apply dirac_delta to [1] |
266 | 266 | About to apply arccsch to [0] |
… |
… |
|
333 | 333 | sage: for i,j in CartesianProduct(range(0,3),range(0,3)): |
334 | 334 | ... cmp[i,j] = x[i].__cmp__(x[j]) |
335 | 335 | sage: cmp |
336 | | [ 0 1 1] |
337 | | [-1 0 -1] |
338 | | [-1 1 0] |
| 336 | [ 0 -1 -1] |
| 337 | [ 1 0 -1] |
| 338 | [ 1 1 0] |
339 | 339 | """ |
340 | 340 | from sage.matrix.constructor import matrix |
341 | 341 | from sage.combinat.cartesian_product import CartesianProduct |