R Programming/Mathematics

The inner product
The inner product is also called the dot product or the scalar product. It is the sum of the item-by-item product.

The outer product
The outer product is also called the cross product or the vector product. It is a matrix resulting from the product of the elements of the two vectors.

Matrix Algebra
If you want to create a new matrix, one way is to use the matrix function. You have to enter a vector of data, the number of rows and/or columns and finally you can specify if you want R to read your vector by row or by column (the default option) with byrow. You can also combine vectors using cbind or rbind. The dimension of a matrix can be obtained using the dim function or alternatively nrow and ncol.

Some special matrix
The identity matrix has ones on the diagonal and zeros outside the diagonal.
 * eye (matlab)
 * <tt>diag(1,nrow=10,ncol=10)</tt>
 * <tt>diag(rep(1,10))</tt>

J matrix is full of ones
 * <tt>ones</tt> (matlab)

A matrix full of zeros
 * <tt>zeros</tt> (matlab)

Diagonal matrix

Upper triangular

Lower triangular

Same as upper triangular but using lower.tri instead


 * create an Hilbert matrix using <tt>hilbert</tt> (fUtilities).

Matrix calculations

 * compute a matrix multiplication <tt>X%*%Y</tt>.


 * compute the using <tt>%x%</tt> or <tt>kron</tt> (fUtilities).

Matrix transposition

 * Transpose the matrix

The trace and determinant of a matrix

 * compute the trace of a matrix using <tt>tr</tt> (fUtilities)
 * returns the rank of a matrix using <tt>rk</tt> (fBasics:)

Matrix inversion

 * Invert a matrix using <tt>solve</tt> or <tt>inv</tt> (fUtilities). We can also compute the generalized inverse using <tt>ginv</tt> in the MASS package.

Eigenvalue, eigenvector and eigenspace

 * Eigenvalues and eigenvectors

Misc

 * compute the norm of a matrix using <tt>norm</tt> (fUtilities).
 * check if a matrix is positive definite <tt>isPositiveDefinite</tt> (fUtilities).
 * make a matrix positive definite <tt>makePositiveDefinite</tt> (fUtilities).


 * computes row statistics and column statistics (fUtilities).
 * extract the upper and the lower part of a matrix <tt>triang</tt> and <tt>Triang</tt> (fUtilities).
 * See also the matrix, matlab, matrixcalc, matrixStats packages.

Logarithm and Exponents
We have the power function <tt>10^3</tt> or <tt> 10**3 </tt>, the logarithm and the exponential <tt>log(2.71)</tt>, <tt>log10(10)</tt>,<tt>exp(1)</tt>.

Polynomial equations
To solve $$ a x^k + bx^{k-1} +\cdots+ n=0$$, where $$a,b,\dots,n$$ are given numbers, use the command

So, for example, to calculate the roots of the equation $$ 2x^2 - 5x - 3 = 0$$ one would do as follows:

and the solution can be read to be $$x_1 = -0.5 \land x_2 = 3$$.

See also polynom and multipol packages

Symbolic calculations
R can give the derivative of an expression. You need to convert your function as an expression using the <tt>expression</tt> function. Otherwise you get an error message.

Here are some examples :

Numerical approximation

 * numDeriv package

Integration
R can perform one dimensional integration. For example we can integrate over the density of the normal distribution between $$-\infty$$ and $$+\infty$$

see the adapt package for multivariate integration.


 * See also <tt>integrate.gh</tt> in the ecoreg package.

Probability

 * The number of combination of length k within n numbers : $$C_n^k$$


 * Union and intersection

The factorial function
<tt>factorial</tt> returns the factorial of an integer. This can also be computed using the <tt>prod</tt> (product) applied to the vector of integers between 1 and the number of interest.

Note that by convention $$0!=1$$. <tt>factorial</tt> returns 1 in 0. This is not the case with the <tt>prod</tt> functions.

Factorial numbers can be very large and cannot be computed for high values.

The modulo function and euclidian division

 * Modulo and integer division (i.e. euclidean division)

Note: R is affected by the problem with non integer numbers and euclidian divisions.

Geometry

 * <tt>pi</tt> the constant
 * <tt>cos</tt>, <tt>sin</tt>, <tt>tan</tt> the trigonometric functions.

Symbolic calculus
rSymPy (rsympy) provides sympy (link) functions in R.

If you want to do more symbolic calculus, see Maxima, SAGE , Mathematica