Title: | Generating 'Sweave' Code for 'R/exams' Questions in Mathematics |
---|---|
Description: | When using the R package 'exams' to write mathematics questions in 'Sweave' files, the output of a lot of R functions need to be adjusted for display in mathematical formulas. Specifically, the functions were accumulated when writing questions for the topics of the mathematics courses College Algebra, Precalculus, Calculus, Differential Equations, Introduction to Probability, and Linear Algebra. The output of the developed functions can be used in 'Sweave' files. |
Authors: | Qingwen Hu [aut, cre, cph] |
Maintainer: | Qingwen Hu <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.2 |
Built: | 2024-11-20 06:50:32 UTC |
Source: | CRAN |
The function 'polynomial' in the 'polynom' package converts a sequence of rational numbers into a polynomial with decimal coefficients. This function 'c2p' converts the output of 'polynomial' into the TeX form of a polynomial where coefficients are of vertical fraction form using the package 'fractional'.
c2p(m)
c2p(m)
m |
a list of rational numbers which are coefficients of a polynomial in descending order. |
The function uses 'polynomial' function from the package 'polynom' which defaults the polynomial in ascending order.
The function returns a string of TeX code of the polynomial with rational coefficients.
m <- sample(c(1:100),5) m c2p(m)
m <- sample(c(1:100),5) m c2p(m)
Convert a sequence of rational numbers into a string separated with a comma where the fractions are in backslash form.
c2str(x)
c2str(x)
x |
a list of rational numbers. |
The output string was originally designed for 'string' type answers of 'R/exams' when a sequence of rational numbers are the answers from multiple parts of the question.
The output is a string of rational numbers with backslash denoting division.
x <- sample(c(1:100),5)/100 x c2str(x)
x <- sample(c(1:100),5)/100 x c2str(x)
Convert a sequence of numbers into a string of TeX code for the sequence with plus minus signs for each number of the sequence, where the fractions are in vertical form.
c2strpm(x)
c2strpm(x)
x |
a list of rational numbers. |
The output is a string of rational numbers with backslash denoting division and plus minus signs in front of each number.
x <- sample(c(1:100),5) x c2strpm(x)
x <- sample(c(1:100),5) x c2strpm(x)
The output of cm2l is a vector of strings with length equal to the column size of the input matrix. The i-th entry is the string of the numbers from the i-th column of the matrix. For example, the 2 by 2 identity matrix is converted into c("1,0", "0,1").
cm2l(matrix)
cm2l(matrix)
matrix |
a matrix. |
a vector of the strings of the columns of x. Each entry is a string of the column.
a <- matrix(sample(c((-10):10),12),nrow =3,byrow=TRUE) cm2l(a)
a <- matrix(sample(c((-10):10),12),nrow =3,byrow=TRUE) cm2l(a)
Convert a cosine value into TeX code with simplification on the radical from the hypotenuse, where the input (a,b) are the integer lengths of the legs of the right triangle with a the vertical leg, b the horizontal leg. The simplification is provided by another function 'simpRad' in the same package.
costex(a,b)
costex(a,b)
a |
The vertical length of the right triangle, which can be negative. |
b |
The horizontal length of the right triangle, which can be negative. |
Given integer lengths of a the legs of a triangle, the function returns a string of tex code for the value of the associated cosine.
The function returns a string of TeX code for the value of the associated cosine.
Caution: Integer coordinates (x,y) in the plane should switch order to be the input (y,x) of the function.
a <- sample(c(1:5),1) b <- sample(c(1:5),1) costex(a,b)
a <- sample(c(1:5),1) b <- sample(c(1:5),1) costex(a,b)
Display the cycle notation of the permutation using the output matrix of the function permucycle().
cycledisplay(x)
cycledisplay(x)
x |
output matrix of the function 'permucycle'. |
A cycle notation of a permutation.
cycledisplay(permucycle(c(3,2,1))) paste0(cycledisplay(permucycle(c(3,2,1))),collapse ="")
cycledisplay(permucycle(c(3,2,1))) paste0(cycledisplay(permucycle(c(3,2,1))),collapse ="")
The output remainder of polydiv(x,y)[2] from the package 'pracma' may contain a zero in the first place which is not needed for text presentation. The function will modify the output by deleting the first zero, if any.
delzero(x)
delzero(x)
x |
a list of numbers whose first entry may be zero or close to zero with absolute value less than or equal to 1e-10. |
The function truncates the first zero entry of the list.
x <- c(0,sample(c((-10):10),5)) delzero(x)
x <- c(0,sample(c((-10):10),5)) delzero(x)
Convert a decimal number into exactly 4 decimal places without scientific notation.
fmt4(x)
fmt4(x)
x |
a decimal number. |
Round a decimal number into exactly 4 decimal places without scientific notation.
A decimal number rounded into exactly 4 decimal places.
x <- sin (sample(c(1:5),1)) fmt4(x)
x <- sin (sample(c(1:5),1)) fmt4(x)
Convert a decimal number into exactly n decimal places without scientific notation.
fmtN(x,n)
fmtN(x,n)
x , n
|
where x is a decimal number and n is the numbers of decimal places to keep. |
Round a decimal number into exactly n decimal places.
A decimal number rounded into exactly n decimal places.
fmt4
is a special case of fmtN but is simpler to use with one argument.
x <- sin (sample(c(1:5),1)) n <- sample(c(4:10),1) fmtN(x,n)
x <- sin (sample(c(1:5),1)) n <- sample(c(4:10),1) fmtN(x,n)
Apply the Gram-Schmidt process to convert the group of 3 vectors (x,y,z) into an orthogonal group (u,v,w) without normalizing to unit vectors. The output is a matrix with columns (u,v,w).
G3S(x,y,z)
G3S(x,y,z)
x , y , z
|
a group of 3 vectors (x,y,z) |
Need the one dimensional projection function 'myGS' from the same package.
The output is a matrix with columns (u,v,w) which are an orthogonal set of vectors.
G3S(c(1,2,3),c(3,2,1),c(4,5,9)) fractional(G3S(c(1,2,3),c(3,2,1),c(4,5,9)))
G3S(c(1,2,3),c(3,2,1),c(4,5,9)) fractional(G3S(c(1,2,3),c(3,2,1),c(4,5,9)))
Given the lengths of the integer legs of a right triangle, the function generates the TeX code of the length of the hypotenuse in simplified form.
hypotex(a,b)
hypotex(a,b)
a , b
|
a pair of the integer leg lengths of a right triangle. |
The function generates the TeX code of the hypotenuse in simplified form of the radicals.
a <- sample(c(1:5),1) b <- sample(c(1:5),1) hypotex(a,b)
a <- sample(c(1:5),1) b <- sample(c(1:5),1) hypotex(a,b)
inversions() counts the number of inversions of a permutation.
inversions(x)
inversions(x)
x |
a permutation of 1, 2, ..., n. |
Input must be a list of numbers.
The total number of inversions in a list.
inversions(c(3,1,2))
inversions(c(3,1,2))
inversionv() returns the vector of inversions for each entry in the permutation. For example, inversionv(1,2,0) = (1,1,0), inversionv(0,1,2) = (0,0,0).
inversionv(x)
inversionv(x)
x |
a list of numbers, or a permutation. |
inversionv returns a vector of inversions for each member of the permutation.
inversionv(c(3,1,2,4))
inversionv(c(3,1,2,4))
m22l converts a matrix into a matrix without brackets or parentheses around the array of numbers.
m22l(matrix)
m22l(matrix)
matrix |
a matrix. |
array a numbers without parentheses.
a <- matrix(sample(c(-10:10),12),nrow =3,byrow=TRUE) m22l(a)
a <- matrix(sample(c(-10:10),12),nrow =3,byrow=TRUE) m22l(a)
m2l converts a matrix into TeX code with square brackets.
m2l(matrix)
m2l(matrix)
matrix |
a matrix |
TeX code of the matrix in 'Sweave' file.
a <- matrix(sample(c(-10:10),12),nrow =3,byrow=TRUE) m2l(a)
a <- matrix(sample(c(-10:10),12),nrow =3,byrow=TRUE) m2l(a)
The function converts multiple decimals into tex code of vertical fractions.
mfrac(z)
mfrac(z)
z |
a list of decimal numbers. |
The function returns a string of tex code for the numbers in vertical fraction form.
The function used function 'rfrac' in the same package which depends on the package 'fractional'.
z <- sample(c(1:55),6)/100 mfrac(z)
z <- sample(c(1:55),6)/100 mfrac(z)
Orthogonal projection of y onto x.
myGS(x,y)
myGS(x,y)
x , y
|
a pairs of vectors. |
myGS returns a vector of the projection of y onto x.
x <- sample(c((-10):10),3) y <- sample(c((-10):10),3) myGS(x,y) fractional(myGS(x,y))
x <- sample(c((-10):10),3) y <- sample(c((-10):10),3) myGS(x,y) fractional(myGS(x,y))
perm(v) lists permutations of a vector v with distinct entries. The output is a matrix with each row a permutation. It cannot distinguish identical permutations.
perm(v)
perm(v)
v |
is a vector with distinct entries. |
an n! by n matrix with each row a permuation of the enties of v.
permucycle
, permuorder
,cycledisplay
x <- sample(c((-10):10),3) perm(x)
x <- sample(c((-10):10),3) perm(x)
permucycle returns a matrix containing information of a permutation. See the Value section for details.
permucycle(x)
permucycle(x)
x |
a permutation |
Given a permutation x of the numbers 1, 2, ..., n, and i with value from 1 to n. permucycle() returns a (n+1) by (n+1) matrix A with
A[1 , 1]
|
the total number of cycles |
A[(i+1) , 1]
|
the length of the i-th cycle |
A[(i+1) , 2:(n+1)]
|
the members of the i-th cycle |
For example, permucycle(c(3,2,1)) will produce the following matrix:
[,1] [,2] [,3] [,4]
[1,] 2 0 0 0
[2,] 2 1 3 0
[3,] 1 2 0 0
[4,] 0 0 0 0
The 2 in the first row means there are two cycles; The second row means there is a cycle of length 2, with members (1,3); The third row means there is a cycle of length 1, with member (2); The fourth row is redundant for this specific case. One can read from the output of permucycle() to obtain cycle notation (13)(2) of the permutation, and other information.
permucycle(c(3,2,1))
permucycle(c(3,2,1))
Computing the order of the permutation x of the numbers 1, 2, ..., n, using the first column of the output matrix of the function 'permucycle', whose first entry N = permucycle()[1,1] is the total number of the cycles in the permutation, and permucycle()[2:(n+1),1] are the lengths of each cycle. Note: Since matrix can be regarded as a 1 dimensional vector with each column attached with the previous one, the argument of permuorder() can be the whole output of permucycle() when permuorder() just uses the first N+1 entries in the first column. Certainly specifying the first column of permucycle() to be the input will save some memory usage.
permuorder(x)
permuorder(x)
x |
output matrix of the function permucycle() |
The order of the permutation, which is the least common multiple of the orders of each contained cycle.
permuorder(permucycle(c(3,1,2)))
permuorder(permucycle(c(3,1,2)))
Produce the TeX code of positivity of randomaized values.
pos(x)
pos(x)
x |
a numeric number. |
The function returns one of the symbols "<0", ">0", or "=0".
x <- sample(c((-10):10),1) pos(x)
x <- sample(c((-10):10),1) pos(x)
The output of rcm2l is a vector of strings with length equal to the row size of the input matrix. The i-th entry is the string of the numbers from the i-th row of the matrix. For example, the standard 2 by 2 Jordan block with 2 in the main diagonal is converted into c("2,1", "0,2").
rcm2l(matrix)
rcm2l(matrix)
matrix |
a matrix |
a vector of strings with length equal to the row size of the input matrix. The i-th entry is the string of the numbers from the i-th row of the matrix.
a <- matrix(sample(c(-10:10),12),nrow =3,byrow=TRUE) rcm2l(a)
a <- matrix(sample(c(-10:10),12),nrow =3,byrow=TRUE) rcm2l(a)
The function converts a decimal into TeX code of vertical fractions, using functions 'denominators' and 'numerators' from the package 'fractional'.
rfrac(x)
rfrac(x)
x |
a list of decimal numbers. |
The function returns a string of TeX code for the number in vertical fraction form.
The function depends on the package 'fractional'.
x <- sample(c(1:55),1)/100 rfrac(x)
x <- sample(c(1:55),1)/100 rfrac(x)
The function converts a decimal into TeX code of vertical fractions, using functions 'denominators' and 'numerators' from the packaege 'fractional'. This function differs from 'rfrac' only in the place of the negative signs. 'rfracF' returns the fraction with the sign in front.
rfracF(x)
rfracF(x)
x |
a list of decimal numbers. |
The function returns a string of tex code for the number in vertical fraction form with the sign in front of the fraction.
The function depends on the package 'fractional'.
x <- sample(c(1:55),1)/100 rfrac(x)
x <- sample(c(1:55),1)/100 rfrac(x)
rm2l converts a matrix into its transpose in TeX code.
rm2l(matrix)
rm2l(matrix)
matrix |
a matrix. |
It return the transpose of the input matrix in latex code.
a <- matrix(sample(c((-10):10),12),nrow =3,byrow=TRUE) rm2l(a)
a <- matrix(sample(c((-10):10),12),nrow =3,byrow=TRUE) rm2l(a)
The minus sign in fractions should be in front of the fraction for display purpose, while many numerical algorithms may produce fractions with minus sign in the numerator. This function modifies the output of those packages into the correct display form in TeX code.
signF(a)
signF(a)
a |
a numeric number. |
The function returns one of the symbols "-" for negative numbers, or empty "" for nonnegative ones.
a <- sample(c((-10):10),1)/100 signF(a)
a <- sample(c((-10):10),1)/100 signF(a)
SimpRad uses the package 'numbers' to simplify square roots of positive integers.
simpRad(n)
simpRad(n)
n |
a positive integer. |
The function returns a string of TeX code for radical in simplified form.
The function used functions 'primFactors' and 'radical' from the package of 'numbers'.
radical, primeFactors
n <- sample(c(4:100),1) simpRad(n)
n <- sample(c(4:100),1) simpRad(n)
Convert a sine value into TeX code with simplification on the radical from the hypotenuse, where the input (a,b) are the integer lengths of the legs of the right triangle with a the vertical leg, b the horizontal leg. The simplification is provided by another function 'simpRad' in the same package.
sintex(a,b)
sintex(a,b)
a |
The vertical length of the right triangle, which can be negative. |
b |
The horizontal length of the right triangle, which can be negative. |
Given integer lengths of the legs of a triangle, the function returns a string of TeX code for the value of the associated sine.
The function returns a string of TeX code for the value of the associated sine.
Caution: Integer coordinates (x,y) in the plane should switch order to be the input (y,x) of the function.
a <- sample(c(1:5),1) b <- sample(c(1:5),1) sintex(a,b)
a <- sample(c(1:5),1) b <- sample(c(1:5),1) sintex(a,b)
The function converts multiple decimals into TeX code of back slash fractions.
smfrac(z)
smfrac(z)
z |
a list of decimal numbers. |
The function returns a string of TeX code for the numbers in back slash fraction form.
The function used function 'rfrac' in the same package which depends on the package 'fractional'.
z <- sample(c(1:55),6)/100 smfrac(z)
z <- sample(c(1:55),6)/100 smfrac(z)