Ask Your Question

Revision history [back]

Also, a possibe feature for teaching could be that you are not allowed to add things with different units (you can use the convert() function to test that):

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

Also, a possibe feature for teaching could be that you are not allowed to add things with different units (you can use the convert() function to test that):

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule - second
TypeError: Physics do not like such additions

This may also help avoiding bad cancellations.

Also, Perhaps, a possibe feature for teaching could be that you are not allowed to add things with different units (you can use the convert() function to test that):

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

This may also help avoiding bad cancellations.

Perhaps, a possibe feature for teaching could be that you are not allowed to add things with different incompatible units (you can use the convert() function to test that):

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

This may also help avoiding bad cancellations.

Perhaps, a Another possibe feature point of view could be that you are not allowed to add allow adding things with incompatible units (you can use the convert() function to test that):

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

This may also help avoiding bad cancellations.

Another possibe point of view could be not to allow adding things with incompatible units (you can use the convert() function to test that):

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

This may also help avoiding to avoid bad cancellations.

Another possibe point of view could be not to allow adding things with incompatible units (you can use the convert() function to test that):that). For this, you have to modify the .__add__() method in the class i defined above.

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

This may If you also help define .__pow__() method as of above (imitate the product), i guess you have a way to avoid bad cancellations.test wether an expression makes sense, and in which units wil be the result.

Another possibe point of view could be not to allow adding things with incompatible units (you can use the convert() function to test that). that: this function put your units in the international system standard). For this, you have to modify the .__add__() method in the class i defined above.

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

If you also define .__pow__() method as of above (imitate the product), i guess you have a way to test wether an expression makes sense, and in which units wil be the result.

Another Now, another possibe point of view (perhaps more pedagogical) could be not to allow adding things with incompatible units (you can use the convert() function to test that: this function put transforms your units in the international system standard). For this, you have to modify the .__add__() method in the class i defined above.

def __add__(self,other):
    if self.convert() == other.convert():
        return self
    else:
        raise TypeError('Physics do not like such additions')

sage: (joule + second*watt)*newton
joule*newton
sage: joule-joule
joule
sage: joule - second
TypeError: Physics do not like such additions

If you also define .__pow__() method as of above (imitate the product), i guess you have a way to test wether an expression makes sense, and in which units wil be the result.