metaprogramming


In Julia, is it possible to pass values for evaluation in an Expr object without using global variables?


I'd like to be able to convert a string expression, e.g. "2.0*x*log(x)", to a function in Julia. The usual way to do pass the value of "x" would be through global variables:
julia> func1 = parse("2.0*x*log(x)");
julia> x = 2.718281828459;
julia> eval(func1)/2.0
2.7182818284589545
However, I'd like to know if I can avoid using global variables. I've tried the following two methods, but was not successful:
Method 1
julia> func2(x) = parse("2.0*x*log(x)");
julia> eval(func2(1.0))/2.0 # should return zero
2.7182818284589545
Method 2
julia> function new_func1(input_value)
x = input_value
eval(func1)
end
new_func1 (generic function with 1 method)
julia> new_func1(1.0)/2.0 # should return zero
2.7182818284589545
How about doing this:
ex = parse(string)
#eval f(x) = $ex
When I plugged in your string and the number 1 it returned 0, 2 returned 2.772588 and so on.
With func2, since you're not interpolating x within the function, the expression is being built with just symbol x. This is falling back to your global x you set when writing func1.
Try this:
julia> func2(x) = parse("2.0*$x*log($x)");
julia> eval(func2(1.0))
0.0
Or, alternatively,
julia> func2(x) = eval(parse("2.0*$x*log($x)"));
julia> func2(1.0)
0.0
It appears it's also possible to use #generated to do this:
string = "2.0*x*log(x)"
#generated function my_func(x)
return parse(string)
end
Sample output:
julia> println(my_func(1.0))
0.0
julia> println(my_func(exp(1.0))/2.0)
2.718281828459045
julia> println(my_func(0.5))
-0.6931471805599453
Thinking about this further, perhaps it would have been better to use the dot notation for multiplication:
string = "2.0.*x.*log(x)"
#generated function my_func(x)
return parse(string)
end
julia> x = [1.0 0.5 exp(1.0)]
1x3 Array{Float64,2}:
1.0 0.5 2.71828
julia> my_func(x)
1x3 Array{Float64,2}:
0.0 -0.693147 5.43656

Related Links

Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string
Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D
Meta construction capabilities?
How to manipulate the value of variables based on an array of symbols in Julia?
Multiple #:build macros
Julia automatically generate functions and export them
Dealing with end keyword in macros for array indices
Implementation of AnyMap and runtime overhead of `struct Port(u32);`
Can function application be overriden with Macro in Elixir?
“Registering” trait implementations + factory method for trait objects
Statement list composition
Rust syntax extension and traits

Categories

HOME
embedded
swt
serialization
fftw
mstest
jelastic
seo
simulink
big-o
babeljs
trac
nstableheaderview
modal-dialog
extjs4.2
syntaxnet
augeas
alamofire
jsonpath
libc
atlassian
altera
delphi-xe5
breadth-first-search
kendo-mobile
spring-async
libigl
mongoid6
comparator
maze
shared
outliers
linked-data
android-things
tpm
jaws
mockjax
hibernate-validator
card.io
nested-lists
modalviewcontroller
video-capture
xajax
lint
git-rewrite-history
health-monitoring
jvmti
doc
sharp-snmp
apollostack
android-exoplayer
xmldocument
phpdbg
getchar
automapper-5
twilio-click-to-call
bluez
er-diagram
check-mk
mouseleave
reachability
slackware
redbeard-ios
segment-io
file-uri
udpclient
refinerycms
uiactionsheet
nio2
invoke-sqlcmd
cycle2
maybe
linklabel
dynatree
fiware-monitoring
oracle-spatial
coda
evolus-pencil
nutiteq
fail2ban
jquery-forms-plugin
user-permissions
actionfilterattribute
mmwormhole
java-security
openbabel
ssis-data-flow
deform
paw
gii
alternate
gae-quotas
koala
conditionaltagsupport
scriptaculous
abstract-data-type
panda3d
sentestingkit
vfs
web-farm
faye
correlated-subquery
lambdaj
ikimagebrowserview
mysql-error-1205
binomial-heap
visualj#
fail-fast-fail-early
application-planning
flvplayback
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App