Dear all.
Recently I stumbled upon the Newton polynomial and took it as a practice to
implement it without using loops, but failed. I first didn't get a grab on
how to create the matrix used in
https://en.wikipedia.org/wiki/Newton_polynomial#Main_idea, and eventually
came up with code following
https://en.wikipedia.org/wiki/Newton_polynomial#Application:
NB. Newton polynomial
np=: 4 : 0
a=. {. y
for_i. }.>:i.#x do.
y=. (2 (-~)/\ y) % i ({:-{.)\ x NB. Divided differences
a=. a, {. y NB. Coefficients are the topmost entries
end.
NB. Convert the summands aᵢ(x-x₀)…(x-xᵢ₋₁) of the polynomial
NB. from multiplier-and-roots to coefficients form and add them up
+/@,:/ p."1 (;/a) ,. (<''), }:<\ x
)
x=: _3r2 _3r4 0 3r4 3r2
y=: 3&o. x
load'plot'
load'stats'
plot (];(x np y)&p.) steps _1.5 1.5 30
I also tried replacing the loop with fold F:. but again was not able to do
so. Anyone out there who can enlighten me?
Many thanks. Stefan.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm