Skip to content

Commit

Permalink
Merge pull request #123 from thofma/matconstr
Browse files Browse the repository at this point in the history
Clean up matrix constructors
  • Loading branch information
wbhart committed Feb 22, 2017
2 parents cedf029 + dc6a0b8 commit 69e7082
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 65 deletions.
40 changes: 39 additions & 1 deletion src/Nemo.jl
Expand Up @@ -34,7 +34,7 @@ export create_accessors, get_handle, package_handle, zeros,

export flint_cleanup, flint_set_num_threads

export error_dim_negative
export error_dim_negative, ErrorConstrDimMismatch

export is_windows64

Expand Down Expand Up @@ -256,6 +256,44 @@ MaximalRealSubfield = AnticMaximalRealSubfield
#
###############################################################################

type ErrorConstrDimMismatch <: Exception
expect_r::Int
expect_c::Int
get_r::Int
get_c::Int
get_l::Int

function ErrorConstrDimMismatch(er::Int, ec::Int, gr::Int, gc::Int)
e = new(er, ec, gr, gc, -1)
return e
end

function ErrorConstrDimMismatch(er::Int, ec::Int, gl::Int)
e = new(er, ec, -1, -1, gl)
return e
end

function ErrorConstrDimMismatch{T}(er::Int, ec::Int, a::Array{T, 2})
gr, gc = size(a)
return ErrorConstrDimMismatch(er, ec, gr, gc)
end

function ErrorConstrDimMismatch{T}(er::Int, ec::Int, a::Array{T, 1})
gl = length(a)
return ErrorConstrDimMismatch(er, ec, gl)
end
end

function Base.showerror(io::IO, e::ErrorConstrDimMismatch)
if e.get_l == -1
print(io, "Expected dimension $(e.expect_r) x $(e.expect_c), ")
print(io, "got $(e.get_r) x $(e.get_c)")
else
print(io, "Expected an array of length $(e.expect_r * e.expect_c), ")
print(io, "got $(e.get_l)")
end
end

const error_dim_negative = ErrorException("Dimensions must be non-negative")

###############################################################################
Expand Down
193 changes: 187 additions & 6 deletions src/arb/ArbTypes.jl
Expand Up @@ -512,6 +512,22 @@ type arb_mat <: MatElem{arb}
return z
end

function arb_mat{T <: Union{Int, UInt, fmpz, Float64, BigFloat,
arb}}(r::Int, c::Int, arr::Array{T, 1})
z = new()
ccall((:arb_mat_init, :libarb), Void,
(Ptr{arb_mat}, Int, Int), &z, r, c)
finalizer(z, _arb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:arb_mat_entry_ptr, :libarb), Ptr{arb},
(Ptr{arb_mat}, Int, Int), &z, i - 1, j - 1)
Nemo._arb_set(el, arr[(i-1)*c+j])
end
end
return z
end

function arb_mat{T <: Union{Int, UInt, fmpz, fmpq, Float64, BigFloat, arb,
AbstractString}}(r::Int, c::Int, arr::Array{T, 2},
prec::Int)
Expand All @@ -529,6 +545,23 @@ type arb_mat <: MatElem{arb}
return z
end

function arb_mat{T <: Union{Int, UInt, fmpz, fmpq, Float64, BigFloat, arb,
AbstractString}}(r::Int, c::Int, arr::Array{T, 1},
prec::Int)
z = new()
ccall((:arb_mat_init, :libarb), Void,
(Ptr{arb_mat}, Int, Int), &z, r, c)
finalizer(z, _arb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:arb_mat_entry_ptr, :libarb), Ptr{arb},
(Ptr{arb_mat}, Int, Int), &z, i - 1, j - 1)
_arb_set(el, arr[(i-1)*c+j], prec)
end
end
return z
end

function arb_mat(a::fmpq_mat, prec::Int)
z = new()
ccall((:arb_mat_init, :libarb), Void,
Expand Down Expand Up @@ -630,8 +663,8 @@ type acb_mat <: MatElem{acb}
return z
end

function acb_mat{T <: Union{Int, UInt, Float64, fmpz, BigFloat, acb,
arb}}(r::Int, c::Int, arr::Array{T, 2})
function acb_mat{T <: Union{Int, UInt, Float64, fmpz}}(r::Int,
c::Int, arr::Array{T, 2})
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
Expand All @@ -646,8 +679,71 @@ type acb_mat <: MatElem{acb}
return z
end

function acb_mat{T <: Union{Int, UInt, fmpz, fmpq, Float64, BigFloat, arb,
AbstractString, acb}}(r::Int, c::Int,
function acb_mat{T <: Union{BigFloat, acb, arb}}(r::Int,
c::Int, arr::Array{T, 2})
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[i, j])
end
end
return z
end

function acb_mat{T <: Union{Int, UInt, Float64, fmpz}}(r::Int,
c::Int, arr::Array{T, 1})
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[(i-1)*c+j])
end
end
return z
end

function acb_mat{T <: Union{BigFloat, acb, arb}}(r::Int, c::Int,
arr::Array{T, 1})
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[(i-1)*c+j])
end
end
return z
end

function acb_mat{T <: Union{Int, UInt, fmpz, fmpq, Float64}}(r::Int, c::Int,
arr::Array{T, 2}, prec::Int)
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[i, j], prec)
end
end
return z
end

function acb_mat{T <: Union{BigFloat, arb, AbstractString, acb}}(r::Int, c::Int,
arr::Array{T, 2}, prec::Int)
z = new()
ccall((:acb_mat_init, :libarb), Void,
Expand All @@ -663,8 +759,39 @@ type acb_mat <: MatElem{acb}
return z
end

function acb_mat{T <: Union{Int, UInt, Float64, fmpz, fmpq, BigFloat, arb,
AbstractString}}(r::Int, c::Int,
function acb_mat{T <: Union{Int, UInt, fmpz, fmpq, Float64}}(r::Int, c::Int,
arr::Array{T, 1}, prec::Int)
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[(i-1)*c+j], prec)
end
end
return z
end

function acb_mat{T <: Union{BigFloat, arb, AbstractString, acb}}(r::Int, c::Int,
arr::Array{T, 1}, prec::Int)
z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[(i-1)*c+j], prec)
end
end
return z
end

function acb_mat{T <: Union{Int, UInt, Float64, fmpz}}(r::Int, c::Int,
arr::Array{Tuple{T, T}, 2},
prec::Int)

Expand All @@ -682,6 +809,60 @@ type acb_mat <: MatElem{acb}
return z
end

function acb_mat{T <: Union{fmpq, BigFloat, arb, AbstractString}}(r::Int, c::Int,
arr::Array{Tuple{T, T}, 2},
prec::Int)

z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[i, j][1], arr[i,j][2], prec)
end
end
return z
end

function acb_mat{T <: Union{Int, UInt, Float64, fmpz}}(r::Int, c::Int,
arr::Array{Tuple{T, T}, 1},
prec::Int)

z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[(i-1)*c+j][1], arr[(i-1)*c+j][2], prec)
end
end
return z
end

function acb_mat{T <: Union{fmpq, BigFloat, arb, AbstractString}}(r::Int, c::Int,
arr::Array{Tuple{T, T}, 1},
prec::Int)

z = new()
ccall((:acb_mat_init, :libarb), Void,
(Ptr{acb_mat}, Int, Int), &z, r, c)
finalizer(z, _acb_mat_clear_fn)
for i = 1:r
for j = 1:c
el = ccall((:acb_mat_entry_ptr, :libarb), Ptr{acb},
(Ptr{acb_mat}, Int, Int), &z, i - 1, j - 1)
_acb_set(el, arr[(i-1)*c+j][1], arr[(i-1)*c+j][2], prec)
end
end
return z
end

function acb_mat(a::fmpq_mat, prec::Int)
z = new()
ccall((:acb_mat_init, :libarb), Void,
Expand Down
32 changes: 21 additions & 11 deletions src/arb/acb_mat.jl
Expand Up @@ -626,31 +626,41 @@ end

function (x::AcbMatSpace){T <: Union{Int, UInt, Float64, fmpz, fmpq, BigFloat, arb, acb,
AbstractString}}(y::Array{T, 2})
(x.rows, x.cols) != size(y) && error("Dimensions are wrong")
_check_dim(x.rows, x.cols, y)
z = acb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

(x::AcbMatSpace){T <: Union{Int, UInt, Float64, fmpz, fmpq, BigFloat, arb, acb,
AbstractString}}(y::Array{T, 1}) = x(y'')

function (x::AcbMatSpace){T <: Union{Int, UInt, Float64, fmpz, fmpq, BigFloat, arb, acb,
AbstractString}}(y::Array{T, 1})
_check_dim(x.rows, x.cols, y)
z = acb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

function (x::AcbMatSpace){T <: Union{Int, UInt, Float64, fmpz, fmpq, BigFloat, arb,
AbstractString}}(y::Array{Tuple{T, T}, 2})
(x.rows, x.cols) != size(y) && error("Dimensions are wrong")
_check_dim(x.rows, x.cols, y)
z = acb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

(x::AcbMatSpace){T <: Union{BigFloat, AbstractString, arb}}(y::Array{Tuple{T, T}, 1}) = x(y'')

(x::AcbMatSpace){T <: Union{Int, UInt, Float64, fmpz, fmpq}}(y::Array{Tuple{T, T}, 1}) = x(y'')

(x::ArbMatSpace){T <: Union{Int, UInt, fmpz, fmpq, Float64}}(y::Array{T, 1}) = x(y'')
function (x::AcbMatSpace){T <: Union{Int, UInt, Float64, fmpz, fmpq}}(y::Array{Tuple{T, T}, 1})
_check_dim(x.rows, x.cols, y)
z = acb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

(x::ArbMatSpace){T <: Union{BigFloat, arb, acb, AbstractString}}(y::Array{T, 1}) = x(y'')
function (x::AcbMatSpace){T <: Union{BigFloat, arb, AbstractString}}(y::Array{Tuple{T, T}, 1})
_check_dim(x.rows, x.cols, y)
z = acb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

function (x::AcbMatSpace)(y::Union{Int, UInt, fmpz, fmpq, Float64,
BigFloat, arb, acb, AbstractString})
Expand Down
11 changes: 8 additions & 3 deletions src/arb/arb_mat.jl
Expand Up @@ -580,14 +580,19 @@ end

function (x::ArbMatSpace){T <: Union{Int, UInt, fmpz, fmpq, Float64, BigFloat,
arb, AbstractString}}(y::Array{T, 2})
(x.rows, x.cols) != size(y) && error("Dimensions are wrong")
_check_dim(x.rows, x.cols, y)
z = arb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

(x::ArbMatSpace){T <: Union{Int, UInt, fmpz, fmpq, Float64, BigFloat, arb,
AbstractString}}(y::Array{T, 1}) = x(y'')
function (x::ArbMatSpace){T <: Union{Int, UInt, fmpz, fmpq, Float64, BigFloat,
arb, AbstractString}}(y::Array{T, 1})
_check_dim(x.rows, x.cols, y)
z = arb_mat(x.rows, x.cols, y, prec(x))
z.parent = x
return z
end

function (x::ArbMatSpace)(y::Union{Int, UInt, fmpz, fmpq, Float64,
BigFloat, arb, AbstractString})
Expand Down

0 comments on commit 69e7082

Please sign in to comment.