-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_operator.jl
79 lines (56 loc) · 1.81 KB
/
abstract_operator.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export AbstractOperator
export get_row_iterator, get_column_iterator, get_iterator
export scalartype
export bintype
import LinearAlgebra
"""
AbstractOperator{S<:Number}
Represent an abstract operator in Hilbert space.
"""
abstract type AbstractOperator{S<:Number} end
"""
scalartype(lhs::Type{<:AbstractOperator{S}})
Returns the scalar type of the given AbstractOperator.
"""
scalartype(lhs::Type{<:AbstractOperator{S}}) where S = S
scalartype(lhs::AbstractOperator{S}) where S = S
"""
valtype(lhs::Type{<:AbstractOperator{S}})
Returns the `valtype` (scalar type) of the given AbstractOperator.
"""
Base.valtype(lhs::Type{<:AbstractOperator{S}}) where S = S
Base.valtype(lhs::AbstractOperator{S}) where S = S
bintype(lhs::AbstractOperator{S}) where S = bintype(typeof(lhs))::DataType
Base.:(-)(lhs::AbstractOperator{S1}, rhs::AbstractOperator{S2}) where {S1, S2} = (lhs) + (-rhs)
Base.:(+)(op::AbstractOperator{S}) where S = op
function LinearAlgebra.issymmetric(arg::AbstractOperator{S}) where S
return isa(simplify(arg - transpose(arg)), NullOperator)
end
function LinearAlgebra.ishermitian(arg::AbstractOperator{S}) where S
return isa(simplify(arg - adjoint(arg)), NullOperator)
end
function Base.:(^)(lhs::AbstractOperator{S}, p::Integer) where S
p <= 0 && error("Non-positive power for AbstractOperator not supported")
# smallest nonzero power
pow = simplify(lhs)
while (p & 0x1) == 0
pow = simplify(pow * pow)
if isa(pow, NullOperator)
return pow
end
p = p >> 1
end
out = pow
p = p >> 1
while p > 0
pow = simplify(pow * pow)
if (p & 0b1) != 0
out = simplify(out * pow)
end
if isa(out, NullOperator)
return out
end
p = p >> 1
end
return out
end