Get the magnitude of a vector (x,y)
Question:
I’m struggling to get this to work because all the answers I can find that most answers are more focused on vectors in numpy arrays and not like mine in classes (OOP). (I hope I am phrasing all this the correct way, please excuse if I am not).
I want to find the magnitude of a vector (x,y), here is my code:
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
I have code that can sum and subtract the two vectors, but how to get the magnitude with this equation:
magnitude = math.sqrt(sum(v**2 for v in vector))
This is my code but it is not giving me what I need:
def vector_mag(self):
x_vector = self.x
y_vector = self.y
vector = (x_vector, y_vector)
magnitude = math.sqrt(sum(v**2 for v in vector))
return Vector(magnitude)
This gives an error that says:
line 22, in
print vector.vector_mag()
line 14, in vector_mag
return Vector(magnitude)
TypeError: init() takes exactly 3 arguments (2 given)
So when I add another "string" in the return part like this…:
return Vector("Vector magnitude is:", magnitude)
…the program runs but gives a strange result.
What am I doing wrong?
Here is my "full" code:
import math
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
def vector_mag(self):
x_vector = self.x
y_vector = self.y
vector = (x_vector, y_vector)
magnitude = math.sqrt(sum(v**2 for v in vector))
return Vector("Vector magnitude is: ", magnitude)
vector1 = Vector(10, 1)
vector2 = Vector(20, 2)
vector_list = [(vector1), (vector2)]
for vector in vector_list:
print vector.vector_mag()
Answers:
return Vector(magnitude)
The magnitude of a vector is a scalar value; in other words, it’s a single number. A vector is a collection of numbers, so it doesn’t make sense to try to convert a scalar to a vector. Just return the magnitude unchanged.
return magnitude
You can define vector_mag
like so:
def __abs__(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
Then calling your method is as easy as this:
vector1 = Vector(10, 1)
print 'Magnitude =', abs(vector1)
For accuracy reason, you should use math hypot to avoid any overflow.
from math import hypot
def __abs__(self):
return hypot(self.x, self.y)
math.hypot(x, y)
: Return the Euclidean norm, sqrt(x*x + y*y)
. This is the length of the vector from the origin to point (x, y).
This would give:
from math import hypot
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __abs__(self):
return hypot(self.x, self.y)
v=Vector(3,1)
magnitude = abs(v)
I’m struggling to get this to work because all the answers I can find that most answers are more focused on vectors in numpy arrays and not like mine in classes (OOP). (I hope I am phrasing all this the correct way, please excuse if I am not).
I want to find the magnitude of a vector (x,y), here is my code:
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
I have code that can sum and subtract the two vectors, but how to get the magnitude with this equation:
magnitude = math.sqrt(sum(v**2 for v in vector))
This is my code but it is not giving me what I need:
def vector_mag(self):
x_vector = self.x
y_vector = self.y
vector = (x_vector, y_vector)
magnitude = math.sqrt(sum(v**2 for v in vector))
return Vector(magnitude)
This gives an error that says:
line 22, in
print vector.vector_mag()
line 14, in vector_mag
return Vector(magnitude)
TypeError: init() takes exactly 3 arguments (2 given)
So when I add another "string" in the return part like this…:
return Vector("Vector magnitude is:", magnitude)
…the program runs but gives a strange result.
What am I doing wrong?
Here is my "full" code:
import math
class Vector(object):
def __init__(self, x, y): self.x = x self.y = y def vector_mag(self): x_vector = self.x y_vector = self.y vector = (x_vector, y_vector) magnitude = math.sqrt(sum(v**2 for v in vector)) return Vector("Vector magnitude is: ", magnitude)
vector1 = Vector(10, 1)
vector2 = Vector(20, 2)
vector_list = [(vector1), (vector2)]
for vector in vector_list:
print vector.vector_mag()
return Vector(magnitude)
The magnitude of a vector is a scalar value; in other words, it’s a single number. A vector is a collection of numbers, so it doesn’t make sense to try to convert a scalar to a vector. Just return the magnitude unchanged.
return magnitude
You can define vector_mag
like so:
def __abs__(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
Then calling your method is as easy as this:
vector1 = Vector(10, 1)
print 'Magnitude =', abs(vector1)
For accuracy reason, you should use math hypot to avoid any overflow.
from math import hypot
def __abs__(self):
return hypot(self.x, self.y)
math.hypot(x, y)
: Return the Euclidean norm,sqrt(x*x + y*y)
. This is the length of the vector from the origin to point (x, y).
This would give:
from math import hypot
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __abs__(self):
return hypot(self.x, self.y)
v=Vector(3,1)
magnitude = abs(v)