# On adjoints and custom postfix and infix operators

**URL:** <https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863>\
**Category:** Internals & Design\
**Created:** [August 3, 2018, 7:24am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863 "2018-08-03T07:24:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 7:24am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/1 "2018-08-03T07:24:25Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/27):
>
> > [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/24):
> >
> > (I wish that they’d gotten rid of the hard-coded `f' = adjoint(f)` as well.)
> 
> Why hard-coded `f' = adjoint(f)` is annoying? Is there any example for this?

I’d like `'` to be a post-fix operator that maps `f'` to `'(f)` where `Base.:'(x::AbstractMatrix) = adjoint(x)` and the user is free to add other methods that have nothing to do with adjoints. (For example, some people might like `f'` to refer to df/dt.)

Of course it is possible to overload `adjoint` to the same effect, but aesthetically this is less satisfying. It looks like I’m saying “the adjoint of my object is …” when really I’m saying “the effect of the postfix `'` on my object is …”

Edit: With the operator suffixes introduced in 0.7, I would also like `f'ᵃ` to map to `'ᵃ(f)`, and so on, allowing the user to define his own postfix operators. This would make it possible to have `Base.:'ᵀ(x::AbstractMatrix) = transpose(x)`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 3, 2018, 7:26am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/2 "2018-08-03T07:26:13Z")

</div>

That’s much better! I thought you wanted everyone to write `adjoint(A) * b` instead of `A' * b`, so I almost fell out of my chair 😃

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [August 3, 2018, 7:30am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/3 "2018-08-03T07:30:41Z")

</div>

Maybe we could have self-defined operators like Haskell in the future? And you can define your own operators rather then hard-coded operators.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [August 3, 2018, 7:44am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/4 "2018-08-03T07:44:16Z")

</div>

Though I am not against different meanings for postfix `'` per se, I used `ApproxFun` in combination with matrix-valued functions, say `A(x)`, and was bitten by `A'` not being the adjoint but the derivative of that function more than once.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 8:03am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/5 "2018-08-03T08:03:31Z")

</div>

Overloading functions and operators can always lead to ambiguous situations, but all other functions and operators in Julia (as far as I know) can be overloaded directly. The `'` is a special case in a language that otherwise avoids special-casing whenever possible.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 8:20am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/6 "2018-08-03T08:20:08Z")

</div>

Oh, and while we’re on the subject, I would have liked `ᵀ` to be a postfix operator as well. This would have been a pain for everyone who currently names their variable `Aᵀ` in order to indicate that it is using row-major storage, but I feel they could re-write their code to make `A` a `Transpose` object which would probably make `Aᵀ` behave just as before with no performance penalty.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 3, 2018, 8:20am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/7 "2018-08-03T08:20:44Z")

</div>

This seems like a pretty obvious improvement. Has it been decided against, or is it just not prioritized?

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [August 3, 2018, 8:24am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/8 "2018-08-03T08:24:11Z")

</div>

Oh then we can also have `ᴴ` as a postfix operator instead of `adjoint`. But why not just let the parser support Haskell like self defined operators…

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [August 3, 2018, 8:25am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/9 "2018-08-03T08:25:48Z")

</div>

Yes this is an issue in ApproxFun and hopefully future versions of Julia allows overriding `’` and `adjoint ` separately. Until then, there might be a work around possible by supporting `Adjoint(f)`…

Edit: or just use `transpose(f) `

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 3, 2018, 8:30am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/10 "2018-08-03T08:30:33Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/33):
>
> Overloading functions and operators can always lead to ambiguous situations, but all other functions and operators in Julia (as far as I know) can be overloaded directly. The `'` is a special case in a language that otherwise avoids special-casing whenever possible.

No, many operators are overloaded indirectly.  
`getindex`, `hcat`, `vcat`, `vect`, `braces` and `bracescat`.  
They are all a bit nontradition as far as operators go, as they are circumfix.  
But `adjoint` is also a bit nontraditional as the only postfix operator.  
It might be nice. It is what it is.

> I would have liked `ᵀ` to be a postfix operator as well.

That also might be nice.  
But AFAIK it won’t happen until julia 2.0.  
0.7 has now been cut, and is not taking not features.  
That would require a deprecation.  
1.0 is not going to be introducing any new deprecations.  
1.x might, but that kinda language level change can’t be resolved without a major release (via semver rules).

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [August 3, 2018, 8:32am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/11 "2018-08-03T08:32:00Z")

</div>

They were complex-matrix-valued 🙂

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [August 3, 2018, 8:34am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/12 "2018-08-03T08:34:43Z")

</div>

> They were complex-matrix-valued ![:slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=5)

Ah, `conj(transpose(f))` or better yet `adjoint.(f)`?

This makes more sense than using `adjoint` as it’s not clear what the “adjoint” of a function (or matrix-valued function) should actually mean.

Note `adjoint(::Matrix{<:Fun})` is completely broken since `adjoint` is recursive…

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [August 3, 2018, 8:36am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/13 "2018-08-03T08:36:53Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/38):
>
> 1.x might, but that kinda language level change can’t be resolved without a major release (via semver rules).

As long as existing code still works (which wouldn’t be affected by adding postfix operators like `ᵀ`) then semver says adding it to `1.x` is fine.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 8:38am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/14 "2018-08-03T08:38:33Z")

</div>

But existing code wouldn’t work, because `Aᵀ` is currently a legal variable name.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [August 3, 2018, 8:43am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/15 "2018-08-03T08:43:42Z")

</div>

Personally, I think this won’t be solved until Julia can add self-defined operators by users, people may want more and this cannot always done by asking the parser to parse an operator.

We have already have a bunch of operators written in the parser, but they won’t be used frequently. And since they are all defined globally, it can be ambiguous like `'`. Asking to parse a new operator each time does not look like sustainable.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 8:49am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/16 "2018-08-03T08:49:16Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/36):
>
> But why not just let the parser support Haskell like self defined operators…

That would be cool, but would at minimum require spaces around operators to avoid ambiguity.

As it is, you can easily create a macro that lets you write, for example: `my_syntax" Aᵀb "` and have that expand to `transpose(A)*b`.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [August 3, 2018, 8:52am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/17 "2018-08-03T08:52:18Z")

</div>

Actually, the [QuDirac.jl](https://github.com/JuliaQuantum/QuDirac.jl) did so. However, manipulating raw strings is painful… I remember there was someone asking for supporting Dirac notation in the parser: `|>`. But this is quite painful…

And as a Julian, I would like to define our operators in Julia rather than flisp 😉

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [August 3, 2018, 8:54am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/18 "2018-08-03T08:54:45Z")

</div>

But we can work around this in the following `1.x` versions in this way I guess

```julia
syntax"Aᵀb"

```

Maybe your guys will want some utils for this kind of personal operator syntax? We can make a package.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 9:03am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/19 "2018-08-03T09:03:43Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/45):
>
> as a Julian, I would like to define our operators in Julia rather than flisp

There exists [a Julia parser written in Julia](https://github.com/JuliaLang/JuliaParser.jl) (MIT license). It has no recent commits, so some work would be required to bring it up to 0.7…

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [August 3, 2018, 9:35am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/20 "2018-08-03T09:35:44Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/35):
>
> Has it been decided against, or is it just not prioritized?

I have no idea. I have a vague memory of having suggested this some time ago, but I can’t seem to find the discussion, and I don’t even remember if it was on discourse or github. At any rate, it would be a non-breaking change and it only affects aesthetics, so there’s no rush.

[Next page](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863.md?page=2)
