1 /*************** -*- Mode: MACSYMA; Package: MAXIMA -*- ******************/
2 /***************************************************************************
4 *** Copyright (c) 1984 by William Schelter,University of Texas *****
5 *** All rights reserved *****
6 ***************************************************************************/
12 for a from -3 step 7 thru 26 do ldisplay(a);
16 for i while i <= 10 do s:s+i;
25 (term:diff(term,x)/p,series:series+subst(x = 0,term)*x^p);
28 x^7/90-x^6/240-x^5/15-x^4/8+x^2/2+x+1$
31 for i thru 5 do (for j from i step -1 thru 1 do poly:poly+i*x^j);
34 5*x^5+9*x^4+12*x^3+14*x^2+15*x$
38 (guess:subst(guess,x,0.5*(x+10/x)),
39 if abs(guess^2-10) < 5.0e-5 then return(guess));
42 for count from 2 next 3*count thru 20 do ldisplay(count);
46 thru 10 while x # 0 do x:0.5*(x+5/x);
52 newton(f,guess):=block([numer,y],local(f,df,x,guess),numer:true,
53 define(df(x),diff(f(x),x)),
54 do (y:df(guess),if y = 0 then error("derivative at",guess,"is zero"),
55 guess:guess-f(guess)/y,
56 if abs(f(guess)) < 5.0e-6 then return(guess)));
57 newton(f,guess):=block([numer,y],local(f,df,x,guess),numer:true,
58 define(df(x),diff(f(x),x)),
59 do (y:df(guess),if y = 0 then error("derivative at",guess,"is zero"),
60 guess:guess-f(guess)/y,
61 if abs(f(guess)) < 5.0e-6 then return(guess)))$
66 for f in [log,rho,atan] do ldisp(f(1.0));
68 ev(concat(e,linenum-1),numer);
70 kill(functions,values,arrays);
75 x*'diff(f(x),x,1)+f(x)$
104 'integrate(f(x),x,a,b);
105 'integrate(x^2,x,a,b)$
106 for i thru 5 do s:s+i^2;
114 exp:'sum(g(i),i,0,n);
130 declare(integrate,noun);
136 f(y):=diff(y*log(y),y,2);
137 f(y):=diff(y*log(y),y,2)$
152 ev(%e^x*sin(x)^2,exponentialize);
153 -%e^x*(%e^(%i*x)-%e^-(%i*x))^2/4;
155 -((%e^((2*%i+1)*x)/(2*%i+1)+%e^((1-2*%i)*x)/(1-2*%i)-2*%e^x)/4);
157 -((%e^x*(%i*sin(2*x)+cos(2*x))/(2*%i+1)
158 +%e^x*(cos(2*x)-%i*sin(2*x))/(1-2*%i)-2*%e^x)
161 -%e^x*sin(2*x)/5-%e^x*cos(2*x)/10+%e^x/2;
162 ev(ans,x:1,numer)-ev(ans,x:0,numer);
166 ev(ans,x:1,bfloat)-ev(ans,x:0,bfloat);
167 5.7791601820424019599988308251707781b-1;
168 integrate(%e^x*sin(x)^2,x);
169 -(((2*%e^x*sin(2*x)+%e^x*cos(2*x)-5*%e^x)/10));
171 -((2*%e^x*sin(2*x)+%e^x*cos(2*x)-5*%e^x)/10);
181 sin(%pi/12)+tan(%pi/6);
182 sin(%pi/12)+1/sqrt(3)$
185 /* tops 20 : 0.83616931$ */
194 /* tops 20: 3.67909265$ */
195 diff(atanh(sqrt(x)),x);
200 4.794255386042030002732879b-1$
204 exp:cos(x)^2-sin(x)^2;
211 (sin(2*x)/2+x)/2-(x-sin(2*x)/2)/2$
220 %-exp,trigreduce,ratsimp;
223 sech(x)^2*sinh(x)*tanh(x)/coth(x)^2+cosh(x)^2*sech(x)^2*tanh(x)/coth(x)^2
224 +sech(x)^2*tanh(x)/coth(x)^2;
225 sech(x)^2*sinh(x)*tanh(x)/coth(x)^2+cosh(x)^2*sech(x)^2*tanh(x)/coth(x)^2
226 +sech(x)^2*tanh(x)/coth(x)^2$
228 (sinh(x)^5+sinh(x)^4+2*sinh(x)^3)/cosh(x)^5$
229 /* These are from the trgsmp.dem file.
230 * I (rtoy) hand-verified these results (using maxima, of course)
232 (1-sin(x)^2)*cos(x)/cos(x)^2+tan(x)*sec(x)^2;
233 (1-sin(x)^2)*cos(x)/cos(x)^2+tan(x)*sec(x)^2$
235 (sin(x)+cos(x)^4)/cos(x)^3$
237 tan(x)^2+sec(x)^2/(1-tan(x)*sec(x));
238 tan(x)^2+sec(x)^2/(1-tan(x)*sec(x))$
240 (sin(x)^4+sin(x)^3-1)/(cos(x)^2*sin(x)-cos(x)^4)$
242 (sin(x)^4-6*cos(x)^2*sin(x)^2+4*(cos(x)^2-sin(x)^2)+8*sin(x)+cos(x)^4+3)/(8*cos(x)^3);
243 (sin(x)^4-6*cos(x)^2*sin(x)^2+4*(cos(x)^2-sin(x)^2)+8*sin(x)+cos(x)^4+3)/(8*cos(x)^3)$
245 (sin(x)+cos(x)^4)/cos(x)^3$
248 sech(x)^2*sinh(x)*tanh(x)/coth(x)^2+cosh(x)^2*sech(x)^2*tanh(x)/coth(x)^2+sech(x)^2*tanh(x)/coth(x)^2;
249 sech(x)^2*sinh(x)*tanh(x)/coth(x)^2+cosh(x)^2*sech(x)^2*tanh(x)/coth(x)^2+sech(x)^2*tanh(x)/coth(x)^2$
251 (sinh(x)^5+sinh(x)^4+2*sinh(x)^3)/cosh(x)^5$
253 -sech(x)^5*(sinh(x)^5+2*(sinh(x)^4+6*cosh(x)^2*sinh(x)^2+cosh(x)^4)-13*(sinh(x)^3+3*cosh(x)^2*sinh(x))+10*cosh(x)^2*sinh(x)^3-8*(sinh(x)^2+cosh(x)^2)+5*cosh(x)^4*sinh(x)+34*sinh(x)+6)/16;
254 -sech(x)^5*(sinh(x)^5+2*(sinh(x)^4+6*cosh(x)^2*sinh(x)^2+cosh(x)^4)-13*(sinh(x)^3+3*cosh(x)^2*sinh(x))+10*cosh(x)^2*sinh(x)^3-8*(sinh(x)^2+cosh(x)^2)+5*cosh(x)^4*sinh(x)+34*sinh(x)+6)/16$
256 -((sinh(x)^5+sinh(x)^4-2*sinh(x)^3)/cosh(x)^5)$
258 cos(x)*(sec(x)^2*tan(x)+1)-sec(x)^2*sin(x)-cos(x);
259 cos(x)*(sec(x)^2*tan(x)+1)-sec(x)^2*sin(x)-cos(x)$
263 v*cos(x)*sec(x)^2*tan(x)+(-v*sec(x)^2-2*'diff(v,x))*sin(x)+'diff(v,x)*cos(x)*sec(x)+'diff(v,x,2)*cos(x);
264 v*cos(x)*sec(x)^2*tan(x)+(-v*sec(x)^2-2*'diff(v,x))*sin(x)+'diff(v,x)*cos(x)*sec(x)+'diff(v,x,2)*cos(x)$
266 -2*'diff(v,x,1)*sin(x)+'diff(v,x,2)*cos(x)+'diff(v,x,1)$
275 x/(sqrt(1-x)*sqrt(x+1));
281 1/(sqrt(1-x)*sqrt(x+1));
287 sqrt(x-1)*sqrt(x+1)/x;
289 /* A few checks to see that triginverses false disables the above transformations */
299 /* SF bug # 1981518, Calling desolve inside a "for...do" makes it loop endlessly
300 * (protect against endless loop by throw--catch in case bug is triggered)
302 catch (block ([foo:1],
303 for i thru 3 do (ilt (1/s, s, t),
304 if foo > 3 then throw ('i = i) else foo : foo + 1)));
307 /* bug reported to mailing list 2009-05-09
308 * unexpected behavior in for loop with variable step
311 block ([L : []], for r:0 thru 7 step +2 do L : cons (r, L), L);
314 block ([L : []], for r:7 thru 0 step -2 do L : cons (r, L), L);
317 block ([L : [], r0 : 0, r1 : 7, s : +2], for r:r0 thru r1 step s do L : cons (r, L), L);
320 block ([L : [], r0 : 7, r1 : 0, s : -2], for r:r0 thru r1 step s do L : cons (r, L), L);
323 /* step is evaluated once at start of loop, so these loops are defined */
325 block ([L : [], s : +2], for i:1 thru 10 step s do L : cons (s : -s, L), L);
328 block ([L : [], s : -2], for i:10 thru 1 step s do L : cons (s : -s, L), L);
331 /* bug reported to mailing list 2009-05-13 "reset ( radexpand, domain )"
333 * display2d is a resettable option variable. We save the value of display2d
334 * and restore it after the reset. This allows to run the testsuite in both
337 (save:display2d, done);
339 (reset (), [radexpand, domain]);
341 (display2d:save, done);
344 [radexpand, domain] : [all, complex];
347 reset (radexpand, domain);
353 ([foo, bar, baz] : [1, 2, 3],
354 /* should ignore these non-defmvar's */
355 reset (foo, bar, baz));
358 /* verify that ORDFNA can handle CRE.
360 (kill (a, b), [doallmxops, doscmxops] : [false, false], 0);
364 b*matrix([''(rat(a))]);
366 (reset (doallmxops, doscmxops), 0);
369 /* SF bug #2936: stack overflow in integrate */
371 kill (x, A, B, MU, SIGMA);
374 trigsimp (gamma_incomplete (1, log (x)));
375 gamma_incomplete (1, log (x));
377 trigsimp ((%i*gamma_incomplete(1,(1-2*log(x))^2/4)*(1-2*log(x))^2)
379 %i*gamma_incomplete(1,(4*log(x)^2-4*log(x)+1)/4)$
381 trigsimp (integrate (%e^((-log(x)^2)-1)*log(x),x));
382 -(%e^-(3/4)*(2*gamma_incomplete(1,(4*log(x)^2-4*log(x)+1)/4)*abs(2*log(x)-1)
383 +2*gamma_incomplete(1/2,(4*log(x)^2-4*log(x)+1)/4)*log(x)
384 -gamma_incomplete(1/2,(4*log(x)^2-4*log(x)+1)/4)))
385 /(4*abs(2*log(x)-1))$
387 /* throw away results of integrate, just make sure it runs without crashing */
388 block ([foo, bar, ctxt],
389 foo : exp(-(log(x) - MU)*(log(x) - MU)/(2*SIGMA*SIGMA))/(x*SIGMA*sqrt(2*%pi)),
390 bar : (log(B) - log(x*SIGMA) + ((x-A)*(x-A)/(2*B*B) - (log(x) -MU)*(log(x) -MU)/(2*SIGMA*SIGMA))),
391 [foo, bar] : subst ([A=2, MU=3], [foo, bar]),
392 ctxt : newcontext (),
393 assume (SIGMA > 0, B > 0),
394 integrate (expand (foo*bar), x),
395 integrate (expand (foo*bar), x, 2, inf),
400 /* mailing list 2015-09-07: How can I catch this error? "errcatch" doesn't do the trick. */
402 /* result for this test will change if ever "quotient by zero" bug is fixed */
403 errcatch (integrate(ev(ratsimp(1/(x^(5/2)+3*x^(1/3))),algebraic),x));
406 (kill (foo, bar, baz), foo () := bar (), bar () := (errcatch (integrate(ev(ratsimp(1/(x^(5/2)+3*x^(1/3))),algebraic),x)), throw ('baz)), catch (foo ()));
409 /* This bug can be fixed by changing simplus to simplify 2^(3/2)*x^2 - sqrt(2)*x^2
410 to sqrt(2)*x^2. Until this bug is fixed, we'll errcatch this test. */
411 errcatch (integrate (exp(2^(3/2)*x^2 - sqrt(2)*x^2), x));
412 [-((sqrt(%pi)*%i*erf(2^(1/4)*%i*x))/2^(5/4))];
414 (kill (quux), bar () := (errcatch (integrate (exp(2^(3/2)*x^2 - sqrt(2)*x^2), x)), throw ('quux)), catch (foo ()));
417 /* result for this test will change if ever "quotient by zero" bug is fixed */
418 errcatch (taylor(coth(x), x, %i*%pi, 0));
421 (kill (mumble), bar () := (errcatch (taylor(coth(x), x, %i*%pi, 0)), throw ('mumble)), catch (foo ()));
424 /* Verify that some special variables that were recently given DEFMVAR's are now resettable.
425 * If ever the default values of these variables are changed,
426 * some of the following tests will fail.
427 * In that case just update these tests to use the new default value.
431 mydefaults['verbose] : false,
432 mydefaults['exptsubst] : false,
433 mydefaults['partswitch] : false,
434 mydefaults['inflag] : false,
435 mydefaults['derivsubst] : false,
436 mydefaults['opsubst] : true,
437 mydefaults['demoivre] : false,
438 mydefaults['nointegrate] : false,
439 mydefaults['tlimswitch] : true,
440 mydefaults['limsubst] : false,
441 mydefaults['packagefile] : false,
442 mydefaults['factlim] : 100000,
443 mydefaults['cflength] : 1,
444 mydefaults['taylordepth] : 3,
445 mydefaults['maxtaydiff] : 4,
446 mydefaults['lhospitallim] : 4,
447 mydefaults['linel] : 79,
452 every (lambda ([v], is(mydefaults[v] = ev(v))), flatten (rest (arrayinfo (mydefaults), 2))));
455 (myflags : '[verbose, exptsubst, partswitch, inflag, derivsubst, opsubst, demoivre,
456 nointegrate, tlimswitch, limsubst, packagefile],
457 /* "not" causes an extra evaluation of its argument,
458 * so this next line won't work as expected if "not" ever becomes
459 * simplifying operator; update this test case as needed if that
460 * ever comes to pass.
462 myflags :: map ("not", myflags),
464 [true, true, true, true, true, false, true, true, false, true, true];
466 map (reset, myflags);
467 [[verbose], [exptsubst], [partswitch], [inflag], [derivsubst], [opsubst], [demoivre],
468 [nointegrate], [tlimswitch], [limsubst], [packagefile]];
471 [false, false, false, false, false, true, false, false, true, false, false];
473 (myvals : '[factlim, cflength, taylordepth, maxtaydiff, lhospitallim, linel],
474 myvals :: makelist (99, v, myvals),
476 [99, 99, 99, 99, 99, 99];
479 [[factlim], [cflength], [taylordepth], [maxtaydiff], [lhospitallim], [linel]];
482 [100000, 1, 3, 4, 4, 79];
484 declare_index_properties
487 C, [postsubscript, postsuperscript],
488 D, [postsubscript, postsuperscript, presuperscript],
489 E, [postsubscript, postsuperscript, presuperscript, presubscript],
490 F, [postsubscript, postsuperscript, presuperscript, presubscript,
491 postsubscript, postsuperscript, presuperscript, presubscript],
492 G, [postsubscript, postsuperscript, presuperscript, presubscript,
493 postsuperscript, presubscript]);
496 map (get_index_properties, '[A, B, C, D, E, F, G]);
499 [postsubscript, postsuperscript],
500 [postsubscript, postsuperscript, presuperscript],
501 [postsubscript, postsuperscript, presuperscript, presubscript],
502 [postsubscript, postsuperscript, presuperscript, presubscript,
503 postsubscript, postsuperscript, presuperscript, presubscript],
504 [postsubscript, postsuperscript, presuperscript, presubscript,
505 postsuperscript, presubscript]];
507 kill (a, b, c, d, w, x, y, z);
510 /* This business about capturing the output to a string and comparing that
511 * is a little fragile in the sense that changing invisible stuff (trailing
512 * spaces, newline character) can make these tests fail.
513 * But the pretty printer code changes very infrequently,
514 * and also I believe that these tests should work the same on Windows as
515 * on Unix-like platforms, because TERPRI (according to CLHS) just outputs #\Newline.
516 * Changing this file from NL to CR-NL line endings will presumably
517 * make these tests fail.
520 with_default_2d_display ([S: make_string_output_stream ()],
521 with_stdout (S, ?terpri (), print (A[x])),
522 get_output_stream_string (S));
528 with_default_2d_display ([S: make_string_output_stream ()],
529 with_stdout (S, ?terpri (), print (B[x])),
530 get_output_stream_string (S));
536 with_default_2d_display ([S: make_string_output_stream ()],
537 with_stdout (S, ?terpri (), print (C[x, y])),
538 get_output_stream_string (S));
545 with_default_2d_display ([S: make_string_output_stream ()],
546 with_stdout (S, ?terpri (), print (D[x, y, z])),
547 get_output_stream_string (S));
554 with_default_2d_display ([S: make_string_output_stream ()],
555 with_stdout (S, ?terpri (), print (E[w, x, y, z])),
556 get_output_stream_string (S));
563 with_default_2d_display ([S: make_string_output_stream ()],
564 with_stdout (S, ?terpri (), print (F[a, b, c, d, w, x, y, z])),
565 get_output_stream_string (S));
572 with_default_2d_display ([S: make_string_output_stream ()],
573 with_stdout (S, ?terpri (), print (G[a, b, c, d, w, x])),
574 get_output_stream_string (S));
581 with_default_2d_display ([S: make_string_output_stream ()],
582 with_stdout (S, ?terpri (), print (G[a, B[a], C[a, b], D[a, b, c], E[a, b, c, d], F[a, b, c, d, w, x, y, z]])),
583 get_output_stream_string (S));
594 with_default_2d_display ([S: make_string_output_stream ()],
595 with_stdout (S, ?terpri (), print (sqrt (G[a, b, c, d, w, x]))),
596 get_output_stream_string (S));
603 with_default_2d_display ([S: make_string_output_stream ()],
604 with_stdout (S, ?terpri (), print ((1 - G[a, b, c, d, w, x])/E[1, 1/2, 2/3, 17/29])),
605 get_output_stream_string (S));
616 remove_index_properties (A, B, C, D, E, F, G);
619 map (get_index_properties, '[A, B, C, D, E, F, G]);
620 [[], [], [], [], [], [], []];
622 /* email from Oleg Nesterov 2020-05-21: "maxima: bug in dsumprod() ?" */
624 (print_string_2d (e) := with_default_2d_display (printf (false, "~m", e)), 0);
627 print_string_2d (lsum(1/f(g(x)/h(x)), x, LOOOOOOOOONG_EXPR));
633 x in LOOOOOOOOONG_EXPR h(x)
636 /* other examples which call DSUMPROD in test suite */
638 print_string_2d ('sum(x^k / k,k,1,inf));
648 print_string_2d (subst (k = \*index, 'sum(x^k / k,k,1,inf)));
658 print_string_2d ('sum(i!,i,1,4));
668 print_string_2d ('sum(g(i),i,0,n));
678 print_string_2d ('sum(g(i),i,0,n) + 1);
688 print_string_2d (foo: unsum(product(i^2,i,1,n),n));
692 ( ! ! i ) (n - 1) (n + 1)
697 print_string_2d (nusum(foo,n,1,n));
706 print_string_2d (powerseries(log(sin(x)/x),x,0));
708 ==== i2 2 i2 - 1 2 i2
709 \\ (- 1) 2 bern(2 i2) x
710 > ----------------------------------
716 print_string_2d (product((x^i+1)^2.5,i,1,inf)/(x^2+1));
728 /* additional DSUMPROD examples */
730 print_string_2d ('lsum(1/(1+f(x)/2), kskdsksdkkdksdksd, w999393293923939losl));
736 kskdsksdkkdksdksd in w999393293923939losl 2
739 print_string_2d ('lsum(1/(1+f(x)/2), kskdsksdkkdksdksd, w999393293923939losl^2));
746 kskdsksdkkdksdksd in w999393293923939losl
749 print_string_2d ('lsum(1/(1+f(x)/2), kskdsksdkkdksdksd, w999393293923939losl^skdkskdsk));
756 kskdsksdkkdksdksd in w999393293923939losl
759 /* ensure that nounified operators are displayed same as verbs
760 * follow-on work for bug reported to mailing list 2020-09-13: "Function name for matrix/vector dot product?"
763 kill (foo, x, y, z, a, b, c);
766 print_string_2d (' "'"(foo));
770 print_string_2d (' ":"(foo, 123));
774 print_string_2d (' "::"(foo, 123));
778 print_string_2d (' ":="(foo(a, b), [x, y, z]));
779 "foo(a, b) := [x, y, z]
782 print_string_2d (' "::="(foo(a, b), [x, y, z]));
783 "foo(a, b) ::= [x, y, z]
786 print_string_2d (' "!"(4));
790 print_string_2d (' "^"(2, x));
795 print_string_2d (' "^^"(a, b));
800 print_string_2d (' "."(a, b, c));
804 print_string_2d (' ?rat(a, b));
810 print_string_2d (' "/"(a, b));
816 print_string_2d (' "*"(a, b, c));
820 print_string_2d (' "+"(a, b, c));
824 print_string_2d (' "-"(a));
828 print_string_2d ('?marrow(a, b));
832 print_string_2d (' ">"(a, b));
836 print_string_2d (' ">="(a, b));
840 print_string_2d (' "="(a, b));
844 print_string_2d (' "#"(a, b));
848 print_string_2d (' "<="(a, b));
852 print_string_2d (' "<"(a, b));
856 print_string_2d (' "not"(a));
860 print_string_2d (' "and"(a, b));
864 print_string_2d (' "or"(a, b));
868 print_string_2d ('?mprogn(a, b, c));
872 print_string_2d ('?mlist(a, b, c));
876 /* example from mailing list 2019-04-02: "maxima lists" */
877 print_string_2d ('[x][1]);
882 print_string_2d ('?mangle(a, b, c));
886 print_string_2d ('?mcomma(a, b, c));
890 print_string_2d ('?mabs(a));
894 print_string_2d ('matrix([a, b, c]));
898 print_string_2d ('?mbox(a));
904 print_string_2d ('?mlabox (a, b));
910 print_string_2d ('?mtext ("hello"));
914 block ([linel: 65], print_string_2d ('?mlabel (a, b)));
918 /* SF bug #3301: "fpprintprec do not round bfloat correctly(another case)"
919 * Test exponent of printed bigfloat is correct when number is rounded.
922 (fpprec:64, fpprintprec:16, done);
925 print_string_2d(0.99999999999999999999b0);
929 print_string_2d(1.99999999999999999999b0);
936 print_string_2d(0.99999999999999999999b0);
940 print_string_2d(1.99999999999999999999b0);
947 block([fpprintprec:4],string(0.9999499999b0));
949 block([fpprintprec:4],string(0.99995b0));
951 block([fpprintprec:4],string(0.99999b0));
953 block([fpprintprec:4],string(1.0005b0));
955 block([fpprintprec:4],string(1.000499999b0));
957 block([fpprintprec:4],string(1.00050001b0));
960 block([fpprintprec:10],string(0.9999999999499b0));
962 block([fpprintprec:10],string(0.99999999999b0));
964 block([fpprintprec:10],string(0.999999999995b0));
966 block([fpprintprec:10],string(1.0000000005b0));
968 block([fpprintprec:10],string(1.00000000051b0));
971 block( [ bad: [], str],
972 for fpprintprec:4 thru 15 do
973 for a in [1,5,50,-1,-5] do
974 if parse_string(str: string(1+bfloat(a*10^(-fpprintprec-1)))) # 1.0b0
975 then push([fpprintprec,a,str],bad),
979 (reset(fpprec, fpprintprec), done);