Normalize a vector, Python Programming

Normalize a vector

Let's imagine we want  to normalize a vector  of three  variables;  that  is to compute a new  vector  of three values,  such that its size  is 1. Here is our ?rst attempt; it is a procedure that takes as input a list of three values, and gives a list of three numbers:

def normalize3(v):


return [v[0]/math.sqrt(v[0]**2+v[1]**2+v[2]**2), v[1]/math.sqrt(v[0]**2+v[1]**2+v[2]**2), v[2]/math.sqrt(v[0]**2+v[1]**2+v[2]**2)]


This is correct, but it looks pretty complicated. Let's start by giving that we're recalculating the denominator three times, and instead store the variable value.


def normalize3(v):

magv = math.sqrt(v[0]**2+v[1]**2+v[2]**2)

return [v[0]/magv,v[1]/magv,v[2]/magv]


Now,  we can see a repeated structure, of going through and dividing each component by magv.  Also, we seem that  the  computation of the  magnitude of a vector  is a useful  and  understandable operation in its own  right,  and  could probably be give  in its own method. That tends  to this procedure:


def mag(v):

return math.sqrt(sum([vi**2 for vi in v]))


def normalize3(v):

return [vi/mag(v) for vi in v]


This is especially nice, because  now,  in fact, it gives not just to vectors  of size  three.  So, it's both smaller and  more general than  what  we started with.  But, one of our real causes has snuck back in: we're recomputing mag(v) once for each element of the vector.  So, At last, here's a version that we're very happy with:9


def mag(v):

return math.sqrt(sum([vi**2 for vi in v]))


def normalize3(v):

magv = mag(v)

return [vi/magv for vi in v]


Posted Date: 8/9/2012 7:23:36 AM | Location : United States

Related Discussions:- Normalize a vector, Assignment Help, Ask Question on Normalize a vector, Get Answer, Expert's Help, Normalize a vector Discussions

Write discussion on Normalize a vector
Your posts are moderated
Related Questions
Programs and Data Object-oriented programming is a popular way  of managing programs, which  groups together data  with  the procedures that  works on them,  thus  facilitatin

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

write a program that creates s dictionary containing course

Write a program on python to give solution for driven and damped rlc circuit

Perimeter of a polygon Now, let's consider the problem of computing the length  of the perimeter of a polygon. The input is a structure of vertices,  encoded as a list of list

List Comprehensions   Python has a very nice built-in  facility for doing  many  iterative methods, known as list comprehensions. The basic template is       [

Procedure calls When you compute an expression of the form ( ,  ..., )   the Python interpreter treats  this as a procedure call. It will be simpler to talk about

Expressions and assignment We can think of most computer programs as performing some sort of transformation on data.  Our code may get as input the exam scores of everyone in

inputWithinRange (prompt, lowValue, highValue) This function will ask for a number using the prompt parameter. The function will only return a value that is within the inclusive ra

I need server, client and trusted side communicating and charging a service with that protocol (or needham shroeder)