Ejemplo n.º 1
0
bool gaussian_elimination(vvd &matrix, vd &output) {
  for (int i = 0; i < matrix.size(); i++) {
    int pivot = find_pivot(matrix, i);
    if (fabs(matrix[pivot][i]) < EPS) {
      return false;
    }
    swap_row(matrix, pivot, i);
    double factor = matrix[i][i];
    for (int j = 0; j < matrix[i].size(); j++) {
      matrix[i][j] /= factor;
    }
    for (int j = 0; j < matrix.size(); j++) {
      if (j == i) {
        continue;
      }
      factor = matrix[j][i];
      for (int k = 0; k < matrix[j].size(); k++) {
        matrix[j][k] -= factor*matrix[i][k];
      }
    }
  }
  output.clear();
  for (int i = 0; i < matrix.size(); i++) {
    output.push_back(matrix[i][matrix[i].size() - 1]);
  }
  return true;
}
Ejemplo n.º 2
0
int find_pivot(const vvd &matrix, int index) {
  double max_value = fabs(matrix[index][index]);
  int max_index = index;
  for (int i = index + 1; i < matrix.size(); i++) {
    if (fabs(matrix[i][index]) > max_value) {
      max_value = fabs(matrix[i][index]);
      max_index = i;
    }
  }
  return max_index;
}
void printTask(const vvd& a, const vd& b, const vd& c, const std::string& filename = "task.txt")
{
    size_t n = c.size();
    size_t m = a.size();
    std::ofstream out(filename);
    for (size_t i = 0; i < n; ++i)
    {
        auto& t = c[i];
        if (std::abs(t) < EPS)
        {
            continue;
        }
        if (t > 0)
        {
            out << "+\t" << t;
        }
        if (t < 0)
        {
            out << "-\t" << -t;
        }
        out << " * x" << i + 1 << "\t";
    }
    out << "-----> MAX\n";
    for (size_t i = 0; i < m; ++i)
    {
        out << "x" << n + i + 1 << "\t=\t" << b[i] << "\t";
        for (size_t j = 0; j < n; ++j)
        {
            auto t = -a[i][j];
            if (std::abs(t) < EPS)
            {
                continue;
            }
            if (t > 0)
            {
                out << "+\t" << t;
            }
            if (t < 0)
            {
                out << "-\t" << -t;
            }
            out << " * x" << j + 1 << "\t";
        }
        out << endl;
    }
}
void getDualProblem(const vvd& a, vd& b, vd& c, vvd& dual_a, vd& dual_b, vd& dual_c)
{
    for (auto x : b)
    {
        dual_c.push_back(-x);
    }
    for (auto x : c)
    {
        dual_b.push_back(-x);
    }
    dual_a.assign(a[0].size(), vd());
    for (size_t i = 0; i < a.size(); ++i)
    {
        for (size_t j = 0; j < a[i].size(); ++j)
        {
            dual_a[j].push_back(-a[i][j]);
        }
    }
}
Ejemplo n.º 5
0
Archivo: B.cpp Proyecto: gzgreg/ACM
bool gaussian(vvd& A) { int m=A.size(), n=A[0].size()-1; vector<bool> used(m,0);
  for (int c = 0; c < n; ++c) for (int r = 0; r < m; ++r)
    if (!used[r] && !ISZERO(A[r][c]) ) { pivot(A, r, c); used[r] = true; }
  for (int r = 0; r < m; ++r) if(!used[r] && !ISZERO(A[r].back())) return false;
  return true; }
Ejemplo n.º 6
0
Archivo: B.cpp Proyecto: gzgreg/ACM
void pivot(vvd& A, int r, int c) { // A is A|b
  int m = A.size(), n = A[0].size(); ld tmp = A[r][c];
  for (int i = 0; i < n; ++i) A[r][i]/=tmp;
  for (int i = 0; i < m; ++i) if (i != r) { ld k = A[i][c];
    for(int j = 0; j < n; ++j) A[i][j] -= A[r][j]*k; } }