Skip to content

Commit 015bfdb

Browse files
Bugs related to solver fixed & minor fixes
1 parent ea058cf commit 015bfdb

5 files changed

Lines changed: 31 additions & 11 deletions

File tree

AngouriMath/Convenience/MathS.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*/
1515

1616

17-
18-
using AngouriMath.Core;
17+
using System;
18+
using AngouriMath.Core;
1919
using AngouriMath.Core.FromString;
2020
using System.Linq.Expressions;
2121
using AngouriMath.Core.FromLinq;
@@ -53,6 +53,7 @@ public static EquationSystem Equations(params Entity[] equations)
5353
/// matrix.shape[0] - number of solutions
5454
/// matrix.shape[1] is equal to amount of variables
5555
/// </returns>
56+
[ObsoleteAttribute("Use MathS.Equations instead")]
5657
public static Tensor Solve(List<Entity> equations, List<VariableEntity> vars)
5758
=> EquationSolver.SolveSystem(equations, vars);
5859

@@ -62,9 +63,19 @@ public static Tensor Solve(List<Entity> equations, List<VariableEntity> vars)
6263
/// <param name="equation"></param>
6364
/// <param name="var"></param>
6465
/// <returns></returns>
66+
[ObsoleteAttribute("Use either MathS.Equations or MathS.SolveEquation or yourexpr.SolveEquation instead")]
6567
public static EntitySet Solve(Entity equation, VariableEntity var)
6668
=> EquationSolver.Solve(equation, var);
6769

70+
/// <summary>
71+
/// Solves one equation over one variable
72+
/// </summary>
73+
/// <param name="equation"></param>
74+
/// <param name="var"></param>
75+
/// <returns></returns>
76+
public static EntitySet SolveEquation(Entity equation, VariableEntity var)
77+
=> EquationSolver.Solve(equation, var);
78+
6879
/// <summary>
6980
/// https://en.wikipedia.org/wiki/Trigonometric_functions
7081
/// </summary>

AngouriMath/Core/Sys/EquationSystem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Linq;
2121
using System.Text;
2222
using AngouriMath.Core.Sys.Interfaces;
23+
using AngouriMath.Functions.Algebra.Solver;
2324

2425
namespace AngouriMath.Core.Sys
2526
{
@@ -47,7 +48,7 @@ public EquationSystem(params Entity[] equations)
4748
/// </param>
4849
/// <returns></returns>
4950
public Tensor Solve(params VariableEntity[] vars)
50-
=> MathS.Solve(equations, vars.ToList());
51+
=> EquationSolver.SolveSystem(equations, vars.ToList());
5152

5253
/// <summary>
5354
/// Returns latexised version of the system

AngouriMath/Functions/Algebra/Solver/Analytical/AnalyticalSolver.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ EntitySet GetNotNullEntites(EntitySet set)
315315
return GetNotNullEntites(FindInvertExpression(a, MathS.Pow(b, value), x));
316316
else
317317
// log(a, x) = value => a = x ^ value => x = a ^ (1 / value)
318-
return GetNotNullEntites(FindInvertExpression(a, 1 / MathS.Pow(b, value), x));
318+
return GetNotNullEntites(FindInvertExpression(b, MathS.Pow(a, 1 / value), x));
319319
default:
320320
throw new SysException("Unknown function");
321321
}
@@ -424,7 +424,7 @@ internal static void Solve(Entity expr, VariableEntity x, EntitySet dst, bool co
424424
continue;
425425
var newExpr = replacement.Item2.DeepCopy();
426426
TreeAnalyzer.FindAndReplace(ref newExpr, replacement.Item1, newVar);
427-
solutions = newExpr.Solve(newVar);
427+
solutions = newExpr.SolveEquation(newVar);
428428
if (solutions.Count > 0/* && !newExpr.Children.Any(c => c == newVar)*/)
429429
{
430430
bestReplacement = replacement.Item1;
@@ -440,7 +440,7 @@ internal static void Solve(Entity expr, VariableEntity x, EntitySet dst, bool co
440440
foreach (var solution in solutions)
441441
{
442442
var str = bestReplacement.ToString();
443-
if (bestReplacement - solution != expr)
443+
if (!compensateSolving || bestReplacement - solution != expr)
444444
Solve(bestReplacement - solution, x, newDst, compensateSolving: true);
445445
}
446446
dst.AddRange(newDst);

Samples/Samples/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,8 @@ Entity __c_min(Number val, int depth)
253253

254254
static void Main(string[] _)
255255
{
256-
Entity a = MathS.Vector(2, 3, 4);
257-
Entity b = MathS.Vector(5, 6, 7);
258-
if (a.IsTensoric() && b.IsTensoric())
259-
Console.WriteLine(MathS.ScalarProduct(a as Tensor, b as Tensor));
256+
Entity eqs = "x^y-3";
257+
Console.WriteLine(eqs.SolveEquation("y"));
260258
/*
261259
Entity expr = "32 + x";
262260
var q = new List<Entity>();

Tests/UnitTests/Algebra/SolveTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ public void TestInvertSin()
342342

343343
public void AssertSystemSolvable(List<Entity> equations, List<VariableEntity> vars, int rootCount = -1)
344344
{
345-
var sol = MathS.Solve(equations, vars);
345+
var sys = MathS.Equations(equations.ToArray());
346+
var sol = sys.Solve(vars.ToArray());
346347
Assert.IsTrue(sol.Shape[0] == rootCount || rootCount == -1, "Got " + sol.Shape[0] + " instead of " + rootCount);
347348
for (int i = 0; i < sol.Shape[0]; i++)
348349
{
@@ -431,6 +432,15 @@ public void TestTrigSystem4()
431432
);
432433
AssertSystemSolvable(eqs, VA("a", "b"), 0);
433434
}
435+
436+
[TestMethod]
437+
public void TestLogs()
438+
{
439+
Entity eqs = "log(32, x) - 5";
440+
var roots = eqs.SolveEquation("x");
441+
Assert.IsTrue(roots.Count == 1);
442+
AssertRoots(eqs, "x", roots[0]);
443+
}
434444
}
435445
}
436446

0 commit comments

Comments
 (0)