LABTITLE=: 'Polynomials'
LABFOCUS=: 0

NB. =========================================================
Lab Section INTRODUCTION
An expression of the form:
 
    2     5     4
  3x  + 4x  + 2x

is called a polynomial, with argument x, and coefficients 3 4 2, and exponents 2 5 4. Using vectors (lists) it may be evaluated (for an argument value 3) as follows:
)
x=: 3
c=: 3 4 2
e=: 2 5 4

x^e

c * x^e

+/c * x^e

NB. =========================================================
Lab Section
Since addition is both associative and commutative, the
terms may be permuted to any order, without changing the result, for example:

    5    4    2 
  4x + 2x + 3x

Thus:
)
perm=: 1 2 0

] ep=: perm { e

] cp=: perm { c

cp * x^ep

+/cp * x^ep

NB. =========================================================
Lab Section
Since:

   0 
  x

is 1, and since any "missing" term, such as the cube in the example, can be included with a zero coefficient, any polynomial can be written in a standard descending form, with an unbroken sequence of exponents from the largest down to zero:
)
cd=: 4 2 0 3 0 0
ed=: 5 4 3 2 1 0

cd * x^ed

+/cd * x^ed

NB. A polynomial may also be written in a standard ascending order:

] ca=: |. cd

] ea=: |. ed

+/ca*x^ea

NB. =========================================================
Lab Section
The descending order is commonly used in elementary mathematics, but the ascending order is more common in advanced work because it allows the use of power series, polynomials with an indefinite (or even infinite) number of terms. We will mainly use the ascending form, but the descending form will be used in discussing number systems.

In a polynomial in standard form, the exponents are completely determined by the coefficients. For example:
)
c=: 0 0 3 0 2 4

# c

]e=: i.#c

+/c*x^i.#c

NB. These steps may be collected in a single function as follows:

p=: (+/ @ ([ * ] ^ i.@#@[)) " 1 0

c p x

c p 1

NB. Because of its importance, the polynomial is provided as
NB. a primitive function in J, with the two-character name p.:

c p. x

y=: 2 3 5 7 11

c p. y

NB. =========================================================
Lab Section
Although a polynomial is said to be a sum of terms, it is commonly written with the sign for subtraction as well as
addition, as in:

    2     5     4 
  3x  - 4x  + 2x

This can be handled by using only addition, but with negative elements (such as _4) in the list of coefficients. Thus:
)

c=: 3 _4 2

e=: 2 5 4

+/c*x^e

0 0 3 0 2 _4 p. x

NB. =========================================================
Lab Section
The term polynomial refers to a function of a single argument, the x in the expression

     5     4     2
   4x  + 2x  + 3x

The primitive p. is a function of two arguments, and is therefore not properly a polynomial. However, an expression of the form c&p. bonds it with a left argument of coefficients to produce a polynomial function. For example:
)
c2=: 1 2 1
c3=: 1 3 3 1

f=: c2&p.
g=: c3&p.

x=: 2 3 5 7 11

f x
g x

h=: f*g
h x

(f+g) x

NB. =========================================================
Lab Section POLYNOMIAL FUNCTIONS
The sum and the product of polynomials used above are themselves polynomials, as is the composition f@g. Thus:
)
f g x

f@g x

k=: f@g
k x

4 12 21 22 15 6 1&p. x

NB. As illustrated above, the coefficients of the polynomial
NB. k are 4 12 21 22 15 6 1. We will examine means of finding
NB. the coefficients of other polynomials (such as the sum f+g
NB. and product f*g), after analyzing the effects of appending
NB. zeros to coefficients.

NB. =========================================================
Lab Section POWERS AND APPENDED ZEROS
The cube function ^&3 is itself a polynomial, and is equivalent to:

  0 0 0 1&p.

Thus:
)
x=: 2 3 5 7 11
x^3
^&3 x
0 0 0 1&p. x

NB. Moreover, the polynomial (0 0 0,c)&p. is equivalent to
NB. the product of the cube function with the polynomial c&p..
NB. For example:

c2=: 1 2 1
]pre=: 0 0 0,c2

pre&p. x
c2&p. x
(^&3 * c2&p.) x

NB. However, appending zeros to the end of a list of
NB. coefficients has no effect. For example:

]post=: c2,0 0 0
post&p. x
(post&p. = c2&p.) x

NB. =========================================================
Lab Section THE TAYLOR OPERATOR FOR COEFFICIENTS
The Taylor operator t. applied to a polynomial function produces a function that yields the coefficients that represent the polynomial. For example, using the coefficients and polynomials defined earlier:
)
k=: f@g

tk=: k t.             NB. Taylor coefficient function for k

c=: tk 0 1 2 3 4 5 6  NB. Coefficients 0 through 6 for k
c                     NB. Compare with earlier result

c p. x

k x

tk 2 3                NB. Coefficients 2 and 3 for k

f@g t. i. 10          NB. Coefficients with trailing zeros

NB. =========================================================
Lab Section
Coefficients for the sum, difference, and product may be obtained similarly:
)
]csum=: (f+g) t. i.10

csum p. x

(f+g) x

]cdif=: (f-g) t. i. 10

]cprod=: (f*g) t. i. 10

NB. =========================================================
Lab Section
Since h=: f*g is the product of f and g, then f is said to be a divisor of h, and the quotient h%f is equivalent to g. For example:
)
(h%f) x

g x

(h%f) t. i. 10

NB. Similarly, f is a divisor of g, but g is not a divisor of f:

(g%f) x

(g%f) t. i. 10

1 1&p. x

(f%g) x

]cq=:(f%g) t. i. 10

NB. =========================================================
Lab Section
Note that although f%g is not a polynomial, it is a function, called a rational function. Moreover, the Taylor operator applies to it to provide a result that does not terminate with zeros as would a polynomial. Moreover, the leading coefficients of such a "non-terminating" list can be meaningfully used to define a polynomial, provided that it is applied only to arguments whose magnitudes are less than one. The higher powers of such an argument become so small that "neglected" coefficients have no appreciable effect. For example:
)

]y=: 10^-i.9

cq&p. y

(f%g) y

NB. =========================================================
Lab Section TAYLOR SERIES
Results produced by the Taylor operator are called Taylor coefficients, and the functions defined by bonding them with p. are called Taylor series. Not only are Taylor series useful for approximating non-polynomials, but the patterns of the coefficients may provide important insights. For example:
)
x=: i. 5
i=: i. 8

exp=: ^

sin=: 1&o.

cos=: 2&o.

exp x                        NB. Exponentials

]etc=: exp t. i              NB. Exponential Taylor coefficients

etc&p. x                     NB. Taylor series approximation

NB. The Taylor coefficients for the exponential follow a
NB. pattern that is made clear in their reciprocals:

% etc

! i

etc * !i

NB. =========================================================
Lab Section
Similar patterns occur in a number of important functions. They are made clear in the weighted Taylor coefficients produced by the operator t: -- Taylor coefficients multiplied by corresponding factorials:
)
dec=: ^@-      NB. Decaying exponential
sinh=: 5&o.    NB. Hyperbolic sine
cosh=: 6&o.    NB. Hyperbolic cosine

a=: (exp t:,dec t:,sinh t:,sin t:,cosh t:,:cos t:) i

comments=. > ;: 'Exp Decay Sinh Sin Cosh Cos'

comments;a

NB. =========================================================
Lab Section COEFFICIENTS FROM COEFFICIENTS
In an earlier section, we have seen how the operator t. can be used to obtain Taylor coefficients for sums and products of polynomials f=: c&p. and g=: d&p.. We will now examine how they may be obtained directly from the coefficients c and d. If c and d have equal numbers of coefficients, the case of the sum is obvious:
)
x=: i. 6
c=: 3 1 4
d=: 5 3 1
(c&p. + d&p.) x
(c+d)&p. x

NB. Lists of differing lengths cannot be so added, but can
NB. be extended by zeros by laminating them to form a table,
NB.and their sum can then be obtained by adding the two rows:

c2=: 1 2 1
c3=: 1 3 3 1

c2,:c3

+/c2,:c3

(c2 p. x) + (c3 p. x)

(+/ c2,:c3) p. x

NB. A function for "adding" polynomial coefficients may
NB. therefore be defined and used as follows:

   plus=: +/@,:

   c2 plus c3

NB. =========================================================
Lab Section
A function for "multiplying" polynomial coefficients can be defined similarly:
)
times=: +//.@(*/)

c2 times c3

NB. In the function times, the diagonal operator /. applies
NB. its argument (the sum function +/) to each of the diagonals
NB. of the results of the multiplication table function */.
NB. The details may be seen as follows:

] table=: c2 */ c3

</.table        NB. The box function shows the diagonals
                NB. to which any function applies

+//. table

NB. =========================================================
Lab Section
The reason why these diagonal sums of the multiplication table yield the coefficients of the product polynomial may appear obvious. If not, consider the following: 

The product of two sums of terms is the sum of the products of a term from one with each of the terms from the other. The product of typical terms: 

                     
      j        
   c x  

and 

      k         
   d x  

is 

        j+k
   c d x    


The multiplication table of all coefficients (c*/d) may therefore be multiplied by x raised to the powers in the addition table for the indices (that is, (i.#c +/ (i.#d)), and the entire resulting table can be summed. Thus:
)
x=: 3

] exp=: (i.#c2) +/ (i.#c3)

table;exp;(x^exp);(table*x^exp);(+/+/table*x^exp)

NB. Any element of the table exp is an exponent of the
NB. final result that concerns the corresponding element
NB. in the table; the fact that equal elements of exp lie
NB. along the diagonals indicates that the elements of the
NB. table must be summed along these diagonals.

NB. =========================================================
Lab Section
Repeated multiplication by 1 1 produces the table of binomial coefficients. Thus:
)
1 1&times 1

1 1&times 1 1&times 1

1 1&times ^:(i.6) 1

NB. =========================================================
Lab Section
The coefficients of the composition f@g can be obtained as follows:
)
]q=: c3&times ^: (i.#c2) 1     NB. Powers of c3 times

c2*q                           NB. weighted by c2

+/c2*q

on=: 4 : '+/ x. * y.&times ^: (i.#x.) 1'

c2 on c3

NB. =========================================================
Lab Section SECANT SLOPE
The points with coordinates x,f x and y,f y lie on the graph of the function f. The straight line through them is called a secant, and its slope is the quotient rise%run, where rise is the difference (f y)-(f x), and run is the difference y-x. The slope of the secant through x and y gives an "average" value for the rate at which the function f is changing in the interval. If y=: x+s and the spacing s is small, the secant slope is a good approximation to the rate of change of the function f at x; geometrically, it is an approximation to the slope of the tangent to the graph of f at x.

The slope of the cube function may be explored as follows:
)
f=: ^&3

x=: 0 1 2 3 4 5

((f x+1) - f x) % 1

((f x+0.0001) - f x) % 0.0001

ss=: ((f@+ - f@]) % [) " 0

0.0001 ss x

]s=: 10 ^ - i.5

s ss/ x

3*x^2

NB. =========================================================
Lab Section
We may also define a corresponding secant-slope operator:
)
SS=: 1 : 0
((x.@+ - x.@]) % [) "0
)

,. (s ^&3 SS/ x) ; (s ^&4 SS/ x)

4 * x ^ 3

NB. =========================================================
Lab Section DERIVATIVE
The derivative of a function f is a function g that gives the rate of change of f, that is, the slope of the tangent to the graph of f. As suggested by the approximations provided by the secant slopes of the cube and fourth power in the preceding section, the derivative of the power:

    n
   x  

is: 

     n-1 
   nx 

The derivative of a constant multiple of a function is the same multiple of its derivative, and the derivative of the
term:

     n
   cx  

is therefore 

      n-1   
   ncx   

Since the derivative of a sum is the sum of the derivatives of the terms, the
derivative of the polynomial 

                2     3
   c + c x + c x + c x 

is
                    
               1      2 
   0 + c + 2c x + 3c x  

In general, the coefficients of the derivative polynomial are obtained by multiplying the coefficients by their indices, and dropping the first. Thus:
)
c=: 3 1 4 2

c*i.#c

}.c*i.#c

der=: }.@(] * i.@#)

der c

x=: 0 1 2 3 4 5 6 7

c p. x

(der c) p. x

NB. =========================================================
Lab Section
We will test this derivative of the function f=: c&p. in two ways; first by applying the first derivative operator D.1 to f, and secondly by comparing the derivative with the secant slope obtained from the operator SS of an earlier section. Thus:
)
f=: 3 1 4 2 & p.

f D.1 x

0.01 c&p. SS x

NB. =========================================================
Lab Section INGTEGRAL
We now define an integral function analogous to the derivative function der. Thus:
)
der

int=: 0: , ]  % 1: + i.@#

c

int c

der int c

x

(int c) p. x

NB. =========================================================
Lab Section NUMBER SYSTEMS
If d is the list of digits representing a number in the decimal system, then the number represented is obtained by summing the product of d with the powers of 10, in descending order.

It can therefore be treated as a polynomial with argument 10 and coefficients that are the list d in reverse order, |.d. In particular the function times=: +//.@(*/) can be adapted for the product of decimal digits:
)
d=: 1 9 9 7

|.d

(|.d) p. 10