Created
November 27, 2018 22:16
-
-
Save mevangelista-alvarado/67f8b98972cb1405db15aa264605e4d7 to your computer and use it in GitHub Desktop.
This file is a method for know if a differentiable form is symplectic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import ga | |
import sympy | |
#import ast | |
class PoissonGeometry(object): | |
def __init__(self, *args, **kwargs): | |
'''Inicialize global variables''' | |
self.variables = {} | |
def create_variables(self, dimension, variable='x'): | |
''' This method generate dinamic symbols variables | |
from a number that is the dimension and the variable | |
for the default is 'x' | |
''' | |
iteration = 1 | |
variables = self.variables | |
while iteration <= dimension: | |
#create symbols variables | |
x = "{}{}".format(variable, iteration) | |
variables[x] = sympy.Symbol("{}".format(x)) | |
iteration = iteration + 1 | |
#return a type symbolic list | |
return variables | |
def geometry(self, dimension): | |
''' This method create a geometry con a orthonormal metric | |
i.e; e_{i}=e_{j}=1 if i=j and e_{i}=e_{j}=0 if i!=j | |
''' | |
coordinates_aux = [] | |
variables = self.create_variables(dimension) | |
keys_variables = variables.keys() | |
for key in keys_variables: | |
coordinates_aux.append(variables[key]) | |
coordinates = self.reorder_list(coordinates_aux, self.list_coordinates(dimension)) | |
g=ga.Ga(self.variable_string(dimension), g=self.list_with_one(dimension), coords=coordinates) | |
#return a type multivector list g.mv() | |
return g.mv() | |
def bivector_basis(self, dimension): | |
''' This method create a basis for a bivector | |
''' | |
geometry = self.geometry(dimension) | |
basis = [] | |
i=0 | |
while i<len(geometry): | |
j=i+1 | |
while j<len(geometry): | |
basis.append(geometry[i]^geometry[j]) | |
j = j + 1 | |
i = i + 1 | |
#return a type symbol list | |
return basis | |
def bivector(self, dimension, bivector_aux=False): | |
''' This method calculates a bivector dynamically | |
''' | |
basis = self.bivector_basis(dimension) | |
variables = self.create_variables(dimension) | |
keys_variables = variables.keys() | |
bivector_list = [] | |
bivector = 0 | |
print("Please Only write the followig variables {}".format(keys_variables)) | |
for base in basis: | |
#print("Element {}".format(base)) | |
variable_bivector = str(raw_input("Enter a variable/constant for {}: ".format(base))) | |
test = self.split_plus_sign(variable_bivector) | |
split_final = self.split_minus_sign(test) | |
factor_aux = 1 | |
for element in split_final: | |
factores = self.factor(element, variables) | |
for factor in factores: | |
for variable in keys_variables: | |
if factor.find(variable) >= 0: | |
if factor.find('^') >= 0: | |
if factor.find('-') >= 0: | |
if factor.find('-'+variable) >= 0: | |
factor_aux = factor_aux*(-1)*variables[variable]**eval(factor[factor.find('^')+1:len(factor)]) | |
else: | |
const = factor[factor.find('-'):factor.find(variable)] | |
factor_aux = factor_aux*eval(const)*variables[variable]**eval(factor[factor.find('^')+1:len(factor)]) | |
else: | |
factor_aux = factor_aux*variables[variable]**eval(factor[factor.find('^')+1:len(factor)]) | |
elif factor.find('-') >= 0: | |
if factor.find('-'+variable) >= 0: | |
factor_aux = factor_aux*(-1)*variables[variable] | |
else: | |
const = factor[factor.find('-'):factor.find(variable)] | |
factor_aux = factor_aux*eval(const)*variables[variable] | |
else: | |
factor_aux = factor_aux*variables[variable] | |
else: | |
try: | |
factor_aux = factor_aux*eval(factor) | |
break | |
except: | |
continue | |
if bivector_aux: | |
bivector_list.append(factor_aux*base) | |
#HERE FOR BRACKET | |
factor_aux = 1 | |
else: | |
bivector_list.append(factor_aux*base) | |
factor_aux = 1 | |
for element in bivector_list: | |
bivector = bivector + element | |
#return a multivector type | |
return bivector | |
def is_symplectic(self, dimension): | |
''' This method has a dynamic differentiable form | |
from the dimension | |
''' | |
if dimension % 2 == 0: | |
bivector = self.bivector(dimension) | |
dimension = dimension/2 | |
form_volum = bivector | |
count = 2 | |
while count <= dimension: | |
form_volum = form_volum^bivector | |
count = count + 1 | |
if form_volum.is_zero(): | |
return "No simplectica" | |
else: | |
return "Es Simplectica" | |
else: | |
return "Numero no par, imposible obtener una estructura simplectica" | |
def SN_bracket(self, dimension): | |
bivector = self.bivector(dimension) | |
return bivector | |
def is_poisson(self, dimension): | |
bivector = self.bivector(dimension) | |
##### Functions Auxiliares ##### | |
def factor(self, chain, variables): | |
''' | |
''' | |
factores = [] | |
for variable in variables: | |
if chain.find(variable+"^") >= 0: | |
factores.append(chain[chain.find(variable+"^"):chain.find(variable+"^")+len(variable+"^")+1]) | |
chain = chain.replace(chain[chain.find(variable+"^"):chain.find(variable+"^")+len(variable+"^")+1], "") | |
elif chain.find("-"+variable) >= 0: | |
factores.append("-"+variable) | |
chain = chain.replace(variable, "") | |
elif chain.find(variable) >= 0: | |
factores.append(variable) | |
chain = chain.replace(variable, "") | |
else: | |
continue | |
try: | |
eval(chain) | |
factores.append(chain) | |
except Exception as e: | |
print('[HELLO] {}, type{}'.format(e, type(e))) | |
#clean the factores list | |
for factor in factores: | |
if factor == "": | |
factores.remove(factor) | |
else: | |
pass | |
#return a type list | |
return factores | |
def split_plus_sign(self, chain): | |
'''This method split a chain between '+' | |
''' | |
chain_split = [] | |
if chain.find("+") > 0: | |
chain_split.append(chain[0:chain.find("+")]) | |
rest_chain = chain[chain.find("+")+1:len(chain)] | |
chain_split.append(rest_chain) | |
while rest_chain.find("+") > 0: | |
chain_split.remove(rest_chain) | |
if not rest_chain[0:rest_chain.find("+")] in chain_split: | |
chain_split.append(rest_chain[0:rest_chain.find("+")]) | |
rest_chain = rest_chain[rest_chain.find("+")+1:len(rest_chain)] | |
chain_split.append(rest_chain) | |
else: | |
chain_split.append(chain) | |
#return a type list | |
return chain_split | |
def split_minus_sign(self, chain_split): | |
'''This method split a chain between '-' | |
''' | |
final_split = [] | |
for element in chain_split: | |
if element.find("-") >= 0: | |
final_split.append(element[0:element.find("-")]) | |
final_split.append(element[element.find("-"):len(element)]) | |
else: | |
final_split.append(element) | |
for element in final_split: | |
aux_element = element[1:len(element)] | |
if aux_element.find('-') >= 0: | |
final_split.remove(element) | |
final_split.append(element[0:aux_element.find('-')+1]) | |
final_split.append(element[aux_element.find('-')+1:len(element)]) | |
for element in final_split: | |
if element == "": | |
final_split.remove(element) | |
else: | |
pass | |
#return a type list | |
return final_split | |
def list_coordinates(self, dimension): | |
'''This method create a list of legth "dimension" | |
''' | |
coordinates = [] | |
iteration = 1 | |
if dimension <= 3: | |
while iteration <= dimension: | |
coordinates.append(" ") | |
iteration = iteration + 1 | |
return coordinates | |
else: | |
return ["", "", ""] | |
def reorder_list(self, messy_list, list_coordinates): | |
'''This method orders a list with symbolic values | |
''' | |
if len(list_coordinates) >= 3: | |
list_coordinates[0] = messy_list[2] | |
list_coordinates[1] = messy_list[0] | |
list_coordinates[2] = messy_list[1] | |
return list_coordinates + messy_list[3:] | |
if len(list_coordinates) == 2: | |
list_coordinates[0] = messy_list[1] | |
list_coordinates[1] = messy_list[0] | |
return list_coordinates | |
def list_with_one(self, dimension): | |
'''This method creates a "dimension" length | |
list with value one in all the entries | |
''' | |
list_one = [] | |
iteration = 1 | |
while iteration <= dimension: | |
list_one.append(1) | |
iteration = iteration + 1 | |
return list_one | |
def variable_string(self, dimension, symbol='dx'): | |
'''This method generate dinamic variables | |
from a number (dimension) in a string | |
''' | |
iteration = 1 | |
variable = "" | |
while iteration <= dimension: | |
#create symbols variables | |
variable = variable + "{}{} ".format(symbol, iteration) | |
iteration = iteration + 1 | |
#return a type symbolic list | |
return variable[0:len(variable)-1] | |
#import code; code.interact(local=locals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment