コード例 #1
0
ファイル: IMesh.cpp プロジェクト: openhumanoids/exotica
 void IMesh::computeLaplace(int t)
 {
   int N = eff_size_;
   dist.resize(N, N);
   dist.setZero();
   wsum.resize(N);
   wsum.setZero();
   int j, l;
   double w;
   /** Compute distance matrix (inverse proportional) */
   for (j = 0; j < N; j++)
   {
     for (l = j + 1; l < N; l++)
     {
       if (!(j >= N && l >= N))
       {
         dist(j, l) = dist(l, j) = sqrt(
             (EFFPHI.segment(j, 3) - EFFPHI.segment(l, 3)).dot(
                 ( EFFPHI.segment(j, 3) - EFFPHI.segment(l, 3))));
       }
     }
   }
   /** Computer weight normaliser */
   for (j = 0; j < N; j++)
   {
     for (l = 0; l < N; l++)
     {
       if (dist(j, l) > 0 && j != l)
       {
         wsum(j) += weights_(j, l) / dist(j, l);
       }
     }
   }
   /** Compute Laplace coordinates */
   for (j = 0; j < N; j++)
   {
     PHI.segment(j, 3) = EFFPHI.segment(j, 3);
     for (l = 0; l < N; l++)
     {
       if (j != l)
       {
         if (dist(j, l) > 0 && wsum(j) > 0)
         {
           w = weights_(j, l) / (dist(j, l) * wsum(j));
           PHI.segment(j, 3) -= EFFPHI.segment(l, 3) * w;
         }
       }
     }
   }
 }
コード例 #2
0
int main(int argc, char* argv[]) {
    if (argc < 2) {
        return print_usage(argv[0]);
    }

    std::fstream fs;
    fs.open(argv[1], std::ios_base::in);

    if (!fs) {
        std::cerr << "File " << argv[1] << " could not be opened for reading" << std::endl;
        return 1;
    }

    std::string s;
    std::getline(fs, s);
    //std::cout << s << std::endl;
    std::vector<Task> t;
    std::vector<Task1> t1;

    while (std::getline(fs, s)) {
        int w, l;
        std::istringstream iss {s, std::ios_base::in};
        iss >> w;
        iss >> l;
        Task task_i {w, l}; 
        Task1 task_i1 {w, l}; 
        t.push_back(task_i);
        t1.push_back(task_i1);
    }

    std::sort(t.begin(), t.end(), [](const Task& t1, const Task& t2) {if (t1 < t2) return false; else return true;}) ;

    std::sort(t1.begin(), t1.end(), [](const Task1& t1, const Task1& t2) {if (t1 < t2) return false; else return true;}) ;

    //for (unsigned int i = 0; i < t.size(); i++) {
    //    std::cout << t[i].get_weight() << " " << t[i].get_length() << " " << t[i].get_cost() << std::endl;
    //}
    std::cout << "Minimum weighted sum of completion times: " << wsum(t1) << std::endl;
    std::cout << "Optimal Minimum weighted sum of completion times: " << wsum(t) << std::endl;
    //std::cout << "Size of int" << " " << sizeof(int) << std::endl ;
    //std::cout << "Size of long int" << " " << sizeof(long int) << std::endl ;
    //[&t]() { if (t[2] < t[1]) return true; else return false;} ;
    //if (t[1] < t[2]) 
    //    std::cout << "true" ;


    fs.close();
}
コード例 #3
0
inline void
FixedTripleListTypesInteractionTemplate<_Potential>::computeVirialTensor(Tensor &w) {
  LOG4ESPP_INFO(theLogger, "compute the virial tensor for the FixedTriple List");

  Tensor wlocal(0.0);
  const bc::BC& bc = *getSystemRef().bc;
  for (FixedTripleList::TripleList::Iterator it(*fixedtripleList); it.isValid(); ++it) {
    const Particle &p1 = *it->first;
    const Particle &p2 = *it->second;
    const Particle &p3 = *it->third;
    int type1 = p1.type();
    int type2 = p2.type();
    int type3 = p3.type();
    const Potential &potential = getPotential(type1, type2, type3);
    Real3D r12, r32;
    bc.getMinimumImageVectorBox(r12, p1.position(), p2.position());
    bc.getMinimumImageVectorBox(r32, p3.position(), p2.position());
    Real3D force12, force32;
    potential._computeForce(force12, force32, r12, r32);
    wlocal += Tensor(r12, force12) + Tensor(r32, force32);
  }

  // reduce over all CPUs
  Tensor wsum(0.0);
  boost::mpi::all_reduce(*mpiWorld, wlocal, wsum, std::plus<Tensor>());
  w += wsum;
}
コード例 #4
0
    inline void
    FixedQuadrupleListInteractionTemplate < _DihedralPotential >::
    computeVirialTensor(Tensor& w) {
      LOG4ESPP_INFO(theLogger, "compute the virial tensor of the quadruples");
    
      Tensor wlocal(0.0);
      const bc::BC& bc = *getSystemRef().bc;

      for (FixedQuadrupleList::QuadrupleList::Iterator it(*fixedquadrupleList); it.isValid(); ++it) {
        const Particle &p1 = *it->first;
        const Particle &p2 = *it->second;
        const Particle &p3 = *it->third;
        const Particle &p4 = *it->fourth;

        Real3D dist21, dist32, dist43; 

        bc.getMinimumImageVectorBox(dist21, p2.position(), p1.position());
        bc.getMinimumImageVectorBox(dist32, p3.position(), p2.position());
        bc.getMinimumImageVectorBox(dist43, p4.position(), p3.position());

        Real3D force1, force2, force3, force4;

        potential->_computeForce(force1, force2, force3, force4,
                                dist21, dist32, dist43);

        // TODO: formulas are not correct yet

        wlocal += Tensor(dist21, force1) - Tensor(dist32, force2);
      }
      // reduce over all CPUs
      Tensor wsum(0.0);
      boost::mpi::all_reduce(*mpiWorld, (double*)&wlocal, 6, (double*)&wsum, std::plus<double>());
      w += wsum;
    }
コード例 #5
0
ファイル: hdrgen.cpp プロジェクト: davll/vfx2014
static cv::Mat generateRadianceMap(const cv::Mat& rc)
{
  cv::Size size = getImage(0).size();
  
  cv::Mat_<cv::Vec3f> radiance(size);
  
  for (int y = 0; y < size.height; ++y) {
    for (int x = 0; x < size.width; ++x) {
      cv::Vec3f sum(0,0,0), wsum(0,0,0);
      
      for (int p = 0, n = numImages(); p < n; ++p) {
        const cv::Mat& img = getImage(p);
        cv::Vec3b c = img.at<cv::Vec3b>(y, x);
        float exptime = getExposureTime(p);
        
        cv::Vec3f tmp, wtmp(weight(c[0]), weight(c[1]), weight(c[2]));
        tmp[0] = (rc.at<float>(c[0], 0) - std::log(exptime)) * wtmp[0];
        tmp[1] = (rc.at<float>(c[1], 1) - std::log(exptime)) * wtmp[1];
        tmp[2] = (rc.at<float>(c[2], 2) - std::log(exptime)) * wtmp[2];
        sum += tmp;
        wsum += wtmp;
      }
      
      cv::Vec3f tmp;
      tmp[0] = std::exp( sum[0] / wsum[0] );
      tmp[1] = std::exp( sum[1] / wsum[1] );
      tmp[2] = std::exp( sum[2] / wsum[2] );
      
      radiance.at<cv::Vec3f>(y, x) = tmp;
    }
  }
  
  return radiance;
}
    template < typename _AngularPotential > inline void
    FixedTripleAngleListInteractionTemplate < _AngularPotential >::
    computeVirialTensor(Tensor& w) {
      LOG4ESPP_INFO(theLogger, "compute the virial tensor of the triples");

      Tensor wlocal(0.0);
      const bc::BC& bc = *getSystemRef().bc;
      for (FixedTripleAngleList::TripleList::Iterator it(*fixedtripleList); it.isValid(); ++it){
        const Particle &p1 = *it->first;
        const Particle &p2 = *it->second;
        const Particle &p3 = *it->third;
        //const Potential &potential = getPotential(0, 0);
        Real3D r12, r32;
        bc.getMinimumImageVectorBox(r12, p1.position(), p2.position());
        bc.getMinimumImageVectorBox(r32, p3.position(), p2.position());
        Real3D force12, force32;

        real currentAngle = fixedtripleList->getAngle(p1.getId(), p2.getId(), p3.getId());
        
        potential->_computeForce(force12, force32, r12, r32, currentAngle);
        wlocal += Tensor(r12, force12) + Tensor(r32, force32);
      }
      
      // reduce over all CPUs
      Tensor wsum(0.0);
      boost::mpi::all_reduce(*mpiWorld, (double*)&wlocal,6, (double*)&wsum, std::plus<double>());
      w += wsum;
    }
コード例 #7
0
inline void
FixedQuadrupleListTypesInteractionTemplate<_DihedralPotential>::
computeVirialTensor(Tensor &w, real z) {
    LOG4ESPP_INFO(theLogger, "compute the virial tensor of the quadruples");

    Tensor wlocal(0.0);
    const bc::BC &bc = *getSystemRef().bc;

    std::cout << "Warning!!! computeVirialTensor in specified volume doesn't work for "
              "FixedQuadrupleListTypesInteractionTemplate at the moment" << std::endl;

    for (FixedQuadrupleList::QuadrupleList::Iterator it(*fixedquadrupleList); it.isValid(); ++it) {
        const Particle &p1 = *it->first;
        const Particle &p2 = *it->second;
        const Particle &p3 = *it->third;
        const Particle &p4 = *it->fourth;

        longint type1 = p1.type();
        longint type2 = p2.type();
        longint type3 = p3.type();
        longint type4 = p4.type();

        const Potential &potential = getPotential(type1, type2, type3, type4);

        Real3D dist21, dist32, dist43;

        bc.getMinimumImageVectorBox(dist21, p2.position(), p1.position());
        bc.getMinimumImageVectorBox(dist32, p3.position(), p2.position());
        bc.getMinimumImageVectorBox(dist43, p4.position(), p3.position());

        Real3D force1, force2, force3, force4;

        potential.computeForce(force1, force2, force3, force4,
                               dist21, dist32, dist43);

        // TODO: formulas are not correct yet

        wlocal += Tensor(dist21, force1) - Tensor(dist32, force2);
    }
    // reduce over all CPUs
    Tensor wsum(0.0);
    boost::mpi::all_reduce(*mpiWorld, (double *) &wlocal, 6, (double *) &wsum, std::plus<double>());
    w += wsum;
}
コード例 #8
0
// Coordinate descent for logistic models (no active set cycling)
RcppExport SEXP cdfit_binomial_hsr_slores_nac(SEXP X_, SEXP y_, SEXP n_pos_, SEXP ylab_, 
                                          SEXP row_idx_, SEXP lambda_, SEXP nlambda_,
                                          SEXP lam_scale_, SEXP lambda_min_, 
                                          SEXP alpha_, SEXP user_, 
                                          SEXP eps_, SEXP max_iter_, SEXP multiplier_, 
                                          SEXP dfmax_, SEXP ncore_, SEXP warn_,
                                          SEXP safe_thresh_, SEXP verbose_) {
  XPtr<BigMatrix> xMat(X_);
  double *y = REAL(y_);
  int n_pos = INTEGER(n_pos_)[0];
  IntegerVector ylabel = Rcpp::as<IntegerVector>(ylab_); // label vector of {-1, 1}
  int *row_idx = INTEGER(row_idx_);
  double lambda_min = REAL(lambda_min_)[0];
  double alpha = REAL(alpha_)[0];
  int n = Rf_length(row_idx_); // number of observations used for fitting model
  int p = xMat->ncol();
  int L = INTEGER(nlambda_)[0];
  int lam_scale = INTEGER(lam_scale_)[0];
  double eps = REAL(eps_)[0];
  int max_iter = INTEGER(max_iter_)[0];
  double *m = REAL(multiplier_);
  int dfmax = INTEGER(dfmax_)[0];
  int warn = INTEGER(warn_)[0];
  int user = INTEGER(user_)[0];
  double slores_thresh = REAL(safe_thresh_)[0]; // threshold for safe test
  int verbose = INTEGER(verbose_)[0];

  NumericVector lambda(L);
  NumericVector Dev(L);
  IntegerVector iter(L);
  IntegerVector n_reject(L); // number of total rejections;
  IntegerVector n_slores_reject(L); // number of safe rejections;
  NumericVector beta0(L);
  NumericVector center(p);
  NumericVector scale(p);
  int p_keep = 0; // keep columns whose scale > 1e-6
  int *p_keep_ptr = &p_keep;
  vector<int> col_idx;
  vector<double> z;
  double lambda_max = 0.0;
  double *lambda_max_ptr = &lambda_max;
  int xmax_idx = 0;
  int *xmax_ptr = &xmax_idx;
  
  // set up omp
  int useCores = INTEGER(ncore_)[0];
#ifdef BIGLASSO_OMP_H_
  int haveCores = omp_get_num_procs();
  if(useCores < 1) {
    useCores = haveCores;
  }
  omp_set_dynamic(0);
  omp_set_num_threads(useCores);
#endif
  
  if (verbose) {
    char buff1[100];
    time_t now1 = time (0);
    strftime (buff1, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now1));
    Rprintf("\nPreprocessing start: %s\n", buff1);
  }
  
  // standardize: get center, scale; get p_keep_ptr, col_idx; get z, lambda_max, xmax_idx;
  standardize_and_get_residual(center, scale, p_keep_ptr, col_idx, z, lambda_max_ptr, xmax_ptr, xMat, 
                               y, row_idx, lambda_min, alpha, n, p);
  p = p_keep; // set p = p_keep, only loop over columns whose scale > 1e-6

  if (verbose) {
    char buff1[100];
    time_t now1 = time (0);
    strftime (buff1, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now1));
    Rprintf("Preprocessing end: %s\n", buff1);
    Rprintf("\n-----------------------------------------------\n");
  }

  arma::sp_mat beta = arma::sp_mat(p, L); //beta
  double *a = Calloc(p, double); //Beta from previous iteration
  double a0 = 0.0; //beta0 from previousiteration
  double *w = Calloc(n, double);
  double *s = Calloc(n, double); //y_i - pi_i
  double *eta = Calloc(n, double);
  // int *e1 = Calloc(p, int); //ever-active set
  int *e2 = Calloc(p, int); //strong set
  double xwr, xwx, pi, u, v, cutoff, l1, l2, shift, si;
  double max_update, update, thresh; // for convergence check
  int i, j, jj, l, violations, lstart;
  
  double ybar = sum(y, n) / n;
  a0 = beta0[0] = log(ybar / (1-ybar));
  double nullDev = 0;
  double *r = Calloc(n, double);
  for (i = 0; i < n; i++) {
    r[i] = y[i];
    nullDev = nullDev - y[i]*log(ybar) - (1-y[i])*log(1-ybar);
    s[i] = y[i] - ybar;
    eta[i] = a0;
  }
  thresh = eps * nullDev / n;
  double sumS = sum(s, n); // temp result sum of s
  double sumWResid = 0.0; // temp result: sum of w * r

  // set up lambda
  if (user == 0) {
    if (lam_scale) { // set up lambda, equally spaced on log scale
      double log_lambda_max = log(lambda_max);
      double log_lambda_min = log(lambda_min*lambda_max);
      
      double delta = (log_lambda_max - log_lambda_min) / (L-1);
      for (l = 0; l < L; l++) {
        lambda[l] = exp(log_lambda_max - l * delta);
      }
    } else { // equally spaced on linear scale
      double delta = (lambda_max - lambda_min*lambda_max) / (L-1);
      for (l = 0; l < L; l++) {
        lambda[l] = lambda_max - l * delta;
      }
    }
    Dev[0] = nullDev;
    lstart = 1;
    n_reject[0] = p;
  } else {
    lstart = 0;
    lambda = Rcpp::as<NumericVector>(lambda_);
  }

  // Slores variables
  vector<double> theta_lam;
  double g_theta_lam = 0.0;
  double prod_deriv_theta_lam = 0.0;
  double *g_theta_lam_ptr = &g_theta_lam;
  double *prod_deriv_theta_lam_ptr = &prod_deriv_theta_lam;
  vector<double> X_theta_lam_xi_pos; 
  vector<double> prod_PX_Pxmax_xi_pos;
  vector<double> cutoff_xi_pos;
  int *slores_reject = Calloc(p, int);
  int *slores_reject_old = Calloc(p, int);
  for (int j = 0; j < p; j++) slores_reject_old[j] = 1;
  
  int slores; // if 0, don't perform Slores rule
  if (slores_thresh < 1) {
    slores = 1; // turn on slores
    theta_lam.resize(n);
    X_theta_lam_xi_pos.resize(p);
    prod_PX_Pxmax_xi_pos.resize(p);
    cutoff_xi_pos.resize(p);

    slores_init(theta_lam, g_theta_lam_ptr, prod_deriv_theta_lam_ptr, cutoff_xi_pos,
                X_theta_lam_xi_pos, prod_PX_Pxmax_xi_pos, 
                xMat, y, z, xmax_idx, row_idx, col_idx, 
                center, scale, ylabel, n_pos, n, p);
  } else {
    slores = 0;
  }
  
  if (slores == 1 && user == 0) n_slores_reject[0] = p;

  for (l = lstart; l < L; l++) {
    if(verbose) {
      // output time
      char buff[100];
      time_t now = time (0);
      strftime (buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now));
      Rprintf("Lambda %d. Now time: %s\n", l, buff);
    }
    
    if (l != 0) {
      // Check dfmax
      int nv = 0;
      for (j = 0; j < p; j++) {
        if (a[j] != 0) {
          nv++;
        }
      }
      if (nv > dfmax) {
        for (int ll=l; ll<L; ll++) iter[ll] = NA_INTEGER;
        Free(slores_reject);
        Free(slores_reject_old);
        Free_memo_bin_hsr_nac(s, w, a, r, e2, eta);
        return List::create(beta0, beta, center, scale, lambda, Dev, 
                            iter, n_reject, Rcpp::wrap(col_idx));
      }
      cutoff = 2*lambda[l] - lambda[l-1];
    } else {
      cutoff = 2*lambda[l] - lambda_max;
    }

    if (slores) {
      slores_screen(slores_reject, theta_lam, g_theta_lam, prod_deriv_theta_lam,
                    X_theta_lam_xi_pos, prod_PX_Pxmax_xi_pos, cutoff_xi_pos,
                    row_idx, col_idx, center, scale, xmax_idx, ylabel, 
                    lambda[l], lambda_max, n_pos, n, p);
      n_slores_reject[l] = sum(slores_reject, p);
      
      // update z[j] for features which are rejected at previous lambda but accepted at current one.
      update_zj(z, slores_reject, slores_reject_old, xMat, row_idx, col_idx, center, scale, sumS, s, m, n, p);
      
      #pragma omp parallel for private(j) schedule(static) 
      for (j = 0; j < p; j++) {
        slores_reject_old[j] = slores_reject[j];
        // hsr screening
        // if (slores_reject[j] == 0 && (fabs(z[j]) > (cutoff * alpha * m[col_idx[j]]))) {
        if (fabs(z[j]) > (cutoff * alpha * m[col_idx[j]])) {
          e2[j] = 1;
        } else {
          e2[j] = 0;
        }
      }
    } else {
      n_slores_reject[l] = 0; 
      // hsr screening over all
      #pragma omp parallel for private(j) schedule(static) 
      for (j = 0; j < p; j++) {
        if (fabs(z[j]) > (cutoff * alpha * m[col_idx[j]])) {
          e2[j] = 1;
        } else {
          e2[j] = 0;
        }
      }
    }
    n_reject[l] = p - sum(e2, p);
    
    while (iter[l] < max_iter) {
      while (iter[l] < max_iter) {
        while (iter[l] < max_iter) {
          iter[l]++;
          Dev[l] = 0.0;
          
          for (i = 0; i < n; i++) {
            if (eta[i] > 10) {
              pi = 1;
              w[i] = .0001;
            } else if (eta[i] < -10) {
              pi = 0;
              w[i] = .0001;
            } else {
              pi = exp(eta[i]) / (1 + exp(eta[i]));
              w[i] = pi * (1 - pi);
            }
            s[i] = y[i] - pi;
            r[i] = s[i] / w[i];
            if (y[i] == 1) {
              Dev[l] = Dev[l] - log(pi);
            } else {
              Dev[l] = Dev[l] - log(1-pi);
            }
          }
          
          if (Dev[l] / nullDev < .01) {
            if (warn) warning("Model saturated; exiting...");
            for (int ll=l; ll<L; ll++) iter[ll] = NA_INTEGER;
            Free(slores_reject);
            Free(slores_reject_old);
            Free_memo_bin_hsr_nac(s, w, a, r, e2, eta);
            return List::create(beta0, beta, center, scale, lambda, Dev, iter, n_reject, n_slores_reject, Rcpp::wrap(col_idx));
          }
          // Intercept
          xwr = crossprod(w, r, n, 0);
          xwx = sum(w, n);
          beta0[l] = xwr / xwx + a0;
          si = beta0[l] - a0;
          if (si != 0) {
            a0 = beta0[l];
            for (i = 0; i < n; i++) {
              r[i] -= si; //update r
              eta[i] += si; //update eta
            }
          }
          sumWResid = wsum(r, w, n); // update temp result: sum of w * r, used for computing xwr;
          max_update = 0.0;
          for (j = 0; j < p; j++) {
            if (e2[j]) {
              jj = col_idx[j];
              xwr = wcrossprod_resid(xMat, r, sumWResid, row_idx, center[jj], scale[jj], w, n, jj);
              v = wsqsum_bm(xMat, w, row_idx, center[jj], scale[jj], n, jj) / n;
              u = xwr/n + v * a[j];
              l1 = lambda[l] * m[jj] * alpha;
              l2 = lambda[l] * m[jj] * (1-alpha);
              beta(j, l) = lasso(u, l1, l2, v);

              shift = beta(j, l) - a[j];
              if (shift != 0) {
                // update change of objective function
                // update = - u * shift + (0.5 * v + 0.5 * l2) * (pow(beta(j, l), 2) - pow(a[j], 2)) + l1 * (fabs(beta(j, l)) - fabs(a[j]));
                update = pow(beta(j, l) - a[j], 2) * v;
                if (update > max_update) max_update = update;
                update_resid_eta(r, eta, xMat, shift, row_idx, center[jj], scale[jj], n, jj); // update r
                sumWResid = wsum(r, w, n); // update temp result w * r, used for computing xwr;
                a[j] = beta(j, l); // update a
              }
            }
          }
          // Check for convergence
          if (max_update < thresh)  break;
        }

      }
      // Scan for violations in rest
      if (slores) {
        violations = check_rest_set_hsr_slores_nac(e2, slores_reject, z, xMat, row_idx, col_idx, center, scale, a, lambda[l], sumS, alpha, s, m, n, p);
      } else {
        violations = check_rest_set_bin_nac(e2, z, xMat, row_idx, col_idx, center, scale, a, lambda[l], sumS, alpha, s, m, n, p);
      }
      if (violations == 0) break;
      if (n_slores_reject[l] <= p * slores_thresh) {
        slores = 0; // turn off slores screening for next iteration if not efficient
      }
    }
  }
  Free(slores_reject);
  Free(slores_reject_old);
  Free_memo_bin_hsr_nac(s, w, a, r, e2, eta);
  return List::create(beta0, beta, center, scale, lambda, Dev, iter, n_reject, n_slores_reject, Rcpp::wrap(col_idx));
}
コード例 #9
0
ファイル: cevt.c プロジェクト: mcromaz/petcat
int main(int argc, char **argv) {

  FILE *fin, *fou, *ftr;
  struct gebData ghdr, defaulthdr = {100, sizeof(Mario), 0ll};
  Mario *evt;
  char evtlabel[] = {'a', 'b', 's'}, seglabel[] = {'n', 'l', 'r', 'u', 'd'};
  struct option opts[] = {{"verbose", no_argument, 0, 'v'},
                          {"sum-only", no_argument, 0, 's'},
                          {"append", no_argument, 0, 'a'},
                          {"ratio", required_argument, 0, 'r'},
                          { 0, 0, 0, 0}};
  int verboseFlag = 0, sumOnlyFlag = 0, appendFlag = 0;
  int i, j, k, num, seg, baselineFlag = 1;
  double ratio = 0.5;   // default
  char ch;

  for (i = 0; i < 2; i++) {
    runList[i].rawEvts = malloc(MAX_EVT * sizeof(Mario));
  }

  while ((ch = getopt_long(argc, argv, "vsar:", opts, 0)) != -1) {
    switch(ch) {
    case 'v': verboseFlag = 1;
              fprintf(stdout, "I'm verbose ..\n");
              break;
    case 's': sumOnlyFlag = 1;
              break;
    case 'a': appendFlag = 1;
              break;
    case 'r': ratio = atof(optarg);
              break;
    default: fprintf(stderr, "usage: cevt [-vsar:]\n");
             exit(1);
    }
  }

  for (i = 0; i < 2; i++) {
    fin = fopen(runList[i].filename, "r");
    if (fin == 0) { fprintf(stderr, "could not open file %s\n", runList[i].filename); exit(1);}
    while ((runList[i].numEvts < MAX_EVT) && (fread(&ghdr, sizeof(struct gebData), 1, fin) == 1)) {
      if (ghdr.type == 100) {
        num = fread(runList[i].rawEvts + runList[i].numEvts++, sizeof(Mario), 1, fin);
        assert(num == 1);
      }
      else {
        fseek(fin, ghdr.length, SEEK_CUR);
      }
    }
    fclose(fin);
  }

  pList[0].rawEvts = avg(runList + 0, baselineFlag);
  pList[1].rawEvts = avg(runList + 1, baselineFlag);
  pList[2].rawEvts = wsum(pList[0].rawEvts, pList[1].rawEvts, ratio);

  fou = (appendFlag == 1) ? fopen("cevt.dat", "a") : fopen("cevt.dat", "w");
  if (fou == 0) { fprintf(stderr, "could not open file cevt.dat\n"); exit(1);}
  for (i = 0; i < 3; i++) {
    if (sumOnlyFlag == 1 && i != 2) { continue; }
    assert( 1 == fwrite(&defaulthdr, sizeof(struct gebData), 1, fou));
    assert( 1 == fwrite(pList[i].rawEvts, sizeof(Mario), 1, fou));
  }
  fclose(fou);

  printf("list 1: %d evts, list 2: %d evts\n", runList[0].numEvts, runList[1].numEvts);

  if (appendFlag == 0) {
    ftr = fopen("tr.csv", "w");
    if (ftr == 0) { fprintf(stderr, "could not open file tr.csv\n"); exit(1);}
    fprintf(ftr, "evt, seg, ch, val\n");
    for (i = 0; i < 3; i++) {
      if (sumOnlyFlag == 1 && i != 2) { continue; }
      for (j = 0; j < 5; j++) {  // 'n', 'l', 'r', 'u', 'd'
          //seg = (j == 0) ? runList[i].seg : neigh[runList[i].seg][j - 1];
          //evt = runList[i].rawEvts + 7;  // take first evt
          seg = (j == 0) ? pList[i].seg : neigh[pList[i].seg][j - 1];
          evt = pList[i].rawEvts;  // take 1st evt
          for (k = 0; k < 300; k++) {
            fprintf(ftr, "%c,%c,%d,%d\n", evtlabel[i], seglabel[j], k + 1, evt->wf[seg][k]);
        }
      }
    }
    fclose(ftr);
  }
  return 0;
}
コード例 #10
0
ファイル: IMesh.cpp プロジェクト: openhumanoids/exotica
  void IMesh::computeIMesh(int t)
  {
    int M = eff_size_;

    computeLaplace(t);

    if (updateJacobian_)
    {
      double A, _A, Sk, Sl, w, _w;
      int i, j, k, l, N;
      N = EFFJAC.cols();
      Eigen::Vector3d distance, _distance = Eigen::Vector3d::Zero(3, 1);
      for (i = 0; i < N; i++)
      {
        for (j = 0; j < M; j++)
        {
          if (j < M)
          JAC.block(3 * j, i, 3, 1) = EFFJAC.block(3 * j, i, 3, 1);
          for (l = 0; l < M; l++)
          {
            if (j != l)
            {
              if (dist(j, l) > 0 && wsum(j) > 0 && weights_(j, l) > 0)
              {
                A = dist(j, l) * wsum(j);
                w = weights_(j, l) / A;

                _A = 0;
                distance = EFFPHI.segment(j, 3) - EFFPHI.segment(l, 3);
                if (j < M)
                {
                  if (l < M)
                    //Both j and l are points on the robot
                    _distance = EFFJAC.block(3 * j, i, 3, 1)
                        - EFFJAC.block(3 * l, i, 3, 1);
                  else
                    //l is not on the robot
                    _distance = EFFJAC.block(3 * j, i, 3, 1);
                }
                else
                {
                  if (l < M)
                  //j is not on the robot
                    _distance = -EFFJAC.block(3 * l, i, 3, 1);
                }

                Sl = distance.dot(_distance) / dist(j, l);
                for (k = 0; k < M; k++)
                {
                  if (j != k && dist(j, k) > 0 && weights_(j, k) > 0)
                  {
                    distance = EFFPHI.segment(j, 3) - EFFPHI.segment(k, 3);
                    if (j < M)
                    {
                      if (k < M)
                        _distance = EFFJAC.block(3 * j, i, 3, 1)
                            - EFFJAC.block(3 * k, i, 3, 1);
                      else
                        _distance = EFFJAC.block(3 * j, i, 3, 1);
                    }
                    else
                    {
                      if (k < M) _distance = -EFFJAC.block(3 * k, i, 3, 1);
                    }
                    Sk = distance.dot(_distance) / dist(j, k);
                    _A += weights_(j, k) * (Sl * dist(j, k) - Sk * dist(j, l))
                        / (dist(j, k) * dist(j, k));
                  }
                }
                _w = -weights_(j, l) * _A / (A * A);
              }
              else
              {
                _w = 0;
                w = 0;
              }
              if (l < M)
                JAC.block(3 * j, i, 3, 1) -= EFFPHI.segment(l, 3) * _w
                    + EFFJAC.block(3 * l, i, 3, 1) * w;
              else
                JAC.block(3 * j, i, 3, 1) -= EFFPHI.segment(l, 3) * _w;
            }
          }
        }
      }
    }
  }
コード例 #11
0
ファイル: util.c プロジェクト: natsuhiko/badjust
void wshift(double* x, double* k, long n){
    double mm = wsum(x, k, n);
    long i;
    for(i=0; i<n; i++){ x[i] -= mm; }
}
コード例 #12
0
ファイル: cevt_list.c プロジェクト: ryotani/petcat-rt
int cevt(double ratio, char *datname1, int runn1, int segn1, char *datname2, int runn2, int segn2){

  FILE *fin, *fou, *ftr;
  struct gebData ghdr, defaulthdr = {100, sizeof(Mario), 0ll};
  Mario *evt;
  char evtlabel[] = {'a', 'b', 's'}, seglabel[] = {'n', 'l', 'r', 'u', 'd', 'c'};
  char suffix[50];
  sprintf(suffix, "_Run%iSeg%i_Run%iSeg%i", runn1, segn1, runn2, segn2);
  //int i, j, k, num, seg, baselineFlag = 1, CFDFlag = 0;
  /*  char *filename;
  int seg;
  Mario *rawEvts;
  int numEvts;
  runList[0]. = (struct evtList){datname1, segn1, 0, 0};
  runList[1] = (struct evtList){datname2, segn2, 0, 0};
  */

  for (i = 0; i < 2; i++) {
    runList[i].rawEvts = malloc(MAX_EVT * sizeof(Mario));
  }
  runList[0].filename = datname1;
  runList[0].seg = segn1;
  runList[0].numEvts = 0;
  runList[1].filename = datname2;
  runList[1].seg = segn2;
  runList[1].numEvts = 0;


  pList[0].seg = segn1;
  pList[1].seg = segn2;
  pList[2].seg = segn1;

  //printf("%i\n",runList[i].numEvts);

  for (i = 0; i < 2; i++) {
    fin = fopen(runList[i].filename, "r");
    if (fin == 0) { fprintf(stderr, "could not open file %s\n", runList[i].filename); exit(1);}
    while ((runList[i].numEvts < MAX_EVT) && (fread(&ghdr, sizeof(struct gebData), 1, fin) == 1)) {
      if (ghdr.type == 100) {
        num = fread(runList[i].rawEvts + runList[i].numEvts++, sizeof(Mario), 1, fin);
        assert(num == 1);
      }
      else {
        fseek(fin, ghdr.length, SEEK_CUR);
      }
    }
    fclose(fin);
  }

  if(CFDFlag == 1) {
    thoffset[0] = cc_avg_cft(runList + 0, baselineFlag); // Deduce the reference time

    pList[0].rawEvts = avg_thoffset(runList + 0, baselineFlag, thoffset[0], suffix);
    pList[1].rawEvts = avg_thoffset(runList + 1, baselineFlag, thoffset[0], suffix); //use theoffset[0] not [1]
  } else {
    pList[0].rawEvts = avg(runList + 0, baselineFlag);
    pList[1].rawEvts = avg(runList + 1, baselineFlag);
  }

  pList[2].rawEvts = wsum(pList[0].rawEvts, pList[1].rawEvts, ratio);
  
  char outname[100];
  CFDFlag == 1 ? sprintf(outname, "./cevtout/cevt%s_cfd.dat",suffix) : sprintf(outname, "./cevtout/cevt%s.dat",suffix);
  fou = (appendFlag == 1) ? fopen(outname, "a") : fopen(outname, "w");
  if (fou == 0) { fprintf(stderr, "could not open file %s\n",outname); exit(1);}

  for (i = 0; i < 3; i++) {
    if (sumOnlyFlag == 1 && i != 2) { continue; }
    assert( 1 == fwrite(&defaulthdr, sizeof(struct gebData), 1, fou));
    assert( 1 == fwrite(pList[i].rawEvts, sizeof(Mario), 1, fou));
  }
  fclose(fou);

  fprintf(frunlistout, "%s %d %d\n", outname, 1000*runn1 + runn2, segn1);
  printf("\n%s %d %d\n", outname, 1000*runn1 + runn2, segn1);
	
  printf("list 1: %d evts, list 2: %d evts\n", runList[0].numEvts, runList[1].numEvts);

  if (appendFlag == 0) {
    CFDFlag == 1 ? sprintf(outname, "./cevtout/tr/tr_cfd%s.csv",suffix) : sprintf(outname, "./cevtout/tr/tr%s.csv",suffix);
    ftr = fopen(outname, "w");
    if (ftr == 0) { fprintf(stderr, "could not open file %s\n", outname); exit(1);}
    fprintf(ftr, "evt, seg, ch, val\n");
    for (i = 0; i < 3; i++) {
      if (sumOnlyFlag == 1 && i != 2) { continue; }
      evt = pList[i].rawEvts;  // take 1st evt
      for (j = 0; j < 5; j++) {  // 'n', 'l', 'r', 'u', 'd'(, 'c')
	seg = (j == 0) ? pList[i].seg : neigh[pList[i].seg][j - 1];
	for (k = 0; k < 300; k++) {
	  fprintf(ftr, "%c,%c,%d,%d\n", evtlabel[i], seglabel[j], k + 1, evt->wf[seg][k]);
	}
      }
      j = 5;
      seg = 36; // Center Contact
      for (k = 0; k < 300; k++) {
	fprintf(ftr, "%c,%c,%d,%d\n", evtlabel[i], seglabel[j], k + 1, evt->wf[seg][k]);
      }
    }
    close(ftr);
  }
  return 0;
}