Using np.einsum to calculate Ricci scalar

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Ricci scalar Scalar
Click For Summary

Discussion Overview

The discussion revolves around using NumPy's einsum function to calculate the Ricci scalar from the Ricci tensor and the metric tensor. Participants explore issues related to implementation, output expectations, and the use of simplification methods in Python.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in calculating the Ricci scalar using einsum, providing code and resources for reference.
  • Another participant claims success with a similar approach, emphasizing the correct interpretation of the Einstein summation notation.
  • Several participants challenge the original poster to clarify what "did not work" means, suggesting that more specific feedback is necessary for effective assistance.
  • There is a discussion about the use of the simplify function, with one participant noting that NumPy is primarily a numeric package and may not support algebraic simplification as expected.
  • A later reply indicates that the original poster eventually resolved the issue, attributing it to a type error and the removal of certain components from the code.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the initial problem, as there are multiple interpretations of what "did not work" means and differing opinions on the use of simplification methods. The final resolution appears to be individual to the original poster.

Contextual Notes

The discussion includes unresolved issues regarding the assumptions made about the input types and the behavior of the einsum function in conjunction with other methods like simplify.

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   Reactions: 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   Reactions: 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..
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
17K