[julia-users] Re: if-elseif-else programmatically

2016-09-25 Thread lapeyre . math122a
This is probably more efficient in high dimensions. I am thinking mostly 
about 1,2, and 3.  I have many routines and
want to avoid keeping three versions of each in sync. Your method will 
perform worse in 1D, and maybe 2D and 3D  too.
But, I'm starting to think that, for most of my applications, the 
performance hit may be worth the reduction in code complexity.
 

On Sunday, September 25, 2016 at 12:09:14 AM UTC+2, David P. Sanders wrote:
>
>
>
> El sábado, 24 de septiembre de 2016, 21:26:52 (UTC+2), 
> lapeyre@gmail.com escribió:
>>
>> I want to generate something like an if-elseif-else construct or 
>> switch-case programmatically.
>>
>> The use I have in mind is to write an expression for taking a step in a 
>> random walk in n dimensions,
>> so I need 2*n branches.
>>
>
> This is not a good way to solve this problem. Since you are in n 
> dimensions, just use a vector of length n,
> so that x[i] is the position in direction i (rather than generating a 
> variable called x1, another x2, etc.). Then do something like this:
>
> julia> n = 10
>
> julia> x = zeros(n)
>
> julia> direction = rand(1:n)
>
> julia> if rand() < 0.5
>  x[direction] += 1
>else
>  x[direction] -= 1
>end
>
>>
>>

[julia-users] Re: if-elseif-else programmatically

2016-09-25 Thread lapeyre . math122a
This is probably more efficient in high dimensions. I am thinking mostly 
about 1,2, and 3.  I have many routines and
want to avoid keeping three versions of each in sync. Your method will 
perform worse in 1D, and maybe 2D and 3D  too.
But, I'm starting to think that, for most of my applications, the 
performance hit may be worth the reduction in code complexity.
 

On Sunday, September 25, 2016 at 12:09:14 AM UTC+2, David P. Sanders wrote:
>
>
>
> El sábado, 24 de septiembre de 2016, 21:26:52 (UTC+2), 
> lapeyre@gmail.com escribió:
>>
>> I want to generate something like an if-elseif-else construct or 
>> switch-case programmatically.
>>
>> The use I have in mind is to write an expression for taking a step in a 
>> random walk in n dimensions,
>> so I need 2*n branches.
>>
>
> This is not a good way to solve this problem. Since you are in n 
> dimensions, just use a vector of length n,
> so that x[i] is the position in direction i (rather than generating a 
> variable called x1, another x2, etc.). Then do something like this:
>
> julia> n = 10
>
> julia> x = zeros(n)
>
> julia> direction = rand(1:n)
>
> julia> if rand() < 0.5
>  x[direction] += 1
>else
>  x[direction] -= 1
>end
>
>>
>>

[julia-users] Re: if-elseif-else programmatically

2016-09-24 Thread Fengyang Wang
Oh yeah, or x[direction] += rand(-1:2:1) to avoid the need for the 
allocation in the first place.

On Sunday, September 25, 2016 at 1:01:47 AM UTC-4, Fengyang Wang wrote:
>
> You could even do x[direction] += rand([-1, 1]). The allocation can be 
> avoided by defining a global constant with [-1, 1] as its contents.
>
> On Saturday, September 24, 2016 at 6:54:47 PM UTC-4, Steven G. Johnson 
> wrote:
>>
>>
>>
>> On Saturday, September 24, 2016 at 6:09:14 PM UTC-4, David P. Sanders 
>> wrote:
>>>
>>> julia> if rand() < 0.5
>>>
>>
>> You can also do "if rand(Bool)"
>>
>

[julia-users] Re: if-elseif-else programmatically

2016-09-24 Thread Fengyang Wang
You could even do x[direction] += rand([-1, 1]). The allocation can be 
avoided by defining a global constant with [-1, 1] as its contents.

On Saturday, September 24, 2016 at 6:54:47 PM UTC-4, Steven G. Johnson 
wrote:
>
>
>
> On Saturday, September 24, 2016 at 6:09:14 PM UTC-4, David P. Sanders 
> wrote:
>>
>> julia> if rand() < 0.5
>>
>
> You can also do "if rand(Bool)"
>


[julia-users] Re: if-elseif-else programmatically

2016-09-24 Thread Steven G. Johnson


On Saturday, September 24, 2016 at 6:09:14 PM UTC-4, David P. Sanders wrote:
>
> julia> if rand() < 0.5
>

You can also do "if rand(Bool)"


[julia-users] Re: if-elseif-else programmatically

2016-09-24 Thread David P. Sanders


El sábado, 24 de septiembre de 2016, 21:26:52 (UTC+2), 
lapeyre@gmail.com escribió:
>
> I want to generate something like an if-elseif-else construct or 
> switch-case programmatically.
>
> The use I have in mind is to write an expression for taking a step in a 
> random walk in n dimensions,
> so I need 2*n branches.
>

This is not a good way to solve this problem. Since you are in n 
dimensions, just use a vector of length n,
so that x[i] is the position in direction i (rather than generating a 
variable called x1, another x2, etc.). Then do something like this:

julia> n = 10

julia> x = zeros(n)

julia> direction = rand(1:n)

julia> if rand() < 0.5
 x[direction] += 1
   else
 x[direction] -= 1
   end

>
>