Skip to content

Commit

Permalink
Add conversion from AbstractArray to mxarray (#229)
Browse files Browse the repository at this point in the history
* Add conversion from AbstractArray to mxarray

* Update support for complex abstract vectors

* Extend types and test cases
  • Loading branch information
tqml authored Feb 7, 2025
1 parent 3bd0547 commit 8183663
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
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))
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

0 comments on commit 8183663

Please sign in to comment.