Haskell 中的多项式因式分解
在 hammar 的帮助下,我制作了一个模板 Haskell 位,它编译
$(zModP 5)
为
newtype Z5 = Z5 Int
instance Additive.C Z5 where
(Z5 x) + (Z5 y) = Z5 $ (x + y) `mod` 5
...
我现在面临的我认为我无法通过这种方式解决问题。
关于多项式的一个值得注意的事实是,如果它们对某个素数 p
取模是不可约的,那么它们在有理数上是不可约的。我已经有了一种方法,可以强力尝试在给定(有限)域上对多项式进行因式分解。
我想尝试为多个字段运行此函数。这就是我想要的:
isIrreducible :: (FiniteField.C a) => Poly.T a -> Bool
isIrreducible p = ...
intPolyIrreducible :: Poly.T Int -> Bool
intPolyIrreducible p = isIrreducible (p :: Poly.T Z2) ||
isIrreducible (p :: Poly.T Z3) ||
isIrreducible (p :: Poly.T Z5) ||
...
基本上我想尝试针对大量“除法”的定义运行我的因式分解算法。
我认为这对 TH 来说是可能的,但似乎需要很长时间。我想知道将算术运算作为参数传递给 isIrrreducible
是否会更容易?
或者,这似乎可能是 Newtype 模块可以帮助解决的问题,但我无法想象如果不使用 TH 的话它会如何工作,这将同样困难......
任何人都对如何最好地完成有任何想法这?
With hammar's help I have made a template Haskell bit which compiles
$(zModP 5)
to
newtype Z5 = Z5 Int
instance Additive.C Z5 where
(Z5 x) + (Z5 y) = Z5 $ (x + y) `mod` 5
...
I'm now facing a problem that I don't think I can solve this way.
A remarkable fact about polynomials is that they are irreducible in the rationals if they are irreducible modulo some prime p
. I already have a method which brute-force attempts to factor polynomials over a given (finite) field.
I want to try running this function for multiple fields. Here's kind of what I want:
isIrreducible :: (FiniteField.C a) => Poly.T a -> Bool
isIrreducible p = ...
intPolyIrreducible :: Poly.T Int -> Bool
intPolyIrreducible p = isIrreducible (p :: Poly.T Z2) ||
isIrreducible (p :: Poly.T Z3) ||
isIrreducible (p :: Poly.T Z5) ||
...
Basically I want to try running my factoring algorithm for a large number of definitions of "division".
I think this is possible to do with TH, but it seems like it would take forever. I'm wondering if it would be easier to just pass my arithmetical operations in as a parameter to isIrreducible
?
Alternatively it seems like this might be something the Newtype module could help with, but I can't think of how it would work without using TH in a way which would be just as hard...
Anyone have any thoughts on how best to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用类型级数字在有限字段中进行计算,例如使用
type-level
包:此方法的优点是不需要为每个数字类型创建新的模板 haskell 实例。缺点是它可能比模板 haskell 解决方案慢,因为每个操作都通过类字典传递有限域的大小。
You can do computations in finite fields using type-level numerics, for example with the
type-level
package:This approach has the advantage of not requiring a new template haskell instance for each numeric type. The disadvantage is that it's probably slower than the template haskell solution, since each operation passes the size of the finite field around via a class dictionary.
这在一定程度上取决于 Poly.T 的样子,但是你能编写一个类型的函数(例如)
吗?如果是这样,那么使用
Z
类型可能是有意义的,当其模数不匹配时,其操作会在运行时失败:然后您可以在普通的旧 Haskell 中编写类似的内容:
当然,这是一个有点不太类型安全。但从参数化来看,
fmap (Z m)
显然不会引入任何不匹配的模数。It depends a little bit what Poly.T looks like, but can you write a function of type (for example)
? If so, it might make sense to have a
Z
type whose operations fail at runtime when their modulus doesn't match:Then you can write something like this in plain old Haskell:
Of course, this is a tad less type-safe. But it's clear from parametricity that
fmap (Z m)
won't introduce any mismatched moduluses.