-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensorAlgebra.py
57 lines (45 loc) · 1.83 KB
/
tensorAlgebra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import polygnomeObject
import pureTensor
import tensor
import abstractPolynomial
class tensorAlgebra(polygnomeObject.polygnomeObject):
"""
File: tensorAlgebra.py
Author: Chris Campbell
Email: c (dot) j (dot) campbell (at) ed (dot) ac (dot) uk
Github: https://github.com/campbellC
Description: This class encapsulates tensor algebras so that one can reduce
tensors of elements from different algebras.
"""
def __init__(self, algebras=()):
self.algebras = tuple(algebras)
def reduce(self,tens):
if len(self) == 1: #If this is just an algebra
return self[0].reduce(tens)
newTens = tensor.tensor()
for pure in tens:
polys = []
for index in range(len(pure)):
currentAlgebra = self[index]
polys.append(currentAlgebra.reduce(pure[index]))
def listOfPolysToTensors(ps):
ps = iter(ps)
pureTensors = [pureTensor.pureTensor(mono) for mono in next(ps)]
for poly in ps:
newPureTensors = []
for mono in poly:
newPureTensors.extend([x.tensorProduct(mono) for x in pureTensors])
pureTensors = newPureTensors
return tensor.tensor(pureTensors)
newTens = newTens + pure.coefficient * listOfPolysToTensors(polys)
return newTens
def zero(self):
return pureTensor.pureTensor([0] * len(self))
def __getitem__(self,index):
return self.algebras[index]
def __len__(self):
return len(self.algebras)
def __repr__(self):
return 'A tensor algebra with algebras ' + '|'.join(repr(a) for a in self.algebras)
def toLatex(self):
return 'A tensor algebra with algebras ' + '|'.join(a.toLatex() for a in self.algebras)