Buncha changes. Removed subtract, sum, product, divide, and did other stuff.
This commit is contained in:
parent
c1570b74e1
commit
809660d58e
@ -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));
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerd_STF.Mathematics.Abstract;
|
||||
|
||||
public interface IDivide<T> : IDivisionOperators<T, T, T> where T : IDivide<T>
|
||||
{
|
||||
public static abstract T Divide(T num, params T[] vals);
|
||||
}
|
||||
@ -1,8 +1,7 @@
|
||||
namespace Nerd_STF.Mathematics.Abstract;
|
||||
|
||||
public interface IMatrix<T> : IAbsolute<T>, ICeiling<T>, IClamp<T>, IDivide<T>,
|
||||
IEquatable<T>, IFloor<T>, IGroup2d<float>, ILerp<T, float>, IProduct<T>, IRound<T>,
|
||||
ISubtract<T>, ISum<T>
|
||||
public interface IMatrix<T> : IAbsolute<T>, ICeiling<T>, IClamp<T>,
|
||||
IEquatable<T>, IFloor<T>, IGroup2d<float>, ILerp<T, float>, IRound<T>
|
||||
where T : IMatrix<T>
|
||||
{
|
||||
public Int2 Size { get; }
|
||||
@ -28,6 +27,15 @@ public interface IMatrix<T> : IAbsolute<T>, ICeiling<T>, IClamp<T>, IDivide<T>,
|
||||
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<This, TMinor> : IMatrix<This> where This : IMatrix<This, TMinor>
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerd_STF.Mathematics.Abstract;
|
||||
|
||||
public interface IProduct<T> : IMultiplyOperators<T, T, T>
|
||||
where T : IProduct<T>
|
||||
{
|
||||
public static abstract T Product(params T[] vals);
|
||||
}
|
||||
9
Nerd_STF/Mathematics/Abstract/IProjectionMatrix.cs
Normal file
9
Nerd_STF/Mathematics/Abstract/IProjectionMatrix.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Nerd_STF.Mathematics.Abstract;
|
||||
|
||||
public interface IProjectionMatrix<TThis, TBaseMatrix, TDim> : IMatrix<TThis>
|
||||
where TThis : IProjectionMatrix<TThis, TBaseMatrix, TDim>
|
||||
where TBaseMatrix : IMatrix<TBaseMatrix>
|
||||
where TDim : IGroup<float>
|
||||
{
|
||||
public Fill<TDim> Project(Fill<TDim> toProject);
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerd_STF.Mathematics.Abstract;
|
||||
|
||||
public interface ISubtract<T> : ISubtractionOperators<T, T, T> where T : ISubtract<T>
|
||||
{
|
||||
public static abstract T Subtract(T num, params T[] vals);
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerd_STF.Mathematics.Abstract;
|
||||
|
||||
public interface ISum<T> : IAdditionOperators<T, T, T>
|
||||
where T : ISum<T>
|
||||
{
|
||||
public static abstract T Sum(params T[] vals);
|
||||
}
|
||||
@ -166,35 +166,10 @@ public class Matrix : IMatrix<Matrix, Matrix>
|
||||
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)
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
namespace Nerd_STF.Mathematics.Algebra;
|
||||
|
||||
public class Matrix2x2 : IStaticMatrix<Matrix2x2>
|
||||
public class Matrix2x2 : ICloneable, IStaticMatrix<Matrix2x2>
|
||||
{
|
||||
public static Matrix2x2 Identity => new(new[,]
|
||||
{
|
||||
@ -175,11 +175,6 @@ public class Matrix2x2 : IStaticMatrix<Matrix2x2>
|
||||
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>
|
||||
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<Matrix2x2>
|
||||
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<Matrix2x2>
|
||||
yield return r2c2;
|
||||
}
|
||||
|
||||
public object Clone() => new Matrix2x2(r1c1, r1c2, r2c1, r2c2);
|
||||
|
||||
public float[] ToArray() => new[] { r1c1, r1c2, r2c1, r2c2 };
|
||||
public float[,] ToArray2D() => new[,]
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
namespace Nerd_STF.Mathematics.Algebra;
|
||||
|
||||
public class Matrix3x3 : IStaticMatrix<Matrix3x3>
|
||||
public class Matrix3x3 : ICloneable, IStaticMatrix<Matrix3x3>
|
||||
{
|
||||
public static Matrix3x3 Identity => new(new[,]
|
||||
{
|
||||
@ -268,11 +268,6 @@ public class Matrix3x3 : IStaticMatrix<Matrix3x3>
|
||||
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>
|
||||
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>
|
||||
{
|
||||
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<Matrix3x3>
|
||||
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<Matrix3x3>
|
||||
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[,]
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Nerd_STF.Mathematics.Algebra;
|
||||
|
||||
public class Matrix4x4 : IStaticMatrix<Matrix4x4>
|
||||
public class Matrix4x4 : ICloneable, IStaticMatrix<Matrix4x4>
|
||||
{
|
||||
public static Matrix4x4 Identity => new(new[,]
|
||||
{
|
||||
@ -318,11 +318,6 @@ public class Matrix4x4 : IStaticMatrix<Matrix4x4>
|
||||
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>
|
||||
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<Matrix4x4>
|
||||
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<Matrix4x4>
|
||||
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,
|
||||
|
||||
@ -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<float> fill) : this(fill(0), fill(1), fill(2),
|
||||
fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { }
|
||||
public ProjectionMatrix(Fill<int> 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<float> 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<int> 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<Float3> fill) : this(fill(0), fill(1), fill(2)) { }
|
||||
public ProjectionMatrix(Fill<Int3> fill) : this((IEnumerable<int>)fill(0), fill(1), fill(2)) { }
|
||||
public ProjectionMatrix(IEnumerable<float> r1, IEnumerable<float> r2, IEnumerable<float> r3)
|
||||
: this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { }
|
||||
public ProjectionMatrix(IEnumerable<int> r1, IEnumerable<int> r2, IEnumerable<int> r3)
|
||||
: this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { }
|
||||
public ProjectionMatrix(Fill<float> r1, Fill<float> r2, Fill<float> r3)
|
||||
: this(r1(0), r1(1), r1(2), r2(0), r2(1), r2(2), r3(0), r3(1), r3(2)) { }
|
||||
public ProjectionMatrix(Fill<int> r1, Fill<int> r2, Fill<int> 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<Float3> Project(Fill<Float3> toProject) => i => this * toProject(i);
|
||||
}
|
||||
157
Nerd_STF/Mathematics/Algebra/SimpleProjectionMatrix.cs
Normal file
157
Nerd_STF/Mathematics/Algebra/SimpleProjectionMatrix.cs
Normal file
@ -0,0 +1,157 @@
|
||||
namespace Nerd_STF.Mathematics.Algebra;
|
||||
|
||||
public class SimpleProjectionMatrix : Matrix3x3,
|
||||
IProjectionMatrix<SimpleProjectionMatrix, Matrix3x3, Float3>
|
||||
{
|
||||
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<float> fill) : this(fill(0), fill(1), fill(2),
|
||||
fill(3), fill(4), fill(5), fill(6), fill(7), fill(8)) { }
|
||||
public SimpleProjectionMatrix(Fill<int> 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<float> 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<int> 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<Float3> fill) : this(fill(0), fill(1), fill(2)) { }
|
||||
public SimpleProjectionMatrix(Fill<Int3> fill) : this((IEnumerable<int>)fill(0), fill(1), fill(2)) { }
|
||||
public SimpleProjectionMatrix(IEnumerable<float> r1, IEnumerable<float> r2, IEnumerable<float> r3)
|
||||
: this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { }
|
||||
public SimpleProjectionMatrix(IEnumerable<int> r1, IEnumerable<int> r2, IEnumerable<int> r3)
|
||||
: this(r1.ToFill(), r2.ToFill(), r3.ToFill()) { }
|
||||
public SimpleProjectionMatrix(Fill<float> r1, Fill<float> r2, Fill<float> r3)
|
||||
: this(r1(0), r1(1), r1(2), r2(0), r2(1), r2(2), r3(0), r3(1), r3(2)) { }
|
||||
public SimpleProjectionMatrix(Fill<int> r1, Fill<int> r2, Fill<int> 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<Float3> Project(Fill<Float3> 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);
|
||||
}
|
||||
@ -2,11 +2,11 @@
|
||||
|
||||
public record struct Float2 : IAbsolute<Float2>, IAverage<Float2>, ICeiling<Float2, Int2>,
|
||||
IClamp<Float2>, IClampMagnitude<Float2, float>, IComparable<Float2>,
|
||||
ICross<Float2, float>, IDivide<Float2>, IDot<Float2, float>, IEquatable<Float2>,
|
||||
ICross<Float2, float>, IDot<Float2, float>, IEquatable<Float2>,
|
||||
IFloor<Float2, Int2>, IFromTuple<Float2, (float x, float y)>, IGroup<float>,
|
||||
ILerp<Float2, float>, IMathOperators<Float2>, IMax<Float2>, IMedian<Float2>, IMin<Float2>,
|
||||
IIndexAll<float>, IIndexRangeAll<float>, IPresets2d<Float2>, IProduct<Float2>, IRound<Float2, Int2>,
|
||||
ISplittable<Float2, (float[] Xs, float[] Ys)>, ISubtract<Float2>, ISum<Float2>
|
||||
IIndexAll<float>, IIndexRangeAll<float>, IPresets2d<Float2>, IRound<Float2, Int2>,
|
||||
ISplittable<Float2, (float[] Xs, float[] Ys)>
|
||||
{
|
||||
public static Float2 Down => new(0, -1);
|
||||
public static Float2 Left => new(-1, 0);
|
||||
@ -98,7 +98,6 @@ public record struct Float2 : IAbsolute<Float2>, IAverage<Float2>, ICeiling<Floa
|
||||
}
|
||||
public static float Cross(Float2 a, Float2 b, bool normalized = false) =>
|
||||
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<Float2>, IAverage<Float2>, ICeiling<Floa
|
||||
foreach (Float2 f in vals) val = f.Magnitude < val.Magnitude ? f : val;
|
||||
return val;
|
||||
}
|
||||
public static Float2 Product(params Float2[] vals)
|
||||
{
|
||||
if (vals.Length < 1) return Zero;
|
||||
Float2 val = One;
|
||||
foreach (Float2 f in vals) val *= f;
|
||||
return val;
|
||||
}
|
||||
public static Int2 Round(Float2 val) =>
|
||||
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)
|
||||
{
|
||||
|
||||
@ -4,11 +4,11 @@ namespace Nerd_STF.Mathematics;
|
||||
|
||||
public record struct Float3 : IAbsolute<Float3>, IAverage<Float3>,
|
||||
ICeiling<Float3, Int3>, IClamp<Float3>, IClampMagnitude<Float3, float>, IComparable<Float3>,
|
||||
ICross<Float3>, IDivide<Float3>, IDot<Float3, float>, IEquatable<Float3>,
|
||||
ICross<Float3>, IDot<Float3, float>, IEquatable<Float3>,
|
||||
IFloor<Float3, Int3>, IFromTuple<Float3, (float x, float y, float z)>, IGroup<float>,
|
||||
IIndexAll<float>, IIndexRangeAll<float>, ILerp<Float3, float>, IMathOperators<Float3>, IMax<Float3>,
|
||||
IMedian<Float3>, IMin<Float3>, IPresets3d<Float3>, IProduct<Float3>, IRound<Float3, Int3>,
|
||||
ISplittable<Float3, (float[] Xs, float[] Ys, float[] Zs)>, ISubtract<Float3>, ISum<Float3>
|
||||
IMedian<Float3>, IMin<Float3>, IPresets3d<Float3>, IRound<Float3, Int3>,
|
||||
ISplittable<Float3, (float[] Xs, float[] Ys, float[] Zs)>
|
||||
{
|
||||
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<Float3>, IAverage<Float3>,
|
||||
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<Float3>, IAverage<Float3>,
|
||||
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)
|
||||
{
|
||||
|
||||
@ -2,12 +2,11 @@
|
||||
|
||||
public record struct Float4 : IAbsolute<Float4>,
|
||||
IAverage<Float4>, ICeiling<Float4, Int4>, IClamp<Float4>, IClampMagnitude<Float4, float>,
|
||||
IComparable<Float4>, IDivide<Float4>, IDot<Float4, float>, IEquatable<Float4>,
|
||||
IComparable<Float4>, IDot<Float4, float>, IEquatable<Float4>,
|
||||
IFloor<Float4, Int4>, IFromTuple<Float4, (float x, float y, float z, float w)>,
|
||||
IGroup<float>, IIndexAll<float>, IIndexRangeAll<float>, ILerp<Float4, float>, IMathOperators<Float4>,
|
||||
IMax<Float4>, IMedian<Float4>, IMin<Float4>, IPresets4d<Float4>, IProduct<Float4>, IRound<Float4, Int4>,
|
||||
ISplittable<Float4, (float[] Xs, float[] Ys, float[] Zs, float[] Ws)>, ISubtract<Float4>,
|
||||
ISum<Float4>
|
||||
IMax<Float4>, IMedian<Float4>, IMin<Float4>, IPresets4d<Float4>, IRound<Float4, Int4>,
|
||||
ISplittable<Float4, (float[] Xs, float[] Ys, float[] Zs, float[] Ws)>
|
||||
{
|
||||
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<Float4>,
|
||||
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<Float4>,
|
||||
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)
|
||||
{
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
namespace Nerd_STF.Mathematics;
|
||||
|
||||
public record struct Int2 : IAbsolute<Int2>, IAverage<Int2>, IClamp<Int2>, IClampMagnitude<Int2, int>,
|
||||
IComparable<Int2>, ICross<Int2, Int3>, IDivide<Int2>, IDot<Int2, int>, IEquatable<Int2>,
|
||||
IComparable<Int2>, ICross<Int2, Int3>, IDot<Int2, int>, IEquatable<Int2>,
|
||||
IFromTuple<Int2, (int x, int y)>, IGroup<int>, IIndexAll<int>, IIndexRangeAll<int>, ILerp<Int2, float>,
|
||||
IMathOperators<Int2>, IMax<Int2>, IMedian<Int2>, IMin<Int2>, IPresets2d<Int2>, IProduct<Int2>,
|
||||
ISplittable<Int2, (int[] Xs, int[] Ys)>, ISubtract<Int2>, ISum<Int2>
|
||||
IMathOperators<Int2>, IMax<Int2>, IMedian<Int2>, IMin<Int2>, IPresets2d<Int2>,
|
||||
ISplittable<Int2, (int[] Xs, int[] Ys)>
|
||||
{
|
||||
public static Int2 Down => new(0, -1);
|
||||
public static Int2 Left => new(-1, 0);
|
||||
@ -94,7 +94,6 @@ public record struct Int2 : IAbsolute<Int2>, IAverage<Int2>, IClamp<Int2>, 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<Int2>, IAverage<Int2>, IClamp<Int2>, 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)
|
||||
{
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
namespace Nerd_STF.Mathematics;
|
||||
|
||||
public record struct Int3 : IAbsolute<Int3>, IAverage<Int3>, IClamp<Int3>, IClampMagnitude<Int3, int>,
|
||||
IComparable<Int3>, ICross<Int3>, IDivide<Int3>, IDot<Int3, int>, IEquatable<Int3>,
|
||||
IComparable<Int3>, ICross<Int3>, IDot<Int3, int>, IEquatable<Int3>,
|
||||
IFromTuple<Int3, (int x, int y, int z)>, IGroup<int>, IIndexAll<int>, IIndexRangeAll<int>, ILerp<Int3, float>,
|
||||
IMathOperators<Int3>, IMax<Int3>, IMedian<Int3>, IMin<Int3>, IPresets3d<Int3>, IProduct<Int3>,
|
||||
ISplittable<Int3, (int[] Xs, int[] Ys, int[] Zs)>, ISubtract<Int3>, ISum<Int3>
|
||||
IMathOperators<Int3>, IMax<Int3>, IMedian<Int3>, IMin<Int3>, IPresets3d<Int3>,
|
||||
ISplittable<Int3, (int[] Xs, int[] Ys, int[] Zs)>
|
||||
{
|
||||
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<Int3>, IAverage<Int3>, IClamp<Int3>, 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<Int3>, IAverage<Int3>, IClamp<Int3>, 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)
|
||||
{
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
namespace Nerd_STF.Mathematics;
|
||||
|
||||
public record struct Int4 : IAbsolute<Int4>, IAverage<Int4>, IClamp<Int4>, IClampMagnitude<Int4, int>,
|
||||
IComparable<Int4>, IDivide<Int4>, IDot<Int4, int>, IEquatable<Int4>,
|
||||
IComparable<Int4>, IDot<Int4, int>, IEquatable<Int4>,
|
||||
IFromTuple<Int4, (int x, int y, int z, int w)>, IGroup<int>, IIndexAll<int>, IIndexRangeAll<int>,
|
||||
ILerp<Int4, float>, IMathOperators<Int4>, IMax<Int4>, IMedian<Int4>, IMin<Int4>, IPresets4d<Int4>,
|
||||
IProduct<Int4>, ISplittable<Int4, (int[] Xs, int[] Ys, int[] Zs, int[] Ws)>, ISubtract<Int4>, ISum<Int4>
|
||||
ILerp<Int4, float>, IMathOperators<Int4>, IMax<Int4>, IMedian<Int4>, IMin<Int4>, IPresets4d<Int4>,
|
||||
ISplittable<Int4, (int[] Xs, int[] Ys, int[] Zs, int[] Ws)>
|
||||
{
|
||||
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<Int4>, IAverage<Int4>, IClamp<Int4>, 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<Int4>, IAverage<Int4>, IClamp<Int4>, 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)
|
||||
{
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
namespace Nerd_STF.Mathematics.NumberSystems;
|
||||
|
||||
public record struct Complex(float u, float i) : IAbsolute<Complex>, IAverage<Complex>, ICeiling<Complex>,
|
||||
IClampMagnitude<Complex, float>, IComparable<Complex>, IDivide<Complex>, IDot<Complex, float>,
|
||||
IClampMagnitude<Complex, float>, IComparable<Complex>, IDot<Complex, float>,
|
||||
IEquatable<Complex>, IFloor<Complex>, IGroup<float>, IIndexAll<float>, IIndexRangeAll<float>,
|
||||
ILerp<Complex, float>, IMax<Complex>, IMedian<Complex>, IMin<Complex>, IPresets2d<Complex>, IProduct<Complex>,
|
||||
IRound<Complex>, ISplittable<Complex, (float[] Us, float[] Is)>, ISum<Complex>
|
||||
ILerp<Complex, float>, IMax<Complex>, IMedian<Complex>, IMin<Complex>, IPresets2d<Complex>,
|
||||
IRound<Complex>, ISplittable<Complex, (float[] Us, float[] Is)>
|
||||
{
|
||||
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<Complex>, IAverage<Co
|
||||
public static Complex Clamp(Complex val, Complex min, Complex max) => 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<Float2> 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<Complex>, IAverage<Co
|
||||
foreach (Complex c in vals) floats.Add(c);
|
||||
return Float2.Min(floats.ToArray());
|
||||
}
|
||||
public static Complex Product(params Complex[] vals)
|
||||
{
|
||||
List<Float2> 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<Float2> floats = new();
|
||||
foreach (Complex c in vals) floats.Add(c);
|
||||
return Float2.Subtract(num, floats.ToArray());
|
||||
}
|
||||
public static Complex Sum(params Complex[] vals)
|
||||
{
|
||||
List<Float2> 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)
|
||||
{
|
||||
|
||||
@ -4,10 +4,10 @@ namespace Nerd_STF.Mathematics.NumberSystems;
|
||||
|
||||
public record struct Quaternion(float u, float i, float j, float k) : IAbsolute<Quaternion>, IAverage<Quaternion>,
|
||||
ICeiling<Quaternion>, IClamp<Quaternion>, IClampMagnitude<Quaternion, float>, IComparable<Quaternion>,
|
||||
IDivide<Quaternion>, IDot<Quaternion, float>, IEquatable<Quaternion>, IFloor<Quaternion>, IGroup<float>,
|
||||
IDot<Quaternion, float>, IEquatable<Quaternion>, IFloor<Quaternion>, IGroup<float>,
|
||||
IIndexAll<float>, IIndexRangeAll<float>, ILerp<Quaternion, float>, IMax<Quaternion>, IMedian<Quaternion>,
|
||||
IMin<Quaternion>, IPresets4d<Quaternion>, IProduct<Quaternion>, IRound<Quaternion>,
|
||||
ISplittable<Quaternion, (float[] Us, float[] Is, float[] Js, float[] Ks)>, ISum<Quaternion>
|
||||
IMin<Quaternion>, IPresets4d<Quaternion>, IRound<Quaternion>,
|
||||
ISplittable<Quaternion, (float[] Us, float[] Is, float[] Js, float[] Ks)>,
|
||||
{
|
||||
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<Float4> 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<Float4> 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<Float4> floats = new();
|
||||
foreach (Quaternion q in vals) floats.Add(q);
|
||||
return Float4.Subtract(num, floats.ToArray());
|
||||
}
|
||||
public static Quaternion Sum(params Quaternion[] vals)
|
||||
{
|
||||
List<Float4> 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)
|
||||
{
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
namespace Nerd_STF.Mathematics;
|
||||
|
||||
public readonly record struct Rational : IAbsolute<Rational>, IAverage<Rational>, ICeiling<Rational, int>, IClamp<Rational>,
|
||||
IComparable<Rational>, IComparable<float>, IDivide<Rational>, IEquatable<Rational>, IEquatable<float>,
|
||||
IComparable<Rational>, IComparable<float>, IEquatable<Rational>, IEquatable<float>,
|
||||
IFloor<Rational, int>, IIndexGet<int>, IIndexRangeGet<int>, ILerp<Rational, float>, IMathOperators<Rational>,
|
||||
IMax<Rational>, IMedian<Rational>, IMin<Rational>, IPresets1d<Rational>, IProduct<Rational>,
|
||||
IRound<Rational, int>, ISplittable<Rational, (int[] nums, int[] dens)>, ISubtract<Rational>,
|
||||
ISum<Rational>
|
||||
IMax<Rational>, IMedian<Rational>, IMin<Rational>, IPresets1d<Rational>,
|
||||
IRound<Rational, int>, ISplittable<Rational, (int[] nums, int[] dens)>
|
||||
{
|
||||
public static Rational One => new(1, 1);
|
||||
public static Rational Zero => new(0, 1);
|
||||
@ -90,26 +89,10 @@ public readonly record struct Rational : IAbsolute<Rational>, IAverage<Rational>
|
||||
}
|
||||
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)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user