Feeds:
Posts

## SAGE tip: GRmodule. Day 04.

Hello everyone. I’ll do it quick, ’cause I’m really tired, and by the way, today… Feb. 8th, I’m turning 30 years old 🙂

First of all, I create a page in my google site for the GRmodule files. Note that the name is GRmodule and not GR-module, because the dash is not admissible in a module name (beginner mistake!)

#### In the last post…

The Metric class was defined, and the first steps of the implementation were walked.

## Inverting the metric

Once the components of the metric are given, one can call the invert method for assigning the inverse of the metric. Don’t know why, but I’d like to keep track of what’s going on! so, I change a little bit the code from Sergei, and my invert method returns the inverse metric tensor,

```  def invert(self):
'''Find the inverse of the metric and store the result in a
Metric object self.inverse'''

'''Create a unit matrix of dimension dim'''
temp = sympy.eye(self.dim)

'''Assign the values of the metric to temp'''
for key in self.components.keys():
id = tuple(np.abs(key))
temp[id] = self.components[key]

'''invert the matrix with inv() from sympy'''
inv = temp.inv()
'''convert the matrix in a dictionary'''
inverse = self._dictkeycopy(self.components)
for i in range(self.dim):
for j in range(self.dim):
inverse[i,j] = inv[i,j]
self.inverse = Metric(self.coord,rank=(2,0),sh=(1,1),symbol='g_inv')
self.inverse.components = inverse
return self.inverse```

First a temporal matrix is created, and just for assuring it’s invertible, one creates a unit matrix of dimension dim. That’s what the sympy.eye does!

Secondly, the values of the metric are assigned to temp.

The temp matrix is inverted. The sympy command to inver a matrix is inv.

Next, the inverse matrix is converted in a dictionary (just like in previous cases).

Finally, the characteristic of tensor is given to the inverse matrix object… and it’s returned!, i.e., it can be assigned.

In the implementation file the components of the metric are assigned, the metric is invert… and the result of the inversion is printed.

### How to run it?!

• Download the latest files and store them in the same folder, e.g., GR.
• Open a terminal and move to the GR folder ` cd path/to/GR`
• In the terminal type ` \$ python Proof-GR-module.py > Result.txt`
• Open the Result.txt file, i.e., `\$ emacs Result.txt `
• Enjoy life!

DOX

Happy B-day to me!!! 🙂

### 4 Responses

1. Hi,

you can also try the relativity module that I wrote for sympy, it’s in the examples directory, here is a sample output:

I just noticed the final solve() fails in the latest sympy, so I reported it:

anyway, if you are interested in this, feel free to get in touch with us on the sympy mailinglist (see sympy.org). You could for example contribute a GR module into SymPy, so far it’s only an example. That way, a lot more people could use it.

Happy birthday!

Ondrej

• Hi Ondrej:

Than you for your comment! I saw the links you posted, and looks great! But I found the output of the GR calculation, and not the source python file.

The other thing is that in the sympy documentation there is a section about Geometric algebra… quite wonderful, but I found nothing about differential geometry.

Finally, I’ll add your list to my feeds 😉 for keeping the contact!

Thank you for the contribution and the congraz! 🙂

• Hi Oscar,

the source file is in the sympy git repository (should be also in the tarballs):