Normalize a vector, Python Programming

Assignment Help:

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]

 


Related Discussions:- Normalize a vector

corse information, write a program that creates s dictionary containing co...

write a program that creates s dictionary containing course

Stuck on homework, There are three seating categories at a stadium. For a s...

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

Conditionals- booleans, Booleans   Before we talk about  conditional...

Booleans   Before we talk about  conditionals, we require  to clarify the Boolean  data  type.  It has two values False and True. Typical statement that have Boolean values

Programming, We sell pdf''s from our site and pdfs can be ordered from the ...

We sell pdf''s from our site and pdfs can be ordered from the site by selecting a book, putting it in a shopping cart, then signing in with a password or as a guest, and then check

List comprehensions, List Comprehensions   Python has a very nice b...

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

Driven rlc circuit and damped tlc circuit, Write a program on python to gi...

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

Small Viruses, I need how to write small kind of viruses...

I need how to write small kind of viruses...

Help, When investing money, an important concept to know is compound intere...

When investing money, an important concept to know is compound interest. The equation FV = PV (1+rate)periods . This relates the following four quantities. The present value (

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd