>>  <<  Ndx  Usr  Pri  JfC  LJ  Phr  Dic  Rel  Voc  !:  wd  Help  Learning J

Chapter 23: Calculus

This chapter covers J operators for differentiation and integration. It covers

  • The conjunction d. which differentiates and integrates analytically, that is, it transforms expressions denoting functions into expressions denoting functions.
  • The conjunction D. which differentiates numerically, and thus broadens the range of functions which can be differentiated. It also covers partial derivatives.
  • A library script with functions for numerical integration.

23.1 Differentiation

There is a built-in conjunction d.(lowercase d dot). Its left argument is a function to be differentiated. Its right argument is 1 if the first derivative is required, or 2 for the second derivative, and so on. The first derivative of the "cube" function ^&3 is "3 times the square".

   ^&3  d. 1

The general scheme is that if e is (an expression denoting) a function, then e d. n is (an expression denoting) the n'th derivative of e. Here is another example, expressing the cube function as the polynomial 0 0 0 1 & p.

   0 0 0 1 & p. d. 1
0 0 3 & p.

Suppose we define a verb cube:

cube =: 0 0 0 1 & p. cube 2
0 0 0 1&p. 8

Differentiating with d., we see that the derivative is, as expected, 3-times-the-square, but the expression for the derivative is not very informative.

(cube d. 1) 2 cube d. 1
12 cube d.1

The reason is that cube is a name denoting a verb, and such names are in general not evaluated until the verb is applied. (See Appendix 1 .) If we want to inspect the derivative of cube, we can force evaluation of the name cube by applying the f. adverb.

cube d. 1 (cube f.) d. 1
cube d.1 0 0 3&p.

Alternatively, we could force evaluation of the expression for the derivative, again by applying f.

cube d. 1 (cube d. 1) f.
cube d.1 0 0 3&p.

23.2 Integration

With a right argument of _1, the conjunction d. integrates the left argument. The integral of "3 times the square" is "cube".

   0 0 3 & p. d. _1
0 0 0 1 & p.

23.3 The Domain of d.

Functions which are differentiable or integrable with d. must firstly be scalar. That is, they must take scalar arguments and deliver scalar results, and all intermediate quantities must be scalars.

Here is an example. The function "(x-1)*(x-2)" can be written in several different ways. Here are two:

f =: -&1 * -&2 g =: (*/) @: (- & 1 2) f 3 g 3
-&1 * -&2 */@:(-&1 2) 2 2

f is scalar, and in the domain of d. However, g is not scalar, because it forms the intermediate quantity x - 1 2 which is a vector. Thus g is not in the domain of d.. To demonstrate this, we force evaluation of the derivatives.

(f d. 1) f. (g d. 1) f.
_3 2x&p. error

Secondly, d. can differentiate constant functions, polynomials, exponentials ^x and integral powers x^n.

3: d. 1 0 2 &p. d. 1 ^ d. 1 ^&4 d. 1
0"0 2"0 ^ 4&*@(^&3)

If f and g are differentiable with d., then so are the forks (f+g), (f-g), (f*g) and f%g.

f =: ^&3 g =: 0 2 & p. ((f + g) d. 1) f.
^&3 0 2&p. 2 0 3x&p.

Trigonometric functions are differentiable with d. The derivative of the fork (sin + cos) is (cos - sin).

sin =: 1&o. cos =: 2&o. (sin + cos) f. d. 1
1&o. 2&o. 2&o. + -@(1&o.)

If f and g are differentiable with d., then so are the compositions f@g and f@:g

f g (f @ g d. 1) f.
^&3 0 2&p. 0 0 24x&p.

23.4 The Conjunction D.

The conjunction D. (uppercase D dot) computes derivatives. It differs from d. in two ways.

  • By using numerical methods it can differentiate arbitrary functions, that is, it is not limited to the domain of d.
  • It is not limited to scalar functions: it can differentiate functions with vector arguments to produce partial derivatives.

23.4.1 The Domain of D.

Since D. can use numerical methods, its arguments can be arbitrary functions. For example, recall the function g above, to compute "(x-1)*(x-2)",which was demonstrated above to be outside the domain of d. . However it is within the domain of D.. Its derivative is "2x-3"

g =: (*/) @: (- & 1 2) (g d. 1) 3 (g D. 1) 3
*/@:(-&1 2) error 3

23.4.2 Partial Derivatives with D.

Next we look at functions which compute a scalar from a vector argument. For example consider a surface where the height at a point (x,y) is given by

             (sin x) * (cos y)

The height-function, with the vector argument (x,y) might be written:

   h =: (sin @ {.) * (cos @ {:)

The expression (h D.1)(x,y) computes the numerical values of the two slopes, in the x-direction and the y-direction, of the function h at the point (x,y).

x=: 0.4 y =: 0.5 p =: h D. 1 x,y
0.4 0.5 0.8083071 _0.1866971

The result p gives the values of the partial derivatives with respect to x and with respect to y.

We can check this result. Suppose we define a function q say, for the height along the line y=0.5. We want q(x) to be h(x,0.5) and thus

   q =: h @: (, & 0.5)

The idea now is that the derivative of q applied to argument x should be the same as the first partial derivative of h at x,0.5.

h D.1 x,y q D. 1 x
0.8083071 _0.1866971 0.8083071

Now we look at partial derivatives of functions which compute vectors from vectors. Here is an example, a function which takes the point (x,y,z) in 3-space to the point (2x,3y) in 2-space.

foo =: (2 3 & *) @: (1 1 0 & #) foo 1 1 1
2 3&*@:(1 1 0&#) 2 3

In general such a function will take an argument-vector of length m and produce a result-vector of length n. Hence there will be m*n partial derivatives, one for each element of the result with respect to each element of the argument. The six partial derivatives of foo at the point xyz = 1 1 1 are shown by:

pd =: foo D. 1 pd 1 1 1
foo D.1 2 0
0 3
0 0

Consider now a function such as cube which produces scalars from scalars. Given a vector argument, it will produce a vector result of the same length, where an element of the result depends only on the corresponding element of the argument.

cube cube a =: 1 2 3
0 0 0 1&p. 1 8 27

Therefore, for a scalar function, all partial derivatives are zero except those for elements of the result with respect to the corresponding elements of the argument.

pd =: cube D. 1 pd 2 3 4
cube D.1 12  0  0
0 27  0
0  0 48

If a scalar function is given in fully-evaluated form, and is in the domain of d., the D. conjunction will produce an analytic expression for the partial derivatives function:

PD =: (0 0 0 1 & p.) D.1 PD 2 3 4
(* =/~@(i.@$))@:(0 0 3&p.) 12  0  0
0 27  0
0  0 48

23.5 Numerical Integration

There is a library script containing several different operators for integration. Documentation is given in the script file. It can be loaded like this:

   load jpath '~system\packages\math\integrat.ijs'

One of these integration operators is the conjunction adapt ("numeric integration by adaptive quadrature"). The expression f adapt (L,U) computes the numeric value of the definite integral of f between limits L and U. For example, we expect the integral of 3&* between 0 and 1 to be 1.5

f =: 3&* f adapt 0 1
3&* 1.5

This is the end of Chapter 23.

Table of Contents

The examples in this chapter were executed using J version 601-o-beta. This chapter last updated 25 Jun 2006 .
Copyright © Roger Stokes 2006. This material may be freely reproduced, provided that this copyright notice is also reproduced.

>>  <<  Ndx  Usr  Pri  JfC  LJ  Phr  Dic  Rel  Voc  !:  wd  Help  Learning J