Re: [julia-users] if .. else on a whole array
Note that both of these versions will return an array, allowing for type stability Den torsdag den 11. februar 2016 kl. 09.40.27 UTC+1 skrev Michael Borregaard: > > If you are worried about maintaining two versions, that can be solved > simply: > function foo(x::Vector{bar}) > #the main code of your function > end > > function foo(x::bar) > foo([x]) > end > > > >
Re: [julia-users] if .. else on a whole array
If you are worried about maintaining two versions, that can be solved simply: function foo(x::Vector{bar}) #the main code of your function end function foo(x::bar) foo([x]) end Den onsdag den 10. februar 2016 kl. 13.15.30 UTC+1 skrev Ferran Mazzanti: > > Thanks Mauro... > > using two methods was the first thing I thought, but I strongly dislike > the idea because I'd have to maintain two different functions > to do the same, which doubles the possibility of introducing bugs. I like > the idea of keeping the main function simple and unique, > so if I change something the changes apply to all calculations in the same > way. > > Best regards, > > Ferran. > > On Wednesday, February 10, 2016 at 12:11:00 PM UTC+1, Mauro wrote: >> >> Probably cleanest would be to make two methods, one for scalars, one for >> arrays. For the array one just loop. >> >> This also works, but returns an array for scalar input (type inference >> should work once wrapped in a function): >> >> julia> x = 5 >> 5 >> >> julia> [ xi>5 ? 0:1 for xi in x] >> 1-element Array{Any,1}: >> 1 >> >> julia> x = 1:10 >> 1:10 >> >> julia> [ xi>5 ? 0:blah for xi in x] >> 10-element Array{Any,1}: >> 1 >> 1 >> 1 >> 1 >> 1 >> 0 >> 0 >> 0 >> 0 >> 0 >> >> >> On Wed, 2016-02-10 at 11:58, Ferran Mazzanti>> wrote: >> > Hi folks, >> > >> > probably a stupid question but can't find the answer, so please help if >> you >> > can :) >> > I would like to evaluate a if.. else.. statement on a whole array. >> Actually >> > it's a bit more complicated, as I have a function that >> > previously was >> > >> > function u2(x) >> > return 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) >> > end; >> > >> > for some other defined function u2(x) and constant L_box. The thing is >> that >> > I could directly evaluate that on a scalar x and on an array. >> > Now I have to change it and check if x is smaller than Lbox/2, >> returning >> > the same as above if it is, or 0 otherwise. >> > I tried something of the form >> > >> > function u2(x) >> > return x.>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) >> > end; >> > >> > but that complains when x is an array. What would be the easiest way to >> > achieve this? It should work for both scalars and arrays... >> > >> > Best regards and thanks, >> > >> > Ferran. >> >
Re: [julia-users] if .. else on a whole array
If the input is a scalar and the output is a scalar, that's also type-stable. Type-stability is about "predictability of the output type, given the input types," not "always returns the same type." Also, wrapping scalars in an array will have a significant performance cost. Best, --Tim On Thursday, February 11, 2016 12:44:19 AM Michael Borregaard wrote: > Note that both of these versions will return an array, allowing for type > stability > > Den torsdag den 11. februar 2016 kl. 09.40.27 UTC+1 skrev Michael > > Borregaard: > > If you are worried about maintaining two versions, that can be solved > > simply: > > function foo(x::Vector{bar}) > > > > #the main code of your function > > > > end > > > > function foo(x::bar) > > > > foo([x]) > > > > end
Re: [julia-users] if .. else on a whole array
Really sorry, just saw this is identical to Mauro's suggestion :-/ Michael Den torsdag den 11. februar 2016 kl. 11.39.12 UTC+1 skrev Michael Borregaard: > > Thanks for clarifying that! > So do you suggest > [code] > function foo(x::bar) > #the function body > end > > function foo(x::AbstractVector{bar}) > map(foo, x) > end > [/code] > > On Thu, Feb 11, 2016 at 10:33 AM, Tim Holywrote: > >> If the input is a scalar and the output is a scalar, that's also >> type-stable. >> Type-stability is about "predictability of the output type, given the >> input >> types," not "always returns the same type." >> >> Also, wrapping scalars in an array will have a significant performance >> cost. >> >> Best, >> --Tim >> >> On Thursday, February 11, 2016 12:44:19 AM Michael Borregaard wrote: >> > Note that both of these versions will return an array, allowing for type >> > stability >> > >> > Den torsdag den 11. februar 2016 kl. 09.40.27 UTC+1 skrev Michael >> > >> > Borregaard: >> > > If you are worried about maintaining two versions, that can be solved >> > > simply: >> > > function foo(x::Vector{bar}) >> > > >> > > #the main code of your function >> > > >> > > end >> > > >> > > function foo(x::bar) >> > > >> > > foo([x]) >> > > >> > > end >> >> >
Re: [julia-users] if .. else on a whole array
You can try the @vectorize_1arg macro. Just define u2 as you did, then @vectorize_1arg Number u2. Or, if you don't want to use the macro, you can try a for loop. Example: function u2(x::AbstractArray) out = similar(x) for i in eachindex(x) out[i] = u2(x[i]) end return out end On Wednesday, February 10, 2016 at 7:15:30 AM UTC-5, Ferran Mazzanti wrote: > > Thanks Mauro... > > using two methods was the first thing I thought, but I strongly dislike > the idea because I'd have to maintain two different functions > to do the same, which doubles the possibility of introducing bugs. I like > the idea of keeping the main function simple and unique, > so if I change something the changes apply to all calculations in the same > way. > > Best regards, > > Ferran. > > On Wednesday, February 10, 2016 at 12:11:00 PM UTC+1, Mauro wrote: >> >> Probably cleanest would be to make two methods, one for scalars, one for >> arrays. For the array one just loop. >> >> This also works, but returns an array for scalar input (type inference >> should work once wrapped in a function): >> >> julia> x = 5 >> 5 >> >> julia> [ xi>5 ? 0:1 for xi in x] >> 1-element Array{Any,1}: >> 1 >> >> julia> x = 1:10 >> 1:10 >> >> julia> [ xi>5 ? 0:blah for xi in x] >> 10-element Array{Any,1}: >> 1 >> 1 >> 1 >> 1 >> 1 >> 0 >> 0 >> 0 >> 0 >> 0 >> >> >> On Wed, 2016-02-10 at 11:58, Ferran Mazzanti>> wrote: >> > Hi folks, >> > >> > probably a stupid question but can't find the answer, so please help if >> you >> > can :) >> > I would like to evaluate a if.. else.. statement on a whole array. >> Actually >> > it's a bit more complicated, as I have a function that >> > previously was >> > >> > function u2(x) >> > return 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) >> > end; >> > >> > for some other defined function u2(x) and constant L_box. The thing is >> that >> > I could directly evaluate that on a scalar x and on an array. >> > Now I have to change it and check if x is smaller than Lbox/2, >> returning >> > the same as above if it is, or 0 otherwise. >> > I tried something of the form >> > >> > function u2(x) >> > return x.>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) >> > end; >> > >> > but that complains when x is an array. What would be the easiest way to >> > achieve this? It should work for both scalars and arrays... >> > >> > Best regards and thanks, >> > >> > Ferran. >> >
Re: [julia-users] if .. else on a whole array
Thanks for clarifying that! So do you suggest [code] function foo(x::bar) #the function body end function foo(x::AbstractVector{bar}) map(foo, x) end [/code] On Thu, Feb 11, 2016 at 10:33 AM, Tim Holywrote: > If the input is a scalar and the output is a scalar, that's also > type-stable. > Type-stability is about "predictability of the output type, given the input > types," not "always returns the same type." > > Also, wrapping scalars in an array will have a significant performance > cost. > > Best, > --Tim > > On Thursday, February 11, 2016 12:44:19 AM Michael Borregaard wrote: > > Note that both of these versions will return an array, allowing for type > > stability > > > > Den torsdag den 11. februar 2016 kl. 09.40.27 UTC+1 skrev Michael > > > > Borregaard: > > > If you are worried about maintaining two versions, that can be solved > > > simply: > > > function foo(x::Vector{bar}) > > > > > > #the main code of your function > > > > > > end > > > > > > function foo(x::bar) > > > > > > foo([x]) > > > > > > end > >
Re: [julia-users] if .. else on a whole array
Probably cleanest would be to make two methods, one for scalars, one for arrays. For the array one just loop. This also works, but returns an array for scalar input (type inference should work once wrapped in a function): julia> x = 5 5 julia> [ xi>5 ? 0:1 for xi in x] 1-element Array{Any,1}: 1 julia> x = 1:10 1:10 julia> [ xi>5 ? 0:blah for xi in x] 10-element Array{Any,1}: 1 1 1 1 1 0 0 0 0 0 On Wed, 2016-02-10 at 11:58, Ferran Mazzantiwrote: > Hi folks, > > probably a stupid question but can't find the answer, so please help if you > can :) > I would like to evaluate a if.. else.. statement on a whole array. Actually > it's a bit more complicated, as I have a function that > previously was > > function u2(x) > return 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) > end; > > for some other defined function u2(x) and constant L_box. The thing is that > I could directly evaluate that on a scalar x and on an array. > Now I have to change it and check if x is smaller than Lbox/2, returning > the same as above if it is, or 0 otherwise. > I tried something of the form > > function u2(x) > return x.>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) > end; > > but that complains when x is an array. What would be the easiest way to > achieve this? It should work for both scalars and arrays... > > Best regards and thanks, > > Ferran.
Re: [julia-users] if .. else on a whole array
Thanks Mauro... using two methods was the first thing I thought, but I strongly dislike the idea because I'd have to maintain two different functions to do the same, which doubles the possibility of introducing bugs. I like the idea of keeping the main function simple and unique, so if I change something the changes apply to all calculations in the same way. Best regards, Ferran. On Wednesday, February 10, 2016 at 12:11:00 PM UTC+1, Mauro wrote: > > Probably cleanest would be to make two methods, one for scalars, one for > arrays. For the array one just loop. > > This also works, but returns an array for scalar input (type inference > should work once wrapped in a function): > > julia> x = 5 > 5 > > julia> [ xi>5 ? 0:1 for xi in x] > 1-element Array{Any,1}: > 1 > > julia> x = 1:10 > 1:10 > > julia> [ xi>5 ? 0:blah for xi in x] > 10-element Array{Any,1}: > 1 > 1 > 1 > 1 > 1 > 0 > 0 > 0 > 0 > 0 > > > On Wed, 2016-02-10 at 11:58, Ferran Mazzanti> wrote: > > Hi folks, > > > > probably a stupid question but can't find the answer, so please help if > you > > can :) > > I would like to evaluate a if.. else.. statement on a whole array. > Actually > > it's a bit more complicated, as I have a function that > > previously was > > > > function u2(x) > > return 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) > > end; > > > > for some other defined function u2(x) and constant L_box. The thing is > that > > I could directly evaluate that on a scalar x and on an array. > > Now I have to change it and check if x is smaller than Lbox/2, returning > > the same as above if it is, or 0 otherwise. > > I tried something of the form > > > > function u2(x) > > return x.>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) > > end; > > > > but that complains when x is an array. What would be the easiest way to > > achieve this? It should work for both scalars and arrays... > > > > Best regards and thanks, > > > > Ferran. >
Re: [julia-users] if .. else on a whole array
I'm not quite sure if this is what you're asking, but if you want a scalar function to work elementwise on an array, try `map`. --Tim On Wednesday, February 10, 2016 04:15:30 AM Ferran Mazzanti wrote: > Thanks Mauro... > > using two methods was the first thing I thought, but I strongly dislike the > idea because I'd have to maintain two different functions > to do the same, which doubles the possibility of introducing bugs. I like > the idea of keeping the main function simple and unique, > so if I change something the changes apply to all calculations in the same > way. > > Best regards, > > Ferran. > > On Wednesday, February 10, 2016 at 12:11:00 PM UTC+1, Mauro wrote: > > Probably cleanest would be to make two methods, one for scalars, one for > > arrays. For the array one just loop. > > > > This also works, but returns an array for scalar input (type inference > > should work once wrapped in a function): > > > > julia> x = 5 > > 5 > > > > julia> [ xi>5 ? 0:1 for xi in x] > > > > 1-element Array{Any,1}: > > 1 > > > > julia> x = 1:10 > > 1:10 > > > > julia> [ xi>5 ? 0:blah for xi in x] > > > > 10-element Array{Any,1}: > > 1 > > 1 > > 1 > > 1 > > 1 > > 0 > > 0 > > 0 > > 0 > > 0 > > > > On Wed, 2016-02-10 at 11:58, Ferran Mazzanti> > > > wrote: > > > Hi folks, > > > > > > probably a stupid question but can't find the answer, so please help if > > > > you > > > > > can :) > > > I would like to evaluate a if.. else.. statement on a whole array. > > > > Actually > > > > > it's a bit more complicated, as I have a function that > > > previously was > > > > > > function u2(x) > > > > > > return 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) > > > > > > end; > > > > > > for some other defined function u2(x) and constant L_box. The thing is > > > > that > > > > > I could directly evaluate that on a scalar x and on an array. > > > Now I have to change it and check if x is smaller than Lbox/2, returning > > > the same as above if it is, or 0 otherwise. > > > I tried something of the form > > > > > > function u2(x) > > > > > > return x.>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) > > > > > > end; > > > > > > but that complains when x is an array. What would be the easiest way to > > > achieve this? It should work for both scalars and arrays... > > > > > > Best regards and thanks, > > > > > > Ferran.
Re: [julia-users] if .. else on a whole array
So, using Tim's suggestion this would be: # assume anything which isn't an array is scalar u2(x, Lbox) = x>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) u2(x::AbstractArray, Lbox) = map( x->u2(x,Lbox), x) So, no code duplication. However, on the current release writing the second function with a loop will be more preformant (and no code duplication either). On Wed, 2016-02-10 at 13:21, Tim Holywrote: > I'm not quite sure if this is what you're asking, but if you want a scalar > function to work elementwise on an array, try `map`. > > --Tim > > On Wednesday, February 10, 2016 04:15:30 AM Ferran Mazzanti wrote: >> Thanks Mauro... >> >> using two methods was the first thing I thought, but I strongly dislike the >> idea because I'd have to maintain two different functions >> to do the same, which doubles the possibility of introducing bugs. I like >> the idea of keeping the main function simple and unique, >> so if I change something the changes apply to all calculations in the same >> way. >> >> Best regards, >> >> Ferran. >> >> On Wednesday, February 10, 2016 at 12:11:00 PM UTC+1, Mauro wrote: >> > Probably cleanest would be to make two methods, one for scalars, one for >> > arrays. For the array one just loop. >> > >> > This also works, but returns an array for scalar input (type inference >> > should work once wrapped in a function): >> > >> > julia> x = 5 >> > 5 >> > >> > julia> [ xi>5 ? 0:1 for xi in x] >> > >> > 1-element Array{Any,1}: >> > 1 >> > >> > julia> x = 1:10 >> > 1:10 >> > >> > julia> [ xi>5 ? 0:blah for xi in x] >> > >> > 10-element Array{Any,1}: >> > 1 >> > 1 >> > 1 >> > 1 >> > 1 >> > 0 >> > 0 >> > 0 >> > 0 >> > 0 >> > >> > On Wed, 2016-02-10 at 11:58, Ferran Mazzanti > > >> > > wrote: >> > > Hi folks, >> > > >> > > probably a stupid question but can't find the answer, so please help if >> > >> > you >> > >> > > can :) >> > > I would like to evaluate a if.. else.. statement on a whole array. >> > >> > Actually >> > >> > > it's a bit more complicated, as I have a function that >> > > previously was >> > > >> > > function u2(x) >> > > >> > > return 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) >> > > >> > > end; >> > > >> > > for some other defined function u2(x) and constant L_box. The thing is >> > >> > that >> > >> > > I could directly evaluate that on a scalar x and on an array. >> > > Now I have to change it and check if x is smaller than Lbox/2, returning >> > > the same as above if it is, or 0 otherwise. >> > > I tried something of the form >> > > >> > > function u2(x) >> > > >> > > return x.>Lbox/2 ? 0 : 0.5*(u2_0(x)+u2_0(Lbox-x))-u2_0(Lbox/2) >> > > >> > > end; >> > > >> > > but that complains when x is an array. What would be the easiest way to >> > > achieve this? It should work for both scalars and arrays... >> > > >> > > Best regards and thanks, >> > > >> > > Ferran.