stefanv

357 Reputation

9 Badges

20 years, 28 days

Stefan Vorkoetter is a Senior Architect at Maplesoft, a position that is a combination of design consultant and developer-at-large.

He has been with Maplesoft since May of 1989 after completing a graduate program at the University of Waterloo under Maplesoft co-founder Gaston Gonnet. During his undergraduate career, he worked part time at UW's Symbolic Computation Group, where the Maple system was born.

Stefan's areas of expertise are in algorithms, data structures, and programming language design and implementation. He has worked extensively on various aspects of the Maple kernel, and more recently, the Modelica compiler component of MapleSim. Despite holding a Master of Mathematics degree, he considers himself a computer scientist first.

Stefan was born in Germany, but immigrated to Canada at the age of three. Like many at Maplesoft, he moved to Waterloo to attend the University of Waterloo, met his wife there, and never left. When not working, Stefan is an active participant in the Norwegian Fjordhorse farm he and Lori call home. He also dabbles in electronics, model and full-scale aviation, music, and collecting slide rules and old calculators, and maintains a web site documenting his hobbies.

MaplePrimes Activity


These are Posts that have been published by stefanv

An expression sequence is the underlying data structure for lists, sets, and function call arguments in Maple. Conceptually, a list is just a sequence enclosed in "[" and "]", a set is a sequence (with no duplicate elements) enclosed in "{" and "}", and a function call is a sequence enclosed in "(" and ")". A sequence can also be used as a data structure itself:

> Q := x, 42, "string", 42;
                           Q := x, 42, "string", 42

> L := [ Q ];
                          L := [x, 42, "string", 42]

> S := { Q };
                            S := {42, "string", x}

> F := f( Q );
                          F := f(x, 42, "string", 42)

A sequence, like most data structures in Maple, is immutable. Once created, it cannot be changed. This means the same sequence can be shared by multiple data structures. In the example above, the list assigned to and the function call assigned to both share the same instance of the sequence assigned to . The set assigned to refers to a different sequence, one with the duplicate 42 removed, and sorted into a canonical order.

Appending an element to a sequence creates a new sequence. The original remains unaltered, and still referenced by the list and function call:

> Q := Q, a+b;
                        Q := x, 42, "string", 42, a + b

> L;
                             [x, 42, "string", 42]

> S;
                               {42, "string", x}

> F;
                            f(x, 42, "string", 42)

Because appending to a sequence creates a new sequence, building a long sequence by appending one element at a time is very inefficient in both time and space. Building a sequence of length this way creates sequences of lengths 1, 2, ..., -1, . The extra space used will eventually be reclaimed by Maple's garbage collector, but this takes time.

This leads to the subject of this article, which is how to create long sequences efficiently. For the remainder of this article, the sequence we will use is the Fibonacci numbers, which are defined as follows:

  • Fib(0) = 0
  • Fib(1) = 1
  • Fib() = Fib(-1) + Fib(-2) for all > 1

In a computer algebra system like Maple, the simplest way to generate individual members of this sequence is with a recursive function. This is also very efficient if option is used (and very inefficient if it is not; computing Fib() requires 2 Fib() - 1 calls, and Fib() grows exponentially):

> Fib := proc(N)
>     option remember;
>     if N = 0 then
>         0
>     elif N = 1 then
>         1
>     else
>         Fib(N-1) + Fib(N-2)
>     end if
> end proc:
> Fib(1);
                                       1

> Fib(2);
                                       1

> Fib(5);
                                       5

> Fib(10);
                                      55

> Fib(20);
                                     6765

> Fib(50);
                                  12586269025

> Fib(100);
                             354224848179261915075

> Fib(200);
                  280571172992510140037611932413038677189525

Let's start with the most straightforward, and most inefficient way to generate a sequence of the first 100 Fibonacci numbers, starting with an empty sequence and using a for-loop to append one member at a time. Part of the output has been elided below in the interests of saving space:

> Q := ();
                                     Q :=

> for i from 0 to 99 do
>     Q := Q, Fib(i)
> end do:
> Q;
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,

    4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811,

    ...

    51680708854858323072, 83621143489848422977, 135301852344706746049,

    218922995834555169026

As mentioned previously, this actually produces 100 sequences of lengths 1 to 100, of which 99 will (eventually) be recovered by the garbage collector. This method is O(2) (Big O Notation) in time and space, meaning that producing a sequence of 200 values this way will take 4 times the time and memory as a sequence of 100 values.

The traditional Maple wisdom is to use the seq function instead, which produces only the requested sequence, and no intermediate ones:

> Q := seq(Fib(i),i=0..99);
Q := 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,

    2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418,

    ...

    51680708854858323072, 83621143489848422977, 135301852344706746049,

    218922995834555169026

This is O() in time and space; generating a sequence of 200 elements takes twice the time and memory required for a sequence of 100 elements.

As of Maple 2019, it is also possible to achieve O() performance by constructing a sequence directly using a for-expression, without the cost of constructing the intermediate sequences that a for-statement would incur:

> Q := (for i from 0 to 99 do Fib(i) end do);
Q := 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,

    2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418,

    ...

    51680708854858323072, 83621143489848422977, 135301852344706746049,

    218922995834555169026

This method is especially useful when you wish to add a condition to the elements selected for the sequence, since the full capabilities of Maple loops can be used (see The Two Kinds of Loops in Maple). The following two examples produce a sequence containing only the odd members of the first 100 Fibonacci numbers, and the first 100 odd Fibonacci numbers respectively:

> Q := (for i from 0 to 99 do
>           f := Fib(i);
>           if f :: odd then
>               f
>           else
>               NULL
>           end if
>       end do);
Q := 1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597, 4181, 6765, 17711, 28657,

    75025, 121393, 317811, 514229, 1346269, 2178309, 5702887, 9227465,

    ...

    19740274219868223167, 31940434634990099905, 83621143489848422977,

    135301852344706746049

> count := 0:
> Q := (for i from 0 while count < 100 do
>           f := Fib(i);
>           if f :: odd then
>               count += 1;
>               f
>           else
>               NULL
>           end if
>       end do);
Q := 1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597, 4181, 6765, 17711, 28657,

    75025, 121393, 317811, 514229, 1346269, 2178309, 5702887, 9227465,

    ...

    898923707008479989274290850145, 1454489111232772683678306641953,

    3807901929474025356630904134051, 6161314747715278029583501626149

> i;
                                      150

A for-loop used as an expression generates a sequence, producing one member for each iteration of the loop. The value of that member is the last expression computed during the iteration. If the last expression in an iteration is NULL, no value is produced for that iteration.

Examining after the second loop completes, we can see that 149 Fibonacci numbers were generated to find the first 100 odd ones. (The loop control variable is incremented before the while condition is checked, hence is one more than the number of completed iterations.)

Until now, we've been using calls to the Fib function to generate the individual Fibonacci numbers. These numbers can of course also be generated by a simple loop which, together with assignment of its initial conditions, can be written as a single sequence:

> Q := ((f0 := 0),
>       (f1 := 1),
>       (for i from 2 to 99 do
>            f0, f1 := f1, f0 + f1;
>            f1
>        end do));
Q := 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,

    2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418,

    ...

    51680708854858323072, 83621143489848422977, 135301852344706746049,

    218922995834555169026

A Maple Array is a mutable data structure. Changing an element of an Array modifies the Array in-place; no new copy is generated:

> A := Array([a,b,c]);
                                A := [a, b, c]

> A[2] := d;
                                   A[2] := d

> A;
                                   [a, d, c]

It is also possible to append elements to an array, either by using programmer indexing, or the recently introduced ,= operator:

> A(numelems(A)+1) := e; # () instead of [] denotes "programmer indexing"
                               A := [a, d, c, e]

> A;
                                 [a, d, c, e]

Like appending to a sequence, this sometimes causes the existing data to be discarded and new data to be allocated, but this is done in chunks proportional to the current size of the Array, resulting in time and memory usage that is still O(). This can be used to advantage to generate sequences efficiently:

> A := Array(0..1,[0,1]);
                              [ 0..1 1-D Array       ]
                         A := [ Data Type: anything  ]
                              [ Storage: rectangular ]
                              [ Order: Fortran_order ]

> for i from 2 to 99 do
>     A ,= A[i-1] + A[i-2]
> end do:
> A;
                           [ 0..99 1-D Array      ]
                           [ Data Type: anything  ]
                           [ Storage: rectangular ]
                           [ Order: Fortran_order ]

> Q := seq(A);
Q := 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,

    2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418,

    ...

    51680708854858323072, 83621143489848422977, 135301852344706746049,

    218922995834555169026

Although unrelated specifically to the goal of producing sequences, the same techniques can be used to construct Maple strings efficiently:

> A := Array("0");
                                   A := [48]

> for i from 1 to 99 do
>    A ,= " ", String(Fib(i))
> end do:
> A;
                           [ 1..1150 1-D Array     ]
                           [ Data Type: integer[1] ]
                           [ Storage: rectangular  ]
                           [ Order: Fortran_order  ]

> A[1..10];
                   [48, 32, 49, 32, 49, 32, 50, 32, 51, 32]

> S := String(A);
S := "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 \
    10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 134626\
    9 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 1\
    02334155 165580141 267914296 433494437 701408733 1134903170 1836311903 \
    2971215073 4807526976 7778742049 12586269025 20365011074 32951280099 53\
    316291173 86267571272 139583862445 225851433717 365435296162 5912867298\
    79 956722026041 1548008755920 2504730781961 4052739537881 6557470319842\
     10610209857723 17167680177565 27777890035288 44945570212853 7272346024\
    8141 117669030460994 190392490709135 308061521170129 498454011879264 80\
    6515533049393 1304969544928657 2111485077978050 3416454622906707 552793\
    9700884757 8944394323791464 14472334024676221 23416728348467685 3788906\
    2373143906 61305790721611591 99194853094755497 160500643816367088 25969\
    5496911122585 420196140727489673 679891637638612258 1100087778366101931\
     1779979416004714189 2880067194370816120 4660046610375530309 7540113804\
    746346429 12200160415121876738 19740274219868223167 3194043463499009990\
    5 51680708854858323072 83621143489848422977 135301852344706746049 21892\
    2995834555169026"

A call to the Array constructor with a string as an argument produces an array of bytes (Maple data type integer[1]). The ,= operator can then be used to append additional characters or strings, with O() efficiency. Finally, the Array can be converted back into a Maple string.

Constructing sequences in Maple is a common operation when writing Maple programs. Maple gives you many ways to do this, and it's worthwhile taking the time to choose a method that is efficient, and suitable to the task at hand.

When discussing Maple programming, we often refer to for-loops, while-loops, until-loops, and do-loops (the latter being an infinite loop). But under the hood, Maple has only two kinds of loop, albeit very flexible and powerful ones that can combine the capabilities of any or all of the above, making it possible to write very concise code in a natural way.

Before looking at some actual examples, here is the formal definition of the loops' syntax, expressed in Wirth Syntax Notation, where "|" denotes alternatives, "[...]" denotes an optional part, "(...)" denotes grouping, and Maple keywords are in boldface:

[ for  ] [ from  ] [ by  ] [ to  ]
    [ while  ]
do
    
( end do | until  )
[ for  [ , variable ] ] in 
    [ while  ]
do
    
( end do | until  )

In the first form, every part of the loop syntax is optional, except the do keyword before the body of the loop, and either end do or an until clause after the body. (For those who prefer it, end do can also be written as od.) In the second form, only the in clause is required.

The simplest loop is just:

do
    
end do

This will repeat the forever, unless a break or return statement is executed, or an error occurs.

One or two loop termination conditions can be added:

  • A while clause can be written before the do, specifying a condition that is tested before each iteration begins. If the condition evaluates to false, the loop ends.
  • An until clause can be written instead of the end do, specifying a condition that is tested after each iteration finishes. If the condition evaluates to true, the loop ends.

A so-called for-loop is just a loop to which iteration clauses have been added. These can take one of two forms:

  • Any combination of for (with a single variable), from, by, and to clauses. The last three can appear in any order.
  • A for clause with one or two variables, followed by an in clause.

The following for-loop executes 10 times:

for  from 1 to 10 do
    
end do

However, if the doesn't depend on the value of , both the for and from clauses can be omitted:

to 10 do
    
end do

In this case, Maple supplies an implicit for clause (with an inaccessible internal variable), as well as an implicit "from 1" clause. In fact, all of the clauses are optional, and the infinite loop shown earlier is understood by Maple in exactly the same way as:

for  from 1 by 1 to infinity while true do
    
until false

When looping over the contents of a container, such as a one-dimensional array A, there are several possible approaches. The one closest to how it would be done in most other programming languages is (this example and those that follow can be copied and pasted into a Maple session):

 := Array([,"foo",42]);
for  from lowerbound() to upperbound() do
    print([],[])
end do;

If only the entries in the container are of interest, it is not necessary to loop over the indices. Instead, one can write:

 := Array([,"foo",42]);
for  in  do
    print()
end do;

If both the indices and values are needed, one can write:

 := Array([,"foo",42]);
for ,  in  do
    print([],)
end do;

For a numerically indexed container such as an Array, this is equivalent to the for-from-to example. However, this method also works with arbitrarily indexed containers such as a Matrix or table:

 := LinearAlgebra:-RandomMatrix(2,3);
for ,  in  do
    print([],)
end do;
 := table({1="one","hello"="world",=42});
for ,  in eval() do
    print([],)
end do;

(The second example requires the call to eval due to last-name evaluation of tables in Maple, a topic for another post.)

As with a simple do-loop, a while and/or until clause can be added. For example, the following finds the first negative entry, if any, in a Matrix (traversing the Matrix in storage order):

 := LinearAlgebra:-RandomMatrix(2,3);
for ,  in  do
    # nothing to do here
until  < 0;
if  < 0 then
    print([],)
end if;

Notice that the test, < 0, is written twice, since it is possible that the Matrix has no negative entry. Another way to write the same loop but only perform the test once is as follows:

 := LinearAlgebra:-RandomMatrix(2,3);
for ,  in  do
    if  < 0 then
	print([],);
	break
    end if;
end do;

Here, we perform the test within the loop, perform the desired processing on the found value (just printing in this case), and use a break statement to terminate the loop.

Sometimes, it is useful to abort the current iteration of the loop and move on to the next one. The next statement does exactly that. The following loop prints all the indices but only the positive values in a Matrix:

 := LinearAlgebra:-RandomMatrix(2,3);
for ,  in  do
    print(=[]);
    if  < 0 then
	next
    end if;
    print(=);
end do;

(Note that a simple example like this would be better written by enclosing the printing of the value in an if-statement instead of using next. The latter is generally only used if the former is not possible.)

Maple's loop statements are very flexible and powerful, making it possible to write loops with complex combinations of termination conditions in a concise yet readable way. The ability to use while and/or until in conjunction with for means that break statements are often unnecessary, further improving clarity.

This is the final installment in a series of three articles outlining new features of Maple's command line (TTY) interface. Today we'll cover improvements to ImageTools:-Preview for use from a terminal.

Character based terminals are not well suited for displaying graphics, although the earliest Maple plotting support was specifically for such devices because better options were not widely available. But for text-based work such as writing code and documentation, many developers still prefer to work in a terminal, where everything is just a keystroke or two away. A problem arises when working on projects involving graphics. Obviously, it's easy to open an appropriate image viewer for such tasks, but for quick previews where details aren't important, a terminal-based solution can be convenient.

As a sample image for the first few examples, we'll use this well known public domain test image, commonly referred to as the baboon, although it is actually a mandrill:

The ImageTools:-Preview function has always been able to render output in a terminal. The output is akin to what one might have seen hanging on a data center wall in the 1970s:

> interface(screenheight=40,screenwidth=72,plotdevice=char):
> with(ImageTools):
> img := Read("../../Pictures/baboon.png");
                     [ 1..512 x 1..512 x 1..3 3-D Array ]
              img := [ Data Type: float[8]              ]
                     [ Storage: rectangular             ]
                     [ Order: C_order                   ]

> Preview(img);
oO*O*o++==**+o+=+-+=*o**=++=+*++++o*++~=o===*****==+===+++--~~=~~~+*
@o*++++~=+~++++++=++**oO*oOOooOOOOOOoo*++ooo*+++====+*++====~~~-==+*
Ooo++===+==+***o*oo***oooOo*++==**==~~=*oO****ooooooooo***++=~~~~~+*
@o*+=+~===*+oOo@@@@@@@@@@@@@@oooOOO*oO@@OOO@@@@@@@@@@@@@o*+===~-=++*
O**==~+=~=+*O@O@@@@@@OO@@O@@@@@@@OOO@@o*o@+=**=+@ooOOOoo+=+==~~~~=++
o++++=-~~~=++*oOO**O@*=**+o@Oo@@@@@@@o*O@@o===+*@+~+o*+++~===~~=~++*
+*++==~~==~=+=++*+=o@@@@@@@@ooo@@@@OO*Ooo@@@@OO*+=*oo*+==~~~~--=~++o
o*+=+~=~~=~~++++*****oo*+**=+O@OOO*o+*oo+++*+=~+*oo**+*+~=~~-~~~~=+=
O*=*=~=~~===~==+****OOOooooOOOO+O*=+*+Oo=~-~~=OOo**+*++=~~~-~-~-~+=+
O+++~~+~=~~+==+o**oo@@*~-----O*=@o++=o@=   -~~=*ooo*o+====~~~~-~~+=+
*o**++===+~=+=*+OOOOo=-~~   -o*=o*=++=o     -~==+oo**o++~~~----~~~++
o*oo*===+==+=*oo*oO=-=-  -  -*=+o+=++=+    -----=**+++*+~~==~-~-=+~*
*o**+*=++=~==~=+**O~- ~     -+=+*+=++==       ~~-~++=+~=~~~~-~~~+==+
ooo*o***+++~+==+oo==-  -    -+=++=+++==      -- -~+=+~~~~~~~=~~===+*
oo*o*==*+=====oooo-~-  -    -==*+==++==         --~*++=~=--~-=====**
*oooo*+*+===++=*@*--~-      ~=+*+==++==         ~--+*==+=~~~-=~+++**
oOOoOo**+*+=+=+O@=- ~-      ~=+*+~~=++~        -- -*o*+====~=+=+*++*
++*ooooOo+**+*OoO~~ -~      ~=+*+-~+++~-      -~  ~ooo+=~=~=+=******
*o+OO@@ooOooO@O@@~=- --     ==++=~~+++~       ~  -+Oo*==++=*+**+***+
*++*ooOOOO@OOO@@@=-=- -     =+++=-~+++~      ~   -*o***++=++*o*oo*oo
~===++**oOOO@@@@@@-~-  -    ~***=- =++-     -   -~oOo**o*oooo***oooo
~~~=~~==+O@@@@@@@@=-=  -  - -++*=-~=++     --   ~+@OO*******o**o*OOO
=~-~-=+oO@@@@@@@@@*~--  -    =+*+==+*~  -  -   -~O@@o*ooo*ooooooooOO
----~==+o@@@@@@@@@o+-~  -  - ~**+=++*- -  -- ---=@@@OO**oooooo*ooooo
-~~~~=+**o@@@@@@@@@+~~~-=- --=**+==++- ~  + -~~~*OOOOoo*ooooo***oOOO
---~====*@@@@@@@@@@O+~+=~= -~+*++=+++-~- =+~~~++*oOOOOoooooooo**oooo
---~--~=O@@O@@O**oOO++*+~+--++++==+++=~ -+~~=*=***oOoOoooOOOoooooo*o
-    --+**oo+===*Oooo**+~=++++++==++++=~~~~+++o*+**oOo****oOOoooo*o*
      ~++oO+-~~~+@Oo***+====+++===+++=====+=+++******++**++*oOooo***
-     ~+***=~~--=ooo**+++oo*~~~~~~~~~+*+===+++=***+++*+++****oo***++
      -=***=~---~+oOo*+++==*o=+=~==+*+===+++**+***+**+=====+oo**+++=
   - --~**===~~-~~=*O*++===++*+===+==+++++++******o**=~~~===+**++++=
 -   --~+====- --~~=+*+++++++++==++==++++++++*****~=+=~~---=+**++===
-    --~======~~-~~ -===+++++**=++++++++++++=+++~=~~+=-----~=***+===
   -----~~+=*=~-~-~+- --~=++++++++++++++++=+-~- +=++~--~~-~==++*+===
-----~-~======~~-~~~~--   ~~~~~~==~===~=~~- --=~=o=~~~~~~~=++++++=++
~~--~--~~~==+++==~-- ~=-~--~~=~~~---~-~~-~-~~=+*=+=~~~-===++***+++=+
-~~~~~~~~~==++=++=--    -~~=++++===+=~==+==~**=+=~==~~=+*++****+++++
~~=~~~~~~~==++***+=--     --~=====++=~===+~~+~--~~~=++++****o*+****o
=====~~~~===+***oo*+-        - --------  --     -~=++*o*ooooo*+*oooO

If you squint really hard, you can almost discern the image. As of Maple 2018.1, ImageTools:-Preview now uses color output if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@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

In addition to providing quick previews of image files, this feature can also be used to get more information about plots. For example, consider the following 3D plot generated using Maple's color character plot driver:

> plot3d([1,x,y],x=0..2*Pi,y=0..2*Pi,coords=toroidal(10));

                                                                        
                              ------------                              
                        ----------||-|----------                        
                     --------\-\\-||-|-//-/--------                     
                   ---------\\\\\|||-||/-///---------                   
                 --/-//-----\\\\||||-|||/////---\\\-\--                 
                -/-//-//-/-\\\\\||||||||//-/--\\\\-\\---                
               --|-||-||-|--\\\\||||||||////--|/||-||-|--               
              /--|-\-\-\\---\\\||||||||||///-////-/-/-|--\              
             //-----\------\\\\\||||||||////-------/-----\              
             //////--/--------\||||||||||/--------\--\\\\\\             
            /////// //// /-/--/-/-|-|||\-\--\-\ \\\\ \-\\\\\            
            |/////-////-/-//-/-/-|--|-|-\-\-\\-\-\\\\-\ \\\|            
            |//|////-/-// / /--|-|--|-|-|--\ \\-\-\ \\\\|\\|            
            ||| |-/-/  |-/--|-|--|  | | -|-|--\-|| \-\-| |||            
            |||-||| /-|--| |  | -|--|-|- |  | |--|-\ ||-||||            
            |||| |-|  | |--|--|--|  | |--|--|--| |  |-| ||||            
             |||-| |-||-|  |  |  ---|-|  |  |  |-||-| |-|||             
              ||-|-| | -|--|--|  |  | || |--|--|- | |-|-||/             
              \||| |-|- |  |  ---|--|--|--| |  | -|-| |-|/              
               \-|-| | -|--|-|| |   |  |  | |--|--| |-|//               
                \|||||- |  |-|--|---|--|--|-|  | -|-|||/                
                 \|\|-|-|--|--| |   |  |  |-|--|-|-|-|/                 
                   \\\|-|- |  |--|--|--|-|  | -|-|-//                   
                     --\-\-|--|--|--|-|--|--|-/-/--                     
                        ----\-|--|--|-|--|-/----                        
                              -\-|--|-|-/-                              
                                                                        

The output shows the general shape of the plot, but unless you already know what it looks like, it can be hard to visualize, especially since the character drivers don't support shading. One can glean additional insight by generating a high resolution bitmap version of the plot, and then Preview-ing that:

> interface(plotdevice="bmp",plotoutput="plot.bmp");
                          colorchar, terminal

> plot3d([1,x,y],x=0..2*Pi,y=0..2*Pi,coords=toroidal(10));
# Revert to colorchar or Preview won't generate a textual preview.
> interface(plotdevice="colorchar",plotoutput=terminal);
                             bmp, plot.bmp

> img := Read("plot.bmp");
                     [ 1..360 x 1..480 x 1..3 3-D Array ]
              img := [ Data Type: float[8]              ]
                     [ Storage: rectangular             ]
                     [ Order: C_order                   ]

> Preview(img);
@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

By default, Preview uses the extended xterm 256 color mode. The image is resized to fit within the current settings of interface(screenwidth) and interface(screenheight), and then each pixel is plotted using an "@" character over a colored background. The foreground and background colors are chosen so that their weighted average comes as close as possible to the actual RGB value of the pixel. Since this rarely produces exactly the right color, Preview also performs dithering, taking the error from the current pixel and distributing it over surrounding pixels. In effect, this uses spatial resolution to make up for the lack of color resolution.

ImageTools:-Preview has two keyword options, dither and sixteen, that let you override this behaviour. Passing the option dither=false will tell Preview not to dither. Passing sixteen=true (or just sixteen) will result in Preview using only the sixteen standard colors, of which only eight are used for the background.

The following examples are based on this Maple logo image:

By default, ImageTools:-Preview renders this with 256 colors and dithering as follows:

> img := Read("../../Pictures/logo.png");
                     [ 1..428 x 1..459 x 1..3 3-D Array ]
              img := [ Data Type: float[8]              ]
                     [ Storage: rectangular             ]
                     [ Order: C_order                   ]

> Preview(img);
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

If we render it without dithering, the result is sharper, but the subtle gradient from lighter to darker blue turns into a series of very obvious concentric rings, as there are not enough distinct colors in the terminal's palette to render it accurately:

> Preview(img,dither=false);
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

In sixteen color mode, the palette is severely reduced, and as a result, the dithering is much more pronounced as the per-pixel errors are larger:

> Preview(img,sixteen);
@@@@@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

One thing to note is that Maple has no way of knowing what the sixteen "standard" colors of your terminal actually look like, since many terminals let you change the palette. Maple assumes you are using the 16-color VGA palette, since that is close to the actual default palette on most terminals. If you are using a different palette (for example, Solarized), 16-color images will look worse than they are shown above.

Last week, in the first of a series of three articles, I demonstrated the new color syntax highlighting in the command line (TTY) interface of Maple 2018.1. This week, we'll look at a new facility for manipulating the command line history, the history meta-commands.

For the series of screen shots in this article, assume that the .maple_history file in your home directory initially contains the following:

    p1 := plot(sin,color="DeepPink"):
    p2 := plot(cos,color="DodgerBlue"):
    plots[display](p1,p2);

Now we'll start cmaple and execute a series of commands:

    |\^/|     Maple 2018.1 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2018
 \  MAPLE  /  All rights reserved. Maple is a trademark of
 <____ ____>  Waterloo Maple Inc.
      |       Type ? for help.
> y := 1/(x^4+1);
                                     1
                              y := ------
                                    4
                                   x  + 1

> int(y,x);
     1/2           1/2             1/2           1/2
1/4 2    arctan(x 2    + 1) + 1/4 2    arctan(x 2    - 1)

                    2      1/2
            1/2    x  + x 2    + 1
     + 1/8 2    ln(---------------)
                    2      1/2
                   x  - x 2    + 1

> diff(%,x);
          1                       1              1/2
--------------------- + --------------------- + 2
       1/2     2               1/2     2
2 ((x 2    + 1)  + 1)   2 ((x 2    - 1)  + 1)

    /         1/2        2      1/2              1/2 \
    |  2 x + 2         (x  + x 2    + 1) (2 x - 2   )|
    |--------------- - ------------------------------|
    | 2      1/2               2      1/2     2      |
    \x  - x 2    + 1         (x  - x 2    + 1)       /

      2      1/2        /      2      1/2
    (x  - x 2    + 1)  /  (8 (x  + x 2    + 1))
                      /

> simplify(%);
memory used=5.2MB, alloc=41.3MB, time=0.11
                                 4
                                x  + 1
                 -------------------------------------
                   2      1/2     2     1/2    2     2
                 (x  + x 2    + 1)  (x 2    - x  - 1)

> normal(%,expanded);
                                   1
                                 ------
                                  4
                                 x  + 1

There are two parts to the history:

  1. Session history consists of the commands you've entered in the current session.
  2. Command history consists of all commands from previous sessions, together with the session history (up to a maxium specified by interface(historysize), which is 1000 lines by default).

History meta-commands all begin with two exclamation marks at the beginning of the line. The !!= meta-command lists the session history:

> !!=
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

Notice that each displayed line of the history is preceeded by << to differentiate it from Maple input and other Maple output. Each meta-command also has a long form. The long form for !!= is !!list:

> !!list
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

Most meta-commands can take an argument specifying the amount of history to be affected. For example, a numeric argument refers to the last lines of the command history. Here, !!=8 includes the 3 lines from the previous history, as well as the 5 lines of session history:

> !!=8
<< p1 := plot(sin,color="DeepPink"):
<< p2 := plot(cos,color="DodgerBlue"):
<< plots[display](p1,p2);
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

An argument beginning with a forward slash ("/") refers to all history lines beginning with the most recent one that contains the text entered after the slash (if the text to be searched for does not begin with a space or a digit, the slash can be omitted):

> !!=/diff
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

We'll quit Maple and start a new session to illustrate another aspect of the history meta-commands.

> quit
memory used=6.4MB, alloc=41.3MB, time=0.14

In the new session, the meta-command !!=/x^4 will list everyting from the previous session starting from the most recent matching line:

    |\^/|     Maple 2018.1 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2018
 \  MAPLE  /  All rights reserved. Maple is a trademark of
 <____ ____>  Waterloo Maple Inc.
      |       Type ? for help.
> !!=/x^4
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

If a !!= meta-command with a numeric or search argument is executed as the first command in a fresh session, not only is the specified amount of command history listed, but that part of the history is then considered to be session history. Executing just !!= with no argument shows that this is the case:

> !!=
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

The !!! meta-command (long form !!play) re-executes the entire session history:

> !!!
>> y := 1/(x^4+1);
                                     1
                              y := ------
                                    4
                                   x  + 1

>> int(y,x);
     1/2           1/2             1/2           1/2
1/4 2    arctan(x 2    + 1) + 1/4 2    arctan(x 2    - 1)

                    2      1/2
            1/2    x  + x 2    + 1
     + 1/8 2    ln(---------------)
                    2      1/2
                   x  - x 2    + 1

>> diff(%,x);
          1                       1              1/2
--------------------- + --------------------- + 2
       1/2     2               1/2     2
2 ((x 2    + 1)  + 1)   2 ((x 2    - 1)  + 1)

    /         1/2        2      1/2              1/2 \
    |  2 x + 2         (x  + x 2    + 1) (2 x - 2   )|
    |--------------- - ------------------------------|
    | 2      1/2               2      1/2     2      |
    \x  - x 2    + 1         (x  - x 2    + 1)       /

      2      1/2        /      2      1/2
    (x  - x 2    + 1)  /  (8 (x  + x 2    + 1))
                      /

>> simplify(%);
memory used=5.2MB, alloc=41.3MB, time=0.10
                                 4
                                x  + 1
                 -------------------------------------
                   2      1/2     2     1/2    2     2
                 (x  + x 2    + 1)  (x 2    - x  - 1)

>> normal(%,expanded);
                                   1
                                 ------
                                  4
                                 x  + 1

Notice that each command is displayed with a >> prompt. This prompt is used whenever a command in the history is being played back.

History commands can be played back one step at a time using the !!. (or !!step) meta-command. Each command is displayed with the >> prompt, and you are given the opportunity to edit it before pressing Enter to execute it. Here, we've changed the exponent of from 4 to 3 before pressing Enter, and then pressed Enter four more times to re-execute the remaining commands:

> !!.
>> y := 1/(x^3+1);
                                     1
                              y := ------
                                    3
                                   x  + 1

>> int(y,x);
                                                                  1/2
                          2                 1/2        (2 x - 1) 3
  1/3 ln(x + 1) - 1/6 ln(x  - x + 1) + 1/3 3    arctan(--------------)
                                                             3

>> diff(%,x);
                1          2 x - 1               2
            --------- - -------------- + ------------------
            3 (x + 1)       2              /         2    \
                        6 (x  - x + 1)     |(2 x - 1)     |
                                         3 |---------- + 1|
                                           \    3         /

>> simplify(%);
                                   1
                          --------------------
                                    2
                          (x + 1) (x  - x + 1)

>> normal(%,expanded);
                                   1
                                 ------
                                  3
                                 x  + 1

When editing a command prefixed with the >> prompt, the command is edited in-place in the history. In other words, the history is permanently modified. History meta-commands are not played back (or infinite loops could result), and lines containing only comments are displayed but not offered for editing.

Still in the same session, let's use !!. with a search pattern to generate and display the plots from the older history, pressing Enter after each command:

> !!./p1 :=
>> p1 := plot(sin,color="DeepPink"):
>> p2 := plot(cos,color="DodgerBlue"):
>> plots[display](p1,p2);

                                                                        
                                    |                                   
 -*\    /*-*                      1-+*     *-*\                     /*- 
   \   //  \\                    // |\\   /   \                     /   
    \  /    \\                   /  | \  //    \                   /    
    \ /      *                  *   |  * *     \\                  /    
     **      ||                 |   |  |*       *                 *     
     *|       |                |    |   *       ||               ||     
     ||       ||               |    |  |*        |               |      
    |||        |              |     |  | |       |               |      
    | ||       |              |     | || |        |             ||      
    |  |       ||            || 0.5 | |  |        |             |       
   |   |        |            |      | |   |       |             |       
   |   ||       |            |      |||   |        |           ||       
   |    |       ||           |      ||    |        |           |        
  |     |        |          |       ||     |       |           |        
  |     ||       |          |       ||     |        |         ||        
  |      |       ||         |       |      |        |         |         
 ||      |        |        |        |       |       |         |         
 |       ||       |        |        |       |       ||       ||         
 +--------+-------++-------+-------++-------+--------+-------+--------+ 
  -6      |  -4    |    -2|       0||        |2      |   4   |      6|| 
          ||       |      |        ||        |       ||     ||       |  
           |       |      |       | |        |        |     |        |  
           |        |    ||       | |        ||       |     |       ||  
           |        |    |        | |         |       ||    |       |   
            |       |    |       || |         |        |   |        |   
            |        |  ||       |  |         ||       |   |        |   
            |        |  |      -0.5 |          |       ||  |       |    
             |       |  |       ||  |          |        | |        |    
             |        |||       |   |          ||       | |       ||    
             ||       ||        |   |           |       |*|       |     
              |       **       |    |           |        *        |     
              |*      |*       |    |            |      **|      *      
               \     /*\\     *     |            *\     / *      /      
               \\    /  \    //     |             \    // \\    /       
                \\  //   \   /      |             \\   /   \\  //       
                 *-*/    \*-*    -1 |              \*-*     *-*/        
                                    |                                   
                                                                        

>> !! y := 1/(x^3+1);
> _

On the last line above, we typed !! followed by a space (long form !!stop) to tell Maple to stop playing back commands and give a fresh prompt. If we now issue !!=, we see that the session history still contains only the five commands it contained originally. The played back commands were not appended to the history:

> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);

Issuing a new command at the regular > prompt adds it to the end of the session history:

> sin(Pi/2) + 1;
                                   2

> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
<< sin(Pi/2) + 1;

The !!- (or !!drop) meta-command deletes one or more commands from the history:

> !!-
> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
> !!-/simpl
> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);

In addition to the meta-commands described above for manipulating the history within a session, there are three meta-commands to copy history to and from files:

  • !!> (or !!save ) writes the session history to the specified file.
  • !!< (or !!read ) replaces the session history with the contents of the specified file.
  • !!+ (or !!append ) appends the contents of the specified file to the session history.

Used together, these meta-commands let you save important sessions for later examination or reuse without relying on the .maple_history file, or let you make large changes to the current session history using your favorite text editor.

The !!? (or !!help) meta-command displays a short summary of all the meta-commands:

> !!?

The sequence "!!" at the beginning of a line introduces a history meta-command.
Each has a short or long form (shown below in parentheses). The long form name
may be used instead of the single character appearing after "!!".

!!=specifier    - list session history or specified lines (list)
!!!specifier    - play back session history or specified lines (play)
!!.specifier    - step through session history or specified lines (step)
!!-specifier    - drop one or specified lines from history (drop)
!!<filename     - read file into session history (read)
!!+filename     - append file to session history (append)
!!>filename     - save session history to file (save)
!!?             - help for history meta-commands (help)
!!              - stop the single-step playback in progress (stop)

The 'specifier' is optional. If present, it can be an integer, N, referring to
the N most recent command lines, or a string beginning with a "/" character,
referring to the lines from the most recent one containing that string. The "/"
may be omitted if the search string does not begin with a space or digit.

Detailed information can be found in the ?edit_history help page.

The Maple command line interface (cmaple), often referred to as the "TTY interface" for its original use on Teletype terminals, is still the tool of choice for many Maple developers and power users. Maple 2018.1 introduces several new capabilities to this long-lived interface:

This post, the first in a series of three, will address color syntax highlighting. We'll start with a very short sample session:

    |\^/|     Maple 2018.1 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2018
 \  MAPLE  /  All rights reserved. Maple is a trademark of
 <____ ____>  Waterloo Maple Inc.
      |       Type ? for help.
> piecewise(4 < x^2 and x < 8, f(x));
                            {                  2
                            { f(x)        4 < x  and x < 8
                            {
                            {  0             otherwise

> p := unapply(%,x);
                                               2
                      p := x -> piecewise(4 < x  and x < 8, f(x))

> 1/p(1);
Error, numeric exception: division by zero
> quit
memory used=5.3MB, alloc=41.3MB, time=0.07

In the above example, you can see that general keywords are in bold blue, variables in italics (not supported by all terminals), error messages in bold red, control flow interrupting keywords in bold magenta, and memory usage messages in normal blue.

Color syntax highlighting is turned on by default in cmaple for Linux and OS/X if the terminal you are using (as specified by the TERM environment variable) is known to support it. It is currently turned off by default under Windows. It can be explicitly turned on or off for 2D and message output using interface(ansi=) where is true or false (under Windows, you can put interface(ansi=true) in your maple.ini file to automatically turn it on). Likewise, interface(ansilprint=) controls highlighting for 1D output (such as that produced by lprint), and interface(ansiedit=) for input.

Not all terminals support all possible highlighting modes. The following two commands show what colors your terminal can display, and how they are used by Maple's syntax highlighting:

> interface(showtermcolors):

ANSI X3.64 Standard Attributes

Normal  Bold  Italic  Underlined  Reverse  

System Colors (0-15) Using ANSI Escape Sequences

Color00  Color01  Color02  Color03  Color04  Color05  Color06  Color07  
Color08  Color09  Color10  Color11  Color12  Color13  Color14  Color15  

System Colors (0-15) Using Extended Escape Sequences

  0    0    1    1    2    2    3    3    4    4    5    5    6    6    7    7  
  8    8    9    9   10   10   11   11   12   12   13   13   14   14   15   15  

Extended 6x6x6 Color Cube (16-231)

 16   16   17   17   18   18   19   19   20   20   21   21  
 22   22   23   23   24   24   25   25   26   26   27   27  
 28   28   29   29   30   30   31   31   32   32   33   33  
 34   34   35   35   36   36   37   37   38   38   39   39  
 40   40   41   41   42   42   43   43   44   44   45   45  
 46   46   47   47   48   48   49   49   50   50   51   51  

 52   52   53   53   54   54   55   55   56   56   57   57  
 58   58   59   59   60   60   61   61   62   62   63   63  
 64   64   65   65   66   66   67   67   68   68   69   69  
 70   70   71   71   72   72   73   73   74   74   75   75  
 76   76   77   77   78   78   79   79   80   80   81   81  
 82   82   83   83   84   84   85   85   86   86   87   87  

 88   88   89   89   90   90   91   91   92   92   93   93  
 94   94   95   95   96   96   97   97   98   98   99   99  
100  100  101  101  102  102  103  103  104  104  105  105  
106  106  107  107  108  108  109  109  110  110  111  111  
112  112  113  113  114  114  115  115  116  116  117  117  
118  118  119  119  120  120  121  121  122  122  123  123  

124  124  125  125  126  126  127  127  128  128  129  129  
130  130  131  131  132  132  133  133  134  134  135  135  
136  136  137  137  138  138  139  139  140  140  141  141  
142  142  143  143  144  144  145  145  146  146  147  147  
148  148  149  149  150  150  151  151  152  152  153  153  
154  154  155  155  156  156  157  157  158  158  159  159  

160  160  161  161  162  162  163  163  164  164  165  165  
166  166  167  167  168  168  169  169  170  170  171  171  
172  172  173  173  174  174  175  175  176  176  177  177  
178  178  179  179  180  180  181  181  182  182  183  183  
184  184  185  185  186  186  187  187  188  188  189  189  
190  190  191  191  192  192  193  193  194  194  195  195  

196  196  197  197  198  198  199  199  200  200  201  201  
202  202  203  203  204  204  205  205  206  206  207  207  
208  208  209  209  210  210  211  211  212  212  213  213  
214  214  215  215  216  216  217  217  218  218  219  219  
220  220  221  221  222  222  223  223  224  224  225  225  
226  226  227  227  228  228  229  229  230  230  231  231  

Extended 24-Level Grayscale (232-255)

232  232  233  233  234  234  235  235  236  236  237  237  238  238  239  239  
240  240  241  241  242  242  243  243  244  244  245  245  246  246  247  247  
248  248  249  249  250  250  251  251  252  252  253  253  254  254  255  255  

If your terminal does not support 256 color mode, then many of the colored blocks shown above will appear differently or not at all.

> interface(showcolors):

 1 Normal output:           evalf(1/2) = 0.5
 2 Italics (variables):     x, y, z
 3 Symbol text (not used):  symbol
 4 Bold (fallback):         Begin, be bold, and venture to be wise.
 5 Underlined (fallback):   Morality, like art, means drawing a line someplace.
 6 Reversed (not used):      The reverse side also has a reverse side. 
 7 Input prompts:           >  DBG>
 8 User input:              1/(x^4+1);
 9 Userinfo output:         message, x, y
10 Trace output:            {--> enter f, args = x, y
11 Warning messages:        Warning, x is implicitly declared local
12 Error messages:          Error, (in f) invalid subscript selector
13 Debugger output:         No breakpoints set
14 General Maple keywords:  for  from  to  while  do  until
15 Declaration keywords:    local  option  description
16 Flow interruptions:      break  return
17 Exception keywords:      error  try  catch
18 Subexpression labels:    %1  %2
19 Special & quoted names:  thisproc  `diff/sin`
20 String literals:         "Hello, world!"
21 Maple startup message:   Maple 2019
22 Output from printf:      x=1.234 y=5.678
23 Status messages:         memory used=1.7MB, alloc=8.3MB, time=0.03
24 System command output:   1466  4739  43140  myprog.mpl
25 Maple comments:          # Comments are free but facts are sacred.

The colors used for the different categories of output as listed by the command above are user selectable. The default is to use only the sixteen ANSI X3.64 standard colors (or Windows command prompt standard colors). These may appear differently than shown here depending on the color palette of your terminal window.

The color settings can be queried or set as follows:

> currentColors := interface(ansicolor);
currentColors := [-1, -1, -1, -1, -1, -1, 2, -1, 2, 3, 11, 9, 6, 12, 10, 13, 9, 14, 6,

    5, 2, 136, 4, 134, 3]

# Individual colours, as numbered in the output of interface(showcolors), can
# be changed. Let's make keywords bright yellow:
> myColors := subsop(14=226,currentColors);
myColors := [-1, -1, -1, -1, -1, -1, 2, -1, 2, 3, 11, 9, 6, 226, 10, 13, 9, 14, 6, 5,

    2, 136, 4, 134, 3]

> interface(ansicolor=myColors);
[-1, -1, -1, -1, -1, -1, 2, -1, 2, 3, 11, 9, 6, 12, 10, 13, 9, 14, 6, 5, 2, 136, 4,

    134, 3]

> piecewise(4 < x^2 and x < 8, f(x));
                            {                  2
                            { f(x)        4 < x  and x < 8
                            {
                            {  0             otherwise

There are several predefined color schemes that can be selected using interface(ansicolor=), where is an integer from 0 to 6. Scheme 0, the default, should work on any terminal. Of the remaining schemes, the odd numbered ones are designed to look good on light backgrounds, and the even numbered ones on dark backgrounds.

There is also a new character plot driver, selectable using interface(plotdevice=colorchar), which supports character plotting in color. Colors are mapped to the nearest color supported by the terminal:

> interface(plotdevice=colorchar):
> p1 := plot(sin(x),x=-Pi..Pi,thickness=1,color="DeepPink"):
> p2 := plot(sin(x)+sin(3*x)/3,x=-Pi..Pi,thickness=2,color="LawnGreen"):
> p3 := plot(sin(x)+sin(5*x)/5,x=-Pi..Pi,thickness=3,color="DodgerBlue"):
> plots[display](p1,p2,p3);

                                                                                       
                                           |                                           
                                           |                  @@@@@                    
                                           |                 @@   @@                   
                                           |                 @     @                   
                                         1 |                *.......*                  
                                           |       *******.*@       @*..******         
                                           |      **    .**@         @**.    **        
                                           |     **   ..  ***       ***  ..   **       
                                           |    **   ..  @@ **** **** @@  ..   **      
                                           |    *   ..  @@     ***     @@  ..   *      
                                           |   *@@@**@@@                 @@@**@@@*     
                                       0.5 |  **@ ..                         .. @**    
                                           |  *@ ..                           .. @*    
                                           | ** .                               . **   
                                           | * .                                 . *   
                                           |**..                                 ..**  
                                           |*..                                   ..*  
                                           |*.                                     .*  
                                           *.                                       .* 
 **---------------------------------------**-----------------------------------------* 
  -3           -2            -1          0*|            1             2            3   
  *..                                   ..*|                                           
  **..                                 ..**|                                           
   * .                                 . * |                                           
   ** ..                              . ** |                                           
    *@ ..                           .. @*  |                                           
    **@ ..                         .. -0.5 |                                           
     *@@@**@@@                 @@@**@@@*   |                                           
      *   ..  @@     ***     @@  ..   *    |                                           
      **   ..  @@ **** **** @@  ..   **    |                                           
       **   ..  @**       **@  ..   **     |                                           
        **    .**@         @**.    **      |                                           
         *******.*@       @*.*******       |                                           
                  *.......*             -1 |                                           
                   @     @                 |                                           
                   @@   @@                 |                                           
                    @@@@@                  |                                           
                                           |                                           
                                                                                       

> plot3d([1,x,y],x=0..2*Pi,y=0..2*Pi,coords=toroidal(10));

                                                                                       
                                  -------------------                                  
                             --------\\\-|-|-|-///--------                             
                          ---------\\\-\-|-|-|-/-///---------                          
                        ----------\\\-\-||-|-||-/-///----------                        
                      /-/-/-/-----\\\\\-|||||-|-/////------------                      
                     -//-/-/-/---\-\\-\||-|||-||/-//-/---\-\-\-\--                     
                   --//-|||-/-/--\\\\\\|||||||-|//////--\-\-|||\|\-\                   
                  /---||-||||-|--\\-\\\|||||||||///-//--|-||||--|---\                  
                 /--|--\-\-\-\\--\\/\\|||||||||||//-//-|-///-/-/--|--\                 
                //-//--\\-\-----\-\\\\|||||||||||////-///////-//--\\-\\                
                |//\/\------------\\\|||||||||||||////-------//--/\\\\|                
               /// / / -- /--- -----\|||||||||||||/---------\ --/\ \\\\\               
               //\/ /-//-/-//-//-/--/-/--/-|-\--\-\--\-\\-\\-\-\\/\ \/\\               
               ///\/ /--/ //-// /--/--/-|--|--|-\--\--\ \\-\\ \--\ \/\\\               
               /||/-// /--/  /--/-//-|  |  |  |  |-\\-\--\  \\-\ \\/\||\               
               || |-/ /  /--/  /  | -|--|--|--|--|- |  \ -\--\  \ \/| |||              
              |||| | -/ /  ||--/-||- |  |  |  |  | -||--\  | \\-\- | |/|               
               |||-| | -/--|  |  |  -|--|--|--|---|  |  |  |--\  | |/|||               
               |\| |-|  | ||--|- |  |   |  |  |   |  | -|--|| |  |-- |/|               
               |\||  |-|  |  |  -|--|---|--|---|--|--|-  |  |  |-|  ||/|               
               \|-| |  |--|- |   |  |  |   |   |  |  |   | -|--| |  |/|/               
                \||-|- |  |--|--|-  |  |   |   |  |  -|--|--|  | -|-||/                
                \|-|||-|- |  |  | --|--|---|---|--|-- |  |  |  |-|||/|/                
                 \\|-| |--|--|  |   |  |   |   |  |   |  |--|--| |-|//                 
                  \\ |-|  |  |--|---|  |   |   |  |---|--|  |  |-| //                  
                   -\| |--|  |  |   |--|---|---|--|   |  |  |--| |//                   
                     \|-| |--|--|   |  |   |   |  |   |--|--| |-|/                     
                      \\|\|| |  |---|--|---|---|--|---|  | ||-|//                      
                        -\----|--|  |  |   |   |  |  |--|----/-                        
                          --\-\\ |--|--||--|---|--|--|  /-/--                          
                             ----\\-||--|--|--|---|--///--                             
                                  \--\--|--|--|--/--/                                  
                                                                                       

For more details, please refer to the help page, ?ansicolor.

1 2 Page 1 of 2