#NEWLINE# def unit_vector( data , axis = None , out = None ) : #NEWLINE# #INDENT# 'Return ndarray normalized by length, i.e. eucledian norm, along axis.\n\n    >>> v0 = numpy.random.random(3)\n    >>> v1 = unit_vector(v0)\n    >>> numpy.allclose(v1, v0 / numpy.linalg.norm(v0))\n    True\n    >>> v0 = numpy.random.rand(5, 4, 3)\n    >>> v1 = unit_vector(v0, axis=-1)\n    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=2)), 2)\n    >>> numpy.allclose(v1, v2)\n    True\n    >>> v1 = unit_vector(v0, axis=1)\n    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=1)), 1)\n    >>> numpy.allclose(v1, v2)\n    True\n    >>> v1 = numpy.empty((5, 4, 3), dtype=numpy.float64)\n    >>> unit_vector(v0, axis=1, out=v1)\n    >>> numpy.allclose(v1, v2)\n    True\n    >>> list(unit_vector([]))\n    []\n    >>> list(unit_vector([1.0]))\n    [1.0]\n\n    ' #NEWLINE# if ( out is None ) : #NEWLINE# #INDENT# data = numpy . array ( data , dtype = numpy . float64 , copy = True ) #NEWLINE# if ( data . ndim == 1 ) : #NEWLINE# #INDENT# data /= math . sqrt ( numpy . dot ( data , data ) ) #NEWLINE# return data #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( out is not data ) : #NEWLINE# #INDENT# out [ : ] = numpy . array ( data , copy = False ) #NEWLINE# #UNINDENT# data = out #NEWLINE# #UNINDENT# length = numpy . atleast_1d ( numpy . sum ( ( data * data ) , axis ) ) #NEWLINE# numpy . sqrt ( length , length ) #NEWLINE# if ( axis is not None ) : #NEWLINE# #INDENT# length = numpy . expand_dims ( length , axis ) #NEWLINE# #UNINDENT# data /= length #NEWLINE# if ( out is None ) : #NEWLINE# #INDENT# return data