Operator Overloading for Mathematical Libraries

Operator Overloading in C#.NET

This article discuses the concept of operator overloading in C# and its application in mathematical libraries. In C#, operators are static methods that 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 that contains data. The data thus can 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. The Gaussian elimination method is used to find the inverse of the matrix. All these operations return a Matrix. The operations +, -, and * take two Matrix objects as parameters whereas 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 "==" and "!=" operators 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 an object when the above operators are defined. This is necessary to provide compatibility with other .NET languages that 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;
   }
}

Uses of Operator Overloading

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



About the Author

VijayaSekhar Gullapalli

VijayaSekhar Gullapalli, M.Sc. , is a R&D Engineer who has developed many engineering and mathematical libraries in Microsoft Visual C#.net. His primary interests are in the areas of Fluid Mechanics, Heat Transfer and Computational Engineering.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 29, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Leveraging Flash storage to accelerate an Oracle Real Application Clusters (RAC) environment is one of today's hottest technology topics. Oracle databases require guaranteed levels of storage performance and high availability of data. Until recently, Oracle RAC could only use Flash storage for SmartFlash Cache, which addresses some performance improvements, but limits the benefits that can be gained from a shared Flash infrastructure. Enter …

  • As businesses have grown in size and global reach, emerging technologies such as cloud computing and virtualization have appeared to help companies effectively connect and grow. However, the networking strategies and infrastructures that keep organizations connected have often remained in the past. Now, new strategies that leverage global connectivity and locations exist to provide a more flexible and cost-effective alternative to traditional networking systems. This Aberdeen report analyzes how top performing …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds