Using np.einsum to calculate Ricci scalar

In summary, the problem was that you were using an Array instead of a list, and the simplify function was causing a type error.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
I was trying to calculate

$$R = g^{ij}R_{ij}$$ bu using einsum but I couldn't not work it out. Anyone can help me ? Here are some of the resources

https://stackoverflow.com/questions/26089893/understanding-numpys-einsum
https://www.gormanalysis.com/blog/python-numpy-for-your-grandma-6-2-einsum/
https://numpy.org/doc/stable/reference/generated/numpy.einsum.html

I have tried

Code:
self.ricciscalar_obj = simplify(einsum('ij,ij->', ricci_tensor, metric_tensor, optimize='optimal'))

but it did not work.
 
Technology news on Phys.org
  • #2
It worked for me. See code and output below. Perhaps you were expecting the wrong answer. The correct interpretation of the Einstein summation notation is that you multiply the two matrices componentwise and then sum the components.

Code:
import numpy as np

numpy.random.seed(10)
g = np.random.randint(0, high = 20, size = (4,4) )
print(g)
R = np.random.randint(0, high = 20, size = (4,4) )
print(R)
print(np.einsum('ij,ij', g, R) )
print(np.einsum('ij,ij->', g, R) )
prodd = 0
for i in range(4):
    for j in range(4):
        prodd += g[i,j] * R[i,j]
print(prodd )
Output:
Python:
[[ 9  4 15  0]
[17 16 17  8]
[ 9  0 10  8]
[ 4 19 16  4]]
[[15 11 11  1]
[ 8  4 14 17]
[19 13  5 13]
[19 13 12  1]]
1762
1762
1762
 
  • Like
Likes Arman777
  • #3
Arman777 said:
but it did not work.
This is not the first time you have posted 'it did not work'. This is not good enough, if you want anyone to help you, state how it did not work. Was the answer not what you expected? Was it close but not accurate enough? Did it fail to execute? Does the einsum part work but not when you add simplify? Does it work without the optimize='optimal' argument but run too slowly? Did your computer catch fire?
 
Last edited:
  • Like
Likes Arman777
  • #4
What are you trying to do with simplify? You do realize numpy is a numeric package, it doesn't do algebra so doesn't produce anything that can be sympy.simplifyed?

Edit: actually I think einsum may just call the + and * operators on the list elements so it might just work with non-numerics, but I wouldn't count on it. I'll edit my previous post to take up this point.
 
Last edited:
  • #5
Arman777 said:
it did not work
Which, as @pbuk says, is useless to us as far as helping you is concerned. You need to post the actual output from when you run the program, the way @andrewkirk did.
 
  • #6
Okay..Somehow I manage to do it..
 
  • #7
Arman777 said:
Okay..Somehow I manage to do it..
What was the problem, and how did you fix it?
 
  • #8
PeterDonis said:
What was the problem, and how did you fix it?
There was some sort of a type error

Writing this solved it

Code:
self.ricciscalar_obj = einsum('ij,ij', self.riccitensor_obj, self.inverse_metric_obj, optimize='optimal')

I just removed the Array part and also the simplify..I guess they were causing the problem..
 
Back
Top