Reversing axis in Numpy array using C-API
NickName:Andreas Mueller Ask DateTime:2011-08-09T18:26:04

Reversing axis in Numpy array using C-API

I am using the Python C-API to wrap some C++ code as a Python package. In the end, I have to reverse an axis in a numpy array, i.e. doing

x = x[:, ::-1]

Is there some way of doing this using the Numpy C-API? I know there are routines for transposing and swaping axes but I haven't found much about indexing. Any ideas? Thanks, Andy

Copyright Notice:Content Author:「Andreas Mueller」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6994725/reversing-axis-in-numpy-array-using-c-api

More about “Reversing axis in Numpy array using C-API” related questions

Reversing axis in Numpy array using C-API

I am using the Python C-API to wrap some C++ code as a Python package. In the end, I have to reverse an axis in a numpy array, i.e. doing x = x[:, ::-1] Is there some way of doing this using the ...

Show Detail

What is the corresponding C-api name of the following numpy function in Python

I wanna write a C extension lib for Python, aiming to replace Python code with C. and the Python codes has several lines like below: import numpy as np a = np.array([1,3,12,0.43,234,-3,-4]) b = a[...

Show Detail

Undo sorting of multidimensional numpy array along specific axis

I am sorting a large multidimensional numpy array along an axis. After some computations I'd like to undo the sorting with the values themselves being different to when I did the initial sorting.

Show Detail

How do I share the random number generator in numpy c-api?

I wrote a Python program for Monte Carlo simulations that calls numpy functions that makes use of the built-in random number generator (e.g., np.random.normal() or np.random.choice()) and also impo...

Show Detail

Convert numpy array to MemoryView object

I'm trying to convert a numpy array to a MemoryView object because I have to communicate between two programs. The one can only handle NumPy arrays and the other only MemoryView objects. Convertin...

Show Detail

Using numpy C API with ctypes

I want to use numpy C array api (https://numpy.org/doc/stable/reference/c-api/array.html) using ctypes python library. Similarly to how I can use python c api with ctypes. For example, I can refere...

Show Detail

Reversing an array in Cython

Problem This is related to a question I asked here, about reversing a 2D numpy array column-wise, with random indices per row. For example. np.random.seed(0) arr = np.repeat(np.arange(6)[np.new...

Show Detail

Determinant over a specific axis using numpy

Suppose I have a numpy array A with shape (j,d,d) and I want to obtain an array with shape j, in which each entry corresponds to the determinant of each (d,d) array. I tried using np.apply_along_ax...

Show Detail

axis = 3 for a 3D array in numpy?

I am confused as to why I dont get an error for axis = 3 in the following import numpy as np a = np.arange(27) b = np.arange(27) a = a.reshape((3,3,3)) b = b.reshape((3,3,3)) c = np.mean([a,b],a...

Show Detail

Iterate over last axis of a numpy array

Let's say we have a (20, 5) array. We can iterate over each row very pythonically: import numpy as np xs = np.array(range(100)).reshape(20, 5) for x in xs: print(x) If we want to iterate over

Show Detail