void pre_order(bip inst, bool with_output){ bip temp=init_bip(inst.rows, inst.columns); for (int i=0; i<inst.rows; ++i) { for (int j=0; j<inst.columns; ++j) { set_elem(temp, i, j, *get_elem(inst, i, j)); } temp.vector[i]=inst.vector[i]; } double order[inst.rows]; for (int i=0; i<inst.rows; ++i) { for (int j=0; j<inst.columns; ++j) { order[i]+=(double)*get_elem(inst, i, j); } if(0<inst.vector[i])order[i]=order[i]/ (double)(inst.vector[i]); if (0>inst.vector[i])order[i]=order[i]*(double)-inst.vector[i]; //printf("%f bei %d \n", order[i], i); } double* pointers[inst.rows]; for (int i=0; i<inst.rows; ++i) { pointers[i]=&order[i]; } qsort(pointers, inst.rows, sizeof(pointers[0]), cmpfunc); int orderind[inst.rows]; for (int i=0; i<inst.rows; i++) { orderind[i]=pointers[i]-order; } for (int i=0; i<inst.rows; ++i) { for (int j=0; j<inst.columns; ++j) { set_elem(inst, i, j, *get_elem(temp, orderind[i], j)); } inst.vector[i]=temp.vector[orderind[i]]; } free_bip(temp); if(with_output)print_bip(inst); }
template <typename T, typename X> void dense_matrix<T, X>::swap_rows(unsigned a, unsigned b) { for (unsigned i = 0; i < m_n; i++) { T t = get_elem(a, i); set_elem(a, i, get_elem(b, i)); set_elem(b, i, t); } }
template <typename T, typename X> void dense_matrix<T, X>::swap_columns(unsigned a, unsigned b) { for (unsigned i = 0; i < m_m; i++) { T t = get_elem(i, a); set_elem(i, a, get_elem(i, b)); set_elem(i, b, t); } }
void colvarvalue::apply_constraints() { switch (value_type) { case colvarvalue::type_scalar: case colvarvalue::type_3vector: case colvarvalue::type_unit3vectorderiv: case colvarvalue::type_quaternionderiv: break; case colvarvalue::type_unit3vector: rvector_value /= cvm::sqrt(rvector_value.norm2()); break; case colvarvalue::type_quaternion: quaternion_value /= cvm::sqrt(quaternion_value.norm2()); break; case colvarvalue::type_vector: if (elem_types.size() > 0) { // if we have information about non-scalar types, use it size_t i; for (i = 0; i < elem_types.size(); i++) { if (elem_sizes[i] == 1) continue; // TODO this can be optimized further colvarvalue cvtmp(vector1d_value.slice(elem_indices[i], elem_indices[i] + elem_sizes[i]), elem_types[i]); cvtmp.apply_constraints(); set_elem(i, cvtmp); } } break; case colvarvalue::type_notset: default: break; } }
void colvarvalue::set_elem(int const icv, colvarvalue const &x) { if (elem_types.size() > 0) { check_types_assign(elem_types[icv], x.value_type); set_elem(elem_indices[icv], elem_indices[icv] + elem_sizes[icv], x); } else { cvm::error("Error: trying to set a colvarvalue element for a colvarvalue that was initialized as a plain array.\n"); } }
void colvarvalue::add_elem(colvarvalue const &x) { if (this->value_type != type_vector) { cvm::error("Error: trying to set an element for a variable that is not set to be a vector.\n"); return; } size_t const n = vector1d_value.size(); size_t const nd = num_dimensions(x.value_type); elem_types.push_back(x.value_type); elem_indices.push_back(n); elem_sizes.push_back(nd); vector1d_value.resize(n + nd); set_elem(n, x); }
/* Multiplies the two matrices m1 and m2, storing the results into the result * matrix. */ void multiply_matrices(const matrix_t *m1, const matrix_t *m2, matrix_t *result) { int r, c, i, val; assert(m1 != NULL); assert(m2 != NULL); assert(result != NULL); assert(m1->cols == m2->rows); assert(m1->rows == result->rows); assert(m2->cols == result->cols); for (r = 0; r < result->rows; r++) { for (c = 0; c < result->cols; c++) { val = 0; for (i = 0; i < m1->cols; i++) val += get_elem(m1, r, i) * get_elem(m2, i, c); set_elem(result, r, c, val); } } }
//test all possible solutions on the bip long long find_solutions(bip inst, bool with_print){ assert(is_initialized(inst)); //check for overflow if(!check_overflow(inst)){ fprintf(stderr, "Coefficients too large. Overflow! \n"); return -1; } if (greatereq==inst.ordin) { for (int i=0; i<inst.rows; ++i) { for (int j=0; j<inst.columns; ++j) { int temp = -(*get_elem(inst, i, j)); set_elem(inst, i, j, temp); } inst.vector[i]=-inst.vector[i]; } } pre_order(inst, with_print); long long sols=bip_enumerate(inst, with_print); return sols; }
template <typename T, typename X> void dense_matrix<T, X>::multiply_row_by_constant(unsigned row, T & t) { for (unsigned i = 0; i < m_n; i++) { set_elem(row, i, t * get_elem(row, i)); } }