예제 #1
0
def polynomial_print(m, o, c, e, title):

    #*****************************************************************************80
    #
    ## POLYNOMIAL_PRINT prints a polynomial.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    25 October 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, the spatial dimension.
    #
    #    Input, integer O, the "order" of the polynomial, that is,
    #    simply the number of terms.
    #
    #    Input, real C(O), the coefficients.
    #
    #    Input, integer E(O), the indices of the exponents.
    #
    #    Input, string TITLE, a title.
    #
    from mono_unrank_grlex import mono_unrank_grlex
    import sys

    sys.stdout.write(title)
    sys.stdout.write('\n')

    if (o == 0):
        sys.stdout.write('      0.')
    else:
        for j in range(0, o):
            sys.stdout.write('    ')
            if (c[j] < 0):
                sys.stdout.write('- ')
            else:
                sys.stdout.write('+ ')
            sys.stdout.write(repr(abs(c[j])))
            sys.stdout.write(' * x^(')
            f = mono_unrank_grlex(m, e[j])
            for i in range(0, m):
                sys.stdout.write(repr(f[i]))
                if (i < m - 1):
                    sys.stdout.write(',')
                else:
                    sys.stdout.write(')')
            if (j == o - 1):
                sys.stdout.write('.')
            sys.stdout.write('\n')

    return
def polynomial_print ( m, o, c, e, title ):

#*****************************************************************************80
#
## POLYNOMIAL_PRINT prints a polynomial.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 October 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer M, the spatial dimension.
#
#    Input, integer O, the "order" of the polynomial, that is,
#    simply the number of terms.
#
#    Input, real C(O), the coefficients.
#
#    Input, integer E(O), the indices of the exponents.
#        
#    Input, string TITLE, a title.
#
  from mono_unrank_grlex import mono_unrank_grlex
  import sys

  sys.stdout.write ( title )
  sys.stdout.write ( '\n' )

  if ( o == 0 ):
    sys.stdout.write ( '      0.' )
  else:
    for j in range ( 0, o ):
      sys.stdout.write ( '    ' )
      if ( c[j] < 0 ):
        sys.stdout.write ( '- ' )
      else:
        sys.stdout.write ( '+ ' )
      sys.stdout.write ( repr ( abs ( c[j] ) ) )
      sys.stdout.write ( ' * x^(' )
      f = mono_unrank_grlex ( m, e[j] )
      for i in range ( 0, m ):
        sys.stdout.write ( repr ( f[i] ) )
        if ( i < m - 1 ):
          sys.stdout.write ( ',' )
        else:
          sys.stdout.write ( ')' )
      if ( j == o - 1 ):
        sys.stdout.write ( '.' )
      sys.stdout.write ( '\n' )

  return
예제 #3
0
def polynomial_value(m, o, c, e, nx, x):

    #*****************************************************************************80
    #
    ## POLYNOMIAL_VALUE evaluates a polynomial.
    #
    #  Discussion:
    #
    #    The polynomial is evaluated term by term, and no attempt is made to
    #    use an approach such as Horner's method to speed up the process.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    27 October 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, int M, the spatial dimension.
    #
    #    Input, integer O, the "order" of the polynomial.
    #
    #    Input, real C[O], the coefficients of the scaled polynomial.
    #
    #    Input, integer E[O], the indices of the exponents of
    #    the scaled polynomial.
    #
    #    Input, integer NX, the number of evaluation points.
    #
    #    Input, real X[M*NX], the coordinates of the evaluation points.
    #
    #    Output, real V[NX], the value of the polynomial at X.
    #
    from mono_unrank_grlex import mono_unrank_grlex
    from mono_value import mono_value
    import numpy as np

    p = np.zeros(nx, dtype=np.float64)

    for j in range(0, o):
        f = mono_unrank_grlex(m, e[j])
        v = mono_value(m, nx, f, x)
        for k in range(0, nx):
            p[k] = p[k] + c[j] * v[k]

    return p
def polynomial_value ( m, o, c, e, nx, x ):

#*****************************************************************************80
#
## POLYNOMIAL_VALUE evaluates a polynomial.
#
#  Discussion:
#
#    The polynomial is evaluated term by term, and no attempt is made to
#    use an approach such as Horner's method to speed up the process.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    27 October 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, int M, the spatial dimension.
#
#    Input, integer O, the "order" of the polynomial.
#
#    Input, real C[O], the coefficients of the scaled polynomial.
#
#    Input, integer E[O], the indices of the exponents of 
#    the scaled polynomial.
#
#    Input, integer NX, the number of evaluation points.
#
#    Input, real X[M*NX], the coordinates of the evaluation points.
#
#    Output, real V[NX], the value of the polynomial at X.
#
  from mono_unrank_grlex import mono_unrank_grlex
  from mono_value import mono_value
  import numpy as np

  p = np.zeros ( nx, dtype = np.float64 )

  for j in range ( 0, o ):
    f = mono_unrank_grlex ( m, e[j] )
    v = mono_value ( m, nx, f, x )
    for k in range ( 0, nx ):
      p[k] = p[k] + c[j] * v[k]

  return p
예제 #5
0
def mono_between_random(m, n1, n2, seed):

    #*****************************************************************************80
    #
    ## MONO_BETWEEN_RANDOM: random monomial with total degree between N1 and N2.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    25 October 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, the spatial dimension.
    #
    #    Input, integer N1, N2, the minimum and maximum degrees.
    #    0 <= N1 <= N2.
    #
    #    Input, integer SEED, the random number seed.
    #
    #    Output integer X[M], the random monomial.
    #
    #    Output integer RANK, the rank of the monomial.
    #
    #    Output, integer SEED, the updated random number seed.
    #
    from i4_uniform_ab import i4_uniform_ab
    from mono_unrank_grlex import mono_unrank_grlex
    from mono_upto_enum import mono_upto_enum

    n1_copy = max(n1, 0)
    rank_min = mono_upto_enum(m, n1_copy - 1) + 1
    rank_max = mono_upto_enum(m, n2)
    rank, seed = i4_uniform_ab(rank_min, rank_max, seed)
    x = mono_unrank_grlex(m, rank)

    return x, rank, seed
예제 #6
0
def mono_total_random(m, n, seed):

    #*****************************************************************************80
    #
    ## MONO_TOTAL_RANDOM: random monomial with total degree equal to N.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    21 November 2013
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, the spatial dimension.
    #
    #    Input, integer N, the degree.
    #    0 <= N.
    #
    #    Input, integer SEED, the random number seed.
    #
    #    Output, integer X[M], the random monomial.
    #
    #    Output, integer RANK, the rank of the monomial.
    #
    #    Output, integer SEED, the random number seed.
    #
    from i4_uniform_ab import i4_uniform_ab
    from mono_unrank_grlex import mono_unrank_grlex
    from mono_upto_enum import mono_upto_enum

    rank_min = mono_upto_enum(m, n - 1) + 1
    rank_max = mono_upto_enum(m, n)
    rank, seed = i4_uniform_ab(rank_min, rank_max, seed)
    x = mono_unrank_grlex(m, rank)

    return x, rank, seed
def mono_upto_random ( m, n, seed  ):

#*****************************************************************************80
#
## MONO_UPTO_RANDOM: random monomial with total degree less than or equal to N.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 October 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer M, the spatial dimension.
#
#    Input, integer N, the degree.
#    0 <= N.
#
#    Input, integer SEED, the random number seed.
#
#    Output, integer X[M], the random monomial.
#
#    Output, integer RANK, the rank of the monomial.
#
#    Output, integer SEED, the random number seed.
#
  from i4_uniform_ab import i4_uniform_ab
  from mono_unrank_grlex import mono_unrank_grlex
  from mono_upto_enum import mono_upto_enum

  rank_min = 1
  rank_max = mono_upto_enum ( m, n )
  rank, seed = i4_uniform_ab ( rank_min, rank_max, seed )
  x = mono_unrank_grlex ( m, rank )

  return x, rank, seed
예제 #8
0
def lpp_to_polynomial(m, l, o_max):

    #*****************************************************************************80
    #
    ## LPP_TO_POLYNOMIAL writes a Legendre Product Polynomial as a polynomial.
    #
    #  Discussion:
    #
    #    For example, if
    #      M = 3,
    #      L = ( 1, 0, 2 ),
    #    then
    #      L(1,0,2)(X,Y,Z)
    #      = L(1)(X) * L(0)(Y) * L(2)(Z)
    #      = X * 1 * ( 3Z^2-1)/2
    #      = - 1/2 X + (3/2) X Z^2
    #    so
    #      O = 2 (2 nonzero terms)
    #      C = -0.5
    #           1.5
    #      E = 4    <-- index in 3-space of exponent (1,0,0)
    #          15   <-- index in 3-space of exponent (1,0,2)
    #
    #    The output value of O is no greater than
    #      O_MAX = product ( 1 <= I <= M ) (L(I)+2)/2
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    31 October 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, int M, the spatial dimension.
    #
    #    Input, int L[M], the index of each Legendre product
    #    polynomial factor.  0 <= L(*).
    #
    #    Input, int O_MAX, an upper limit on the size of the
    #    output arrays.
    #      O_MAX = product ( 1 <= I <= M ) (L(I)+2)/2.
    #
    #    Output, int O, the "order" of the polynomial product.
    #
    #    Output, double C[O], the coefficients of the polynomial product.
    #
    #    Output, int E[O], the indices of the exponents of the
    #    polynomial product.
    #
    from lp_coefficients import lp_coefficients
    from mono_rank_grlex import mono_rank_grlex
    from mono_unrank_grlex import mono_unrank_grlex
    from polynomial_compress import polynomial_compress
    from polynomial_sort import polynomial_sort
    import numpy as np

    c1 = np.zeros(o_max, dtype=np.float64)
    c2 = np.zeros(o_max, dtype=np.float64)
    e1 = np.zeros(o_max, dtype=np.int32)
    e2 = np.zeros(o_max, dtype=np.int32)
    f2 = np.zeros(o_max, dtype=np.int32)
    pp = np.zeros(m, dtype=np.int32)

    o1 = 1
    c1[0] = 1.0
    e1[0] = 1
    #
    #  Implicate one factor at a time.
    #
    for i in range(0, m):

        o2, c2, f2 = lp_coefficients(l[i])

        o = 0
        c = np.zeros(o_max, dtype=np.float64)
        e = np.zeros(o_max, dtype=np.int32)

        for j2 in range(0, o2):
            for j1 in range(0, o1):
                c[o] = c1[j1] * c2[j2]
                if (0 < i):
                    p = mono_unrank_grlex(i, e1[j1])
                for i2 in range(0, i):
                    pp[i2] = p[i2]
                pp[i] = f2[j2]
                e[o] = mono_rank_grlex(i + 1, pp)
                o = o + 1

        c, e = polynomial_sort(o, c, e)
        o, c, e = polynomial_compress(o, c, e)

        o1 = o
        for i1 in range(0, o):
            c1[i1] = c[i1]
            e1[i1] = e[i1]

    return o1, c1, e1
def lpp_to_polynomial ( m, l, o_max ):

#*****************************************************************************80
#
## LPP_TO_POLYNOMIAL writes a Legendre Product Polynomial as a polynomial.
#
#  Discussion:
#
#    For example, if 
#      M = 3,
#      L = ( 1, 0, 2 ),
#    then
#      L(1,0,2)(X,Y,Z) 
#      = L(1)(X) * L(0)(Y) * L(2)(Z)
#      = X * 1 * ( 3Z^2-1)/2
#      = - 1/2 X + (3/2) X Z^2
#    so
#      O = 2 (2 nonzero terms)
#      C = -0.5
#           1.5
#      E = 4    <-- index in 3-space of exponent (1,0,0)
#          15   <-- index in 3-space of exponent (1,0,2)
#
#    The output value of O is no greater than
#      O_MAX = product ( 1 <= I <= M ) (L(I)+2)/2
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    31 October 2014
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, int M, the spatial dimension.
#
#    Input, int L[M], the index of each Legendre product 
#    polynomial factor.  0 <= L(*).
#
#    Input, int O_MAX, an upper limit on the size of the 
#    output arrays.
#      O_MAX = product ( 1 <= I <= M ) (L(I)+2)/2.
#
#    Output, int O, the "order" of the polynomial product.
#
#    Output, double C[O], the coefficients of the polynomial product.
#
#    Output, int E[O], the indices of the exponents of the 
#    polynomial product.
#
  from lp_coefficients import lp_coefficients
  from mono_rank_grlex import mono_rank_grlex
  from mono_unrank_grlex import mono_unrank_grlex
  from polynomial_compress import polynomial_compress
  from polynomial_sort import polynomial_sort
  import numpy as np

  c1 = np.zeros ( o_max, dtype = np.float64 )
  c2 = np.zeros ( o_max, dtype = np.float64 )
  e1 = np.zeros ( o_max, dtype = np.int32 )
  e2 = np.zeros ( o_max, dtype = np.int32 )
  f2 = np.zeros ( o_max, dtype = np.int32 )
  pp = np.zeros ( m, dtype = np.int32 )

  o1 = 1
  c1[0] = 1.0
  e1[0] = 1
#
#  Implicate one factor at a time.
#
  for i in range ( 0, m ):

    o2, c2, f2 = lp_coefficients ( l[i] );
 
    o = 0;
    c = np.zeros ( o_max, dtype = np.float64 )
    e = np.zeros ( o_max, dtype = np.int32 )

    for j2 in range ( 0, o2 ):
      for j1 in range ( 0, o1 ):
        c[o] = c1[j1] * c2[j2]
        if ( 0 < i ):
          p = mono_unrank_grlex ( i, e1[j1] )
        for i2 in range ( 0, i ):
          pp[i2] = p[i2]
        pp[i] = f2[j2]
        e[o] = mono_rank_grlex ( i + 1, pp )
        o = o + 1

    c, e = polynomial_sort ( o, c, e )
    o, c, e = polynomial_compress ( o, c, e )

    o1 = o;
    for i1 in range ( 0, o ):
      c1[i1] = c[i1]
      e1[i1] = e[i1]

  return o1, c1, e1