Example #1
0
 def __truediv__(self, other):
     """Implements division between Variables.
         See __add__ for reference.
     """
     if isinstance(other, ReverseVariable):
         if not isinstance(other.val, float):
             raise ValueError("Vector cannot be the denominator!")
         if close(other.val, 0):
             raise ValueError("Divided by 0!")
         out_val = self.val / other.val
         res = ReverseVariable(out_val)
         self.children.append(res)
         other.children.append(res)
         res.left = self
         res.leftgrad = 1.0 / other.val
         res.right = other
         res.rightgrad = -self.val / (other.val ** 2)
         return res
     else:
         new_val = get_right_shape(other)
         if not isinstance(new_val, float):
             raise ValueError("Vector cannot be the denominator!")
         if close(new_val, 0):
             raise ValueError("Divided by 0!")
         out_val = self.val / new_val
         res = ReverseVariable(out_val)
         self.children.append(res)
         res.left = self
         res.leftgrad = 1.0 / new_val
         return res
Example #2
0
 def __eq__(self, other):
     #TODO-DOC about this
     if isinstance(other, Variable):
         if close(self.val, other.val) and close(self.grad, other.grad):
             return True
         else:
             return False
     else:
         new_val = get_right_shape(other)
         if close(self.val, new_val) and close(self.grad, get_right_shape(np.zeros(self.grad.shape))):
             return True
         else:
             return False
Example #3
0
 def get_val(self, x):
     if not isinstance(x, float):
         raise ValueError("Cannot be a vector!")
     tmp = (x - np.pi / 2) / np.pi
     if close(tmp, round(tmp)):
         raise ValueError("Value not in the domain!")
     return np.tan(x)
Example #4
0
 def __rtruediv__(self, other):
     """Implements division between other objects and Variables.
         See __add__ for reference.
     """
     new_val = get_right_shape(other)
     if not isinstance(self.val, float):
         raise ValueError("Vector cannot be the denominator!")
     if close(self.val, 0):
             raise ValueError("Divided by 0!")
     out_val = new_val / self.val
     res = ReverseVariable(out_val)
     self.children.append(res)
     res.left = self
     res.leftgrad = -new_val / (self.val ** 2)
     return res
Example #5
0
 def get_val(self, x):
     for t in x.flatten():
         tmp = (t - np.pi / 2) / np.pi
         if close(tmp, round(tmp)):
             raise ValueError("Value not in the domain!")
     return np.tan(x)