def test_real_coeffs(self): i = complex(imaginary=1) z = complex() r = complex(real=1) rt2 = pow(2,0.5) rt3 = pow(3,0.5) c_x3_m_1 = cubic(1,0,0,-1) self.check_vector(c_x3_m_1.coeffs(),(1,0,0,-1)) self.check_vector(c_x3_m_1.find_real_roots(),[1]) c = cubic(1,0,0,1) self.check_vector(c.find_real_roots(),[-1]) c = cubic(1,3,3,1) self.check_vector(c.find_real_roots(),[-1, -1, -1]) c = cubic(1,-3,3,-1) self.check_vector(c.find_real_roots(),[1, 1, 1]) c = cubic(1,-6,11,-6) self.check_vector(c.find_real_roots(),[3,1,2]) c = cubic(1,0,0,1) self.check_vector(c.find_all_roots(),[-r, r/2-i*rt3/2, r/2+i*rt3/2]) c = cubic(1,-6,11,-6) self.check_vector(c.find_all_roots(),[r*3,r*1,r*2]) pass
def test_real_coeffs(self): i = complex(imaginary=1) z = complex() r = complex(real=1) rt2 = pow(2, 0.5) rt3 = pow(3, 0.5) c_x3_m_1 = cubic(1, 0, 0, -1) self.check_vector(c_x3_m_1.coeffs(), (1, 0, 0, -1)) self.check_vector(c_x3_m_1.find_real_roots(), [1]) c = cubic(1, 0, 0, 1) self.check_vector(c.find_real_roots(), [-1]) c = cubic(1, 3, 3, 1) self.check_vector(c.find_real_roots(), [-1, -1, -1]) c = cubic(1, -3, 3, -1) self.check_vector(c.find_real_roots(), [1, 1, 1]) c = cubic(1, -6, 11, -6) self.check_vector(c.find_real_roots(), [3, 1, 2]) c = cubic(1, 0, 0, 1) self.check_vector(c.find_all_roots(), [-r, r / 2 - i * rt3 / 2, r / 2 + i * rt3 / 2]) c = cubic(1, -6, 11, -6) self.check_vector(c.find_all_roots(), [r * 3, r * 1, r * 2]) pass
def test_complex_coeffs(self): i = complex(imaginary=1) z = complex() r = complex(real=1) rt2 = pow(2, 0.5) rt3 = pow(3, 0.5) for c in [ cubic(r, z, r, r), cubic(r, z, r, r), cubic(i * 16, r * 4 + i * 2, i * 3, r), cubic(1, 0, 1, 1), cubic(1, -6, 11, -6), ]: coeffs = c.coeffs() for r in c.find_all_roots(): f = r * r * r * coeffs[0] f += r * r * coeffs[1] f += r * coeffs[2] f += coeffs[3] #print f,coeffs,r self.check_vector([f], [z]) pass pass pass
def test_math2(self): e = complex(real=math.exp(1)) d=pow(e,complex(imaginary=math.pi)) self.check_scalar(d.real(),-1) self.check_scalar(d.imaginary(),0) d=pow(e,complex(imaginary=math.pi/2)) self.check_scalar(d.real(),0) self.check_scalar(d.imaginary(),1) d=pow(e,complex(real=1,imaginary=math.pi/2)) self.check_scalar(d.real(),0) self.check_scalar(d.imaginary(),math.exp(1)) pass
def test_real_coeffs(self): q_xsq_m_1 = quadratic(1,0,-1) q_xsq_p_1 = quadratic(1,0,1) self.check_vector(q_xsq_m_1.coeffs(),(1,0,-1)) r = q_xsq_m_1.find_real_roots() self.check_vector(r,(1,-1)) r = q_xsq_p_1.find_real_roots() self.check_vector(r,[]) q = quadratic(1,-2,1) self.check_vector(q.find_real_roots(),(1,1)) q = quadratic(1,2,1) self.check_vector(q.find_real_roots(),(-1,-1)) q = quadratic(4,4,1) self.check_vector(q.find_real_roots(),(-0.5,-0.5)) r = q_xsq_p_1.find_all_roots() self.check_vector(r,[complex(imaginary=1),complex(imaginary=-1)])
def test_bugs(self): a = complex(real=1) a.to_polar() b = -a self.check_scalar(b.cartesian()[0],-1) self.check_scalar(b.cartesian()[1],0) b = complex() self.check_scalar(b.cartesian()[0],0) self.check_scalar(b.cartesian()[1],0) c = b * complex(real=1) * complex(imaginary=1) self.check_scalar(c.cartesian()[0],0) self.check_scalar(c.cartesian()[1],0) c = pow(b,1) self.check_scalar(c.cartesian()[0],0) self.check_scalar(c.cartesian()[1],0) pass
def test_real_coeffs(self): q_xsq_m_1 = quadratic(1, 0, -1) q_xsq_p_1 = quadratic(1, 0, 1) self.check_vector(q_xsq_m_1.coeffs(), (1, 0, -1)) r = q_xsq_m_1.find_real_roots() self.check_vector(r, (1, -1)) r = q_xsq_p_1.find_real_roots() self.check_vector(r, []) q = quadratic(1, -2, 1) self.check_vector(q.find_real_roots(), (1, 1)) q = quadratic(1, 2, 1) self.check_vector(q.find_real_roots(), (-1, -1)) q = quadratic(4, 4, 1) self.check_vector(q.find_real_roots(), (-0.5, -0.5)) r = q_xsq_p_1.find_all_roots() self.check_vector(r, [complex(imaginary=1), complex(imaginary=-1)])
def test_make(self): for (r,i) in [(1,0),(0,1),(1,1),(2,3),(4,5),(3,-1),(0,0)]: c = complex(cartesian=(r,i)) self.check_scalar(c.cartesian()[0],r) self.check_scalar(c.cartesian()[1],i) self.check_scalar(c.polar()[0],math.sqrt(i*i+r*r)) if (r==0) and (i==0): self.assertFalse(bool(c), 'Zero is apparently nonzero') else: self.assertTrue(bool(c), 'Nonzero is apparently zero') d = c.copy() c += complex(cartesian=(1,1)) self.check_scalar(d.cartesian()[0],r) self.check_scalar(d.cartesian()[1],i) self.check_scalar(d.polar()[0],math.sqrt(i*i+r*r)) self.check_scalar(c.cartesian()[0],r+1) self.check_scalar(c.cartesian()[1],i+1) pass pass
def test_complex_coeffs(self): i = complex(imaginary=1) z = complex() r = complex(real=1) rt2 = pow(2,0.5) rt3 = pow(3,0.5) for c in [cubic(r,z,r,r), cubic(r,z,r,r), cubic(i*16,r*4+i*2,i*3,r), cubic(1,0,1,1), cubic(1,-6,11,-6), ]: coeffs = c.coeffs() for r in c.find_all_roots(): f = r*r*r*coeffs[0] f += r*r*coeffs[1] f += r*coeffs[2] f += coeffs[3] #print f,coeffs,r self.check_vector([f],[z]) pass pass pass
def test_math(self): for (r,i) in [(1,0),(0,1),(1,1),(2,3),(4,5),(3,-1)]: c = complex(cartesian=(r,i)) d = c.conjugate() self.check_scalar(c.cartesian()[0],r) self.check_scalar(c.cartesian()[1],i) self.check_scalar(d.cartesian()[0],r) self.check_scalar(d.cartesian()[1],-i) e = c*d m = c.modulus() self.check_scalar(e.cartesian()[0],m*m) self.check_scalar(e.cartesian()[1],0) self.check_scalar(abs(c),m) ci = c.reciprocal() f = ci*c self.check_scalar(f.cartesian()[0],1) self.check_scalar(f.cartesian()[1],0) f = c*c/c self.check_scalar(f.cartesian()[0],r) self.check_scalar(f.cartesian()[1],i) f = (c+c)/2 self.check_scalar(f.cartesian()[0],r) self.check_scalar(f.cartesian()[1],i) f = (c+c)/c self.check_scalar(f.cartesian()[0],2) self.check_scalar(f.cartesian()[1],0) f = (c-c) self.check_scalar(f.cartesian()[0],0) self.check_scalar(f.cartesian()[1],0) f = ((c*2)*c-(c*c))/c self.check_scalar(f.cartesian()[0],r) self.check_scalar(f.cartesian()[1],i) f = c.sqrt() g = f*f self.check_scalar(g.cartesian()[0],c.cartesian()[0]) self.check_scalar(g.cartesian()[1],c.cartesian()[1]) self.check_scalar(g.cartesian()[0],r) self.check_scalar(g.cartesian()[1],i) f = c.pow(0.25) g = f*f g = g*g self.check_scalar(g.cartesian()[0],c.cartesian()[0]) self.check_scalar(g.cartesian()[1],c.cartesian()[1]) self.check_scalar(g.cartesian()[0],r) self.check_scalar(g.cartesian()[1],i) pass pass
def test_complex_coeffs(self): i = complex(imaginary=1) z = complex() r = complex(real=1) rt2 = pow(2,0.5) q_xsq_m_i = quadratic(r,z,i) self.check_vector(q_xsq_m_i.coeffs(),(r,z,i)) self.check_vector(q_xsq_m_i.find_all_roots(),(complex(1/rt2,-1/rt2), complex(-1/rt2,1/rt2))) q_ixsq_m_1 = quadratic(i,z,r) self.check_vector(q_ixsq_m_1.coeffs(),(i,z,r)) self.check_vector(q_ixsq_m_1.find_all_roots(),(complex(1/rt2,1/rt2), complex(-1/rt2,-1/rt2))) q = quadratic(1,2,1) self.check_vector(q.find_all_roots(),(complex(-1),complex(-1))) q = quadratic(i,i*2,i) self.check_vector(q.find_all_roots(),(complex(-1),complex(-1))) q = quadratic(r,i*2,r) self.check_vector(q.find_all_roots(),(i*(rt2-1),-i*(rt2+1)))
def test_complex_coeffs(self): i = complex(imaginary=1) z = complex() r = complex(real=1) rt2 = pow(2, 0.5) q_xsq_m_i = quadratic(r, z, i) self.check_vector(q_xsq_m_i.coeffs(), (r, z, i)) self.check_vector( q_xsq_m_i.find_all_roots(), (complex(1 / rt2, -1 / rt2), complex(-1 / rt2, 1 / rt2))) q_ixsq_m_1 = quadratic(i, z, r) self.check_vector(q_ixsq_m_1.coeffs(), (i, z, r)) self.check_vector( q_ixsq_m_1.find_all_roots(), (complex(1 / rt2, 1 / rt2), complex(-1 / rt2, -1 / rt2))) q = quadratic(1, 2, 1) self.check_vector(q.find_all_roots(), (complex(-1), complex(-1))) q = quadratic(i, i * 2, i) self.check_vector(q.find_all_roots(), (complex(-1), complex(-1))) q = quadratic(r, i * 2, r) self.check_vector(q.find_all_roots(), (i * (rt2 - 1), -i * (rt2 + 1)))