# Norm in a quadratic space

Hi

I'm working in a vector space where the inner product is not the usual one and I would need to access directly to the norm induced by inner product. I found how to create such a space but the 'norm' function gives me the usual norm, which is not the one I want.

Here is an example :

SP=matrix(QQ,[[2,0],[0,3]])
V=VectorSpace(QQ,2,inner_product_matrix=SP)


What I want to compute is e.g.

e0=V.0
e0.inner_product(e0)


while e0.norm() gives me 1 (wrong answer for me)

I assume this should be possible directly. Any idea how ?

Thanks,

edit retag close merge delete

Sort by » oldest newest most voted

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 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()
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.

more

Shouldn't the induced norm be sqrt(e0.inner_product(e0))? I would just write a function to do this:

def mynorm(v):
return sqrt(v.inner_product(v))


I've heard that Sage could do more to support working with non-standard inner product matrices. This might be an example of a place that could be improved.

Alternatively, if you wanted to modify the code in devel/sage/sage/modules/free_module_element.py, you could submit a patch. In the norm function, I would check to see if the inner product matrix defines the dot product:

if not self.parent().__inner_product_is_dot_product():
special-case code for inner product matrices


Also, you might change the __abs__ function.

more

oops -- I forgot to add sqrt; fixing now . . . my understanding was that the OP needed to use the built-in method e0.norm() for some reason (presumably it's being called from some other function that also cannot be modified). If this is not the case, your solution is of course better :)

( 2012-03-02 23:57:03 -0600 )edit