<jupyter_start><jupyter_text>Polynomial rootsFor this example we will use a publicly available package for working with polynomials. It should be available using the following line, if you have followed installation instructions for these scripts.<jupyter_code>using Polynomials<jupyter_output><empty_output><jupyter_text>Our first step is to construct a polynomial with six known roots.<jupyter_code>r = [-2.0,-1,1,1,3,6]
@show p = fromroots(r);<jupyter_output>p = fromroots(r) = Polynomial(36.0 - 36.0*x - 43.0*x^2 + 44.0*x^3 + 6.0*x^4 - 8.0*x^5 + 1.0*x^6)<jupyter_text>Now we use a standard numerical method for finding those roots, pretending that we don't know them already.<jupyter_code>@show r_computed = sort(roots(p));<jupyter_output>r_computed = sort(roots(p)) = [-2.0000000000000013, -0.999999999999999, 0.9999999902778504, 1.0000000097221495, 2.9999999999999996, 5.999999999999992]<jupyter_text>Here are the relative errors in each of the computed roots. The `@.` notation at the start means essentially to do the given operations on each element of the given vectors.<jupyter_code>@. abs(r - r_computed) / r<jupyter_output><empty_output><jupyter_text>It seems that the forward error is acceptably close to machine epsilon for double precision in all cases except the double root at $x=1$. This is not a surprise, though, given the poor conditioning at such roots.Let's consider the backward error. The data in the rootfinding problem are the polynomial coefficients. We can apply `fromroots` to find the coefficients of the polynomial (that is, the data) whose roots were actually computed by the numerical algorithm.<jupyter_code>@show p_computed = fromroots(r_computed);<jupyter_output>p_computed = fromroots(r_computed) = Polynomial(35.99999999999993 - 35.999999999999915*x - 42.99999999999997*x^2 + 43.99999999999998*x^3 + 5.999999999999973*x^4 - 7.999999999999991*x^5 + 1.0*x^6)<jupyter_text>We find that in a relative sense, these coefficients are very close to those of the original, exact polynomial:<jupyter_code>cp = coeffs(p)
cpc = coeffs(p_computed)
@. abs(cp-cpc)/cp<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09566446748350613, 'fraction_numerical': 0.11074458058435438, 'mean_word_length': 6.148148148148148, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '264161', 'license': 'MIT', 'n_tokens_mistral': 855, 'n_tokens_neox': 680, 'n_words': 220, 'path': 'book/intro/demos/stability-roots.ipynb', 'repo_name': 'krislock/fnc', 'size': '2122'}