@@ -83,6 +83,14 @@ def binary_funcr(lhs, rhs, c_func):
8383
8484 return out
8585
86+ def transpose (a , conj = False ):
87+ out = array ()
88+ safe_call (clib .af_transpose (ct .pointer (out .arr ), a .arr , conj ))
89+ return out
90+
91+ def transpose_inplace (a , conj = False ):
92+ safe_call (clib .af_transpose_inplace (a .arr , conj ))
93+
8694class seq (ct .Structure ):
8795 _fields_ = [("begin" , ct .c_double ),
8896 ("end" , ct .c_double ),
@@ -163,6 +171,17 @@ def slice_to_length(key, dim):
163171
164172 return int (((tkey [1 ] - tkey [0 ] - 1 ) / tkey [2 ]) + 1 )
165173
174+ def ctype_to_lists (ctype_arr , dim , shape , offset = 0 ):
175+ if (dim == 0 ):
176+ return list (ctype_arr [offset : offset + shape [0 ]])
177+ else :
178+ dim_len = shape [dim ]
179+ res = [[]] * dim_len
180+ for n in range (dim_len ):
181+ res [n ] = ctype_to_lists (ctype_arr , dim - 1 , shape , offset )
182+ offset += shape [0 ]
183+ return res
184+
166185def get_assign_dims (key , idims ):
167186 dims = [1 ]* 4
168187
@@ -518,6 +537,31 @@ def __setitem__(self, key, val):
518537 except RuntimeError as e :
519538 raise IndexError (str (e ))
520539
540+ def to_ctype (self , row_major = False , return_shape = False ):
541+ tmp = transpose (self ) if row_major else self
542+ ctype_type = to_c_type [self .type ()] * self .elements ()
543+ res = ctype_type ()
544+ safe_call (clib .af_get_data_ptr (ct .pointer (res ), self .arr ))
545+ if (return_shape ):
546+ return res , self .dims ()
547+ else :
548+ return res
549+
550+ def to_array (self , row_major = False , return_shape = False ):
551+ res = self .to_ctype (row_major , return_shape )
552+
553+ host = __import__ ("array" )
554+ h_type = to_typecode [self .type ()]
555+
556+ if (return_shape ):
557+ return host .array (h_type , res [0 ]), res [1 ]
558+ else :
559+ return host .array (h_type , res )
560+
561+ def to_list (self , row_major = False ):
562+ ct_array , shape = self .to_ctype (row_major , True )
563+ return ctype_to_lists (ct_array , len (shape ) - 1 , shape )
564+
521565def display (a ):
522566 expr = inspect .stack ()[1 ][- 2 ]
523567 if (expr is not None ):
0 commit comments