Ask Your Question

Norm in a quadratic space

asked 2012-03-01 04:29:43 -0500

Bertrand gravatar image


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 :


What I want to compute is e.g.


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

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


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-03-01 05:45:58 -0500

niles gravatar image

updated 2012-03-02 23:58:15 -0500

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()
sage: f0.norm()

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()
sage: e0 == f0

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.

edit flag offensive delete link more

answered 2012-03-02 19:42:36 -0500

Jason Grout gravatar image

updated 2012-03-02 19:44:25 -0500

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/, 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.

edit flag offensive delete link 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 :)

niles gravatar imageniles ( 2012-03-02 23:57:03 -0500 )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


Asked: 2012-03-01 04:29:43 -0500

Seen: 172 times

Last updated: Mar 02 '12