def test_u(): theta, phi, lambd = symbols("theta phi lambd") actual_1 = simplify(Circuit().u(theta, phi, lambd, -(phi + lambd) / 2)[0].run(backend="sympy_unitary")) assert actual_1[0, 0] != 0 expected_1 = simplify(Circuit().rz(lambd)[0].ry(theta)[0].rz(phi)[0].run_with_sympy_unitary()) assert expected_1[0, 0] != 0 assert simplify(nfloat(actual_1)) == simplify(nfloat(expected_1))
def NR_Multivar(X0, F, kk): J = Matrix.jacobian(F, [x, y, z]) # calculate Jacobian matrix for i in range(kk): Jinv = invJ(J, X0) Fi = sp.nfloat(F.subs([(x, X0[0]), (y, X0[1]), (z, X0[2])]), 7) # 7 is numbers after the point X0 = Iteration(X0, Jinv, Fi) print(sp.nfloat(X0, 7))
def test_nfloat(): from sympy.core.basic import _aresame x = Symbol("x") eq = x**(S(4)/3) + 4*x**(S(1)/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(S(1)/3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**(S(4)/3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 Float_big = Float(big, '') assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big)
def spec(f, a, b, T): mag = [nfloat(sqrt(a[i]**2 + b[i]**2)) for i in range(len(a))] phi = len(a) * [0] for i in range(len(b)): if a[i] == S.Zero: phi[i] = 0 else: phi[i] = nfloat(arg(a[i] - S.ImaginaryUnit * b[i])) mag.insert(0, a0(f, T)) if mag[0] >= 0: phi.insert(0, 0) else: phi.insert(0, pi) return (mag, phi)
def test_evalf_relational(): assert Eq(x/5, y/10).evalf() == Eq(0.2*x, 0.1*y) # if this first assertion fails it should be replaced with # one that doesn't assert unchanged(Eq, (3 - I)**2/2 + I, 0) assert Eq((3 - I)**2/2 + I, 0).n() is S.false assert nfloat(Eq((3 - I)**2 + I, 0)) == S.false
def test_evalf_relational(): assert Eq(x/5, y/10).evalf() == Eq(0.2*x, 0.1*y) # if this first assertion fails it should be replaced with # one that doesn't assert unchanged(Eq, (3 - I)**2/2 + I, 0) assert Eq((3 - I)**2/2 + I, 0).n() is S.false # note: these don't always evaluate to Boolean assert nfloat(Eq((3 - I)**2 + I, 0)) == Eq((3.0 - I)**2 + I, 0)
def test_nfloat(): from sympy.core.basic import _aresame x = Symbol("x") eq = x**(S(4)/3) + 4*x**(S(1)/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(S(1)/3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**(S(4)/3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 Float_big = Float(big, '') assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big) # issues 3243 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solve(f.subs(x, -0.139)))
def test_evalf_relational(): assert Eq(x / 5, y / 10).evalf() == Eq(0.2 * x, 0.1 * y) # if this first assertion fails it should be replaced with # one that doesn't assert unchanged(Eq, (3 - I)**2 / 2 + I, 0) assert Eq((3 - I)**2 / 2 + I, 0).n() is S.false # note: these don't always evaluate to Boolean assert nfloat(Eq((3 - I)**2 + I, 0)) == Eq((3.0 - I)**2 + I, 0)
def test_nfloat(): from sympy.core.basic import _aresame x = Symbol("x") eq = x**(S(4) / 3) + 4 * x**(S(1) / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(S(1) / 3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**(S(4) / 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 Float_big = Float(big, '') assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big) # issues 3243 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solve(f.subs(x, -0.139)))
def sroots(self, Am, tol=None, nn=None, char=None): #returns sympy format, python format λ = sp.Symbol('λ') if tol == None: tol = 10**-15 if nn == None: nv = 15 elif nn != None: nv = int(nn) if char == None: A = sp.Matrix(Am) M, N = np.array(Am).shape II = sp.eye(M) Ad = A - II*λ Deta = Ad.det() elif char != None: Deta = char Detsimp = sp.nfloat(sp.nsimplify(Deta, tolerance=tol, full=True), n=nv) # rlist = list(sp.solve(Detsimp, λ)) rlist = list(sp.solve(Detsimp, λ, **{'set': True ,'particular': True})) rootsd = [sp.nsimplify(i, tolerance=tol, full=True) for i in rlist] sympl = [] numpr = [] numpi = [] for i, j in enumerate(rootsd): sympl.append(sp.simplify(sp.nfloat(rootsd[i], n=nv), rational=False)) vals = j.as_real_imag() vali = sp.nfloat(vals[0], n=nv) valj = sp.nfloat(vals[1], n=nv) numpr.append(vali) numpi.append(valj) reals = [] iss = [] simps = [] for i, j in zip(numpr, numpi): reale = i compe = j reals.append(reale) if compe != 0.0: iss.append(compe) simps.append(complex(i,j)) else: simps.append(reale) return rlist, simps
def test_sympy_backend_for_two_qubit_gate2(): x, y, z = symbols('x y z', real=True) E = eye(2) UPPER = Matrix([[1, 0], [0, 0]]) LOWER = Matrix([[0, 0], [0, 1]]) RX = Matrix([[cos(x / 2), -I * sin(x / 2)], [-I * sin(x / 2), cos(x / 2)]]) RY = Matrix([[cos(y / 2), -sin(y / 2)], [sin(y / 2), cos(y / 2)]]) RZ = Matrix([[cos(z / 2) - I * sin(z / 2), 0], [0, cos(z / 2) + I * sin(z / 2)]]) CRX_3 = reduce(TensorProduct, [E, UPPER, E]) + reduce(TensorProduct, [E, LOWER, RX]) CRY_4 = reduce(TensorProduct, [UPPER, E, E]) + reduce(TensorProduct, [LOWER, RY, E]) CRZ_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce(TensorProduct, [RZ, E, LOWER]) actual_9 = Circuit().crx(x)[1, 0].i[2].run(backend="sympy_unitary") assert simplify(actual_9 - CRX_3) == zeros(8) actual_10 = Circuit().cry(y)[2, 1].run(backend="sympy_unitary") assert simplify(actual_10 - CRY_4) == zeros(8) actual_11 = Circuit().crz(z)[0, 2].run(backend="sympy_unitary") m = simplify(nfloat(actual_11) - nfloat(CRZ_3))
def cfourreeks(f, T, N): """Let op! Periode tussen -T/2 en T/2!""" N += 1 omega0 = 2 * pi / T cp = (N - 1) * [0] cn = (N - 1) * [0] mag = (2 * N - 1) * [0] phase = (2 * N - 1) * [0] c0 = 1 / T * integrate(f, (t, -T / 2, T / 2)).nsimplify() mag[N - 1] = Abs(nfloat(c0)) phase[N - 1] = 0 som = c0 for n in range(1, N): cp[n - 1] = ac(f, T, n, omega0) cn[n - 1] = conjugate(cp[n - 1]) mag[N - n - 1] = 2 * Abs(nfloat(cn[n - 1])) mag[N + n - 1] = 2 * Abs(nfloat(cp[n - 1])) phase[N - n - 1] = arg(cn[n - 1]) phase[N + n - 1] = arg(cp[n - 1]) som += cp[n - 1] * exp(S.ImaginaryUnit * n * omega0 * t) + cn[ n - 1] * exp(-S.ImaginaryUnit * n * omega0 * t) cp.insert(0, c0) return som, mag, phase
def compute_lenth(p,interval): ''' 计算多项式函数的弧长 :param p: 多项式系数的 narray :param interval: 区间,传入一个 narray,包含上下限 :return: 长度 ''' x = sy.symbols('x') n = len(p) # 有多少个系数 # p_1 = np.zeros(n) # 产生多项式函数 func = 0 for i in range(n): #p_1[i] = sy.evaluate(p[i]) func += (p[i] * (x ** (n-1-i))) print('\n拟合得到的多项式函数为:') sy.pprint(func) # 用高数求弧长公式求弧长 print('\n用以下公式求弧长:') func_compute(func) # 对其求导 print('\n先对其求导,其导数为:') func1 = sy.diff(func,x) sy.pprint(func1) func2 = sy.sqrt(1 + func1**2) #sy.pprint(func2) # func3 = sy.integrate(func2,x) # sy.pprint(func3) a = sy.integrate(func2,(x,interval[0],interval[1])) # 积分函数 b = sy.nfloat(a,5) # 积分值,保留五位有效数字 print('\n这是求解该长度定积分的函数表达式:') sy.pprint(a) print('\n估计长度为:') sy.pprint(b)
def parse(string, env): ''' Parses a value. ''' if isinstance(string, bool): # If it is already a boolean, just edit it return sp.nfloat(int(string)) if isinstance(string, str): # If it is a string, parse it if '"' in string or '\'' in string: return string.strip('"').strip( '\'') # Strip quotes and parse strings else: var = env.getval( string) # If it is a variable, try and get its value try: if var: return var except ValueError: # Numpy throws this if you try to use if on an array return var try: return sp.Float(string, '') # Make it into a number except: return string # Give up return string # Give up again
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import rootof x = Symbol("x") eq = x**(S(4)/3) + 4*x**(S(1)/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(S(1)/3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**(S(4)/3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big*x), Float_big*x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 6342 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solveset(f.subs(x, -0.139))) # issue 6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 7122 eq = cos(3*x**4 + y)*rootof(x**5 + 3*x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
def test_nfloat(): from sympy.core.basic import _aresame x = Symbol("x") eq = x**(S(4) / 3) + 4 * x**(S(1) / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(S(1) / 3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**(S(4) / 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 Float_big = Float(big) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issues 3243 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solve(f.subs(x, -0.139))) # issue 3533 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import RootOf x = Symbol("x") eq = x**(S(4)/3) + 4*x**(S(1)/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(S(1)/3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**(S(4)/3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 Float_big = Float(big) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 3243 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solve(f.subs(x, -0.139))) # issue 3533 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 4023 eq = cos(3*x**4 + y)*RootOf(x**5 + 3*x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import rootof x = Symbol("x") eq = x**(S(4) / 3) + 4 * x**(S(1) / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(S(1) / 3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**(S(4) / 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big * x), Float_big * x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 6342 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solveset(f.subs(x, -0.139))) # issue 6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 7122 eq = cos(3 * x**4 + y) * rootof(x**5 + 3 * x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import RootOf x = Symbol("x") eq = x**(S(4) / 3) + 4 * x**(S(1) / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(S(1) / 3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**(S(4) / 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**(S(4) / 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 Float_big = Float(big) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 3243 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solve(f.subs(x, -0.139))) # issue 3533 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 4023 eq = cos(3 * x**4 + y) * RootOf(x**5 + 3 * x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)'
def test_issue_10395(): eq = x * Max(0, y) assert nfloat(eq) == eq eq = x * Max(y, -1.1) assert nfloat(eq) == eq assert Max(y, 4).n() == Max(4.0, y)
def test_issue_10395(): eq = x*Max(0, y) assert nfloat(eq) == eq eq = x*Max(y, -1.1) assert nfloat(eq) == eq assert Max(y, 4).n() == Max(4.0, y)
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import rootof x = Symbol("x") eq = x**Rational(4, 3) + 4 * x**(S.One / 3) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * x**(S.One / 3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**Rational(4, 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big * x), Float_big * x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 6342 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solveset(f.subs(x, -0.139))) # issue 6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 7122 eq = cos(3 * x**4 + y) * rootof(x**5 + 3 * x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)' # issue 10933 for ti in (dict, Dict): d = ti({S.Half: S.Half}) n = nfloat(d) assert isinstance(n, ti) assert _aresame(list(n.items()).pop(), (S.Half, Float(.5))) for ti in (dict, Dict): d = ti({S.Half: S.Half}) n = nfloat(d, dkeys=True) assert isinstance(n, ti) assert _aresame(list(n.items()).pop(), (Float(.5), Float(.5))) d = [S.Half] n = nfloat(d) assert type(n) is list assert _aresame(n[0], Float(.5)) assert _aresame(nfloat(Eq(x, S.Half)).rhs, Float(.5)) assert _aresame(nfloat(S(True)), S(True)) assert _aresame(nfloat(Tuple(S.Half))[0], Float(.5)) assert nfloat(Eq((3 - I)**2 / 2 + I, 0)) == S.false # pass along kwargs assert nfloat([{S.Half: x}], dkeys=True) == [{Float(0.5): x}] # Issue 17706 A = MutableMatrix([[1, 2], [3, 4]]) B = MutableMatrix( [[Float('1.0', precision=53), Float('2.0', precision=53)], [Float('3.0', precision=53), Float('4.0', precision=53)]]) assert _aresame(nfloat(A), B) A = ImmutableMatrix([[1, 2], [3, 4]]) B = ImmutableMatrix( [[Float('1.0', precision=53), Float('2.0', precision=53)], [Float('3.0', precision=53), Float('4.0', precision=53)]]) assert _aresame(nfloat(A), B)
def test_nfloat(): from sympy.core.basic import _aresame x = Symbol("x") eq = x**(S(4)/3) + 4*x**(S(1)/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(S(1)/3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0/3) + (4.0/3)*x**(1.0/3)) eq = x**(S(4)/3) + 4*x**(x/3)/3 assert _aresame(nfloat(eq), x**(S(4)/3) + (4.0/3)*x**(x/3)) big = 12345678901234567890 Float_big = Float(big) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert _aresame(nfloat(big), Float_big) assert nfloat({x: sqrt(2)}) == {x: nfloat(sqrt(2))} assert nfloat({sqrt(2): x}) == {sqrt(2): x} assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issues 3243 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solve(f.subs(x, -0.139))) # issue 3533 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11