哈希克尔方程求解实数 [英] Haskell equation solving in the real numbers
本文介绍了哈希克尔方程求解实数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Prelude> [x | x < - [1..20],x ^ 2 == 4]
[2]
(如预期的那样,只找到一个根)
现在,为什么我不能用解决方案求解方程式,因为解决方案是包含在指定范围内?
[x | x < - [0.1,0.2..2.0],x * 4 == 2]
编辑:对不起,我的意思当然是 0.1
。
解决方案
正如其他人所提到的,这不是解决方程式的有效方法,但可以用比率来完成。
Prelude> :m + Data.Ratio
Prelude Data.Ratio> [x | x < - [1%10,2%10..2],x * 4 == 2]
[1%2]
$ b 阅读 x%y
为 x除以y
。
I've just started playing with GHCi. I see that list generators basically solve an equation within a given set:
Prelude> [x | x <- [1..20], x^2 == 4]
[2]
(finds only one root, as expected)
Now, why can't I solve equations with results in ℝ, given that the solution is included in the specified range?
[x | x <- [0.1,0.2..2.0], x*4 == 2]
How can I solve such equations within real numbers set?
Edit: Sorry, I meant 0.1
, of course.
解决方案
As others have mentioned, this is not an efficient way to solve equations, but it can be done with ratios.
Prelude> :m +Data.Ratio
Prelude Data.Ratio> [x|x<-[1%10, 2%10..2], x*4 == 2]
[1 % 2]
Read x % y
as x divided by y
.
这篇关于哈希克尔方程求解实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文