Hello everyone!
I am trying to write two functions that accept 3D and 2D arrays that can be either real or complex. The first function receives arguments of one dimension higher than the second function. Then, it iterates over that extra dimension calling the second function for each “slice” of the arguments along that dimension. The purpose of the code is converting S-matrices for many frequencies at given reference impedances for all ports, also at that many frequencies.
The problem is that all arguments can be either Real or Complex arrays and even different between them, although the most common case is that the first argument is complex and the rest are real. How can I write the header of the function to admit Real and Complex arrays without explicitly typing
CR = Union{Real,Complex}
function S2S(S::Array{T1,3},Za::Array{T2,2},Zb::Array{T3,2}) where {T1 <:CR && T2 <: CR && T3 <: CR}
...
end
I leave my current code here. Thanks!
function S2S(Sₐ::Array{ComplexF64,3},Zₐ::Array{Float64,2},Zᵦ::Array{Float64,2})
Sᵦ = similar(Sₐ)
for f in 1:size(Sₐ,3)
Sᵦ[:,:,f] = S2S(Sₐ[:,:,f],Zₐ[:,f],Zᵦ[:,f])
end
return Sᵦ
end
function S2S(Sₐ::Array{ComplexF64,2},Zₐ::Array{Float64,1},Zᵦ::Array{Float64,1})
U = UniformScaling(size(Sₐ,1))
G = diagm(sqrt.(Zₐ./Zᵦ))
Γ = diagm((Zᵦ-Zₐ)./(Zᵦ+Zₐ))
return Sᵦ = G*inv(U-Sₐ)*(Sₐ-Γ)*inv(U-Sₐ*Γ)*(U-Sₐ)*inv(G)
end