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
SUMMARY

The discussion centers on using NumPy's einsum function to calculate the Ricci scalar, represented as R = g^{ij}R_{ij}. A user initially encountered issues with their implementation, specifically with the line self.ricciscalar_obj = simplify(einsum('ij,ij->', ricci_tensor, metric_tensor, optimize='optimal')). The solution involved removing the Array part and the simplify function, leading to a successful calculation with self.ricciscalar_obj = einsum('ij,ij', self.riccitensor_obj, self.inverse_metric_obj, optimize='optimal'). This highlights the importance of understanding the output and error messages when debugging.

PREREQUISITES
  • Familiarity with NumPy 1.21 or later, specifically the einsum function.
  • Understanding of Einstein summation notation in tensor calculus.
  • Basic knowledge of Python programming and error handling.
  • Experience with matrix operations and tensor manipulations.
NEXT STEPS
  • Study the documentation for numpy.einsum to understand its syntax and capabilities.
  • Learn about Einstein summation notation and its applications in physics and mathematics.
  • Explore debugging techniques in Python to effectively handle type errors and other exceptions.
  • Investigate the differences between NumPy and SymPy for numerical versus symbolic computations.
USEFUL FOR

This discussion is beneficial for data scientists, physicists, and software developers working with numerical computations in Python, particularly those utilizing NumPy for tensor operations and matrix calculations.

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