def poly_power_linear_test(): #*****************************************************************************80 # ## POLY_POWER_LINEAR_TEST tests POLY_POWER_LINEAR. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'POLY_POWER_LINEAR_TEST:' print ' POLY_POWER_LINEAR computes the N-th power of' print ' a linear polynomial in X and Y.' # # P = ( 1 + 2 x + 3 y )^2 # d1 = 1 p1 = np.array([1.0, 2.0, 3.0]) print '' poly_print(d1, p1, ' p1(x,y)') dp, pp = poly_power_linear(d1, p1, 2) print '' poly_print(dp, pp, ' p1(x,y)^n') dc = 2 pc = np.array([1.0, 4.0, 6.0, 4.0, 12.0, 9.0]) print '' poly_print(dc, pc, ' Correct answer: p1(x,y)^2') # # P = ( 2 - x + 3 y )^3 # d1 = 1 p1 = np.array([2.0, -1.0, 3.0]) print '' poly_print(d1, p1, ' p1(x,y)') dp, pp = poly_power_linear(d1, p1, 3) print '' poly_print(dp, pp, ' p1(x,y)^3') dc = 3 pc = np.array([8.0, -12.0, 36.0, 6.0, -36.0, 54.0, -1.0, 9.0, -27.0, 27.0]) print '' poly_print(dc, pc, ' Correct answer: p1(x,y)^n') # # Terminate. # print '' print 'POLY_POWER_LINEAR_TEST' print ' Normal end of execution.' 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 triangle_poly_integral_test ( ): #*****************************************************************************80 # ## TRIANGLE_POLY_INTEGRAL_TEST estimates integrals over a triangle. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'TRIANGLE_POLY_INTEGRAL_TEST' print ' TRIANGLE_POLY_INTEGRAL returns the integral Q of' print ' a polynomial over the interior of a triangle.' # # Test 1: # Integrate x over reference triangle. # t = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 0.0, 1.0 ] ] ) d = 1 p = np.array ( [ 0.0, 1.0, 0.0 ] ) print '' print ' Triangle vertices:' print ' (X1,Y1) = (%g,%g)' % ( t[0,0], t[0,1] ) print ' (X2,Y2) = (%g,%g)' % ( t[1,0], t[1,1] ) print ' (X3,Y3) = (%g,%g)' % ( t[2,0], t[2,1] ) print '' poly_print ( d, p, ' Integrand p(x,y)' ) q = triangle_poly_integral ( d, p, t ) q2 = 1.0 / 6.0 print '' print ' Computed Q = %g' % ( q ) print ' Exact Q = %g' % ( q2 ) # # Test 2: # Integrate xy over a general triangle. # t = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 1.0, 2.0 ] ] ) d = 2 p = np.array ( [ 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ] ) print '' print ' Triangle vertices:' print ' (X1,Y1) = (%g,%g)' % ( t[0,0], t[0,1] ) print ' (X2,Y2) = (%g,%g)' % ( t[1,0], t[1,1] ) print ' (X3,Y3) = (%g,%g)' % ( t[2,0], t[2,1] ) print '' poly_print ( d, p, ' Integrand p(x,y)' ) q = triangle_poly_integral ( d, p, t ) q2 = 0.5 print '' print ' Computed Q = %g' % ( q ) print ' Exact Q = %g' % ( q2 ) # # Test 3: # Integrate 2-3x+xy over a general triangle. # t = np.array ( [ \ [ 0.0, 0.0 ], \ [ 1.0, 0.0 ], \ [ 1.0, 3.0 ] ] ) d = 2 p = np.array ( [ 2.0, -3.0, 0.0, 0.0, 1.0, 0.0 ] ) print '' print ' Triangle vertices:' print ' (X1,Y1) = (%g,%g)' % ( t[0,0], t[0,1] ) print ' (X2,Y2) = (%g,%g)' % ( t[1,0], t[1,1] ) print ' (X3,Y3) = (%g,%g)' % ( t[2,0], t[2,1] ) print '' poly_print ( d, p, ' Integrand p(x,y)' ) q = triangle_poly_integral ( d, p, t ) q2 = 9.0 / 8.0 print '' print ' Computed Q = %g' % ( q ) print ' Exact Q = %g' % ( q2 ) # # Test 4: # Integrate -40y + 6x^2 over a general triangle. # t = np.array ( [ \ [ 0.0, 3.0 ], \ [ 1.0, 1.0 ], \ [ 5.0, 3.0 ] ] ) d = 2 p = np.array ( [ 0.0, 0.0,-40.0, 6.0, 0.0, 0.0 ] ) print '' print ' Triangle vertices:' print ' (X1,Y1) = (%g,%g)' % ( t[0,0], t[0,1] ) print ' (X2,Y2) = (%g,%g)' % ( t[1,0], t[1,1] ) print ' (X3,Y3) = (%g,%g)' % ( t[2,0], t[2,1] ) print '' poly_print ( d, p, ' Integrand p(x,y)' ) q = triangle_poly_integral ( d, p, t ) q2 = - 935.0 / 3.0 print '' print ' Computed Q = %g' % ( q ) print ' Exact Q = %g' % ( q2 ) # # Terminate. # print '' print 'TRIANGLE_POLY_INTEGRAL_TEST' print ' Normal end of execution.' return
def poly_power_test ( ): #*****************************************************************************80 # ## POLY_POWER_TEST tests POLY_POWER. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'POLY_POWER_TEST:' print ' POLY_POWER computes the N-th power of an X,Y polynomial.' # # P1 = ( 1 + 2 x + 3 y ) # P2 = P1^2 = 1 + 4x + 6y + 4x^2 + 12xy + 9y^2 # d1 = 1 p1 = np.array ( [ 1.0, 2.0, 3.0 ] ) n = 2 print '' poly_print ( d1, p1, ' p1(x,y)' ) d2, p2 = poly_power ( d1, p1, n ) print '' poly_print ( d2, p2, ' p2(x,y) = p1(x,y)^2' ) d3 = 2 p3 = np.array ( [ 1.0, 4.0, 6.0, 4.0, 12.0, 9.0 ] ) print '' poly_print ( d3, p3, ' p3(x,y)=correct answer' ) # # P1 = ( 1 - 2 x + 3 y - 4 x^2 + 5 xy - 6 y^2 ) # P2 = P1^3 = # 1 # -6x +9y # +0x^2 - 21xy + 9y^2 # +40x^3 - 96x^2y + 108x^y2 - 81y^3 # +0x^4 + 84x^3y - 141 x^2y^2 +171xy^3 - 54y^4 # -96x^5 + 384x^4y -798x^3y^2 + 1017 x^2y^3 - 756 xy^4 + 324 y^5 # -64x^6 + 240x^5y - 588x^4y^2 + 845 x^3y^3 - 882 x^2y^4 +540 xy^5 - 216y^6 # d1 = 2 p1 = np.array ( [ 1.0, -2.0, 3.0, -4.0, +5.0, -6.0 ] ) n = 3 print '' poly_print ( d1, p1, ' p1(x,y)' ) d2, p2 = poly_power ( d1, p1, n ) print '' poly_print ( d2, p2, ' p2(x,y) = p1(x,y)^3' ) d3 = 6 p3 = np.array ( [ \ 1.0, \ -6.0, 9.0, \ 0.0, -21.0, 9.0, \ 40.0, -96.0, 108.0, -81.0, \ 0.0, 84.0, -141.0, 171.0, -54.0, \ -96.0, 384.0, -798.0, 1017.0, -756.0, 324.0, \ -64.0, 240.0, -588.0, 845.0, -882.0, 540.0, -216.0 ] ); print '' poly_print ( d3, p3, ' p3(x,y)=correct answer' ) # # Terminate. # print '' print 'POLY_POWER_TEST' print ' Normal end of execution.' return
def poly_power_test(): #*****************************************************************************80 # ## POLY_POWER_TEST tests POLY_POWER. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'POLY_POWER_TEST:' print ' POLY_POWER computes the N-th power of an X,Y polynomial.' # # P1 = ( 1 + 2 x + 3 y ) # P2 = P1^2 = 1 + 4x + 6y + 4x^2 + 12xy + 9y^2 # d1 = 1 p1 = np.array([1.0, 2.0, 3.0]) n = 2 print '' poly_print(d1, p1, ' p1(x,y)') d2, p2 = poly_power(d1, p1, n) print '' poly_print(d2, p2, ' p2(x,y) = p1(x,y)^2') d3 = 2 p3 = np.array([1.0, 4.0, 6.0, 4.0, 12.0, 9.0]) print '' poly_print(d3, p3, ' p3(x,y)=correct answer') # # P1 = ( 1 - 2 x + 3 y - 4 x^2 + 5 xy - 6 y^2 ) # P2 = P1^3 = # 1 # -6x +9y # +0x^2 - 21xy + 9y^2 # +40x^3 - 96x^2y + 108x^y2 - 81y^3 # +0x^4 + 84x^3y - 141 x^2y^2 +171xy^3 - 54y^4 # -96x^5 + 384x^4y -798x^3y^2 + 1017 x^2y^3 - 756 xy^4 + 324 y^5 # -64x^6 + 240x^5y - 588x^4y^2 + 845 x^3y^3 - 882 x^2y^4 +540 xy^5 - 216y^6 # d1 = 2 p1 = np.array([1.0, -2.0, 3.0, -4.0, +5.0, -6.0]) n = 3 print '' poly_print(d1, p1, ' p1(x,y)') d2, p2 = poly_power(d1, p1, n) print '' poly_print(d2, p2, ' p2(x,y) = p1(x,y)^3') d3 = 6 p3 = np.array ( [ \ 1.0, \ -6.0, 9.0, \ 0.0, -21.0, 9.0, \ 40.0, -96.0, 108.0, -81.0, \ 0.0, 84.0, -141.0, 171.0, -54.0, \ -96.0, 384.0, -798.0, 1017.0, -756.0, 324.0, \ -64.0, 240.0, -588.0, 845.0, -882.0, 540.0, -216.0 ] ) print '' poly_print(d3, p3, ' p3(x,y)=correct answer') # # Terminate. # print '' print 'POLY_POWER_TEST' print ' Normal end of execution.' 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_test ( ): #*****************************************************************************80 # ## POLY_PRODUCT_TEST tests POLY_PRODUCT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'POLY_PRODUCT_TEST:' print ' POLY_PRODUCT computes the product of two X,Y polynomials.' # # P1 = ( 1 + 2 x + 3 y ) # P2 = ( 4 + 5 x ) # P3 = 4 + 13x + 12y + 10x^2 + 15xy + 0y^2 # d1 = 1 p1 = np.array ( [ 1.0, 2.0, 3.0 ] ) print '' poly_print ( d1, p1, ' p1(x,y)' ) d2 = 1 p2 = np.array ( [ 4.0, 5.0, 0.0 ] ) print '' poly_print ( d2, p2, ' p2(x,y)' ) d3, p3 = poly_product ( d1, p1, d2, p2 ) print '' poly_print ( d3, p3, ' p3(x,y) = p1(x,y) * p2(x,y)' ) dc = 2 pc = np.array ( [ 4.0, 13.0, 12.0, 10.0, 15.0, 0.0 ] ) print '' poly_print ( dc, pc, ' Correct answer: p3(x,y)=p1(x,y)*p2(x,y)' ) # # P1 = ( 1 - 2 x + 3 y - 4x^2 + 5xy - 6y^2) # P2 = ( 7 + 3x^2 ) # P3 = 7 # - 14x + 21y # - 25 x^2 + 35 xy - 42y^2 # - 6x^3 + 9x^2y + 0xy^2 + 0y^3 # - 12x^4 + 15x^3y - 18x^2y^2 + 0 xy^3 + 0y^4 # d1 = 2 p1 = np.array ( [ 1.0, -2.0, 3.0, -4.0, +5.0, -6.0 ] ) print '' poly_print ( d1, p1, ' p1(x,y)' ) d2 = 2 p2 = np.array ( [ 7.0, 0.0, 0.0, 3.0, 0.0, 0.0 ] ) print '' poly_print ( d2, p2, ' p2(x,y)' ) d3, p3 = poly_product ( d1, p1, d2, p2 ) print '' poly_print ( d3, p3, ' p3(x,y) = p1(x,y) * p2(x,y)' ) dc = 4 pc = np.array ( [ \ 7.0, \ -14.0, 21.0, \ -25.0, +35.0, -42.0, \ -6.0, 9.0, 0.0, 0.0, \ -12.0, +15.0, -18.0, 0.0, 0.0 ] ) print '' poly_print ( dc, pc, ' Correct answer: p3(x,y)=p1(x,y)*p2(x,y)' ) # # Terminate. # print '' print 'POLY_PRODUCT_TEST' print ' Normal end of execution.' return
def poly_power_linear_test ( ): #*****************************************************************************80 # ## POLY_POWER_LINEAR_TEST tests POLY_POWER_LINEAR. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'POLY_POWER_LINEAR_TEST:' print ' POLY_POWER_LINEAR computes the N-th power of' print ' a linear polynomial in X and Y.' # # P = ( 1 + 2 x + 3 y )^2 # d1 = 1 p1 = np.array ( [ 1.0, 2.0, 3.0 ] ) print '' poly_print ( d1, p1, ' p1(x,y)' ) dp, pp = poly_power_linear ( d1, p1, 2 ) print '' poly_print ( dp, pp, ' p1(x,y)^n' ) dc = 2 pc = np.array ( [ 1.0, 4.0, 6.0, 4.0, 12.0, 9.0 ] ) print '' poly_print ( dc, pc, ' Correct answer: p1(x,y)^2' ) # # P = ( 2 - x + 3 y )^3 # d1 = 1 p1 = np.array ( [ 2.0, -1.0, 3.0 ] ) print '' poly_print ( d1, p1, ' p1(x,y)' ) dp, pp = poly_power_linear ( d1, p1, 3 ) print '' poly_print ( dp, pp, ' p1(x,y)^3' ) dc = 3 pc = np.array ( [ 8.0, -12.0, 36.0, 6.0, -36.0, 54.0, -1.0, 9.0, -27.0, 27.0 ] ) print '' poly_print ( dc, pc, ' Correct answer: p1(x,y)^n' ) # # Terminate. # print '' print 'POLY_POWER_LINEAR_TEST' print ' Normal end of execution.' return
def poly_product_test(): #*****************************************************************************80 # ## POLY_PRODUCT_TEST tests POLY_PRODUCT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 April 2015 # # Author: # # John Burkardt # import numpy as np from poly_print import poly_print print '' print 'POLY_PRODUCT_TEST:' print ' POLY_PRODUCT computes the product of two X,Y polynomials.' # # P1 = ( 1 + 2 x + 3 y ) # P2 = ( 4 + 5 x ) # P3 = 4 + 13x + 12y + 10x^2 + 15xy + 0y^2 # d1 = 1 p1 = np.array([1.0, 2.0, 3.0]) print '' poly_print(d1, p1, ' p1(x,y)') d2 = 1 p2 = np.array([4.0, 5.0, 0.0]) print '' poly_print(d2, p2, ' p2(x,y)') d3, p3 = poly_product(d1, p1, d2, p2) print '' poly_print(d3, p3, ' p3(x,y) = p1(x,y) * p2(x,y)') dc = 2 pc = np.array([4.0, 13.0, 12.0, 10.0, 15.0, 0.0]) print '' poly_print(dc, pc, ' Correct answer: p3(x,y)=p1(x,y)*p2(x,y)') # # P1 = ( 1 - 2 x + 3 y - 4x^2 + 5xy - 6y^2) # P2 = ( 7 + 3x^2 ) # P3 = 7 # - 14x + 21y # - 25 x^2 + 35 xy - 42y^2 # - 6x^3 + 9x^2y + 0xy^2 + 0y^3 # - 12x^4 + 15x^3y - 18x^2y^2 + 0 xy^3 + 0y^4 # d1 = 2 p1 = np.array([1.0, -2.0, 3.0, -4.0, +5.0, -6.0]) print '' poly_print(d1, p1, ' p1(x,y)') d2 = 2 p2 = np.array([7.0, 0.0, 0.0, 3.0, 0.0, 0.0]) print '' poly_print(d2, p2, ' p2(x,y)') d3, p3 = poly_product(d1, p1, d2, p2) print '' poly_print(d3, p3, ' p3(x,y) = p1(x,y) * p2(x,y)') dc = 4 pc = np.array ( [ \ 7.0, \ -14.0, 21.0, \ -25.0, +35.0, -42.0, \ -6.0, 9.0, 0.0, 0.0, \ -12.0, +15.0, -18.0, 0.0, 0.0 ] ) print '' poly_print(dc, pc, ' Correct answer: p3(x,y)=p1(x,y)*p2(x,y)') # # Terminate. # print '' print 'POLY_PRODUCT_TEST' print ' Normal end of execution.' return