Learn Special Methods by Building a Vector Space - Step 74

Tell us what’s happening:

My question is not about the code or the exercise, but rather, about the exercise prompt.

In the prompt, we have stuff like:

"The cross product between two 3D vectors ( \mathbf{a} ) and ( \mathbf{b} ) can be computed as it follows:

[ \mathbf{a} \times \mathbf{b} = \begin{pmatrix} a_yb_z - a_zb_y \ a_zb_x - a_xb_z \ a_xb_y - a_yb_x \end{pmatrix} ]"

The formatting is super hard to read, which makes me imagine this should rather appear as a LaTeX formula or something else to denote mathematical expressions, but that is not rendering correctly. I’m not sure if the staff is aware of that or if I’m being redundant here, but it felt better to be sorry than safe here as I want to see the best version of FCC out there.

This rendering issue happens earlier in this lesson, but I brought it up here because this was the first time for me that I could not read the math properly. Anyhow, I managed to google cross product and advance through the lesson, so no feedback needed :slight_smile:

Best,

Your code so far

class R2Vector:
    def __init__(self, *, x, y):
        self.x = x
        self.y = y

    def norm(self):
        return sum(val**2 for val in vars(self).values())**0.5

    def __str__(self):
        return str(tuple(getattr(self, i) for i in vars(self)))

    def __repr__(self):
        arg_list = [f'{key}={val}' for key, val in vars(self).items()]
        args = ', '.join(arg_list)
        return f'{self.__class__.__name__}({args})'

    def __add__(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = {i: getattr(self, i) + getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

    def __sub__(self, other):
        if type(self) != type(other):
            return NotImplemented
        kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}
        return self.__class__(**kwargs)

    def __mul__(self, other):
        if type(other) in (int, float):
            kwargs = {i: getattr(self, i) * other for i in vars(self)}
            return self.__class__(**kwargs)        
        elif type(self) == type(other):
            args = [getattr(self, i) * getattr(other, i) for i in vars(self)]
            return sum(args)            
        return NotImplemented

    def __eq__(self, other):
        if type(self) != type(other):
            return NotImplemented
        return all(getattr(self, i) == getattr(other, i) for i in vars(self))
        
    def __ne__(self, other):
        return not self == other

    def __lt__(self, other):
        if type(self) != type(other):
            return NotImplemented
        return self.norm() < other.norm()

    def __gt__(self, other):
        if type(self) != type(other):
            return NotImplemented
        return self.norm() > other.norm()

    def __le__(self, other):
        return not self > other

    def __ge__(self, other):
        return not self < other

# User Editable Region

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 = {}

# User Editable Region

v1 = R2Vector(x=2, y=3)
v2 = R2Vector(x=0.5, y=1.25)
print(f'v1 = {v1}')
print(f'v2 = {v2}')
v3 = v1 + v2
print(f'v1 + v2 = {v3}')
v4 = v1 - v2
print(f'v1 - v2 = {v4}')
v5 = v1 * v2
print(f'v1 * v2 = {v5}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0

Challenge Information:

Learn Special Methods by Building a Vector Space - Step 74

1 Like

There is an issue with Mathjax at the minute. It will be fixed soon.

In the meantime you can use a Mathjax previewer.

1 Like

Awesome, good to know what’s the issue. I’ll use a Mathjax previewer for the following lessons. I appreciate the quick response