def test_Real(): # NOTE prec is the whole number of decimal digits assert str(Real('1.23', prec=1+2)) == '1.23' assert str(Real('1.23456789', prec=1+8)) == '1.23456789' assert str(Real('1.234567890123456789', prec=1+18)) == '1.234567890123456789' assert str(pi.evalf(1+2)) == '3.14' assert str(pi.evalf(1+14)) == '3.14159265358979' assert str(pi.evalf(1+64)) == '3.1415926535897932384626433832795028841971693993751058209749445923'
def test_Float(): # NOTE prec is the whole number of decimal digits assert str(Float("1.23", prec=1 + 2)) == "1.23" assert str(Float("1.23456789", prec=1 + 8)) == "1.23456789" assert str(Float("1.234567890123456789", prec=1 + 18)) == "1.234567890123456789" assert str(pi.evalf(1 + 2)) == "3.14" assert str(pi.evalf(1 + 14)) == "3.14159265358979" assert str(pi.evalf(1 + 64)) == "3.1415926535897932384626433832795028841971693993751058209749445923"
def test_Float(): # NOTE prec is the whole number of decimal digits assert str(Float("1.23", prec=1 + 2)) == "1.23" assert str(Float("1.23456789", prec=1 + 8)) == "1.23456789" assert str(Float("1.234567890123456789", prec=1 + 18)) == "1.234567890123456789" assert str(pi.evalf(1 + 2)) == "3.14" assert str(pi.evalf(1 + 14)) == "3.14159265358979" assert str(pi.evalf(1 + 64)) == ("3.141592653589793238462643383279" "5028841971693993751058209749445923") assert str(pi.round(-1)) == "0." assert str((pi ** 400 - (pi ** 400).round(1)).n(2)) == "-0.e+88"
def test_Float(): # NOTE prec is the whole number of decimal digits assert str(Float('1.23', prec=1+2)) == '1.23' assert str(Float('1.23456789', prec=1+8)) == '1.23456789' assert str(Float('1.234567890123456789', prec=1+18)) == '1.234567890123456789' assert str(pi.evalf(1+2)) == '3.14' assert str(pi.evalf(1+14)) == '3.14159265358979' assert str(pi.evalf(1+64)) == '3.1415926535897932384626433832795028841971693993751058209749445923' assert str(pi.round(-1)) == '0.' assert str((pi**400 - (pi**400).round(1)).n(2)) == '-0.e+88'
def test_Float(): # NOTE prec is the whole number of decimal digits assert str(Float('1.23', prec=1 + 2)) == '1.23' assert str(Float('1.23456789', prec=1 + 8)) == '1.23456789' assert str(Float('1.234567890123456789', prec=1 + 18)) == '1.234567890123456789' assert str(pi.evalf(1 + 2)) == '3.14' assert str(pi.evalf(1 + 14)) == '3.14159265358979' assert str(pi.evalf(1 + 64)) == ('3.141592653589793238462643383279' '5028841971693993751058209749445923') assert str(pi.round(-1)) == '0.' assert str((pi**400 - (pi**400).round(1)).n(2)) == '-0.e+88'
def test_fcode_NumberSymbol(): assert fcode(Catalan) == ' parameter (Catalan = 0.915965594177219)\n Catalan' assert fcode(EulerGamma) == ' parameter (EulerGamma = 0.577215664901533)\n EulerGamma' assert fcode(E) == ' parameter (E = 2.71828182845905)\n E' assert fcode(GoldenRatio) == ' parameter (GoldenRatio = 1.61803398874989)\n GoldenRatio' assert fcode(pi) == ' parameter (pi = 3.14159265358979)\n pi' assert fcode(pi,precision=5) == ' parameter (pi = 3.1416)\n pi' assert fcode(Catalan,human=False) == ([('Catalan', Catalan.evalf(15))], set([]), ' Catalan') assert fcode(EulerGamma,human=False) == ([('EulerGamma', EulerGamma.evalf(15))], set([]), ' EulerGamma') assert fcode(E,human=False) == ([('E', E.evalf(15))], set([]), ' E') assert fcode(GoldenRatio,human=False) == ([('GoldenRatio', GoldenRatio.evalf(15))], set([]), ' GoldenRatio') assert fcode(pi,human=False) == ([('pi', pi.evalf(15))], set([]), ' pi') assert fcode(pi,precision=5,human=False) == ([('pi', pi.evalf(5))], set([]), ' pi')
def test_fcode_NumberSymbol(): prec = 17 p = FCodePrinter() assert fcode( Catalan ) == " parameter (Catalan = %sd0)\n Catalan" % Catalan.evalf( prec) assert fcode( EulerGamma ) == " parameter (EulerGamma = %sd0)\n EulerGamma" % EulerGamma.evalf( prec) assert fcode(E) == " parameter (E = %sd0)\n E" % E.evalf(prec) assert fcode( GoldenRatio ) == " parameter (GoldenRatio = %sd0)\n GoldenRatio" % GoldenRatio.evalf( prec) assert fcode( pi) == " parameter (pi = %sd0)\n pi" % pi.evalf(prec) assert fcode( pi, precision=5) == " parameter (pi = %sd0)\n pi" % pi.evalf(5) assert fcode(Catalan, human=False) == ( set([(Catalan, p._print(Catalan.evalf(prec)))]), set([]), " Catalan", ) assert fcode(EulerGamma, human=False) == ( set([(EulerGamma, p._print(EulerGamma.evalf(prec)))]), set([]), " EulerGamma", ) assert fcode(E, human=False) == ( set([(E, p._print(E.evalf(prec)))]), set([]), " E", ) assert fcode(GoldenRatio, human=False) == ( set([(GoldenRatio, p._print(GoldenRatio.evalf(prec)))]), set([]), " GoldenRatio", ) assert fcode(pi, human=False) == ( set([(pi, p._print(pi.evalf(prec)))]), set([]), " pi", ) assert fcode(pi, precision=5, human=False) == ( set([(pi, p._print(pi.evalf(5)))]), set([]), " pi", )
def test_fcode_NumberSymbol(): p = FCodePrinter() assert fcode(Catalan) == ' parameter (Catalan = 0.915965594177219d0)\n Catalan' assert fcode(EulerGamma) == ' parameter (EulerGamma = 0.577215664901533d0)\n EulerGamma' assert fcode(E) == ' parameter (E = 2.71828182845905d0)\n E' assert fcode(GoldenRatio) == ' parameter (GoldenRatio = 1.61803398874989d0)\n GoldenRatio' assert fcode(pi) == ' parameter (pi = 3.14159265358979d0)\n pi' assert fcode(pi,precision=5) == ' parameter (pi = 3.1416d0)\n pi' assert fcode(Catalan,human=False) == (set([(Catalan, p._print(Catalan.evalf(15)))]), set([]), ' Catalan') assert fcode(EulerGamma,human=False) == (set([(EulerGamma, p._print(EulerGamma.evalf(15)))]), set([]), ' EulerGamma') assert fcode(E,human=False) == (set([(E, p._print(E.evalf(15)))]), set([]), ' E') assert fcode(GoldenRatio,human=False) == (set([(GoldenRatio, p._print(GoldenRatio.evalf(15)))]), set([]), ' GoldenRatio') assert fcode(pi,human=False) == (set([(pi, p._print(pi.evalf(15)))]), set([]), ' pi') assert fcode(pi,precision=5,human=False) == (set([(pi, p._print(pi.evalf(5)))]), set([]), ' pi')
def test_Float(): # NOTE dps is the whole number of decimal digits assert str(Float('1.23', dps=1 + 2)) == '1.23' assert str(Float('1.23456789', dps=1 + 8)) == '1.23456789' assert str( Float('1.234567890123456789', dps=1 + 18)) == '1.234567890123456789' assert str(pi.evalf(1 + 2)) == '3.14' assert str(pi.evalf(1 + 14)) == '3.14159265358979' assert str(pi.evalf(1 + 64)) == ('3.141592653589793238462643383279' '5028841971693993751058209749445923') assert str(pi.round(-1)) == '0.' assert str((pi**400 - (pi**400).round(1)).n(2)) == '-0.e+88' assert str(Float(S.Infinity)) == 'inf' assert str(Float(S.NegativeInfinity)) == '-inf'
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' #1659 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' #1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * ((1 - x))** 1000).n()) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y' #3561. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * sin(oo)).n() == S.Zero assert (0 / sin(oo)).n() == S.Zero assert (0 * E**(oo)).n() == S.NaN assert (0 / E**(oo)).n() == S.Zero assert (0 + sin(oo)).n() == S.NaN assert (0 - sin(oo)).n() == S.NaN assert (0 + E**(oo)).n() == S.Infinity assert (0 - E**(oo)).n() == S.NegativeInfinity assert (5 * sin(oo)).n() == S.NaN assert (5 / sin(oo)).n() == S.NaN assert (5 * E**(oo)).n() == S.Infinity assert (5 / E**(oo)).n() == S.Zero assert (5 + sin(oo)).n() == S.NaN assert (5 - sin(oo)).n() == S.NaN assert (5 + E**(oo)).n() == S.Infinity assert (5 - E**(oo)).n() == S.NegativeInfinity
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' #1659 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' #1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * ((1 - x))** 1000).n()) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y'
def test_Float(): # NOTE dps is the whole number of decimal digits assert str(Float('1.23', dps=1 + 2)) == '1.23' assert str(Float('1.23456789', dps=1 + 8)) == '1.23456789' assert str( Float('1.234567890123456789', dps=1 + 18)) == '1.234567890123456789' assert str(pi.evalf(1 + 2)) == '3.14' assert str(pi.evalf(1 + 14)) == '3.14159265358979' assert str(pi.evalf(1 + 64)) == ('3.141592653589793238462643383279' '5028841971693993751058209749445923') assert str(pi.round(-1)) == '0.0' assert str((pi**400 - (pi**400).round(1)).n(2)) == '-0.e+88' assert sstr(Float("100"), full_prec=False, min=-2, max=2) == '1.0e+2' assert sstr(Float("100"), full_prec=False, min=-2, max=3) == '100.0' assert sstr(Float("0.1"), full_prec=False, min=-2, max=3) == '0.1' assert sstr(Float("0.099"), min=-2, max=3) == '9.90000000000000e-2'
def test_nsolve(): # onedimensional from sympy import Symbol, sin, pi x = Symbol('x') assert nsolve(sin(x), 2) - pi.evalf() < 1e-16 assert nsolve(Eq(2*x, 2), x, -10) == nsolve(2*x - 2, -10) # multidimensional x1 = Symbol('x1') x2 = Symbol('x2') f1 = 3 * x1**2 - 2 * x2**2 - 1 f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8 f = Matrix((f1, f2)).T F = lambdify((x1, x2), f.T, modules='mpmath') for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]: x = nsolve(f, (x1, x2), x0, tol=1.e-8) assert mnorm(F(*x),1) <= 1.e-10 # The Chinese mathematician Zhu Shijie was the very first to solve this # nonlinear system 700 years ago (z was added to make it 3-dimensional) x = Symbol('x') y = Symbol('y') z = Symbol('z') f1 = -x + 2*y f2 = (x**2 + x*(y**2 - 2) - 4*y) / (x + 4) f3 = sqrt(x**2 + y**2)*z f = Matrix((f1, f2, f3)).T F = lambdify((x, y, z), f.T, modules='mpmath') def getroot(x0): root = nsolve((f1, f2, f3), (x, y, z), x0) assert mnorm(F(*root),1) <= 1.e-8 return root assert map(round, getroot((1, 1, 1))) == [2.0, 1.0, 0.0]
def test_nsolve(): # onedimensional from sympy import Symbol, sin, pi x = Symbol('x') assert nsolve(sin(x), 2) - pi.evalf() < 1e-16 assert nsolve(Eq(2 * x, 2), x, -10) == nsolve(2 * x - 2, -10) # multidimensional x1 = Symbol('x1') x2 = Symbol('x2') f1 = 3 * x1**2 - 2 * x2**2 - 1 f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8 f = Matrix((f1, f2)).T F = lambdify((x1, x2), f.T, modules='mpmath') for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]: x = nsolve(f, (x1, x2), x0, tol=1.e-8) assert mnorm(F(*x), 1) <= 1.e-10 # The Chinese mathematician Zhu Shijie was the very first to solve this # nonlinear system 700 years ago (z was added to make it 3-dimensional) x = Symbol('x') y = Symbol('y') z = Symbol('z') f1 = -x + 2 * y f2 = (x**2 + x * (y**2 - 2) - 4 * y) / (x + 4) f3 = sqrt(x**2 + y**2) * z f = Matrix((f1, f2, f3)).T F = lambdify((x, y, z), f.T, modules='mpmath') def getroot(x0): root = nsolve((f1, f2, f3), (x, y, z), x0) assert mnorm(F(*root), 1) <= 1.e-8 return root assert map(round, getroot((1, 1, 1))) == [2.0, 1.0, 0.0]
def test_evalf_bugs(): assert NS(sin(1)+exp(-10**10),10) == NS(sin(1),10) assert NS(exp(10**10)+sin(1),10) == NS(exp(10**10),10) assert NS('log(1+1/10**50)',20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)',10) == '100.0000000' assert NS('log(2)',10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x:'1/10**50'}) == '-0.166666666666667' assert NS(sin(1)+Rational(1,10**100)*I,15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1+I)**2*I, 6) == '-2.00000' d={n: (-1)**Rational(6,7), y: (-1)**Rational(4,7), x: (-1)**Rational(2,7)} assert NS((x*(1+y*(1 + n))).subs(d).evalf(),6) == '0.346011 + 0.433884*I' assert NS(((-I-sqrt(2)*I)**2).evalf()) == '-5.82842712474619' assert NS((1+I)**2*I,15) == '-2.00000000000000' #1659 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' #1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n, subs={n:.01}) == '19.8100000000000' assert NS(((x - 1)*((1 - x))**1000).n()) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2*x).n()) == '-2.00000000000000*x' assert NS((-2*x*y).n()) == '-2.00000000000000*x*y'
def test_Float(): # NOTE dps is the whole number of decimal digits assert str(Float("1.23", dps=1 + 2)) == "1.23" assert str(Float("1.23456789", dps=1 + 8)) == "1.23456789" assert str(Float("1.234567890123456789", dps=1 + 18)) == "1.234567890123456789" assert str(pi.evalf(1 + 2)) == "3.14" assert str(pi.evalf(1 + 14)) == "3.14159265358979" assert str(pi.evalf(1 + 64)) == ("3.141592653589793238462643383279" "5028841971693993751058209749445923") assert str(pi.round(-1)) == "0.0" assert str((pi**400 - (pi**400).round(1)).n(2)) == "-0.e+88" assert sstr(Float("100"), full_prec=False, min=-2, max=2) == "1.0e+2" assert sstr(Float("100"), full_prec=False, min=-2, max=3) == "100.0" assert sstr(Float("0.1"), full_prec=False, min=-2, max=3) == "0.1" assert sstr(Float("0.099"), min=-2, max=3) == "9.90000000000000e-2"
def circuit_from_qasm(qasm): """Creates a QCircuit from a QASM circuit, uses Qiskit transpiler to unroll the QASM circuit into basic U gates. Args: qasm (str): a QASM circuit Returns: QCircuit: the QCircuit equivalent of the provided QASM circuit """ pm = PassManager() pm.append(Unroller(['u3', 'cx'])) qasm = transpile(QuantumCircuit.from_qasm_str(qasm), pass_manager=pm).qasm() q_circuit = QCircuit() lines = list(qasm.split(';\n')) for line in lines: if line.startswith('qreg'): q_circuit.add_q_register(line.split(' ')[-1].split('[')[0], int(line.split('[')[1][:-1])) elif line.startswith('creg'): q_circuit.add_c_register(line.split(' ')[-1].split('[')[0], int(line.split('[')[1][:-1])) else: if line.startswith('u3'): q_reg = _q_reg_1q_qasm_gate(line) q_arg = (q_circuit.q_regs[q_reg[0]][0], q_reg[1]) params = _qasm_gate_params(line) # print(line) if params[0] == pi.evalf(5)/2 and params[1] == 0 and params[2] == pi.evalf(5): # print('H') q_circuit.h(q_arg) else: q_circuit.dummy_gate(name='u3', q_args=[q_arg], params=params) elif line.startswith('cx'): q_regs = _q_regs_2q_qasm_gate(line) q_circuit.cx((q_circuit.q_regs[q_regs[0][0]][0], q_regs[0][1]), (q_circuit.q_regs[q_regs[1][0]][0], q_regs[1][1])) elif line.startswith('measure'): q_reg = line.split(' ')[1] c_reg = line.split(' ')[3] q_circuit.measure((q_circuit.q_regs[q_reg.split('[')[0]][0], int(q_reg.split('[')[-1][:-1])), (q_circuit.c_regs[c_reg.split('[')[0]][0], int(c_reg.split('[')[-1][:-1]))) elif line.startswith('barrier'): q_args = list() for q_arg in line.split(' ')[-1].split(','): q_args.append((q_circuit.q_regs[q_arg.split('[')[0]][0], int(q_arg.split('[')[-1][:-1]))) q_circuit.barrier(*q_args) return q_circuit
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS( '(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational( 1, 10**100)*I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2*I, 6) == '-2.00000' d = {n: ( -1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7)} assert NS((x*(1 + y*(1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2)*I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2*I, 15) == '-2.00000000000000' # issue 4758 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' # issue 4758 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n, subs={n: .01}) == '19.8100000000000' assert NS(((x - 1)*((1 - x))**1000).n() ) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2*x).n()) == '-2.00000000000000*x' assert NS((-2*x*y).n()) == '-2.00000000000000*x*y' assert cos(x).n(subs={x: 1+I}) == cos(x).subs(x, 1+I).n() # issue 6660. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0*sin(oo)).n() == S.Zero assert (0/sin(oo)).n() == S.Zero assert (0*E**(oo)).n() == S.NaN assert (0/E**(oo)).n() == S.Zero assert (0+sin(oo)).n() == S.NaN assert (0-sin(oo)).n() == S.NaN assert (0+E**(oo)).n() == S.Infinity assert (0-E**(oo)).n() == S.NegativeInfinity assert (5*sin(oo)).n() == S.NaN assert (5/sin(oo)).n() == S.NaN assert (5*E**(oo)).n() == S.Infinity assert (5/E**(oo)).n() == S.Zero assert (5+sin(oo)).n() == S.NaN assert (5-sin(oo)).n() == S.NaN assert (5+E**(oo)).n() == S.Infinity assert (5-E**(oo)).n() == S.NegativeInfinity #issue 7416 assert as_mpmath(0.0, 10, {'chop': True}) == 0
def test_angle_between(): a = Point(1, 2, 3, 4) b = a.orthogonal_direction o = a.origin assert feq(Line.angle_between(Line(Point(0, 0), Point(1, 1)), Line(Point(0, 0), Point(5, 0))).evalf(), pi.evalf() / 4) assert Line(a, o).angle_between(Line(b, o)) == pi / 2 assert Line3D.angle_between(Line3D(Point3D(0, 0, 0), Point3D(1, 1, 1)), Line3D(Point3D(0, 0, 0), Point3D(5, 0, 0))), acos(sqrt(3) / 3)
def test_issue1512(): assert abs(pi._evalf(50) - 3.14159265358979) < 1e-10 assert abs(E._evalf(50) - 2.71828182845905) < 1e-10 assert abs(Catalan._evalf(50) - 0.915965594177219) < 1e-10 assert abs(EulerGamma._evalf(50) - 0.577215664901533) < 1e-10 assert abs(GoldenRatio._evalf(50) - 1.61803398874989) < 1e-10 x = Symbol("x") assert (pi+x).evalf() == pi.evalf()+x assert (E+x).evalf() == E.evalf()+x assert (Catalan+x).evalf() == Catalan.evalf()+x assert (EulerGamma+x).evalf() == EulerGamma.evalf()+x assert (GoldenRatio+x).evalf() == GoldenRatio.evalf()+x
def test_issue_4611(): assert abs(pi._evalf(50) - 3.14159265358979) < 1e-10 assert abs(E._evalf(50) - 2.71828182845905) < 1e-10 assert abs(Catalan._evalf(50) - 0.915965594177219) < 1e-10 assert abs(EulerGamma._evalf(50) - 0.577215664901533) < 1e-10 assert abs(GoldenRatio._evalf(50) - 1.61803398874989) < 1e-10 x = Symbol("x") assert (pi + x).evalf() == pi.evalf() + x assert (E + x).evalf() == E.evalf() + x assert (Catalan + x).evalf() == Catalan.evalf() + x assert (EulerGamma + x).evalf() == EulerGamma.evalf() + x assert (GoldenRatio + x).evalf() == GoldenRatio.evalf() + x
def multiply_vector(expr, n0, horner=False): """ Multiply the vector n0 into the expression expr It is recommended to make the vector noncommutative (Symbol("n0", commutative=False). This makes assumptions that the form of expr will be one of the ones generated by this file. To apply the horner scheme to the numerator and denominator, use horner=True. """ # Make sure we are using a version of SymPy that has # https://github.com/sympy/sympy/pull/12088. x, y = symbols('x y') if Poly(pi.evalf(100)*x*y, x).as_expr() != pi.evalf(100)*x*y: raise RuntimeError("multiply_vector requires https://github.com/sympy/sympy/pull/12088") from sympy import horner as _horner if horner: # horner doesn't work on noncommutatives n1 = Symbol(n0.name) num, den = fraction(expr) return _horner(num*n1).subs(n1, n0)/_horner(den) # TODO: Don't distribute across complex numbers if expr.is_Add: if expr.is_number: return expr*n0 # expand_mul(deep=False) does too much (it breaks horner) return Add(*[multiply_vector(i, n0) for i in expr.args]) coeff, rest = expr.as_coeff_Mul() if isinstance(rest, customre): return coeff*customre(multiply_vector(rest.args[0], n0)) num, den = fraction(expr) if expr != num: return multiply_vector(num, n0)/den return expr*n0
def test_fcode_NumberSymbol(): prec = 17 p = FCodePrinter() assert fcode(Catalan) == ' parameter (Catalan = %sd0)\n Catalan' % Catalan.evalf(prec) assert fcode(EulerGamma) == ' parameter (EulerGamma = %sd0)\n EulerGamma' % EulerGamma.evalf(prec) assert fcode(E) == ' parameter (E = %sd0)\n E' % E.evalf(prec) assert fcode(GoldenRatio) == ' parameter (GoldenRatio = %sd0)\n GoldenRatio' % GoldenRatio.evalf(prec) assert fcode(pi) == ' parameter (pi = %sd0)\n pi' % pi.evalf(prec) assert fcode( pi, precision=5) == ' parameter (pi = %sd0)\n pi' % pi.evalf(5) assert fcode(Catalan, human=False) == (set( [(Catalan, p._print(Catalan.evalf(prec)))]), set([]), ' Catalan') assert fcode(EulerGamma, human=False) == (set([(EulerGamma, p._print( EulerGamma.evalf(prec)))]), set([]), ' EulerGamma') assert fcode(E, human=False) == ( set([(E, p._print(E.evalf(prec)))]), set([]), ' E') assert fcode(GoldenRatio, human=False) == (set([(GoldenRatio, p._print( GoldenRatio.evalf(prec)))]), set([]), ' GoldenRatio') assert fcode(pi, human=False) == ( set([(pi, p._print(pi.evalf(prec)))]), set([]), ' pi') assert fcode(pi, precision=5, human=False) == ( set([(pi, p._print(pi.evalf(5)))]), set([]), ' pi')
def test_evalf_bugs(): assert NS(sin(1) + exp(-10 ** 10), 10) == NS(sin(1), 10) assert NS(exp(10 ** 10) + sin(1), 10) == NS(exp(10 ** 10), 10) assert NS("log(1+1/10**50)", 20) == "1.0000000000000000000e-50" assert NS("log(10**100,10)", 10) == "100.0000000" assert NS("log(2)", 10) == "0.6931471806" assert NS("(sin(x)-x)/x**3", 15, subs={x: "1/10**50"}) == "-0.166666666666667" assert NS(sin(1) + Rational(1, 10 ** 100) * I, 15) == "0.841470984807897 + 1.00000000000000e-100*I" assert x.evalf() == x assert NS((1 + I) ** 2 * I, 6) == "-2.00000" d = {n: (-1) ** Rational(6, 7), y: (-1) ** Rational(4, 7), x: (-1) ** Rational(2, 7)} assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == "0.346011 + 0.433884*I" assert NS(((-I - sqrt(2) * I) ** 2).evalf()) == "-5.82842712474619" assert NS((1 + I) ** 2 * I, 15) == "-2.00000000000000" # 1659 (1/2): assert NS(pi.evalf(69) - pi) == "-4.43863937855894e-71" # 1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n ** 25 - 477638700 * n ** 37 - 19 * n, subs={n: 0.01}) == "19.8100000000000" assert NS(((x - 1) * ((1 - x)) ** 1000).n()) == "(-x + 1.00000000000000)**1000*(x - 1.00000000000000)" assert NS((-x).n()) == "-x" assert NS((-2 * x).n()) == "-2.00000000000000*x" assert NS((-2 * x * y).n()) == "-2.00000000000000*x*y" assert cos(x).n(subs={x: 1 + I}) == cos(x).subs(x, 1 + I).n() # 3561. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * sin(oo)).n() == S.Zero assert (0 / sin(oo)).n() == S.Zero assert (0 * E ** (oo)).n() == S.NaN assert (0 / E ** (oo)).n() == S.Zero assert (0 + sin(oo)).n() == S.NaN assert (0 - sin(oo)).n() == S.NaN assert (0 + E ** (oo)).n() == S.Infinity assert (0 - E ** (oo)).n() == S.NegativeInfinity assert (5 * sin(oo)).n() == S.NaN assert (5 / sin(oo)).n() == S.NaN assert (5 * E ** (oo)).n() == S.Infinity assert (5 / E ** (oo)).n() == S.Zero assert (5 + sin(oo)).n() == S.NaN assert (5 - sin(oo)).n() == S.NaN assert (5 + E ** (oo)).n() == S.Infinity assert (5 - E ** (oo)).n() == S.NegativeInfinity
def test_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2 * x)) assert fcode(g(x)) == " 2*x" g = implemented_function('g', Lambda(x, 2 * pi / x)) assert fcode(g(x)) == (" parameter (pi = %sd0)\n" " 2*pi/x") % pi.evalf(17) A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x * (1 + x) * (2 + x))) assert fcode( g(A[i]), assign_to=A[i]) == (" do i = 1, n\n" " A(i) = (A(i) + 1)*(A(i) + 2)*A(i)\n" " end do")
def test_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2*x)) assert fcode(g(x)) == " 2*x" g = implemented_function('g', Lambda(x, 2*pi/x)) assert fcode(g(x)) == ( " parameter (pi = %sd0)\n" " 2*pi/x" ) % pi.evalf(17) A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x))) assert fcode(g(A[i]), assign_to=A[i]) == ( " do i = 1, n\n" " A(i) = (A(i) + 1)*(A(i) + 2)*A(i)\n" " end do" )
def test_nsolve(): # onedimensional from sympy import Symbol, sin, pi x = Symbol('x') assert nsolve(sin(x), 2) - pi.evalf() < 1e-16 assert nsolve(Eq(2 * x, 2), x, -10) == nsolve(2 * x - 2, -10) # Testing checks on number of inputs raises(TypeError, "nsolve(Eq(2*x,2))") raises(TypeError, "nsolve(Eq(2*x,2),x,1,2)") # Issue 1730 assert nsolve(x**2 / (1 - x) / (1 - 2 * x)**2 - 100, x, 0) # doesn't fail # multidimensional x1 = Symbol('x1') x2 = Symbol('x2') f1 = 3 * x1**2 - 2 * x2**2 - 1 f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8 f = Matrix((f1, f2)).T F = lambdify((x1, x2), f.T, modules='mpmath') for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]: x = nsolve(f, (x1, x2), x0, tol=1.e-8) assert mnorm(F(*x), 1) <= 1.e-10 # The Chinese mathematician Zhu Shijie was the very first to solve this # nonlinear system 700 years ago (z was added to make it 3-dimensional) x = Symbol('x') y = Symbol('y') z = Symbol('z') f1 = -x + 2 * y f2 = (x**2 + x * (y**2 - 2) - 4 * y) / (x + 4) f3 = sqrt(x**2 + y**2) * z f = Matrix((f1, f2, f3)).T F = lambdify((x, y, z), f.T, modules='mpmath') def getroot(x0): root = nsolve((f1, f2, f3), (x, y, z), x0) assert mnorm(F(*root), 1) <= 1.e-8 return root assert map(round, getroot((1, 1, 1))) == [2.0, 1.0, 0.0] assert nsolve([Eq(f1), Eq(f2), Eq(f3)], [x, y, z], (1, 1, 1)) # just see that it works a = Symbol('a') assert nsolve(1 / (0.001 + a)**3 - 6 / (0.9 - a)**3, a, 0.3).ae(mpf('0.31883011387318591'))
def test_evalf_bugs(): assert NS(sin(1) + exp(-10 ** 10), 10) == NS(sin(1), 10) assert NS(exp(10 ** 10) + sin(1), 10) == NS(exp(10 ** 10), 10) assert NS("log(1+1/10**50)", 20) == "1.0000000000000000000e-50" assert NS("log(10**100,10)", 10) == "100.0000000" assert NS("log(2)", 10) == "0.6931471806" assert NS("(sin(x)-x)/x**3", 15, subs={x: "1/10**50"}) == "-0.166666666666667" assert NS(sin(1) + Rational(1, 10 ** 100) * I, 15) == "0.841470984807897 + 1.00000000000000e-100*I" assert x.evalf() == x assert NS((1 + I) ** 2 * I, 6) == "-2.00000 + 2.32831e-10*I" d = {n: (-1) ** Rational(6, 7), y: (-1) ** Rational(4, 7), x: (-1) ** Rational(2, 7)} assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == "0.346011 + 0.433884*I" assert NS(((-I - sqrt(2) * I) ** 2).evalf()) == "-5.82842712474619" assert NS((1 + I) ** 2 * I, 15) == "-2.00000000000000 + 2.16840434497101e-19*I" # 1659 (1/2): assert NS(pi.evalf(69) - pi) == "-4.43863937855894e-71" # 1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n ** 25 - 477638700 * n ** 37 - 19 * n, subs={n: 0.01}) == "19.8100000000000"
def test_nsolve(): # onedimensional x = Symbol('x') assert nsolve(sin(x), 2) - pi.evalf() < 1e-15 assert nsolve(Eq(2*x, 2), x, -10) == nsolve(2*x - 2, -10) # Testing checks on number of inputs raises(TypeError, lambda: nsolve(Eq(2*x, 2))) raises(TypeError, lambda: nsolve(Eq(2*x, 2), x, 1, 2)) # issue 4829 assert nsolve(x**2/(1 - x)/(1 - 2*x)**2 - 100, x, 0) # doesn't fail # multidimensional x1 = Symbol('x1') x2 = Symbol('x2') f1 = 3 * x1**2 - 2 * x2**2 - 1 f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8 f = Matrix((f1, f2)).T F = lambdify((x1, x2), f.T, modules='mpmath') for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]: x = nsolve(f, (x1, x2), x0, tol=1.e-8) assert mnorm(F(*x), 1) <= 1.e-10 # The Chinese mathematician Zhu Shijie was the very first to solve this # nonlinear system 700 years ago (z was added to make it 3-dimensional) x = Symbol('x') y = Symbol('y') z = Symbol('z') f1 = -x + 2*y f2 = (x**2 + x*(y**2 - 2) - 4*y) / (x + 4) f3 = sqrt(x**2 + y**2)*z f = Matrix((f1, f2, f3)).T F = lambdify((x, y, z), f.T, modules='mpmath') def getroot(x0): root = nsolve(f, (x, y, z), x0) assert mnorm(F(*root), 1) <= 1.e-8 return root assert list(map(round, getroot((1, 1, 1)))) == [2.0, 1.0, 0.0] assert nsolve([Eq( f1), Eq(f2), Eq(f3)], [x, y, z], (1, 1, 1)) # just see that it works a = Symbol('a') assert nsolve(1/(0.001 + a)**3 - 6/(0.9 - a)**3, a, 0.3).ae( mpf('0.31883011387318591'))
def test_nsolve(): # onedimensional x = Symbol("x") assert nsolve(sin(x), 2) - pi.evalf() < 1e-15 assert nsolve(Eq(2 * x, 2), x, -10) == nsolve(2 * x - 2, -10) # Testing checks on number of inputs raises(TypeError, lambda: nsolve(Eq(2 * x, 2))) raises(TypeError, lambda: nsolve(Eq(2 * x, 2), x, 1, 2)) # multidimensional x1 = Symbol("x1") x2 = Symbol("x2") f1 = 3 * x1**2 - 2 * x2**2 - 1 f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8 f = Matrix((f1, f2)).T F = lambdify((x1, x2), f.T, modules="mpmath") for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]: x = nsolve(f, (x1, x2), x0, tol=1.0e-8) assert mnorm(F(*x), 1) <= 1.0e-10 # The Chinese mathematician Zhu Shijie was the very first to solve this # nonlinear system 700 years ago (z was added to make it 3-dimensional) x = Symbol("x") y = Symbol("y") z = Symbol("z") f1 = -x + 2 * y f2 = (x**2 + x * (y**2 - 2) - 4 * y) / (x + 4) f3 = sqrt(x**2 + y**2) * z f = Matrix((f1, f2, f3)).T F = lambdify((x, y, z), f.T, modules="mpmath") def getroot(x0): root = nsolve(f, (x, y, z), x0) assert mnorm(F(*root), 1) <= 1.0e-8 return root assert list(map(round, getroot((1, 1, 1)))) == [2.0, 1.0, 0.0] assert nsolve([Eq(f1, 0), Eq(f2, 0), Eq(f3, 0)], [x, y, z], (1, 1, 1)) # just see that it works a = Symbol("a") assert (abs( nsolve(1 / (0.001 + a)**3 - 6 / (0.9 - a)**3, a, 0.3) - mpf("0.31883011387318591")) < 1e-15)
def get_test_program(measure: bool = False) -> Program: PI = float(pi.evalf()) p = Program() p += X(0) p += Y(1) p += Z(2) p += H(3) p += S(0) p += T(1) p += RX(PI / 2, 2) p += RY(PI / 2, 3) p += RZ(PI / 2, 0) p += CZ(0, 1) p += CNOT(2, 3) p += CCNOT(0, 1, 2) p += CPHASE(PI / 4, 2, 1) p += SWAP(0, 3) if measure: ro = p.declare("ro", "BIT", 4) p += MEASURE(0, ro[0]) p += MEASURE(3, ro[1]) p += MEASURE(2, ro[2]) p += MEASURE(1, ro[3]) return p
def test_evalf_arguments(): raises(TypeError, lambda: pi.evalf(method="garbage"))
def test_issue_8821_highprec_from_str(): s = str(pi.evalf(128)) p = N(s) assert Abs(sin(p)) < 1e-15 p = N(s, 64) assert Abs(sin(p)) < 1e-64
def test_polygon(): t = Triangle(Point(0, 0), Point(2, 0), Point(3, 3)) assert Polygon(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 3)) == t assert Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(0, 0)) == t assert Polygon(Point(2, 0), Point(3, 3), Point(0, 0), Point(1, 0)) == t p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) p5 = Polygon(Point(0, 0), Point(4, 4), Point(0, 4)) # # General polygon # assert p1 == p2 assert len(p1.args) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() # ensure convex for both CW and CCW point specification assert p3.is_convex() assert p4.is_convex() dict5 = p5.angles assert dict5[Point(0, 0)] == pi / 4 assert dict5[Point(0, 4)] == pi / 2 assert p5.encloses_point(Point(x, y)) is None assert p5.encloses_point(Point(1, 3)) assert p5.encloses_point(Point(0, 0)) is False assert p5.encloses_point(Point(4, 0)) is False p5.plot_interval('x') == [x, 0, 1] assert p5.distance(Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2) assert p5.distance( Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4 warnings.filterwarnings( "error", message="Polygons may intersect producing erroneous output") raises( UserWarning, lambda: Polygon(Point(0, 0), Point(1, 0), Point(1, 1)).distance( Polygon(Point(0, 0), Point(0, 1), Point(1, 1)))) warnings.filterwarnings( "ignore", message="Polygons may intersect producing erroneous output") assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4))) assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5 assert p5 != Point(0, 4) assert Point(0, 1) in p5 assert p5.arbitrary_point('t').subs(Symbol('t', real=True), 0) == \ Point(0, 0) raises( ValueError, lambda: Polygon(Point(x, 0), Point(0, y), Point(x, y)). arbitrary_point('x')) # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), Point(0, 1), Point(1, 1))) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), 1, 2)) raises(ValueError, lambda: RegularPolygon(Point(0, 0), 1, 2.5)) assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcenter == p1.circumcenter == Point(0, 0) assert p1.circumradius == p1.radius == 10 assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4) p2.spin(pi / 10) dict1 = p2.angles assert dict1[Point(0, 5)] == 3 * pi / 5 assert p1.is_convex() assert p1.rotation == 0 assert p1.encloses_point(Point(0, 0)) assert p1.encloses_point(Point(11, 0)) is False assert p2.encloses_point(Point(0, 4.9)) p1.spin(pi / 3) assert p1.rotation == pi / 3 assert p1.vertices[0] == Point(5, 5 * sqrt(3)) for var in p1.args: if isinstance(var, Point): assert var == Point(0, 0) else: assert var == 5 or var == 10 or var == pi / 3 assert p1 != Point(0, 0) assert p1 != p5 # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi / 3) == RegularPolygon(Point(0, 0), 10, 5, 2 * pi / 3) assert p1 == p1_old assert p1.area == (-250 * sqrt(5) + 1250) / (4 * tan(pi / 5)) assert p1.length == 20 * sqrt(-sqrt(5) / 8 + S(5) / 8) assert p1.scale(2, 2) == \ RegularPolygon(p1.center, p1.radius*2, p1._n, p1.rotation) assert RegularPolygon((0, 0), 1, 4).scale(2, 3) == \ Polygon(Point(2, 0), Point(0, 3), Point(-2, 0), Point(0, -3)) assert repr(p1) == str(p1) # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2) raises(GeometryError, lambda: Triangle(Point(0, 0))) # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2 * 2, p2 * 3) == Segment(p2, p2 * 3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() is False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() is False assert t2.is_equilateral() assert t3.is_equilateral() is False assert are_similar(t1, t2) is False assert are_similar(t1, t3) assert are_similar(t2, t3) is False assert t1.is_similar(Point(0, 0)) is False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == t1.incircle.radius == 5 - 5 * sqrt(2) / 2 assert t2.inradius == t2.incircle.radius == 5 * sqrt(3) / 6 assert t3.inradius == t3.incircle.radius == x1**2 / ( (2 + sqrt(2)) * Abs(x1)) # Circumcircle assert t1.circumcircle.center == Point(2.5, 2.5) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5)) # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] assert t1.orthocenter == p1 t = S('''Triangle( Point(100080156402737/5000000000000, 79782624633431/500000000000), Point(39223884078253/2000000000000, 156345163124289/1000000000000), Point(31241359188437/1250000000000, 338338270939941/1000000000000000))''') assert t.orthocenter == S( '''Point(-780660869050599840216997''' '''79471538701955848721853/80368430960602242240789074233100000000000000,''' '''20151573611150265741278060334545897615974257/16073686192120448448157''' '''8148466200000000000)''') # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon(Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4)) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2 '''Polygon to Polygon''' # p1.distance(p2) emits a warning # First, test the warning warnings.filterwarnings( "error", message="Polygons may intersect producing erroneous output") raises(UserWarning, lambda: p1.distance(p2)) # now test the actual output warnings.filterwarnings( "ignore", message="Polygons may intersect producing erroneous output") assert p1.distance(p2) == half / 2 assert p1.distance(p3) == sqrt(2) / 2 assert p3.distance(p4) == (sqrt(2) / 2 - sqrt(Rational(2) / 25) / 2)
def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5))/2 assert nsimplify((1 + sqrt(5))/4, [GoldenRatio]) == GoldenRatio/2 assert nsimplify(2/GoldenRatio, [GoldenRatio]) == 2*GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == \ sympify('1/2 - sqrt(3)*I/2') assert nsimplify(sin(3*pi/5, evaluate=False)) == \ sympify('sqrt(sqrt(5)/8 + 5/8)') assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2*atan('1/4')*I)) == sympify('49/17 + 8*I/17') assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1/3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == \ 2**Rational(1, 3) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1/.3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == \ sympify('109861228866811/100000000000000') assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi*log(2)/8 assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == \ -pi/4 - log(2) + S(7)/4 assert nsimplify(x/7.0) == x/7 assert nsimplify(pi/1e2) == pi/100 assert nsimplify(pi/1e2, rational=False) == pi/100.0 assert nsimplify(pi/1e-7) == 10000000*pi assert not nsimplify( factor(-3.0*z**2*(z**2)**(-2.5) + 3*(z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == Rational(33) assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30) assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40) assert nsimplify(-203.1) == -S(2031)/10 assert nsimplify(.2, tolerance=0) == S.One/5 assert nsimplify(-.2, tolerance=0) == -S.One/5 assert nsimplify(.2222, tolerance=0) == S(1111)/5000 assert nsimplify(-.2222, tolerance=0) == -S(1111)/5000 # issue 7211, PR 4112 assert nsimplify(S(2e-8)) == S(1)/50000000 # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue 10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i)*oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(0.33333333, rational=True, rational_conversion='exact') == Rational(0.33333333) # Make sure nsimplify on expressions uses full precision assert nsimplify(pi.evalf(100)*x, rational_conversion='exact').evalf(100) == pi.evalf(100)*x
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('expand_log(log(1+1/10**50))', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' # issue 4758 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' # issue 4758 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * ((1 - x))** 1000).n()) == '(1.00000000000000 - x)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y' assert cos(x).n(subs={x: 1 + I}) == cos(x).subs(x, 1 + I).n() # issue 6660. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * E**(oo)).n() == S.NaN assert (0 / E**(oo)).n() == S.Zero assert (0 + E**(oo)).n() == S.Infinity assert (0 - E**(oo)).n() == S.NegativeInfinity assert (5 * E**(oo)).n() == S.Infinity assert (5 / E**(oo)).n() == S.Zero assert (5 + E**(oo)).n() == S.Infinity assert (5 - E**(oo)).n() == S.NegativeInfinity #issue 7416 assert as_mpmath(0.0, 10, {'chop': True}) == 0 #issue 5412 assert ((oo * I).n() == S.Infinity * I) assert ((oo + oo * I).n() == S.Infinity + S.Infinity * I) #issue 11518 assert NS(2 * x**2.5, 5) == '2.0000*x**2.5000' #issue 13076 assert NS(Mul(Max(0, y), x, evaluate=False).evalf()) == 'x*Max(0, y)'
def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5)) / 2 assert nsimplify((1 + sqrt(5)) / 4, [GoldenRatio]) == GoldenRatio / 2 assert nsimplify(2 / GoldenRatio, [GoldenRatio]) == 2 * GoldenRatio - 2 assert nsimplify(exp(pi*I*Rational(5, 3), evaluate=False)) == \ sympify('1/2 - sqrt(3)*I/2') assert nsimplify(sin(pi*Rational(3, 5), evaluate=False)) == \ sympify('sqrt(sqrt(5)/8 + 5/8)') assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2 * atan('1/4') * I)) == sympify('49/17 + 8*I/17') assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1 / 3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == \ 2**Rational(1, 3) assert nsimplify(x + .5, rational=True) == S.Half + x assert nsimplify(1 / .3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == \ sympify('109861228866811/100000000000000') assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi * log(2) / 8 assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == \ -pi/4 - log(2) + Rational(7, 4) assert nsimplify(x / 7.0) == x / 7 assert nsimplify(pi / 1e2) == pi / 100 assert nsimplify(pi / 1e2, rational=False) == pi / 100.0 assert nsimplify(pi / 1e-7) == 10000000 * pi assert not nsimplify( factor(-3.0 * z**2 * (z**2)**(-2.5) + 3 * (z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == Rational(33) assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30) assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40) assert nsimplify(-203.1) == Rational(-2031, 10) assert nsimplify(.2, tolerance=0) == Rational(1, 5) assert nsimplify(-.2, tolerance=0) == Rational(-1, 5) assert nsimplify(.2222, tolerance=0) == Rational(1111, 5000) assert nsimplify(-.2222, tolerance=0) == Rational(-1111, 5000) # issue 7211, PR 4112 assert nsimplify(S(2e-8)) == Rational(1, 50000000) # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue 10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i) * oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(0.33333333, rational=True, rational_conversion='exact') == Rational(0.33333333) # Make sure nsimplify on expressions uses full precision assert nsimplify( pi.evalf(100) * x, rational_conversion='exact').evalf(100) == pi.evalf(100) * x
def test_polygon(): t = Triangle(Point(0, 0), Point(2, 0), Point(3, 3)) assert Polygon(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 3)) == t assert Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(0, 0)) == t assert Polygon(Point(2, 0), Point(3, 3), Point(0, 0), Point(1, 0)) == t p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) # # General polygon # assert p1 == p2 assert len(p1) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex() # ensure convex for both CW and CCW point specification # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p1.is_convex() # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Real("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Real("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Real("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Real("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Real("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Real("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Real("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Real("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides s2 = t2.sides s3 = t3.sides # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2 * 2, p2 * 3) == Segment(p2, p2 * 3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == 5 - 5 * sqrt(2) / 2 assert t2.inradius == 5 * sqrt(3) / 6 assert t3.inradius == x1 ** 2 / ((2 + sqrt(2)) * Abs(x1)) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon( Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4) ) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) p5 = Polygon( Point(half, 3 ** (half) / 2), Point(-half, 3 ** (half) / 2), Point(-1, 0), Point(-half, -(3) ** (half) / 2), Point(half, -(3) ** (half) / 2), Point(1, 0), ) p6 = Polygon( Point(2, Rational(3) / 10), Point(Rational(17) / 10, 0), Point(2, -Rational(3) / 10), Point(Rational(23) / 10, 0), ) pt1 = Point(half, half) pt2 = Point(1, 1) """Polygon to Point""" assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2 """Polygon to Polygon""" import warnings # p1.distance(p2) emits a warning # First, test the warning warnings.filterwarnings("error", "Polygons may intersect producing erroneous output") raises(UserWarning, "p1.distance(p2)") # now test the actual output warnings.filterwarnings("ignore", "Polygons may intersect producing erroneous output") assert p1.distance(p2) == half / 2 # Keep testing reasonably thread safe, so reset the warning warnings.filterwarnings("default", "Polygons may intersect producing erroneous output") # Note, in Python 2.6+, this can be done more nicely using the # warnings.catch_warnings context manager. # See http://docs.python.org/library/warnings#testing-warnings. assert p1.distance(p3) == sqrt(2) / 2 assert p3.distance(p4) == (sqrt(2) / 2 - sqrt(Rational(2) / 25) / 2) assert p5.distance(p6) == Rational(7) / 10
def test_polygon(): t = Triangle(Point(0, 0), Point(2, 0), Point(3, 3)) assert Polygon(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 3)) == t assert Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(0, 0)) == t assert Polygon(Point(2, 0), Point(3, 3), Point(0, 0), Point(1, 0)) == t p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) p5 = Polygon(Point(0, 0), Point(4, 4), Point(0, 4)) # # General polygon # assert p1 == p2 assert len(p1.args) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex() # ensure convex for both CW and CCW point specification dict5 = p5.angles assert dict5[Point(0, 0)] == pi / 4 assert dict5[Point(0, 4)] == pi / 2 assert p5.encloses_point(Point(x, y)) == None assert p5.encloses_point(Point(1, 3)) assert p5.encloses_point(Point(0, 0)) == False assert p5.encloses_point(Point(4, 0)) == False p5.plot_interval("x") == [x, 0, 1] assert p5.distance(Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2) assert p5.distance(Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4 raises( UserWarning, lambda: Polygon(Point(0, 0), Point(1, 0), Point(1, 1)).distance(Polygon(Point(0, 0), Point(0, 1), Point(1, 1))), ) assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4))) assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5 assert p5 != Point(0, 4) assert Point(0, 1) in p5 assert p5.arbitrary_point("t").subs(Symbol("t", real=True), 0) == Point(0, 0) raises(ValueError, lambda: Polygon(Point(x, 0), Point(0, y), Point(x, y)).arbitrary_point("x")) # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), Point(0, 1), Point(1, 1))) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), 1, 2)) raises(ValueError, lambda: RegularPolygon(Point(0, 0), 1, 2.5)) assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcenter == p1.circumcenter == Point(0, 0) assert p1.circumradius == p1.radius == 10 assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4) p2.spin(pi / 10) dict1 = p2.angles assert dict1[Point(0, 5)] == 3 * pi / 5 assert p1.is_convex() assert p1.rotation == 0 assert p1.encloses_point(Point(0, 0)) assert p1.encloses_point(Point(11, 0)) == False assert p2.encloses_point(Point(0, 4.9)) p1.spin(pi / 3) assert p1.rotation == pi / 3 assert p1.vertices[0] == Point(5, 5 * sqrt(3)) for var in p1.args: if isinstance(var, Point): assert var == Point(0, 0) else: assert var == 5 or var == 10 or var == pi / 3 assert p1 != Point(0, 0) assert p1 != p5 # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi / 3) == RegularPolygon(Point(0, 0), 10, 5, 2 * pi / 3) assert p1 == p1_old assert p1.area == (-250 * sqrt(5) + 1250) / (4 * tan(pi / 5)) assert p1.length == 20 * sqrt(-sqrt(5) / 8 + S(5) / 8) assert p1.scale(2, 2) == RegularPolygon(p1.center, p1.radius * 2, p1._n, p1.rotation) assert RegularPolygon((0, 0), 1, 4).scale(2, 3) == Polygon(Point(2, 0), Point(0, 3), Point(-2, 0), Point(0, -3)) assert ` p1 ` == str(p1) # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2) raises(GeometryError, lambda: Triangle(Point(0, 0))) # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2 * 2, p2 * 3) == Segment(p2, p2 * 3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False assert t1.is_similar(Point(0, 0)) == False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == t1.incircle.radius == 5 - 5 * sqrt(2) / 2 assert t2.inradius == t2.incircle.radius == 5 * sqrt(3) / 6 assert t3.inradius == t3.incircle.radius == x1 ** 2 / ((2 + sqrt(2)) * Abs(x1)) # Circumcircle assert t1.circumcircle.center == Point(2.5, 2.5) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5)) # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] assert t1.orthocenter == p1 t = S( """Triangle( Point(100080156402737/5000000000000, 79782624633431/500000000000), Point(39223884078253/2000000000000, 156345163124289/1000000000000), Point(31241359188437/1250000000000, 338338270939941/1000000000000000))""" ) assert t.orthocenter == S( """Point(-780660869050599840216997""" """79471538701955848721853/80368430960602242240789074233100000000000000,""" """20151573611150265741278060334545897615974257/16073686192120448448157""" """8148466200000000000)""" ) # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon( Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4) ) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) pt1 = Point(half, half) pt2 = Point(1, 1) """Polygon to Point""" assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2
def test_line(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x1) p4 = Point(y1, y1) p5 = Point(x1, 1 + x1) l1 = Line(p1, p2) l2 = Line(p3, p4) l3 = Line(p3, p5) # Basic stuff assert Line(p1, p2) == Line(p2, p1) assert l1 == l2 assert l1 != l3 assert l1.slope == 1 assert l3.slope == oo assert p1 in l1 # is p1 on the line l1? assert p1 not in l3 assert simplify(l1.equation()) in (x-y, y-x) assert simplify(l3.equation()) in (x-x1, x1-x) assert l2.arbitrary_point() in l2 for ind in xrange(0, 5): assert l3.random_point() in l3 # Orthogonality p1_1 = Point(-x1, x1) l1_1 = Line(p1, p1_1) assert l1.perpendicular_line(p1) == l1_1 assert Line.is_perpendicular(l1, l1_1) assert Line.is_perpendicular(l1 , l2) == False # Parallelity p2_1 = Point(-2*x1, 0) l2_1 = Line(p3, p5) assert l2.parallel_line(p1_1) == Line(p2_1, p1_1) assert l2_1.parallel_line(p1) == Line(p1, Point(0, 2)) assert Line.is_parallel(l1, l2) assert Line.is_parallel(l2, l3) == False assert Line.is_parallel(l2, l2.parallel_line(p1_1)) assert Line.is_parallel(l2_1, l2_1.parallel_line(p1)) # Intersection assert intersection(l1, p1) == [p1] assert intersection(l1, p5) == [] assert intersection(l1, l2) in [[l1], [l2]] assert intersection(l1, l1.parallel_line(p5)) == [] # Concurrency l3_1 = Line(Point(5, x1), Point(-Rational(3,5), x1)) assert Line.is_concurrent(l1, l3) assert Line.is_concurrent(l1, l3, l3_1) assert Line.is_concurrent(l1, l1_1, l3) == False # Projection assert l2.projection(p4) == p4 assert l1.projection(p1_1) == p1 assert l3.projection(p2) == Point(x1, 1) # Finding angles l1_1 = Line(p1, Point(5, 0)) assert feq(Line.angle_between(l1, l1_1).evalf(), pi.evalf()/4) # Testing Rays and Segments (very similar to Lines) r1 = Ray(p1, Point(-1, 5)) r2 = Ray(p1, Point(-1, 1)) r3 = Ray(p3, p5) assert l1.projection(r1) == Ray(p1, p2) assert l1.projection(r2) == p1 assert r3 != r1 s1 = Segment(p1, p2) s2 = Segment(p1, p1_1) assert s1.midpoint == Point(Rational(1,2), Rational(1,2)) assert s2.length == sqrt( 2*(x1**2) ) assert s1.perpendicular_bisector() == Line(Point(0, 1), Point(1, 0)) # Testing distance from a Segment to an object s1 = Segment(Point(0, 0), Point(1, 1)) s2 = Segment(Point(half, half), Point(1, 0)) pt1 = Point(0, 0) pt2 = Point(Rational(3)/2, Rational(3)/2) assert s1.distance(pt1) == 0 assert s2.distance(pt1) == 2**(half)/2 assert s2.distance(pt2) == 2**(half) # Special cases of projection and intersection r1 = Ray(Point(1, 1), Point(2, 2)) r2 = Ray(Point(2, 2), Point(0, 0)) r3 = Ray(Point(1, 1), Point(-1, -1)) r4 = Ray(Point(0, 4), Point(-1, -5)) assert intersection(r1, r2) == [Segment(Point(1, 1), Point(2, 2))] assert intersection(r1, r3) == [Point(1, 1)] assert r1.projection(r3) == Point(1, 1) assert r1.projection(r4) == Segment(Point(1, 1), Point(2, 2)) r5 = Ray(Point(0, 0), Point(0, 1)) r6 = Ray(Point(0, 0), Point(0, 2)) assert r5 in r6 assert r6 in r5 s1 = Segment(Point(0, 0), Point(2, 2)) s2 = Segment(Point(-1, 5), Point(-5, -10)) s3 = Segment(Point(0, 4), Point(-2, 2)) assert intersection(r1, s1) == [Segment(Point(1, 1), Point(2, 2))] assert r1.projection(s2) == Segment(Point(1, 1), Point(2, 2)) assert s3.projection(r1) == Segment(Point(0, 4), Point(-1, 3)) l1 = Line(Point(0, 0), Point(3, 4)) r1 = Ray(Point(0, 0), Point(3, 4)) s1 = Segment(Point(0, 0), Point(3, 4)) assert intersection(l1, l1) == [l1] assert intersection(l1, r1) == [r1] assert intersection(l1, s1) == [s1] assert intersection(r1, l1) == [r1] assert intersection(s1, l1) == [s1] entity1 = Segment(Point(-10,10), Point(10,10)) entity2 = Segment(Point(-5,-5), Point(-5,5)) assert intersection(entity1, entity2) == []
def test_Float_default_to_highprec_from_str(): s = str(pi.evalf(128)) assert same_and_same_prec(Float(s), Float(s, ''))
def test_polygon(): p1 = Polygon( Point(0, 0), Point(3,-1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon( Point(6, 0), Point(3,-1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon( Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon( Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) # # General polygon # assert p1 == p2 assert len(p1) == Rational(6) assert len(p1.sides) == 6 assert p1.perimeter == 5+2*sqrt(10)+sqrt(29)+sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex() # ensure convex for both CW and CCW point specification # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) assert p1 != p2 assert p1.interior_angle == 3*pi/5 assert p1.exterior_angle == 2*pi/5 assert p2.apothem == 5*cos(pi/5) assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p1.is_convex() # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Real("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Real("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Real("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Real("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Real("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Real("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Real("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Real("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5,2), sqrt(Rational(75,4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides s2 = t2.sides s3 = t3.sides # Basic stuff assert t1.area == Rational(25,2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf()/2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False # Bisectors bisectors = t1.bisectors assert bisectors[p1] == Segment(p1, Point(Rational(5,2), Rational(5,2))) ic = (250 - 125*sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == 5 - 5*2**(S(1)/2)/2 assert t2.inradius == 5*3**(S(1)/2)/6 assert t3.inradius == (2*x1**2*Abs(x1) - 2**(S(1)/2)*x1**2*Abs(x1))/(2*x1**2) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5,3), Rational(5,3)) assert m[p1] == Segment(p1, Point(Rational(5,2), Rational(5,2))) assert t3.medians[p1] == Segment(p1, Point(x1/2, x1/2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5,2), Rational(5,2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon( Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon( Point(0, Rational(5)/4), Point(1, Rational(5)/4), Point(1, Rational(9)/4), Point(0, Rational(9)/4)) p3 = Polygon( Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon( Point(1, 1), Point(Rational(6)/5, 1), Point(1, Rational(6)/5)) p5 = Polygon( Point(half, 3**(half)/2), Point(-half, 3**(half)/2), Point(-1, 0), Point(-half, -(3)**(half)/2), Point(half, -(3)**(half)/2), Point(1, 0)) p6 = Polygon(Point(2, Rational(3)/10), Point(Rational(17)/10, 0), Point(2, -Rational(3)/10), Point(Rational(23)/10, 0)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3)/4 assert p3.distance(pt2) == sqrt(2)/2 '''Polygon to Polygon''' assert p1.distance(p2) == half/2 assert p1.distance(p3) == sqrt(2)/2 assert p3.distance(p4) == (sqrt(2)/2 - sqrt(Rational(2)/25)/2) assert p5.distance(p6) == Rational(7)/10
def test_mpmath_precision(): mpmath.mp.dps = 100 assert str(lambdify((), pi.evalf(100), 'mpmath')()) == str(pi.evalf(100))
def test_line(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x1) p4 = Point(y1, y1) p5 = Point(x1, 1 + x1) p6 = Point(1, 0) p7 = Point(0, 1) p8 = Point(2, 0) p9 = Point(2, 1) l1 = Line(p1, p2) l2 = Line(p3, p4) l3 = Line(p3, p5) l4 = Line(p1, p6) l5 = Line(p1, p7) l6 = Line(p8, p9) l7 = Line(p2, p9) raises(ValueError, 'Line(Point(0, 0), Point(0, 0))') # Basic stuff assert Line((1, 1), slope=1) == Line((1, 1), (2, 2)) assert Line((1, 1), slope=oo) == Line((1, 1), (1, 2)) assert Line((1, 1), slope=-oo) == Line((1, 1), (1, 2)) raises(ValueError, 'Line((1, 1), 1)') assert Line(p1, p2) == Line(p2, p1) assert l1 == l2 assert l1 != l3 assert l1.slope == 1 assert l1.length == oo assert l3.slope == oo assert l4.slope == 0 assert l4.coefficients == (0, 1, 0) assert l4.equation(x=x, y=y) == y assert l5.slope == oo assert l5.coefficients == (1, 0, 0) assert l5.equation() == x assert l6.equation() == x - 2 assert l7.equation() == y - 1 assert p1 in l1 # is p1 on the line l1? assert p1 not in l3 assert simplify(l1.equation()) in (x - y, y - x) assert simplify(l3.equation()) in (x - x1, x1 - x) assert Line(p1, p2).scale(2, 1) == Line(p1, p9) assert l2.arbitrary_point() in l2 for ind in xrange(0, 5): assert l3.random_point() in l3 # Orthogonality p1_1 = Point(-x1, x1) l1_1 = Line(p1, p1_1) assert l1.perpendicular_line(p1) == l1_1 assert Line.is_perpendicular(l1, l1_1) assert Line.is_perpendicular(l1, l2) == False p = l1.random_point() assert l1.perpendicular_segment(p) == p # Parallelity p2_1 = Point(-2 * x1, 0) l2_1 = Line(p3, p5) assert l2.parallel_line(p1_1) == Line(p2_1, p1_1) assert l2_1.parallel_line(p1) == Line(p1, Point(0, 2)) assert Line.is_parallel(l1, l2) assert Line.is_parallel(l2, l3) == False assert Line.is_parallel(l2, l2.parallel_line(p1_1)) assert Line.is_parallel(l2_1, l2_1.parallel_line(p1)) # Intersection assert intersection(l1, p1) == [p1] assert intersection(l1, p5) == [] assert intersection(l1, l2) in [[l1], [l2]] assert intersection(l1, l1.parallel_line(p5)) == [] # Concurrency l3_1 = Line(Point(5, x1), Point(-Rational(3, 5), x1)) assert Line.is_concurrent(l1) == False assert Line.is_concurrent(l1, l3) assert Line.is_concurrent(l1, l3, l3_1) assert Line.is_concurrent(l1, l1_1, l3) == False # Projection assert l2.projection(p4) == p4 assert l1.projection(p1_1) == p1 assert l3.projection(p2) == Point(x1, 1) raises( GeometryError, 'Line(Point(0, 0), Point(1, 0)).projection(Circle(Point(0, 0), 1))') # Finding angles l1_1 = Line(p1, Point(5, 0)) assert feq(Line.angle_between(l1, l1_1).evalf(), pi.evalf() / 4) # Testing Rays and Segments (very similar to Lines) assert Ray((1, 1), angle=pi / 4) == Ray((1, 1), (2, 2)) assert Ray((1, 1), angle=pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=-pi / 2) == Ray((1, 1), (1, 0)) assert Ray((1, 1), angle=-3 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5.0 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=3.0 * pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=4.0 * pi) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1)) # XXX don't know why this fails without str assert str(Ray( (1, 1), angle=4.2 * pi)) == str(Ray(Point(1, 1), Point(2, 1 + C.tan(0.2 * pi)))) assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + C.tan(5))) raises(ValueError, 'Ray((1, 1), 1)') r1 = Ray(p1, Point(-1, 5)) r2 = Ray(p1, Point(-1, 1)) r3 = Ray(p3, p5) r4 = Ray(p1, p2) r5 = Ray(p2, p1) r6 = Ray(Point(0, 1), Point(1, 2)) r7 = Ray(Point(0.5, 0.5), Point(1, 1)) assert l1.projection(r1) == Ray(p1, p2) assert l1.projection(r2) == p1 assert r3 != r1 t = Symbol('t', real=True) assert Ray( (1, 1), angle=pi / 4).arbitrary_point() == Point(1 / (1 - t), 1 / (1 - t)) s1 = Segment(p1, p2) s2 = Segment(p1, p1_1) assert s1.midpoint == Point(Rational(1, 2), Rational(1, 2)) assert s2.length == sqrt(2 * (x1**2)) assert s1.perpendicular_bisector() == Line(Point(0, 1), Point(1, 0)) assert Segment((1, 1), (2, 3)).arbitrary_point() == Point(1 + t, 1 + 2 * t) # intersections assert s1.intersection(Line(p6, p9)) == [] s3 = Segment(Point(0.25, 0.25), Point(0.5, 0.5)) assert s1.intersection(s3) == [s1] assert s3.intersection(s1) == [s3] assert r4.intersection(s3) == [s3] assert r4.intersection(Segment(Point(2, 3), Point(3, 4))) == [] assert r4.intersection(Segment(Point(-1, -1), Point( 0.5, 0.5))) == [Segment(p1, Point(0.5, 0.5))] s3 = Segment(Point(1, 1), Point(2, 2)) assert s1.intersection(s3) == [Point(1, 1)] s3 = Segment(Point(0.5, 0.5), Point(1.5, 1.5)) assert s1.intersection(s3) == [Segment(Point(0.5, 0.5), p2)] assert s1.intersection(Segment(Point(4, 4), Point(5, 5))) == [] assert s1.intersection(Segment(Point(-1, -1), p1)) == [p1] assert s1.intersection(Segment(Point(-1, -1), Point( 0.5, 0.5))) == [Segment(p1, Point(0.5, 0.5))] assert r4.intersection(r5) == [s1] assert r5.intersection(r6) == [] assert r4.intersection(r7) == r7.intersection(r4) == [r7] # Segment contains a, b = symbols('a,b') s = Segment((0, a), (0, b)) assert Point(0, (a + b) / 2) in s s = Segment((a, 0), (b, 0)) assert Point((a + b) / 2, 0) in s raises(Undecidable, "Point(2*a, 0) in s") # Testing distance from a Segment to an object s1 = Segment(Point(0, 0), Point(1, 1)) s2 = Segment(Point(half, half), Point(1, 0)) pt1 = Point(0, 0) pt2 = Point(Rational(3) / 2, Rational(3) / 2) assert s1.distance(pt1) == 0 assert s2.distance(pt1) == 2**(half) / 2 assert s2.distance(pt2) == 2**(half) # Special cases of projection and intersection r1 = Ray(Point(1, 1), Point(2, 2)) r2 = Ray(Point(2, 2), Point(0, 0)) r3 = Ray(Point(1, 1), Point(-1, -1)) r4 = Ray(Point(0, 4), Point(-1, -5)) r5 = Ray(Point(2, 2), Point(3, 3)) assert intersection(r1, r2) == [Segment(Point(1, 1), Point(2, 2))] assert intersection(r1, r3) == [Point(1, 1)] assert r1.projection(r3) == Point(1, 1) assert r1.projection(r4) == Segment(Point(1, 1), Point(2, 2)) r5 = Ray(Point(0, 0), Point(0, 1)) r6 = Ray(Point(0, 0), Point(0, 2)) assert r5 in r6 assert r6 in r5 s1 = Segment(Point(0, 0), Point(2, 2)) s2 = Segment(Point(-1, 5), Point(-5, -10)) s3 = Segment(Point(0, 4), Point(-2, 2)) assert intersection(r1, s1) == [Segment(Point(1, 1), Point(2, 2))] assert r1.projection(s2) == Segment(Point(1, 1), Point(2, 2)) assert s3.projection(r1) == Segment(Point(0, 4), Point(-1, 3)) l1 = Line(Point(0, 0), Point(3, 4)) r1 = Ray(Point(0, 0), Point(3, 4)) s1 = Segment(Point(0, 0), Point(3, 4)) assert intersection(l1, l1) == [l1] assert intersection(l1, r1) == [r1] assert intersection(l1, s1) == [s1] assert intersection(r1, l1) == [r1] assert intersection(s1, l1) == [s1] entity1 = Segment(Point(-10, 10), Point(10, 10)) entity2 = Segment(Point(-5, -5), Point(-5, 5)) assert intersection(entity1, entity2) == [] r1 = Ray(p1, Point(0, 1)) r2 = Ray(Point(0, 1), p1) r3 = Ray(p1, p2) r4 = Ray(p2, p1) s1 = Segment(p1, Point(0, 1)) assert Line(r1.source, r1.random_point()).slope == r1.slope assert Line(r2.source, r2.random_point()).slope == r2.slope assert Segment(Point(0, -1), s1.random_point()).slope == s1.slope p_r3 = r3.random_point() p_r4 = r4.random_point() assert p_r3.x >= p1.x and p_r3.y >= p1.y assert p_r4.x <= p2.x and p_r4.y <= p2.y p10 = Point(2000, 2000) s1 = Segment(p1, p10) p_s1 = s1.random_point() assert p1.x <= p_s1.x and p_s1.x <= p10.x and p1.y <= p_s1.y and p_s1.y <= p10.y s2 = Segment(p10, p1) assert hash(s1) == hash(s2) p11 = p10.scale(2, 2) assert s1.is_similar(Segment(p10, p11)) assert s1.is_similar(r1) == False assert (r1 in s1) == False assert Segment(p1, p2) in s1 assert s1.plot_interval() == [t, 0, 1] assert s1 in Line(p1, p10) assert Line(p1, p10) == Line(p10, p1) assert Line(p1, p10) != p1 assert Line(p1, p10).plot_interval() == [t, -5, 5]
def test_polygon(): p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) # # General polygon # assert p1 == p2 assert len(p1) == Rational(6) assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex( ) # ensure convex for both CW and CCW point specification # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p1.is_convex() # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Real("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Real("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Real("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Real("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Real("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Real("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Real("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Real("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides s2 = t2.sides s3 = t3.sides # Basic stuff assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False # Bisectors bisectors = t1.bisectors assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == 5 - 5 * 2**(S(1) / 2) / 2 assert t2.inradius == 5 * 3**(S(1) / 2) / 6 assert t3.inradius == (2 * x1**2 * Abs(x1) - 2**(S(1) / 2) * x1**2 * Abs(x1)) / (2 * x1**2) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon(Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4)) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) p5 = Polygon(Point(half, 3**(half) / 2), Point(-half, 3**(half) / 2), Point(-1, 0), Point(-half, -(3)**(half) / 2), Point(half, -(3)**(half) / 2), Point(1, 0)) p6 = Polygon(Point(2, Rational(3) / 10), Point(Rational(17) / 10, 0), Point(2, -Rational(3) / 10), Point(Rational(23) / 10, 0)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2 '''Polygon to Polygon''' assert p1.distance(p2) == half / 2 assert p1.distance(p3) == sqrt(2) / 2 assert p3.distance(p4) == (sqrt(2) / 2 - sqrt(Rational(2) / 25) / 2) assert p5.distance(p6) == Rational(7) / 10
def test_polygon(): t = Triangle(Point(0, 0), Point(2, 0), Point(3, 3)) assert Polygon(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 3)) == t assert Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(0, 0)) == t assert Polygon(Point(2, 0), Point(3, 3), Point(0, 0), Point(1, 0)) == t p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) p5 = Polygon(Point(0, 0), Point(4, 4), Point(0, 4)) # # General polygon # assert p1 == p2 assert len(p1) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex( ) # ensure convex for both CW and CCW point specification dict5 = p5.angles assert dict5[Point(0, 0)] == pi / 4 assert dict5[Point(0, 4)] == pi / 2 assert p5.encloses_point(Point(x, y)) == None assert p5.encloses_point(Point(1, 3)) assert p5.encloses_point(Point(0, 0)) == False assert p5.encloses_point(Point(4, 0)) == False p5.plot_interval('x') == [x, 0, 1] assert p5.distance(Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2) assert p5.distance( Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4 raises( UserWarning, 'Polygon(Point(0, 0), Point(1, 0), Point(1,1)).distance(Polygon(Point(0, 0), Point(0, 1), Point(1, 1)))' ) assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4))) assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5 assert p5 != Point(0, 4) assert Point(0, 1) in p5 assert p5.arbitrary_point('t').subs(Symbol('t', real=True), 0) == Point(0, 0) raises( ValueError, "Polygon(Point(x, 0), Point(0, y), Point(x, y)).arbitrary_point('x')") # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) raises(GeometryError, 'RegularPolygon(Point(0, 0), Point(0, 1), Point(1, 1))') raises(GeometryError, 'RegularPolygon(Point(0, 0), 1, 2)') assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcenter == p1.circumcenter == Point(0, 0) assert p1.circumradius == p1.radius == 10 assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4) p2.spin(pi / 10) dict1 = p2.angles assert dict1[Point(0, 5)] == 3 * pi / 5 assert p1.is_convex() assert p1.rotation == 0 assert p1.encloses_point(Point(0, 0)) assert p1.encloses_point(Point(11, 0)) == False assert p2.encloses_point(Point(0, 4.9)) p1.spin(pi / 3) assert p1.rotation == pi / 3 assert p1[0] == Point(5, 5 * sqrt(3)) for var in p1: if isinstance(var, Point): assert var == Point(0, 0) else: assert var == 5 or var == 10 or var == pi / 3 assert p1 != Point(0, 0) assert p1 != p5 raises(IndexError, 'RegularPolygon(Point(0, 0), 1, 3)[4]') # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi / 3) == RegularPolygon(Point(0, 0), 10, 5, 2 * pi / 3) assert p1 == p1_old assert ` p1 ` == str(p1) # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2) raises(GeometryError, 'Triangle(Point(0, 0))') # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2 * 2, p2 * 3) == Segment(p2, p2 * 3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False assert t1.is_similar(Point(0, 0)) == False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == t1.incircle.radius == 5 - 5 * sqrt(2) / 2 assert t2.inradius == t2.incircle.radius == 5 * sqrt(3) / 6 assert t3.inradius == t3.incircle.radius == x1**2 / ( (2 + sqrt(2)) * Abs(x1)) # Circumcircle assert t1.circumcircle.center == Point(2.5, 2.5) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5)) # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] assert t1.orthocenter == p1 # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon(Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4)) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2
def test_issue_8821_highprec_from_str(): s = str(pi.evalf(128)) p = sympify(s) assert Abs(sin(p)) < 1e-127
#!/usr/bin/env python from sympy import pi print(pi.evalf(100))
def test_line(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x1) p4 = Point(y1, y1) p5 = Point(x1, 1 + x1) l1 = Line(p1, p2) l2 = Line(p3, p4) l3 = Line(p3, p5) # Basic stuff assert Line(p1, p2) == Line(p2, p1) assert l1 == l2 assert l1 != l3 assert l1.slope == 1 assert l3.slope == oo assert p1 in l1 # is p1 on the line l1? assert p1 not in l3 assert simplify(l1.equation()) in (x - y, y - x) assert simplify(l3.equation()) in (x - x1, x1 - x) assert l2.arbitrary_point() in l2 for ind in xrange(0, 5): assert l3.random_point() in l3 # Orthogonality p1_1 = Point(-x1, x1) l1_1 = Line(p1, p1_1) assert l1.perpendicular_line(p1) == l1_1 assert Line.is_perpendicular(l1, l1_1) assert Line.is_perpendicular(l1, l2) == False # Parallelity p2_1 = Point(-2 * x1, 0) l2_1 = Line(p3, p5) assert l2.parallel_line(p1_1) == Line(p2_1, p1_1) assert l2_1.parallel_line(p1) == Line(p1, Point(0, 2)) assert Line.is_parallel(l1, l2) assert Line.is_parallel(l2, l3) == False assert Line.is_parallel(l2, l2.parallel_line(p1_1)) assert Line.is_parallel(l2_1, l2_1.parallel_line(p1)) # Intersection assert intersection(l1, p1) == [p1] assert intersection(l1, p5) == [] assert intersection(l1, l2) in [[l1], [l2]] assert intersection(l1, l1.parallel_line(p5)) == [] # Concurrency l3_1 = Line(Point(5, x1), Point(-Rational(3, 5), x1)) assert Line.is_concurrent(l1, l3) assert Line.is_concurrent(l1, l3, l3_1) assert Line.is_concurrent(l1, l1_1, l3) == False # Projection assert l2.projection(p4) == p4 assert l1.projection(p1_1) == p1 assert l3.projection(p2) == Point(x1, 1) # Finding angles l1_1 = Line(p1, Point(5, 0)) assert feq(Line.angle_between(l1, l1_1).evalf(), pi.evalf() / 4) # Testing Rays and Segments (very similar to Lines) r1 = Ray(p1, Point(-1, 5)) r2 = Ray(p1, Point(-1, 1)) r3 = Ray(p3, p5) assert l1.projection(r1) == Ray(p1, p2) assert l1.projection(r2) == p1 assert r3 != r1 s1 = Segment(p1, p2) s2 = Segment(p1, p1_1) assert s1.midpoint == Point(Rational(1, 2), Rational(1, 2)) assert s2.length == sqrt(2 * (x1**2)) assert s1.perpendicular_bisector() == Line(Point(0, 1), Point(1, 0)) # Testing distance from a Segment to an object s1 = Segment(Point(0, 0), Point(1, 1)) s2 = Segment(Point(half, half), Point(1, 0)) pt1 = Point(0, 0) pt2 = Point(Rational(3) / 2, Rational(3) / 2) assert s1.distance(pt1) == 0 assert s2.distance(pt1) == 2**(half) / 2 assert s2.distance(pt2) == 2**(half) # Special cases of projection and intersection r1 = Ray(Point(1, 1), Point(2, 2)) r2 = Ray(Point(2, 2), Point(0, 0)) r3 = Ray(Point(1, 1), Point(-1, -1)) r4 = Ray(Point(0, 4), Point(-1, -5)) assert intersection(r1, r2) == [Segment(Point(1, 1), Point(2, 2))] assert intersection(r1, r3) == [Point(1, 1)] assert r1.projection(r3) == Point(1, 1) assert r1.projection(r4) == Segment(Point(1, 1), Point(2, 2)) r5 = Ray(Point(0, 0), Point(0, 1)) r6 = Ray(Point(0, 0), Point(0, 2)) assert r5 in r6 assert r6 in r5 s1 = Segment(Point(0, 0), Point(2, 2)) s2 = Segment(Point(-1, 5), Point(-5, -10)) s3 = Segment(Point(0, 4), Point(-2, 2)) assert intersection(r1, s1) == [Segment(Point(1, 1), Point(2, 2))] assert r1.projection(s2) == Segment(Point(1, 1), Point(2, 2)) assert s3.projection(r1) == Segment(Point(0, 4), Point(-1, 3)) l1 = Line(Point(0, 0), Point(3, 4)) r1 = Ray(Point(0, 0), Point(3, 4)) s1 = Segment(Point(0, 0), Point(3, 4)) assert intersection(l1, l1) == [l1] assert intersection(l1, r1) == [r1] assert intersection(l1, s1) == [s1] assert intersection(r1, l1) == [r1] assert intersection(s1, l1) == [s1] entity1 = Segment(Point(-10, 10), Point(10, 10)) entity2 = Segment(Point(-5, -5), Point(-5, 5)) assert intersection(entity1, entity2) == []
def test_polygon(): t = Triangle(Point(0, 0), Point(2, 0), Point(3, 3)) assert Polygon(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 3)) == t assert Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(0, 0)) == t assert Polygon(Point(2, 0), Point(3, 3), Point(0, 0), Point(1, 0)) == t p1 = Polygon( Point(0, 0), Point(3,-1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon( Point(6, 0), Point(3,-1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon( Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon( Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) p5 = Polygon( Point(0, 0), Point(4, 4), Point(0, 4)) # # General polygon # assert p1 == p2 assert len(p1.args) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5+2*sqrt(10)+sqrt(29)+sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex() # ensure convex for both CW and CCW point specification dict5 = p5.angles assert dict5[Point(0, 0)] == pi / 4 assert dict5[Point(0, 4)] == pi / 2 assert p5.encloses_point(Point(x, y)) == None assert p5.encloses_point(Point(1, 3)) assert p5.encloses_point(Point(0, 0)) == False assert p5.encloses_point(Point(4, 0)) == False p5.plot_interval('x') == [x, 0, 1] assert p5.distance(Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2) assert p5.distance(Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4 raises(UserWarning, 'Polygon(Point(0, 0), Point(1, 0), Point(1,1)).distance(Polygon(Point(0, 0), Point(0, 1), Point(1, 1)))') assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4))) assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5 assert p5 != Point(0, 4) assert Point(0, 1) in p5 assert p5.arbitrary_point('t').subs(Symbol('t', real=True), 0) == Point(0, 0) raises(ValueError, "Polygon(Point(x, 0), Point(0, y), Point(x, y)).arbitrary_point('x')") # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) raises(GeometryError, 'RegularPolygon(Point(0, 0), Point(0, 1), Point(1, 1))') raises(GeometryError, 'RegularPolygon(Point(0, 0), 1, 2)') raises(ValueError, 'RegularPolygon(Point(0, 0), 1, 2.5)') assert p1 != p2 assert p1.interior_angle == 3*pi/5 assert p1.exterior_angle == 2*pi/5 assert p2.apothem == 5*cos(pi/5) assert p2.circumcenter == p1.circumcenter == Point(0, 0) assert p1.circumradius == p1.radius == 10 assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4) p2.spin(pi / 10) dict1 = p2.angles assert dict1[Point(0, 5)] == 3 * pi / 5 assert p1.is_convex() assert p1.rotation == 0 assert p1.encloses_point(Point(0, 0)) assert p1.encloses_point(Point(11, 0)) == False assert p2.encloses_point(Point(0, 4.9)) p1.spin(pi/3) assert p1.rotation == pi/3 assert p1.vertices[0] == Point(5, 5*sqrt(3)) for var in p1.args: if isinstance(var, Point): assert var == Point(0, 0) else: assert var == 5 or var == 10 or var == pi / 3 assert p1 != Point(0, 0) assert p1 != p5 # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi/3) == RegularPolygon(Point(0, 0), 10, 5, 2*pi/3) assert p1 == p1_old assert `p1` == str(p1) # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5,2), sqrt(Rational(75,4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2) raises(GeometryError, 'Triangle(Point(0, 0))') # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2*2, p2*3) == Segment(p2, p2*3) assert t1.area == Rational(25,2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf()/2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False assert t1.is_similar(Point(0, 0)) == False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5,2), Rational(5,2))) ic = (250 - 125*sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == t1.incircle.radius == 5 - 5*sqrt(2)/2 assert t2.inradius == t2.incircle.radius == 5*sqrt(3)/6 assert t3.inradius == t3.incircle.radius == x1**2/((2 + sqrt(2))*Abs(x1)) # Circumcircle assert t1.circumcircle.center == Point(2.5, 2.5) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5,3), Rational(5,3)) assert m[p1] == Segment(p1, Point(Rational(5,2), Rational(5,2))) assert t3.medians[p1] == Segment(p1, Point(x1/2, x1/2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5)) # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5,2), Rational(5,2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] assert t1.orthocenter == p1 # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon( Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon( Point(0, Rational(5)/4), Point(1, Rational(5)/4), Point(1, Rational(9)/4), Point(0, Rational(9)/4)) p3 = Polygon( Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon( Point(1, 1), Point(Rational(6)/5, 1), Point(1, Rational(6)/5)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3)/4 assert p3.distance(pt2) == sqrt(2)/2
def test_line(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x1) p4 = Point(y1, y1) p5 = Point(x1, 1 + x1) p6 = Point(1, 0) p7 = Point(0, 1) p8 = Point(2, 0) p9 = Point(2, 1) l1 = Line(p1, p2) l2 = Line(p3, p4) l3 = Line(p3, p5) l4 = Line(p1, p6) l5 = Line(p1, p7) l6 = Line(p8, p9) l7 = Line(p2, p9) raises(ValueError, lambda: Line(Point(0, 0), Point(0, 0))) # Basic stuff assert Line((1, 1), slope=1) == Line((1, 1), (2, 2)) assert Line((1, 1), slope=oo) == Line((1, 1), (1, 2)) assert Line((1, 1), slope=-oo) == Line((1, 1), (1, 2)) raises(ValueError, lambda: Line((1, 1), 1)) assert Line(p1, p2) == Line(p2, p1) assert l1 == l2 assert l1 != l3 assert l1.slope == 1 assert l1.length == oo assert l3.slope == oo assert l4.slope == 0 assert l4.coefficients == (0, 1, 0) assert l4.equation(x=x, y=y) == y assert l5.slope == oo assert l5.coefficients == (1, 0, 0) assert l5.equation() == x assert l6.equation() == x - 2 assert l7.equation() == y - 1 assert p1 in l1 # is p1 on the line l1? assert p1 not in l3 assert Line((-x, x), (-x + 1, x - 1)).coefficients == (1, 1, 0) assert simplify(l1.equation()) in (x - y, y - x) assert simplify(l3.equation()) in (x - x1, x1 - x) assert Line(p1, p2).scale(2, 1) == Line(p1, p9) assert l2.arbitrary_point() in l2 for ind in xrange(0, 5): assert l3.random_point() in l3 # Orthogonality p1_1 = Point(-x1, x1) l1_1 = Line(p1, p1_1) assert l1.perpendicular_line(p1) == l1_1 assert Line.is_perpendicular(l1, l1_1) assert Line.is_perpendicular(l1, l2) == False p = l1.random_point() assert l1.perpendicular_segment(p) == p # Parallelity p2_1 = Point(-2 * x1, 0) l2_1 = Line(p3, p5) assert l2.parallel_line(p1_1) == Line(p2_1, p1_1) assert l2_1.parallel_line(p1) == Line(p1, Point(0, 2)) assert Line.is_parallel(l1, l2) assert Line.is_parallel(l2, l3) == False assert Line.is_parallel(l2, l2.parallel_line(p1_1)) assert Line.is_parallel(l2_1, l2_1.parallel_line(p1)) # Intersection assert intersection(l1, p1) == [p1] assert intersection(l1, p5) == [] assert intersection(l1, l2) in [[l1], [l2]] assert intersection(l1, l1.parallel_line(p5)) == [] # Concurrency l3_1 = Line(Point(5, x1), Point(-Rational(3, 5), x1)) assert Line.is_concurrent(l1) == False assert Line.is_concurrent(l1, l3) assert Line.is_concurrent(l1, l3, l3_1) assert Line.is_concurrent(l1, l1_1, l3) == False # Projection assert l2.projection(p4) == p4 assert l1.projection(p1_1) == p1 assert l3.projection(p2) == Point(x1, 1) raises(GeometryError, lambda: Line(Point(0, 0), Point(1, 0)).projection(Circle(Point(0, 0), 1))) # Finding angles l1_1 = Line(p1, Point(5, 0)) assert feq(Line.angle_between(l1, l1_1).evalf(), pi.evalf() / 4) # Testing Rays and Segments (very similar to Lines) assert Ray((1, 1), angle=pi / 4) == Ray((1, 1), (2, 2)) assert Ray((1, 1), angle=pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=-pi / 2) == Ray((1, 1), (1, 0)) assert Ray((1, 1), angle=-3 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5.0 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=3.0 * pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=4.0 * pi) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1)) # XXX don't know why this fails without str assert str(Ray((1, 1), angle=4.2 * pi)) == str(Ray(Point(1, 1), Point(2, 1 + C.tan(0.2 * pi)))) assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + C.tan(5))) raises(ValueError, lambda: Ray((1, 1), 1)) r1 = Ray(p1, Point(-1, 5)) r2 = Ray(p1, Point(-1, 1)) r3 = Ray(p3, p5) r4 = Ray(p1, p2) r5 = Ray(p2, p1) r6 = Ray(Point(0, 1), Point(1, 2)) r7 = Ray(Point(0.5, 0.5), Point(1, 1)) assert l1.projection(r1) == Ray(p1, p2) assert l1.projection(r2) == p1 assert r3 != r1 t = Symbol("t", real=True) assert Ray((1, 1), angle=pi / 4).arbitrary_point() == Point(1 / (1 - t), 1 / (1 - t)) s1 = Segment(p1, p2) s2 = Segment(p1, p1_1) assert s1.midpoint == Point(Rational(1, 2), Rational(1, 2)) assert s2.length == sqrt(2 * (x1 ** 2)) assert s1.perpendicular_bisector() == Line(Point(0, 1), Point(1, 0)) assert Segment((1, 1), (2, 3)).arbitrary_point() == Point(1 + t, 1 + 2 * t) # intersections assert s1.intersection(Line(p6, p9)) == [] s3 = Segment(Point(0.25, 0.25), Point(0.5, 0.5)) assert s1.intersection(s3) == [s1] assert s3.intersection(s1) == [s3] assert r4.intersection(s3) == [s3] assert r4.intersection(Segment(Point(2, 3), Point(3, 4))) == [] assert r4.intersection(Segment(Point(-1, -1), Point(0.5, 0.5))) == [Segment(p1, Point(0.5, 0.5))] s3 = Segment(Point(1, 1), Point(2, 2)) assert s1.intersection(s3) == [Point(1, 1)] s3 = Segment(Point(0.5, 0.5), Point(1.5, 1.5)) assert s1.intersection(s3) == [Segment(Point(0.5, 0.5), p2)] assert s1.intersection(Segment(Point(4, 4), Point(5, 5))) == [] assert s1.intersection(Segment(Point(-1, -1), p1)) == [p1] assert s1.intersection(Segment(Point(-1, -1), Point(0.5, 0.5))) == [Segment(p1, Point(0.5, 0.5))] assert r4.intersection(r5) == [s1] assert r5.intersection(r6) == [] assert r4.intersection(r7) == r7.intersection(r4) == [r7] # Segment contains a, b = symbols("a,b") s = Segment((0, a), (0, b)) assert Point(0, (a + b) / 2) in s s = Segment((a, 0), (b, 0)) assert Point((a + b) / 2, 0) in s raises(Undecidable, lambda: Point(2 * a, 0) in s) # Testing distance from a Segment to an object s1 = Segment(Point(0, 0), Point(1, 1)) s2 = Segment(Point(half, half), Point(1, 0)) pt1 = Point(0, 0) pt2 = Point(Rational(3) / 2, Rational(3) / 2) assert s1.distance(pt1) == 0 assert s2.distance(pt1) == 2 ** (half) / 2 assert s2.distance(pt2) == 2 ** (half) # Special cases of projection and intersection r1 = Ray(Point(1, 1), Point(2, 2)) r2 = Ray(Point(2, 2), Point(0, 0)) r3 = Ray(Point(1, 1), Point(-1, -1)) r4 = Ray(Point(0, 4), Point(-1, -5)) r5 = Ray(Point(2, 2), Point(3, 3)) assert intersection(r1, r2) == [Segment(Point(1, 1), Point(2, 2))] assert intersection(r1, r3) == [Point(1, 1)] assert r1.projection(r3) == Point(1, 1) assert r1.projection(r4) == Segment(Point(1, 1), Point(2, 2)) r5 = Ray(Point(0, 0), Point(0, 1)) r6 = Ray(Point(0, 0), Point(0, 2)) assert r5 in r6 assert r6 in r5 s1 = Segment(Point(0, 0), Point(2, 2)) s2 = Segment(Point(-1, 5), Point(-5, -10)) s3 = Segment(Point(0, 4), Point(-2, 2)) assert intersection(r1, s1) == [Segment(Point(1, 1), Point(2, 2))] assert r1.projection(s2) == Segment(Point(1, 1), Point(2, 2)) assert s3.projection(r1) == Segment(Point(0, 4), Point(-1, 3)) l1 = Line(Point(0, 0), Point(3, 4)) r1 = Ray(Point(0, 0), Point(3, 4)) s1 = Segment(Point(0, 0), Point(3, 4)) assert intersection(l1, l1) == [l1] assert intersection(l1, r1) == [r1] assert intersection(l1, s1) == [s1] assert intersection(r1, l1) == [r1] assert intersection(s1, l1) == [s1] entity1 = Segment(Point(-10, 10), Point(10, 10)) entity2 = Segment(Point(-5, -5), Point(-5, 5)) assert intersection(entity1, entity2) == [] r1 = Ray(p1, Point(0, 1)) r2 = Ray(Point(0, 1), p1) r3 = Ray(p1, p2) r4 = Ray(p2, p1) s1 = Segment(p1, Point(0, 1)) assert Line(r1.source, r1.random_point()).slope == r1.slope assert Line(r2.source, r2.random_point()).slope == r2.slope assert Segment(Point(0, -1), s1.random_point()).slope == s1.slope p_r3 = r3.random_point() p_r4 = r4.random_point() assert p_r3.x >= p1.x and p_r3.y >= p1.y assert p_r4.x <= p2.x and p_r4.y <= p2.y p10 = Point(2000, 2000) s1 = Segment(p1, p10) p_s1 = s1.random_point() assert p1.x <= p_s1.x and p_s1.x <= p10.x and p1.y <= p_s1.y and p_s1.y <= p10.y s2 = Segment(p10, p1) assert hash(s1) == hash(s2) p11 = p10.scale(2, 2) assert s1.is_similar(Segment(p10, p11)) assert s1.is_similar(r1) == False assert (r1 in s1) == False assert Segment(p1, p2) in s1 assert s1.plot_interval() == [t, 0, 1] assert s1 in Line(p1, p10) assert Line(p1, p10) == Line(p10, p1) assert Line(p1, p10) != p1 assert Line(p1, p10).plot_interval() == [t, -5, 5] assert Ray((0, 0), angle=pi / 4).plot_interval() == [t, 0, 5 * sqrt(2) / (1 + 5 * sqrt(2))]
def test_polygon(): t = Triangle(Point(0, 0), Point(2, 0), Point(3, 3)) assert Polygon(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 3)) == t assert Polygon(Point(1, 0), Point(2, 0), Point(3, 3), Point(0, 0)) == t assert Polygon(Point(2, 0), Point(3, 3), Point(0, 0), Point(1, 0)) == t p1 = Polygon(Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon(Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon(Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon(Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) # # General polygon # assert p1 == p2 assert len(p1) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2 * sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() assert p3.is_convex() assert p4.is_convex( ) # ensure convex for both CW and CCW point specification # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) assert p1 != p2 assert p1.interior_angle == 3 * pi / 5 assert p1.exterior_angle == 2 * pi / 5 assert p2.apothem == 5 * cos(pi / 5) assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p1.is_convex() assert p1.rotation == 0 p1.spin(pi / 3) assert p1.rotation == pi / 3 assert p1[0] == Point(5, 5 * sqrt(3)) # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi / 3) == RegularPolygon(Point(0, 0), 10, 5, 2 * pi / 3) assert p1 == p1_old # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2 * 2, p2 * 3) == Segment(p2, p2 * 3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() == False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf() / 2) assert t1.is_equilateral() == False assert t2.is_equilateral() assert t3.is_equilateral() == False assert are_similar(t1, t2) == False assert are_similar(t1, t3) assert are_similar(t2, t3) == False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125 * sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == 5 - 5 * sqrt(2) / 2 assert t2.inradius == 5 * sqrt(3) / 6 assert t3.inradius == x1**2 / ((2 + sqrt(2)) * Abs(x1)) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1 / 2, x1 / 2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon(Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon(Point(0, Rational(5) / 4), Point(1, Rational(5) / 4), Point(1, Rational(9) / 4), Point(0, Rational(9) / 4)) p3 = Polygon(Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon(Point(1, 1), Point(Rational(6) / 5, 1), Point(1, Rational(6) / 5)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3) / 4 assert p3.distance(pt2) == sqrt(2) / 2
def test_line(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x1) p4 = Point(y1, y1) p5 = Point(x1, 1 + x1) p6 = Point(1, 0) p7 = Point(0, 1) p8 = Point(2, 0) p9 = Point(2, 1) l1 = Line(p1, p2) l2 = Line(p3, p4) l3 = Line(p3, p5) l4 = Line(p1, p6) l5 = Line(p1, p7) l6 = Line(p8, p9) l7 = Line(p2, p9) # Basic stuff assert Line((1, 1), slope=1) == Line((1, 1), (2, 2)) assert Line((1, 1), slope=oo) == Line((1, 1), (1, 2)) assert Line((1, 1), slope=-oo) == Line((1, 1), (1, 2)) raises(ValueError, "Line((1, 1), 1)") assert Line(p1, p2) == Line(p2, p1) assert l1 == l2 assert l1 != l3 assert l1.slope == 1 assert l3.slope == oo assert l4.slope == 0 assert l4.coefficients == (0, 1, 0) assert l4.equation(x=x, y=y) == y assert l5.slope == oo assert l5.coefficients == (1, 0, 0) assert l5.equation() == x assert l6.equation() == x - 2 assert l7.equation() == y - 1 assert p1 in l1 # is p1 on the line l1? assert p1 not in l3 assert simplify(l1.equation()) in (x - y, y - x) assert simplify(l3.equation()) in (x - x1, x1 - x) assert l2.arbitrary_point() in l2 for ind in xrange(0, 5): assert l3.random_point() in l3 # Orthogonality p1_1 = Point(-x1, x1) l1_1 = Line(p1, p1_1) assert l1.perpendicular_line(p1) == l1_1 assert Line.is_perpendicular(l1, l1_1) assert Line.is_perpendicular(l1, l2) == False # Parallelity p2_1 = Point(-2 * x1, 0) l2_1 = Line(p3, p5) assert l2.parallel_line(p1_1) == Line(p2_1, p1_1) assert l2_1.parallel_line(p1) == Line(p1, Point(0, 2)) assert Line.is_parallel(l1, l2) assert Line.is_parallel(l2, l3) == False assert Line.is_parallel(l2, l2.parallel_line(p1_1)) assert Line.is_parallel(l2_1, l2_1.parallel_line(p1)) # Intersection assert intersection(l1, p1) == [p1] assert intersection(l1, p5) == [] assert intersection(l1, l2) in [[l1], [l2]] assert intersection(l1, l1.parallel_line(p5)) == [] # Concurrency l3_1 = Line(Point(5, x1), Point(-Rational(3, 5), x1)) assert Line.is_concurrent(l1, l3) assert Line.is_concurrent(l1, l3, l3_1) assert Line.is_concurrent(l1, l1_1, l3) == False # Projection assert l2.projection(p4) == p4 assert l1.projection(p1_1) == p1 assert l3.projection(p2) == Point(x1, 1) # Finding angles l1_1 = Line(p1, Point(5, 0)) assert feq(Line.angle_between(l1, l1_1).evalf(), pi.evalf() / 4) # Testing Rays and Segments (very similar to Lines) assert Ray((1, 1), angle=pi / 4) == Ray((1, 1), (2, 2)) assert Ray((1, 1), angle=pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=-pi / 2) == Ray((1, 1), (1, 0)) assert Ray((1, 1), angle=-3 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=5.0 * pi / 2) == Ray((1, 1), (1, 2)) assert Ray((1, 1), angle=pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=3.0 * pi) == Ray((1, 1), (0, 1)) assert Ray((1, 1), angle=4.0 * pi) == Ray((1, 1), (2, 1)) assert Ray((1, 1), angle=0) == Ray((1, 1), (2, 1)) # XXX don't know why this fails without str assert str(Ray((1, 1), angle=4.2 * pi)) == str(Ray(Point(1, 1), Point(2, 1 + C.tan(0.2 * pi)))) assert Ray((1, 1), angle=5) == Ray((1, 1), (2, 1 + C.tan(5))) raises(ValueError, "Ray((1, 1), 1)") r1 = Ray(p1, Point(-1, 5)) r2 = Ray(p1, Point(-1, 1)) r3 = Ray(p3, p5) assert l1.projection(r1) == Ray(p1, p2) assert l1.projection(r2) == p1 assert r3 != r1 t = Symbol("t", real=True) assert Ray((1, 1), angle=pi / 4).arbitrary_point() == Point(1 / (1 - t), 1 / (1 - t)) s1 = Segment(p1, p2) s2 = Segment(p1, p1_1) assert s1.midpoint == Point(Rational(1, 2), Rational(1, 2)) assert s2.length == sqrt(2 * (x1 ** 2)) assert s1.perpendicular_bisector() == Line(Point(0, 1), Point(1, 0)) assert Segment((1, 1), (2, 3)).arbitrary_point() == Point(1 + t, 1 + 2 * t) # Segment contains a, b = symbols("a,b") s = Segment((0, a), (0, b)) assert Point(0, (a + b) / 2) in s s = Segment((a, 0), (b, 0)) assert Point((a + b) / 2, 0) in s assert (Point(2 * a, 0) in s) is False # XXX should be None? # Testing distance from a Segment to an object s1 = Segment(Point(0, 0), Point(1, 1)) s2 = Segment(Point(half, half), Point(1, 0)) pt1 = Point(0, 0) pt2 = Point(Rational(3) / 2, Rational(3) / 2) assert s1.distance(pt1) == 0 assert s2.distance(pt1) == 2 ** (half) / 2 assert s2.distance(pt2) == 2 ** (half) # Special cases of projection and intersection r1 = Ray(Point(1, 1), Point(2, 2)) r2 = Ray(Point(2, 2), Point(0, 0)) r3 = Ray(Point(1, 1), Point(-1, -1)) r4 = Ray(Point(0, 4), Point(-1, -5)) assert intersection(r1, r2) == [Segment(Point(1, 1), Point(2, 2))] assert intersection(r1, r3) == [Point(1, 1)] assert r1.projection(r3) == Point(1, 1) assert r1.projection(r4) == Segment(Point(1, 1), Point(2, 2)) r5 = Ray(Point(0, 0), Point(0, 1)) r6 = Ray(Point(0, 0), Point(0, 2)) assert r5 in r6 assert r6 in r5 s1 = Segment(Point(0, 0), Point(2, 2)) s2 = Segment(Point(-1, 5), Point(-5, -10)) s3 = Segment(Point(0, 4), Point(-2, 2)) assert intersection(r1, s1) == [Segment(Point(1, 1), Point(2, 2))] assert r1.projection(s2) == Segment(Point(1, 1), Point(2, 2)) assert s3.projection(r1) == Segment(Point(0, 4), Point(-1, 3)) l1 = Line(Point(0, 0), Point(3, 4)) r1 = Ray(Point(0, 0), Point(3, 4)) s1 = Segment(Point(0, 0), Point(3, 4)) assert intersection(l1, l1) == [l1] assert intersection(l1, r1) == [r1] assert intersection(l1, s1) == [s1] assert intersection(r1, l1) == [r1] assert intersection(s1, l1) == [s1] entity1 = Segment(Point(-10, 10), Point(10, 10)) entity2 = Segment(Point(-5, -5), Point(-5, 5)) assert intersection(entity1, entity2) == []