def __pow__(self, other):
if self.data is None:
raise ValueError("No power without ndarray data.")
numpy = import_module('numpy')
free = self.free
marray = self.data
for metric in free:
marray = numpy.tensordot(
marray,
numpy.tensordot(
metric[0]._tensortype.data,
marray,
(1, 0)
),
(0, 0)
)
pow2 = marray[()]
return pow2 ** (Rational(1, 2) * other)
python类Rational()的实例源码
def test_legendre_poly():
raises(ValueError, lambda: legendre_poly(-1, x))
assert legendre_poly(1, x, polys=True) == Poly(x)
assert legendre_poly(0, x) == 1
assert legendre_poly(1, x) == x
assert legendre_poly(2, x) == Q(3, 2)*x**2 - Q(1, 2)
assert legendre_poly(3, x) == Q(5, 2)*x**3 - Q(3, 2)*x
assert legendre_poly(4, x) == Q(35, 8)*x**4 - Q(30, 8)*x**2 + Q(3, 8)
assert legendre_poly(5, x) == Q(63, 8)*x**5 - Q(70, 8)*x**3 + Q(15, 8)*x
assert legendre_poly(6, x) == Q(
231, 16)*x**6 - Q(315, 16)*x**4 + Q(105, 16)*x**2 - Q(5, 16)
assert legendre_poly(1).dummy_eq(x)
assert legendre_poly(1, polys=True) == Poly(x)
def test_laguerre_poly():
raises(ValueError, lambda: laguerre_poly(-1, x))
assert laguerre_poly(1, x, polys=True) == Poly(-x + 1)
assert laguerre_poly(0, x) == 1
assert laguerre_poly(1, x) == -x + 1
assert laguerre_poly(2, x) == Q(1, 2)*x**2 - Q(4, 2)*x + 1
assert laguerre_poly(3, x) == -Q(1, 6)*x**3 + Q(9, 6)*x**2 - Q(18, 6)*x + 1
assert laguerre_poly(4, x) == Q(
1, 24)*x**4 - Q(16, 24)*x**3 + Q(72, 24)*x**2 - Q(96, 24)*x + 1
assert laguerre_poly(5, x) == -Q(1, 120)*x**5 + Q(25, 120)*x**4 - Q(
200, 120)*x**3 + Q(600, 120)*x**2 - Q(600, 120)*x + 1
assert laguerre_poly(6, x) == Q(1, 720)*x**6 - Q(36, 720)*x**5 + Q(450, 720)*x**4 - Q(2400, 720)*x**3 + Q(5400, 720)*x**2 - Q(4320, 720)*x + 1
assert laguerre_poly(0, x, a) == 1
assert laguerre_poly(1, x, a) == -x + a + 1
assert laguerre_poly(2, x, a) == x**2/2 + (-a - 2)*x + a**2/2 + 3*a/2 + 1
assert laguerre_poly(3, x, a) == -x**3/6 + (a/2 + Q(
3)/2)*x**2 + (-a**2/2 - 5*a/2 - 3)*x + a**3/6 + a**2 + 11*a/6 + 1
assert laguerre_poly(1).dummy_eq(-x + 1)
assert laguerre_poly(1, polys=True) == Poly(-x + 1)
def test_triangle_orth_exact():
x = numpy.array([Rational(1, 3), Rational(1, 7)])
L = 2
exacts = [
[sympy.sqrt(2)],
[-Rational(8, 7), 8*sympy.sqrt(3)/21],
[-197*sympy.sqrt(6)/441,
-136*sympy.sqrt(2)/147,
-26*sympy.sqrt(30)/441],
]
bary = numpy.array([
x[0], x[1], 1-x[0]-x[1]
])
vals = orthopy.triangle.tree(L, bary, 'normal', symbolic=True)
for val, ex in zip(vals, exacts):
for v, e in zip(val, ex):
assert v == e
return
def test_triangle_orth_1_exact():
x = numpy.array([
[Rational(1, 5), Rational(2, 5), Rational(3, 5)],
[Rational(1, 7), Rational(2, 7), Rational(3, 7)],
])
L = 2
exacts = [
[[1, 1, 1]],
[
[-Rational(34, 35), Rational(2, 35), Rational(38, 35)],
[Rational(2, 35), Rational(4, 35), Rational(6, 35)]
],
]
bary = numpy.array([
x[0], x[1], 1-x[0]-x[1]
])
vals = orthopy.triangle.tree(L, bary, '1', symbolic=True)
for val, ex in zip(vals, exacts):
for v, e in zip(val, ex):
assert numpy.all(v == e)
return
def __init__(self, n):
self.degree = 3
data = [
(fr(3-n, 3), z(n)),
(fr(1, 6), fsd(n, (1, 1))),
]
self.points, self.weights = untangle(data)
reference_volume = 2**n
self.weights *= reference_volume
return
def __init__(self, n):
self.degree = 3
data = [
(fr(2, 3), z(n)),
(fr(1, 3 * 2**n), pm(n, 1)),
]
self.points, self.weights = untangle(data)
reference_volume = 2**n
self.weights *= reference_volume
return
def __init__(self, n, index):
self.dim = n
if index == 2:
self.degree = 2
r = sqrt(3) / 6
data = [
(1.0, numpy.array([numpy.full(n, 2*r)])),
(+r, _s(n, -1, r)),
(-r, _s(n, +1, r)),
]
else:
assert index == 3
self.degree = 3
n2 = n // 2 if n % 2 == 0 else (n-1)//2
i_range = range(1, 2*n+1)
pts = [[
[sqrt(fr(2, 3)) * cos((2*k-1)*i*pi / n) for i in i_range],
[sqrt(fr(2, 3)) * sin((2*k-1)*i*pi / n) for i in i_range],
] for k in range(1, n2+1)]
if n % 2 == 1:
sqrt3pm = numpy.full(2*n, 1/sqrt(3))
sqrt3pm[1::2] *= -1
pts.append(sqrt3pm)
pts = numpy.vstack(pts).T
data = [(fr(1, 2*n), pts)]
self.points, self.weights = untangle(data)
reference_volume = 2**n
self.weights *= reference_volume
return
def __init__(self, n):
self.degree = 5
r = sqrt(fr(2, 5))
data = [
(fr(8 - 5*n, 9), z(n)),
(fr(5, 18), fsd(n, (r, 1))),
(fr(1, 9 * 2**n), pm(n, 1)),
]
self.points, self.weights = untangle(data)
reference_volume = 2**n
self.weights *= reference_volume
return
def __init__(self, index):
if index == '1-2':
self.degree = 3
data = [
(1, fsd(2, (sqrt(fr(2, 3)), 1)))
]
elif index == '2-2':
self.degree = 5
alpha = sqrt(fr(3, 5))
data = [
(fr(64, 81), z(2)),
(fr(40, 81), fsd(2, (alpha, 1))),
(fr(25, 81), pm(2, alpha)),
]
else:
assert index == '3-2'
self.degree = 7
alpha = sqrt(fr(3, 5))
xi1, xi2 = [
sqrt(fr(3, 287) * (38 - i*sqrt(583)))
for i in [+1, -1]
]
data = [
(fr(98, 405), fsd(2, (sqrt(fr(6, 7)), 1))),
(0.5205929166673945, pm(2, xi1)),
(0.2374317746906302, pm(2, xi2)),
]
self.points, self.weights = untangle(data)
return
def __init__(self):
self.name = 'Burnside'
self.degree = 5
r = sqrt(fr(7, 15))
s = sqrt(fr(7, 9))
data = [
(fr(10, 49), _symm_r_0(r)),
(fr(9, 196), _symm_s(s)),
]
self.points, self.weights = untangle(data)
self.weights *= 4
return
def __init__(self):
self.degree = 7
sqrt17770 = sqrt(17770)
r, s = [
sqrt((1715 - plus_minus * 7 * sqrt17770) / 2817)
for plus_minus in [+1, -1]
]
t = sqrt(fr(7, 18))
u = sqrt(fr(7, 27))
B1, B2 = [
(2965 * sqrt17770 + plus_minus * 227816) / 72030 / sqrt17770
for plus_minus in [+1, -1]
]
B3 = fr(324, 12005)
B4 = fr(2187, 96040)
data = [
(B1, fsd(3, (r, 1))),
(B2, fsd(3, (s, 1))),
(B3, fsd(3, (t, 2))),
(B4, pm(3, u)),
]
self.points, self.weights = untangle(data)
self.weights *= fr(4, 3) * pi
return
def __init__(self):
self.degree = 3
data = [
(fr(1, 4), z()),
(fr(1, 12), fs_r00(1)),
(fr(1, 48), fs_rr0(1)),
]
self.points, self.weights = untangle(data)
self.weights *= 8
return
def __init__(self):
self.degree = 5
sqrt19 = sqrt(19)
t = sqrt(71440 + 6802 * sqrt19)
lmbd, gmma = [
sqrt((1919 - 148*sqrt19 + i * 4*t) / 3285)
for i in [+1, -1]
]
xi, mu = [
-sqrt((1121 + 74*sqrt19 - i * 2*t) / 3285)
for i in [+1, -1]
]
mu *= -1
B, C = [
133225 / (260072 - 1520*sqrt19 + i*(133 - 37*sqrt19)*t)
for i in [+1, -1]
]
data = [
(fr(32, 19), z()),
(B, rss_pm(lmbd, xi)),
(C, rss_pm(gmma, mu)),
]
self.points, self.weights = untangle(data)
return
def __init__(self):
self.degree = 5
data = [
(fr(91, 450), fs_r00(1)),
(fr(-20, 225), fs_rr0(1)),
(fr(8, 225), fs_rrs(sqrt(fr(5, 8)), 1)),
]
self.points, self.weights = untangle(data)
self.weights *= 8
return
def viii():
nu = sqrt(30)
eta = sqrt(10)
A = fr(3, 5)
B = fr(2, 75)
C = fr(3, 100)
data = [
(A, numpy.array([[0, 0, 0]])),
(B, fsd(3, (nu, 1))),
(C, pm(3, eta)),
]
return 5, data
def ix():
eta = sqrt(10)
xi, nu = [sqrt(15 - p_m * 5 * sqrt(5)) for p_m in [+1, -1]]
A = fr(3, 5)
B = fr(1, 50)
data = [
(A, numpy.array([[0, 0, 0]])),
(B, pm(3, eta)),
(B, pm_roll(3, [xi, nu])),
]
return 5, data
def viiia():
r = sqrt(fr(5, 4))
s = sqrt(fr(5, 2))
data = [
(fr(4, 25), fsd(3, (r, 1))),
(fr(1, 200), pm(3, s)),
]
return 5, data
def viiib():
r = sqrt(fr(5, 2))
s = sqrt(fr(5, 6))
data = [
(fr(2, 5), numpy.array([[0, 0, 0]])),
(fr(1, 25), fsd(3, (r, 1))),
(fr(9, 200), pm(3, s)),
]
return 5, data
def __init__(self, n, index):
self.dim = n
if index == 'I':
self.degree = 2
data = [
(fr(1, n+1), sqrt(fr(1, 2)) * _nsimplex(n))
]
elif index == 'II':
self.degree = 3
nu = sqrt(fr(n, 2))
data = [
(fr(1, 2*n), fsd(n, (nu, 1)))
]
elif index == 'III':
self.degree = 3
nu = sqrt(fr(1, 2))
data = [
(fr(1, 2**n), pm(n, nu))
]
else:
assert index == 'IV'
self.degree = 5
nu = sqrt(fr(n+2, 2))
xi = sqrt(fr(n+2, 4))
A = fr(2, n+2)
B = fr(4-n, 2 * (n+2)**2)
C = fr(1, (n+2)**2)
data = [
(A, numpy.full((1, n), 0)),
(B, fsd(n, (nu, 1))),
(C, fsd(n, (xi, 2))),
]
self.points, self.weights = untangle(data)
self.weights *= sqrt(pi)**n
return