On Tue, Apr 19, 2016 at 11:31 PM, Po Choi <[email protected]> wrote:
> Given coefficients, say, coeff = [1,2,3]
> I want to get a function for the polynomial: f(x) = coeff[1] * x + coeff[1]
> * x^2 + coeff[1] * x^3
>
> First way
> create_function(coeff) = x -> sum(coeff .* [x^k for k in 1:length(coeff)])
>
> Second way
> immutable Myfunction
>     coeff::Vector

^^ Vector is an abstract type. You probably want to parametrize this.

> end
> call(f::Myfunction, x) = sum(f.coeff .* [x^k for k in 1:length(f.coeff)])
>
> Then
> coeff = [1,2,3]
> foo = create_function(coeff)
> bar = Myfunction(coeff)

If you fix the type instability/uncertainty mentioned above. The
second one is better on 0.4. They are the same on 0.5.

>
> foo(1.2)
> bar(1.2)
>
> Question: Which way is better in Julia?

Reply via email to