[R] FAQ 7.31

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Fri 06 May 2005 - 17:27:11 EST

Jacobi's theta functions crop up here and there in various branches of mathematics,
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

R-help@stat.math.ethz.ch mailing list
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri May 06 17:32:47 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:37 EST