-
Notifications
You must be signed in to change notification settings - Fork 6
/
arithmetic.jl
89 lines (84 loc) · 2.07 KB
/
arithmetic.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
80
81
82
83
84
85
86
87
88
89
Base.promote_rule(::Type{<:MagLike}, ::Type{<:Union{Float64,MagLike}}) = Mag
Base.promote_rule(::Type{<:ArfLike}, ::Type{<:Union{AbstractFloat,Integer,MagLike}}) = Arf
Base.promote_rule(
::Type{<:ArbLike},
::Type{<:Union{AbstractFloat,Integer,Rational,ArfLike,ArbRef}},
) = Arb
Base.promote_rule(
::Type{<:AcbLike},
::Type{
<:Union{
AbstractFloat,
Integer,
Rational,
Complex{<:Union{AbstractFloat,Integer,Rational}},
ArfLike,
ArbLike,
AcbRef,
},
},
) = Acb
for (jf, af) in [(:+, :add!), (:-, :sub!), (:*, :mul!), (:/, :div!)]
@eval function Base.$jf(x::T, y::T) where {T<:MagOrRef}
z = T()
$af(z, x, y)
z
end
end
function Base.:(^)(x::T, k::Integer) where {T<:MagOrRef}
z = T()
pow!(z, x, convert(UInt, k))
z
end
for (jf, af) in [(:+, :add!), (:-, :sub!), (:*, :mul!), (:/, :div!)]
@eval function Base.$jf(x::T, y::T) where {T<:Union{Arf,ArfRef,Arb,ArbRef,Acb,AcbRef}}
z = T(prec = max(precision(x), precision(y)))
$af(z, x, y)
z
end
end
function Base.:(-)(x::T) where {T<:Union{Arf,ArfRef,Arb,ArbRef,Acb,AcbRef}}
z = T(prec = precision(x))
neg!(z, x)
z
end
function Base.:(^)(x::T, k::Integer) where {T<:Union{Arb,ArbRef,Acb,AcbRef}}
z = T(prec = precision(x))
pow!(z, x, convert(UInt, k))
z
end
for f in [
:sqrt,
:exp,
:log,
:log1p,
:sin,
:cos,
:tan,
:cot,
:asin,
:acos,
:atan,
:acot,
:sinc,
:sinh,
:cosh,
:tanh,
:asinh,
:acosh,
:atanh,
:sec,
:asec,
:sech,
:asech,
]
@eval function Base.$f(x::T) where {T<:Union{Arb,ArbRef,Acb,AcbRef}}
z = T(prec = precision(x))
$(Symbol(f, :!))(z, x)
z
end
end
Base.real(z::AcbLike; prec = _precision(z)) = get_real!(Arb(prec = prec), z)
Base.imag(z::AcbLike; prec = _precision(z)) = get_imag!(Arb(prec = prec), z)
Base.conj(z::AcbLike) = conj!(Acb(prec = _precision(z)), z)
Base.abs(z::AcbLike) = abs!(Arb(prec = _precision(z)), z)