def add(self, other): """Return the sum of this instance and the input.""" res = self.js("add", [other]) pres = self.value + other.value if (res.value != pres): raise Exception("Addition failed: \n%d + %d != %d\n%s + %s != %s" \ % (self.value, other.value, res.value, h(self.value), h(other.value), h(res.value))) return res
def sub(self, other): """Return the difference of this instance and the input.""" res = self.js("sub", [other]) pres = self.value - other.value if (res.value != pres): raise Exception("Subtraction failed: \n%d - %d != %d\n%s - %s != %s" \ % (self.value, other.value, res.value, h(self.value), h(other.value), h(res.value))) return res
def square(self): """Return the square of this instance.""" res = self.js("square", []) pres = self.value * self.value if (res.value != pres): raise Exception("Squaring failed: \n%d^2 = %d != %d\n%s^2 = %s != %s" \ % (self.value, res.value, pres, h(self.value), h(res.value), h(pres))) return res
def neg(self): """Return the negative of this instance.""" res = self.js("neg", []) pres = -self.value if (res.value != pres): raise Exception("Negation failed: \n-%d != %d\n-%s != -%s" \ % (self.value, res.value, h(self.value), h(res.value))) return res
def mul(self, other): """Return the product of this instance and the input.""" res = self.js("mul", [other]) pres = self.value * other.value if (res.value != pres): raise Exception("Multiplication failed: \n%d * %d != %d\n%s * %s = %s != %s" \ % (self.value, other.value, res.value, h(self.value), h(other.value), h(res.value), h(pres))) return res
def modPow(self, other, secondOther): res = self.js("modPow", [other, secondOther]) pres = pow(self.value, other.value, secondOther.value); if (res.value != pres): raise Exception("Modular exponentiation failed: \n%s ^ %s mod %s = %s != %s\n%s ^ %s mod %s = %s != %s" \ % (self.value, other.value, secondOther.value, res.value, pres, h(self.value), h(other.value), h(secondOther.value), h(res.value), h(pres))) return res
def p(self, z): """ :param z: :return: """ result = 0 for s in xrange(-1, self.K + 1): result += (self.ds(s + 1) * (h(z - s * self.deltaL) - h(z - (s + 1) * self.deltaL))) return result
def mod(self, other): """Return the remainder of this instance modulo the input.""" res = self.js("mod", [other]) pres = self.value % other.value if (res.value != pres): raise Exception("Modulo failed: \n%s %% %s = %s != %s\n%s %% %s = %s != %s" \ % (self.value, other.value, res.value, pres, h(self.value), h(other.value), h(res.value), h(pres))) return res
def div(self, other): """Return the quotient of this instance and the input.""" res = self.js("div", [other]) pres = self.value / other.value if (res.value != pres): raise Exception("Division failed: \n%s / %s = %s != %s\n%s / %s = %s != %s" \ % (self.value, other.value, res.value, pres, h(self.value), h(other.value), h(res.value), h(pres))) return res
def js(self, op, pars): """Invocation of a method taking a number of integers as parameters. """ ls = "new verificatum.arithm.LargeInteger" # Convert Python integers to hexadecimal strings. hpars = [] for p in pars: hpars.append("%s(\\\"%s\\\")" % (ls, h(p.value))); # Compile expression. c = "(%s(\\\"%s\\\")).%s(%s).toHexString()" \ % (ls, h(self.value), op, ",".join(hpars)) # Evaluate and convert resulting hexadecimal string into a # Python integer. hexres = evjs(c) return LargeInteger(int(hexres, 16))
def e_endr(self, x): result = 0 for n in xrange(0, self.M + 1): result += self.b[n] * self.ey(self.K + 1, n, x) * (1 - h(x - self.d)) return result.real
def e_right(self, x): result = 0 for n in xrange(0, self.M + 1): result += (self.d1[n] * self.ey(1, n, x) + self.f1[n] * self.ey(1, n, x)) * (1 - h(x - self.ds(1))) return result.real