Пример #1
0
static int generate(struct ccdrbg_state *drbg, size_t dataOutLength, void *dataOut,
                    size_t additionalLength, const void *additional)
{
    struct ccdrbg_nisthmac_state *state = (struct ccdrbg_nisthmac_state *)drbg;
    const struct ccdrbg_nisthmac_custom *custom = state->custom;
    const struct ccdigest_info *di = custom->di;
    
    int rc = validate_gen_params(state->reseed_counter, dataOutLength, additional==NULL?0:additionalLength);
    if(rc!=CCDRBG_STATUS_OK) return rc;
    
    // 2. If additional_input ≠ Null, then (Key, V) = HMAC_DRBG_Update (additional_input, Key, V).
    if (additional && additionalLength)
        hmac_dbrg_update(drbg, additionalLength, additional, 0, NULL, 0, NULL);
    
    // hmac_dbrg_generate_algorithm
    char *outPtr = (char *) dataOut;
    while (dataOutLength > 0) {
        if (!state->bytesLeft) {
            //  5. V=HMAC(K,V).
            cchmac(di, state->keysize, state->key, state->vsize, state->nextvptr, state->vptr);        // Won't be returned
            // FIPS 140-2 4.9.2 Conditional Tests
            // "Each subsequent generation of an n-bit block shall be compared with the previously generated block. The test shall fail if any two compared n-bit blocks are equal."
            if (0==cc_cmp_safe(state->vsize, state->vptr, state->nextvptr)) {
                //The world as we know it has come to an end
                //the DRBG data structure is zeroized. subsequent calls to
                //DRBG ends up in NULL dereferencing and/or unpredictable state.
                //catastrophic error in SP 800-90A
                done(drbg);
                rc=CCDRBG_STATUS_ABORT;
                cc_abort(NULL);
                goto errOut;
            }
            CC_SWAP(state->nextvptr, state->vptr);
            state->bytesLeft = state->vsize;
#if DRBG_NISTHMAC_DEBUG
            cc_print("generate blk: ", state->vsize, state->vptr);
#endif
        }
        size_t outLength = dataOutLength > state->bytesLeft ? state->bytesLeft : dataOutLength;
        CC_MEMCPY(outPtr, state->vptr, outLength);
        state->bytesLeft -= outLength;
        outPtr += outLength;
        dataOutLength -= outLength;
    }

    // 6. (Key, V) = HMAC_DRBG_Update (additional_input, Key, V).
    hmac_dbrg_update(drbg, additionalLength, additional, 0, NULL, 0, NULL);
    
    // 7. reseed_counter = reseed_counter + 1.
    state->reseed_counter++;
    
#if DRBG_NISTHMAC_DEBUG
    dumpState("generate end: ", state);
    cc_print("generate end nxt: ", state->vsize, state->nextvptr);
#endif
    rc=CCDRBG_STATUS_OK;
errOut:
    return rc;
}
Пример #2
0
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 tests CC_HEADER_READ and CC_DATA_READ.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    16 July 2014

  Author:

    John Burkardt
*/
{
  double *acc;
  int *ccc;
  int *icc;
  int m;
  int n;
  int ncc;
  char prefix[] = "simple";

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  Read a sparse matrix in CC format from 3 files.\n" );
/*
  Read the header.
*/
  cc_header_read ( prefix, &ncc, &n );
/*
  Allocate space.
*/
  acc = ( double * ) malloc ( ncc * sizeof ( double ) );
  ccc = ( int * ) malloc ( ( n + 1 ) * sizeof ( int ) );
  icc = ( int * ) malloc ( ncc * sizeof ( int ) );
/*
  Read the matrix data.
*/
  cc_data_read ( prefix, ncc, n, icc, ccc, acc );
/*
  Print the CC matrix.
*/
  m = n;
  cc_print ( m, n, ncc, icc, ccc, acc, "  The matrix in 0-based CC format:" );
/*
  Free memory.
*/
  free ( acc );
  free ( ccc );
  free ( icc );

  return;
}
Пример #3
0
static int hmac_dbrg_error(int val, const char *msg) {
    if (msg) {
        char buffer[1024];
        snprintf(buffer, sizeof(buffer)-1, "Error: %s", msg);
        cc_print(buffer, 0, NULL);
    }
    return val;
}
Пример #4
0
static int tests_rng(const char *seed) {
    byteBuffer entropyBuffer;
    int status=-1; // Allocation error;

    if (seed==NULL || strlen(seed)<=0) {
        // If the seed is not in the argument, we generate one
        struct ccrng_system_state system_rng;
        size_t entropy_size=16; // Default size of the seed
        cc_require((entropyBuffer=mallocByteBuffer(entropy_size))!=NULL,errOut);
        cc_require((status=ccrng_system_init(&system_rng))==0, errOut);
        cc_require((status=ccrng_generate((struct ccrng_state *)&system_rng, entropyBuffer->len, entropyBuffer->bytes))==0, errOut);
        ccrng_system_done(&system_rng);
        cc_print("random seed value:",entropyBuffer->len,entropyBuffer->bytes);
    } else {
        // Otherwise, take the value from the arguments
        entropyBuffer = hexStringToBytes(seed);
        cc_print("input seed value:",entropyBuffer->len,entropyBuffer->bytes);
    }
    cc_require((status=ccrng_test_init(&test_rng, entropyBuffer->len,entropyBuffer->bytes))==0, errOut);
    return status;
errOut:
    printf("Error initializing test rng: %d\n",status);
    return -1;
}
Пример #5
0
static void dumpState(const char *label, struct ccdrbg_nisthmac_state *state) {
    cc_print(label, state->vsize, state->v);
    cc_print(label, state->keysize, state->key);
}
Пример #6
0
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 tests ST_TO_CC using a tiny matrix.

  Discussion:

    This test uses a trivial matrix whose full representation is:

          2  3  0  0  0
          3  0  4  0  6
      A = 0 -1 -3  2  0
          0  0  1  0  0
          0  4  2  0  1

    A (1-based) ST representation, reading in order by rows is:

      I  J   A
     -- --  --
      1  1   2
      1  2   3

      2  1   3
      2  3   4
      2  5   6

      3  2  -1
      3  3  -3
      3  4   2

      4  3   1

      5  2   4
      5  3   2
      5  5   1

    The CC representation (which goes in order by columns) is

      #   I  JC   A
     --  --  --  --
      1   1   1   2
      2   2       3

      3   1   3   3
      4   3      -1
      5   5       4

      6   2   6   4
      7   3      -3
      8   4       1
      9   5       2

     10   3  10   2

     11   2  11   6
     12   5       1

     13   *  13

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    23 July 2014

  Author:

    John Burkardt
*/
{
# define NST 12

  double *acc;
  double ast[NST] = {
    2.0,  3.0, 
    3.0,  4.0,  6.0, 
   -1.0, -3.0,  2.0, 
    1.0, 
    4.0,  2.0,  1.0 };
  int *ccc;
  int i_max;
  int i_min;
  int *icc;
  int ist[NST] = {
    1, 1, 
    2, 2, 2, 
    3, 3, 3, 
    4, 
    5, 5, 5 };
  int j_max;
  int j_min;
  int jst[NST] = {
    1, 2, 
    1, 3, 5, 
    2, 3, 4, 
    3, 
    2, 3, 5 };
  int m = 5;
  int n = 5;
  int ncc;
  int nst = NST;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Convert a sparse matrix from ST to CC format.\n" );
  printf ( "  ST: sparse triplet,    I, J,  A.\n" );
  printf ( "  CC: compressed column, I, CC, A.\n" );

  i_min = i4vec_min ( nst, ist );
  i_max = i4vec_max ( nst, ist );
  j_min = i4vec_min ( nst, jst );
  j_max = i4vec_max ( nst, jst );

  st_header_print ( i_min, i_max, j_min, j_max, m, n, nst );
/*
  Decrement the 1-based data.
*/
  i4vec_dec ( nst, ist );
  i4vec_dec ( nst, jst );
/*
  Print the ST matrix.
*/
  st_print ( m, n, nst, ist, jst, ast, "  The matrix in ST format:" );
/*
  Get the CC size.
*/
  ncc = st_to_cc_size ( nst, ist, jst );

  printf ( "\n" );
  printf ( "  Number of CC values = %d\n", ncc );
/*
  Create the CC indices.
*/
  icc = ( int * ) malloc ( ncc * sizeof ( int ) );
  ccc = ( int * ) malloc ( ( n + 1 ) * sizeof ( int ) );

  st_to_cc_index ( nst, ist, jst, ncc, n, icc, ccc );
/*
  Create the CC values.
*/
  acc = st_to_cc_values ( nst, ist, jst, ast, ncc, n, icc, ccc );
/*
  Print the CC matrix.
*/
  cc_print ( m, n, ncc, icc, ccc, acc, "  CC Matrix:" );
/*
  Free memory.
*/
  free ( acc );
  free ( ccc );
  free ( icc );

  return;
# undef NST
}
Пример #7
0
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 tests ST_TO_CC on a matrix stored in a file.

  Discussion:

    We assume no prior knowledge about the matrix except the filename.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    23 July 2014

  Author:

    John Burkardt
*/
{
  double *acc;
  double *ast;
  int *ccc;
  char filename_st[] = "west_st.txt";
  int i_max;
  int i_min;
  int *icc;
  int *ist;
  int j_max;
  int j_min;
  int *jst;
  int m;
  int n;
  int ncc;
  int nst;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  Convert a sparse matrix from ST to CC format.\n" );
  printf ( "  ST: sparse triplet,    I, J,  A.\n" );
  printf ( "  CC: compressed column, I, CC, A.\n" );
  printf ( "  This matrix is read from the file '%s'\n", filename_st );
/*
  Get the size of the ST matrix.
*/
  st_header_read ( filename_st, &i_min, &i_max, &j_min, &j_max, &m, &n, &nst );

  st_header_print ( i_min, i_max, j_min, j_max, m, n, nst );
/*
  Allocate space.
*/
  ist = ( int * ) malloc ( nst * sizeof ( int ) );
  jst = ( int * ) malloc ( nst * sizeof ( int ) );
  ast = ( double * ) malloc ( nst * sizeof ( double ) );
/*
  Read the ST matrix.
*/
  st_data_read ( filename_st, m, n, nst, ist, jst, ast );
/*
  Decrement the 1-based data.
*/
  i4vec_dec ( nst, ist );
  i4vec_dec ( nst, jst );
/*
  Print the ST matrix.
*/
  st_print ( m, n, nst, ist, jst, ast, "  The matrix in ST format:" );
/*
  Get the CC size.
*/
  ncc = st_to_cc_size ( nst, ist, jst );

  printf ( "\n" );
  printf ( "  Number of CC values = %d\n", ncc );
/*
  Create the CC indices.
*/
  icc = ( int * ) malloc ( ncc * sizeof ( int ) );
  ccc = ( int * ) malloc ( ( n + 1 ) * sizeof ( int ) );

  st_to_cc_index ( nst, ist, jst, ncc, n, icc, ccc );
/*
  Create the CC values.
*/
  acc = st_to_cc_values ( nst, ist, jst, ast, ncc, n, icc, ccc );
/*
  Print the CC matrix.
*/
  cc_print ( m, n, ncc, icc, ccc, acc, "  CC Matrix:" );
/*
  Free memory.
*/
  free ( acc );
  free ( ast );
  free ( ccc );
  free ( icc );
  free ( ist );
  free ( jst );

  return;
}
int
ccecies_encrypt_gcm_composite(ccec_pub_ctx_t public_key,
                    const ccecies_gcm_t ecies,
                    uint8_t *exported_public_key, /* output - length from ccecies_pub_key_size */
                    uint8_t *ciphertext,          /* output - length same as plaintext_len */
                    uint8_t *mac_tag,             /* output - length ecies->mac_length */
                    size_t plaintext_len,   const uint8_t *plaintext,
                    size_t sharedinfo1_byte_len, const void *sharedinfo_1,
                    size_t sharedinfo2_byte_len, const void *sharedinfo_2
)
{
    int status=-1;

    // Contexts:
    ccec_full_ctx_decl_cp(ccec_ctx_cp(public_key), ephemeral_key);
    size_t   skey_size = ccec_cp_prime_size(ccec_ctx_cp(public_key));
    uint8_t  skey[skey_size];
    const struct ccmode_gcm *gcm_encrypt=ecies->gcm;
    ccgcm_ctx_decl(gcm_encrypt->size,gcm_ctx);
    size_t exported_public_key_size;

    // 1) Generate ephemeral EC key pair
    cc_assert(ecies->rng!=NULL);
    cc_require(ccecdh_generate_key(ccec_ctx_cp(public_key), ecies->rng, ephemeral_key)==0,errOut);

#if CC_DEBUG_ECIES
    ccec_print_full_key("Ephemeral key",ephemeral_key);
#endif

    // 2) ECDH with input public key
    cc_require(ccecdh_compute_shared_secret(ephemeral_key, public_key, &skey_size, skey,ecies->rng)==0,errOut);

#if CC_DEBUG_ECIES
    cc_print("Shared secret key",skey_size,skey);
#endif

    // 3) Export ephemeral public key
    cc_require( ccecies_export(0, ecies->options, exported_public_key, ephemeral_key)==0, errOut);

    // 4) Derive Enc / Mac key
    // Hash(skey|00000001|sharedinfo_1)
    cc_assert(ecies->key_length<=skey_size);
    exported_public_key_size=ccecies_pub_key_size(ephemeral_key,ecies);
    if (ECIES_EPH_PUBKEY_IN_SHAREDINFO1 == (ecies->options & ECIES_EPH_PUBKEY_IN_SHAREDINFO1))
    {   // use ephemeral public key as shared info 1
        cc_require(ccansikdf_x963(ecies->di,
                                  skey_size,skey,
                                  exported_public_key_size,exported_public_key,
                                  ecies->key_length,skey)==0,errOut);
    }
    else
    {
        cc_require(ccansikdf_x963(ecies->di,
                                  skey_size,skey,
                                  sharedinfo1_byte_len,sharedinfo_1,
                                  ecies->key_length,skey)==0,errOut);
    }

#if CC_DEBUG_ECIES
    cc_print("Cipher key",ecies->key_length,skey);
#endif

    // 5) Encrypt
    ccgcm_init(gcm_encrypt, gcm_ctx,ecies->key_length,skey);
    ccgcm_set_iv(gcm_encrypt,gcm_ctx,sizeof(ecies_iv_data),ecies_iv_data);
    if ((sharedinfo_2!=NULL) && (sharedinfo2_byte_len>0)) {
        ccgcm_gmac(gcm_encrypt,gcm_ctx,sharedinfo2_byte_len,sharedinfo_2);
    }
    else
    {
        ccgcm_gmac(gcm_encrypt,gcm_ctx,0,NULL);
    }
    ccgcm_update(gcm_encrypt,gcm_ctx,
                 plaintext_len,plaintext,
                 ciphertext);

#if CC_DEBUG_ECIES
    cc_print("Encrypted message",plaintext_len,ciphertext);
#endif

    // 6) Mac (with SharedInfo 2)
    // sec1, p51: recommended: SharedInfo2 ended in a counter giving its length.
    ccgcm_finalize(gcm_encrypt,gcm_ctx,ecies->mac_length,mac_tag);
#if CC_DEBUG_ECIES
    cc_print("Mac Tag",ecies->mac_length,mac_tag);
#endif

    // Success
    status=0;

errOut:
    // Clear key material info
    ccgcm_ctx_clear(gcm_encrypt->size,gcm_ctx);
    cc_clear(sizeof(skey),skey);
    ccec_full_ctx_clear_cp(ccec_ctx_cp(public_key), ephemeral_key);
    return status;
}
Пример #9
0
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 tests CC_WRITE using a tiny matrix.

  Discussion:

    This test uses a trivial matrix whose full representation is:

          2  3  0  0  0
          3  0  4  0  6
      A = 0 -1 -3  2  0
          0  0  1  0  0
          0  4  2  0  1

    The 1-based CC representation is

      #  ICC  CCC  ACC
     --  ---  ---  ---
      1    1    1    2
      2    2         3

      3    1    3    3
      4    3        -1
      5    5         4

      6    2    6    4
      7    3        -3
      8    4         1
      9    5         2

     10    3   10    2

     11    2   11    6
     12    5         1

     13    *   13

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    16 July 2014

  Author:

    John Burkardt
*/
{
# define N 5
# define NCC 12

  double acc[NCC] = {
    2.0,  3.0,
    3.0, -1.0,  4.0,
    4.0, -3.0,  1.0, 2.0,
    2.0,
    6.0, 1.0 };
  int ccc[N+1] = {
    1, 3, 6, 10, 11, 13 };
  int icc[NCC] = {
    1, 2,
    1, 3, 5,
    2, 3, 4, 5,
    3,
    2, 5 };
  int m = N;
  int n = N;
  int ncc = NCC;
  char prefix[] = "simple";

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Write a sparse matrix in CC format to 3 files.\n" );
/*
  Full storage statistics
*/
  printf ( "\n" );
  printf ( "  Full rows    M = %d\n", m );
  printf ( "  Full columns N = %d\n", n );
  printf ( "  Full storage   = %d\n", m * n );
/*
  Decrement the 1-based data.
*/
  i4vec_dec ( n + 1, ccc );
  i4vec_dec ( ncc, icc );
/*
  Print the CC matrix.
*/
  cc_print ( m, n, ncc, icc, ccc, acc, "  The matrix in 0-based CC format:" );
/*
  Write the matrix to 3 files.
*/
  cc_write ( prefix, ncc, n, icc, ccc, acc );

  return;
# undef NCC
}
Пример #10
0
int main ( )

/******************************************************************************/
/*
  Purpose:

    MAIN is the main program for UMFPACK_WEST.

  Discussion:

    This program uses UMFPACK to solve a linear system A*X=B for which the
    matrix is stored, in compressed column (CC) format, in three files.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    16 July 2014

  Author:

    John Burkardt

  Reference:

    Timothy Davis,
    UMFPACK User Guide,
    Version 5.6.2, 25 April 2013
    http://suitesparse.com
*/
{
  double *acc;
  double *b;
  int *ccc;
  int i;
  int *icc;
  int m;
  int n;
  int ncc;
  double *null = ( double * ) NULL;
  void *Numeric;
  char prefix[] = "west";
  double r;
  int seed;
  int status;
  void *Symbolic;
  double *x1;
  double *x2;

  timestamp ( );
  printf ( "\n" );
  printf ( "UMFPACK_WEST:\n" );
  printf ( "  C version\n" );
  printf ( "  Use UMFPACK to solve the sparse linear system A*x=b.\n" );
  printf ( "  The matrix A is stored, in CC format, in 3 files.\n" );
/*
  Get the matrix size.
*/
  cc_header_read ( prefix, &ncc, &n );
  printf ( "\n" );
  printf ( "  Number of rows and columns = %d\n", n );
  printf ( "  Number of nonzeros NCC = %d\n", ncc );
/*
  Allocate space.
*/
  acc = ( double * ) malloc ( ncc * sizeof ( double ) );
  ccc = ( int * ) malloc ( ( n + 1 ) * sizeof ( int ) );
  icc = ( int * ) malloc ( ncc * sizeof ( int ) );
/*
  Read the matrix data.
*/
  cc_data_read ( prefix, ncc, n, icc, ccc, acc );
/*
  Print the matrix.
*/
  m = n;
  cc_print ( m, n, ncc, icc, ccc, acc, "  The CC matrix:" ); 
/*
  Set up the solution.
*/
  seed = 123456789;
  x1 = r8vec_uniform_01_new ( n, &seed );
/*
  Set the right hand side.
*/
  b = cc_mv ( m, n, ncc, icc, ccc, acc, x1 );
/*
  From the matrix data, create the symbolic factorization information.
*/
  status = umfpack_di_symbolic ( n, n, ccc, icc, acc, &Symbolic, null, null );
/*
  From the symbolic factorization information, carry out the numeric factorization.
*/
  status = umfpack_di_numeric ( ccc, icc, acc, Symbolic, &Numeric, null, null );
/*
  Free the symbolic factorization memory.
*/
  umfpack_di_free_symbolic ( &Symbolic );
/*
  Using the numeric factorization, solve the linear system.
*/
  x2 = ( double * ) malloc ( n * sizeof ( double ) );
  status = umfpack_di_solve ( UMFPACK_A, ccc, icc, acc, x2, b, Numeric, null, null );
/*
  Free the numeric factorization.
*/
  umfpack_di_free_numeric ( &Numeric );
/*
  Compute the error:
*/
  r = r8vec_diff_norm ( n, x1, x2 );
  printf ( "\n" );
  printf ( "  Residual: ||A*x-b|| = %g\n", r );
/*
  Free memory.
*/
  free ( acc );
  free ( b );
  free ( ccc );
  free ( icc );
  free ( x1 );
  free ( x2 );
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "UMFPACK_WEST:\n" );
  printf ( "  Normal end of execution.\n" );
  printf ( "\n" );
  timestamp ( );

  return 0;
}
Пример #11
0
int main ( )

/******************************************************************************/
/*
  Purpose:

    D_SAMPLE_ST tests the SUPERLU solver with a 5x5 double precision real matrix.

  Discussion:

    The general (GE) representation of the matrix is:

      [ 19  0 21 21  0
        12 21  0  0  0
         0 12 16  0  0 
         0  0  0  5 21
        12 12  0  0 18 ]

    The (0-based) compressed column (CC) representation of this matrix is:

      I  CC   A
     --  --  --
      0   0  19
      1      12
      4      12

      1   3  21
      2      12
      4      12

      0   6  21
      2      16

      0   8  21
      3       5

      3  10  21
      4      18

      *  12   *

    The right hand side B and solution X are

      #   B     X
     --  --  ----------
      0   1  -0.03125
      1   1   0.0654762
      2   1   0.0133929
      3   1   0.0625
      4   1   0.0327381 

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    18 July 2014

  Author:

    John Burkardt

  Reference:

    James Demmel, John Gilbert, Xiaoye Li,
    SuperLU Users's Guide.
*/
{
  SuperMatrix A;
  double *acc;
  double *b;
  double *b2;
  SuperMatrix B;
  int *ccc;
  int i;
  int *icc;
  int info;
  int j;
  SuperMatrix L;
  int m;
  int n;
  int nrhs = 1;
  int ncc;
  superlu_options_t options;
  int *perm_c;
  int permc_spec;
  int *perm_r;
  SuperLUStat_t stat;
  SuperMatrix U;

  timestamp ( );
  printf ( "\n" );
  printf ( "D_SAMPLE_ST:\n" );
  printf ( "  C version\n" );
  printf ( "  SUPERLU solves a double precision real linear system.\n" );
  printf ( "  The matrix is read from a Sparse Triplet (ST) file.\n" );
/*
  Read the matrix from a file associated with standard input,
  in sparse triplet (ST) format, into compressed column (CC) format.
*/
  dreadtriple ( &m, &n, &ncc, &acc, &icc, &ccc );
/*
  Print the matrix.
*/
  cc_print ( m, n, ncc, icc, ccc, acc, "  CC Matrix:" );
/*
  Convert the compressed column (CC) matrix into a SuperMatrix A. 
*/
  dCreate_CompCol_Matrix ( &A, m, n, ncc, acc, icc, ccc, SLU_NC, SLU_D, SLU_GE );    
/*
  Create the right-hand side matrix.
*/
  b = ( double * ) malloc ( m * sizeof ( double ) );
  for ( i = 0; i < m; i++ )
  {
    b[i] = 1.0;
  }
  printf ( "\n" );
  printf ( "  Right hand side:\n" );
  printf ( "\n" );
  for ( i = 0; i < m; i++ )
  {
    printf ( "%g\n", b[i] );
  }
/*
  Create Super Right Hand Side.
*/
  dCreate_Dense_Matrix ( &B, m, nrhs, b, m, SLU_DN, SLU_D, SLU_GE );
/*
  Set space for the permutations.
*/
  perm_r = ( int * ) malloc ( m * sizeof ( int ) );
  perm_c = ( int * ) malloc ( n * sizeof ( int ) );
/*
  Set the input options. 
*/
  set_default_options ( &options );
  options.ColPerm = NATURAL;
/*
  Initialize the statistics variables. 
*/
  StatInit ( &stat );
/*
  Solve the linear system. 
*/
  dgssv ( &options, &A, perm_c, perm_r, &L, &U, &B, &stat, &info );
    
  dPrint_CompCol_Matrix ( ( char * ) "A", &A );
  dPrint_CompCol_Matrix ( ( char * ) "U", &U );
  dPrint_SuperNode_Matrix ( ( char * ) "L", &L );
  print_int_vec ( ( char * ) "\nperm_r", m, perm_r );
/*
  By some miracle involving addresses, 
  the solution has been put into the B vector.
*/
  printf ( "\n" );
  printf ( "  Computed solution:\n" );
  printf ( "\n" );
  for ( i = 0; i < m; i++ )
  {
    printf ( "%g\n", b[i] );
  }
/*
  Demonstrate that RHS is really the solution now.
  Multiply it by the matrix.
*/
  b2 = cc_mv ( m, n, ncc, icc, ccc, acc, b );
  printf ( "\n" );
  printf ( "  Product A*X:\n" );
  printf ( "\n" );
  for ( i = 0; i < m; i++ )
  {
    printf ( "%g\n", b2[i] );
  }
/*
  Free memory.
*/
  free ( b );
  free ( b2 );
  free ( perm_c );
  free ( perm_r );

  Destroy_SuperMatrix_Store ( &A );
  Destroy_SuperMatrix_Store ( &B );
  Destroy_SuperNode_Matrix ( &L );
  Destroy_CompCol_Matrix ( &U );
  StatFree ( &stat );
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "D_SAMPLE_ST:\n" );
  printf ( "  Normal end of execution.\n" );
  printf ( "\n" );
  timestamp ( );

  return 0;
}
Пример #12
0
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 tests CC_TO_ST using a 1-based matrix.

  Discussion:

    This test uses a trivial matrix whose full representation is:

          2  3  0  0  0
          3  0  4  0  6
      A = 0 -1 -3  2  0
          0  0  1  0  0
          0  4  2  0  1

    The 1-based CC representation is

      #  ICC  CCC  ACC
     --  ---  ---  ---
      1    1    1    2
      2    2         3

      3    1    3    3
      4    3        -1
      5    5         4

      6    2    6    4
      7    3        -3
      8    4         1
      9    5         2

     10    3   10    2

     11    2   11    6
     12    5         1

     13    *   13

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    18 July 2014

  Author:

    John Burkardt
*/
{
# define N 5
# define NCC 12

    double acc[NCC] = {
        2.0,  3.0,
        3.0, -1.0,  4.0,
        4.0, -3.0,  1.0, 2.0,
        2.0,
        6.0, 1.0
    };
    double *ast;
    int ccc[N+1] = {
        1, 3, 6, 10, 11, 13
    };
    int i;
    int icc[NCC] = {
        1, 2,
        1, 3, 5,
        2, 3, 4, 5,
        3,
        2, 5
    };
    int *ist;
    int *jst;
    int m = 5;
    int n = N;
    int ncc = NCC;
    int nst;

    printf ( "\n" );
    printf ( "TEST01\n" );
    printf ( "  Convert a 1-based CC matrix to ST format.\n" );
    /*
      Print the CC matrix.
    */
    cc_print ( m, n, ncc, icc, ccc, acc, "  The CC matrix:" );
    /*
      Convert it.
    */
    ist = ( int * ) malloc ( ncc * sizeof ( int ) );
    jst = ( int * ) malloc ( ncc * sizeof ( int ) );
    ast = ( double * ) malloc ( ncc * sizeof ( double ) );

    cc_to_st ( m, n, ncc, icc, ccc, acc, &nst, ist, jst, ast );
    /*
      Print the ST matrix.
    */
    st_print ( m, n, nst, ist, jst, ast, "  The ST matrix:" );
    /*
      Free memory.
    */
    free ( ast );
    free ( ist );
    free ( jst );

    return;
# undef N
# undef NCC
}
Пример #13
0
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 tests CC_TO_ST using a 0-based matrix.

  Discussion:

    This test uses a trivial matrix whose full representation is:

          2  3  0  0  0
          3  0  4  0  6
      A = 0 -1 -3  2  0
          0  0  1  0  0
          0  4  2  0  1

    The 0-based CC representation is

      #  ICC  CCC  ACC
     --  ---  ---  ---
      0    0    0    2
      1    1         3

      2    0    2    3
      3    2        -1
      4    4         4

      5    1    5    4
      6    2        -3
      7    3         1
      8    4         2

      9    2    9    2

     10    1   10    6
     11    4         1

     12    *   12

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    23 July 2014

  Author:

    John Burkardt
*/
{
# define N 5
# define NCC 12

    double acc[NCC] = {
        2.0,  3.0,
        3.0, -1.0,  4.0,
        4.0, -3.0,  1.0, 2.0,
        2.0,
        6.0, 1.0
    };
    double *ast;
    int ccc[N+1] = {
        0, 2, 5, 9, 10, 12
    };
    int i;
    int icc[NCC] = {
        0, 1,
        0, 2, 4,
        1, 2, 3, 4,
        2,
        1, 4
    };
    int *ist;
    int *jst;
    int m = 5;
    int n = N;
    int ncc = NCC;
    int nst;

    printf ( "\n" );
    printf ( "TEST02\n" );
    printf ( "  Convert a 0-based CC matrix to ST format.\n" );
    /*
      Print the CC matrix.
    */
    cc_print ( m, n, ncc, icc, ccc, acc, "  The CC matrix:" );
    /*
      Convert it.
    */
    ist = ( int * ) malloc ( ncc * sizeof ( int ) );
    jst = ( int * ) malloc ( ncc * sizeof ( int ) );
    ast = ( double * ) malloc ( ncc * sizeof ( double ) );

    cc_to_st ( m, n, ncc, icc, ccc, acc, &nst, ist, jst, ast );
    /*
      Print the ST matrix.
    */
    st_print ( m, n, nst, ist, jst, ast, "  The ST matrix:" );
    /*
      Free memory.
    */
    free ( ast );
    free ( ist );
    free ( jst );

    return;
}