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
|
# mode: run
# tag: numpy, memoryview
import numpy as np
# Cython used to forget the "is contig" helper functions when both are used.
def copy_fortran3(double[:, :, :] mat):
"""
>>> a = np.ones((1, 1, 1), dtype=np.float64)
>>> c = copy_fortran3(a)
>>> bool((a == c).all())
True
>>> a = np.ones((4, 6, 8), dtype=np.float64, order='F')
>>> c = copy_fortran3(a)
>>> bool((a == c).all())
True
>>> a = np.ones((4, 6, 8), dtype=np.float64, order='C')
>>> c = copy_fortran3(a)
>>> bool((a == c).all())
True
"""
cdef int x, y, z
x, y, z = np.shape(mat)
if 1 == x == y == z:
# C- or F- contiguous just means "contiguous".
if mat.is_c_contig() or mat.is_f_contig():
return mat.base
else:
return np.asarray(mat.copy_fortran())
elif mat.is_f_contig():
return mat.base
else:
return np.asarray(mat.copy_fortran())
def copy_fortran2(double[:, :] mat):
"""
>>> a = np.ones((1, 1), dtype=np.float64)
>>> c = copy_fortran2(a)
>>> bool((a == c).all())
True
>>> a = np.ones((4, 6), dtype=np.float64, order='F')
>>> c = copy_fortran2(a)
>>> bool((a == c).all())
True
>>> a = np.ones((4, 6), dtype=np.float64, order='C')
>>> c = copy_fortran2(a)
>>> bool((a == c).all())
True
"""
cdef int rows, cols
rows, cols = np.shape(mat)
if rows == 1 or cols == 1:
# C- or F- contiguous just means "contiguous".
if mat.is_c_contig() or mat.is_f_contig():
return mat.base
else:
return np.asarray(mat.copy_fortran())
elif mat.is_f_contig():
return mat.base
else:
return np.asarray(mat.copy_fortran())
|