Is there a way to include `⅔` in function name?

I want to define a function named ^⅔, which means

using SymPy: Sym

^⅔(x::Sym) = x^(2 // 3)
^⅔(x::Number) = x^(2 / 3)

That is, I need 2 // 3 for pretty-printing expressions in fractionals, but 2 / 3 for speed in real calculations. But it does not seem to be parsable:

julia> ^⅔(x) = x^(2 / 3)
ERROR: syntax: invalid character "⅔" near column 2
...

I wish to somehow keep the simple syntax a^⅔ while I can dispatch on different types of a.

^ is an infix operator, so you can’t use it in identifiers. You need to give your function a different name. (If you really want to, you could use var"^⅔" as a name, but then you always need to write it like that with the var.)

4 Likes

You could look through the table of unicode input for alternatives. For example, \invv\2/3 which yields ʌ⅔.

Another idea would be something like this:

using SymPy: Sym, @vars

struct TwoThirds end
const _⅔ = TwoThirds()
Base.:(^)(x::Number, ::TwoThirds) = x^(2 / 3)
Base.:(^)(x::Sym, ::TwoThirds) = x^(2 // 3)

Now:

julia> a, b = @vars a b;

julia> 3.4 ^ _⅔
 2.261097438656042

julia> a * b ^ _⅔
    2/3
a⋅b

You could also define other methods for TwoThirds() like + or *.

5 Likes

That’s exactly what I am using. Thank you!