Ask Your Question
1

Redefine Method in Sage class

asked 2019-01-19 07:42:20 -0600

sagenotdead gravatar image

updated 2019-01-19 07:48:08 -0600

I am using the a class to manipulate vector fields of $\mathbb{R}^n$. I am using an other convention on the lie brackets : to compute $XY=[X,Y]$ I have to enter $XY=-X.bracket(Y)$.

I would like to redefine the method "bracket()" so that I include this minus sign in the redefinition.

Of course I can each time put a minus sign in front all my definition or call Y.bracket(X) to compute [X,Y] but I don't want to use them (the display is then not adapted ...).

Here is my code

 from sage import *
   reset()

   def mybracket(A,B):
     return B.bracket(A)

   M.<x,y> = EuclideanSpace()
   X = M.vector_field(0,1-y*x^2, name='X')
   Y = M.vector_field(-(y-1),x, name='Y')

   YX = Y.mybracket(X)

AttributeError: 'VectorFieldFreeModule_with_category.element_class' object has no attribute 'mybracket'

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-01-19 08:16:23 -0600

eric_g gravatar image

updated 2019-01-19 08:18:59 -0600

It suffices to attach the function mybracket that you have defined to the vector field class, which is VectorField. You have to import the latter first:

from sage.manifolds.differentiable.vectorfield import VectorField
VectorField.mybracket = mybracket

So the whole code becomes:

from sage.manifolds.differentiable.vectorfield import VectorField
def mybracket(A, B):
     return B.bracket(A)
VectorField.mybracket = mybracket

Then

M.<x,y> = EuclideanSpace()
X = M.vector_field(0,1-y*x^2, name='X')
Y = M.vector_field(-(y-1),x, name='Y')
X.mybracket(Y).display()

yields

[Y,X] = (-x^2*y + 1) e_x + (-x^3 + 2*x*y^2 - 2*x*y) e_y

and

X.mybracket(Y) == -X.bracket(Y)

gives

True
edit flag offensive delete link more

Comments

Out of curiosity, could you do something similar for cython (pyx) files (i mean without creating a new class that inherits from the previous) ?

tmonteil gravatar imagetmonteil ( 2019-01-22 14:19:13 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-01-19 07:42:20 -0600

Seen: 53 times

Last updated: Jan 19