Advertisement
Guest User

Untitled

a guest
May 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include "pch.h"
  2. #include <omp.h>
  3. #include <iostream>
  4.  
  5. constexpr auto SIZE = 5;
  6.  
  7.  
  8. double getDeterminant(double matrix[][SIZE], int size) {
  9.     double result = 0;
  10.     double submatrix[SIZE][SIZE];
  11.  
  12.     if (size == 2)
  13.         return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
  14.     else {
  15.         for (int x = 0; x < size; x++) {
  16.             int subi = 0;
  17.             for (int i = 1; i < size; i++) {
  18.                 int subj = 0;
  19.                 for (int j = 0; j < size; j++) {
  20.                     if (j == x)
  21.                         continue;
  22.                     submatrix[subi][subj] = matrix[i][j];
  23.                     subj++;
  24.                 }
  25.                 subi++;
  26.             }
  27.             result = result + (pow(-1, x) * matrix[0][x] * getDeterminant(submatrix, size - 1));
  28.         }
  29.     }
  30.     return result;
  31. }
  32.  
  33.  
  34. double** replaceColumn(double source[][SIZE], double column[SIZE], int pos) {
  35.     double** result = new double*[SIZE];
  36.     for (int i = 0; i < SIZE; i++) {
  37.         result[i] = new double[SIZE];
  38.     }
  39.  
  40.     for (int i = 0; i < SIZE; i++) {
  41.         for (int j = 0; j < SIZE; j++) {
  42.             if (j == pos) {
  43.                 result[i][j] = column[i];
  44.             } else {
  45.                 result[i][j] = source[i][j];
  46.             }
  47.         }
  48.     }
  49.  
  50.     return result;
  51. }
  52.  
  53.  
  54. int main() {
  55.     setlocale(LC_ALL, "Russian");
  56.  
  57.     omp_set_dynamic(0);      
  58.     omp_set_num_threads(SIZE);
  59.            
  60.     double matrix[SIZE][SIZE] = {
  61.         {4,  7,  8, 14,   0},
  62.         {9, 66, -1, 29, -13},
  63.         {5, -5,  0, 49,  13},
  64.         {3, -3, 30, 60,  41},
  65.         {1,  1,  1,  0,  47},
  66.     };
  67.  
  68.     double coefficients[SIZE] = {
  69.         1, 2, 3, 4, 5
  70.     };
  71.  
  72.     double unknown[SIZE];
  73.    
  74.     double det = getDeterminant(matrix, SIZE);
  75.     std::cout.precision(17);
  76.     std::cout << "Детерминант общий: " << det << std::endl;
  77.  
  78.     int i;
  79.  
  80.     #pragma omp parallel for shared(matrix, coefficients, unknown) private(i)
  81.     for (i = 0; i < SIZE; i++) {
  82.         double** array = replaceColumn(matrix, coefficients, i);
  83.         double temp[SIZE][SIZE];
  84.         for (int j = 0; j < SIZE; j++) {
  85.             for (int k = 0; k < SIZE; k++) {
  86.                 temp[j][k] = array[j][k];
  87.             }
  88.         }
  89.         unknown[i] = getDeterminant(temp, SIZE);
  90.     }
  91.  
  92.  
  93.     for (int i = 0; i < SIZE; i++) {
  94.         std::cout << "Детерминант " << i + 1 << ": " << unknown[i] << ". X" << i + 1 << ": " << unknown[i] / det << std::endl;
  95.     }
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement