本文整理汇总了Python中fractions.Fraction.expand方法的典型用法代码示例。如果您正苦于以下问题:Python Fraction.expand方法的具体用法?Python Fraction.expand怎么用?Python Fraction.expand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fractions.Fraction
的用法示例。
在下文中一共展示了Fraction.expand方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cubic_sym_span_matrix
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import expand [as 别名]
def cubic_sym_span_matrix():
"""
Compute a symbolic span matrix for B-Splines.
Based on "A practical review of uniform b-splines" by Kristin Branson
Notation used: [1 s s^2 s^3] Bi Pi^T
See: http://vision.ucsd.edu/~kbranson/research/bsplines.html
"""
# Symbolic variables
i = sp.Symbol('i')
s = sp.Symbol('s')
P = sp.MatrixSymbol('p', 4, 1)
# Use the result given in the paper
# TODO: implement the part that leads to this result
res = Fraction(1,6)*( (1-(s-i))**3 * P[0,0]
+ (3*(s-i)**3 - 6*(s-i)**2 + 4) * P[1,0]
+ (-3*(s-i)**3 + 3*(s-i)**2 + 3*(s-i) + 1) * P[2,0]
+ (s-i)**3 * P[3,0]
)
res = res.expand()
# Handle as a symbolic polynomial
res = sp.Poly(res, P[0,0], P[1,0], P[2,0], P[3,0])
# Span matrix Bi
Bi = sp.Matrix(4,4, lambda i,j: 0)
# For each column (control points Pj)
for col in xrange(4):
basis = sp.zeros(4)
basis[col] = 1
# Substitute by the right vector to get the proper coefficient
# (there may be a better way to achieve that with sympy)
p_ctrl_pt = res.subs( {P[0,0]: basis[0],
P[1,0]: basis[1],
P[2,0]: basis[2],
P[3,0]: basis[3]})
coeffs = sp.Poly(p_ctrl_pt, s).as_poly().all_coeffs()[::-1]
# For each row (s^k)
for row in xrange(len(coeffs)):
# Store the factorized result in Bi
Bi[row,col] = coeffs[row].factor(i)
return Bi