# Proposing an arbitrary precision class building on BigDecimal andbeing derived from Numeric

Discussion in 'Ruby' started by ulrichmutze@aol.com, Dec 6, 2008.

1. ### Guest

Dear Ruby commuity,

this note deals with arbitrary precision arithmetics and Ruby
module BigMath and Ruby class BigDecimal.

So we are dealing with the mind children of Shigeo Kobayashi, and
my first action in promoting my proposed addition to BigMath was
to comunicate it to Shigeo.

His reply ends in the sentences:

'The only advice I can give you at this moment, is to annouce your
excelent work to Ruby community(open to any user).=20
=C2=A0...
I (and any Ruby user ) will be happy if your work is incorporated into
the BigMath library.'=20

This work defines and tests a wrapper class for Shigeo's class BigDecimal.
This wrapper makes the class fit into the framework of the standard Ruby
number classes Fixnum, Bignum, and Float by having
=C2=A0 Numeric
as its base class. The name which I propose for this class is
=C2=A0 R (which is standard mathematical usage),
other names that I considered were=20
=C2=A0 Real, BigReal, BigR.

The next unifying structural property of R ( besides R < Numeric) is that
it implements as member functions all the mathematical functions
=C2=A0 sqrt, hypot, sin, ... atan2, ... , erf, erfc
which module Math implements for class Float.

This is an interesting point:=20
Although in any OO-language terms containing calls of methods (member functi=
ons)
are cleaner and easier to read than calls of non-member functions, actual
language definitions prefer sin(x) to x.sin. Be this as it is, my class=20
R allows to write=20
=C2=A0 diff =3D x.sin**2 + x.cos**2 - 1=20
which is very small20for, say,=20
=C2=A0 x =3D R.new("1.23456789E123")
For this to work, one obviously needs to work with more than=20
the 123 decimals which come in already with the integer part of x.
So, for this computation, the default value of 40 decimals is too small.
We may set a sufficient accuracy by=20
=C2=A0 R.dig =3D 1000
On my system (an off-the shelf laptop) it takes then 6.7 seconds
to find diff.abs.log10.round as -876.

Algorithms for these mathematical functions which are suitable for
arbitrary precision are implemented in BigMath and BigDecimal based on
everywhere convergent power series expansions. Although such expansions -
take the well-knwn one for exp(x) as a prime example - converge by the=20
exponential growth of the denominators of the generic series term,
the growth of x^n may dominate the result for many, many, terms in the
early live of the series. So, such expansions are convergent rapidly only if
|x| < 1. What I did was to figure out the mathematical identities that
allow to reduce computing x.f for arbitrary x to fuction evalutions at
auxiliar arguments y satisfying |y| < 1. What is needed here, hardly
transcends the tricks which people of my generation had to exercise at schoo=
l
when working with logarithmic, exponential, and trigonometric functions
by means of printed tables instead of pocket calclators.

Of course, the question how to implement these functions by means of algorit=
hms
is independent of the question whether to use member functions or non-member
functions in their definition.=20
However, the20member function choice suggests a way of coping with=20
the number of allowed decimal places which is used in class R:=20
R has a class variable @@dig, the value of which (default is 40) controls th=
e actual
execution of any member function. It is not necessary to be aware of the fac=
t that=20
'deep inside' Shigeo's powers series algorithms different numbers of decimal
places may be used, according to the needs of the algorithm.

This may suffice as a first presentation of class R.

A complete package of Ruby code and rdoc-generated documentation can be foun=
d on
(and freely downloaded from)=20

=C2=A0 www.ulrichmutze.de
=C2=A0=20
where the section=20
=C2=A0 Free Ruby code
is the one which matters.

Every comment and suggestion for modification is wellcome!

Especially those that help to relate the present proposal to other projects
that add to he strength of Ruby as a tool in scientific computing.

Presently my idea is to make R a part of BigMath (it is a part of my=20
module AppMath, applied mathematics, in my present implementation) and to=20
become informed about the expectations that users of the BigMath library
may have concerning an arbitrary precision version of Float (which R in effe=
ct is).

Ulrich
________________________________________________________________________
AOL eMail auf Ihrem Handy! Ab sofort k=C3=B6nnen Sie auch unterwegs Ihre AOL=
email abrufen. Registrieren Sie sich jetzt kostenlos.

, Dec 6, 2008

2. ### Roger PackGuest

Re: Proposing an arbitrary precision class building on BigDe

> this note deals with arbitrary precision arithmetics and Ruby
> module BigMath and Ruby class BigDecimal.

Sounds very cool
Some random feedbacks:

> as its base class. The name which I propose for this class is
> Â  R (which is standard mathematical usage),
> other names that I considered were
> Â  Real, BigReal, BigR.

Anything except just "R" -- this could stand for anything--"random"
"radical" "the R statistics library"...

> R allows to write
> Â  diff = x.sin**2 + x.cos**2 - 1
> which is very small20for, say,
> Â  x = R.new("1.23456789E123")

> Algorithms for these mathematical functions which are suitable for
> arbitrary precision are implemented in BigMath and BigDecimal based on
> everywhere convergent power series expansions. Although such expansions

Have you glanced at lib gmp?

Take care.
-=R
--
Posted via http://www.ruby-forum.com/.

Roger Pack, Dec 8, 2008

3. ### Ulrich MutzeGuest

Re: Proposing an arbitrary precision class building on BigDe

Roger Pack wrote:
>> this note deals with arbitrary precision arithmetics and Ruby
>> module BigMath and Ruby class BigDecimal.

>
> Sounds very cool
> Some random feedbacks:
>
>> as its base class. The name which I propose for this class is
>> Â  R (which is standard mathematical usage),
>> other names that I considered were
>> Â  Real, BigReal, BigR.

>
> Anything except just "R" -- this could stand for anything--"random"
> "radical" "the R statistics library"...
>
>
>
>> R allows to write
>> Â  diff = x.sin**2 + x.cos**2 - 1
>> which is very small20for, say,
>> Â  x = R.new("1.23456789E123")

>
>
>> Algorithms for these mathematical functions which are suitable for
>> arbitrary precision are implemented in BigMath and BigDecimal based on
>> everywhere convergent power series expansions. Although such expansions

>
> Have you glanced at lib gmp?
>
> Take care.
> -=R

Hi Roger,
thanks for your respond.
I glanced at gmp again, and found no sin, cos, exp, ... implemented
there.
Did I miss something?
You hold a strong oppinion concerning name R and so do I.
In short,
classes express concepts (Stroustrup),
deep concepts are expressed by classes with short names (Ulrich).
For a mathematically inclined programmer there is nothing more
fundamental than the basic number types.
This is only to explain that the material on my website
www.ulrichmutze.de
still uses the name R.
If the matter should finds its way into a broader discussion, I would be
open
for accepting majority wishes.
Thanks again
Ulrich
--
Posted via http://www.ruby-forum.com/.

Ulrich Mutze, Dec 16, 2008
4. ### Roger PackGuest

Re: Proposing an arbitrary precision class building on BigDe

> Hi Roger,
> thanks for your respond.
> I glanced at gmp again, and found no sin, cos, exp, ... implemented
> there.
> Did I miss something?

R definitely looks like a cool library.

MPFR? I actually really know nothing about it, I just ran into their
page one day and thought it looked interesting

> You hold a strong oppinion concerning name R and so do I.
> In short,
> classes express concepts (Stroustrup),
> deep concepts are expressed by classes with short names (Ulrich).
> For a mathematically inclined programmer there is nothing more
> fundamental than the basic number types.
> This is only to explain that the material on my website
> www.ulrichmutze.de
> still uses the name R.
> If the matter should finds its way into a broader discussion, I would be
> open
> for accepting majority wishes.

You might be able to get away with creating a gem first then suggesting
it be merged into core. Or you could ping ruby-core and see if they
have feedback on incorporating it already.

Other naming convention possibilities that come to mind:
"Real"
"BigFloat"
"BigDecimal2"
Good luck!
-=R
--
Posted via http://www.ruby-forum.com/.

Roger Pack, Dec 18, 2008
5. ### Ulrich MutzeGuest

Re: Proposing an arbitrary precision class building on BigDe

Roger Pack wrote:
>> Hi Roger,
>> thanks for your respond.
>> I glanced at gmp again, and found no sin, cos, exp, ... implemented
>> there.
>> Did I miss something?

> R definitely looks like a cool library.
>
> MPFR? I actually really know nothing about it, I just ran into their
> page one day and thought it looked interesting

MPFR offers all I looked for (in the wrong places as it is
clear now). Thank you for this hint!
I did a computation of the graph of cos(x)*cos(x) +sin(x)*sin(x) - 1
using
1. Ruby and my class R
2. C++ and class mpfr::mpreal of Pavel Holoborodko.
As a matter of fact, the C++ version was 70 times faster.

>
>> You hold a strong oppinion concerning name R and so do I.
>> In short,
>> classes express concepts (Stroustrup),
>> deep concepts are expressed by classes with short names (Ulrich).
>> For a mathematically inclined programmer there is nothing more
>> fundamental than the basic number types.
>> This is only to explain that the material on my website
>> www.ulrichmutze.de
>> still uses the name R.
>> If the matter should finds its way into a broader discussion, I would be
>> open
>> for accepting majority wishes.

>
> You might be able to get away with creating a gem first then suggesting

The gem is available as appmath-0.0.1.gem , the name of the real number
class is still R. It presents also complex numbers, vectors and
matrices
of multiple precision components.
I also implement singular value decomposition (in pure Ruby) and
multiple precision. It works, and was surprised how fast it is.
However, also here, C++ turned out to be even much faster.

> it be merged into core. Or you could ping ruby-core and see if they
> have feedback on incorporating it already.
>
> Other naming convention possibilities that come to mind:
> "Real"
> "BigFloat"
> "BigDecimal2"
> Good luck!
> -=R

Thanks again and Good luck too!

--
Posted via http://www.ruby-forum.com/.

Ulrich Mutze, Feb 5, 2009
6. ### Roger PackGuest

Re: Proposing an arbitrary precision class building on BigDe

> The gem is available as appmath-0.0.1.gem , the name of the real number
> class is still R. It presents also complex numbers, vectors and
> matrices
> of multiple precision components.
> I also implement singular value decomposition (in pure Ruby) and
> multiple precision. It works, and was surprised how fast it is.
> However, also here, C++ turned out to be even much faster.

Thanks for your work on this.
-=r
--
Posted via http://www.ruby-forum.com/.

Roger Pack, Feb 5, 2009

### Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.