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
python program of motion of a particle in viscus medium

What if you had a list of integer values, and you need to add  them  up and give the sum?  Here are a number of different types of doing  it. First,  here  is a type in a style

Python programs Every  general-purpose computer has a various detailed design, which  defines  that  the way  its program requires  to be specified is different. The "machine

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

I have a C++ code (10-15 line) which i need to re-write in python with the help of ctypes library. Is it possible i can get some help with it? Thanks & regards Tanmoy

Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and final

Write a program that asks the user to enter a number of seconds. The responses of the program will vary depending on the length of seconds: • If the number of seconds is under 60

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

#question.Program 10 Assignment (Banking Objects – Savings/Checking Accounts) Create a program named that performs the following: Create a class named ChkAcct, and a class n

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many