I would like to compute the norm of a function, and use that value in the definition of a new normalized function of beta. However, if i do so, the resulting function does not treat the normalization as having the same variable beta as the rest of the function, one beta is is not substituted in both ways iv tried to introduce the result of the normalization (ie, beta^3/2 never gets substituted as a function variable should).
killall(beta, psi11, no11, norm, psi1, psitest2)$
psi11(x,beta):= x*%e**(-(beta*x)**2/2)$
no11: integrate(psi11(x,beta)**2, x, minf, inf)$
norm: sqrt(no11)$
psi1(x,beta):= x*%e**(-(beta*x)**2/2)/ norm ;
(o268) psi1(x,beta):=(x*%e^((-(beta*x)^2)/2))/norm
psi1(x,bb) ;
(o269) (sqrt(2)*beta^(3/2)*x*%e^(-((bb^2*x^2)/2)))/%pi^(1/4) /*this has beta and bb*/
psitest2(x,beta):= x*%e**(-(beta*x)**2/2)/ sqrt(no11) ;
(o270) psitest2(x,beta):=(x*%e^((-(beta*x)^2)/2))/sqrt(no11)
psitest2(x,bb);
(o271) (sqrt(2)*beta^(3/2)*x*%e^(-((bb^2*x^2)/2)))/%pi^(1/4) /*this has beta and bb*/
Maxima's evaluation policy is one-time evaluation (in the absence of flags to modify evaluation). What is happening in your example is that the function body of psi1
is being evaluated with beta
temporarily assigned the value bb
. The variable norm
is in the body, and it evaluates to %pi^(1/4)/(sqrt(2)*beta^(3/2))
. However, having evaluated norm
one time, the result is not evaluated again, and so beta
shows up in the function result.
There are a few different ways to get to the expected result.
(1) A general way is to call define
to define a function instead of :=
, since define
evaluates the function body, as opposed to :=
which does not.
(2) You can apply the quote-quote operator ''
to evaluate all or part of the function body. Note that quote-quote is applied only the first time an expression is parsed, so it is usually only useful at the top-level input prompt. But that's usually where functions are defined, so that's OK here.
(3) You can call the function with the infeval
flag for ev
, which will cause the function value to be repeatedly evaluated. ?? ev
will say more about infeval
.
Here are the results I get.
(0) Preliminaries:
(%i2) psi11(x,beta):= x*%e**(-(beta*x)**2/2);
2
- (beta x)
───────────
2
(%o2) psi11(x, beta) := x %e
(%i3) no11: integrate(psi11(x,beta)**2, x, minf, inf);
Is beta zero or nonzero?
nz;
Is beta positive or negative?
p;
sqrt(%pi)
(%o3) ─────────
3
2 beta
(%i4) norm: sqrt(no11);
1/4
%pi
(%o4) ───────────────
3/2
sqrt(2) beta
(1) Via define
:
(%i5) define (psi1 (x, beta), x*%e**(-(beta*x)**2/2)/ norm);
2 2
x beta
- ────────
3/2 2
sqrt(2) x beta %e
(%o5) psi1(x, beta) := ──────────────────────────────
1/4
%pi
(%i6) psi1 (x, bb);
2 2
bb x
- ──────
3/2 2
sqrt(2) bb x %e
(%o6) ──────────────────────────
1/4
%pi
(2) Via quote-quote:
(%i7) kill (psi1);
(%o7) done
(%i8) psi1 (x, beta) := x*%e**(-(beta*x)**2/2)/ (''norm);
2
- (beta x)
───────────
2
x %e
(%o8) psi1(x, beta) := ───────────────
1/4
%pi
───────────────
3/2
sqrt(2) beta
(%i9) psi1 (x, bb);
2 2
bb x
- ──────
3/2 2
sqrt(2) bb x %e
(%o9) ──────────────────────────
1/4
%pi
(3) Via infeval
:
(%i10) kill (psi1);
(%o10) done
(%i18) psi1 (x, beta) := ev (x*%e**(-(beta*x)**2/2)/ norm, infeval);
2
- (beta x)
───────────
2
x %e
(%o18) psi1(x, beta) := ev(───────────────, infeval)
norm
(%i19) psi1 (x, bb);
2 2
bb x
- ──────
3/2 2
sqrt(2) bb x %e
(%o19) ──────────────────────────
1/4
%pi