1

If x denotes a d dimensional column vector and e_i denotes the ith standard basis in the Euclidean space R^d, I would like to compute the following third-order tensor in python in an efficient way:

enter image description here

Here, the circled-cross refers to the tensor outer product. For now I have been relying on the following rather inefficient code:

import numpy as np
from sktensor import ktensor,dtensor

d=5
x= np.random.normal(0,1,(d,1))

z= np.zeros((d,1))
I= np.identity(d)

T1= ktensor([x,x,x])
T2= ktensor([z,z,z])
T3= ktensor([z,z,z])
T4= ktensor([z,z,z])

for j in range(d):
    T2 = T2+ ktensor([I(:,j),I(:,j),x]
    T3 = T3+ ktensor([I(:,j),x,I(:,j)]
    T4 = T4+ ktensor([x,I(:,j),I(:,j)]

T= T1-T2-T3-T4
4
  • I'd suggest moving away from sktensor for a start, seems poorly maintained (I can't seem to get it running to compare). You could write this in pure numpy or maybe have a look at tensorflow. Commented Dec 18, 2017 at 16:40
  • @ncfirth: What would be the implementation of this quantity in numpy? Commented Dec 19, 2017 at 2:51
  • I'll write an answer in numpy, but I can't test whether it's faster as I can't install sktensor. Commented Dec 19, 2017 at 10:05
  • @ncfirth: It's possible to install sktensor in Ubuntu. Windows I am not sure. You need a distribution called nose and afterwards it's straightforward. Commented Dec 19, 2017 at 16:32

1 Answer 1

1

numpy based answer as request by OP's comment. Not tested against sktensor

import numpy as np
np.random.seed(42)
d = 5
x = np.random.normal(0,1, size=(d,1))
I = np.identity(d)

ans = np.outer(x, np.outer(x, x))
for i in range(d):
    ans -= np.outer(I[:, i], np.outer(I[:, i], x))
    ans -= np.outer(I[:, i], np.outer(x, I[:, i]))
    ans -= np.outer(x, np.outer(I[:, i], I[:, i]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.