I wonder why these can’t be parsed.

julia> a, b = 2, 3;

julia> a'b
6

julia> 2'b
6

julia> a'3
ERROR: ParseError:
# Error @ REPL[5]:1:3
a'3
# ╙ ── extra tokens after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

julia> 2'3
ERROR: ParseError:
# Error @ REPL[6]:1:3
2'3
# ╙ ── extra tokens after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

The apostrophe is not the dot product operator, but the adjoint operator (conjugate transpose). The dot product operator is the centred dot (·, which is in many keyboards, but can be also written by the escape secuence \cdot followed by tab). But you need the module LinearAlgebra to use it:

julia> using LinearAlgebra

julia> a, b = 2, 3;

julia> a·b
6

julia> 2·b
6

julia> a·3
6

julia> 2·3
6

Actually, I would also have expected an error in the two first examples (a'b and 2'b). It seems that the “syntax sugar” that allows to omit the multiplication operator in expressions like 2b (equivalent to 2*b) made its magic also in them, but I thought that it only worked in the very specific case of numbers followed without space by a variable name .

2 Likes

I think the adjoint operator’s parsing is separate from numeric literal coefficients’, they just happen to both do implicit *.

julia> x = [3, 4im]
2-element Vector{Complex{Int64}}:
 3 + 0im
 0 + 4im

julia> x'x # compute the dot product, equivalently x' * x
25 + 0im

julia> Meta.@lower a'b
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = *
│   %2 = var"'"(a)
│   %3 = b
│   %4 = (%1)(%2, %3)
└──      return %4
))))
1 Like

Also this

julia> 3'
3

julia> -3'
ERROR: ParseError:
# Error @ REPL[1]:1:3
-3'
# ╙ ── extra tokens after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

Looks like a bug of the adjoint operator ' when used next to number literals. True that at least the given examples are maybe too trivial, but the combination of cases where it works (without even a warning) at it doesn’t seem inconsistent.

1 Like