From 809660d58e1db7795ee1a350d18d18963b4db5ca Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Mon, 11 Sep 2023 08:50:29 -0400 Subject: [PATCH] Buncha changes. Removed subtract, sum, product, divide, and did other stuff. --- Nerd_STF/Extensions/EquationExtension.cs | 48 +----- Nerd_STF/Mathematics/Abstract/IDivide.cs | 8 - Nerd_STF/Mathematics/Abstract/IMatrix.cs | 14 +- Nerd_STF/Mathematics/Abstract/IProduct.cs | 9 - .../Mathematics/Abstract/IProjectionMatrix.cs | 9 + Nerd_STF/Mathematics/Abstract/ISubtract.cs | 8 - Nerd_STF/Mathematics/Abstract/ISum.cs | 9 - Nerd_STF/Mathematics/Algebra/Matrix.cs | 25 --- Nerd_STF/Mathematics/Algebra/Matrix2x2.cs | 29 +--- Nerd_STF/Mathematics/Algebra/Matrix3x3.cs | 32 +--- Nerd_STF/Mathematics/Algebra/Matrix4x4.cs | 32 +--- .../Mathematics/Algebra/ProjectionMatrix.cs | 88 ---------- .../Algebra/SimpleProjectionMatrix.cs | 157 ++++++++++++++++++ Nerd_STF/Mathematics/Float2.cs | 21 +-- Nerd_STF/Mathematics/Float3.cs | 21 +-- Nerd_STF/Mathematics/Float4.cs | 22 +-- Nerd_STF/Mathematics/Int2.cs | 21 +-- Nerd_STF/Mathematics/Int3.cs | 21 +-- Nerd_STF/Mathematics/Int4.cs | 21 +-- Nerd_STF/Mathematics/Mathf.cs | 46 +---- Nerd_STF/Mathematics/NumberSystems/Complex.cs | 30 +--- .../Mathematics/NumberSystems/Quaternion.cs | 30 +--- Nerd_STF/Mathematics/Rational.cs | 23 +-- 23 files changed, 223 insertions(+), 501 deletions(-) delete mode 100644 Nerd_STF/Mathematics/Abstract/IDivide.cs delete mode 100644 Nerd_STF/Mathematics/Abstract/IProduct.cs create mode 100644 Nerd_STF/Mathematics/Abstract/IProjectionMatrix.cs delete mode 100644 Nerd_STF/Mathematics/Abstract/ISubtract.cs delete mode 100644 Nerd_STF/Mathematics/Abstract/ISum.cs delete mode 100644 Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs create mode 100644 Nerd_STF/Mathematics/Algebra/SimpleProjectionMatrix.cs diff --git a/Nerd_STF/Extensions/EquationExtension.cs b/Nerd_STF/Extensions/EquationExtension.cs index e533133..dd3be8e 100644 --- a/Nerd_STF/Extensions/EquationExtension.cs +++ b/Nerd_STF/Extensions/EquationExtension.cs @@ -65,14 +65,7 @@ public static class EquationExtension public static Equation Coth(this Equation equ) => x => Mathf.Coth(equ(x)); public static Equation Csch(this Equation equ) => x => Mathf.Csch(equ(x)); - public static Equation Divide(this Equation equ, params float[] dividends) => - x => Mathf.Divide(equ(x), dividends); - public static Equation Divide(this Equation equ, params Equation[] dividends) => delegate (float x) - { - float[] dividendsAtValue = new float[dividends.Length]; - for (int i = 0; i < dividends.Length; i++) dividendsAtValue[i] = dividends[i](x); - return Mathf.Divide(equ(x), dividendsAtValue); - }; + // todo: add divide, multiply, add, subtract public static Equation Factorial(this Equation equ) => x => Mathf.Factorial((int)equ(x)); @@ -115,21 +108,6 @@ public static class EquationExtension public static Equation Power(this Equation equ, float pow) => x => Mathf.Power(equ(x), pow); public static Equation Power(this Equation equ, Equation pow) => x => Mathf.Power(equ(x), pow(x)); - public static Equation Product(this Equation equ, params float[] vals) => delegate (float x) - { - float[] valsAtValue = new float[vals.Length + 1]; - valsAtValue[0] = equ(x); - for (int i = 0; i < vals.Length; i++) valsAtValue[i + 1] = vals[i]; - return Mathf.Product(valsAtValue); - }; - public static Equation Product(this Equation equ, params Equation[] vals) => delegate (float x) - { - float[] valsAtValue = new float[vals.Length + 1]; - valsAtValue[0] = equ(x); - for (int i = 0; i < vals.Length; i++) valsAtValue[i + 1] = vals[i](x); - return Mathf.Product(valsAtValue); - }; - public static Equation Root(this Equation equ, float index) => x => Mathf.Root(equ(x), index); public static Equation Root(this Equation equ, Equation index) => x => Mathf.Root(equ(x), index(x)); @@ -153,30 +131,6 @@ public static class EquationExtension public static Equation Sqrt(this Equation equ) => x => Mathf.Sqrt(equ(x)); - public static Equation Subtract(this Equation equ, params float[] vals) => - x => Mathf.Subtract(equ(x), vals); - public static Equation Subtract(this Equation equ, params Equation[] vals) => delegate (float x) - { - float[] valsAtValue = new float[vals.Length]; - for (int i = 0; i < vals.Length; i++) valsAtValue[i] = vals[i](x); - return Mathf.Subtract(equ(x), valsAtValue); - }; - - public static Equation Sum(this Equation equ, params float[] vals) => delegate (float x) - { - float[] valsAtValue = new float[vals.Length + 1]; - valsAtValue[0] = equ(x); - for (int i = 0; i < vals.Length; i++) valsAtValue[i + 1] = vals[i]; - return Mathf.Sum(valsAtValue); - }; - public static Equation Sum(this Equation equ, params Equation[] vals) => delegate (float x) - { - float[] valsAtValue = new float[vals.Length + 1]; - valsAtValue[0] = equ(x); - for (int i = 0; i < vals.Length; i++) valsAtValue[i + 1] = vals[i](x); - return Mathf.Sum(valsAtValue); - }; - public static Equation Tan(this Equation equ) => x => Mathf.Tan(equ(x)); public static Equation Tanh(this Equation equ) => x => Mathf.Tanh(equ(x)); diff --git a/Nerd_STF/Mathematics/Abstract/IDivide.cs b/Nerd_STF/Mathematics/Abstract/IDivide.cs deleted file mode 100644 index f6450bb..0000000 --- a/Nerd_STF/Mathematics/Abstract/IDivide.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Numerics; - -namespace Nerd_STF.Mathematics.Abstract; - -public interface IDivide : IDivisionOperators where T : IDivide -{ - public static abstract T Divide(T num, params T[] vals); -} diff --git a/Nerd_STF/Mathematics/Abstract/IMatrix.cs b/Nerd_STF/Mathematics/Abstract/IMatrix.cs index f664310..1bf2d33 100644 --- a/Nerd_STF/Mathematics/Abstract/IMatrix.cs +++ b/Nerd_STF/Mathematics/Abstract/IMatrix.cs @@ -1,8 +1,7 @@ namespace Nerd_STF.Mathematics.Abstract; -public interface IMatrix : IAbsolute, ICeiling, IClamp, IDivide, - IEquatable, IFloor, IGroup2d, ILerp, IProduct, IRound, - ISubtract, ISum +public interface IMatrix : IAbsolute, ICeiling, IClamp, + IEquatable, IFloor, IGroup2d, ILerp, IRound where T : IMatrix { public Int2 Size { get; } @@ -28,6 +27,15 @@ public interface IMatrix : IAbsolute, ICeiling, IClamp, IDivide, public void ScaleRowMutable(int rowIndex, float value); public T SwapRows(int rowA, int rowB); public void SwapRowsMutable(int rowA, int rowB); + + public static abstract T operator +(T a, T b); + public static abstract T? operator -(T m); + public static abstract T operator -(T a, T b); + public static abstract T operator *(T a, T b); + public static abstract T operator /(T a, T b); + public static abstract T operator ^(T a, T b); + public static abstract bool operator ==(T a, T b); + public static abstract bool operator !=(T a, T b); } public interface IMatrix : IMatrix where This : IMatrix diff --git a/Nerd_STF/Mathematics/Abstract/IProduct.cs b/Nerd_STF/Mathematics/Abstract/IProduct.cs deleted file mode 100644 index 49651ef..0000000 --- a/Nerd_STF/Mathematics/Abstract/IProduct.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Numerics; - -namespace Nerd_STF.Mathematics.Abstract; - -public interface IProduct : IMultiplyOperators - where T : IProduct -{ - public static abstract T Product(params T[] vals); -} diff --git a/Nerd_STF/Mathematics/Abstract/IProjectionMatrix.cs b/Nerd_STF/Mathematics/Abstract/IProjectionMatrix.cs new file mode 100644 index 0000000..0223894 --- /dev/null +++ b/Nerd_STF/Mathematics/Abstract/IProjectionMatrix.cs @@ -0,0 +1,9 @@ +namespace Nerd_STF.Mathematics.Abstract; + +public interface IProjectionMatrix : IMatrix + where TThis : IProjectionMatrix + where TBaseMatrix : IMatrix + where TDim : IGroup +{ + public Fill Project(Fill toProject); +} diff --git a/Nerd_STF/Mathematics/Abstract/ISubtract.cs b/Nerd_STF/Mathematics/Abstract/ISubtract.cs deleted file mode 100644 index 29daabe..0000000 --- a/Nerd_STF/Mathematics/Abstract/ISubtract.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Numerics; - -namespace Nerd_STF.Mathematics.Abstract; - -public interface ISubtract : ISubtractionOperators where T : ISubtract -{ - public static abstract T Subtract(T num, params T[] vals); -} diff --git a/Nerd_STF/Mathematics/Abstract/ISum.cs b/Nerd_STF/Mathematics/Abstract/ISum.cs deleted file mode 100644 index 1cc9d72..0000000 --- a/Nerd_STF/Mathematics/Abstract/ISum.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Numerics; - -namespace Nerd_STF.Mathematics.Abstract; - -public interface ISum : IAdditionOperators - where T : ISum -{ - public static abstract T Sum(params T[] vals); -} diff --git a/Nerd_STF/Mathematics/Algebra/Matrix.cs b/Nerd_STF/Mathematics/Algebra/Matrix.cs index 17f6c6f..02d41b7 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix.cs @@ -166,35 +166,10 @@ public class Matrix : IMatrix public static Matrix Ceiling(Matrix val) => new(val.Size, (r, c) => Mathf.Ceiling(val[r, c])); public static Matrix Clamp(Matrix val, Matrix min, Matrix max) => new(val.Size, (r, c) => Mathf.Clamp(val[r, c], min[r, c], max[r, c])); - public static Matrix Divide(Matrix num, params Matrix[] vals) - { - foreach (Matrix m in vals) num /= m; - return num; - } public static Matrix Floor(Matrix val) => new(val.Size, (r, c) => Mathf.Floor(val[r, c])); public static Matrix Lerp(Matrix a, Matrix b, float t, bool clamp = true) => new(a.Size, (r, c) => Mathf.Lerp(a[r, c], b[r, c], t, clamp)); - public static Matrix Product(params Matrix[] vals) - { - if (vals.Length < 1) throw new InvalidSizeException("Array must contain at least one matrix."); - if (!CheckSize(vals)) throw new InvalidSizeException("All matricies must be the same size."); - Matrix val = Identity(vals[0].Size); - foreach (Matrix m in vals) val *= m; - return val; - } public static Matrix Round(Matrix val) => new(val.Size, (r, c) => Mathf.Round(val[r, c])); - public static Matrix Subtract(Matrix num, params Matrix[] vals) - { - foreach (Matrix m in vals) num -= m; - return num; - } - public static Matrix Sum(params Matrix[] vals) - { - if (!CheckSize(vals)) throw new InvalidSizeException("All matricies must be the same size."); - Matrix val = Zero(vals[0].Size); - foreach (Matrix m in vals) val += m; - return val; - } public void Apply(Modifier2d modifier) { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs index 1b702d0..8025e93 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs @@ -1,6 +1,6 @@ namespace Nerd_STF.Mathematics.Algebra; -public class Matrix2x2 : IStaticMatrix +public class Matrix2x2 : ICloneable, IStaticMatrix { public static Matrix2x2 Identity => new(new[,] { @@ -175,11 +175,6 @@ public class Matrix2x2 : IStaticMatrix public static Matrix2x2 Clamp(Matrix2x2 val, Matrix2x2 min, Matrix2x2 max) => new(Mathf.Clamp(val.r1c1, min.r1c1, max.r1c1), Mathf.Clamp(val.r1c2, min.r1c2, max.r1c2), Mathf.Clamp(val.r2c1, min.r2c1, max.r2c1), Mathf.Clamp(val.r2c2, min.r2c2, max.r2c2)); - public static Matrix2x2 Divide(Matrix2x2 num, params Matrix2x2[] vals) - { - foreach (Matrix2x2 m in vals) num /= m; - return num; - } public static Matrix2x2 Floor(Matrix2x2 val) => new(Mathf.Floor(val.r1c1), Mathf.Floor(val.r1c2), Mathf.Floor(val.r2c1), Mathf.Floor(val.r2c2)); public static Matrix2x2 Lerp(Matrix2x2 a, Matrix2x2 b, float t, bool clamp = true) => @@ -191,26 +186,8 @@ public class Matrix2x2 : IStaticMatrix Matrix2x2 valA = vals[Mathf.Floor(index)], valB = vals[Mathf.Ceiling(index)]; return Average(valA, valB); } - public static Matrix2x2 Product(params Matrix2x2[] vals) - { - if (vals.Length < 1) return Zero; - Matrix2x2 val = Identity; - foreach (Matrix2x2 m in vals) val *= m; - return val; - } public static Matrix2x2 Round(Matrix2x2 val) => new(Mathf.Round(val.r1c1), Mathf.Round(val.r1c2), Mathf.Round(val.r2c1), Mathf.Round(val.r2c2)); - public static Matrix2x2 Subtract(Matrix2x2 num, params Matrix2x2[] vals) - { - foreach (Matrix2x2 m in vals) num -= m; - return num; - } - public static Matrix2x2 Sum(params Matrix2x2[] vals) - { - Matrix2x2 val = Zero; - foreach (Matrix2x2 m in vals) val += m; - return val; - } public static (float[] r1c1s, float[] r1c2s, float[] r2c1s, float[] r2c2s) SplitArray(params Matrix2x2[] vals) { @@ -318,7 +295,7 @@ public class Matrix2x2 : IStaticMatrix else if (obj is Matrix2x2 m2x2) return Equals(m2x2); return false; } - public virtual bool Equals(Matrix2x2? other) + public bool Equals(Matrix2x2? other) { if (other is null) return false; return r1c1 == other.r1c1 && r1c2 == other.r1c2 && r2c1 == other.r2c1 && r2c2 == other.r2c2; @@ -335,6 +312,8 @@ public class Matrix2x2 : IStaticMatrix yield return r2c2; } + public object Clone() => new Matrix2x2(r1c1, r1c2, r2c1, r2c2); + public float[] ToArray() => new[] { r1c1, r1c2, r2c1, r2c2 }; public float[,] ToArray2D() => new[,] { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs index f766436..312fe8e 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs @@ -1,6 +1,6 @@ namespace Nerd_STF.Mathematics.Algebra; -public class Matrix3x3 : IStaticMatrix +public class Matrix3x3 : ICloneable, IStaticMatrix { public static Matrix3x3 Identity => new(new[,] { @@ -268,11 +268,6 @@ public class Matrix3x3 : IStaticMatrix Mathf.Clamp(val.r2c2, min.r2c2, max.r2c2), Mathf.Clamp(val.r2c3, min.r2c3, max.r2c3), Mathf.Clamp(val.r3c1, min.r3c1, max.r3c1), Mathf.Clamp(val.r3c2, min.r3c2, max.r3c2), Mathf.Clamp(val.r3c3, min.r3c3, max.r3c3)); - public static Matrix3x3 Divide(Matrix3x3 num, params Matrix3x3[] vals) - { - foreach (Matrix3x3 m in vals) num /= m; - return num; - } public static Matrix3x3 Floor(Matrix3x3 val) => new(Mathf.Floor(val.r1c1), Mathf.Floor(val.r1c2), Mathf.Floor(val.r1c3), Mathf.Floor(val.r2c1), Mathf.Floor(val.r2c2), Mathf.Floor(val.r2c3), @@ -289,28 +284,10 @@ public class Matrix3x3 : IStaticMatrix Matrix3x3 valA = vals[Mathf.Floor(index)], valB = vals[Mathf.Ceiling(index)]; return Average(valA, valB); } - public static Matrix3x3 Product(params Matrix3x3[] vals) - { - if (vals.Length < 1) return Zero; - Matrix3x3 val = Identity; - foreach (Matrix3x3 m in vals) val *= m; - return val; - } public static Matrix3x3 Round(Matrix3x3 val) => new(Mathf.Round(val.r1c1), Mathf.Round(val.r1c2), Mathf.Round(val.r1c3), Mathf.Round(val.r2c1), Mathf.Round(val.r2c2), Mathf.Round(val.r2c3), Mathf.Round(val.r3c1), Mathf.Round(val.r3c2), Mathf.Round(val.r3c3)); - public static Matrix3x3 Subtract(Matrix3x3 num, params Matrix3x3[] vals) - { - foreach (Matrix3x3 m in vals) num -= m; - return num; - } - public static Matrix3x3 Sum(params Matrix3x3[] vals) - { - Matrix3x3 val = Zero; - foreach (Matrix3x3 m in vals) val += m; - return val; - } public static (float[] r1c1s, float[] r1c2s, float[] r1c3s, float[] r2c1s, float[] r2c2s, float[] r2c3s, float[] r3c1s, float[] r3c2s, float[] r3c3s) SplitArray(params Matrix3x3[] vals) @@ -338,7 +315,8 @@ public class Matrix3x3 : IStaticMatrix { Matrix3x3 dets = Zero; Matrix2x2[,] minors = Minors(); - for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) dets[r, c] = minors[r, c].Determinant(); + for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) + dets[r, c] = minors[r, c].Determinant(); return dets ^ SignGrid; } public float Determinant() @@ -441,7 +419,7 @@ public class Matrix3x3 : IStaticMatrix else if (obj is Matrix3x3 m3x3) return Equals(m3x3); return false; } - public virtual bool Equals(Matrix3x3? other) + public bool Equals(Matrix3x3? other) { if (other is null) return false; return r1c1 == other.r1c1 && r1c2 == other.r1c2 && r1c3 == other.r1c3 && @@ -468,6 +446,8 @@ public class Matrix3x3 : IStaticMatrix yield return r3c3; } + public object Clone() => new Matrix3x3(r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3); + public float[] ToArray() => new[] { r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3 }; public float[,] ToArray2D() => new[,] { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs index 2834f47..9a34a2c 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs @@ -2,7 +2,7 @@ namespace Nerd_STF.Mathematics.Algebra; -public class Matrix4x4 : IStaticMatrix +public class Matrix4x4 : ICloneable, IStaticMatrix { public static Matrix4x4 Identity => new(new[,] { @@ -318,11 +318,6 @@ public class Matrix4x4 : IStaticMatrix Mathf.Clamp(val.r3c3, min.r3c3, max.r3c3), Mathf.Clamp(val.r3c4, min.r3c4, max.r3c4), Mathf.Clamp(val.r4c1, min.r4c1, max.r4c1), Mathf.Clamp(val.r4c2, min.r4c2, max.r4c2), Mathf.Clamp(val.r4c3, min.r4c3, max.r4c3), Mathf.Clamp(val.r4c4, min.r4c4, max.r4c4)); - public static Matrix4x4 Divide(Matrix4x4 num, params Matrix4x4[] vals) - { - foreach (Matrix4x4 m in vals) num /= m; - return num; - } public static Matrix4x4 Floor(Matrix4x4 val) => new(Mathf.Floor(val.r1c1), Mathf.Floor(val.r1c2), Mathf.Floor(val.r1c3), Mathf.Floor(val.r1c4), Mathf.Floor(val.r2c1), Mathf.Floor(val.r2c2), Mathf.Floor(val.r2c3), Mathf.Floor(val.r2c4), @@ -343,29 +338,11 @@ public class Matrix4x4 : IStaticMatrix Matrix4x4 valA = vals[Mathf.Floor(index)], valB = vals[Mathf.Ceiling(index)]; return Average(valA, valB); } - public static Matrix4x4 Product(params Matrix4x4[] vals) - { - if (vals.Length < 1) return Zero; - Matrix4x4 val = Identity; - foreach (Matrix4x4 m in vals) val *= m; - return val; - } public static Matrix4x4 Round(Matrix4x4 val) => new(Mathf.Round(val.r1c1), Mathf.Round(val.r1c2), Mathf.Round(val.r1c3), Mathf.Round(val.r1c4), Mathf.Round(val.r2c1), Mathf.Round(val.r2c2), Mathf.Round(val.r2c3), Mathf.Round(val.r2c4), Mathf.Round(val.r3c1), Mathf.Round(val.r3c2), Mathf.Round(val.r3c3), Mathf.Round(val.r3c4), Mathf.Round(val.r4c1), Mathf.Round(val.r4c2), Mathf.Round(val.r4c3), Mathf.Round(val.r4c4)); - public static Matrix4x4 Subtract(Matrix4x4 num, params Matrix4x4[] vals) - { - foreach (Matrix4x4 m in vals) num -= m; - return num; - } - public static Matrix4x4 Sum(params Matrix4x4[] vals) - { - Matrix4x4 val = Zero; - foreach (Matrix4x4 m in vals) val += m; - return val; - } public static (float[] r1c1s, float[] r1c2, float[] r1c3, float[] r1c4, float[] r2c1, float[] r2c2s, float[] r2c3, float[] r2c4, float[] r3c1, float[] r3c2, float[] r3c3s, float[] r3c4, float[] r4c1, @@ -536,7 +513,7 @@ public class Matrix4x4 : IStaticMatrix else if (obj is Matrix4x4 m4x4) return Equals(m4x4); return false; } - public virtual bool Equals(Matrix4x4? other) + public bool Equals(Matrix4x4? other) { if (other is null) return false; return r1c1 == other.r1c1 && r1c2 == other.r1c2 && r1c3 == other.r1c3 && r1c4 == other.r1c4 && @@ -572,6 +549,11 @@ public class Matrix4x4 : IStaticMatrix yield return r4c4; } + public object Clone() => new Matrix4x4(r1c1, r1c2, r1c3, r1c4, + r2c1, r2c2, r2c3, r2c4, + r3c1, r3c2, r3c3, r3c4, + r4c1, r4c2, r4c3, r4c4); + public float[] ToArray() => new[] { r1c1, r2c1, r3c1, r4c1, diff --git a/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs b/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs deleted file mode 100644 index a4a8c2c..0000000 --- a/Nerd_STF/Mathematics/Algebra/ProjectionMatrix.cs +++ /dev/null @@ -1,88 +0,0 @@ -namespace Nerd_STF.Mathematics.Algebra; - -public class ProjectionMatrix : Matrix3x3 -{ - // TODO: i need to remove the record check from everything, add new equals comparitors - // and re-implement IsValidProjectionMatrix - - public ProjectionMatrix(float all) : this(all, all, all, all, all, all, all, all, all) { } - public ProjectionMatrix(float r1c1, float r1c2, float r1c3, float r2c1, - float r2c2, float r2c3, float r3c1, float r3c2, float r3c3) : - base(r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3) - { - //if (!IsValidProjectionMatrix) throw new InvalidProjectionMatrixException(this); - } - public ProjectionMatrix(float[] nums) : this(nums[0], nums[1], nums[2], - nums[3], nums[4], nums[5], nums[6], nums[7], nums[8]) { } - public ProjectionMatrix(int[] nums) : this(nums[0], nums[1], nums[2], - nums[3], nums[4], nums[5], nums[6], nums[7], nums[8]) { } - public ProjectionMatrix(Fill fill) : this(fill(0), fill(1), fill(2), - fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { } - public ProjectionMatrix(Fill fill) : this(fill(0), fill(1), fill(2), - fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { } - public ProjectionMatrix(float[,] nums) : this(nums[0, 0], nums[0, 1], nums[0, 2], - nums[1, 0], nums[1, 1], nums[1, 2], nums[2, 0], nums[2, 1], nums[2, 2]) { } - public ProjectionMatrix(int[,] nums) : this(nums[0, 0], nums[0, 1], nums[0, 2], - nums[1, 0], nums[1, 1], nums[1, 2], nums[2, 0], nums[2, 1], nums[2, 2]) { } - public ProjectionMatrix(Fill2d fill) : this(fill(0, 0), fill(0, 1), fill(0, 2), - fill(1, 0), fill(1, 1), fill(1, 2), fill(2, 0), fill(2, 1), fill(2, 2)) { } - public ProjectionMatrix(Fill2d fill) : this(fill(0, 0), fill(0, 1), fill(0, 2), - fill(1, 0), fill(1, 1), fill(1, 2), fill(2, 0), fill(2, 1), fill(2, 2)) { } - public ProjectionMatrix(Float3 r1, Float3 r2, Float3 r3) : this(r1.x, r1.y, r1.z, r2.x, r2.y, r2.z, r3.x, r3.y, r3.z) { } - public ProjectionMatrix(Fill fill) : this(fill(0), fill(1), fill(2)) { } - public ProjectionMatrix(Fill fill) : this((IEnumerable)fill(0), fill(1), fill(2)) { } - public ProjectionMatrix(IEnumerable r1, IEnumerable r2, IEnumerable r3) - : this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { } - public ProjectionMatrix(IEnumerable r1, IEnumerable r2, IEnumerable r3) - : this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { } - public ProjectionMatrix(Fill r1, Fill r2, Fill r3) - : this(r1(0), r1(1), r1(2), r2(0), r2(1), r2(2), r3(0), r3(1), r3(2)) { } - public ProjectionMatrix(Fill r1, Fill r2, Fill r3) - : this(r1(0), r1(1), r1(2), r2(0), r2(1), r2(2), r3(0), r3(1), r3(2)) { } - - public static ProjectionMatrix Orthographic(CrossSection2d section) => new(new[,] - { - { section == CrossSection2d.XY || section == CrossSection2d.ZX ? 1 : 0, 0, 0 }, - { 0, section == CrossSection2d.XY || section == CrossSection2d.YZ ? 1 : 0, 0 }, - { 0, 0, section == CrossSection2d.YZ || section == CrossSection2d.ZX ? 1 : 0 } - }); - - public static ProjectionMatrix Isometric() - { - const float invSqrt2 = 0.707106781187f, - invSqrt3 = 0.57735026919f, - invSqrt6 = 0.408248290464f, - invSqrt6_2 = 0.816496580928f; - return new(new[,] - { - { invSqrt2, 0, -invSqrt2 }, - { invSqrt6, invSqrt6_2, invSqrt6 }, - { 0, 0, 0 }, - }); - } - public static ProjectionMatrix Isometric(Angle alpha, Angle beta) - { - Matrix3x3 alphaMat = new(new[,] - { - { 1, 0, 0 }, - { 0, Mathf.Cos(alpha), Mathf.Sin(alpha) }, - { 0, -Mathf.Sin(alpha), Mathf.Cos(alpha) } - }); - Matrix3x3 betaMat = new(new[,] - { - { Mathf.Cos(beta), 0, -Mathf.Sin(beta) }, - { 0, 1, 0 }, - { Mathf.Sin(beta), 0, Mathf.Cos(beta) } - }); - Matrix3x3 flatten = new(new[,] - { - { 1, 0, 0 }, - { 0, 1, 0 }, - { 0, 0, 0 } - }); - Matrix3x3 result = (alphaMat * betaMat).Transpose() * flatten; - return new(result.Transpose().ToFill2D()); - } - - public Fill Project(Fill toProject) => i => this * toProject(i); -} diff --git a/Nerd_STF/Mathematics/Algebra/SimpleProjectionMatrix.cs b/Nerd_STF/Mathematics/Algebra/SimpleProjectionMatrix.cs new file mode 100644 index 0000000..7131308 --- /dev/null +++ b/Nerd_STF/Mathematics/Algebra/SimpleProjectionMatrix.cs @@ -0,0 +1,157 @@ +namespace Nerd_STF.Mathematics.Algebra; + +public class SimpleProjectionMatrix : Matrix3x3, + IProjectionMatrix +{ + public SimpleProjectionMatrix(Matrix3x3 nonProjection) : this(nonProjection.ToFill2D()) { } + public SimpleProjectionMatrix(float all) : this(all, all, all, all, all, all, all, all, all) { } + public SimpleProjectionMatrix(float r1c1, float r1c2, float r1c3, float r2c1, + float r2c2, float r2c3, float r3c1, float r3c2, float r3c3) : + base(r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3) { } + public SimpleProjectionMatrix(float[] nums) : this(nums[0], nums[1], nums[2], + nums[3], nums[4], nums[5], nums[6], nums[7], nums[8]) { } + public SimpleProjectionMatrix(int[] nums) : this(nums[0], nums[1], nums[2], + nums[3], nums[4], nums[5], nums[6], nums[7], nums[8]) { } + public SimpleProjectionMatrix(Fill fill) : this(fill(0), fill(1), fill(2), + fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { } + public SimpleProjectionMatrix(Fill fill) : this(fill(0), fill(1), fill(2), + fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { } + public SimpleProjectionMatrix(float[,] nums) : this(nums[0, 0], nums[0, 1], nums[0, 2], + nums[1, 0], nums[1, 1], nums[1, 2], nums[2, 0], nums[2, 1], nums[2, 2]) { } + public SimpleProjectionMatrix(int[,] nums) : this(nums[0, 0], nums[0, 1], nums[0, 2], + nums[1, 0], nums[1, 1], nums[1, 2], nums[2, 0], nums[2, 1], nums[2, 2]) { } + public SimpleProjectionMatrix(Fill2d fill) : this(fill(0, 0), fill(0, 1), fill(0, 2), + fill(1, 0), fill(1, 1), fill(1, 2), fill(2, 0), fill(2, 1), fill(2, 2)) { } + public SimpleProjectionMatrix(Fill2d fill) : this(fill(0, 0), fill(0, 1), fill(0, 2), + fill(1, 0), fill(1, 1), fill(1, 2), fill(2, 0), fill(2, 1), fill(2, 2)) { } + public SimpleProjectionMatrix(Float3 r1, Float3 r2, Float3 r3) : this(r1.x, r1.y, r1.z, r2.x, r2.y, r2.z, r3.x, r3.y, r3.z) { } + public SimpleProjectionMatrix(Fill fill) : this(fill(0), fill(1), fill(2)) { } + public SimpleProjectionMatrix(Fill fill) : this((IEnumerable)fill(0), fill(1), fill(2)) { } + public SimpleProjectionMatrix(IEnumerable r1, IEnumerable r2, IEnumerable r3) + : this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { } + public SimpleProjectionMatrix(IEnumerable r1, IEnumerable r2, IEnumerable r3) + : this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { } + public SimpleProjectionMatrix(Fill r1, Fill r2, Fill r3) + : this(r1(0), r1(1), r1(2), r2(0), r2(1), r2(2), r3(0), r3(1), r3(2)) { } + public SimpleProjectionMatrix(Fill r1, Fill r2, Fill r3) + : this(r1(0), r1(1), r1(2), r2(0), r2(1), r2(2), r3(0), r3(1), r3(2)) { } + + public static SimpleProjectionMatrix SingleView(CrossSection2d section) => new(new[,] + { + { section == CrossSection2d.XY || section == CrossSection2d.ZX ? 1 : 0, 0, 0 }, + { 0, section == CrossSection2d.XY || section == CrossSection2d.YZ ? 1 : 0, 0 }, + { 0, 0, section == CrossSection2d.YZ || section == CrossSection2d.ZX ? 1 : 0 } + }); + + public static SimpleProjectionMatrix Isometric() + { + // Hand-calculated optimization of an axonometric projection + // with alpha set to arcsin(tan(30deg)) and beta set to 45deg. + + const float invSqrt2 = 0.707106781187f, + invSqrt6 = 0.408248290464f, + invSqrt6_2 = 0.816496580928f; + return new(new[,] + { + { invSqrt2, 0, -invSqrt2 }, + { invSqrt6, invSqrt6_2, invSqrt6 }, + { 0, 0, 0 }, + }); + } + public static SimpleProjectionMatrix Axonometric(Angle alpha, Angle beta) + { + Matrix3x3 alphaMat = new(new[,] + { + { 1, 0, 0 }, + { 0, Mathf.Cos(alpha), Mathf.Sin(alpha) }, + { 0, -Mathf.Sin(alpha), Mathf.Cos(alpha) } + }); + Matrix3x3 betaMat = new(new[,] + { + { Mathf.Cos(beta), 0, -Mathf.Sin(beta) }, + { 0, 1, 0 }, + { Mathf.Sin(beta), 0, Mathf.Cos(beta) } + }); + Matrix3x3 flatten = new(new[,] + { + { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 0, 0 } + }); + Matrix3x3 result = (alphaMat * betaMat).Transpose() * flatten; + return new(result.Transpose().ToFill2D()); + } + + public Fill Project(Fill toProject) => i => this * toProject(i); + + public static SimpleProjectionMatrix Absolute(SimpleProjectionMatrix val) => + new(Matrix3x3.Absolute(val)); + public static SimpleProjectionMatrix Average(SimpleProjectionMatrix val) => + new(Matrix3x3.Average(val)); + public static SimpleProjectionMatrix Ceiling(SimpleProjectionMatrix val) => + new(Matrix3x3.Ceiling(val)); + public static SimpleProjectionMatrix Clamp(SimpleProjectionMatrix val, + SimpleProjectionMatrix min, SimpleProjectionMatrix max) => + new(Matrix3x3.Clamp(val, min, max)); + public static SimpleProjectionMatrix Floor(SimpleProjectionMatrix val) => + new(Matrix3x3.Floor(val)); + public static SimpleProjectionMatrix Lerp(SimpleProjectionMatrix a, SimpleProjectionMatrix b, + float t, bool clamp = true) => + new(Matrix3x3.Lerp(a, b, t, clamp)); + public static SimpleProjectionMatrix Median(params SimpleProjectionMatrix[] vals) => + new(Matrix3x3.Median(vals)); + public static SimpleProjectionMatrix Round(SimpleProjectionMatrix val) => + new(Matrix3x3.Round(val)); + + public static (float[] r1c1s, float[] r1c2s, float[] r1c3s, float[] r2c1s, float[] r2c2s, + float[] r2c3s, float[] r3c1s, float[] r3c2s, float[] r3c3s) + SplitArray(params SimpleProjectionMatrix[] vals) => Matrix3x3.SplitArray(vals); + + new public SimpleProjectionMatrix Adjugate() => new(base.Adjugate()); + new public SimpleProjectionMatrix Cofactor() => new(base.Cofactor()); + new public SimpleProjectionMatrix? Inverse() + { + Matrix3x3? mInverse = base.Inverse(); + if (mInverse is null) return null; + return new(mInverse); + } + new public Matrix2x2[,] Minors() => base.Minors(); + new public SimpleProjectionMatrix Transpose() => new(base.Transpose()); + + new public SimpleProjectionMatrix AddRow(int rowToChange, int referenceRow, float factor = 1) => + new(base.AddRow(rowToChange, referenceRow, factor)); + new public SimpleProjectionMatrix ScaleRow(int rowIndex, float factor) => + new(base.ScaleRow(rowIndex, factor)); + new public SimpleProjectionMatrix SwapRows(int rowA, int rowB) => + new(base.SwapRows(rowA, rowB)); + + public override bool Equals(object? obj) => base.Equals(obj); + public override int GetHashCode() => base.GetHashCode(); + public bool Equals(SimpleProjectionMatrix? other) => base.Equals(other); + + new public object Clone() => new SimpleProjectionMatrix(r1c1, r1c2, r1c3, + r2c1, r2c2, r2c3, + r3c1, r3c2, r3c3); + + public static SimpleProjectionMatrix operator +(SimpleProjectionMatrix a, + SimpleProjectionMatrix b) => new((Matrix3x3)a + b); + public static SimpleProjectionMatrix? operator -(SimpleProjectionMatrix m) => m.Inverse(); + public static SimpleProjectionMatrix operator -(SimpleProjectionMatrix a, + SimpleProjectionMatrix b) => new((Matrix3x3)a - b); + public static SimpleProjectionMatrix operator *(SimpleProjectionMatrix a, float b) => + new((Matrix3x3)a * b); + public static SimpleProjectionMatrix operator *(SimpleProjectionMatrix a, + SimpleProjectionMatrix b) => new((Matrix3x3)a * b); + public static Float3 operator *(SimpleProjectionMatrix a, Float3 b) => (Matrix3x3)a * b; + public static SimpleProjectionMatrix operator /(SimpleProjectionMatrix a, float b) => + new((Matrix3x3)a / b); + public static SimpleProjectionMatrix operator /(SimpleProjectionMatrix a, + SimpleProjectionMatrix b) => new((Matrix3x3)a / b); + public static Float3 operator /(SimpleProjectionMatrix a, Float3 b) => (Matrix3x3)a / b; + public static SimpleProjectionMatrix operator ^(SimpleProjectionMatrix a, + SimpleProjectionMatrix b) => new((Matrix3x3)a ^ b); + public static bool operator ==(SimpleProjectionMatrix a, SimpleProjectionMatrix b) => + a.Equals(b); + public static bool operator !=(SimpleProjectionMatrix a, SimpleProjectionMatrix b) => + !a.Equals(b); +} diff --git a/Nerd_STF/Mathematics/Float2.cs b/Nerd_STF/Mathematics/Float2.cs index e43d253..72f26b3 100644 --- a/Nerd_STF/Mathematics/Float2.cs +++ b/Nerd_STF/Mathematics/Float2.cs @@ -2,11 +2,11 @@ public record struct Float2 : IAbsolute, IAverage, ICeiling, IClamp, IClampMagnitude, IComparable, - ICross, IDivide, IDot, IEquatable, + ICross, IDot, IEquatable, IFloor, IFromTuple, IGroup, ILerp, IMathOperators, IMax, IMedian, IMin, - IIndexAll, IIndexRangeAll, IPresets2d, IProduct, IRound, - ISplittable, ISubtract, ISum + IIndexAll, IIndexRangeAll, IPresets2d, IRound, + ISplittable { public static Float2 Down => new(0, -1); public static Float2 Left => new(-1, 0); @@ -98,7 +98,6 @@ public record struct Float2 : IAbsolute, IAverage, ICeiling Float3.Cross(a, b, normalized).z; - public static Float2 Divide(Float2 num, params Float2[] vals) => num / Product(vals); public static float Dot(Float2 a, Float2 b) => a.x * b.x + a.y * b.y; public static float Dot(params Float2[] vals) { @@ -135,22 +134,8 @@ public record struct Float2 : IAbsolute, IAverage, ICeiling new(Mathf.RoundInt(val.x), Mathf.RoundInt(val.y)); - public static Float2 Subtract(Float2 num, params Float2[] vals) => num - Sum(vals); - public static Float2 Sum(params Float2[] vals) - { - Float2 val = Zero; - foreach (Float2 f in vals) val += f; - return val; - } public static (float[] Xs, float[] Ys) SplitArray(params Float2[] vals) { diff --git a/Nerd_STF/Mathematics/Float3.cs b/Nerd_STF/Mathematics/Float3.cs index 72a48d7..72d6d01 100644 --- a/Nerd_STF/Mathematics/Float3.cs +++ b/Nerd_STF/Mathematics/Float3.cs @@ -4,11 +4,11 @@ namespace Nerd_STF.Mathematics; public record struct Float3 : IAbsolute, IAverage, ICeiling, IClamp, IClampMagnitude, IComparable, - ICross, IDivide, IDot, IEquatable, + ICross, IDot, IEquatable, IFloor, IFromTuple, IGroup, IIndexAll, IIndexRangeAll, ILerp, IMathOperators, IMax, - IMedian, IMin, IPresets3d, IProduct, IRound, - ISplittable, ISubtract, ISum + IMedian, IMin, IPresets3d, IRound, + ISplittable { public static Float3 Back => new(0, 0, -1); public static Float3 Down => new(0, -1, 0); @@ -144,7 +144,6 @@ public record struct Float3 : IAbsolute, IAverage, a.x * b.y - b.x * a.y); return normalized ? val.Normalized : val; } - public static Float3 Divide(Float3 num, params Float3[] vals) => num / Product(vals); public static float Dot(Float3 a, Float3 b) => a.x * b.x + a.y * b.y + a.z * b.z; public static float Dot(params Float3[] vals) { @@ -182,22 +181,8 @@ public record struct Float3 : IAbsolute, IAverage, foreach (Float3 d in vals) val = d.Magnitude < val.Magnitude ? d : val; return val; } - public static Float3 Product(params Float3[] vals) - { - if (vals.Length < 1) return Zero; - Float3 val = One; - foreach (Float3 d in vals) val *= d; - return val; - } public static Int3 Round(Float3 val) => new(Mathf.RoundInt(val.x), Mathf.RoundInt(val.y), Mathf.RoundInt(val.z)); - public static Float3 Subtract(Float3 num, params Float3[] vals) => num - Sum(vals); - public static Float3 Sum(params Float3[] vals) - { - Float3 val = Zero; - foreach (Float3 d in vals) val += d; - return val; - } public static (float[] Xs, float[] Ys, float[] Zs) SplitArray(params Float3[] vals) { diff --git a/Nerd_STF/Mathematics/Float4.cs b/Nerd_STF/Mathematics/Float4.cs index 3107213..17e8482 100644 --- a/Nerd_STF/Mathematics/Float4.cs +++ b/Nerd_STF/Mathematics/Float4.cs @@ -2,12 +2,11 @@ public record struct Float4 : IAbsolute, IAverage, ICeiling, IClamp, IClampMagnitude, - IComparable, IDivide, IDot, IEquatable, + IComparable, IDot, IEquatable, IFloor, IFromTuple, IGroup, IIndexAll, IIndexRangeAll, ILerp, IMathOperators, - IMax, IMedian, IMin, IPresets4d, IProduct, IRound, - ISplittable, ISubtract, - ISum + IMax, IMedian, IMin, IPresets4d, IRound, + ISplittable { public static Float4 Back => new(0, 0, -1, 0); public static Float4 Down => new(0, -1, 0, 0); @@ -214,7 +213,6 @@ public record struct Float4 : IAbsolute, else if (mag > maxMag) val *= maxMag; return val; } - public static Float4 Divide(Float4 num, params Float4[] vals) => num / Product(vals); public static float Dot(Float4 a, Float4 b) => a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; public static float Dot(params Float4[] vals) { @@ -257,20 +255,6 @@ public record struct Float4 : IAbsolute, public static Int4 Round(Float4 val) => new(Mathf.RoundInt(val.x), Mathf.RoundInt(val.y), Mathf.RoundInt(val.z), Mathf.RoundInt(val.w)); - public static Float4 Product(params Float4[] vals) - { - if (vals.Length < 1) return Zero; - Float4 val = One; - foreach (Float4 d in vals) val *= d; - return val; - } - public static Float4 Subtract(Float4 num, params Float4[] vals) => num - Sum(vals); - public static Float4 Sum(params Float4[] vals) - { - Float4 val = Zero; - foreach (Float4 d in vals) val += d; - return val; - } public static (float[] Xs, float[] Ys, float[] Zs, float[] Ws) SplitArray(params Float4[] vals) { diff --git a/Nerd_STF/Mathematics/Int2.cs b/Nerd_STF/Mathematics/Int2.cs index 094892f..440027d 100644 --- a/Nerd_STF/Mathematics/Int2.cs +++ b/Nerd_STF/Mathematics/Int2.cs @@ -1,10 +1,10 @@ namespace Nerd_STF.Mathematics; public record struct Int2 : IAbsolute, IAverage, IClamp, IClampMagnitude, - IComparable, ICross, IDivide, IDot, IEquatable, + IComparable, ICross, IDot, IEquatable, IFromTuple, IGroup, IIndexAll, IIndexRangeAll, ILerp, - IMathOperators, IMax, IMedian, IMin, IPresets2d, IProduct, - ISplittable, ISubtract, ISum + IMathOperators, IMax, IMedian, IMin, IPresets2d, + ISplittable { public static Int2 Down => new(0, -1); public static Int2 Left => new(-1, 0); @@ -94,7 +94,6 @@ public record struct Int2 : IAbsolute, IAverage, IClamp, IClam } public static Int3 Cross(Int2 a, Int2 b, bool normalized = false) => Int3.Cross(a, b, normalized); - public static Int2 Divide(Int2 num, params Int2[] vals) => num / Product(vals); public static int Dot(Int2 a, Int2 b) => a.x * b.x + a.y * b.y; public static int Dot(params Int2[] vals) { @@ -129,20 +128,6 @@ public record struct Int2 : IAbsolute, IAverage, IClamp, IClam foreach (Int2 d in vals) val = d.Magnitude < val.Magnitude ? d : val; return val; } - public static Int2 Product(params Int2[] vals) - { - if (vals.Length < 1) return Zero; - Int2 val = One; - foreach (Int2 d in vals) val *= d; - return val; - } - public static Int2 Subtract(Int2 num, params Int2[] vals) => num - Sum(vals); - public static Int2 Sum(params Int2[] vals) - { - Int2 val = Zero; - foreach (Int2 d in vals) val += d; - return val; - } public static (int[] Xs, int[] Ys) SplitArray(params Int2[] vals) { diff --git a/Nerd_STF/Mathematics/Int3.cs b/Nerd_STF/Mathematics/Int3.cs index 705ea5d..69a10f9 100644 --- a/Nerd_STF/Mathematics/Int3.cs +++ b/Nerd_STF/Mathematics/Int3.cs @@ -3,10 +3,10 @@ namespace Nerd_STF.Mathematics; public record struct Int3 : IAbsolute, IAverage, IClamp, IClampMagnitude, - IComparable, ICross, IDivide, IDot, IEquatable, + IComparable, ICross, IDot, IEquatable, IFromTuple, IGroup, IIndexAll, IIndexRangeAll, ILerp, - IMathOperators, IMax, IMedian, IMin, IPresets3d, IProduct, - ISplittable, ISubtract, ISum + IMathOperators, IMax, IMedian, IMin, IPresets3d, + ISplittable { public static Int3 Back => new(0, 0, -1); public static Int3 Down => new(0, -1, 0); @@ -139,7 +139,6 @@ public record struct Int3 : IAbsolute, IAverage, IClamp, IClam a.x * b.y - b.x * a.y); return normalized ? val.Normalized : val; } - public static Int3 Divide(Int3 num, params Int3[] vals) => num / Product(vals); public static int Dot(Int3 a, Int3 b) => a.x * b.x + a.y * b.y + a.z * b.z; public static int Dot(params Int3[] vals) { @@ -175,20 +174,6 @@ public record struct Int3 : IAbsolute, IAverage, IClamp, IClam foreach (Int3 d in vals) val = d.Magnitude < val.Magnitude ? d : val; return val; } - public static Int3 Product(params Int3[] vals) - { - if (vals.Length < 1) return Zero; - Int3 val = One; - foreach (Int3 d in vals) val *= d; - return val; - } - public static Int3 Subtract(Int3 num, params Int3[] vals) => num - Sum(vals); - public static Int3 Sum(params Int3[] vals) - { - Int3 val = Zero; - foreach (Int3 d in vals) val += d; - return val; - } public static (int[] Xs, int[] Ys, int[] Zs) SplitArray(params Int3[] vals) { diff --git a/Nerd_STF/Mathematics/Int4.cs b/Nerd_STF/Mathematics/Int4.cs index 3fbbcff..1cc576b 100644 --- a/Nerd_STF/Mathematics/Int4.cs +++ b/Nerd_STF/Mathematics/Int4.cs @@ -1,10 +1,10 @@ namespace Nerd_STF.Mathematics; public record struct Int4 : IAbsolute, IAverage, IClamp, IClampMagnitude, - IComparable, IDivide, IDot, IEquatable, + IComparable, IDot, IEquatable, IFromTuple, IGroup, IIndexAll, IIndexRangeAll, - ILerp, IMathOperators, IMax, IMedian, IMin, IPresets4d, - IProduct, ISplittable, ISubtract, ISum + ILerp, IMathOperators, IMax, IMedian, IMin, IPresets4d, + ISplittable { public static Int4 Back => new(0, 0, -1, 0); public static Int4 Down => new(0, -1, 0, 0); @@ -208,7 +208,6 @@ public record struct Int4 : IAbsolute, IAverage, IClamp, IClam else if (mag > maxMag) val *= maxMag; return val; } - public static Int4 Divide(Int4 num, params Int4[] vals) => num / Product(vals); public static int Dot(Int4 a, Int4 b) => a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; public static int Dot(params Int4[] vals) { @@ -246,20 +245,6 @@ public record struct Int4 : IAbsolute, IAverage, IClamp, IClam foreach (Int4 d in vals) val = d.Magnitude < val.Magnitude ? d : val; return val; } - public static Int4 Product(params Int4[] vals) - { - if (vals.Length < 1) return Zero; - Int4 val = One; - foreach (Int4 d in vals) val *= d; - return val; - } - public static Int4 Subtract(Int4 num, params Int4[] vals) => num - Sum(vals); - public static Int4 Sum(params Int4[] vals) - { - Int4 val = Zero; - foreach (Int4 d in vals) val += d; - return val; - } public static (int[] Xs, int[] Ys, int[] Zs, int[] Ws) SplitArray(params Int4[] vals) { diff --git a/Nerd_STF/Mathematics/Mathf.cs b/Nerd_STF/Mathematics/Mathf.cs index 1391ff8..ba83714 100644 --- a/Nerd_STF/Mathematics/Mathf.cs +++ b/Nerd_STF/Mathematics/Mathf.cs @@ -95,9 +95,6 @@ public static class Mathf public static float Csch(float value) => 1 / Sinh(value); - public static float Divide(float val, params float[] dividends) => val / Product(dividends); - public static int Divide(int val, params int[] dividends) => val / Product(dividends); - public static float Dot(float[] a, float[] b) { if (a.Length != b.Length) throw new InvalidSizeException("Both arrays must have the same length"); @@ -337,27 +334,6 @@ public static class Mathf return factors.ToArray(); } - public static float Product(params float[] vals) - { - if (vals.Length < 1) return 0; - float val = 1; - foreach (float d in vals) val *= d; - return val; - } - public static int Product(params int[] vals) - { - if (vals.Length < 1) return 0; - int val = 1; - foreach (int i in vals) val *= i; - return val; - } - public static float Product(Equation equ, float min, float max, float step = 1) - { - float total = 1; - for (float f = min; f <= max; f += step) total *= equ(f); - return total; - } - public static float Power(float num, float pow) { if (pow == 0) return 1; @@ -515,27 +491,7 @@ public static class Mathf public static float Sqrt(float value) => SolveNewton(x => x * x - value, 1); - public static float Subtract(float num, params float[] vals) => num - Sum(vals); - public static int Subtract(int num, params int[] vals) => num - Sum(vals); - - public static float Sum(params float[] vals) - { - float val = 0; - foreach (float d in vals) val += d; - return val; - } - public static int Sum(params int[] vals) - { - int val = 0; - foreach (int i in vals) val += i; - return val; - } - public static float Sum(Equation equ, float min, float max, float step = 1) - { - float total = 0; - for (float f = min; f <= max; f += step) total += equ(f); - return total; - } + // TODO: include equation product and sum // Known as stdev public static float StandardDeviation(params float[] vals) => Sqrt(Variance(vals)); diff --git a/Nerd_STF/Mathematics/NumberSystems/Complex.cs b/Nerd_STF/Mathematics/NumberSystems/Complex.cs index e0dea16..cfdc359 100644 --- a/Nerd_STF/Mathematics/NumberSystems/Complex.cs +++ b/Nerd_STF/Mathematics/NumberSystems/Complex.cs @@ -3,10 +3,10 @@ namespace Nerd_STF.Mathematics.NumberSystems; public record struct Complex(float u, float i) : IAbsolute, IAverage, ICeiling, - IClampMagnitude, IComparable, IDivide, IDot, + IClampMagnitude, IComparable, IDot, IEquatable, IFloor, IGroup, IIndexAll, IIndexRangeAll, - ILerp, IMax, IMedian, IMin, IPresets2d, IProduct, - IRound, ISplittable, ISum + ILerp, IMax, IMedian, IMin, IPresets2d, + IRound, ISplittable { public static Complex Down => new(0, -1); public static Complex Left => new(-1, 0); @@ -86,12 +86,6 @@ public record struct Complex(float u, float i) : IAbsolute, IAverage Float2.Clamp(val, min, max); public static Complex ClampMagnitude(Complex val, float minMag, float maxMag) => Float2.ClampMagnitude(val, minMag, maxMag); - public static Complex Divide(Complex num, params Complex[] vals) - { - List floats = new(); - foreach (Complex c in vals) floats.Add(c); - return Float2.Divide(num, floats.ToArray()); - } public static float Dot(Complex a, Complex b) => Float2.Dot(a, b); public static float Dot(params Complex[] vals) { @@ -119,25 +113,7 @@ public record struct Complex(float u, float i) : IAbsolute, IAverage floats = new(); - foreach (Complex c in vals) floats.Add(c); - return Float2.Product(floats.ToArray()); - } public static Complex Round(Complex val) => Float2.Round(val); - public static Complex Subtract(Complex num, params Complex[] vals) - { - List floats = new(); - foreach (Complex c in vals) floats.Add(c); - return Float2.Subtract(num, floats.ToArray()); - } - public static Complex Sum(params Complex[] vals) - { - List floats = new(); - foreach (Complex c in vals) floats.Add(c); - return Float2.Sum(floats.ToArray()); - } public static (float[] Us, float[] Is) SplitArray(params Complex[] vals) { diff --git a/Nerd_STF/Mathematics/NumberSystems/Quaternion.cs b/Nerd_STF/Mathematics/NumberSystems/Quaternion.cs index 41e7848..ea95b02 100644 --- a/Nerd_STF/Mathematics/NumberSystems/Quaternion.cs +++ b/Nerd_STF/Mathematics/NumberSystems/Quaternion.cs @@ -4,10 +4,10 @@ namespace Nerd_STF.Mathematics.NumberSystems; public record struct Quaternion(float u, float i, float j, float k) : IAbsolute, IAverage, ICeiling, IClamp, IClampMagnitude, IComparable, - IDivide, IDot, IEquatable, IFloor, IGroup, + IDot, IEquatable, IFloor, IGroup, IIndexAll, IIndexRangeAll, ILerp, IMax, IMedian, - IMin, IPresets4d, IProduct, IRound, - ISplittable, ISum + IMin, IPresets4d, IRound, + ISplittable, { public static Quaternion Back => new(0, 0, -1, 0); public static Quaternion Down => new(0, -1, 0, 0); @@ -117,12 +117,6 @@ public record struct Quaternion(float u, float i, float j, float k) : IAbsolute< public static Quaternion Clamp(Quaternion val, Quaternion min, Quaternion max) => Float4.Clamp(val, min, max); public static Quaternion ClampMagnitude(Quaternion val, float minMag, float maxMag) => Float4.ClampMagnitude(val, minMag, maxMag); - public static Quaternion Divide(Quaternion num, params Quaternion[] vals) - { - List floats = new(); - foreach (Quaternion q in vals) floats.Add(q); - return Float4.Divide(num, floats.ToArray()); - } public static float Dot(Quaternion a, Quaternion b) => a.u * b.u + a.i * b.i + a.j * b.j + a.k * b.k; public static float Dot(params Quaternion[] vals) { @@ -150,25 +144,7 @@ public record struct Quaternion(float u, float i, float j, float k) : IAbsolute< foreach (Quaternion q in vals) floats.Add(q); return Float4.Min(floats.ToArray()); } - public static Quaternion Product(params Quaternion[] vals) - { - List floats = new(); - foreach (Quaternion q in vals) floats.Add(q); - return Float4.Product(floats.ToArray()); - } public static Quaternion Round(Quaternion val) => Float4.Round(val); - public static Quaternion Subtract(Quaternion num, params Quaternion[] vals) - { - List floats = new(); - foreach (Quaternion q in vals) floats.Add(q); - return Float4.Subtract(num, floats.ToArray()); - } - public static Quaternion Sum(params Quaternion[] vals) - { - List floats = new(); - foreach (Quaternion q in vals) floats.Add(q); - return Float4.Sum(floats.ToArray()); - } public static Quaternion FromAngles(Angle yaw, Angle pitch, Angle? roll = null) { diff --git a/Nerd_STF/Mathematics/Rational.cs b/Nerd_STF/Mathematics/Rational.cs index a7410c6..6f655a8 100644 --- a/Nerd_STF/Mathematics/Rational.cs +++ b/Nerd_STF/Mathematics/Rational.cs @@ -1,11 +1,10 @@ namespace Nerd_STF.Mathematics; public readonly record struct Rational : IAbsolute, IAverage, ICeiling, IClamp, - IComparable, IComparable, IDivide, IEquatable, IEquatable, + IComparable, IComparable, IEquatable, IEquatable, IFloor, IIndexGet, IIndexRangeGet, ILerp, IMathOperators, - IMax, IMedian, IMin, IPresets1d, IProduct, - IRound, ISplittable, ISubtract, - ISum + IMax, IMedian, IMin, IPresets1d, + IRound, ISplittable { public static Rational One => new(1, 1); public static Rational Zero => new(0, 1); @@ -90,26 +89,10 @@ public readonly record struct Rational : IAbsolute, IAverage } public static Rational Clamp(Rational val, Rational min, Rational max) => FromFloat(Mathf.Clamp(val.GetValue(), min.GetValue(), max.GetValue())); - public static Rational Divide(Rational val, params Rational[] vals) => - val / Product(vals); public static int Floor(Rational val) => val.numerator / val.denominator; public static Rational Lerp(Rational a, Rational b, float t, bool clamp = true) => FromFloat(Mathf.Lerp(a.GetValue(), b.GetValue(), t, clamp)); - public static Rational Product(params Rational[] vals) - { - Rational res = One; - foreach (Rational r in vals) res *= r; - return res; - } public static int Round(Rational r) => (int)Mathf.Round(r.numerator, r.denominator) / r.denominator; - public static Rational Subtract(Rational val, params Rational[] vals) => - val - Sum(vals); - public static Rational Sum(params Rational[] vals) - { - Rational sum = Zero; - foreach (Rational r in vals) sum += r; - return sum; - } public static (int[] nums, int[] dens) SplitArray(params Rational[] vals) {