Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion from AbstractArray to mxarray #229

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/mxarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ function mxarray(a::Array{T}) where T<:MxRealNum
return mx
end

function mxarray(a::Array{T}) where T<:MxComplexNum

function mxarray(a::Array{T}) where {T<:MxComplexNum}
mx = mxarray(T, size(a))
na = length(a)
rdat = unsafe_wrap(Array, real_ptr(mx), na)
Expand All @@ -276,8 +277,30 @@ function mxarray(a::Array{T}) where T<:MxComplexNum
mx
end

mxarray(a::BitArray) = mxarray(convert(Array{Bool}, a))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the function below still work?

mxarray(a::AbstractRange) = mxarray([a;])

function mxarray(a::AbstractArray{T}) where {T<:MxRealNum}
mx = mxarray(T, size(a))
ptr = data_ptr(mx)
na = length(a)
dat = unsafe_wrap(Array{T}, ptr, na)
for (i, ix) in enumerate(eachindex(a))
dat[i] = a[ix]
end
return mx
end

function mxarray(a::AbstractArray{T}) where {T<:MxComplexNum}
mx = mxarray(T, size(a))
na = length(a)
rdat = unsafe_wrap(Array, real_ptr(mx), na)
idat = unsafe_wrap(Array, imag_ptr(mx), na)
for (i, ix) in enumerate(eachindex(a))
rdat[i] = real(a[ix])
idat[i] = imag(a[ix])
end
return mx
end


# sparse matrix

Expand Down
50 changes: 50 additions & 0 deletions test/mxarray.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MATLAB
using Test
using SparseArrays

# Unit testing for MxArray

Expand Down Expand Up @@ -361,6 +362,55 @@ delete(a_mx)
@test a == a_jl
@test isa(a_jl, SparseMatrixCSC{Complex{Float64}})

##############################
# Abstract Array Conversions
##############################

a = transpose(rand(10))
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Array{Float64,2})
@test size(y) == size(a)
@test isequal(y, a)

a = rand(10,10)
a_ = @view a[3:7, 4:8]
x = mxarray(a_)
y = jvalue(x)
delete(x)
@test isa(y, Array{Float64,2})
@test size(y) == size(a_)
@test isequal(y, a_)

a_ = rand(ComplexF32, 10, 10, 10)
a = @view a_[3:7, 4:8, 2:5]
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Array{ComplexF32,3})
@test size(y) == size(a)

a = 1:100 # range
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Array{Int64,1})
@test isequal(y, collect(a))

a = BitArray(rand(Bool, 5,20,10))
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Array{Bool,3})
@test isequal(y, a)



##############################
# String Conversions
##############################

a = "MATLAB"
x = mxarray(a)
y = jvalue(x)
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ using Test
is_ci() = lowercase(get(ENV, "CI", "false")) == "true"

if !is_ci() # only test if not CI
include("engine.jl")
include("matfile.jl")
include("matstr.jl")
include("mxarray.jl")
include("engine.jl")
include("matfile.jl")
include("matstr.jl")
include("mxarray.jl")
end
Loading