It makes sense now.  The ternary operator only evaluates one branch, while 
a function would force both to be evaluated.  So the answer to the original 
question should be

inside_disc(x,y,radius) = ifelse((sqrt(x.^2 .+ y.^2) .< radius), 1 , 0)

This should be faster than the map version. 

Amazingly (at least to me), the ifelse version is only 10% faster than the 
map version when processing million item vectors of floats, and generates 
just a slight amount more garbage. 

On Saturday, June 7, 2014 1:05:20 PM UTC-4, Stefan Karpinski wrote:
>
> That's because ifelse is just a function, not an operator with special 
> syntax. The ternary operator is just a one-line form of the if-else and 
> requires the condition to be a single boolean value. It is a genuine 
> control flow construct, not a function.
>
>
> On Fri, Jun 6, 2014 at 11:54 PM, David Einstein <[email protected] 
> <javascript:>> wrote:
>
>> It looks like you have vectorized ifelse, the operator version just 
>> doesn't work.  This code is on line 379 of operators.jl
>>
>> function ifelse(c::AbstractArray{Bool}, x, y)
>>     reshape([ifelse(ci, x, y) for ci in c], size(c))
>> end
>>
>> If I do
>>
>> ifelse((1:10 .< 5) , 1 , 0)
>>
>> then everything works.
>>
>> however
>>
>> (1:10 .< 5) ? 1 : 0
>>
>> does not.  I'm not sure why.
>>
>> Also it appears that ifelse is not automatically imported like the rest 
>> of the operators from base are.
>>
>>
>> On Friday, June 6, 2014 10:58:10 PM UTC-4, Stefan Karpinski wrote:
>>
>>> We should probably vectorize the ifelse function.
>>>
>>> On Jun 6, 2014, at 10:51 PM, David Einstein <[email protected]> wrote:
>>>
>>> sqrt(x.^2 .+ y.^2) .< radius 
>>> is a vector of booleans
>>>
>>> ?:
>>> expects a boolean
>>>
>>> you probably want something like:
>>>
>>> inside_disc(x,y,radius) = map(good -> good ? 1 : 0, 
>>> sqrt(x.^2+y.^2).<radius)
>>>
>>>
>>>
>>> On Friday, June 6, 2014 10:18:36 PM UTC-4, Zahirul ALAM wrote:
>>>>
>>>> When I pass two arrays the function returns:
>>>>
>>>> type: non-boolean (BitArray{1}) used in boolean context while loading 
>>>> In[10], in expression starting on line 1
>>>>
>>>>
>>>> I have modified the function to address the element wise operation as 
>>>> follows:
>>>>
>>>>
>>>> inside_disc(x,y,radius) = sqrt(x.^2 .+ y.^2) .< radius ? 1 : 0
>>>>
>>>> seems to me that the error is being thrown by .< operation. What am I 
>>>> missing?
>>>>
>>>>
>>>> On Friday, 6 June 2014 20:34:22 UTC-4, Miguel Bazdresch wrote:
>>>>>
>>>>> I'm not sure I understand the question. Do you mean something like 
>>>>> this?
>>>>>
>>>>> inside_disc(x,y,radius) = sqrt(x^2+y^2)<radius ? 1 : 0
>>>>>
>>>>> -- mb
>>>>>
>>>>>
>>>>> On Fri, Jun 6, 2014 at 8:28 PM, Zahirul ALAM <[email protected]> 
>>>>> wrote:
>>>>>
>>>>>> I guess one can do a for loop. But how do I vectorize the code?
>>>>>>
>>>>>>
>>>>>> On Friday, 6 June 2014 20:27:46 UTC-4, Zahirul ALAM wrote:
>>>>>>>
>>>>>>> How would one implement a step function like behaviour in julia? In 
>>>>>>> mathematica one can write the following to create a circle with value 
>>>>>>> of 1 
>>>>>>> within the radius and 0 outside
>>>>>>>
>>>>>>> UnitBox[Sqrt[X^2 + Y^2]*0.5/radius];
>>>>>>>
>>>>>>> X and Y are the coordinates. 
>>>>>>>
>>>>>>
>>>>>
>

Reply via email to