
Chapter 23: CalculusThis chapter covers J operators for differentiation and integration. It covers
23.1 DifferentiationThere is a builtin 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 3&*@(^&2)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:
Differentiating with d., we see that the derivative is, as expected, 3timesthesquare, but the expression for the derivative is not very informative.
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.
Alternatively, we could force evaluation of the expression for the derivative, again by applying f.
23.2 IntegrationWith 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 "(x1)*(x2)" can be written in several different ways. Here are two:
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.
Secondly, d. can differentiate constant functions, polynomials, exponentials ^x and integral powers x^n.
If f and g are differentiable with d., then so are the forks (f+g), (fg), (f*g) and f%g.
Trigonometric functions are differentiable with d. The derivative of the fork (sin + cos) is (cos  sin).
If f and g are differentiable with d., then so are the compositions f@g and f@:g
23.4 The Conjunction D.The conjunction D. (uppercase D dot) computes derivatives. It differs from d. in two ways.
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 "(x1)*(x2)",which was demonstrated above to be outside the domain of d. . However it is within the domain of D.. Its derivative is "2x3"
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 heightfunction, 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 xdirection and the ydirection, of the function h at the point (x,y).
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.
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 3space to the point (2x,3y) in 2space.
In general such a function will take an argumentvector of length m and produce a resultvector 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:
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.
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.
If a scalar function is given in fullyevaluated form, and is in the domain of d., the D. conjunction will produce an analytic expression for the partial derivatives function:
23.5 Numerical IntegrationThere is a library scriptfile called integrat.ijs . It contains several different operators for integration. Documentation is given in the script file.It can be be downloaded from the JSoftware website: here is a link to integrat.ijs Assuming that we have downloaded into a directory, say C:\temp for example, then we load it into the J session with: load 'c:\temp\integrat.ijs'One of the integration operators provided 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
This is the end of Chapter 23. 
The examples in this chapter
were executed using J version j701/beta/20101124/22:45.
This chapter last updated 22 Dec 2010
Copyright © Roger Stokes 2010.
This material may be freely reproduced,
provided that this copyright notice is also reproduced.