Multivariable piecewise functions: Julia-basics help

I wouldn’t do it this way. Just define a function to work on scalar inputs, and then broadcast it. (And it’s good style to avoid non-constant globals, i.e. pass a as a parameter.)

e.g.

del(x,y,a=1/2) = ((a^2*x^2-2(a*y-1)^3)*x^2
r(x,y,a=1/2) = del(x,y,a) ≥ 0 ? -(a*y+1)/(3a) : (a*y+1)/(3a)

x = y = range(-10,10, length=100)
z = del.(x, y') # 100×100 grid of values for plotting

Get out of the habit (from Matlab or Python or R) of trying to define “vectorized” functions for things that are logically scalar. Scalar functions are fast in Julia, and are easier to work with.

1 Like