numpyfloating-accuracypolynomialspolynomial-math# Addressing polynomial multiplication and division "overflow" issue

### Found a second solution

I have a list of the coefficient to degree 1 polynomials, with `a[i][0]*x^1 + a[i][1]`

```
a = np.array([[ 1. , 77.48514702],
[ 1. , 0. ],
[ 1. , 2.4239275 ],
[ 1. , 1.21848739],
[ 1. , 0. ],
[ 1. , 1.18181818],
[ 1. , 1.375 ],
[ 1. , 2. ],
[ 1. , 2. ],
[ 1. , 2. ]])
```

And running into issues with the following operation,

```
np.polydiv(reduce(np.polymul, a), a[0])[0] != reduce(np.polymul, a[1:])
```

where

```
In [185]: reduce(np.polymul, a[1:])
Out[185]:
array([ 1. , 12.19923307, 63.08691612, 179.21045388,
301.91486027, 301.5756213 , 165.35814595, 38.39582615,
0. , 0. ])
```

and

```
In [186]: np.polydiv(reduce(np.polymul, a), a[0])[0]
Out[186]:
array([ 1.00000000e+00, 1.21992331e+01, 6.30869161e+01, 1.79210454e+02,
3.01914860e+02, 3.01575621e+02, 1.65358169e+02, 3.83940472e+01,
1.37845155e-01, -1.06809521e+01])
```

First of all the remainder of `np.polydiv(reduce(np.polymul, a), a[0])`

is way bigger than 0, `827.61514239`

to be exact, and secondly, the last two terms to quotient should be 0, but way larger from 0. `1.37845155e-01, -1.06809521e+01`

.

I'm wondering what are my options to improve the accuracy?

Solution

There is a slightly complicated way to keep the product first and then divide structure.

By first employ `n`

points and evaluate on `a`

.

```
xs = np.linspace(0, 1., 10)
ys = np.array([np.prod(list(map(lambda r: np.polyval(r, x), a))) for x in xs])
```

then do the division on `ys`

instead of coefficients.

```
ys = ys/np.array([np.polyval(a[0], x) for x in xs])
```

finally recover the coefficient using polynomial interpolation with `xs`

and `ys`

```
from scipy.interpolate import lagrange
lagrange(xs, ys)
```

```
b = a[:,::-1]
np.polydiv(reduce(np.polymul, b), b[0])[0][::-1]
```

- Convert c-order index into f-order index in Python
- Max in a sliding window in NumPy array
- Pandas: change between mean/std and plus/minus notations
- How can I extract the duration and offset from a numpy array representing audio?
- Are Python C extensions faster than Numba JIT?
- sklearn can't find lapack in new conda environment
- JAX jax.grad on simple function that takes an array: `ConcretizationTypeError: Abstract tracer value encountered where concrete value is expected`
- Is it possible to flatten an array with two different major orders without using for loop?
- Pythonic optimization of per-pixel image processing
- Selecting specific rows and columns from NumPy array
- Numpy slicing with bound checks
- Match close element pairs from two numpy arrays
- Pandas Dataframe Min/Max Range
- Intel RealSense - Align depth and color in two numpy arrays
- How to put number of a particular element in a particular row and column constraints in a matrix?
- For loop is slowing down performance. Any alternatives?
- How to make custom Python classes compatible with numpy?
- Comparing a numeric Python's list against a Pandas dataframe
- Pandas replace with value from another row
- Solve matrix and vector multiplication with parameters instead of values (preferably in python)
- Add a string to a NumPy array
- Numpy slicing given row for each column
- Interpolate 2d numpy array to change shape
- Create dataframe with all unique combinations given a set of constraints
- How to calculate a Kernel/Matrix efficiently
- Vectorize a folding process in a dataframe
- NumPy create a 3D array using other 2 3D arrays and a 1D array to discriminate
- Shared memory numpy array code not working
- Pandas replace by condition in the 1st row
- Lengths of overlapping time ranges listed by rows