# Vector.normalize() function

My question is something of a small complaint and need of an explination. Why is the normalize() function for vectors defined as it is, and not the "normal" way one would expect? This is somewhat confusing and frustrating especially if you need to have a normalized vector and what you get is a vector divided by the first non-zero element.

edit retag close merge delete

This sounds like a good question. Can you post an explicit example in an edit to your question, just to make it easy to verify for others looking at it?

( 2011-08-29 02:59:12 -0600 )edit

Sort by » oldest newest most voted

Note that as of Sage 5.7 (see trac #13393) the normalized method does what you'd expect, 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)

more

You can easily figure out what any given function or method does by examining its docstring as follows:

sage: v = vector([3,0,9])
sage: v.normalize?

Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in method normalize of sage.modules.vector_integer_dense.Vector_integer_dense object at 0x4badd08>
Namespace:  Interactive
Definition: v.normalize(self)
Docstring:

Return this vector divided through by the first nonzero entry of
this vector.

EXAMPLES:

sage: v = vector(QQ,[0,4/3,5,1,2])
sage: v.normalize()
(0, 1, 15/4, 3/4, 3/2)


So there shouldn't be any confusion about what this method does. The operation that is performed by this method is a perfectly reasonable notion of "normalization". It depends on the context, of course. This definition of normalize is probably useful when working with projective spaces or doing Guass-Jordan elimination.

To compute the unit vector in the direction of v, you could do:

sage: v/v.norm()
(1/10*sqrt(10), 0, 3/10*sqrt(10))

more