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

Save variables to file at runtime
In Julia, is it possible to pass values for evaluation in an Expr object without using global variables?
Metaprogramming in Julia — Splice integer into variable name
What's the difference between `use` and `#before_compile`?
How to define functions based on attribute to elixir?
Remove a const qualifier from a variable in D
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
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

Categories

HOME
ng-idle
mysql-workbench
mstest
oracle-apex-5
graphics
ms-access-2016
google-apps
web-hosting
jtextpane
owin
startup
gource
flash-player
libusb
package.json
mathprog
smartclient
gmm
charles
solrcloud
parcelable
resourcebundle
perlbrew
pic32
stackexchange
gpio
pylons
angular-translate
panoramas
mobx
record
database-comparison
dsx-desktop
searchkit
latitude-longitude
imagenet
zoomify
graphhopper
modernizr
projects-and-solutions
levenshtein-distance
apriori
wmp
microsoft-metro
oracle-service-bus
apollostack
importerror
vaadin-elements
jboss-4.2.x
sonarlint-eclipse
chai-as-promised
dbcontext
tinymce-3
posixct
remedy
amq
boxing
gameplay-kit
baidu
rsa-archer-grc
renaming
rmongo
vcloud-director-rest-api
drawstring
hclust
installshield-2010
dimension
observablecollection
respondcms
jparsec
httpruntime.cache
user-management
jbase
assertion
opl
java-money
gocql
morton-number
rubaxa-sortable
dock
fail2ban
cross-join
boost-program-options
encryption-asymmetric
rcaller
live555
matrix-inverse
memset
android-framework
cfcache
openargs
gwt-openlayers
code-duplication
isqlquery
printdialog
inappsettingskit
qtconcurrent
adobe-media-server
relative
delimited-text
message-passing
openfaces
blender-2.49
alsb
pbcopy
great-circle

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