Skip to content
Merged
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
1 change: 0 additions & 1 deletion AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ public static partial class MathS
/// <param name="name"></param>
/// <returns></returns>
public static VariableEntity Var(string name) => new VariableEntity(name);
public static readonly VarFunc Symbol = v => new VariableEntity(v);

/// <summary>
/// Creates a complex instance of Number (not NumberEntity!)
Expand Down
94 changes: 94 additions & 0 deletions AngouriMath/Convenience/SympySyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using AngouriMath.Core.TreeAnalysis;
using System;
using System.Collections.Generic;
using System.Text;

namespace AngouriMath.Convenience
{
/// <summary>
/// Sympy - is a super-powerful library for algebra cumputation,
/// multi-variable expressions, physics, matrices, etc.
/// But it is only available for python. If you need the same syntax for C#,
/// this module is for you.
/// </summary>
public static class SySyn
{
/// Here sympy-like syntax implementation goes

/// <summary>
/// Differentiation
/// </summary>
/// <param name="expr">
/// Expression to differentiate
/// </param>
/// <param name="vars">
/// Variable to differentiate over. If you need more than first derivative,
/// specify as many variables as you need
/// </param>
/// <returns></returns>
public static Entity Diff(Entity expr, params VariableEntity[] vars)
{
foreach (var v in vars)
expr = expr.Derive(v);
return expr.Simplify();
}

/// <summary>
/// Simplification of expression
/// </summary>
/// <param name="expr"></param>
/// <returns></returns>
public static Entity Simplify(Entity expr) => expr.SimplifyIntelli();

/// <summary>
/// Attempt to find analytical roots of a custom equation
/// </summary>
/// <param name="x"></param>
/// <returns>
/// Returns EntitySet. Work with it as with a list
/// </returns>
public static EntitySet Solve(Entity expr, VariableEntity x) => expr.Solve(x);

/// <summary>
/// Expands an equation trying to eliminate all the parentheses ( e. g. 2 * (x + 3) = 2 * x + 2 * 3 )
/// </summary>
/// <returns>
/// An expanded Entity
/// </returns>
public static Entity Expand(Entity expr) => expr.Expand();

/// <summary>
/// Collapses an equation trying to eliminate as many power-uses as possible ( e. g. x * 3 + x * y = x * (3 + y) )
/// </summary>
/// <returns></returns>
public static Entity Collapse(Entity expr) => expr.Collapse();

/// <summary>
/// Simplification synonim. Recommended to use in case of
/// computing a concrete number, knowing that you don't have
/// any other symbols but numbers and functions.
/// </summary>
/// <returns></returns>
public static Entity Evalf(Entity expr) => expr.Eval();

/// <summary>
/// Returns the expression in format of latex (for example, a / b -> \frac{a}{b})
/// </summary>
/// <returns></returns>
public static Entity Latex(Entity expr) => expr.Latexise();

/// <summary>
/// Creates an instance of variable entity.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static VariableEntity Symbol(string name) => new VariableEntity(name);

/// <summary>
/// e ^ power
/// </summary>
/// <param name="power"></param>
/// <returns></returns>
public static Entity Exp(Entity power) => MathS.Pow(MathS.e, power);
}
}
2 changes: 0 additions & 2 deletions AngouriMath/Convenience/SynonimFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ internal static class SynonimFunctions
{
{ "sqrtf", args => MathS.Pow(args[0], 0.5) },
{ "sqrf", args => MathS.Pow(args[0], 2) },
{ "bf", args => args[0] * MathS.Sin(args[0]) },
{ "tbf", args => args[0] * MathS.Cos(args[0]) },
{ "lnf", args => MathS.Log(args[0], MathS.e) },
{ "secf", args => MathS.Sec(args[0]) },
{ "cosecf", args => MathS.Cosec(args[0]) },
Expand Down
2 changes: 1 addition & 1 deletion AngouriMath/Core/FromString/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static Entity ParseAsVariable(Lexer lexer)
{
e = new NumberEntity(Number.Parse(current.Value));
}
else if (current.Type == TokenType.VARIABLE)
else if (current.Type == TokenType.VARIABLE || current.Type == TokenType.FUNCTION && lexer.GlanceNext().Type != TokenType.PARENTHESIS_OPEN)
{
e = new VariableEntity(current.Value);
}
Expand Down
2 changes: 0 additions & 2 deletions AngouriMath/Core/FromString/SyntaxInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ internal static class SyntaxInfo
{ "ln", 1 },
{ "tan", 1 },
{ "cotan", 1 },
{ "b", 1 },
{ "tb", 1 },
{ "sec", 1 },
{ "cosec", 1 },
{ "arcsin", 1 },
Expand Down
1 change: 1 addition & 0 deletions AngouriMath/Core/Sys/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Entity DeepCopy()
public static implicit operator Entity(int num) => new NumberEntity(num);
public static implicit operator Entity(Number num) => new NumberEntity(num);
public static implicit operator Entity(double num) => new NumberEntity(num);
public static implicit operator Entity(string expr) => MathS.FromString(expr);

public static bool operator ==(Entity a, Entity b)
{
Expand Down
5 changes: 2 additions & 3 deletions AngouriMath/Core/TreeAnalysis/LinearChildren.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace AngouriMath.Core.TreeAnalysis
{
public static partial class TreeAnalyzer
internal static partial class TreeAnalyzer
{
internal static List<Entity> LinearChildren(Entity tree,
string funcName /*e. g. "sumf" */,
Expand All @@ -31,7 +31,6 @@ internal static List<Entity> LinearChildren(Entity tree,
res.Add(tree);
return res;
}
internal static void Sort(List<Entity> children, SortLevel level) => children.Sort((a, b) => a.Hash(level).CompareTo(b.Hash(level)));

internal static void Sort(List<Entity> children, SortLevel level) => children.Sort((a, b) => a.Hash(level).CompareTo(b.Hash(level)));
}
}
50 changes: 50 additions & 0 deletions AngouriMath/Core/TreeAnalysis/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,56 @@
using System.Collections.Generic;
using System.Text;

namespace AngouriMath.Core.TreeAnalysis
{
public class EntitySet : List<Entity>
{
private HashSet<string> exsts = new HashSet<string>();
public override string ToString()
{
return "[" + string.Join(", ", this) + "]";
}
public new void Add(Entity ent)
{
if (ent == null)
return;
if (ent is NumberEntity && ent.GetValue().IsNull)
return;
ent = ent.SimplifyIntelli();
var hash = ent.ToString();
if (!exsts.Contains(hash))
{
base.Add(ent);
exsts.Add(hash);
}
}
public void Merge(IEnumerable<Number> list)
{
foreach (var l in list)
Add(l);
}
public void Merge(IEnumerable<Entity> list)
{
foreach (var l in list)
Add(l);
}
}
internal static partial class TreeAnalyzer
{



internal static void GetUniqueVariables(Entity expr, EntitySet dst)
{
if (expr is VariableEntity)
dst.Add(expr);
else
foreach (var child in expr.Children)
GetUniqueVariables(child, dst);
}
}
}

namespace AngouriMath
{
public abstract partial class Entity
Expand Down
2 changes: 1 addition & 1 deletion AngouriMath/Core/TreeAnalysis/Sort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal Entity Sort(SortLevel level)

namespace AngouriMath.Core.TreeAnalysis
{
public static partial class TreeAnalyzer
internal static partial class TreeAnalyzer
{
internal enum SortLevel
{
Expand Down
33 changes: 10 additions & 23 deletions AngouriMath/Functions/Algebra/AnalyticalSolver/AnalyticalSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public EntitySet Solve(VariableEntity x)

namespace AngouriMath.Core.TreeAnalysis
{
public static partial class TreeAnalyzer
internal static partial class TreeAnalyzer
{
/// <summary>
/// Searches for a subtree containing `ent` and being minimal possible size.
Expand Down Expand Up @@ -280,29 +280,16 @@ internal static void Solve(Entity expr, VariableEntity x, EntitySet dst)
else
dst.AddRange(res);
}
else
{
EntitySet vars = new EntitySet();
TreeAnalyzer.GetUniqueVariables(expr, vars);
if (vars.Count == 1)
dst.Merge(expr.SolveNt(x));
}

}
}
}
public class EntitySet : List<Entity>
{
private HashSet<string> exsts = new HashSet<string>();
public override string ToString()
{
return "[" + string.Join(", ", this) + "]";
}
public new void Add(Entity ent)
{
if (ent == null)
return;
if (ent is NumberEntity && ent.GetValue().IsNull)
return;
ent = ent.SimplifyIntelli();
var hash = ent.ToString();
if (!exsts.Contains(hash))
{
base.Add(ent);
exsts.Add(hash);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Entity GetMonomialByPower(int power)
// Provided a x ^ n + b x ^ m = 0
// a = -b x ^ (m - n)
// (- a / b) ^ (1 / (m - n)) = x
res.Add(MathS.Pow(-1 * monomialsByPower[powers[0]] / monomialsByPower[powers[1]], 1 / (powers[1] - powers[0])));
res.Add(MathS.Pow(-1 * monomialsByPower[powers[0]] / monomialsByPower[powers[1]], 1.0 / (powers[1] - powers[0])));
return res;
}

Expand Down
3 changes: 3 additions & 0 deletions AngouriMath/Functions/Core/InvokeTableFuncs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Globalization;
using AngouriMath.Core;
using AngouriMath.Core.TreeAnalysis;

namespace AngouriMath
{
Expand All @@ -22,6 +23,8 @@ namespace AngouriMath
/// </summary>
public static partial class MathS
{


public delegate Entity OneArg(Entity a);
public delegate Entity TwoArg(Entity a, Entity n);
public delegate VariableEntity VarFunc(string v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ internal Dictionary<int, Entity> EqFits(Entity tree)

namespace AngouriMath.Core.TreeAnalysis
{
public static partial class TreeAnalyzer
internal static partial class TreeAnalyzer
{
internal static Entity ReplaceOne(Entity source, Pattern oldPattern, Entity newPattern)
{
Expand Down
7 changes: 3 additions & 4 deletions AngouriMath/Functions/Evaluation/Patterns/Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,9 @@ internal static class Patterns
internal static readonly RuleList ExpandRules = new RuleList
{
// (any1 + any2)2
{ Powf.PHang(any1 + any2, Num(2)), Powf.PHang(any1, Num(2)) + Num(2) * any1 * any2 + Powf.PHang(any2, Num(2)) },

// (any1 - any2)2
{ Powf.PHang(any1 + any2, Num(2)), Powf.PHang(any1, Num(2)) - Num(2) * any1 * any2 + Powf.PHang(any2, Num(2)) },
{ Powf.PHang(any1, Num(2)), any1 * any1 },
{ Powf.PHang(any1, Num(3)), any1 * any1 * any1 },
{ Powf.PHang(any1, Num(4)), any1 * any1 * any1 * any1 },

// ({1} - {2}) ({1} + {2}) = x2 - {}2
{ (any1 - any2) * (any1 + any2), Powf.PHang(any1, Num(2)) - Powf.PHang(any2, Num(2)) },
Expand Down
2 changes: 1 addition & 1 deletion AngouriMath/Functions/Output/Latex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract partial class Entity
/// </summary>
/// <returns></returns>
public string Latexise() => Latexise(false);
public string Latexise(bool parenthesesRequired)
internal string Latexise(bool parenthesesRequired)
{
if (IsLeaf)
return this is NumberEntity ? this.GetValue().ToString() : this.ToString();
Expand Down
Loading