## Gems from the Little Red Book of Maple Magic

Maple

Since coming to Maplesoft in 2003, I've kept a notebook of "gems" I've gleaned from consulting with the programmers in the building. I call it my "Little Red Book of Maple Magic." It really is red. The first spiral-bound notebook was little, and it was red. When it overflowed, I moved the notes to a red ring-binder. But it's not so little any more.

I thought I'd share a few recently recorded bits of this Maple magic. An earlier version of these remarks appears in the Tips & Techniques column in the January issue of the Maple Reporter. The present version includes some related Red Book gems I found after writing the first version.

Gem 1 Just the other day I needed to add a math expression to a graph that was to appear in a manual I'm updating. The code for the graph is in a table, and via the table-property dialog, the code can be hidden, but the graph displayed. This makes for an easy update of the graph in any future revision.

The math expression was , which I added to my graph with the command from the plots package. In particular, I used  and the Display command to merge the text onto the graph. Unfortunately, Maple immediately changes the expression to and adds that to the graph.

OK, how do I prevent Maple from evaluating the expression? The suggestion I got was to convert the expression to an Atomic Identifier. This works, but interactively, and only in math mode.

But looking through my Little Red Book, I noted a similar problem that had been recorded earlier. I wanted the legend on the graph of an inverse function to show . I tried using the plot option , but Maple again evaluated the expression and displayed .

The Atomic Identifier technique works here, too, but I wanted to avoid math-mode input and the use of the mouse to create the Atomic Identifier. So now, the advice I got was the more complicated solution shown in Table 1.

 > qx := Typesetting:-Typeset(q(x)): fnx := subs(Typesetting:-Typeset(q) = Typesetting:-Typeset(f^n),qx): f1x := subs(Typesetting:-Typeset(n) = Typesetting:-Typeset(-1),fnx): plot(x, x=0..1, legend = (f1x), color=black);

Table 1   Using text input to display in the legend on a graph

Looking back to some very early pages in the Little Red Book, I then noticed I had recorded a way to add to the caption for a graph. The recorded magic: caption=typeset(mrow(mi("x"),mo("&pm;"),mfrac(mn("1"),mn("2")))).

So now I wanted to know if I could generate the same kind of string for the first two examples. But how to do this? It turns out that the requisite encryption is called "TypeMK" and can presently be extracted from Maple by the following steps.

 1 Type the expression in math mode and convert to an Atomic Identifier.
 2 Press the Enter key so that a label is generated for this echo.
 3 Apply the command to the equation label. The result is the sought-for TypeMK string. When I double-checked on this process, I discovered that TypeMK is still under development, and the magic I had written years ago is still not fully supported. "Use it at your own risk - it might not remain backward-compatible" was the warning. But I was able to carry out the three steps give above and find the TypeMK that, at least in Maple 14, would let me add to a graph generated with a command written in text mode, any of title, caption, legend, or text in math notation. And this included the two cases detailed above. Gem 2 From my earliest days of using Maple in the calculus classroom, I've always wanted Maple to return the derivatives of tangent, cotangent, hyperbolic tangent, and hyperbolic cotangent in the form that my students' textbooks used. The Maple and textbook forms of these derivatives are listed in Table 2.

 Maple Typical Calculus Text =   =   =   =  Table 2   Certain derivatives in Maple and standard calculus texts

The reasoning behind Maple's choice of expressions for these derivatives is that they are "simpler;" that is, each rule involves just a single function, not two, as on the right in Table 2. When this was first "explained" to me years ago, I was also told that the rules could easily be modified. Well, just recently, someone showed me how to re-program Maple to do this. See Table 3. (The  is in deference to any remember-table issues.)

 > restart; unprotect(`diff/tan`): unprotect(`diff(cot`): unprotect(`diff(tanh`): unprotect(`diff(coth`): `diff/tan`  := proc(a,x)  sec(a)^2*diff(a,x);end: `diff/cot`  := proc(a,x) -csc(a)^2*diff(a,x);end: `diff/tanh` := proc(a,x)  sech(a)^2*diff(a,x);end: `diff/coth` := proc(a,x) -csch(a)^2*diff(a,x);end:

Table 3   Code to modify the differentiation rules for tangent, cotangent, and their hyperbolic counterparts

Table 4 shows that the code in Table 3 really does change the differentiation rules for tangent, cotangent, and their hyperbolic counterparts.  =  =  =  = Table 4   Verification that the code in Table 3 changes the differentiation rules in Maple Gem 3 Not so long ago I was asked by a user how one could mark points along a curve "parametrically." By this, the user meant how can the points on a parametric curve be marked for specified . This needed no help from a programmer - the desired functionality is built into the command in the VectorCalculus packages. Figure 1 shows a simple example of the usage. A curve is defined parametrically with the command, and graphed with the PlotPositionVector command. Figure 1   Use of the PlotPositionVector command to mark points "parametrically"

The alternative is tedious, since the location of the parametrically defined points must be computed, the points defined and graphed separately, and then added to a graph of the curve. Gem 4

A user recently asked how the graph of in Figure 2 could be improved, since the range of is , but the graph doesn't touch the plane . Even with the option grid=[100,100], the graph still isn't satisfactory.  Figure 2   Naive graph of  Our in-house graphics expert made two suggestions, the results of which are shown in Figures 3 and 4.  Figure 3   Improvement 1 Figure 4   Improvement 2

The trick in Figure 3 is to graph over just the disk whose boundary is the intersection of the surface with the plane . This is done by graphing between the bounding half-circles. Figure 4 exploits the circular symmetry by graphing in cylindrical coordinates. Gem 5 Elementary trig provides the half-angle identity  Unfortunately, Maple does not have a really good implementation of either of the half-angle identities. In fact, the  command   does not list any form of the standard half-angle identities, whereas the command   includes, as the last item in its list, the square of the half-angle identity for the sine function. Thus, Maple does not easily convert expressions such as to radical form. In particular, when , for example, one needs a definite sprinkling of Maple magic to obtain radicals.  Table 5 shows two different versions of this magic, applied to the fourth-quadrant case .  =   =  Table 5   Two methods for converting to radicals

If is in the fourth-quadrant, then is in the second-quadrant where the sine function is negative. Hence, in the first method, the appropriate transformation rule must be known beforehand. In the second method, the assumptions on and allow Maple to apply the appropriate form of the half-angle identity unaided.

See my next Blog for a continued discussion of the conversion to radicals of expressions of the form , where , is one of sine, cosine, or tangent, and is one of arcsine, arccosine, or arctangent.  