Calculate list of first derivative of function in Maxima for selected interval - maxima

I am facing a simple problem in Maxima: I want to calculate list of first derivatives of function / expression in various points. I know how to calculate list of "points" for normal expression:
float(makelist((x^2/sin(x-x/2),x,1,1000))
but when I run the expression through diff it changes expression to function and I don't know how to work with functions. I tried:
float(makelist(diff(x^2/sin(x-x/2)),x,1,1000))
which "zeroed" on me. Then I tried this:
float(makelist(''(diff(x^2)),x,1,1000))
which gives you right answer, but the result is not a number anymore. It's a list of something like:
[2.0 del(1.0), 4.0 del(2.0), 6.0 del(3.0), 8.0 del(4.0), ...
Would someone care to enlighten me about Maxima functions and numeric evaluation?
I know that this is a silly beginner's question, but I have never worked with Maxima before.

I think you want float(makelist(''(diff(x^2, x)),x,1,1000)) i.e. you need to write diff(expression, variable) instead of just diff(expression).

Related

Printing numbers without exponential notation in Maxima

I have trouble with WordMat, when calculating with Maxima, which didn't occur before, but started recently. When i calculate something that either results in x00000 or 0,0000x i get the result returned as its scientific expression, IE x*10^5 or x*10^-5. Even though this is correct, i would rather have it returned as a full number.
This only tends to happen when the number of 0's goes beyonda certain number, in the case of decimal numbers its from xe-5 and higher numbers its from xe9 and beyond. I can not turn this off in the settings as far as i can see, but it seems to be a setting in maxima simplification, with the variable option "expon" from what i found here:
file:///C:/Program%20Files%20(x86)/WordMat/Maxima-5.45.1/share/maxima/5.45.1/doc/html/maxima_46.html
Does anyone know of a way to either change the setting in wordmat, or edit the simplification rules in maxima?
I tried:
Turning on and off most setting in wordmat
Restarting my pc
Reinstalling WordMat
Looking through the manual for maxima/wordmat and looking for changeable settings
When you want to control the printed format of numbers, I think the best general answer is to use printf to specify exactly how to print the numbers. For what you want, printf(true, "~f", x) will print the value of x without ever introducing exponential format.
(The default maxima output is basically "~g" which basically automatically chooses between "~f" and "~e" depending on the value of x).
Perhaps a future version of maxima will allow you to change the default output range, but in general printf is the method to use if you want fine-grained control of the output.

How to understand the the sum expression, sum(A14+A15:A19)

I write a wrong expression sum(A14+A15:A19). Actually the right way for me is A14+sum(A15:A19). Curiously, the former can run as well with no error. So I spent much time to check the adequate result and find the odd phenomenon.
Besides, when I input the expression in the row 20,21 , error appears. Is that a bug? What's the meaning?
Thanks.
The sum function takes in parameters separated by ,s (not +s).
It's a little counterintuitive, since you're trying to add things together, but instead of =sum(A14+A15:A19) try = sum(A14,A15:A19).
All that being said, I assume there's a reason why you wouldn't simplify the whole thing and use =sum(A14:A19).

Maxima: How to add superscript to a symbolic variable?

How can I add a superscript to a variable, when I try to type it in to the Maxima Computer Algebra System?
So for example, I would like to have variables named U^(AC), U^(DC) where my intention is not to raise the variable to the power of something, but to have it as part of its name.
UPDATE, NEW ANSWER: Code to implement presuperscripts, presubscripts, postsuperscripts, and postsubscripts has been merged into Maxima. It is available now in the current version from Git, and it will be included in the next release of Maxima, which will be Maxima 5.44. See declare_index_properties in the online documentation (via ?).
OLD ANSWER: There isn't a built-in way to achieve that. That said, to some extent you can use A^B as a symbolic variable in some ways, depending on what you are trying to do. For example, given e:X*A^B + Y you can say solve(e, A^B) and it will return [A^B = -Y/X]. If you say more about exactly what you are trying to achieve, I might be able to give more specific advice.
A while ago I wrote some code to enable Maxima to treat indices of variables as subscripts as well as subscripts (as put the indices before as well as after the variable). I will dust off that code and write more about it here.
You can name it like that :
U^"AC"
U^"AC"*2=456; solve(%, U^"AC");
But it is a good idea to 'define' it before with something like :
UAC : U^"AC"; UAC *2=456; solve(%, UAC );

Maxima gives crazy answer for integrate(exp(x^2))

I'm trying to learn how to use Maxima. Something goes wrong with integrate :
(%i) integrate(exp(x^3),x,1,2);
(%o) (gamma_incomplete(1/3,-8)-gamma_incomplete(1/3,-1))/3
(%i) float(%);
(%o) .3333333333333333 (- 715.7985328824436 %i - 413.26647564521807)
(%i) expand(%);
(%o) - 238.59951096081454 %i - 137.75549188173935
What do you think?
Comparing Maxima's result to Wolfram Alpha, looks like Maxima has assumed that -x/((-x^3)^(1/3)) = 1. After debugging this for a bit, I can't tell if that term was originally in the result and it got simplified away, or if it was never there. With that term in place, and using the principal branch for the cube root, I get 275.510983 + (epsilon)*%i which agrees with a numerical result, namely quad_qags(exp(x^3), x, 1, 2) => 275.510983.
For the record, this integral is handled as "Type 1a" in maxima/src/sin.lisp, in the function INTEGRATE-EXP-SPECIAL.
Mathematically, I don't think there's anything fundamentally wrong with a complex answer to an exponential integration. In general, If you integrate e^(x^n) you're going to run into strange functions like the incomplete gamma function etc, because the answer isn't expressible in conventional functions, so has no conventional real analytic solution.
However, I think that there's definitely some inaccuracy here. Mathematica gives a different answer, much closer to a real answer, and as I ask for more accuracy, the real part appears to tend to zero, as you would expect.
If you want to numerically integrate (and it sounds like you do), you'll could use a different function. integrate is for analytical integration, which is why it gave you a formula rather than a number. Look up quad_qags and its friends for some really clever numerical integration functions.

How to convert Latex formula to C/C++ code?

I need to convert a math formula written in the Latex style to the function of a C/C++ code.
For example:
y = sin(x)^2 would become something like
double y = sin(x) * sin(x);
or
double y = pow(sin(x), 2);
where x is a variable defined somewhere before.
I mean that it should convert the latex formula to the C/C++ syntax. So that if there is a function y = G(x, y)^F(x) it doesn't matter what is G(x,y) and F(x),
it is a problem of the programmer to define it. It will just generate
double y = pow(G(x, y), F(x));
When the formula is too complicated it will take some time to make include it in the C/C++ formula. Is there any way to do this conversion?
Emacs' built-in calculator calc-mode can do this (and much more). Your examples can be converted like this:
Put the formula in some emacs buffer
$ y = sin(x)^2 $
With the cursor in the formula, activate calc-embedded mode
M-x calc-embedded
Switch the display language to C:
M-x calc-c-language
There you are:
$ y == pow(sin(x), 2) $
Note that it interprets the '=' sign in latex as an equality, which results in '==' for C. The latex equivalent to Cs assignment operator '=' would be '\gets'.
More on this topic on Turong's blog
I know the question is too old, but I'll just add a reply anyway as a think it might help someone else later. The question popped up a lot for me in my searches.
I'm working on a tool that does something similar, in a public git repo
You'll have to put some artificial limitations on your latex input, that's out of question.
Currently the tool I wrote only supports mul, div, add, sub, sqrt, pow, frac and sum as those are the only set of operations I need to handle, and the imposed limitations can be a bit loose by providing a preprocessor (see preproc.l for an [maybe not-so-good] example) that would clean away the raw latex input.
A mathematical equation, such as the ones in LaTeX, and a C expression are not interchangeable. The former states a relation between two terms, the latter defines an entity that can be evaluated, unambiguously yielding one value. a = b in C means 'take the value in variable b and store it in variable a', wheres in Math, it means 'in the current context, a and b are equal'. The first describes a computation process, the second describes a static fact. Consequently, the Math equation can be reversed: a = b is equivalent to b = a, but doing the same to the C equation yields something quite different.
To make matters worse, LaTeX formulae only contain the information needed to render the equations; often, this is not enough to capture their meaning.
Of course some LaTeX formulae, like your example, can be converted into C computations, but many others cannot, so any automated way of doing so would only make limited sense.
I'm not sure there is a simple answer, because mathematical formulaes (in LaTeX documents) are actually ambiguous, so to automate their translation to some code requires automating their understanding.
And the MathML standard has, IIRC, two forms representing formulaes (one for displaying, another for computing) and there is some reason for that.

Resources