@@ -296,7 +296,6 @@ def _get_assign_dims(key, idims):
296
296
else :
297
297
raise IndexError ("Invalid type while assigning to arrayfire.array" )
298
298
299
-
300
299
def transpose (a , conj = False ):
301
300
"""
302
301
Perform the transpose on an input.
@@ -504,7 +503,9 @@ def __init__(self, src=None, dims=None, dtype=None, is_device=False, offset=None
504
503
if (offset is None and strides is None ):
505
504
self .arr = _create_array (buf , numdims , idims , to_dtype [_type_char ], is_device )
506
505
else :
507
- self .arr = _create_strided_array (buf , numdims , idims , to_dtype [_type_char ], is_device , offset , strides )
506
+ self .arr = _create_strided_array (buf , numdims , idims ,
507
+ to_dtype [_type_char ],
508
+ is_device , offset , strides )
508
509
509
510
else :
510
511
@@ -1159,6 +1160,19 @@ def __setitem__(self, key, val):
1159
1160
except RuntimeError as e :
1160
1161
raise IndexError (str (e ))
1161
1162
1163
+ def _reorder (self ):
1164
+ """
1165
+ Returns a reordered array to help interoperate with row major formats.
1166
+ """
1167
+ ndims = self .numdims ()
1168
+ if (ndims == 1 ):
1169
+ return self
1170
+
1171
+ rdims = tuple (reversed (range (ndims ))) + tuple (range (ndims , 4 ))
1172
+ out = Array ()
1173
+ safe_call (backend .get ().af_reorder (c_pointer (out .arr ), self .arr , * rdims ))
1174
+ return out
1175
+
1162
1176
def to_ctype (self , row_major = False , return_shape = False ):
1163
1177
"""
1164
1178
Return the data as a ctype C array after copying to host memory
@@ -1312,6 +1326,44 @@ def __array__(self):
1312
1326
safe_call (backend .get ().af_get_data_ptr (c_void_ptr_t (res .ctypes .data ), self .arr ))
1313
1327
return res
1314
1328
1329
+ def to_ndarray (self , output = None ):
1330
+ """
1331
+ Parameters
1332
+ -----------
1333
+ output: optional: numpy. default: None
1334
+
1335
+ Returns
1336
+ ----------
1337
+ If output is None: Constructs a numpy.array from arrayfire.Array
1338
+ If output is not None: copies content of af.array into numpy array.
1339
+
1340
+ Note
1341
+ ------
1342
+
1343
+ - An exception is thrown when output is not None and it is not contiguous.
1344
+ - When output is None, The returned array is in fortran contiguous order.
1345
+ """
1346
+ if output is None :
1347
+ return self .__array__ ()
1348
+
1349
+ if (output .dtype != to_typecode [self .type ()]):
1350
+ raise TypeError ("Output is not the same type as the array" )
1351
+
1352
+ if (output .size != self .elements ()):
1353
+ raise RuntimeError ("Output size does not match that of input" )
1354
+
1355
+ flags = output .flags
1356
+ tmp = None
1357
+ if flags ['F_CONTIGUOUS' ]:
1358
+ tmp = self
1359
+ elif flags ['C_CONTIGUOUS' ]:
1360
+ tmp = self ._reorder ()
1361
+ else :
1362
+ raise RuntimeError ("When output is not None, it must be contiguous" )
1363
+
1364
+ safe_call (backend .get ().af_get_data_ptr (c_void_ptr_t (output .ctypes .data ), tmp .arr ))
1365
+ return output
1366
+
1315
1367
def display (a , precision = 4 ):
1316
1368
"""
1317
1369
Displays the contents of an array.
0 commit comments