Added a warning when constructing a Matrix without bracket + test modified
[sympy.git] / examples / tensors.py
blob4eda747b68f916c0021311b0cdfa1a228ac6f06c
1 #!/usr/bin/env python
2 import iam_sympy_example
4 """
5 http://www.lncc.br/~portugal/Canon.html
6 http://www.lncc.br/~portugal/Invar.html
7 http://www.ginac.de/tutorial/Indexed-objects.html
8 http://grtensor.phy.queensu.ca/
10 """
12 from sympy import exp, Symbol, sin, Rational, Derivative, dsolve
14 from sympy.core import Basic, Function
15 from sympy.matrices import Matrix
17 class Indexed(Basic):
18 def __init__(self, A, idxlist):
19 self._args = [A, idxlist]
21 def __str__(self):
22 r = str(self[0])
23 for idx in self[1]:
24 r+=str(idx)
25 return r
27 class Idx(Symbol):
28 def __init__(self, name, dim = 4, up = True):
29 Symbol.__init__(self, name)
30 #self._args.extend([dim,up])
31 self._name = name
32 self._dim = dim
33 self._up = up
35 def __str__(self):
36 if self._up:
37 r = "^"
38 else:
39 r = "_"
40 return r+self._name
42 @property
43 def up(self):
44 return Idx(self._name, self._dim, True)
46 @property
47 def dn(self):
48 return Idx(self._name, self._dim, False)
50 def values(self):
51 return range(self._dim)
53 t=Symbol("t")
54 r=Symbol("r")
55 theta=Symbol(r"\theta")
56 phi=Symbol(r"\phi")
58 class nu(Function):
59 pass
60 class lam(Function):
61 pass
63 gdd=Matrix((
64 (-exp(nu(r)),0,0,0),
65 (0, exp(lam(r)), 0, 0),
66 (0, 0, r**2, 0),
67 (0, 0, 0, r**2*sin(theta)**2)
70 mu = Idx("mu")
71 nu = Idx("mu")
72 i = Idx("i")
73 m = Idx("m")
74 k = Idx("k")
75 l = Idx("l")
76 g = Indexed(Symbol("A"), [mu,nu])
77 Chr = g[i.up, m.up]/2 * (g[m.dn, k.dn].diff(l.up) + g[m.dn,l.dn].diff(k.up) \
78 - g[k.dn, l.dn].diff(m.up))
79 #G = g.uu(i,m)/2 * (g.dd(m,k).diff(x[l])+g.dd(m,l).diff(x[k]) \
80 # - g.dd(k,l).diff(x[m]))
82 print g
83 print Chr