From 15322f7f51b7787bde99c86b141c77879d9f239e Mon Sep 17 00:00:00 2001 From: Yiwu Chen <210at85@gmail.com> Date: Mon, 17 Jun 2024 13:59:18 +0000 Subject: [PATCH] [stdlib] Improve `_horner_evaluate` docstring - Small simplification to the function `_horner_evaluate`. - Improve its docstring to match the argument names. - Finish off removing `unroll` by cleaning up the imports. Signed-off-by: Yiwu Chen <210at85@gmail.com> --- stdlib/src/math/polynomial.mojo | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/stdlib/src/math/polynomial.mojo b/stdlib/src/math/polynomial.mojo index 2f4473c9f..1fc938e9f 100644 --- a/stdlib/src/math/polynomial.mojo +++ b/stdlib/src/math/polynomial.mojo @@ -21,7 +21,6 @@ from math.polynomial import polynomial_evaluate from collections import List -from utils.loop import unroll # ===----------------------------------------------------------------------===# # polynomial_evaluate @@ -64,11 +63,12 @@ fn _horner_evaluate[ ](x: SIMD[dtype, simd_width]) -> SIMD[dtype, simd_width]: """Evaluates the polynomial using the passed in value and the specified coefficients using the Horner scheme. The Horner scheme evaluates the - polynomial as `horner(val, coeffs)` where val is a scalar and coeffs is a - list of coefficients [c0, c1, c2, ..., cn] by: + polynomial at point x as `horner(x, coeffs)` where x is a scalar and coeffs + is a list of coefficients [c0, c1, c2, ..., cn] by: ``` - horner(val, coeffs) = c0 + val * (c1 + val * (c2 + val * (... + val * cn))) - = fma(val, horner(val, coeffs[1:]), c0) + horner(x, coeffs) + = c0 + x * (c1 + x * (c2 + x * (... + x * cn))) + = fma(x, horner(x, coeffs[1:]), coeffs[0]) ``` Parameters: @@ -80,17 +80,12 @@ fn _horner_evaluate[ x: The value to compute the polynomial with. Returns: - The polynomial evaluation results using the specified value and the - constant coefficients. + The polynomial specified by the coefficients evaluated at value x. """ - alias num_coefficients = len(coefficients) - alias c_last = coefficients[num_coefficients - 1] - alias c_second_from_last = coefficients[num_coefficients - 2] - - var result = x.fma(c_last, c_second_from_last) + var result = x.fma(coefficients[-1], coefficients[-2]) @parameter - for i in reversed(range(num_coefficients - 2)): + for i in reversed(range(len(coefficients) - 2)): alias c = coefficients[i] result = result.fma(x, c)