Update 'aplK.py'

This commit is contained in:
calquezar 2021-04-26 20:14:59 +02:00
parent d014315b01
commit 21d24227b3

224
aplK.py
View File

@ -1,9 +1,39 @@
reset()
r"""
<Short one-line summary that ends with no period>
<Paragraph description>
EXAMPLES::
<Lots and lots of examples>
AUTHORS:
- YOUR NAME (2005-01-03): initial version
- person (date in ISO year-month-day format): short desc
"""
# ****************************************************************************
# Copyright (C) 2021 YOUR NAME <your email>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.structure.parent import Parent
from sage.structure.element import Element
from sage.structure.unique_representation import UniqueRepresentation
from sage.categories.rings import Rings
from sage.structure.richcmp import rich_to_bool, op_EQ, op_NE, op_LT, op_LE, op_GT, op_GE
from sage.categories.morphism import Morphism
from sage.rings.rational_field import QQ
from sage.algebras.commutative_dga import GradedCommutativeAlgebra
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.functions.other import factorial
#####################################################################
class Apln(UniqueRepresentation):
@ -24,6 +54,7 @@ class Apln(UniqueRepresentation):
self.gca_with_relations = self.gca.quotient(self.gca.ideal(t_relation,y_relation),check=False)
dfs = {self.gca.gens_dict()[ts[i]]:self.gca.gens_dict()[ys[i]] for i in range(n+1)}
self.cdga = self.gca.cdg_algebra(dfs)
#self.inject_variables()
def _repr_(self):
return "Algebra Apln of dimension %s" % self.n
@ -38,6 +69,7 @@ class Apln(UniqueRepresentation):
return self.gca_with_relations(pol)
def homogeneousPart(self,pol,degree):
parts = [pol.homogeneous_parts()[k] for k in pol.homogeneous_parts() if list(k)[0]==degree]
hompart = 0
for p in parts:
@ -45,6 +77,7 @@ class Apln(UniqueRepresentation):
return hompart
def face(self,i):
if(self.n==0):
return 0
domain = self.cdga
@ -66,6 +99,7 @@ class Apln(UniqueRepresentation):
return domain.hom(codomain=codomain,im_gens=images)
def _reduce_polynomial(self,p,n):
## Input:
# - p: polynomial in variables t_0,..t_n,y_0,...,y_n
# - n: dimension of the algebra (apl)_n where p is defined
@ -100,6 +134,7 @@ class Apln(UniqueRepresentation):
# (After reducing the variables t_{n},y_{n} by the corresponding relations,
# the algorithm removes the variables y_{0},..,y_{n-1}).
xs = ['x'+str(x) for x in range(n)]
zs = ['z'+str(z) for z in range(n)]
R = PolynomialRing(QQ,names=xs+zs)
@ -117,6 +152,7 @@ class Apln(UniqueRepresentation):
## Output:
# - the integral of the polynomial after reducing the variables t_n, y_n by the corresponding relations
#
xs = ['x'+str(x) for x in range(n)]
R = PolynomialRing(QQ,names=xs)
r_gens = R.gens()
@ -127,13 +163,56 @@ class Apln(UniqueRepresentation):
result = result.subs({r_gens[n-1-i]:1-sum(r_gens[:n-1-i])})
return R(((-1)**(n*(n-1)/2)))*result
def integral2(self,p,n):
## Input:
# - p: polynomial in variables t_0,..t_n,y_0,...,y_n
# - n: dimension of the algebra (apl)_n where p is defined
## Output:
# - the integral of the polynomial after reducing the variables t_n, y_n by the corresponding relations
#
xs = ['x'+str(x) for x in range(n)]
R = PolynomialRing(QQ,names=xs)
r_gens = R.gens()
pol = R(self._convert_integrable_polynomial(p,n))
result = 0
for k in pol.dict().keys(): # k is a monomial
numerator = 1
denominator = n
for e in k: # e is an exponents
numerator *= factorial(e)
denominator += e
coef = pol.dict()[k]
result += coef*numerator/factorial(denominator)
return R(((-1)**(n*(n-1)/2)))*result
#####################################################################
class MorphismFromQQToAplK(Morphism):
def __init__(self, codomain):
self.simplicialComplex= codomain.K
Morphism.__init__(self, QQ, codomain)
def _call_(self, x):
S = self.simplicialComplex
params1 = {}
for d in range(S.dimension()+1):
for c in S._n_cells_sorted(d):
params1[tuple(c)]=x
A = AplK(S)
return AplKElement(A,params1)
#####################################################################
class AplK(UniqueRepresentation, Parent):
def __init__(self,K, category=None):
self.K = K
self.n = K.dimension()
Parent.__init__(self,category=category )
f = MorphismFromQQToAplK(self)
self.register_coercion(f)
def _repr_(self):
return "Algebra AplK over %s" % self.K
@ -173,6 +252,32 @@ class AplKElement(Element):
return C(self.parent(),values)
def _add_(self,other):
r"""
Return sum of the elements
INPUT:
- ``other`` -- An AplKEelement
OUTPUT: An AplKElement equal to the sum of self and the element passed as argument
EXAMPLES:
This example illustrates ... ::
sage: S = SimplicialComplex([[0,1],[0,2],[1,2]],is_mutable=False)
sage: apl = Apln(S.dimension())
sage: (t0, t1, y0, y1) = apl.generators()
sage: params1 = {(0,): t0, (1,): t0, (2,):3*t0, (0,1): t0+t1,(0,2): t0+3*t1, (1,2): t0+3*t1}
sage: params2 = {(0,): 3*t0, (1,): 4*t0, (2,):5*t0, (0,1): 3*t0+4*t1,(0,2): 3*t0+5*t1, (1,2): 4*t0+5*t1}
sage: A = AplK(S)
sage: e1 = AplKElement(A,params1)
sage: e2 = AplKElement(A,params2)
sage: e1+e2
{(0,): 4*t0, (1,): 5*t0, (2,): 8*t0, (0, 1): 4*t0 + 5*t1, (0, 2): 4*t0 + 8*t1, (1, 2): 5*t0 + 8*t1}
"""
C = self.__class__
if(self.elements.keys()!=other.elements.keys()):
raise ValueError("error: keys do not match")
@ -182,6 +287,7 @@ class AplKElement(Element):
return C(self.parent(),newValues)
def _sub_(self,other):
C = self.__class__
if(self.elements.keys()!=other.elements.keys()):
raise ValueError("error: keys do not match")
@ -191,6 +297,7 @@ class AplKElement(Element):
return C(self.parent(),newValues)
def _mul_(self,other):
C = self.__class__
if(self.elements.keys()!=other.elements.keys()):
raise ValueError("error: keys do not match")
@ -200,6 +307,7 @@ class AplKElement(Element):
return C(self.parent(),newValues)
def _richcmp_(self, other, op):
if(op==op_EQ):
if(self.elements.keys()!=other.elements.keys()):
return False
@ -213,6 +321,7 @@ class AplKElement(Element):
raise NotImplementedError("operation not implemented for this element class")
def differential(self):
C = self.__class__
newValues = {}
for k in self.elements.keys():
@ -220,10 +329,11 @@ class AplKElement(Element):
return C(self.parent(),newValues)
def check(self):
S = self.parent().simplicialComplex()
dim = S.dimension()
for n in range(1,dim+1):
print("Checking dimension " + str(n))
#print("Checking dimension " + str(n))
for simplex in S._n_cells_sorted(n):
for f in range(n+1):
sFace = tuple(S.face(simplex,f))
@ -232,6 +342,7 @@ class AplKElement(Element):
raise ValueError("the assignation is not valid")
def homogeneous_component(self,degree):
C = self.__class__
component = {}
for k in self.elements.keys():
@ -239,18 +350,21 @@ class AplKElement(Element):
return C(self.parent(),component)
def homogeneous_components(self):
components = {}
for n in range(self.dimension()+1):
components[n] = self.homogeneous_component(n)
return components
def is_homogeneous(self):
isHomogeneous = True
for k in self.elements.keys():
isHomogeneous = isHomogeneous and self.apl.cdga(self.elements[k]).is_homogeneous()
return isHomogeneous
def is_in_kernel(self):
diff = self.differential()
if(diff == self.nullElement()):
return True
@ -272,112 +386,8 @@ class AplKElement(Element):
#integration of each polynomial
for k in component.keys():
print("Integrating simplex " + str(k))
n = len(k)
n = len(k)-1
pol = self.apl.cdga(component[k])
cochain[k] = self.apl.integral(pol,n)
return cochain
#####################################################################
#S = SimplicialComplex([[0,1,2]],is_mutable=False)
S = SimplicialComplex([[0,1],[0,2],[1,2]],is_mutable=False)
apl = Apln(S.dimension())
apl.inject_variables()
params1 = {}
params1[(0,)] = t0
params1[(1,)] = t0
params1[(2,)] = 3*t0
params1[(0,1)] = t0+t1
params1[(0,2)] = t0+3*t1
params1[(1,2)] = t0+3*t1
params2 = {}
params2[(0,)] = 3*t0
params2[(1,)] = 4*t0
params2[(2,)] = 5*t0
params2[(0,1)] = 3*t0+4*t1
params2[(0,2)] = 3*t0+5*t1
params2[(1,2)] = 4*t0+5*t1
params3 = {}
for d in range(S.dimension()+1):
for c in S._n_cells_sorted(d):
params3[tuple(c)]=randint(1,100)*t0
params4 = {}
params4[(0,)] = 3*t0+y0
params4[(1,)] = 4*t0
params4[(2,)] = 5*t0
params4[(0,1)] = 3*t0+4*t1+y0
params4[(0,2)] = 3*t0+5*t1+y0
params4[(1,2)] = 4*t0+5*t1
A = AplK(S)
e1 = AplKElement(A,params1)
e2 = AplKElement(A,params2)
#e3 = AplKElement(A,params3)
e4 = AplKElement(A,params4)
class morphism_from_QQ_to_AplK(Morphism):
def __init__(self, codomain):
self.simplicialComplex= codomain.K
Morphism.__init__(self, QQ, codomain)
def _call_(self, x):
S = self.simplicialComplex
params1 = {}
for d in range(S.dimension()+1):
for c in S._n_cells_sorted(d):
params1[tuple(c)]=x
A = AplK(S)
return AplKElement(A,params1)
f = morphism_from_QQ_to_AplK(A)
A.register_coercion(f)
#p = t0*t1**2*y0+t0*y1
S = SimplicialComplex([[0,1,2,3,4,5,6]],is_mutable=False)
apl = Apln(S.dimension())
apl.inject_variables()
p = y0*y1*y2*y3*y4*y5+y0*y1*y2*y3*y6+t4*t5
initial_assignation = {}
initial_assignation[(0,1,2,3,4,5,6)] = p
def generateElement(S,initial_assignation):
apl = Apln(S.dimension())
aplk_structure = initial_assignation.copy()
for n in reversed(range(1,S.dimension()+1)):
print("Generating dimension "+ str(n))
for simplex in S._n_cells_sorted(n):
p = aplk_structure[tuple(simplex)]
for f in range(n+1):
sFace = tuple(S.face(simplex,f))
aFace = apl.face(f)(p)
if(sFace not in aplk_structure.keys()):
aplk_structure[tuple(sFace)] = aFace
elif(aplk_structure[tuple(sFace)] != aFace):
raise ValueError("Error during the checking. The assignation is not valid")
A = AplK(S)
return AplKElement(A,aplk_structure)
g = generateElement(S,initial_assignation)
#g.check()
#cochain = g.integral()
#n = 1
#a = Apln(n)
#a.inject_variables()
#p = t0*y1
##print(p)
#print(a._reduce_polynomial(p,n))
#print(a._convert_integrable_polynomial(p,n))
#print(a.integral(p,n))