shril / cubicequationsolver Goto Github PK
View Code? Open in Web Editor NEWPython Script to find the roots of a Cubic Equation
License: Apache License 2.0
Python Script to find the roots of a Cubic Equation
License: Apache License 2.0
comparing to "zero" is problematic
try changing the first few line to
aa=abs(a)
ab=abs(b)
ac=abs(c)
tol=max((aa,ab,ac))/1099511627776 #/2**40
if (aa<tol and ab<tol): # Case for handling Liner Equation
return np.array([(-d * 1.0) / c]) # Returning linear root as numpy array.
elif (aa < tol):
for a=-5.32282969291e-15 b=0.43709356404 c=0.819917378017 d=-0.199301067523
goes from
complex_roots: [ 8.211676669305905e+13 +0.j
-8.867187500000000e-01+353649.2408007157j
-8.867187500000000e-01-353649.2408007157j]
to
complex_roots: [ 0.2177888401916245 -2.093628352534504 ]
when numpy.roots says
complex_roots: [ 8.2116766693059141e+13 -2.0936283525344557e+00 2.1778884019162431e-01]
this came up in the context of a highly specialized pchip spline implementation I was working on
Thanks a lot for this useful code.
Would you agree to change the licence of the code to apache 2 so that it could be used in an open-source project? If yes, I'll be happy to do the corresponding pull request.
Thank you
Exactly I ended up succesfully by changing the directory for the cubicequationsolver.py file and now it returs the values for the roots in the Spyder console.
But how to plot the roots graphically by using matplotlib ?
Which commands has to be run in the Spyder console for plotting the roots graphically ?
An idea, you can squeeze some more performance juice by replacing x**2 with x*x and x**3 with x*x*x etc.
>>> from timeit import repeat
>>> repeat('findH(1, 2)', setup='def findH(g, f):\n\treturn ((g ** 2.0) / 4.0 + (f ** 3.0) / 27.0)')
[0.7711835503748148, 0.7689730036987186, 0.7664158748830658]
>>> repeat('findH(1, 2)', setup='def findH(g, f):\n\treturn ((g*g) / 4.0 + (f*f*f) / 27.0)')
[0.5918213720389645, 0.568905897817487, 0.5710548649790184]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.