Project

General

Profile

Actions

Feature #15571

open

Add methods: iroot, root, and roots

Added by jzakiya (Jabari Zakiya) almost 6 years ago. Updated almost 6 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:91320]

Description

Proposal
The rubygem roots provides a few methods to find the numerical roots
of real, complex, and integer numbers. This proposal requests including the
following three (3) methods into Ruby.

https://rubygems.org/gems/roots
https://github.com/jzakiya/roots

iroot: provide the accurate integer nth root value of any size integer

2.6.0 :002 > require 'roots'
 => true 


2.6.0 :010 > n = 12345678901234567890
 => 12345678901234567890

2.6.0 :011 > Integer.sqrt n
 => 3513641828 

2.6.0 :012 > n.iroot 2
 => 3513641828 

2.6.0 :013 > n.iroot 3
 => 2311204

2.6.0 :014 > n.iroot 4
 => 59275 

root: provide the accurate real value nth root of a real, complex, or integer numbers
roots: provide a collection of all real|complex nth values

2.6.0 :020 > n = 12345678901234567890
 => 12345678901234567890

2.6.0 :021 > Math.sqrt n
 => 3513641828.820144

2.6.0 :022 > n**(0.5)
 => 3513641828.820144 

2.6.0 :023 > n.root 2
 => 3513641828.820144 

2.6.0 :024 > n**(1.0/3)
 => 2311204.2409018343 

2.6.0 :025 > n.root 3
 => 2311204.24090183 

2.6.0 :026 > n.root 3,1
 => (2311204.24090183+0.0i)
 
2.6.0 :027 > n.root 3,2
 => (-1155602.12045092+2001561.58595532i) 

2.6.0 :028 > n.root 3,3
 => (-1155602.12045092-2001561.58595532i)
 
2.6.0 :029 > n.roots 3
 => [(2311204.24090183+0.0i), (-1155602.12045092+2001561.58595532i), (-1155602.12045092-2001561.58595532i)] 

2.6.0 :031 > n.roots 3, :real
 => [(2311204.24090183+0.0i)] 

2.6.0 :032 > n.roots 3, :complex
 => [(-1155602.12045092+2001561.58595532i), (-1155602.12045092-2001561.58595532i)] 

2.6.0 :033 > n.roots 3, :odd
 => [(2311204.24090183+0.0i), (-1155602.12045092-2001561.58595532i)] 

2.6.0 :034 > n.roots 3, :even
 => [(-1155602.12045092+2001561.58595532i)] 

2.6.0 :035 > (247823 + 398439i).root 4
 => (25.33541017+6.56622124i) 

2.6.0 :036 > (247823 + 398439i).roots 4
 => [(25.33541017+6.56622124i), (-6.56622124+25.33541017i), (-25.33541017-6.56622124i), (6.56622124-25.33541017i)] 

Motivation
Ruby 2.5 included the method Integer.sqrt. It accurately returns the
integer squareroot of integers, whereas performing Math.sqrt(n).floor
produced rounding errows once n exceeded a certain threshold.

Whereas Integer.srt solved that problem for squareroots, the same problem
exists for the other nth roots when n reaches a certain (large) value too.
Adding iroot completes providing this functionality for all nth roots.

Adding root and roots adds funcitionality either not currently present
and/or provides it in an easier to use, standard, and more flexible manner.

I created the roots gem to help me do Project Euler (https://projecteuler.net/) problems.
To probably most people|programmers, Ruby is primarily associated with web
development through frameworks like Rails, Sinatra, Hanami, etc. However Ruby
has great utility in math and numerical analysis fields. These methods provide
basic arithmetic primitives upon which higher order functions can be created
without the need to search for third-party packages. They will increase Ruby's
footprint into numerical|analysis fields now dominated by Python and Julia.

Pros

  • only 3 methods with no dependices
  • fast and numerically accurate (can change shown digits for root(s)
  • adds previously unavailable functionality
  • provides existing funcionality in an easier to use, standard, and flexible manner
  • provides more math primitives to create higher order algorithms
  • makes Ruby, out-of-the-box, more useful for doing math, cryptography, etc
  • enhances Ruby's reputation as a more math friendly language
  • makes programmers doing math Happy! :-)

Cons

  • it adds 3 methods to language core
  • better names(?)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0