Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <omp.h>
- #include <iostream>
- constexpr auto SIZE = 5;
- double getDeterminant(double matrix[][SIZE], int size) {
- double result = 0;
- double submatrix[SIZE][SIZE];
- if (size == 2)
- return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
- else {
- for (int x = 0; x < size; x++) {
- int subi = 0;
- for (int i = 1; i < size; i++) {
- int subj = 0;
- for (int j = 0; j < size; j++) {
- if (j == x)
- continue;
- submatrix[subi][subj] = matrix[i][j];
- subj++;
- }
- subi++;
- }
- result = result + (pow(-1, x) * matrix[0][x] * getDeterminant(submatrix, size - 1));
- }
- }
- return result;
- }
- double** replaceColumn(double source[][SIZE], double column[SIZE], int pos) {
- double** result = new double*[SIZE];
- for (int i = 0; i < SIZE; i++) {
- result[i] = new double[SIZE];
- }
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- if (j == pos) {
- result[i][j] = column[i];
- } else {
- result[i][j] = source[i][j];
- }
- }
- }
- return result;
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- omp_set_dynamic(0);
- omp_set_num_threads(SIZE);
- double matrix[SIZE][SIZE] = {
- {4, 7, 8, 14, 0},
- {9, 66, -1, 29, -13},
- {5, -5, 0, 49, 13},
- {3, -3, 30, 60, 41},
- {1, 1, 1, 0, 47},
- };
- double coefficients[SIZE] = {
- 1, 2, 3, 4, 5
- };
- double unknown[SIZE];
- double det = getDeterminant(matrix, SIZE);
- std::cout.precision(17);
- std::cout << "Детерминант общий: " << det << std::endl;
- int i;
- #pragma omp parallel for shared(matrix, coefficients, unknown) private(i)
- for (i = 0; i < SIZE; i++) {
- double** array = replaceColumn(matrix, coefficients, i);
- double temp[SIZE][SIZE];
- for (int j = 0; j < SIZE; j++) {
- for (int k = 0; k < SIZE; k++) {
- temp[j][k] = array[j][k];
- }
- }
- unknown[i] = getDeterminant(temp, SIZE);
- }
- for (int i = 0; i < SIZE; i++) {
- std::cout << "Детерминант " << i + 1 << ": " << unknown[i] << ". X" << i + 1 << ": " << unknown[i] / det << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement