7

I'm building a symbolic derivative engine. For example

let f = <@ fun x:double -> x * x @>
let df = der f

and the resulting expression will be

<@ 2 * x @>

The actual equations could be arbitrarily complex.

The generation of the derivatives are not too hard using recursive pattern matching and transformations but in the end I want to use the generated equations in tight numerical loops as if I had hand written them. This is numerical computing code so faster is always better ( if possible )

I've looked at the FSharpX quotation compiler but it looks like an interpreter rather than a compiler.

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
  • 6
    I also know maple, matlab, pylab and calculus by pen and paper. My question was about compiling code quotations. – bradgonesurfing Mar 10 '13 at 13:52
  • My suggestion was that you might wish to use languages that inherently support symbolic manipulation, instead of trying to implement it in other languages. – Ramon Snir Mar 10 '13 at 14:00
  • Also - many people have previously asked this. Just search on Google or StackOverflow. – Ramon Snir Mar 10 '13 at 14:01
  • 5
    Again please note my question is about code quotation compilation not specifically about symbolic math. Yes I know I can do symbolic math in other languages. That is beyond the point. – bradgonesurfing Mar 10 '13 at 14:08

1 Answers1

7

I have not tested this, but the code that translates F# quotations to LINQ expressions (and compiles them) has now moved from F# PowerPack into the F# Core library, so I think that is the most up-to-date version:

open Microsoft.FSharp.Linq.RuntimeHelpers

LeafExpressionConverter.EvaluateQuotation <@ 1 + 2 @>

and to use it for lambdas

let d=LeafExpressionConverter.EvaluateQuotation <@ fun y -> y+1.0 @> 
    :?> ( double -> double )

Console.WriteLine(d 10)

outputs

11

Note the cast at the end to convert the ''obj'' to a lambda of the correct type

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Very cool new stuff. No references to this on google except the very short undescriptive doc. btw if I had EvaluateQuotation <@ fun y -> y + 1 @> for example I get returned type obj. How to convert this to a proper lambda usable. I guess I need a cast. – bradgonesurfing Mar 10 '13 at 18:08
  • I guess the obvious next question is how do LINQ expressions get converted to real code at run time vs compile time? – bradgonesurfing Mar 10 '13 at 18:18