开发者

Irrational number representation in any programming language?

Does anyone know of an irrational number representation type/object/class/whatever in any programming language?

All suggestions welcome.

Simply put, if I have two irrational objects, both representing the square root of five, and I multiply those objects, I want to get back the integer five, 开发者_运维问答not float 4 point lots o' 9s.

Specifically, I need the representation to be able to collect terms, not just resolve every time to an integer/float. For instance, if I want to add the square root of five to one, I don't want it to return some approximation integer/float, I want it to return an object that I can add/multiply with another irrational object, such that I can tell the object to resolve at the latest time possible to minimize the float approximation error.

Thanks much!


What you are looking for is called symbolic mathematics. You might want to try some computer algebra system like Maxima, Maple or Mathematica. There are also libraries for this purpose, for example the SymPy library for Python.


You could try sympy since you appear to be after symbolic computation and are amenable to using Python.


It looks like the already mentioned SymPy would be the most apropriate way to go - as you might do what you need and do not require that your software be written in a specific purpose proprietary language such as of the mathematical products mentioned.

On the other hand, if you don't want to introduce further dependencies, and your irrational cases are limited to multiplications of square roots, in Python it is an easy task:

class Irrational(float):
    def __new__(cls, base, radix=1):
        self = float.__new__(cls, base ** (1.0/radix))
        self.base =  base
        self.radix = radix
        return self
    def __mul__(self, other):
        if  isinstance(other, Irrational) and other.radix == self.radix:
            return Irrational(self.base * other.base, self.radix)
        return float.__mul__(self, other)

Example:

>>> a = Irrational(5,2)
>>> a
2.2360679774997898
>>> a * Irrational(5,2)
5.0

You can pursue it further and include ohter operations and corner cases. But for compes expressions, you'd soon realize you'd need to use symbolic math anyway.


To compile some things that others have said, as well as add a few solutions for other languages...


C++

Piranha

Try bluescarni's Piranha "computer algebra library"

// Include the standard I/O header.
#include <iostream>

// Include the global Piranha header.
#include <piranha/piranha.hpp>

// Import the names from the Piranha namespace.
using namespace piranha;

int main()
{
    // Piranha initialisation.
    init();
    // Print the rational number 4/3 to screen.
    std::cout << rational{4, 3} << '\n';
}

There is also a Python wrapper ("Pyranha") available.

SymEngine

SymEngine is another C++-based symbolic mathematics library, with wrappers for Python, Ruby, Julia, and Haskell. SymEngine integrates nicely with SymPy and Sage, both for Python.


Java

SymJa

Try axkr's Symja Library

>>> sin(30*degree)
1/2

>>> sin(pi/2)
1

>>> a+a+4*b^2+3*b^2
2*a+7*b^2

Alternatively, you can use Jython to get SymPy in Java.


Python

SymPy

As others have mentioned, SymPy is the go-to for symbolic mathematics in Python:

Irrational number representation in any programming language?


Ruby

Symbolic

Brainopia's Symbolic is a symbolic mathematics library (with no external dependencies) for Ruby:

0 * x           # => 0
2 + x + 1       # => x + 3
-(x-y) + 2*x    # => x + y
(x**2)**3 / x   # => x**5

Symbolic::Math.cos(x**2).diff(x)
# => -2*(sin(x**2))*x
Symbolic::Math.cos(x).taylor(x,0,3)
# => -0.5*x**2+1.0

Please feel free to edit or amend this answer with other symbolic mathematics libraries. As a rule of thumb, I haven't included any "arbitrary precision" mathematics libraries, because this is not the same thing as an infinite-precision symbolic mathematics library. I also haven't included stale packages which haven't been updated recently.


The fundamental numeric type in Matlab is a matrix of complex floats. Specifically, if you type x = 1, what you really assign to x is a a 1x1 matrix with its [0,0] element equal 1+0i.


In ruby there's http://flt.rubyforge.org/ which gives you what you want I believe.


I remember MathMorph in Smalltalk has a representation for AlgebraicNumbers (that includes radicals) as the root of a polynomial of single variable with integer coefficients, lying in a certain interval.
You'll find interesting applications of Sturm's theorem http://en.wikipedia.org/wiki/Sturm%27s_theorem
You'll have to google a bit and dig in old archives though, MathMorph is an old project...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜