Python Using np.einsum to calculate Ricci scalar

AI Thread Summary
The discussion revolves around calculating the Ricci scalar using the Einstein summation convention with NumPy's einsum function. The initial attempt to compute R = g^{ij}R_{ij} faced issues, particularly with the integration of the simplify function. Users emphasized the importance of clearly stating the nature of the problem encountered, such as whether the output was incorrect, execution failed, or if there were type errors. A successful solution was found by removing the 'Array' part and the simplify function, which resolved a type error. The final working code effectively computes the Ricci scalar using einsum without complications.
Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
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
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:
[CODE lang="python" highlight="9-11"][[ 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[/CODE]
 
  • Like
Likes Arman777
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
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:
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.
 
Okay..Somehow I manage to do it..
 
Arman777 said:
Okay..Somehow I manage to do it..
What was the problem, and how did you fix it?
 
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