Assign only if not already defined in Julia - julia

Is there simple Julia syntax for assigning to a variable only if it is undefined (or falsy)? I mean something like Ruby's x ||= NEW_VALUE. I have tried x || x=NEW_VALUE but it throws an error. Barring easy syntax, what function can I use to check if a variable is defined?

You can use the #isdefined macro: (#isdefined x) || (x = NEW_VALUE).

I've prepared a macro to deal with that little inconvenience.
macro ifund(exp)
local e = :($exp)
isdefined(Main, e.args[1]) ? :($(e.args[1])) : :($(esc(exp)))
end
Then in REPL:
julia> z
ERROR: UndefVarError: z not defined
julia> #ifund z=1
1
julia> z
1
julia> z=10
10
julia> #ifund z=2
10
julia> z
10
An example of interpolation:
julia> w
ERROR: UndefVarError: w not defined
julia> w = "$(#ifund w="start:") end"
"start: end"
julia> w
"start: end"
But, remember of the scope (y is in the scope of for-loop):
julia> y
ERROR: UndefVarError: y not defined
julia> for i=1:10 y = "$(#ifund y="") $i" end
julia> y
ERROR: UndefVarError: y not defined
Let me know if it works. I'm curious, because it's my first exercise with macros.
edited: Julia v1.0 adaptation done.

Related

Using ForwardDiff.jl for a function of many variables and parameters Julia

The github repo for ForwardDiff.jl has some examples. I am trying to extend the example to take in addition to a vector of variables, a parameter. I cannot get it to work.
This is the example (it is short so I will show it rather than linking)
using ForwardDiff
x = rand(5)
f(x::Vector) = sum(sin, x) .+ prod(tan, x) * sum(sqrt, x);
g = x -> ForwardDiff.gradient(f, x);
g(x) # this outputs the gradient.
I want to modify this since I use functions with multiple parameters as well as variables. As a simple modification I have tried adding a single parameter.
f(x::Vector, y) = (sum(sin, x) .+ prod(tan, x) * sum(sqrt, x)) * y;
I have tried the following to no avail:
fp = x -> ForwardDiff.gradient(f, x);
fp = x -> ForwardDiff.gradient(f, x, y);
y = 1
println("test grad: ", fp(x, y))
I get the following error message:
ERROR: LoadError: MethodError: no method matching (::var"#73#74")(::Array{Float64,1}, ::Int64)
A similar question was not answered in 2017. A comment led me to here and it seems the function can only accept one input?
The target function must be unary (i.e., only accept a single argument). ForwardDiff.jacobian is an exception to this rule.
Has this changed? It seems very limited to only be able to differentiate unary functions.
A possible workaround would be to concatenate the list of variables and parameters and then just slice the returned gradient to not include the gradients with respect to the parameters, but this seems silly.
I personally think it makes sense to have this unary-only syntax for ForwardDiff. In your case, you could just pack/unpack x and y into a single vector (nominally x2 below):
julia> using ForwardDiff
julia> x = rand(5)
5-element Array{Float64,1}:
0.4304735670747184
0.3939269364431113
0.7912705403776603
0.8942024934250143
0.5724373306715196
julia> f(x::Vector, y) = (sum(sin, x) .+ prod(tan, x) * sum(sqrt, x)) * y;
julia> y = 1
1
julia> f(x2::Vector) = f(x2[1:end-1], x2[end]) % unpacking in f call
f (generic function with 2 methods)
julia> fp = x -> ForwardDiff.gradient(f, x);
julia> println("test grad: ", fp([x; y])) % packing in fp call
test grad: [2.6105844240785796, 2.741442601659502, 1.9913192377198885, 1.9382805843854594, 2.26202717745402, 3.434350946190029]
But my preference would be to explicitly name the partial derivatives differently:
julia> ∂f∂x(x,y) = ForwardDiff.gradient(x -> f(x,y), x)
∂f∂x (generic function with 1 method)
julia> ∂f∂y(x,y) = ForwardDiff.derivative(y -> f(x,y), y)
∂f∂y (generic function with 1 method)
julia> ∂f∂x(x, y)
5-element Array{Float64,1}:
2.6105844240785796
2.741442601659502
1.9913192377198885
1.9382805843854594
2.26202717745402
julia> ∂f∂y(x, y)
3.434350946190029
Here's a quick attempt at a function which takes multiple arguments, the same signature as Zygote.gradient:
julia> using ForwardDiff, Zygote
julia> multigrad(f, xs...) = ntuple(length(xs)) do i
g(y) = f(ntuple(j -> j==i ? y : xs[j], length(xs))...)
xs[i] isa AbstractArray ? ForwardDiff.gradient(g, xs[i]) :
xs[i] isa Number ? ForwardDiff.derivative(g, xs[i]) : nothing
end;
julia> f1(x,y,z) = sum(x.^2)/y;
julia> multigrad(f1, [1,2,3], 4)
([0.5, 1.0, 1.5], -0.875)
julia> Zygote.gradient(f1, [1,2,3], 4)
([0.5, 1.0, 1.5], -0.875)
For a function with several scalar arguments, this evaluates each derivative separately, and perhaps it would be more efficient to use one evaluation with some Dual(x, (dx, dy, dz)). With large-enough array arguments, ForwardDiff.gradient will already perform multiple evaluations, each with some number of perturbations (the chunk size, which you can control).

Julia Metaprogramming: Function for Mathematical Series

I'm trying to build a function that will output an expression to be assigned to a new in-memory function. I might be misinterpreting the capability of metaprogramming but, I'm trying to build a function that generates a math series and assigns it to a function such as:
main.jl
function series(iter)
S = ""
for i in 1:iter
a = "x^$i + "
S = S*a
end
return chop(S, tail=3)
end
So, this will build the pattern and I'm temporarily working with it in the repl:
julia> a = Meta.parse(series(4))
:(x ^ 1 + x ^ 2 + x ^ 3 + x ^ 4)
julia> f =eval(Meta.parse(series(4)))
120
julia> f(x) =eval(Meta.parse(series(4)))
ERROR: cannot define function f; it already has a value
Obviously eval isn't what I'm looking for in this case but, is there another function I can use? Or, is this just not a viable way to accomplish the task in Julia?
The actual error you get has to do nothing with metaprogramming, but with the fact that you are reassigning f, which was assigned a value before:
julia> f = 10
10
julia> f(x) = x + 1
ERROR: cannot define function f; it already has a value
Stacktrace:
[1] top-level scope at none:0
[2] top-level scope at REPL[2]:1
It just doesn't like that. Call either of those variables differently.
Now to the conceptual problem. First, what you do here is not "proper" metaprogramming in Julia: why deal with strings and parsing at all? You can work directly on expressions:
julia> function series(N)
S = Expr(:call, :+)
for i in 1:N
push!(S.args, :(x ^ $i))
end
return S
end
series (generic function with 1 method)
julia> series(3)
:(x ^ 1 + x ^ 2 + x ^ 3)
This makes use of the fact that + belongs to the class of expressions that are automatically collected in repeated applications.
Second, you don't call eval at the appropriate place. I assume you meant to say "give me the function of x, with the body being what series(4) returns". Now, while the following works:
julia> f3(x) = eval(series(4))
f3 (generic function with 1 method)
julia> f3(2)
30
it is not ideal, as you newly compile the body every time the function is called. If you do something like that, it is preferred to expand the code once into the body at function definition:
julia> #eval f2(x) = $(series(4))
f2 (generic function with 1 method)
julia> f2(2)
30
You just need to be careful with hygiene here. All depends on the fact that you know that the generated body is formulated in terms of x, and the function argument matches that. In my opinion, the most Julian way of implementing your idea is through a macro:
julia> macro series(N::Int, x)
S = Expr(:call, :+)
for i in 1:N
push!(S.args, :($x ^ $i))
end
return S
end
#series (macro with 1 method)
julia> #macroexpand #series(4, 2)
:(2 ^ 1 + 2 ^ 2 + 2 ^ 3 + 2 ^ 4)
julia> #series(4, 2)
30
No free variables remaining in the output.
Finally, as has been noted in the comments, there's a function (and corresponding macro) evalpoly in Base which generalizes your use case. Note that this function does not use code generation -- it uses a well-designed generated function, which in combination with the optimizations results in code that is usually equal to the macro-generated code.
Another elegant option would be to use the multiple-dispatch mechanism of Julia and dispatch the generated code on type rather than value.
#generated function series2(p::Val{N}, x) where N
S = Expr(:call, :+)
for i in 1:N
push!(S.args, :(x ^ $i))
end
return S
end
Usage
julia> series2(Val(20), 150.5)
3.5778761722367333e43
julia> series2(Val{20}(), 150.5)
3.5778761722367333e43
This task can be accomplished with comprehensions. I need to RTFM...
https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions

Can I pass keyword args to do-blocks in julia?

Below is an example:
def(f::Function) = f
f1 = def() do x
x
end
f2 = def() do x, y
x, y
end
f3 = def() do x, y; z # this syntax is accepted
x, y, z
end
f1(1)
f2(1, 2)
f3(1, 2, z=3) # ERROR: LoadError: function #13 does not accept keyword arguments
The example below is not my actual use case, but it illustrates the question. How should I handle kwargs in a do-block?
As far as I know this is not possible. If you look at /src/julia-parser.scm file you can find that parsing of a do block is defined in parse-do here. And you can see that do-block is rewritten to an anonymous function that can only accept positional arguments in doargs variable separated by commas.
In this line you can see that ; explicitly terminates parsing of arguments of do-block. This means that:
julia> z = "hello"
"hello"
julia> f3 = def() do x, y; println(z) # this syntax is accepted
x, y, z
end
#24 (generic function with 1 method)
julia> f3(100, 1000)
hello
(100, 1000, "hello")
will work as the code past ; is treated as a part of do-block body.
You could try to put ( and ) around the arguments like this:
julia> f3 = def() do (x, y; z)
x, y, z
end
ERROR: syntax: invalid assignment location "; z"
julia> f3 = def() do (y; z)
y, z
end
ERROR: syntax: "begin
y
# REPL[52], line 1
z
end" is not a valid function argument name
This will go through parser, but then julia-syntax.scm will throw an error. This probably could be improved as:
f3 = def(function(x, y; z)
x, y, z
end)
works correctly (you can consider opening an issue for this on https://github.com/JuliaLang/julia/issues).
Until this is fixed the only thing you can do is to use anonymous function definition approach I have given above (although it is not very clean).
You can do this, you just need to pass the kwargs before the do:
julia> f(g, xs; kwargs...) = begin
println(kwargs...)
map(g,xs)
end
f (generic function with 1 method)
julia> f(1:5; thisworks=true) do x x^2 end
:thisworks => true
5-element Array{Int64,1}:
1
4
9
16
25

Escaping a collection of symbols

I'm trying to escape a collection of symbols so that way I get a collection of the variables, but am running into issues. Here's an MWE:
macro escape_all(x...)
:($(esc.(x))...)
end
x = 1
y = 2
z = 3
macroexpand(:(#escape_all x y z))
This returns
:(((:($(Expr(:escape, :x))), :($(Expr(:escape, :y))), :($(Expr(:escape, :z))))...,))
but what I'm looking for it to return is just
(x,y,z)
Calling Expr explicitely works:
julia> macro escape_all(xs...)
Expr(:tuple, esc.(xs)...)
end
#escape_all (macro with 1 method)
julia> #macroexpand #escape_all x y z
:((x, y, z))
But you can also use the following unquote syntax in a context where list-splicing (like ,# in Lisp, I guess) makes sense:
julia> macro escape_list(xs...)
:([$(esc.(xs)...)])
end
#escape_list (macro with 1 method)
julia> macro escape_f(xs...)
:(f($(esc.(xs)...)))
end
#escape_f (macro with 1 method)
julia> #macroexpand #escape_list x y z
:([x, y, z])
julia> #macroexpand #escape_f x y z
:((Main.f)(x, y, z))
Funnily, I never saw $(x...) to be talked about anywhere. I stumbled upon it recently reading someone's code. But it's mentioned in the current "latest" docs as splatting interpolation.

A simple interpolation using expressions in julia

I would like to form expressions like
julia> ex = :(a[$i,$j] + b[$i,$j])
because I am interested in writting a program that it is able to write a system of equations in which the user only gives the structure of them and then that julia writes explicitly the chunk of code involved. More generally, I would like to embed functions in a expression, something like
julia> ex = :(a[$myfunc(i,j),$j] + b[$i,$j])
Upon evaluating the first line of code, I get
ERROR: UndefVarError: i not defined
in eval(::Module, ::Any) at ./boot.jl:226
On the other hand, if I write
julia> ex = :(a[i,j] + b[i,j])
:(a[i,j] + b[i,j])
julia> for i in 1:2
for j in 1:2
println(eval(ex))
end
end
ERROR: UndefVarError: i not defined
in eval(::Module, ::Any) at ./boot.jl:226
[inlined code] from ./boot.jl:225
in anonymous at ./<no file>:4294967295
in eval(::Module, ::Any) at ./boot.jl:226
How is interpolation done in this case?
eval works at global scope, so it cannot reference any local variables. What you can do, however, is interpolate that expression into a larger chunk of code that's intended to be executed at the global scope:
julia> a = [1 2; 3 4]
b = [5 6; 7 8]
ex = :(a[i,j] + b[i,j]);
julia> #eval for i in 1:2
for j in 1:2
println($ex)
end
end
6
8
10
12
The #eval macro is a simple shorthand for quoting the passed syntax and passing it along to the eval() function. It'd probably even be better to define a custom function:
julia> #eval function f()
for i in 1:2
for j in 1:2
println($ex)
end
end
end
f (generic function with 1 method)
julia> f()
6
8
10
12
The usual caveats about eval apply: any passed code can be maliciously crafted to do nefarious or surprising things. Or just crash the program.

Resources