'polynomial transformation in python

I'm trying to shift a polynomial. I'm currently using numpy.poly1d() to make a quadratic equation.

example: 2x^2 + 3x +4

but I need to shift the function by tau, such that

2(x-tau)^2 + 3(x-tau) + 4

Tau is a value that will change base on some other variables in my code.



Solution 1:[1]

Create a burner variable, store x-tau into it, and feed that into your function

Solution 2:[2]

Sometimes horizontal translation (transforming a polynomial by a certain amount horizontaly) i.e. f(x) => f(x ± ?) in it's expanded form is of great importance. Basically finding the coefficients of the new polynomial f(y) where y = x ± ?, is an extention of the Polynomial Synthetic Division which is in fact an extention of Polynomial Application.

Now, let our polynomial be f(x) = x^{3}-7x+7. We express it as an array in the form of [1,0,-7,7].

Polynomial Application:

Since it is very simple and intuitive, when a certain x value has to be applied to a polynomial, even seasoned coders would just do it by the paper and pen method by using powers, multiplication and addition. However the simplest algorithm for a computer would be different;

The Ruffini Horner method.

In JS it's implementation is as simple as

poly.reduce((p,c) => p*x+c);

where poly is say [1,0,-7,7] for the polynomial given above and x is the applied value. As one would expect, it yields 13 once x = 3 is applied.

Polynomial Synthetic Division:

Now this is in fact exactly the same thing as Polynomial Application. The only difference is that, we keep the intermediate values as the coefficients of the resulting polynomial and the final value as the remainder. Notice that; the remainder of the polynomial synthetic division is same with the result of the polynomial application.

In order to implement polynomial synthetic division the following code should be sufficient;

function div(y,x){
  var t = y[0],
      r = y.map((n,i,a) => i ? t = t*x+n : n);
  return { pol: r.slice(0,-1)
         , rem: r[r.length-1]
         };
}

Polynomial Transformation:

This is something a little deeper math. It's been well studied in the book Elements of Computer Algebra with Application - Alkiviadis G. Akridas - 1989 @ Section 3.1.2 The Ruffini Horner Method.

As a summary; In order to perform a transformation of a polynomial f(x) by ? amount, we initially perform a Synthetic division; f(x)/(x-?). Let the resulting polynomial be f'(x) and the remainder be c_0. Then perform the very same operation like f'(x)/(x-?) and get f''(x) and c_1 (the remainder). Repeat this n times where n is the degree of the original polynomial f(x) and you should get n+1 remainders like c_n,c_n-1,...,c_0. These are the coefficients of your transposed polynomial f(x+?) including the constant c_0 at the very end.

A JS code for this could be;

function transpose(y,x){
  var r = [],
      t;
  while (y.length) {
    t = div(y,x);
    y = t.pol;
    r.unshift(t.rem);
  }
  return r;
}

As a result, if we shift the above given blue polynomial f(x) = x^{3}-7x+7 ([1,0,-7,7]) by ? = -3 units (to the right) the resulting coefficients turn out to be [ 1, -9, 20, 1 ] which is the red polynomial f(x) = x^{3}-9x{2}+20x+1 that can be seen in the below graph.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YOLO Baby
Solution 2