Re: [julia-users] Re: Values from Dict assigned to "variables" (symbols?) named as keys?

2015-02-17 Thread Martin Johansson
Thanks, it seems composite types is a perfect fit for most of my 
needs(required fields and their size will typically be known at time of 
type definition for the present problem). 

Regards, m

On Monday, February 16, 2015 at 6:57:57 PM UTC+1, Tim Holy wrote:
>
> Composite types are much higher performance but have no flexibility (you 
> can't 
> add new fields later). So which you choose depends on your use case. 
>
> --Tim 
>
> On Monday, February 16, 2015 09:38:13 AM Martin Johansson wrote: 
> > Yes, composite types could well be the answer! I'm still trying to find 
> out 
> > which choices would make the transition from Matlab easiest. This (old) 
> > discussion <https://github.com/JuliaLang/julia/issues/1263>didn't seem 
> to 
> > recommend one over the other (of Dicts and composite types). 
> > 
> > Regards, m 
> > 
> > On Monday, February 16, 2015 at 4:30:31 PM UTC+1, Johan Sigfrids wrote: 
> > > Could you not use a composite type for this? It would seem more Julian 
> to 
> > > me. 
>
>

[julia-users] Re: Values from Dict assigned to "variables" (symbols?) named as keys?

2015-02-16 Thread Martin Johansson
Yes, composite types could well be the answer! I'm still trying to find out 
which choices would make the transition from Matlab easiest. This (old) 
discussion 
didn't seem to recommend 
one over the other (of Dicts and composite types). 

Regards, m


On Monday, February 16, 2015 at 4:30:31 PM UTC+1, Johan Sigfrids wrote:
>
> Could you not use a composite type for this? It would seem more Julian to 
> me. 



[julia-users] Re: Values from Dict assigned to "variables" (symbols?) named as keys?

2015-02-16 Thread Martin Johansson
Thanks, that's a good point regarding the scope and not a "side effect" I 
would want.

If I understand the keyword part correctly, I would have to explicitly 
state all the variables in the function definition to be able to use them 
locally which, as you write, can become verbose (very verbose for many of 
my functions, actually). In addition, the Dict that I call the function 
with is longer "preserved" (correct?) as a variable. I want to supply that 
Dict to many other functions at various levels below the function I first 
call. So that will not work. I guess explicitly using the Dict is the best 
way forward. That's the way I do with structs in Matlab, but with a 
somewhat more compact notation, so I can live with that.

Regards, m

On Monday, February 16, 2015 at 12:01:24 PM UTC+1, Michael Hatherly wrote:
>
> One thing to note about using @eval as mentioned in Sean’s reply is that 
> eval (and by extension @eval) does not evaluate expressions in a 
> function’s scope, but rather in the global scope of the module.
> This might have unexpected results:
>
> julia> function f(d)
>for (k, v) in d
>@eval $k = $v
>end
>end
> f (generic function with 1 method)
>
> julia> x
> ERROR: UndefVarError: x not defined
>
> julia> f(Dict(:x => 1))
>
> julia> x
> 1
>
> You could instead use a function with keyword arguments and pass it a 
> dictionary:
>
> julia> g(; x = error("provide x"), args...) = nothing
> g (generic function with 1 method)
>
> where args... discards any variables you don’t want to handle. x could 
> also be given a default value rather than throwing an error when not 
> provided.
> You’d then call it like so:
>
> julia> g(; Dict(:x => 1, :y => 2)...)
>
> Although it’s slightly more verbose, this way won’t result in side-effects 
> caused by using eval.
>
> — Mike
> On Monday, 16 February 2015 00:17:54 UTC+2, Martin Johansson wrote:
>>
>> Is there a way to "assign" the values of a Dict to the corresponding keys 
>> such that I get "variables" (symbols?) with names given by the keys? Let's 
>> assume the keys are for example strings which would work as variables.
>>
>> The reason for why I would want to do this is to have functions where 
>> explicit variables are used rather than Dicts, primarily for readability. 
>> I'm suspecting that this is not a recommended way of doing things (since I 
>> couldn't find any info along these lines when searching), and if this is 
>> the case please set me straight.
>>
>> Regards, m
>>
>>
>>

[julia-users] Re: Values from Dict assigned to "variables" (symbols?) named as keys?

2015-02-15 Thread Martin Johansson
Thanks, the second solution was what I was looking for!

Regards, m

On Sunday, February 15, 2015 at 11:41:02 PM UTC+1, Sean Marshallsay wrote:
>
> I'm not too certain what you're asking here.
>
> Do you mean something like this:
>
> julia> d = Dict()
> Dict{Any,Any} with 0 entries
>
> julia> for i in [:var1, :var2, :var3]
>d[i] = string(i)
>end
>
> julia> d
> Dict{Any,Any} with 3 entries:
>   :var3 => "var3"
>   :var1 => "var1"
>   :var2 => "var2"
>
> Or something more like this:
>
> julia> d = [:a=>1, :b=>2, :c=>3]
> Dict{Symbol,Int64} with 3 entries:
>   :b => 2
>   :c => 3
>   :a => 1
>
> julia> for (k,v) in d
> @eval $k = $v
>end
>
> julia> a
> 1
>
> julia> b
> 2
>
> julia> c
> 3
>
> Or maybe something else entirely?
>
> On Sunday, 15 February 2015 22:17:54 UTC, Martin Johansson wrote:
>>
>> Is there a way to "assign" the values of a Dict to the corresponding keys 
>> such that I get "variables" (symbols?) with names given by the keys? Let's 
>> assume the keys are for example strings which would work as variables.
>>
>> The reason for why I would want to do this is to have functions where 
>> explicit variables are used rather than Dicts, primarily for readability. 
>> I'm suspecting that this is not a recommended way of doing things (since I 
>> couldn't find any info along these lines when searching), and if this is 
>> the case please set me straight.
>>
>> Regards, m
>>
>>
>>

[julia-users] Values from Dict assigned to "variables" (symbols?) named as keys?

2015-02-15 Thread Martin Johansson
Is there a way to "assign" the values of a Dict to the corresponding keys 
such that I get "variables" (symbols?) with names given by the keys? Let's 
assume the keys are for example strings which would work as variables.

The reason for why I would want to do this is to have functions where 
explicit variables are used rather than Dicts, primarily for readability. 
I'm suspecting that this is not a recommended way of doing things (since I 
couldn't find any info along these lines when searching), and if this is 
the case please set me straight.

Regards, m




[julia-users] Re: Plotting with Julia versus Mathematica

2015-02-05 Thread Martin Johansson
Oops, I forgot to say that you need to use .+ and .- to get "outer" 
addition and subtraction between X and Y (assuming they are defined as in 
your example), but I guess you figured that out since you got it working.

If you by "Density plot" refer to Mathematica's 'DensityPlot' than maybe 
simply using contourf could be OK. Unfortunately I don't know much about 
the plotting facilities in Julia (read: PyPlot). 

//martin

On Thursday, February 5, 2015 at 4:20:26 PM UTC+1, Giacomo Kresak wrote:
>
> Yes, you right! 
> Do you know how to get Density plots with Julia? 
> GK 
>
> On Thursday, February 5, 2015 at 2:16:37 PM UTC+1, Martin Johansson wrote:
>>
>> Hi!
>>
>> You need to add explicit .* between the parentheses (I guess you copied 
>> the Mathematica code and forgot to add multiplication). That worked for me. 
>> Also, the (41253 .* l) part came out as 41253 .* "ell" (not "one") when 
>> I copied it, but that might be a browser problem on my end.
>>
>> //martin
>>
>> On Thursday, February 5, 2015 at 12:02:15 PM UTC+1, Giacomo Kresak wrote:
>>>
>>> *Good morning, *
>>>
>>> *Would you please give me some lights here: *
>>>
>>>
>>>   In [104]:
>>>
>>> IP(X,Y) = (0.0011) .* (cos(152.309 .* X - 1324.58 .* Y) + cos(152.309 
>>> .* X - 1050.42 .* Y) + cos(152.309 .* X - 776.265 .* Y) 
>>>
>>> + cos(152.309 .* X - 502.11 .* Y) + cos(152.309 .* X - 227.955 .* Y) +  2 
>>> .* cos(676.25 .* X) (cos(152.309 .* Y) 
>>>
>>> + cos(426.464 .* Y) + cos(700.619 .* Y) + cos(974.775 .* Y) + cos(1248.93 
>>> .* Y)) + 2 .* cos(414.279 .* X) (cos(182.77 .* Y) 
>>>
>>> + cos(456.926 .* Y) + cos(731.081 .* Y) + cos(1005.24 .* Y) + cos(1279.39 
>>> .* Y)) + cos(152.309 .* X + 227.955 .* Y) 
>>>
>>> + cos(152.309 .* X + 502.11 .* Y) + cos(152.309 .* X + 776.265 .* Y) 
>>>
>>> + cos(152.309 .* X + 1050.42 .* Y) + cos(152.309 .* X + 1324.58 .* Y)).^2
>>>
>>> Out[104]: IP (generic function with 1 method)
>>>
>>>
>>> In [105]:
>>>
>>>   fig = figure()
>>>
>>> X = linspace(-0.90, 0.90, 100)'
>>>
>>> Y = linspace(-0.90, 0.90, 100)
>>>
>>> R = sqrt(((1600 .* pi ./(41253 .* l)).^2) .* (X.^2 .+ Y.^2))
>>>
>>> Z = (2 .* besselj1(R) ./ R).^2 .* IP(X,Y)
>>>
>>> surf = plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0, 
>>> antialiased=false, cmap="coolwarm")
>>>
>>> zlim(0,1.0)
>>>
>>> ax = gca()
>>>
>>> ax[:zaxis][:set_major_locator](matplotlib[:ticker][:LinearLocator](10))
>>>
>>> ax[:zaxis][:set_major_formatter](matplotlib[:ticker][:FormatStrFormatter]("%.02f"))
>>>
>>> fig[:colorbar](surf, shrink=0.5, aspect=5)
>>>
>>> dimensions must match
>>> while loading In[105], in expression starting on line 5
>>>
>>>  in getindex at /Users/gilmoretto/.julia/v0.3/PyCall/src/PyCall.jl:642
>>>  in pysequence_query at 
>>> /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:743
>>>  in pytype_query at 
>>> /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:759
>>>  in convert at /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:808
>>>  in pycall at /Users/gilmoretto/.julia/v0.3/PyCall/src/PyCall.jl:812
>>>  in fn at /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:181
>>>  in close_queued_figs at 
>>> /Users/gilmoretto/.julia/v0.3/PyPlot/src/PyPlot.jl:295
>>>
>>>
>>> *I was able to plot the besselj1. But when I multiply it to IP(X,Y), I am 
>>> getting dimension matching issue!   *
>>>
>>> *What am I doing wrong? *
>>>
>>>
>>> *Do you know how to get a Density Plot of *
>>>
>>>
>>> *Z = (2 .* besselj1(R) ./ R).^2 .* IP(X,Y)*
>>>
>>> ...
>>
>>

[julia-users] Re: Plotting with Julia versus Mathematica

2015-02-05 Thread Martin Johansson
Hi!

You need to add explicit .* between the parentheses (I guess you copied the 
Mathematica code and forgot to add multiplication). That worked for me. 
Also, the (41253 .* l) part came out as 41253 .* "ell" (not "one") when I 
copied it, but that might be a browser problem on my end.

//martin

On Thursday, February 5, 2015 at 12:02:15 PM UTC+1, Giacomo Kresak wrote:
>
> *Good morning, *
>
> *Would you please give me some lights here: *
>
>
>   In [104]:
>
> IP(X,Y) = (0.0011) .* (cos(152.309 .* X - 1324.58 .* Y) + cos(152.309 .* 
> X - 1050.42 .* Y) + cos(152.309 .* X - 776.265 .* Y) 
>
> + cos(152.309 .* X - 502.11 .* Y) + cos(152.309 .* X - 227.955 .* Y) +  2 .* 
> cos(676.25 .* X) (cos(152.309 .* Y) 
>
> + cos(426.464 .* Y) + cos(700.619 .* Y) + cos(974.775 .* Y) + cos(1248.93 .* 
> Y)) + 2 .* cos(414.279 .* X) (cos(182.77 .* Y) 
>
> + cos(456.926 .* Y) + cos(731.081 .* Y) + cos(1005.24 .* Y) + cos(1279.39 .* 
> Y)) + cos(152.309 .* X + 227.955 .* Y) 
>
> + cos(152.309 .* X + 502.11 .* Y) + cos(152.309 .* X + 776.265 .* Y) 
>
> + cos(152.309 .* X + 1050.42 .* Y) + cos(152.309 .* X + 1324.58 .* Y)).^2
>
> Out[104]: IP (generic function with 1 method)
>
>
> In [105]:
>
>   fig = figure()
>
> X = linspace(-0.90, 0.90, 100)'
>
> Y = linspace(-0.90, 0.90, 100)
>
> R = sqrt(((1600 .* pi ./(41253 .* l)).^2) .* (X.^2 .+ Y.^2))
>
> Z = (2 .* besselj1(R) ./ R).^2 .* IP(X,Y)
>
> surf = plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0, 
> antialiased=false, cmap="coolwarm")
>
> zlim(0,1.0)
>
> ax = gca()
>
> ax[:zaxis][:set_major_locator](matplotlib[:ticker][:LinearLocator](10))
>
> ax[:zaxis][:set_major_formatter](matplotlib[:ticker][:FormatStrFormatter]("%.02f"))
>
> fig[:colorbar](surf, shrink=0.5, aspect=5)
>
> dimensions must match
> while loading In[105], in expression starting on line 5
>
>  in getindex at /Users/gilmoretto/.julia/v0.3/PyCall/src/PyCall.jl:642
>  in pysequence_query at 
> /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:743
>  in pytype_query at 
> /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:759
>  in convert at /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:808
>  in pycall at /Users/gilmoretto/.julia/v0.3/PyCall/src/PyCall.jl:812
>  in fn at /Users/gilmoretto/.julia/v0.3/PyCall/src/conversions.jl:181
>  in close_queued_figs at 
> /Users/gilmoretto/.julia/v0.3/PyPlot/src/PyPlot.jl:295
>
>
> *I was able to plot the besselj1. But when I multiply it to IP(X,Y), I am 
> getting dimension matching issue!   *
>
> *What am I doing wrong? *
>
>
> *Do you know how to get a Density Plot of *
>
>
> *Z = (2 .* besselj1(R) ./ R).^2 .* IP(X,Y)*
>
> ...