def test_to_tuple(self): for i in xrange(50): t = rand_int_tuple(40, 14) m = PackedPowers(t, bits=4) tt = m.to_tuple() self.assert_(type(tt) == tuple) self.assert_(t == tt)
def test_call_all_zero(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) zero = (0.0,)*len(eg) if p.degree()==0: self.assertEquals(p(zero), 1.0) else: self.assertEquals(p(zero), 0.0)
def test_pow(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) for i in xrange(10): eg10 = tuple([i*j for j in eg]) try: self.assertEquals(p**i, PackedPowers(eg10)) except InsufficientBitsError: pass
def test_diff(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) for var in xrange(len(eg)): coeff, q = p.diff(var) self.assertEquals(coeff, eg[var]) msg = repr(eg)+' -> '+repr(q) for j in xrange(len(eg)): if j==var: if eg[j]==0: self.assertEquals(q[j], 0, msg) else: self.assertEquals(q[j], eg[j]-1, msg) else: self.assertEquals(q[j], eg[j], msg)
def test_call_wrong_len(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) for l in xrange(0, len(eg)*2): x = rand_int_tuple(l, 10) if not l==len(eg): self.assertRaises(IndexError, p.__call__, x)
def test_dict(self): t = (1, 2, 3) p = PackedPowers(t, bits=3) d = {} d[t] = -99.0 self.assert_(d.has_key(p)) self.assert_(d[p] == d[t])
def test_mul(self): lengths = 50 cases = 20 bits = 25 max_val = int((1<<bits)-1)/2 for length in xrange(lengths): for case in xrange(cases): pt = rand_int_tuple(length, max_val) p = PackedPowers(pt, bits) qt = rand_int_tuple(length, max_val) q = PackedPowers(qt, bits) rt = tuple([a+b for a,b in zip(pt, qt)]) r = PackedPowers(rt, bits) self.assertEquals(p*q, r) self.assertEquals(repr(p*q), 'PackedPowers(%s, bits=%d)'%(repr(rt), bits))
def test_call_negative_counts(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) deg = sum(eg) neg = sum([1 for i in eg if i%2])%2 eno = (-1.0,)*len(eg) r = p(eno) if deg==0: self.assert_(r==1.0) if neg: self.assert_(r==-1.0) else: self.assert_(r==1.0)
def test_call_any_zero(self): """any monomial (no coeff) evaluated at (1.0,)*len gives 1.0""" for eg, bits in self.examples: p = PackedPowers(eg, bits) for var in range(len(eg)): x = [float(i+1) for i in rand_int_tuple(len(eg), 3)] x[var] = 0.0 if p[var]==0: self.assert_(not p(x)==0.0) else: # test for NaN, doubles may/will overflow px = p(x) if px==px: self.assertEquals(px, 0.0)
def test_negative_index(self): for i in xrange(-10, 0): m = PackedPowers() self.assertRaises(IndexError, m.diff, i)
def test_call_all_one(self): """any monomial (no coeff) evaluated at (1.0,)*len gives 1.0""" for eg, bits in self.examples: p = PackedPowers(eg, bits) one = (1.0,)*len(eg) self.assertEquals(p(one), 1.0)
def test_equal_other_same(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(p, PackedPowers(eg, bits))
def test_number_of_variables(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(p.number_of_variables(), len(eg))
def test_not_eq(self): t = () p = PackedPowers(t, bits=4) self.assert_(not ((1,) == p))
def test_empty(self): t = () m = TuplePowers(t) p = PackedPowers(t, bits=4) self.assert_(t == p) self.assert_(m == p)
def test_negative_pow(self): for i in xrange(-10, 0): m = PackedPowers() self.assertRaises(ValueError, m.__pow__, i)
def test_repr(self): m = PackedPowers() self.assertEquals(repr(m), 'PackedPowers((1,), bits=4)')
def test_pow_one_is_same(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(p**1, p)
def test_pow_zero_is_one(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(p**0, PackedPowers((0,)*len(eg)))
def test_repr(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(repr(p), 'PackedPowers(%s, bits=%d)'%(repr(eg), bits))
def test_degree(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(p.degree(), sum(eg))
def test_bad_index(self): for i in xrange(1, 10): m = PackedPowers() self.assertRaises(IndexError, m.diff, i)
def test_diff_powers(self): m = PackedPowers() c, d = m.diff(0) self.assertEquals(repr(d), 'PackedPowers((0,), bits=4)')
def test_equal_self(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assertEquals(p, p)
def test_pows(self): p = PackedPowers() for i in xrange(10): self.assertEquals(p**i, PackedPowers((i,)))
def test_less_equal_self(self): for eg, bits in self.examples: p = PackedPowers(eg, bits) self.assert_(p<=p)
def test_empty(self): t = () p = PackedPowers(t, bits=4) self.assert_(t == p)
def test_diff_coeff(self): m = PackedPowers() c, d = m.diff(0) self.assertEquals(c, 1)
def test_example(self): t = (1, 2, 3) p = PackedPowers(t, bits=3) self.assert_(t == p)
def test_degree(self): m = PackedPowers() self.assertEquals(m.degree(), 1)