# C# matrix-class design (transpose method)

i'm currently working on a matrix implementation in C# . It's not a question about how sth. should work or sth. similar. It's more about the "design-part" ... So, i want to implement a function, which transposes a matrix ( http://en.wikipedia.org/wiki/Transpose ) . Easy thing thought, but it's really hard for me to choose, which implemantation way is the most elegant.

But here first a bit code of the matrix class:

namespace Math { public class Matrix { protected double[,] matrix; public Matrix(byte m, byte n)[...] public Matrix(Matrix matrix)[...] public byte M { get; private set; } public byte N { get; private set; } // Possibility 1 (changes the matrix directly) public void Transpose()[...] // Possibility 2 (getter method) public Matrix GetTransposed()[...] // Possibility 3 (property) public Matrix TransposedMatrix { get[...] } // Possibility 4 (static method; a bit like an operator) public static Matrix Transpose(Matrix matrix)[...] } }

And here, how you would use the different possibilities:

namespace MathTest { class Program { static void Main(string[] args) { // Create a new matrix object... var mat1 = new Math.Matrix(4, 4); // Using possibility 2 (getter method, like "GetHashCode()" or sth. similar) var mat2 = mat1.GetTransposed(); // Using possibility 3 (the transposed matrix is a property of each matrix) var mat3 = mat1.TransposedMatrix; // Using possibility 4 (definition and use is like an unary operator) var mat4 = Math.Matrix.Transpose(mat1); // Using possibility 1 (changes the matrix directly) mat1.Transpose(); } } }

Which way would you prefer and why? Or are there even better ways to implement the transposition of a matrix?

Thank you very much!

Benjamin

## Answers

I would rather vote for

public Matrix Transpose() { ... }

Possibility 1

Is, IMHO, dubious since Matrix seems to be *immutable* (all other methods don't change it). Moreover, other operations (if you decide to implement them) e.g. arithmetics +, -, *, /, *don't change* the initial Matrix as well, they return a *new* Matrix instead.

Matrix B = -A; // <- A doesn't changed Matrix D = A + B * C; // <- A, B, C don't changed Matrix E = F.Transpose(); // <- I hope to have F being intact as well

Possibility 2

Is the best one; I'd rather rename method from GetTransposed() to Transpose() - we usuallly, use active names - Perform, Send, Write instead of GetPerformed, GetSent etc.

// looks better than // A.GetPerformed().GetValidated().GetSentTo(@"Me@MyServer.com"); A.Perform().Validate().SendTo(@"Me@MyServer.com"); // The same with transpose: // easier to read than // A.GetTransposed().GetAppied(x => x * x).GetToString(); A.Transpose().Apply(x => x * x).ToString();

Possibility 3

I, personally, don't like it because TransposedMatrix is not *a property in the typical sence of its name* like RowCount, ColCount, IsUnit, IsDegenrate... Instead TransposedMatrix looks very similar to *functions*, like Exp(), Sqrt()...

A.ColCount; // <- property of the matrix A.IsDegenerate; // <- another property of the matrix A.ToString(); // <- is not a property: it's conversion (function) into string representation A.Sqrt(); // <- is not a property, square root is a function A.Transpose(); // <- is not a propery either: it's a function too

Possibility 4:

IMHO, sounds innaturally. My way of thinking is: "I have a matrix instance A, I want to have a transposed matrix from it, say, B, so I should do something with A". And I'll start looking for the method:

B = A.Transpose(); B = A.ToTransposed(); B = A.GetTransposed(); B = A.Rotate(); B = A.Transform(...); B = A.DoSomething();

The static methods are good, IMHO, for *creating*. e.g.

A = Math.Matrix.Zero(5); // <- Create 5x5 Matrix, all zeroes B = Math.Matrix.Unit(6); // <- 6x6 unit matrix