C++ value class that preserves and simplifies radicals?

T

thirtyseven

I was wondering if anyone knew of some public code for a c++ class that
represents a value, but can preserve fractions and powers and such
without just turning the value into a float.

Hard to explain without an example...

I want a class that I can assign a value of (1 + sqrt(5)) to, but it
will keep this "exact" value symbolically... so when I multiply this
value by itself, it will now internally be stored as (6 + 2*sqrt(5))
(and can of course print itself in symbolic format when requested)

I made a fraction class already that internally stores a numerator and
a denominator. So you could store a variable like 3/7, then add it to
another of 5/14, and it would then be 11/14.

So I want to make this new class that's even better at keeping an exact
form, but I figured I'd ask if anyone knows of any similar projects out
there.

Thanks.

- 37
 
A

Alan Johnson

thirtyseven said:
I was wondering if anyone knew of some public code for a c++ class that
represents a value, but can preserve fractions and powers and such
without just turning the value into a float.

Hard to explain without an example...

I want a class that I can assign a value of (1 + sqrt(5)) to, but it
will keep this "exact" value symbolically... so when I multiply this
value by itself, it will now internally be stored as (6 + 2*sqrt(5))
(and can of course print itself in symbolic format when requested)

I made a fraction class already that internally stores a numerator and
a denominator. So you could store a variable like 3/7, then add it to
another of 5/14, and it would then be 11/14.

So I want to make this new class that's even better at keeping an exact
form, but I figured I'd ask if anyone knows of any similar projects out
there.

Thanks.

- 37

You might check out the GNU Multiple Precision library. It may or may
not fit your needs.

http://www.swox.com/gmp/index.orig.html

-Alan
 
B

Bob Hairgrove

I was wondering if anyone knew of some public code for a c++ class that
represents a value, but can preserve fractions and powers and such
without just turning the value into a float.

Hard to explain without an example...

I want a class that I can assign a value of (1 + sqrt(5)) to, but it
will keep this "exact" value symbolically... so when I multiply this
value by itself, it will now internally be stored as (6 + 2*sqrt(5))
(and can of course print itself in symbolic format when requested)

I made a fraction class already that internally stores a numerator and
a denominator. So you could store a variable like 3/7, then add it to
another of 5/14, and it would then be 11/14.

So I want to make this new class that's even better at keeping an exact
form, but I figured I'd ask if anyone knows of any similar projects out
there.

At first, I thought you might want to use function objects, but on
pondering this some more I don't see how you can avoid a rounding
error when evaluating sqrt(5)*sqrt(5) unless you deal with it up
front, i.e. increase the precision of the sqrt() to compensate for it.
IOW, you would probably get something like (5.999987 + 2*sqrt(5))
instead of (6 + 2*sqrt(5)).

You might want to look at your STL's implementation of std::complex
for starters.
 
T

thirtyseven

Alan, I'll look into that, thanks.

Bob,

The point is, I want something that will *never* round, because it'll
never deal with floats (besides trivial ones like .5) unless it's
asked to. In other words, when you have a variable assigned to (1 +
sqrt(5)) and you multiply it by itself, it will internally do the
algebra to get 1 + sqrt(5) + sqrt(5) + 5, then simplify to 6 + 2sqrt(5)

I know this is *not* easy. Each object could be composed of other
objects in multiple ways, and they would be allocating memory on the
fly to do this.

So if an object was actually a sum of 9 other objects (like sqrt(2) +
sqrt(3) + sqrt(5) + ....) and you multiplied it by 2, it could either
distribute that 2 across its 9 parts, or it could become a product of 2
objects (one being a 2, and one being the original object)... neither
of these is the "best" way to represent the total value... you don't
know until you perform another operation on it.

The hard part really is the simplification... I don't think the
aggregation is *that* tricky.

This idea came to me when I was solving a system of 4 simultaneous
equations... there were lots of sqrt(2)'s and sqrt(5)'s in it... I
threw it in my calculator, and instantly got values for the 4
coefficients, but I was curious what the *exact* values were... so I
want a data type I can feed to a simult solver algorithm and end up
with the exact answers.

Hope that clarifies what I'm lookin' for.

- 37
 
D

Donovan Rebbechi

I was wondering if anyone knew of some public code for a c++ class that
represents a value, but can preserve fractions and powers and such
without just turning the value into a float.

Hard to explain without an example...

I want a class that I can assign a value of (1 + sqrt(5)) to, but it
will keep this "exact" value symbolically... so when I multiply this
value by itself, it will now internally be stored as (6 + 2*sqrt(5))
(and can of course print itself in symbolic format when requested)

I made a fraction class already that internally stores a numerator and
a denominator. So you could store a variable like 3/7, then add it to
another of 5/14, and it would then be 11/14.

So I want to make this new class that's even better at keeping an exact
form, but I figured I'd ask if anyone knows of any similar projects out
there.

One way to view these algebraic numbers is as multivariate polynomials in the
numbers p^{1/n} where p is prime, n is an integer.

My suggestion would be to start by looking for code from a multi variable
polynomial class. That way, at least you already have the code to perform
addition and subtraction, as well as the basic multiplication algorithm.
Your variables would be of the form v_pa p^{1/n) for prime number p and
integer n, and an integer term v_11. Then you just need a reduction algorithm
that applies the relation v_pn^{n} = p v_11.

Cheers,
 
R

Richard Herring

Bob Hairgrove said:
At first, I thought you might want to use function objects, but on
pondering this some more I don't see how you can avoid a rounding
error when evaluating sqrt(5)*sqrt(5) unless you deal with it up
front, i.e. increase the precision of the sqrt() to compensate for it.

The key is *not* to evaluate the sqrt at all, but to store the
expression symbolically, and only to calculate it at the very last
moment. Searching on "symbolic algebra" and "lazy evaluation" might give
some clues.

Sooner or later someone will write a library that does it with
compile-time template magic ;-)
IOW, you would probably get something like (5.999987 + 2*sqrt(5))
instead of (6 + 2*sqrt(5)).

You might want to look at your STL's implementation of std::complex
for starters.

I doubt if that'll help. It probably does some very straightforward (in
some cases, simple to the point of naivety) floating-point arithmetic.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top