The Horner rule is used to simplify the process of computing a polynomial based on specific values of variables. https://rosettacode.org/wiki/Horner%27s_rule_for_polynomial_evaluation#Standard_ML
I easily applied the method using SML to one variable polynomial represented as an int list:
fun horner coeffList x = foldr (fn (a, b) => a + b * x) (0.0) coeffList
It works great. Then we can call it using:
- val test = horner [1.0, 2.0, 3.0] 2.0;
> val test = 17.0 : real
Where [1.0, 2.0, 3.0]is the list representing the coefficients of the polynomial, 2.0is the value of the variable x, and 17.0is the result of computing the polynomial.
My problem is this: We have two polynomial variables represented (list of list of objects). The nth element in the high-level list will represent all polynomial members containing y ^ n, and the mth element in the low-level list will represent all polynomial members containing x ^ m.
For example: [[2],[3,0,0,3],[1,2]]- polynomial
(2 (x ^ 0) (y ^ 0)) +
(3 (x ^ 0) (y ^ 1) + 0 (x ^ 1) (y ^ 1) + 0 (x ^ 2) (y ^ 1) + 3 (x ^ 3) (y ^ 1)) +
(1 (x ^ 0) (y ^ 2) + 2 (x ^ 1) (y ^ 2))
The function should return the polynomial value for the given x and y.
I tried various methods using the mlton compiler.
First I tried the foldr nested function:
fun evalXY (z::zs) x y =
foldr
(fn (s, li:list) =>
s + ((foldr (fn(a, b) => a + b*x) 0 li)*y)
)
0
z:zs
, "s" , , "a" . , foldr, "" , foldr , foldr. , , . * , , , , foldr, . > , li , . *
. :
fun evalXY (z::zs) x y =
map
(foldr (fn(a, b) => a + b*x) 0 ???)
z:zs
* , ints. int, int foldr. , y .
: fn evalXY: (int list list) * int * int) → ... → int list *
SML, , , - . , , ,