diff --git a/Changelog.md b/Changelog.md index 4440fd6..ee2d21a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,18 +2,38 @@ TODO +added row operations, fixed cofactor bugs, added setters + Here's the full changelog: ``` * Nerd_STF * Mathematics * Abstract * IMatrix + + AddRow(int, int, float) + Cofactor() + + ScaleRow(int, float) + + SwapRows(int, int) + + SolveRowEchelon() * Algebra * 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. * Matrix2x2 + + AddRow(int, int, float) + + ScaleRow(int, float) + + SwapRows(int, int) = 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 * Complex + operator Complex(SystemComplex) diff --git a/Nerd_STF/Mathematics/Abstract/IMatrix.cs b/Nerd_STF/Mathematics/Abstract/IMatrix.cs index 34974f5..cbcc7bf 100644 --- a/Nerd_STF/Mathematics/Abstract/IMatrix.cs +++ b/Nerd_STF/Mathematics/Abstract/IMatrix.cs @@ -10,6 +10,12 @@ public interface IMatrix : IAbsolute, ICeiling, IClamp, IDivide, public float Determinant(); public T? Inverse(); 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 : IMatrix where This : IMatrix diff --git a/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs b/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs index e8287b9..ea3dfd3 100644 --- a/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs +++ b/Nerd_STF/Mathematics/Abstract/IStaticMatrix.cs @@ -3,5 +3,5 @@ public interface IStaticMatrix : IAverage, IMatrix, IMedian, IMatrixPresets where T : IStaticMatrix { - + } diff --git a/Nerd_STF/Mathematics/Algebra/Matrix.cs b/Nerd_STF/Mathematics/Algebra/Matrix.cs index b48d138..09ea5c5 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix.cs @@ -276,6 +276,35 @@ public class Matrix : IMatrix }); } + 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) { if (obj == null) return base.Equals(obj); diff --git a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs index f842a94..0eb26aa 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs @@ -241,6 +241,36 @@ public record class Matrix2x2 : IStaticMatrix { 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) { if (other is null) return false; diff --git a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs index d028038..8a8d66d 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs @@ -336,6 +336,35 @@ public record class Matrix3x3 : IStaticMatrix { 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) { if (other is null) return false; diff --git a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs index 1297034..262da21 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs @@ -451,6 +451,35 @@ public record class Matrix4x4 : IStaticMatrix { 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) { if (other is null) return false;