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

What is np.einsum and how does it calculate the Ricci scalar?

np.einsum is a NumPy function used for efficient multi-dimensional array operations. It allows for the contraction of indices in a specified pattern, which is useful for calculating the Ricci scalar. The Ricci scalar is a mathematical quantity that represents the curvature of a space-time manifold.

What is the syntax for using np.einsum to calculate the Ricci scalar?

The syntax for using np.einsum to calculate the Ricci scalar is: np.einsum("ij,ji->", metric_tensor, ricci_tensor). This specifies the contraction of the metric tensor and the Ricci tensor, with the resulting scalar value as the output.

What are the advantages of using np.einsum for calculating the Ricci scalar?

Using np.einsum for calculating the Ricci scalar is advantageous because it allows for efficient and concise code. It also avoids the need for nested loops, which can be time-consuming and computationally expensive. Additionally, np.einsum can handle higher-dimensional tensors, making it a versatile tool for more complex calculations.

Can np.einsum be used for other tensor operations besides calculating the Ricci scalar?

Yes, np.einsum can be used for a variety of tensor operations, such as matrix multiplication, tensor contraction, and outer products. It is a powerful tool for performing efficient and concise array operations in scientific computing.

Are there any limitations or potential issues when using np.einsum for calculating the Ricci scalar?

One potential issue when using np.einsum for calculating the Ricci scalar is the need to carefully specify the indices and their contraction pattern. This can be confusing and error-prone, especially for more complex tensor operations. Additionally, for very large arrays, the memory usage and computational time may become a limitation.

Similar threads

  • Special and General Relativity
Replies
14
Views
2K
  • Special and General Relativity
Replies
1
Views
1K
  • Special and General Relativity
Replies
7
Views
2K
  • Advanced Physics Homework Help
Replies
9
Views
4K
  • Advanced Physics Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
16K
  • STEM Academic Advising
Replies
15
Views
4K
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top