 |
|
 |
I hope that I do something wrong!
Sample you putted inside the article throws error.
In my opinion, method 'FindVariable' does not exist in class 'Expression'. What's going on?
(compile error: Error 1 'ExpressionFramework.Expression' does not contain a definition for 'FindVariable' 52 7 NumericalIntegration)
Greetings - Gajatko
|
|
|
|
 |
|
 |
I have written my own method:
static Variable FindVariable(Expression expr, string name)
{
return Array.Find(expr.Variables, delegate(Variable var) { return var.Name == name; });
}
But there is not present 'Expression.FindVariable' method anyway.
One more thing: Could you provide a list of functions which are supported by ExpressionFramework? Or maybe just a source code?
Greetings - Gajatko
|
|
|
|
 |
|
 |
Hi gajatko
Thanks for using the Code.
Due to some unknown and embarrassing reason, the uploaded source code is not updating itself, even if i try to reupload the latest source code.Really Sorry for that.
I have the newer version, which contains the definition of FindVariable + some code optimizations. Your Test Code is working fine, producing the correct answer with it. Please give me your email address so that i can send you the updated project.
|
|
|
|
 |
|
 |
<code>using System; using ExpressionFramework; namespace TestExpressionFramework { class Program { /// <summary> /// Method Called when Registered Operator is Encountered in Expression.Calculate() /// </summary> /// <param name="a">First Argument of Binary Operator</param> /// <param name="b">Second Argument of Binary Operator</param> /// <returns></returns> static double MyBinaryOperator(double a, double b) { return (a * b) / (a + b); } /// <summary> /// Method Called when Registered Operator is Encountered in Expression.ToString() /// </summary> /// <param name="a">First Argument of Binary Operator</param> /// <param name="b">Second Argument of Binary Operator</param> /// <returns></returns> static string MyBinaryOperatorFormatProvider(string a, string b) { return a + " @ " + b; //'@' is supposed to be the Operator Symbol } static void Main(string[] args) { Operator BinOp = new Operator( "@" //Operator Symbol through which it is recognized in the Expression ,10 //Operator Precedence, See Typical Precedence Conventions in OperatorLibrary.cs ,MyBinaryOperator //Reference to Actual Calculation Method ,MyBinaryOperatorFormatProvider //Reference to String Representation Method ); OperatorLibrary.Add(BinOp); //Registers the Operator in OperatorLibrary ExpressionFramework.Expression ex = new Expression("a @ b"); ex.FindVariable("a").Value = 3; ex.FindVariable("b").Value = 5; Console.WriteLine(ex.Calculate()); //should be 1.875 for 3 and 5 } } }</code>
|
|
|
|
 |
|
 |
thanks. my e-mail is e.g. "gajatko@interia.pl".
By the way, I had the same problem - I couldn't update files in my article; I don't remember how did I solved this problem, but propably removing file from the server, then accepting changes and then adding updated files fixes that.
"just a source code", I meant source of whole framework - I will be greatful if you could send it on gajatko@interia.pl, too (limit is 25 MB in one message). Or "just" publish it here, whatever. It is a Code Project website.
List of functions: I meant: sin, cos, tan, log, ... and what else?
Greetings - gajatko
|
|
|
|
 |
|
 |
First Of All Great Work! ... I was checking it (so that i could use it in my project) what i found out was that expression "4*(1+x)^(1/2)" throws an exception and when i further investigate it a little i found out that it is because your parser was unable to parse multiple braces and even (x*3)+(x^8) throws exception ... plz see throw it!
|
|
|
|
 |
|
 |
Thanks for the reply.
Please find the updated ExpressionFramework.zip in the updated tutorial (with example). Feel free to inform more issues.
|
|
|
|
 |
|
 |
Recently I had a project that required similar functionality as your code described. While beginning to build the code I discovered a wonderful free library for performing complex mathematical functions. The library is called dotmath. You should give it a look as it is very complete and stable as well as open to usage.
Just thought I would chime in.
Aaron VanWieren
GIS Software Engineer
GeoFields
|
|
|
|
 |
|
 |
Hi, Thank you for your kind suggestion. Actually it was my dream to write an extensible mathematical expression parsing library, just for fun. I did it during my academics, and just wanted to see how its done. Anyway i really liked the sample page provided by .Math Author
http://hebertsoft.com/dotMath/sample/index.aspx
|
|
|
|
 |
|
 |
It is definitely a fascinating and fun endevour. I found his library was a great solution to a project I am currently working on with a tight deadline. I would have loved to have had time to design and build something similar, but deadlines prevailed.
The impressive feature that was most beneficial in my project is the ability to handle undefined factors. I am currently working on creating a nested(multiple layers)formula builder and I have found that with a wrapper I could implement this functionality easily.
Great Fun
Aaron VanWieren
|
|
|
|
 |
|
 |
The function Sqrt for complex numbers returns incorrect results.
Regards
Chris Saunders
evas@mountaincable.net
|
|
|
|
 |
|
 |
Hi, Thank you Chris for using the library I think i had not provided the Sqrt Operator in the OperatorLibrary Class. One thing I should tell you that I have tried to make ExpressionFramework, extensible in terms of Operators. you can add your implementation like this. class Program { static double MySqrt(double a) { return Math.Sqrt(a); } static string MySqrtFormatString(string a) { return "Sqrt " + a; } static void Main(string[] args) { Operator sqrt = new Operator("Sqrt", 1, MySqrt, MySqrtFormatString); OperatorLibrary.Add(sqrt); Expression ex = new Expression("Sqrt 9"); Console.WriteLine(ex.Calculate()); } } Similarly you can implement your own Sqrt function for complex numbers and the Expression Class will be able to parse it from your expressions.
|
|
|
|
 |
|
 |
Hi Syed.
I have not been using your library but was inspecting your code.
The algorithm (If I remember correctly from class ComplexD) was not correct.
Regards
Chris Saunders
evas@mountaincable.net
|
|
|
|
 |
|
 |
Hi Chris If you take a look at my article, you will definitely find that i have no such class ComplexD, & I have never presented any algorithm for calculating Sqrt of Complex Numbers. I guess you are mixing your comments with someone else's Article.
|
|
|
|
 |
|
 |
Hi Syed.
My apologies for this mistake. I'm not sure how I made this error as the project I was attempting to respond to is called Sharp3D.Math. I hope you can arrange to remove these comments from this list.
Regards,
Chris Saunders
evas@mountaincable.net
|
|
|
|
 |
|
 |
I find it very useful. Something can be done to make it into a library. Well done.
|
|
|
|
 |
|
 |
Hi Thanks for reading the article. I think ExpressionFramework.dll (in the Project's Source Debug Directory) can already be used as a Library, as it was used in the Demo Project "Numerical Integration". See example below using ExpressionFramework; namespace NumericalIntegration { public partial class Form1 : Form { private void button1_Click(object sender, EventArgs e) { Expression t = new Expression(textBox1.Text); //Parse the input equation into Term Container int n = Int32.Parse(textBox2.Text); //Store number of intervals double b = Int32.Parse(textBox3.Text); //Store upper bound double a = Int32.Parse(textBox4.Text); //Store lower bound double h = (b - a) / n; //Calculate difference b/w bounds in terms of intervals double[] y = new double[n + 1]; //Create an array from Yo to Yn Variable x = t.Variables[0]; //Parse the first variable from the equation into x int j = 0; for (double i = a; i <= b; i += h) //loop from lower bound to upper bound incrementing by h { x.Value = i; y[j ++] = t.Calculate(); } double Y1 = 0; //for 2(Y3+Y6+Y9+....) double Y2 = 0; //for 3(rest of Ys) for (int i = 1; i < y.Length - 1; i++) { if (i % 3 == 0) Y1 += y[i]; else Y2 += y[i]; } double result = ((3 * h) / 8) * (y[0] + y[n] + (2 * Y1) + (3 * Y2)); textBox5.Text = result.ToString(); } } }
|
|
|
|
 |