Skip to content

Commit 3f9faf3

Browse files
authored
lower a' as var"'"(a) (#34634)
1 parent 7583d87 commit 3f9faf3

File tree

8 files changed

+32
-24
lines changed

8 files changed

+32
-24
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ Language changes
7070
* The line number of function definitions is now added by the parser as an
7171
additional `LineNumberNode` at the start of each function body ([#35138]).
7272

73+
* Statements of the form `a'` now get lowered to `var"'"(a)` instead of `Base.adjoint(a)`. This
74+
allows for shadowing this function in local scopes, although this is generally discouraged.
75+
By default, Base exports `var"'"` as an alias of `Base.adjoint`, so custom types should still
76+
extend `Base.adjoint`. ([#34634])
77+
7378
Compiler/Runtime improvements
7479
-----------------------------
7580

base/docs/basedocs.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -486,26 +486,6 @@ Expr
486486
"""
487487
Expr
488488

489-
"""
490-
'
491-
492-
The conjugate transposition operator, see [`adjoint`](@ref).
493-
494-
# Examples
495-
```jldoctest
496-
julia> A = [1.0 -2.0im; 4.0im 2.0]
497-
2×2 Array{Complex{Float64},2}:
498-
1.0+0.0im -0.0-2.0im
499-
0.0+4.0im 2.0+0.0im
500-
501-
julia> A'
502-
2×2 Array{Complex{Float64},2}:
503-
1.0-0.0im 0.0-4.0im
504-
-0.0+2.0im 2.0-0.0im
505-
```
506-
"""
507-
kw"'"
508-
509489
"""
510490
\$
511491

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ export
459459
startswith,
460460

461461
# linear algebra
462+
var"'", # to enable syntax a' for adjoint
462463
adjoint,
463464
transpose,
464465
kron,

base/operators.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ for op in (:+, :*, :&, :|, :xor, :min, :max, :kron)
542542
end
543543
end
544544

545+
const var"'" = adjoint
546+
545547
"""
546548
\\(x, y)
547549

src/julia-syntax.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2338,7 +2338,7 @@
23382338
,.(apply append rows)))
23392339
`(call (top typed_vcat) ,t ,@a)))))
23402340

2341-
'|'| (lambda (e) (expand-forms `(call (top adjoint) ,(cadr e))))
2341+
'|'| (lambda (e) (expand-forms `(call |'| ,(cadr e))))
23422342

23432343
'generator
23442344
(lambda (e)

stdlib/LinearAlgebra/src/adjtrans.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,14 @@ Base.unaliascopy(A::Union{Adjoint,Transpose}) = typeof(A)(Base.unaliascopy(A.par
100100

101101
# wrapping lowercase quasi-constructors
102102
"""
103+
A'
103104
adjoint(A)
104105
105-
Lazy adjoint (conjugate transposition) (also postfix `'`).
106-
Note that `adjoint` is applied recursively to elements.
106+
Lazy adjoint (conjugate transposition). Note that `adjoint` is applied recursively to
107+
elements.
108+
109+
For number types, `adjoint` returns the complex conjugate, and therefore it is equivalent to
110+
the identity function for real numbers.
107111
108112
This operation is intended for linear algebra usage - for general data manipulation see
109113
[`permutedims`](@ref Base.permutedims).
@@ -119,6 +123,14 @@ julia> adjoint(A)
119123
2×2 Adjoint{Complex{Int64},Array{Complex{Int64},2}}:
120124
3-2im 8-7im
121125
9-2im 4-6im
126+
127+
julia> x = [3, 4im]
128+
2-element Array{Complex{Int64},1}:
129+
3 + 0im
130+
0 + 4im
131+
132+
julia> x'x
133+
25 + 0im
122134
```
123135
"""
124136
adjoint(A::AbstractVecOrMat) = Adjoint(A)

stdlib/REPL/src/docview.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ function _helpmode(io::IO, line::AbstractString)
4343
x = Meta.parse(line, raise = false, depwarn = false)
4444
assym = Symbol(line)
4545
expr =
46-
if haskey(keywords, assym) || Base.isoperator(assym) || isexpr(x, :error) || isexpr(x, :invalid)
46+
if haskey(keywords, Symbol(line)) || Base.isoperator(assym) || isexpr(x, :error) ||
47+
isexpr(x, :invalid) || isexpr(x, :incomplete)
4748
# Docs for keywords must be treated separately since trying to parse a single
4849
# keyword such as `function` would throw a parse error due to the missing `end`.
4950
assym

test/syntax.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,3 +2269,10 @@ _temp_33553 = begin
22692269
end
22702270
@test _temp_33553 == 2
22712271
@test !@isdefined(_x_this_remains_undefined)
2272+
2273+
# lowering of adjoint
2274+
@test (1 + im)' == 1 - im
2275+
x = let var"'"(x) = 2x
2276+
3'
2277+
end
2278+
@test x == 6

0 commit comments

Comments
 (0)