1bbbf1280Sopenharmony_ci// polynomial for approximating sin(x)
2bbbf1280Sopenharmony_ci//
3bbbf1280Sopenharmony_ci// Copyright (c) 2019, Arm Limited.
4bbbf1280Sopenharmony_ci// SPDX-License-Identifier: MIT
5bbbf1280Sopenharmony_ci
6bbbf1280Sopenharmony_cideg = 15;  // polynomial degree
7bbbf1280Sopenharmony_cia = -pi/2; // interval
8bbbf1280Sopenharmony_cib = pi/2;
9bbbf1280Sopenharmony_ci
10bbbf1280Sopenharmony_ci// find even polynomial with minimal abs error compared to sin(x)/x
11bbbf1280Sopenharmony_ci
12bbbf1280Sopenharmony_ci// account for /x
13bbbf1280Sopenharmony_cideg = deg-1;
14bbbf1280Sopenharmony_ci
15bbbf1280Sopenharmony_ci// f = sin(x)/x;
16bbbf1280Sopenharmony_cif = 1;
17bbbf1280Sopenharmony_cic = 1;
18bbbf1280Sopenharmony_cifor i from 1 to 60 do { c = 2*i*(2*i + 1)*c; f = f + (-1)^i*x^(2*i)/c; };
19bbbf1280Sopenharmony_ci
20bbbf1280Sopenharmony_ci// return p that minimizes |f(x) - poly(x) - x^d*p(x)|
21bbbf1280Sopenharmony_ciapprox = proc(poly,d) {
22bbbf1280Sopenharmony_ci  return remez(f(x)-poly(x), deg-d, [a;b], x^d, 1e-10);
23bbbf1280Sopenharmony_ci};
24bbbf1280Sopenharmony_ci
25bbbf1280Sopenharmony_ci// first coeff is fixed, iteratively find optimal double prec coeffs
26bbbf1280Sopenharmony_cipoly = 1;
27bbbf1280Sopenharmony_cifor i from 1 to deg/2 do {
28bbbf1280Sopenharmony_ci  p = roundcoefficients(approx(poly,2*i), [|D ...|]);
29bbbf1280Sopenharmony_ci  poly = poly + x^(2*i)*coeff(p,0);
30bbbf1280Sopenharmony_ci};
31bbbf1280Sopenharmony_ci
32bbbf1280Sopenharmony_cidisplay = hexadecimal;
33bbbf1280Sopenharmony_ciprint("abs error:", accurateinfnorm(sin(x)-x*poly(x), [a;b], 30));
34bbbf1280Sopenharmony_ciprint("in [",a,b,"]");
35bbbf1280Sopenharmony_ciprint("coeffs:");
36bbbf1280Sopenharmony_cifor i from 0 to deg do coeff(poly,i);
37