예제 #1
0
def r8poly_add_test():

    # *****************************************************************************80
    #
    ## R8POLY_ADD_TEST tests R8POLY_ADD.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    28 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    import numpy as np
    from r8poly_degree import r8poly_degree
    from r8poly_print import r8poly_print

    print ""
    print "R8POLY_ADD_TEST"
    print "  R8POLY_ADD adds two R8POLY's."

    na = 5
    a = np.array([0.0, 1.1, 2.2, 3.3, 4.4, 5.5])
    nb = 5
    b = np.array([1.0, -2.1, 7.2, 8.3, 0.0, -5.5])

    c = r8poly_add(na, a, nb, b)

    r8poly_print(na, a, "  Polynomial A:")

    r8poly_print(nb, b, "  Polynomial B:")

    nc = max(na, nb)

    nc2 = r8poly_degree(nc, c)

    r8poly_print(nc2, c, "  Polynomial C = A+B:")
    #
    #  Terminate.
    #
    print ""
    print "R8POLY_ADD_TEST:"
    print "  Normal end of execution."

    return
예제 #2
0
def r8poly_add_test():

    #*****************************************************************************80
    #
    ## R8POLY_ADD_TEST tests R8POLY_ADD.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    28 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    import numpy as np
    from r8poly_degree import r8poly_degree
    from r8poly_print import r8poly_print

    print ''
    print 'R8POLY_ADD_TEST'
    print '  R8POLY_ADD adds two R8POLY\'s.'

    na = 5
    a = np.array([0.0, 1.1, 2.2, 3.3, 4.4, 5.5])
    nb = 5
    b = np.array([1.0, -2.1, 7.2, 8.3, 0.0, -5.5])

    c = r8poly_add(na, a, nb, b)

    r8poly_print(na, a, '  Polynomial A:')

    r8poly_print(nb, b, '  Polynomial B:')

    nc = max(na, nb)

    nc2 = r8poly_degree(nc, c)

    r8poly_print(nc2, c, '  Polynomial C = A+B:')
    #
    #  Terminate.
    #
    print ''
    print 'R8POLY_ADD_TEST:'
    print '  Normal end of execution.'

    return
예제 #3
0
def r8poly_div(na, a, nb, b):

    #*****************************************************************************80
    #
    ## R8POLY_DIV computes the quotient and remainder of two polynomials.
    #
    #  Discussion:
    #
    #    The polynomials are assumed to be stored in power sum form.
    #
    #    The power sum form of a polynomial is:
    #
    #      p(x) = a(0) + a(1) * x + ... + a(n-1) * x^(n-1) + a(n) * x^(n)
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    30 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer NA, the dimension of A.
    #
    #    Input, real A(1:NA+1), the coefficients of the polynomial to be divided.
    #
    #    Input, integer NB, the dimension of B.
    #
    #    Input, real B(1:NB+1), the coefficients of the divisor polynomial.
    #
    #    Output, integer NQ, the degree of Q.
    #    If the divisor polynomial is zero, NQ is returned as -1.
    #
    #    Output, real Q(1:NA-NB+1), contains the quotient of A/B.
    #    If A and B have full degree, Q should be dimensioned Q(0:NA-NB).
    #    In any case, Q(0:NA) should be enough.
    #
    #    Output, integer NR, the degree of R.
    #    If the divisor polynomial is zero, NR is returned as -1.
    #
    #    Output, real R(1:NB), contains the remainder of A/B.
    #    If B has full degree, R should be dimensioned R(0:NB-1).
    #    Otherwise, R will actually require less space.
    #
    import numpy as np
    from r8poly_degree import r8poly_degree

    na2 = r8poly_degree(na, a)
    nb2 = r8poly_degree(nb, b)

    if (b[nb2] == 0.0):
        nq = -1
        nr = -1
        return nq, q, nr, r

    nq = na2 - nb2
    q = np.zeros(nq + 1)

    for i in range(nq, -1, -1):
        q[i] = a[i + nb2] / b[nb2]
        a[i + nb2] = 0.0
        for j in range(0, nb2):
            a[i + j] = a[i + j] - q[i] * b[j]

    nr = nb2 - 1
    r = np.zeros(nr + 1)
    for i in range(0, nr + 1):
        r[i] = a[i]

    return nq, q, nr, r
예제 #4
0
def r8poly_div ( na, a, nb, b ):

#*****************************************************************************80
#
## R8POLY_DIV computes the quotient and remainder of two polynomials.
#
#  Discussion:
#
#    The polynomials are assumed to be stored in power sum form.
#
#    The power sum form of a polynomial is:
#
#      p(x) = a(0) + a(1) * x + ... + a(n-1) * x^(n-1) + a(n) * x^(n)
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    30 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer NA, the dimension of A.
#
#    Input, real A(1:NA+1), the coefficients of the polynomial to be divided.
#
#    Input, integer NB, the dimension of B.
#
#    Input, real B(1:NB+1), the coefficients of the divisor polynomial.
#
#    Output, integer NQ, the degree of Q.
#    If the divisor polynomial is zero, NQ is returned as -1.
#
#    Output, real Q(1:NA-NB+1), contains the quotient of A/B.
#    If A and B have full degree, Q should be dimensioned Q(0:NA-NB).
#    In any case, Q(0:NA) should be enough.
#
#    Output, integer NR, the degree of R.
#    If the divisor polynomial is zero, NR is returned as -1.
#
#    Output, real R(1:NB), contains the remainder of A/B.
#    If B has full degree, R should be dimensioned R(0:NB-1).
#    Otherwise, R will actually require less space.
#
  import numpy as np
  from r8poly_degree import r8poly_degree

  na2 = r8poly_degree ( na, a )
  nb2 = r8poly_degree ( nb, b )

  if ( b[nb2] == 0.0 ):
    nq = -1
    nr = -1
    return nq, q, nr, r

  nq = na2 - nb2
  q = np.zeros ( nq + 1 )

  for i in range ( nq, -1, -1 ):
    q[i] = a[i+nb2] / b[nb2]
    a[i+nb2] = 0.0
    for j in range ( 0, nb2 ):
      a[i+j] = a[i+j] - q[i] * b[j]

  nr = nb2 - 1
  r = np.zeros ( nr + 1 )
  for i in range ( 0, nr + 1 ):
    r[i] = a[i]

  return nq, q, nr, r
예제 #5
0
def r8poly_print ( n, a, title ):

#*****************************************************************************80
#
## R8POLY_PRINT prints out a polynomial.
#
#  Discussion:
#
#    The power sum form is:
#
#      p(x) = a(0) + a(1) * x + ... + a(n-1) * x^(n-1) + a(n) * x^(n)
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    28 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the dimension of A.
#
#    Input, real A[0:N], the polynomial coefficients.
#    A(1) is the constant term and
#    A(N+1) is the coefficient of X^N.
#
#    Input, character TITLE(*), an optional title.
#
  from r8poly_degree import r8poly_degree

  if ( 0 < len ( title ) ):
    print ''
    print title

  print ''

  n = r8poly_degree ( n, a )

  if ( a[n] < 0.0 ):
    plus_minus = '-'
  else:
    plus_minus = ' '

  mag = abs ( a[n] )

  if ( 2 <= n ):
    print '  p(x) = %c%14f * x^%d' % ( plus_minus, mag, n )
  elif ( n == 1 ):
    print '  p(x) = %c%14f * x' % ( plus_minus, mag )
  elif ( n == 0 ):
    print '  p(x) = %c%14f' % ( plus_minus, mag )

  for i in range ( n - 1, -1, -1 ):

    if ( a[i] < 0.0 ):
      plus_minus = '-'
    else:
      plus_minus = '+'

    mag = abs ( a[i] )

    if ( mag != 0.0 ):

      if ( 2 <= i ):
        print '         %c%14f * x^%d' % ( plus_minus, mag, i )
      elif ( i == 1 ):
        print '         %c%14f * x' % ( plus_minus, mag )
      elif ( i == 0 ):
        print '         %c%14f' % ( plus_minus, mag )

  return
예제 #6
0
def r8poly_print(m, a, title):

    #*****************************************************************************80
    #
    ## R8POLY_PRINT prints out a polynomial.
    #
    #  Discussion:
    #
    #    The power sum form is:
    #
    #      p(x) = a(0) + a(1) * x + ... + a(m-1) * x^(m-1) + a(m) * x^(m)
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    05 January 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, the nominal degree of the polynomial.
    #
    #    Input, real A[0:M], the polynomial coefficients.
    #    A[0] is the constant term and
    #    A[M] is the coefficient of X^M.
    #
    #    Input, string TITLE, a title.
    #
    from r8poly_degree import r8poly_degree

    print ''
    print title
    print ''

    m2 = r8poly_degree(m, a)

    if (a[m2] < 0.0):
        plus_minus = '-'
    else:
        plus_minus = ' '

    mag = abs(a[m2])

    if (2 <= m2):
        print '  p(x) = %c %g * x^%d' % (plus_minus, mag, m2)
    elif (m2 == 1):
        print '  p(x) = %c %g * x' % (plus_minus, mag)
    elif (m2 == 0):
        print '  p(x) = %c %g' % (plus_minus, mag)

    for i in range(m2 - 1, -1, -1):

        if (a[i] < 0.0):
            plus_minus = '-'
        else:
            plus_minus = '+'

        mag = abs(a[i])

        if (mag != 0.0):

            if (2 <= i):
                print '         %c %g * x^%d' % (plus_minus, mag, i)
            elif (i == 1):
                print '         %c %g * x' % (plus_minus, mag)
            elif (i == 0):
                print '         %c %g' % (plus_minus, mag)
예제 #7
0
def r8poly_print ( m, a, title ):

#*****************************************************************************80
#
## R8POLY_PRINT prints out a polynomial.
#
#  Discussion:
#
#    The power sum form is:
#
#      p(x) = a(0) + a(1) * x + ... + a(m-1) * x^(m-1) + a(m) * x^(m)
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    05 January 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer M, the nominal degree of the polynomial.
#
#    Input, real A[0:M], the polynomial coefficients.
#    A[0] is the constant term and
#    A[M] is the coefficient of X^M.
#
#    Input, string TITLE, a title.
#
  from r8poly_degree import r8poly_degree

  print ''
  print title
  print ''

  m2 = r8poly_degree ( m, a )

  if ( a[m2] < 0.0 ):
    plus_minus = '-'
  else:
    plus_minus = ' '

  mag = abs ( a[m2] )

  if ( 2 <= m2 ):
    print '  p(x) = %c %g * x^%d' % ( plus_minus, mag, m2 )
  elif ( m2 == 1 ):
    print '  p(x) = %c %g * x' % ( plus_minus, mag )
  elif ( m2 == 0 ):
    print '  p(x) = %c %g' % ( plus_minus, mag )

  for i in range ( m2 - 1, -1, -1 ):

    if ( a[i] < 0.0 ):
      plus_minus = '-'
    else:
      plus_minus = '+'

    mag = abs ( a[i] )

    if ( mag != 0.0 ):

      if ( 2 <= i ):
        print '         %c %g * x^%d' % ( plus_minus, mag, i )
      elif ( i == 1 ):
        print '         %c %g * x' % ( plus_minus, mag )
      elif ( i == 0 ):
        print '         %c %g' % ( plus_minus, mag )