diff --git a/Nerd_STF/Helpers/MatrixHelper.cs b/Nerd_STF/Helpers/MatrixHelper.cs index 8f4b522..9d63174 100644 --- a/Nerd_STF/Helpers/MatrixHelper.cs +++ b/Nerd_STF/Helpers/MatrixHelper.cs @@ -18,10 +18,10 @@ namespace Nerd_STF.Helpers { matrix[byRows ? (y, x) : (x, y)] = v; y++; - if (byRows ? y >= size.y : y >= size.x) break; + if (byRows ? y >= size.x : y >= size.y) break; } x++; - if (byRows ? x >= size.x : x >= size.y) break; + if (byRows ? x >= size.y : x >= size.x) break; } } public static void SetMatrixValues(TMat matrix, IEnumerable> vals, bool byRows) diff --git a/Nerd_STF/Mathematics/Algebra/Matrix.cs b/Nerd_STF/Mathematics/Algebra/Matrix.cs index e0171d7..5545284 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix.cs @@ -148,6 +148,16 @@ namespace Nerd_STF.Mathematics.Algebra } } + public double TryGet(int r, int c) + { + if (r >= Size.x || c >= Size.y) + { + if (r == c) return 1; + else return 0; + } + else return terms[FlattenIndex(r, c)]; + } + public static Matrix FromMatrix(T mat) where T : IMatrix => new Matrix(mat.Size, (int r, int c) => mat[r, c]); @@ -296,6 +306,12 @@ namespace Nerd_STF.Mathematics.Algebra for (int i = 0; i < size.x; i++) sum += terms[FlattenIndex(i, i)]; return sum; } + public double TraceIsh() + { + double sum = 0; + for (int i = 0; i < MathE.Min(size.x, size.y); i++) sum += terms[FlattenIndex(i, i)]; + return sum; + } public IEnumerator GetEnumerator() { diff --git a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs index cf9ee3e..cf10c16 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs @@ -349,6 +349,9 @@ namespace Nerd_STF.Mathematics.Algebra public static bool operator ==(Matrix2x2 a, Matrix2x2 b) => a.Equals(b); public static bool operator !=(Matrix2x2 a, Matrix2x2 b) => !a.Equals(b); + public static explicit operator Matrix2x2(Matrix mat) => + new Matrix2x2(mat.TryGet(0, 0), mat.TryGet(0, 1), + mat.TryGet(1, 0), mat.TryGet(1, 1)); public static explicit operator Matrix2x2(Matrix3x3 mat) => new Matrix2x2(mat.r0c0, mat.r0c1, mat.r1c0, mat.r1c1); diff --git a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs index b618d2b..e04b7a6 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs @@ -442,6 +442,10 @@ namespace Nerd_STF.Mathematics.Algebra public static bool operator ==(Matrix3x3 a, Matrix3x3 b) => a.Equals(b); public static bool operator !=(Matrix3x3 a, Matrix3x3 b) => !a.Equals(b); + public static explicit operator Matrix3x3(Matrix mat) => + new Matrix3x3(mat.TryGet(0, 0), mat.TryGet(0, 1), mat.TryGet(0, 2), + mat.TryGet(1, 0), mat.TryGet(1, 1), mat.TryGet(1, 2), + mat.TryGet(2, 0), mat.TryGet(2, 1), mat.TryGet(2, 2)); public static implicit operator Matrix3x3(Matrix2x2 mat) => new Matrix3x3(mat.r0c0, mat.r0c1, 0, mat.r1c0, mat.r1c1, 0, diff --git a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs index 38a3f05..7e467f8 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs @@ -589,6 +589,11 @@ namespace Nerd_STF.Mathematics.Algebra public static bool operator ==(Matrix4x4 a, Matrix4x4 b) => a.Equals(b); public static bool operator !=(Matrix4x4 a, Matrix4x4 b) => !a.Equals(b); + public static explicit operator Matrix4x4(Matrix mat) => + new Matrix4x4(mat.TryGet(0, 0), mat.TryGet(0, 1), mat.TryGet(0, 2), mat.TryGet(0, 3), + mat.TryGet(1, 0), mat.TryGet(1, 1), mat.TryGet(1, 2), mat.TryGet(1, 3), + mat.TryGet(2, 0), mat.TryGet(2, 1), mat.TryGet(2, 2), mat.TryGet(2, 3), + mat.TryGet(3, 0), mat.TryGet(3, 1), mat.TryGet(3, 2), mat.TryGet(3, 3)); public static implicit operator Matrix4x4(Matrix2x2 mat) => new Matrix4x4(1, 0 , 0 , 0, 0, mat.r0c0, mat.r0c1, 0, diff --git a/Nerd_STF/Mathematics/Float2.cs b/Nerd_STF/Mathematics/Float2.cs index 02d4e84..db4aa53 100644 --- a/Nerd_STF/Mathematics/Float2.cs +++ b/Nerd_STF/Mathematics/Float2.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Numerics; namespace Nerd_STF.Mathematics { @@ -260,6 +261,19 @@ namespace Nerd_STF.Mathematics public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)})"; public double[] ToArray() => new double[] { x, y }; + public Fill ToFill() + { + Float2 copy = this; + return delegate (int i) + { + switch (i) + { + case 0: return copy.x; + case 1: return copy.y; + default: throw new ArgumentOutOfRangeException(nameof(i)); + } + }; + } public List ToList() => new List { x, y }; public static Float2 operator +(Float2 a, Float2 b) => new Float2(a.x + b.x, a.y + b.y); @@ -281,6 +295,9 @@ namespace Nerd_STF.Mathematics public static implicit operator Float2(PointF point) => new Float2(point.X, point.Y); public static implicit operator Float2(Size point) => new Float2(point.Width, point.Height); public static implicit operator Float2(SizeF size) => new Float2(size.Width, size.Height); + public static implicit operator Float2(Vector2 vec) => new Float2(vec.X, vec.Y); + public static explicit operator Float2(Vector3 vec) => new Float2(vec.X, vec.Y); + public static explicit operator Float2(Vector4 vec) => new Float2(vec.X, vec.Y); public static implicit operator Float2(ListTuple tuple) => new Float2(tuple[0], tuple[1]); public static implicit operator Float2(ListTuple tuple) => new Float2(tuple[0], tuple[1]); public static implicit operator Float2((double, double) tuple) => new Float2(tuple.Item1, tuple.Item2); @@ -289,6 +306,9 @@ namespace Nerd_STF.Mathematics public static implicit operator PointF(Float2 group) => new PointF((float)group.x, (float)group.y); public static explicit operator Size(Float2 group) => new Size((int)group.x, (int)group.y); public static implicit operator SizeF(Float2 group) => new SizeF((float)group.x, (float)group.y); + public static implicit operator Vector2(Float2 group) => new Vector2((float)group.x, (float)group.y); + public static implicit operator Vector3(Float2 group) => new Vector3((float)group.x, (float)group.y, 0); + public static implicit operator Vector4(Float2 group) => new Vector4((float)group.x, (float)group.y, 0, 0); public static implicit operator ListTuple(Float2 group) => new ListTuple(group.x, group.y); public static explicit operator ListTuple(Float2 group) => new ListTuple((int)group.x, (int)group.y); public static implicit operator ValueTuple(Float2 group) => (group.x, group.y); diff --git a/Nerd_STF/Mathematics/Float3.cs b/Nerd_STF/Mathematics/Float3.cs index 78498f9..e39a2d7 100644 --- a/Nerd_STF/Mathematics/Float3.cs +++ b/Nerd_STF/Mathematics/Float3.cs @@ -3,6 +3,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace Nerd_STF.Mathematics { @@ -287,6 +288,20 @@ namespace Nerd_STF.Mathematics public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})"; public double[] ToArray() => new double[] { x, y, z }; + public Fill ToFill() + { + Float3 copy = this; + return delegate (int i) + { + switch (i) + { + case 0: return copy.x; + case 1: return copy.y; + case 2: return copy.z; + default: throw new ArgumentOutOfRangeException(nameof(i)); + } + }; + } public List ToList() => new List { x, y, z }; public static Float3 operator +(Float3 a, Float3 b) => new Float3(a.x + b.x, a.y + b.y, a.z + b.z); @@ -304,10 +319,16 @@ namespace Nerd_STF.Mathematics public static implicit operator Float3(Int2 ints) => new Float3(ints.x, ints.y, 0); public static implicit operator Float3(Int3 ints) => new Float3(ints.x, ints.y, ints.z); public static explicit operator Float3(Int4 ints) => new Float3(ints.x, ints.y, ints.z); + public static implicit operator Float3(Vector2 vec) => new Float3(vec.X, vec.Y, 0); + public static implicit operator Float3(Vector3 vec) => new Float3(vec.X, vec.Y, vec.Z); + public static explicit operator Float3(Vector4 vec) => new Float3(vec.X, vec.Y, vec.Z); public static implicit operator Float3(ListTuple tuple) => new Float3(tuple[0], tuple[1], tuple[2]); public static implicit operator Float3(ListTuple tuple) => new Float3(tuple[0], tuple[1], tuple[2]); public static implicit operator Float3((double, double, double) tuple) => new Float3(tuple.Item1, tuple.Item2, tuple.Item3); + public static explicit operator Vector2(Float3 group) => new Vector2((float)group.x, (float)group.y); + public static implicit operator Vector3(Float3 group) => new Vector3((float)group.x, (float)group.y, (float)group.z); + public static implicit operator Vector4(Float3 group) => new Vector4((float)group.x, (float)group.y, (float)group.z, 0); public static implicit operator ListTuple(Float3 group) => new ListTuple(group.x, group.y, group.z); public static explicit operator ListTuple(Float3 group) => new ListTuple((int)group.x, (int)group.y, (int)group.z); public static implicit operator ValueTuple(Float3 group) => (group.x, group.y, group.z); diff --git a/Nerd_STF/Mathematics/Float4.cs b/Nerd_STF/Mathematics/Float4.cs index de71d73..497b6f0 100644 --- a/Nerd_STF/Mathematics/Float4.cs +++ b/Nerd_STF/Mathematics/Float4.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Numerics; using Nerd_STF.Exceptions; namespace Nerd_STF.Mathematics @@ -308,6 +309,21 @@ namespace Nerd_STF.Mathematics public string ToString(string format) => $"({w.ToString(format)}, {x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})"; public double[] ToArray() => new double[] { w, x, y, z }; + public Fill ToFill() + { + Float4 copy = this; + return delegate (int i) + { + switch (i) + { + case 0: return copy.w; + case 1: return copy.x; + case 2: return copy.y; + case 3: return copy.z; + default: throw new ArgumentOutOfRangeException(nameof(i)); + } + }; + } public List ToList() => new List { w, x, y, z }; public static Float4 operator +(Float4 a, Float4 b) => new Float4(a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z); @@ -325,10 +341,16 @@ namespace Nerd_STF.Mathematics public static implicit operator Float4(Int4 ints) => new Float4(ints.w, ints.x, ints.y, ints.z); public static implicit operator Float4(Float2 floats) => new Float4(0, floats.x, floats.y, 0); public static implicit operator Float4(Float3 floats) => new Float4(0, floats.x, floats.y, floats.z); + public static implicit operator Float4(Vector2 vec) => new Float4(0, vec.X, vec.Y, 0); + public static implicit operator Float4(Vector3 vec) => new Float4(0, vec.X, vec.Y, vec.Z); + public static implicit operator Float4(Vector4 vec) => new Float4(vec.W, vec.X, vec.Y, vec.Z); public static implicit operator Float4(ListTuple tuple) => new Float4(tuple[0], tuple[1], tuple[2], tuple[3]); public static implicit operator Float4(ListTuple tuple) => new Float4(tuple[0], tuple[1], tuple[2], tuple[3]); public static implicit operator Float4((double, double, double, double) tuple) => new Float4(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); + public static explicit operator Vector2(Float4 group) => new Vector2((float)group.x, (float)group.y); + public static explicit operator Vector3(Float4 group) => new Vector3((float)group.x, (float)group.y, (float)group.z); + public static implicit operator Vector4(Float4 group) => new Vector4((float)group.x, (float)group.y, (float)group.z, (float)group.w); public static implicit operator ListTuple(Float4 group) => new ListTuple(group.w, group.x, group.y, group.z); public static explicit operator ListTuple(Float4 group) => new ListTuple((int)group.w, (int)group.x, (int)group.y, (int)group.z); public static implicit operator ValueTuple(Float4 group) => (group.w, group.x, group.y, group.z); diff --git a/Nerd_STF/Mathematics/INumberGroup.cs b/Nerd_STF/Mathematics/INumberGroup.cs index 42ee6e1..ac909cc 100644 --- a/Nerd_STF/Mathematics/INumberGroup.cs +++ b/Nerd_STF/Mathematics/INumberGroup.cs @@ -21,6 +21,7 @@ namespace Nerd_STF.Mathematics TItem this[int index] { get; set; } TItem[] ToArray(); + Fill ToFill(); List ToList(); } } diff --git a/Nerd_STF/Mathematics/Int2.cs b/Nerd_STF/Mathematics/Int2.cs index 6d84344..efb0fe0 100644 --- a/Nerd_STF/Mathematics/Int2.cs +++ b/Nerd_STF/Mathematics/Int2.cs @@ -231,6 +231,19 @@ namespace Nerd_STF.Mathematics public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)})"; public int[] ToArray() => new int[] { x, y }; + public Fill ToFill() + { + Int2 copy = this; + return delegate (int i) + { + switch (i) + { + case 0: return copy.x; + case 1: return copy.y; + default: throw new ArgumentOutOfRangeException(); + } + }; + } public List ToList() => new List { x, y }; public static Int2 operator +(Int2 a, Int2 b) => new Int2(a.x + b.x, a.y + b.y); diff --git a/Nerd_STF/Mathematics/Int3.cs b/Nerd_STF/Mathematics/Int3.cs index a667f75..a0592e8 100644 --- a/Nerd_STF/Mathematics/Int3.cs +++ b/Nerd_STF/Mathematics/Int3.cs @@ -252,6 +252,20 @@ namespace Nerd_STF.Mathematics public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})"; public int[] ToArray() => new int[] { x, y, z }; + public Fill ToFill() + { + Int3 copy = this; + return delegate (int i) + { + switch (i) + { + case 0: return copy.x; + case 1: return copy.y; + case 2: return copy.z; + default: throw new ArgumentOutOfRangeException(nameof(i)); + } + }; + } public List ToList() => new List { x, y, z }; public static Int3 operator +(Int3 a, Int3 b) => new Int3(a.x + b.x, a.y + b.y, a.z + b.z); diff --git a/Nerd_STF/Mathematics/Int4.cs b/Nerd_STF/Mathematics/Int4.cs index 9525e54..e2d4f3e 100644 --- a/Nerd_STF/Mathematics/Int4.cs +++ b/Nerd_STF/Mathematics/Int4.cs @@ -265,6 +265,21 @@ namespace Nerd_STF.Mathematics public string ToString(string format) => $"({w.ToString(format)}, {x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})"; public int[] ToArray() => new int[] { w, x, y, z }; + public Fill ToFill() + { + Int4 copy = this; + return delegate (int i) + { + switch (i) + { + case 0: return copy.w; + case 1: return copy.x; + case 2: return copy.y; + case 3: return copy.z; + default: throw new ArgumentOutOfRangeException(nameof(i)); + } + }; + } public List ToList() => new List { w, x, y, z }; public static Int4 operator +(Int4 a, Int4 b) => new Int4(a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z); diff --git a/Nerd_STF/Nerd_STF.csproj b/Nerd_STF/Nerd_STF.csproj index e168182..7b9c601 100644 --- a/Nerd_STF/Nerd_STF.csproj +++ b/Nerd_STF/Nerd_STF.csproj @@ -97,17 +97,19 @@ P.S. I know that the System library also includes `Vector2`, `Vector3`, and `Vec - + - + + - + - + +