The Ruby-gem appmath (for applied mathematics)
implements arbitrary precision mathematics in a form
which is convenient to use. As a reality test of the
underying ideas it applies this arbitrary precision
programming scheme to linear algebra (including
singular value decomposition and Penrose inverse
of real matrices), graphical representation of functions,
and physical simuations (2 dimensional Kepler motion).
The origin and motivation of the work becomes clear
from the follwing note that I sent out to the
Ruby-Talk mailing list and which is placed in Ruby Forum
under the heading
Proposing an arbitrary precision class building on
BigDecimal and being derived from Numeric.
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.sin2 + x.cos2 - 1
which is very small for, 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, the member 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
Further, file rnum.rb of the present package contains a full description
of the scope of this gem.