MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Prefix notation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40581] Re: [mg40530] Prefix notation
  • From: Dr Bob <majort at cox-internet.com>
  • Date: Thu, 10 Apr 2003 03:42:12 -0400 (EDT)
  • References: <NDBBJGNHKLMPLILOIPPOGEPODHAA.djmp@earthlink.net>
  • Reply-to: majort at cox-internet.com
  • Sender: owner-wri-mathgroup at wolfram.com

Yes!  Each function in the composition needs [] because of the evaluation 
order, but your notation keeps everything straight with minimum confusion.  
We might make it even more visually clear (if necessary) with a linebreak 
after each "@":

R[Pi/4, 0]@
  T[1 + I]@
    R[Pi/4, 1]@
      z

(Mathematica adds the indentations automatically.)

It's much more confusing (I think) if we write

R[Pi/4, 0][T[1 + I][R[Pi/4, 1][z]]]

Matching the brackets is a nightmare, and there's no useful way to help 
with linebreaks.

Worse, we could do it the traditional way:

Clear[T, R]
T[v_, z_] := z + v
R[theta_, a_, z_] := E^(I*theta)*(z - a) + a

R[Pi/4, 0, T[1 + I, R[Pi/4, 1, z]]]

With linebreaks, that becomes

R[Pi/4, 0,
  T[1 + I,
    R[Pi/4, 1,
      z]
    ]
  ]

All those alternatives are harder to read and understand than your 
original.

In addition, there are times when we want to use dynamic programming or 
otherwise optimize the definitions of the functions.  Your example might 
become

Clear[R, T]
T[v_] := T[v] = # + v &
R[theta_, a_] := R[theta, a] =
    E^(I*theta)*(# - a) + a &
R[Pi/4, 0]@T[1 + I]@R[Pi/4, 1]@z

That becomes more relevant if determining T[v] and/or R[theta,a] is a 
complicated process itself, but doesn't depend on z.  There's an example 
(function g[n_]) in my recent post at

http://forums.wolfram.com/mathgroup/archive/2003/Apr/msg00145.html

where doing it that way greatly speeds up a Plot.

Bobby

On Wed, 9 Apr 2003 11:42:21 -0400, David Park <djmp at earthlink.net> wrote:

> Bobby,
>
> Another nice use for the prefix notation, @, is in writing compositions. 
> It
> even looks a little like the composition symbol.
>
> Here is an example. Suppose we define translations T[v] and rotations
> R[angle, center] in the complex plane.
>
> T[v_][z_] := z + v
> R[theta_, a_][z_] := E^(I*theta)*(z - a) + a
>
> Then to perform a rotation of Pi/4 about 1, followed by a translation of 
> 1,
> followed by a rotation of Pi/4 about 0 we write the composition...
>
> R[Pi/4, 0]@T[1 + I]@R[Pi/4, 1]@z
>
> E^((I*Pi)/4)*(2 + I + E^((I*Pi)/4)*(-1 + z))
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
>
> From: Dr Bob [mailto:majort at cox-internet.com]
To: mathgroup at smc.vnet.net
>
>
>>> You make judicious use of what I guess is "infix" notation.
>>> Maybe you're minimizing the number of keystrokes one uses.
>
> It's actually called prefix notation.  I just discovered functions called
> Infix, Prefix, and Postfix whose help entries make this clear.
>
> I like "@" when there's only one argument because, when I see "[", it's
> often hard to tell where the matching "]" is, and vice-versa.  If I avoid
> some brackets by using "@" or "//", that makes it easier to visually 
> match
> the remaining brackets, and "@" does save a keystroke, after all.  I
> generally don't like "//", but it's handy for applying a function of one
> argument to a long and complicated mess.
>
> Sometimes, if the argument needs parentheses, I use f[arg] instead of
> f@(arg).
>
> I tend to use [] if there may be other arguments added later, even though
> there's only one argument at the moment.
>
> Evaluate the following:
>
> parabola[zero_] := (# - zero)^2 &
> parabola@3
> parabola[3][x]
> parabola[3]@x
> parabola@3@x
> (parabola@3)@x
> x // parabola@3
> (3 // parabola)@x
> 3 // parabola // x
>
> -- and you'll see that evaluation order might not be what we want or 
> expect
> sometimes.
>
> In this example, I think "parabola[3]@x" might be easiest to read and
> write.
>
> I often use my patented (not really) "multi-click" method to decide what
> the evaluation order will be.  In x // parabola@3, for instance, click
> three or four times in "parabola" and you'll see the selection expand
> (under Windows, anyway) in a way that corresponds to evaluation order.
>
> The "multi-click" method works for finding matching brackets too, if you
> already have them where they belong.  Prefix notation helps me get them
> right, by eliminating some.
>
> Bobby
>
> On Tue, 8 Apr 2003 15:58:29 -0400 (EDT), Stewart Mandell
> <stewart at rentec.com> wrote:
>
>>
>> thanks for pointing out ComplexExpand to me.
>>
>> You answer lots of queries on the Mathematica NewsGroup.
>> You make judicious use of what I guess is "infix" notation.
>> Maybe you're minimizing the number of keystrokes onw uses.
>> I have to get use to this.
>>
>> regards, Stewart
>>
>
>
>
> --
> majort at cox-internet.com
> Bobby R. Treat
>
>
>



-- 
majort at cox-internet.com
Bobby R. Treat



  • Prev by Date: Formatting Indefinite Series Expressions
  • Next by Date: Re: comments
  • Previous by thread: Re: RE: Prefix notation
  • Next by thread: sum with lists