develooper Front page | perl.perl6.users | Postings from July 2020

Re: root cubic

Thread Previous | Thread Next
Tobias Boege
July 9, 2020 21:30
Re: root cubic
Message ID:
On Thu, 09 Jul 2020, Tobias Boege wrote:
> Now, there are effective ways to represent algebraic numbers like 9 ** (1/3)
> in such a way that you can do arithmetic with them, but I'm not aware of any
> implementation of that available to Raku. For someone with enough tuits,
> I think this [1] can serve as a sort of high-level manual for creating such
> a module. In terms of ready-made non-Raku packages, I only know of CGAL [2],
> which provides a clean and even (template-)parametric interface to real
> algebraic numbers. In fact, I might have a try at NativeCall'ing it this
> evening.

Here is a super spartan version of this:

The module is:

  - not the most straightforward to build (but given all dependencies,
    zef should do everything required automatically, and it should
    warn about missing dependencies),
  - not well integrated with the rest of Raku,
  - untested.

But it accomplishes the following:

  my \η = - (8ra).kth-root(4);

Turn the literal 8 into a Real::Algebraic number so that methods and
operator overloads work, then take its real, positive 4th root and negate
that number for good measure. η is now an instance of Real::Algebraic.

  say η ** -4 == (1/8)ra;  # OUTPUT: «True»

As expected, raising it to the -4th power produces another Real::Algebraic
number (in some hidden internal representation) that can be successfully
compared for equality against 1/8.

Note that the Num version of this, `8.sqrt.sqrt ** -4 == 1/8`, yields
False, just like `0.1 + 0.2 == 0.3` is false in languages that don't
use rationals for decimal literals.

And finally, there is a FatRat coercer that gives you arbitrarily close
rational approximations, here accurate to within at least 200 decimals
(compare with WolframAlpha, for instance):

  say η.FatRat(1e-200).nude;
  (-2319046107914249546164017690226327191927995914167183915057429614886355365218221502899659586060964349175885087790712999436533151194306883153880674634813417790513862919501362233566580543228616216472448597249046963887795 1378913065775496824682182051857728448902028277271278088224317349054049721856053955032165000485952146958446223387833982704161766047792183079895777875237766653530662154044294980748355504146827894396365898183024673030144)

But this is as far as my interest in this goes at the moment. Hope it
is interesting to some.


"There's an old saying: Don't change anything... ever!" -- Mr. Monk

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About