【发布时间】:2021-05-09 18:19:11
【问题描述】:
我正在尝试编写一个函数,该函数采用矩阵向量并递归计算它们的乘积。函数本身是这样的:
using LinearAlgebra: I
"""
Recursively calculates the product of the matrices in a given vector `V`.
"""
function ∏_(V)
if length(V) == 1
return V[1]
elseif length(V) == 0
UniformScaling(1)
else
V[1] * ∏_(V[2:end])
end
end
这一切都很好,但我想将输入的类型限制为类似
V::Vector{Matrix{Number}} ,
其中Number 可以是任何字段元素(整数、有理数或复数)。我如何使用 Julia 的类型注释来实现这一点?我知道子类型运算符<:,但是写
V::Vector{Matrix{<:Number}}
只会产生错误
ERROR: LoadError: MethodError: no method matching ∏_(::Array{Array{Rational{Int64},2},1})
Closest candidates are:
∏_(::Array{Array{var"#s1",2} where var"#s1"<:Number,1})
当给函数一个有理整数矩阵数组时。我该如何解决这个问题?
【问题讨论】:
标签: types type-conversion julia