# \_at\_evalpoly

**URL:** <https://discourse.julialang.org/t/-at-evalpoly/2857>\
**Category:** New to Julia\
**Created:** [March 24, 2017, 10:33am UTC](https://discourse.julialang.org/t/-at-evalpoly/2857 "2017-03-24T10:33:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [March 24, 2017, 10:33am UTC](https://discourse.julialang.org/t/-at-evalpoly/2857/1 "2017-03-24T10:33:49Z")

</div>

I do not seem to understand the use of `@evalpoly` ([doc](http://docs.julialang.org/en/latest/stdlib/math.html#Base.Math.@evalpoly))

```julia
julia> @evalpoly(3.0, 1)
ERROR: BoundsError: attempt to access (1,)
  at index [0]

julia> f(p) = @evalpoly(3.0, p)
ERROR: BoundsError: attempt to access (:p,)
  at index [0]

julia> f(p) = @evalpoly(1, p...)
ERROR: BoundsError: attempt to access (:(p...),)
  at index [0]

```

From what I understand, the first error comes from [https://github.com/JuliaLang/julia/blob/e976e63cc791411b1e7d88b7ca705e87e10d8574/base/math.jl#L110](https://github.com/JuliaLang/julia/blob/e976e63cc791411b1e7d88b7ca705e87e10d8574/base/math.jl#L110).

As for the second and third, is there a way to delay the evaluation of the macro until `p` is passed to the function `f`? Or is this not the way the macro is intended to be used?

Thanks,  
michele

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 24, 2017, 11:10am UTC](https://discourse.julialang.org/t/-at-evalpoly/2857/2 "2017-03-24T11:10:42Z")

</div>

> [@mzaffalon](#):
>
> As for the second and third, is there a way to delay the evaluation of the macro until p is passed to the function f? Or is this not the way the macro is intended to be used?

The whole point of this macro is to inline the evaluation of a polynomial whose degree (and usually its coefficients) is known at compile time. This allows things like polynomial approximations of special-function implementations to be extremely efficient. Hence, the arguments must be an explicit list of coefficients.

(The case of a single coefficient, i.e. a degree-0 polynomial, is so pointless that we never implemented it. i.e. you might as well just write `1` instead of `@evalpoly 3.0 1`. We might as well implement it for completeness, I suppose.)

So, for example, if you knew that `p` was an array of length 4, you could do:

```julia
julia> f(p) = @evalpoly(1, p[1], p[2], p[3], p[4])
f (generic function with 1 method)

julia> f([2,3,4,5])
14

```

In practice, `@evalpoly` is mainly used in cases where the coefficients are compile-time constants as well, mainly in the implementation of special functions like `erfinv` and `polygamma`.

If you have an array `p` whose length is not known, there is no point in using a macro to evaluate the polynomial. It will need an explicit loop that is evaluated at runtime, rather than at compile-time as in `@evalpoly`.

At one point, I suggested that we have an `evalpoly` function for runtime evaluation of polynomials ([RFC: add evalpoly function, mirroring @evalpoly macro by stevengj · Pull Request #7186 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/7186)), but there wasn’t much interest. In most such cases you probably want to use the [Polynomials.jl package](https://github.com/Keno/Polynomials.jl).

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [March 24, 2017, 11:21am UTC](https://discourse.julialang.org/t/-at-evalpoly/2857/3 "2017-03-24T11:21:36Z")

</div>

Thank you for the reply and the example.

I remember that at a certain point, you (most likely) implemented a `horner` method (or a macro) and I could not find it anymore. The `@evalpoly` is what came up when I searched the latest documentation for horner.

I completely agree with you that the evaluation of a 0th order polynomial is useless for the specialized cases of special functions.

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 24, 2017, 1:19pm UTC](https://discourse.julialang.org/t/-at-evalpoly/2857/4 "2017-03-24T13:19:05Z")

</div>

I believe [Stefan Karpinski](https://m.youtube.com/watch?v=dK3zRXhrFZY) addressed this specific macro at about 50m in the _Julia - To Lisp or not to Lisp_ talk on YouTube.  
Hope it helps.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [March 25, 2017, 4:44am UTC](https://discourse.julialang.org/t/-at-evalpoly/2857/5 "2017-03-25T04:44:36Z")

</div>

Interesting presentation, `@evalpoly` macro aside. Thank you.

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 25, 2017, 5:30am UTC](https://discourse.julialang.org/t/-at-evalpoly/2857/6 "2017-03-25T05:30:10Z")

</div>

My pleasure - but we should probably be thanking Stefan!  
👍

-mrg
