Re: [R] Sweave and complex numbers

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Mon 11 Jul 2005 - 22:02:27 EST

Robin Hankin wrote:
> Hi
>
> When using Sweave, most of my functions get called with complex
> arguments.
>
> They get typeset in with additions that I don't want; "1+1i" appears
> as "1 + (0 + 1i)"
> and I would rather have plain old "1+1i".
>
> Example follows:
>
>
>
>
> \documentclass[a4paper]{article}

>
> \title{A Test File}
> \author{Robin Hankin}
>
> \usepackage{a4wide}
>
> \begin{document}
>
> \maketitle
>
> A simple example:
> <<print=TRUE>>=
> f <- function(x){
> x+c(1,1+1i)}
> f(6+7i)
> @
>
>
> Question: why does \verb=c(1,1+1i)= get printed as
> \verb=c(1,1 + (0+1i))= ?
>
> And how can I stop it?
>
> \end{document}
>
>
>
>
> can anyone help me here?

The R parser only understands pure imaginary constants as complex numbers. It parses 1+1i as the sum of the real constant 1 and the complex constant 0+1i.

This isn't easy to work around. Sweave could special case these, or the parser or deparser could, but it looks messy to me. I'd guess the best solution would be if it happened in the parser (i.e. a real constant plus an imaginary constant was folded into a complex constant), but right now our parser doesn't do any sorts of optimizations like this. It's not easy to add the first one, not least because the parser doesn't know for sure that 1+1i really is a complex number: you might have redefined the meaning of "+".

Maybe someone else has an answer to your 2nd question.

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Jul 11 22:07:58 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:28 EST