cmpl disp_sample_table_n_value(const disp_t *disp, double lam) { const struct disp_sample_table *dt = & disp->disp.sample_table; if (lam <= get_wavelength(dt, 0)) { return get_n(dt, 0) - get_k(dt, 0) * I; } else if (lam >= get_wavelength(dt, dt->len - 1)) { return get_n(dt, dt->len - 1) - get_k(dt, dt->len - 1) * I; } double nx = gsl_interp_eval(dt->interp_n, wavelength_const_array(dt), n_const_array(dt), lam, dt->accel); double kx = gsl_interp_eval(dt->interp_k, wavelength_const_array(dt), k_const_array(dt), lam, dt->accel); return nx - kx * I; }
void sync_const() { String s; Form2->Edit1->Text = get_nb(); Form2->Edit2->Text = get_k(); Form2->Edit3->Text = make_name(get_Pb()); Form2->Edit4->Text = make_name(get_E()); Form2->Edit5->Text = make_name(get_G()); }
double get_edf_bound(double u, int k) { if (k <= 0) k = (double)get_k(P_MIN); /* return (k * BUDGET / PERIOD) / (k + 2 * (1 - (BUDGET / PERIOD))); */ return (k * u) / (k + 2 * (1 - u)); }
bool pb_util::is_eq(expr* a, rational& k) const { if (is_eq(a)) { k = get_k(a); return true; } else { return false; } }
void IterQuantityAccessContiguous<T_info>::print_concrete_type( std::ostream& out ) const { const int last_updated = this->last_updated(); if(last_updated != base_t::NONE_UPDATED) out << typeName(get_k(last_updated)); else if( abstract_factory_.get() == NULL ) out << "NULL"; else out << typeName(*abstract_factory_->create()); }
void rs_encode(void *code,char *data[],int size) { int k=get_k(code); int n=get_n(code); for(int i=k;i<n;i++) { fec_encode(code, (void **)data, data[i],i, size); } return ; }
void QtUtil :: Grid3DView :: paint (QPainter & p, int w, int h) const { const int SIZE = 2 * m_width + 1; // Background p .fillRect (0, 0, w, h, Qt :: gray); p .setWindow (QRect (0, SIZE, SIZE, -SIZE)); for (int i = -m_width; i <= int (m_width); ++i) { for (int k = -m_width; k <= int (m_width); ++k) { int I = get_i (m_position) + i; int K = get_k (m_position) + k; const bool dark = (0 == I % 10) || (0 == K % 10); const bool light = (0 == I % 100) || (0 == K % 100); const auto & ODD = dark ? BG_DARK_ODD : BG_ODD; const auto & EVEN = dark ? BG_DARK_EVEN : BG_EVEN; p .fillRect ( i + m_width, k + m_width, 1, 1, (I^K)&1 ? (light ? BG_LIGHT_ODD : ODD) : (light ? BG_LIGHT_EVEN : EVEN)); } } for (int i = -m_width; i <= int (m_width); ++i) { for (int k = -m_width; k <= int (m_width); ++k) { p .save (); p .translate (i + m_width, k + m_width); draw (p, m_position + to_global (i, k)); p .restore (); } } draw_cursor (p, w, h); draw_axes (p, w, h); }
/* Encodes block data and returns k parameter for block */ int encode(char *input_data, int *output_data, int size) { int n = 0; int k = 0; int z = 0; int bit_mask = 0; char num_bits = 0; /* Clear buf */ memset(output_data, 0x00, size); /* Get k parameter given average sum squared error (input_data) */ k = get_k(get_sse(input_data, size)/size); write_byte((char)k, "Output/compressed.out"); for(n=0;n<size;n++) { /* Get last k bits */ bit_mask = 0xFF >> (8-k); output_data[n] = (int)input_data[n]; output_data[n] &= bit_mask; /* Add |1| after kth bit */ output_data[n] |= (0x01 << k); /* Buffer error and get last Z remaining bits */ bit_mask = 0xFF << k; z = (int)input_data[n]; z &= bit_mask; /* Align Z with bit 0 */ z >>= k; /* How many bits to write */ num_bits = k+1+z+1; /* Prefix sign bit and Z amount of zeroes */ output_data[n] |= (0x02 << (k+z)); /* Start putting bits from sign bit */ bit_mask = 0x01 << (num_bits-1); /* Code generated, write out */ while(num_bits > 0){ put_bit(output_data[n] & bit_mask); bit_mask >>= 1; num_bits--; } } return k; }
double get_rm_bound(double u, int k) { if (k <= 0) k = (double)get_k(P_MIN); double r; r = (2 * k + 2 * (1 - u)) / (k + 2 * (1 - u)); r = pow(r, 1 / N); r = r - 1; r = u * N * r; return r; }
void psnr_mae_calculator(){ int i,j,k; long double psnr,mae,a; long double psnr_add=0.0,mae_add=0.0; for(i=1;i<(fa.y+1);i++){ for(j=1;j<(fa.x+1);j++){ k=get_k(i,j,fa); psnr_add=psnr_add + pow( abs((double)pxb[k]-(double)pxa[k]),2); mae_add=mae_add + abs((double)pxb[k]-(double)pxa[k]); } } a=255*255; a=a*(fa.x*fa.y); a=a/psnr_add; psnr=10*log10(a); mae=mae_add/(fa.x*fa.y); //cout<<"\nPSNR = "<<psnr<<" ; MAE = "<<mae<<endl; //printf("\nPSNR = %Lf ; MAE = %Lf\n",psnr,mae); printf("%Lf\t%Lf\n", psnr, mae); }
int rs_decode(void *code,char *data[],int size) { int k=get_k(code); int n=get_n(code); int index[n]; int count=0; for(int i=0;i<n;i++) { if(data[i]!=0) { index[count++]=i; } } if(count<k) return -1; for(int i=0;i<n;i++) { if(i<count) data[i]=data[index[i]]; else data[i]=0; } return fec_decode(code,(void**)data,index,size); }
real PseudoBoolean<real>::minimize_reduction_fixetal(vector<label>& x, int& nlabelled) const { int n = index( x.size() ); // Work with a copy of the coefficients map<triple, real> aijk = this->aijk; //map<pair, real> aij = this->aij; //map<int, real> ai = this->ai; //real constant = this->constant; // The quadratic function we are reducing to int nedges = int( aij.size() + aijk.size() + aijkl.size() + 1000 ); int nvars = int( n + aijkl.size() + aijk.size() + 1000 ); QPBO<real> qpbo(nvars, nedges, err_function_qpbo); qpbo.AddNode(n); map<int,bool> var_used; // // Step 1: reduce all positive higher-degree terms // // Go through the variables one by one and perform the // reductions of the quartic terms //for (int ind=0; ind<n; ++ind) { // // Collect all terms with positive coefficients // // containing variable i // real alpha_sum = 0; // // Holds new var // int y = -1; // for (auto itr=aijkl.begin(); itr != aijkl.end(); ++itr) { // int i = get_i(itr->first); // int j = get_j(itr->first); // int k = get_k(itr->first); // int l = get_l(itr->first); // real a = itr->second; // // We only have to test for ind==i because of the // // order we process the indices // if (ind==i && a > 0) { // alpha_sum += a; // // Add term of degree 3 // aijk[ make_triple(j,k,l) ] += a; // // Add negative term of degree 4 // // -a*y*xj*xk*xl // if (y<0) y = qpbo.AddNode(); // int z = qpbo.AddNode(); // qpbo.AddUnaryTerm(z, 0, 3*a); // qpbo.AddPairwiseTerm(z,y, 0,0,0, -a); // qpbo.AddPairwiseTerm(z,j, 0,0,0, -a); // qpbo.AddPairwiseTerm(z,k, 0,0,0, -a); // qpbo.AddPairwiseTerm(z,l, 0,0,0, -a); // } // } // for (auto itr=aijk.begin(); itr != aijk.end(); ++itr) { // int i = get_i(itr->first); // int j = get_j(itr->first); // int k = get_k(itr->first); // real a = itr->second; // // We only have to test for ind==i because of the // // order we process the indices // if (ind==i && a > 0) { // alpha_sum += a; // // Add term of degree 2 // qpbo.AddPairwiseTerm(j,k, 0,0,0, a); // // Add negative term of degree 3 // // -a*y*xj*xk // if (y<0) y = qpbo.AddNode(); // int z = qpbo.AddNode(); // qpbo.AddUnaryTerm(z, 0, 2*a); // qpbo.AddPairwiseTerm(z,y, 0,0,0, -a); // qpbo.AddPairwiseTerm(z,j, 0,0,0, -a); // qpbo.AddPairwiseTerm(z,k, 0,0,0, -a); // } // } // if (alpha_sum > 0) { // // Add the new quadratic term // qpbo.AddPairwiseTerm(y,ind, 0,0,0, alpha_sum); // } //} // // This code should be equivalent to the commented // block above, but faster // vector<real> alpha_sum(n, 0); vector<int> y(n, -1); for (auto itr=aijkl.begin(); itr != aijkl.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); int l = get_l(itr->first); real a = itr->second; if (a > 0) { alpha_sum[i] += a; // Add term of degree 3 aijk[ make_triple(j,k,l) ] += a; // Add negative term of degree 4 // -a*y*xj*xk*xl if (y[i]<0) y[i] = qpbo.AddNode(); int z = qpbo.AddNode(); qpbo.AddUnaryTerm(z, 0, 3*a); qpbo.AddPairwiseTerm(z,y[i], 0,0,0, -a); qpbo.AddPairwiseTerm(z,j, 0,0,0, -a); qpbo.AddPairwiseTerm(z,k, 0,0,0, -a); qpbo.AddPairwiseTerm(z,l, 0,0,0, -a); } var_used[i] = true; var_used[j] = true; var_used[k] = true; var_used[l] = true; } for (auto itr=aijk.begin(); itr != aijk.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); real a = itr->second; if (a > 0) { alpha_sum[i] += a; // Add term of degree 2 qpbo.AddPairwiseTerm(j,k, 0,0,0, a); //aij[ make_pair(j,k) ] += a; // Add negative term of degree 3 // -a*y*xj*xk if (y[i]<0) y[i] = qpbo.AddNode(); /*int z = qpbo.AddNode(); qpbo.AddUnaryTerm(z, 0, 2*a); qpbo.AddPairwiseTerm(z,y[i], 0,0,0, -a); qpbo.AddPairwiseTerm(z,j, 0,0,0, -a); qpbo.AddPairwiseTerm(z,k, 0,0,0, -a);*/ aijk[ make_triple(y[i],j,k) ] += -a; } var_used[i] = true; var_used[j] = true; var_used[k] = true; } // No need to continue with the lower degree terms //for (auto itr=aij.begin(); itr != aij.end(); ++itr) { // int i = get_i(itr->first); // int j = get_j(itr->first); // real& a = itr->second; // if (a > 0) { // alpha_sum[i] += a; // // Add term of degree 1 // ai[ j ] += a; // // Add negative term of degree 2 // // -a*y*xj // if (y[i]<0) y[i] = qpbo.AddNode(); // aij[ make_pair(y[i],j) ] += -a; // // Now remove this term // a = 0; // } //} //for (auto itr=ai.begin(); itr != ai.end(); ++itr) { // int i =itr->first; // real& a = itr->second; // if (a > 0) { // alpha_sum[i] += a; // // Add term of degree 0 // constant += a; // // Add negative term of degree 1 // // -a*y*xj // if (y[i]<0) y[i] = qpbo.AddNode(); // ai[ y[i] ] += -a; // // Now remove this term // a = 0; // } //} for (int i=0;i<n;++i) { if (alpha_sum[i] > 0) { // Add the new quadratic term qpbo.AddPairwiseTerm(y[i],i, 0,0,0, alpha_sum[i]); } } // // Done with reducing all positive higher-degree terms // // Add all negative quartic terms for (auto itr=aijkl.begin(); itr != aijkl.end(); ++itr) { real a = itr->second; if (a < 0) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); int l = get_l(itr->first); int z = qpbo.AddNode(); qpbo.AddUnaryTerm(z, 0, -3*a); qpbo.AddPairwiseTerm(z,i, 0,0,0, a); qpbo.AddPairwiseTerm(z,j, 0,0,0, a); qpbo.AddPairwiseTerm(z,k, 0,0,0, a); qpbo.AddPairwiseTerm(z,l, 0,0,0, a); } } // Add all negative cubic terms for (auto itr=aijk.begin(); itr != aijk.end(); ++itr) { real a = itr->second; if (a < 0) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); int z = qpbo.AddNode(); qpbo.AddUnaryTerm(z, 0, -2*a); qpbo.AddPairwiseTerm(z,i, 0,0,0, a); qpbo.AddPairwiseTerm(z,j, 0,0,0, a); qpbo.AddPairwiseTerm(z,k, 0,0,0, a); } } // Add all quadratic terms for (auto itr=aij.begin(); itr != aij.end(); ++itr) { real a = itr->second; int i = get_i(itr->first); int j = get_j(itr->first); qpbo.AddPairwiseTerm(i,j, 0,0,0, a); var_used[i] = true; var_used[j] = true; } // Add all linear terms for (auto itr=ai.begin(); itr != ai.end(); ++itr) { real a = itr->second; int i = itr->first; qpbo.AddUnaryTerm(i, 0, a); var_used[i] = true; } qpbo.MergeParallelEdges(); qpbo.Solve(); qpbo.ComputeWeakPersistencies(); nlabelled = 0; for (int i=0; i<n; ++i) { if (var_used[i] || x.at(i)<0) { x[i] = qpbo.GetLabel(i); } if (x[i] >= 0) { nlabelled++; } } real energy = constant + qpbo.ComputeTwiceLowerBound()/2; return energy; }
void initialize_potential( ) { int i, j; double ro[Dim], rc[Dim], dr[Dim], mdr2 , pref , mdr , k2, kv[Dim] ; pref = V / ( pow( 2.0 * sqrt(PI) *Range, Dim ) ) ; // Note: the factor of V comes from the FFT for ( j=0 ; j<Dim ; j++ ) ro[j] = 0.0 ; for ( i=0 ; i<M ; i++ ) { get_r( i , rc ) ; mdr2 = pbc_mdr2( ro, rc, dr ) ; mdr = sqrt( mdr2 ) ; uG[i] = exp( -mdr2 / 4.0/Range2 ) * pref ; r_dudr[i] = -mdr2 * exp( -mdr2 / 4.0/Range2 ) ; tmp[i] = rho0 / 2.0 * ( 1.0 - erf( ( mdr - Rp ) / Xi ) ) * V; gammaP[i] = tmp[i] ; } // Set up the particle-particle potential // fftw_fwd( tmp , ktmp ) ; for ( i=0 ; i<M ; i++ ) ktmp2[i] = ktmp[i] * ktmp[i] ; fftw_back( ktmp2 , uP ) ; // Set up particle-polymer potential // #pragma omp parallel for private(k2,kv) for ( i=0 ; i<M ; i++ ) { k2 = get_k( i , kv ) ; ktmp[i] *= exp( -Range2*k2 /2.0) ; } fftw_back( ktmp , uPG ) ; for ( j=0 ; j<Dim ; j++ ) { field_gradient( uG , grad_uG[j] , j ) ; field_gradient( uP , grad_uP[j] , j ) ; field_gradient( uPG , grad_uPG[j] , j ) ; } int j2; for ( j=0 ; j<Dim ; j++ ) { #pragma omp parallel for private(j2, mdr2,rc, dr ) for ( i=0 ; i<M ; i++ ) { get_r( i , rc ) ; mdr2 = pbc_mdr2( rc , ro , dr ) ; for ( j2=0 ; j2<Dim ; j2++ ){ vir_func[j][j2][i] = dr[j2] * -grad_uG[j][i] ; vir_funcpp[j][j2][i] = dr[j2] * -grad_uP[j][i] ; vir_funcpg[j][j2][i] = dr[j2] * -grad_uPG[j][i] ; } } } for ( j=0 ; j<Dim ; j++ ) for ( j2=0 ; j2<Dim ; j2++ ){ fftw_fwd( vir_func[j][j2], vir_func_hat[j][j2] ) ; fftw_fwd( vir_funcpp[j][j2], vir_funcpp_hat[j][j2] ) ; fftw_fwd( vir_funcpg[j][j2], vir_funcpg_hat[j][j2] ) ; } // write_grid_data( "ug.dat" , uG ) ; // write_grid_data( "up.dat" , uP ) ; // write_grid_data( "upg.dat" , uPG ) ; for ( j=0 ; j<Dim ; j++ ) { char nm[20] ; fftw_fwd( grad_uG[j] , grad_uG_hat[j] ) ; // sprintf( nm , "grad_ug_%d.dat" , j ) ; // write_grid_data( nm , grad_uG[j] ) ; fftw_fwd( grad_uP[j] , grad_uP_hat[j] ) ; // sprintf( nm , "grad_up_%d.dat" , j ) ; // write_grid_data( nm , grad_uP[j] ) ; fftw_fwd( grad_uPG[j] , grad_uPG_hat[j] ) ; // sprintf( nm , "grad_upg_%d.dat" , j ) ; // write_grid_data( nm , grad_uPG[j] ) ; } }
// Message Block Processing Function. void block_proc(const word_t* block) { static const int proc_buffer_words = 80; word_t proc_buffer[proc_buffer_words]; // initialize proc_buffer. Copy from block and reversed Endian. int i; for(i=0; i<proc_block_words; i++) { proc_buffer[i] = reverse_endian(block[i]); } // initialize proc_buffer of 16-63 for(; i < proc_buffer_words; i++) { proc_buffer[i] = sigma_b1(proc_buffer[i-2]) + proc_buffer[i-7] + sigma_b0(proc_buffer[i-15]) + proc_buffer[i-16]; } ////////////////////////////////////////////// word_t a = buffer[0]; word_t b = buffer[1]; word_t c = buffer[2]; word_t d = buffer[3]; word_t e = buffer[4]; for(i = 0; i < PROC_BUFFER_WORDS; i+=2) { word_t temp1 = h + sigma_a1(e) + func_ch(e, f, g) + get_k(i) + proc_buffer(i) word_t temp2 = sigma_a0(a), + func_maj(a, b, c); word_t a_, b_, d_; d_ = circular_b_shift(b); if ( i <= 19 ){ a_ = func1(b,c,d) + K0_19; b_ = func1(a,d_,c) + K0_19; } else if ( i <= 39 ){ a_ = func2(b,c,d) + K20_39; b_ = func2(a,d_,c) + K20_39; } else if ( i <= 59 ){ a_ = func3(b,c,d) + K40_59; b_ = func3(a,d_,c) + K40_59; } else{ a_ = func2(b,c,d) + K60_79; b_ = func2(a,d_,c) + K60_79; } word_t temp_b = circular_a_shift(a) + e + proc_buffer[i] + a_; word_t temp_a = circular_a_shift(temp_b) + d + proc_buffer[i+1] + b_; e = c; d = d_; c = circular_b_shift(a); b = temp_b; a = temp_a; } buffer[0] += a; buffer[1] += b; buffer[2] += c; buffer[3] += d; buffer[4] += e; }
void ParticleAroundWalls::phi_t(arr& phi, arr& J, uint t, const arr& x_bar){ uint T=get_T(), n=dim_x(), k=get_k(); //assert some dimensions CHECK(x_bar.d0==k+1,""); CHECK(x_bar.d1==n,""); CHECK(t<=T,""); //-- transition costs: append to phi if(k==1) phi = x_bar[1]-x_bar[0]; //penalize velocity if(k==2) phi = x_bar[2]-2.*x_bar[1]+x_bar[0]; //penalize acceleration if(k==3) phi = x_bar[3]-3.*x_bar[2]+3.*x_bar[1]-x_bar[0]; //penalize jerk //-- walls: append to phi //Note: here we append to phi ONLY in certain time slices: the dimensionality of phi may very with time slices; see dim_phi(uint t) double eps=.1, power=2.; if(!hardConstrained){ //-- wall costs for(uint i=0;i<n;i++){ //add barrier costs to each dimension if(t==T/4) phi.append(MT::ineqConstraintCost(i+1.-x_bar(k,i), eps, power)); //middle factor: ``greater than i'' if(t==T/2) phi.append(MT::ineqConstraintCost(x_bar(k,i)+i+1., eps, power)); //last factor: ``lower than -i'' if(t==3*T/4) phi.append(MT::ineqConstraintCost(i+1.-x_bar(k,i), eps, power)); //middle factor: ``greater than i'' if(t==T) phi.append(MT::ineqConstraintCost(x_bar(k,i)+i+1., eps, power)); //last factor: ``lower than -i'' } }else{ //-- wall constraints for(uint i=0;i<n;i++){ //add barrier costs to each dimension if(t==T/4) phi.append((i+1.-x_bar(k,i))); //middle factor: ``greater than i'' if(t==T/2) phi.append((x_bar(k,i)+i+1.)); //last factor: ``lower than -i'' if(t==3*T/4) phi.append((i+1.-x_bar(k,i))); //middle factor: ``greater than i'' if(t==T) phi.append((x_bar(k,i)+i+1.)); //last factor: ``lower than -i'' } } uint m=phi.N; CHECK(m==dim_phi(t),""); if(&J){ //we also need to return the Jacobian J.resize(m,k+1,n).setZero(); //-- transition costs for(uint i=0;i<n;i++){ if(k==1){ J(i,1,i) = 1.; J(i,0,i) = -1.; } if(k==2){ J(i,2,i) = 1.; J(i,1,i) = -2.; J(i,0,i) = 1.; } if(k==3){ J(i,3,i) = 1.; J(i,2,i) = -3.; J(i,1,i) = +3.; J(i,0,i) = -1.; } } //-- walls if(!hardConstrained){ for(uint i=0;i<n;i++){ if(t==T/4) J(n+i,k,i) = -MT::d_ineqConstraintCost(i+1.-x_bar(k,i), eps, power); if(t==T/2) J(n+i,k,i) = MT::d_ineqConstraintCost(x_bar(k,i)+i+1., eps, power); if(t==3*T/4) J(n+i,k,i) = -MT::d_ineqConstraintCost(i+1.-x_bar(k,i), eps, power); if(t==T) J(n+i,k,i) = MT::d_ineqConstraintCost(x_bar(k,i)+i+1., eps, power); } }else{ for(uint i=0;i<n;i++){ if(t==T/4) J(n+i,k,i) = -1.; if(t==T/2) J(n+i,k,i) = +1.; if(t==3*T/4) J(n+i,k,i) = -1.; if(t==T) J(n+i,k,i) = +1.; } } } }
real PseudoBoolean<real>::minimize_reduction_M(vector<label>& x, int& nlabelled) const { // // Compute a large enough value for M // real M = 0; for (auto itr=aijkl.begin(); itr != aijkl.end(); ++itr) { real a = itr->second; M += abs(a); } for (auto itr=aijk.begin(); itr != aijk.end(); ++itr) { real a = itr->second; M += abs(a); } for (auto itr=aij.begin(); itr != aij.end(); ++itr) { real a = itr->second; M += abs(a); } for (auto itr=ai.begin(); itr != ai.end(); ++itr) { real a = itr->second; M += abs(a); } // Copy of the polynomial. Will contain the reduced polynomial PseudoBoolean<real> pb = *this; // Number of variables (will be increased int n = nvars(); int norg = n; // // Reduce the degree-4 terms // double M2 = M; for (auto itr=pb.aijkl.begin(); itr != pb.aijkl.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); int l = get_l(itr->first); real a = itr->second; // a*xi*xj*xk*xl will be converted to // a*xi*xj*z + M( xk*xl - 2*xk*z - 2*xl*z + 3*z ) int z = n++; pb.add_monomial(i,j,z, a); pb.add_monomial(k,l,M); pb.add_monomial(k,z, -2*M); pb.add_monomial(l,z, -2*M); pb.add_monomial(z, 3*M); M2 += a + 4*M; } // Remove all degree-4 terms. pb.aijkl.clear(); // // Reduce the degree-3 terms // for (auto itr=pb.aijk.begin(); itr != pb.aijk.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); real a = itr->second; // a*xi*xj*xk will be converted to // a*xi*z + M( xj*xk -2*xj*z -2*xk*z + 3*z ) int z = n++; pb.add_monomial(i,z, a); pb.add_monomial(j,k,M2); pb.add_monomial(j,z, -2*M2); pb.add_monomial(k,z, -2*M2); pb.add_monomial(z, 3*M2); } // Remove all degree-3 terms. pb.aijk.clear(); // // Minimize the reduced polynomial // vector<label> xtmp(n,-1); real bound = pb.minimize(xtmp, HOCR); nlabelled = 0; for (int i=0;i<norg;++i) { x.at(i) = xtmp[i]; if (x[i] >= 0) { nlabelled++; } } return bound; }
real PseudoBoolean<real>::minimize_reduction(vector<label>& x, int& nlabelled) const { index nVars = index( x.size() ); // Here it is important that all indices appear as pairs // in aij or ai ASSERT_STR( nvars() <= nVars , "x too small"); PBF<real, 4> hocr; map<int,bool> var_used; for (auto itr=ai.begin(); itr != ai.end(); ++itr) { int i = itr->first; real a = itr->second; hocr.AddUnaryTerm(i, 0, a); var_used[i] = true; } for (auto itr=aij.begin(); itr != aij.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); real a = itr->second; hocr.AddPairwiseTerm(i,j, 0,0,0, a); var_used[i] = true; var_used[j] = true; } for (auto itr=aijk.begin(); itr != aijk.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); real a = itr->second; int ind[] = {i,j,k}; real E[8] = {0,0,0,0, 0,0,0,0}; E[7] = a; hocr.AddHigherTerm(3, ind, E); var_used[i] = true; var_used[j] = true; var_used[k] = true; } for (auto itr=aijkl.begin(); itr != aijkl.end(); ++itr) { int i = get_i(itr->first); int j = get_j(itr->first); int k = get_k(itr->first); int l = get_l(itr->first); real a = itr->second; int ind[] = {i,j,k,l}; real E[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; E[15] = a; hocr.AddHigherTerm(4, ind, E); var_used[i] = true; var_used[j] = true; var_used[k] = true; var_used[l] = true; } index nOptimizedVars = hocr.maxID() + 1; PBF<real,2> qpbf; hocr.toQuadratic(qpbf); hocr.clear(); QPBO<real> qpbo(nVars, qpbf.size(), err_function_qpbo); convert(qpbo, qpbf); qpbo.MergeParallelEdges(); qpbo.Solve(); qpbo.ComputeWeakPersistencies(); nlabelled = 0; for (int i=0; i < nOptimizedVars; ++i) { if (var_used[i] || x.at(i)<0) { x[i] = qpbo.GetLabel(i); } if (x[i] >= 0) { nlabelled++; } } // These variables were not part of the minimization ASSERT(nVars >= nOptimizedVars); nlabelled += nVars - nOptimizedVars; real energy = constant + qpbo.ComputeTwiceLowerBound()/2; //double energy = eval(x); //Only when x is fully labelled return energy; }
void disp_sample_table_get_sample(const struct disp_sample_table *dt, int index, double *w, double *n, double *k) { *w = get_wavelength(dt, index); *n = get_n(dt, index); *k = get_k(dt, index); }
double get_b(segment a) { if (!cmp(a.a.x, a.b.x)) return 0; return a.a.y - a.a.x * get_k(a); }
void QtUtil :: Grid3DView :: draw_cursor (QPainter & p, int, int) const { int c_i = get_i (m_cursor) - get_i (m_position); int c_k = get_k (m_cursor) - get_k (m_position); if (get_m (m_cursor) > get_m (m_position)) p .setPen (CURSOR_ABOVE); else if (get_m (m_cursor) < get_m (m_position)) p .setPen (CURSOR_BELOW); else p .setPen (CURSOR_LEVEL); auto arrow = [&] (int x, int y, int dir) { p .save (); p .translate (m_width + x + 0.5, m_width + y + 0.5); p .rotate (dir * 360 / 8); p .scale (0.15, 0.15); p .drawLine (0, 0, 3, 0); p .drawLine (2, 1, 3, 0); p .drawLine (2, -1, 3, 0); p .restore (); }; const int W = m_width; if (c_i < -W) { if (c_k < -W) arrow (-W, -W, 5); else if (c_k > W) arrow (-W, W, 3); else arrow (-W, c_k, 4); } else if (c_i > W) { if (c_k < -W) arrow (W, -W, 7); else if (c_k > W) arrow (W, W, 1); else arrow (W, c_k, 0); } else if (c_k < -W) { assert (-W <= c_i && c_i <= W); arrow (c_i, -W, 6); } else if (c_k > W) { assert (-W <= c_i && c_i <= W); arrow (c_i, W, 2); } else { assert (-W <= c_i && c_i <= W); assert (-W <= c_k && c_k <= W); p .drawRect (c_i + W, c_k + W, 1, 1); const int di = m_drag_info .di; const int dk = m_drag_info .dk; if (m_drag_info .dragging && (di || dk)) { int angle = 0; if (dk == 0) { angle = (di > 0) ? 0 : 4; } else { if (di > 0) angle = 1; else if (0 == di) angle = 2; else angle = 3; if (dk < 0) angle = -angle; } arrow (c_i + di, c_k + dk, angle); } } }