| Title: | Miscellaneous Mathematical Tools |
|---|---|
| Description: | Some basic math calculators for finding angles for triangles and for finding the greatest common divisor of two numbers and so on. |
| Authors: | W.J. Braun [aut, cre] |
| Maintainer: | W.J. Braun <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.1 |
| Built: | 2026-05-16 05:31:25 UTC |
| Source: | https://github.com/cran/MiscMath |
A random assortment of elementary mathematical formulas and calculators.
# Find the greatest common divisor of 57, 93 and 117. gcd(c(57, 93, 117))# Find the greatest common divisor of 57, 93 and 117. gcd(c(57, 93, 117))
Convert a given decimal constant in the interval (0, 1) to the corresponding binary representation.
DecToBin(x, m = 32, format = "character")DecToBin(x, m = 32, format = "character")
x |
a numeric vector of values in the interval (0, 1) |
m |
a numeric constant specifying the number of binary digits to use in the output |
format |
a character string constant specifying the form of the output |
Default format is as a character string. Alternatives are plain which prints
results to the device, and vector which output a binary vector.
a vector containing the binary representation
x <- c(.81, .57, .333) DecToBin(x) # character output DecToBin(x, format="vector") # binary vector output DecToBin(x, format="plain")x <- c(.81, .57, .333) DecToBin(x) # character output DecToBin(x, format="vector") # binary vector output DecToBin(x, format="plain")
The sieve of Eratosthenes is an ancient method for listing all
prime numbers up to a given value n.
EratosthenesSieve(n)EratosthenesSieve(n)
n |
a numeric vector consisting of a single positive integer. |
The algorithm scans through the vector from 2 through n, eliminating all multiples of 2, then eliminating all multiples of the next smallest integer (3), and so on, until only the prime numbers less than n remain.
a numeric vector containing all primes less than n.
EratosthenesSieve(100)EratosthenesSieve(100)
Greatest common divisor or factor for all elements of a positive-integer-valued vector.
gcd(x)gcd(x)
x |
a numeric vector consisting of at least two positive integer values. |
The gcd is calculated using the Euclidean algorithm applied to successive pairs of the elements of x.
a numeric constant containing the greatest common divisor.
x <- c(81, 57, 333) gcd(x)x <- c(81, 57, 333) gcd(x)
Convert positive integers to their corresponding binary representation.
IntDecToBin(x, m = 31)IntDecToBin(x, m = 31)
x |
an integer vector |
m |
a numeric constant specifying the number of binary digits to use in the output |
a matrix containing the binary representations
x <- c(81, 57, 333) IntDecToBin(as.integer(x))x <- c(81, 57, 333) IntDecToBin(as.integer(x))
Use of the ancient law of cosines to determine the angle between two sides of a triangle, given lengths of all three sides. This is a generalization of Pythagoras' Theorem.
LawofCosines(sides)LawofCosines(sides)
sides |
a numeric vector of length 3, containing the side lengths. |
a numeric constant giving the angle in between the sides corresponding to the
first two components in sides. Result is expressed in degrees.
LawofCosines(c(3, 4, 5))LawofCosines(c(3, 4, 5))
Use of the ancient law of sines to determine the angle opposite one side of a triangle, given the length of the opposite side as well as the angle opposite another side whose length is also known. Alternatively, one can find the length of the side opposite a given angle.
LawofSines(sides, angles, findAngle)LawofSines(sides, angles, findAngle)
sides |
a numeric vector of length 1 or 2, containing the side lengths. |
angles |
a numeric vector of length 1 or 2, containing the angles (in degrees). |
findAngle |
a logical constant |
If findAngle is TRUE, sides should be of length 2 and the
function will compute angle opposite the side with length given by the second element of
sides. Otherwise, angles should be of length 2, and the function will
calculate the length of the side opposite the angle corresponding to the second
element of angles.
a numeric constant giving the angle opposite the given side, if findAngle is
TRUE, or giving the length of the side opposite the given angle, if findAngle is
FALSE.
LawofSines(c(3, 4), 50) # find angle opposite the side of length 4. LawofSines(3, c(70, 50), findAngle = FALSE) # find length of side opposite the 50 degree angleLawofSines(c(3, 4), 50) # find angle opposite the side of length 4. LawofSines(3, c(70, 50), findAngle = FALSE) # find length of side opposite the 50 degree angle
Implementation of efficient algorithm to compute the pth power of a matrix.
Matpower(X, p)Matpower(X, p)
X |
numeric square matrix |
p |
integer: nonnegative exponent. |
a matrix containing the pth power of X.
Golub and Van Loan (1983) Matrix Computations. Algorithm 11.2-1. p. 393.
Calculate the maximum run length of of 0's in a binary vector.
MaxRunLength(x)MaxRunLength(x)
x |
a binary vector |
the maximum run length of 0's
x <- c(0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L) MaxRunLength(x) # should be 2 MaxRunLength(1L - x) # should be 3 # Test of Mersenne Twishter RNGkind("Mers") # ensure that default generator is in use N <- 10000 x <- runif(N) y <- DecToBin(x, format = "vector", m = 40) MaxHeadRunsM <- apply(y, 1, MaxRunLength) # 0 run lengths (Heads) MaxTailRunsM <- apply(1-y, 1, MaxRunLength) # 1 run lengths (Tails) # distributions of Max 0 run lengths and Max 1 run lengths should match boxplot(MaxHeadRunsM, MaxTailRunsM, axes=FALSE, main="Maximum Run Length") axis(side=1, at=c(1, 2), label=c("Heads", "Tails")) axis(2) box() # Comparison with Wichmann-Hill Generator RNGkind("Wich") x <- runif(N) y <- DecToBin(x, format = "vector", m = 40) MaxHeadRunsW <- apply(y, 1, MaxRunLength) MaxTailRunsW <- apply(1-y, 1, MaxRunLength) boxplot(MaxHeadRunsW, MaxTailRunsW, axes=FALSE, main="Maximum Run Length") axis(side=1, at=c(1, 2), label=c("Heads", "Tails")) axis(2) box() RNGkind("Mers") # restore default generatorx <- c(0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L) MaxRunLength(x) # should be 2 MaxRunLength(1L - x) # should be 3 # Test of Mersenne Twishter RNGkind("Mers") # ensure that default generator is in use N <- 10000 x <- runif(N) y <- DecToBin(x, format = "vector", m = 40) MaxHeadRunsM <- apply(y, 1, MaxRunLength) # 0 run lengths (Heads) MaxTailRunsM <- apply(1-y, 1, MaxRunLength) # 1 run lengths (Tails) # distributions of Max 0 run lengths and Max 1 run lengths should match boxplot(MaxHeadRunsM, MaxTailRunsM, axes=FALSE, main="Maximum Run Length") axis(side=1, at=c(1, 2), label=c("Heads", "Tails")) axis(2) box() # Comparison with Wichmann-Hill Generator RNGkind("Wich") x <- runif(N) y <- DecToBin(x, format = "vector", m = 40) MaxHeadRunsW <- apply(y, 1, MaxRunLength) MaxTailRunsW <- apply(1-y, 1, MaxRunLength) boxplot(MaxHeadRunsW, MaxTailRunsW, axes=FALSE, main="Maximum Run Length") axis(side=1, at=c(1, 2), label=c("Heads", "Tails")) axis(2) box() RNGkind("Mers") # restore default generator
Infers the modulus m for a congruential random number generator.
mCracker(U, par = 1e6, maxit = 100)mCracker(U, par = 1e6, maxit = 100)
U |
a numeric vector consisting of n (say 10000) uniform(0,1) pseudorandom numbers of the form x1/m, x2/m, ..., xn/m. |
par |
an integer guess as to an upper bound on the smallest integer in the sequence x1, x2, ..., xn. |
maxit |
maximum number of iterations allowed. |
Basic idea: Let x(1) denote the minimum order statistic in x1, x2, ..., xn. Then the set (x1/m)/(x(1)/m)*(1:par) must contain at least one integer, and m is in that set, if par has been set correctly.
a list consisting of
m |
the integer value of m |
firstInteger |
the minimum order statistic of the set x1, x2, ..., xm |
# set.seed(33663) x <- runif(1000000) Y <- mCracker(x)$m log(Y, 2) # should be 32# set.seed(33663) x <- runif(1000000) Y <- mCracker(x)$m log(Y, 2) # should be 32
Simple linear regression estimators for slope, intercept and noise standard deviation with absolute value penalty on slope.
microLASSO(x, y, lambda)microLASSO(x, y, lambda)
x |
a numeric vector of covariate values |
y |
a numeric vector of response values |
lambda |
a numeric constant which should be nonnegative |
a list consisting of
Coefficients |
a numeric vector containing intercept and slope estimates |
RMSE |
a numeric constant containing the (penalized) maximum likelihood estimate of the noise standard deviation |
x <- runif(30) y <- x + rnorm(30) microLASSO(x, y, lambda = 0.5)x <- runif(30) y <- x + rnorm(30) microLASSO(x, y, lambda = 0.5)
Efficient method for generating discrete random variates from any distribution with a finite number (N) of states. The method is described in detail in Section 10.1 of the given reference.
rAlias(n, P)rAlias(n, P)
n |
numeric, constant number of variates to be simulated |
P |
numeric, probability mass function, assuming states from 1 through N |
numeric vector of containing n simulated values from the given discrete distribution
S. Ross (1990) A Course in Simulation, MacMillan.
x <- rAlias(n = 100, P = c(1:5)/15) table(x)/100x <- rAlias(n = 100, P = c(1:5)/15) table(x)/100
Basic implementation of the linear congruential random number generator.
rlincong(n, seed, par)rlincong(n, seed, par)
n |
numeric: number of variates to generate. |
seed |
numeric: initial seed. |
par |
an integer vector containing parameters for the generator: a, cc, m. |
a vector of n uniform random numbers
x <- rlincong(1000, 6976, c(171, 0, 30269)) summary(x)x <- rlincong(1000, 6976, c(171, 0, 30269)) summary(x)
Basic implementation of the multiply-with-carry generator.
rMWC(n, par)rMWC(n, par)
n |
numeric: number of variates to generate. |
par |
an integer vector containing parameters for the generator: X, C, A, B. |
a vector of n uniform random numbers
Marsaglia, G. (2003) Random Number Generators. Journal of Modern Applied Statistical Methods. 2(1):2.
x <- rMWC(58, c(5, 3, 6, 10)) summary(x)x <- rMWC(58, c(5, 3, 6, 10)) summary(x)
Given a sequence of pseudorandom numbers, this function constructs a random forest prediction model for successive values, based on previous values up to a given lag. The ability of the random forest model to predict future values is inversely related to the quality of the sequence as an approximation to locally random numbers.
RNGtest(u, m=5)RNGtest(u, m=5)
u |
numeric, a vector of pseudorandom numbers to test |
m |
numeric, number of lags to test |
Side effect is a two way layout of graphs showing effectiveness of prediction on a training and a testing subset of data. Good predictions indicate a poor quality sequence.
W. John Braun
x <- runif(200) RNGtest(x, m = 4)x <- runif(200) RNGtest(x, m = 4)
Basic vectorized implementation of the linear congruential generator for simulating uniform random numbers on the interval (0, 1).
rngV(n, seed, par)rngV(n, seed, par)
n |
numeric: number of variates to generate. |
seed |
numeric: initial seed. |
par |
an integer vector containing parameters for the generator: a, cc, m, L. |
a vector of n uniform random numbers
Anderson, S.L. (1990) Random number generators on vector supercomputers and other advanced architectures. SIAM Review, 32(2):221-251.
x <- rngV(1000, 6976, c(171, 0, 30269, 10)) summary(x)x <- rngV(1000, 6976, c(171, 0, 30269, 10)) summary(x)
Implementation of a simple shuffling algorithm that can be used to randomly permute a given set of simulated random numbers.
shuffle(n, k = 100, x = runif(n))shuffle(n, k = 100, x = runif(n))
n |
numeric: number of variates to be output. |
k |
numeric: a tuning parameter for the shuffler. |
x |
a vector containing a sequence to be randomly permuted with the shuffler. |
a numeric vector