Alastair
Alastair

Reputation: 121

Maxima syntax for calling function n times

In mathematics you can write f(f(f(x))) as f^3(x). I've trawled through the Maxima manuals but can't find a way to express this for an arbitrary power.

Upvotes: 2

Views: 158

Answers (1)

Robert Dodier
Robert Dodier

Reputation: 17595

Maxima doesn't have a built-in operator for that. But I think you can define a workable solution yourself. Maybe this works for you:

(%i1) infix ("o^") $
(%i2) matchdeclare (ff, symbolp, xx, all) $
(%i3) tellsimp ((ff o^ 0)(xx), xx) $
(%i4) tellsimp ((ff o^ 1)(xx), ff(xx)) $
(%i5) matchdeclare (nn, lambda ([e], integerp(e) and e > 1)) $
(%i6) tellsimp ((ff o^ nn)(xx), (ff o^ (nn - 1))(ff (xx))) $

I've defined simplification rules for "o^" since I want it to be applied only if it appears in a function call expression (i.e., something like (f o^ 3)(x), not just f o^ 3 by itself), and the second argument is a literal, nonnegative integer.

You have to write a space between "o^" and the following token, since otherwise the parser gets confused.

Here are some examples:

(%i7) (h o^ 0)(u);
(%o7)                                  u
(%i8) (h o^ 1)(u);
(%o8)                                h(u)
(%i9) (h o^ 2)(u);
(%o9)                               h(h(u))
(%i10) (h o^ 3)(u);
(%o10)                            h(h(h(u)))
(%i11) (h o^ n)(u);
(%o11)                             h o^ n(u)
(%i12) %, n=2;
(%o12)                              h(h(u))
(%i13) h o^ 3;
(%o13)                              h o^ 3
(%i14) %(z);
(%o14)                            h(h(h(z)))
(%i15) (h o^ -1)(u);
(%o15)                           h o^ (- 1)(u)

Note that in cases which don't match the rules, the expression is not simplified. This leaves the door open to defining additional rules to handle those cases, without changing the stuff that already works.

The display of unsimplified expressions (e.g. %o11) is ambiguous; this is a bug.

Upvotes: 3

Related Questions