(wx)Maxima: texput for powers of expressions - tex

I have used texput to set the tex1 output of log(x) to be \ln(x) with
texput('log, lambda([e],[a]:args(e), printf(false, "\\ln(~a)", tex1(a))));
and am wondering if it is possible to also set the output for something like (log(x))^n? In particular I'd like to use the \ln^n(x) convention.

The strangeness around TeX output for log is a bug, which I'm working on. But here is a work around which gets the behavior you want, I think.
Here I'm calling both texput(log, "\\ln ") and texput(log, "\\ln ", prefix), to set the TeX output for log in different contexts, and also :lisp (push log *tex-mexpt-trig-like-fns*) to have log treated similarly to trig functions.
(%i1) stringdisp: true $
(%i2) map (tex1, [log(x), log(x+1), log(x)^n]);
(%o2) ["\log x", "\log \left(x+1\right)", "\left(\log x\right)^{n}"]
(%i3) texput (log, "\\ln ");
(%o3) "\ln "
(%i4) texput (log, "\\ln ", prefix);
(%o4) "\ln "
(%i5) map (tex1, [log(x), log(x+1), log(x)^n]);
(%o5) ["\ln x", "\ln \left(x+1\right)", "\ln x^{n}"]
Hmm, that's not quite enough. Oh, that's right, I forgot the bit about trig functions.
(%i6) :lisp (push '%log *tex-mexpt-trig-like-fns*)
(%LOG %SIN %COS %TAN %SINH %COSH %TANH %ASIN %ACOS %ATAN %ASINH %ACOSH %ATANH)
(%i6) map (tex1, [log(x), log(x+1), log(x)^n]);
(%o6) ["\ln x", "\ln \left(x+1\right)", "\ln ^{n}x"]
Does that seem right?

Related

How to check whether an input field was left empty in Maxima?

I am creating linear algebra problems in Stack. The integrated computer algebra system that's used by Stack is Maxima.
To perform certain checks on the students' answers I need to know whether an input field was completed by the student at all or not. I can refer to the answers of the students by their names ans1, ans2, ans3,... . How can I check in Maxima whether for ex. the variable ans1 is empty or null or something similar?
You can use ?boundp or member(..., values):
(%i1) x: 42$
(%i2) ?boundp('x);
(%o2) true
(%i3) ?boundp('y);
(%o3) false
(%i4) member('x, values);
(%o4) true
(%i5) member('y, values);
(%o5) false

Analytic solution to an equation including the error function in Maxima

Maxima does not seem to come up with an analytic solution to this equation which includes the error function. The independent variable here is "p" and the dependent variable to be solved for is "x".
see an illustration of equation follow link
(%i3) solveexplicit:true$ ratprint:false$ fpprintprec:6$
(%i4) eqn: (sqrt(%pi)*(25*2^(3/2)*p-25*sqrt(2))*erf(1/(25*2^(3/2)*x))*x+1)/(25*p) = 0.04;
(%i5) solve (eqn, x);
(%o5) []
(%i6) eqn, [p=2,x=0.00532014],numer;
(%o6) 0.04=0.04
Any help or pointing in the right direction is appreciated.
As far as I know, Maxima can't solve equations containing erf. You can get a numerical result via find_root:
(%i5) find_root (eqn, x, 0.001, 0.999), p=2;
(%o5) 0.005320136894034347
As for symbolic solutions, I worked with the equation a little bit. One can get it into the form erf(something/x)*x = otherstuff, or equivalently erf(y) = somethingelse*y where y = something/x and somethingelse = otherstuff/something if I'm not mistaken. I don't know anything in particular about equations of that form, but perhaps you can find something.
Yes, solve can only do polynominals. I used the series expansion for small values of x and the accuracy is good enough.
(%i11) seriesE: 1$
termE: erf(x)$
for p: 1 unless p > 3 do
(termE: diff (termE, x)/p,
seriesE: seriesE + subst (x=0, termE)*x^p)$
seriesE;
(%o11) -(2*x^3)/(3*sqrt(%pi))+(2*x)/sqrt(%pi)+1
However, the "Expression longer than allowed by the configuration setting!"

Simplification of expressions involving abstract derivatives in maxima

I'm trying to get maxima to perform some "abstract" Taylor series expansions, and I'm running into a simplification issue. A prototype of the problem might be the finite-difference analog of the gradient,
g(x1,dx1) := (f(x1+dx1) - f(x1))/dx1; /* dx1 is small */
taylor(g(x1,dx1), [dx1], [0], 0);
for which maxima returns
So far so good. But now try the finite-difference analog of the second derivative (Hessian),
h(x1,dx1) := (f(x1+dx1) - 2*f(x1) + f(x1-dx1))/dx1^2;
taylor(h(x1,dx1), dx1, 0, 0);
for which I get
which is not nearly as helpful.
A prototype of the "real" problem I want to solve is to compute the low-order errors of the finite-difference approximation to ∂^2 f/(∂x1 ∂x2),
(f(x1+dx1, x2+dx2) - f(x1+dx1, x2) - f(x1, x2+dx2) + f(x1, x2))/(dx1*dx2)
and to collect the terms up to second order (which involves up to 4th derivatives of f). Without reasonably effective simplification I suspect it will be easier to do by hand than by computer algebra, so I am wondering what can be done to coax maxima into doing the simplification for me.
Consider this example. It uses Barton Willis' pdiff package. I
simplified notation a bit: moved center to [0, 0] and introduced
notation for partial derivatives.
(%i1) load("pdiff") $
(%i2) matchdeclare([n, m], integerp) $
(%i3) tellsimpafter(f(0, 0), 'f00) $
(%i4) tellsimpafter(pderivop(f,n,m)(0,0), concat('f, n, m)) $
(%i5) e: (f(dx, dy) - f(dx, -dy) - f(-dx, dy) + f(-dx, -dy))/(4*dx*dy)$
(%i6) taylor(e, [dx, dy], [0, 0], 3);
2 2
f31 dx + f13 dy
(%o6)/T/ f11 + ----------------- + . . .
6

How to simplify terms with usage of square root in maxima

Let's say we have a term like 1/4 * x/sqrt(2) * x^2 / 2; in Maxima.
As an output (without further modification) it gives x^3/2^(7/2).
How can I force the output format to be like x^3/(8*sqrt(2)) with usage of square roots whenever possible?
(%i1) sq2: " "(sqrt(2))$
(%i2) matchdeclare(n, lambda([n], oddp(n) and n#1))$
(%i3) defrule(r_sq2, 2^(n/2), sq2*2^((n-1)/2)) $
(%i4) e: 1/4 * x/sqrt(2) * x^2 / 2;
3
x
(%o4) ----
7/2
2
(%i5) apply1(e, r_sq2);
3
(sqrt(2)) x
(%o5) -------------
16
A rule can help to insert sqrt(2). In the example I use a "null" function to prevent simplification. You can also consider box and rembox functions or leave sq2 undefined.

Maxima plot not working

What am I doing wrong in this code?
atvalue(y(x),[x=0],1)$
desolve(diff(y(x),x)=y(x),y(x));
plot2d(y(x),[x,-6,6]);
Output:
plot2d: expression evaluates to non-numeric value everywhere in plotting range.
plot2d: nothing to plot
false
I want to plot y(x) which is obtained from a differential equation.
In Maxima y(x) = ... is an equation, and y(x) := ... is a function, and those two things are different. Try this:
atvalue (y(x), [x=0], 1)$
desolve (diff(y(x),x)=y(x), y(x));
define (y(x), rhs(%));
plot2d (y(x), [x, -6, 6]);
Here define(y(x), ...) is a different way to define a function. define evaluates the function body rhs(%) to yield exp(x) but := quotes it (not what you want).
The reason is that the result you see after the desolve does not mean y is defined as a function of x; in fact you obtain the same error if you change y(x) with f(x) (or any other unknown function) in plot2d. See the difference:
(%i9) atvalue(y(x),[x=0],1)$
(%i10) desolve(diff(y(x),x)=y(x),y(x));
x
(%o10) y(x) = %e
(%i11) y(x);
(%o11) y(x)
(%i12) y(x):=%e^x;
x
(%o12) y(x) := %e
(%i13) y(x);
x
(%o13) %e
I don't know if there's a way to “transform” the equation (the result) into a function definition automatically. If I find a way, I will complete the answer.

Resources