Group Abstract Group Abstract

Message Boards Message Boards

2
|
11.3K Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Creating a function from an expression containing variables

Posted 9 years ago
POSTED BY: Ta'a Nwa Dombou
2 Replies

A variation on the theme:

exprToFunction[expr_, vars_] := 
 Function[Evaluate[expr /. Thread[vars -> Array[Slot, Length[vars]]]]]

If you didn't insist on pure functions, you could get away with

exprToFunction[expr_, vars_] := Function[vars, expr]
POSTED BY: Gianluca Gorni

You can avoid ToExpression and ToString by :

exprToFunction[expr, vars] := Function @ expr /. MapIndexed[#1 -> Slot @@ #2 &, vars]

POSTED BY: Rolf Mertig
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard