dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.83k stars 4.62k forks source link

[API Proposal]: Matrix and Vector types using generic math #76406

Open MichalBrylka opened 1 year ago

MichalBrylka commented 1 year ago

Background and motivation

In .NET there already exist specialized matrix types MatrixXxX and VectorX (these are mainly for vectorization). It seems that we could provide a robust implementation for matrix and vector that could also be a showcase for newly introduced generic math.

API Proposal

class Matrix<TNumber> : ISpanParsable<Matrix<TNumber>>, ISpanFormattable,
    IAdditionOperators<Matrix<TNumber>, Matrix<TNumber>, Matrix<TNumber>>,
    IMultiplyOperators<Matrix<TNumber>, Matrix<TNumber>, Matrix<TNumber>>,
    IAdditionOperators<Matrix<TNumber>, TNumber, Matrix<TNumber>>
    //TODO add relevant interfaces
       where TNumber : struct, IComparisonOperators<TNumber, TNumber, bool>, INumberBase<TNumber>
{
    private readonly TNumber[,] _data;
    public int Rows { get; }
    public int Columns { get; }

    public TNumber Determinant();
   //TODO add typical matrix operations 
}

API Usage

var det = new Matrix<float>(new float[,] {
        { 1, 2, 3, 4 },
        { 4, 3, 2, 1 },
        { 2, 1, 3, 4 },
        { 2, 3, 1, 4 }
    }).Determinant(); //prints -60

Alternative Designs

No response

Risks

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-numerics See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation In .NET there already exist specialized matrix types MatrixXxX and VectorX (these are mainly for vectorization). It seems that we could provide a robust implementation for matrix and vector that could also be a showcase for newly introduced generic math. ### API Proposal ```csharp class Matrix : ISpanParsable>, ISpanFormattable, IAdditionOperators, Matrix, Matrix>, IMultiplyOperators, Matrix, Matrix>, IAdditionOperators, TNumber, Matrix> //TODO add relevant interfaces where TNumber : struct, IComparisonOperators, INumberBase { private readonly TNumber[,] _data; public int Rows { get; } public int Columns { get; } public TNumber Determinant(); //TODO add typical matrix operations } ``` ### API Usage ```csharp var det = new Matrix(new float[,] { { 1, 2, 3, 4 }, { 4, 3, 2, 1 }, { 2, 1, 3, 4 }, { 2, 3, 1, 4 } }).Determinant(); //prints -60 ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: MichalBrylka
Assignees: -
Labels: `api-suggestion`, `area-System.Numerics`
Milestone: -
tfenise commented 1 year ago

class Matrix<TNumber> : ..., IAdditionOperators<Matrix<TNumber>, TNumber, Matrix<TNumber>>

You mean IMultiplyOperators<Matrix<TNumber>, TNumber, Matrix<TNumber>>?

where TNumber : struct, IComparisonOperators<TNumber, TNumber, bool>, INumberBase

Why TNumber : struct, IComparisonOperators<TNumber, TNumber, bool>? Does TNumber really have to be a struct? Does TNumber really have to be comparable? What about finite fields or complex numbers?

Maybe also worth discussing is the practical use of it. It's conceivably hard to apply techniques like vectorization to optimize the type, as TNumber and the dimensions are variable, unlike the pre-existing types.

MichalBrylka commented 1 year ago

You mean IMultiplyOperators<Matrix<TNumber>, TNumber, Matrix<TNumber>>?

yes, INumber<> or INumberBase<> seem to be not relevant here

Why TNumber : struct, IComparisonOperators<TNumber, TNumber, bool>?

No, this can be dropped

Maybe also worth discussing is the practical use of it. It's conceivably hard to apply techniques like vectorization to optimize the type, as TNumber and the dimensions are variable, unlike the pre-existing types.

I'm not sure if we need to optimize internal structure/operations with vectorization. I guess it would not hurt for standard number types. As for use cases - that can be a generic type for all places where matrices are used without limiting oneselves to hardcoded double/Complex types