Re: Large Number support in Matlab
- From: Berk Birand <dont@xxxxxxxx>
- Date: Sat, 07 Jul 2007 16:02:14 -0400
On Sat, 07 Jul 2007 19:51:44 +0000, Roger Stafford wrote:
In article <pan.2007.07.07.19.08.18.705791@xxxxxxxx>, Berk Birand
<dont@xxxxxxxx> wrote:
Hi,------------------
I am doing research in my university's cryptography lab. For one of our
projects, we need to generate a table with certain probability values,
obtained by making many combinatorics calculations including factorials,
combinations (nchoosek) etc..
The code was fully written using Matlab, and worked perfectly well.
However, yesterday I was assigned the task of converting the code from
Matlab to Maple because supposedly Matlab couldn't perform high accuracy
calculations on large numbers. This apparently caused some rounding
problems.
When I first heard about this, I explained to my advisor that the Matlab
was *the* program to use when making numerical calculations on large
datasets, while Maple was for analytical/symbolical calculations.
Therefore Matlab should be able to do any calculations much better than
any other math package. Or so I thought.
I then tried some simple operations, and was amazed by the results! Doing
factorial(200)
in Matlab, produces "Inf", while doing the same thing in Maple instantly
gives out the value. What's even more amazing, is that my handheld HP
calculator can also give me the answer to that (7.88E374) within 2
seconds. Similar errors in nchoosek (which says the result is not precise,
whereas binomial in Maple gives out the exact result) can easily be
discovered.
Surely there has to be something wrong with this!! How can a $1,000
software, running on a $2,000, 2.5GHz computer not do an operation that a
$150 calculator can!
Is there a secret option to allow these? A package, a module? I spent most
of my friday trying to get this to work in Matlab instead of writing it
over again in Maple. I still think there has to be a way....
Thanks in advance,
Berk Birand
That fact is that double precision floating point numbers in matlab
cannot calculate an exact factorial past 18 factorial. This is because
higher factorials require more than the 53-bit accuracy that is present in
'double' numbers. It will overflow to infinity for factorials past 170
factorial because of the 'realmax' limitation. Your $150 calculator may
be able to get finite answers for larger factorials but you can be sure
these aren't exact past the range of digits it possesses. For a ten-digit
calculator, anything past 15 factorial would only be an approximation.
However, matlab is quite capable of the same precision as Maple if you
use their Symbolic Toolbox. In fact it uses Maple. This is because the
quantities dealt with here are strings of indefinite length, and any
desired accuracy can be obtained with it provided the accuracy level is
set sufficiently high. Just to prove it, here is 200 factorial, courtesy
of the matlab Symbolic Toolbox:
788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
Of course you must realize that the speed of execution becomes slower as
the number of digits climbs.
Thanks a lot for your help Roger! Your explanation was truly impeccable.
I am not very experienced with the Symbolic Toolbox (yet), but I will
definitely start reading more about it.
But just to make sure I'm on the right path, is there a sure way of using
Maple variables in Matlab? Suppose I use the binomial function (which is
called nchoosek in Matlab) through the maple() function. Would I be able
to use the return value for native Matlab operations?
Thanks again for you help!
bB
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: Large Number support in Matlab
- From: Roger Stafford
- Re: Large Number support in Matlab
- References:
- Large Number support in Matlab
- From: Berk Birand
- Re: Large Number support in Matlab
- From: Roger Stafford
- Large Number support in Matlab
- Prev by Date: Re: Large Number support in Matlab
- Next by Date: Re: Large Number support in Matlab
- Previous by thread: Re: Large Number support in Matlab
- Next by thread: Re: Large Number support in Matlab
- Index(es):
Relevant Pages
|