-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Numerics; | ||
using System.Numerics.Tensors; | ||
using System.Runtime.InteropServices; | ||
using CommunityToolkit.Diagnostics; | ||
using CommunityToolkit.HighPerformance; | ||
using MathNet.Numerics; | ||
using MathNet.Numerics.LinearAlgebra.Double; | ||
using WPIMath.Numbers; | ||
|
||
namespace WPIMath; | ||
|
||
public readonly struct Matrix<R, C> where R : class, Nat where C : class, Nat | ||
{ | ||
private readonly DenseMatrix m_storage; | ||
|
||
public Matrix() | ||
{ | ||
int rows = R.Num; | ||
int cols = C.Num; | ||
m_storage = new DenseMatrix(rows, cols); | ||
} | ||
|
||
private Matrix(DenseMatrix storage) | ||
{ | ||
Debug.Assert(storage.RowCount == R.Num); | ||
Debug.Assert(storage.ColumnCount == C.Num); | ||
m_storage = storage; | ||
} | ||
|
||
public readonly DenseMatrix Storage => m_storage; | ||
|
||
public readonly MatrixSpan<R, C> Span => new(this); | ||
|
||
public readonly int NumRows => R.Num; | ||
public readonly int NumColumns => C.Num; | ||
|
||
public readonly double this[int row, int col] | ||
{ | ||
get | ||
{ | ||
return m_storage[row, col]; | ||
} | ||
set | ||
{ | ||
m_storage[row, col] = value; | ||
} | ||
} | ||
|
||
public void SetRow(MatrixSpan<N1, C> val) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void SetColumn(MatrixSpan<R, N1> val) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void Fill(double value) | ||
{ | ||
m_storage.Values.AsSpan().Fill(value); | ||
} | ||
|
||
public Matrix<R, C> Diag() | ||
{ | ||
return new(DenseMatrix.OfDiagonalVector(m_storage.Diagonal())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using CommunityToolkit.HighPerformance; | ||
|
||
namespace WPIMath; | ||
|
||
public ref struct MatrixSpan<R, C> where R : class, Nat where C : class, Nat | ||
{ | ||
private Span2D<double> m_storage; | ||
|
||
public MatrixSpan() | ||
{ | ||
int rows = R.Num; | ||
int cols = C.Num; | ||
int length = rows * cols; | ||
m_storage = new Memory2D<double>(new double[length], rows, cols).Span; | ||
} | ||
|
||
public MatrixSpan(Matrix<R, C> storage) | ||
{ | ||
m_storage = storage.Storage.Span; | ||
} | ||
|
||
public readonly Span2D<double> Storage => m_storage; | ||
|
||
public readonly int NumRows => R.Num; | ||
public readonly int NumColumns => C.Num; | ||
|
||
public double this[int row, int col] | ||
{ | ||
get | ||
{ | ||
return m_storage[row, col]; | ||
} | ||
set | ||
{ | ||
m_storage[row, col] = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace WPIMath; | ||
|
||
public interface Nat | ||
{ | ||
public static abstract int Num {get;} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WPIMath.Numbers; | ||
|
||
public sealed class N1 : Nat | ||
{ | ||
private N1() { } | ||
|
||
public static int Num => 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WPIMath.Numbers; | ||
|
||
public sealed class N2 : Nat | ||
{ | ||
private N2() { } | ||
|
||
public static int Num => 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WPIMath.Numbers; | ||
|
||
public sealed class N3 : Nat | ||
{ | ||
private N3() { } | ||
|
||
public static int Num => 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters