示例#1
0
def triangle01_poly_integral(d, p):

    #*****************************************************************************80
    #
    ## TRIANGLE01_POLY_INTEGRAL: polynomial integral over the unit triangle.
    #
    #  Location:
    #
    #    http://people.sc.fsu.edu/~jburkardt/py_src/triangle_integrals/triangle01_poly_integral.py
    #
    #  Discussion:
    #
    #    The unit triangle is T = ( (0,0), (1,0), (0,1) ).
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    23 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer D, the degree of the polynomial.
    #
    #    Input, real P(M), the polynomial coefficients.
    #    M = ((D+1)*(D+2))/2.
    #
    #    Output, real Q, the integral.
    #
    from i4_to_pascal import i4_to_pascal
    from triangle01_monomial_integral import triangle01_monomial_integral

    m = ((d + 1) * (d + 2)) / 2

    q = 0.0
    for k in range(1, m + 1):
        km1 = k - 1
        i, j = i4_to_pascal(k)
        qk = triangle01_monomial_integral(i, j)
        q = q + p[km1] * qk

    return q
def triangle01_poly_integral ( d, p ):

#*****************************************************************************80
#
## TRIANGLE01_POLY_INTEGRAL: polynomial integral over the unit triangle.
#
#  Location:
#
#    http://people.sc.fsu.edu/~jburkardt/py_src/triangle_integrals/triangle01_poly_integral.py
#
#  Discussion:
#
#    The unit triangle is T = ( (0,0), (1,0), (0,1) ).
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    23 April 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer D, the degree of the polynomial.
#
#    Input, real P(M), the polynomial coefficients.
#    M = ((D+1)*(D+2))/2.
#
#    Output, real Q, the integral.
#
  from i4_to_pascal import i4_to_pascal
  from triangle01_monomial_integral import triangle01_monomial_integral

  m = ( ( d + 1 ) * ( d + 2 ) ) / 2

  q = 0.0
  for k in range ( 1, m + 1 ):
    km1 = k - 1
    i, j = i4_to_pascal ( k )
    qk = triangle01_monomial_integral ( i, j )
    q = q + p[km1] * qk

  return q
示例#3
0
def triangle01_poly_integral_test():

    #*****************************************************************************80
    #
    ## TRIANGLE01_POLY_INTEGRAL_TEST: polynomial integrals over the unit triangle.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    23 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    import numpy as np
    from i4_to_pascal import i4_to_pascal
    from poly_print import poly_print
    from triangle01_monomial_integral import triangle01_monomial_integral

    d_max = 6
    k_max = ((d_max + 1) * (d_max + 2)) / 2
    qm = np.zeros(k_max)
    for k in range(1, k_max + 1):
        km1 = k - 1
        i, j = i4_to_pascal(k)
        qm[km1] = triangle01_monomial_integral(i, j)

    print ''
    print 'TRIANGLE01_POLY_INTEGRAL_TEST'
    print '  TRIANGLE01_POLY_INTEGRAL returns the integral Q of'
    print '  a polynomial P(X,Y) over the interior of the unit triangle.'

    d = 1
    m = ((d + 1) * (d + 2)) / 2
    p = np.array([1.0, 2.0, 3.0])
    print ''
    poly_print(d, p, '  p(x,y)')
    q = triangle01_poly_integral(d, p)
    print ''
    print '  Q =         %g' % (q)
    q2 = np.dot(p, qm[0:m])
    print '  Q (exact) = %g' % (q2)

    d = 2
    m = ((d + 1) * (d + 2)) / 2
    p = np.array([0.0, 0.0, 0.0, 0.0, 1.0, 0.0])
    print ''
    poly_print(d, p, '  p(x,y)')
    q = triangle01_poly_integral(d, p)
    print ''
    print '  Q =         %g' % (q)
    q2 = np.dot(p, qm[0:m])
    print '  Q (exact) = %g' % (q2)

    d = 2
    m = ((d + 1) * (d + 2)) / 2
    p = np.array([1.0, -2.0, 3.0, -4.0, 5.0, -6.0])
    print ''
    poly_print(d, p, '  p(x,y)')
    q = triangle01_poly_integral(d, p)
    print ''
    print '  Q =         %g' % (q)
    q2 = np.dot(p, qm[0:m])
    print '  Q (exact) = %g' % (q2)
    #
    #  Terminate.
    #
    print ''
    print 'TRIANGLE01_POLY_INTEGRAL_TEST'
    print '  Normal end of execution.'

    return
示例#4
0
def poly_print ( d, p, title ):

#*****************************************************************************80
#
## POLY_PRINT prints an XY polynomial.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    17 April 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer D, the degree of the polynomial.
#
#    Output, real P(M), the coefficients of all monomials of degree 0 through D.
#    P must contain ((D+1)*(D+2))/2 entries.
#
#    Input, string TITLE, a title string.
#
  from i4_to_pascal import i4_to_pascal

  m = ( ( d + 1 ) * ( d + 2 ) ) / 2

  allzero = True

  for i in range ( 0, m ):
    if ( p[i] != 0.0 ):
      allzero = False
  
  if ( allzero ):

    print '%s = 0.0' % ( title )

  else:

    print '%s = ' % ( title )

    for k in range ( 1, m + 1 ):

      i, j = i4_to_pascal ( k )
      di = i + j
      km1 = k - 1

      if ( p[km1] != 0.0 ):

        if ( p[km1] < 0.0 ):
          print '  -%g' % ( abs ( p[km1] ) ),
        else:
          print '  +%g' % ( p[km1] ),

        if ( di != 0 ):
          print '',
#
#  "PASS" does nothing, but Python requires a statement here.
#
#  Python idiotically insists on putting a space between successive prints,
#  and I don't feel like calling sys.stdout.write ( string ) in order to
#  suppress something I shouldn't have to deal with in the first place,
#  so pretend you like it.
#
        if ( i == 0 ):
          pass
        elif ( i == 1 ):
          print 'x',
        else:
          print 'x^%d' % ( i ),

        if ( j == 0 ):
          pass
        elif ( j == 1 ):
          print 'y',
        else:
          print 'y^%d' % ( j ),

        print ''

  return
def triangle01_poly_integral_test ( ):

#*****************************************************************************80
#
## TRIANGLE01_POLY_INTEGRAL_TEST: polynomial integrals over the unit triangle.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    23 April 2015
#
#  Author:
#
#    John Burkardt
#
  import numpy as np
  from i4_to_pascal import i4_to_pascal
  from poly_print import poly_print
  from triangle01_monomial_integral import triangle01_monomial_integral

  d_max = 6
  k_max = ( ( d_max + 1 ) * ( d_max + 2 ) ) / 2
  qm = np.zeros ( k_max )
  for k in range ( 1, k_max + 1 ):
    km1 = k - 1
    i, j = i4_to_pascal ( k )
    qm[km1] = triangle01_monomial_integral ( i, j )

  print ''
  print 'TRIANGLE01_POLY_INTEGRAL_TEST'
  print '  TRIANGLE01_POLY_INTEGRAL returns the integral Q of'
  print '  a polynomial P(X,Y) over the interior of the unit triangle.'

  d = 1
  m = ( ( d + 1 ) * ( d + 2 ) ) / 2
  p = np.array ( [ 1.0, 2.0, 3.0 ] )
  print ''
  poly_print ( d, p, '  p(x,y)' )
  q = triangle01_poly_integral ( d, p )
  print ''
  print '  Q =         %g' % ( q )
  q2 = np.dot ( p, qm[0:m] )
  print '  Q (exact) = %g' % ( q2 )

  d = 2
  m = ( ( d + 1 ) * ( d + 2 ) ) / 2
  p = np.array ( [ 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ] )
  print ''
  poly_print ( d, p, '  p(x,y)' )
  q = triangle01_poly_integral ( d, p )
  print ''
  print '  Q =         %g' % ( q )
  q2 = np.dot ( p, qm[0:m] )
  print '  Q (exact) = %g' % ( q2 )

  d = 2
  m = ( ( d + 1 ) * ( d + 2 ) ) / 2
  p = np.array ( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] )
  print ''
  poly_print ( d, p, '  p(x,y)' )
  q = triangle01_poly_integral ( d, p )
  print ''
  print '  Q =         %g' % ( q )
  q2 = np.dot ( p, qm[0:m] )
  print '  Q (exact) = %g' % ( q2 )
#
#  Terminate.
#
  print ''
  print 'TRIANGLE01_POLY_INTEGRAL_TEST'
  print '  Normal end of execution.'

  return
def poly_product ( d1, p1, d2, p2 ):

#*****************************************************************************80
#
#% POLY_PRODUCT computes P3(x,y) = P1(x,y) * P2(x,y) for polynomials.
#
#  Location:
#
#    http://people.sc.fsu.edu/~jburkardt/py_src/triangle_integrals/poly_product.py
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    22 April 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer D1, the degree of factor 1.
#
#    Input, real P1(M1), the factor 1 coefficients.
#    M1 = ((D1+1)*(D1+2))/2.
#
#    Input, integer D2, the degree of factor 2.
#
#    Input, real P2(M2), the factor2 coefficients.
#    M2 = ((D2+1)*(D2+2))/2.
#
#    Output, integer D3, the degree of the result.
#
#    Output, real P3(M3), the result coefficients.
#    M3 = ((D3+1)*(D3+2))/2.
#
  import numpy as np
  from i4_to_pascal import i4_to_pascal
  from pascal_to_i4 import pascal_to_i4

  d3 = d1 + d2
  m3 = ( ( d3 + 1 ) * ( d3 + 2 ) ) / 2
  p3 = np.zeros ( m3 )
#
#  Consider each entry in P1:
#    P1(K1) * X^I1 * Y^J1
#  and multiply it by each entry in P2:
#    P2(K2) * X^I2 * Y^J2
#  getting 
#    P3(K3) = P3(K3) + P1(K1) * P2(X2) * X^(I1+I2) * Y(J1+J2)
#
  m1 = ( ( d1 + 1 ) * ( d1 + 2 ) ) / 2
  m2 = ( ( d2 + 1 ) * ( d2 + 2 ) ) / 2

  for k1m1 in range ( 0, m1 ):
    k1 = k1m1 + 1
    i1, j1 = i4_to_pascal ( k1 )
    for k2m1 in range ( 0, m2 ):
      k2 = k2m1 + 1
      i2, j2 = i4_to_pascal ( k2 )
      i3 = i1 + i2
      j3 = j1 + j2
      k3 = pascal_to_i4 ( i3, j3 )
      k3m1 = k3 - 1
      p3[k3m1] = p3[k3m1] + p1[k1m1] * p2[k2m1]

  return d3, p3
示例#7
0
def poly_product(d1, p1, d2, p2):

    #*****************************************************************************80
    #
    #% POLY_PRODUCT computes P3(x,y) = P1(x,y) * P2(x,y) for polynomials.
    #
    #  Location:
    #
    #    http://people.sc.fsu.edu/~jburkardt/py_src/triangle_integrals/poly_product.py
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    22 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer D1, the degree of factor 1.
    #
    #    Input, real P1(M1), the factor 1 coefficients.
    #    M1 = ((D1+1)*(D1+2))/2.
    #
    #    Input, integer D2, the degree of factor 2.
    #
    #    Input, real P2(M2), the factor2 coefficients.
    #    M2 = ((D2+1)*(D2+2))/2.
    #
    #    Output, integer D3, the degree of the result.
    #
    #    Output, real P3(M3), the result coefficients.
    #    M3 = ((D3+1)*(D3+2))/2.
    #
    import numpy as np
    from i4_to_pascal import i4_to_pascal
    from pascal_to_i4 import pascal_to_i4

    d3 = d1 + d2
    m3 = ((d3 + 1) * (d3 + 2)) / 2
    p3 = np.zeros(m3)
    #
    #  Consider each entry in P1:
    #    P1(K1) * X^I1 * Y^J1
    #  and multiply it by each entry in P2:
    #    P2(K2) * X^I2 * Y^J2
    #  getting
    #    P3(K3) = P3(K3) + P1(K1) * P2(X2) * X^(I1+I2) * Y(J1+J2)
    #
    m1 = ((d1 + 1) * (d1 + 2)) / 2
    m2 = ((d2 + 1) * (d2 + 2)) / 2

    for k1m1 in range(0, m1):
        k1 = k1m1 + 1
        i1, j1 = i4_to_pascal(k1)
        for k2m1 in range(0, m2):
            k2 = k2m1 + 1
            i2, j2 = i4_to_pascal(k2)
            i3 = i1 + i2
            j3 = j1 + j2
            k3 = pascal_to_i4(i3, j3)
            k3m1 = k3 - 1
            p3[k3m1] = p3[k3m1] + p1[k1m1] * p2[k2m1]

    return d3, p3