def r8mat_is_null_left(m, n, a, x):

    #*****************************************************************************80
    #
    ## R8MAT_IS_NULL_LEFT determines if x is a left null vector of matrix A.
    #
    #  Discussion:
    #
    #    The nonzero M vector x is a left null vector of the MxN matrix A if
    #
    #      x'*A = A'*x = 0
    #
    #    If A is a square matrix, then this implies that A is singular.
    #
    #    If A is a square matrix, this implies that 0 is an eigenvalue of A,
    #    and that x is an associated eigenvector.
    #
    #    This routine returns 0 if x is exactly a left null vector of A.
    #
    #    It returns a "huge" value if x is the zero vector.
    #
    #    Otherwise, it returns the L2 norm of A' * x divided by the L2 norm of x:
    #
    #      ERROR_L2 = NORM_L2 ( A' * x ) / NORM_L2 ( x )
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    07 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer M, N, the row and column dimensions of
    #    the matrix.  M and N must be positive.
    #
    #    Input, real A(M,N), the matrix.
    #
    #    Input, real X(M), the vector.
    #
    #    Output, real VALUE, the result.
    #    0.0 indicates that X is exactly a left null vector.
    #    A "huge" value indicates that ||x|| = 0;
    #    Otherwise, the value returned is a relative error ||A'*x||/||x||.
    #
    from r8mat_mtv import r8mat_mtv
    from r8vec_norm_l2 import r8vec_norm_l2
    #
    #  X_NORM
    #
    x_norm = r8vec_norm_l2(m, x)
    #
    #  ATX = A'*X
    #
    atx = r8mat_mtv(m, n, a, x)
    #
    #  ATX_NORM
    #
    atx_norm = r8vec_norm_l2(n, atx)
    #
    #  Value
    #
    value = atx_norm / x_norm

    return value
Example #2
0
def parameter_spiral_test():

    #*****************************************************************************80
    #
    ## PARAMETER_SPIRAL_TEST monitors solution norms over time for various values of NU, RHO.
    #
    #  Location:
    #
    #    http://people.sc.fsu.edu/~jburkardt/py_src/navier_stokes_2d_exact/parameter_spiral_test.py
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    31 January 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    import numpy as np
    from r8vec_norm_l2 import r8vec_norm_l2
    from r8vec_uniform_ab import r8vec_uniform_ab
    from uvp_spiral import uvp_spiral

    print ''
    print 'PARAMETER_SPIRAL_TEST'
    print '  Spiral Flow'
    print '  Monitor solution norms over time for various'
    print '  values of NU, RHO.'

    n = 1000
    xy_lo = 0.0
    xy_hi = 1.0
    seed = 123456789
    x, seed = r8vec_uniform_ab(n, xy_lo, xy_hi, seed)
    y, seed = r8vec_uniform_ab(n, xy_lo, xy_hi, seed)
    #
    #  Vary RHO.
    #
    print ''
    print '  RHO affects the pressure scaling.'
    print ''
    print '     RHO         NU           T     ||U||       ||V||       ||P||'
    print ''

    nu = 1.0
    rho = 1.0

    for j in range(0, 3):

        for k in range(0, 6):

            t = k / 5.0

            u, v, p = uvp_spiral(nu, rho, n, x, y, t)

            u_norm = r8vec_norm_l2(n, u) / n
            v_norm = r8vec_norm_l2(n, v) / n
            p_norm = r8vec_norm_l2(n, p) / n

            print '  %10.4g  %10.4g  %8.4g  %10.4g  %10.4g  %10.4g' \
              % ( rho, nu, t, u_norm, v_norm, p_norm )

        print ''
        rho = rho / 100.0


#
#  Vary NU.
#
    print ''
    print '  NU affects the time scaling.'
    print ''
    print '     RHO         NU           T     ||U||       ||V||       ||P||'
    print ''

    nu = 1.0
    rho = 1.0

    for i in range(0, 4):

        for k in range(0, 6):

            t = k / 5.0

            u, v, p = uvp_spiral(nu, rho, n, x, y, t)

            u_norm = r8vec_norm_l2(n, u) / n
            v_norm = r8vec_norm_l2(n, v) / n
            p_norm = r8vec_norm_l2(n, p) / n

            print '  %10.4g  %10.4g  %8.4g  %10.4g  %10.4g  %10.4g' \
              % ( rho, nu, t, u_norm, v_norm, p_norm )

        print ''

        nu = nu / 10.0

    print ''
    print 'PARAMETER_TAYLOR_TEST:'
    print '  Normal end of execution.'

    return
def test_null_right():

    #*****************************************************************************80
    #
    ## TEST_NULL_RIGHT tests right null vectors.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    12 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from a123 import a123
    from a123 import a123_null_right
    from archimedes import archimedes
    from archimedes import archimedes_null_right
    from cheby_diff1 import cheby_diff1
    from cheby_diff1 import cheby_diff1_null_right
    from creation import creation
    from creation import creation_null_right
    from dif1 import dif1
    from dif1 import dif1_null_right
    from dif1cyclic import dif1cyclic
    from dif1cyclic import dif1cyclic_null_right
    from dif2cyclic import dif2cyclic
    from dif2cyclic import dif2cyclic_null_right
    from fibonacci1 import fibonacci1
    from fibonacci1 import fibonacci1_null_right
    from hamming import hamming
    from hamming import hamming_null_right
    from line_adj import line_adj
    from line_adj import line_adj_null_right
    from moler2 import moler2
    from moler2 import moler2_null_right
    from neumann import neumann
    from neumann import neumann_null_right
    from one import one
    from one import one_null_right
    from r8_uniform_ab import r8_uniform_ab
    from r8mat_is_null_right import r8mat_is_null_right
    from r8mat_norm_fro import r8mat_norm_fro
    from r8vec_norm_l2 import r8vec_norm_l2
    from ring_adj import ring_adj
    from ring_adj import ring_adj_null_right
    from rosser1 import rosser1
    from rosser1 import rosser1_null_right
    from zero import zero
    from zero import zero_null_right

    print ''
    print 'TEST_NULL_RIGHT'
    print '  A = a test matrix of order M by N'
    print '  x = an N vector, candidate for a right null vector.'
    print ''
    print '  ||A|| = Frobenius norm of A.'
    print '  ||x|| = L2 norm of x.'
    print '  ||A*x||/||x|| = L2 norm of A*x over L2 norm of x.'
    print ''
    print '  Title                    M     N      ',
    print '||A||            ||x||        ||A*x||/||x||'
    print ''
    #
    #  A123
    #
    title = 'A123'
    m = 3
    n = 3
    a = a123()
    x = a123_null_right()
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  ARCHIMEDES
    #
    title = 'ARCHIMEDES'
    m = 7
    n = 8
    a = archimedes()
    x = archimedes_null_right()
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  CHEBY_DIFF1
    #
    title = 'CHEBY_DIFF1'
    m = 5
    n = m
    a = cheby_diff1(n)
    x = cheby_diff1_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  CREATION
    #
    title = 'CREATION'
    m = 5
    n = m
    a = creation(m, n)
    x = creation_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  DIF1
    #  Only has null vectors for N odd.
    #
    title = 'DIF1'
    m = 5
    n = m
    a = dif1(m, n)
    x = dif1_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  DIF1CYCLIC
    #
    title = 'DIF1CYCLIC'
    m = 5
    n = m
    a = dif1cyclic(n)
    x = dif1cyclic_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  DIF2CYCLIC
    #
    title = 'DIF2CYCLIC'
    m = 5
    n = m
    a = dif2cyclic(n)
    x = dif2cyclic_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  FIBONACCI1
    #
    title = 'FIBONACCI1'
    m = 5
    n = m
    r8_lo = -5.0
    r8_hi = +5.0
    seed = 123456789
    f1, seed = r8_uniform_ab(r8_lo, r8_hi, seed)
    f2, seed = r8_uniform_ab(r8_lo, r8_hi, seed)
    a = fibonacci1(n, f1, f2)
    x = fibonacci1_null_right(m, n, f1, f2)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  HAMMING
    #
    title = 'HAMMING'
    m = 5
    n = (2**m) - 1
    a = hamming(m, n)
    x = hamming_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  LINE_ADJ
    #
    title = 'LINE_ADJ'
    m = 7
    n = m
    a = line_adj(n)
    x = line_adj_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  MOLER2
    #
    title = 'MOLER2'
    m = 5
    n = 5
    a = moler2()
    x = moler2_null_right()
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  NEUMANN
    #
    title = 'NEUMANN'
    row_num = 5
    col_num = 5
    m = row_num * col_num
    n = row_num * col_num
    a = neumann(row_num, col_num)
    x = neumann_null_right(row_num, col_num)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  ONE
    #
    title = 'ONE'
    m = 5
    n = 5
    a = one(m, n)
    x = one_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  RING_ADJ
    #  N must be a multiple of 4 for there to be a null vector.
    #
    title = 'RING_ADJ'
    m = 12
    n = 12
    a = ring_adj(m, n)
    x = ring_adj_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  ROSSER1
    #
    title = 'ROSSER1'
    m = 8
    n = 8
    a = rosser1()
    x = rosser1_null_right()
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  ZERO
    #
    title = 'ZERO'
    m = 5
    n = 5
    a = zero(m, n)
    x = zero_null_right(m, n)
    error_l2 = r8mat_is_null_right(m, n, a, x)
    norm_a_frobenius = r8mat_norm_fro(m, n, a)
    norm_x_l2 = r8vec_norm_l2(n, x)
    print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
      % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
    #
    #  Terminate.
    #
    print ''
    print 'TEST_NULL_RIGHT:'
    print '  Normal end of execution.'

    return
def parameter_spiral_test ( ):

#*****************************************************************************80
#
## PARAMETER_SPIRAL_TEST monitors solution norms over time for various values of NU, RHO.
#
#  Location:
#
#    http://people.sc.fsu.edu/~jburkardt/py_src/navier_stokes_2d_exact/parameter_spiral_test.py
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    31 January 2015
#
#  Author:
#
#    John Burkardt
#
  import numpy as np
  from r8vec_norm_l2 import r8vec_norm_l2
  from r8vec_uniform_ab import r8vec_uniform_ab
  from uvp_spiral import uvp_spiral

  print ''
  print 'PARAMETER_SPIRAL_TEST'
  print '  Spiral Flow'
  print '  Monitor solution norms over time for various'
  print '  values of NU, RHO.'

  n = 1000
  xy_lo = 0.0
  xy_hi = 1.0
  seed = 123456789
  x, seed = r8vec_uniform_ab ( n, xy_lo, xy_hi, seed )
  y, seed = r8vec_uniform_ab ( n, xy_lo, xy_hi, seed )
#
#  Vary RHO.
#
  print ''
  print '  RHO affects the pressure scaling.'
  print ''
  print '     RHO         NU           T     ||U||       ||V||       ||P||'
  print ''

  nu = 1.0
  rho = 1.0

  for j in range ( 0, 3 ):

    for k in range ( 0, 6 ):

      t = k / 5.0

      u, v, p = uvp_spiral ( nu, rho, n, x, y, t )

      u_norm = r8vec_norm_l2 ( n, u ) / n
      v_norm = r8vec_norm_l2 ( n, v ) / n
      p_norm = r8vec_norm_l2 ( n, p ) / n

      print '  %10.4g  %10.4g  %8.4g  %10.4g  %10.4g  %10.4g' \
        % ( rho, nu, t, u_norm, v_norm, p_norm )

    print ''
    rho = rho / 100.0
#
#  Vary NU.
#
  print ''
  print '  NU affects the time scaling.'
  print ''
  print '     RHO         NU           T     ||U||       ||V||       ||P||'
  print ''

  nu = 1.0;
  rho = 1.0;
  
  for i in range ( 0, 4 ):

    for k in range ( 0, 6 ):

      t = k / 5.0

      u, v, p = uvp_spiral ( nu, rho, n, x, y, t )

      u_norm = r8vec_norm_l2 ( n, u ) / n
      v_norm = r8vec_norm_l2 ( n, v ) / n
      p_norm = r8vec_norm_l2 ( n, p ) / n

      print '  %10.4g  %10.4g  %8.4g  %10.4g  %10.4g  %10.4g' \
        % ( rho, nu, t, u_norm, v_norm, p_norm )

    print ''

    nu = nu / 10.0

  print ''
  print 'PARAMETER_TAYLOR_TEST:'
  print '  Normal end of execution.'

  return
def r8mat_is_null_left ( m, n, a, x ):

#*****************************************************************************80
#
## R8MAT_IS_NULL_LEFT determines if x is a left null vector of matrix A.
#
#  Discussion:
#
#    The nonzero M vector x is a left null vector of the MxN matrix A if
#
#      x'*A = A'*x = 0
#
#    If A is a square matrix, then this implies that A is singular.
#
#    If A is a square matrix, this implies that 0 is an eigenvalue of A,
#    and that x is an associated eigenvector.
#
#    This routine returns 0 if x is exactly a left null vector of A.
#
#    It returns a "huge" value if x is the zero vector.
#
#    Otherwise, it returns the L2 norm of A' * x divided by the L2 norm of x:
#
#      ERROR_L2 = NORM_L2 ( A' * x ) / NORM_L2 ( x )
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    07 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer M, N, the row and column dimensions of 
#    the matrix.  M and N must be positive.
#
#    Input, real A(M,N), the matrix.
#
#    Input, real X(M), the vector.
#
#    Output, real VALUE, the result.
#    0.0 indicates that X is exactly a left null vector.
#    A "huge" value indicates that ||x|| = 0;
#    Otherwise, the value returned is a relative error ||A'*x||/||x||.
#
  from r8mat_mtv import r8mat_mtv
  from r8vec_norm_l2 import r8vec_norm_l2
#
#  X_NORM
#
  x_norm = r8vec_norm_l2 ( m, x )
#
#  ATX = A'*X
#
  atx = r8mat_mtv ( m, n, a, x )
#
#  ATX_NORM
#
  atx_norm = r8vec_norm_l2 ( n, atx )
#
#  Value
#
  value = atx_norm / x_norm

  return value
Example #6
0
def helmert2 ( n, x ):

#*****************************************************************************80
#
## HELMERT2 returns the HELMERT2 matrix.
#
#  Formula:
#
#    Row 1 = the vector, divided by its L2 norm.
#
#    Row 2 is computed by the requirements that it be orthogonal to row 1,
#    be nonzero only from columns 1 to 2, and have a negative diagonal.
#
#    Row 3 is computed by the requirements that it be orthogonal to
#    rows 1 and 2, be nonzero only from columns 1 to 3, and have a
#    negative diagonal, and so on.
#
#  Properties:
#
#    The first row of A should be the vector X, divided by its L2 norm.
#
#    A is orthogonal: A' * A = A * A' = I.
#
#    A is not symmetric: A' ~= A.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    HO Lancaster,
#    The Helmert Matrices,
#    American Mathematical Monthly,
#    Volume 72, 1965, pages 4-12.
#
#  Parameters:
#
#    Input, integer N, the order of A.
#
#    Input, real X(N), the vector that defines the first row.
#    X must not have 0 L2 norm, and its first entry must not be 0.
#
#    Output, real A(N,N), the matrix.
#
  import numpy as np
  from r8vec_norm_l2 import r8vec_norm_l2
  from sys import exit

  a = np.zeros ( ( n, n ) )

  x_norm_l2 = r8vec_norm_l2 ( n, x )

  if ( x_norm_l2 == 0.0 ):
    print ''
    print 'HELMERT2 - Fatal error!'
    print '  Input vector has zero L2 norm.'
    exit ( 'HELMERT2 - Fatal error!' );

  if ( x[0] == 0.0 ):
    print ''
    print 'HELMERT2 - Fatal error!'
    print '  Input vector has X[0] = 0.'
    exit ( 'HELMERT2 - Fatal error!' );

  w = np.zeros ( n )
  for i in range ( 0, n ):
    w[i] = ( x[i] / x_norm_l2 ) ** 2

  s = np.zeros ( n )
  s[0] = w[0]
  for i in range ( 1, n ):
    s[i] = s[i-1] + w[i]

  for j in range ( 0, n ):
    a[0,j] = np.sqrt ( w[j] )

  for i in range ( 1, n ):
    for j in range ( 0, i ):
      a[i,j] = np.sqrt ( w[i] * w[j] / ( s[i] * s[i-1] ) )
    a[i,i] = - np.sqrt ( s[i-1] / s[i] )

  return a
def test_null_right ( ):

#*****************************************************************************80
#
## TEST_NULL_RIGHT tests right null vectors.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    12 March 2015
#
#  Author:
#
#    John Burkardt
#
  from a123                 import a123
  from a123                 import a123_null_right
  from archimedes           import archimedes
  from archimedes           import archimedes_null_right
  from cheby_diff1          import cheby_diff1
  from cheby_diff1          import cheby_diff1_null_right
  from creation             import creation
  from creation             import creation_null_right
  from dif1                 import dif1
  from dif1                 import dif1_null_right
  from dif1cyclic           import dif1cyclic
  from dif1cyclic           import dif1cyclic_null_right
  from dif2cyclic           import dif2cyclic
  from dif2cyclic           import dif2cyclic_null_right
  from fibonacci1           import fibonacci1
  from fibonacci1           import fibonacci1_null_right
  from hamming              import hamming
  from hamming              import hamming_null_right
  from line_adj             import line_adj
  from line_adj             import line_adj_null_right
  from moler2               import moler2
  from moler2               import moler2_null_right
  from neumann              import neumann
  from neumann              import neumann_null_right
  from one                  import one
  from one                  import one_null_right
  from r8_uniform_ab        import r8_uniform_ab
  from r8mat_is_null_right  import r8mat_is_null_right
  from r8mat_norm_fro       import r8mat_norm_fro
  from r8vec_norm_l2        import r8vec_norm_l2
  from ring_adj             import ring_adj
  from ring_adj             import ring_adj_null_right
  from rosser1              import rosser1
  from rosser1              import rosser1_null_right
  from zero                 import zero
  from zero                 import zero_null_right

  print ''
  print 'TEST_NULL_RIGHT'
  print '  A = a test matrix of order M by N'
  print '  x = an N vector, candidate for a right null vector.'
  print ''
  print '  ||A|| = Frobenius norm of A.'
  print '  ||x|| = L2 norm of x.'
  print '  ||A*x||/||x|| = L2 norm of A*x over L2 norm of x.'
  print ''
  print '  Title                    M     N      ',
  print '||A||            ||x||        ||A*x||/||x||'
  print ''
#
#  A123
#
  title = 'A123'
  m = 3
  n = 3
  a = a123 ( )
  x = a123_null_right ( )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ARCHIMEDES
#
  title = 'ARCHIMEDES'
  m = 7
  n = 8
  a = archimedes ( )
  x = archimedes_null_right ( )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  CHEBY_DIFF1
#
  title = 'CHEBY_DIFF1'
  m = 5
  n = m
  a = cheby_diff1 ( n )
  x = cheby_diff1_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  CREATION
#
  title = 'CREATION'
  m = 5
  n = m
  a = creation ( m, n )
  x = creation_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  DIF1
#  Only has null vectors for N odd.
#
  title = 'DIF1'
  m = 5
  n = m
  a = dif1 ( m, n )
  x = dif1_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  DIF1CYCLIC
#
  title = 'DIF1CYCLIC'
  m = 5
  n = m
  a = dif1cyclic ( n )
  x = dif1cyclic_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  DIF2CYCLIC
#
  title = 'DIF2CYCLIC'
  m = 5
  n = m
  a = dif2cyclic ( n )
  x = dif2cyclic_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  FIBONACCI1
#
  title = 'FIBONACCI1'
  m = 5
  n = m
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  f1, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  f2, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = fibonacci1 ( n, f1, f2 )
  x = fibonacci1_null_right ( m, n, f1, f2 )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  HAMMING
#
  title = 'HAMMING'
  m = 5
  n = ( 2 ** m ) - 1
  a = hamming ( m, n )
  x = hamming_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  LINE_ADJ
#
  title = 'LINE_ADJ'
  m = 7
  n = m
  a = line_adj ( n )
  x = line_adj_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  MOLER2
#
  title = 'MOLER2'
  m = 5
  n = 5
  a = moler2 ( )
  x = moler2_null_right ( )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  NEUMANN
#
  title = 'NEUMANN'
  row_num = 5
  col_num = 5
  m = row_num * col_num
  n = row_num * col_num
  a = neumann ( row_num, col_num )
  x = neumann_null_right ( row_num, col_num )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ONE
#
  title = 'ONE'
  m = 5
  n = 5
  a = one ( m, n )
  x = one_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  RING_ADJ
#  N must be a multiple of 4 for there to be a null vector.
#
  title = 'RING_ADJ'
  m = 12
  n = 12
  a = ring_adj ( m, n )
  x = ring_adj_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ROSSER1
#
  title = 'ROSSER1'
  m = 8
  n = 8
  a = rosser1 ( )
  x = rosser1_null_right ( )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ZERO
#
  title = 'ZERO'
  m = 5
  n = 5
  a = zero ( m, n )
  x = zero_null_right ( m, n )
  error_l2 = r8mat_is_null_right ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( n, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  Terminate.
#
  print ''
  print 'TEST_NULL_RIGHT:'
  print '  Normal end of execution.'

  return
Example #8
0
def helmert2(n, x):

    #*****************************************************************************80
    #
    ## HELMERT2 returns the HELMERT2 matrix.
    #
    #  Formula:
    #
    #    Row 1 = the vector, divided by its L2 norm.
    #
    #    Row 2 is computed by the requirements that it be orthogonal to row 1,
    #    be nonzero only from columns 1 to 2, and have a negative diagonal.
    #
    #    Row 3 is computed by the requirements that it be orthogonal to
    #    rows 1 and 2, be nonzero only from columns 1 to 3, and have a
    #    negative diagonal, and so on.
    #
    #  Properties:
    #
    #    The first row of A should be the vector X, divided by its L2 norm.
    #
    #    A is orthogonal: A' * A = A * A' = I.
    #
    #    A is not symmetric: A' ~= A.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    HO Lancaster,
    #    The Helmert Matrices,
    #    American Mathematical Monthly,
    #    Volume 72, 1965, pages 4-12.
    #
    #  Parameters:
    #
    #    Input, integer N, the order of A.
    #
    #    Input, real X(N), the vector that defines the first row.
    #    X must not have 0 L2 norm, and its first entry must not be 0.
    #
    #    Output, real A(N,N), the matrix.
    #
    import numpy as np
    from r8vec_norm_l2 import r8vec_norm_l2
    from sys import exit

    a = np.zeros((n, n))

    x_norm_l2 = r8vec_norm_l2(n, x)

    if (x_norm_l2 == 0.0):
        print ''
        print 'HELMERT2 - Fatal error!'
        print '  Input vector has zero L2 norm.'
        exit('HELMERT2 - Fatal error!')

    if (x[0] == 0.0):
        print ''
        print 'HELMERT2 - Fatal error!'
        print '  Input vector has X[0] = 0.'
        exit('HELMERT2 - Fatal error!')

    w = np.zeros(n)
    for i in range(0, n):
        w[i] = (x[i] / x_norm_l2)**2

    s = np.zeros(n)
    s[0] = w[0]
    for i in range(1, n):
        s[i] = s[i - 1] + w[i]

    for j in range(0, n):
        a[0, j] = np.sqrt(w[j])

    for i in range(1, n):
        for j in range(0, i):
            a[i, j] = np.sqrt(w[i] * w[j] / (s[i] * s[i - 1]))
        a[i, i] = -np.sqrt(s[i - 1] / s[i])

    return a
def test_null_left ( ):

#*****************************************************************************80
#
## TEST_NULL_LEFT tests left null vectors.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    17 March 2015
#
#  Author:
#
#    John Burkardt
#
  from a123                import a123
  from a123                import a123_null_left
  from cheby_diff1         import cheby_diff1
  from cheby_diff1         import cheby_diff1_null_left
  from creation            import creation
  from creation            import creation_null_left
  from dif1                import dif1
  from dif1                import dif1_null_left
  from dif1cyclic          import dif1cyclic
  from dif1cyclic          import dif1cyclic_null_left
  from dif2cyclic          import dif2cyclic
  from dif2cyclic          import dif2cyclic_null_left
  from eberlein            import eberlein
  from eberlein            import eberlein_null_left
  from fibonacci1          import fibonacci1
  from fibonacci1          import fibonacci1_null_left
  from lauchli             import lauchli
  from lauchli             import lauchli_null_left
  from line_adj            import line_adj
  from line_adj            import line_adj_null_left
  from moler2              import moler2
  from moler2              import moler2_null_left
  from one                 import one
  from one                 import one_null_left
  from r8_uniform_ab       import r8_uniform_ab
  from r8mat_is_null_left  import r8mat_is_null_left
  from r8mat_norm_fro      import r8mat_norm_fro
  from r8vec_norm_l2       import r8vec_norm_l2
  from ring_adj            import ring_adj
  from ring_adj            import ring_adj_null_left
  from rosser1             import rosser1
  from rosser1             import rosser1_null_left
  from zero                import zero
  from zero                import zero_null_left

  print ''
  print 'TEST_NULL_LEFT'
  print '  A = a test matrix of order M by N'
  print '  x = an M vector, candidate for a left null vector.'
  print ''
  print '  ||A|| = Frobenius norm of A.'
  print '  ||x|| = L2 norm of x.'
  print '  ||A''*x||/||x|| = L2 norm of A\'*x over L2 norm of x.'
  print ''
  print '  Title                    M     N      ',
  print '||A||            ||x||        ||A\'*x||/||x||'
  print ''
#
#  A123
#
  title = 'A123'
  m = 3
  n = 3
  a = a123 ( )
  x = a123_null_left ( )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  CHEBY_DIFF1
#
  title = 'CHEBY_DIFF1'
  m = 5
  n = m
  a = cheby_diff1 ( n )
  x = cheby_diff1_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  CREATION
#
  title = 'CREATION'
  m = 5
  n = m
  a = creation ( m, n )
  x = creation_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  DIF1
#
  title = 'DIF1'
  m = 5
  n = 5
  a = dif1 ( m, n )
  x = dif1_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  DIF1CYCLIC
#
  title = 'DIF1CYCLIC'
  m = 5
  n = m
  a = dif1cyclic ( n )
  x = dif1cyclic_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  DIF2CYCLIC
#
  title = 'DIF2CYCLIC'
  m = 5
  n = 5
  a = dif2cyclic ( n )
  x = dif2cyclic_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  EBERLEIN
#
  title = 'EBERLEIN'
  m = 5
  n = 5
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  alpha, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = eberlein ( alpha, n )
  x = eberlein_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  FIBONACCI1
#
  title = 'FIBONACCI1'
  m = 5
  n = m
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  f1, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  f2, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = fibonacci1 ( n, f1, f2 )
  x = fibonacci1_null_left ( m, n, f1, f2 )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  LAUCHLI
#
  title = 'LAUCHLI'
  m = 6
  n = m - 1
  r8_lo = -5.0
  r8_hi = +5.0
  seed = 123456789
  alpha, seed = r8_uniform_ab ( r8_lo, r8_hi, seed )
  a = lauchli ( alpha, m, n )
  x = lauchli_null_left ( alpha, m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  LINE_ADJ
#
  title = 'LINE_ADJ'
  m = 7
  n = m
  a = line_adj ( n )
  x = line_adj_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  MOLER2
#
  title = 'MOLER2'
  m = 5
  n = m
  a = moler2 ( )
  x = moler2_null_left ( )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ONE
#
  title = 'ONE'
  m = 5
  n = 5
  a = one ( m, n )
  x = one_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  RING_ADJ
#  N must be a multiple of 4 for there to be a null vector.
#
  title = 'RING_ADJ'
  m = 12
  n = 12
  a = ring_adj ( m, n )
  x = ring_adj_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ROSSER1
#
  title = 'ROSSER1'
  m = 8
  n = 8
  a = rosser1 ( )
  x = rosser1_null_left ( )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  ZERO
#
  title = 'ZERO'
  m = 5
  n = 5
  a = zero ( m, n )
  x = zero_null_left ( m, n )
  error_l2 = r8mat_is_null_left ( m, n, a, x )
  norm_a_frobenius = r8mat_norm_fro ( m, n, a )
  norm_x_l2 = r8vec_norm_l2 ( m, x )
  print '  %-20s  %4d  %4d  %14g  %14g  %10.2g' \
    % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 )
#
#  Terminate.
#
  print ''
  print 'TEST_NULL_LEFT:'
  print '  Normal end of execution.'

  return