任何编程语言中的无理数表示?

发布于 2024-10-25 22:46:02 字数 279 浏览 2 评论 0原文

有谁知道任何编程语言中的无理数表示类型/对象/类/任何东西?

欢迎所有建议。

简而言之,如果我有两个无理对象,都代表 5 的平方根,并且我将这些对象相乘,我想要返回整数 5,而不是浮点 4 点的 9。

具体来说,我需要表示能够收集术语,而不仅仅是每次解析为整数/浮点数。例如,如果我想将五的平方根加到一,我不希望它返回一些近似整数/浮点数,我希望它返回一个可以与另一个无理对象相加/相乘的对象,这样我可以告诉对象尽可能晚地解析,以最小化浮点近似误差。

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

苦行僧 2024-11-01 22:46:03

您正在寻找的称为符号数学。您可能想尝试一些计算机代数系统,例如 Maxima、Maple 或 Mathematica。还有一些用于此目的的库,例如用于 Python 的 SymPy 库。

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.

我ぃ本無心為│何有愛 2024-11-01 22:46:03

您可以尝试 sympy 因为您似乎在进行符号计算并且适合使用 Python。

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

雨的味道风的声音 2024-11-01 22:46:03

看起来已经提到的 SymPy 将是最合适的方法 - 正如您可能所做的那样您需要和不要求您的软件以特定目的专有语言(例如提到的数学产品)编写。

另一方面,如果您不想引入进一步的依赖关系,并且您的非理性情况仅限于平方根的乘法,那么在 Python 中这是一项简单的任务:

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)

示例:

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

您可以进一步追求它并包括其他操作和极端情况。但对于 compes 表达式,您很快就会意识到无论如何都需要使用符号数学。

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.

倾城°AllureLove 2024-11-01 22:46:03

编译一些别人说过的东西,以及添加一些其他语言的解决方案...


C++

食人鱼

尝试 bluescarni 的 Piranha“计算机代数库”

// 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';
}

还有一个 Python 包装器(“天花”)可用。

符号引擎

SymEngine 是另一个基于 C++ 的符号数学库,带有 Python 的包装器、RubyJuliaHaskell。 SymEngine 与 SymPy 和 Sage 很好地集成,两者都适用于 Python。


爪哇

SymJa

尝试 axkr 的 Symja 库

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

>>> sin(pi/2)
1

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

或者,您可以使用 Jython 获取 SymPy 在 Java 中。


Python

符号

正如其他人提到的,SymPy 是 Python 中符号数学的首选

输入图片此处描述


红宝石

象征性

Brainopia 的 Symbolic 是一个 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

请感受一下可以使用其他符号数学库自由编辑或修改此答案。根据经验,我没有包含任何“任意精度”数学库,因为这与无限-不同。精度符号数学库。我也没有包含最近未更新的陈旧软件包。

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:

enter image description here


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.

从此见与不见 2024-11-01 22:46:03

Matlab 中的基本数字类型是复数浮点矩阵。具体来说,如果您输入 x = 1,您真正分配给 x 的是一个 1x1 矩阵,其 [0,0] 元素等于 1+0i。

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.

◇流星雨 2024-11-01 22:46:03

在 ruby​​ 中,有 http://flt.rubyforge.org/ 我相信它可以给你你想要的东西。

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

我做我的改变 2024-11-01 22:46:03

我记得 Smalltalk 中的 MathMorph 将代数数字(包括根式)表示为具有整数系数的单变量多项式的根,位于某个区间内。
您会发现 Sturm 定理的有趣应用 http://en.wikipedia.org/wiki/Sturm% 27s_定理
不过,你必须用谷歌搜索一下并挖掘旧档案,MathMorph 是一个旧项目......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文