Irrational number representation in any programming language?

会有一股神秘感。 提交于 2019-11-30 19:20:25

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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!