Hi I would like to have a class:
class Matrix <T>
where T : // I don't know what to write here
{
T[][] elements;
}
I would like T to be addable and multiplyable by + and * operators
Hi I would like to have a class:
class Matrix <T>
where T : // I don't know what to write here
{
T[][] elements;
}
I would like T to be addable and multiplyable by + and * operators
I would highly recommend you take a look at the "MiscUtil" library from Messrs Skeet and Gravell:
http://www.yoda.arachsys.com/csharp/miscutil/
Contained within is an incredible "generic math operators" implementation, which should line up nicely with your attempts to create a generic matrix math class. It actually reminds me (a bit) of python and how it handles referencing the operator functions directly.
(aside to @jon-skeet : any chance of this making its way to Nuget?)
Some example usages from their tests:
double sumOperator = 0;
for (int i = 0; i < COUNT; i++)
{
sumOperator = Operator.Add(sumOperator, rand.NextDouble());
}
int sumOperator = 0;
for (int i = 0; i < COUNT; i++)
{
sumOperator = Operator.Add(sumOperator, rand.Next(MAXVALUE));
}
This is not possible because the unconstrained generic type is assumed to be of type Object
, which doesn't define the arithmetic operators. Hence, it seems you need to specify an interface.
But, the primitive types (Int32, Double, etc) define the arithmetic operations effectively as static methods (actually the compiler treats them specially and generates the code inline) and an interface in C# cannot define static methods (although the design of the CLR actually allows it). Hence, no interface be written in C# which satisfies the requirements of supporting the arithmetic operations; even if it could be written, the primitive types don't inherit it, so it still wouldn't work.
The end result is that there's no way to do what you want in C#.
If you do try it, you get an error similar to
Operator '+' cannot be applied to operands of type 'T' and 'T'
If you do a web search for this message, you'll find various discussions about possible workarounds.
As I'm not aware of any built-in solution, check this out:
public abstract class BaseClass
{
public static BaseClass operator +(BaseClass p1, BaseClass p2)
{
return p1 + p2;
}
public static BaseClass operator *(BaseClass p1, BaseClass p2)
{
return p1 * p2;
}
}
public class ChildClass : BaseClass
{
}
public class Matrix <T> where T : BaseClass
{
T[][] elements;
}
Hope this helps.
However, other types, such as int
, won't be supported.
+
and *
but doesn't inherit BaseClass
this will not work. For instance int
–
Geo May be this way will be helpful for someone. You can improve this by put add, multiplicate functions in fabric-library class.
UPDATE May be putting Operators from JerKimball post into counstructor
class Matrix <T>
{
T[][] elements;
public Matrix(Func<T, T, T> add, Func<T,T,T> multiplicate)
{
this.Add = add;
this.Mult = multiplicate;
}
public Matrix<T> operator +(Matrix<T> p1, Matrix<T> p2)
{
// correct this
var t = Add(p1.elements[0][0], p2.elements[0][0]);
return this;
}
public Matrix<T> operator *(Matrix<T> p1, Matrix<T> p2)
{
// correct this
var t = Mult(p1.elements[0][0], p2.elements[0][0]);
return this;
}
private Func<T, T, T> Add { get; set; }
private Func<T, T, T> Mult { get; set; }
}
static void Main(string[] args)
{
var m1 = new Matrix<int>((x,y) => x + y, (x,y) => x * y);
var m2 = new Matrix<int>((x, y) => x + y, (x, y) => x * y);
}
UPDATE 2
just provide access to property like this:
public T[][] Elements { get; set; }
and you can do this:
m1.Elements[0][0] = 10;
var m3 = new Matrix<int?>((x, y) => x + y, (x, y) => x * y);
m3.Elements[0][0] = null;
© 2022 - 2024 — McMap. All rights reserved.
dynamic
. – Oweint
and fMatrix forfloat
)? – Geo