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

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.

Related

Z3: Complex numbers?

I have been searching on whether z3 supports complex numbers and have found the following: https://leodemoura.github.io/blog/2013/01/26/complex.html
The author states that (1) Complex numbers are not yet implemented in Z3 as a built-in (this was written in 2013), and (2) that Complex numbers can be encoded on top of the Real numbers provided by Z3.
The basic idea is to represent a Complex number as a pair of Real numbers. He defines the basic imaginary number with I=(0,1), that is: I means the real part equals 0 and the imaginary part equals 1.
He offers the encoding (I mean, we can test it on our machines), where we can solve the equation x^2+2=0. I received the following result:
sat
x = (-1.4142135623?)*I
The sat result does make sense, since this equation is solvable in the simulation of the theory of complex numbers (as a result of the theory of algebraically closed fields) we have just made. However, the root result does not make sense to me. I mean: what about (1.4142135623?)*I?
I would understand to receive the two roots, but, if only one received, I do not understand why I get the negated solution.
Maybe I misread something or I missed something.
Also, I would like to say if complex numbers have already been implemented built in Z3. I mean, with a standard:
x = Complex("x")
And with tactics of kind of a NCA (from nonlinear complex arithmetic).
I have not seen any reference to this theory in SMT-LIB either.
AFAIK there is no plan to add complex numbers to SMT-LIB. There's a Google group for SMT-LIB and it might make sense to send a post there to see if there is any interest there.
Note, that particular blog post says "find a root"; this is just satisfiability, i.e. it finds one solution, not all of them. (But you can ask for another one by adding an assertion that says x should be different from the first result.)

Performance issues with z3py using modulo and optimization

I'm experimenting with Z3 (using the python api) where I'm building up a scheduling model for a class assignment, where I have to use modulo quite often (because its periodic). Modulo seems already to slow down z3 by a lot, but if I try do some optimization on top (minimize a cost function which is a sum), then it takes quite some time for fairly small problems.
Without optimization it works okayish (few seconds for a smaller problem). So that being said, I have now 2 questions:
1) Is there any trick with the modulo function of how to use it? I already assign the modulo value to a function. Or is there any other way to express periodic/ring behavior?
2) I am not interested in finding THE best solution. A good one, will be good enough. Is there a way to set some bounds for the cost function. Like, if know the upper and lower bound of it? Any other tricks, where I could use domain knowledge to find solutions fast.
Furthermore, I thought that if I ll use timeout option solver.set("timeout" 10000), then the solver would time out with the best solution so far. That doesnt seem to be the case. It just times out.
Impossible to comment on the mod function without seeing some code. But as a rule of thumb, division is difficult. Are you using Int values, or bit-vectors? If you are using unbounded integers, you might want to try bit-vectors which might benefit from better internal heuristics. Or try Real values, and then do a proper reduction.
Regarding how to get the "best so far" optimal value, see this answer: Finding suboptimal solution (best solution so far) with Z3 command line tool and timeout
Instead of using the built-in modulo and division, you could introduce uninterpreted functions mymod and mydiv, for which you only provide the necessary axioms of division and modulo that your problem requires. If I remember correctly, Microsoft's Ironclad and/or Ironfleet team did that when they had modulo/division-related performance problems (using the pipeline Dafny -> Boogie -> Z3).

how to solve equation instead of giving one model in z3

This might not beyond the scope of z3, I know in Z3 we can simplify expression, but I wonder if z3 can solve the equation instead of giving a model.
For example, I want the following equation always be true for any value of a. Using ForAll quantifier in this case would return unsat.
a == b - c + 2
The solution I expect is a formula for one specified variable while simplify doesn't deal with this, like
b == a + c - 2 or c == b - a + 2
Is there any API for this ? Thanks in advance.
Not sure what exactly the question is here, but it sounds like you want to get all satisfying solutions instead of a single satisfying solution. It is possible to encode that (with quantifiers), but not necessarily easy to solve; depending on the fragment of logic used within the quantifiers, Z3 may be slow, or it may simply give up relatively early (returning unknown). There is no dedicated API for this purpose, but the existing API has all the pieces required to solve the puzzle (e.g., use an uninterpreted function f to encode the solution symbolically and then get the func_interp for f from the model.)
For more information about getting more than one solution, see also Z3: finding all satisfying models and Z3 Enumerating all satisfying assignments.

Can you "teach" computers to do algebra using variable expressions (eg aX+bX=(a+b)X)

Let's say in the example lower case is constant and upper case is variable.
I'd like to have programs that can "intelligently" do specified tasks like algebra, but teaching the program new methods should be easy using symbols understood by humans. For example if the program told these facts:
aX+bX=(a+b)X
if a=bX then X=a/b
Then it should be able to perform these operations:
2a+3a=5a
3x+3x=6x
3x=1 therefore x=1/3
4x+2x=1 -> 6x=1 therefore x= 1/6
I was trying to do similar things with Prolog as it can easily "understand" variables, but then I had too many complications, mainly because two describing a relationship both ways results in a crash. (not easy to sort out)
To summarise: I want to know if a program which can be taught algebra by using mathematic symbols only. I'd like to know if other people have tried this and how complicated it is expected to be. The purpose of this is to make programming easier (runtime is not so important)
It depends on what do you want machine to do and how intelligent it should be.
Your question is mostly about AI but not ML. AI deals with formalization of "human" tasks while ML (though being a subset of AI) is about building models from data.
Described program may be implemented like this:
Each fact form a pattern. Program given with an expression and some patterns can try to apply some of them to expression and see what happens. If you want your program to be able to, for example, solve quadratic equations given rule like ax² + bx + c = 0 → x = (-b ± sqrt(b²-4ac))/(2a) then it'd be designed as follows:
Somebody gives a set of rules. Rule consists of a pattern and an outcome (solution or equivalent form). Think about the pattern as kind of a regular expression.
Then the program is asked to show some intelligence and prove its knowledge via doing something with a given expression. Here comes the major part:
you build a graph of expressions by applying possible rules (if a pattern is applicable to an expression you add new vertex with the corresponding outcome).
Then you run some path-search algorithm (A*, for example) to find sequence of transformations leading to the form like x = ...
I think this is an interesting question, although it off topic in SO (tool recommendation)
But nevertheless, because it captured my imagination, I wrote couple of function using R that can solve stuff like that quite easily
First, you'll have to install R, after words you'll need to download package called stringr
So in R console run
install.packages("stringr")
library(stringr)
And then you can define the following functions that I wrote
FirstFunc <- function(temp){
paste0(eval(parse(text = gsub("[A-Z]", "", temp))), unique(str_extract_all(temp, "[A-Z]")[[1]]))
}
SecondFunc <- function(temp){
eval(parse(text = strsplit(temp, "=")[[1]][2])) / eval(parse(text = gsub("[[:alpha:]]", "", strsplit(temp, "=")[[1]][1])))
}
Now, the first function will solve equations like
aX+bX=(a+b)X
While the second will solve equations like
4x+2x=1
For example
FirstFunc("3X+6X-2X-3X")
will return
"4X"
Now this functions is pretty primitive (mostly for the propose of illustration) and will solve equation that contain only one variable type, something like FirstFunc("3X-2X-2Y") won't give the correct result (but the function could be easily modified)
The second function will solve stuff like
SecondFunc("4x-2x=1")
will return
0.5
or
SecondFunc("4x+2x*3x=1")
will return
0.1
Note that this function also works only for one unknown variable (x) but could be easily modified too

How to overcome an apparent REST vs. DRY dilemma in rails?

A rails app I'm working on features examples of quadratic equations. Obviously, these are all of a common structure: ax^2 + bx + c = 0.
I don't want to store every single example of these. I'd rather generate them from a template. Storing hundreds of possible versions of this structure seems highly wasteful and un-DRY.
On the other hand, if I generate them, I can't access them again reliably as I could if they were simply multiple database objects.
I'm sure there must be a way to overcome this, but I'm still learning rails and I'm obviously not grasping something here. Thanks.
The answer is "it depends". If you're only ever going to do examples of quadratic equations, then you can just store a, b and c. If you think you're going to do other types of equations, store the whole thing.
What really shouldn't be a consideration is that its a "waste" to store the full equation in the sense that you're wasting disk space. Unless you're storing millions of these things, don't worry about it. Disk is cheap.
Other viewpoints to take are YAGNI (Ya Ain't Gonna Need It) which would say to code with the situation you have now, quadratic equations, and don't worry about generalizing it. If you need to you can refactor the code and the data later.
Another way to look at it is KISS (Keep It Simple, Stupid). The simplest thing to do would likely to be to just store the complete equation. This makes retrieving the equation a simple database fetch, no code to generate the equation is necessary.
Personally, assuming you're not doing a huge amount of these things, I'd favor KISS in this situation. I wouldn't be confident that the system is only going to be used for quadratic equations. What you can do is make the system handle whatever equations but make the input form take a, b and c and turn that into a quadratic equation. If you need other equation types later, changing the input logic is much simpler than changing the data structure.
Resources don't have to be stored in the database to be restful and reliably accessible. You just need a one-to-one correspondence between the identifier of the resource and the resource you generate.
Just use a,b,c as the identifier of ax^2 + bx + c = 0. Then if your route is resources :quadratics you can generate the url like quadratic_url([a,b,c].join(',')) and in your show method of the QuadraticsController, generate it by doing generate_quadratic(params[:id].split(',').map(&:to_i)).
You have to store the coefficients a, b, c to define your quadratic equations if your examples are arbitrary and independent.
If you want unique quadratic equations, you can throw out ones for which a, b, and c for one equation differ from those of another equation by a constant multiple.
You can generate 1,210 different quadratic equations by choosing a, b, and c to be integers between -5 and +5, inclusive - a can't be zero, of course - but I don't know if that's what you want?

Resources