我试图在他的2015年斯坦福大学讲座中实施Andreas Noack的草图后的GF类型,但我在早期遇到了一些问题。我使用的朱莉娅0.3.10朱利亚参数构造函数 - 外部构造函数的问题
他相关的代码如下:
# Scalar finite fields
immutable GF{P,T<:Integer} <: Number
data::T
function GF(x::Integer)
return new(mod(x, P))
end
end
# methods for scalar finite field
import Base: convert, inv, one, promote_rule, show, zero
function call{P}(::Type{GF{P}}, x::Integer)
if !isprime(P)
throw(ArgumentError("P must be a prime"))
end
return GF{P,typeof(x)}(mod(x, P))
end
convert{P,T}(::Type{GF{P,T}}, x::Integer) = GF{P}(x)
convert{P}(::Type{GF{P}}, x::Integer) = GF{P}(x)
convert{P,T}(::Type{GF{P,T}}, x::GF{P}) = GF{P,T}(x.data)
promote_rule{P,T1,T2<:Integer}(::Type{GF{P,T1}}, ::Type{T2}) = GF{P,promote_type(T1,T2
)}
show(io::IO, x::GF) = show(io, x.data)
当您尝试,只是像这样定义
GF{2}(11)
东西,你得到
所以出现的问题类型不能构造
好的,所以没有自动构造函数。
GF{2,Int64}(11)
工作正常。
问题是没有自动构造函数的其他函数(如zero(x))失败。
企图使外部构造没有工作对我来说:
我觉得GF{P}(x::Integer) = GF{P,Int64}(x)
应该工作,但我得到
警告:静态参数P不签名在发生了GF在[4]:1。 该方法将不可调用。
基本上我跑出来的想法,如何指定像 GF打电话{3}(X)应建立GF的实例{3中的typeof(X)}(x)的
我知道我错过了一些非常明显的东西。
谢谢
很多感谢,这是巨大的帮助,移动到开发分支似乎是要走的路... – Robin
@Robin如果你觉得这个(优秀)答复回答你的问题,那么请通过点击勾号标记回应并考虑投票。干杯。 –