Try to reproduce NumPy's add.at in Julia

The title as it is.

NumPy code you want to reproduce

It's an in-place change in NumPy.

Python(NumPy)



>>> A = np.ones((3,3))
>>> A
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
>>> B = np.array([[1, 1, 1], [2, 2, 2]])
>>> B
array([[1, 1, 1],
       [2, 2, 2]])
>>> np.add.at(A, [0, 2], B)
>>> A
array([[2., 2., 2.],
       [1., 1., 1.],
       [3., 3., 3.]])

Julia code

Note that the . + operator is added to the + = operator to make it . + =, And broadcasting is performed. + = does not broadcast and is not an in-place change. However, . + = (Of course) broadcasts, but it is a ** in-place change ** [^ 1].

Julia



julia> A = ones(3,3)
3×3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> B = [1. 1. 1.; 2. 2. 2.]
2×3 Array{Float64,2}:
 1.0  1.0  1.0
 2.0  2.0  2.0

julia> selectdim(A, 1, [1, 3]) .+= B
2×3 view(::Array{Float64,2}, [1, 3], :) with eltype Float64:
 2.0  2.0  2.0
 3.0  3.0  3.0

julia> A
3×3 Array{Float64,2}:
 2.0  2.0  2.0
 1.0  1.0  1.0
 3.0  3.0  3.0

Sources numpy.ufunc.at — NumPy v1.19 Manual Arrays · The Julia Language Multi-dimensional Arrays · The Julia Language Mathematical Operations and Elementary Functions · The Julia Language

Recommended Posts

Try to reproduce NumPy's add.at in Julia
Try to calculate Trace in Python
Try to put data in MongoDB
Cython to try in the shortest
How to call PyTorch in Julia
Try logging in to qiita with Python
Try to implement Oni Maitsuji Miserable in python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Draw graphs in Julia ... Leave the graphs to Python
Try to calculate RPN in Python (for beginners)
Try to imitate Rails' _method parameter in webapp2
Try to make a Python module in C language
Try to improve your own intro quiz in Python
Try to solve Sudoku in various ways (SAT, CSP)
Try to delete tweets in bulk using Twitter API
Try to put LED in your own PC (slightly)
Try to decipher the login data stored in Firefox
Try to implement yolact
Try gRPC in Python
Try 9 slices in Python
Try to make it using GUI and PyQt in Python
Try to extract the keywords that are popular in COTOHA
Image analysis with Object Detection API to try in 1 hour
Try Q-learning in Dragon Quest-style battle [Introduction to Reinforcement Learning]
Try HeloWorld in your own language (with How to & code)
Just try to receive a webhook in ngrok and python