Skip to content

Commit

Permalink
IsolateVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmatech committed Nov 6, 2014
1 parent d12a62c commit d44012d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Symbolism/IsolateVariable.cs
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Symbolism.CoefficientGpe;
using Symbolism.AlgebraicExpand;

namespace Symbolism.IsolateVariable
{
public static class Extensions
{
public static MathObject IsolateVariable(this Equation eq, Symbol sym)
{
if (eq.b.Has(sym)) return IsolateVariable(new Equation(eq.a - eq.b, 0), sym);

if (eq.a == sym) return eq;

if (eq.a.AlgebraicExpand().DegreeGpe(new List<MathObject>() { sym }) == 2)
{
var a = eq.a.AlgebraicExpand().CoefficientGpe(sym, 2);
var b = eq.a.AlgebraicExpand().CoefficientGpe(sym, 1);
var c = eq.a.AlgebraicExpand().CoefficientGpe(sym, 0);

//return
// sym == (-b - (((b ^ 2) - 4 * a * c) ^ (new Integer(1) / 2))) / (2 * a);

return
new Or(
sym == (-b + (((b ^ 2) - 4 * a * c) ^ (new Integer(1) / 2))) / (2 * a),
sym == (-b - (((b ^ 2) - 4 * a * c) ^ (new Integer(1) / 2))) / (2 * a)
).Simplify();
}



if (eq.a is Sum)
{
var items = ((Sum)eq.a).elts.FindAll(elt => elt.FreeOf(sym));

//return IsolateVariable(
// new Equation(
// eq.a - new Sum() { elts = items }.Simplify(),
// eq.b - new Sum() { elts = items }.Simplify()),
// sym);

var new_a = eq.a; items.ForEach(elt => new_a = new_a - elt);
var new_b = eq.b; items.ForEach(elt => new_b = new_b - elt);

return IsolateVariable(new Equation(new_a, new_b), sym);

//return IsolateVariable(
// new Equation(
// eq.a + new Sum() { elts = items.ConvertAll(elt => elt * -1) }.Simplify(),
// eq.b - new Sum() { elts = items }.Simplify()),
// sym);
}

if (eq.a is Product)
{
var items = ((Product)eq.a).elts.FindAll(elt => elt.FreeOf(sym));

return IsolateVariable(
new Equation(
eq.a / new Product() { elts = items }.Simplify(),
eq.b / new Product() { elts = items }.Simplify()),
sym);
}

throw new Exception();
}
}
}
1 change: 1 addition & 0 deletions Symbolism/Symbolism.csproj
Expand Up @@ -50,6 +50,7 @@
<Compile Include="CoefficientGpe.cs" />
<Compile Include="ExpandPower.cs" />
<Compile Include="ExpandProduct.cs" />
<Compile Include="IsolateVariable.cs" />
<Compile Include="Symbolism.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions Tests/Tests.cs
Expand Up @@ -23,6 +23,7 @@

using Symbolism.CoefficientGpe;
using Symbolism.AlgebraicExpand;
using Symbolism.IsolateVariable;

namespace Tests
{
Expand Down Expand Up @@ -284,6 +285,12 @@ static void Main(string[] args)

#endregion

#region IsolateVariable

Assert((0 == x - y).IsolateVariable(x).Equals(x == y), "(0 == x - y).IsolateVariable(x).Equals(x == y)");

#endregion

}

#region PSE 5E Example 4.3
Expand Down

0 comments on commit d44012d

Please sign in to comment.