Home All Groups Group Topic Archive Search About

Generics, Type Operators, Matrix operations

Author
19 Jan 2006 2:38 PM
Marc
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

Author
20 Jan 2006 8:14 AM
Frans Bouma [C# MVP]
Marc wrote:

Show quote
> 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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Author
20 Jan 2006 2:03 PM
Marc
Thanks,

Marc


Show quote
"Frans Bouma [C# MVP]" wrote:

> Marc wrote:
>
> > 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
>
> --
> ------------------------------------------------------------------------
> Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> My .NET blog: http://weblogs.asp.net/fbouma
> Microsoft MVP (C#)
> ------------------------------------------------------------------------
>

AddThis Social Bookmark Button