# Making a Boolean Operator Not a Boolean Operator (And Operator Precedence)

**URL:** <https://discourse.julialang.org/t/making-a-boolean-operator-not-a-boolean-operator-and-operator-precedence/70138>\
**Category:** General Usage\
**Created:** [October 21, 2021, 9:45am UTC](https://discourse.julialang.org/t/making-a-boolean-operator-not-a-boolean-operator-and-operator-precedence/70138 "2021-10-21T09:45:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 21, 2021, 9:45am UTC](https://discourse.julialang.org/t/making-a-boolean-operator-not-a-boolean-operator-and-operator-precedence/70138/1 "2021-10-21T09:45:57Z")

</div>

I’ve defined the binary “parallel” operator as:

`∥(a::Number,b::Number) = 1/(1/a+1/b)`

This operation is very common in electronics, and is related to the harmonic mean.

I’ve also overloaded it with `∥(a::Component,b::Component)` to allow the paralleling of a custom data structure I created.

It works very well (although of course I’d prefer to type \par instead of \parallel, but I’ll save that for later)…

```julia
julia> 1 ∥ 2
0.6666666666666666

```

However, if I try cascading it, it doesn’t work so well.

```julia
julia> 1 ∥ 2 ∥ 3
TypeError: non-boolean (Float64) used in boolean context

Stacktrace:
 [1] top-level scope at In[48]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

I found from [this link](https://discourse.julialang.org/t/overloading-yields-error-typeerror-non-boolean-used-in-boolean-context/64132) that the problem is that Julia expects this operator to return a Boolean and performs conditional evaluation.

```julia
julia> Meta.@lower 1 ∥ 2 ∥ 3
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = 1 ∥ 2
└── goto #3 if not %1
2 ─ %3 = 2 ∥ 3
└── return %3
3 ─ return false
))))

```

1. Can I disable this Boolean treatment somehow? (Without custom macros)
2. How can I set its operator precedence to the same as `+`? (Since ∥ and + share the same arithmetic properties.)

Thanks!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 21, 2021, 10:31am UTC](https://discourse.julialang.org/t/making-a-boolean-operator-not-a-boolean-operator-and-operator-precedence/70138/2 "2021-10-21T10:31:55Z")

</div>

Re (2), you can’t - see Matt’s comment [here](https://stackoverflow.com/questions/51755911/override-precedence-of-operator-in-julia) and the linked post from Stefan here: [On adjoints and custom postfix and infix operators - #61 by StefanKarpinski](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/61)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [October 22, 2021, 12:33am UTC](https://discourse.julialang.org/t/making-a-boolean-operator-not-a-boolean-operator-and-operator-precedence/70138/3 "2021-10-22T00:33:48Z")

</div>

Okay, I think I’ve found a solution. 😉

Instead of overloading the parallel operator \parallel to return something non-Boolean, I’ve realized that the notation I seek exists so I’ve chosen to call my harmonic sum +\_p where p=-1.

This is similar to the notation for S\_p sum of powers, L\_p norms, and M\_p power means, in which cases p is used to signify the power that the arguments are raised to before performing the summing function. They’re defined as:

S\_p(x\_1,\ldots,x\_n)=\sum\_{i=1}^nx\_i^p,

L\_p(x\_1, \ldots, x\_n) = \left(\sum\_{i=1}^n |x\_i|^p\right)^{1/p}, and

M\_p(x\_1, \ldots, x\_n) = \left(\frac{1}{n}\sum\_{i=1}^n x\_i^p\right)^{1/p}.

Similarly I can define

a+\_pb=(a^p+b^p)^{1/p},

which is similar to the S\_p sum of powers (but taking the result to the 1/p), the L\_p norm (but without taking the absolute power), and the M\_p mean (but without dividing by n^{1/p}). They could be related as:

S\_p(x\_1,\ldots,x\_n)=(x\_1+\_p\ldots+\_px\_n)^p,

L\_p(x\_1,\ldots,x\_n)=(|x\_1|+\_p\ldots+\_p|x\_n|), and

M\_p(x\_1,\ldots,x\_n)=\left(\frac{1}{n}\right)^{1/p}(x\_1+\_p\ldots+\_px\_n).

For example, the quadratic sum of a and b is a+\_2 b = L\_2(a,b), and for my harmonic sum I can write:

`+₋₁(a::Number,b::Number) = 1/(1/a+1/b)`

(Super yay for allowing Unicode characters in operator names! 😃😃😃)

Fun sidenote, the distributive property a(b+\_pc)=ab+\_pac holds for any p\ne 0.

To incorporate the implicit meaning of a _harmonic sum_ where two rates add in harmony, I can refer to +\_{-1} as simply +\_h. So,

`+ₕ(a::Number,b::Number) = 1/(1/a+1/b)`

This notation is consistent with that for the Kolmogorov-Nagumo generalized f-mean M\_f, which is defined for some continuous monotone function f as:

M\_f(x\_1,\ldots,x\_n)=f^{-1}\left(\frac{1}{n}\sum\_{i=1}^nf(x\_i)\right).

I can write a generalized f-sum as

a+\_fb = f^{-1}(f(a)+f(b)).

M\_h is the harmonic mean where h(x)=x^{-1}, and with my notation, +\_h is the harmonic sum. Similarly, M\_q is the quadratic mean and +\_q is the quadratic sum where q(x)=x^2.
