J Labs

Polynomials

(1 of 25) INTRODUCTION

An expression of the form $3x^2+4x^5+2x^4$ 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:


In [ ]:
x=: 3

In [ ]:
c=: 3 4 2

In [ ]:
e=: 2 5 4

In [ ]:
x^e

In [ ]:
c * x^e

In [ ]:
+/c * x^e

(2 of 25) INTRODUCTION (ctd)

Since addition is both associative and commutative, the terms may be permuted to any order, without changing the result, for example $4x^5+2x^4+3x^2$

Thus:


In [ ]:
perm=: 1 2 0

In [ ]:
] ep=: perm { e

In [ ]:
] cp=: perm { c

In [ ]:
cp * x^ep

In [ ]:
+/cp * x^ep

(3 of 25) INTRODUCTION (ctd)

Since $x^0$ 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:


In [ ]:
cd=: 4 2 0 3 0 0

In [ ]:
ed=: 5 4 3 2 1 0

In [ ]:
cd * x^ed

In [ ]:
+/cd * x^ed

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


In [ ]:
] ca=: |. cd

In [ ]:
] ea=: |. ed

In [ ]:
+/ca*x^ea

(4 of 25) INTRODUCTION (ctd)

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:


In [ ]:
c=: 0 0 3 0 2 4

In [ ]:
# c

In [ ]:
]e=: i.#c

In [ ]:
+/c*x^i.#c

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


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

In [ ]:
c p x

In [ ]:
c p 1

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


In [ ]:
c p. x

In [ ]:
y=: 2 3 5 7 11

In [ ]:
c p. y

(5 of 25) INTRODUCTION (ctd)

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 $3x^2-4x^5+2x^4$.

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


In [ ]:
c=: 3 _4 2

In [ ]:
e=: 2 5 4

In [ ]:
+/c*x^e

In [ ]:
0 0 3 0 2 _4 p. x

(6 of 25) INTRODUCTION (ctd)

The term polynomial refers to a function of a single argument, the x in the expression $4x^5+2x^4+3x^2$.

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:


In [ ]:
c2=: 1 2 1

In [ ]:
c3=: 1 3 3 1

In [ ]:
f=: c2&p.

In [ ]:
g=: c3&p.

In [ ]:
x=: 2 3 5 7 11

In [ ]:
f x

In [ ]:
g x

In [ ]:
h=: f*g

In [ ]:
h x

In [ ]:
(f+g) x

(7 of 25) POLYNOMIAL FUNCTIONS

The sum and the product of polynomials used above are themselves polynomials, as is the composition f@g. Thus:


In [ ]:
f g x

In [ ]:
f@g x

In [ ]:
k=: f@g

In [ ]:
k x

In [ ]:
4 12 21 22 15 6 1&p. x

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

(8 of 25) POWERS AND APPENDED ZEROS

The cube function ^&3 is itself a polynomial, and is equivalent to:

```0 0 0 1&p.```

Thus:


In [ ]:
x=: 2 3 5 7 11

In [ ]:
x^3

In [ ]:
^&3 x

In [ ]:
0 0 0 1&p. x

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


In [ ]:
c2=: 1 2 1

In [ ]:
]pre=: 0 0 0,c2

In [ ]:
pre&p. x

In [ ]:
c2&p. x

In [ ]:
(^&3 * c2&p.) x

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


In [ ]:
]post=: c2,0 0 0

In [ ]:
post&p. x

In [ ]:
(post&p. = c2&p.) x

(9 of 25) 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:


In [ ]:
k=: f@g

In [ ]:
tk=: k t.             NB. Taylor coefficient function for k

In [ ]:
c=: tk 0 1 2 3 4 5 6  NB. Coefficients 0 through 6 for k

In [ ]:
c                     NB. Compare with earlier result

In [ ]:
c p. x

In [ ]:
k x

In [ ]:
tk 2 3                NB. Coefficients 2 and 3 for k

In [ ]:
f@g t. i. 10          NB. Coefficients with trailing zeros

(10 of 25) THE TAYLOR OPERATOR FOR COEFFICIENTS (ctd)

Coefficients for the sum, difference, and product may be obtained similarly:


In [ ]:
]csum=: (f+g) t. i.10

In [ ]:
csum p. x

In [ ]:
(f+g) x

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

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

(11 of 25) THE TAYLOR OPERATOR FOR COEFFICIENTS (ctd)

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:


In [ ]:
(h%f) x

In [ ]:
g x

In [ ]:
(h%f) t. i. 10

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


In [ ]:
(g%f) x

In [ ]:
(g%f) t. i. 10

In [ ]:
1 1&p. x

In [ ]:
(f%g) x

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

(12 of 25) THE TAYLOR OPERATOR FOR COEFFICIENTS (ctd)

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:


In [ ]:
]y=: 10^-i.9

In [ ]:
cq&p. y

In [ ]:
(f%g) y

(13 of 25) 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:


In [ ]:
x=: i. 5

In [ ]:
i=: i. 8

In [ ]:
exp=: ^

In [ ]:
sin=: 1&o.

In [ ]:
cos=: 2&o.

In [ ]:
exp x                        NB. Exponentials

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

In [ ]:
etc&p. x                     NB. Taylor series approximation

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


In [ ]:
% etc

In [ ]:
! i

In [ ]:
etc * !i

(14 of 25) TAYLOR SERIES (ctd)

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:


In [ ]:
dec=: ^@-      NB. Decaying exponential

In [ ]:
sinh=: 5&o.    NB. Hyperbolic sine

In [ ]:
cosh=: 6&o.    NB. Hyperbolic cosine

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

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

In [ ]:
comments;a

(15 of 25) 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:


In [ ]:
x=: i. 6

In [ ]:
c=: 3 1 4

In [ ]:
d=: 5 3 1

In [ ]:
(c&p. + d&p.) x

In [ ]:
(c+d)&p. x

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


In [ ]:
c2=: 1 2 1

In [ ]:
c3=: 1 3 3 1

In [ ]:
c2,:c3

In [ ]:
+/c2,:c3

In [ ]:
(c2 p. x) + (c3 p. x)

In [ ]:
(+/ c2,:c3) p. x

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


In [ ]:
plus=: +/@,:

In [ ]:
c2 plus c3

(16 of 25) COEFFICIENTS FROM COEFFICIENTS (ctd)

A function for "multiplying" polynomial coefficients can be defined similarly:


In [ ]:
times=: +//.@(*/)

In [ ]:
c2 times c3

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


In [ ]:
] table=: c2 */ c3

In [ ]:
</.table        NB. The box function shows the diagonals

to which any function applies


In [ ]:
+//. table

(17 of 25) COEFFICIENTS FROM COEFFICIENTS (ctd)

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:


In [ ]:
x=: 3

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

In [ ]:
table;exp;(x^exp);(table*x^exp);(+/+/table*x^exp)

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

(18 of 25) COEFFICIENTS FROM COEFFICIENTS (ctd)

Repeated multiplication by 1 1 produces the table of binomial coefficients. Thus:


In [ ]:
1 1&times 1

In [ ]:
1 1&times 1 1&times 1

In [ ]:
1 1&times ^:(i.6) 1

(19 of 25) COEFFICIENTS FROM COEFFICIENTS (ctd)

The coefficients of the composition f@g can be obtained as follows:


In [ ]:
]q=: c3&times ^: (i.#c2) 1     NB. Powers of c3 times

In [ ]:
c2*q                           NB. weighted by c2

In [ ]:
+/c2*q

In [ ]:
coeffs=. 4 : '+/ x * y&times ^: (i.#x) 1'

In [ ]:
c2 coeffs c3

(20 of 25) 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:


In [ ]:
f=: ^&3

In [ ]:
x=: 0 1 2 3 4 5

In [ ]:
((f x+1) - f x) % 1

In [ ]:
((f x+0.0001) - f x) % 0.0001

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

In [ ]:
0.0001 scs x

In [ ]:
]s=: 10 ^ - i.5

In [ ]:
s scs/ x

In [ ]:
3*x^2

(21 of 25) SECANT SLOPE (ctd)

We may also define a corresponding secant-slope operator:


In [ ]:
SCS=: 1 : 0
((x@+ - x@]) % [) "0
)

In [ ]:
,. (s ^&3 SCS/ x) ; (s ^&4 SCS/ x)

In [ ]:
4 * x ^ 3

(22 of 25) 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 $x^n$ is $nx^{n-1}$.

The derivative of a constant multiple of a function is the same multiple of its derivative, and the derivative of the term $cx^n$ is $ncx^{n-1}$.

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

$c + cx + cx^2 + cx^3$

is

$0 + c + 2cx^1 + 3cx^2$.

In general, the coefficients of the derivative polynomial are obtained by multiplying the coefficients by their indices, and dropping the first. Thus:


In [ ]:
c=: 3 1 4 2

In [ ]:
c*i.#c

In [ ]:
}.c*i.#c

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

In [ ]:
der c

In [ ]:
x=: 0 1 2 3 4 5 6 7

In [ ]:
c p. x

In [ ]:
(der c) p. x

(23 of 25) DERIVATIVE (ctd)

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 SCS of an earlier section. Thus:


In [ ]:
f=: 3 1 4 2 & p.

In [ ]:
f D.1 x

In [ ]:
0.01 c&p. SCS x

(24 of 25) INTEGRAL

We now define an integral function analogous to the derivative function der. Thus:


In [ ]:
der

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

In [ ]:
c

In [ ]:
int c

In [ ]:
der int c

In [ ]:
x

In [ ]:
(int c) p. x

(25 of 25) 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:


In [ ]:
d=: 1 9 9 7

In [ ]:
|.d

In [ ]:
(|.d) p. 10

End of Lab


In [ ]: