Skip to content
Draft
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
11 changes: 11 additions & 0 deletions Sources/AngouriMath/Core/Exceptions/Interruptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AngouriMath.Core.Exceptions
{
public sealed class OutOfQuotaInterruption : Exception
{
[ConstantField] internal static OutOfQuotaInterruption Instance = new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal static void ExitIfCancelled()
var token = globalCancellationToken.Value;
if (token is { } tok)
tok.ThrowIfCancellationRequested();
System.Console.WriteLine($"Token check at {System.DateTime.Now.Millisecond}");
}
}
}
39 changes: 39 additions & 0 deletions Sources/AngouriMath/Core/QuotaGC/QuotaCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using AngouriMath.Convenience;
using AngouriMath.Core.Exceptions;

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

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

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

public void Reset() => left = initial;

public bool DecreaseAndCheck()
{
if (infinite)
return true;
left--;
if (left is 0)
throw OutOfQuotaInterruption.Instance;
return true;
}
}

public static class QuotaCounter
{
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>
78 changes: 61 additions & 17 deletions Sources/Samples/Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
using System;
using AngouriMath;
using AngouriMath.Extensions;
using static AngouriMath.MathS;
using static AngouriMath.Entity;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

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


// 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());
using AngouriMath.Core;
using AngouriMath.Core.Exceptions;
using System.Collections.Generic;
using System.Linq;


Console.WriteLine(Simplify(expr: 0, quota: 40));


static TOut? TryExecuteConstrained<TIn, TOut>(TIn input, int quota, Func<TIn, TOut> alg)
{
using var _ = QuotaCounter.QuotaLeft.Set(QuotaLeft.CreateFinite(quota));
try
{
return alg(input);
}
catch (OutOfQuotaInterruption)
{
return default;
}
}

static void AssignIfCan<TIn>(ref TIn toWhat, TIn? value)
{
if (value is { } valid)
toWhat = valid;
}

static int Simplify(int expr, int quota)
{
var subQuota = 5;
while (quota > 0)
{
AssignIfCan(ref expr, TryExecuteConstrained(expr, subQuota, SubSim1));
AssignIfCan(ref expr, TryExecuteConstrained(expr, subQuota, SubSim2));
quota -= subQuota * 2;
subQuota += 2;
}
return expr;

static int SubSim1(int curr)
{
for (int i = 0; i < 100; i++)
{
QuotaCounter.QuotaLeft.Value.DecreaseAndCheck();
curr++;
}
return curr;
}

static int SubSim2(int curr)
{
for (int i = 0; i < 70; i++)
{
QuotaCounter.QuotaLeft.Value.DecreaseAndCheck();
curr += 2;
}
return curr;
}
}