void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, std::string title ) { # define INCX 5 int i; int i2; int i2hi; int i2lo; int inc; int j; int j2hi; int j2lo; std::cout << "\n"; std::cout << title << "\n"; for ( i2lo = i4_max ( ilo, 1 ); i2lo <= i4_min ( ihi, m ); i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); inc = i2hi + 1 - i2lo; std::cout << "\n"; std::cout << " Row: "; for ( i = i2lo; i <= i2hi; i++ ) { std::cout << std::setw(7) << i << " "; } std::cout << "\n"; std::cout << " Col\n"; std::cout << "\n"; j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { std::cout << std::setw(5) << j << " "; for ( i2 = 1; i2 <= inc; i2++ ) { i = i2lo - 1 + i2; std::cout << std::setw(14) << a[(i-1)+(j-1)*m]; } std::cout << "\n"; } } return; # undef INCX }
void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo, int ihi, int jhi, std::string title ) { # define INCX 10 int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; std::cout << "\n"; std::cout << title << "\n"; // // Print the columns of the matrix, in strips of INCX. // for ( i2lo = ilo; i2lo <= ihi; i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); std::cout << "\n"; // // For each row I in the current range... // // Write the header. // std::cout << " Row: "; for ( i = i2lo; i <= i2hi; i++ ) { std::cout << std::setw(6) << i << " "; } std::cout << "\n"; std::cout << " Col\n"; std::cout << "\n"; // // Determine the range of the rows in this strip. // j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { // // Print out (up to INCX) entries in column J, that lie in the current strip. // std::cout << std::setw(5) << j << " "; for ( i = i2lo; i <= i2hi; i++ ) { std::cout << std::setw(6) << a[i-1+(j-1)*m] << " "; } std::cout << "\n"; } } return; # undef INCX }
int i4_wrap ( int ival, int ilo, int ihi ) { int jhi; int jlo; int value; int wide; jlo = i4_min ( ilo, ihi ); jhi = i4_max ( ilo, ihi ); wide = jhi + 1 - jlo; if ( wide == 1 ) { value = jlo; } else { value = jlo + i4_modp ( ival - jlo, wide ); } return value; }
void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, char *title ) /******************************************************************************/ /* Purpose: R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed. Licensing: This code is distributed under the GNU LGPL license. Modified: 20 August 2010 Author: John Burkardt Parameters: Input, int M, N, the number of rows and columns. Input, double A[M*N], an M by N matrix to be printed. Input, int ILO, JLO, the first row and column to print. Input, int IHI, JHI, the last row and column to print. Input, char *TITLE, a title. */ { # define INCX 5 int i; int i2; int i2hi; int i2lo; int inc; int j; int j2hi; int j2lo; fprintf ( stdout, "\n" ); fprintf ( stdout, "%s\n", title ); if ( m <= 0 || n <= 0 ) { fprintf ( stdout, "\n" ); fprintf ( stdout, " (None)\n" ); return; } for ( i2lo = i4_max ( ilo, 1 ); i2lo <= i4_min ( ihi, m ); i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); inc = i2hi + 1 - i2lo; fprintf ( stdout, "\n" ); fprintf ( stdout, " Row:" ); for ( i = i2lo; i <= i2hi; i++ ) { fprintf ( stdout, " %7d ", i - 1 ); } fprintf ( stdout, "\n" ); fprintf ( stdout, " Col\n" ); fprintf ( stdout, "\n" ); j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { fprintf ( stdout, "%5d:", j - 1 ); for ( i2 = 1; i2 <= inc; i2++ ) { i = i2lo - 1 + i2; fprintf ( stdout, " %14f", a[(i-1)+(j-1)*m] ); } fprintf ( stdout, "\n" ); } } return; # undef INCX }
void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo, int ihi, int jhi, char *title ) /******************************************************************************/ /* Purpose: I4MAT_TRANSPOSE_PRINT_SOME prints some of an I4MAT, transposed. Discussion: An I4MAT is an MxN array of I4's, stored by (I,J) -> [I+J*M]. Licensing: This code is distributed under the GNU LGPL license. Modified: 20 August 2010 Author: John Burkardt Parameters: Input, int M, the number of rows of the matrix. M must be positive. Input, int N, the number of columns of the matrix. N must be positive. Input, int A[M*N], the matrix. Input, int ILO, JLO, IHI, JHI, designate the first row and column, and the last row and column to be printed. Input, char *TITLE, a title. */ { # define INCX 10 int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; fprintf ( stdout, "\n" ); fprintf ( stdout, "%s\n", title ); if ( m <= 0 || n <= 0 ) { fprintf ( stdout, "\n" ); fprintf ( stdout, " (None)" ); return; } /* Print the columns of the matrix, in strips of INCX. */ for ( i2lo = ilo; i2lo <= ihi; i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); fprintf ( stdout, "\n" ); /* For each row I in the current range... Write the header. */ fprintf ( stdout, " Row: " ); for ( i = i2lo; i <= i2hi; i++ ) { fprintf ( stdout, "%6d ", i - 1 ); } fprintf ( stdout, "\n" ); fprintf ( stdout, " Col\n" ); fprintf ( stdout, "\n" ); /* Determine the range of the rows in this strip. */ j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { /* Print out (up to INCX) entries in column J, that lie in the current strip. */ fprintf ( stdout, "%5d: ", j - 1 ); for ( i = i2lo; i <= i2hi; i++ ) { fprintf ( stdout, "%6d ", a[i-1+(j-1)*m] ); } fprintf ( stdout, "\n" ); } } return; # undef INCX }
void smolyak_coefficients ( int l_max, int m, int c[], int w[] ) /******************************************************************************/ /* Purpose: SMOLYAK_COEFFICIENTS returns the Smolyak coefficients and counts. Discussion: The Smolyak sparse interpolant can be written as: A(L,M)(X) = sum ( L-M+1 <= |L| <= L_max ) C(|L|) * g(l1)(x1) * g(l2)(x2) * ... * g(lm)(xm). where: * L=(l1,l2,...,lm) is a vector of M nonnegative integers; * |L| is the sum of the entries of L; * X=(x1,x2,...,xm) is an M-dimensional point in a product space; * g(i)(xj) is the i-th 1-d interpolation function in dimension j; Note that: * W(|L|) will represent the number of distinct interpolants for which the sublevel, or sum of the L vector entries, is |L|; * the coefficients C and counts W will be zero for sublevels 0 through L_MAX - M (and MATLAB indices 1 through L_MAX-M+1). * it will be the case that W' * C = 1, essentially because the interpolant to the identity function must be the identity function. Licensing: This code is distributed under the GNU LGPL license. Modified: 05 October 2012 Author: John Burkardt Parameters: Input, int L_MAX, the (maximum) level. 0 <= L_MA. Input, int M, the spatial dimension. 1 <= M. Output, int C[L_MAX+1], the coefficients for objects at sublevels 0 through L_MAX. Output, int W[L_MAX+1], the number of objects at sublevels 0 through L_MAX. */ { int l; int l_min; l_min = i4_max ( l_max - m + 1, 0 ); for ( l = 0; l < l_min; l++ ) { c[l] = 0; } for ( l = l_min; l <= l_max; l++ ) { c[l] = i4_mop ( l_max - l ) * i4_choose ( m - 1, l_max - l ); } for ( l = 0; l < l_min; l++ ) { w[l] = 0; } for ( l = l_min; l <= l_max; l++ ) { w[l] = i4_choose ( l + m - 1, m - 1 ); } return; }
int i4_choose ( int n, int k ) /******************************************************************************/ /* Purpose: I4_CHOOSE computes the binomial coefficient C(N,K). Discussion: The value is calculated in such a way as to avoid overflow and roundoff. The calculation is done in integer arithmetic. The formula used is: C(N,K) = N! / ( K! * (N-K)! ) Licensing: This code is distributed under the GNU LGPL license. Modified: 03 May 2008 Author: John Burkardt Reference: ML Wolfson, HV Wright, Algorithm 160: Combinatorial of M Things Taken N at a Time, Communications of the ACM, Volume 6, Number 4, April 1963, page 161. Parameters: Input, int N, K, are the values of N and K. Output, int I4_CHOOSE, the number of combinations of N things taken K at a time. */ { int i; int mn; int mx; int value; mn = i4_min ( k, n - k ); if ( mn < 0 ) { value = 0; } else if ( mn == 0 ) { value = 1; } else { mx = i4_max ( k, n - k ); value = mx + 1; for ( i = 2; i <= mn; i++ ) { value = ( value * ( mx + i ) ) / i; } } return value; }
void st_header_read ( char *input_filename, int *i_min, int *i_max, int *j_min, int *j_max, int *m, int *n, int *nst ) /******************************************************************************/ /* Purpose: ST_HEADER_READ reads the header of an ST file. Licensing: This code is distributed under the GNU LGPL license. Modified: 25 January 2014 Author: John Burkardt Parameters: Input, char *INPUT_FILENAME, the name of the ST file. Input, int *I_MIN, *I_MAX, the minimum and maximum rows. Input, int *J_MIN, *J_MAX, the minimum and maximum columns. Output, int *M, the number of rows. Output, int *N, the number of columns. Output, int *NST, the number of nonzeros. */ { double aij; int i; const int i4_huge = 2147483647; FILE *input; int j; int num; input = fopen ( input_filename, "rt" ); *nst = 0; *i_min = + i4_huge; *i_max = - i4_huge; *j_min = + i4_huge; *j_max = - i4_huge; for ( ; ; ) { num = fscanf ( input, "%i%i%lf", &i, &j, &aij ); if ( num != 3 ) { break; } *nst = *nst + 1; *i_min = i4_min ( *i_min, i ); *i_max = i4_max ( *i_max, i ); *j_min = i4_min ( *j_min, j ); *j_max = i4_max ( *j_max, j ); } fclose ( input ); *m = *i_max - *i_min + 1; *n = *j_max - *j_min + 1; return; }
int i4_uniform ( int a, int b, int *seed ) /******************************************************************************/ /* Purpose: I4_UNIFORM returns a scaled pseudorandom I4. Discussion: The pseudorandom number should be uniformly distributed between A and B. Licensing: This code is distributed under the GNU LGPL license. Modified: 12 November 2006 Author: John Burkardt Reference: Paul Bratley, Bennett Fox, Linus Schrage, A Guide to Simulation, Springer Verlag, pages 201-202, 1983. Pierre L'Ecuyer, Random Number Generation, in Handbook of Simulation, edited by Jerry Banks, Wiley Interscience, page 95, 1998. Bennett Fox, Algorithm 647: Implementation and Relative Efficiency of Quasirandom Sequence Generators, ACM Transactions on Mathematical Software, Volume 12, Number 4, pages 362-376, 1986. Peter Lewis, Allen Goodman, James Miller A Pseudo-Random Number Generator for the System/360, IBM Systems Journal, Volume 8, pages 136-143, 1969. Parameters: Input, int A, B, the limits of the interval. Input/output, int *SEED, the "seed" value, which should NOT be 0. On output, SEED has been updated. Output, int I4_UNIFORM, a number between A and B. */ { int k; float r; int value; if ( *seed == 0 ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "I4_UNIFORM - Fatal error!\n" ); fprintf ( stderr, " Input value of SEED = 0.\n" ); exit ( 1 ); } k = *seed / 127773; *seed = 16807 * ( *seed - k * 127773 ) - k * 2836; if ( *seed < 0 ) { *seed = *seed + 2147483647; } r = ( float ) ( *seed ) * 4.656612875E-10; /* Scale R to lie between A-0.5 and B+0.5. */ r = ( 1.0 - r ) * ( ( float ) ( i4_min ( a, b ) ) - 0.5 ) + r * ( ( float ) ( i4_max ( a, b ) ) + 0.5 ); /* Use rounding to convert R to an integer between A and B. */ value = r4_nint ( r ); value = i4_max ( value, i4_min ( a, b ) ); value = i4_min ( value, i4_max ( a, b ) ); return value; }
void spy_file ( char *header, char *data_filename ) /******************************************************************************/ /* Purpose: SPY_FILE plots a sparsity pattern stored in a file. Licensing: This code is distributed under the GNU LGPL license. Modified: 16 September 2014 Author: John Burkardt Parameters: Input, char *HEADER, the name to be used for the title of the plot, and as part of the names of the command and plot files. Input, char *DATA_FILENAME, the name of the file containing the indices of nonzero matrix entries. */ { char command_filename[255]; FILE *command_unit; FILE *data_unit; int i; const int i4_huge = 2147483647; int j; int m0; int m1; int n0; int n1; int nz_num; char png_filename[255]; int status; n0 = + i4_huge; n1 = - i4_huge; m0 = + i4_huge; m1 = - i4_huge; nz_num = 0; data_unit = fopen ( data_filename, "rt" ); for ( ; ; ) { status = fscanf ( data_unit, "%d%d", &i, &j ); if ( status != 2 ) { break; } nz_num = nz_num + 1; m0 = i4_min ( m0, i ); m1 = i4_max ( m1, i ); n0 = i4_min ( n0, j ); n1 = i4_max ( n1, j ); } fclose ( data_unit ); /* Create command file. */ strcpy ( command_filename, header ); strcat ( command_filename, "_commands.txt" ); command_unit = fopen ( command_filename, "wt" ); fprintf ( command_unit, "# %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "# Usage:\n" ); fprintf ( command_unit, "# gnuplot < %s\n", command_filename ); fprintf ( command_unit, "#\n" ); fprintf ( command_unit, "unset key\n" ); fprintf ( command_unit, "set term png\n" ); strcpy ( png_filename, header ); strcat ( png_filename, ".png" ); fprintf ( command_unit, "set output '%s'\n", png_filename ); fprintf ( command_unit, "set size ratio -1\n" ); fprintf ( command_unit, "set xlabel '<--- J --->'\n" ); fprintf ( command_unit, "set ylabel '<--- I --->'\n" ); fprintf ( command_unit, "set title '%d nonzeros for \"%s\"'\n", nz_num, header ); fprintf ( command_unit, "set timestamp\n" ); fprintf ( command_unit, "plot [y=%d:%d] [x=%d:%d] '%s' with points pt 5\n", m0, m1, n0, n1, data_filename ); fclose ( command_unit ); printf ( " Created graphics command file '%s'\n", command_filename ); return; }
void mesh_base_zero ( int node_num, int element_order, int element_num, int element_node[] ) /******************************************************************************/ /* Purpose: MESH_BASE_ZERO ensures that the element definition is zero-based. Discussion: The ELEMENT_NODE array contains nodes indices that form elements. The convention for node indexing might start at 0 or at 1. Since a C or C++ program will naturally assume a 0-based indexing, it is necessary to check a given element definition and, if it is actually 1-based, to convert it. This function attempts to detect 1-based node indexing and correct it. Licensing: This code is distributed under the GNU LGPL license. Modified: 08 October 2010 Author: John Burkardt Parameters: Input, int NODE_NUM, the number of nodes. Input, int ELEMENT_ORDER, the order of the elements. Input, int ELEMENT_NUM, the number of elements. Input/output, int ELEMENT_NODE[ELEMENT_ORDER*ELEMENT_NUM], the element definitions. */ { int element; int node; int node_max; int node_min; int order; node_min = node_num + 1; node_max = -1; for ( element = 0; element < element_num; element++ ) { for ( order = 0; order < element_order; order++ ) { node = element_node[order+element*element_order]; node_min = i4_min ( node_min, node ); node_max = i4_max ( node_max, node ); } } if ( node_min == 1 && node_max == node_num ) { printf ( "\n" ); printf ( "MESH_BASE_ZERO:\n" ); printf ( " The element indexing appears to be 1-based!\n" ); printf ( " This will be converted to 0-based.\n" ); for ( element = 0; element < element_num; element++ ) { for ( order = 0; order < element_order; order++ ) { element_node[order+element*element_order] = element_node[order+element*element_order] - 1; } } } else if ( node_min == 0 && node_max == node_num - 1 ) { printf ( "\n" ); printf ( "MESH_BASE_ZERO:\n" ); printf ( " The element indexing appears to be 0-based!\n" ); printf ( " No conversion is necessary.\n" ); } else { printf ( "\n" ); printf ( "MESH_BASE_ZERO - Warning!\n" ); printf ( " The element indexing is not of a recognized type.\n" ); printf ( " NODE_MIN = %d\n", node_min ); printf ( " NODE_MAX = %d\n", node_max ); printf ( " NODE_NUM = %d\n", node_num ); } return; }
void r8utp_print_some ( int n, double a[], int ilo, int jlo, int ihi, int jhi, char *title ) /******************************************************************************/ /* Purpose: R8UTP_PRINT_SOME prints some of a R8UTP matrix. Discussion: The R8UTP storage format is appropriate for an upper triangular matrix. Only the upper triangle of the matrix is stored, by successive partial columns, in an array of length (N*(N+1))/2, which contains (A11,A12,A22,A13,A23,A33,A14,...,ANN) Licensing: This code is distributed under the GNU LGPL license. Modified: 16 April 2014 Author: John Burkardt Parameters: Input, int N, the order of the matrix. N must be positive. Input, double A[(N*(N+1))/2], the matrix. Input, int ILO, JLO, IHI, JHI, designate the first row and column, and the last row and column to be printed. Input, char *TITLE, a title. */ { # define INCX 5 double aij; int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; printf ( "\n" ); printf ( "%s\n", title ); /* Print the columns of the matrix, in strips of 5. */ for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX ) { j2hi = j2lo + INCX - 1; j2hi = i4_min ( j2hi, n ); j2hi = i4_min ( j2hi, jhi ); printf ( "\n" ); printf ( " Col: " ); for ( j = j2lo; j <= j2hi; j++ ) { printf ( "%7d ", j ); } printf ( "\n" ); printf ( " Row\n" ); printf ( " ---\n" ); /* Determine the range of the rows in this strip. */ i2lo = i4_max ( ilo, 1 ); i2hi = i4_min ( ihi, n ); for ( i = i2lo; i <= i2hi; i++ ) { printf ( "%6d ", i ); /* Print out (up to) 5 entries in row I, that lie in the current strip. */ for ( j = j2lo; j <= j2hi; j++ ) { if ( i <= j ) { aij = a[i-1+(j*(j-1))/2]; } else { aij = 0.0; } printf ( "%12g ", aij ); } printf ( "\n" ); } } return; # undef INCX }
void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, char *title ) /******************************************************************************/ /* Purpose: R8MAT_PRINT_SOME prints some of an R8MAT. Discussion: An R8MAT is a doubly dimensioned array of R8's, which may be stored as a vector in column-major order. Licensing: This code is distributed under the GNU LGPL license. Modified: 20 August 2010 Author: John Burkardt Parameters: Input, int M, the number of rows of the matrix. M must be positive. Input, int N, the number of columns of the matrix. N must be positive. Input, double A[M*N], the matrix. Input, int ILO, JLO, IHI, JHI, designate the first row and column, and the last row and column to be printed. Input, char *TITLE, a title. */ { # define INCX 5 int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; fprintf ( stdout, "\n" ); fprintf ( stdout, "%s\n", title ); if ( m <= 0 || n <= 0 ) { fprintf ( stdout, "\n" ); fprintf ( stdout, " (None)\n" ); return; } /* Print the columns of the matrix, in strips of 5. */ for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX ) { j2hi = j2lo + INCX - 1; j2hi = i4_min ( j2hi, n ); j2hi = i4_min ( j2hi, jhi ); fprintf ( stdout, "\n" ); /* For each column J in the current range... Write the header. */ fprintf ( stdout, " Col: "); for ( j = j2lo; j <= j2hi; j++ ) { fprintf ( stdout, " %7d ", j - 1 ); } fprintf ( stdout, "\n" ); fprintf ( stdout, " Row\n" ); fprintf ( stdout, "\n" ); /* Determine the range of the rows in this strip. */ i2lo = i4_max ( ilo, 1 ); i2hi = i4_min ( ihi, m ); for ( i = i2lo; i <= i2hi; i++ ) { /* Print out (up to) 5 entries in row I, that lie in the current strip. */ fprintf ( stdout, "%5d:", i - 1 ); for ( j = j2lo; j <= j2hi; j++ ) { fprintf ( stdout, " %14f", a[i-1+(j-1)*m] ); } fprintf ( stdout, "\n" ); } } return; # undef INCX }