GithubHelp home page GithubHelp logo

shril / cubicequationsolver Goto Github PK

View Code? Open in Web Editor NEW
40.0 40.0 16.0 18 KB

Python Script to find the roots of a Cubic Equation

License: Apache License 2.0

Python 100.00%
equation-solver python scripting

cubicequationsolver's People

Contributors

devojoyti avatar shril avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cubicequationsolver's Issues

problematic round off error when abs(a)<max(abs(b),abs(c))/2**40, solution listed

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

License

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

CubicEquationSolver and Spyder

skaermbillede 2018-11-25 03 10 33

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 ?

Replace power operator with inline multiplications

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]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.