Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ewdlop patch 30 refactor numeric types to use system.double consistently #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Add(string item)
{
byte[] baseHash = _hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(item));

// Create multiple hash functions using the double hashing technique
// Create multiple hash functions using the System.Double hashing technique
for (int i = 0; i < _hashFunctions; i++)
{
int hashValue = Math.Abs((BitConverter.ToInt32(baseHash, 0) + i * BitConverter.ToInt32(baseHash, 4)) % _size);
Expand Down
2 changes: 1 addition & 1 deletion CSharpDataStructureAndAlogrithm/Algorithm/FuzzySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Algorithm;
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
public record FuzzySet<T1, T2> where T1 : INumber<T1>, IMultiplyOperators<T1, T2, T1>
public abstract record FuzzySet<T1, T2> where T1 : INumber<T1>, IMultiplyOperators<T1, T2, T1>
where T2 : struct, INumber<T2>, IMultiplyOperators<T2,T1,T1>
{
public required FrozenSet<T1> BaseSet { get; init; }
Expand Down
19 changes: 19 additions & 0 deletions CSharpDataStructureAndAlogrithm/Algorithm/Quarternion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Algorithm;

public record Quarternion(System.Double? Real, System.Double? X, System.Double? Y, System.Double? Z) : QuarternionBase<System.Double>(Real, X, Y, Z)
{
public override double? Half() => 0.5;

public override double? NegativeOne() => -1.0;

public override System.Double? Norm()
{
if(Real is not null && X is not null && Y is not null && Z is not null)
{
return System.Math.Sqrt(Real.Value* Real.Value + X.Value * X.Value + Y.Value * Y.Value+ Z.Value * Z.Value);
}
return null;
}


}
82 changes: 82 additions & 0 deletions CSharpDataStructureAndAlogrithm/Algorithm/QuarternionBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Numerics;

namespace Algorithm;

public abstract record QuarternionBase<T>(T? Real, T? X, T? Y, T? Z) where T : struct, INumber<T>
{
/// <summary>
/// Norm of any generic type?
/// </summary>
/// public T Norm

public virtual QuarternionBase<T> ToReal => this with { X = T.Zero, Y = T.Zero, Z = T.Zero };

public virtual QuarternionBase<T> ToImaginary => this with { Real = T.Zero };

public virtual QuarternionBase<T> ToConjugate => this with { X = -X, Y = -Y, Z = -Z };

public virtual QuarternionBase<T> ToInverse()
{
T? normSquared = Real * Real + X * X + Y * Y + Z * Z;
return this with { Real = Real / normSquared, X = -X / normSquared, Y = -Y / normSquared, Z = -Z / normSquared };
}

public abstract T? Norm();
public abstract T? Half();
public abstract T? NegativeOne();


public virtual QuarternionBase<T> ToPlus => this with { Real = Real + Z, X = X - Y, Y = T.Zero, Z = T.Zero } * this with {Real = Half(), X = T.Zero, Y = T.Zero, Z = Half()};

public virtual QuarternionBase<T> ToMinus => this with {Real = Real - Z, X = X + Y, Y = T.Zero, Z = T.Zero } * this with { Real = Half(), X = T.Zero, Y = T.Zero, Z = NegativeOne() * Half() };


public static QuarternionBase<T> operator +(QuarternionBase<T> p, QuarternionBase<T> q) => p with { Real = p.Real + q.Real, X = p.X + q.X, Y = p.Y + q.Y, Z = p.Z + q.Z };

public static QuarternionBase<T> operator -(QuarternionBase<T> p, QuarternionBase<T> q) => p with { Real= p.Real - q.Real, X= p.X - q.X, Y= p.Y - q.Y, Z= p.Z - q.Z };

public static QuarternionBase<T> operator *(QuarternionBase<T> p, QuarternionBase<T> q)
{
return p with
{
Real = p.Real * q.Real - p.X * q.X - p.Y * q.Y - p.Z * q.Z,
X = p.Real * q.X + p.X * q.Real + p.Y * q.Z - p.Z * q.Y,
Y = p.Real * q.Y - p.X * q.Z + p.Y * q.Real + p.Z * q.X,
Z = p.Real * q.Z + p.X * q.Y - p.Y * q.X + p.Z * q.Real
};
}

public static QuarternionBase<T> operator *(QuarternionBase<T> p, T scalar) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

public static QuarternionBase<T> operator *(T scalar, QuarternionBase<T> p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

//public static QuarternionBase<T> operator *(QuarternionBase<T> p, System.Single scalar) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

//public static QuarternionBase<T> operator *(System.Single scalar, QuarternionBase<T> p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

//public static QuarternionBase<T> operator *(System.Int64 scalar, QuarternionBase<T> p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

//public static QuarternionBase<T> operator *(QuarternionBase<T> p, System.Int64 scalar) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

//public static QuarternionBase<T> operator *(QuarternionBase<T> p, System.Int32 scalar) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };

//public static QuarternionBase<T> operator *(System.Int32 scalar, QuarternionBase<T> p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };


public virtual T? Distance(QuarternionBase<T> q) => (this - q).Norm();

public virtual T? Dot(QuarternionBase<T> q) => Half() * (this * q.ToConjugate + q * ToConjugate).Real;

/// <summary>
/// <seealso cref="https://www.johndcook.com/blog/2012/02/15/dot-cross-and-quaternion-products/#:~:text=quaternion%20product%20%3D%20cross%20product%20%E2%88%92%20dot,what%20the%20equation%20above%20means.&text=i2%20%3D%20j2%20%3D%20k,i%2C%20j%2C%20and%20k."/>
/// </summary>
/// <param name="q"></param>
/// <returns></returns>
public virtual QuarternionBase<T>? Cross(QuarternionBase<T> q) => this with
{
Real = NegativeOne() * (X * q.X + Y * q.Y + Z * q.Z),
X = Y * q.Z - Z * q.Y,
Y = Z * q.X - X * q.Z,
Z = X * q.Y - Y * q.X
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static string ToSegments<T>(this ReadOnlyMemory<T> s, int lineLength)
return string.Join(Environment.NewLine, Enumerable.Range(0, times + 1).Select(i =>
{
string ce = s.Slice(i * lineLength, Math.Min(s.Length - i * lineLength, lineLength)).ToString();
if (ce.Length < lineLength) ce = $"{ce}{new string(Enumerable.Repeat(' ', lineLength - ce.Length).ToArray())}";
return new string(ce.Reverse().ToArray());
if (ce.Length < lineLength) ce = $"{ce}{new string([.. Enumerable.Repeat(' ', lineLength - ce.Length)])}";
return new string([.. ce.Reverse()]);
}));
}

Expand Down Expand Up @@ -77,7 +77,7 @@ public static int DamerauLevenshteinDistance(this ReadOnlyMemory<char> s1, ReadO
return d[len1, len2];
}

public static double JaccardSimilarity(this ReadOnlyMemory<char> s1, ReadOnlyMemory<char> s2)
public static System.Double JaccardSimilarity(this ReadOnlyMemory<char> s1, ReadOnlyMemory<char> s2)
{
HashSet<char> set1 = new HashSet<char>();
foreach (char c in s1.Span)
Expand All @@ -96,7 +96,7 @@ public static double JaccardSimilarity(this ReadOnlyMemory<char> s1, ReadOnlyMem
HashSet<char> union = new HashSet<char>(set1);
union.UnionWith(set2);

return (double)intersection.Count / union.Count;
return (System.Double)intersection.Count / union.Count;
}

/// <summary>
Expand All @@ -105,7 +105,7 @@ public static double JaccardSimilarity(this ReadOnlyMemory<char> s1, ReadOnlyMem
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public static double JaroDistance(this ReadOnlyMemory<char> s1, ReadOnlyMemory<char> s2)
public static System.Double JaroDistance(this ReadOnlyMemory<char> s1, ReadOnlyMemory<char> s2)
{
int len1 = s1.Length;
int len2 = s2.Length;
Expand Down Expand Up @@ -147,7 +147,7 @@ public static double JaroDistance(this ReadOnlyMemory<char> s1, ReadOnlyMemory<c
k++;
}

return ((matches / (double)len1) + (matches / (double)len2) + ((matches - transpositions / 2.0) / matches)) / 3.0;
return ((matches / (System.Double)len1) + (matches / (System.Double)len2) + ((matches - transpositions / 2.0) / matches)) / 3.0;
}

/// <summary>
Expand All @@ -156,9 +156,9 @@ public static double JaroDistance(this ReadOnlyMemory<char> s1, ReadOnlyMemory<c
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public static double JaroWinklerDistance(this ReadOnlyMemory<char> s1, ReadOnlyMemory<char> s2)
public static System.Double JaroWinklerDistance(this ReadOnlyMemory<char> s1, ReadOnlyMemory<char> s2)
{
double jaroDistance = JaroDistance(s1, s2);
System.Double jaroDistance = JaroDistance(s1, s2);

int prefixLength = 0;
for (int i = 0; i < Math.Min(s1.Length, s2.Length); i++)
Expand Down
12 changes: 6 additions & 6 deletions CSharpDataStructureAndAlogrithm/Algorithm/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static int DamerauLevenshteinDistance(this string s1, string s2)
return d[len1, len2];
}

public static double JaccardSimilarity(this string s1, string s2)
public static System.Double JaccardSimilarity(this string s1, string s2)
{
HashSet<char> set1 = new HashSet<char>(s1);
HashSet<char> set2 = new HashSet<char>(s2);
Expand All @@ -85,7 +85,7 @@ public static double JaccardSimilarity(this string s1, string s2)
HashSet<char> union = new HashSet<char>(set1);
union.UnionWith(set2);

return (double)intersection.Count / union.Count;
return (System.Double)intersection.Count / union.Count;
}

/// <summary>
Expand All @@ -94,7 +94,7 @@ public static double JaccardSimilarity(this string s1, string s2)
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public static double JaroDistance(this string s1, string s2)
public static System.Double JaroDistance(this string s1, string s2)
{
int len1 = s1.Length;
int len2 = s2.Length;
Expand Down Expand Up @@ -136,7 +136,7 @@ public static double JaroDistance(this string s1, string s2)
k++;
}

return ((matches / (double)len1) + (matches / (double)len2) + ((matches - transpositions / 2.0) / matches)) / 3.0;
return ((matches / (System.Double)len1) + (matches / (System.Double)len2) + ((matches - transpositions / 2.0) / matches)) / 3.0;
}

/// <summary>
Expand All @@ -145,9 +145,9 @@ public static double JaroDistance(this string s1, string s2)
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public static double JaroWinklerDistance(this string s1, string s2)
public static System.Double JaroWinklerDistance(this string s1, string s2)
{
double jaroDistance = JaroDistance(s1, s2);
System.Double jaroDistance = JaroDistance(s1, s2);

int prefixLength = 0;
for (int i = 0; i < Math.Min(s1.Length, s2.Length); i++)
Expand Down
82 changes: 82 additions & 0 deletions CSharpDataStructureAndAlogrithm/Algorithm/ValueQuarternion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Numerics;
using System.Runtime.InteropServices;

namespace Algorithm;

/// <summary>
/// <seealso href="https://github.com/ewdlop/System-Calls-Stuffs-Notes/blob/main/C%2B%2B/VeryBasicMath/Quaternion.cpp"/>
/// </summary>
/// <typeparam name="T"></typeparam>
//[StructLayout(LayoutKind.Explicit, Size = 16)] // 4 * 4 = 16 bytes
//[StructLayout(LayoutKind.Explicit, Size = 32)] // 8 * 4 = 32 bytes
public readonly record struct ValueQuarternion<T> where T : struct, INumber<T>
{
public ValueQuarternion()
{
Real = T.Zero;
X = T.Zero;
Y = T.Zero;
Z = T.Zero;

}

//[FieldOffset(sizeof(T))]
public T Real { get; init; } = T.Zero;
public T X { get; init; } = T.Zero;
public T Y { get; init; } = T.Zero;
public T Z { get; init; } = T.Zero;

/// <summary>
/// Norm of any generic type?
/// </summary>
/// public T Norm

public ValueQuarternion<T> ToReal => this with { X = T.Zero, Y = T.Zero, Z = T.Zero };

public ValueQuarternion<T> ToImaginary => this with { Real = T.Zero};

public static ValueQuarternion<T> operator +(ValueQuarternion<T> p, ValueQuarternion<T> q) =>
new()
{
Real = p.Real + q.Real,
X = p.X + q.X,
Y = p.Y + q.Y,
Z = p.Z + q.Z
};

public static ValueQuarternion<T> operator -(ValueQuarternion<T> p, ValueQuarternion<T> q) =>
new()
{
Real = p.Real - q.Real,
X = p.X - q.X,
Y = p.Y - q.Y,
Z = p.Z - q.Z
};

public static ValueQuarternion<T> operator *(ValueQuarternion<T> p, ValueQuarternion<T> q) =>
new()
{
Real = p.Real * q.Real - p.X * q.X - p.Y * q.Y - p.Z * q.Z,
X = p.Real * q.X + p.X * q.Real + p.Y * q.Z - p.Z * q.Y,
Y = p.Real * q.Y - p.X * q.Z + p.Y * q.Real + p.Z * q.X,
Z = p.Real * q.Z + p.X * q.Y - p.Y * q.X + p.Z * q.Real
};

public static ValueQuarternion<T> operator *(ValueQuarternion<T> p, T scalar) =>
new()
{
Real = p.Real * scalar,
X = p.X * scalar,
Y = p.Y * scalar,
Z = p.Z * scalar
};

public static ValueQuarternion<T> operator *(T scalar, ValueQuarternion<T> p) =>
new()
{
Real = p.Real * scalar,
X = p.X * scalar,
Y = p.Y * scalar,
Z = p.Z * scalar
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public ControlFlowTests()
[TestMethod()]
public void RunTest()
{
//Mental model: Random.Shared.NextDouble() is [0, 1)
//double.Epsilon is the smallest positive double value that is significant?
//Mental model: Random.Shared.NextSystem.Double() is [0, 1)
//System.Double.Epsilon is the smallest positive System.Double value that is significant?

//Math.Abs(1.0 - (1.0 + double.Epsilon) * Random.Shared.NextDouble()???
//Math.Abs(1.0 - (1.0 + System.Double.Epsilon) * Random.Shared.NextSystem.Double()???

//use double-floating point error to break the loop?
//use System.Double-floating point error to break the loop?
while (Random.Shared.NextDouble() is >= 0.0 and < 1.0)
{
if(CancellationTokenSource.Token.IsCancellationRequested)
Expand All @@ -36,7 +36,7 @@ public void RunTest()
#endif
Assert.IsFalse(Random.Shared.Next(0, 九十六) < 四十八);
Assert.IsTrue(Random.Shared.Next(0, 九十六) is >= 0 and < 九十六);
Assert.IsTrue(Random.Shared.NextDouble() is >= double.Epsilon and < 1.0);
Assert.IsTrue(Random.Shared.NextDouble() is >= System.Double.Epsilon and < 1.0);
Assert.IsFalse(Random.Shared.Next(0, 九十六) is < 0 or >= 九十六);
}
}
Expand Down
6 changes: 3 additions & 3 deletions CSharpDataStructureAndAlogrithm/DataStructure/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public class Point
{
public double X { get; set; }
public double Y { get; set; }
public System.Double X { get; set; }
public System.Double Y { get; set; }

public Point(double x, double y)
public Point(System.Double x, System.Double y)
{
X = x;
Y = y;
Expand Down
8 changes: 4 additions & 4 deletions CSharpDataStructureAndAlogrithm/DataStructure/QuadTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public bool Insert(Point point)

private void Subdivide()
{
double x = Boundary.X;
double y = Boundary.Y;
double w = Boundary.Width / 2;
double h = Boundary.Height / 2;
System.Double x = Boundary.X;
System.Double y = Boundary.Y;
System.Double w = Boundary.Width / 2;
System.Double h = Boundary.Height / 2;

Rectangle ne = new Rectangle(x + w, y - h, w, h);
NE = new QuadTreeNode(ne);
Expand Down
4 changes: 2 additions & 2 deletions CSharpDataStructureAndAlogrithm/DataStructure/RTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ private RTreeNode ChooseLeaf(RTreeNode node, Rectangle rect)
}

RTreeNode? bestChild = null;
double minAreaIncrease = double.MaxValue;
System.Double minAreaIncrease = System.Double.MaxValue;

foreach (RTreeNode child in node.Children)
{
Rectangle union = Rectangle.Union(child.Entries[0], rect);
double areaIncrease = union.Area() - child.Entries[0].Area();
System.Double areaIncrease = union.Area() - child.Entries[0].Area();
if (areaIncrease < minAreaIncrease)
{
minAreaIncrease = areaIncrease;
Expand Down
Loading