def test_abs(self): r0 = RationalNumber(-1, 2) r1 = RationalNumber(3, 6) r2 = RationalNumber(1, 2) self.assertEqual(abs(r0), r1) self.assertEqual(abs(r2), r1)
def test_pow(self): r0 = RationalNumber(1, 2) r1 = RationalNumber(0, 1) self.assertEqual(r0**3, RationalNumber(1, 8)) self.assertEqual(r1**4, 0) self.assertEqual(r0**0, 1) self.assertEqual(r0**(-2), 4)
def test__init__(self): m = Matrix([[3, 2], [RationalNumber(1, 2), 3]]) self.assertEqual(m[[0, 0]], RationalNumber(3)) self.assertEqual(m[[0, 1]], RationalNumber(2)) self.assertEqual(m[[1, 0]], RationalNumber(1, 2)) self.assertEqual(m[[1, 1]], RationalNumber(3)) self.assertEqual(str(m), '( 3, 2) \n( 1/2, 3) \n')
def test_neg(self): values = ( (RationalNumber(1, 2), RationalNumber(-1, 2)), (RationalNumber(0, 1), 0), (RationalNumber(-4, 8), RationalNumber(1, 2)) ) for v in values: r = -v[0] self.assertEqual(r, v[1])
def test_init(self): values = ( (1, 2, 1, 2), (1, 1, 1, 1), (1, -3, -1, 3), (RationalNumber(3, 1), RationalNumber(2, 9), 27, 2), (0.005, 5, 1, 1000), (0.00000005, 1, 0, 1) ) for v in values: r = RationalNumber(v[0], v[1]) self.assertEqual(r.num, v[2]) self.assertEqual(r.den, v[3])
def __init__(self, terms): # term['1'] is a coefficient of integer term. dict_ = {'1': 0} for key in terms.keys(): # We check this kwargs. if isinstance(terms[key], (int, RationalNumber, float)): terms[key] = RationalNumber(terms[key]) else: raise TypeError('you should input int after =, for example x_2=7') if terms[key] == 0: continue t_key = Polynomial.normalize_representation_of(key) # input coefficient into terms. try: dict_[t_key] = terms[key] + dict_[t_key] except KeyError: dict_[t_key] = terms[key] self.terms = {} # we change the order of terms for key in Polynomial.sort_order_of(list(dict_.keys())): self.terms[key] = dict_[key]
def __init__(self, args): if not isinstance(args, (list, tuple)): raise TypeError('args is not list') self.args = [] for i, row in enumerate(args): if not isinstance(row, (list, tuple)): raise TypeError('Row' + str(i) + 'is not list.') if len(row) != len(args[0]): raise TypeError('Row' + str(i) + 'is failed.') s_row = [] for comp in row: if isinstance(comp, type(args[0][0])): s_row.append(comp) elif isinstance(comp, (int, RationalNumber, float)): s_row.append(RationalNumber(comp)) else: raise TypeError('component in row ' + str(i) + 'is failed.') self.args.append(tuple(s_row)) self.args = tuple(self.args)
def __init__(self, a, b=0, c=0): if 'internal_access' in dir(EllipticCurve): if EllipticCurve.internal_access: EllipticCurve.internal_access = False else: raise TypeError('you are wrong') else: raise TypeError('you are wrong') a = RationalNumber(a) b = RationalNumber(b) c = RationalNumber(c) if -4 * (a**3) * (c**3) + (a**2) * (b**2) + 18 * a * b * c - 4 * ( b**3) - 27 * (c**2) == 0: raise TypeError('this poly has multiple root. ') else: super().__init__({'y^2': -1, 'x^3': 1, 'x^2': a, 'x^1': b, '1': c})
def __init__(self, x0=0, x1=None): ec = None if EllipticCurve.has_instance(): ec = EllipticCurve.get_instance() self.ec = ec else: raise TypeError('EC does not exist') if x0 is None or x1 is None: # pair = (0, None) is a point at infinity. # because the point at infinity in elliptic curve is x = 0. (x^3=0) x0 = 0 x1 = None else: x0 = RationalNumber(x0, 1) x1 = RationalNumber(x1, 1) if ec.includes(x0, x1) or x1 is None: self.pair = (x0, x1) else: raise TypeError('EC does not have this point')
def test__operator__(self): m1 = Matrix([[3, 2], [RationalNumber(1, 2), 3]]) m2 = Matrix([[1, 2], [RationalNumber(1, 5), 2]]) m3 = Matrix([[3, 2], [RationalNumber(1, 2), 3]]) self.assertEqual(m1+m2, Matrix([[4, 4], [RationalNumber(7, 10), 5]])) self.assertEqual(m2*m3, Matrix([ [4, 8], [RationalNumber(8, 5), RationalNumber(32, 5)] ])) self.assertEqual(m2*3, Matrix( [[3, 6], [RationalNumber(3, 5), 6]]))
def __mul__(self, other): if isinstance(other, Matrix): if self.clm_size != other.row_size: raise TypeError('we can not do multication') args = [] for i in range(self.row_size): args.append([]) for j in range(other.clm_size): args[i].append(0) for k in range(self.clm_size): args[i][j] += (self[i][k]) * (other[k][j]) return Matrix(args) elif isinstance(other, type(self.args[0][0])): return Matrix([[comp * other for comp in row] for row in self]) elif isinstance(other, (int, RationalNumber, float)): return Matrix([[comp * RationalNumber(other) for comp in row] for row in self]) else: return NotImplemented
def test_order(self): r0 = RationalNumber(1, 2) r1 = RationalNumber(3, 6) r2 = RationalNumber(3, 1) r3 = RationalNumber(-12, -4) self.assertEqual(r0, r1) self.assertEqual(r2, r3) self.assertEqual(r2, 3) self.assertTrue(r0.__ge__(r1)) self.assertTrue(r1.__gt__(0)) self.assertTrue(r3.__gt__(-0.1)) self.assertFalse(r1 < r0) self.assertFalse(r1.__rgt__(0)) self.assertFalse(r3.__rgt__(-0.1))
def test_add(self): values = ( (RationalNumber(1, 2), RationalNumber(1, 3), RationalNumber(5, 6)), (RationalNumber(1, 2), 1, RationalNumber(3, 2)), (1, RationalNumber(1, 3), RationalNumber(4, 3)), (RationalNumber(1, 2), 0.5, 1), (1.5, RationalNumber(1, 3), RationalNumber(11, 6)), (RationalNumber(1, 6), RationalNumber(5, 6), 1), (RationalNumber(1, 6), RationalNumber(-1, 6), 0) ) for v in values: r = v[0] + v[1] self.assertEqual(r, v[2])
def test_mul(self): values = ( (RationalNumber(1, 2), RationalNumber(1, 3), RationalNumber(1, 6)), (RationalNumber(1, 2), 2, 1), (-6, RationalNumber(1, 3), -2), (RationalNumber(1, 2), 0.5, RationalNumber(1, 4)), (-0.3, RationalNumber(1, 3), RationalNumber(1, -10)), (RationalNumber(1, 6), RationalNumber(-6, 1), -1), (RationalNumber(1, 6), RationalNumber(0, 1), 0) ) for v in values: r = v[0] * v[1] self.assertEqual(r, v[2])
def test_str_(self): self.assertEqual(str(RationalNumber(-2, 3)), '-2/3')
def test_is_integer(self): r0 = RationalNumber(1, 2) r1 = RationalNumber(3, 3) self.assertFalse(r0.is_integer()) self.assertTrue(r1.is_integer())
def test_div(self): values = ( (RationalNumber(1, 2), RationalNumber(1, 3), RationalNumber(3, 2)), (RationalNumber(1, 2), 2, RationalNumber(1, 4)), (6, RationalNumber(1, 3), 18), (RationalNumber(1, 2), 2.5, RationalNumber(1, 5)), (0.4, RationalNumber(1, 3), RationalNumber(12, 10)), (RationalNumber(1, 6), RationalNumber(-1, 3), RationalNumber(-1, 2)), (RationalNumber(0, 6), RationalNumber(1, 1), 0) ) for v in values: r = v[0] / v[1] self.assertEqual(r, v[2])
def test_init_error(self): with self.assertRaises(TypeError): RationalNumber(1, 0.000005) with self.assertRaises(TypeError): RationalNumber(1, 0)
def test_convertion(self): r0 = RationalNumber(3, 1) r1 = RationalNumber(1, 2) self.assertEqual(int(r0), 3) self.assertEqual(float(r1), 0.5)
def test(self): EllipticCurve.get_instance(0, -2) o = RationalPointInEC() p0 = RationalPointInEC(2, 2) p1 = RationalPointInEC(2, -2) p2 = RationalPointInEC(RationalNumber(9, 4), RationalNumber(-21, 8)) p3 = RationalPointInEC(RationalNumber(12769, 7056), RationalNumber(900271, 592704)) self.assertEqual(p0.pair, (2, 2)) self.assertEqual(p1.pair, (2, -2)) self.assertEqual((-p1).pair, (2, 2)) self.assertEqual(p2.pair, (RationalNumber(9, 4), RationalNumber(-21, 8))) self.assertEqual(p3.pair, (RationalNumber(12769, 7056), RationalNumber(900271, 592704))) self.assertEqual(2 * p0, p2) self.assertEqual(3 * p0 + p1, p2) self.assertEqual(4 * p0, p3) self.assertEqual(2 * p0 + 2 * p0, p3) self.assertEqual(p0 + p1, o) self.assertEqual(p0 - p1, p2) EllipticCurve.remove_instance() EllipticCurve.get_instance(0, -2) r0 = RationalPointInEC(0, 0) r1 = RationalPointInEC(-1, 1) r2 = RationalPointInEC(2, 2) r3 = RationalPointInEC(RationalNumber(-8, 9), RationalNumber(-28, 27)) r4 = RationalPointInEC(RationalNumber(-1, 169), RationalNumber(239, 2197)) self.assertEqual(r0.pair, (0, 0)) self.assertEqual(r1.pair, (-1, 1)) self.assertEqual(r2.pair, (2, 2)) self.assertEqual(r3.pair, (RationalNumber(-8, 9), RationalNumber(-28, 27))) self.assertEqual(r4.pair, (RationalNumber(-1, 169), RationalNumber(239, 2197))) self.assertEqual(r1 + r2, r3) self.assertEqual(r1 + 2 * r2, r4) with self.assertRaises(TypeError): RationalPointInEC(0, -1)