LABTITLE=: 'Function Display'
LABFOCUS=: 0

NB. =========================================================
Lab Section INTRODUCTION

Although a simple representation, such as the vector of coefficients of a polynomial, may give all essential information about a particular function, it may also be helpful to display it in a more elaborate and more familiar form.

For example, we may use the following function "display" to show the polynomial represented by the coefficients c:
)
c=: 1 3 3 1
y=: 0 1 2 3 4 5 6
c p. y

display=: 3 : 0
r=. '',:''                NB. 2 row 0 col

while. 0<#y. do.
  r=. ('',:~":<:# y.),.r  NB. exponent
  r=. ('',:,'x'),.r       NB. argument x
  r=. ('',:":{:y.),.r     NB. coefficient
  r=. ('',:,'+'),.r       NB. plus sign
  y.=. }:y.               NB. delete last coefficient
end.

}."1 r                    NB. delete leading +
)

display c

display c,c,c,c,c,c

NB. Put cursor in field and use right and left arrows
NB. to view long displays.

NB. =========================================================
Lab Section FUNCTIONS OF TWO ARGUMENTS

Although expressions such as a+b and a-b and a*b provide a completely general scheme for functions of two arguments, other schemes abound in mathematics. Many of these fall in two popular classes:

.                                              n
The use of superscripts and subscripts, as in x  for x
to the power n, in log y for the base b logarithm of y,
.                       b
and in 1066  for the base 8 value of the digits 1066.
.          8

2. The use of various brackets to embrace the arguments, as
       n                                  n       n
   in ( ) for binomial coefficients, and [ ] and { } for
       k                                  k       k
   Stirling numbers of the first and second kind, and [m\n]
   for a test of divisibility of n by m. The brackets, of
   course, appear in a font large enough to completely
   embrace the arguments.

Several of these dyadic functions occur as primitives in J. For example, try entering the following expressions:

   5^3         NB. 5 to the power 3

   10^.1000    NB. Base 10 logarithm of 1000

   3!5         NB. Binomial coeff (# of ways of 3 outof 5)

   8(0&=@|)32  NB. Test if 8 divides 32

   10 #. 1 0 6 6

   8 #. 1 0 6 6
)

NB. =========================================================
Lab Section

The functions ^ and ^. and ! follow the standard pattern set by + and * for extension to array arguments. For example, try the following:

   0 1 2 3 4 ! 4

   0 1 2 3 4 !/ 0 1 2 3 4

Although bracket and super- and sub-script notation might be extended similarly, expressions of the following form would be rather jarring:

        4
   (         )
    0 1 2 3 4

   log          20
      2 3 4 5 6

   log          20 30 40 50 60
      2 3 4 5 6

   log20 30 40 40 50 60
      2 3 4 5 6
)

NB. =========================================================
Lab Section

The operator / serves to apply a function over an array argument, as in +/ for summation, and */ for product-over.

It may be remarked that the superscript used to denote the power function provides no symbol for such use with an operator.

Although type fonts and special symbols may no longer present a problem, it is easy to see how printers found it easier to provide superscripts and subscripts, as well as common brackets in various sizes, than to cast new special symbols for use in mathematical texts.
)

NB. =========================================================
Lab Section SIGMA AND PI

The capital Greek Sigma and Pi are commonly used to signify the sum and product over what follows them. We will use the functions S and PI, as defined and illustrated below:
)
  S=: +/
 PI=: */

 AS=: -/   NB. Alternating sum
API=: %/   NB. Alternating product

d=: 1 4 6 4 1

S d
1+4+6+4+1

PI d
1*4*6*4*1

AS d
1-4-6-4-1

API d
1%4%6%4%1

NB. =========================================================
Lab Section POLYNOMIALS

Operations on polynomials can be treated in terms of their coefficients. For example, the coefficients of the product of two polynomials can be obtained as the diagonal sums of the product table of the coefficients. Thus:
)
c=: 1 3 3 1
d=: 1 4 6 4 1

c*/d

e=: +//. c*/d

e

e p. y

(c p. y) * (d p. y)

display e

NB. =========================================================
Lab Section

A polynomial product function may therefore be defined and used as follows:
)
times=: +//.@(*/)

f=: e times e

f

g=: f times f times c

8 4 $ g

display g

,. g p. x: y  NB. The function x: introduces extended precision

(6+1) ^ x: 31

NB. =========================================================
Lab Section RATIONALS

A function defined as the quotient of two polynomials c&p. and d&p. is called a rational function. Such a function is commonly displayed as the first polynomial above the second. Thus:
)
rational=: (display@[),('-'"_),(display@])

c rational d

e rational f

NB. Put cursor in field and use right arrow to see long display

NB. =========================================================
Lab Section

We will now define and use a rational operator RAT such that c RAT d gives the rational function defined by the polynomial with coefficients c in the numerator, and the polynomial with coefficients d in the denominator:
)
RAT=: 2 : 'm.&p. % n.&p.' NB. Rational operator

c;d;y

c RAT d

c RAT d y

d RAT c y

PROD=: 2 : 'm.&p. * n.&p.' NB. Product operator

c PROD d y

NB. =========================================================
Lab Section TAYLOR COEFFICIENTS

The expression f t. applies the Taylor operator t. to the function f to produce a function which, applied to an index, produces the corresponding Taylor coefficient. For example:
)
^ t. 0 1 2 3 4 5  NB. Coeffs of approximation to exponential

i=: 0 1 2 3 4 5 6 7 8 9 10

c;d

c&p. t. i

c PROD d t. i

c RAT d t. i  NB. Non-terminating rational

d RAT c t. i

NB. =========================================================
Lab Section FUNCTIONS OF n ARGUMENTS

Expressions of the form f(a,b,c) and f(a,b,c,d) are commonly used to denote functions of more than two arguments.

If the individual arguments a, b, etc. are scalars, the same form may be used in J. For example:
)
f=: +/          NB. Sum

g=: +/@(]*])    NB. Sum of products

h=: */@(]^])    NB. Product of powers

f(1,2,3,4,5,6)

g(1,2,3,4)

h(1,2,3,4)

NB. =========================================================
Lab Section

However, a function such as the product of polynomial coefficients requires vector arguments, and these may not be simply catenated, but must be linked in separate boxes to preserve their identities. For example:
)
times=: +//.@(*/)

b=: 1 2 1
c=: 1 3 3 1
d=: 1 4 6 4 1

c times d

b times c times d

b,c,d

b;c;d

times &.>/ b;c;d

>times &.>/ b;c;d

NB. =========================================================
Lab Section

The case of vector arguments appears to present a similar problem in the conventional notation f(b,c,d) . On the one hand, the comma is commonly used to denote catenation, as in (3,4,5), but cannot be so construed in the expression f(b,c,d) when b and c and d are vectors.
)

NB. =========================================================
Lab Section

A continued fraction is commonly expressed in the following form:

   3 + 1
       -
       7 + 1
           -
           14 + 1
                -
                1

It may be expressed in terms of the list 3 7 14 1 as follows:
)
pr=: + %   NB. Plus reciprocal

3 pr 7

3 pr 7 pr 14

pr/3 7 14 1

NB. =========================================================
Lab Section

A general continued fraction (with numerators other than one) would be written as:

  2 + 3
      -
      5 + 7
          -
          11 + 13
               --
               17

It may be expressed in terms of the list 2 3 5 7 11 13 17 as follows:
)
pd=: +`%   NB. Plus divide

pd/ 2 3 5 7 11 13 17

2 + 3 % 5 + 7 % 11 + 13 % 17
