in reply to solve cubic equations

This is an area where I actually prefer to use Python instead of Perl, anytime I implement MATHS!
import math def cubic(a,b,c): q = a*a/9 - b/3 r = (a*a/27 - b/6)*a + c/2 s = a/-3 d = r*r - q*q*q if d > 0: t = (math.sqrt(d) + abs(r)) ** (1/3) u = (t + q/t) return s - u if r > 0 else s + u else: t = math.atan2(math.sqrt(-d), r) / 3 u = 2 * math.sqrt(q) return ( s - u * math.cos(t), s - u * math.cos(t + 2/3*math.pi), s - u * math.cos(t - 2/3*math.pi) ) print( cubic(10,10,-10) )

Replies are listed 'Best First'.
Re^2: solve cubic equations (Python)
by no_slogan (Deacon) on May 03, 2017 at 16:40 UTC
    Not sure what the advantage of Python is in this case. Your code is basically the same, except that it doesn't have any dollar signs or semicolons. I never liked Python's "x if cond else y" as a replacement for "cond ? x : y", it just doesn't seem right to put the condition in the middle of the expression. But I have to admit that Python's built-in multiple precision ints are a huge advantage for certain kinds of math programming.
        Try and solve a few problems on hackerrank.com that way. Their cpu time restrictions are usually too stringent for Math::BigInt, even with a "fast" backend. Python ints are built-in, bignum is an afterthought.
      "Your code is basically the same, except that it doesn't have any dollar signs or semicolons."

      Precisely why it looks more like MATHS. :)

        > > "Your code is basically the same, except that it doesn't have any dollar signs or semicolons."

        > Precisely why it looks more like MATHS. :)

        You don't like sigils?

        Try this

        use strict; use warnings; $,="\t"; my ($a,$b,$c); sub A() :lvalue { $a }; sub B() :lvalue { $b }; sub C() :lvalue { $c }; A = 1; B = A + 1; C = A + B; print A, B, C, B**3, sqrt(B);

        1    2    3    8    1.4142135623731

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: solve cubic equations (Python)
by Anonymous Monk on May 05, 2017 at 15:59 UTC
    This code does not produce the right answer.
    It produces
    (-9.433363736780528, -9.433363736780528, -9.433363736780528)
    Adding ".0" to all the numbers in the code seems to help.
      Yep that's Python, if the first assignment (declaration) is an integer the variable is typed to be an integer and all operators will only perform integer operations.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Python variables aren't typed. The result type is determined by the value (in Perl, it's usually determined by the operator). Float-vs-int-division is one of the annoying unnecessary incompatibilities in python3.
        > python2.7 -c 'print(5/2)' 2 > python3.5 -c 'print(5/2)' 2.5
        Precisely why Python is faster than Perl at runtime. If you would spend your energy learning new tools instead of cursing them ... oh right ... we are talking about LanX.
      Thanks. The point was simply to show how using the right tool for the job leads to a cleaner solution. For all things string related, I would pick Python as a last resort.