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.0 |
Built: | 2025-02-21 05:29:53 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")
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)
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 angle
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 angle
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 generator
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 generator
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)/100
x <- rAlias(n = 100, P = c(1:5)/15) table(x)/100
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)