Learn Special Methods by Building a Vector Space - Step 74

Tell us what’s happening:

Hi,

I’m trying to create a cross-multiplication method for a 3D vector class in Python (step 74 in the Python Scientific Programming course). My two solutions are as follows:

  1. The first one (now commented out) is a dictionary comprehesion using nested for-loops to create the arguments used to instantiate a new R3 vector object.
  2. The second one REALLY SPELLS IT OUT.

The error message I get reads as follows: " The cross() method should return a new R3Vector instance resulting from the dot product computation."

I’ve successfully tested both my solutions by printing into console, and even checking the type of the cross multiplication product (which confirms it’s an object of class R3Vector) - unsure what else to do.

Your code so far

class R3Vector(R2Vector):
    def __init__(self, *, x, y, z):
        super().__init__(x=x, y=y)
        self.z = z
        
    def cross(self, other):
        if type(self) != type(other):
            return NotImplemented
        #kwargs = {i:(getattr(self, k)*getattr(other,j) - getattr(self, j)*getattr(other,k)) for i in vars(self) for j in vars(self) for k in vars(self) if all([i,j,k].count(n) == 1 for n in vars(self))}
        kwargs = {
            'x': getattr(self, 'y')*getattr(other,'z') - getattr(self, 'z')*getattr(other,'y'), 
            'y': getattr(self, 'x')*getattr(other,'z') - getattr(self, 'z')*getattr(other,'x'),
            'z': getattr(self, 'x')*getattr(other,'y') - getattr(self, 'y')*getattr(other,'x')
        }

        return self.__class__(**kwargs)
       
        
#v9 = R3Vector(x=2,y=3,z=4)
#v10 = R3Vector(x=3,y=4,z=2)
#print(type(v9.cross(v10)))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 74

Check the again the formula. You are almost multiplying the correct components.

Yeah, messed up my ‘y’ - thanks Dario!

2 Likes