Exemplo n.º 1
0
void matrix_copy_block( matrix_type * target_matrix , int target_row , int target_column , int rows , int columns,
                        const matrix_type * src_matrix , int src_row , int src_column) {
  matrix_type * target_view = matrix_alloc_shared(target_matrix , target_row , target_column , rows , columns);
  matrix_type * src_view    = matrix_alloc_shared( src_matrix , src_row , src_column , rows , columns);
  matrix_assign( target_view , src_view );
  matrix_free( target_view );
  matrix_free( src_view );
}
Exemplo n.º 2
0
static void lars_estimate_init( lars_type * lars, matrix_type * X , matrix_type * Y) {
  int nvar     = matrix_get_columns( lars->X );
  
  matrix_assign( X , lars->X );
  matrix_assign( Y , lars->Y );
  if (lars->X_norm != NULL)
    matrix_free( lars->X_norm );
  lars->X_norm = matrix_alloc(1 , nvar );

  if (lars->X_mean != NULL)
    matrix_free( lars->X_mean );
  lars->X_mean = matrix_alloc(1 , nvar );

  if (lars->beta != NULL)
    matrix_free( lars->beta );
  lars->beta = matrix_alloc( nvar , nvar );
  lars->Y_mean = regression_scale( X , Y , lars->X_mean , lars->X_norm);
}
Exemplo n.º 3
0
matrix_type * matrix_realloc_copy(matrix_type * T , const matrix_type * src) {
  if (T == NULL)
    return matrix_alloc_copy( src );
  else {
    matrix_resize( T , src->rows , src->columns , false );
    matrix_assign( T , src );
    return T;
  }
}
Exemplo n.º 4
0
void test_column_equal() {
  matrix_type * m1 = matrix_alloc(5,5);
  matrix_type * m2 = matrix_alloc(5,5);
  matrix_type * m3 = matrix_alloc(6,5);
  rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); 

  matrix_random_init( m1 , rng );
  matrix_assign( m2 , m1 );

  test_assert_true( matrix_columns_equal( m1 , 2 , m2 , 2 ));
  test_assert_false( matrix_columns_equal( m1 , 2 , m2 , 3 ));
  test_assert_false( matrix_columns_equal( m1 , 2 , m3 , 3 ));
  
  rng_free( rng );
  matrix_free( m1 );
  matrix_free( m2 );
  matrix_free( m3 );
}
Exemplo n.º 5
0
void test_resize() {
  matrix_type * m1 = matrix_alloc(5,5);
  matrix_type * m2 = matrix_alloc(5,5);
  rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); 

  matrix_random_init( m1 , rng );
  matrix_assign( m2 , m1 );
  
  test_assert_true( matrix_equal( m1 , m2 ));
  matrix_resize( m1 , 5 , 5 , false );
  test_assert_true( matrix_equal( m1 , m2 ));
  matrix_resize( m1 , 5 , 5 , true );
  test_assert_true( matrix_equal( m1 , m2 ));
  
  rng_free( rng );
  matrix_free( m1 );
  matrix_free( m2 );
}
Exemplo n.º 6
0
static bool matrix_resize__(matrix_type * matrix , int rows , int columns , bool copy_content , bool safe_mode) {
  if (!matrix->data_owner)
    util_abort("%s: sorry - can not resize shared matrizes. \n",__func__);
  {
    bool resize_OK = true;

    if ((rows != matrix->rows) || (columns != matrix->columns)) {
      int copy_rows            = util_int_min( rows    , matrix->rows );
      int copy_columns         = util_int_min( columns , matrix->columns);
      matrix_type * copy_view  = NULL;
      matrix_type * copy       = NULL;

      if (copy_content) {
        copy_view = matrix_alloc_shared( matrix , 0 , 0 , copy_rows , copy_columns);         /* This is the part of the old matrix which should be copied over to the new. */
        copy      = matrix_alloc_copy__( copy_view , safe_mode );                            /* Now copy contains the part of the old matrix which should be copied over - with private storage. */
      }
      {
        int old_rows , old_columns, old_row_stride , old_column_stride;
        matrix_get_dims( matrix , &old_rows , &old_columns , &old_row_stride , &old_column_stride);        /* Storing the old header information - in case the realloc() fails. */

        matrix_init_header(matrix , rows , columns , 1 , rows);                                            /* Resetting the header for the matrix */
        matrix_realloc_data__(matrix , safe_mode);
        if (matrix->data != NULL) {  /* Realloc succeeded */
          if (copy_content) {
            matrix_type * target_view = matrix_alloc_shared(matrix , 0 , 0 , copy_rows , copy_columns);
            matrix_assign( target_view , copy);
            matrix_free( target_view );
          }
        } else {
          /* Failed to realloc new storage; RETURNING AN INVALID MATRIX */
          matrix_init_header(matrix , old_rows , old_columns , old_row_stride , old_column_stride);
          resize_OK = false;
        }
      }

      if (copy_content) {
        matrix_free(copy_view);
        matrix_free(copy);
      }
    }
    return resize_OK;
  }
}
Exemplo n.º 7
0
void test_readwrite() {
  test_work_area_type * test_area = test_work_area_alloc("matrix-test");
  {
    rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); 
    matrix_type * m1 = matrix_alloc(3  , 3);
    matrix_type * m2 = matrix_alloc(3  , 3);
    matrix_random_init( m1 , rng );
    matrix_assign(m2 , m1);

    test_assert_true( matrix_equal( m1 , m2 ) );
    {
      FILE * stream = util_fopen("m1" , "w");
      matrix_fwrite( m1 , stream );
      fclose( stream );
    }
    matrix_random_init( m1 , rng );
    test_assert_false( matrix_equal( m1 , m2 ) );
    {
      FILE * stream = util_fopen("m1" , "r");
      matrix_free( m1 );
      m1 = matrix_alloc(1,1);
      printf("-----------------------------------------------------------------\n");
      matrix_fread( m1 , stream );
      test_assert_int_equal( matrix_get_rows(m1) , matrix_get_rows( m2));
      test_assert_int_equal( matrix_get_columns(m1) , matrix_get_columns( m2));
      util_fseek( stream , 0 , SEEK_SET);
      {
        matrix_type * m3 = matrix_fread_alloc( stream );
        test_assert_true( matrix_equal( m2 , m3 ));
        matrix_free( m3 );
      }
      fclose( stream );
    }
    test_assert_true( matrix_equal( m1 , m2 ) );

    matrix_free( m2 );
    matrix_free( m1 );
    rng_free( rng );
  }
  test_work_area_free( test_area );
}
Exemplo n.º 8
0
int main( int argc, char ** argv)  {
  rng_type * rng   =  rng_alloc( MZRAN , INIT_DEV_RANDOM );
  matrix_type * A  =  matrix_alloc( 12 , 12 );
  matrix_type * B  =  matrix_alloc( 12 , 12 );
  matrix_random_init( A , rng );
  matrix_assign( B , A );
  matrix_pretty_print( A , "    A " , "%8.4f" );
  

#ifdef WITH_LAPACK
  matrix_inv( B );
  printf("\n");
  matrix_pretty_print( B , "inv(A)" , "%8.4f" );
  matrix_inplace_matmul( B , A );
  printf("\n");
  matrix_pretty_print( B , "    I " , "%8.4f" );
  {
    matrix_type * A3 = matrix_alloc(3,3);
    matrix_random_init( A3 , rng );
    matrix_iset( A3 , 0 , 0 , sin(0.98));
    printf("matrix_det3:%g  ",matrix_det3( A3 ));
    printf("matrix_det:%g \n",matrix_det( A3 ));
  }

  {
    matrix_type * A4 = matrix_alloc(4,4);
    matrix_random_init( A4 , rng );
    matrix_iset( A4 , 0 , 0 , sin(0.98));
    printf("matrix_det4:%g  ",matrix_det4( A4 ));
    printf("matrix_det:%g \n",matrix_det( A4 ));
  }
#endif

  matrix_free( A );
  matrix_free( B );
  rng_free( rng );
}
Exemplo n.º 9
0
/**
   Will not respect strides - that is considered low level data
   layout.
*/
static matrix_type * matrix_alloc_copy__( const matrix_type * src , bool safe_mode) {
  matrix_type * copy = matrix_alloc__( matrix_get_rows( src ), matrix_get_columns( src ) , safe_mode);
  if (copy != NULL)
    matrix_assign(copy , src);
  return copy;
}