forked from robol/MPSolve.jl
/
MpsNumberTypes.jl
175 lines (145 loc) · 4.41 KB
/
MpsNumberTypes.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module MpsNumberTypes
using Base.GMP: Limb
using Base.MPFR: MPFRRoundingMode,MPFRRoundNearest
import Base: convert, big
export Mpz,Mpq,Mpf,Mpsc,Rdpe,Cplx,mpsf_precision,mpsf_setprecision,mps_clear!
function __init__()
try
# set GMP floating types precision to current precsion of julia's BigFloat
mpsf_setprecision(precision(BigFloat))
catch ex
Base.showerror_nostdio(ex, "WARNING: Error during initialization of MpsNumberTypes")
end
nothing
end
struct Mpz
alloc::Cint
size::Cint
d::Ptr{Limb}
Mpz() = Mpz(0)
Mpz(b::T) where T<:Signed = Mpz(BigInt(b))
function Mpz(b::BigInt)
m = Ref{Mpz}()
ccall((:__gmpz_init_set, :libgmp), Cvoid, (Ref{Mpz},Ref{BigInt}), m, b)
return m[]
end
end
Base.convert(::Type{Mpz}, x::T) where T<:Signed = Mpz(x)
function BigInt(m::Mpz)
b = BigInt()
ccall((:__gmpz_set, :libgmp), Cvoid, (Ref{BigInt},Ref{Mpz}), b, m)
return b
end
big(::Type{Mpz}) = BigInt
big(v::Mpz) = BigInt(v)
struct Mpq
num::Mpz
den::Mpz
Mpq() = Mpq(0)
Mpq(i::T) where T<:Signed = Mpq(i, 1)
Mpq(q::Rational{T}) where T<:Signed = Mpq(q.num, q.den)
Mpq(num::T, den::S) where {T, S<:Signed} = Mpq(BigInt(num), BigInt(den))
function Mpq(num::BigInt, den::BigInt)
q = Ref{Mpq}()
ccall((:__gmpq_init, :libgmp), Cvoid, (Ref{Mpq},), q)
ccall((:__gmpq_set_num, :libgmp), Cvoid, (Ref{Mpq},Ref{BigInt}), q, num)
ccall((:__gmpq_set_den, :libgmp), Cvoid, (Ref{Mpq},Ref{BigInt}), q, den)
return q[]
end
function Mpq(f::BigFloat)
q = Ref{Mpq}()
ccall((:__gmpq_init, :libgmp), Cvoid, (Ref{Mpq},), q)
ccall((:mpfr_get_q, :libmpfr), Cvoid, (Ref{Mpq}, Ref{BigFloat}), q, f)
return q[]
end
end
mps_clear!(m::Mpz) = ccall((:__gmpz_clear, :libgmp), Cvoid, (Ref{Mpz},), m)
mps_clear!(m::Mpq) = ccall((:__gmpq_clear, :libgmp), Cvoid, (Ref{Mpq},), m)
Mpq(f::AbstractFloat) = Mpq(big(f))
Base.convert(::Type{Mpq}, x::T) where T<:Union{Signed,Rational} = Mpq(x)
Base.Rational(q::Mpq) = Rational(BigInt(q.num), BigInt(q.den))
(::Type{T})(q::Mpq) where T<: AbstractFloat = T(Rational(q))
big(::Type{Mpq}) = BigFloat
big(v::Mpq) = BigFloat(v)
# Arbitrary precision floating point type from gmp.h used by MPSolve
# is different from Julia's BigFloat
struct Mpf
_mp_prec::Cint
_mp_size::Cint
_mp_exp::Clong
_mp_d::Ptr{Limb}
function Mpf(b::BigFloat)
f = Ref{Mpf}()
ccall((:__gmpf_init, :libgmp), Cvoid, (Ref{Mpf},), f)
ccall(
(:mpfr_get_f, :libmpfr),
Cint,
(Ref{Mpf},Ref{BigFloat},MPFRRoundingMode),
f,
b,
MPFRRoundNearest
)
return f[]
end
function Mpf(b::BigInt)
f = Ref{Mpf}()
ccall((:__gmpf_init, :libgmp), Cvoid, (Ref{Mpf},), f)
ccall(
(:__gmpf_set_z, :libgmp),
Cvoid, (Ref{Mpf}, Ref{BigInt}),
f,
b
)
return f[]
end
function Mpf(d::Float64)
f = Ref{Mpf}()
ccall((:__gmpf_init_set_d, :libgmp), Cvoid, (Ref{Mpf},Cdouble), f, d)
return f[]
end
function Mpf(i::Int32)
f = Ref{Mpf}()
ccall((:__gmpf_init_set_si, :libgmp), Cvoid, (Ref{Mpf}, Clong), f, i)
return f[]
end
end
Base.convert(::Type{Mpf}, x::Signed) = Mpf(BigInt(x))
Base.convert(::Type{Mpf}, x::T) where T<:Union{Int32,Int16,Int8} = Mpf(Int32(x))
Base.convert(::Type{Mpf}, x::AbstractFloat) = Mpf(x)
function BigFloat(f::Mpf)
b = BigFloat()
ccall(
(:mpfr_set_f, :libmpfr),
Cint,
(Ref{BigFloat},Ref{Mpf},MPFRRoundingMode),
b,
f,
MPFRRoundNearest
)
return b
end
big(::Type{Mpf}) = BigFloat
big(v::Mpf) = BigFloat(v)
mpsf_precision() = ccall((:__gmpf_get_default_prec, :libgmp), Culong, ())
mpsf_setprecision(p) = ccall((:__gmpf_set_default_prec, :libgmp), Cvoid, (Culong,), p)
struct Mpsc
r::Mpf
i::Mpf
end
Mpsc(x) = Mpsc(x, 0)
Mpsc() = Mpsc(0)
mps_clear!(f::Mpf) = ccall((:__gmpf_clear, :libgmp), Cvoid, (Ref{Mpf},), f)
mps_clear!(c::Mpsc) = mps_clear!([c.r, c.i])
mps_clear!(m::Array{T}) where T<:Union{Mpz, Mpq, Mpf, Mpsc} = (mps_clear!.(m);nothing)
struct Rdpe
r::Cdouble
e::Clong
end
Float64(d::Rdpe) = ccall((:rdpe_get_d, :libmps), Cdouble, (Ref{Rdpe},) ,d)
struct Cplx
r::Cdouble
i::Cdouble
end
Cplx(x) = Cplx(x, 0.0)
Cplx(z::Complex) = Cplx(z.re, z.im)
end