Questions tagged [numpy-einsum]

NumPy's `einsum` function implements the Einstein summation convention for multidimensional array objects. Use this tag for questions about how `einsum` can be applied to a particular problem in NumPy, or more questions about how the function works.

NumPy's einsum function implements the Einstein summation convention for multidimensional array objects. This allows many operations involving the multiplication or summation of values along particular axes to be expressed succinctly.

249 questions
5
votes
2 answers

Write numpy einsum operation as eigen tensors

I want to write the following numpy einsum as a an Eigen Tensor op import numpy as np L = np.random.rand(2, 2, 136) U = np.random.rand(2, 2, 136) result = np.einsum('ijl,jkl->ikl', U, L) I can write it with for loops like so in C++ for (int i =…
Niteya Shah
  • 1,809
  • 1
  • 17
  • 30
5
votes
1 answer

Determining the validity of a multi-hot encoding

Suppose I have N items and a multi-hot vector of values {0, 1} that represents inclusion of these items in a result: N = 4 # items 1 and 3 will be included in the result vector = [0, 1, 0, 1] # item 2 will be included in the result vector = [0, 0,…
osama
  • 622
  • 2
  • 10
  • 19
5
votes
4 answers

Numpy make the product between all elemens and then insert into a triangular 2d array

Suppose we got a 1D array below arr = np.array([a,b,c]) The first thing I need to do is the make the product of all of the elments, i.e [ab,ac,bc] Then construct a 2d triangular array with this element [ [a,ab,ac], [0,b,bc], [0,0,c] ]
Nicolas H
  • 535
  • 3
  • 13
5
votes
1 answer

Most Pythonic way to multiply these two vectors?

I have two ndarrays with shapes: A = (32,512,640) B = (4,512) I need to multiply A and B such that I get a new ndarray: C = (4,32,512,640) Another way to think of it is that each row of vector B is multiplied along axis=-2 of A, which results in…
wolfblade87
  • 173
  • 10
5
votes
1 answer

Numpy Einsum Path Differences and the Optimize Argument

I have the following tensor execution, np.einsum('k,pjqk,yzjqk,yzk,ipqt->it', A, B, C, D, E) And I noticed that when 'z' or 'q' expanded in dimension the execution time really suffered, although my intuition was that it probably shouldn't be that…
Attack68
  • 4,437
  • 1
  • 20
  • 40
5
votes
1 answer

Vectorising numpy.einsum

I have following four tensors H (h, r) A (a, r) D (d, r) T (a, t, r) For each i in a, there is a corresponding T[i] of the shape (t, r). I need to do a np.einsum to produce the following result (pred): pred = np.einsum('hr, ar, dr, tr ->hadt', H,…
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
5
votes
1 answer

How do I do an einsum that mimics 'keepdims'?

a python question: I've got a np.einsum operation that I'm doing on a pair of 3d arrays: return np.einsum('ijk, ijk -> ik', input_array, self._beta_array) Problem I'm having is the result is 2d; the operation collapses the 'j' dimension. What I'd…
Calvin_xc1
  • 107
  • 2
  • 7
5
votes
1 answer

Processing upper triangular elements only with NumPy einsum

I'm using numpy einsum to calculate the dot products of an array of column vectors pts, of shape (3,N), with itself, resulting on a matrix dotps, of shape (N,N), with all the dot products. This is the code I use: dotps = np.einsum('ij,ik->jk', pts,…
martinako
  • 2,690
  • 1
  • 25
  • 44
4
votes
0 answers

Comparing Tullio to numpy.einsum

I am currently importing a Julia script that uses Tullio because of its speed. The function is using Tullio, LoopVectorization function testfunction_tullio(my_arr, other_arr, sec_arr, third_arr) new_array = Array{ComplexF64}(undef,…
Andrew Hardy
  • 232
  • 1
  • 8
4
votes
2 answers

Combining sparse and einsum to perform large sparse sum

I have a matrix A with shape=(N, N) and a matrix B with the same shape=(N, N). I am constructing a matrix M using the following einsum (using the opt_einsum library): M = oe.contract('nm,in,jm,pn,qm->ijpq', A, B, B, B, B) This is evaluating the…
Jack Wetherell
  • 565
  • 3
  • 8
  • 16
4
votes
2 answers

Looping over np.einsum many times... Is there a faster way?

I have a likelihood function that I am trying to sample with MCMC. I have used no for loops in the log likelihood itself, but I do call np.einsum() once. Here's a sample of what my current code looks like: A = np.random.rand(4,50,60,200) # Random…
Jsn
  • 107
  • 6
4
votes
2 answers

Outer product calculation by numpy einsum

I am trying to dive into the einsum notation. This question and answers have helped me a lot. But now I can't grasp the machinery of the einsum when calculating outer product: x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) np.einsum('i,j->ij', x,…
Kenenbek Arzymatov
  • 8,439
  • 19
  • 58
  • 109
4
votes
2 answers

In Python's Numpy, a dot product isn't equivalent to an einsum, and I'm not sure why not

But obviously I'm doing something wrong. I've been chasing a bug all night, and I've finally solved it. Consider: xs = np.arange(100 * 3).reshape(100, 3) W = np.arange(3 * 17).reshape(3, 17) a = np.einsum('df, hg -> dg', xs, W) b = np.dot(xs,…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
4
votes
1 answer

Einstein notation for numpy dot product

How can I write the following dot product using einstein notation? import numpy as np LHS = np.ones((5,20,2)) RHS = np.ones((20,2)) np.sum([ np.dot(LHS[:,:,0], RHS[:,0]), np.dot(LHS[:,:,1], RHS[:,1]), ], axis=0)
Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73
4
votes
1 answer

einstein summation of boolean arrays in numpy

Einstein summation (numpy.einsum) of boolean arrays in numpy doesn't produce expected results. Numpy.einsum function does logical operations on boolean arrays, which is questionable in the numeric contexts. # summation of a boolean numpy array x =…
yuyangtj
  • 43
  • 5
1
2
3
16 17