Skip to content
Newer
Older
100644 130 lines (106 sloc) 3.37 KB
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
1 ### Tests of complex arithemetic.
2
b110e60 strict test for complex power
maechler authored Aug 5, 2009
3 Meps <- .Machine$double.eps
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
4 ## complex
5 z <- 0i ^ (-3:3)
6 stopifnot(Re(z) == 0 ^ (-3:3))
7
8
9 ## powers, including complex ones
10 a <- -4:12
11 m <- outer(a +0i, b <- seq(-.5,2, by=.5), "^")
12 dimnames(m) <- list(paste(a), "^" = sapply(b,format))
13 round(m,3)
b110e60 strict test for complex power
maechler authored Aug 5, 2009
14 stopifnot(m[,as.character(0:2)] == cbind(1,a,a*a),
15 # latter were only approximate
16 all.equal(unname(m[,"0.5"]),
17 sqrt(abs(a))*ifelse(a < 0, 1i, 1),
18 tol= 20*Meps))
bf4d2a1 allow R to be compiled without C99 double complex trig functions
ripley authored Feb 6, 2011
19
20 ## 2.10.0-2.12.1 got z^n wrong in the !HAVE_C99_COMPLEX case
21 z <- 0.2853725+0.3927816i
22 z2 <- z^(1:20)
23 z3 <- z^-(1:20)
24 z0 <- cumprod(rep(z, 20))
25 stopifnot(all.equal(z2, z0), all.equal(z3, 1/z0))
26 ## was z^3 had value z^2 ....
27
1cc0bd9 enhance printing of complex numbers to use pairs together.
ripley authored Aug 11, 2005
28 ## fft():
29 for(n in 1:30) cat("\nn=",n,":", round(fft(1:n), 8),"\n")
30
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
31
bf4d2a1 allow R to be compiled without C99 double complex trig functions
ripley authored Feb 6, 2011
32 ## polyroot():
33 stopifnot(abs(1 + polyroot(choose(8, 0:8))) < 1e-10)# maybe smaller..
34
35 ## precision of complex numbers
36 signif(1.678932e80+0i, 5)
37 signif(1.678932e-300+0i, 5)
38 signif(1.678932e-302+0i, 5)
39 signif(1.678932e-303+0i, 5)
40 signif(1.678932e-304+0i, 5)
41 signif(1.678932e-305+0i, 5)
42 signif(1.678932e-306+0i, 5)
43 signif(1.678932e-307+0i, 5)
44 signif(1.678932e-308+0i, 5)
45 signif(1.678932-1.238276i, 5)
46 signif(1.678932-1.238276e-1i, 5)
47 signif(1.678932-1.238276e-2i, 5)
48 signif(1.678932-1.238276e-3i, 5)
49 signif(1.678932-1.238276e-4i, 5)
50 signif(1.678932-1.238276e-5i, 5)
51 signif(8.678932-9.238276i, 5)
52 ## prior to 2.2.0 rounded real and imaginary parts separately.
53
54
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
55 ## Complex Trig.:
56 abs(Im(cos(acos(1i))) - 1) < 2*Meps
57 abs(Im(sin(asin(1i))) - 1) < 2*Meps
58 ##P (1 - Im(sin(asin(Ii))))/Meps
59 ##P (1 - Im(cos(acos(Ii))))/Meps
60 abs(Im(asin(sin(1i))) - 1) < 2*Meps
c79a373 FAQ 7.31 strikes again: the two differ by 1lup on Cygwin.
ripley authored Nov 1, 2011
61 all.equal(cos(1i), cos(-1i)) # i.e. Im(acos(*)) gives + or - 1i:
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
62 abs(abs(Im(acos(cos(1i)))) - 1) < 4*Meps
63
64
823787e so many people use non-integer .Random.seed that make a warning for now
ripley authored Jun 23, 2007
65 set.seed(123) # want reproducible output
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
66 Isi <- Im(sin(asin(1i + rnorm(100))))
67 all(abs(Isi-1) < 100* Meps)
68 ##P table(2*abs(Isi-1) / Meps)
69 Isi <- Im(cos(acos(1i + rnorm(100))))
70 all(abs(Isi-1) < 100* Meps)
71 ##P table(2*abs(Isi-1) / Meps)
72 Isi <- Im(atan(tan(1i + rnorm(100)))) #-- tan(atan(..)) does NOT work (Math!)
73 all(abs(Isi-1) < 100* Meps)
74 ##P table(2*abs(Isi-1) / Meps)
75
bf4d2a1 allow R to be compiled without C99 double complex trig functions
ripley authored Feb 6, 2011
76 set.seed(123)
77 z <- complex(real = rnorm(100), imag = rnorm(100))
78 stopifnot(Mod ( 1 - sin(z) / ( (exp(1i*z)-exp(-1i*z))/(2*1i) )) < 20 * Meps)
79 ## end of moved from complex.Rd
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
80
81
82 ## PR#7781
83 ## This is not as given by e.g. glibc on AMD64
84 (z <- tan(1+1000i)) # 0+1i from R's own code.
85 stopifnot(is.finite(z))
86 ##
87
88
89 ## Branch cuts in complex inverse trig functions
90 atan(2)
91 atan(2+0i)
92 tan(atan(2+0i))
eba1658 more tweaks to complex
ripley authored Aug 10, 2005
93 ## should not expect exactly 0i in result
94 round(atan(1.0001+0i), 7)
95 round(atan(0.9999+0i), 7)
544a9ee move complex arithmetic to a separate file, make a sloppy test for now
ripley authored Aug 10, 2005
96 ## previously not as in Abramowitz & Stegun.
97
98
99 ## typo in z_atan2.
100 (z <- atan2(0+1i, 0+0i))
101 stopifnot(all.equal(z, pi/2+0i))
102 ## was NA in 2.1.1
75917b9 change rounding for complex numbers
ripley authored Aug 11, 2005
103
104
bf4d2a1 allow R to be compiled without C99 double complex trig functions
ripley authored Feb 6, 2011
105 ## Hyperbolic
106 x <- seq(-3, 3, len=200)
107 Meps <- .Machine$double.eps
108 stopifnot(
109 Mod(cosh(x) - cos(1i*x)) < 20*Meps,
9d6bd5f clean up
ripley authored Feb 15, 2011
110 Mod(sinh(x) - sin(1i*x)/1i) < 20*Meps
bf4d2a1 allow R to be compiled without C99 double complex trig functions
ripley authored Feb 6, 2011
111 )
112 ## end of moved from Hyperbolic.Rd
113
f3295d5 more on complex
ripley authored Feb 7, 2011
114 ## values near and on branch cuts
115 options(digits=5)
116 z <- c(2+0i, 2-0.0001i, -2+0i, -2+0.0001i)
117 asin(z)
118 acos(z)
119 atanh(z)
120 z <- c(0+2i, 0.0001+2i, 0-2i, -0.0001i-2i)
121 asinh(z)
122 acosh(z)
123 atan(z)
9d6bd5f clean up
ripley authored Feb 15, 2011
124 ## According to C99, should have continuity from the side given if there
125 ## are not signed zeros
f3295d5 more on complex
ripley authored Feb 7, 2011
126 ## Both glibc 2.12 and Mac OS X 10.6 use continuity from above in the first set
9d6bd5f clean up
ripley authored Feb 15, 2011
127 ## but they seem to assume signed zeros.
f3295d5 more on complex
ripley authored Feb 7, 2011
128 ## Windows gave incorrect (NaN) values on the cuts.
129
Something went wrong with that request. Please try again.