Added row operators to matrixes. Row-echelon coming soon.

This commit is contained in:
That-One-Nerd 2023-07-10 17:10:40 -04:00
parent be59b70e00
commit 63e70f7124
7 changed files with 144 additions and 1 deletions

View File

@ -2,18 +2,38 @@
TODO TODO
added row operations, fixed cofactor bugs, added setters
Here's the full changelog: Here's the full changelog:
``` ```
* Nerd_STF * Nerd_STF
* Mathematics * Mathematics
* Abstract * Abstract
* IMatrix * IMatrix
+ AddRow(int, int, float)
+ Cofactor() + Cofactor()
+ ScaleRow(int, float)
+ SwapRows(int, int)
+ SolveRowEchelon()
* Algebra * Algebra
* Matrix * Matrix
+ AddRow(int, int, float)
+ ScaleRow(int, float)
+ SwapRows(int, int)
= Fixed a blunder in `SignGrid(Int2)` with signs being incorrectly placed on matrixes with even column count. = Fixed a blunder in `SignGrid(Int2)` with signs being incorrectly placed on matrixes with even column count.
* Matrix2x2 * Matrix2x2
+ AddRow(int, int, float)
+ ScaleRow(int, float)
+ SwapRows(int, int)
= Fixed a blunder in `Cofactor()` with the position of elements. = Fixed a blunder in `Cofactor()` with the position of elements.
* Matrix3x3
+ AddRow(int, int, float)
+ ScaleRow(int, float)
+ SwapRows(int, int)
* Matrix4x4
+ AddRow(int, int, float)
+ ScaleRow(int, float)
+ SwapRows(int, int)
* NumberSystems * NumberSystems
* Complex * Complex
+ operator Complex(SystemComplex) + operator Complex(SystemComplex)

View File

@ -10,6 +10,12 @@ public interface IMatrix<T> : IAbsolute<T>, ICeiling<T>, IClamp<T>, IDivide<T>,
public float Determinant(); public float Determinant();
public T? Inverse(); public T? Inverse();
public T Transpose(); public T Transpose();
public T AddRow(int rowToChange, int referenceRow, float factor = 1);
public T ScaleRow(int rowIndex, float value);
public T SwapRows(int rowA, int rowB);
//public T SolveRowEchelon();
} }
public interface IMatrix<This, TMinor> : IMatrix<This> where This : IMatrix<This, TMinor> public interface IMatrix<This, TMinor> : IMatrix<This> where This : IMatrix<This, TMinor>

View File

@ -276,6 +276,35 @@ public class Matrix : IMatrix<Matrix, Matrix>
}); });
} }
public Matrix AddRow(int rowToChange, int referenceRow, float factor = 1)
{
Matrix @this = this;
return new(Size, delegate (int r, int c)
{
if (r == rowToChange) return @this[r, c] += factor * @this[referenceRow, c];
else return @this[r, c];
});
}
public Matrix ScaleRow(int rowIndex, float factor)
{
Matrix @this = this;
return new(Size, delegate (int r, int c)
{
if (r == rowIndex) return @this[r, c] * factor;
else return @this[r, c];
});
}
public Matrix SwapRows(int rowA, int rowB)
{
Matrix @this = this;
return new(Size, delegate (int r, int c)
{
if (r == rowA) return @this[rowB, c];
else if (r == rowB) return @this[rowA, c];
else return @this[r, c];
});
}
public override bool Equals([NotNullWhen(true)] object? obj) public override bool Equals([NotNullWhen(true)] object? obj)
{ {
if (obj == null) return base.Equals(obj); if (obj == null) return base.Equals(obj);

View File

@ -241,6 +241,36 @@ public record class Matrix2x2 : IStaticMatrix<Matrix2x2>
{ r1c2, r2c2 } { r1c2, r2c2 }
}); });
public Matrix2x2 AddRow(int rowToChange, int referenceRow, float factor = 1)
{
Matrix2x2 @this = this;
return new(delegate (int r, int c)
{
if (r == rowToChange) return @this[r, c] += factor * @this[referenceRow, c];
else return @this[r, c];
});
}
public Matrix2x2 ScaleRow(int rowIndex, float factor)
{
Matrix2x2 @this = this;
return new(delegate (int r, int c)
{
if (r == rowIndex) return @this[r, c] * factor;
else return @this[r, c];
});
}
public Matrix2x2 SwapRows(int rowA, int rowB)
{
Matrix2x2 @this = this;
return new(delegate (int r, int c)
{
if (r == rowA) return @this[rowB, c];
else if (r == rowB) return @this[rowA, c];
else return @this[r, c];
});
}
public virtual bool Equals(Matrix2x2? other) public virtual bool Equals(Matrix2x2? other)
{ {
if (other is null) return false; if (other is null) return false;

View File

@ -336,6 +336,35 @@ public record class Matrix3x3 : IStaticMatrix<Matrix3x3>
{ r1c3, r2c3, r3c3 } { r1c3, r2c3, r3c3 }
}); });
public Matrix3x3 AddRow(int rowToChange, int referenceRow, float factor = 1)
{
Matrix3x3 @this = this;
return new(delegate (int r, int c)
{
if (r == rowToChange) return @this[r, c] += factor * @this[referenceRow, c];
else return @this[r, c];
});
}
public Matrix3x3 ScaleRow(int rowIndex, float factor)
{
Matrix3x3 @this = this;
return new(delegate (int r, int c)
{
if (r == rowIndex) return @this[r, c] * factor;
else return @this[r, c];
});
}
public Matrix3x3 SwapRows(int rowA, int rowB)
{
Matrix3x3 @this = this;
return new(delegate (int r, int c)
{
if (r == rowA) return @this[rowB, c];
else if (r == rowB) return @this[rowA, c];
else return @this[r, c];
});
}
public virtual bool Equals(Matrix3x3? other) public virtual bool Equals(Matrix3x3? other)
{ {
if (other is null) return false; if (other is null) return false;

View File

@ -451,6 +451,35 @@ public record class Matrix4x4 : IStaticMatrix<Matrix4x4>
{ r1c4, r2c4, r3c4, r4c4 } { r1c4, r2c4, r3c4, r4c4 }
}); });
public Matrix4x4 AddRow(int rowToChange, int referenceRow, float factor = 1)
{
Matrix4x4 @this = this;
return new(delegate (int r, int c)
{
if (r == rowToChange) return @this[r, c] += factor * @this[referenceRow, c];
else return @this[r, c];
});
}
public Matrix4x4 ScaleRow(int rowIndex, float factor)
{
Matrix4x4 @this = this;
return new(delegate (int r, int c)
{
if (r == rowIndex) return @this[r, c] * factor;
else return @this[r, c];
});
}
public Matrix4x4 SwapRows(int rowA, int rowB)
{
Matrix4x4 @this = this;
return new(delegate (int r, int c)
{
if (r == rowA) return @this[rowB, c];
else if (r == rowB) return @this[rowA, c];
else return @this[r, c];
});
}
public virtual bool Equals(Matrix4x4? other) public virtual bool Equals(Matrix4x4? other)
{ {
if (other is null) return false; if (other is null) return false;