Skip to content

Commit 58a6b89

Browse files
authored
Add real/imag (#251)
* Add real/imag * Add tests
1 parent 8c71cca commit 58a6b89

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.7.28"
3+
version = "0.7.29"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/ApproxFunBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Base: values, convert, getindex, setindex!, *, +, -, ==, <, <=, >, |, !,
2323
isless, union, angle, join, isnan, isapprox, isempty, sort, merge, promote_rule,
2424
minimum, maximum, extrema, argmax, argmin, findmax, findmin, isfinite,
2525
zeros, zero, one, promote_rule, repeat, length, resize!, isinf,
26-
getproperty, findfirst, unsafe_getindex, fld, cld, div, imag,
26+
getproperty, findfirst, unsafe_getindex, fld, cld, div,
2727
@_inline_meta, eachindex, firstindex, lastindex, keys, isreal, OneTo,
2828
Array, Vector, Matrix, view, ones, @propagate_inbounds, print_array,
2929
split, iszero, permutedims, rad2deg, deg2rad

src/Operators/general/ReImSpace.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## just takes realpart of operator
2+
struct ReOperator{O,T} <: Operator{T}
3+
op::O
4+
end
5+
6+
ReOperator(op)=ReOperator{typeof(op),real(eltype(op))}(op)
7+
convert(::Type{Operator{T}},R::ReOperator) where {T} = ReOperator{typeof(R.op),T}(R.op)
8+
9+
@wrapperstructure ReOperator
10+
@wrapperspaces ReOperator
11+
12+
13+
14+
15+
16+
getindex(RI::ReOperator{O,T},k::Integer,j::Integer) where {O,T} =
17+
convert(T,real(RI.op[k,j]))
18+
19+
choosedomainspace(R::ReOperator,sp::Space) = choosedomainspace(R.op,sp)
20+
for OP in (:promotedomainspace,:promoterangespace)
21+
@eval begin
22+
$OP(R::ReOperator,sp::UnsetSpace) = ReOperator($OP(R.op,sp))
23+
$OP(R::ReOperator,sp::Space) = ReOperator($OP(R.op,sp))
24+
end
25+
end
26+
27+
28+
29+
# TODO: can't do this because UnsetSpace might change type
30+
#real{T<:Real}(op::Operator{T})=op
31+
real(op::Operator) = ReOperator(op)
32+
imag(op::Operator) = real(-im * op)
33+
conj(op::Operator) = real(op) - im * imag(op)

src/Operators/general/general.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ include("OperatorLayout.jl")
55
include("PartialInverseOperator.jl")
66
include("InterlaceOperator.jl")
77
include("OperatorFunction.jl")
8+
include("ReImSpace.jl")

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ end
268268
M = Multiplication(Fun(identity, PointSpace(1:3)))
269269
@test_throws ErrorException Matrix(M)
270270
end
271+
@testset "real-imag" begin
272+
A = (3 + 2im)*I : PointSpace(1:4)
273+
Ar = ApproxFunBase.real(A)
274+
Ai = imag(A)
275+
@test Ar[1:4, 1:4] == diagm(0=>fill(3, 4))
276+
@test Ai[1:4, 1:4] == diagm(0=>fill(2, 4))
277+
B = conj(A)
278+
Bi = imag(B)
279+
@test Bi[1:4, 1:4] == diagm(0=>fill(-2, 4))
280+
C = convert(Operator{ComplexF64}, A)
281+
@test C isa Operator{ComplexF64}
282+
@test imag(C)[1:4, 1:4] == diagm(0=>fill(2, 4))
283+
284+
A = (3 - 2im)*I : PointSpace(1:4)
285+
Ar = ApproxFunBase.real(A)
286+
Ai = imag(A)
287+
@test Ar[1:4, 1:4] == diagm(0=>fill(3, 4))
288+
@test Ai[1:4, 1:4] == diagm(0=>fill(-2, 4))
289+
end
271290
end
272291

273292
@testset "RowVector" begin

0 commit comments

Comments
 (0)