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

Example of python code, Worked example 1   Let's examine what  happens...

Worked example 1   Let's examine what  happens when  we compute the following Python code:   def p(x, y): z = x*x - y return z + 1/z   >>> p(1.0, 2.0) -2.0

Basics of python-introduction, Basics of python-introduction Python is...

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T

A method defination , De?nition   A method de?nition has the abstrac...

De?nition   A method de?nition has the abstract form: def ( , ..., ): ...   There are essentially three parts: is a name for

Programming embedded systems-programming models, Programming models Ju...

Programming models Just  as there  are several methods for organizing entire  software systems, there  are  different strategies for formally expressing computational processe

#title.display of vanderwaals isotherm, draw the variation of pressure with...

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

Bit Stuffing, How to write program on bit stuffing using python?

How to write program on bit stuffing using python?

Re-writing a C++ code in Python with the help of ctypes, I have a C++ code ...

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

List mutation and shared structure, List mutation and shared structure ...

List mutation and shared structure Lists are mutable data  structures, which defines  that  we can actually modifies  the values  stored in their components. We do this by usi

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