Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmatech committed Jun 8, 2019
1 parent 5d66520 commit e10887d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 51 deletions.
6 changes: 2 additions & 4 deletions Symbolism/AlgebraicExpand.cs
Expand Up @@ -22,10 +22,8 @@ public static MathObject AlgebraicExpand(this MathObject u)
} }


if (u is Sum) if (u is Sum)
{ return (u as Sum).Map(elt => elt.AlgebraicExpand());
return Sum.FromRange((u as Sum).elts.Select(elt => elt.AlgebraicExpand())).Simplify();
}

if (u is Product) if (u is Product)
{ {
var v = (u as Product).elts[0]; var v = (u as Product).elts[0];
Expand Down
10 changes: 4 additions & 6 deletions Symbolism/DeepSelect.cs
Expand Up @@ -14,12 +14,10 @@ public static MathObject DeepSelect(this MathObject obj, Func<MathObject, MathOb
var result = proc(obj); var result = proc(obj);


if (result is Power) if (result is Power)
{
return return
(result as Power).bas.DeepSelect(proc) ^ (result as Power).bas.DeepSelect(proc) ^
(result as Power).exp.DeepSelect(proc); (result as Power).exp.DeepSelect(proc);
}

if (result is Or) if (result is Or)
return (result as Or).Map(elt => elt.DeepSelect(proc)); return (result as Or).Map(elt => elt.DeepSelect(proc));


Expand All @@ -32,14 +30,14 @@ public static MathObject DeepSelect(this MathObject obj, Func<MathObject, MathOb
(result as Equation).a.DeepSelect(proc), (result as Equation).a.DeepSelect(proc),
(result as Equation).b.DeepSelect(proc), (result as Equation).b.DeepSelect(proc),
(result as Equation).Operator); (result as Equation).Operator);

if (result is Sum) if (result is Sum)
return return
Sum.FromRange((result as Sum).elts.Select(elt => elt.DeepSelect(proc))).Simplify(); (result as Sum).Map(elt => elt.DeepSelect(proc));


if (result is Product) if (result is Product)
return return
Product.FromRange((result as Product).elts.Select(elt => elt.DeepSelect(proc))).Simplify(); (result as Product).Map(elt => elt.DeepSelect(proc));


return result; return result;
} }
Expand Down
9 changes: 3 additions & 6 deletions Symbolism/EliminateVariable.cs
Expand Up @@ -52,7 +52,7 @@ public static MathObject CheckVariable(this MathObject expr, Symbol sym)
(((expr as Equation).SimplifyEquation() as Equation).a as Power).exp is Integer && (((expr as Equation).SimplifyEquation() as Equation).a as Power).exp is Integer &&
((((expr as Equation).SimplifyEquation() as Equation).a as Power).exp as Integer).val < 0) ((((expr as Equation).SimplifyEquation() as Equation).a as Power).exp as Integer).val < 0)
return false; return false;

if (expr is And) if (expr is And)
{ {
var result = (expr as And).Map(elt => elt.CheckVariable(sym)); var result = (expr as And).Map(elt => elt.CheckVariable(sym));
Expand All @@ -67,16 +67,13 @@ public static MathObject CheckVariable(this MathObject expr, Symbol sym)
return result; return result;
} }



if (expr is Or && if (expr is Or &&
(expr as Or).args.All(elt => elt is And)) (expr as Or).args.All(elt => elt is And))
return (expr as Or).Map(elt => elt.CheckVariable(sym)); return (expr as Or).Map(elt => elt.CheckVariable(sym));


return expr; return expr;
} }




// EliminateVarAnd // EliminateVarAnd
// EliminateVarOr // EliminateVarOr
// EliminateVarLs // EliminateVarLs
Expand Down Expand Up @@ -162,7 +159,7 @@ public static MathObject EliminateVariable(this MathObject expr, Symbol sym)
throw new Exception(); throw new Exception();
} }


public static MathObject EliminateVariables(this MathObject expr, params Symbol[] syms) public static MathObject EliminateVariables(this MathObject expr, params Symbol[] syms) =>
{ return syms.Aggregate(expr, (result, sym) => result.EliminateVariable(sym)); } syms.Aggregate(expr, (result, sym) => result.EliminateVariable(sym));
} }
} }
5 changes: 2 additions & 3 deletions Symbolism/RationalizeExpression.cs
Expand Up @@ -34,9 +34,8 @@ public static MathObject RationalizeExpression(this MathObject u)


if (u is Product) if (u is Product)
return return
Product.FromRange((u as Product).elts.Select(elt => elt.RationalizeExpression())).Simplify(); (u as Product).Map(elt => elt.RationalizeExpression());
// (u as Product).Map(elt => elt.RationalizeExpression())

if (u is Sum) if (u is Sum)
{ {
var f = (u as Sum).elts[0]; var f = (u as Sum).elts[0];
Expand Down
20 changes: 10 additions & 10 deletions Symbolism/Substitute.cs
Expand Up @@ -34,11 +34,11 @@ public static MathObject Substitute(this MathObject obj, MathObject a, MathObjec


if (obj is Product) if (obj is Product)
return return
Product.FromRange((obj as Product).elts.ConvertAll(elt => elt.Substitute(a, b))).Simplify(); (obj as Product).Map(elt => elt.Substitute(a, b));


if (obj is Sum) if (obj is Sum)
return return
Sum.FromRange((obj as Sum).elts.ConvertAll(elt => elt.Substitute(a, b))).Simplify(); (obj as Sum).Map(elt => elt.Substitute(a, b));


if (obj is Function) if (obj is Function)
{ {
Expand All @@ -54,17 +54,17 @@ public static MathObject Substitute(this MathObject obj, MathObject a, MathObjec
return obj; return obj;
} }


public static MathObject SubstituteEq(this MathObject obj, Equation eq) public static MathObject SubstituteEq(this MathObject obj, Equation eq) =>
{ return obj.Substitute(eq.a, eq.b); } obj.Substitute(eq.a, eq.b);


public static MathObject SubstituteEqLs(this MathObject obj, List<Equation> eqs) public static MathObject SubstituteEqLs(this MathObject obj, List<Equation> eqs) =>
{ return eqs.Aggregate(obj, (a, eq) => a.SubstituteEq(eq)); } eqs.Aggregate(obj, (a, eq) => a.SubstituteEq(eq));


public static MathObject Substitute(this MathObject obj, MathObject a, int b) public static MathObject Substitute(this MathObject obj, MathObject a, int b) =>
{ return obj.Substitute(a, new Integer(b)); } obj.Substitute(a, new Integer(b));


public static MathObject Substitute(this MathObject obj, MathObject a, double b) public static MathObject Substitute(this MathObject obj, MathObject a, double b) =>
{ return obj.Substitute(a, new DoubleFloat(b)); } obj.Substitute(a, new DoubleFloat(b));


} }
} }
Expand Down
31 changes: 13 additions & 18 deletions Symbolism/Symbolism.cs
Expand Up @@ -699,19 +699,14 @@ static MathObject AndProc(MathObject[] ls)


public static And FromRange(IEnumerable<MathObject> ls) => new And(ls.ToArray()); public static And FromRange(IEnumerable<MathObject> ls) => new And(ls.ToArray());


public MathObject Add(MathObject obj) public MathObject Add(MathObject obj) =>
{ And.FromRange(args.Add(obj)).Simplify();
var ls = new List<MathObject>(args);

ls.Add(obj);

return And.FromRange(ls).Simplify();
}


public MathObject AddRange(IEnumerable<MathObject> ls) => public MathObject AddRange(IEnumerable<MathObject> ls) =>
And.FromRange(args.AddRange(ls)).Simplify(); And.FromRange(args.AddRange(ls)).Simplify();


public MathObject Map(Func<MathObject, MathObject> proc) => new And(args.Select(proc).ToArray()).Simplify(); public MathObject Map(Func<MathObject, MathObject> proc) =>
And.FromRange(args.Select(proc)).Simplify();
} }


public class Or : Function public class Or : Function
Expand Down Expand Up @@ -958,14 +953,8 @@ public MathObject Simplify()
{ return ((Power)v).bas ^ (((Power)v).exp * w); } { return ((Power)v).bas ^ (((Power)v).exp * w); }


if (v is Product && w is Integer) if (v is Product && w is Integer)
{ return (v as Product).Map(elt => elt ^ w);
var list = new List<MathObject>();

((Product)v).elts.ForEach(elt => list.Add(elt ^ w));

return Product.FromRange(list).Simplify();
}

return new Power(v, w); return new Power(v, w);
} }


Expand Down Expand Up @@ -1159,6 +1148,9 @@ public MathObject Simplify()


public override MathObject Denominator() => public override MathObject Denominator() =>
Product.FromRange(elts.Select(elt => elt.Denominator())).Simplify(); Product.FromRange(elts.Select(elt => elt.Denominator())).Simplify();

public MathObject Map(Func<MathObject, MathObject> proc) =>
Product.FromRange(elts.Select(proc)).Simplify();
} }


public class Sum : MathObject public class Sum : MathObject
Expand Down Expand Up @@ -1317,6 +1309,9 @@ public override string StandardForm()


return result; return result;
} }

public MathObject Map(Func<MathObject, MathObject> proc) =>
Sum.FromRange(elts.Select(proc)).Simplify();
} }


class Difference : MathObject class Difference : MathObject
Expand Down
10 changes: 6 additions & 4 deletions Symbolism/Trigonometric.cs
Expand Up @@ -430,12 +430,14 @@ public static class Extensions
{ {
public static Symbol Pi = new Symbol("Pi"); public static Symbol Pi = new Symbol("Pi");


public static MathObject ToRadians(this MathObject n) { return n * Pi / 180; } public static MathObject ToRadians(this MathObject n) => n * Pi / 180;


public static MathObject ToDegrees(this MathObject n) { return 180 * n / Pi; } public static MathObject ToDegrees(this MathObject n) => 180 * n / Pi;


public static MathObject ToRadians(this int n) { return new Integer(n) * Pi / 180; } public static MathObject ToRadians(this int n) => new Integer(n) * Pi / 180;


public static MathObject ToDegrees(this int n) { return 180 * new Integer(n) / Pi; } public static MathObject ToDegrees(this int n) => 180 * new Integer(n) / Pi;

// (Integer) 180 * n / Pi
} }
} }

0 comments on commit e10887d

Please sign in to comment.