def __str__(self): if self._prefac == 1.0: return self._string if self.is_zero(): return "0.0" if self._prefac == -1.0: # string can potentially be a sum, insert parantheses return "-(" + self._string + ")" return c_string_from_num(self._prefac) + " * (" + self._string + ")"
def __add__(self, other): if isinstance(other, c_variable): # avoid explicit additions of zeros if other.is_zero(): return c_variable(self._string, self._prefac) if self.is_zero(): return c_variable(other._string, other._prefac) # make use of associativity if both have same prefac if (self._prefac == other._prefac): string = self._string + " + " + other._string return c_variable(string, self._prefac) # make use of associativity if both have same prefac if (self._prefac == -other._prefac): string = self._string + " - (" + other._string + ")" return c_variable(string, self._prefac) string = self.__str__() + " + " + other.__str__() return c_variable(string, 1.0) else: if other == 0: return c_variable(self._string, self._prefac) string = self.__str__() + " + " + c_string_from_num(other) return c_variable(string, 1.0)
def __add__(self, other): if isinstance(other, c_variable): # avoid explicit additions of zeros if other.is_zero(): return c_variable(self._string, self._prefac) if self.is_zero(): return c_variable(other._string, other._prefac) # make use of associativity if both have same prefac if self._prefac == other._prefac: string = self._string + " + " + other._string return c_variable(string, self._prefac) # make use of associativity if both have same prefac if self._prefac == -other._prefac: string = self._string + " - (" + other._string + ")" return c_variable(string, self._prefac) string = self.__str__() + " + " + other.__str__() return c_variable(string, 1.0) else: if other == 0: return c_variable(self._string, self._prefac) string = self.__str__() + " + " + c_string_from_num(other) return c_variable(string, 1.0)