forked from JuliaInterop/MATLAB.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmxarray.jl
468 lines (397 loc) · 9.46 KB
/
mxarray.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
using MATLAB
using Test
using SparseArrays
# Unit testing for MxArray
m = 5
n = 6
# test basic types in 1D & 2D
macro mx_test_basic_types(ty, testfun)
quote
a = mxarray($(ty), n)
@test elsize(a) == sizeof($(ty))
@test eltype(a) === $(ty)
@test nrows(a) == n
@test ncols(a) == 1
@test nelems(a) == n
@test ndims(a) == 2
@test size(a) == (n, 1)
@test size(a, 1) == n
@test size(a, 2) == 1
@test size(a, 3) == 1
@test !is_complex(a)
@test $(testfun)(a)
delete(a)
b = mxarray($(ty), m, n)
@test elsize(b) == sizeof($(ty))
@test eltype(b) === $(ty)
@test nrows(b) == m
@test ncols(b) == n
@test nelems(b) == m * n
@test ndims(b) == 2
@test size(b) == (m, n)
@test size(b, 1) == m
@test size(b, 2) == n
@test size(b, 3) == 1
@test !is_complex(b)
@test $(testfun)(b)
delete(b)
return nothing
end
end
# empty array
a = mxarray(Float64, 0, 0)
@test nrows(a) == 0
@test ncols(a) == 0
@test nelems(a) == 0
@test ndims(a) == 2
@test eltype(a) == Float64
@test is_empty(a)
# basic arrays
@mx_test_basic_types Float64 is_double
@mx_test_basic_types Float32 is_single
@mx_test_basic_types Int64 is_int64
@mx_test_basic_types UInt64 is_uint64
@mx_test_basic_types Int32 is_int32
@mx_test_basic_types UInt32 is_uint32
@mx_test_basic_types Int16 is_int16
@mx_test_basic_types UInt16 is_uint16
@mx_test_basic_types Int8 is_int8
@mx_test_basic_types UInt8 is_uint8
@mx_test_basic_types Bool is_logical
# complex arrays
macro mx_test_complex_type(ty, testfun)
quote
b = mxarray(Complex{$(ty)}, m, n)
@test elsize(b) == sizeof($(ty))
@test eltype(b) === $(ty)
@test nrows(b) == m
@test ncols(b) == n
@test nelems(b) == m * n
@test ndims(b) == 2
@test size(b) == (m, n)
@test size(b, 1) == m
@test size(b, 2) == n
@test size(b, 3) == 1
@test is_complex(b)
@test $(testfun)(b)
delete(b)
return nothing
end
end
@mx_test_complex_type Float64 is_double
@mx_test_complex_type Float32 is_single
# test creating multi-dimensional arrays
a = mxarray(Float64, (6, 5, 4))
@test elsize(a) == sizeof(Float64)
@test eltype(a) === Float64
@test size(a) == (6, 5, 4)
@test size(a, 1) == 6
@test size(a, 2) == 5
@test size(a, 3) == 4
@test size(a, 4) == 1
@test nelems(a) == 6 * 5 * 4
@test is_numeric(a)
@test !is_sparse(a)
a = mxarray(Bool, (6, 5, 4))
@test elsize(a) == 1
@test eltype(a) === Bool
@test size(a) == (6, 5, 4)
@test size(a, 1) == 6
@test size(a, 2) == 5
@test size(a, 3) == 4
@test size(a, 4) == 1
@test nelems(a) == 6 * 5 * 4
@test is_logical(a)
@test !is_sparse(a)
# scalars
a_mx = mxarray(3.25)
@test eltype(a_mx) == Float64
@test size(a_mx) == (1, 1)
@test jscalar(a_mx) == 3.25
delete(a_mx)
a_mx = mxarray(Int32(12))
@test eltype(a_mx) == Int32
@test size(a_mx) == (1, 1)
@test jscalar(a_mx) == Int32(12)
delete(a_mx)
a_mx = mxarray(true)
@test eltype(a_mx) == Bool
@test size(a_mx) == (1, 1)
@test jscalar(a_mx)
delete(a_mx)
a_mx = mxarray(false)
@test eltype(a_mx) == Bool
@test size(a_mx) == (1, 1)
@test !jscalar(a_mx)
delete(a_mx)
a_mx = mxarray(3.25 + 4im)
@test eltype(a_mx) == Float64
@test size(a_mx) == (1, 1)
@test jscalar(a_mx) == 3.25 + 4im
delete(a_mx)
# conversion between Julia and MATLAB numeric arrays
a = rand(5, 6)
a_mx = mxarray(a)
a2 = jarray(a_mx)
@test isequal(a, a2)
delete(a_mx)
a = rand(5)
a_mx = mxarray(a)
a2 = jvector(a_mx)
@test isequal(a, a2)
delete(a_mx)
a_t = reshape(a, 1, 5)
a_mx = mxarray(a_t)
a2 = jvector(a_mx)
@test isequal(a, a2)
delete(a_mx)
a = 1:5
a_mx = mxarray(a)
a2 = jvector(a_mx)
@test isequal([1:5;], a2)
delete(a_mx)
a = rand(5, 6) + rand(5, 6)*im
a_mx = mxarray(a)
a2 = jarray(a_mx)
@test isequal(a, a2)
delete(a_mx)
# sparse matrices
a = sprand(8, 9, 0.2)
a_mx = mxarray(a)
@test is_double(a_mx)
@test is_sparse(a_mx)
@test nrows(a_mx) == 8
@test ncols(a_mx) == 9
a2 = jsparse(a_mx)
@test size(a2) == (8, 9)
@test count(!iszero, a2) == count(!iszero, a)
@test isequal(a2, a)
delete(a_mx)
a = sparse(convert(Array{Bool}, rand(8, 9) .< 0.3))
a_mx = mxarray(a)
@test is_logical(a_mx)
@test is_sparse(a_mx)
@test nrows(a_mx) == 8
@test ncols(a_mx) == 9
a2 = jsparse(a_mx)
@test size(a2) == (8, 9)
@test count(!iszero, a2) == count(!iszero, a)
@test isequal(a2, a)
delete(a_mx)
a = sparse([1.0 1.0im])
a_mx = mxarray(a)
@test is_sparse(a_mx)
@test is_double(a_mx)
@test is_complex(a_mx)
@test nrows(a_mx) == 1
@test ncols(a_mx) == 2
delete(a_mx)
# strings
s = "MATLAB.jl"
s_mx = mxarray(s)
@test classid(s_mx) == MATLAB.mxCHAR_CLASS
@test nrows(s_mx) == 1
@test ncols(s_mx) == length(s)
@test nelems(s_mx) == length(s)
@test ndims(s_mx) == 2
@test is_char(s_mx)
s2 = jstring(s_mx)
@test s == s2
delete(s_mx)
s = ""
s_mx = mxarray(s)
@test classid(s_mx) == MATLAB.mxCHAR_CLASS
@test is_char(s_mx)
@test is_empty(s_mx)
s2 = jstring(s_mx)
@test s == s2
delete(s_mx)
# cell arrays
a = mxcellarray(10)
@test nrows(a) == 10
@test ncols(a) == 1
@test nelems(a) == 10
@test classid(a) == MATLAB.mxCELL_CLASS
@test is_cell(a)
delete(a)
a = mxcellarray(4, 5)
@test nrows(a) == 4
@test ncols(a) == 5
@test nelems(a) == 20
@test classid(a) == MATLAB.mxCELL_CLASS
@test is_cell(a)
delete(a)
a = mxcellarray((3, 4, 5))
@test size(a) == (3, 4, 5)
@test nelems(a) == 60
@test classid(a) == MATLAB.mxCELL_CLASS
@test is_cell(a)
delete(a)
s = ["abc", "efg"]
s_mx = mxcellarray(s)
@test jstring(get_cell(s_mx, 1)) == "abc"
@test jstring(get_cell(s_mx, 2)) == "efg"
delete(s_mx)
# struct
a = mxstruct("abc", "efg", "xyz")
@test is_struct(a)
@test mxnfields(a) == 3
@test nrows(a) == 1
@test ncols(a) == 1
@test nelems(a) == 1
@test ndims(a) == 2
@test get_fieldname(a, 1) == "abc"
@test get_fieldname(a, 2) == "efg"
@test get_fieldname(a, 3) == "xyz"
delete(a)
s = Dict("name"=>"MATLAB", "version"=>12.0, "data"=>[1,2,3])
a = mxstruct(s)
@test is_struct(a)
@test mxnfields(a) == 3
@test jstring(get_field(a, "name")) == "MATLAB"
@test jscalar(get_field(a, "version")) == 12.0
@test isequal(jvector(get_field(a, "data")), [1,2,3])
delete(a)
mutable struct TestType
name::String
version::Float64
data::Vector{Int}
end
t = TestType("MATLAB", 12.0, [1,2,3])
a = mxstruct(t)
@test is_struct(a)
@test mxnfields(a) == 3
@test jstring(get_field(a, "name")) == "MATLAB"
@test jscalar(get_field(a, "version")) == 12.0
@test isequal(jvector(get_field(a, "data")), [1,2,3])
delete(a)
a = mxstructarray([TestType("MATLAB", 12.0, [1,2,3]),
TestType("Julia", 0.2, [4,5,6])])
@test is_struct(a)
@test mxnfields(a) == 3
@test jstring(get_field(a, 1, "name")) == "MATLAB"
@test jscalar(get_field(a, 1, "version")) == 12.0
@test isequal(jvector(get_field(a, 1, "data")), [1,2,3])
@test jstring(get_field(a, 2, "name")) == "Julia"
@test jscalar(get_field(a, 2, "version")) == 0.2
@test isequal(jvector(get_field(a, 2, "data")), [4,5,6])
delete(a)
# bi-directional conversions
x = mxarray(12.0)
y = jvalue(x)
delete(x)
@test isa(y, Float64)
@test y == 12.0
a = rand(5)
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Vector{Float64})
@test isequal(y, a)
a = rand(3, 4)
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Matrix{Float64})
@test isequal(y, a)
a = rand(3, 4, 5)
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Array{Float64,3})
@test isequal(y, a)
a = sparse([1.0 2.0im; 0 -1.0im])
a_mx = mxarray(a)
a_jl = jvalue(a_mx)
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)
# Issue: Tuples converted to MATLAB structs
# https://github.com/JuliaInterop/MATLAB.jl/issues/178
a = (2.5, 2.6)
x = mxarray(a)
y = jvalue(x)
@test classid(x) == MATLAB.mxDOUBLE_CLASS
@test nrows(x) == 2
@test ncols(x) == 1
delete(x)
@test isa(y, Vector{Float64})
@test isequal(y, collect(a))
# Tuple with mixed types
a = (1, 2.0, "MATLAB", [1, 2, 3])
x = mxarray(a)
y = jvalue(x)
@test nrows(x) == 4
@test ncols(x) == 1
@test classid(x) == MATLAB.mxCELL_CLASS
@test isa(y, Vector{Any})
@test length(y) == length(a)
@test isequal(y, collect(a))
##############################
# String Conversions
##############################
a = "MATLAB"
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, String)
@test y == a
a = ["abc", 3, "efg"]
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Vector{Any})
@test length(y) == 3
@test y[1] == a[1]
@test y[2] == a[2]
@test y[3] == a[3]
a = Dict("abc"=>10.0, "efg"=>[1, 2, 3], "xyz"=>"MATLAB")
x = mxarray(a)
y = jvalue(x)
delete(x)
@test isa(y, Dict{String, Any})
@test y["abc"] == 10.0
@test isequal(y["efg"], [1, 2, 3])
@test y["xyz"] == "MATLAB"
# Test string encoding
str = "λ α γ"
@test jstring(mxarray(str)) == str
@test mat"all($str == [955 32 945 32 947])"
GC.gc()