Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] numpy.insert() #463

Open
water5 opened this issue Dec 19, 2021 · 8 comments
Open

[FEATURE REQUEST] numpy.insert() #463

water5 opened this issue Dec 19, 2021 · 8 comments
Labels
enhancement New feature or request snippet `numpy` functions that should be implemented in `python`
Milestone

Comments

@water5
Copy link

water5 commented Dec 19, 2021

https://numpy.org/doc/1.21/reference/generated/numpy.insert.html

b = np.array([[1, 2, 3], [4, 5, 6]])

array([[1, 2, 3],
[4, 5, 6]])

np.insert(b, 1, [7, 8, 9])

array([1, 7, 8, 9, 2, 3, 4, 5, 6])

np.insert(b, 1, [7, 8, 9], axis = 0)

array([[1, 2, 3],
[7, 8, 9],
[4, 5, 6]])

@water5 water5 added the enhancement New feature or request label Dec 19, 2021
@v923z v923z added this to the 4.1 milestone Jan 2, 2022
@v923z
Copy link
Owner

v923z commented Jan 19, 2022

@water5 Wouldn't this basically duplicate the functionality of concatenate? https://micropython-ulab.readthedocs.io/en/latest/ulab-ndarray.html?highlight=concatenate#concatenate

@water5
Copy link
Author

water5 commented Jan 19, 2022

But that can't specify where to insert, I consider numpy.append before post this request.

@v923z
Copy link
Owner

v923z commented Jan 19, 2022

Obviously, you first have to split your original array into two with slicing (that is, where you specify where you want to insert the values), and then concatenate the first slice, the value, and the second slice.

a = array([1, 2, 3, 4, 5, 6])
value = array([10, 11])
concatenate(a[:3], value, a[3:])

@v923z
Copy link
Owner

v923z commented Jan 19, 2022

Append is similar

a = array([1, 2, 3, 4, 5, 6])
b = array([10, 20, 30])
c = zeros(len(a) + len(b))
c[:len(a)] = a
c[len(a):] = b

@water5
Copy link
Author

water5 commented Jan 19, 2022

On 2D array,

a = np.arange(25).reshape(5, 5)
a

array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])

b = np.arange(25, 31)
b

array([25, 26, 27, 28, 29])

np.insert(a, 1, b, axis = 0)

array([[ 0, 1, 2, 3, 4],
[25, 26, 27, 28, 29],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])

np.insert(a, 1, b, axis = 1)

array([[ 0, 25, 1, 2, 3, 4],
[ 5, 26, 6, 7, 8, 9],
[10, 27, 11, 12, 13, 14],
[15, 28, 16, 17, 18, 19],
[20, 29, 21, 22, 23, 24]])

np.concatenate(a[ : 1], b, a[1 : ])

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 180, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index

@v923z
Copy link
Owner

v923z commented Jan 19, 2022

a = array(range(25)).reshape((5,5))
b = np.arange(25, 30).reshape((1,5))
np.concatenate((a[:1], b, a[1:]))

@water5
Copy link
Author

water5 commented Jan 19, 2022

Right, that seems could implement most numpy.insert function.
I close this issue directly, or make a snippet?

@v923z
Copy link
Owner

v923z commented Jan 19, 2022

Right, that seems could implement most numpy.insert function. I close this issue directly, or make a snippet?

You could definitely write a small snippet, if you feel like it. In that case, add the snippet label to the issue, and keep it open till you are done, otherwise, close it.

@v923z v923z added the snippet `numpy` functions that should be implemented in `python` label Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request snippet `numpy` functions that should be implemented in `python`
Projects
None yet
Development

No branches or pull requests

2 participants