public override RawExpression SimplifyInternal() { RawExpression sleft = this.Left.Simplify(); RawExpression sright = this.Right.Simplify(); NumberExpression nleft = sleft as NumberExpression; NumberExpression nright = sright as NumberExpression; if (nleft != null) { if (nleft.Number == 0) { return(new NumberExpression { Number = 0, }); } } else if (nright != null) { if (nright.Number == 1) { return(sleft); } } return(new DivExpression { Left = sleft, Right = sright, }); }
public void RawExpression2() { var e = new RawExpression(new StringLiteral("a")); Assert.IsFalse(e.IsTrivial); Assert.AreEqual("{= \"a\"}", e.ToString()); }
public override RawExpression SimplifyInternal() { RawExpression sleft = this.Left.Simplify(); RawExpression sright = this.Right.Simplify(); NumberExpression nleft = sleft as NumberExpression; NumberExpression nright = sright as NumberExpression; if (nleft != null && nleft.Number == 0) { return(new NegExpression { Op = sright, }); } else if (nright != null && nright.Number == 0) { return(sleft); } else { return(new SubExpression { Left = sleft, Right = sright, }); } }
private void TextBoxChanged() { RawExpression tempFunction = null; int tempUnitPixels = 0; double tempOriginX = 0; double tempOriginY = 0; try { tempFunction = RawExpression.Parse(textBoxFunction.Text); } catch (Exception e) { buttonRender.Enabled = false; labelErrorMessage.Text = "[Function]" + e.Message; return; } try { tempUnitPixels = int.Parse(textBoxUnitPixels.Text, NumberStyles.None); } catch (Exception e) { buttonRender.Enabled = false; labelErrorMessage.Text = "[UnitPixels]" + e.Message; return; } try { tempOriginX = double.Parse(textBoxOriginX.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign); } catch (Exception e) { buttonRender.Enabled = false; labelErrorMessage.Text = "[OriginX]" + e.Message; return; } try { tempOriginY = double.Parse(textBoxOriginY.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign); } catch (Exception e) { buttonRender.Enabled = false; labelErrorMessage.Text = "[OriginY]" + e.Message; return; } this.function = tempFunction; this.unitPixels = tempUnitPixels; this.originX = tempOriginX; this.originY = tempOriginY; buttonRender.Enabled = true; labelErrorMessage.Text = "(Ready)"; }
public void NextCharacter_NonEmptyStringAllChars_ReturnsCharactersThenThrows() { RawExpression expression = new RawExpression("foo"); Assert.AreEqual('f', expression.Next()); Assert.AreEqual('o', expression.Next()); Assert.AreEqual('o', expression.Next()); Assert.Throws <IndexOutOfRangeException>(() => expression.Next()); }
public void TestIterateWithBrackets() { RawExpression rawExpression1 = new RawExpression(0, "(x + y)(y^2 + 10) - (5)"); RawExpressionParser parser = new RawExpressionParser(rawExpression1); List <RawMultiplier> multipliers = parser.Iterator().ToList(); Assert.AreEqual(multipliers.Count, 2); Assert.AreEqual(multipliers[0].rawString, "(x + y)(y^2 + 10) "); Assert.AreEqual(multipliers[1].rawString, "(5)"); rawExpression1 = new RawExpression(0, "((1 - z) + 4) + (((4))) + ((x))"); parser = new RawExpressionParser(rawExpression1); multipliers = parser.Iterator().ToList(); Assert.AreEqual(multipliers.Count, 3); Assert.AreEqual(multipliers[0].rawString, "((1 - z) + 4) "); Assert.AreEqual(multipliers[1].rawString, "(((4))) "); Assert.AreEqual(multipliers[2].rawString, "((x))"); }
static void Main(string[] args) { IExpressionServiceClient client = ExpressionServiceConfiguration.CreateProtocolFactory() .WaitForClient <IExpressionServiceClient>( "localhost/" + ExpressionServiceConfiguration.ProtocolName, ExpressionServiceConfiguration.EndpointName ); string input = "x^2-(2+3)*x+2*3"; Console.WriteLine("Input = " + input); RawExpression expression = client.Parse(input); Console.WriteLine("Parse() = " + client.ToCode(expression)); RawExpression simplified = client.Simplify(expression); Console.WriteLine("Simplify() = " + client.ToCode(simplified)); RawExpression differentiated = client.Simplify(client.Different(simplified, "x")); Console.WriteLine("Different(x) = " + client.ToCode(differentiated)); double evaluated1 = client.Evaluate(client.Apply(expression, "x", 1)); Console.WriteLine("Evaluate(x=1) = " + evaluated1.ToString()); double evaluated2 = client.EvaluateWithArguments(expression, new Dictionary <string, double>() { { "x", 2 } }); Console.WriteLine("Evaluate(x=2) = " + evaluated2.ToString()); double solved = client.Solve(expression, "x", 0); Console.WriteLine("Solve(x) = " + solved.ToString()); Console.ReadLine(); }
public double Solve(RawExpression expression, string variable, double start) { Console.WriteLine("received request: Solve({0}, {1}, {2})", expression, variable, start); return(expression.Solve(variable, start)); }
public string ToCode(RawExpression expression) { return(expression.ToCode()); }
public double Evaluate(RawExpression expression) { Console.WriteLine("received request: Evaluate({0})", expression); return(expression.Execute(new Dictionary <string, double>())); }
public double EvaluateWithArguments(RawExpression expression, Dictionary <string, double> arguments) { Console.WriteLine("received request: EvaluateWithArguments({0})", expression); return(expression.Execute(arguments)); }
public RawExpression Different(RawExpression expression, string variable) { Console.WriteLine("received request: Different({0}, {1})", expression, variable); return(expression.Different(variable)); }
public RawExpression Apply(RawExpression expression, string variable, double value) { Console.WriteLine("received request: Apply({0}, {1}, {2})", expression, variable, value); return(expression.Apply(variable, value)); }
public RawExpression Parse(string input) { Console.WriteLine("received request: Parse({0})", input); return(RawExpression.Parse(input)); }
static void Main(string[] args) { { string e = "(k1 *Cs1 + k2 * Co2) / (k1 * C1 + k2 * C2)"; { RawExpression exp = RawExpression.Parse(e); RawExpression dk1 = exp.Different("k1").Simplify(); RawExpression dk2 = exp.Different("k2").Simplify(); Console.WriteLine("input:\t\t" + e); Console.WriteLine("parse:\t\t" + exp.ToCode()); Console.WriteLine("dk1:\t\t" + dk1.ToCode()); Console.WriteLine("dk2:\t\t" + dk2.ToCode()); } // cs1 = 128, Co2 = 1024, C1 = 256, C2 = 2048 Dictionary <string, double> values = new Dictionary <string, double> { { "Cs1", 128 }, { "Co2", 1024 }, { "C1", 256 }, { "C2", 2048 }, }; foreach (var p in values) { Console.WriteLine("{0} => {1}", p.Value, p.Key); } { RawExpression exp = RawExpression.Parse(e).Apply(values).Simplify(); RawExpression dk1 = exp.Different("k1").Simplify(); RawExpression dk2 = exp.Different("k2").Simplify(); Console.WriteLine("applied:\t" + exp.ToCode()); Console.WriteLine("dk1:\t\t" + dk1.ToCode()); Console.WriteLine("dk2:\t\t" + dk2.ToCode()); } } { string[] expressions = { "1", "1.2", "-3", "12+34", "56*78", "x^y", "x+2*y+4", "(x+y)*(3+4)", "exp(x+y)", "x+ln(y)", "x/y", "exp(x)/ln(x)", "sin(x)", "cos(x)", "tan(x)", "cot(x)", "sec(x)", "csc(x)", }; foreach (var e in expressions) { RawExpression exp = RawExpression.Parse(e); Console.WriteLine("input:\t\t" + e); Console.WriteLine("parse:\t\t" + exp.ToCode()); Console.WriteLine("simplified:\t" + exp.Simplify().ToCode()); Console.WriteLine("dx:\t\t" + exp.Different("x").Simplify().ToCode()); Console.WriteLine("dy:\t\t" + exp.Different("y").Simplify().ToCode()); Console.WriteLine("no x:\t\t" + exp.Apply("x", 0).Simplify().ToCode()); Console.WriteLine("no y:\t\t" + exp.Apply("y", 0).Simplify().ToCode()); Console.WriteLine(); Debug.Assert(exp.ToCode() == RawExpression.Parse(exp.ToCode()).ToCode()); } } { string[] expressions = { "(x-3)*(x-5)", "ln(x)/ln(2)-3", "(x-5)^2-4", }; foreach (var e in expressions) { RawExpression exp = RawExpression.Parse(e); Console.WriteLine("input:\t\t" + e); Console.WriteLine("parse:\t\t" + exp.ToCode()); for (int i = 0; i < 10; i++) { Console.WriteLine("solve:\t\t" + i.ToString() + " => " + exp.Solve("x", i).ToString()); } Console.WriteLine(); } } }
public void CanRead_NonEmptyString_ReturnsTrue() { RawExpression expression = new RawExpression("foo"); Assert.True(expression.CanRead()); }
public void PeekCharacter_NonEmptyString_ReturnsCharacter() { RawExpression expression = new RawExpression("foo"); Assert.AreEqual('f', expression.Peek()); }
public abstract void Visit(RawExpression expression);
private void RenderFunction() { int w = this.imageBuffer.Width - 2; int h = this.imageBuffer.Height - 2; int cx = this.imageBuffer.Width / 2; int cy = this.imageBuffer.Height / 2; double[, ,] points = new double[h, w, 4]; RawExpression efx = this.function.Simplify(); var edfdx = efx.Different("x").Simplify(); var edfdy = efx.Different("y").Simplify(); var fx = efx.ToCode(); var dfdx = edfdx.ToCode(); var dfdy = edfdy.ToCode(); UpdateMessage("Computing..."); gpu.FillPoints(this.imageBuffer.Width, this.imageBuffer.Height, this.unitPixels, this.originX, this.originY, fx, dfdx, dfdy, points); //int done = 0; //int max = w + h; //int total = Environment.ProcessorCount * 8; //Parallel.For(0, total, (i) => //{ // int dh = (h + total - h % total) / total; // int dw = (w + total - w % total) / total; // int starty = 1 + i * dh; // int endy = Math.Min(h + 1, starty + dh); // int startx = 1 + i * dw; // int endx = Math.Min(w + 1, startx + dw); // int loops = (endy - starty + 1) + (endx - startx + 1); // for (int y = starty; y < endy; y++) // { // double py = this.originY + (double)(y - cy) / this.unitPixels; // RawExpression efx = this.function.Apply("y", py).Simplify(); // RawExpression edfx = efx.Different("x").Simplify(); // Func<double, double> fx = efx.Compile("x"); // Func<double, double> dfx = edfx.Compile("x"); // for (int x = 1; x <= w; x++) // { // points[y - 1, x - 1, 0] = fx.Solve(dfx, points[y - 1, x - 1, 0]); // } // int current = Interlocked.Increment(ref done); // if (current % 10 == 0) // { // UpdateMessage(current.ToString() + "/" + max.ToString()); // } // } // for (int x = startx; x < endx; x++) // { // double px = this.originX + (double)(cx - x) / this.unitPixels; // RawExpression efy = this.function.Apply("x", px).Simplify(); // RawExpression edfy = efy.Different("y").Simplify(); // Func<double, double> fy = efy.Compile("y"); // Func<double, double> dfy = edfy.Compile("y"); // for (int y = 1; y <= h; y++) // { // points[y - 1, x - 1, 3] = fy.Solve(dfy, points[y - 1, x - 1, 3]); // } // int current = Interlocked.Increment(ref done); // if (current % 10 == 0) // { // UpdateMessage(current.ToString() + "/" + max.ToString()); // } // } //}); UpdateMessage("Rendering..."); BitmapData data = this.imageBuffer.LockBits(new Rectangle(0, 0, this.imageBuffer.Width, this.imageBuffer.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); for (int y = 1; y <= h; y++) { for (int x = 1; x <= w; x++) { double x1 = points[y - 1, x - 1, 0]; double y1 = points[y - 1, x - 1, 1]; double x2 = points[y - 1, x - 1, 2]; double y2 = points[y - 1, x - 1, 3]; if (!double.IsInfinity(x1) && !double.IsNaN(x1)) { Fill(data, x1, y1, cx, cy, w, h); } if (!double.IsInfinity(y2) && !double.IsNaN(y2)) { Fill(data, x2, y2, cx, cy, w, h); } } } this.imageBuffer.UnlockBits(data); UpdateMessage("(Ready)"); }
public RawLine() { node = new RawExpression(); guards = new List<RawExpression>(); children = new List<RawLine>(); }
public void PeekCharacter_EmptyString_ThrowsException() { RawExpression expression = new RawExpression(""); Assert.Throws <IndexOutOfRangeException>(() => expression.Peek()); }
public RawExpression Simplify(RawExpression expression) { Console.WriteLine("received request: Simplify({0})", expression); return(expression.Simplify()); }
public void CanRead_EmptyString_ReturnsFalse() { RawExpression expression = new RawExpression(""); Assert.False(expression.CanRead()); }
public void TestIterate() { // things are going right RawExpression rawExpression1 = new RawExpression(0, "-4 + 6 -9xy"); RawExpressionParser parser = new RawExpressionParser(rawExpression1); List <RawMultiplier> multipliers = parser.Iterator().ToList(); Assert.AreEqual(multipliers.Count, 3); Assert.AreEqual(multipliers[0].rawString, "4 "); Assert.AreEqual(multipliers[1].rawString, "6 "); Assert.AreEqual(multipliers[2].rawString, "9xy"); Assert.AreEqual(multipliers[0].coeff, -1); Assert.AreEqual(multipliers[1].coeff, 1); Assert.AreEqual(multipliers[2].coeff, -1); Assert.AreEqual(multipliers[0].offset, 1); Assert.AreEqual(multipliers[1].offset, 5); Assert.AreEqual(multipliers[2].offset, 8); parser = new RawExpressionParser(new RawExpression(0, "- +")); // things are going wrong try { multipliers = parser.Iterator().ToList(); Assert.Fail(); } catch (ParseException e) { Assert.AreEqual(e.index, 2); Assert.AreEqual(e.Message, "Summand symbols are expected here, not operand"); } parser = new RawExpressionParser(new RawExpression(0, " +78z")); try { multipliers = parser.Iterator().ToList(); Assert.Fail(); } catch (ParseException e) { Assert.AreEqual(e.index, 1); Assert.AreEqual(e.Message, "'+' sign is not expected to be here"); } parser = new RawExpressionParser(new RawExpression(0, "78z + uh + - 9")); try { multipliers = parser.Iterator().ToList(); Assert.Fail(); } catch (ParseException e) { Assert.AreEqual(e.index, 11); Assert.AreEqual(e.Message, "Summand symbols are expected here, not operand"); } parser = new RawExpressionParser(new RawExpression(0, "78z + (dfdg) + 78x - ")); try { multipliers = parser.Iterator().ToList(); Assert.Fail(); } catch (ParseException e) { Assert.AreEqual(e.index, 21); Assert.AreEqual(e.Message, "Symbols are expected after operand"); } }