The reason is & and | operators are bitwise in Julia, and have a higher 
precedence than == and !=.

julia> 1 == 1 & 3 == 3
false
julia> 1 == 1 && 3 == 3
true
julia> (1 == 1) & (3 == 3)
true
julia> 1 & 3
1

There are a few issues on this, combining the scalar bitwise and 
array-elementwise meanings into the same & and | operators is not ideal and 
can lead to confusion here. When in doubt use extra parentheses around 
conditionals, and use && or || for scalar comparisons.


On Tuesday, July 28, 2015 at 10:17:26 PM UTC-7, meib...@163.com wrote:
>
> #####R code
> f=c(0.5,0.5)
> n.al <- length(f)
> P.1 <- P.2 <- array(0,dim=rep(n.al,4))
>     for(a2 in 1:n.al) for(a1 in 1:n.al)
>     for(a3 in 1:n.al) for(a4 in 1:n.al)
>     {
>         if (a1==a3 & a2==a4)
>         {
>             P.2[a1,a2,a3,a4] <- f[a1]*f[a2]
>             P.1[a1,a2,a3,a4] <- f[a1]*f[a2]*(f[a1]+f[a2])/2
>         }
>         if (a1==a3 & a2!=a4)
>         {
>             P.1[a1,a2,a3,a4] <- f[a1]*f[a2]*f[a4]/2
>         }
>         if (a1!=a3 & a2==a4)
>         {
>             P.1[a1,a2,a3,a4] <- f[a1]*f[a3]*f[a2]/2
>         }
>     }
> #####julia code 
> f=Array(Float64,1,2)
> f[:,:]=0.5
> p1=Array(Float64,nal,nal,nal,nal)
> p2=Array(Float64,nal,nal,nal,nal)
> p1[:,:,:,:]=0.0
> p2[:,:,:,:]=0.0
>     for (a2 = 1:nal)  for (a1 = 1:nal)
> for (a3 = 1:nal)  for (a4 = 1:nal)
>    if (a1==a3 & a2==a4)
>       p2[a1,a2,a3,a4] = f[a1]*f[a2]
>   p1[a1,a2,a3,a4] = f[a1]*f[a2]*(f[a1]+f[a2])/2.0
>    end
>    
>    if (a1==a3 & a2!=a4)
>       p1[a1,a2,a3,a4] = f[a1]*f[a2]*f[a4]/2.0
>    end
>    
>    if (a1!=a3 & a2==a4)
>       p1[a1,a2,a3,a4] = f[a1]*f[a3]*f[a2]/2.0
>    end
> end end
> end end 
>
>
> <https://lh3.googleusercontent.com/-V-fR1Z3wCS0/VbhSoLSjSjI/AAAAAAAAAAM/rNp8Wb1-wk8/s1600/Untitled.png>
>
>

Reply via email to