10 @c -----------------------------------------------------------------------------
11 @node Numbers, Strings, Data Types and Structures, Data Types and Structures
13 @c -----------------------------------------------------------------------------
16 * Introduction to Numbers::
17 * Functions and Variables for Numbers::
20 @c -----------------------------------------------------------------------------
21 @node Introduction to Numbers, Functions and Variables for Numbers, Numbers, Numbers
22 @subsection Introduction to Numbers
23 @c -----------------------------------------------------------------------------
25 @c -----------------------------------------------------------------------------
26 @subheading Complex numbers
27 @c -----------------------------------------------------------------------------
29 A complex expression is specified in Maxima by adding the real part of the
30 expression to @code{%i} times the imaginary part. Thus the roots of the
31 equation @code{x^2 - 4*x + 13 = 0} are @code{2 + 3*%i} and @code{2 - 3*%i}.
32 Note that simplification of products of complex expressions can be effected by
33 expanding the product. Simplification of quotients, roots, and other functions
34 of complex expressions can usually be accomplished by using the @code{realpart},
35 @code{imagpart}, @code{rectform}, @code{polarform}, @code{abs}, @code{carg}
39 @category{Complex variables}
42 @c -----------------------------------------------------------------------------
43 @node Functions and Variables for Numbers, , Introduction to Numbers, Numbers
44 @subsection Functions and Variables for Numbers
45 @c -----------------------------------------------------------------------------
47 @c -----------------------------------------------------------------------------
49 @deffn {Function} bfloat (@var{expr})
51 @code{bfloat} replaces integers, rationals, floating point numbers, and some symbolic constants
52 in @var{expr} with bigfloat (variable-precision floating point) numbers.
54 The constants @code{%e}, @code{%gamma}, @code{%phi}, and @code{%pi}
55 are replaced by a numerical approximation.
56 However, @code{%e} in @code{%e^x} is not replaced by a numeric value
57 unless @code{bfloat(x)} is a number.
59 @code{bfloat} also causes numerical evaluation of some built-in functions,
60 namely trigonometric functions, exponential functions, @code{abs}, and @code{log}.
61 @c ALSO ENTIER BUT LET'S NOT GO INTO IT.
63 The number of significant digits in the resulting bigfloats is specified by the
64 global variable @mrefdot{fpprec}
65 Bigfloats already present in @var{expr} are replaced with values which have
66 precision specified by the current value of @mref{fpprec}.
68 When @mref{float2bf} is @code{false}, a warning message is printed when
69 a floating point number is replaced by a bigfloat number with less precision.
73 @code{bfloat} replaces integers, rationals, floating point numbers, and some symbolic constants
74 in @var{expr} with bigfloat numbers.
77 @c bfloat([123, 17/29, 1.75]);
78 @c bfloat([%e, %gamma, %phi, %pi]);
79 @c bfloat((f(123) + g(h(17/29)))/(x + %gamma));
82 (%i1) bfloat([123, 17/29, 1.75]);
83 (%o1) [1.23b2, 5.862068965517241b-1, 1.75b0]
84 (%i2) bfloat([%e, %gamma, %phi, %pi]);
85 (%o2) [2.718281828459045b0, 5.772156649015329b-1,
86 1.618033988749895b0, 3.141592653589793b0]
87 (%i3) bfloat((f(123) + g(h(17/29)))/(x + %gamma));
88 1.0b0 (g(h(5.862068965517241b-1)) + f(1.23b2))
89 (%o3) ----------------------------------------------
90 x + 5.772156649015329b-1
93 @code{bfloat} also causes numerical evaluation of some built-in functions.
96 @c bfloat(sin(17/29));
98 @c bfloat(abs(-%gamma));
102 (%i1) bfloat(sin(17/29));
103 (%o1) 5.532051841609784b-1
104 (%i2) bfloat(exp(%pi));
105 (%o2) 2.314069263277927b1
106 (%i3) bfloat(abs(-%gamma));
107 (%o3) 5.772156649015329b-1
108 (%i4) bfloat(log(%phi));
109 (%o4) 4.812118250596035b-1
113 @category{Numerical evaluation}
117 @c -----------------------------------------------------------------------------
119 @deffn {Function} bfloatp (@var{expr})
121 Returns @code{true} if @var{expr} is a bigfloat number, otherwise @code{false}.
124 @category{Numerical evaluation}
125 @category{Predicate functions}
129 @c --- 03.11.2011 --------------------------------------------------------------
131 @defvr {Option variable} bftorat
132 Default value: @code{false}
134 @code{bftorat} controls the conversion of bfloats to rational numbers. When
135 @code{bftorat} is @code{false}, @mref{ratepsilon} will be used to control the
136 conversion (this results in relatively small rational numbers). When
137 @code{bftorat} is @code{true}, the rational number generated will accurately
138 represent the bfloat.
140 Note: @code{bftorat} has no effect on the transformation to rational numbers
141 with the function @mrefdot{rationalize}
147 @c rat(bfloat(11111/111111)), bftorat:false;
148 @c rat(bfloat(11111/111111)), bftorat:true;
151 (%i1) ratepsilon:1e-4;
153 (%i2) rat(bfloat(11111/111111)), bftorat:false;
154 `rat' replaced 9.99990999991B-2 by 1/10 = 1.0B-1
158 (%i3) rat(bfloat(11111/111111)), bftorat:true;
159 `rat' replaced 9.99990999991B-2 by 11111/111111 = 9.99990999991B-2
166 @category{Numerical evaluation}
170 @c -----------------------------------------------------------------------------
172 @defvr {Option variable} bftrunc
173 Default value: @code{true}
175 @code{bftrunc} causes trailing zeroes in non-zero bigfloat numbers not to be
176 displayed. Thus, if @code{bftrunc} is @code{false}, @code{bfloat (1)}
177 displays as @code{1.000000000000000B0}. Otherwise, this is displayed as
181 @category{Numerical evaluation}
185 @c -----------------------------------------------------------------------------
187 @deffn {Function} evenp (@var{expr})
189 @c THIS IS STRANGE -- SHOULD RETURN NOUN FORM IF INDETERMINATE
190 Returns @code{true} if @var{expr} is a literal even integer, otherwise
193 @code{evenp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
194 is declared @code{even}.
197 @category{Predicate functions}
201 @c -----------------------------------------------------------------------------
203 @deffn {Function} float (@var{expr})
205 Converts integers, rational numbers and bigfloats in @var{expr} to floating
206 point numbers. It is also an @mrefcomma{evflag} @code{float} causes
207 non-integral rational numbers and bigfloat numbers to be converted to floating
211 @category{Numerical evaluation}
212 @category{Evaluation flags}
216 @c --- 08.10.2010 DK -----------------------------------------------------------
218 @defvr {Option variable} float2bf
219 Default value: @code{true}
221 When @mref{float2bf} is @code{false}, a warning message is printed when
222 a floating point number is replaced by a bigfloat number with less precision.
223 @c DOES THAT APPLY ONLY TO BFLOAT, OR DO OTHER FUNCTIONS CALL IT ??
226 @category{Numerical evaluation}
230 @c -----------------------------------------------------------------------------
232 @deffn {Function} floatnump (@var{expr})
234 Returns @code{true} if @var{expr} is a floating point number, otherwise
238 @category{Numerical evaluation}
239 @category{Predicate functions}
243 @c -----------------------------------------------------------------------------
245 @defvr {Option variable} fpprec
248 @code{fpprec} is the number of significant digits for arithmetic on bigfloat
249 numbers. @code{fpprec} does not affect computations on ordinary floating point
252 See also @mref{bfloat} and @mrefdot{fpprintprec}
255 @category{Numerical evaluation}
259 @c -----------------------------------------------------------------------------
261 @defvr {Option variable} fpprintprec
264 @code{fpprintprec} is the number of digits to print when printing an ordinary
265 float or bigfloat number.
267 For ordinary floating point numbers,
268 when @code{fpprintprec} has a value between 2 and 16 (inclusive),
269 the number of digits printed is equal to @code{fpprintprec}.
270 Otherwise, @code{fpprintprec} is 0, or greater than 16,
271 and the number of digits printed is 16.
273 For bigfloat numbers,
274 when @code{fpprintprec} has a value between 2 and @code{fpprec} (inclusive),
275 the number of digits printed is equal to @code{fpprintprec}.
276 Otherwise, @code{fpprintprec} is 0, or greater than @code{fpprec},
277 and the number of digits printed is equal to @code{fpprec}.
279 For both ordinary floats and bigfloats,
280 trailing zero digits are suppressed.
281 The actual number of digits printed is less than @code{fpprintprec}
282 if there are trailing zero digits.
284 @code{fpprintprec} cannot be 1.
287 @category{Numerical evaluation}
288 @category{Display flags and variables}
292 @c -----------------------------------------------------------------------------
294 @deffn {Function} integerp (@var{expr})
296 Returns @code{true} if @var{expr} is a literal numeric integer, otherwise
299 @code{integerp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
300 is declared @code{integer}.
312 @c declare (n, integer);
320 (%i3) integerp (-17);
322 (%i4) integerp (0.0);
324 (%i5) integerp (1.0);
326 (%i6) integerp (%pi);
330 (%i8) declare (n, integer);
337 @category{Predicate functions}
341 @c -----------------------------------------------------------------------------
343 @defvr {Option variable} m1pbranch
344 Default value: @code{false}
346 @code{m1pbranch} is the principal branch for @code{-1} to a power.
347 Quantities such as @code{(-1)^(1/3)} (that is, an "odd" rational exponent) and
348 @code{(-1)^(1/4)} (that is, an "even" rational exponent) are handled as follows:
350 @c REDRAW THIS AS A TABLE
355 (-1)^(1/4): (-1)^(1/4)
358 m1pbranch:false m1pbranch:true
359 (-1)^(1/3) 1/2+%i*sqrt(3)/2
360 (-1)^(1/4) sqrt(2)/2+%i*sqrt(2)/2
364 @category{Expressions}
365 @category{Global flags}
369 @c -----------------------------------------------------------------------------
370 @anchor{nonnegintegerp}
371 @deffn {Function} nonnegintegerp (@var{n})
373 Return @code{true} if and only if @code{@var{n} >= 0} and @var{n} is an integer.
376 @category{Package linearalgebra}
377 @category{Predicate functions}
381 @c -----------------------------------------------------------------------------
383 @deffn {Function} numberp (@var{expr})
385 Returns @code{true} if @var{expr} is a literal integer, rational number,
386 floating point number, or bigfloat, otherwise @code{false}.
388 @code{numberp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
389 is a symbolic number such as @code{%pi} or @code{%i}, or declared to be
390 @code{even}, @code{odd}, @code{integer}, @code{rational}, @code{irrational},
391 @code{real}, @code{imaginary}, or @code{complex}.
398 @c numberp (3.14159);
399 @c numberp (-1729b-4);
400 @c map (numberp, [%e, %pi, %i, %phi, inf, minf]);
401 @c declare (a, even, b, odd, c, integer, d, rational, e, irrational, f, real, g, imaginary, h, complex);
402 @c map (numberp, [a, b, c, d, e, f, g, h]);
407 (%i2) numberp (-13/19);
409 (%i3) numberp (3.14159);
411 (%i4) numberp (-1729b-4);
413 (%i5) map (numberp, [%e, %pi, %i, %phi, inf, minf]);
414 (%o5) [false, false, false, false, false, false]
415 (%i6) declare (a, even, b, odd, c, integer, d, rational,
416 e, irrational, f, real, g, imaginary, h, complex);
418 (%i7) map (numberp, [a, b, c, d, e, f, g, h]);
419 (%o7) [false, false, false, false, false, false, false, false]
423 @category{Predicate functions}
427 @c NEEDS CLARIFICATION, EXAMPLES
428 @c WHAT ARE THE FUNCTIONS WHICH ARE EVALUATED IN FLOATING POINT ??
429 @c WHAT IS A "NUMERVAL" ?? (SOMETHING DIFFERENT FROM A NUMERIC VALUE ??)
430 @c NEED TO MENTION THIS IS AN evflag
432 @c -----------------------------------------------------------------------------
434 @defvr {Option variable} numer
436 @code{numer} causes some mathematical functions (including exponentiation)
437 with numerical arguments to be evaluated in floating point. It causes
438 variables in @code{expr} which have been given numerals to be replaced by
439 their values. It also sets the @mref{float} switch on.
441 See also @mrefdot{%enumer}
446 @c [sqrt(2), sin(1), 1/(1+sqrt(3))];
447 @c [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
451 (%i1) [sqrt(2), sin(1), 1/(1+sqrt(3))];
453 (%o1) [sqrt(2), sin(1), -----------]
457 (%i2) [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
458 (%o2) [1.414213562373095, 0.8414709848078965, 0.3660254037844387]
463 @category{Numerical evaluation}
464 @category{Evaluation flags}
468 @c -----------------------------------------------------------------------------
469 @anchor{numer_pbranch}
470 @defvr {Option variable} numer_pbranch
471 Default value: @code{false}
473 The option variable @code{numer_pbranch} controls the numerical evaluation of
474 the power of a negative integer, rational, or floating point number. When
475 @code{numer_pbranch} is @code{true} and the exponent is a floating point number
476 or the option variable @mref{numer} is @code{true} too, Maxima evaluates
477 the numerical result using the principal branch. Otherwise a simplified, but
478 not an evaluated result is returned.
484 @c (-2)^0.75,numer_pbranch:true;
487 @c (-2)^(3/4),numer,numer_pbranch:true;
496 (%i2) (-2)^0.75,numer_pbranch:true;
497 (%o2) 1.189207115002721 %i - 1.189207115002721
505 (%i4) (-2)^(3/4),numer;
507 (%o4) 1.681792830507429 (- 1)
510 (%i5) (-2)^(3/4),numer,numer_pbranch:true;
511 (%o5) 1.189207115002721 %i - 1.189207115002721
516 @category{Numerical evaluation}
520 @c NEEDS CLARIFICATION, EXAMPLES
521 @c HOW TO FIND ALL VARIABLES WHICH HAVE NUMERVALS ??
523 @c -----------------------------------------------------------------------------
525 @deffn {Function} numerval (@var{x_1}, @var{expr_1}, @dots{}, @var{var_n}, @var{expr_n})
527 Declares the variables @code{x_1}, @dots{}, @var{x_n} to have
528 numeric values equal to @code{expr_1}, @dots{}, @code{expr_n}.
529 The numeric value is evaluated and substituted for the variable
530 in any expressions in which the variable occurs if the @code{numer} flag is
531 @code{true}. See also @mrefdot{ev}
533 The expressions @code{expr_1}, @dots{}, @code{expr_n} can be any expressions,
534 not necessarily numeric.
537 @category{Declarations and inferences}
538 @category{Numerical evaluation}
542 @c -----------------------------------------------------------------------------
544 @deffn {Function} oddp (@var{expr})
546 @c THIS IS STRANGE -- SHOULD RETURN NOUN FORM IF INDETERMINATE
547 Returns @code{true} if @var{expr} is a literal odd integer, otherwise
550 @code{oddp} returns @code{false} if @var{expr} is a symbol, even if @var{expr}
551 is declared @code{odd}.
554 @category{Predicate functions}
558 @c --- 03.11.2011 --------------------------------------------------------------
560 @defvr {Option variable} ratepsilon
561 Default value: @code{2.0e-15}
563 @code{ratepsilon} is the tolerance used in the conversion
564 of floating point numbers to rational numbers, when the option variable
565 @mref{bftorat} has the value @code{false}. See @code{bftorat} for an example.
568 @category{Numerical evaluation}
569 @category{Rational expressions}
573 @c -----------------------------------------------------------------------------
575 @deffn {Function} rationalize (@var{expr})
577 Convert all double floats and big floats in the Maxima expression @var{expr} to
578 their exact rational equivalents. If you are not familiar with the binary
579 representation of floating point numbers, you might be surprised that
580 @code{rationalize (0.1)} does not equal 1/10. This behavior isn't special to
581 Maxima -- the number 1/10 has a repeating, not a terminating, binary
585 @c rationalize (0.5);
586 @c rationalize (0.1);
588 @c rationalize (0.1b0);
590 @c rationalize (0.1b0);
591 @c rationalize (sin (0.1*x + 5.6));
595 (%i1) rationalize (0.5);
601 (%i2) rationalize (0.1);
603 (%o2) -----------------
608 (%i4) rationalize (0.1b0);
615 (%i6) rationalize (0.1b0);
616 236118324143482260685
617 (%o6) ----------------------
618 2361183241434822606848
621 (%i7) rationalize (sin (0.1*x + 5.6));
622 3602879701896397 x 3152519739159347
623 (%o7) sin(------------------ + ----------------)
624 36028797018963968 562949953421312
629 @category{Numerical evaluation}
633 @c -----------------------------------------------------------------------------
635 @deffn {Function} ratnump (@var{expr})
637 Returns @code{true} if @var{expr} is a literal integer or ratio of literal
638 integers, otherwise @code{false}.
641 @category{Predicate functions}
642 @category{Rational expressions}
647 @c -----------------------------------------------------------------------------
649 @node Strings, Constants, Numbers, Data Types and Structures
651 @c -----------------------------------------------------------------------------
654 * Introduction to Strings::
655 * Functions and Variables for Strings::
658 @c -----------------------------------------------------------------------------
659 @node Introduction to Strings, Functions and Variables for Strings, Strings, Strings
660 @subsection Introduction to Strings
661 @c -----------------------------------------------------------------------------
664 @c The following three lines were commented out since they made "make pdf" abort
670 @c adding the backslash to the index here breaks the LaTeX syntax of the file
671 @c maxima.fns that is created by the first pdfLaTeX run by "make pdf".
674 Strings (quoted character sequences) are enclosed in double quote marks @code{"}
675 for input, and displayed with or without the quote marks, depending on the
676 global variable @mrefdot{stringdisp}
678 Strings may contain any characters, including embedded tab, newline, and
679 carriage return characters. The sequence @code{\"} is recognized as a literal
680 double quote, and @code{\\} as a literal backslash. When backslash appears at
681 the end of a line, the backslash and the line termination (either newline or
682 carriage return and newline) are ignored, so that the string continues with the
683 next line. No other special combinations of backslash with another character
684 are recognized; when backslash appears before any character other than @code{"},
685 @code{\}, or a line termination, the backslash is ignored. There is no way to
686 represent a special character (such as tab, newline, or carriage return)
687 except by embedding the literal character in the string.
689 There is no character type in Maxima; a single character is represented as a
690 one-character string.
692 The @code{stringproc} add-on package contains many functions for working with
698 @c s_1 : "This is a string.";
699 @c s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
700 @c s_3 : "Embedded line termination
702 @c s_4 : "Ignore the \
703 @c line termination \
706 @c stringdisp : false;
708 @c stringdisp : true;
713 (%i1) s_1 : "This is a string.";
714 (%o1) This is a string.
717 (%i2) s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
718 (%o2) Embedded "double quotes" and backslash \ characters.
721 (%i3) s_3 : "Embedded line termination
722 (%o3) Embedded line termination
726 (%i4) in this string.";
727 (%o4) Ignore the line termination characters in this string.
730 (%i5) s_4 : "Ignore the \
734 (%i6) line termination \
735 (%o6) This is a string.
738 (%i7) characters in \
743 (%o8) "This is a string."
745 (%i9) stringdisp : false;
752 @c -----------------------------------------------------------------------------
753 @node Functions and Variables for Strings, , Introduction to Strings, Strings
754 @subsection Functions and Variables for Strings
755 @c -----------------------------------------------------------------------------
757 @c -----------------------------------------------------------------------------
759 @deffn {Function} concat (@var{arg_1}, @var{arg_2}, @dots{})
761 Concatenates its arguments. The arguments must evaluate to atoms. The return
762 value is a symbol if the first argument is a symbol and a string otherwise.
764 @code{concat} evaluates its arguments. The single quote @code{'} prevents
767 See also @mrefcomma{sconcat} that works on non-atoms, too, @mrefcomma{simplode}
768 @mref{string} and @mrefdot{eval_string}
769 For complex string conversions see also @mref{printf}.
780 (%i3) concat (y, z/2);
782 (%i4) concat ('y, z/2);
786 A symbol constructed by @code{concat} may be assigned a value and appear in
787 expressions. The @mref{::} (double colon) assignment operator evaluates its
791 @c a: concat ('y, z/2);
798 (%i5) a: concat ('y, z/2);
814 Note that although @code{concat (1, 2)} looks like a number, it is a string.
817 @c concat (1, 2) + 3;
820 (%i10) concat (1, 2) + 3;
825 @category{Expressions}
830 @c -----------------------------------------------------------------------------
832 @deffn {Function} sconcat (@var{arg_1}, @var{arg_2}, @dots{})
834 Concatenates its arguments into a string. Unlike @mrefcomma{concat} the
835 arguments do @i{not} need to be atoms.
837 See also @mrefcomma{concat} @mrefcomma{simplode} @mref{string} and @mrefdot{eval_string}
838 For complex string conversions see also @mref{printf}.
841 @c sconcat ("xx[", 3, "]:", expand ((x+y)^3));
845 (%i1) sconcat ("xx[", 3, "]:", expand ((x+y)^3));
846 (%o1) xx[3]:y^3+3*x*y^2+3*x^2*y+x^3
850 Another purpose for @code{sconcat} is to convert arbitrary objects to strings.
867 @category{Expressions}
872 @c NEEDS CLARIFICATION AND EXAMPLES
874 @c -----------------------------------------------------------------------------
876 @deffn {Function} string (@var{expr})
878 Converts @code{expr} to Maxima's linear notation just as if it had been typed
881 The return value of @code{string} is a string, and thus it cannot be used in a
884 See also @mrefcomma{concat} @mrefcomma{sconcat} @mref{simplode} and
885 @mrefdot{eval_string}
892 @c SHOULD BE WRITTEN WITH LEADING ? BUT THAT CONFUSES CL-INFO SO WORK AROUND
894 @c -----------------------------------------------------------------------------
896 @defvr {Option variable} stringdisp
897 Default value: @code{false}
899 When @code{stringdisp} is @code{true}, strings are displayed enclosed in double
900 quote marks. Otherwise, quote marks are not displayed.
902 @code{stringdisp} is always @code{true} when displaying a function definition.
907 @c stringdisp: false$
908 @c "This is an example string.";
910 @c print ("This is a string in a function definition.");
912 @c "This is an example string.";
915 (%i1) stringdisp: false$
917 (%i2) "This is an example string.";
918 (%o2) This is an example string.
922 print ("This is a string in a function definition.");
924 print("This is a string in a function definition.")
926 (%i4) stringdisp: true$
928 (%i5) "This is an example string.";
929 (%o5) "This is an example string."
934 @category{Display flags and variables}