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

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

#1 - Pseudo Code, Ask question #Minimum 100 worIn this project, create the ...

Ask question #Minimum 100 worIn this project, create the pseudo code from the following request. The program must have some sort of menu that allows users to make selections to

While loop, You should use for whenever you can, because  it creates  the s...

You should use for whenever you can, because  it creates  the structure of your  loops clear. Sometimes, however, you require to do an operation various times, but you don't want t

Graphing data, Write a program that will take price data for stocks and pri...

Write a program that will take price data for stocks and print it graphically to the screen. Your program should begin by asking the user for the file name. It should then create a

Procedures as in First-class objects, Procedures as first-class objects ...

Procedures as first-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

Foundation of programming, hi i want to make a assignmnt of foundation of p...

hi i want to make a assignmnt of foundation of programming which include 4 task. I just want to know how much will be the price for that

Turtle graphics module in python programming language, Your assignment for ...

Your assignment for the semester will involve the development of a system for drawing trees using the Python programming language and the turtle graphics module (turtle.py). Comple

assignment 10, #question.Program 10 Assignment (Banking Objects – Savings/...

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

Euler method, python program of motion of a particle in viscus medium

python program of motion of a particle in viscus medium

Lost, Import the sample code below into the Python IDLE and enhance it, run...

Import the sample code below into the Python IDLE and enhance it, run it and debug it. Add features to make this a more realistic database tool by providing for easy data entry an

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