-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbimoduleMapDecorator.py
32 lines (31 loc) · 1.3 KB
/
bimoduleMapDecorator.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
import pureTensor
class bimoduleMapDecorator(object):
"""
File: chainMaps.py
Author: Chris Campbell
Email: c (dot) j (dot) campbell (at) ed (dot) ac (dot) uk
Github: https://github.com/campbellC
Description: A decorator that extends a function defined on a tensor algebra bilinearly with respect to some algebra.
You must pass it the domain and codomain tensor algebras in order for it to reduce input and output for you.
"""
def __init__(self, domain, codomain):
self.domain = domain
self.codomain = codomain
def __call__(self,func):
def wrapped_func(tens):
if tens == 0:
return self.codomain.zero()
tens = tens.clean()
tens = self.domain.reduce(tens)
firstItem = True
for pure in tens:
left =pure[0]
right = pure[-1]
middle = pureTensor.pureTensor(1).tensorProduct(pure.subTensor(1,len(pure)-1)).tensorProduct(1)
if firstItem:
answer = pure.coefficient * left * func(middle) * right
firstItem = False
else:
answer = answer + pure.coefficient * left * func(middle) * right
return self.codomain.reduce(answer)
return wrapped_func