Click here to Skip to main content
11,641,222 members (65,528 online)
Click here to Skip to main content

Application of Fraction class: Matrix class in C#

, 16 Dec 2004 CPOL 89.2K 4K 42
Rate this:
Please Sign up or sign in to vote.
An article on developing a matrix class consisting of fractions.

Introduction

This article demonstrates how to use a fraction class to create a matrix class. We all study mathematics and hence matrices at high schools. So there is a need for matrix programs in computers. But the problem is that all matrix programs/classes (as far as I know) represent matrix in the form of floating point variable arrays. In our schools, we study matrices in the form of integers and fractions, so there is need of an efficient Matrix program/class that presents matrices in the form of fractions. I wrote my fraction class because I needed a matrix class consisting of fractions.

Background

I assume that the reader has fundamental knowledge of high school mathematics.

Implementation

Matrix is a 2D array of fractions. The class uses simple high school mathematics formulae for its internal working, some of the functions include:

  • MultiplyRow() for multiplying a whole row by an integer/fraction/double.
  • AddRow() for adding multiple of one row to another.
  • InterchangeRow() for interchanging two rows.
  • Concatenate() to concatenate two matrices column wise (this function is helpful when we want to create augmented matrix for solving equations).
  • Adjoint() to find adjoint of the matrix.
  • Transpose() to return transpose of the matrix.
  • Determinent() there are two functions, one finds the determinant by the common minor method while the other is a very fast. algorithm that makes use of reduced echelon form to find the determinant.
  • Inverse() there are two functions, one finds the inverse by the common adjoints method while the other uses reduced echelon form method to find it. Obviously, the second one is far more efficient that the first one which slows mainly due to recursion.
  • EchelonForm() can be useful in equation solving by Gaussian Elimination method.
  • ReducedEchelonForm() can be useful in equation solving by Gauss-Jordan method.

Using the code

The class is very simple to use, it contains a variety of constructors for various situations.

int[,] intarr={{1,2,3},{1,4,9},{4,5,6}};    // initializes a 2D integer array
Matrix matrix=new Matrix( intarr );  // initializes the matrix with the 2D array, 
       // note that the array has been converted to fractions

Console.WriteLine(matrix); //display the matrix

The following code block demonstrates some functions, see how simple they are to use:

Console.WriteLine( matrix.Transpose() );
// displays transpose

Console.WriteLine( matrix.Inverse() ); // displays inverse of the matrix
Console.WriteLine( matrix.Inverse()*matrix ); 
        // obviously this will display an identity matrix 
        // (recall your school mathematics)


Console.WriteLine( matrix.EchelonForm() );     // displays Echelon form

Console.WriteLine( matrix+matrix.Transpose() ); 
        // forgotten high school maths? this will display a 
        // symmetric matrix (still forgotten what is symmetric matrix, 
        // buy some book of mathematics :) )

Application: Equation Solving by this class

Consider the following system of equation:

X + 2Y - 3Z = 10
4X - 2Y - Z = 6
X - Y - 2Z = -3

Using Gauss-Jordan method, let us solve it using the matrix class.

int[,] arrA={{1,2,-3},{4,-2,-1},{1,-1,-2}};    //array of coefficients
int[,] arrB={ {2},{5},{-3}};            // array of constants
Matrix matrixA=new Matrix( arrA );        // convert array to matrix
Matrix matrixB=new Matrix( arrB );        // convert array to matrix
// get augmented matrix by concatenation
Matrix augmentedMatrix=Matrix.Concatenate( matrixA, matrixB );
Console.WriteLine("Augmented matrix for the given system of equation is" + 
                  augmentedMatrix);

// evaluate reduced echelon form
Matrix reducedEchForm=augmentedMatrix.ReducedEchelonForm();
Console.WriteLine("Reduced Echelon form for the given augmented matrix is" + 
                  reducedEchForm);
// the last column of augmented matrix gives the answer values
Console.WriteLine("Value for X is " + reducedEchForm[0,3] );
Console.WriteLine("Value for Y is " + reducedEchForm[1,3] );
Console.WriteLine("Value for Z is " + reducedEchForm[2,3] );

The above code yield values X=65/23, Y=52/23, Z=41/23.

Other Applications

Yes, matrices are not over here. In a similar fashion, the class can be used in Linear programming problems. (I hope you will also be familiar with Economics. By the way, I implemented this class because I wanted to verify my results in linear programming problems.) The class can also be used in various transformations (a topic of analytic geometry), Eigen Value/Eigen Space problems, and in other problems related to vector space, etc.

History

What's new in version 1.1

  • Added DeterminentFast() method
  • Added InverseFast() method
  • Renamed ConvertToString to (override) ToString()
  • Fixed some minor bugs

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Syed Mehroz Alam
Software Developer
Pakistan Pakistan

Syed Mehroz Alam, living in Karachi, Pakistan, is a developer focusing Microsoft technologies. He has completed his bachelors as a Computer Systems Engineer in 2006 and is currently pursuing a Masters degree in Computer Science. He loves to learn, discover and master all aspects of .NET and SQL Server. Mehroz has developed rich internet enterprise applications using Silverlight in addition to the traditional ASP.NET and Windows Forms applications. He has worked with all three components of SQL Business Intelligence Studio: SSIS, SSRS and SSAS for developing BI Solutions and Data warehouse. He loves to write complex TSQL queries and evaluate his skills by participating in various TSQL Challenges. His blog can be viewed at http://smehrozalam.wordpress.com.


You may also be interested in...

Comments and Discussions

 
BugErrors in parsing Pin
elw00d12324-Sep-12 1:19
memberelw00d12324-Sep-12 1:19 
QuestionGreat lib but wrong method names - not mathimatically standart Pin
AlexanderSapronov6-Jun-12 23:06
memberAlexanderSapronov6-Jun-12 23:06 
Questionincorrect? Pin
cowlinator24-Apr-10 8:36
membercowlinator24-Apr-10 8:36 
AnswerRe: incorrect? Pin
Syed Mehroz Alam16-May-10 23:30
memberSyed Mehroz Alam16-May-10 23:30 
QuestionCan you get the resualt? help me, please! Pin
sgncdc23-Jul-08 17:52
membersgncdc23-Jul-08 17:52 
GeneralException by parsing number Pin
slady.net23-Aug-07 6:13
memberslady.net23-Aug-07 6:13 
GeneralError Handling Fractions Pin
bostamy2-Aug-06 5:39
memberbostamy2-Aug-06 5:39 
GeneralA correction in the Equation Solving example Pin
Syed Mehroz Alam16-Dec-04 23:38
memberSyed Mehroz Alam16-Dec-04 23:38 
Many of you would have noticed that the result of equation solving code example does not satisfy the given system of equation. The problem is that the value of constants given in the equations does not match with the array of constants in the code block. The correct system of equation which complies with the code and hence the answer is:
X + 2Y - 3Z = 2
4X - 2Y - Z = 5
X - Y - 2Z = -3

I regret for this mistake.

Syed Mehroz Alam
Email: smehrozalam@yahoo.com
Homepage: Programming Home
URL: http://www.geocities.com/smehrozalam/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 17 Dec 2004
Article Copyright 2004 by Syed Mehroz Alam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid