Product Tips & Techniques

Tips and Tricks on how to get the most about Maple and MapleSim

Let us consider

plots:-inequal(max(1, min(x, 2))+max(1, min(y, 2)) <= 3, x = -4 .. 4, y = -4 .. 4);


and compare it with

plots:-implicitplot(max(1, min(x, 2))+max(1, min(y, 2)) = 3, x = -4 .. 4, y = -4 .. 4, gridrefine = 2);

The latter plot must be a subset of the former plot, but it isn't so. bug_in_inequal.mw

Maple users frequently solve differential equations. If you want to use the results later in Maple, you need to deconstruct the solution, and then assign the functions -- something that isn't done automatically in Maple. We wrote a multi-purpose routine to help you out. For instance, suppose you solve a simple linear system of equations:

restart;

eqs := { x + y = 3, x - y = 1 };
soln := solve( eqs ); # { x = 2, y = 1 }
x, y; # plain x and y

To assign the values from the solution to the corresponding variables:

assign( soln );
x, y; # 2, 1

This won't work for solutions of differential equations:

restart;

sys := { D(x)(t) = y(t), D(y)(t) = -x(t), x(0) = 1, y(0) = 0 };
soln := dsolve( sys ); # { x(t) = cos(t), y(t) = -sin(t) }
assign( soln );
x(s), y(s); # plain x(s) and y(s)

To make this work, we wrote this multi-purpose routine:

restart;

# Type for a variable expression, e.g. x=5.
TypeTools:-AddType( 'varexpr', u -> type( u, 'And'('name','Non'('constant'))='algebraic' ) ):

# Type for a function expression, e.g. f(x)=x^2.
TypeTools:-AddType( 'funcexpr', u -> type( u, 'function'('And'('name','Non'('constant')))='algebraic' ) ):

# Procedure to assign variable and function expressions.
my_assign := proc( u :: {
        varexpr, 'list'(varexpr), 'rtable'(varexpr), 'set'(varexpr),
        funcexpr, 'list'(funcexpr), 'rtable'(funcexpr), 'set'(funcexpr)
}, $ )

        local F, L, R, V:       

        # Map the procedure if input is a data container, or apply regular assign(), where applicable.
        if not u :: {'varexpr','funcexpr'} then
               map( procname, u ):
               return NULL:
        elif u :: 'varexpr' then
               assign( u ):
               return NULL:
        end if:       

        L, R := lhs(u), rhs(u):
        F := op(0,L): 
        V := [ indets( L, 'And'( 'name', 'Non'('constant') ) )[] ]:    

        map( assign, F, unapply( R, V ) ):
        return NULL:

end proc:

# Example 1.

eqs := { x + y = 3, x - y = 1 };
my_assign( solve( eqs ) );
'x' = x, 'y' = y; # x=1, y=2

# Example 2.

unassign( 'x', 'y' ):
E := [ f(x,y) = x + y, g(x,y) = x - y ];
my_assign( E );
'f(u,v)' = f(u,v), 'g(u,v)' = g(u,v); # f(u,v)=u+v, g(u,v)=u-v

# Example 3.

sys := { D(x)(t) = y(t), D(y)(t) = -x(t), x(0) = 1, y(0) = 0 };
soln := dsolve( sys );
my_assign( soln ):
'x(s)' = x(s); # x(s)=cos(s)
'y(s)' = y(s); # y(s)=-sin(s)

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 interface(ansi) is set to true and interface(plotdevice) is set to colorchar:

> interface(plotdevice=colorchar):
> Preview(img);
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@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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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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.

Hi MaplePrimes Users!

It’s your friendly, neighborhood tech support team; here to share some tips and tricks from issues we help users with on a daily basis.

A customer contacted us through a Help Page feedback form, asking how to add a row or column in a Matrix. The form came from the Row Operations help page, but the wording of the message suggested that the customer actually wanted to insert a new row or column altogether. Such manipulations can often be accomplished by a command in the ArrayTools package, but the only Insert command currently available is the one for Vectors and 1-D Arrays. Using the Concatenate command from that package, and various commands from the LinearAlgebra package (including the SubMatrix command), we were able to write two custom procedures to perform these manipulations:

InsertRow := proc (A::rtable, n::integer, v::Vector[row])
    local R, C, top, bottom;
    uses LinearAlgebra;
    R := RowDimension(A); C := ColumnDimension(A);
    top := SubMatrix(A, [1 .. n-1], [1 .. C]);
    bottom := SubMatrix(A, [n .. R], [1 .. C]);
    return ArrayTools:-Concatenate(1, top, v, bottom);
end proc:

InsertColumn := proc (A::rtable, n::integer, v::Vector[column])
    local R, C, left, right;
    uses LinearAlgebra;
    R := RowDimension(A); C := ColumnDimension(A);
    left := SubMatrix(A, [1 .. R], [1 .. n-1]);
    right := SubMatrix(A, [1 .. R], [n .. C]);
    return ArrayTools:-Concatenate(2, left, v, right)
end proc:

# test cases:

M := Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]):
v := Vector[row]([2, 2, 2]):
v2 := Vector[column]([2, 2, 2]):
seq(InsertRow(M, i, v), i = 1 .. 4);
seq(InsertColumn(M, i, v2), i = 1 .. 4);

We then reworked this problem using some handy indexing and construction notation that allows our previous procedures to save on the variable constructions and syntax:

InsertRow := proc( A :: rtable, V :: Vector[row], r :: posint )
    return < A[1..r-1,..]; V; A[r..-1,..] >:
end proc:

InsertColumn := proc( A :: rtable, V :: Vector[column], c :: posint )
    return < A[..,1..c-1] | V | A[..,c..-1] >:
end proc:

M := Matrix(3, 3, [seq(i, i = 1 .. 9)]);
A := convert(M, Array);
U := Vector[row]( [ a, b, c ] );
V := convert( U, 'Vector[column]' );
seq(InsertRow( A, U, i ), i=1..4);
seq(InsertColumn( A, V, i ), i=1..4);
seq(InsertRow( M, U, i ), i=1..4);
seq(InsertColumn( M, V, i ), i=1..4);

We have released an update to Maple, Maple 2018.1. This release provides enhancements to the mathematical computation engine, including physics and DEs.  It also provides substantial improvements to the command line version, easier access to group management tools in the MapleCloud, and a few other interface improvements.

This update is available through Tools>Check for Updates in Maple, and is also available from our website on the Maple 2018.1 download page, where you can also find more details.

There is a bug in inttrans:-hilbert:

restart;

inttrans:-hilbert(sin(a)*sin(t+b), t, s);
# should be:
sin(a)*cos(s+b);   expand(%);

sin(a)*cos(s)

 

sin(a)*cos(s+b)

 

sin(a)*cos(s)*cos(b)-sin(a)*sin(s)*sin(b)

(1)

########## correction ##############

`inttrans/expandc` := proc(expr, t)
local xpr, j, econst, op1, op2;
      xpr := expr;      
      for j in indets(xpr,specfunc(`+`,exp)) do
          econst := select(type,op(j),('freeof')(t));
          if 0 < nops(econst) and econst <> 0 then
              xpr := subs(j = ('exp')(econst)*combine(j/('exp')(econst),exp),xpr)
          end if
      end do;
      for j in indets(xpr,{('cos')(linear(t)), ('sin')(linear(t))}) do
          if type(op(j),`+`) then
              op1:=select(has, op(j),t); ##
              op2:=op(j)-op1;            ##
              #op1 := op(1,op(j));
              #op2 := op(2,op(j));
              if op(0,j) = sin then
                  xpr := subs(j = cos(op2)*sin(op1)+sin(op2)*cos(op1),xpr)
              else
                  xpr := subs(j = cos(op1)*cos(op2)-sin(op1)*sin(op2),xpr)
              end if
          end if
      end do;
      return xpr
end proc:

#######################################

inttrans:-hilbert(sin(a)*sin(t+b), t, s); expand(%);

-(1/2)*cos(a-b)*sin(s)+(1/2)*sin(a-b)*cos(s)+(1/2)*cos(a+b)*sin(s)+(1/2)*sin(a+b)*cos(s)

 

sin(a)*cos(s)*cos(b)-sin(a)*sin(s)*sin(b)

(2)

 


Download hilbert.mw

 


Minimum:
1. Maple Tour
2. Maple Quick Start
3. Quick Help
4. Quick Reference Card
5. Math Apps
6. Plotting Guide
...
https://drive.google.com/file/d/1ZAlFQ8_MbuKNsr2PDIyEHmIoogKInBmE/view?usp=sharing

Is that only for my students?

In worksheets:
https://drive.google.com/file/d/1cfA8WKPXSQQxJQR7KbyYHBY6g4OjHaSn/view?usp=sharing

This is about the recent implementation of tensor products of quantum state spaces in the Physics package, in connection with an exchange with the Physics of Information Lab of the University of Waterloo. As usual this development is available to everybody from the Maplesoft R&D Physics webpage. This is the last update for Maple 2017. The updates for Maple 2018, starting with this same material, will begin being distributed through the MapleCloud next week.

Tensor Product of Quantum State Spaces

 

Basic ideas and design

 

 

Suppose A and B are quantum operators and Ket(A, n), et(B, m) are, respectively, their eigenkets. The following works since the introduction of the Physics package in Maple

with(Physics)

Setup(op = {A, B})

`* Partial match of  'op' against keyword 'quantumoperators'`

 

[quantumoperators = {A, B}]

(1)

A*Ket(A, alpha) = A.Ket(A, alpha)

Physics:-`*`(A, Physics:-Ket(A, alpha)) = alpha*Physics:-Ket(A, alpha)

(2)

B*Ket(B, beta) = B.Ket(B, beta)

Physics:-`*`(B, Physics:-Ket(B, beta)) = beta*Physics:-Ket(B, beta)

(3)

where on the left-hand sides the product operator `*` is used as a sort of inert form (it has all the correct mathematical properties but does not perform the contraction) of the dot product operator `.`, used on the right-hand sides.

 

Suppose now that A and B act on different, disjointed, Hilbert spaces.

 

1) To represent that, a new keyword in Setup , is introduced, to indicate which spaces are disjointed, say as in disjointedhilbertspaces = {A, B}.  We want this notation to pop up at some point as {`&Hscr;`[A], `&Hscr;`[B]} where the indexation indicates all the operators acting on that Hilbert space. The disjointedspaces keyword has for synonyms disjointedhilbertspaces and hilbertspaces. The display `&Hscr;`[A] is not yet implemented.

 

NOTE: noncommutative quantum operators acting on disjointed spaces commute between themselves, so after setting  - for instance - disjointedspaces = {A, B}, automatically, A, B become quantum operators satisfying (see comment (ii) on page 156 of ref. [1])

 

"[A,B][-]=0"

 

2) Product of Kets and Bras (KK, BB, KB and BK) where K and B belong to disjointed spaces, are understood as tensor products satisfying, for instance with disjointed spaces A and B (see footnote on page 154 of ref. [1]),

 

`&otimes;`(Ket(A, alpha), Ket(B, beta)) = `&otimes;`(Ket(B, beta), Ket(A, alpha)) 

 

`&otimes;`(Bra(A, alpha), Ket(B, beta)) = `&otimes;`(Ket(B, beta), Bra(A, alpha)) 

 

while of course

Bra(A, alpha)*Ket(A, alpha) <> Bra(A, alpha)*Ket(A, alpha)

 

Details

   

 

3) All the operators of one disjointed space act transparently over operators, Bras and Kets of the other disjointed spaces, for example

 

A*Ket(B, n) = A*Ket(B, n)

and the same for the Dagger of this equation, that is

Bra(B, n)*Dagger(A) = Bra(B, n)*Dagger(A)

 

And this happens automatically. Hence, when we write the left-hand sides and press enter, they are automatically rewritten (returned) as the right-hand sides.

 

Note that for the product of an operator times a Bra or a Ket we are not using the notation that expresses the product with the symbol 5.

 

Regarding the display of Bras and Kets and their tensor products, two enhancements are happening:

 

• 

A new Setup option hideketlabel makes all the labels in Kets and Bras to be hidden when displaying Kets, Bras and Bracket(s), with the indices presented one level up, as if they were a sequence of labels, so that:

 "Ket(A,m,n,l"  

is displayed as

Ket(A, m, n, l)

 

  

This is the notation used more frequently when working in quantum information. This hideketlabel option is already implemented entering Setup(hideketlabel = true)

• 

Tensor products formed with operators, or Bras and Kets, that belong to disjointed spaces (set as such using Setup ), are displayed with the symbol 5 in between, as in Ket(A, n)*Ket(B, n) instead of Ket(A, n)*Ket(B, n), and `&otimes;`(A, B) instead of A*B.

Tensor product notation and the hideketlabel option

   

The implementation of tensor products using `*` and `.`

   

Basic exercising with the new functionality

   

Related functionality already in place before these changes

   

Reference

 

[1] Cohen-Tannoudji, Diue, Laloe, Quantum Mechanics, Chapter 2, section F.

[2] Griffiths Robert B., Hilbert Space Quantum Mechanics, Quantum Computation and Quantum Information Theory Course, Physics Department, Carnegie Mellon University, 2014.

See also

   

 


 

Download TensorProductDesign.mw
 

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

Carmichael's lambda(n) function (as it relates to Euler's Totient Function).....this is just one of 8 stages of animation. 

 

Here's the complete animation with supporting music by the mighty Stormtroopers Of Death....

https://youtu.be/QN-s3EpZICs

 

 

 

 

 

Automatic handling of collision of tensor indices in products

 

 

The design of products of tensorial expressions that have contracted indices got enhanced. The idea: repeated indices in certain subexpressions are actually dummies. So suppose T[a, b] and B[b] are tensors, then in T[trace] = T[a, `~a`], a is just a dummy, therefore T[a, `~a`]*B[a] = T[b, `~b`]*B[a] is a well defined object. The new design automatically maps input like T[a, `~a`]*B[a] into T[b, `~b`]*B[a]. This significantly simplifies the manipulation of indices when working with products of tensorial expressions: each tensorial expression being multiplied has its repeated indices automatically transformed into different ones when they would collide with the free or repeated indices of the other expressions being multiplied.

 

This functionality is available within the Physics update distributed at the Maplesoft R&D Physics webpage (but for what you see under Preview that makes use of a new kernel feature of the Maple version under development).

 

restart

with(Physics); Setup(spacetimeindices = lowercaselatin, quiet)

[spacetimeindices = lowercaselatin]

(1)

Define(T[a, b], B[b])

`Defined objects with tensor properties`

 

{B[b], Physics:-Dgamma[a], Physics:-Psigma[a], T[a, b], Physics:-d_[a], Physics:-g_[a, b], Physics:-KroneckerDelta[a, b], Physics:-LeviCivita[a, b, c, d]}

(2)

This shows the automatic handling of collision of indices

T[a, a]*B[a]

T[b, `~b`]*B[a]

(3)

T[a, a]^2

T[a, `~a`]*T[b, `~b`]

(4)

``

Preview only in the upcomming version under development

 

Consider now the case of three tensors

Define(A[a], C[a])

`Defined objects with tensor properties`

 

{A[a], B[b], C[a], Physics:-Dgamma[a], Physics:-Psigma[a], T[a, b], Physics:-d_[a], Physics:-g_[a, b], Physics:-KroneckerDelta[a, b], Physics:-LeviCivita[a, b, c, d]}

(5)

A[a]*B[a]*C[a]

A[a]*B[a]*C[a]

(6)

The product above has indeed the index a repeated more than once, therefore none of its occurrences got automatically transformed into contravariant in the output, and Check  detects the problem interrupting with an error  message

Check(A[a]*B[a]*C[a])

Error, (in Physics:-Check) wrong use of the summation rule for repeated indices: `a repeated 3 times`, in A[a]*B[a]*C[a]

 

 

However, it is now also possible to indicate, using parenthesis, that the product of two of these tensors actually form a subexpression, so that the following two tensorial expressions are well defined, and the colliding dummy is automatically replaced making that explicit

A[a]*B[a]*C[a]

A[b]*B[`~b`]*C[a]

(7)

A[a]*B[a]*C[a]

A[a]*B[b]*C[`~b`]

(8)

 

 

This change in design makes concretely simpler the use of indices in that it eliminates the need for manually replacing dummies. For example, consider the tensorial expression for the angular momentum in terms of the coordinates and momentum vectors, in 3 dimensions

 

Setup(coordinates = cartesian, dimension = 3, metric = euclidean, quiet)

[coordinatesystems = {X}, dimension = 3, metric = {(1, 1) = 1, (2, 2) = 1, (3, 3) = 1}]

(9)

Define L[j], p[k] respectively representing angular and linear momentum

Define(L[j], p[k])

`Defined objects with tensor properties`

 

{Physics:-Dgamma[a], L[j], Physics:-Psigma[a], Physics:-d_[a], Physics:-g_[a, b], p[k], Physics:-KroneckerDelta[a, b], Physics:-LeviCivita[a, b, c], Physics:-SpaceTimeVector[a](X)}

(10)

Introduce the tensorial expression for L[a]

L[a] = LeviCivita[a, b, c]*X[b]*p[c]

L[a] = Physics:-LeviCivita[a, b, c]*Physics:-SpaceTimeVector[b](X)*p[c]

(11)

The left-hand side has one free index, a, while the right-hand side has two dummy indices b and c

Check(L[a] = Physics[LeviCivita][a, b, c]*Physics[SpaceTimeVector][b](X)*p[c], all)

`The repeated indices per term are: `[{`...`}, {`...`}, `...`]*`; the free indices are: `*{`...`}

 

([{}], {a}) = ([{b, c}], {a})

(12)

If we want to compute`#mrow(msup(mfenced(mover(mi("L"),mo("&rarr;")),open = "&Vert;",close = "&Vert;"),mn("2")),mo("&equals;"),msubsup(mi("L"),mi("a"),mn("2")))`we can now take the square of (11) directly, and the dummy indices on the right-hand side are automatically handled, there is now no need to manually substitute the repeated indices to avoid their collision

(L[a] = Physics[LeviCivita][a, b, c]*Physics[SpaceTimeVector][b](X)*p[c])^2

L[a]^2 = Physics:-LeviCivita[a, b, c]*Physics:-SpaceTimeVector[b](X)*p[c]*Physics:-LeviCivita[a, d, e]*Physics:-SpaceTimeVector[d](X)*p[e]

(13)

The repeated indices on the right-hand side are now a, b, c, d, e

Check(L[a]^2 = Physics[LeviCivita][a, b, c]*Physics[SpaceTimeVector][b](X)*p[c]*Physics[LeviCivita][a, d, e]*Physics[SpaceTimeVector][d](X)*p[e], all)

`The repeated indices per term are: `[{`...`}, {`...`}, `...`]*`; the free indices are: `*{`...`}

 

([{a}], {}) = ([{a, b, c, d, e}], {})

(14)

NULL


 

Download AutomaticHandlingCollisionOfTensorIndices.mw

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

 

Minimize the number of tensor components according to its symmetries
(and relabel, redefine or count the number of independent tensor components)

 

 

The nice development described below is work in collaboration with Pascal Szriftgiser from Laboratoire PhLAM, Université Lille 1, France, used in the Mapleprimes post Magnetic traps in cold-atom physics

 

A new keyword in Define  and Setup : minimizetensorcomponents, allows for automatically minimizing the number of tensor components taking into account the tensor symmetries. For example, if a tensor with two indices in a 4D spacetime is defined as antisymmetric using Define with this new keyword, the number of different tensor components will be exactly 6, and the elements of the diagonal are automatically set equal to 0. After setting this keyword to true with Setup , all subsequent definitions of tensors automatically minimize the number of components while using this keyword with Define  makes this minimization only happen with the tensors being defined in the call to Define .

 

Related to this new functionality, 4 new Library routines were added: MinimizeTensorComponents, NumberOfIndependentTensorComponents, RelabelTensorComponents and RedefineTensorComponents

 

Example:

restart; with(Physics)

 

Define an antisymmetric tensor with two indices

Define(F[mu, nu], antisymmetric)

`Defined objects with tensor properties`

 

{Physics:-Dgamma[mu], F[mu, nu], Physics:-Psigma[mu], Physics:-d_[mu], Physics:-g_[mu, nu], Physics:-KroneckerDelta[mu, nu], Physics:-LeviCivita[alpha, beta, mu, nu]}

(1.1)

Although the system knows that F[mu, nu] is antisymmetric, you need to use Simplify to apply the (anti)symmetry

F[mu, nu]+F[nu, mu]

F[mu, nu]+F[nu, mu]

(1.2)

 

Simplify(F[mu, nu]+F[nu, mu])

0

(1.3)

so by default the components of F[mu, nu] do not automatically reflect the (anti)symmetry; likewise

F[1, 2]+F[2, 1]

F[1, 2]+F[2, 1]

(1.4)

Simplify(F[1, 2]+F[2, 1])

0

(1.5)

and computing the array form of F[mu, nu]we do not see the elements of the diagonal equal to zero nor the lower-left triangle equal to the upper-right triangle but for a different sign:

TensorArray(F[mu, nu])

Matrix(%id = 18446744078270093062)

(1.6)

 

On the other hand, this new functionality, here called minimizetensorcomponents, makes the symmetries of the tensor be explicitly reflected in its components.

 

There are three ways to use it. First, one can minimize the number of tensor components of a tensor previously defined. For example

 

Library:-MinimizeTensorComponents(F)

Matrix(%id = 18446744078270064630)

(1.7)

After this, both (1.2) and (1.3) are automatically equal to 0 without having to use Simplify

F[mu, nu]+F[nu, mu]

0

(1.8)

0

0

(1.9)

And the output of TensorArray  in (1.6) becomes equal to (1.7).

 

NOTE: in addition, after using minimizetensorcomponents in the definition of a tensor, say F, all the keywords implemented for Physics tensors are available for F:

 

F[]

F[mu, nu] = Matrix(%id = 18446744078247910206)

(1.10)

F[trace]

0

(1.11)

F[nonzero]

F[mu, nu] = {(1, 2) = F[1, 2], (1, 3) = F[1, 3], (1, 4) = F[1, 4], (2, 1) = -F[1, 2], (2, 3) = F[2, 3], (2, 4) = F[2, 4], (3, 1) = -F[1, 3], (3, 2) = -F[2, 3], (3, 4) = F[3, 4], (4, 1) = -F[1, 4], (4, 2) = -F[2, 4], (4, 3) = -F[3, 4]}

(1.12)

"F[~1,mu,matrix]"

F[`~1`, mu] = Vector[row](%id = 18446744078247885990)

(1.13)

Alternatively, one can define a tensor, specifying that the symmetries should be taken into account to minimize the number of its components passing the keyword minimizetensorcomponents to Define .

 

Example:

 

Define a tensor with the symmetries of the Riemann  tensor, that is, a tensor of 4 indices that is symmetric with respect to interchanging the positions of the 1st and 2nd pair of indices and antisymmetric with respect to interchanging the position of its 1st and 2nd indices, or 3rd and 4th indices, and define it minimizing the number of tensor components

 

Define(R[alpha, beta, mu, nu], symmetric = {[[1, 2], [3, 4]]}, antisymmetric = {[1, 2], [3, 4]}, minimizetensorcomponents)

`Defined objects with tensor properties`

 

{Physics:-Dgamma[mu], F[mu, nu], Physics:-Psigma[mu], R[mu, nu, alpha, beta], Physics:-d_[mu], Physics:-g_[mu, nu], Physics:-KroneckerDelta[mu, nu], Physics:-LeviCivita[alpha, beta, mu, nu]}

(1.14)

We now have

R[1, 2, 3, 4]+R[2, 1, 3, 4]

0

(1.15)

R[alpha, beta, mu, nu]-R[mu, nu, alpha, beta]

0

(1.16)
• 

One can always retrieve the symmetry properties in the abstract notation used by the Define command using the new Library:-GetTensorSymmetryProperties, its output is ordered, first the symmetric then the antisymmetric properties

 

Library:-GetTensorSymmetryProperties(R)

{[[1, 2], [3, 4]]}, {[1, 2], [3, 4]}

(1.17)
• 

After making the symmetries explicit (and also before that), it is frequently useful to know the number of independent components of a given tensor. For this purpose you can use the new Library:-NumberOfIndependentTensorComponents

 

Library:-NumberOfIndependentTensorComponents(R)

21

(1.18)

and besides taking into account the symmetries, in the case of the Riemann  tensor, after taking into account the first Bianchi identity this number of components is further reduced to 20.

 

A third way of using the new minimizetensorcomponents functionality is using Setup , so that, automatically, every subsequent definition of tensors with symmetries is performed minimizing the number of its components using the indicated symmetries

 

Example:

Setup(minimizetensorcomponents = true)

[minimizetensorcomponents = true]

(1.19)

So from hereafter you can define tensors taking into account their symmetries explicitly and without having to include the keyword minimizetensorcomponents at each definition

 

Define(C[alpha, beta], antisymmetric)

`Defined objects with tensor properties`

 

{C[mu, nu], Physics:-Dgamma[mu], F[mu, nu], Physics:-Psigma[mu], R[mu, nu, alpha, beta], Physics:-d_[mu], Physics:-g_[mu, nu], Physics:-KroneckerDelta[mu, nu], Physics:-LeviCivita[alpha, beta, mu, nu]}

(1.20)

 

C[]

C[mu, nu] = Matrix(%id = 18446744078408747598)

(1.21)
• 

Two new related functionalities are provided via Library:-RelabelTensorComponents and Library:-RedefineTensorComponent, the first one to have the number of tensor components directly reflected in the names of the components, the second one to redefine only one of these components

Library:-RelabelTensorComponents(C)

Matrix(%id = 18446744078408729774)

(1.22)

 

Suppose now we want to make one of these components equal to 1, say C__2

Library:-RedefineTensorComponent(C[1, 2] = 1)

C[mu, nu] = Matrix(%id = 18446744078270104390)

(1.23)

This nice development is work in collaboration with Pascal Szriftgiser from Laboratoire PhLAM, UMR CNRS 8523, Université Lille 1, F-59655, France.

``

 

Download MinimizeTensorComponents.mw

 

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

Lesson_on_functions.mws

As the title says, a lesson on functions:  eg the -> operator, f(2), eval, evalf etc 

This post is devoted to the rigorous proof of Miquel's five circles theorem, which I learned about from this question. The proof is essentially very simple and takes only 15 lines of code. The figure below, in which all the labels coincide with the corresponding names in the code, illustrates the basic ideas of the code. First, we symbolically define common points of intersection of blue circles with a red unit circle  (these parameters  s1 .. s5  are the polar coordinates of these points). All other parameters of this configuration can be expressed through them. Then we find the centers  M  and  N  of two circles. Then we find the coordinates of the point  K  from the condition that  CK  is perpendicular to  MN . Then we find the point  and using the result obtained, we easily find the coordinates  of all the points  A1 .. A5. Then we find the coordinates of the point   P  as the point of intersection of the lines  A1A2  and  A3A4 . Finally, we verify that the point  P  lies on a circle with center at the point  N , which completes the proof.

                      

 

Below - the code of the proof. Note that the code does not use any special (in particular geometric) packages, only commands from the Maple kernel. I usually try any geometric problems to solve in this style, it is more reliable,  and often shorter.

restart;
t1:=s1/2+s2/2: t2:=s2/2+s3/2:
M:=[cos(t1),sin(t1)]: N:=[cos(t2),sin(t2)]:
C:=[cos(s2),sin(s2)]: K:=(1-t)*~M+t*~N:
CK:=K-C: MN:=M-N:
t0:=simplify(solve(CK[1]*MN[1]+CK[2]*MN[2]=0, t)):
E:=combine(simplify(C+2*eval(CK,t=t0))):
s0:=s5-2*Pi: s6:=s1+2*Pi:
assign(seq(A||i=eval(E,[s2=s||i,s1=s||(i-1),s3=s||(i+1)]), i=1..5)):
Dist:=(p,q)->sqrt((p[1]-q[1])^2+(p[2]-q[2])^2):
LineEq:=(P,Q)->(y-P[2])*(Q[1]-P[1])=(x-P[1])*(Q[2]-P[2]):
Line1:=LineEq(A1,A2):
Line2:=LineEq(A3,A4):
P:=combine(simplify(solve({Line1,Line2},[x,y])))[]:
Circle:=(x-N[1])^2+(y-N[2])^2-Dist(N,C)^2:
is(eval(Circle, P)=0);  
# The final result

                                                                    true


It may seem that this proof is easy to repeat manually. But this is not so. Maple brilliantly coped with very cumbersome trigonometric transformations. Look at the coordinates of point  , expressed through the initial parameters  s1 .. s5 :

simplify(eval([x,y], P));  # The coordinates of the point  P

  

  

 

ProofMiquel.mw

First 11 12 13 14 15 16 17 Last Page 13 of 64