Created
June 12, 2019 18:51
-
-
Save mzandvliet/331d51f9b3b66166eb560aa6f7fc20ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.0/pattern-matching | |
Cool proposal. Here I do a quick rewrite using the Unity ~C#6 syntax: | |
*/ | |
namespace Algebra { | |
public abstract class Expr { } | |
public class X : Expr { | |
} | |
public class Const : Expr { | |
public double Value; | |
public Const(double v) { | |
Value = v; | |
} | |
public static Const Zero = new Const(0); | |
public static Const One = new Const(1); | |
} | |
public class Add : Expr { | |
public Expr Left; | |
public Expr Right; | |
} | |
public class Mult : Expr { | |
public Expr Left; | |
public Expr Right; | |
} | |
public class Neg : Expr { | |
public Expr Value; | |
} | |
public static class Manipulate { | |
public static Expr Simplify(Expr e) { | |
switch (e) { | |
case Mult m when m.Left == Const.Zero: return Const.Zero; | |
case Mult m when m.Right == Const.Zero: return Const.Zero; | |
case Mult m when m.Left == Const.One: return Simplify(m.Right); | |
case Mult m when m.Right == Const.One: return Simplify(m.Left); | |
case Mult m when m.Left is Const && m.Right is Const: return new Const((m.Left as Const).Value * (m.Right as Const).Value); | |
case Add a when a.Left == Const.Zero: return Simplify(a.Right); | |
case Add a when a.Right == Const.Zero: return Simplify(a.Left); | |
case Add a when a.Left is Const && a.Right is Const: return new Const((a.Left as Const).Value + (a.Right as Const).Value); | |
case Neg n when n.Value is Const: return new Const(-(n.Value as Const).Value); | |
default: return e; | |
} | |
} | |
} | |
} | |
namespace Algebra7 { | |
abstract class Expr; | |
class X() : Expr; | |
class Const(double Value) : Expr; | |
class Add(Expr Left, Expr Right) : Expr; | |
class Mult(Expr Left, Expr Right) : Expr; | |
class Neg(Expr Value) : Expr; | |
Expr Deriv(Expr e) { | |
switch (e) { | |
case X(): return Const(1); | |
case Const(*): return Const(0); | |
case Add(var Left, var Right): | |
return Add(Deriv(Left), Deriv(Right)); | |
case Mult(var Left, var Right): | |
return Add(Mult(Deriv(Left), Right), Mult(Left, Deriv(Right))); | |
case Neg(var Value): | |
return Neg(Deriv(Value)); | |
} | |
} | |
Expr Simplify(Expr e) { | |
switch (e) { | |
case Mult(Const(0), *): return Const(0); | |
case Mult(*, Const(0)): return Const(0); | |
case Mult(Const(1), var x): return Simplify(x); | |
case Mult(var x, Const(1)): return Simplify(x); | |
case Mult(Const(var l), Const(var r)): return Const(l * r); | |
case Add(Const(0), var x): return Simplify(x); | |
case Add(var x, Const(0)): return Simplify(x); | |
case Add(Const(var l), Const(var r)): return Const(l + r); | |
case Neg(Const(var k)): return Const(-k); | |
default: return e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment