ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 03 Mar 2012 06:57:03 +0100Norm in a quadratic spacehttps://ask.sagemath.org/question/8763/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,Thu, 01 Mar 2012 11:29:43 +0100https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/Answer by Jason Grout for <p>Hi</p>
<p>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. </p>
<p>Here is an example :</p>
<pre><code>SP=matrix(QQ,[[2,0],[0,3]])
V=VectorSpace(QQ,2,inner_product_matrix=SP)
</code></pre>
<p>What I want to compute is e.g.</p>
<pre><code>e0=V.0
e0.inner_product(e0)
</code></pre>
<p>while <code>e0.norm()</code> gives me 1 (wrong answer for me)</p>
<p>I assume this should be possible directly. Any idea how ?</p>
<p>Thanks,</p>
https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/?answer=13325#post-id-13325Shouldn'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.Sat, 03 Mar 2012 02:42:36 +0100https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/?answer=13325#post-id-13325Comment by niles for <p>Shouldn't the induced norm be <code>sqrt(e0.inner_product(e0))</code>? I would just write a function to do this:</p>
<pre><code>def mynorm(v):
return sqrt(v.inner_product(v))
</code></pre>
<p>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.</p>
<p>Alternatively, if you wanted to modify the code in <code>devel/sage/sage/modules/free_module_element.py</code>, you could submit a patch. In the norm function, I would check to see if the inner product matrix defines the dot product:</p>
<pre><code>if not self.parent().__inner_product_is_dot_product():
special-case code for inner product matrices
</code></pre>
<p>Also, you might change the <code>__abs__</code> function.</p>
https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/?comment=20178#post-id-20178oops -- 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 :) Sat, 03 Mar 2012 06:57:03 +0100https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/?comment=20178#post-id-20178Answer by niles for <p>Hi</p>
<p>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. </p>
<p>Here is an example :</p>
<pre><code>SP=matrix(QQ,[[2,0],[0,3]])
V=VectorSpace(QQ,2,inner_product_matrix=SP)
</code></pre>
<p>What I want to compute is e.g.</p>
<pre><code>e0=V.0
e0.inner_product(e0)
</code></pre>
<p>while <code>e0.norm()</code> gives me 1 (wrong answer for me)</p>
<p>I assume this should be possible directly. Any idea how ?</p>
<p>Thanks,</p>
https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/?answer=13321#post-id-13321You 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.
Thu, 01 Mar 2012 12:45:58 +0100https://ask.sagemath.org/question/8763/norm-in-a-quadratic-space/?answer=13321#post-id-13321