Functions for testing and other
Functions for testing on equality, exactly or with a tolerance, functions usable as parameters in other functions, pythagorean sums, etc.
chsvd( s ) chsvd( s ) divmod( i, n ) divmodL( i, n ) dsm( x, w ) equal( x, y ) equalFuzzy( x, y, prec=8*.Machine$double.eps, rel=TRUE ) exch( x, L, R ) frac(x,d) int( x ) inrange( x, y ) Ko(z) Km(z) last( x ) LE ( x ) loop.vp( ve, overlap=1 ) LS ( ) lV ( x ) mod( x, y ) modR( x, y ) modS( x, y ) norm2( x ) one ( x ) onebyx( x ) powr( a, x ) pythag( a, b ) quotmean( x, y ) safeDiv( num, den ) signp( x ) solveQeq( a, b, c ) sqr( x ) sqrtH( x ) submod( x, v ) zero ( x )
prec,L,R |
Real |
a,b,c,z |
Complex |
i |
Integer vector |
d |
If not missing, 'frac' shows 'd' decimals after "." as integer |
n,num,den |
Integer |
rel |
Boolean |
s |
square matrix, result of |
v |
real vector > 0, preferably cumsum of some other positive vector |
ve |
real any vector or matrix |
overlap |
integer vector, giving element indices/column numbers to be appended at the end, see examples |
x,y |
Real vector |
w |
Real vector > 0 |
BEWARE of NAs !!chsvd
Check for svd
to reproduce matrix. divmod
rbind(div, mod) for ease of use.divmodL
list(d = div, m = mod) dsm
combination of divmod and submod, used in Jul2Dat
equalFuzzy
One can choose between relative and absolute precisionequal
x == y, of same length.inrange
Check if 'x' (scalar) is in the range (min(y),max(y)).int
returns 'x' as integer in fix formatlast
return the last element of a vector.LE
short for 'length(x)'.LS
short for '.Last.value'.loop.vr
: loop around vector with overlap.LS
short for '.Last.value'.modS
: same as 'mod', symmetric to 0.mod
= x %% y, x and y with same number of elements. onebyx
= 1.0/xone
returns 1.0, same length as 'x'powr
= x^y, with 0^0 := 1, 0^y := 0, any yquotmean
Compute quotient of means of non-NA elements of x by ysafeDiv
Compute quotient, set 0/0 -> 1, and safeguard r/0 <- c3Q
otherwise signp(0)
-> 1, signp(complex)
-> NA ! solveQeq
Solve the quadratic equation a*z^2+ b*z+c
given
the coefficients a,b,c
OR c(a,b,c)
, returns always *two*
solutions; if a = b = 0
returns c(Inf, Inf)sqr
= x^2submod
analog to divmod for unequally spaced data, c(greatest index gi
of v
s.t. v < x, x - v['gi'] zero
returns 0.0, same length as 'x'
exch
: Exchanges elements 'L' and 'R': x[which( x == L )] <- R; x[which( x == R )] <- LK
: Cayley transform (z - i)/(z + i)
Km
: (1 + z)i/(1 - z)
, inverse transformation of K
norm2
: 2-norm.last
: last element of vector
LE
: = length of vector
pythag
: c(A,B,C), A=final a' = sqrt( a^2 + b^2 ) without squaring and
taking the square root, avoiding overflow and underflow, B=final b',
C=residual = final (b'/a')^2, see note. signp
: ifelse( is.na(x) | (!is.finite(x) | x>=0),1,-1 ), avoiding
NA, NaN and 0 in the result.sqrtH
: Square root with Halley's hyperbolical method.
see also examples of date
Note that 1
results with signp( 0 )
;
It is not possible to discriminate between Inf
and -Inf
, by definition in R,
but: as.character(-Inf) = "-Inf".
pythag
: The invariant of the iteration is sqrt(a^2 + b^2), iterating a':=max(a,b) and reducing b':=min(a,b).
Christian W. Hoffmann <christian@echoffmann.ch>
Moler, C. and Morrison. D, 1983 Replacing Square Roots by Pythagorean Sums, IBM J.Res.Devel., 27, 6, 577–589.
jjam(Prime numbers); Mathpath(Formula for Halley); Wikipedia(Derivation of Halley)
int (c(0,pi,2*pi,30*pi)) # 0 3 6 94 frac(c(0,pi,2*pi,30*pi)) # 0.000000 0.141593 0.283185 0.247780 frac(c(0,pi,2*pi,30*pi), 3) # 0 142 283 248 y <- c( Inf, -Inf,NA, NaN, -NaN,-1, 0, 1 ) signp(c(-1:1,NA,NaN,Inf, -Inf)) # -1 1 1 1 1 1 1 # instead of sign() = -1 0 1 NA NaN 1 -1 mod((-3:5),4 ) # 1 2 3 0 1 2 3 0 1 modS((-3:5),4) # -3 -2 -1 0 1 2 3 0 1 x <- 200; y <- x + 0.1 equalFuzzy(x,y,0.1*c(10^(-3:0))) # FALSE TRUE TRUE TRUE equalFuzzy(x,y,0.1*c(10^(-3:0)),FALSE) # FALSE FALSE FALSE TRUE loop.vp(1:4) # 1 2 3 4 1 loop.vp(matrix(1:12,nrow=3),c(2,4)) # [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 1 4 7 10 4 10 #[2,] 2 5 8 11 5 11 #[3,] 3 6 9 12 6 12 safeDiv(0:3,c(0,0:2)) # 1.552518e+231 signp(c(-1:1,NA,NaN,Inf, -Inf)) # -1 1 1 1 1 1 1 # instead of sign() = -1 0 1 NA NaN 1 -1 solveQeq(0,0,1) # NA NA solveQeq(0,1,0) # 0 solveQeq(0,1,1) # -1 solveQeq(1,0,0) # 0 0 solveQeq(1,0,1) # 0-1i 0+1i solveQeq(1,1,0) # -1 0 solveQeq(1,1,1) # -0.5-0.866025i -0.5+0.866025i solveQeq(sample(1:4,1),sample(1:4,1),sample(1:4,1)) x <- matrix(rnorm(9),3,3) s <- svd(x) lV(s$d) norm(chsvd(s) - x) # 9.4368957e-16 submod(8.1,c(10.3, 31) ) # 0.0 8.1 submod(18.1,c(10.3, 31) ) # 1.0 7.8 exch(LETTERS, "A", "Y") # "Y" "B" ... "W" "X" "A" "Z" exch(1:5, "2", "Y") # "1" "Y" "3" "4" "5" pythag(19,180) # 1.8100000e+02 3.8414499e-23
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.