diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/Claude/BloomFilter.cs b/CSharpDataStructureAndAlogrithm/Algorithm/Claude/BloomFilter.cs
index b6bf8cc..bae9031 100644
--- a/CSharpDataStructureAndAlogrithm/Algorithm/Claude/BloomFilter.cs
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/Claude/BloomFilter.cs
@@ -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);
diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/FuzzySet.cs b/CSharpDataStructureAndAlogrithm/Algorithm/FuzzySet.cs
index c305e05..47b909e 100644
--- a/CSharpDataStructureAndAlogrithm/Algorithm/FuzzySet.cs
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/FuzzySet.cs
@@ -10,7 +10,7 @@ namespace Algorithm;
///
///
///
-public record FuzzySet where T1 : INumber, IMultiplyOperators
+public abstract record FuzzySet where T1 : INumber, IMultiplyOperators
where T2 : struct, INumber, IMultiplyOperators
{
public required FrozenSet BaseSet { get; init; }
diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/Quarternion.cs b/CSharpDataStructureAndAlogrithm/Algorithm/Quarternion.cs
new file mode 100644
index 0000000..963a678
--- /dev/null
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/Quarternion.cs
@@ -0,0 +1,19 @@
+namespace Algorithm;
+
+public record Quarternion(System.Double? Real, System.Double? X, System.Double? Y, System.Double? Z) : QuarternionBase(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;
+ }
+
+
+}
diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/QuarternionBase.cs b/CSharpDataStructureAndAlogrithm/Algorithm/QuarternionBase.cs
new file mode 100644
index 0000000..21b68ab
--- /dev/null
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/QuarternionBase.cs
@@ -0,0 +1,82 @@
+using System.Numerics;
+
+namespace Algorithm;
+
+public abstract record QuarternionBase(T? Real, T? X, T? Y, T? Z) where T : struct, INumber
+{
+ ///
+ /// Norm of any generic type?
+ ///
+ /// public T Norm
+
+ public virtual QuarternionBase ToReal => this with { X = T.Zero, Y = T.Zero, Z = T.Zero };
+
+ public virtual QuarternionBase ToImaginary => this with { Real = T.Zero };
+
+ public virtual QuarternionBase ToConjugate => this with { X = -X, Y = -Y, Z = -Z };
+
+ public virtual QuarternionBase 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 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 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 operator +(QuarternionBase p, QuarternionBase 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 operator -(QuarternionBase p, QuarternionBase 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 operator *(QuarternionBase p, QuarternionBase 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 operator *(QuarternionBase p, T scalar) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };
+
+ public static QuarternionBase operator *(T scalar, QuarternionBase p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };
+
+ //public static QuarternionBase operator *(QuarternionBase 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 operator *(System.Single scalar, QuarternionBase p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };
+
+ //public static QuarternionBase operator *(System.Int64 scalar, QuarternionBase p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };
+
+ //public static QuarternionBase operator *(QuarternionBase 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 operator *(QuarternionBase 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 operator *(System.Int32 scalar, QuarternionBase p) => p with { Real = p.Real * scalar, X = p.X * scalar, Y = p.Y * scalar, Z = p.Z * scalar };
+
+
+ public virtual T? Distance(QuarternionBase q) => (this - q).Norm();
+
+ public virtual T? Dot(QuarternionBase q) => Half() * (this * q.ToConjugate + q * ToConjugate).Real;
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ public virtual QuarternionBase? Cross(QuarternionBase 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
+ };
+}
diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/ReadOnlyMemoryExtensions.cs b/CSharpDataStructureAndAlogrithm/Algorithm/ReadOnlyMemoryExtensions.cs
index 9c4b1cb..46219ee 100644
--- a/CSharpDataStructureAndAlogrithm/Algorithm/ReadOnlyMemoryExtensions.cs
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/ReadOnlyMemoryExtensions.cs
@@ -14,8 +14,8 @@ public static string ToSegments(this ReadOnlyMemory 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()]);
}));
}
@@ -77,7 +77,7 @@ public static int DamerauLevenshteinDistance(this ReadOnlyMemory s1, ReadO
return d[len1, len2];
}
- public static double JaccardSimilarity(this ReadOnlyMemory s1, ReadOnlyMemory s2)
+ public static System.Double JaccardSimilarity(this ReadOnlyMemory s1, ReadOnlyMemory s2)
{
HashSet set1 = new HashSet();
foreach (char c in s1.Span)
@@ -96,7 +96,7 @@ public static double JaccardSimilarity(this ReadOnlyMemory s1, ReadOnlyMem
HashSet union = new HashSet(set1);
union.UnionWith(set2);
- return (double)intersection.Count / union.Count;
+ return (System.Double)intersection.Count / union.Count;
}
///
@@ -105,7 +105,7 @@ public static double JaccardSimilarity(this ReadOnlyMemory s1, ReadOnlyMem
///
///
///
- public static double JaroDistance(this ReadOnlyMemory s1, ReadOnlyMemory s2)
+ public static System.Double JaroDistance(this ReadOnlyMemory s1, ReadOnlyMemory s2)
{
int len1 = s1.Length;
int len2 = s2.Length;
@@ -147,7 +147,7 @@ public static double JaroDistance(this ReadOnlyMemory s1, ReadOnlyMemory
@@ -156,9 +156,9 @@ public static double JaroDistance(this ReadOnlyMemory s1, ReadOnlyMemory
///
///
- public static double JaroWinklerDistance(this ReadOnlyMemory s1, ReadOnlyMemory s2)
+ public static System.Double JaroWinklerDistance(this ReadOnlyMemory s1, ReadOnlyMemory 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++)
diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/StringExtensions.cs b/CSharpDataStructureAndAlogrithm/Algorithm/StringExtensions.cs
index 946b051..b6a15eb 100644
--- a/CSharpDataStructureAndAlogrithm/Algorithm/StringExtensions.cs
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/StringExtensions.cs
@@ -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 set1 = new HashSet(s1);
HashSet set2 = new HashSet(s2);
@@ -85,7 +85,7 @@ public static double JaccardSimilarity(this string s1, string s2)
HashSet union = new HashSet(set1);
union.UnionWith(set2);
- return (double)intersection.Count / union.Count;
+ return (System.Double)intersection.Count / union.Count;
}
///
@@ -94,7 +94,7 @@ public static double JaccardSimilarity(this string s1, string s2)
///
///
///
- 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;
@@ -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;
}
///
@@ -145,9 +145,9 @@ public static double JaroDistance(this string s1, string s2)
///
///
///
- 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++)
diff --git a/CSharpDataStructureAndAlogrithm/Algorithm/ValueQuarternion.cs b/CSharpDataStructureAndAlogrithm/Algorithm/ValueQuarternion.cs
new file mode 100644
index 0000000..e26f35b
--- /dev/null
+++ b/CSharpDataStructureAndAlogrithm/Algorithm/ValueQuarternion.cs
@@ -0,0 +1,82 @@
+using System.Numerics;
+using System.Runtime.InteropServices;
+
+namespace Algorithm;
+
+///
+///
+///
+///
+//[StructLayout(LayoutKind.Explicit, Size = 16)] // 4 * 4 = 16 bytes
+//[StructLayout(LayoutKind.Explicit, Size = 32)] // 8 * 4 = 32 bytes
+public readonly record struct ValueQuarternion where T : struct, INumber
+{
+ 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;
+
+ ///
+ /// Norm of any generic type?
+ ///
+ /// public T Norm
+
+ public ValueQuarternion ToReal => this with { X = T.Zero, Y = T.Zero, Z = T.Zero };
+
+ public ValueQuarternion ToImaginary => this with { Real = T.Zero};
+
+ public static ValueQuarternion operator +(ValueQuarternion p, ValueQuarternion 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 operator -(ValueQuarternion p, ValueQuarternion 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 operator *(ValueQuarternion p, ValueQuarternion 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 operator *(ValueQuarternion p, T scalar) =>
+ new()
+ {
+ Real = p.Real * scalar,
+ X = p.X * scalar,
+ Y = p.Y * scalar,
+ Z = p.Z * scalar
+ };
+
+ public static ValueQuarternion operator *(T scalar, ValueQuarternion p) =>
+ new()
+ {
+ Real = p.Real * scalar,
+ X = p.X * scalar,
+ Y = p.Y * scalar,
+ Z = p.Z * scalar
+ };
+}
diff --git a/CSharpDataStructureAndAlogrithm/AlgorithmTests/ControlFlowTests.cs b/CSharpDataStructureAndAlogrithm/AlgorithmTests/ControlFlowTests.cs
index fbab095..fc72a8e 100644
--- a/CSharpDataStructureAndAlogrithm/AlgorithmTests/ControlFlowTests.cs
+++ b/CSharpDataStructureAndAlogrithm/AlgorithmTests/ControlFlowTests.cs
@@ -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)
@@ -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 >= 九十六);
}
}
diff --git a/CSharpDataStructureAndAlogrithm/DataStructure/Point.cs b/CSharpDataStructureAndAlogrithm/DataStructure/Point.cs
index 739bb95..c0fd95b 100644
--- a/CSharpDataStructureAndAlogrithm/DataStructure/Point.cs
+++ b/CSharpDataStructureAndAlogrithm/DataStructure/Point.cs
@@ -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;
diff --git a/CSharpDataStructureAndAlogrithm/DataStructure/QuadTreeNode.cs b/CSharpDataStructureAndAlogrithm/DataStructure/QuadTreeNode.cs
index 1def78d..e496e59 100644
--- a/CSharpDataStructureAndAlogrithm/DataStructure/QuadTreeNode.cs
+++ b/CSharpDataStructureAndAlogrithm/DataStructure/QuadTreeNode.cs
@@ -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);
diff --git a/CSharpDataStructureAndAlogrithm/DataStructure/RTree.cs b/CSharpDataStructureAndAlogrithm/DataStructure/RTree.cs
index 50c641d..ea9c1c1 100644
--- a/CSharpDataStructureAndAlogrithm/DataStructure/RTree.cs
+++ b/CSharpDataStructureAndAlogrithm/DataStructure/RTree.cs
@@ -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;
diff --git a/CSharpDataStructureAndAlogrithm/DataStructure/Rectangle.cs b/CSharpDataStructureAndAlogrithm/DataStructure/Rectangle.cs
index 36e4c4f..db6bf1e 100644
--- a/CSharpDataStructureAndAlogrithm/DataStructure/Rectangle.cs
+++ b/CSharpDataStructureAndAlogrithm/DataStructure/Rectangle.cs
@@ -2,17 +2,17 @@
public class Rectangle
{
- public double MinX => X - Width;
- public double MinY => Y - Height;
- public double MaxX => X + Width;
- public double MaxY => Y + Height;
+ public System.Double MinX => X - Width;
+ public System.Double MinY => Y - Height;
+ public System.Double MaxX => X + Width;
+ public System.Double MaxY => Y + Height;
- public double X { get; private set; }
- public double Y { get; private set; }
- public double Width { get; private set; }
- public double Height { get; private set; }
+ public System.Double X { get; private set; }
+ public System.Double Y { get; private set; }
+ public System.Double Width { get; private set; }
+ public System.Double Height { get; private set; }
- public Rectangle(double x, double y, double width, double height)
+ public Rectangle(System.Double x, System.Double y, System.Double width, System.Double height)
{
X = x;
Y = y;
@@ -28,7 +28,7 @@ public Rectangle(Rectangle other)
Height = other.Height;
}
- public Rectangle(Point point, double width, double height)
+ public Rectangle(Point point, System.Double width, System.Double height)
{
X = point.X;
Y = point.Y;
@@ -87,7 +87,7 @@ public static Rectangle Union(Rectangle a, Rectangle b)
);
}
- public double Area()
+ public System.Double Area()
{
return (MaxX - MinX) * (MaxY - MinY);
}