GithubHelp home page GithubHelp logo

Comments (6)

RNavega avatar RNavega commented on June 3, 2024 1

Hi @shril, the output list comes from timeit.repeat() and is the time taken, in seconds, to call the piece of code one million times.
There's three results because repeat() profiles it three times by default, so we can see what was the best (the smallest) time.

On all three repeats the inline multiplication was faster.
x*x*x*x is faster than x**4 for example.

Edit: I'll look into a PR.

from cubicequationsolver.

shril avatar shril commented on June 3, 2024

@RNavega can you please explain the output list? And please further raise a PR.
@devojoyti please look into this.

from cubicequationsolver.

shril avatar shril commented on June 3, 2024

@RNavega thanks. Please raise a PR.

from cubicequationsolver.

RNavega avatar RNavega commented on June 3, 2024

@shril sorry for the delay, end of year. I'll work on a PR, thank you.

from cubicequationsolver.

RNavega avatar RNavega commented on June 3, 2024

I don't know if a PR would be useful.
If you're using Python 3+ and your a, b, c, d coefficients are always float values, then you can use this shortened code (this code starts at the findF, findG and findH calls, which were inlined):

    f = ((3.0 * c / a) - ((b*b) / (a*a))) / 3.0
    _a2 = a*a
    g = (((2.0 * (b*b*b)) / (_a2*a)) - ((9.0 * b * c) / _a2) + (27.0 * d / a)) / 27.0
    h = ((g*g) / 4.0 + (f*f*f) / 27.0)

    if f == g == h == 0:                # All 3 Roots are Real and Equal
        d_a = (d / a)
        x = (d_a ** 0.33333333333333333) if d_a >= 0.0 else -((-d_a) ** 0.33333333333333333) # 0.333... = (1.0 / 3.0)
        return np.array((x, x, x))

    elif h <= 0.0:                        # All 3 roots are Real
        i = sqrt(((g*g) / 4.0) - h)
        j = i ** 0.33333333333333333
        k_ = acos(-g / (2 * i)) / 3.0
        L = -j
        M = cos(k_)   
        N = 1.73205080756887729 * sin(k_) # 1.732... = sqrt(3.0)
        P = -b / (3.0 * a)

        x1 = 2.0 * j * M + P
        x2 = L * (M + N) + P
        x3 = L * (M - N) + P
        return np.array((x1, x2, x3))

    else:                               # One Real Root and two Complex Roots
        hSqr = sqrt(h)
        gH = -g / 2.0
        R = (gH + hSqr)
        S = R ** 0.33333333333333333 if R >= 0.0 else -((-R) ** 0.33333333333333333)
        T = (gH - hSqr)
        U = T ** 0.33333333333333333 if T >= 0.0 else -((-T) ** 0.33333333333333333)
        return np.array(((S + U) - (b / (3.0 * a)), ))

This was done reading your code as well as following the main reference (www.1728.org/cubic2.htm).

edit: This removes a few arithmetic operations as well as one redundant sqrt() and cos() calls.

from cubicequationsolver.

RNavega avatar RNavega commented on June 3, 2024

Oops, now I get why you're negating values below zero if they're being cubic-rooted. If not, the numbers become complex.
I added that back.

from cubicequationsolver.

Related Issues (4)

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.