Click here to Skip to main content
Licence 
First Posted 16 Jan 2005
Views 34,042
Bookmarked 18 times

Operator overloading for Mathematical Libraries

By | 16 Jan 2005 | Article
Operator overloading for Mathematical libraries in C#.NET.

Introduction

This article discuses the concept of operator overloading in C# and its application in mathematical libraries. In C#, operators are static methods which take the operands as parameters and return the result of the operation. An operator can be created for a class by overloading it just like overloading any member method. In this article, operator overloading is demonstrated for a Matrix class. Operators for operations such as addition, subtraction, multiplication and inverse are defined.

The Matrix Class

The Matrix class has a public two dimensional array member which contains data. The data can thus be modified dynamically. Given below is the example of Matrix initialization.

double[,] d3 = {{1,2,3},{5,6,7},{2,3,4}};
Matrix M = new Matrix (d3);

The operations +, -, * and / are defined in the Matrix class. The operator ‘/’ represents the Matrix inverse. Gaussian elimination method is used to find the inverse of the matrix. All these operations return a Matrix. The operations +, -, * take two Matrix objects as parameters while the ‘/’ operation takes a double value and a Matrix object as parameters. The operator overloading is done as shown below.

public static Matrix operator+ (Matrix M1, Matrix M2)
public static Matrix operator- (Matrix M1, Matrix M2)
public static Matrix operator* (Matrix M1, Matrix M2)
public static Matrix operator/ (double D, Matrix M)

Using the Operators

Once the Matrix objects are created, the operations can be done as shown below. However, the matrix dimensions must agree for the operations. The matrices should have equal dimensions for addition and subtraction. The number of columns in the first matrix should be equal to the number of rows in the second matrix for multiplication. The matrix should be a square matrix for the ‘/’ operation.

double[,] d1 = {{1,2,3},{5,6,7},{2,3,4}};
double[,] d2 = {{3,4,5},{1,2,8},{2,9,3}};
Matrix M1 = new Matrix (d1);
Matrix M2 = new Matrix (d2);

Matrix Madd = M1+M2;
Matrix Msub = M1-M2;
Matrix Mmul = M1*M2;
Matrix Minv = 1/M1;
Minv.display();

Defining the == and != operators

The operators ‘==’ and ‘!=’ are matching operators and hence should be overridden together. The compiler shows an error if one of the operators is overloaded and the other is not. These operators return a boolean value. These are overridden as shown below.

public static bool operator== (Matrix M1, Matrix M2)
public static bool operator!= (Matrix M1, Matrix M2)

It is a good practice to overload the Equals() method provided by object when the above operators are defined. This is necessary to provide compatibility with other .NET languages which do not overload operators, but do support method overloading. The Equals() method is overridden as shown below.

public override bool Equals(object obj)
{
    if (!(obj is Matrix))
    {
        return false;
    }
    return this==(Matrix)obj;
}

History

I used this method of operator overloading, when developing an application for pipe network simulation. The pipe networks contain a variety of components such as pipes, valves and bends etc. When pipes are connected end to end, it is computationally efficient to add them into a single pipe. Various types of pipe fittings who are behaviorally connected can also be added using operator overloading.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

VijayaSekhar Gullapalli



Sweden Sweden

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalnew PinmemberSendilkumar.M20:13 18 Jan '05  
GeneralOverloadable Operators PinmemberArmoghan Asif16:54 25 Jan '05  
GeneralRe: Overloadable Operators PinmemberSendilkumar.M16:23 26 Jan '05  
GeneralRe: Overloadable Operators PinmemberVijayaSekhar Gullapalli2:02 28 Jan '05  
GeneralRe: Overloadable Operators PinmemberSendilkumar.M20:35 28 Jan '05  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 16 Jan 2005
Article Copyright 2005 by VijayaSekhar Gullapalli
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid