1 | initial version |
You could redefine the norm
function in a subclass. Note that your e0
is an object of class Vector_rational_dense
:
sage: V=VectorSpace(QQ,2,inner_product_matrix=SP)
sage: e0=V.0
sage: type(e0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
So you can create a subclass, with a new norm
function:
sage: from sage.modules.vector_rational_dense import Vector_rational_dense as Vrd
sage: class MyVector(Vrd):
....: def norm(self):
....: return self.inner_product(self)
....:
and write a helper function to take objects from the old class and make ones in the new class:
sage: def improve_norm(v):
....: return MyVector(v.parent(),v.list())
....:
Here's how you could use it:
sage: f0 = improve_norm(e0)
sage: e0.norm()
1
sage: f0.norm()
2
Since MyVector
is a subclass of Vector_rational_dense
, the two should behave entirely the same, except for the new methods of MyVector
:
sage: e0.parent() == f0.parent()
True
sage: e0 == f0
True
Be careful though; unless you do a bit more work, arithmetic with MyVector
objects will produce Vector_rational_dense
objects by default:
sage: type(f0)
<class '__main__.MyVector'>
sage: type(e0 + f0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
sage: type(f0+f0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
sage: type(3*f0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
this is because the addition and multiplication methods are still those of Vector_rational_dense
, which return an object of that type. If you want to fix this, you could also redefine _rmul_
, _add_
, etc.
2 | norm should be sqrt(self.inner_product(self)) |
You could redefine the norm
function in a subclass. Note that your e0
is an object of class Vector_rational_dense
:
sage: V=VectorSpace(QQ,2,inner_product_matrix=SP)
sage: e0=V.0
sage: type(e0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
So you can create a subclass, with a new norm
function:
sage: from sage.modules.vector_rational_dense import Vector_rational_dense as Vrd
sage: class MyVector(Vrd):
....: def norm(self):
....: return self.inner_product(self)
sqrt(self.inner_product(self))
....:
and write a helper function to take objects from the old class and make ones in the new class:
sage: def improve_norm(v):
....: return MyVector(v.parent(),v.list())
....:
Here's how you could use it:
sage: f0 = improve_norm(e0)
sage: e0.norm()
1
sage: f0.norm()
2
sqrt(2)
Since MyVector
is a subclass of Vector_rational_dense
, the two should behave entirely the same, except for the new methods of MyVector
:
sage: e0.parent() == f0.parent()
True
sage: e0 == f0
True
Be careful though; unless you do a bit more work, arithmetic with MyVector
objects will produce Vector_rational_dense
objects by default:
sage: type(f0)
<class '__main__.MyVector'>
sage: type(e0 + f0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
sage: type(f0+f0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
sage: type(3*f0)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
this is because the addition and multiplication methods are still those of Vector_rational_dense
, which return an object of that type. If you want to fix this, you could also redefine _rmul_
, _add_
, etc.