1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use Complex;
/// Horner evalution (float)
///
/// $$ P(x) = a_0 x^n + a_1 x^{n-1} + \cdots + a_n $$
///
/// Evaluated via Horner's method: $$ b_0 = a_0,\quad b_k = b_{k-1} \cdot x + a_k,\quad P(x) = b_n $$
///
/// The `horner_eval_f` function in Rust implements the Horner's method for evaluating a polynomial with
/// given coefficients at a specific value.
///
/// Arguments:
///
/// * `coeffs`: A vector of floating-point coefficients representing a polynomial. The coefficients are
/// ordered from highest degree to lowest degree.
/// * `zval`: The `zval` parameter in the `horner_eval_f` function represents the value at which the
/// polynomial is evaluated. It is of type `f64`, which means it is a floating-point number.
///
/// Returns:
///
/// The function `horner_eval_f` returns a `f64` value, which is the result of evaluating the polynomial
/// with the given coefficients at the specified value `zval`.
///
/// # Examples
///
/// ```
/// use ginger::horner::horner_eval_f;
///
/// let coeffs = vec![1.0, 2.0, 3.0]; // represents 3 + 2x + 1x^2 (coeffs in reverse order)
/// let result = horner_eval_f(&coeffs, 2.0); // evaluates at x = 2
/// assert_eq!(result, 11.0); // 3 + 2*2 + 1*4 = 3 + 4 + 4 = 11
/// ```
/// Horner evalution (complex)
///
/// $$ P(z) = a_0 z^n + a_1 z^{n-1} + \cdots + a_n $$
///
/// Evaluated via Horner's method: $$ b_0 = a_0,\quad b_k = b_{k-1} \cdot z + a_k $$
///
/// The `horner_eval_c` function in Rust implements the Horner evaluation method for complex
/// polynomials.
///
/// Arguments:
///
/// * `coeffs`: A vector of coefficients representing a polynomial. The coefficients are in descending
/// order of degree.
/// * `zval`: The `zval` parameter is a complex number that represents the value at which the polynomial is evaluated.
///
/// Returns:
///
/// The function `horner_eval_c` returns a complex number of type `Complex<f64>`.
///
/// # Examples
///
/// ```
/// use ginger::horner::horner_eval_c;
/// use num_complex::Complex;
///
/// let coeffs = vec![1.0, 2.0, 3.0]; // represents 3 + 2x + 1x^2 (coeffs in reverse order)
/// let zval = Complex::new(1.0, 1.0);
/// let result = horner_eval_c(&coeffs, &zval);
/// // evaluates at x = 1 + i: 3 + 2(1+i) + 1(1+i)^2 = 3 + 2+2i + 1(2i) = 5 + 4i
/// assert_eq!(result.re, 5.0);
/// assert_eq!(result.im, 4.0);
/// ```