Generics, Type Operators, Matrix operations

G

Guest

I want to create a Matrix class of a type using Generics.
class Matrix<T> { ... }

I want to provide an Add method that will add the the 2 Matrices under the
type of int. My add method would look like:

public static Matrix<T> operator +(Matrix<T> a, Matrix<T> b) {
// check for same size
Matrix<T> c = new Matrix<T>(a.Row, a.Col);
for(int i=0;i<a.Row;i++)
for(int j=0;j<a.Col;j++)
c[i,j] = a[i,j]+b[i,j];
return c;
}

For a matrix of Matrix(int) I want to add the elements of type int. There
is a problem with operators and Types.
Is there a way around this?

Thanks,
Marc
 
F

Frans Bouma [C# MVP]

Marc said:
I want to create a Matrix class of a type using Generics.
class Matrix<T> { ... }

I want to provide an Add method that will add the the 2 Matrices
under the type of int. My add method would look like:

public static Matrix<T> operator +(Matrix<T> a, Matrix<T> b) {
// check for same size
Matrix<T> c = new Matrix<T>(a.Row, a.Col);
for(int i=0;i<a.Row;i++)
for(int j=0;j<a.Col;j++)
c[i,j] = a[i,j]+b[i,j];
return c;
}

For a matrix of Matrix(int) I want to add the elements of type int.
There is a problem with operators and Types.
Is there a way around this?

Not at the moment in the sense that it's currently not possible to
write the code you want to write as above.

The 'common' way of solving it is by a delegate passed into the method
which performs all the work (in this case adding two elements). This is
slower than the situation where you just add two ints for example.

I also don't understand why on earth MS didn't include operator
restrictions for 'where' as the absense currently severily limits
generic support (and brings it down to 'a way of doing strongly typed
collections'..)

FB

--
 
G

Guest

Thanks,

Marc


Frans Bouma said:
Marc said:
I want to create a Matrix class of a type using Generics.
class Matrix<T> { ... }

I want to provide an Add method that will add the the 2 Matrices
under the type of int. My add method would look like:

public static Matrix<T> operator +(Matrix<T> a, Matrix<T> b) {
// check for same size
Matrix<T> c = new Matrix<T>(a.Row, a.Col);
for(int i=0;i<a.Row;i++)
for(int j=0;j<a.Col;j++)
c[i,j] = a[i,j]+b[i,j];
return c;
}

For a matrix of Matrix(int) I want to add the elements of type int.
There is a problem with operators and Types.
Is there a way around this?

Not at the moment in the sense that it's currently not possible to
write the code you want to write as above.

The 'common' way of solving it is by a delegate passed into the method
which performs all the work (in this case adding two elements). This is
slower than the situation where you just add two ints for example.

I also don't understand why on earth MS didn't include operator
restrictions for 'where' as the absense currently severily limits
generic support (and brings it down to 'a way of doing strongly typed
collections'..)

FB

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top