Skip to content

Commit

Permalink
Experimenting..
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiteBlackGoose committed Apr 14, 2021
1 parent f4858a1 commit 87a86a8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Sources/AngouriMath/Core/Exceptions/Interruptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AngouriMath.Core.Exceptions
{
internal sealed class OutOfQuotaException : Exception
public sealed class OutOfQuotaException : Exception
{
[ConstantField] internal static OutOfQuotaException Instance = new();
}
Expand Down
17 changes: 10 additions & 7 deletions Sources/AngouriMath/Core/QuotaGC/QuotaCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@

namespace AngouriMath.Core
{
internal sealed class QuotaLeft
public sealed class QuotaLeft
{
private int left;
private int initial;
private bool infinite;

internal static QuotaLeft CreateFinite(int quotaInitial)
=> new() { left = quotaInitial, infinite = false };
public static QuotaLeft CreateFinite(int quotaInitial)
=> new() { left = quotaInitial, initial = quotaInitial, infinite = false };

internal static QuotaLeft CreateInfinite()
public static QuotaLeft CreateInfinite()
=> new() { left = 0, infinite = true };

internal void DecreaseAndCheck()
public void Reset() => left = initial;

public void DecreaseAndCheck()
{
if (infinite)
return;
Expand All @@ -27,9 +30,9 @@ internal void DecreaseAndCheck()
}
}

internal static class QuotaCounter
public static class QuotaCounter
{
internal static Setting<QuotaLeft> QuotaLeft => quotaLeft ??= new(Core.QuotaLeft.CreateInfinite());
public static Setting<QuotaLeft> QuotaLeft => quotaLeft ??= new(Core.QuotaLeft.CreateInfinite());
[ThreadStatic] internal static Setting<QuotaLeft>? quotaLeft;
}
}
6 changes: 6 additions & 0 deletions Sources/Samples/Samples/Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
<ProjectReference Include="..\..\AngouriMath\AngouriMath.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Build.Tasks.v4.0">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\Microsoft.Build.Tasks.v4.0.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
49 changes: 42 additions & 7 deletions Sources/Samples/Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,50 @@
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using AngouriMath.Core;
using AngouriMath.Core.Exceptions;
using System.Collections.Generic;
using System.Linq;

// Entity expr = "alpha_beta";
// Console.WriteLine(Unsafe.SizeOf<GCHandle>());

foreach (var res in ComputeMaxInt(40).Take(10))
Console.WriteLine(res);

// Console.WriteLine("[ [ 1, 2 ] ; [ 3, 4 ] ]".ToEntity());
Matrix m = "[[3 - lambda, -1, 0, -2, 0], [-3, -4 - lambda, -2, 1, 3], [0, -7, 1 - lambda, -5, 2], [3, 4, 1, 1 - lambda, -2], [-6, -19, -5, -3, 10 - lambda]]";
Console.WriteLine(m.Determinant.Simplify());
// using var _ = MathS.Settings.MaxExpansionTermCount.Set(50);
// Console.WriteLine("(a + b)100".Expand());

static Exception? Except(Action action)
{
try
{
action();
}
catch (Exception e)
{
return e;
}
return null;
}


static IEnumerable<int> ComputeMaxInt(int quota)
{
static int SubAlgo(int curr)
{
for (int i = 0; i < 15; i++)
{
curr++;
QuotaCounter.QuotaLeft.Value.DecreaseAndCheck();
}
return curr;
}
using var _ = QuotaCounter.QuotaLeft.Set(QuotaLeft.CreateFinite(quota));
var curr = 0;
while (true)
{
if (Except(() => curr = SubAlgo(curr)) is OutOfQuotaException)
{
yield return curr;
QuotaCounter.QuotaLeft.Value.Reset();
}
}
}

0 comments on commit 87a86a8

Please sign in to comment.