Small matrix things. I think I'm going to start color next.

This commit is contained in:
That-One-Nerd 2025-01-13 09:40:01 -05:00
parent 0f9bcd2720
commit 27c64c4291
13 changed files with 142 additions and 6 deletions

View File

@ -18,10 +18,10 @@ namespace Nerd_STF.Helpers
{ {
matrix[byRows ? (y, x) : (x, y)] = v; matrix[byRows ? (y, x) : (x, y)] = v;
y++; y++;
if (byRows ? y >= size.y : y >= size.x) break; if (byRows ? y >= size.x : y >= size.y) break;
} }
x++; x++;
if (byRows ? x >= size.x : x >= size.y) break; if (byRows ? x >= size.y : x >= size.x) break;
} }
} }
public static void SetMatrixValues<TMat>(TMat matrix, IEnumerable<ListTuple<double>> vals, bool byRows) public static void SetMatrixValues<TMat>(TMat matrix, IEnumerable<ListTuple<double>> vals, bool byRows)

View File

@ -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>(T mat) public static Matrix FromMatrix<T>(T mat)
where T : IMatrix<T> => where T : IMatrix<T> =>
new Matrix(mat.Size, (int r, int c) => mat[r, c]); 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)]; for (int i = 0; i < size.x; i++) sum += terms[FlattenIndex(i, i)];
return sum; 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<double> GetEnumerator() public IEnumerator<double> GetEnumerator()
{ {

View File

@ -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 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) => public static explicit operator Matrix2x2(Matrix3x3 mat) =>
new Matrix2x2(mat.r0c0, mat.r0c1, new Matrix2x2(mat.r0c0, mat.r0c1,
mat.r1c0, mat.r1c1); mat.r1c0, mat.r1c1);

View File

@ -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 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) => public static implicit operator Matrix3x3(Matrix2x2 mat) =>
new Matrix3x3(mat.r0c0, mat.r0c1, 0, new Matrix3x3(mat.r0c0, mat.r0c1, 0,
mat.r1c0, mat.r1c1, 0, mat.r1c0, mat.r1c1, 0,

View File

@ -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 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) => public static implicit operator Matrix4x4(Matrix2x2 mat) =>
new Matrix4x4(1, 0 , 0 , 0, new Matrix4x4(1, 0 , 0 , 0,
0, mat.r0c0, mat.r0c1, 0, 0, mat.r0c0, mat.r0c1, 0,

View File

@ -4,6 +4,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Numerics;
namespace Nerd_STF.Mathematics namespace Nerd_STF.Mathematics
{ {
@ -260,6 +261,19 @@ namespace Nerd_STF.Mathematics
public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)})"; public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)})";
public double[] ToArray() => new double[] { x, y }; public double[] ToArray() => new double[] { x, y };
public Fill<double> 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<double> ToList() => new List<double> { x, y }; public List<double> ToList() => new List<double> { x, y };
public static Float2 operator +(Float2 a, Float2 b) => new Float2(a.x + b.x, a.y + b.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(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(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(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<double> tuple) => new Float2(tuple[0], tuple[1]); public static implicit operator Float2(ListTuple<double> tuple) => new Float2(tuple[0], tuple[1]);
public static implicit operator Float2(ListTuple<int> tuple) => new Float2(tuple[0], tuple[1]); public static implicit operator Float2(ListTuple<int> tuple) => new Float2(tuple[0], tuple[1]);
public static implicit operator Float2((double, double) tuple) => new Float2(tuple.Item1, tuple.Item2); 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 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 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 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<double>(Float2 group) => new ListTuple<double>(group.x, group.y); public static implicit operator ListTuple<double>(Float2 group) => new ListTuple<double>(group.x, group.y);
public static explicit operator ListTuple<int>(Float2 group) => new ListTuple<int>((int)group.x, (int)group.y); public static explicit operator ListTuple<int>(Float2 group) => new ListTuple<int>((int)group.x, (int)group.y);
public static implicit operator ValueTuple<double, double>(Float2 group) => (group.x, group.y); public static implicit operator ValueTuple<double, double>(Float2 group) => (group.x, group.y);

View File

@ -3,6 +3,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics;
namespace Nerd_STF.Mathematics 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 string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})";
public double[] ToArray() => new double[] { x, y, z }; public double[] ToArray() => new double[] { x, y, z };
public Fill<double> 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<double> ToList() => new List<double> { x, y, z }; public List<double> ToList() => new List<double> { 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); 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(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 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 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<double> tuple) => new Float3(tuple[0], tuple[1], tuple[2]); public static implicit operator Float3(ListTuple<double> tuple) => new Float3(tuple[0], tuple[1], tuple[2]);
public static implicit operator Float3(ListTuple<int> tuple) => new Float3(tuple[0], tuple[1], tuple[2]); public static implicit operator Float3(ListTuple<int> 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 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<double>(Float3 group) => new ListTuple<double>(group.x, group.y, group.z); public static implicit operator ListTuple<double>(Float3 group) => new ListTuple<double>(group.x, group.y, group.z);
public static explicit operator ListTuple<int>(Float3 group) => new ListTuple<int>((int)group.x, (int)group.y, (int)group.z); public static explicit operator ListTuple<int>(Float3 group) => new ListTuple<int>((int)group.x, (int)group.y, (int)group.z);
public static implicit operator ValueTuple<double, double, double>(Float3 group) => (group.x, group.y, group.z); public static implicit operator ValueTuple<double, double, double>(Float3 group) => (group.x, group.y, group.z);

View File

@ -2,6 +2,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics;
using Nerd_STF.Exceptions; using Nerd_STF.Exceptions;
namespace Nerd_STF.Mathematics 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 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 double[] ToArray() => new double[] { w, x, y, z };
public Fill<double> 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<double> ToList() => new List<double> { w, x, y, z }; public List<double> ToList() => new List<double> { 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); 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(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(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(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<double> tuple) => new Float4(tuple[0], tuple[1], tuple[2], tuple[3]); public static implicit operator Float4(ListTuple<double> tuple) => new Float4(tuple[0], tuple[1], tuple[2], tuple[3]);
public static implicit operator Float4(ListTuple<int> tuple) => new Float4(tuple[0], tuple[1], tuple[2], tuple[3]); public static implicit operator Float4(ListTuple<int> 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 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<double>(Float4 group) => new ListTuple<double>(group.w, group.x, group.y, group.z); public static implicit operator ListTuple<double>(Float4 group) => new ListTuple<double>(group.w, group.x, group.y, group.z);
public static explicit operator ListTuple<int>(Float4 group) => new ListTuple<int>((int)group.w, (int)group.x, (int)group.y, (int)group.z); public static explicit operator ListTuple<int>(Float4 group) => new ListTuple<int>((int)group.w, (int)group.x, (int)group.y, (int)group.z);
public static implicit operator ValueTuple<double, double, double, double>(Float4 group) => (group.w, group.x, group.y, group.z); public static implicit operator ValueTuple<double, double, double, double>(Float4 group) => (group.w, group.x, group.y, group.z);

View File

@ -21,6 +21,7 @@ namespace Nerd_STF.Mathematics
TItem this[int index] { get; set; } TItem this[int index] { get; set; }
TItem[] ToArray(); TItem[] ToArray();
Fill<TItem> ToFill();
List<TItem> ToList(); List<TItem> ToList();
} }
} }

View File

@ -231,6 +231,19 @@ namespace Nerd_STF.Mathematics
public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)})"; public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)})";
public int[] ToArray() => new int[] { x, y }; public int[] ToArray() => new int[] { x, y };
public Fill<int> 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<int> ToList() => new List<int> { x, y }; public List<int> ToList() => new List<int> { x, y };
public static Int2 operator +(Int2 a, Int2 b) => new Int2(a.x + b.x, a.y + b.y); public static Int2 operator +(Int2 a, Int2 b) => new Int2(a.x + b.x, a.y + b.y);

View File

@ -252,6 +252,20 @@ namespace Nerd_STF.Mathematics
public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})"; public string ToString(string format) => $"({x.ToString(format)}, {y.ToString(format)}, {z.ToString(format)})";
public int[] ToArray() => new int[] { x, y, z }; public int[] ToArray() => new int[] { x, y, z };
public Fill<int> 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<int> ToList() => new List<int> { x, y, z }; public List<int> ToList() => new List<int> { 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); public static Int3 operator +(Int3 a, Int3 b) => new Int3(a.x + b.x, a.y + b.y, a.z + b.z);

View File

@ -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 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 int[] ToArray() => new int[] { w, x, y, z };
public Fill<int> 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<int> ToList() => new List<int> { w, x, y, z }; public List<int> ToList() => new List<int> { 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); 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);

View File

@ -97,17 +97,19 @@ P.S. I know that the System library also includes `Vector2`, `Vector3`, and `Vec
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.1'"> <ItemGroup Condition="'$(TargetFramework)'=='netstandard1.1'">
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" /> <PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <!-- Version that comes with .NET has vulnerability. --> <PackageReference Include="System.Net.Http" Version="4.3.4" /> <!-- Version that comes with .NET has vulnerabilities. -->
<PackageReference Include="System.Memory" Version="4.5.5" /> <!-- Newer versions not supported. --> <PackageReference Include="System.Memory" Version="4.5.5" /> <!-- Newer versions not supported. -->
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" /> <!-- Version that comes with .NET has vulnerability. --> <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> <!-- Newer versions not supported. -->
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" /> <!-- Version that comes with .NET has vulnerabilities. -->
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.3'"> <ItemGroup Condition="'$(TargetFramework)'=='netstandard1.3'">
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" /> <PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <!-- Version that comes with .NET has vulnerability. --> <PackageReference Include="System.Net.Http" Version="4.3.4" /> <!-- Version that comes with .NET has vulnerabilities. -->
<PackageReference Include="System.Memory" Version="4.5.5" /> <!-- Newer versions not supported. --> <PackageReference Include="System.Memory" Version="4.5.5" /> <!-- Newer versions not supported. -->
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" /> <!-- Version that comes with .NET has vulnerability. --> <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" /> <!-- Newer versions not supported. -->
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" /> <!-- Version that comes with .NET has vulnerabilities. -->
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup> </ItemGroup>