The greatest common divisor of two numbers is the largest number that divides evenly into both numbers.
The method of calculating this is is to use repeated division and subtraction.
Given two numbers a and b, the first step is to repeatedly divide both numbers by 2 as long as both are even, keeping count of the number of iterations as d.
Now, we loop again while a is not equal to b. If either of the numbers are even we divide the number by 2, without incrementing d in this case. If both numbers are odd, we then replace the bigger of a or b with the difference of the two numbers divided by 2.
Eventually the two numbers will be equal and the loop exits.
Now at this point the greatest common divisor is given by gcd = a (or b) * 2d
Factorials are defined as a number that is multiplied by every positive number below it, and is denoted by an exclamation mark, ie n! = n * (n – 1) * (n – 2) … 2 * 1.
Finding the roots of a quadratic equation (which is where it crosses the x axis) can be a rather involved calculation. I remember in school having to do them, and the equations they gave usually worked out to nice numbers. Often in the real world, it is not quite so easy.
Fortunately, computers are not scared by numbers that are difficult, and can solve quadratics quite well.
The function below solves quadratics in the form of Ax2 + Bx + C = 0. Providing A, B and C, the function finds the two roots of the equation, and also determines whether the roots are real, real repeating or complex roots.
Complex numbers are a vital part of many mathematical calculations, yet Visual Studio does not natively support complex numbers, but fortunately, this is a very easy oversight to fix.
First, a little maths lesson. Imaginary numbers are numbers that do not exist, like for example the square root of -1. This number is denoted by i and is central to complex numbers. Complex numbers are defined as a number that is made up of an imaginary component as well as a real component, and is denoted by the formula x*i + y.
So some examples of complex numbers are 5i + 4, 2i + 0 (which simplifies to 2i), and 0i + 6 (which simplifies to simply 6).
What the above example shows as well, is that the range of complex numbers also includes all the real numbers.
The complex number class contains two variables to contain the real and imaginary components, namely, re and im. Most of the rest of the class are overloaded arithmetic operators.
The ^ operator takes a complex number and raises it to a specified power, returning a new complex number.
The + and - operators add and subtract two complex numbers, which entails adding or subtracting the real and imaginary parts of the one to the real and imaginary parts of the other complex number.
The * and / operators are a bit more complicated in how they are calculated, but the net result is the multiplication and division of the complex numbers.
The Abs() function returns the absolute value of the complex number, which is the square root of the sum of the squares of the real and imaginary components - very much like the absolute value of a normal vector.
Lastly, the Arg() function returns the arg of the complex number in degrees, which allows the complex number to be expressed in polar notation.
Matrices are a very useful thing in computer programming, especially in 3D graphics programming. Out of all the mathematics I learned at University, matrices are the one thing that keep rearing their head over and over.
I am not going to go into major detail over what a matrix is - wikipedia can tell you that much better than I can - so will focus mainly on how to implement one in C#.
Essentially, a matrix is a 2-dimensional array of numbers, often representing a set of equations. The number of columns and rows is flexible.
Using the Matrix class below, the constructor can either take an x and y value or an array to determine how big to make the internal array for the matrix.
The Zero() function sets all entries in the matrix to zero by looping through the entire array.
The SetIdentity() function makes the matrix an identity matrix, which means that all the values are 0, except for the values along the diagonal from top left to bottom right being set to 1.
The Transpose function transforms the columns of the matrix into the rows, and vice versa.
The Scale function multiplies each value in the matrix by a scaling factor.
The determinant of an array is found by adding all the products of the entries of each diagonal sloping in a right direction, and subtracting the products of the entries of each diagonal sloping in a opposite direction. This is done with the Determinant function
The Trace function returns the sum of the main diagonal of the matrix.
The matrix class has also got a few overloaded operators, to make handling matrices easier.
The + operator is overloaded so that when passed two matrices, each value in the one matrix is added to the equivalent value in the second matrix. Both matrices need to be the same size for this to work.
A second overload for the + operator takes a scalar value and adds it to the matrix, which just adds that value to each value in the matrix.
The - operator performs identically to the + operator, except that it subtracts instead of adds the matrices.
The** * **operator multiplies the two matrices together, which is a little more involved than addition.
Read More »
A vector is simply a 1-dimensional array of numbers that often refer to coordinates, or directions. The theory of vectors is best left to wikipedia.
Implementing a vector class in C# is pretty easy. The vector is stored as an array within the class. The constructor can either set this array directly, or initialises it to a particular size.
The Zero() function sets the entire vector to 0.
The Abs() function calculates the absolute value of the vector, which is the square root of the sum of the squares of the components of the vector.
The Dot() function returns the dot product of the vector, with another vector. The dot product is the sum of the products of each component in the vectors.
The Scale() function scales the vector by the scaling factor supplied.
The + and - operators are overloaded to make it easy to add and subtract vectors.
Each component of first vector is added (or subtracted) to the equivalent component of the second vector, and then the resulting vector is returned.
Read More »
We have already looked at several other ways of doing a least squares fit to find an quation representing a set of data. We now look at the least squares fit using full logs, which tries to match the data with the equation y = B*xM, using the least squares method.
As with the previous least squares functions, the function below returns the calculated values for M and B, and if no solution exists returns 0 for both of them.
This algorithm for the least squares fit uses a logarithmic abscissa, and tries to find an equation matching the data set with the form y = M * log(X)/log(10) + B, using the least squares method.
The function below finds M and B, and if no solution exists, it returns 0 for both these values.
We have already looked at the linear least squares fit, but that does not always produces a nice fit to the data. The least squares fit with a log ordinate tries to match up the data to the equation y = B * 10Mx using the least squares method.
In the code below, if there is no solution, the function returns M and B as 0.
When analysing data it is often useful to find an equation to show the relationship in a certain set of data. The linear least squares fit tries to do exactly that.
Given a set of data points, it tries to find a straight line with the equation y = Mx + B which best fits the data, using the least squares technique, which works out the squares of the deviations of each point and then tries to minimize those.