Test Improvement

This commit is contained in:
h z
2024-09-27 22:23:22 +01:00
parent 9126f98173
commit c6843279ce
9 changed files with 75 additions and 20 deletions

View File

@@ -13,5 +13,7 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\net6.0\Skeleton.xml</DocumentationFile> <DocumentationFile>bin\Release\net6.0\Skeleton.xml</DocumentationFile>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project> </Project>

BIN
src/Algebra.zip Normal file

Binary file not shown.

View File

@@ -200,6 +200,7 @@ public partial class CategoryOf<TDimension>
(c1, c2) = (c2, c1); (c1, c2) = (c2, c1);
} }
} }
} }
} }

View File

@@ -87,6 +87,7 @@ public abstract class Matrix< TMatrix> : Matrix
internal abstract TMatrix Mul(TMatrix other); internal abstract TMatrix Mul(TMatrix other);
internal abstract void SelfMul(TMatrix other); internal abstract void SelfMul(TMatrix other);
internal abstract void SelfLMul(TMatrix other); internal abstract void SelfLMul(TMatrix other);
internal abstract void SelfAdd(TMatrix other);
/// <summary> /// <summary>
/// Add two matrix of the same dim over same field /// Add two matrix of the same dim over same field
/// </summary> /// </summary>
@@ -158,7 +159,9 @@ public abstract class Matrix< TMatrix> : Matrix
/// <summary> /// <summary>
/// transpose current matrix, modify on current matrix without return a new one /// transpose current matrix, modify on current matrix without return a new one
/// </summary> /// </summary>
public abstract void SelfTranspose(); internal abstract void SelfTranspose();
internal abstract void SelfHermitian();
/// <summary> /// <summary>
/// Eigen decomposition of the matrix, M = JD inv(J)<br/> /// Eigen decomposition of the matrix, M = JD inv(J)<br/>
@@ -442,13 +445,24 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
} }
/// <inheritdoc /> /// <inheritdoc />
public override void SelfTranspose() internal override void SelfTranspose()
{ {
for (int i = 1; i <= Dim; i++) for (int i = 1; i <= Dim; i++)
for (int j = 1; j < i; j++) for (int j = 1; j < i; j++)
(this[i, j], this[j, i]) = (this[j, i], this[i, j]); (this[i, j], this[j, i]) = (this[j, i], this[i, j]);
} }
internal override void SelfHermitian()
{
for(int i = 1; i <= Dim; i++)
for(int j = 1; j<i;j++)
{
TScalar hij = Structure.Conj(this[i, j]);
TScalar hji = Structure.Conj(this[j, i]);
(this[i, j], this[j, i]) = (hji, hij);
}
}
/// <inheritdoc /> /// <inheritdoc />
public override TMatrix Transpose() public override TMatrix Transpose()
{ {
@@ -610,6 +624,12 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
res[i, j] = Structure.Addition(this[i, j], other[i, j]); res[i, j] = Structure.Addition(this[i, j], other[i, j]);
return res; return res;
} }
internal override void SelfAdd(TMatrix other)
{
for (int i = 1; i <= Dim; i++)
for (int j = 1; j <= Dim; j++)
this[i, j] = Structure.Addition(this[i, j], other[i, j]);
}
internal override TMatrix Negation() internal override TMatrix Negation()
{ {
@@ -878,7 +898,6 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
(other as CategoryOf<IDim2>.OnField<Complex>.FMatrix)! (other as CategoryOf<IDim2>.OnField<Complex>.FMatrix)!
) as TMatrix)!; ) as TMatrix)!;
if (this is CategoryOf<IDim3>.OnField<Complex>.FMatrix self3) if (this is CategoryOf<IDim3>.OnField<Complex>.FMatrix self3)
return (ComplexMatrixExtension.FastMul( return (ComplexMatrixExtension.FastMul(
self3, self3,
(other as CategoryOf<IDim3>.OnField<Complex>.FMatrix)! (other as CategoryOf<IDim3>.OnField<Complex>.FMatrix)!
@@ -1248,11 +1267,6 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
double errorDecrease = Structure.MaxError(a[wIndex, wIndex - 1]) - diff; double errorDecrease = Structure.MaxError(a[wIndex, wIndex - 1]) - diff;
for (int i = 0; i < maxIters; i++) for (int i = 0; i < maxIters; i++)
{ {
if (i == maxIters - 10)
{
Console.WriteLine("HERE");
}
diff = Structure.MaxError(a[wIndex, wIndex - 1]); diff = Structure.MaxError(a[wIndex, wIndex - 1]);
TScalar s = diff < firstTol TScalar s = diff < firstTol
? a[wIndex, wIndex] ? a[wIndex, wIndex]
@@ -1265,10 +1279,15 @@ public abstract class Matrix<TScalar, TVector, TMatrix> : Matrix<TVector, TMatri
for (int j = 1; j <= Dim; j++) for (int j = 1; j <= Dim; j++)
shift[j, j] = s; shift[j, j] = s;
qr = (a - shift).QRDecomposition(); qr = (a - shift).QRDecomposition();
a = qr.R * qr.Q + shift; qr.R.SelfMul(qr.Q);
u = qr.Q.Hermitian() * u; qr.R.SelfAdd(shift);
errorDecrease = Structure.MaxError(a[wIndex, wIndex - 1]) - diff; qr.Q.SelfHermitian();
diff = Structure.MaxError(a[wIndex, wIndex - 1]); qr.Q.SelfMul(u);
a = qr.R;
u = qr.Q;
double maxError = Structure.MaxError(a[wIndex, wIndex - 1]);
errorDecrease = maxError - diff;
diff = maxError;
if (a.Property.IsUpperTriangle) if (a.Property.IsUpperTriangle)
{ {
if (triangle || a.Property.IsDiagonal) if (triangle || a.Property.IsDiagonal)

View File

@@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices;
using Skeleton.Algebra.Extensions; using Skeleton.Algebra.Extensions;
using Skeleton.Algebra.FixableObjects; using Skeleton.Algebra.FixableObjects;
using Skeleton.Algebra.ScalarFieldStructure; using Skeleton.Algebra.ScalarFieldStructure;

16
src/DataStructure/Box.cs Normal file
View File

@@ -0,0 +1,16 @@
namespace Skeleton.DataStructure;
public class Box<TValue> where TValue : new()
{
public Box(Func<TValue> f)
{
Value = f();
}
public Box()
{
Value = new();
}
public TValue Value { get; set; }
}

View File

@@ -1,6 +1,7 @@
using System.Numerics; using System.Numerics;
using Skeleton.Algebra.DimensionProviders; using Skeleton.Algebra.DimensionProviders;
using Skeleton.Utils.Helpers; using Skeleton.Utils.Helpers;
using Skeleton.Utils.RandomEngines;
namespace Skeleton.Samples; namespace Skeleton.Samples;
using C22 = Algebra.CategoryOf<IDim2>.OnField<Complex>.FMatrix; using C22 = Algebra.CategoryOf<IDim2>.OnField<Complex>.FMatrix;
@@ -35,5 +36,4 @@ public static class C22Samples
if (m.Det().Magnitude > 0.2d) if (m.Det().Magnitude > 0.2d)
yield return m; yield return m;
} }
} }

View File

@@ -20,7 +20,7 @@ public static class LinqHelper
/// </summary> /// </summary>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> a) public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> a)
{ {
return a.OrderBy(_ => RandomSource.Rnd.NextDouble()); return a.OrderBy(_ => RandomSource.Get());
} }
/// <summary> /// <summary>
@@ -62,7 +62,7 @@ public static class LinqHelper
T[] ac = a.ToArray(); T[] ac = a.ToArray();
int len = ac.Length; int len = ac.Length;
for (int i = 0; i <= count; i++) for (int i = 0; i <= count; i++)
yield return ac[RandomSource.Rnd.NextInt64(0, len)]; yield return ac[RandomSource.Rnd.Value.NextInt64(0, len)];
} }
/// <summary> /// <summary>
@@ -70,7 +70,7 @@ public static class LinqHelper
/// </summary> /// </summary>
public static IEnumerable<T> Sample<T>(this IEnumerable<T> a, double freq) public static IEnumerable<T> Sample<T>(this IEnumerable<T> a, double freq)
{ {
return a.Where(x => RandomSource.Rnd.NextDouble() < freq); return a.Where(x => RandomSource.Rnd.Value.NextDouble() < freq);
} }
/// <summary> /// <summary>
@@ -559,6 +559,12 @@ public static class LinqHelper
public static CategoryOf<TDim>.FSpecialLieUnitaryMatrix SpLieSum<TDim> public static CategoryOf<TDim>.FSpecialLieUnitaryMatrix SpLieSum<TDim>
(this IEnumerable<CategoryOf<TDim>.FSpecialLieUnitaryMatrix> es) => (this IEnumerable<CategoryOf<TDim>.FSpecialLieUnitaryMatrix> es) =>
new(es.LieSum()); new(es.LieSum());
public static T WithSeed<T>(this T a, int seed)
{
RandomSource.SetSeed(seed);
if (seed < 0)
RandomSource.Rnd.Value = new Random();
return a;
}
} }

View File

@@ -1,3 +1,5 @@
using Skeleton.DataStructure;
namespace Skeleton.Utils.RandomEngines; namespace Skeleton.Utils.RandomEngines;
/// <summary> /// <summary>
@@ -6,13 +8,21 @@ public static class RandomSource
{ {
/// <summary> /// <summary>
/// </summary> /// </summary>
public static Random Rnd = new(); public static ThreadLocal<Random> Rnd = new(() => new Random());
public static void SetSeed(int seed)
{
Rnd.Value = new Random(seed);
}
/// <summary> /// <summary>
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static double Get() public static double Get()
{ {
return Rnd.NextDouble(); return Rnd.Value.NextDouble();
} }
} }