JuliaTeachingCTU / Scientific-Programming-in-Julia

Repository for B0M36SPJ
https://juliateachingctu.github.io/Scientific-Programming-in-Julia/dev/
MIT License
76 stars 12 forks source link

Lab 01: we should be using `mapreduce` #122

Closed nmheim closed 11 months ago

nmheim commented 11 months ago

we are defining a polynomial function in the first lab like this:

function polynomial(as, x)
    powers = map(enumerate(a)) do (i, a)
        x^(i-1) * a
    end
    accumulator = sum(powers)
    return accumulator
end

this should really be a mapreduce. the example above is allocating much more than it has to (which becomes only really important when x is a matrix, but still)

mapreduce(+, enumerate(as), init=zero(x)) do (i,a)
   x^(i-1) * a
end