Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

A Math Class That Gives You More Than Just the Answer

Rate me:
Please Sign up or sign in to vote.
4.64/5 (5 votes)
6 Aug 2009CPOL4 min read 28.4K   539   31   1
See the intermediate steps involved in a Linear Equation or Fractions problem
classMath

Not Just a Calculator

This dynamic link library contains all the usual functions you'd find in any classMath dedicated to Linear Equations or Fractions. Aside from performing arithmetic and spitting out the answer for you, this fancy do-dad can be a helpful learning tool. If you're studying Linear Equations and are struggling with a Gauss-Jordan Elimination problem, for example, then looking at the answer doesn't necessarily help you. What you need is a solution because though some textbooks/teachers may provide you with answers, they don't always show you how to obtain it. And that's exactly what this program does. With this tool, you can view the intermediate steps required to solve the problem as if your teacher were solving it on the chalk board (minus that know-it-all in the front row who keeps looking at you and laughing because he's already solved it).

Using the Calculator

The testClassMath application could easily be called 'Matrix Equations Calculator' and works just like any other program you'll find on the market. There are two input textboxes, A & B, plus the results textbox on the right. You can perform unary operations on the contexts of any of these textboxes by selecting the one you want and then clicking on the button in the operations groupbox on the right and the answer will appear in the results textbox. Moving the values from one textbox to another, a file, or memory, is done with the 'Move Controls' beneath the selected textbox. These controls are fairly intuitive and shouldn't give you any problem.

classMath

In the above image, the user has entered the two fractions 8/12 and 4/8 into inputs A & B, then did an addition the result of which is seen in the Result textbox, 7/6. Clicking the 'Show Solution' button in the Operations groupbox gives you the following:

classMath

Matrix calculations are done in similar fashion but require a matrix input in an exact format. Specifically, all entries are whole numbers or fractions, separated by commas and there must be the same number of entries in each row. Examples are given below:

classMath

The Result textbox shows the answer to 'Solve Augmented Matrix' for Input A, that is: there is an exact solution where X1=2, X2=0, and X3=-1. The interpretation of the results may not be easy to read and part of using matrices and matrix calculators involves a bit of knowledge on how to read the answer but with the 'Show Solution' button, you can view the following:

classMath

The notation used here is fairly standard and should not be difficult for any student of Linear Equations to understand. You can see the step by step Gauss-Jordan Elimination solution in this 3x3 equation matrix and its answer matrix. You'll notice that at the bottom, the solution is clearly spelled out for you. If there are 'infinitely many solutions' or 'no solution' then that's what this handy tool will tell you so you don't have to ask the guy at the front of the class anymore. Since many of these solutions are too long to fit on a single screen, the program lets you scroll around and view what you like by moving your mouse to the edges of the screen.

Using the Code

If you only want to use this mathClass() to solve matrices and couldn't be bothered to write your own, then you'll find this very handy and you need not look any further. The user-defined-types udtFraction and udtMatrix are as given below.

In the classFractions, you'll find:

C#
public struct udtFraction
    {
        public int numerator;
        public int denominator;
    }

and udtMatrix is in classLinearEquations:

C#
public struct udtMatrix
    {
        public udtMatrixRow[] rows;
        public enuLinearEquationTypeSolution typeSolution;
        public classFractions.udtFraction[] uniqueSolution;
        public bool augmentedMatrix;
    }
public struct udtMatrixRow
    { public classFractions.udtFraction[] elements;}

These are the basic building blocks with which everything is done. On the fractions side, things are pretty clear, as for the Matrices you might need to think for a second before using this because you'll notice that there are arrays and these need to be resized and initialized before you can use them. The classes have functions which convert between fraction/string or matrix/string which you may find easier to deal with. As for instantiating the class itself, nothing could be simpler:

C#
classMath cLibMath = new classMath();

If we look at the 'multiply' button's event handler, you'll get an idea of how to call these functions:

C#
void lblOp_Multiply_Click(object sender, EventArgs e)
{
 switch ((TabIndices)MathFolder.SelectedIndex)
 {
 case TabIndices.LinearEquations:
  txtResult.Text = cLibMath.cLibLinearEquations.convertMatrixToString(
         cLibMath.cLibLinearEquations.matrixMultiplication(
               cLibMath.cLibLinearEquations.convertStringToMatrix(
                                             txtInput_A.Text.ToString()
                                                                  ),
               cLibMath.cLibLinearEquations.convertStringToMatrix(
                                             txtInput_B.Text.ToString()
                                                                  ),
               true
                                                          )
                                                                      );
  lblOp_ShowSolution.Visible = true;
  break;

 case TabIndices.Fractions:
  txtResult.Text = cLibMath.cLibFractions.getStringFromFraction(
   cLibMath.cLibFractions.multiplyFractions(
     cLibMath.cLibFractions.getFractionFromString(txtInput_A.Text.ToString()),
     cLibMath.cLibFractions.getFractionFromString(txtInput_B.Text.ToString()),
     true 
                                            )
                                                                );
  lblOp_ShowSolution.Visible = true;
  break;
 }            
}

Many programmers will find these lines particularly ugly so let me break down some of it for you. Suppose we were to rewrite that one 'line of code' (which actually takes up 10 lines in the IDE) in the case of TabIndices.LinearEquations where txtResults.Text is being assigned, to read as follows:

C#
// convert the text in txtInputA into a matrix 
classLinearEquations.udtMatrix udrMatrixA = 
  cLibMath.cLibLinearEquations.convertStringToMatrix(txtInput_A.Text.ToString());

// convert the text in txtInputB into another matrix
classLinearEquations.udtMatrix udrMatrixB = 
  cLibMath.cLibLinearEquations.convertStringToMatrix(txtInput_B.Text.ToString());

// multiply these two matrices and write the result into a third matrix
classLinearEquations.udtMatrix udrMatrixResult = 
  cLibMath.cLibLinearEquations.matrixMultiplication(udrMatrixA, udrMatrixB, true);

// convert the result matrix into a string and assign it to txtResult
txtResult.Text = cLibMath.cLibLinearEquations.convertMatrixToString(udrMatrixResult);

That's probably more legible for most of you, but I still like my way better.

One final thing you should notice is the event handler for the formSolution...

C#
void formSolution_Disposed(object sender, EventArgs e)
{
 lblOp_ShowSolution.Visible = false;
 formSolution = new Form();
 formSolution.Disposed += new EventHandler(formSolution_Disposed);
}

...and the last two lines of code in formMath():

C#
formSolution = new Form();
formSolution.Disposed += new EventHandler(formSolution_Disposed);

That's about it... for now.

History

  • 6th August, 2009: Initial post

License

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


Written By
CEO unemployable
Canada Canada
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.

Comments and Discussions

 
QuestionUse? Pin
mbyamukama7-Aug-09 1:49
mbyamukama7-Aug-09 1:49 

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

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