22.3. Another example of operator overloading

Just for developers who enjoy dabbling with matrices, here's another full example. [4]

[4] You can skip this if you have understood the previous example. You need to understand matrix mathematics to appreciate what is going on here. (Time to search for that old high school math textbook!)

 1: using System;
 2:
 3: // class represents a 2 " 2 matrix
 4: class Matrix{
 5:
 6:   // these 4 fields represent the 4 values in the 2 " 2 matrix
 7:   public float TopLeft;
 8:   public float TopRight;
 9:   public float BottomLeft;
10:   public float BottomRight;
11:
12:   // constructor
13:   public Matrix
14:   (float TopLeft, float TopRight, float BottomLeft, float BottomRight){
15:     this.TopLeft = TopLeft;
16:     this.TopRight = TopRight;
17:     this.BottomLeft = BottomLeft;
18:     this.BottomRight = BottomRight;
19:   }
20:
21:   // overrides ToString to show matrix values
22:   public override string ToString (){
23:     return "[" + TopLeft + ", " + TopRight + "]
"+
24:            "[" + BottomLeft+ ", " + BottomRight+ "]";
25:   }
26:
27:   // overloads + operator to perform matrix addition
28:   public static Matrix operator + (Matrix m1, Matrix m2){
29:     return new Matrix(m1.TopLeft + m2.TopLeft,
30:                       m1.TopRight + m2.TopRight,
31:                       m1.BottomLeft + m2.BottomLeft,
32:                       m1.BottomRight + m2.BottomRight);
33:   }
34:
35:   // overloads – operator to perform matrix subtraction
36:   public static Matrix operator – (Matrix m1, Matrix m2){
37:     return new Matrix(m1.TopLeft – m2.TopLeft,
38:                       m1.TopRight – m2.TopRight,
39:                       m1.BottomLeft – m2.BottomLeft,
40:                       m1.BottomRight – m2.BottomRight);
41:   }
42:
43:   // overloads * operator to perform product with a real number
44:   public static Matrix operator * (Matrix m1, float realNumber){
45:     return new Matrix(m1.TopLeft * realNumber,
46:                       m1.TopRight * realNumber,
47:                       m1.BottomLeft * realNumber,
48:                       m1.BottomRight * realNumber);
49:   }
50:
51:   // overloads ^ to perform multiplication with another 2 " 2 matrix
52:   public static Matrix operator ^ (Matrix m1, Matrix m2){
53:     return new Matrix
54:       ((m1.TopLeft * m2.TopLeft) + (m1.TopRight * m2.BottomLeft),
55:       (m1.TopLeft * m2.TopRight) + (m1.TopRight * m2.BottomRight),
56:       (m1.BottomLeft * m2.TopLeft) + (m1.BottomRight * m2.BottomLeft),
57:       (m1.BottomLeft * m2.TopRight) + (m1.BottomRight * m2.BottomRight)
58:       );
59:   }
60:
61:   // overloads ! operator to perform matrix inversion.
62:   public static Matrix operator ! (Matrix m1){
63:
64:     float determinant =
65:       (m1.TopLeft * m1.BottomRight) – (m1.TopRight * m1.BottomLeft);
66:
67:     float determinantReciprocal = 1/determinant;
68:
69:     Matrix intermediate = new Matrix
70:       (m1.BottomRight, -m1.TopRight, -m1.BottomLeft, m1.TopLeft);
71:
72:     return (intermediate * determinantReciprocal);
73:    }
74: }
75:
76:
77: class TestClass{
78:   public static void Main(){
79:
80:     // try out inverse function
81:       Matrix m1 = new Matrix (3,5,1,2);
82:     Console.WriteLine ("m1:
" + m1);
83:     Console.WriteLine ("m1 inverse:
" + (!m1));
84:     Console.WriteLine ();
85:
86:     // try out multiplication with a real number
87:       Matrix m2 = new Matrix (1,5,5,1);
88:     Console.WriteLine ("m2:
" + m2);
89:     Console.WriteLine ("3 multiplied by m2:
" + (m2*3));
90:   }
91: }

Output:

c:expt>test
m1:
[3 , 5]
[1 , 2]
m1 inverse:
[2 , -5]
[-1 , 3]
m2:
[1 , 5]
[5 , 1]
3 multiplied by m2:
[3 , 15]
[15 , 3]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.111.208