Subject: A simple question about the 'FUNCTION format.
From: Stavros Macrakis
Date: Tue, 25 Mar 2003 14:23:51 -0500
> func(a,b) := 'func(a,b);
> ev(sum(func(a,b),a,1,2),func);
I think you will get what you want simply with
sum(func(a,b),a,1,2)
which gives
func(2, b) + func(1, b)
The ev(...,func) means "evaluate all the instances of func in the
expression". That is what is leading to the infinite recursion, since
evaluating func leads to a new instance of func.
Why are you using ev here? If you must use ev, you can also use the
"noun form" facility:
func(a,b):= ('func)(a,b);
but this is rather obscure (see below).
What exactly are you trying to accomplish?
-------------------------
As for the meaning of quote ('), this is rather a mess, I'm afraid. It
gets especially confusing since some things that look like evaluation
are actually simplification. Also because functions have a hidden
property, noun/verb.
I do NOT believe the current system is very clean or well-designed,
either in syntax or in semantics, but I will explain it as it is. Let's
start in.
Evaluation does two things: it substitutes variable values for variable
names, and it applies functions to their argments.
In the following, let's assume the following definitions:
five:5$
square(x):=x^2$
Let's start with the simplest case, quoting of expressions. The syntax
for this is '(<<expression>>). When evaluated, this returns the
expression itself without evaluating it. I'll show the non-quoted and
the quoted results:
five => 5 -- Variable substitution
'(five) => five -- Blocked by quoting expression
'five => five -- Equivalent for variables
square(five) => 25 -- Variable subst and func application
square('five) => five^2 -- Quote stops variable subst
'(f(five)) => f(five) -- Quote stops all eval within expr
Note that for functional applications, the syntax 'f(x) means something
else -- see below.
if 3>2 then b else c => b
if 3>2 then five else c => 5
if 3>2 then 'five else c => five
'(if 3>2 then b else c) => if 3>2 then b else c
Simplification, unlike evaluation, never examines variable values, and
never examines function definitions. It does, however, apply
transformations, either built-in or defined by the user (tellsimp etc.).
In particular, most arithmetic in Maxima is done as *simplification*,
not *evaluation*, e.g. 5+3=>8 is simplification.
Quoting does NOT block simplification:
five + five => 10 -- Eval and simp
'(five + five) => 2*five -- Simp but no eval
'(if abs(x^2) >= abs(x)^2 then x+x else 2+3);
=> if x^2 >= x^2 then 2*x else 5)
Notice, too, that not all operations have useful simplifications applied
to them, not even trivial ones. Though abs(x^2) simplifies to x^2
(since variables are assumed real), x^2 >= x^2 does NOT simplify to
TRUE. (Should it? Another story.)
<DETOUR>
There is one complication here. It turns out that conditions in IF,
WHILE, etc. are evaluated in a special way, so that
if '(3>2) then five else c => 5
</DETOUR>
Now let's turn to the quoting of functional applications. There are
three ways to quote a functional application: as a whole expression,
'(f(x)); the application only, ('f)(x); or the function only, 'f(x).
Quoting the whole expression blocks evaluation of the arguments, and
also blocks application of the function to the arguments:
'(square(five)) => square(five)
Quoting the application only blocks application of the function to the
arguments:
('square)(five) => square(5)
Re-evaluating that expression applies the function:
ev(('square)(five)) => 25
Quoting the function actually permanently blocks evaluating the
function:
'square(five) => square(5)
even when it is re-evaluated:
ev('square(five)) => square(5)
This is a so-called "noun form" as opposed to the "verb form" of a
function. The only way to force a noun form to be evaluated is to
explicitly cite it as an argument to ev:
ev('square(five),square) => 25
One of the confusing things about noun forms is that there is no visible
difference to the user between a noun form and a verb form. Moreover,
there is no way to create the noun form of a function without actually
applying it. Consider:
squareverb: 'square => square
squarenoun: part('square(x),0) => square
is(squareverb = squarenoun) => false
map(squareverb, [3,4]) => [9,16]
map(squarenoun, [3,4]) => [square(3),square(4)]
I do not much like the noun/verb system, but that's the way it is....
If a name in functional position does not have a functional value, it is
normally evaluated before being applied:
squareverb(5) => 25
squarenoun(5) => square(5)
All the function quotation techniques block this, but let's not go into
details there.... Suffice it to say that if you want to construct an
application of a variable function to arguments, but not actually apply
it, you must use funmake or substpart. That's another discussion which
I don't think is necessary here.
-------------------------
By the way....
> (C2) func(a,b) := block([],return('func(a,b)));
> (D2) FUNC(A, B) := BLOCK([], RETURN('FUNC(A, B)))
You don't need the block or the return here. Maxima is Lisp-like, so
the following will work:
FUNC(A, B) := BLOCK([], 'FUNC(A, B));
or simply
FUNC(A, B) := 'FUNC(A, B);
Not relevant to your particular problem, but....