英文:
Class with custom Constraint
问题
以下是您要翻译的代码部分:
class Matrix<T> where T : /*已定义为所有加法、乘法和减法操作*/
{
public List<List<T>> Lines { get; set; }
public Matrix()
{
Lines = new List<List<T>>();
}
public T this[int line, int column]
{
get => Lines[column];
set => Lines[column] = value;
}
public List<T> this[int line]
{
get => Lines;
}
private static T LineMultiplyCol(List<T> l, List<T> c)
{
T index = default;
for (int i = 0; i < l.Count; i++)
{
index += l[i] * c[i]; //这里是错误的地方。
}
return index;
}
public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2)
{
//我想要完成这个部分。
}
}
请注意,我只翻译了您提供的代码部分,而不包括其他内容。
英文:
How can I do this? Limit to use defined operation Data Types(T) or
limit to use an T whit Defined for all addition, multiplication and subtraction operations here is my Matrix class
class Matrix<T> where T : /*Defined for all addition, multiplication and subtraction operations*/
{
public List<List<T>> Lines { get; set; }
public Matrix()
{
Lines = new List<List<T>>();
}
public T this[int line, int column]
{
get => Lines[column];
set => Lines[column] = value;
}
public List<T> this[int line]
{
get => Lines;
}
private static T LineMultiplyCol(List<T> l, List<T> c)
{
T index = default;
for (int i = 0; i < l.Count; i++)
{
index += l[i] * c[i]; //Here is the error.
}
return index;
}
public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2)
{
//i want to complete this.
}
}
this is my program, i want to expend to Parametric Matrix Operations, and need to do it
Parametric Matrix Operations and etc.
答案1
得分: 2
.NET 7/C#11引入了新的通用数学。您要查找的约束条件如下:
- IAdditionOperators<TSelf,TOther,TResult>
- IMultiplyOperators<TSelf,TOther,TResult>
- ISubtractionOperators<TSelf,TOther,TResult>
或者只需使用单个约束条件:INumber<TSelf>
英文:
Introduced in .NET 7/C#11 is the new generic math. The constraints you are looking for are:
- IAdditionOperators<TSelf,TOther,TResult>
- IMultiplyOperators<TSelf,TOther,TResult>
- ISubtractionOperators<TSelf,TOther,TResult>
Or just use a single constraint: INumber<TSelf>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论