Sunday, October 08, 2006

Matrix Fun

A simple program written in python that does matrix addition, 
multiplication, scalar multiplication,
transpose and put the matrix into reduced echelon form. 
The main driver program is matrix_fun.py
which does the matrix operations. The run_gen.sh is a 
bash script that runs the program gen_random_test_file.py. 
gen_random_test_file.py generates a flat text file based on the command line parameters given to it.  

python gen_random_test_file.py - h
USAGE : gen_random_test_file.py[options]
Simple program to generate a test file with
random variables for size, min, max and scalar

options : -sm M, --sizemin = M Rows of matrix (random selection of 1 <>-sM N, --sizemax = N Cols of matrix (random selection of 1 <>-m X, --min = N Minimum value of a random element in the matcies
-M Y, --max = Y Maximum value of a random element in the matcies
-cm C, --scalarmin = C Minimum value for a scalar
-cM B, --scalarmax Maximum value for a scalar
-l L, --length = L Length of the list to generate
-f FILENAME, --filename = FILENAME Filename to write list too.


Then the script runs the matrix_fun.py program against it and outputs the results to a text file. Just check the code out its very simple to follow with no real thought about who the user is. Here is an example of just running matrix_fun.py by its self:

what are the dimensions of matrix A [mxn]:
3x3

Please enter in matrix A one row at a time separated by spaces.
1 2 3
5 6 7
4 5 6

What is the dimensions of your matrix B [mxn]:
3x3

Please enter in matrix B one row at a time separated by spaces.
5 6 3
7 5 2
7 9 2

MATRIX A:
| 1 2 3 |
| 5 6 7 |
| 4 5 6 |

MATRIX B:
| 5 6 3 |
| 7 5 2 |
| 7 9 2 |


If Matrix A and B are both mxn we can add them.

We can add Matrix A and B
A + B =
| 6 8 6 |
| 12 11 9 |
| 11 14 8 |

Please enter a scalar value to multiply matrix A by, (cA)
5
| 5 10 15 |
| 25 30 35 |
| 20 25 30 |

Finding the transpose of A:
| 1 5 4 |
| 2 6 5 |
| 3 7 6 |

Matrix Multiplication (AB)
| 40 43 13 |
| 116 123 41 |
| 97 103 34 |

Putting Matrix A into Reduced echelon form.
Before rref:
| 1 2 3 |
| 5 6 7 |
| 4 5 6 |
Row 0 times -5 added to row 1
| 1 2 3 |
| 0 -4 -8 |
| 4 5 6 |
Row 0 times -4 added to row 2
| 1 2 3 |
| 0 -4 -8 |
| 0 -3 -6 |
Row 1 divided by -4
| 1 2 3 |
| 0 1 2 |
| 0 -3 -6 |
Row 1 times -2 added to row 0
| 1 0 -1 |
| 0 1 2 |
| 0 -3 -6 |
Row 1 times 3 added to row 2
| 1 0 -1 |
| 0 1 2 |
| 0 0 0 |

Would you like to enter another matrix [y/n]



No comments: