Evaluate a function for many values fast

I want to evaluate my function fast for many values.
Is there a easy way to do this in Julia? :slight_smile:

y(x) = 4*x^2 + 6*x + 34
x1 = [44,75,45]
y(x1) # my favorite sintax does not work, i was really hopping this works
for i in x1 println(y(i)) end # this works but is there a better way?

What about:

julia> y.(x1)
3-element Vector{Int64}:
  8042
 22984
  8404

?
The dot syntax can be used to apply any scalar function on a vector.

5 Likes

If you want something even faster, do

y(x) = evalpoly(x, (34, 6, 4))
y.(x1)

Btw, the dot syntax is better than y(x1), since is doesn’t make sense to take powers of a vector, and the dot syntax makes it explicit that you are applying y elememtwise.

2 Likes