So I'm taking the determinant of a matrix, then trying to use the Solver in Sympy to solve the expression for a particular variable.
The determinant I am trying to solve while being pretty complicated only has this one variable in it. Being a long expression I don't want to paste it all in but I've shortened it to a snippit that gives the same result;
Determinant = -0.0134365566406344*Nperp**7*sqrt(Nperp**2 + 0.3249)/(3.07787011388119*Nperp**2*sqrt(3.07787011388119*Nperp**2 + 1) + sqrt(3.07787011388119*Nperp**2 + 1)) - 4.2064522609332*Nperp**6/(3.07787011388119*Nperp**2 + 1)
Solutions = solve(Determinant, Nperp**2)
The problem is that when I print Solutions, I get an expression back in terms on Nperp instead of a numerical solution which is what I want.
I'm not sure whether the problem is that Sympy cannot handle the high powers in the polynomial, or if maybe there is no numerical solution possible but I would appreciate some thoughts of people more knowledgeable than I.
Thanks!
Edit: Code not indented
You are solving for Nperp**2. The answers it gives you are correct: they do equal Nperp**2 according to your Determinant equation, but it's probably not what you want. If you give solve an expression, rather than a single symbol, it will just isolate that expression.
You are probably looking for
Solutions = solve(Determinant, Nperp)
For me, this gives two solutions, [-549.228571428573, 0.0].
Related
Are solutions from sympy solve() being ordered in some way? Is it from minimal to maximal solutions?
How can I enforce non-negativity of the solution?
In my problem I need unique minimal positive solution. I appreciate all the help
They aren't ordered, although if they are numeric you can use sorted on them (this will fail if they are symbolic or nonreal). You can force positive only solutions by setting the symbol you solve for as positive, like
x = symbols('x', positive=True)
Also note the caveat with sympy.solve that it is no way guarantees that it has given you all the solutions to an equation---only those which it was able to find with the algorithms that are implemented.
I have an expression in SymPy that involves the normal cumulative function, N(x) which is directly linked to the error function through the equation N(x)=0.5*erf(x/sqrt(2)) + 0.5.
When I use the Normal(0,1).cdf(x) function of SymPy, it is written using the error function. So, when I output latex string of some (complicated) expression, the seem more complicated when using erf (instead of N(x), it outputs the equation mentionned obove). I tried to define a symbol N=0.5*erf(x/sqrt(2)) + 0.5 and tried the command 'rewrite' the rewrite my expression in terms of N, but 'rewrite' seems to work only with internally defined functions.
Does any bodu know how to rewrite erf(some_expression) in terms of N(some_expression), given that I don't know some_expression in advance (can't use subs) ?
Thanks in advance
I take it from your question that you are using Normal from sympy.statistics. You should move to sympy.stats. sympy.statistics has been deprecated for some time, and will be removed in the next version of SymPy.
To answer your question more directly, you can replace functions with functions using replace, like expr.replace(erf, lambda x: (N(x) - 0.5)/0.5).
The problem here is that there is no function N. I would expect this to be done better in sympy.stats, where the distributions are represented symbolically. However, I didn't find a way to do it. I opened https://github.com/sympy/sympy/issues/7819 for this.
I want to find all the real numbers that satisfy a particular equation. I have no trouble finding these values in Mathematica with
Solve[n*9^5 == 10^n-1, n]
which gives both 0 and 5.51257; but when I use SymPy's (0.7.3; Python 2.7.5) solve
n = sympy.symbols('n')
sympy.solve(n*9**5 - 10**n-1, n)
I seem to only get something that looks like 0, and not the second value, which is what I'm really seeking.
How can I get SymPy to produce the non-trivial solution I'm looking for? Is there a different function or package I should be using instead?
solve only gives symbolic solutions, so if it cannot find a closed form for a solution, it will not return it. If you only care about numeric solutions, you want in SymPy is nsolve, or you can use a more numerical oriented Python library. For example
sympy.nsolve(n*9**5 - 10**n-1, n, 5)
will give you the solution you are looking for.
The problem with using solve is that there are infinitely many solutions, each corresponding to a branch of the LambertW function. See WolframAlpha for the full solution set. Unfortunately, only the principal branch of LambertW is implemented in SymPy.
Until this is fixed, another way to fix the issue would be to manually evaluate the LambertW returned by solve on another branch, using mpmath.lambertw. The easiest way to do this is with lambdify:
s = sympy.solve(n*9**5 - 10**n-1, n)
import sympy.mpmath
# Replace -1 with any integer. -1 gives the other real solution, the one you want
lambdify([], s, [{'LambertW': lambda x: sympy.mpmath.lambertw(x, -1)}, "mpmath"])()
This gives [mpf('5.5125649309411875')].
The dictionary tells lambdify to evaluate the LambertW function using the -1 branch, using mpmath. The "mpmath" tells it to use mpmath for any other functions that may be in the solution.
Hi and thanks in advance for the help.
I am a beginner in Python and I am having issues with iteration. I understand itertools is probably part of the solution to my problem but I don't seem to formulate it right and I couldn't find a problem similar enough on the forums.
Based on a numpy indices vector V, another vector Y and some function f, I would like to compute something like:
for i in V[:-1]:
for j in V[i+1:]:
f(Y[i], Y[j])
... which as you know doesn't work because you understand python better than I do! It seems never to get to "j" (V not being iterable twice is what I understood so far, although I'm not sure what it means).
What would be a good way to do that? Note that it is quite important not to compute both f(Y[i],Y[j]) and f(Y[j],Y[i]) since it is symmetric and quite long to run.
Thanks!
Hi as part of my project I have been working on evaluation of arithmetic expression using reg-ex in java.
expressions are like this : 2+3/4(5+7)
first I will modify it to this : 2+3/4*(5+7)
and convert it to postfix
postfix: 23457+*/+
The procedure I have adopted is parsing all of the tokens (i.e. Integers,Operators,Open Paren,Close Paren) using Reg-Ex and then sorting by the i-th position of their occurrence. After that I converted that array of token into post-fix expression and then solved that expression, Until this point every thing is working fine.
Now I would like to extend it to solving differentiation,integration or solving quadratic equation.
foe ex: differentiate or integrate expression x^2+2*x+2
similar to http://integrals.wolfram.com/index.jsp
Is it possible? because right now I dont have any clue how to proceed with that?
Most calculators I am aware of - calculating equations/differentions and integrals using numerical analysis. This allows one to find a close solution to the exact (analytical one) solution, sometimes even for unsolveable equations (This is how we got the standard normal table, for the unsolveable normal density function)
For example, solving integrals - gaussian quardature is very common and efficeint way.
For solving equations - regula-falsi method is a simple and intuitive one
I think that most integral computation engines use the numerical approach just as amit said, however there's a way to compute it symbolically, but it's heuristic more than algorithmic, it's done by pattern matching. I think that Mathematica follows this approach.