Is it possible to define unary operators that act from the right?
For example, typically !
is placed on the right for factorial like 5!
.
Specifically, I’d like to define some operation like xˡ
and xʳ
or alternatively x^l
and x^r
.
If that’s not possible, how can I define it in the same way as +
and -
are defined so that +1
yields 1 and -1
yields -1 value without using parenthesis like +(1)
or -(1)
. How can I define the function ʳ(x) = foo(x)
so that I can use it without parenthesis like ʳx
, preferably with a right-handedness so that I can write xʳ
.
1 Like
The only unary operators in Julia are <: >: + - ! ~ ¬ √ ∛ ∜
, all of which are parsed as acting from the left. Edit: plus '
and .'
, of course, which are postfix operators.
You can’t define arbitrary new symbols as unary operators, only use/extend/redefine those that are already parsed that way. (Nor can you define arbitrary symbols to be binary operators, but this is less of a limitation since there are many more Unicode binary operators to choose from, particularly in 0.7 now that you can add operator suffixes/decorators via #22089.)
And superscripts, in particular, are too useful as part of identifiers to give them up for operators. But note that in 0.7 you can add superscripts to operators, e.g. you can define an operator +ʳ
.
Note for those finding this later:
To overload the postfix operator '
, you can import adjoint
from LinearAlgebra
, and overload the adjoint
function. It appears that the postfix operator '
always points to whatever is the definition of adjoint
, including any overloads.
For example, this approach is used in the definition of '
as a postfix operator for differentiation in ApproxFun.