本文整理汇总了Python中sympy.abs函数的典型用法代码示例。如果您正苦于以下问题:Python abs函数的具体用法?Python abs怎么用?Python abs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了abs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Theta_Equation
def Theta_Equation(self, l="undef", m="undef", theta=theta):
""" l - orbital quantum number
m - magnetic quantum number """
if l == "undef":
l = self.l_val
if m == "undef":
m = self.m_val
# Prevents integer division and float l,m
l = int(l)
m = int(m)
mode = self.select_exec_mode(theta)
gL = self.Generalized_Legendre(l, m, theta).subs(theta, sympy.cos(theta))
if gL:
rat_part = sympy.sqrt(
Rational((2 * l + 1), 2) * Rational(factorial(l - sympy.abs(m)), factorial(l + sympy.abs(m)))
)
if mode == "numer":
return (rat_part * gL).evalf()
elif mode == "analit":
return (rat_part * gL).expand()
return False
示例2: test_latex_functions
def test_latex_functions():
assert latex(exp(x)) == "$e^{x}$"
assert latex(exp(1) + exp(2)) == "$e + e^{2}$"
f = Function("f")
assert latex(f(x)) == "$\\operatorname{f}\\left(x\\right)$"
beta = Function("beta")
assert latex(beta(x)) == r"$\operatorname{beta}\left(x\right)$"
assert latex(sin(x)) == r"$\operatorname{sin}\left(x\right)$"
assert latex(sin(x), fold_func_brackets=True) == r"$\operatorname{sin}x$"
assert latex(sin(2 * x ** 2), fold_func_brackets=True) == r"$\operatorname{sin}2 x^{2}$"
assert latex(sin(x ** 2), fold_func_brackets=True) == r"$\operatorname{sin}x^{2}$"
assert latex(asin(x) ** 2) == r"$\operatorname{asin}^{2}\left(x\right)$"
assert latex(asin(x) ** 2, inv_trig_style="full") == r"$\operatorname{arcsin}^{2}\left(x\right)$"
assert latex(asin(x) ** 2, inv_trig_style="power") == r"$\operatorname{sin}^{-1}\left(x\right)^{2}$"
assert latex(asin(x ** 2), inv_trig_style="power", fold_func_brackets=True) == r"$\operatorname{sin}^{-1}x^{2}$"
assert latex(factorial(k)) == r"$k!$"
assert latex(factorial(-k)) == r"$\left(- k\right)!$"
assert latex(floor(x)) == r"$\lfloor{x}\rfloor$"
assert latex(ceiling(x)) == r"$\lceil{x}\rceil$"
assert latex(abs(x)) == r"$\lvert{x}\rvert$"
assert latex(re(x)) == r"$\Re{x}$"
assert latex(im(x)) == r"$\Im{x}$"
assert latex(conjugate(x)) == r"$\overline{x}$"
assert latex(gamma(x)) == r"$\operatorname{\Gamma}\left(x\right)$"
assert latex(Order(x)) == r"$\operatorname{\mathcal{O}}\left(x\right)$"
示例3: g
def g(yieldCurve, zeroRates,n, verbose):
'''
generates recursively the zero curve
expressions eval('(0.06/1.05)+(1.06/(1+x)**2)-1')
solves these expressions to get the new rate
for that period
'''
if len(zeroRates) >= len(yieldCurve):
print "\n\n\t+zero curve boot strapped [%d iterations]" % (n)
return
else:
legn = ''
for i in range(0,len(zeroRates),1):
if i == 0:
legn = '%2.6f/(1+%2.6f)**%d'%(yieldCurve[n], zeroRates[i],i+1)
else:
legn = legn + ' +%2.6f/(1+%2.6f)**%d'%(yieldCurve[n], zeroRates[i],i+1)
legn = legn + '+ (1+%2.6f)/(1+x)**%d-1'%(yieldCurve[n], n+1)
# solve the expression for this iteration
if verbose:
print "-[%d] %s" % (n, legn.strip())
rate1 = solve(eval(legn), x)
# Abs here since some solutions can be complex
rate1 = min([Real(abs(r)) for r in rate1])
if verbose:
print "-[%d] solution %2.6f" % (n, float(rate1))
# stuff the new rate in the results, will be
# used by the next iteration
zeroRates.append(rate1)
g(yieldCurve, zeroRates,n+1, verbose)
示例4: run_Lagrange_interp_abs_conv
def run_Lagrange_interp_abs_conv(N=[3, 6, 12, 24]):
f = sm.abs(1-2*x)
f = sm.sin(2*sm.pi*x)
fn = sm.lambdify([x], f, modules='numpy')
resolution = 50001
import numpy as np
xcoor = np.linspace(0, 1, resolution)
fcoor = fn(xcoor)
Einf = []
E2 = []
h = []
for _N in N:
psi, points = Lagrange_polynomials_01(x, _N)
u = interpolation(f, psi, points)
un = sm.lambdify([x], u, modules='numpy')
ucoor = un(xcoor)
e = fcoor - ucoor
Einf.append(e.max())
E2.append(np.sqrt(np.sum(e*e/e.size)))
h.append(1./_N)
print Einf
print E2
print h
print N
# Assumption: error = CN**(-N)
print 'convergence rates:'
for i in range(len(E2)):
C1 = E2[i]/(N[i]**(-N[i]/2))
C2 = Einf[i]/(N[i]**(-N[i]/2))
print N[i], C1, C2
示例5: test_latex_functions
def test_latex_functions():
assert latex(exp(x)) == "$e^{x}$"
assert latex(exp(1)+exp(2)) == "$e + e^{2}$"
f = Function('f')
assert latex(f(x)) == '$\\operatorname{f}\\left(x\\right)$'
beta = Function('beta')
assert latex(beta(x)) == r"$\operatorname{beta}\left(x\right)$"
assert latex(sin(x)) == r"$\operatorname{sin}\left(x\right)$"
assert latex(sin(x), fold_func_brackets=True) == r"$\operatorname{sin}x$"
assert latex(sin(2*x**2), fold_func_brackets=True) == \
r"$\operatorname{sin}2 x^{2}$"
assert latex(factorial(k)) == r"$k!$"
assert latex(factorial(-k)) == r"$\left(- k\right)!$"
assert latex(floor(x)) == r"$\lfloor{x}\rfloor$"
assert latex(ceiling(x)) == r"$\lceil{x}\rceil$"
assert latex(abs(x)) == r"$\lvert{x}\rvert$"
assert latex(re(x)) == r"$\Re{x}$"
assert latex(im(x)) == r"$\Im{x}$"
assert latex(conjugate(x)) == r"$\overline{x}$"
assert latex(gamma(x)) == r"$\operatorname{\Gamma}\left(x\right)$"
assert latex(Order(x)) == r"$\operatorname{\mathcal{O}}\left(x\right)$"
示例6: sp_derive
def sp_derive():
import sympy as sp
vars = 'G_s, s_n, s_p_n, w_n, dw_n, ds_n, G_s, G_w, c, phi'
syms = sp.symbols(vars)
for var, sym in zip(vars.split(','), syms):
globals()[var.strip()] = sym
s_n1 = s_n + ds_n
w_n1 = w_n + dw_n
tau_trial = G_s * (s_n1 - s_p_n)
print 'diff', sp.diff(tau_trial, ds_n)
print tau_trial
sig_n1 = G_w * w_n1
print sig_n1
tau_fr = (c + sig_n1 * sp.tan(phi)) * sp.Heaviside(sig_n1 - c / sp.tan(phi))
print tau_fr
d_tau_fr = sp.diff(tau_fr, dw_n)
print d_tau_fr
f_trial = sp.abs(tau_trial) - tau_fr
print f_trial
d_gamma = f_trial / G_s
print 'd_gamma'
sp.pretty_print(d_gamma)
print 'd_gamma_s'
sp.pretty_print(sp.diff(d_gamma, ds_n))
print 'tau_n1'
tau_n1 = sp.simplify(tau_trial - d_gamma * G_s * sp.sign(tau_trial))
sp.pretty_print(tau_n1)
print 'dtau_n1_w'
dtau_n1_w = sp.diff(tau_n1, dw_n)
sp.pretty_print(dtau_n1_w)
print 'dtau_n1_s'
dtau_n1_s = sp.diff(d_gamma * sp.sign(tau_trial), ds_n)
print dtau_n1_s
s_p_n1 = s_p_n + d_gamma * sp.sign(tau_trial)
print s_p_n1
示例7: chop
def chop(expr, tol = 1e-10):
"""suppress small numerical values"""
expr = sp.expand(sp.sympify(expr))
if expr.is_Symbol: return expr
assert expr.is_Add
return sp.Add(*[term for term in expr.as_Add() if sp.abs(term.as_coeff_terms()[0]) >= tol])
示例8: run_Lagrange_leastsq_abs
def run_Lagrange_leastsq_abs(N):
"""Least-squares with of Lagrange polynomials for |1-2x|."""
f = sm.abs(1-2*x)
# This f will lead to failure of sympy integrate, fallback on numerical int.
psi, points = Lagrange_polynomials_01(x, N)
Omega=[0, 1]
u = least_squares(f, psi, Omega)
comparison_plot(f, u, Omega, filename='Lagrange_ls_abs_%d.pdf' % (N+1),
plot_title='Least squares approximation by '\
'Lagrange polynomials of degree %d' % N)
示例9: abs
def abs(*args, **kw):
arg0 = args[0]
if isinstance(arg0, (int, float, long)):
return _math.abs(*args,**kw)
elif isinstance(arg0, complex):
return _cmath.abs(*args,**kw)
elif isinstance(arg0, _sympy.Basic):
return _sympy.abs(*args,**kw)
else:
return _numpy.abs(*args,**kw)
示例10: Generalized_Legendre
def Generalized_Legendre(self, n=0, m=0, x=x):
mode = self.select_exec_mode(x)
if mode != "undef":
legendre_polinomial = self.Legendre(n=n, x=x)
rat_part = (1 - (x ** 2)) ** (sympy.abs(m) / 2.0)
diff_part = sympy.diff(legendre_polinomial, x, abs(m))
return (rat_part * diff_part).expand()
else:
return False
示例11: Phi_Equation
def Phi_Equation(self, m="undef", phi=phi):
if m == "undef":
m = self.m_val
m = int(m)
mode = self.select_exec_mode(phi)
if mode == "numer":
phi_val = self.phi
self.phi = Symbol("phi")
elif mode == "custom_var":
self.phi = Symbol(phi)
elif mode == "undef":
return False
left_part = 1 / sympy.sqrt(2 * sympy.pi)
right_part = sympy.cos(sympy.abs(m) * self.phi) if m >= 0 else sympy.sin(sympy.abs(m) * self.phi)
if mode == "numer":
return (left_part * right_part).evalf()
elif mode == "analit" or mode == "custom_var":
return (left_part * right_part).expand()
示例12: l2_norm
def l2_norm(f, lim):
"""
Calculates L2 norm of the function "f", over the domain lim=(x, a, b).
x ...... the independent variable in f over which to integrate
a, b ... the limits of the interval
Example:
>>> l2_norm(1, (x, -1, 1))
2**(1/2)
>>> l2_norm(x, (x, -1, 1))
1/3*6**(1/2)
"""
return sqrt(integrate(abs(f)**2, lim))
示例13: test_maxima_functions
def test_maxima_functions():
assert parse_maxima('expand( (x+1)^2)') == x**2 + 2*x + 1
assert parse_maxima('factor( x**2 + 2*x + 1)') == (x+1)**2
assert parse_maxima('trigsimp(2*cos(x)^2 + sin(x)^2)') == 2 - sin(x)**2
assert parse_maxima('trigexpand(sin(2*x)+cos(2*x))') == (-1) + 2*cos(x)**2 + 2*cos(x)*sin(x)
assert parse_maxima('solve(x^2-4,x)') == [2, -2]
assert parse_maxima('limit((1+1/x)^x,x,inf)') == E
assert parse_maxima('limit(sqrt(-x)/x,x,0,minus)') == -oo
assert parse_maxima('diff(x^x, x)') == x**x*(1 + log(x))
assert parse_maxima('sum(k, k, 1, n)') == (n**2 +n)/2
assert parse_maxima('product(k, k, 1, n)',
name_dict=dict(
n=Symbol('n',integer=True),
k=Symbol('k',integer=True)
)
) == factorial(n)
assert parse_maxima('ratsimp((x^2-1)/(x+1))') == x-1
assert abs( parse_maxima('float(sec(%pi/3) + csc(%pi/3))') - 3.154700538379252) < 10**(-5)
示例14: test_latex_functions
def test_latex_functions():
assert latex(exp(x)) == "e^{x}"
assert latex(exp(1)+exp(2)) == "e + e^{2}"
f = Function('f')
assert latex(f(x)) == '\\operatorname{f}\\left(x\\right)'
beta = Function('beta')
assert latex(beta(x)) == r"\operatorname{beta}\left(x\right)"
assert latex(sin(x)) == r"\operatorname{sin}\left(x\right)"
assert latex(sin(x), fold_func_brackets=True) == r"\operatorname{sin}x"
assert latex(sin(2*x**2), fold_func_brackets=True) == \
r"\operatorname{sin}2 x^{2}"
assert latex(sin(x**2), fold_func_brackets=True) == \
r"\operatorname{sin}x^{2}"
assert latex(asin(x)**2) == r"\operatorname{asin}^{2}\left(x\right)"
assert latex(asin(x)**2,inv_trig_style="full") == \
r"\operatorname{arcsin}^{2}\left(x\right)"
assert latex(asin(x)**2,inv_trig_style="power") == \
r"\operatorname{sin}^{-1}\left(x\right)^{2}"
assert latex(asin(x**2),inv_trig_style="power",fold_func_brackets=True) == \
r"\operatorname{sin}^{-1}x^{2}"
assert latex(factorial(k)) == r"k!"
assert latex(factorial(-k)) == r"\left(- k\right)!"
assert latex(floor(x)) == r"\lfloor{x}\rfloor"
assert latex(ceiling(x)) == r"\lceil{x}\rceil"
assert latex(abs(x)) == r"\lvert{x}\rvert"
assert latex(re(x)) == r"\Re{x}"
assert latex(im(x)) == r"\Im{x}"
assert latex(conjugate(x)) == r"\overline{x}"
assert latex(gamma(x)) == r"\operatorname{\Gamma}\left(x\right)"
assert latex(Order(x)) == r"\operatorname{\mathcal{O}}\left(x\right)"
示例15: test_parser
def test_parser():
assert abs(parse_maxima('float(1/3)') - 0.333333333) < 10**(-5)
assert parse_maxima('13^26') == 91733330193268616658399616009
assert parse_maxima('sin(%pi/2) + cos(%pi/3)') == Rational(3,2)
assert parse_maxima('log(%e)') == 1