Horner rule for two variables of a polynomial

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 , . *

  1. . :

    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, , , - . , , ,

+4
2

. . C , ,

gif.latex?%5Csum_i&space;%5Csum_j&space;C_%7Bi,j%7D&space;%5Ccdot&space;y%5Ei&space;x%5Ej

, gif.latex?y%5Ei s ,

gif.latex?%5Csum_i&space;y%5Ei&space;%5Cleft(&space;%5Csum_j&space;C_%7Bi,j%7D&space;%5Ccdot&space;x%5Ej&space;%5Cright)

. x , gif.latex?C_i. SML horner

fun sumj Ci = horner Ci x

gif.latex?D_i&space;=&space;%5Csum_j&space;C_%7Bi,j%7D&space;%5Ccdot&space;x%5Ej

SML val D = map sumj C. D:

gif.latex?% 5Csum_i & space;% 5Csum_j & space; C_% 7Bi, j% 7D & space;% 5Ccdot & space; x% 5Ej & space; y% 5Ei & space; = & space;% 5Csum_i & space; y% 5Ei & space;% 5Cleft (% 5Ci_% % 7D & space;% 5Ccdot & space; x% 5Ej% 5Cright & space;) & space; = & space;% 5Csum_i & space; D_i & space;% 5Ccdot & space; y% 5Ei

, horner, gif.latex? D_i. SML

horner D y

... !


:

fun horner2 C x y =
  let
    fun sumj Ci = horner Ci x
    val D = map sumj C
  in
    horner D y
  end

? map.

+3

, , . horner, horner horner applied to inner list x , :

fun evalXY coeffLists x y = horner (map (fn coeffList => horner coeffList x) coeffLists) y

horner , .

, horner, evalXY:

fun horner x coeffList = foldr (fn (a, b) => a + b * x) (0.0) coeffList
fun evalXY x y coeffLists = horner y (map (horner x) coeffLists)

, currying , , horner x coeffList, fn coeffList => horner coeffList x. , curries , , .

, SML . horner , horner list 2. horner list 2.0. , 0, 0.0 .

+2

Source: https://habr.com/ru/post/1668777/


All Articles