自定义约束的类

huangapple go评论98阅读模式
英文:

Class with custom Constraint

问题

以下是您要翻译的代码部分:

  1. class Matrix<T> where T : /*已定义为所有加法、乘法和减法操作*/
  2. {
  3. public List<List<T>> Lines { get; set; }
  4. public Matrix()
  5. {
  6. Lines = new List<List<T>>();
  7. }
  8. public T this[int line, int column]
  9. {
  10. get => Lines
    [column];
  11. set => Lines
    [column] = value;
  12. }
  13. public List<T> this[int line]
  14. {
  15. get => Lines
    ;
  16. }
  17. private static T LineMultiplyCol(List<T> l, List<T> c)
  18. {
  19. T index = default;
  20. for (int i = 0; i < l.Count; i++)
  21. {
  22. index += l[i] * c[i]; //这里是错误的地方。
  23. }
  24. return index;
  25. }
  26. public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2)
  27. {
  28. //我想要完成这个部分。
  29. }
  30. }

请注意,我只翻译了您提供的代码部分,而不包括其他内容。

英文:

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

  1. class Matrix&lt;T&gt; where T : /*Defined for all addition, multiplication and subtraction operations*/
  2. {
  3. public List&lt;List&lt;T&gt;&gt; Lines { get; set; }
  4. public Matrix()
  5. {
  6. Lines = new List&lt;List&lt;T&gt;&gt;();
  7. }
  8. public T this[int line, int column]
  9. {
  10. get =&gt; Lines
    [column];
  11. set =&gt; Lines
    [column] = value;
  12. }
  13. public List&lt;T&gt; this[int line]
  14. {
  15. get =&gt; Lines
    ;
  16. }
  17. private static T LineMultiplyCol(List&lt;T&gt; l, List&lt;T&gt; c)
  18. {
  19. T index = default;
  20. for (int i = 0; i &lt; l.Count; i++)
  21. {
  22. index += l[i] * c[i]; //Here is the error.
  23. }
  24. return index;
  25. }
  26. public static Matrix&lt;T&gt; operator *(Matrix&lt;T&gt; m1, Matrix&lt;T&gt; m2)
  27. {
  28. //i want to complete this.
  29. }
  30. }

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引入了新的通用数学。您要查找的约束条件如下:

或者只需使用单个约束条件:INumber&lt;TSelf&gt;

英文:

Introduced in .NET 7/C#11 is the new generic math. The constraints you are looking for are:

Or just use a single constraint: INumber&lt;TSelf&gt;

huangapple
  • 本文由 发表于 2023年6月5日 23:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定