deriv: Differentiation of 'freealg' objects

derivR Documentation

Differentiation of freealg objects



Differentiation of freealg objects


## S3 method for class 'freealg'
deriv(expr, r, ...)



Object of class freealg


Integer vector. Elements denote variables to differentiate with respect to. If r is a character vector, it is interpreted as a=1,b=2,...,z=26; if of length 1, “aab” is interpreted as c("a","a","b")


Further arguments, currently ignored


Experimental function deriv(S,v) returns \mjeqn\frac\partial^r S\partial v_1\partial v_2...\partial v_rd^rS/ The Leibniz product rule


(u\cdot v\right)'=uv'+u'vomitted; see latex

operates even if (as here) u,v do not commute. For example, if we wish to differentiate aaba with respect to a, we would write f(a) = aaba and then

f(a+δ a) = (a+δ a)(a+δ a)b(a+δ a)

and working to first order we have

f(a+δ a) -f(a)= (δ a)aba + a(δ a)ba + aab(δ a).

In the package:

    > deriv(as.freealg("aaba"),"a")
    free algebra element algebraically equal to
    + 1*aab(da) + 1*a(da)ba + 1*(da)aba

A term of a freealg object can include negative values which correspond to negative powers of variables. Thus:

    > deriv(as.freealg("AAAA"),"a")       
    free algebra element algebraically equal to
    - 1*AAAA(da)A - 1*AAA(da)AA - 1*AA(da)AAA - 1*A(da)AAAA

(see also the examples). Vector r may include negative integers which mean to differentiate with respect to the inverse of the variable:

    > deriv(as.freealg("3abcbCC"),"C")
    free algebra element algebraically equal to
    + 3*abcbC(dC) + 3*abcb(dC)C - 3*abc(dC)cbCC

It is possible to perform repeated differentiation by passing a suitable value of r. For \mjeqn\frac\partial^2\partial a\partial comitted:

    > deriv(as.freealg("aaabAcx"),"ac")
    free algebra element algebraically equal to
    - 1*aaabA(da)A(dc)x + 1*aa(da)bA(dc)x + 1*a(da)abA(dc)x + 1*(da)aabA(dc)x

The infinitesimal indeterminates (“da” etc) are represented by SHRT_MAX+r, where r is the integer for the symbol, and SHRT_MAX is the maximum short integer. This includes negative r. So the maximum number for any symbol is SHRT_MAX. Inverse elements such as A, being represented by negative integers, have differentials that are SHRT_MAX-r.

Function deriv() calls helper function lowlevel_diffn() which is documented at Ops.freealg.Rd.

A vignette illustrating this concept and furnishing numerical verification of the code in the context of matrix algebra is given at inst/freealg_matrix.Rmd.


Robin K. S. Hankin



x <- rfalg()

y <- rfalg(7,7,17,TRUE)

deriv(y,1:5)-deriv(y,sample(1:5)) # should be zero

freealg documentation built on Aug. 28, 2022, 9:05 a.m.