Beispiel #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
def triangle_xy_integral(x1, y1, x2, y2, x3, y3):

    #*****************************************************************************80
    #
    ## TRIANGLE_XY_INTEGRAL computes the integral of XY over a triangle.
    #
    #  Location:
    #
    #    http://people.sc.fsu.edu/~jburkardt/py_src/triangle_integrals/triangle_xy_integral.py
    #
    #  Discussion:
    #
    #    This function was written as a special test case for the general
    #    problem of integrating a monomial x^alpha * y^beta over a general
    #    triangle.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    23 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real X1, Y1, X2, Y2, X3, Y3, the coordinates of the
    #    triangle vertices.
    #
    #    Output, real Q, the integral of X*Y over the triangle.
    #

    #
    #  x = x1 * ( 1 - xi - eta )
    #    + x2 *       xi
    #    + x3 *            eta
    #
    #  y = y1 * ( 1 - xi - eta )
    #    + y2 *       xi
    #    + y3 *            eta
    #
    #  Rewrite as linear polynomials in (xi,eta):
    #
    #  x = x1 + ( x2 - x1 ) * xi + ( x3 - x1 ) * eta
    #  y = y1 + ( y2 - y1 ) * xi + ( y3 - y1 ) * eta
    #
    #  Jacobian:
    #
    #    J = [ ( x2 - x1 )  ( x3 - x1 ) ]
    #        [ ( y2 - y1 )  ( y3 - y1 ) ]
    #
    #    det J = ( x2 - x1 ) * ( y3 - y1 ) - ( y2 - y1 ) * ( x3 - x1 )
    #
    #  Integrand
    #
    #    x * y = ( x1 + ( x2 - x1 ) * xi + ( x3 - x1 ) * eta )
    #          * ( y1 + ( y2 - y1 ) * xi + ( y3 - y1 ) * eta )
    #
    #  Rewrite as linear combination of monomials:
    #
    #    x * y = 1      * x1 * y1
    #          + eta    * ( x1 * ( y3 - y1 ) + ( x3 - x1 ) * y1 )
    #          + xi     * ( x1 * ( y2 - y1 ) + ( x2 - x1 ) * y1 )
    #          + eta^2  * ( x3 - x1 ) * ( y3 - y1 )
    #          + xi*eta * ( ( x2 - x1 ) * ( y3 - y1 ) + ( x3 - x1 ) * ( y2 - y1 ) )
    #          + xi^2   * ( x2 - x1 ) * ( y2 - y1 )
    #
    from triangle01_monomial_integral import triangle01_monomial_integral

    det = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)

    p00 = x1 * y1

    p01 = x1 * (y3 - y1) + (x3 - x1) * y1
    p10 = x1 * (y2 - y1) + (x2 - x1) * y1

    p02 = (x3 - x1) * (y3 - y1)
    p11 = (x2 - x1) * (y3 - y1) + (x3 - x1) * (y2 - y1)
    p20 = (x2 - x1) * (y2 - y1)

    q = 0.0
    q = q + p00 * triangle01_monomial_integral(0, 0)
    q = q + p10 * triangle01_monomial_integral(1, 0)
    q = q + p01 * triangle01_monomial_integral(0, 1)
    q = q + p20 * triangle01_monomial_integral(2, 0)
    q = q + p11 * triangle01_monomial_integral(1, 1)
    q = q + p02 * triangle01_monomial_integral(0, 2)

    q = q * det

    return q
Beispiel #4
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
def triangle_xy_integral(x1, y1, x2, y2, x3, y3):

    # *****************************************************************************80
    #
    ## TRIANGLE_XY_INTEGRAL computes the integral of XY over a triangle.
    #
    #  Location:
    #
    #    http://people.sc.fsu.edu/~jburkardt/py_src/triangle_integrals/triangle_xy_integral.py
    #
    #  Discussion:
    #
    #    This function was written as a special test case for the general
    #    problem of integrating a monomial x^alpha * y^beta over a general
    #    triangle.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    23 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real X1, Y1, X2, Y2, X3, Y3, the coordinates of the
    #    triangle vertices.
    #
    #    Output, real Q, the integral of X*Y over the triangle.
    #

    #
    #  x = x1 * ( 1 - xi - eta )
    #    + x2 *       xi
    #    + x3 *            eta
    #
    #  y = y1 * ( 1 - xi - eta )
    #    + y2 *       xi
    #    + y3 *            eta
    #
    #  Rewrite as linear polynomials in (xi,eta):
    #
    #  x = x1 + ( x2 - x1 ) * xi + ( x3 - x1 ) * eta
    #  y = y1 + ( y2 - y1 ) * xi + ( y3 - y1 ) * eta
    #
    #  Jacobian:
    #
    #    J = [ ( x2 - x1 )  ( x3 - x1 ) ]
    #        [ ( y2 - y1 )  ( y3 - y1 ) ]
    #
    #    det J = ( x2 - x1 ) * ( y3 - y1 ) - ( y2 - y1 ) * ( x3 - x1 )
    #
    #  Integrand
    #
    #    x * y = ( x1 + ( x2 - x1 ) * xi + ( x3 - x1 ) * eta )
    #          * ( y1 + ( y2 - y1 ) * xi + ( y3 - y1 ) * eta )
    #
    #  Rewrite as linear combination of monomials:
    #
    #    x * y = 1      * x1 * y1
    #          + eta    * ( x1 * ( y3 - y1 ) + ( x3 - x1 ) * y1 )
    #          + xi     * ( x1 * ( y2 - y1 ) + ( x2 - x1 ) * y1 )
    #          + eta^2  * ( x3 - x1 ) * ( y3 - y1 )
    #          + xi*eta * ( ( x2 - x1 ) * ( y3 - y1 ) + ( x3 - x1 ) * ( y2 - y1 ) )
    #          + xi^2   * ( x2 - x1 ) * ( y2 - y1 )
    #
    from triangle01_monomial_integral import triangle01_monomial_integral

    det = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)

    p00 = x1 * y1

    p01 = x1 * (y3 - y1) + (x3 - x1) * y1
    p10 = x1 * (y2 - y1) + (x2 - x1) * y1

    p02 = (x3 - x1) * (y3 - y1)
    p11 = (x2 - x1) * (y3 - y1) + (x3 - x1) * (y2 - y1)
    p20 = (x2 - x1) * (y2 - y1)

    q = 0.0
    q = q + p00 * triangle01_monomial_integral(0, 0)
    q = q + p10 * triangle01_monomial_integral(1, 0)
    q = q + p01 * triangle01_monomial_integral(0, 1)
    q = q + p20 * triangle01_monomial_integral(2, 0)
    q = q + p11 * triangle01_monomial_integral(1, 1)
    q = q + p02 * triangle01_monomial_integral(0, 2)

    q = q * det

    return q
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