Mathematics

Note: This page will probably be pretty sparse for awhile. I just needed to use the mod operator and decided to make this page.

How to Learn Math

In most school’s math is taught wrong but that’s just my two cents.  I’ve only had a couple of really good math teachers but the bulk of what I learned about linear algebra came from coding. Anyone can start picking up some high level mathematics quickly if you go about in the right way.

One thing you should do is spend some time on Wikipedia and start reading a few entries on subjects that interest you.  These articles entries are generally well written and have pictures or animated gifs that make seeing the logic easier.  Also, an added benefit of this approach is that they have links and references to other concepts. You can quickly get an understanding what fields are contained in the realm of math.

If you want learn from an actual mathimatics teacher head over to Harvard’s OpenCourseWare classes.  Harvard makes good majority of their classes publicly available for anyone to learn.  I’ve picked up a lot of random awesome stuff there so I’m a little bias when it comes to OCW.  This link will take you to the list of Harvard’s classes you can work through.

http://ocw.mit.edu/courses/#mathematics

Modulo Operation and Arithmetic

The mudulo operation returns the remainder of two numbers.  I’m passing the buck on explaining this one but wikipedia excels in this area.

This snippet was taken from the Modulo operation wiki page and does a great job on showing how to test if a number is odd or even.  If the variable ‘n’ is an even number the snippet of n ‘%’ (mod) 2 will return 0.  The ‘!’ mark stands for not and if a non zero remainder will return true.

bool is_odd(int n) {
 return n % 2 != 0;
 }

You can use the modular division in base conversions and hashing. I cover a dumb way to do a hash later in this page but here is a python script I wrote that converts a denary number to a binary number using list comprehension and the mod operator. This isn’t the most elegant way to write this code but you should get to picture if you have a good understanding of iteration.

class denary():
    def __init__(self, num):
        self.num = num

    def sub(self, x):
        self.num -= x
        return 1

def toBinaryList(x):
    den = denary(x)
    return( [den.sub(y) if (den.num % y != den.num)  else 0 for y in [pow(2,abs(z)) for z in range(-7,1)] ] )

http://en.wikipedia.org/wiki/Modulo_operation

http://en.wikipedia.org/wiki/Modular_arithmetic

Base Conversions

Understanding numerical bases are super easy if you spend some time working on it. Here is a quick break down of the commonly used bases.

Ternary30,1,2

Base Name Base Number Base Characters
Binary 2 0,1
Octal 8 0,1,2,3,4,5,6,7
Denary 10 0,2,3,4,5,6,7,8,9
Hexadecimal 16 0,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Base 64 64 A–Z, a–z, 0–9, +, /

A quick note on base 64, it contains all upper and lower case English letters, denary digits, along with the addition of two extra characters. Normally the 63rd character is “+”, and the 64th character is “/”.

Lets take a quick look at conversions between denary and binary. If you’re doing this in your head or one paper, first break down the available base digits. In binary cases, each base digit is raised to the 2^n power. Here are the first four base digits of the binary numbering system.

4th Base Digit 3rd Base Digit 2nd Base Digit 1st Base Digit
2^3 2^2 2^1 2^0
8 4 2 1

So to take the denary number 10, and to covert it into binary all we have to do now is subtract each base digit where the denary number is the greater than or equal to the base digit. Here is the process broken down in steps.

Is 10 >= 8 ? Yes, 10 – 8 = 2
Is 2 >= 4 ? No, denary number remains 2
Is 2 >= 2 ? Yes 2 – 2 = 0
Is 0 >= 1 ? No, denary number remains 0

That’s all there is too it. We went from the denary number 10 to the binary number 1010. Likewise the process can be reversed to convert a binary number to a denary number. Lets convert 0101 to binary. Since we used subtraction last time, naturally we should use addition to reverse the process. If you already have the base digits broken down, the only thing you have to do then is add each base digit together where a ‘1’ resides. In this case we add together 4 and 1 together to make a denary number 5.

Another commonly used base number is hexadecimal. It’s easy to convert between binary and hexadecimal numbers. There are only 16 characters in the hexadecimal numbering system, and the max value four binary base digits can contain is 16. Converting between hexadecimal and binary numbers is the exact same as converting between denary to binary. The hexadecimal number of A is the denary number of 10. Just solve in the same manner as you would for 10 and you have your binary number but what if you have a byte can you still solve this using the same conversion method? Yep, a byte is 8 binary number string together and all you have to do is break in to nibbles( A nibble is 4 binary numbers). Lets solve for the hexadecimal number FA so you can see how this works. First figure out the denary representation of each hex digit. In this case, F is 15 and A is 10. Next just convert F to binary and then A to binary. Since we already solved for A, I’m only going to solve for F.

Is 15 >= 8 ? Yes, 15 – 8 = 2
Is 7 >= 4 ? Yes, 7 – 4 = 3
Is 3 >= 2 ? Yes 3 – 2 = 1
Is 1 >= 1 ? Yes, 1 – 1 = 0

Alright the binary number for F is 1111 and the binary number for A is 1010. So how do we solve for FA, well we just concatenate both binary numbers together to 1111 1010. Likewise you can now convert 1111 1010 from binary to denary if you want to figure out what is the actual number. First let’s figure out each base digit for a full byte, 8 binary numbers.

2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1

Now that we have a table to work from lets start the conversion. The base digits that have an active value is 128,64,32,16,8, and 2. If we add these number together we get the denary number 250.

Matrices

Wiki Description:

In mathematics, a matrix (plural matrices, or less commonly matrixes) is a rectangular array of numbers, symbols, or expressions. The individual items in a matrix are called its elements or entries.

Single Dimension Matrix:

You can make a single demsional matrix with an array.  The output of this array might look like this:

[0,1, 2]


int[] arr = new int[3]

Two Dimension Matrix – Jagged Array, Arrays of Lists
*Note I’m not sure if this is actually called a jagged array, just what I think I remember hearing it called.

An list array can make a two dimensional matrix. The output of the list array can look something like this:
[ 0, 0, 0
0, 0, 0
0, 0, 0 ]

List[] arr = List[3]

Two Dimension Matrix – Linear Array
Since an array is just a collection of memory address you can produce a two dimensional matrix using a single array.

int MAX_ROW = 3;
int MAX_COL = 3;
int[] arr = int[MAX_ROW * MAX_COL];
// ToDo: initialize array with values

// This is how to seeking a given index in an array, in this case we want to find the value of the element in row 0, column 1.
int row = 0;
int col = 1;

int val = arr[( row * MAX_COL + col )];

http://en.wikipedia.org/wiki/Matrix_(mathematics)

Hashing

Hashing is just shorting a given input. Here is a really dumb way to make a hashing function in python. If the number 123 is entered into this function, the number 23 will be returned. Likewise if the number 321 is entered, we would get 21. As you can see there is plenty of possible colliusions. Both 123 and 223 will result return with the number 23. If you want to learn more about hashing I suggest you check out this site:

http://xlinux.nist.gov/dads//HTML/hash.html

def hashThis(x):
   return(x % 100)

Lambda Calculus

Wikipedia’s description:

The lambda calculus (also written as λ-calculus) is a formal system in mathematical logic for expressing computation by way of variable binding and substitution.

http://en.wikipedia.org/wiki/Lambda_calculus

Geometry

In the realm of coding with geometry, you will probably mess around with points, maybe cos and sin.

http://en.wikipedia.org/wiki/Geometery

Differential Equations

Wiki Description:

A differential equation is a mathematical equation for an unknown function of one or several variables that relates the values of the function itself and its derivatives of various orders.

http://en.wikipedia.org/wiki/Differential_Equations

Integrals

Wiki Description:

If a function has an integral, it is said to be integrable. The function for which the integral is calculated is called the integrand. The region over which a function is being integrated is called the domain of integration. Usually this domain will be an interval, in which case it is enough to give the limits of that interval, which are called the limits of integration. If the integral does not have a domain of integration, it is considered indefinite (one with a domain is considered definite). In general, the integrand may be a function of more than one variable, and the domain of integration may be an area, volume, a higher dimensional region, or even an abstract space that does not have a geometric structure in any usual sense (such as a sample space in probability theory).

http://en.wikipedia.org/wiki/Integrals

Derivatives

Wiki Description:

A derivative is a measure of how a function changes as its input changes.

http://en.wikipedia.org/wiki/Derivative

Statistics

If you’re interested in statistics I highly suggest you take a look at the R programming language.  All the R bindings are ported over to c++ in The Rcpp library which might make it more accessible for some. Also The Rcpp vignettes are an awesome read.  If that’s a little out of your scope take a look at Julia, it’s a new lisp / R hybraid that might be easier to work with.  Fair warning though, Julia isn’t as well developed as R so there are plenty of missing features.  Last resort, seek out to Wolfram Alpha.

Wiki Description:

Statistics is the study of the collection, organization, analysis, and interpretation of data.

http://en.wikipedia.org/wiki/Statistics`

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s