Esempio n. 1
0
void CalculateEdgeLengths(vtkPolyData* mesh, arma::sp_mat& edge_lengths) {
    vtkSmartPointer<vtkIdList> cellIdList = vtkSmartPointer<vtkIdList>::New();
    vtkSmartPointer<vtkIdList> pointIdList = vtkSmartPointer<vtkIdList>::New();

    const int npts = mesh->GetNumberOfPoints();
    edge_lengths.set_size(npts, npts);

    for (vtkIdType i = 0; i < npts; i++) {
        cellIdList->Reset();
        mesh->GetPointCells(i, cellIdList);
        double *pt1 = mesh->GetPoint(i);


        for (vtkIdType j = 0; j < cellIdList->GetNumberOfIds(); j++) {
            pointIdList->Reset();
            mesh->GetCellPoints(cellIdList->GetId(i), pointIdList);
            for (vtkIdType k = 0; k < pointIdList->GetNumberOfIds(); k++) {
                vtkIdType pt2_id = pointIdList->GetId(k);
                if (pt2_id != i) { //not the same point
                    if (edge_lengths(i, pt2_id) != 0) { //is not calculated yet
                        double *pt2 = mesh->GetPoint(pt2_id);

                        const double dx = pt1[0] - pt2[0];
                        const double dy = pt1[1] - pt2[1];
                        const double dz = pt1[2] - pt2[2];
                        const double d = sqrt(dx * dx + dy * dy + dz * dz);

                        edge_lengths(i, pt2_id) = d;
                        edge_lengths(pt2_id, i) = d;
                    }
                }
            }
        }
    }
}
Esempio n. 2
0
void GeneralizedRosenbrockFunction::Gradient(const arma::mat& coordinates,
                                             const size_t i,
                                             arma::sp_mat& gradient) const
{
  gradient.set_size(n);

  const size_t p = visitationOrder[i];

  gradient[p] = 400 * (std::pow(coordinates[p], 3) - coordinates[p] *
      coordinates[p + 1]) + 2 * (coordinates[p] - 1);
  gradient[p + 1] = 200 * (coordinates[p + 1] - std::pow(coordinates[p], 2));
}
Esempio n. 3
0
void CalculateGeneralizedEdgeLengthsE2(const arma::vec& gamma, const arma::sp_mat& I, arma::sp_mat& gen_edge_lengths) {
    const int npts = gamma.n_elem;
    gen_edge_lengths.set_size(npts, npts);

    //the measure is symmetric, process only upper triangle
    for (int i = 0; i < npts; i++) {
        for (int j = i + 1; j < npts; j++) {
            if (I(i, j) > 0) { //only for existing edges
                const double l_ij = sqrt(gamma(i) * gamma(i) + gamma(j) * gamma(j) + 2 * gamma(i) * gamma(j) * I(i, j));
                gen_edge_lengths(i, j) = l_ij;
                gen_edge_lengths(j, i) = l_ij;
            }
        }
    }

}
Esempio n. 4
0
void CalculateInversiveDistanceE2(const arma::sp_mat& l, const arma::vec& gamma, arma::sp_mat& I) {
    const int npts = gamma.n_elem;
    I.set_size(npts, npts);

    //the measure is symmetric, process only upper triangle
    for (int i = 0; i < npts; i++) {
        for (int j = i + 1; j < npts; j++) {
            if (l(i, j) > 0) { //only for existing edges
                const double I_ij = (l(i, j) * l(i, j) - gamma(i) * gamma(i) - gamma(j) * gamma(j)) / (2 * gamma(i) * gamma(j));
                I(i, j) = I_ij;
                I(j, i) = I_ij;
            }
        }
    }

}
void LogisticRegressionFunction<MatType>::PartialGradient(
    const arma::mat& parameters,
    const size_t j,
    arma::sp_mat& gradient) const
{
  const arma::rowvec diffs = responses - (1 / (1 + arma::exp(-parameters(0, 0)
      - parameters.tail_cols(parameters.n_elem - 1) * predictors)));

  gradient.set_size(arma::size(parameters));

  if (j == 0)
  {
    gradient[j] = -arma::accu(diffs);
  }
  else
  {
    gradient[j] = arma::dot(-predictors.row(j - 1), diffs) + lambda *
      parameters(0, j);
  }
}