static void layer_trace_block_content__( layer_type * layer , bool erase , int i , int j , int value , bool * visited , int_vector_type * i_list , int_vector_type * j_list) { int g = layer_get_global_cell_index( layer , i , j); cell_type * cell = &layer->data[g]; if (cell->cell_value != value || visited[g]) return; { visited[g] = true; if (erase) layer_iset_cell_value( layer , i , j , 0); int_vector_append( i_list , i ); int_vector_append( j_list , j ); if (i > 0) layer_trace_block_content__( layer , erase , i - 1 , j , value , visited , i_list , j_list); if (i < (layer->nx - 1)) layer_trace_block_content__( layer , erase , i + 1 , j , value , visited , i_list , j_list); if (j > 0) layer_trace_block_content__( layer , erase , i , j - 1, value , visited , i_list , j_list); if (j < (layer->ny - 1)) layer_trace_block_content__( layer , erase , i , j + 1, value , visited , i_list , j_list); } }
void test_shift() { int default_value = 88; int_vector_type * v = int_vector_alloc(0,default_value); int_vector_append(v , 1 ); int_vector_append(v , 2 ); int_vector_append(v , 3 ); test_assert_int_equal( 1 , int_vector_iget( v , 0 )); test_assert_int_equal( 2 , int_vector_iget( v , 1 )); test_assert_int_equal( 3 , int_vector_iget( v , 2 )); int_vector_rshift(v , 3 ); test_assert_int_equal( 6 , int_vector_size( v )); test_assert_int_equal( default_value , int_vector_iget( v , 0 )); test_assert_int_equal( default_value , int_vector_iget( v , 1 )); test_assert_int_equal( default_value , int_vector_iget( v , 2 )); test_assert_int_equal( 1 , int_vector_iget( v , 3 )); test_assert_int_equal( 2 , int_vector_iget( v , 4 )); test_assert_int_equal( 3 , int_vector_iget( v , 5 )); int_vector_lshift(v,4); test_assert_int_equal( 2 , int_vector_size( v )); test_assert_int_equal( 2 , int_vector_iget( v , 0 )); test_assert_int_equal( 3 , int_vector_iget( v , 1 )); int_vector_free( v ); }
void test_idel_insert() { int_vector_type * vec = int_vector_alloc(0,0); int_vector_append(vec , 1 ); int_vector_append(vec , 2 ); int_vector_append(vec , 2 ); int_vector_append(vec , 3 ); int_vector_fprintf(vec , stdout , "Vec0" , "%2d"); int_vector_idel(vec , 1 ); int_vector_fprintf(vec , stdout , "Vec1" , "%2d"); int_vector_idel(vec , 1 ); int_vector_fprintf(vec , stdout , "Vec2" , "%2d"); test_assert_int_equal( 2 , int_vector_size( vec )); test_assert_int_equal( 1 , int_vector_iget( vec , 0 )); test_assert_int_equal( 3 , int_vector_iget( vec , 1 )); int_vector_insert(vec , 1 , 2 ); int_vector_insert(vec , 1 , 2 ); test_assert_int_equal( 4 , int_vector_size( vec )); test_assert_int_equal( 1 , int_vector_iget( vec , 0 )); test_assert_int_equal( 2 , int_vector_iget( vec , 1 )); test_assert_int_equal( 2 , int_vector_iget( vec , 2 )); test_assert_int_equal( 3 , int_vector_iget( vec , 3 )); int_vector_free( vec ); }
void layer_cells_equal( const layer_type * layer , int value , int_vector_type * i_list , int_vector_type * j_list) { int i,j; for (j=0; j < layer->ny; j++) { for (i=0; i < layer->nx; i++) { cell_type * cell = layer_iget_cell( layer , i , j ); if (cell->cell_value == value) { int_vector_append( i_list , i ); int_vector_append( j_list , j ); } } } }
bool int_vector_init_range(int_vector_type * vector , int min_value , int max_value , int delta) { if (max_value >= min_value) { int current_value = min_value; int_vector_reset( vector ); while (current_value < max_value) { int_vector_append( vector , current_value ); current_value += delta; } int_vector_append( vector , max_value ); return true; } else return false; }
void test_contains_sorted() { int_vector_type * int_vector = int_vector_alloc( 0 , 100); int_vector_append( int_vector , 99 ); int_vector_append( int_vector , 89 ); int_vector_append( int_vector , 79 ); int_vector_append( int_vector , 109 ); int_vector_sort( int_vector ); test_assert_false( int_vector_contains( int_vector , 0 )); test_assert_false( int_vector_contains( int_vector , 100 )); test_assert_true( int_vector_contains( int_vector , 89 )); test_assert_true( int_vector_contains( int_vector , 109 )); }
ecl_rft_file_type * ecl_rft_file_alloc(const char * filename) { ecl_rft_file_type * rft_vector = ecl_rft_file_alloc_empty( filename ); ecl_file_type * ecl_file = ecl_file_open( filename , 0); int global_index = 0; int block_nr = 0; while (true) { if (ecl_file_select_block( ecl_file , TIME_KW , block_nr)) { ecl_rft_node_type * rft_node = ecl_rft_node_alloc( ecl_file ); if (rft_node != NULL) { const char * well_name = ecl_rft_node_get_well_name( rft_node ); ecl_rft_file_add_node(rft_vector , rft_node); if (!hash_has_key( rft_vector->well_index , well_name)) hash_insert_hash_owned_ref( rft_vector->well_index , well_name , int_vector_alloc( 0 , 0 ) , int_vector_free__); { int_vector_type * index_list = hash_get( rft_vector->well_index , well_name ); int_vector_append(index_list , global_index); } global_index++; } } else break; block_nr++; } ecl_file_close( ecl_file ); return rft_vector; }
void test_measure( ert_test_context_type * test_context ) { enkf_main_type * enkf_main = ert_test_context_get_main( test_context ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); enkf_obs_type * enkf_obs = enkf_main_get_obs( enkf_main ); obs_vector_type * rft_obs = enkf_obs_get_vector( enkf_obs , "RFT_TEST"); int_vector_type * ens_active_list = int_vector_alloc(0,0); active_list_type * active_list = active_list_alloc( ); meas_data_type * meas_data_RFT; for (int i=0; i < enkf_main_get_ensemble_size( enkf_main ); i++) int_vector_append( ens_active_list , i ); { bool_vector_type * ens_mask; ens_mask = int_vector_alloc_mask( ens_active_list ); meas_data_RFT = meas_data_alloc( ens_mask ); bool_vector_free( ens_mask ); } obs_vector_measure( rft_obs , fs , 20 , ens_active_list , meas_data_RFT , active_list ); int_vector_free( ens_active_list ); active_list_free( active_list ); meas_data_free( meas_data_RFT ); }
void gen_data_config_add_report_step( gen_data_config_type * config , int report_step) { if (config->dynamic) { if (!gen_data_config_has_report_step( config , report_step)) { int_vector_append( config->active_report_steps , report_step ); int_vector_sort( config->active_report_steps ); } } }
void test_index_list() { int_vector_type * index_list = int_vector_alloc( 0 , 0 ); int_vector_append( index_list , 10 ); int_vector_append( index_list , 20 ); int_vector_append( index_list , 30 ); { bool_vector_type * mask = int_vector_alloc_mask( index_list ); test_assert_false( bool_vector_get_default( mask )); test_assert_int_equal( 31 , bool_vector_size( mask )); test_assert_true( bool_vector_iget( mask , 10 )); test_assert_true( bool_vector_iget( mask , 20 )); test_assert_true( bool_vector_iget( mask , 30 )); test_assert_int_equal( 3 , bool_vector_count_equal( mask , true )); bool_vector_free( mask ); } int_vector_free( index_list ); }
void nnc_info_add_nnc(nnc_info_type * nnc_info, int lgr_nr, int global_cell_number) { bool index_list_initialized = false; if (int_vector_size(nnc_info->lgr_index_map) > lgr_nr) { int lgr_index = int_vector_iget(nnc_info->lgr_index_map, lgr_nr); if (-1 != lgr_index) { int_vector_append(vector_iget(nnc_info->lgr_list, lgr_index), global_cell_number); index_list_initialized = true; } } if (!index_list_initialized) { int_vector_type * nnc_for_lgr_vec = int_vector_alloc(0,0); int_vector_append(nnc_for_lgr_vec, global_cell_number); vector_append_owned_ref(nnc_info->lgr_list, nnc_for_lgr_vec, int_vector_free__); int_vector_iset(nnc_info->lgr_index_map , lgr_nr, vector_get_size(nnc_info->lgr_list) -1 ); } }
int_vector_type * bool_vector_alloc_active_list( const bool_vector_type * mask ) { int_vector_type * active_list = int_vector_alloc(0,0); int i; for (i =0; i < bool_vector_size( mask ); i++) if (bool_vector_iget( mask , i )) int_vector_append( active_list , i ); return active_list; }
bool ecl_sum_vector_add_key( ecl_sum_vector_type * ecl_sum_vector, const char * key){ if (ecl_sum_has_general_var( ecl_sum_vector->ecl_sum , key)) { const smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum_vector->ecl_sum , key ); int params_index = smspec_node_get_params_index( node ); bool is_rate_key = smspec_node_is_rate( node); int_vector_append(ecl_sum_vector->node_index_list, params_index); bool_vector_append(ecl_sum_vector->is_rate_list, is_rate_key); return true; } else return false; }
void int_vector_select_unique(int_vector_type * vector) { int_vector_assert_writable( vector ); if (vector->size > 0) { int_vector_type * copy = int_vector_alloc_copy( vector ); int_vector_sort( copy ); int_vector_reset( vector ); { int i; int previous_value = int_vector_iget( copy , 0); int_vector_append( vector , previous_value); for (i=1; i < copy->size; i++) { int value = int_vector_iget( copy , i ); if (value != previous_value) int_vector_append( vector , value); previous_value = value; } } int_vector_free( copy ); } }
void obs_vector_install_node(obs_vector_type * obs_vector , int index , void * node) { obs_vector_assert_node_type( obs_vector , node ); { if (vector_iget_const( obs_vector->nodes , index ) == NULL) { obs_vector->num_active++; int_vector_append( obs_vector->step_list , index ); int_vector_sort( obs_vector->step_list ); } vector_iset_owned_ref( obs_vector->nodes , index , node , obs_vector->freef ); } }
/** Finds the indicies of the entries matching 's'. */ int_vector_type * stringlist_find(const stringlist_type * stringlist, const char * s) { int_vector_type * indicies = int_vector_alloc(0, -1); int size = stringlist_get_size( stringlist ); int index = 0; while (index < size ) { const char * istring = stringlist_iget(stringlist , index); if (istring != NULL) if (strcmp(istring , s) == 0) int_vector_append(indicies, index); index++; } return indicies; }
void ecl_sum_vector_add_keys( ecl_sum_vector_type * ecl_sum_vector, const char * pattern){ stringlist_type * keylist = ecl_sum_alloc_matching_general_var_list(ecl_sum_vector->ecl_sum , pattern); int num_keywords = stringlist_get_size(keylist); int i; for(i = 0; i < num_keywords ;i++){ const char * key = stringlist_iget(keylist, i); const smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum_vector->ecl_sum , key ); int params_index = smspec_node_get_params_index( node ); bool is_rate_key = smspec_node_is_rate( node); int_vector_append(ecl_sum_vector->node_index_list, params_index); bool_vector_append(ecl_sum_vector->is_rate_list, is_rate_key); } stringlist_free(keylist); }
static int_vector_type * string_util_sscanf_alloc_active_list(const char * range_string ) { int_vector_type *active_list = NULL; bool valid = valid_characters( range_string ); if (valid) { parser_type * parser = parser_alloc( "," , /* No ordinary split characters. */ NULL , /* No quoters. */ NULL , /* No special split */ " \t" , /* Removing ' ' and '\t' */ NULL , /* No comment */ NULL ); stringlist_type * tokens; int item; active_list = int_vector_alloc(0,0); tokens = parser_tokenize_buffer( parser , range_string , true); for (item = 0; item < stringlist_get_size( tokens ); item++) { const char * string_item = stringlist_iget( tokens , item ); char * pos_ptr = (char *) string_item; int value1 , value2; value1 = strtol( string_item , &pos_ptr , 10); if (*pos_ptr == '\0') // The pos_ptr points to the end of the string, i.e. this was a single digit. value2 = value1; else { // OK - this is a range; skip spaces and the range dash '-' while (isspace(*pos_ptr) || *pos_ptr == '-') pos_ptr++; util_sscanf_int( pos_ptr , &value2); } { int value; for (value = value1; value <= value2; value++) int_vector_append( active_list , value ); } } stringlist_free( tokens ); parser_free( parser ); } return active_list; }
/** Should essentially implement Python sliced index lookup. */ int_vector_type * int_vector_alloc_strided_copy( const int_vector_type * src , int start , int stop , int stride ) { int_vector_type * copy = int_vector_alloc( 0 , src->default_value ); if (start < 0) start = src->size - start; if (stop < 0) stop = src->size - stop; { int src_index = start; while (src_index < stop) { int_vector_append( copy , int_vector_iget( src , src_index )); src_index += stride; } } return copy; }
void int_vector_range_fill(int_vector_type * vector , int limit1 , int delta , int limit2) { int current_value = limit1; if (delta == 0) util_abort("%s: sorry can not have delta == 0 \n",__func__); int_vector_reset( vector ); while (true) { int_vector_append( vector , current_value ); current_value += delta; if (delta > 0 && current_value > limit2) break; if (delta < 0 && current_value < limit2) break; } }
static void file_map_make_index( file_map_type * file_map ) { stringlist_clear( file_map->distinct_kw ); hash_clear( file_map->kw_index ); { int i; for (i=0; i < vector_get_size( file_map->kw_list ); i++) { const ecl_file_kw_type * file_kw = vector_iget_const( file_map->kw_list , i); const char * header = ecl_file_kw_get_header( file_kw ); if ( !hash_has_key( file_map->kw_index , header )) { int_vector_type * index_vector = int_vector_alloc( 0 , -1 ); hash_insert_hash_owned_ref( file_map->kw_index , header , index_vector , int_vector_free__); stringlist_append_copy( file_map->distinct_kw , header); } { int_vector_type * index_vector = hash_get( file_map->kw_index , header); int_vector_append( index_vector , i); } } } }
void testS( ert_test_context_type * test_context ) { { enkf_main_type * enkf_main = ert_test_context_get_main( test_context ); enkf_obs_type * enkf_obs = enkf_main_get_obs( enkf_main ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); int_vector_type * active_list = int_vector_alloc(0,0); obs_data_type * obs_data = obs_data_alloc(1.0); local_obsdata_type * obs_set = local_obsdata_alloc( "KEY" ); bool_vector_type * ens_mask; meas_data_type * meas_data; for (int i= 0; i < enkf_main_get_ensemble_size( enkf_main); i++) int_vector_append( active_list , i ); ens_mask = int_vector_alloc_mask( active_list); obs_data = obs_data_alloc(1.0); meas_data = meas_data_alloc( ens_mask ); enkf_obs_add_local_nodes_with_data( enkf_obs , obs_set , fs , ens_mask ); enkf_obs_get_obs_and_measure_data( enkf_obs , fs , obs_set, FORECAST , active_list , meas_data , obs_data); { FILE * stream = util_fopen("analysis/Smatrix" , "r"); matrix_type * S = meas_data_allocS( meas_data ); matrix_type * S0 = matrix_fread_alloc( stream ); test_assert_true( matrix_equal( S0 , S )); matrix_free( S ); matrix_free( S0 ); fclose( stream ); } int_vector_free( active_list ); meas_data_free( meas_data ); obs_data_free( obs_data ); local_obsdata_free( obs_set ); bool_vector_free( ens_mask ); } }
bool string_util_update_active_list( const char * range_string , int_vector_type * active_list ) { int_vector_sort( active_list ); { bool_vector_type * mask = alloc_mask( active_list ); bool valid = false; if (string_util_update_active_mask( range_string , mask )) { int_vector_reset( active_list ); { int i; for (i=0; i < bool_vector_size(mask); i++) { bool active = bool_vector_iget( mask , i ); if (active) int_vector_append( active_list , i ); } } valid = true; } bool_vector_free( mask ); return valid; } }
void test_del() { int_vector_type * vec = int_vector_alloc(0,0); test_assert_int_equal( int_vector_del_value( vec , 77) , 0 ); int_vector_append(vec , 1); int_vector_append(vec , 2); int_vector_append(vec , 3); int_vector_append(vec , 1); int_vector_append(vec , 2); int_vector_append(vec , 3); int_vector_append(vec , 1); int_vector_append(vec , 1); int_vector_fprintf( vec , stdout , "int_vector" , "%3d"); test_assert_int_equal( int_vector_del_value( vec , 77) , 0); test_assert_int_equal( int_vector_del_value( vec , 2) , 2 ); test_assert_int_equal( 6 , int_vector_size( vec )); int_vector_fprintf( vec , stdout , "int_vector" , "%3d"); test_assert_int_equal( 1 , int_vector_iget(vec , 0)); test_assert_int_equal( 3 , int_vector_iget(vec , 1)); test_assert_int_equal( 1 , int_vector_iget(vec , 2)); test_assert_int_equal( 3 , int_vector_iget(vec , 3)); test_assert_int_equal( 1 , int_vector_iget(vec , 4)); test_assert_int_equal( 1 , int_vector_iget(vec , 5)); test_assert_int_equal( 4 , int_vector_del_value( vec , 1 )); test_assert_int_equal( 2 , int_vector_size( vec )); test_assert_int_equal( 3 , int_vector_iget(vec , 0)); test_assert_int_equal( 3 , int_vector_iget(vec , 1)); int_vector_free( vec ); }
int main(int argc , char ** argv) { int_vector_type * int_vector = int_vector_alloc( 0 , 99); int_vector_iset( int_vector , 2 , 0); int_vector_insert( int_vector , 2 , 77 ); int_vector_iset( int_vector , 5 , -10); int_vector_fprintf( int_vector , stdout , "int_vector" , "%3d"); assert_equal( int_vector_iget(int_vector , 0 ) == 99 ); assert_equal( int_vector_iget(int_vector , 1 ) == 99 ); assert_equal( int_vector_iget(int_vector , 2 ) == 77 ); assert_equal( int_vector_iget(int_vector , 3 ) == 00 ); assert_equal( int_vector_iget(int_vector , 4 ) == 99 ); assert_equal( int_vector_iget(int_vector , 5 ) == -10 ); { int N1 = 100000; int N2 = 10*N1; int_vector_type * v1 = int_vector_alloc( N1 , 0 ); int_vector_type * v2; int * data1 = int_vector_get_ptr( v1 ); int_vector_iset( v1 , N1 - 1, 99); int_vector_free_container( v1 ); v2 = int_vector_alloc( N2 , 0 ); int_vector_iset(v2 , N2 - 1, 77 ); test_assert_int_equal( data1[N1-1] , 99); int_vector_free( v2 ); free( data1 ); } test_assert_true( int_vector_init_range( int_vector , 100 , 1000 , 115 ) ); test_assert_int_equal( int_vector_iget( int_vector , 0 ) , 100); test_assert_int_equal( int_vector_iget( int_vector , 1 ) , 215); test_assert_int_equal( int_vector_iget( int_vector , 2 ) , 330); test_assert_int_equal( int_vector_iget( int_vector , 3 ) , 445); test_assert_int_equal( int_vector_get_last( int_vector ) , 1000); test_assert_false( int_vector_init_range( int_vector , 100 , -1000 , 115 ) ); test_assert_int_equal( int_vector_iget( int_vector , 0 ) , 100); test_assert_int_equal( int_vector_iget( int_vector , 1 ) , 215); test_assert_int_equal( int_vector_iget( int_vector , 2 ) , 330); test_assert_int_equal( int_vector_iget( int_vector , 3 ) , 445); test_assert_int_equal( int_vector_get_last( int_vector ) , 1000); { int_vector_type * v1 = int_vector_alloc(0,0); int_vector_type * v2 = int_vector_alloc(0,0); int_vector_append(v1 , 10); int_vector_append(v1 , 15); int_vector_append(v1 , 20); int_vector_append(v2 , 1); int_vector_append(v2 , 2); int_vector_append(v2 , 3); int_vector_append_vector( v1 , v2 ); test_assert_int_equal( int_vector_size( v1 ) , 6 ); test_assert_int_equal( int_vector_iget (v1 , 0 ), 10 ); test_assert_int_equal( int_vector_iget (v1 , 1 ), 15 ); test_assert_int_equal( int_vector_iget (v1 , 2 ), 20 ); test_assert_int_equal( int_vector_iget (v1 , 3 ), 1 ); test_assert_int_equal( int_vector_iget (v1 , 4 ), 2 ); test_assert_int_equal( int_vector_iget (v1 , 5 ), 3 ); int_vector_free( v1 ); int_vector_free( v2 ); } exit(0); }
void int_vector_append_default(int_vector_type * vector , int default_value) { int_vector_append( vector , default_value ); int_vector_set_default( vector , default_value ); }
static void layer_trace_block_edge__( const layer_type * layer , int_point2d_type start_point , int i , int j , int value , edge_dir_enum dir , struct_vector_type * corner_list, int_vector_type * cell_list) { int_point2d_type current_point; int_point2d_type next_point; current_point.i = i; current_point.j = j; next_point = current_point; if (dir == BOTTOM_EDGE) point_shift( &next_point , 1 , 0 ); else if (dir == RIGHT_EDGE) { point_shift( ¤t_point , 1 , 0 ); point_shift( &next_point , 1 , 1 ); } else if (dir == TOP_EDGE) { point_shift( ¤t_point , 1 , 1 ); point_shift( &next_point , 0 , 1 ); } else if (dir == LEFT_EDGE) point_shift( ¤t_point , 0 , 1 ); struct_vector_append( corner_list , ¤t_point ); { int cell_index = i + j*layer->nx; int_vector_append( cell_list , cell_index ); } if ( !point_equal(&start_point , &next_point) ) { if (dir == BOTTOM_EDGE) { if (layer_iget_edge_value( layer , i,j,RIGHT_EDGE) == value) layer_trace_block_edge__( layer , start_point , i , j , value , RIGHT_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i + 1 , j ,BOTTOM_EDGE) == value) layer_trace_block_edge__( layer , start_point , i + 1 , j , value , BOTTOM_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i + 1 , j - 1 , LEFT_EDGE) == -value) layer_trace_block_edge__( layer , start_point , i + 1 , j -1 , value , LEFT_EDGE , corner_list , cell_list); else util_abort("%s: dir == BOTTOM_EDGE \n",__func__); } if (dir == RIGHT_EDGE) { if (layer_iget_edge_value( layer , i,j,TOP_EDGE) == -value) layer_trace_block_edge__( layer , start_point , i , j , value , TOP_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i , j + 1 ,RIGHT_EDGE) == value) layer_trace_block_edge__( layer , start_point , i , j + 1, value , RIGHT_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i + 1 , j + 1 ,BOTTOM_EDGE) == value) layer_trace_block_edge__( layer , start_point , i + 1 , j + 1, value , BOTTOM_EDGE , corner_list , cell_list); else util_abort("%s: dir == RIGHT_EDGE \n",__func__); } if (dir == TOP_EDGE) { if (layer_iget_edge_value( layer , i , j , LEFT_EDGE) == -value) layer_trace_block_edge__( layer , start_point , i , j , value , LEFT_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i - 1 , j ,TOP_EDGE) == -value) layer_trace_block_edge__( layer , start_point , i - 1 , j , value , TOP_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i - 1 , j + 1 ,RIGHT_EDGE) == value) layer_trace_block_edge__( layer , start_point , i - 1 , j + 1, value , RIGHT_EDGE , corner_list , cell_list); else util_abort("%s: dir == TOP_EDGE \n",__func__); } if (dir == LEFT_EDGE) { if (layer_iget_edge_value( layer , i , j , BOTTOM_EDGE) == value) layer_trace_block_edge__( layer , start_point , i , j , value , BOTTOM_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i , j - 1 , LEFT_EDGE) == -value) layer_trace_block_edge__( layer , start_point , i , j - 1, value , LEFT_EDGE , corner_list , cell_list); else if (layer_iget_edge_value( layer , i -1 , j - 1 , TOP_EDGE) == -value) layer_trace_block_edge__( layer , start_point , i-1 , j - 1, value , TOP_EDGE , corner_list , cell_list); else util_abort("%s: dir == LEFT_EDGE \n",__func__); } } }
void lars_estimate(lars_type * lars , int max_vars , double max_beta , bool verbose) { int nvars = matrix_get_columns( lars->X ); int nsample = matrix_get_rows( lars->X ); matrix_type * X = matrix_alloc( nsample, nvars ); // Allocate local X and Y variables matrix_type * Y = matrix_alloc( nsample, 1 ); // which will hold the normalized data lars_estimate_init( lars , X , Y); // during the estimation process. { matrix_type * G = matrix_alloc_gram( X , true ); matrix_type * mu = matrix_alloc( nsample , 1 ); matrix_type * C = matrix_alloc( nvars , 1 ); matrix_type * Y_mu = matrix_alloc_copy( Y ); int_vector_type * active_set = int_vector_alloc(0,0); int_vector_type * inactive_set = int_vector_alloc(0,0); int active_size; if ((max_vars <= 0) || (max_vars > nvars)) max_vars = nvars; { int i; for (i=0; i < nvars; i++) int_vector_iset( inactive_set , i , i ); } matrix_set( mu , 0 ); while (true) { double maxC = 0; /* The first step is to calculate the correlations between the covariates, and the current residual. All the currently inactive covariates are searched; the covariate with the greatest correlation with (Y - mu) is selected and added to the active set. */ matrix_sub( Y_mu , Y , mu ); // Y_mu = Y - mu matrix_dgemm( C , X , Y_mu , true , false , 1.0 , 0); // C = X' * Y_mu { int i; int max_set_index = 0; for (i=0; i < int_vector_size( inactive_set ); i++) { int set_index = i; int var_index = int_vector_iget( inactive_set , set_index ); double value = fabs( matrix_iget(C , var_index , 0) ); if (value > maxC) { maxC = value; max_set_index = set_index; } } /* Remove element corresponding to max_set_index from the inactive set and add it to the active set: */ int_vector_append( active_set , int_vector_idel( inactive_set , max_set_index )); } active_size = int_vector_size( active_set ); /* Now we have calculated the correlations between all the covariates and the current residual @Y_mu. The correlations are stored in the matrix @C. The value of the maximum correlation is stored in @maxC. Based on the value of @maxC we have added one new covariate to the model, technically by moving the index from @inactive_set to @active_set. */ /*****************************************************************/ { matrix_type * weights = matrix_alloc( active_size , 1); double scale; /*****************************************************************/ /* This scope should compute and initialize the variables @weights and @scale. */ { matrix_type * subG = matrix_alloc( active_size , active_size ); matrix_type * STS = matrix_alloc( active_size , active_size ); matrix_type * sign_vector = matrix_alloc( active_size , 1); int i , j; /* STS = S' o S where 'o' is the Schur product and S is given by: [ s1 s2 s3 s4 ] S = [ s1 s2 s3 s4 ] [ s1 s2 s3 s4 ] [ s1 s2 s3 s4 ] Where si is the sign of the correlation between (active) variable 'i' and Y_mu. */ for (i=0; i < active_size ; i++) { int vari = int_vector_iget( active_set , i ); double signi = sgn( matrix_iget( C , vari , 0)); matrix_iset( sign_vector , i , 0 , signi ); for (j=0; j < active_size; j++) { int varj = int_vector_iget( active_set , j ); double signj = sgn( matrix_iget( C , varj , 0)); matrix_iset( STS , i , j , signi * signj ); } } // Extract the elements from G corresponding to active indices and // copy to the matrix subG: for (i=0; i < active_size ; i++) { int ii = int_vector_iget( active_set , i ); for (j=0; j < active_size; j++) { int jj = int_vector_iget( active_set , j ); matrix_iset( subG , i , j , matrix_iget(G , ii , jj)); } } // Weights matrix_inplace_mul( subG , STS ); matrix_inv( subG ); { matrix_type * ones = matrix_alloc( active_size , 1 ); matrix_type * GA1 = matrix_alloc( active_size , 1 ); matrix_set( ones , 1.0 ); matrix_matmul( GA1 , subG , ones ); scale = 1.0 / sqrt( matrix_get_column_sum( GA1 , 0 )); matrix_mul( weights , GA1 , sign_vector ); matrix_scale( weights , scale ); matrix_free( GA1 ); matrix_free( ones ); } matrix_free( sign_vector ); matrix_free( subG ); matrix_free( STS ); } /******************************************************************/ /* The variables weight and scale have been calculated, proceed to calculate the step length @gamma. */ { int i; double gamma; { matrix_type * u = matrix_alloc( nsample , 1 ); int j; for (i=0; i < nsample; i++) { double row_sum = 0; for (j =0; j < active_size; j++) row_sum += matrix_iget( X , i , int_vector_iget( active_set , j)) * matrix_iget(weights , j , 0 ); matrix_iset( u , i , 0 , row_sum ); } gamma = maxC / scale; if (active_size < matrix_get_columns( X )) { matrix_type * equi_corr = matrix_alloc( nvars , 1 ); matrix_dgemm( equi_corr , X , u , true , false , 1.0 , 0); // equi_corr = X'·u for (i=0; i < (nvars - active_size); i++) { int var_index = int_vector_iget( inactive_set , i ); double gamma1 = (maxC - matrix_iget(C , var_index , 0 )) / ( scale - matrix_iget( equi_corr , var_index , 0)); double gamma2 = (maxC + matrix_iget(C , var_index , 0 )) / ( scale + matrix_iget( equi_corr , var_index , 0)); if ((gamma1 > 0) && (gamma1 < gamma)) gamma = gamma1; if ((gamma2 > 0) && (gamma2 < gamma)) gamma = gamma2; } matrix_free( equi_corr ); } /* Update the current estimated 'location' mu. */ matrix_scale( u , gamma ); matrix_inplace_add( mu , u ); matrix_free( u ); } /* We have calculated the step length @gamma, and the @weights. Update the @beta matrix. */ for (i=0; i < active_size; i++) matrix_iset( lars->beta , int_vector_iget( active_set , i ) , active_size - 1 , gamma * matrix_iget( weights , i , 0)); if (active_size > 1) for (i=0; i < nvars; i++) matrix_iadd( lars->beta , i , active_size - 1 , matrix_iget( lars->beta , i , active_size - 2)); matrix_free( weights ); } } if (active_size == max_vars) break; if (max_beta > 0) { double beta_norm2 = matrix_get_column_abssum( lars->beta , active_size - 1 ); if (beta_norm2 > max_beta) { // We stop - we will use an interpolation between this beta estimate and // the previous, to ensure that the |beta| = max_beta criteria is satisfied. if (active_size >= 2) { double beta_norm1 = matrix_get_column_abssum( lars->beta , active_size - 2 ); double s = (max_beta - beta_norm1)/(beta_norm2 - beta_norm1); { int j; for (j=0; j < nvars; j++) { double beta1 = matrix_iget( lars->beta , j , active_size - 2 ); double beta2 = matrix_iget( lars->beta , j , active_size - 1 ); matrix_iset( lars->beta , j , active_size - 1 , beta1 + s*(beta2 - beta1)); } } } break; } } } matrix_free( G ); matrix_free( mu ); matrix_free( C ); matrix_free( Y_mu ); int_vector_free( active_set ); int_vector_free( inactive_set ); matrix_resize( lars->beta , nvars , active_size , true ); if (verbose) matrix_pretty_fprint( lars->beta , "beta" , "%12.5f" , stdout ); lars_select_beta( lars , active_size - 1); } matrix_free( X ); matrix_free( Y ); }
int main(int argc , char ** argv) { int_vector_type * int_vector = int_vector_alloc( 0 , 99); test_abort(); test_assert_int_equal( -1 , int_vector_index(int_vector , 100)); test_assert_int_equal( -1 , int_vector_index_sorted(int_vector , 100)); test_assert_true( int_vector_is_instance( int_vector )); test_assert_false( double_vector_is_instance( int_vector )); int_vector_iset( int_vector , 2 , 0); int_vector_insert( int_vector , 2 , 77 ); int_vector_iset( int_vector , 5 , -10); assert_equal( int_vector_iget(int_vector , 0 ) == 99 ); assert_equal( int_vector_iget(int_vector , 1 ) == 99 ); assert_equal( int_vector_iget(int_vector , 2 ) == 77 ); assert_equal( int_vector_iget(int_vector , 3 ) == 00 ); assert_equal( int_vector_iget(int_vector , 4 ) == 99 ); assert_equal( int_vector_iget(int_vector , 5 ) == -10 ); { int N1 = 100000; int N2 = 10*N1; int_vector_type * v1 = int_vector_alloc( N1 , 0 ); int_vector_type * v2; int * data1 = int_vector_get_ptr( v1 ); int_vector_iset( v1 , N1 - 1, 99); int_vector_free_container( v1 ); v2 = int_vector_alloc( N2 , 0 ); int_vector_iset(v2 , N2 - 1, 77 ); test_assert_int_equal( data1[N1-1] , 99); int_vector_free( v2 ); free( data1 ); } test_assert_true( int_vector_init_range( int_vector , 100 , 1000 , 115 ) ); test_assert_int_equal( int_vector_iget( int_vector , 0 ) , 100); test_assert_int_equal( int_vector_iget( int_vector , 1 ) , 215); test_assert_int_equal( int_vector_iget( int_vector , 2 ) , 330); test_assert_int_equal( int_vector_iget( int_vector , 3 ) , 445); test_assert_int_equal( int_vector_get_last( int_vector ) , 1000); test_assert_false( int_vector_init_range( int_vector , 100 , -1000 , 115 ) ); test_assert_int_equal( int_vector_iget( int_vector , 0 ) , 100); test_assert_int_equal( int_vector_iget( int_vector , 1 ) , 215); test_assert_int_equal( int_vector_iget( int_vector , 2 ) , 330); test_assert_int_equal( int_vector_iget( int_vector , 3 ) , 445); test_assert_int_equal( int_vector_get_last( int_vector ) , 1000); { int_vector_type * v1 = int_vector_alloc(0,0); int_vector_type * v2 = int_vector_alloc(0,0); int_vector_append(v1 , 10); int_vector_append(v1 , 15); int_vector_append(v1 , 20); int_vector_append(v2 , 1); int_vector_append(v2 , 2); int_vector_append(v2 , 3); int_vector_append_vector( v1 , v2 ); test_assert_int_equal( int_vector_size( v1 ) , 6 ); test_assert_int_equal( int_vector_iget (v1 , 0 ), 10 ); test_assert_int_equal( int_vector_iget (v1 , 1 ), 15 ); test_assert_int_equal( int_vector_iget (v1 , 2 ), 20 ); test_assert_int_equal( int_vector_iget (v1 , 3 ), 1 ); test_assert_int_equal( int_vector_iget (v1 , 4 ), 2 ); test_assert_int_equal( int_vector_iget (v1 , 5 ), 3 ); int_vector_free( v1 ); int_vector_free( v2 ); } test_contains(); test_contains_sorted(); test_shift(); test_alloc(); test_div(); test_memcpy_from_data(); test_idel_insert(); test_del(); test_range_fill(); test_iset_block(); test_resize(); test_empty(); test_insert_double(); exit(0); }