Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Proposing an arbitrary precision class building on BigDecimal and being derived from Numeric

2 views
Skip to first unread message

ulric...@aol.com

unread,
Dec 6, 2008, 2:00:48 PM12/6/08
to
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).
 ...
I (and any Ruby user ) will be happy if your work is incorporated into
the BigMath library.'

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
  Numeric
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.

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

This is an interesting point:
Although in any OO-language terms containing calls of methods (member functions)
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
R allows to write
  diff = x.sin**2 + x.cos**2 - 1
which is very small20for, say,
  x = R.new("1.23456789E123")
For this to work, one obviously needs to work with more than
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
  R.dig = 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
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 school
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 algorithms
is independent of the question whether to use member functions or non-member
functions in their definition.
However, the20member function choice suggests a way of coping with
the number of allowed decimal places which is used in class R:
R has a class variable @@dig, the value of which (default is 40) controls the actual
execution of any member function. It is not necessary to be aware of the fact that
'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 found on
(and freely downloaded from)

  www.ulrichmutze.de
 
where the section
  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
module AppMath, applied mathematics, in my present implementation) and to
become informed about the expectations that users of the BigMath library
may have concerning an arbitrary precision version of Float (which R in effect is).

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

Roger Pack

unread,
Dec 8, 2008, 1:05:44 AM12/8/08
to
> 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/.

Ulrich Mutze

unread,
Dec 16, 2008, 6:37:41 AM12/16/08
to

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

Roger Pack

unread,
Dec 18, 2008, 12:24:26 AM12/18/08
to
> 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

Ulrich Mutze

unread,
Feb 5, 2009, 2:20:08 PM2/5/09
to
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!

Roger Pack

unread,
Feb 5, 2009, 3:13:46 PM2/5/09
to

> 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

0 new messages