[R] FAQ 7.31

Robin Hankin r.hankin at noc.soton.ac.uk
Fri May 6 09:27:11 CEST 2005

Jacobi's theta functions crop up here and there in various branches of 
such as unimodular and elliptic functions.

They have Taylor expansions, but the powers increase as the square of 
n, as in

1 + z + z^4 + z^9 + z^16 + z^25 + . . .

so they converge very quickly, provided |z|<1

The following toy function shows how I'm implementing these objects.  I 
just add terms until
they make no difference:

f <- function(z, maxiter=10){
    out.old <- 1
    for(n in 1:maxiter){
      out.new <- out.old + z^(n*n)
      if(identical(out.new, out.old)){
      out.old <- out.new
    stop("not converged")

[NB this is not a real theta function!  Real theta functions  are more 
complicated. f() just shows the issues]

I worry about the use of identical() here, because I am comparing two 
floats for equality
as discussed in FAQ 7.31.    Perhaps all.equal() would be better:

  all.equal(1e99,1e99+1e83, tol=.Machine$double.eps)

Does the List have any comments to make?

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

More information about the R-help mailing list