How to filter data with masks and update with new values

Hello there.

I’m trying to rewrite a function that I wrote in Python but I have no idea how to do it.

My Python function is:

def R( A1, A2 ):
  phi = np.linspace(0,np.pi,1000)
  Rec = ( 4*A1*A2 / (A1 + A2)**2 ) * np.cos(phi)**2
  Rec[np.logical_and(phi>np.pi/2,phi<3*np.pi/2)] = 0.0
  return phi, Rec

in Julia i got to:

function R(A1, A2)
    phi = LinRange( 0, pi, 1000)
    Rec = (4*A1*A2 / (A1+A2)^2) * cos.(phi).^2
    # Filter Rec and update values where phi > (pi/2) and phi < 3*(pi/2) with 0 
    return phi, Rec
end 
@. Rec[pi/2 < phi < 3*pi/2] = 0.0

what does @ do?

@. makes everything broadcast, without @. the line looks like:

Rec[pi/2 .< phi .< 3*pi/2] .= 0.0

c.f
https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting

1 Like