ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 03 Jun 2013 12:33:22 -0500Normalize vector to euclidean unit lengthhttp://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/I know I can compute a vector of unit length in a given direction using something like
v = some_vector_computation()
v = v/v.norm()
some_other_computation(v)
On the other hand, the most intuitive way to write this in a single line won't work:
some_other_computation(some_vector_computation().normalize())
This is because of the way `normalize` is defined, which is well documented and has been discussed in [this question](http://ask.sagemath.org/question/726/vectornormalize-function).
So I wonder, is there some other way to write the above in a single line? I'm not interested in solutions which call `some_vector_computation` twice, or have to name the vector, or use some lambda function, or similar hacks. I very much have a method in mind, but anything with similar complexibility and readability would be fine.
I know I could probably add my own method to the vector class at runtime, but modifying sage classes in this way doesn't exactly feel right. And I fear that using a non-standard method might make my code much harder to read, as others would expect being able to reproduce it, but won't be able to if they missed my custom method.
If there is no such method, do you agree that there should be, and that I should file a feature request on the Sage Trac?Wed, 10 Oct 2012 02:20:15 -0500http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/Comment by John Palmieri for <p>I know I can compute a vector of unit length in a given direction using something like</p>
<pre><code>v = some_vector_computation()
v = v/v.norm()
some_other_computation(v)
</code></pre>
<p>On the other hand, the most intuitive way to write this in a single line won't work:</p>
<pre><code>some_other_computation(some_vector_computation().normalize())
</code></pre>
<p>This is because of the way <code>normalize</code> is defined, which is well documented and has been discussed in <a href="http://ask.sagemath.org/question/726/vectornormalize-function">this question</a>.</p>
<p>So I wonder, is there some other way to write the above in a single line? I'm not interested in solutions which call <code>some_vector_computation</code> twice, or have to name the vector, or use some lambda function, or similar hacks. I very much have a method in mind, but anything with similar complexibility and readability would be fine.</p>
<p>I know I could probably add my own method to the vector class at runtime, but modifying sage classes in this way doesn't exactly feel right. And I fear that using a non-standard method might make my code much harder to read, as others would expect being able to reproduce it, but won't be able to if they missed my custom method.</p>
<p>If there is no such method, do you agree that there should be, and that I should file a feature request on the Sage Trac?</p>
http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/?comment=18901#post-id-18901Re trac tickets: see http://trac.sagemath.org/sage_trac/ticket/13393Wed, 10 Oct 2012 11:20:25 -0500http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/?comment=18901#post-id-18901Answer by Eviatar Bach for <p>I know I can compute a vector of unit length in a given direction using something like</p>
<pre><code>v = some_vector_computation()
v = v/v.norm()
some_other_computation(v)
</code></pre>
<p>On the other hand, the most intuitive way to write this in a single line won't work:</p>
<pre><code>some_other_computation(some_vector_computation().normalize())
</code></pre>
<p>This is because of the way <code>normalize</code> is defined, which is well documented and has been discussed in <a href="http://ask.sagemath.org/question/726/vectornormalize-function">this question</a>.</p>
<p>So I wonder, is there some other way to write the above in a single line? I'm not interested in solutions which call <code>some_vector_computation</code> twice, or have to name the vector, or use some lambda function, or similar hacks. I very much have a method in mind, but anything with similar complexibility and readability would be fine.</p>
<p>I know I could probably add my own method to the vector class at runtime, but modifying sage classes in this way doesn't exactly feel right. And I fear that using a non-standard method might make my code much harder to read, as others would expect being able to reproduce it, but won't be able to if they missed my custom method.</p>
<p>If there is no such method, do you agree that there should be, and that I should file a feature request on the Sage Trac?</p>
http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/?answer=15022#post-id-15022Note that as of Sage 5.7 (see [trac #13393](http://trac.sagemath.org/sage_trac/ticket/13393)) the `normalized` method normalizes to unit length, `normalize` has been deprecated, and `monic` is the old functionality.
sage: v = vector([3, 2, 1])
sage: v.normalized()
(3/14*sqrt(14), 1/7*sqrt(14), 1/14*sqrt(14))
sage: v.monic()
(1, 2/3, 1/3)Mon, 03 Jun 2013 12:33:22 -0500http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/?answer=15022#post-id-15022Answer by benjaminfjones for <p>I know I can compute a vector of unit length in a given direction using something like</p>
<pre><code>v = some_vector_computation()
v = v/v.norm()
some_other_computation(v)
</code></pre>
<p>On the other hand, the most intuitive way to write this in a single line won't work:</p>
<pre><code>some_other_computation(some_vector_computation().normalize())
</code></pre>
<p>This is because of the way <code>normalize</code> is defined, which is well documented and has been discussed in <a href="http://ask.sagemath.org/question/726/vectornormalize-function">this question</a>.</p>
<p>So I wonder, is there some other way to write the above in a single line? I'm not interested in solutions which call <code>some_vector_computation</code> twice, or have to name the vector, or use some lambda function, or similar hacks. I very much have a method in mind, but anything with similar complexibility and readability would be fine.</p>
<p>I know I could probably add my own method to the vector class at runtime, but modifying sage classes in this way doesn't exactly feel right. And I fear that using a non-standard method might make my code much harder to read, as others would expect being able to reproduce it, but won't be able to if they missed my custom method.</p>
<p>If there is no such method, do you agree that there should be, and that I should file a feature request on the Sage Trac?</p>
http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/?answer=14133#post-id-14133If you insist on a one-liner (which isn't necessarily easier to read than your original three liner in my opinion) and don't want to inject a new method or subclass Vector, you can always define your own top level normalize function and use function composition instead of chaining method calls:
def normalize(v): return v/v.norm()
some_other_computation(normalize(some_vector_computation()))Wed, 10 Oct 2012 16:27:37 -0500http://ask.sagemath.org/question/9410/normalize-vector-to-euclidean-unit-length/?answer=14133#post-id-14133