[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Data Types and Structures


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 Numbers


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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

Floating point numbers

Maxima has two types of floating point number. The first is just called a "float" (but will be called a "machine float" for the rest of this section to avoid ambiguity). This is stored in the underlying lisp's DOUBLE-FLOAT type which will almost certainly be IEEE 754 double precision floating point. To type a literal floating point number, just type its decimal expansion (for example, 0.01) or type it with an explicit exponent (such as 1e-2 or 0.1e-1).

The second type of floating point number in Maxima is called a "bigfloat". Bigfloats are stored as a mantissa and exponent in the same way as machine floats but the exponent is an arbitrary precision integer, so they can represent arbitrarily large or small numbers. The user can also customise the precision of bigfloat arithmetic (which corresponds to choosing the range of the mantissa). See fpprec for more information. To type a literal bigfloat, use the exponent notation as above but with the character b in place of e. The example of 0.01 from above could be entered as a bigfloat with 1b-2 or 0.001b0.

Calculations using machine floats can be significantly faster than using bigfloats since modern computer processors have dedicated hardware for them. This is particularly noticeable with compiled Maxima code. However, machine floats suffer from the problem of overflow, where a number can become too large for its exponent to be represented in the bits available. In interpreted code, the default behaviour is that a calculation that would cause a floating point overflow instead generates a bigfloat number. To configure this, see the promote_float_to_bigfloat variable.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1.2 Functions and Variables for Numbers

Function: bfloat (expr)

Converts all numbers and functions of numbers in expr to bigfloat numbers. The number of significant digits in the resulting bigfloats is specified by the global variable fpprec.

When float2bf is false a warning message is printed when a floating point number is converted into a bigfloat number (since this may lead to loss of precision).

Categories:  Numerical evaluation

Function: bfloatp (expr)

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

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.e-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: evenp (expr)

Returns true if expr is an even integer. false is returned in all other cases.

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.

Option variable: float2bf

Default value: true

When float2bf is false, a warning message is printed when a floating point number is converted into a bigfloat number (since this may lead to loss of precision).

Categories:  Numerical evaluation

Function: floatnump (expr)

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

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 of digits printed is 16.

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.

Function: integerp (expr)

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

integerp returns false if its argument is a symbol, even if the argument 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

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.

Function: numberp (expr)

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

numberp returns false if its argument is a symbol, even if the argument 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, .8414709848078965, .3660254037844387]

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;
(%o1) (-2)^0.75

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

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

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

(%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.

Function: oddp (expr)

is true if expr is an odd integer. false is returned in all other cases.

Categories:  Predicate functions

Option variable: promote_float_to_bigfloat

Default value: true

When promote_float_to_bigfloat is true, the result of any floating point calculation that would normally cause a floating point overflow is replaced by a bigfloat number that represents the result. Note that this automatic promotion only happens in interpreted code: compiled code is not affected.

This automatic conversion is often convenient, but can be unhelpful in some cases. For example, it can actually cause a loss of precision if fpprec is currently smaller than the precision in a floating point number. To disable this behaviour, set promote_float_to_bigfloat to false.

Categories:  Numerical evaluation

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.

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);
                               1
(%o2)                          --
                               10
(%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));
                              x    28
(%o7)                     sin(-- + --)
                              10   5

Categories:  Numerical evaluation

Function: ratnump (expr)

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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 Strings


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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.

(%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.

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

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.

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."


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 Constants


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3.1 Functions and Variables for 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.

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 ....

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: minf

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

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(x+zeroa);
(%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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4 Lists


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4.1 Introduction to Lists

Lists are the basic building block for Maxima and Lisp. All data types other than arrays, hash tables, 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))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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, hash array, or array function.

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).

Do example(append); for an example.

Categories:  Lists · Expressions

Function: assoc  
    assoc (key, list, default)  
    assoc (key, list)

This function searches for key in the left hand side of the input list. The list argument should be a list, each of whose elements is an expression with exactly two parts. Most usually, the elements of list are themselves lists, each with two elements.

The assoc function iterates along list, checking the first part of each element for equality with key. If an element is found where the comparison is true, assoc returns the second part of that element. If there is no such element in the list, assoc returns either false or default, if given.

For example, in the expression assoc (y, [[x,1], [y,2], [z,3]]), the assoc function searches for x in the left hand side of the list [[y,1],[x,2]] and finds it at the second term, returning 2. In assoc (z, [[x,1], [z,2], [z,3]]), the search stops at the first term starting with z and returns 2. In assoc(x, [[y,1]]), there is no matching element, so assoc returns false.

(%i1)                 assoc(y, [[x, 1], [y, 2], [z, 3]])
(%o1)                                  2
(%i2)                 assoc(z, [[x, 1], [z, 2], [z, 3]])
(%o2)                                  2
(%i3)                         assoc(x, [[y, 1]])
(%o3)                                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))];
rat: replaced 0.0 by 0/1 = 0.0
`rat' replaced 0.0B0 by 0/1 = 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 8'th 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 5'th 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. 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. Note that the simplifier re-orders expressions. Thus first(x+y) will be x if inflag is true and y if inflag is false (first(y+x) gives the same results). The functions secondtenth yield the second through the tenth part of their input argument.

Categories:  Lists · Expressions

Function: fourth (expr)

Returns the 4'th 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.

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.

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).

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: makelist  
    makelist ()  
    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 9'th 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 second 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) 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)

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 a mapatom, 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 an 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 ^.

(%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.

Categories:  Lists · Expressions

Function: second (expr)

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

Categories:  Lists · Expressions

Function: seventh (expr)

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

Categories:  Lists · Expressions

Function: sixth (expr)

Returns the 6'th 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, %e, %i, %i + 1, inf, minf, sin(1), 
                                                      1.0b0, 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: 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: tenth (expr)

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

Categories:  Lists · Expressions

Function: third (expr)

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

Categories:  Lists · Expressions


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.5 Arrays


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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.

If the user assigns to a subscripted variable before declaring the corresponding array, an undeclared array is created. Undeclared arrays, otherwise known as hashed arrays (because hash coding is done on the subscripts), are more general than declared arrays. The user does not declare their maximum size, and they grow dynamically by hashing as more elements are assigned values. The subscripts of undeclared arrays need not even be numbers. However, unless an array is rather sparse, it is probably more efficient to declare it when possible than to leave it undeclared. The array function can be used to transform an undeclared array into a declared 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, an undeclared (hashed) array, an array 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 array 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 (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 an array 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

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)          {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)          {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 constructed by implicit definition (assigning something to an array element), and array 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)       {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)             {Array:  #(0 0 0 0 0 0 0 0)}
(%i2) fillarray (a2, [1, 2, 3, 4, 5]);
(%o2)             {Array:  #(1 2 3 4 5 5 5 5)}
(%i3) fillarray (a2, [4]);
(%o3)             {Array:  #(4 4 4 4 4 4 4 4)}
(%i4) fillarray (a2, makelist (i, i, 1, 100));
(%o4)             {Array:  #(1 2 3 4 5 6 7 8)}

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

(%i1) a3 : make_array (fixnum, 2, 5);
(%o1)        {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)        {Array:  #2A((1 2 3 4 5) (6 7 8 9 10))}
(%i3) a4 : make_array (fixnum, 5, 2);
(%o3)     {Array:  #2A((0 0) (0 0) (0 0) (0 0) (0 0))}
(%i4) fillarray (a4, a3);
(%o4)     {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 a declared array, an undeclared (hashed) array, an array 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, array functions, and subscripted functions, the elements correspond to the index values returned by arrayinfo.

Unbound elements of declared general arrays (that is, not fixnum and not flonum) are returned as #####. Unbound elements of declared fixnum or flonum arrays are returned as 0 or 0.0, respectively. Unbound elements of undeclared arrays, array 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 (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 an array 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 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)           {Array:  #(0 0 0 0 0 0 0 0 0 0)}
(%i2) A1 [8] : 1729;
(%o2)                         1729
(%i3) A1;
(%o3)          {Array:  #(0 0 0 0 0 0 0 0 1729 0)}
(%i4) A2 : make_array (flonum, 10);
(%o4) {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) 
     {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) {Array:  #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)}
(%i8) A3 [4] : x - y - z;
(%o8)                      - z - y + x
(%i9) A3;
(%o9) {Array:  #(NIL NIL NIL NIL ((MPLUS SIMP) $X ((MTIMES SIMP)\
 -1 $Y) ((MTIMES SIMP) -1 $Z))
  NIL NIL NIL NIL NIL)}
(%i10) A4 : make_array (fixnum, 2, 3, 5);
(%o10) {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) {Array:  #3A(((1 2 3 4 5) (6 7 8 9 10) (11 12 13 14 15))
    ((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, undeclared (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 redefine the values in a hashed array.

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

If true then only two types of arrays are recognized:

  1. The art-q array (t in Common Lisp) which may have several dimensions indexed by integers, and may hold any Lisp or Maxima object as an entry. To construct such an array, enter a:make_array(any,3,4); then a will have as value, an array with twelve slots, and the indexing is zero based.
  2. The Hash_table array which is the default type of array created if one does b[x+1]:y^2 (and b is not already an array, a list, or a matrix - if it were one of these an error would be caused since x+1 would not be a valid subscript for an art-q array, a list or a matrix). Its indices (also known as keys) may be any object. It only takes one key at a time (b[x+1,u]:y would ignore the u). Referencing is done by b[x+1] ==> y^2. Of course the key may be a list, e.g. b[[x+1,u]]:y would be valid. This is incompatible with the old Maxima hash arrays, but saves consing.

An advantage of storing the arrays as values of the symbol is that the usual conventions about local variables of a function apply to arrays as well. The Hash_table type also uses less consing and is more efficient than the old type of Maxima hashar. To obtain consistent behaviour in translated and compiled code set translate_fast_arrays to be true.

Categories:  Arrays · Global flags


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.6 Structures


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.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


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Jaime Villate on November, 25 2014 using texi2html 1.76.