Horner's rule for two-variable polynomial

前端 未结 2 1616
挽巷
挽巷 2021-01-19 00:16

Horner\'s rule is used to simplify the process of evaluating a polynomial at specific variable values. https://rosettacode.org/wiki/Horner%27s_rule_for_polynomial_evaluation

2条回答
  •  醉梦人生
    2021-01-19 01:04

    You're very close. Let's begin by formalizing the problem. Given coefficients C as a nested list like you indicated, you want to evaluate

    Notice that you can pull out the s from the inner sum, to get

    Look closely at the inner sum. This is just a polynomial on variable x with coefficients given by . In SML, we can write the inner sum in terms of your horner function as

    fun sumj Ci = horner Ci x
    

    Let's go a step further and define

    In SML, this is val D = map sumj C. We can now write the original polynomial in terms of D:

    It should be clear that this is just another instance of horner, since we have a polynomial with coefficients . In SML, the value of this polynomial is

    horner D y
    

    ...and we're done!


    Here's the final code:

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

    Isn't that nice? All we need is multiple applications of Horner's method, and map.

提交回复
热议问题