当前位置: 首页>>代码示例>>Python>>正文


Python Fraction.expand方法代码示例

本文整理汇总了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
开发者ID:bchretien,项目名称:Python-sandbox,代码行数:48,代码来源:bspline_utils.py


注:本文中的fractions.Fraction.expand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。