Question

I have written to the code below as an exercise and to do some vector algebra with an application of mine.

from math import acos

class Vector:

    def __init__(self,x,y,z):
        self.x = x; self.y = y; self.z = z

    def __str__(self):
        return str(self.x) + "i + " + str(self.y) + "j + " + str(self.z) + "k"

    def magnitude(self):
        return self.x**2 + self.y**2 + self.z**2 

    def plus(self,Vector):
        return Vector(self.x+Vector.x,self.y+Vector.y,self.z+Vector.z)

    def minus(self,Vector):
        return Vector(self.x-Vector.x,self.y-Vector.y,self.z-Vector.z)

    def dot(self,Vector):
        return self.x*Vector.x,self.y*Vector.y,self.z*Vector.z

    def angle(self,Vector):
        return acos(self.dot(Vector)/(self.magnitude*Vector.magnitude))

    def cross(self,Vector):
        return Vector(self.x*Vector.z-self.z*Vector.y,self.z*Vector.x-self.x*Vector.z,self.x*Vector.y-self.y*Vector.x)

When I want create two instances (v1 and v2) and use v1.cross(v2) I get the error below.

>>> v1.cross(v2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "vector_algebra.py", line 33, in cross
    return acos(self.dot(Vector)/(self.magnitude*Vector.magnitude))
AttributeError: Vector instance has no __call__ method

What am I doing wrong? How can I create a call method for this class?

Était-ce utile?

La solution

In your method signature:

def cross(self,Vector):  # and also the other methods

Vector argument is shading Vector class name. You should name your arguments differently, like vector in lower case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top