当前位置:K88软件开发文章中心编程语言非主流编程语言Julia → 文章内容

Julia 方法

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:28:12

由 陈 创建,youj 最后一次修改 2016-08-12 方法函数中说到,函数是从参数多元组映射到返回值的对象,若没有合适返回值则抛出异常。实际中常需要对不同类型的参数做同样的运算,例如对整数做加法、对浮点数做加法、对整数与浮点数做加法,它们都是加法。在 Julia 中,它们都属于同一对象: + 函数。对同一概念做一系列实现时,可以逐个定义特定参数类型、个数所对应的特定函数行为。方法是对函数中某一特定的行为定义。函数中可以定义多个方法。对一个特定的参数多元组调用函数时,最匹配此参数多元组的方法被调用。函数调用时,选取调用哪个方法,被称为重载。 Julia 依据参数个数、类型来进行重载。定义方法Julia 的所有标准函数和运算符,如前面提到的 + 函数,都有许多针对各种参数类型组合和不同参数个数而定义的方法。定义函数时,可以像复合类型中介绍的那样,使用 :: 类型断言运算符,选择性地对参数类型进行限制:julia> f(x::Float64, y::Float64) = 2x + y;此函数中参数 x 和 y 只能是 Float64 类型: julia> f(2.0, 3.0) 7.0如果参数是其它类型,会引发 “no method” 错误: julia> f(2.0, 3) ERROR: `f` has no method matching f(::Float64, ::Int64) julia> f(float32(2.0), 3.0) ERROR: `f` has no method matching f(::Float32, ::Float64) julia> f(2.0, "3.0") ERROR: `f` has no method matching f(::Float64, ::ASCIIString) julia> f("2.0", "3.0") ERROR: `f` has no method matching f(::ASCIIString, ::ASCIIString)有时需要写一些通用方法,这时应声明参数为抽象类型: julia> f(x::Number, y::Number) = 2x - y; julia> f(2.0, 3) 1.0要想给一个函数定义多个方法,只需要多次定义这个函数,每次定义的参数个数和类型需不同。函数调用时,最匹配的方法被重载: julia> f(2.0, 3.0) 7.0 julia> f(2, 3.0) 1.0 julia> f(2.0, 3) 1.0 julia> f(2, 3) 1对非数值的值,或参数个数少于 2,f 是未定义的,调用它会返回 “no method” 错误: julia> f("foo", 3) ERROR: `f` has no method matching f(::ASCIIString, ::Int64) julia> f() ERROR: `f` has no method matching f()在交互式会话中输入函数对象本身,可以看到函数所存在的方法: julia> f f (generic function with 2 methods)这个输出告诉我们,f 是一个含有两个方法的函数对象。要找出这些方法的签名,可以通过使用 methods 函数来实现: julia> methods(f) # 2 methods for generic function "f": f(x::Float64,y::Float64) at none:1 f(x::Number,y::Number) at none:1这表明,f 有两个方法,一个以两个 Float64 类型作为参数和另一个则以一个 Number 类型作为参数。它也指示了定义方法的文件和行数:因为这些方法在 REPL 中定义,我们得到明显行数值:none:1。定义类型时如果没使用 ::,则方法参数的类型默认为 Any。对 f 定义一个总括匹配的方法: julia> f(x,y) = println("Whoa there, Nelly."); julia> f("foo", 1) Whoa there, Nelly.总括匹配的方法,是重载时的最后选择。重载是 Julia 最强大最核心的特性。核心运算一般都有好几十种方法: julia> methods(+) # 125 methods for generic function "+": +(x::Bool) at bool.jl:36 +(x::Bool,y::Bool) at bool.jl:39 +(y::FloatingPoint,x::Bool) at bool.jl:49 +(A::BitArray{N},B::BitArray{N}) at bitarray.jl:848 +(A::Union(DenseArray{Bool,N},SubArray{Bool,N,A<:DenseArray{T,N},I<:(Union(Range{Int64},Int64)...,)}),B::Union(DenseArray{Bool,N},SubArray{Bool,N,A<:DenseArray{T,N},I<:(Union(Range{Int64},Int64)...,)})) at array.jl:797 +{S,T}(A::Union(SubArray{S,N,A<:DenseArray{T,N},I<:(Union(Range{Int64},Int64)...,)},DenseArray{S,N}),B::Union(SubArray{T,N,A<:DenseArray{T,N},I<:(Union(Range{Int64},Int64)...,)},DenseArray{T,N})) at array.jl:719 +{T<:Union(Int16,Int32,Int8)}(x::T<:Union(Int16,Int32,Int8),y::T<:Union(Int16,Int32,Int8)) at int.jl:16 +{T<:Union(Uint32,Uint16,Uint8)}(x::T<:Union(Uint32,Uint16,Uint8),y::T<:Union(Uint32,Uint16,Uint8)) at int.jl:20 +(x::Int64,y::Int64) at int.jl:33 +(x::Uint64,y::Uint64) at int.jl:34 +(x::Int128,y::Int128) at int.jl:35 +(x::Uint128,y::Uint128) at int.jl:36 +(x::Float32,y::Float32) at float.jl:119 +(x::Float64,y::Float64) at float.jl:120 +(z::Complex{T<:Real},w::Complex{T<:Real}) at complex.jl:110 +(x::Real,z::Complex{T<:Real}) at complex.jl:120 +(z::Complex{T<:Real},x::Real) at complex.jl:121 +(x::Rational{T<:Integer},y::Rational{T<:Integer}) at rational.jl:113 +(x::Char,y::Char) at char.jl:23 +(x::Char,y::Integer) at char.jl:26 +(x::Integer,y::Char) at char.jl:27 +(a::Float16,b::Float16) at float16.jl:132 +(x::BigInt,y::BigInt) at gmp.jl:194 +(a::BigInt,b::BigInt,c::BigInt) at gmp.jl:217 +(a::BigInt,b::BigInt,c::BigInt,d::BigInt) at gmp.jl:223 +(a::BigInt,b::BigInt,c::BigInt,d::BigInt,e::BigInt) at gmp.jl:230 +(x::BigInt,c::Uint64) at gmp.jl:242 +(c::Uint64,x::BigInt) at gmp.jl:246 +(c::Union(Uint32,Uint16,Uint8,Uint64),x::BigInt) at gmp.jl:247 +(x::BigInt,c::Union(Uint32,Uint16,Uint8,Uint64)) at gmp.jl:248 +(x::BigInt,c::Union(Int64,Int16,Int32,Int8)) at gmp.jl:249 +(c::Union(Int64,Int16,Int32,Int8),x::BigInt) at gmp.jl:250 +(x::BigFloat,c::Uint64) at mpfr.jl:147 +(c::Uint64,x::BigFloat) at mpfr.jl:151 +(c::Union(Uint32,Uint16,Uint8,Uint64),x::BigFloat) at mpfr.jl:152 +(x::BigFloat,c::Union(Uint32,Uint16,Uint8,Uint64)) at mpfr.jl:153 +(x::BigFloat,c::Int64) at mpfr.jl:157 +(c::Int6

[1] [2] [3]  下一页


Julia 方法