-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
ConjArray
wrapper type for conjugate views
By default, this is used only in conjugation with `RowVector`, so that both `transpose(vec)` and `ctranspose(vec)` both return views.
- Loading branch information
Andy Ferris
committed
Feb 8, 2017
1 parent
e24faa5
commit db90a8e
Showing
10 changed files
with
148 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
""" | ||
ConjArray(array) | ||
A lazy-view wrapper of an `AbstractArray`, taking the elementwise complex conjugate. This | ||
type is usually constructed (and unwrapped) via the [`conj`](@ref) function (or related | ||
[`ctranspose`](@ref)), but currently this is the default behavior for `RowVector` only. For | ||
other arrays, the `ConjArray` constructor can be used directly. | ||
# Examples | ||
```jldoctest | ||
julia> [1+im, 1-im]' | ||
1×2 RowVector{Complex{Int64},ConjArray{Complex{Int64},1,Array{Complex{Int64},1}}}: | ||
1-1im 1+1im | ||
julia> ConjArray([1+im 0; 0 1-im]) | ||
2×2 ConjArray{Complex{Int64},2,Array{Complex{Int64},2}}: | ||
1-1im 0+0im | ||
0+0im 1+1im | ||
``` | ||
""" | ||
immutable ConjArray{T, N, A <: AbstractArray} <: AbstractArray{T, N} | ||
parent::A | ||
end | ||
|
||
@inline ConjArray{T,N}(a::AbstractArray{T,N}) = ConjArray{conj_type(T), N, typeof(a)}(a) | ||
|
||
typealias ConjVector{T, V <: AbstractVector} ConjArray{T, 1, V} | ||
@inline ConjVector{T}(v::AbstractVector{T}) = ConjArray{conj_type(T), 1, typeof(v)}(v) | ||
|
||
typealias ConjMatrix{T, M <: AbstractMatrix} ConjArray{T, 2, M} | ||
@inline ConjMatrix{T}(m::AbstractMatrix{T}) = ConjArray{conj_type(T), 2, typeof(m)}(m) | ||
|
||
# This type can cause the element type to change under conjugation - e.g. an array of complex arrays. | ||
@inline conj_type(x) = conj_type(typeof(x)) | ||
@inline conj_type{T}(::Type{T}) = promote_op(conj, T) | ||
|
||
@inline parent(c::ConjArray) = c.parent | ||
@inline parent_type(c::ConjArray) = parent_type(typeof(c)) | ||
@inline parent_type{T,N,A}(::Type{ConjArray{T,N,A}}) = A | ||
|
||
@inline size(a::ConjArray) = size(a.parent) | ||
linearindexing{CA <: ConjArray}(::CA) = linearindexing(parent_type(CA)) | ||
linearindexing{CA <: ConjArray}(::Type{CA}) = linearindexing(parent_type(CA)) | ||
|
||
@propagate_inbounds getindex{T,N}(a::ConjArray{T,N}, i::Int) = conj(getindex(a.parent, i)) | ||
@propagate_inbounds getindex{T,N}(a::ConjArray{T,N}, i::Vararg{Int,N}) = conj(getindex(a.parent, i...)) | ||
@propagate_inbounds setindex!{T,N}(a::ConjArray{T,N}, v, i::Int) = setindex!(a.parent, conj(v), i) | ||
@propagate_inbounds setindex!{T,N}(a::ConjArray{T,N}, v, i::Vararg{Int,N}) = setindex!(a.parent, conj(v), i...) | ||
|
||
@inline similar{T,N}(a::ConjArray, ::Type{T}, dims::Dims{N}) = similar(parent(a), T, dims) | ||
|
||
# Currently, this is default behavior for RowVector only | ||
@inline conj(a::ConjArray) = parent(a) | ||
|
||
# Helper functions, currently used by RowVector | ||
@inline _conj(a::AbstractArray) = ConjArray(a) | ||
@inline _conj{T<:Real}(a::AbstractArray{T}) = a | ||
@inline _conj(a::ConjArray) = parent(a) | ||
@inline _conj{T<:Real}(a::ConjArray{T}) = parent(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
@testset "Core" begin | ||
m = [1+im 2; 2 4-im] | ||
cm = ConjArray(m) | ||
@test cm[1,1] == 1-im | ||
@test trace(cm*m) == 27 | ||
|
||
v = [[1+im], [1-im]] | ||
cv = ConjArray(v) | ||
@test cv[1] == [1-im] | ||
end | ||
|
||
@testset "RowVector conjugates" begin | ||
v = [1+im, 1-im] | ||
rv = v' | ||
@test (parent(rv) isa ConjArray) | ||
@test rv' === v | ||
|
||
# Currently, view behavior defaults to only RowVectors. | ||
@test isa((v').', Vector) | ||
@test isa((v.')', Vector) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters