任何编程语言中的无理数表示?
有谁知道任何编程语言中的无理数表示类型/对象/类/任何东西?
欢迎所有建议。
简而言之,如果我有两个无理对象,都代表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在寻找的称为符号数学。您可能想尝试一些计算机代数系统,例如 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.
您可以尝试 sympy 因为您似乎在进行符号计算并且适合使用 Python。
You could try sympy since you appear to be after symbolic computation and are amenable to using Python.
看起来已经提到的 SymPy 将是最合适的方法 - 正如您可能所做的那样您需要和不要求您的软件以特定目的专有语言(例如提到的数学产品)编写。
另一方面,如果您不想引入进一步的依赖关系,并且您的非理性情况仅限于平方根的乘法,那么在 Python 中这是一项简单的任务:
示例:
您可以进一步追求它并包括其他操作和极端情况。但对于 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:
Example:
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.
编译一些别人说过的东西,以及添加一些其他语言的解决方案...
C++
尝试 bluescarni 的 Piranha“计算机代数库”
还有一个 Python 包装器(“天花”)可用。
SymEngine 是另一个基于 C++ 的符号数学库,带有 Python 的包装器、Ruby、Julia 和 Haskell。 SymEngine 与 SymPy 和 Sage 很好地集成,两者都适用于 Python。
爪哇
尝试 axkr 的 Symja 库
或者,您可以使用 Jython 获取 SymPy 在 Java 中。
Python
正如其他人提到的,SymPy 是 Python 中符号数学的首选:
红宝石
Brainopia 的 Symbolic 是一个 Ruby 的符号数学库(没有外部依赖):
请感受一下可以使用其他符号数学库自由编辑或修改此答案。根据经验,我没有包含任何“任意精度”数学库,因为这与无限-不同。精度符号数学库。我也没有包含最近未更新的陈旧软件包。
To compile some things that others have said, as well as add a few solutions for other languages...
C++
Try bluescarni's Piranha "computer algebra library"
There is also a Python wrapper ("Pyranha") available.
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
Try axkr's Symja Library
Alternatively, you can use Jython to get SymPy in Java.
Python
As others have mentioned, SymPy is the go-to for symbolic mathematics in Python:
Ruby
Brainopia's Symbolic is a symbolic mathematics library (with no external dependencies) for Ruby:
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.
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.在 ruby 中,有 http://flt.rubyforge.org/ 我相信它可以给你你想要的东西。
In ruby there's http://flt.rubyforge.org/ which gives you what you want I believe.
我记得 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...