MatrixMath compiling error

I copied and pasted all of the code supplied by this library link but am still getting problems compiling the code:
http://playground.arduino.cc/Code/MatrixMath

I am using the Arduino Mega 2560

TROUBLESHOOTING TAKEN

STEPS I HAVE TAKEN WHILE INSTALLING THE LIBRARY

  • started by copying each section of code into notepad files.
  • When saving each notepad file I choose the 'save type file as' to be 'all'.
  • All text files were saved into a folder named "Matrix Math".

'file name' saved as: 'Name' in MatrixMath folder: 'type' in MatrixMath folder:

MatrixMath.ino MatrixMath Arduino file
MatrixMath.cpp MatrixMath Header file
MatrixMath.h MatrixMath C++ souce file

Then I moved the folder into the appropriate Arduino library folder so that I could access "MatrixMath" in arduino by clicking the following:
file --> examples --> MatrixMath

Then I verify the code and get this error as follows:

MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Copy(float*, int, int, float*)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:31: multiple definition of `MatrixMath::Copy(float*, int, int, float*)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:31: first defined here
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Multiply(float*, float*, int, int, int, float*)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:43: multiple definition of `MatrixMath::Multiply(float*, float*, int, int, int, float*)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:43: first defined here
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Add(float*, float*, int, int, float*)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:63: multiple definition of `MatrixMath::Add(float*, float*, int, int, float*)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:63: first defined here
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Subtract(float*, float*, int, int, float*)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:78: multiple definition of `MatrixMath::Subtract(float*, float*, int, int, float*)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:78: first defined here
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Transpose(float*, int, int, float*)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:93: multiple definition of `MatrixMath::Transpose(float*, int, int, float*)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:93: first defined here
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Scale(float*, int, int, float)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:105: multiple definition of `MatrixMath::Scale(float*, int, int, float)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:105: first defined here
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Invert(float*, int)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:120: multiple definition of `MatrixMath::Invert(float*, int)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:120: first defined here
MatrixMath\MatrixMath.cpp.o: In function `MatrixMath::Print(float*, int, int, String)':
C:\Program Files (x86)\Arduino\libraries\MatrixMath/MatrixMath.cpp:17: multiple definition of `MatrixMath::Print(float*, int, int, String)'
MatrixMath.cpp.o:C:\Users\MATPC~1\AppData\Local\Temp\build5070405843853062295.tmp/MatrixMath.cpp:17: first defined here
MatrixMath\MatrixMath.cpp.o:(.bss.Matrix+0x0): multiple definition of `Matrix'
MatrixMath.cpp.o:(.bss.Matrix+0x0): first defined here
core.a(main.cpp.o): In function `main':
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:40: undefined reference to `setup'
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:43: undefined reference to `loop'

Yes I am a noob! So, any help would be greatly appreciated as this problem is a little over my head.
Thanks in advance.

looks like you have not created this library correctly. So in order to see what you have done wrong then zip up your library file and attach it to your next post, along with the test code you are using when you get these errors.

/*
 *  MatrixMath.cpp Library for Matrix Math
 *
 *  Created by Charlie Matlack on 12/18/10.
 *  Modified from code by RobH45345 on Arduino Forums, algorithm from 
 *  NUMERICAL RECIPES: The Art of Scientific Computing.
 */

#include "MatrixMath.h"

#define NR_END 1

MatrixMath Matrix;			// Pre-instantiate

// Matrix Printing Routine
// Uses tabs to separate numbers under assumption printed float width won't cause problems
void MatrixMath::Print(float* A, int m, int n, String label){
	// A = input matrix (m x n)
	int i,j;
	Serial.println();
	Serial.println(label);
	for (i=0; i<m; i++){
		for (j=0;j<n;j++){
			Serial.print(A[n*i+j]);
			Serial.print("\t");
		}
		Serial.println();
	}
}

void MatrixMath::Copy(float* A, int n, int m, float* B)
{
	int i, j, k;
	for (i=0;i<m;i++)
		for(j=0;j<n;j++)
		{
			B[n*i+j] = A[n*i+j];
		}
}

//Matrix Multiplication Routine
// C = A*B
void MatrixMath::Multiply(float* A, float* B, int m, int p, int n, float* C)
{
	// A = input matrix (m x p)
	// B = input matrix (p x n)
	// m = number of rows in A
	// p = number of columns in A = number of rows in B
	// n = number of columns in B
	// C = output matrix = A*B (m x n)
	int i, j, k;
	for (i=0;i<m;i++)
		for(j=0;j<n;j++)
		{
			C[n*i+j]=0;
			for (k=0;k<p;k++)
				C[n*i+j]= C[n*i+j]+A[p*i+k]*B[n*k+j];
		}
}


//Matrix Addition Routine
void MatrixMath::Add(float* A, float* B, int m, int n, float* C)
{
	// A = input matrix (m x n)
	// B = input matrix (m x n)
	// m = number of rows in A = number of rows in B
	// n = number of columns in A = number of columns in B
	// C = output matrix = A+B (m x n)
	int i, j;
	for (i=0;i<m;i++)
		for(j=0;j<n;j++)
			C[n*i+j]=A[n*i+j]+B[n*i+j];
}


//Matrix Subtraction Routine
void MatrixMath::Subtract(float* A, float* B, int m, int n, float* C)
{
	// A = input matrix (m x n)
	// B = input matrix (m x n)
	// m = number of rows in A = number of rows in B
	// n = number of columns in A = number of columns in B
	// C = output matrix = A-B (m x n)
	int i, j;
	for (i=0;i<m;i++)
		for(j=0;j<n;j++)
			C[n*i+j]=A[n*i+j]-B[n*i+j];
}


//Matrix Transpose Routine
void MatrixMath::Transpose(float* A, int m, int n, float* C)
{
	// A = input matrix (m x n)
	// m = number of rows in A
	// n = number of columns in A
	// C = output matrix = the transpose of A (n x m)
	int i, j;
	for (i=0;i<m;i++)
		for(j=0;j<n;j++)
			C[m*j+i]=A[n*i+j];
}

void MatrixMath::Scale(float* A, int m, int n, float k)
{
	for (int i=0; i<m; i++)
		for (int j=0; j<n; j++)
			A[n*i+j] = A[n*i+j]*k;
}


//Matrix Inversion Routine
// * This function inverts a matrix based on the Gauss Jordan method.
// * Specifically, it uses partial pivoting to improve numeric stability.
// * The algorithm is drawn from those presented in 
//	 NUMERICAL RECIPES: The Art of Scientific Computing.
// * The function returns 1 on success, 0 on failure.
// * NOTE: The argument is ALSO the result matrix, meaning the input matrix is REPLACED
int MatrixMath::Invert(float* A, int n)
{
	// A = input matrix AND result matrix
	// n = number of rows = number of columns in A (n x n)
	int pivrow;		// keeps track of current pivot row
	int k,i,j;		// k: overall index along diagonal; i: row index; j: col index
	int pivrows[n]; // keeps track of rows swaps to undo at end
	float tmp;		// used for finding max value and making column swaps

	for (k = 0; k < n; k++)
	{
		// find pivot row, the row with biggest entry in current column
		tmp = 0;
		for (i = k; i < n; i++)
		{
			if (abs(A[i*n+k]) >= tmp)	// 'Avoid using other functions inside abs()?'
			{
				tmp = abs(A[i*n+k]);
				pivrow = i;
			}
		}

		// check for singular matrix
		if (A[pivrow*n+k] == 0.0f)
		{
			Serial.println("Inversion failed due to singular matrix");
			return 0;
		}

		// Execute pivot (row swap) if needed
		if (pivrow != k)
		{
			// swap row k with pivrow
			for (j = 0; j < n; j++)
			{
				tmp = A[k*n+j];
				A[k*n+j] = A[pivrow*n+j];
				A[pivrow*n+j] = tmp;
			}
		}
		pivrows[k] = pivrow;	// record row swap (even if no swap happened)

		tmp = 1.0f/A[k*n+k];	// invert pivot element
		A[k*n+k] = 1.0f;		// This element of input matrix becomes result matrix

		// Perform row reduction (divide every element by pivot)
		for (j = 0; j < n; j++)
		{
			A[k*n+j] = A[k*n+j]*tmp;
		}

		// Now eliminate all other entries in this column
		for (i = 0; i < n; i++)
		{
			if (i != k)
			{
				tmp = A[i*n+k];
				A[i*n+k] = 0.0f;  // The other place where in matrix becomes result mat
				for (j = 0; j < n; j++)
				{
					A[i*n+j] = A[i*n+j] - A[k*n+j]*tmp;
				}
			}
		}
	}

	// Done, now need to undo pivot row swaps by doing column swaps in reverse order
	for (k = n-1; k >= 0; k--)
	{
		if (pivrows[k] != k)
		{
			for (i = 0; i < n; i++)
			{
				tmp = A[i*n+k];
				A[i*n+k] = A[i*n+pivrows[k]];
				A[i*n+pivrows[k]] = tmp;
			}
		}
	}
	return 1;
}
/*
 *  MatrixMath.h Library for Matrix Math
 *
 *  Created by Charlie Matlack on 12/18/10.
 *  Modified from code by RobH45345 on Arduino Forums, algorithm from 
 *  NUMERICAL RECIPES: The Art of Scientific Computing.
 */

#ifndef MatrixMath_h
#define MatrixMath_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#endif

class MatrixMath
{
public:
	//MatrixMath();
	void Print(float* A, int m, int n, String label);
	void Copy(float* A, int n, int m, float* B);
	void Multiply(float* A, float* B, int m, int p, int n, float* C);
	void Add(float* A, float* B, int m, int n, float* C);
	void Subtract(float* A, float* B, int m, int n, float* C);
	void Transpose(float* A, int m, int n, float* C);
	void Scale(float* A, int m, int n, float k);
	int Invert(float* A, int n);
};

extern MatrixMath Matrix;
#endif
#include <MatrixMath.h>


#define N  (2)

float A[N][N];
float B[N][N];
float C[N][N];
float v[N];      // This is a row vector
float w[N];

float max = 10;  // maximum random matrix entry range

void setup() {
	Serial.begin(9600); 

        // Initialize matrices
        for (int i = 0; i < N; i++)
        {
          v[i] = i+1;                    // vector of sequential numbers
          for (int j = 0; j < N; j++)
          {
            A[i][j] = random(max) - max/2.0f;  // A is random
            if (i == j)
            {
              B[i][j] = 1.0f;                  // B is identity
            } else
            { 
              B[i][j] = 0.0f;
            }
          }
        }

}

void loop(){

  Matrix.Multiply((float*)A,(float*)B,N,N,N,(float*)C);

        Serial.println("\nAfter multiplying C = A*B:");
	Matrix.Print((float*)A,N,N,"A");

	Matrix.Print((float*)B,N,N,"B");
	Matrix.Print((float*)C,N,N,"C");
        Matrix.Print((float*)v,N,1,"v");

        Matrix.Add((float*) B, (float*) C, N, N, (float*) C);
        Serial.println("\nC = B+C (addition in-place)");
        Matrix.Print((float*)C,N,N,"C");
        Matrix.Print((float*)B,N,N,"B");

        Matrix.Copy((float*)A,N,N,(float*)B);
        Serial.println("\nCopied A to B:");
	Matrix.Print((float*)B,N,N,"B");

        Matrix.Invert((float*)A,N);
        Serial.println("\nInverted A:");
	Matrix.Print((float*)A,N,N,"A");

        Matrix.Multiply((float*)A,(float*)B,N,N,N,(float*)C);
        Serial.println("\nC = A*B");
	Matrix.Print((float*)C,N,N,"C");

        // Because the library uses pointers and DIY indexing,
        // a 1D vector can be smoothly handled as either a row or col vector
        // depending on the dimensions we specify when calling a function
        Matrix.Multiply((float*)C,(float*)v,N,N,1,(float*)w);
        Serial.println("\n C*v = w:");
        Matrix.Print((float*)v,N,1,"v");
        Matrix.Print((float*)w,N,1,"w");

while(1);
}

MatrixMath.zip (3.09 KB)

There is nothing much wrong with that.

Down load the attached, uncompressed it and drop it into the libraries folder. Then restart the arduino IDE.
Go to the Files menu -> Examples -> MatrixMath -> mmExample.

This compiles fine for me.

MatrixMath.zip (6.82 KB)

Excellent, it works like a charm! Thanks Mike I can continue on with my project now.