first steps to metaprogramming


#1

Hello! I am trying to learn metaprogramming in Julia, and the first problem I’ve had is the following. When I try to define an arbitrary Wilkinson-type polynomial (for instance, p_5(x) = (x-1) * (x-2) * (x-3) * (x-4) * (x-5) is the polynomial of degree 5), I’ve written this function:

function wp(n::Int)
    ex = :(x-$n)
    for i=n-1:-1:1
        ex = :($ex*(x-$i))
    end
    return ex
end

and it gives an expression which seems correct. However, I don’t know how to evaluate it for some x, say x=1.3. Could you help me? Thank you.


#2

I think you need to use @eval(f).


#3

The first thing you should ask yourself when writing metaprogramming is “why do I need metaprogramming for this”. In this example, the function:

function wp_loop(x, n::Int)
    v  = 1.0
    for i in 1:n 
        v *= (x-i)
    end
    return v
end

or perhaps

function wp_prod(x, n::Int)
    prod(i -> x-i, 1:n)
end

evaluates the polynomial without metaprogramming. If you really need to specialize on n you can do

function wp_val(x, ::Val{n}) where {n}
    v  = 1.0
    for i in 1:n 
        v *= (x-i)
    end
    return v
end

and get the specialized:

julia> @code_llvm wp_vaö(4.0, Val(3))
  %1 = fadd double %0, -1.000000e+00
  %2 = fadd double %0, -2.000000e+00
  %3 = fmul double %1, %2
  %4 = fadd double %0, -3.000000e+00
  %5 = fmul double %3, %4
  ret double %5
}

If this is just a learning exercise and you want to create a function based on an expression, you could do:

julia> @eval function fp_4(x)
           $(wp(4))
       end
fp_4 (generic function with 1 method)

julia> fp_4(7)
360

#4

Thank you. This is a learning exercise and the last part of your answer solves my problem.
I actually need metaprogramming for “creating my own syntax”, I guess. I need to make some macros so that end-users can use them instead of writing more complicated code.