Python sympy.re() Examples

The following are 2 code examples of sympy.re(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sympy , or try the search function .
Example #1
Source File: opt.py    From Blueqat with Apache License 2.0 6 votes vote down vote up
def qubo_to_matrix(self,qubo):
		try:
			import sympy
		except ImportError:
			raise ImportError("optm() requires sympy. Please install before call this function.") 

		qubo = self.expand_qubo(qubo)
		numN = len(qubo.free_symbols)
		optm = np.zeros((numN,numN)) 

		for i in qubo.free_symbols: 
			for j in qubo.free_symbols: 
				if(i!=j):
					optm[int(repr(i)[1:])][int(repr(j)[1:])] = qubo.coeff(i*j)
				else:
					f2 =sympy.re(qubo.coeff(i))
					for k in qubo.free_symbols:
						f2 = f2.subs(k,0)
					optm[int(repr(i)[1:])][int(repr(i)[1:])] = f2

		return np.triu(optm) 
Example #2
Source File: test_recursion.py    From spherical_functions with MIT License 5 votes vote down vote up
def test_WignerDRecursion_accuracy():
    from sympy.physics.quantum.spin import WignerD as sympyWignerD

    """Eq. (29) of arxiv:1403.7698: d^{m',m}_{n}(β) = ϵ(m') ϵ(-m) H^{m',m}_{n}(β)"""

    def ϵ(m):
        m = np.asarray(m)
        eps = np.ones_like(m)
        eps[m >= 0] = (-1)**m[m >= 0]
        return eps

    ell_max = 4
    alpha, beta, gamma = 0.0, 0.1, 0.0
    hcalc = HCalculator(ell_max)
    Hnmpm = hcalc(np.cos(beta))
    max_error = 0.0
    # errors = np.empty(hcalc.nmpm_total_size)

    i = 0
    print()
    for n in range(hcalc.n_max+1):
        print('Testing n={} compared to sympy'.format(n))
        for mp in range(-n, n+1):
            for m in range(-n, n+1):
                sympyd = sympy.re(sympy.N(sympyWignerD(n, mp, m, alpha, -beta, gamma).doit()))
                myd = ϵ(mp) * ϵ(-m) * Hnmpm[i]
                error = float(min(abs(sympyd+myd), abs(sympyd-myd)))
                assert error < 1e-14, "Testing Wigner d recursion: n={}, m'={}, m={}, v1={}, v2={}, error={}".format(n, mp, m, sympyd, myd, error)
                max_error = max(error, max_error)
                # errors[i] = float(min(abs(sympyd+myd), abs(sympyd-myd)))
                # print("{:>5} {:>5} {:>5} {:24} {:24} {:24}".format(n, mp, m, float(sympyd), myd, errors[i]))
                i += 1

    print("Testing Wigner d recursion: max error = {}".format(max_error))