Marouane1994
Marouane1994

Reputation: 534

ERROR: MethodError: no method matching abs(::Array{Complex{Float64},1})

let's suppose that we have this function:

function prob(na)
            @assert count(!iszero, ui2*na) == 0
            b = T0*na
            setc(-b)
            total = 0.0
            for x in Channel(scan)
                nab = vi2*x + b 
                total += prod([c.^complex(n)/factorial(n) for (c, n) in zip(coef, nab)])
            end
            return abs(total*omega)^2
        end

and let's try to ignore some variables in this function since the problem is not related to them, the problem that i'm facing here is when i call this function which is by the way an intern function, i get this following error:

ERROR: MethodError: no method matching abs(::Array{Complex{Float64},1})
Closest candidates are:
  abs(::Bool) at bool.jl:83
  abs(::Float16) at float.jl:520
  abs(::Float32) at float.jl:521
  ...

now i know that the error is in the last part abs(total*omega)^2 , coefand nabare coefficients type Int omegaalso is Int, if i try the same operation on a simple case it would work but here it doesn't i don't know why!!

Upvotes: 0

Views: 3025

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42244

total seems to be a Vector of complex numbers and omega is probably some scalar. Hence, probably it will be enough if you vectorize your operations using the do (.) operator. Have a look at the code below:

julia> cnums = Complex.([2,2,3],[2,7,6])
3-element Array{Complex{Int64},1}:
 2 + 2im
 2 + 7im
 3 + 6im

julia> abs.(cnums .* 1).^2
3-element Array{Float64,1}:
  8.000000000000002
 53.0
 45.00000000000001

Or, better suggested by @DNF:

julia> abs2.(cnums .* 1)
3-element Array{Int64,1}:
  8
 53
 45

Upvotes: 3

Related Questions