## 7 Data Types and Structures ¶

### 7.1 Numbers ¶

#### 7.1.1 Introduction to Numbers ¶

#### Complex numbers ¶

A complex expression is specified in Maxima by adding the real part of the expression to `%i` times the imaginary part. Thus the roots of the equation `x^2 - 4*x + 13 = 0` are `2 + 3*%i` and `2 - 3*%i`. Note that simplification of products of complex expressions can be effected by expanding the product. Simplification of quotients, roots, and other functions of complex expressions can usually be accomplished by using the `realpart`, `imagpart`, `rectform`, `polarform`, `abs`, `carg` functions.

Categories: Complex variables ·

#### 7.1.2 Functions and Variables for Numbers ¶

Function: bfloat (expr) ¶

  `bfloat` replaces integers, rationals, floating point numbers, and some symbolic constants in expr with bigfloat (variable-precision floating point) numbers.

  The constants `%e`, `%gamma`, `%phi`, and `%pi` are replaced by a numerical approximation. However, `%e` in `%e^x` is not replaced by a numeric value unless `bfloat(x)` is a number.

  `bfloat` also causes numerical evaluation of some built-in functions, namely trigonometric functions, exponential functions, `abs`, and `log`.

  The number of significant digits in the resulting bigfloats is specified by the global variable `fpprec`. Bigfloats already present in expr are replaced with values which have precision specified by the current value of `fpprec`.

  When `float2bf` is `false`, a warning message is printed when a floating point number is replaced by a bigfloat number with less precision.

  Examples:

  `bfloat` replaces integers, rationals, floating point numbers, and some symbolic constants in expr with bigfloat numbers.

      (%i1) bfloat([123, 17/29, 1.75]);
      (%o1)        [1.23b2, 5.862068965517241b-1, 1.75b0]
      (%i2) bfloat([%e, %gamma, %phi, %pi]);
      (%o2) [2.718281828459045b0, 5.772156649015329b-1,
                              1.618033988749895b0, 3.141592653589793b0]
      (%i3) bfloat((f(123) + g(h(17/29)))/(x + %gamma));
               1.0b0 (g(h(5.862068965517241b-1)) + f(1.23b2))
      (%o3)    ----------------------------------------------
                          x + 5.772156649015329b-1

  `bfloat` also causes numerical evaluation of some built-in functions.

      (%i1) bfloat(sin(17/29));
      (%o1)                 5.532051841609784b-1
      (%i2) bfloat(exp(%pi));
      (%o2)                  2.314069263277927b1
      (%i3) bfloat(abs(-%gamma));
      (%o3)                 5.772156649015329b-1
      (%i4) bfloat(log(%phi));
      (%o4)                 4.812118250596035b-1

  Categories: Numerical evaluation ·

Function: bfloatp (expr) ¶

  Returns `true` if expr is a bigfloat number, otherwise `false`.

  Categories: Numerical evaluation · Predicate functions ·

Option variable: bftorat ¶

  Default value: `false`

  `bftorat` controls the conversion of bfloats to rational numbers. When `bftorat` is `false`, `ratepsilon` will be used to control the conversion (this results in relatively small rational numbers). When `bftorat` is `true`, the rational number generated will accurately represent the bfloat.

  Note: `bftorat` has no effect on the transformation to rational numbers with the function `rationalize`.

  Example:

      (%i1) ratepsilon:1e-4;
      (%o1)                         1.0e-4
      (%i2) rat(bfloat(11111/111111)), bftorat:false;
      `rat' replaced 9.99990999991B-2 by 1/10 = 1.0B-1
                                     1
      (%o2)/R/                       --
                                     10
      (%i3) rat(bfloat(11111/111111)), bftorat:true;
      `rat' replaced 9.99990999991B-2 by 11111/111111 = 9.99990999991B-2
                                   11111
      (%o3)/R/                     ------
                                   111111

  Categories: Numerical evaluation ·

Option variable: bftrunc ¶

  Default value: `true`

  `bftrunc` causes trailing zeroes in non-zero bigfloat numbers not to be displayed. Thus, if `bftrunc` is `false`, `bfloat (1)` displays as `1.000000000000000B0`. Otherwise, this is displayed as `1.0B0`.

  Categories: Numerical evaluation ·

Function: bigfloat_bits () ¶

  Returns the number of bits of precision in a bigfloat number. This value depends, of course, on the value of `fpprec`.

      (%i1) fpprec:16;
      (%o1)                                 16
      (%i2) bigfloat_bits();
      (%o2)                                 56
      (%i3) fpprec:32;
      (%o3)                                 32
      (%i4) bigfloat_bits();
      (%o4)                                 109

  Categories: Numerical evaluation ·

Function: bigfloat_eps () ¶

  Returns the smallest bigfloat value, `eps`, such that `1+eps` is not equal to 1. The value depends on `fpprec`, of course.

      (%i1) fpprec:16;
      (%o1)                                 16
      (%i2) bigfloat_eps();
      (%o2)                        1.387778780781446b-17
      (%i3) fpprec:32;
      (%o3)                                 32
      (%i4) bigfloat_eps();
      (%o4)                1.5407439555097886824447823540679b-33

  Categories: Numerical evaluation ·

Function: decode_float (f) ¶

  `decode_float` takes a float f and returns a list of three values that characterizes f, which must be either a `float` or `bfloat`. The first value has the same type as f, but is a number in the range `[1, 2)`. The second value is an exponent. The third value is a float of the same type as f and has the value of 1 if f is greater than or equal to 0; otherwise, -1.

  If the returned list is `[mantissa, expo, sign]`, then `scale_float(mantissa, exp)*sign` is identical to f.

      (%i1) decode_float(4e0);
      (%o1)                            [1.0, 2, 1.0]
      (%i2) decode_float(4b0);
      (%o2)                          [1.0b0, 2, 1.0b0]
      (%i3) decode_float(%pi);

      decode_float is only defined for floats and bfloats: %pi
       -- an error. To debug this try: debugmode(true);
      (%i4) decode_float(float(%pi));
      (%o4)                     [1.570796326794897, 1, 1.0]
      (%i5) decode_float(1.1e-5);
      (%o5)                        [1.441792, - 17, 1.0]
      (%i6) %[1]*2^%[2];
      (%o6)                               1.1e-5

  This is a relatively simple interface to Common Lisp decode_float. However we return a signficand in the range `[1,2)` instead of `[0.5, 1)`. The former matches IEEE-754. Of course, this is extended to support bfloats.

  Categories: Numerical evaluation ·

Function: evenp (expr) ¶

  Returns `true` if expr is a literal even integer, otherwise `false`.

  `evenp` returns `false` if expr is a symbol, even if expr is declared `even`.

  Categories: Predicate functions ·

Function: float (expr) ¶

  Converts integers, rational numbers and bigfloats in expr to floating point numbers. It is also an `evflag`, `float` causes non-integral rational numbers and bigfloat numbers to be converted to floating point.

  Categories: Numerical evaluation · Evaluation flags ·

Option variable: float2bf ¶

  Default value: `true`

  When `float2bf` is `false`, a warning message is printed when a floating point number is replaced by a bigfloat number with less precision.

  Categories: Numerical evaluation ·

Function: float_bits () ¶

  Returns the number of bits of precision of a floating-point number.

  Categories: Numerical evaluation ·

Function: float_eps () ¶

  Returns the smallest floating-point value, `eps`, such that `1+eps` is not equal to 1.

  Categories: Numerical evaluation ·

Function: float_precision (f) ¶

  Returns the number of bits of precision of a floating-point number, which can be either a float or bigfloat. This is basically the number of bits used to represent the mantissa of a floating-point number. For floats, this is 53 (for IEEE double-floats), but can be less when denormal numbers occur. For bigfloats, this is equal to `fpprec`, when converted from digits to bits.

  Categories: Numerical evaluation ·

Function: float_sign (f) ¶

  Returns the sign of f. It is \(+1\) or \(-1\) of the same type as f. It is an error if f is not a float or bigfloat. Note that some lisps do not support signed zeros for floating-point numbers. Bigfloats do not support signed zeroes. The examples below assume signed zeroes are supported.

      (%i1) float_sign(1.0);
      (%o1)                                 1.0
      (%i2) float_sign(-5.0);
      (%o2)                                - 1.0
      (%i3) float_sign(-0.0);
      (%o3)                                - 1.0
      (%i4) float_sign(1b0);
      (%o4)                                1.0b0
      (%i5) float_sign(-5b0);
      (%o5)                               - 1.0b0
      (%o6) float_sign(-0b0);
      (%o6)                                1.0b0
      (%i7) float_sign(%pi);

      float_sign is only defined for floats and bfloats: %pi
       -- an error. To debug this try: debugmode(true);

Function: float_infinity_p (x) ¶

  Returns `true` if x is floating point positive infinity or floating point negative infinity, and returns `false` for all other arguments; arguments which are not numbers are allowed, and `float_infinity_p` returns `false` for all such arguments.

  Positive and negative floating point infinity may be distinguished by `sign`, which returns `pos` for positive infinity and `neg` for negative infinity.

  `float_infinity_p` is defined whether or not the Lisp implementation supports float infinity. When float infinity does not exist in the Lisp implementation’s number system, `float_infinity_p` returns `false` for all arguments.

  A Lisp implementation may support more than one precision of floating point numbers. `float_infinity_p` only recognizes double precision floating point infinity, and not any other precision.

  Categories: Numerical evaluation · Predicate functions ·

Function: float_nan_p (x) ¶

  Returns `true` if x is a floating point not-a-number (NaN) value, and returns `false` for all other arguments; arguments which are not numbers are allowed, and `float_nan_p` returns `false` for all such arguments.

  `float_nan_p` is defined whether or not the Lisp implementation supports floating point not-a-number values. When floating point not-a-number does not exist in the Lisp implementation’s number system, `float_nan_p` returns `false` for all arguments.

  A Lisp implementation may support more than one precision of floating point numbers. `float_nan_p` only recognizes double precision floating point not-a-number, and not any other precision.

  Categories: Numerical evaluation · Predicate functions ·

Function: floatnump (expr) ¶

  Returns `true` if expr is a floating point number, otherwise `false`.

  Categories: Numerical evaluation · Predicate functions ·

Option variable: fpprec ¶

  Default value: 16

  `fpprec` is the number of significant digits for arithmetic on bigfloat numbers. `fpprec` does not affect computations on ordinary floating point numbers.

  See also `bfloat` and `fpprintprec`.

  Categories: Numerical evaluation ·

Option variable: fpprintprec ¶

  Default value: 0

  `fpprintprec` is the number of digits to print when printing an ordinary float or bigfloat number.

  For ordinary floating point numbers, when `fpprintprec` has a value between 2 and 16 (inclusive), the number of digits printed is equal to `fpprintprec`. Otherwise, `fpprintprec` is 0, or greater than 16, and the number is printed "readably": that is, it is printed with sufficient digits to exactly reconstruct the number on input.

  For bigfloat numbers, when `fpprintprec` has a value between 2 and `fpprec` (inclusive), the number of digits printed is equal to `fpprintprec`. Otherwise, `fpprintprec` is 0, or greater than `fpprec`, and the number of digits printed is equal to `fpprec`.

  For both ordinary floats and bigfloats, trailing zero digits are suppressed. The actual number of digits printed is less than `fpprintprec` if there are trailing zero digits.

  `fpprintprec` cannot be 1.

  Categories: Numerical evaluation · Display flags and variables ·

Function: integerp (expr) ¶

  Returns `true` if expr is a literal numeric integer, otherwise `false`.

  `integerp` returns `false` if expr is a symbol, even if expr is declared `integer`.

  Examples:

      (%i1) integerp (0);
      (%o1)                         true
      (%i2) integerp (1);
      (%o2)                         true
      (%i3) integerp (-17);
      (%o3)                         true
      (%i4) integerp (0.0);
      (%o4)                         false
      (%i5) integerp (1.0);
      (%o5)                         false
      (%i6) integerp (%pi);
      (%o6)                         false
      (%i7) integerp (n);
      (%o7)                         false
      (%i8) declare (n, integer);
      (%o8)                         done
      (%i9) integerp (n);
      (%o9)                         false

  Categories: Predicate functions ·

Function: integer_decode_float (f) ¶

  `integer_decode_float` takes a float f and returns a list of three values that characterizes f, which must be either a `float` or `bfloat`. The first value is an integer. The second value is an exponent. The third value is 1 if f is positive or zero; otherwise, -1.

  If the returned list is `[mantissa, expo, sign]`, then `scale_float(fl(mantissa), expo)*sign` is identical to f. Here, `fl` is either `float` or `bfloat` depending on whether f is a `float` or a `bfloat`.

      (%i1) integer_decode_float(4.0);
      (%o1)                     [4503599627370496, - 50, 1]
      (%i2) integer_decode_float(4b0);
      (%o2)                    [36028797018963968, - 53, 1]
      (%i3) scale_float(float(%o1[1]), %o1[2]);
      (%o3)                                 4.0
      (%i4) scale_float(bfloat(%o2[1]), %o2[2]);
      (%o4)                                4.0b0
      (%i5) integer_decode_float(4);

      decode_float is only defined for floats and bfloats: 4
       -- an error. To debug this try: debugmode(true);
      (%i6) integer_decode_float(1e-7);
      (%o6)                     [7555786372591432, - 76, 1]
      (%i7) integer_decode_float(1b-7);
      (%o7)                    [60446290980731459, - 79, 1]
      (%i8) scale_float(float(%o6[1]), %o6[2]);
      (%o8)                               1.0e-7

  For lisps that support denormal numbers, we have the following results.

      (%i1) integer_decode_float(least_positive_float);
      (%o1)                           [1, - 1074, 1]
      (%i2) integer_decode_float(100*least_positive_float);
      (%o2)                          [100, - 1074, 1]
      (%i3) integer_decode_float(least_positive_normalized_float);
      (%o3)                    [4503599627370496, - 1074, 1]

  The number of bits in the integer part decreases as the denormal number decreases. Bfloat numbers do not have denormals because the exponent is not bounded.

  This is a relatively simple interface to Common Lisp integer_decode_float. However, the integer part can vary depending on the Lisp implementation; we return the same value, independent of the Lisp implementation. Of course, this is extended to support bfloats.

  Categories: Numerical evaluation ·

Function: is_power_of_two (n) ¶

  `is_power_to_two` returns `true` if n is a power of two and `false` otherwise. n may be an integer, a rational, a float, or a big float.

  Some examples:

      (%i1) is_power_of_two(0);
      (%o1)                                false
      (%i2) is_power_of_two(4);
      (%o2)                                true
      (%i3) is_power_of_two(355/113);
      (%o3)                                false
      (%i4) is_power_of_two(1/32);
      (%o4)                                true
      (%i5) is_power_of_two(1048576);
      (%o5)                                true
      (%i6) is_power_of_two(1048575);
      (%o6)                                false
      (%i7) is_power_of_two(0.0);
      (%o7)                                false
      (%i8) is_power_of_two(1048576.0);
      (%o8)                                true
      (%i9) is_power_of_two(1048575.0);
      (%o9)                                false
      (%i10) is_power_of_two(1/256.0);
      (%o10)                               true
      (%i11) is_power_of_two(0b0);
      (%o11)                               false
      (%i12) is_power_of_two(1048576b0);
      (%o12)                               true
      (%i13) is_power_of_two(1048575b0);
      (%o13)                               false
      (%i14) is_power_of_two(1/256b0);
      (%o14)                               true

  Categories: Predicate functions ·

Option variable: m1pbranch ¶

  Default value: `false`

  `m1pbranch` is the principal branch for `-1` to a power. Quantities such as `(-1)^(1/3)` (that is, an "odd" rational exponent) and `(-1)^(1/4)` (that is, an "even" rational exponent) are handled as follows:

                    domain:real

      (-1)^(1/3):      -1
      (-1)^(1/4):   (-1)^(1/4)

                   domain:complex
      m1pbranch:false          m1pbranch:true
      (-1)^(1/3)               1/2+%i*sqrt(3)/2
      (-1)^(1/4)              sqrt(2)/2+%i*sqrt(2)/2

  Categories: Expressions · Global flags ·

Function: nonnegintegerp (n) ¶

  Return `true` if and only if `n >= 0` and n is an integer.

  Categories: Predicate functions ·

Function: numberp (expr) ¶

  Returns `true` if expr is a literal integer, rational number, floating point number, or bigfloat, otherwise `false`.

  `numberp` returns `false` if expr is a symbol, even if expr is a symbolic number such as `%pi` or `%i`, or declared to be `even`, `odd`, `integer`, `rational`, `irrational`, `real`, `imaginary`, or `complex`.

  Examples:

      (%i1) numberp (42);
      (%o1)                         true
      (%i2) numberp (-13/19);
      (%o2)                         true
      (%i3) numberp (3.14159);
      (%o3)                         true
      (%i4) numberp (-1729b-4);
      (%o4)                         true
      (%i5) map (numberp, [%e, %pi, %i, %phi, inf, minf]);
      (%o5)      [false, false, false, false, false, false]
      (%i6) declare (a, even, b, odd, c, integer, d, rational,
           e, irrational, f, real, g, imaginary, h, complex);
      (%o6)                         done
      (%i7) map (numberp, [a, b, c, d, e, f, g, h]);
      (%o7) [false, false, false, false, false, false, false, false]

  Categories: Predicate functions ·

Option variable: numer ¶

  `numer` causes some mathematical functions (including exponentiation) with numerical arguments to be evaluated in floating point. It causes variables in `expr` which have been given numerals to be replaced by their values. It also sets the `float` switch on.

  See also `%enumer`.

  Examples:

      (%i1) [sqrt(2), sin(1), 1/(1+sqrt(3))];
                                              1
      (%o1)            [sqrt(2), sin(1), -----------]
                                         sqrt(3) + 1

      (%i2) [sqrt(2), sin(1), 1/(1+sqrt(3))],numer;
      (%o2) [1.414213562373095, 0.8414709848078965, 0.3660254037844387]

  Categories: Numerical evaluation · Evaluation flags ·

Option variable: numer_pbranch ¶

  Default value: `false`

  The option variable `numer_pbranch` controls the numerical evaluation of the power of a negative integer, rational, or floating point number. When `numer_pbranch` is `true` and the exponent is a floating point number or the option variable `numer` is `true` too, Maxima evaluates the numerical result using the principal branch. Otherwise a simplified, but not an evaluated result is returned.

  Examples:

      (%i1) (-2)^0.75;
                                       0.75
      (%o1)                       (- 2)

      (%i2) (-2)^0.75,numer_pbranch:true;
      (%o2)       1.189207115002721 %i - 1.189207115002721

      (%i3) (-2)^(3/4);
                                     3/4  3/4
      (%o3)                     (- 1)    2

      (%i4) (-2)^(3/4),numer;
                                                0.75
      (%o4)              1.681792830507429 (- 1)

      (%i5) (-2)^(3/4),numer,numer_pbranch:true;
      (%o5)       1.189207115002721 %i - 1.189207115002721

  Categories: Numerical evaluation ·

Function: numerval (x_1, expr_1, …, var_n, expr_n) ¶

  Declares the variables `x_1`, …, x_n to have numeric values equal to `expr_1`, …, `expr_n`. The numeric value is evaluated and substituted for the variable in any expressions in which the variable occurs if the `numer` flag is `true`. See also `ev`.

  The expressions `expr_1`, …, `expr_n` can be any expressions, not necessarily numeric.

  Categories: Declarations and inferences · Numerical evaluation ·

Function: oddp (expr) ¶

  Returns `true` if expr is a literal odd integer, otherwise `false`.

  `oddp` returns `false` if expr is a symbol, even if expr is declared `odd`.

  Categories: Predicate functions ·

Option variable: ratepsilon ¶

  Default value: `2.0e-15`

  `ratepsilon` is the tolerance used in the conversion of floating point numbers to rational numbers, when the option variable `bftorat` has the value `false`. See `bftorat` for an example.

  Categories: Numerical evaluation · Rational expressions ·

Function: rationalize (expr) ¶

  Convert all double floats and big floats in the Maxima expression expr to their exact rational equivalents. If you are not familiar with the binary representation of floating point numbers, you might be surprised that `rationalize (0.1)` does not equal 1/10. This behavior isn’t special to Maxima – the number 1/10 has a repeating, not a terminating, binary representation.

      (%i1) rationalize (0.5);
                                      1
      (%o1)                           -
                                      2

      (%i2) rationalize (0.1);
                              3602879701896397
      (%o2)                   -----------------
                              36028797018963968

      (%i3) fpprec : 5$

      (%i4) rationalize (0.1b0);
                                   209715
      (%o4)                        -------
                                   2097152

      (%i5) fpprec : 20$

      (%i6) rationalize (0.1b0);
                           236118324143482260685
      (%o6)                ----------------------
                           2361183241434822606848

      (%i7) rationalize (sin (0.1*x + 5.6));
                     3602879701896397 x   3152519739159347
      (%o7)      sin(------------------ + ----------------)
                     36028797018963968    562949953421312

  Categories: Numerical evaluation ·

Function: ratnump (expr) ¶

  Returns `true` if expr is a literal integer or ratio of literal integers, otherwise `false`.

  Categories: Predicate functions · Rational expressions ·

Function: scale_float (f, n) ¶

  `scale_float` scales the float f by the value `2^n`. This is done carefully so that no round-off every occurs. If f is a float, then it is possible to underflow to 0 or overflow, depending on the value of f and n. Bigfloats cannot underflow or overflow.

      (%i1) scale_float(2d0, 2);
      (%o1)                                 8.0
      (%i2) scale_float(2d0, -2);
      (%o2)                                 0.5
      (%i3) scale_float(-2d0, -10);
      (%o3)                            - 0.001953125
      (%i4) scale_float(1d0, -2000);
      (%o4)                                 0.0
      (%i5) scale_float(2b0, 2);
      (%o5)                                8.0b0
      (%i6) scale_float(1b0, -2000);
      (%o6)                       8.709809816217217b-603
      (%i7) scale_float(1, 5);

      scale_float: first arg must be a float or bfloat: 1
       -- an error. To debug this try: debugmode(true);
      (%i8) scale_float(1.0, n);

      scale_float: second arg must be an integer: n
       -- an error. To debug this try: debugmode(true);

  This is a relatively simple interface to Common Lisp scale_float. Of course, this is extended to support bfloats.

Function: unit_in_last_place (n) ¶

  `unit_in_last_place` returns a value that is the gap between n and the nearest other number. See, for example, Kahan, FOOTNOTE 1. `unit_in_last_place` supports rational numbers, floating-point numbers and bigfloat numbers. For integer, the result is always 1, and for rational numbers the result is always 0.

  The examples below assume IEEE-754 arithmetic that supports denormal numbers. Some lisps like Clisp do not have denormal numbers.

      (%i1) unit_in_last_place(0);
      (%o1)                                  1
      (%i2) unit_in_last_place(-123);
      (%o2)                                  1
      (%i3) unit_in_last_place(2/3);
      (%o3)                                  0
      (%i4) unit_in_last_place(355/113);
      (%o4)                                  0
      (%i5) unit_in_last_place(0b0);
      (%o5)                                0.0b0
      (%i6) unit_in_last_place(0.0);
      (%o6)                       4.940656458412465e-324
      (%i7) unit_in_last_place(1.0);
      (%o7)                        1.110223024625157e-16
      (%i8) unit_in_last_place(1b0);
      (%o8)                        1.387778780781446b-17
      (%i9) unit_in_last_place(100.0);
      (%o9)                         1.4210854715202e-14
      (%i10) unit_in_last_place(100b0);
      (%o10)                       1.77635683940025b-15
      (%i11) fpprec:32;
      (%o11)                                32
      (%i12) unit_in_last_place(1b0);
      (%o12)               1.5407439555097886824447823540679b-33
      (%i13) unit_in_last_place(100b0);
      (%o13)               1.972152263052529513529321413207b-31

  Categories: Numerical evaluation ·

### 7.2 Strings ¶

#### 7.2.1 Introduction to Strings ¶

Strings (quoted character sequences) are enclosed in double quote marks `"` for input, and displayed with or without the quote marks, depending on the global variable `stringdisp`.

Strings may contain any characters, including embedded tab, newline, and carriage return characters. The sequence `\"` is recognized as a literal double quote, and `\\` as a literal backslash. When backslash appears at the end of a line, the backslash and the line termination (either newline or carriage return and newline) are ignored, so that the string continues with the next line. No other special combinations of backslash with another character are recognized; when backslash appears before any character other than `"`, `\`, or a line termination, the backslash is ignored. There is no way to represent a special character (such as tab, newline, or carriage return) except by embedding the literal character in the string.

There is no character type in Maxima; a single character is represented as a one-character string.

The `stringproc` add-on package contains many functions for working with strings.

Examples:

    (%i1) s_1 : "This is a string.";
    (%o1)                   This is a string.

    (%i2) s_2 : "Embedded \"double quotes\" and backslash \\ characters.";
    (%o2) Embedded "double quotes" and backslash \ characters.

    (%i3) s_3 : "Embedded line termination
    in this string.";
    (%o3) Embedded line termination
    in this string.

    (%i4) s_4 : "Ignore the \
    line termination \
    characters in \
    this string.";
    (%o4) Ignore the line termination characters in this string.

    (%i5) stringdisp : false;
    (%o5)                         false

    (%i6) s_1;
    (%o6)                   This is a string.

    (%i7) stringdisp : true;
    (%o7)                         true

    (%i8) s_1;
    (%o8)                  "This is a string."

Categories: Syntax ·

#### 7.2.2 Functions and Variables for Strings ¶

Function: concat (arg_1, arg_2, …) ¶

  Concatenates its arguments. The arguments must evaluate to atoms. The return value is a symbol if the first argument is a symbol and a string otherwise.

  `concat` evaluates its arguments. The single quote `'` prevents evaluation.

  See also `sconcat`, that works on non-atoms, too, `simplode`, `string` and `eval_string`. For complex string conversions see also `printf`.

      (%i1) y: 7$
      (%i2) z: 88$
      (%i3) concat (y, z/2);
      (%o3)                          744
      (%i4) concat ('y, z/2);
      (%o4)                          y44

  A symbol constructed by `concat` may be assigned a value and appear in expressions. The `::` (double colon) assignment operator evaluates its left-hand side.

      (%i5) a: concat ('y, z/2);
      (%o5)                          y44
      (%i6) a:: 123;
      (%o6)                          123
      (%i7) y44;
      (%o7)                          123
      (%i8) b^a;
                                     y44
      (%o8)                         b
      (%i9) %, numer;
                                     123
      (%o9)                         b

  Note that although `concat (1, 2)` looks like a number, it is a string.

      (%i10) concat (1, 2) + 3;
      (%o10)                       12 + 3

  Categories: Expressions · Strings ·

Function: sconcat (arg_1, arg_2, …) ¶

  Concatenates its arguments into a string. Unlike `concat`, the arguments do not need to be atoms.

  See also `concat`, `simplode`, `string` and `eval_string`. For complex string conversions see also `printf`.

      (%i1) sconcat ("xx[", 3, "]:", expand ((x+y)^3));
      (%o1)             xx[3]:y^3+3*x*y^2+3*x^2*y+x^3

  Another purpose for `sconcat` is to convert arbitrary objects to strings.

      (%i1) sconcat (x);
      (%o1)                           x

      (%i2) stringp(%);
      (%o2)                         true

  Categories: Expressions · Strings ·

Function: string (expr) ¶

  Converts `expr` to Maxima’s linear notation just as if it had been typed in.

  The return value of `string` is a string, and thus it cannot be used in a computation.

  See also `concat`, `sconcat`, `simplode` and `eval_string`.

  Categories: Strings ·

Option variable: stringdisp ¶

  Default value: `false`

  When `stringdisp` is `true`, strings are displayed enclosed in double quote marks. Otherwise, quote marks are not displayed.

  `stringdisp` is always `true` when displaying a function definition.

  Examples:

      (%i1) stringdisp: false$

      (%i2) "This is an example string.";
      (%o2)              This is an example string.

      (%i3) foo () :=
            print ("This is a string in a function definition.");
      (%o3) foo() :=
                    print("This is a string in a function definition.")

      (%i4) stringdisp: true$

      (%i5) "This is an example string.";
      (%o5)             "This is an example string."

  Categories: Display flags and variables ·

### 7.3 Constants ¶

#### 7.3.1 Functions and Variables for Constants ¶

Constant: %catalan ¶

  `%catalan` represents Catalan’s constant, \(G\), defined by $$ G = \sum_{n=0}^\infty {(-1)^n\over (2n+1)^2} $$

  (It is also sometimes denoted by \(C\)).

  The numeric value of `%catalan` is approximately 0.915965594177219. (See DLMF 25.11.E40).

  Categories: Constants ·

Constant: %e ¶

  `%e` represents the base of the natural logarithm, also known as Euler’s number. The numeric value of `%e` is the double-precision floating-point value 2.718281828459045d0. (See A&S eqn 4.1.16, A&S 4.1.17.)

  Categories: Constants ·

Constant: %i ¶

  `%i` represents the imaginary unit, \(\sqrt{-1}.\)

  Categories: Constants ·

Constant: false ¶

  `false` represents the Boolean constant of the same name. Maxima implements `false` by the value `NIL` in Lisp.

  Categories: Constants ·

Constant: %gamma ¶

  The Euler-Mascheroni constant, 0.5772156649015329.... It is defined by (A&S eqn 6.1.3 and DLMF 5.2.ii) $$ \gamma = \lim_{n \rightarrow \infty} \left(\sum_{k=1}^n {1\over k} - \log n\right) $$

  Categories: Constants ·

Constant: ind ¶

  `ind` represents a bounded, indefinite result.

  See also `limit`.

  Example:

      (%i1) limit (sin(1/x), x, 0);
      (%o1)                          ind

  Categories: Constants ·

Constant: inf ¶

  `inf` represents real positive infinity.

  Categories: Constants ·

Constant: infinity ¶

  `infinity` represents complex infinity.

  Categories: Constants ·

Constant: least_negative_float ¶

  The least negative floating-point number in Maxima. That is, the negative floating-point number closest to 0. It is approximately -4.94065e-324, when denormal numbers are supported. Otherwise it is the same as `least_negative_normalized_float`.

  Categories: Constants ·

Constant: least_negative_normalized_float ¶

  The least negative normalized floating-point number in Maxima. That is, the negative normalized floating-point number closest to 0. It is approximately -2.22507e-308.

  Categories: Constants ·

Constant: least_positive_float ¶

  The least positive floating-point number in Maxima. That is, the positive floating-point number closest to 0. It is approximately 4.94065e-324, when denormal numbers are supported. Otherwise it is the same as `least_positive_normalized_float`.

  Categories: Constants ·

Constant: least_positive_normalized_float ¶

  The least positive normalized floating-point number in Maxima. That is, the positive normalized floating-point number closest to 0. It is approximately 2.22507e-308.

  Categories: Constants ·

Constant: minf ¶

  `minf` represents real minus (i.e., negative) infinity.

  Categories: Constants ·

Constant: most_negative_float ¶

  The most negative floating-point number in Maxima. It is approximately -1.79769e+308.

  Categories: Constants ·

Constant: most_positive_float ¶

  The most positive floating-point number in Maxima. It is approximately 1.797693e+308.

  Categories: Constants ·

Constant: %phi ¶

  `%phi` represents the so-called golden mean, \((1+\sqrt{5})/2.\) The numeric value of `%phi` is the double-precision floating-point value 1.618033988749895d0.

  `fibtophi` expresses Fibonacci numbers `fib(n)` in terms of `%phi`.

  By default, Maxima does not know the algebraic properties of `%phi`. After evaluating `tellrat(%phi^2 - %phi - 1)` and `algebraic: true`, `ratsimp` can simplify some expressions containing `%phi`.

  Examples:

  `fibtophi` expresses Fibonacci numbers `fib(n)` in terms of `%phi`.

      (%i1) fibtophi (fib (n));
                                 n             n
                             %phi  - (1 - %phi)
      (%o1)                  -------------------
                                 2 %phi - 1

      (%i2) fib (n-1) + fib (n) - fib (n+1);
      (%o2)          - fib(n + 1) + fib(n) + fib(n - 1)

      (%i3) fibtophi (%);
                  n + 1             n + 1       n             n
              %phi      - (1 - %phi)        %phi  - (1 - %phi)
      (%o3) - --------------------------- + -------------------
                      2 %phi - 1                2 %phi - 1
                                                n - 1             n - 1
                                            %phi      - (1 - %phi)
                                          + ---------------------------
                                                    2 %phi - 1

      (%i4) ratsimp (%);
      (%o4)                           0

  By default, Maxima does not know the algebraic properties of `%phi`. After evaluating `tellrat (%phi^2 - %phi - 1)` and `algebraic: true`, `ratsimp` can simplify some expressions containing `%phi`.

      (%i1) e : expand ((%phi^2 - %phi - 1) * (A + 1));
                       2                      2
      (%o1)        %phi  A - %phi A - A + %phi  - %phi - 1

      (%i2) ratsimp (e);
                        2                     2
      (%o2)        (%phi  - %phi - 1) A + %phi  - %phi - 1

      (%i3) tellrat (%phi^2 - %phi - 1);
                                  2
      (%o3)                  [%phi  - %phi - 1]

      (%i4) algebraic : true;
      (%o4)                         true

      (%i5) ratsimp (e);
      (%o5)                           0

  Categories: Constants ·

Constant: %pi ¶

  `%pi` represents the ratio of the perimeter of a circle to its diameter. The numeric value of `%pi` is the double-precision floating-point value 3.141592653589793d0.

  Categories: Constants ·

Constant: true ¶

  `true` represents the Boolean constant of the same name. Maxima implements `true` by the value `T` in Lisp.

  Categories: Constants ·

Constant: und ¶

  `und` represents an undefined result.

  See also `limit`.

  Example:

      (%i1) limit (x*sin(x), x, inf);
      (%o1)                          und

  Categories: Constants ·

Constant: zeroa ¶

  `zeroa` represents an infinitesimal above zero. `zeroa` can be used in expressions. `limit` simplifies expressions which contain infinitesimals.

  See also `zerob` and `limit`.

  Example:

  `limit` simplifies expressions which contain infinitesimals:

      (%i1) limit(zeroa);
      (%o1)                           0

      (%i2) limit(zeroa+x);
      (%o2)                           x

  Categories: Constants ·

Constant: zerob ¶

  `zerob` represents an infinitesimal below zero. `zerob` can be used in expressions. `limit` simplifies expressions which contain infinitesimals.

  See also `zeroa` and `limit`.

  Categories: Constants ·

### 7.4 Lists ¶

#### 7.4.1 Introduction to Lists ¶

Lists are the basic building block for Maxima and Lisp. All data types other than arrays, `hashed arrays` and numbers are represented as Lisp lists, These Lisp lists have the form

    ((MPLUS) $A 2)

to indicate an expression `a+2`. At Maxima level one would see the infix notation `a+2`. Maxima also has lists which are printed as

    [1, 2, 7, x+y]

for a list with 4 elements. Internally this corresponds to a Lisp list of the form

    ((MLIST) 1 2 7 ((MPLUS) $X $Y))

The flag which denotes the type field of the Maxima expression is a list itself, since after it has been through the simplifier the list would become

    ((MLIST SIMP) 1 2 7 ((MPLUS SIMP) $X $Y))

#### 7.4.2 Functions and Variables for Lists ¶

Operator: [ ¶
Operator: ] ¶

  `[` and `]` mark the beginning and end, respectively, of a list.

  `[` and `]` also enclose the subscripts of a list, array, `hashed array`, or `memoizing function`. Note that other than for arrays accessing the `n`th element of a list may need an amount of time that is roughly proportional to `n`, See Performance considerations for Lists.

  Note that if an element of a subscripted variable is written to before a list or an array of this name is declared a `hashed array` (see Arrays) is created, not a list.

  Examples:

      (%i1) x: [a, b, c];
      (%o1)                       [a, b, c]

      (%i2) x[3];
      (%o2)                           c

      (%i3) array (y, fixnum, 3);
      (%o3)                           y

      (%i4) y[2]: %pi;
      (%o4)                          %pi

      (%i5) y[2];
      (%o5)                          %pi

      (%i6) z['foo]: 'bar;
      (%o6)                          bar

      (%i7) z['foo];
      (%o7)                          bar

      (%i8) g[k] := 1/(k^2+1);
                                        1
      (%o8)                     g  := ------
                                 k     2
                                      k  + 1

      (%i9) g[10];
                                      1
      (%o9)                          ---
                                     101

  Categories: Lists · Operators ·

Function: append (list_1, …, list_n) ¶

  Returns a single list of the elements of list_1 followed by the elements of list_2, … `append` also works on general expressions, e.g. `append (f(a,b), f(c,d,e));` yields `f(a,b,c,d,e)`.

  See also `addrow`, `addcol` and `join`.

  Do `example(append);` for an example.

  Categories: Lists · Expressions ·

Function: assoc
assoc (key, e, default)
assoc (key, e) ¶

  `assoc` searches for key as the first part of an argument of e and returns the second part of the first match, if any.

  key may be any expression. e must be a nonatomic expression, and every argument of e must have exactly two parts. `assoc` returns the second part of the first matching argument of e. Matches are determined by `is(key = first(a))` where a is an argument of e.

  If there are two or more matches, only the first is returned. If there are no matches, default is returned, if specified. Otherwise, `false` is returned.

  See also `sublist` and `member`.

  Examples:

  key may be any expression. e must be a nonatomic expression, and every argument of e must have exactly two parts. `assoc` returns the second part of the first matching argument of e.

      (%i1) assoc (f(x), foo(g(x) = y, f(x) = z + 1, h(x) = 2*u));
      (%o1)                         z + 1

  If there are two or more matches, only the first is returned.

      (%i1) assoc (yy, [xx = 111, yy = 222, yy = 333, yy = 444]);
      (%o1)                          222

  If there are no matches, default is returned, if specified. Otherwise, `false` is returned.

      (%i1) assoc (abc, [[x, 111], [y, 222], [z, 333]], none);
      (%o1)                         none

      (%i2) assoc (abc, [[x, 111], [y, 222], [z, 333]]);
      (%o2)                         false

  Categories: Lists · Expressions ·

Function: cons
cons (expr, list)
cons (expr_1, expr_2) ¶

  `cons (expr, list)` returns a new list constructed of the element expr as its first element, followed by the elements of list. This is analogous to the Lisp language construction operation "cons".

  The Maxima function `cons` can also be used where the second argument is other than a list and this might be useful. In this case, `cons (expr_1, expr_2)` returns an expression with same operator as expr_2 but with argument `cons(expr_1, args(expr_2))`. Examples:

      (%i1) cons(a,[b,c,d]);
      (%o1)                     [a, b, c, d]

      (%i2) cons(a,f(b,c,d));
      (%o2)                     f(a, b, c, d)

  In general, `cons` applied to a nonlist doesn’t make sense. For instance, `cons(a,b^c)` results in an illegal expression, since ’^’ cannot take three arguments.

  When `inflag` is true, `cons` operates on the internal structure of an expression, otherwise `cons` operates on the displayed form. Especially when `inflag` is true, `cons` applied to a nonlist sometimes gives a surprising result; for example

      (%i1) cons(a,-a), inflag : true;
                                       2
      (%o1)                         - a

      (%i2) cons(a,-a), inflag : false;
      (%o2)                           0

  Categories: Lists · Expressions ·

Function: copylist (list) ¶

  Returns a copy of the list list.

  Categories: Lists ·

Function: create_list (form, x_1, list_1, …, x_n, list_n) ¶

  Create a list by evaluating form with x_1 bound to each element of list_1, and for each such binding bind x_2 to each element of list_2, … The number of elements in the result will be the product of the number of elements in each list. Each variable x_i must actually be a symbol – it will not be evaluated. The list arguments will be evaluated once at the beginning of the iteration.

      (%i1) create_list (x^i, i, [1, 3, 7]);
                                      3   7
      (%o1)                      [x, x , x ]

  With a double iteration:

      (%i1) create_list ([i, j], i, [a, b], j, [e, f, h]);
      (%o1)   [[a, e], [a, f], [a, h], [b, e], [b, f], [b, h]]

  Instead of list_i two args may be supplied each of which should evaluate to a number. These will be the inclusive lower and upper bounds for the iteration.

      (%i1) create_list ([i, j], i, [1, 2, 3], j, 1, i);
      (%o1)   [[1, 1], [2, 1], [2, 2], [3, 1], [3, 2], [3, 3]]

  Note that the limits or list for the `j` variable can depend on the current value of `i`.

  Categories: Lists ·

Function: delete
delete (expr_1, expr_2)
delete (expr_1, expr_2, n) ¶

  `delete(expr_1, expr_2)` removes from expr_2 any arguments of its top-level operator which are the same (as determined by "=") as expr_1. Note that "=" tests for formal equality, not equivalence. Note also that arguments of subexpressions are not affected.

  expr_1 may be an atom or a non-atomic expression. expr_2 may be any non-atomic expression. `delete` returns a new expression; it does not modify expr_2.

  `delete(expr_1, expr_2, n)` removes from expr_2 the first n arguments of the top-level operator which are the same as expr_1. If there are fewer than n such arguments, then all such arguments are removed.

  Examples:

  Removing elements from a list.

      (%i1) delete (y, [w, x, y, z, z, y, x, w]);
      (%o1)                  [w, x, z, z, x, w]

  Removing terms from a sum.

      (%i1) delete (sin(x), x + sin(x) + y);
      (%o1)                         y + x

  Removing factors from a product.

      (%i1) delete (u - x, (u - w)*(u - x)*(u - y)*(u - z));
      (%o1)                (u - w) (u - y) (u - z)

  Removing arguments from an arbitrary expression.

      (%i1) delete (a, foo (a, b, c, d, a));
      (%o1)                     foo(b, c, d)

  Limit the number of removed arguments.

      (%i1) delete (a, foo (a, b, a, c, d, a), 2);
      (%o1)                    foo(b, c, d, a)

  Whether arguments are the same as expr_1 is determined by "=". Arguments which are `equal` but not "=" are not removed.

      (%i1) [is (equal (0, 0)), is (equal (0, 0.0)), is (equal (0, 0b0))];
      (%o1)                  [true, true, true]

      (%i2) [is (0 = 0), is (0 = 0.0), is (0 = 0b0)];
      (%o2)                 [true, false, false]

      (%i3) delete (0, [0, 0.0, 0b0]);
      (%o3)                     [0.0, 0.0b0]

      (%i4) is (equal ((x + y)*(x - y), x^2 - y^2));
      (%o4)                         true

      (%i5) is ((x + y)*(x - y) = x^2 - y^2);
      (%o5)                         false

      (%i6) delete ((x + y)*(x - y), [(x + y)*(x - y), x^2 - y^2]);
                                    2    2
      (%o6)                       [x  - y ]

  Categories: Lists · Expressions ·

Function: eighth (expr) ¶

  Returns the 8th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: endcons
endcons (expr, list)
endcons (expr_1, expr_2) ¶

  `endcons (expr, list)` returns a new list constructed of the elements of list followed by expr. The Maxima function `endcons` can also be used where the second argument is other than a list and this might be useful. In this case, `endcons (expr_1, expr_2)` returns an expression with same operator as expr_2 but with argument `endcons(expr_1, args(expr_2))`. Examples:

      (%i1) endcons(a,[b,c,d]);
      (%o1)                     [b, c, d, a]

      (%i2) endcons(a,f(b,c,d));
      (%o2)                     f(b, c, d, a)

  In general, `endcons` applied to a nonlist doesn’t make sense. For instance, `endcons(a,b^c)` results in an illegal expression, since ’^’ cannot take three arguments.

  When `inflag` is true, `endcons` operates on the internal structure of an expression, otherwise `endcons` operates on the displayed form. Especially when `inflag` is true, `endcons` applied to a nonlist sometimes gives a surprising result; for example

      (%i1) endcons(a,-a), inflag : true;
                                       2
      (%o1)                         - a

      (%i2) endcons(a,-a), inflag : false;
      (%o2)                           0

  Categories: Lists · Expressions ·

Function: fifth (expr) ¶

  Returns the 5th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: first (expr) ¶

  Returns the first part of expr which may result in the first element of a list, the first row of a matrix, the first term of a sum, etc.:

      (%i1) matrix([1,2],[3,4]);
                                         [ 1  2 ]
      (%o1)                              [      ]
                                         [ 3  4 ]
      (%i2) first(%);
      (%o2)                              [1,2]
      (%i3) first(%);
      (%o3)                              1
      (%i4) first(a*b/c+d+e/x);
                                         a b
      (%o4)                              ---
                                          c
      (%i5) first(a=b/c+d+e/x);
      (%o5)                              a

  Note that `first` and its related functions, `rest` and `last`, work on the form of expr which is displayed not the form which is typed on input. If the variable `inflag` is set to `true` however, these functions will look at the internal form of expr. One reason why this may make a difference is that the simplifier re-orders expressions:

      (%i1) x+y;
      (%o1)                              y+1
      (%i2) first(x+y),inflag : true;
      (%o2)                              x
      (%i3) first(x+y),inflag : false;
      (%o3)                              y

  The functions `second` … `tenth` yield the second through the tenth part of their input argument.

  See also `firstn` and `part`.

  Categories: Lists · Expressions ·

Function: firstn (expr, count) ¶

  Returns the first count arguments of expr, if expr has at least count arguments. Returns expr if expr has less than count arguments.

  expr may be any nonatomic expression. When expr is something other than a list, `firstn` returns an expression which has the same operator as expr. count must be a nonnegative integer.

  `firstn` honors the global flag `inflag`, which governs whether the internal form of an expression is processed (when `inflag` is true) or the displayed form (when `inflag` is false).

  Note that `firstn(expr, 1)`, which returns a nonatomic expression containing the first argument, is not the same as `first(expr)`, which returns the first argument by itself.

  See also `lastn` and `rest`.

  Examples:

  `firstn` returns the first count elements of expr, if expr has at least count elements.

      (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
      (%o1)        [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

      (%i2) firstn (mylist, 0);
      (%o2)                          []

      (%i3) firstn (mylist, 1);
      (%o3)                          [1]

      (%i4) firstn (mylist, 2);
      (%o4)                        [1, a]

      (%i5) firstn (mylist, 7);
      (%o5)               [1, a, 2, b, 3, x, 4 - y]

  `firstn` returns expr if expr has less than count elements.

      (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
      (%o1)        [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

      (%i2) firstn (mylist, 100);
      (%o2)        [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

  expr may be any nonatomic expression.

      (%i1) myfoo : foo(1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
      (%o1)      foo(1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))

      (%i2) firstn (myfoo, 4);
      (%o2)                    foo(1, a, 2, b)

      (%i3) mybar : bar[m, n](1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
      (%o3)    bar    (1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))
                  m, n

      (%i4) firstn (mybar, 4);
      (%o4)                  bar    (1, a, 2, b)
                                m, n

      (%i5) mymatrix : genmatrix (lambda ([i, j], 10*i + j), 10, 4) $

      (%i6) firstn (mymatrix, 3);
                             [ 11  12  13  14 ]
                             [                ]
      (%o6)                  [ 21  22  23  24 ]
                             [                ]
                             [ 31  32  33  34 ]

  `firstn` honors the global flag `inflag`.

      (%i1) myexpr : a + b + c + d + e;
      (%o1)                   e + d + c + b + a

      (%i2) firstn (myexpr, 3), inflag=true;
      (%o2)                       c + b + a

      (%i3) firstn (myexpr, 3), inflag=false;
      (%o3)                       e + d + c

  Note that `firstn(expr, 1)` is not the same as `first(expr)`.

      (%i1) firstn ([w, x, y, z], 1);
      (%o1)                          [w]

      (%i2) first ([w, x, y, z]);
      (%o2)                           w

  Categories: Lists · Expressions ·

Function: fourth (expr) ¶

  Returns the 4th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: join (l, m) ¶

  Creates a new list containing the elements of lists l and m, interspersed. The result has elements `[l[1], m[1], l[2], m[2], ...]`. The lists l and m may contain any type of elements.

  If the lists are different lengths, `join` ignores elements of the longer list.

  Maxima complains if l or m is not a list.

  See also `append`.

  Examples:

      (%i1) L1: [a, sin(b), c!, d - 1];
      (%o1)                [a, sin(b), c!, d - 1]

      (%i2) join (L1, [1, 2, 3, 4]);
      (%o2)          [a, 1, sin(b), 2, c!, 3, d - 1, 4]

      (%i3) join (L1, [aa, bb, cc, dd, ee, ff]);
      (%o3)        [a, aa, sin(b), bb, c!, cc, d - 1, dd]

  Categories: Lists ·

Function: last (expr) ¶

  Returns the last part (term, row, element, etc.) of the expr.

  See also `lastn`.

  Categories: Lists · Expressions ·

Function: lastn (expr, count) ¶

  Returns the last count arguments of expr, if expr has at least count arguments. Returns expr if expr has less than count arguments.

  expr may be any nonatomic expression. When expr is something other than a list, `lastn` returns an expression which has the same operator as expr. count must be a nonnegative integer.

  `lastn` honors the global flag `inflag`, which governs whether the internal form of an expression is processed (when `inflag` is true) or the displayed form (when `inflag` is false).

  Note that `lastn(expr, 1)`, which returns a nonatomic expression containing the last argument, is not the same as `last(expr)`, which returns the last argument by itself.

  See also `firstn` and `rest`.

  Examples:

  `lastn` returns the last count elements of expr, if expr has at least count elements.

      (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
      (%o1)        [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

      (%i2) lastn (mylist, 0);
      (%o2)                          []

      (%i3) lastn (mylist, 1);
      (%o3)                    [2 z + sin(u)]

      (%i4) lastn (mylist, 2);
      (%o4)                 [4 - y, 2 z + sin(u)]

      (%i5) lastn (mylist, 7);
      (%o5)         [a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

  `lastn` returns expr if expr has less than count elements.

      (%i1) mylist : [1, a, 2, b, 3, x, 4 - y, 2*z + sin(u)];
      (%o1)        [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

      (%i2) lastn (mylist, 100);
      (%o2)        [1, a, 2, b, 3, x, 4 - y, 2 z + sin(u)]

  expr may be any nonatomic expression.

      (%i1) myfoo : foo(1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
      (%o1)      foo(1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))

      (%i2) lastn (myfoo, 4);
      (%o2)            foo(3, x, 4 - y, 2 z + sin(u))

      (%i3) mybar : bar[m, n](1, a, 2, b, 3, x, 4 - y, 2*z + sin(u));
      (%o3)    bar    (1, a, 2, b, 3, x, 4 - y, 2 z + sin(u))
                  m, n

      (%i4) lastn (mybar, 4);
      (%o4)          bar    (3, x, 4 - y, 2 z + sin(u))
                        m, n

      (%i5) mymatrix : genmatrix (lambda ([i, j], 10*i + j), 10, 4) $

      (%i6) lastn (mymatrix, 3);
                           [ 81   82   83   84  ]
                           [                    ]
      (%o6)                [ 91   92   93   94  ]
                           [                    ]
                           [ 101  102  103  104 ]

  `lastn` honors the global flag `inflag`.

      (%i1) myexpr : a + b + c + d + e;
      (%o1)                   e + d + c + b + a

      (%i2) lastn (myexpr, 3), inflag=true;
      (%o2)                       e + d + c

      (%i3) lastn (myexpr, 3), inflag=false;
      (%o3)                       c + b + a

  Note that `lastn(expr, 1)` is not the same as `last(expr)`.

      (%i1) lastn ([w, x, y, z], 1);
      (%o1)                          [z]

      (%i2) last ([w, x, y, z]);
      (%o2)                           z

  Categories: Lists · Expressions ·

Function: length (expr) ¶

  Returns (by default) the number of parts in the external (displayed) form of expr. For lists this is the number of elements, for matrices it is the number of rows, and for sums it is the number of terms (see `dispform`).

  The `length` command is affected by the `inflag` switch. So, e.g. `length(a/(b*c));` gives 2 if `inflag` is `false` (Assuming `exptdispflag` is `true`), but 3 if `inflag` is `true` (the internal representation is essentially `a*b^-1*c^-1`).

  Determining a list’s length typically needs an amount of time proportional to the number of elements in the list. If the length of a list is used inside a loop it therefore might drastically increase the performance if the length is calculated outside the loop instead.

  Categories: Lists · Expressions ·

Option variable: listarith ¶

  Default value: `true`

  If `false` causes any arithmetic operations with lists to be suppressed; when `true`, list-matrix operations are contagious causing lists to be converted to matrices yielding a result which is always a matrix. However, list-list operations should return lists.

  Categories: Lists · Global flags ·

Function: listp (expr) ¶

  Returns `true` if expr is a list else `false`.

  Categories: Lists · Predicate functions ·

Function: lreduce
lreduce (F, s)
lreduce (F, s, s_0) ¶

  Extends the binary function F to an n-ary function by composition, where s is a list.

  `lreduce(F, s)` returns `F(... F(F(s_1, s_2), s_3), ... s_n)`. When the optional argument s_0 is present, the result is equivalent to `lreduce(F, cons(s_0, s))`.

  The function F is first applied to the leftmost list elements, thus the name "lreduce".

  See also `rreduce`, `xreduce`, and `tree_reduce`.

  Examples:

  `lreduce` without the optional argument.

      (%i1) lreduce (f, [1, 2, 3]);
      (%o1)                     f(f(1, 2), 3)

      (%i2) lreduce (f, [1, 2, 3, 4]);
      (%o2)                  f(f(f(1, 2), 3), 4)

  `lreduce` with the optional argument.

      (%i1) lreduce (f, [1, 2, 3], 4);
      (%o1)                  f(f(f(4, 1), 2), 3)

  `lreduce` applied to built-in binary operators. `/` is the division operator.

      (%i1) lreduce ("^", [a, b, c, d]);
                                     b c d
      (%o1)                       ((a ) )

      (%i2) lreduce ("/", [a, b, c, d]);
                                      a
      (%o2)                         -----
                                    b c d

  Categories: Lists ·

Function: makelist
makelist ()
makelist (expr)
makelist (expr, n)
makelist (expr, i, i_max)
makelist (expr, i, i_0, i_max)
makelist (expr, i, i_0, i_max, step)
makelist (expr, x, list) ¶

  The first form, `makelist ()`, creates an empty list. The second form, `makelist (expr)`, creates a list with expr as its single element. `makelist (expr, n)` creates a list of n elements generated from expr.

  The most general form, `makelist (expr, i, i_0, i_max, step)`, returns the list of elements obtained when `ev (expr, i=j)` is applied to the elements j of the sequence: i_0, i_0 + step, i_0 + 2*step, ..., with |j| less than or equal to |i_max|.

  The increment step can be a number (positive or negative) or an expression. If it is omitted, the default value 1 will be used. If both i_0 and step are omitted, they will both have a default value of 1.

  `makelist (expr, x, list)` returns a list, the `j`th element of which is equal to `ev (expr, x=list[j])` for `j` equal to 1 through `length (list)`.

  Examples:

      (%i1) makelist (concat (x,i), i, 6);
      (%o1)               [x1, x2, x3, x4, x5, x6]

      (%i2) makelist (x=y, y, [a, b, c]);
      (%o2)                 [x = a, x = b, x = c]

      (%i3) makelist (x^2, x, 3, 2*%pi, 2);
      (%o3)                        [9, 25]

      (%i4) makelist (random(6), 4);
      (%o4)                     [2, 0, 2, 5]

      (%i5) flatten (makelist (makelist (i^2, 3), i, 4));
      (%o5)        [1, 1, 1, 4, 4, 4, 9, 9, 9, 16, 16, 16]

      (%i6) flatten (makelist (makelist (i^2, i, 3), 4));
      (%o6)         [1, 4, 9, 1, 4, 9, 1, 4, 9, 1, 4, 9]

  Categories: Lists ·

Function: member (expr_1, expr_2) ¶

  Returns `true` if `is(expr_1 = a)` for some element a in `args(expr_2)`, otherwise returns `false`.

  `expr_2` is typically a list, in which case `args(expr_2) = expr_2` and `is(expr_1 = a)` for some element a in `expr_2` is the test.

  `member` does not inspect parts of the arguments of `expr_2`, so it may return `false` even if `expr_1` is a part of some argument of `expr_2`.

  See also `elementp`.

  Examples:

      (%i1) member (8, [8, 8.0, 8b0]);
      (%o1)                         true

      (%i2) member (8, [8.0, 8b0]);
      (%o2)                         false

      (%i3) member (b, [a, b, c]);
      (%o3)                         true

      (%i4) member (b, [[a, b], [b, c]]);
      (%o4)                         false

      (%i5) member ([b, c], [[a, b], [b, c]]);
      (%o5)                         true

      (%i6) F (1, 1/2, 1/4, 1/8);
                                     1  1  1
      (%o6)                     F(1, -, -, -)
                                     2  4  8

      (%i7) member (1/8, %);
      (%o7)                         true

      (%i8) member ("ab", ["aa", "ab", sin(1), a + b]);
      (%o8)                         true

  Categories: Lists · Expressions · Predicate functions ·

Function: ninth (expr) ¶

  Returns the 9th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: pop (list) ¶

  `pop` removes and returns the first element from the list list. The argument list must be a mapatom that is bound to a nonempty list. If the argument list is not bound to a nonempty list, Maxima signals an error. For examples, see `push`.

  Categories: Lists · Expressions ·

Function: push (item, list) ¶

  `push` prepends the item item to the list list and returns a copy of the new list. The second argument list must be a mapatom that is bound to a list. The first argument item can be any Maxima symbol or expression. If the argument list is not bound to a list, Maxima signals an error.

  To remove the first item from a list, see `pop`.

  Examples:

      (%i1) ll: [];
      (%o1)                          []

      (%i2) push (x, ll);
      (%o2)                          [x]

      (%i3) push (x^2+y, ll);
                                       2
      (%o3)                      [y + x , x]

      (%i4) a: push ("string", ll);
                                           2
      (%o4)                  [string, y + x , x]

      (%i5) pop (ll);
      (%o5)                        string

      (%i6) pop (ll);
                                        2
      (%o6)                        y + x

      (%i7) pop (ll);
      (%o7)                           x

      (%i8) ll;
      (%o8)                          []

      (%i9) a;
                                           2
      (%o9)                  [string, y + x , x]

  Categories: Lists · Expressions ·

Function: rest
rest (expr, n)
rest (expr) ¶

  Returns expr with its first n elements removed if n is positive and its last `- n` elements removed if n is negative. If n is 1 it may be omitted. The first argument expr may be a list, matrix, or other expression. When expr is an atom, `rest` signals an error; when expr is an empty list and `partswitch` is false, `rest` signals an error. When expr is an empty list and `partswitch` is true, `rest` returns `end`.

  Applying `rest` to expression such as `f(a,b,c)` returns `f(b,c)`. In general, applying `rest` to a nonlist doesn’t make sense. For example, because ’^’ requires two arguments, `rest(a^b)` results in an error message. The functions `args` and `op` may be useful as well, since `args(a^b)` returns `[a,b]` and `op(a^b)` returns ^.

  See also `firstn` and `lastn`.

      (%i1) rest(a+b+c);
      (%o1) b+a
      (%i2) rest(a+b+c,2);
      (%o2) a
      (%i3) rest(a+b+c,-2);
      (%o3) c

  Categories: Lists · Expressions ·

Function: reverse (list) ¶

  Reverses the order of the members of the list (not the members themselves). `reverse` also works on general expressions, e.g. `reverse(a=b);` gives `b=a`.

  See also `sreverse`.

  Categories: Lists · Expressions ·

Function: rreduce
rreduce (F, s)
rreduce (F, s, s_{n + 1}) ¶

  Extends the binary function F to an n-ary function by composition, where s is a list.

  `rreduce(F, s)` returns `F(s_1, ... F(s_{n - 2}, F(s_{n - 1}, s_n)))`. When the optional argument s_{n + 1} is present, the result is equivalent to `rreduce(F, endcons(s_{n + 1}, s))`.

  The function F is first applied to the rightmost list elements, thus the name "rreduce".

  See also `lreduce`, `tree_reduce`, and `xreduce`.

  Examples:

  `rreduce` without the optional argument.

      (%i1) rreduce (f, [1, 2, 3]);
      (%o1)                     f(1, f(2, 3))

      (%i2) rreduce (f, [1, 2, 3, 4]);
      (%o2)                  f(1, f(2, f(3, 4)))

  `rreduce` with the optional argument.

      (%i1) rreduce (f, [1, 2, 3], 4);
      (%o1)                  f(1, f(2, f(3, 4)))

  `rreduce` applied to built-in binary operators. `/` is the division operator.

      (%i1) rreduce ("^", [a, b, c, d]);
                                       d
                                      c
                                     b
      (%o1)                         a

      (%i2) rreduce ("/", [a, b, c, d]);
                                     a c
      (%o2)                          ---
                                     b d

  Categories: Lists ·

Function: second (expr) ¶

  Returns the 2nd item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: seventh (expr) ¶

  Returns the 7th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: sixth (expr) ¶

  Returns the 6th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: sort
sort (L, P)
sort (L) ¶

  `sort(L, P)` sorts a list L according to a predicate `P` of two arguments which defines a strict weak order on the elements of L. If `P(a, b)` is `true`, then `a` appears before `b` in the result. If neither `P(a, b)` nor `P(b, a)` are `true`, then `a` and `b` are equivalent, and appear in the result in the same order as in the input. That is, `sort` is a stable sort.

  If `P(a, b)` and `P(b, a)` are both `true` for some elements of L, then P is not a valid sort predicate, and the result is undefined. If `P(a, b)` is something other than `true` or `false`, `sort` signals an error.

  The predicate may be specified as the name of a function or binary infix operator, or as a `lambda` expression. If specified as the name of an operator, the name must be enclosed in double quotes.

  The sorted list is returned as a new object; the argument L is not modified.

  `sort(L)` is equivalent to `sort(L, orderlessp)`.

  The default sorting order is ascending, as determined by `orderlessp`. The predicate `ordergreatp` sorts a list in descending order.

  All Maxima atoms and expressions are comparable under `orderlessp` and `ordergreatp`.

  Operators `<` and `>` order numbers, constants, and constant expressions by magnitude. Note that `orderlessp` and `ordergreatp` do not order numbers, constants, and constant expressions by magnitude.

  `ordermagnitudep` orders numbers, constants, and constant expressions the same as `<`, and all other elements the same as `orderlessp`.

  Examples:

  `sort` sorts a list according to a predicate of two arguments which defines a strict weak order on the elements of the list.

      (%i1) sort ([1, a, b, 2, 3, c], 'orderlessp);
      (%o1)                  [1, 2, 3, a, b, c]

      (%i2) sort ([1, a, b, 2, 3, c], 'ordergreatp);
      (%o2)                  [c, b, a, 3, 2, 1]

  The predicate may be specified as the name of a function or binary infix operator, or as a `lambda` expression. If specified as the name of an operator, the name must be enclosed in double quotes.

      (%i1) L : [[1, x], [3, y], [4, w], [2, z]];
      (%o1)           [[1, x], [3, y], [4, w], [2, z]]

      (%i2) foo (a, b) := a[1] > b[1];
      (%o2)                 foo(a, b) := a  > b
                                          1    1

      (%i3) sort (L, 'foo);
      (%o3)           [[4, w], [3, y], [2, z], [1, x]]

      (%i4) infix (">>");
      (%o4)                          >>

      (%i5) a >> b := a[1] > b[1];
      (%o5)                  (a >> b) := a  > b
                                          1    1

      (%i6) sort (L, ">>");
      (%o6)           [[4, w], [3, y], [2, z], [1, x]]

      (%i7) sort (L, lambda ([a, b], a[1] > b[1]));
      (%o7)           [[4, w], [3, y], [2, z], [1, x]]

  `sort(L)` is equivalent to `sort(L, orderlessp)`.

      (%i1) L : [a, 2*b, -5, 7, 1 + %e, %pi];
      (%o1)             [a, 2 b, - 5, 7, %e + 1, %pi]

      (%i2) sort (L);
      (%o2)             [- 5, 7, %e + 1, %pi, a, 2 b]

      (%i3) sort (L, 'orderlessp);
      (%o3)             [- 5, 7, %e + 1, %pi, a, 2 b]

  The default sorting order is ascending, as determined by `orderlessp`. The predicate `ordergreatp` sorts a list in descending order.

      (%i1) L : [a, 2*b, -5, 7, 1 + %e, %pi];
      (%o1)             [a, 2 b, - 5, 7, %e + 1, %pi]

      (%i2) sort (L);
      (%o2)             [- 5, 7, %e + 1, %pi, a, 2 b]

      (%i3) sort (L, 'ordergreatp);
      (%o3)             [2 b, a, %pi, %e + 1, 7, - 5]

  All Maxima atoms and expressions are comparable under `orderlessp` and `ordergreatp`.

      (%i1) L : [11, -17, 29b0, 9*c, 7.55, foo(x, y), -5/2, b + a];
                                                       5
      (%o1)  [11, - 17, 2.9b1, 9 c, 7.55, foo(x, y), - -, b + a]
                                                       2

      (%i2) sort (L, orderlessp);
                      5
      (%o2)  [- 17, - -, 7.55, 11, 2.9b1, b + a, 9 c, foo(x, y)]
                      2

      (%i3) sort (L, ordergreatp);
                                                        5
      (%o3)  [foo(x, y), 9 c, b + a, 2.9b1, 11, 7.55, - -, - 17]
                                                        2

  Operators `<` and `>` order numbers, constants, and constant expressions by magnitude. Note that `orderlessp` and `ordergreatp` do not order numbers, constants, and constant expressions by magnitude.

      (%i1) L : [%pi, 3, 4, %e, %gamma];
      (%o1)                [%pi, 3, 4, %e, %gamma]

      (%i2) sort (L, ">");
      (%o2)                [4, %pi, 3, %e, %gamma]

      (%i3) sort (L, ordergreatp);
      (%o3)                [%pi, %gamma, %e, 4, 3]

  `ordermagnitudep` orders numbers, constants, and constant expressions the same as `<`, and all other elements the same as `orderlessp`.

      (%i1) L: [%i, 1+%i, 2*x, minf, inf, %e, sin(1), 0, 1,2,3, 1.0, 1.0b0];
      (%o1) [%i, %i + 1, 2 x, minf, inf, %e, sin(1), 0, 1, 2, 3, 1.0,
                                                                 1.0b0]

      (%i2) sort (L, ordermagnitudep);
      (%o2) [minf, 0, sin(1), 1, 1.0, 1.0b0, 2, %e, 3, inf, %i,
                                                           %i + 1, 2 x]

      (%i3) sort (L, orderlessp);
      (%o3) [0, 1, 1.0, 2, 3, sin(1), 1.0b0, %e, %i, %i + 1, inf,
                                                             minf, 2 x]

  Categories: Lists ·

Function: sublist (list, p) ¶

  Returns the list of elements of list for which the predicate `p` returns `true`.

  Example:

      (%i1) L: [1, 2, 3, 4, 5, 6];
      (%o1)                  [1, 2, 3, 4, 5, 6]

      (%i2) sublist (L, evenp);
      (%o2)                       [2, 4, 6]

  Categories: Lists ·

Function: sublist_indices (L, P) ¶

  Returns the indices of the elements `x` of the list L for which the predicate `maybe(P(x))` returns `true`; this excludes `unknown` as well as `false`. P may be the name of a function or a lambda expression. L must be a literal list.

  Examples:

      (%i1) sublist_indices ('[a, b, b, c, 1, 2, b, 3, b],
                             lambda ([x], x='b));
      (%o1)                     [2, 3, 7, 9]

      (%i2) sublist_indices ('[a, b, b, c, 1, 2, b, 3, b], symbolp);
      (%o2)                  [1, 2, 3, 4, 7, 9]

      (%i3) sublist_indices ([1 > 0, 1 < 0, 2 < 1, 2 > 1, 2 > 0],
                             identity);
      (%o3)                       [1, 4, 5]

      (%i4) assume (x < -1);
      (%o4)                       [x < - 1]

      (%i5) map (maybe, [x > 0, x < 0, x < -2]);
      (%o5)                [false, true, unknown]

      (%i6) sublist_indices ([x > 0, x < 0, x < -2], identity);
      (%o6)                          [2]

  Categories: Lists ·

Function: tenth (expr) ¶

  Returns the 10th item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: third (expr) ¶

  Returns the 3rd item of expression or list expr. See `first` for more details.

  Categories: Lists · Expressions ·

Function: tree_reduce
tree_reduce (F, s)
tree_reduce (F, s, s_0) ¶

  Extends the binary function F to an n-ary function by composition, where s is a set or list.

  `tree_reduce` is equivalent to the following: Apply F to successive pairs of elements to form a new list `[F(s_1, s_2), F(s_3, s_4), ...]`, carrying the final element unchanged if there are an odd number of elements. Then repeat until the list is reduced to a single element, which is the return value.

  When the optional argument s_0 is present, the result is equivalent `tree_reduce(F, cons(s_0, s))`.

  For addition of floating point numbers, `tree_reduce` may return a sum that has a smaller rounding error than either `rreduce` or `lreduce`.

  The elements of s and the partial results may be arranged in a minimum-depth binary tree, thus the name "tree_reduce".

  Examples:

  `tree_reduce` applied to a list with an even number of elements.

      (%i1) tree_reduce (f, [a, b, c, d]);
      (%o1)                  f(f(a, b), f(c, d))

  `tree_reduce` applied to a list with an odd number of elements.

      (%i1) tree_reduce (f, [a, b, c, d, e]);
      (%o1)               f(f(f(a, b), f(c, d)), e)

  Categories: Sets · Lists ·

Function: unique (L) ¶

  Returns the unique elements of the list L.

  When all the elements of L are unique, `unique` returns a shallow copy of L, not L itself.

  If L is not a list, `unique` returns L.

  Example:

      (%i1) unique ([1, %pi, a + b, 2, 1, %e, %pi, a + b, [1]]);
      (%o1)              [1, 2, %e, %pi, [1], b + a]

Function: xreduce
xreduce (F, s)
xreduce (F, s, s_0) ¶

  Extends the function F to an n-ary function by composition, or, if F is already n-ary, applies F to s. When F is not n-ary, `xreduce` is the same as `lreduce`. The argument s is a list.

  Functions known to be n-ary include addition `+`, multiplication `*`, `and`, `or`, `max`, `min`, and `append`. Functions may also be declared n-ary by `declare(F, nary)`. For these functions, `xreduce` is expected to be faster than either `rreduce` or `lreduce`.

  When the optional argument s_0 is present, the result is equivalent to `xreduce(s, cons(s_0, s))`.

  Floating point addition is not exactly associative; be that as it may, `xreduce` applies Maxima’s n-ary addition when s contains floating point numbers.

  Examples:

  `xreduce` applied to a function known to be n-ary. `F` is called once, with all arguments.

      (%i1) declare (F, nary);
      (%o1)                         done

      (%i2) F ([L]) := L;
      (%o2)                      F([L]) := L

      (%i3) xreduce (F, [a, b, c, d, e]);
      (%o3)                    [a, b, c, d, e]

  `xreduce` applied to a function not known to be n-ary. `G` is called several times, with two arguments each time.

      (%i1) G ([L]) := L;
      (%o1)                      G([L]) := L

      (%i2) xreduce (G, [a, b, c, d, e]);
      (%o2)                 [[[[a, b], c], d], e]

      (%i3) lreduce (G, [a, b, c, d, e]);
      (%o3)                 [[[[a, b], c], d], e]

  Categories: Sets · Lists ·

#### 7.4.3 Performance considerations for Lists ¶

Lists provide efficient ways of appending and removing elements. They can be created without knowing their final dimensions. Lisp provides efficient means of copying and handling lists. Also nested lists do not need to be strictly rectangular. These advantages over declared arrays come with the drawback that the amount of time needed for accessing a random element within a list may be roughly proportional to the element’s distance from its beginning. Efficient traversal of lists is still possible, though, by using the list as a stack or a fifo:

    (%i1) l:[Test,1,2,3,4];
    (%o1)                  [Test, 1, 2, 3, 4]

    (%i2) while l # [] do
       disp(pop(l));
                                  Test

                                    1

                                    2

                                    3

                                    4

    (%o2)                         done

Another even faster example would be:

    (%i1) l:[Test,1,2,3,4];
    (%o1)                  [Test, 1, 2, 3, 4]

    (%i2) for i in l do
       disp(pop(l));
                                  Test

                                    1

                                    2

                                    3

                                    4

    (%o2)                         done

Beginning traversal with the last element of a list is possible after reversing the list using `reverse ()`. If the elements of a long list need to be processed in a different order performance might be increased by converting the list into a declared array first.

Note also that the ending condition of `for` loops is tested for every iteration which means that the result of a `length` should be cached if it is used in the ending condition:

    (%i1) l:makelist(i,i,1,100000)$

    (%i2) lngth:length(l);
    (%o2)                        100000

    (%i3) x:1;
    (%o3)                           1

    (%i4) for i:1 thru lngth do
        x:x+1$

    (%i5) x;
    (%o5)                        100001

### 7.5 Arrays ¶

Maxima supports 3 array-like constructs:

- If one tries to write to an indexed variable without creating a list first an undeclared array (also named hashed array) is created that grows dynamically and allows numbers, symbols and strings as indices:

      (%i1) a["feww"]:1;
      (%o1)                           1

      (%i2) a[qqwdqwd]:3;
      (%o2)                           3

      (%i3) a[5]:99;
      (%o3)                          99

      (%i4) a[qqwdqwd];
      (%o4)                           3

      (%i5) a[5];
      (%o5)                          99

      (%i6) a["feww"];
      (%o6)                           1

  Since lisp handles hashed arrays and `memoizing functions` similar to arrays many of the functions that can be applied to arrays can be applied to them, as well.

- Lists (see `makelist` allow for fast addition and removal of elements, can be created without knowing their final size.
- Declared arrays that allow fast access to random elements at the cost that their size needs to be known at construction time. (See Performance considerations for Lists.)

#### 7.5.1 Functions and Variables for Arrays ¶

Function: array
array (name, dim_1, …, dim_n)
array (name, type, dim_1, …, dim_n)
array ([name_1, …, name_m], dim_1, …, dim_n) ¶

  Creates an \(n\)-dimensional array. \(n\) may be less than or equal to 5. The subscripts for the \(i\)’th dimension are the integers running from 0 to dim_i.

  `array (name, dim_1, ..., dim_n)` creates a general array.

  `array (name, type, dim_1, ..., dim_n)` creates an array, with elements of a specified type. type can be `fixnum` for integers of limited size or `flonum` for floating-point numbers.

  `array ([name_1, ..., name_m], dim_1, ..., dim_n)` creates \(m\) arrays, all of the same dimensions.

  See also `arraymake`, `arrayinfo` and `make_array`.

  Categories: Arrays ·

Function: arrayapply (A, [i_1, …, i_n]) ¶

  Evaluates `A [i_1, ..., i_n]`, where A is an array and i_1, …, i_n are integers.

  This is reminiscent of `apply`, except the first argument is an array instead of a function.

  Categories: Expressions · Arrays ·

Function: arrayinfo (A) ¶

  Returns information about the array A. The argument A may be a declared array, a `hashed array`, a `memoizing function`, or a subscripted function.

  For declared arrays, `arrayinfo` returns a list comprising the atom `declared`, the number of dimensions, and the size of each dimension. The elements of the array, both bound and unbound, are returned by `listarray`.

  For undeclared arrays (hashed arrays), `arrayinfo` returns a list comprising the atom `hashed`, the number of subscripts, and the subscripts of every element which has a value. The values are returned by `listarray`.

  For `memoizing functions`, `arrayinfo` returns a list comprising the atom `hashed`, the number of subscripts, and any subscript values for which there are stored function values. The stored function values are returned by `listarray`.

  For subscripted functions, `arrayinfo` returns a list comprising the atom `hashed`, the number of subscripts, and any subscript values for which there are lambda expressions. The lambda expressions are returned by `listarray`.

  See also `listarray`.

  Examples:

  `arrayinfo` and `listarray` applied to a declared array.

      (%i1) array (aa, 2, 3);
      (%o1)                          aa

      (%i2) aa [2, 3] : %pi;
      (%o2)                          %pi

      (%i3) aa [1, 2] : %e;
      (%o3)                          %e

      (%i4) arrayinfo (aa);
      (%o4)                 [declared, 2, [2, 3]]

      (%i5) listarray (aa);
      (%o5) [#####, #####, #####, #####, #####, #####, %e, #####,
                                              #####, #####, #####, %pi]

  `arrayinfo` and `listarray` applied to an undeclared array (`hashed array`.).

      (%i1) bb [FOO] : (a + b)^2;
                                         2
      (%o1)                       (b + a)

      (%i2) bb [BAR] : (c - d)^3;
                                         3
      (%o2)                       (c - d)

      (%i3) arrayinfo (bb);
      (%o3)               [hashed, 1, [BAR], [FOO]]

      (%i4) listarray (bb);
                                    3         2
      (%o4)                 [(c - d) , (b + a) ]

  `arrayinfo` and `listarray` applied to a `memoizing function`.

      (%i1) cc [x, y] := y / x;
                                           y
      (%o1)                      cc     := -
                                   x, y    x

      (%i2) cc [u, v];
                                      v
      (%o2)                           -
                                      u

      (%i3) cc [4, z];
                                      z
      (%o3)                           -
                                      4

      (%i4) arrayinfo (cc);
      (%o4)              [hashed, 2, [4, z], [u, v]]

      (%i5) listarray (cc);
                                    z  v
      (%o5)                        [-, -]
                                    4  u

  Using `arrayinfo` in order to convert an undeclared array to a declared array:

      (%i1) for i:0 thru 10 do a[i]:i^2$

      (%i2) indices:map(first,rest(rest(arrayinfo(a))));
      (%o2)          [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

      (%i3) array(A,fixnum,length(indices)-1)$
      (%i4) fillarray(A,map(lambda([x],a[x]),indices))$

      (%i5) listarray(A);
      (%o5)       [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

  `arrayinfo` and `listarray` applied to a subscripted function.

      (%i1) dd [x] (y) := y ^ x;
                                           x
      (%o1)                     dd (y) := y
                                  x

      (%i2) dd [a + b];
                                          b + a
      (%o2)                  lambda([y], y     )

      (%i3) dd [v - u];
                                          v - u
      (%o3)                  lambda([y], y     )

      (%i4) arrayinfo (dd);
      (%o4)             [hashed, 1, [b + a], [v - u]]

      (%i5) listarray (dd);
                               b + a                v - u
      (%o5)      [lambda([y], y     ), lambda([y], y     )]

  Categories: Arrays ·

Function: arraymake (A, [i_1, …, i_n]) ¶

  Returns the expression `A[i_1, ..., i_n]`. The result is an unevaluated array reference.

  `arraymake` is reminiscent of `funmake`, except the return value is an unevaluated array reference instead of an unevaluated function call.

  Examples:

      (%i1) arraymake (A, [1]);
      (%o1)                          A
                                      1

      (%i2) arraymake (A, [k]);
      (%o2)                          A
                                      k

      (%i3) arraymake (A, [i, j, 3]);
      (%o3)                       A
                                   i, j, 3

      (%i4) array (A, fixnum, 10);
      (%o4)                           A

      (%i5) fillarray (A, makelist (i^2, i, 1, 11));
      (%o5)                           A

      (%i6) arraymake (A, [5]);
      (%o6)                          A
                                      5

      (%i7) ''%;
      (%o7)                          36

      (%i8) L : [a, b, c, d, e];
      (%o8)                    [a, b, c, d, e]

      (%i9) arraymake ('L, [n]);
      (%o9)                          L
                                      n

      (%i10) ''%, n = 3;
      (%o10)                          c

      (%i11) A2 : make_array (fixnum, 10);
      (%o11)        {Lisp Array: #(0 0 0 0 0 0 0 0 0 0)}

      (%i12) fillarray (A2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
      (%o12)        {Lisp Array: #(1 2 3 4 5 6 7 8 9 10)}

      (%i13) arraymake ('A2, [8]);
      (%o13)                         A2
                                       8

      (%i14) ''%;
      (%o14)                          9

  Categories: Expressions · Arrays ·

System variable: arrays ¶

  Default value: `[]`

  `arrays` is a list of arrays that have been allocated. These comprise arrays declared by `array`, `hashed arrays` that can be constructed by implicit definition (assigning something to an element that isn’t yet declared as a list or an array), and `memoizing functions` defined by `:=` and `define`. Arrays defined by `make_array` are not included.

  See also `array`, `arrayapply`, `arrayinfo`, `arraymake`, `fillarray`, `listarray`, and `rearray`.

  Examples:

      (%i1) array (aa, 5, 7);
      (%o1)                          aa

      (%i2) bb [FOO] : (a + b)^2;
                                         2
      (%o2)                       (b + a)

      (%i3) cc [x] := x/100;
                                         x
      (%o3)                      cc  := ---
                                   x    100

      (%i4) dd : make_array ('any, 7);
      (%o4)     {Lisp Array: #(NIL NIL NIL NIL NIL NIL NIL)}

      (%i5) arrays;
      (%o5)                     [aa, bb, cc]

  Categories: Arrays · Global variables ·

Function: arraysetapply (A, [i_1, …, i_n], x) ¶

  Assigns x to `A[i_1, ..., i_n]`, where A is an array and i_1, …, i_n are integers.

  `arraysetapply` evaluates its arguments.

  Categories: Expressions · Arrays ·

Function: fillarray (A, B) ¶

  Fills array A from B, which is a list or an array.

  If a specific type was declared for A when it was created, it can only be filled with elements of that same type; it is an error if an attempt is made to copy an element of a different type.

  If the dimensions of the arrays A and B are different, A is filled in row-major order. If there are not enough elements in B the last element is used to fill out the rest of A. If there are too many, the remaining ones are ignored.

  `fillarray` returns its first argument.

  Examples:

  Create an array of 9 elements and fill it from a list.

      (%i1) array (a1, fixnum, 8);
      (%o1)                          a1

      (%i2) listarray (a1);
      (%o2)              [0, 0, 0, 0, 0, 0, 0, 0, 0]

      (%i3) fillarray (a1, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
      (%o3)                          a1

      (%i4) listarray (a1);
      (%o4)              [1, 2, 3, 4, 5, 6, 7, 8, 9]

  When there are too few elements to fill the array, the last element is repeated. When there are too many elements, the extra elements are ignored.

      (%i1) a2 : make_array (fixnum, 8);
      (%o1)           {Lisp Array: #(0 0 0 0 0 0 0 0)}

      (%i2) fillarray (a2, [1, 2, 3, 4, 5]);
      (%o2)           {Lisp Array: #(1 2 3 4 5 5 5 5)}

      (%i3) fillarray (a2, [4]);
      (%o3)           {Lisp Array: #(4 4 4 4 4 4 4 4)}

      (%i4) fillarray (a2, makelist (i, i, 1, 100));
      (%o4)           {Lisp Array: #(1 2 3 4 5 6 7 8)}

  Multiple-dimension arrays are filled in row-major order.

      (%i1) a3 : make_array (fixnum, 2, 5);
      (%o1)      {Lisp Array: #2A((0 0 0 0 0) (0 0 0 0 0))}

      (%i2) fillarray (a3, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
      (%o2)      {Lisp Array: #2A((1 2 3 4 5) (6 7 8 9 10))}

      (%i3) a4 : make_array (fixnum, 5, 2);
      (%o3)   {Lisp Array: #2A((0 0) (0 0) (0 0) (0 0) (0 0))}

      (%i4) fillarray (a4, a3);
      (%o4)   {Lisp Array: #2A((1 2) (3 4) (5 6) (7 8) (9 10))}

  Categories: Arrays ·

Function: listarray (A) ¶

  Returns a list of the elements of the array A. The argument A may be an array, an undeclared array (`hashed array`), a `memoizing function`, or a subscripted function.

  Elements are listed in row-major order. That is, elements are sorted according to the first index, then according to the second index, and so on. The sorting order of index values is the same as the order established by `orderless`.

  For undeclared arrays (`hashed arrays`), `memoizing functions`, and subscripted functions, the elements correspond to the index values returned by `arrayinfo`.

  Unbound elements of general arrays (that is, not `fixnum` and not `flonum`) are returned as `#####`. Unbound elements of `fixnum` or `flonum` arrays are returned as 0 or 0.0, respectively. Unbound elements of hashed arrays, `memoizing functions`, and subscripted functions are not returned.

  Examples:

  `listarray` and `arrayinfo` applied to a declared array.

      (%i1) array (aa, 2, 3);
      (%o1)                          aa

      (%i2) aa [2, 3] : %pi;
      (%o2)                          %pi

      (%i3) aa [1, 2] : %e;
      (%o3)                          %e

      (%i4) listarray (aa);
      (%o4) [#####, #####, #####, #####, #####, #####, %e, #####,
                                              #####, #####, #####, %pi]

      (%i5) arrayinfo (aa);
      (%o5)                 [declared, 2, [2, 3]]

  `listarray` and `arrayinfo` applied to an undeclared array (`hashed array`).

      (%i1) bb [FOO] : (a + b)^2;
                                         2
      (%o1)                       (b + a)

      (%i2) bb [BAR] : (c - d)^3;
                                         3
      (%o2)                       (c - d)

      (%i3) listarray (bb);
                                    3         2
      (%o3)                 [(c - d) , (b + a) ]

      (%i4) arrayinfo (bb);
      (%o4)               [hashed, 1, [BAR], [FOO]]

  `listarray` and `arrayinfo` applied to a `memoizing function`.

      (%i1) cc [x, y] := y / x;
                                           y
      (%o1)                      cc     := -
                                   x, y    x

      (%i2) cc [u, v];
                                      v
      (%o2)                           -
                                      u

      (%i3) cc [4, z];
                                      z
      (%o3)                           -
                                      4

      (%i4) listarray (cc);
                                    z  v
      (%o4)                        [-, -]
                                    4  u

      (%i5) arrayinfo (cc);
      (%o5)              [hashed, 2, [4, z], [u, v]]

  `listarray` and `arrayinfo` applied to a subscripted function.

      (%i1) dd [x] (y) := y ^ x;
                                           x
      (%o1)                     dd (y) := y
                                  x

      (%i2) dd [a + b];
                                          b + a
      (%o2)                  lambda([y], y     )

      (%i3) dd [v - u];
                                          v - u
      (%o3)                  lambda([y], y     )

      (%i4) listarray (dd);
                               b + a                v - u
      (%o4)      [lambda([y], y     ), lambda([y], y     )]

      (%i5) arrayinfo (dd);
      (%o5)             [hashed, 1, [b + a], [v - u]]

  Categories: Arrays ·

Function: make_array (type, dim_1, …, dim_n) ¶

  Creates and returns a Lisp array. type may be `any`, `flonum`, `fixnum`, `hashed` or `functional`. There are \(n\) indices, and the \(i\)’th index runs from 0 to \(\mathit{dim_i} - 1\).

  The advantage of `make_array` over `array` is that the return value doesn’t have a name, and once a pointer to it goes away, it will also go away. For example, if `y: make_array (...)` then `y` points to an object which takes up space, but after `y: false`, `y` no longer points to that object, so the object can be garbage collected.

  Examples:

      (%i1) A1 : make_array (fixnum, 10);
      (%o1)         {Lisp Array: #(0 0 0 0 0 0 0 0 0 0)}

      (%i2) A1 [8] : 1729;
      (%o2)                         1729

      (%i3) A1;
      (%o3)        {Lisp Array: #(0 0 0 0 0 0 0 0 1729 0)}

      (%i4) A2 : make_array (flonum, 10);
      (%o4) {Lisp Array: #(0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)}

      (%i5) A2 [2] : 2.718281828;
      (%o5)                      2.718281828

      (%i6) A2;
      (%o6)
       {Lisp Array: #(0.0 0.0 2.718281828 0.0 0.0 0.0 0.0 0.0 0.0 0.0)}

      (%i7) A3 : make_array (any, 10);
      (%o7) {Lisp Array: #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)}

      (%i8) A3 [4] : x - y - z;
      (%o8)                     (- z) - y + x

      (%i9) A3;
      (%o9) {Lisp Array: #(NIL NIL NIL NIL
                     ((MPLUS SIMP) $X ((MTIMES SIMP) -1 $Y) ((MTIMES S\
      IMP) -1 $Z))
                     NIL NIL NIL NIL NIL)}

      (%i10) A4 : make_array (fixnum, 2, 3, 5);
      (%o10) {Lisp Array: #3A(((0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0))
                       ((0 0 0 0 0) (0 0 0 0 0) (0 0 0 0 0)))}

      (%i11) fillarray (A4, makelist (i, i, 1, 2*3*5));
      (%o11) {Lisp Array: #3A(((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 1\
      5))
                       ((16 17 18 19 20) (21 22 23 24 25) (26 27 28 29\
       30)))}

      (%i12) A4 [0, 2, 1];
      (%o12)                         12

  Categories: Arrays ·

Function: rearray (A, dim_1, …, dim_n) ¶

  Changes the dimensions of an array. The new array will be filled with the elements of the old one in row-major order. If the old array was too small, the remaining elements are filled with `false`, `0.0` or `0`, depending on the type of the array. The type of the array cannot be changed.

  Categories: Arrays ·

Function: remarray
remarray (A_1, …, A_n)
remarray (all) ¶

  Removes arrays and array associated functions and frees the storage occupied. The arguments may be declared arrays, `hashed arrays`, array functions, and subscripted functions.

  `remarray (all)` removes all items in the global list `arrays`.

  It may be necessary to use this function if it is desired to clear the cache of a `memoizing function`.

  `remarray` returns the list of arrays removed.

  `remarray` quotes its arguments.

  Categories: Arrays ·

Function: subvar (x, i) ¶

  Evaluates the subscripted expression `x[i]`.

  `subvar` evaluates its arguments.

  `arraymake (x, [i])` constructs the expression `x[i]`, but does not evaluate it.

  Examples:

      (%i1) x : foo $
      (%i2) i : 3 $

      (%i3) subvar (x, i);
      (%o3)                         foo
                                       3

      (%i4) foo : [aa, bb, cc, dd, ee]$

      (%i5) subvar (x, i);
      (%o5)                          cc

      (%i6) arraymake (x, [i]);
      (%o6)                         foo
                                       3

      (%i7) ''%;
      (%o7)                          cc

  Categories: Expressions · Arrays ·

Function: subvarp (expr) ¶

  Returns `true` if expr is a subscripted variable, for example `a[i]`.

  Categories: Predicate functions ·

Option variable: use_fast_arrays ¶

  Default value: `false`

  When `use_fast_arrays` is `true`, arrays declared by `array` are values instead of properties, and undeclared arrays (`hashed arrays`) are implemented as Lisp hashed arrays.

  When `use_fast_arrays` is `false`, arrays declared by `array` are properties, and undeclared arrays are implemented with Maxima’s own hashed array implementation.

  Note that the code `use_fast_arrays` switches to is not necessarily faster than the default one; Arrays created by `make_array` are not affected by `use_fast_arrays`.

  See also `translate_fast_arrays`.

  Categories: Arrays · Global flags ·

Option variable: translate_fast_arrays ¶

  Default value: `false`

  When `translate_fast_arrays` is `true`, the Maxima-to-Lisp translator generates code that assumes arrays are values instead of properties, as if `use_fast_arrays` were `true`.

  When `translate_fast_arrays` is `false`, the Maxima-to-Lisp translator generates code that assumes arrays are properties, as if `use_fast_arrays` were `false`.

  Categories: Arrays · Translation flags and variables ·

### 7.6 Structures ¶

#### 7.6.1 Introduction to Structures ¶

Maxima provides a simple data aggregate called a structure. A structure is an expression in which arguments are identified by name (the field name) and the expression as a whole is identified by its operator (the structure name). A field value can be any expression.

A structure is defined by the `defstruct` function; the global variable `structures` is the list of user-defined structures. The function `new` creates instances of structures. The `@` operator refers to fields. `kill(S)` removes the structure definition S, and `kill(x@ a)` unbinds the field a of the structure instance x.

In the pretty-printing console display (with `display2d` equal to `true`), structure instances are displayed with the value of each field represented as an equation, with the field name on the left-hand side and the value on the right-hand side. (The equation is only a display construct; only the value is actually stored.) In 1-dimensional display (via `grind` or with `display2d` equal to `false`), structure instances are displayed without the field names.

There is no way to use a field name as a function name, although a field value can be a lambda expression. Nor can the values of fields be restricted to certain types; any field can be assigned any kind of expression. There is no way to make some fields accessible or inaccessible in different contexts; all fields are always visible.

#### 7.6.2 Functions and Variables for Structures ¶

Global variable: structures ¶

  `structures` is the list of user-defined structures defined by `defstruct`.

  Categories: Structures · Global variables ·

Function: defstruct
defstruct (S(a_1, …, a_n))
defstruct (S(a_1 = v_1, …, a_n = v_n)) ¶

  Define a structure, which is a list of named fields a_1, …, a_n associated with a symbol S. An instance of a structure is just an expression which has operator S and exactly `n` arguments. `new(S)` creates a new instance of structure S.

  An argument which is just a symbol a specifies the name of a field. An argument which is an equation `a = v` specifies the field name a and its default value v. The default value can be any expression.

  `defstruct` puts S on the list of user-defined structures, `structures`.

  `kill(S)` removes S from the list of user-defined structures, and removes the structure definition.

  Examples:

      (%i1) defstruct (foo (a, b, c));
      (%o1)                    [foo(a, b, c)]
      (%i2) structures;
      (%o2)                    [foo(a, b, c)]
      (%i3) new (foo);
      (%o3)                     foo(a, b, c)
      (%i4) defstruct (bar (v, w, x = 123, y = %pi));
      (%o4)             [bar(v, w, x = 123, y = %pi)]
      (%i5) structures;
      (%o5)      [foo(a, b, c), bar(v, w, x = 123, y = %pi)]
      (%i6) new (bar);
      (%o6)              bar(v, w, x = 123, y = %pi)
      (%i7) kill (foo);
      (%o7)                         done
      (%i8) structures;
      (%o8)             [bar(v, w, x = 123, y = %pi)]

  Categories: Structures ·

Function: new
new (S)
new (S (v_1, …, v_n)) ¶

  `new` creates new instances of structures.

  `new(S)` creates a new instance of structure S in which each field is assigned its default value, if any, or no value at all if no default was specified in the structure definition.

  `new(S(v_1, ..., v_n))` creates a new instance of S in which fields are assigned the values v_1, …, v_n.

  Examples:

      (%i1) defstruct (foo (w, x = %e, y = 42, z));
      (%o1)              [foo(w, x = %e, y = 42, z)]
      (%i2) new (foo);
      (%o2)               foo(w, x = %e, y = 42, z)
      (%i3) new (foo (1, 2, 4, 8));
      (%o3)            foo(w = 1, x = 2, y = 4, z = 8)

  Categories: Structures ·

Operator: @ ¶

  `@` is the structure field access operator. The expression `x@ a` refers to the value of field a of the structure instance x. The field name is not evaluated.

  If the field a in x has not been assigned a value, `x@ a` evaluates to itself.

  `kill(x@ a)` removes the value of field a in x.

  Examples:

      (%i1) defstruct (foo (x, y, z));
      (%o1)                    [foo(x, y, z)]
      (%i2) u : new (foo (123, a - b, %pi));
      (%o2)           foo(x = 123, y = a - b, z = %pi)
      (%i3) u@z;
      (%o3)                          %pi
      (%i4) u@z : %e;
      (%o4)                          %e
      (%i5) u;
      (%o5)            foo(x = 123, y = a - b, z = %e)
      (%i6) kill (u@z);
      (%o6)                         done
      (%i7) u;
      (%o7)              foo(x = 123, y = a - b, z)
      (%i8) u@z;
      (%o8)                          u@z

  The field name is not evaluated.

      (%i1) defstruct (bar (g, h));
      (%o1)                      [bar(g, h)]
      (%i2) x : new (bar);
      (%o2)                       bar(g, h)
      (%i3) x@h : 42;
      (%o3)                          42
      (%i4) h : 123;
      (%o4)                          123
      (%i5) x@h;
      (%o5)                          42
      (%i6) x@h : 19;
      (%o6)                          19
      (%i7) x;
      (%o7)                    bar(g, h = 19)
      (%i8) h;
      (%o8)                          123

  Categories: Structures · Operators ·

JavaScript license information