:@evalpoly

@evalpoly(z, c...)

Evaluate the polynomial $\sum_k c[k] z^{k-1}$ for the coefficients c[1], c[2], ...; that is, the coefficients are given in ascending order by power of z. This macro expands to efficient inline code that uses either Horner's method or, for complex z, a more efficient Goertzel-like algorithm.

Examples

  1. Evaluate a polynomial using Horner's method:

    julia> @evalpoly(2, 1, 2, 3)
    17

    In this example, the macro @evalpoly evaluates the polynomial 1 + 2z + 3z^2 at z = 2 using Horner's method. The result is 17.

  2. Evaluate a polynomial with complex coefficients:

    julia> @evalpoly(1 + 2im, 1 + 2im, 3 + 4im)
    -6 + 18im

    The @evalpoly macro can handle complex coefficients as well. Here, it evaluates the polynomial (1 + 2im) + (1 + 2im)z + (3 + 4im)z^2 at z = 1 + 2im, resulting in -6 + 18im.

  3. Evaluate a polynomial with different coefficient types:

    julia> @evalpoly(0.5, 1//2, BigFloat(0.2))
    1.1

    The @evalpoly macro can handle different coefficient types. In this example, it evaluates the polynomial 1/2 + (1/2)z + 0.2z^2 at z = 0.5, resulting in 1.1.

  4. Evaluate a polynomial with variable number of coefficients:
    julia> coefficients = [1, 2, 3, 4];
    julia> @evalpoly(5, coefficients...)
    586

    The @evalpoly macro can accept a variable number of coefficients. Here, it evaluates the polynomial 1 + 2z + 3z^2 + 4z^3 at z = 5, resulting in 586.

Note that the macro @evalpoly generates efficient inline code based on the input coefficients and the type of z, whether it is real or complex.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: