Thursday, 14 August 2008

Lambda Expressions and Differentiation (continued)

Today, I made a presentation about the first article that I've posted on this blog. Since then I did some improvements on the code.

The one extra feature that I implemented was a parser for lambda expressions. One of the first things that I did when I started fiddling with lambda expression was trying to generate one from a string. To my disappointment, there's not such a feature included in the .NET Framework. So, I coded a humble one using CodeDom.

I'm sharing the last source code that I have. You're free to play with it with one condition. You have to share enhancements with me. :P

Also, I would like to let a challenge here. If you want to learn Lambda Expressions and don't have an idea to implement, try making code to simplify a polynomial function.

Finally, as always, please, let your comment here.

3 comments:

Leblanc and Janie Wedding said...

this is awesome..

i'm going definitely review this source code.

I've never used codecom to evaluate string containing c# code.

I do know of:
http://www.codeproject.com/KB/cs/cs-script_for_cp.aspx

might give you some ideas of other techniques you could use.


thanks!

-lm

jpbochi said...

I already have a better version of this parser.

It runs the compiler in a different AppDomain, so I can unload the new assembly that is generated every time a expression is parsed. It's in my list for future posts, but I can send you the current version if you want.

I was looking at the NPEG page. Is it a kind of regular expression alternative? I see that it share some concepts, but I don't see how one is related to the other. Maybe you could put a comparison in the project home page.

Please notify me when you publish a release of the source code.

thanks!
JP

Leblanc and Janie Wedding said...

>I see that it share some concepts, but I don't see how one is related to the other.

I agree not related at all.. only in that they use anonymous functions that return other anonymous functions.



>Please notify me when you publish a release of the source code.

download most recent from:
http://www.codeplex.com/NPEG/SourceControl/ListDownloadableCommits.aspx



>Is it a kind of regular expression alternative?

people with regex experience will be familiar with the concepts.

a closer alternative would be
http://www.codeplex.com/irony

based on bnf grammar rules, irony creates a parse tree that will break any valid input into the specified abstract syntax tree.


npeg uses a different style of grammar rules than irony. Called peg that yes look like regex rules with a lot more power. Just off of grammar rules i can parse the following boolean statement:
((((!X*Y*Z)+(!X*Y*!Z)+(X*Z))))

with regex you could create a parser ... but would need a lot of custom logic to keep track of the stack. (eventually not reusable... not so if using grammar.. with grammar you could eventually create all the rules to break down the c# syntax [and that's the benefit of pegs is there is no rules it cannot handle since it has infinite lookahead and backtracking using predicates and priority choice].)


-----------------------

In regards to this library:

Assuming i can successfully create a prototype circuit simulation tool.

The only change i plan on making is instead of taking in a c# function, i plan to accept string functions similar to matlab, maple and use npeg to break down the math expression into a tree.

see: MathematicalFormulaTest inside npeg unit test.

A visitor will then be used to evaluate/transform the expression, as done here in your code.