RobinHankin / clifford

https://robinhankin.github.io/clifford/
5 stars 0 forks source link

automatic differentiation #79

Closed RobinHankin closed 1 year ago

RobinHankin commented 1 year ago

One of the applications of dual numbers given at https://en.wikipedia.org/wiki/Dual_number is automatic differentiation and it would be good to include a discussion of this at inst/dual_numbers.Rmd

RobinHankin commented 1 year ago

It is much easier than I thought:

> signature(0)
> f <- function(x){5*x + (x-2)^3*(x+1)/3 -x^6/10}
> coeffs(grade(f(x),1))
[1] -41057.74
> d <- 1e-6
> (f(9.3+d)-f(9.3-d))/(2*d)
[1] -41057.74
> 

Or even

> f <- function(x){x^5}
> fdash <- function(x){5*x^4}
> x <- 1.3 + e(1)
> f(x)
Element of a Clifford algebra, equal to
+ 3.71293 + 14.2805e_1
> coeffs(grade(f(x),1))
[1] 14.2805
> coeffs(grade(f(x),1)) - fdash(1.3)
[1] -1.776357e-15
> 
RobinHankin commented 1 year ago

But I must remember to cite the dual R package at https://cran.r-project.org/package=dual

RobinHankin commented 1 year ago

I can provide a klunky alternative to the dual package:

> signature(0)
> x <- 1.1 + 3.4*e(1)
> Im <- function(z){z-const(z)}
> sin(const(x)) + Im(x)*cos(const(x))
Element of a Clifford algebra, equal to
+ 0.8912074 + 1.542227e_1
> sin(dual(1.1,3.4))
Real: 0.891207
Duals: 1.542227
> 

(I really ought to have Re() and Im() like in the onion package, will raise a new issue for that)

RobinHankin commented 1 year ago

See issue #81 for Re() and Im()