Advertisement
EgorYankovsky

Cm3

Nov 14th, 2022
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. MATRIX.H
  2.  
  3. #pragma once
  4. #include<cmath>
  5. #include<vector>
  6. #include<string>
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. /*
  11. *       Problem list:
  12. *   1. Необходимо избавиться от передачи аргумента при счёте Вектора и Матрицы
  13. *  2. Все векторы перевести под класс векторов !!!
  14. */
  15.  
  16. namespace solution
  17. {
  18.     vector<double> operator + (vector<double> a, vector<double> b);         // Сумма векторов (++)
  19.     vector<double> operator - (vector<double> a, vector<double> b);         // Разность векторов (++)
  20.     double ScalarMult(vector<double> a, vector<double> b);                      // Скалярное умножение векторов (++)
  21.     double Norma(vector<double> a);                                                     // Норма вектора (++)
  22.     void InputValues(unsigned int &N, unsigned int &maxIter, double &eps);  // Ввод вспомогательных значений (++)
  23.  
  24.     class Matrix
  25.     {
  26.     public:
  27.         friend vector<double> operator * (Matrix A, vector<double> b);          // Умножение матрицы на вектор (+)
  28.         void InputMatrixValues(unsigned int N);                                     // Функция для ввода значений (++)
  29.         Matrix();                                                                               // Конструктор по умолчанию (++)
  30.         Matrix(                                                                                 // Конструктор по факту (++)
  31.             vector<uint8_t> ig,
  32.             vector<uint8_t> jg,
  33.             vector<double> ggl,
  34.             vector<double> ggu,
  35.             vector<double> di,
  36.             unsigned int N
  37.         );
  38.         ~Matrix();                                                                              // Деструктор (++)
  39.  
  40.     private:
  41.         vector<uint8_t> ig, jg;                                                             // Массивы для определения строк и столбцов матрицы
  42.         vector<double> ggl, ggu, di;                                                        // Массивы для хранения элементов матрицы
  43.         unsigned int N;                                                                     // Размер матрицы
  44.     };
  45.  
  46.     class Vector
  47.     {
  48.     public:
  49.         void InputVectorValues(unsigned int N);                                     // Дружественная функция для ввода значений (++)
  50.         Vector();                                                                               // Конструктор по умолчанию (++)
  51.         Vector(                                                                                 // Коструктор по факту (++)
  52.             vector<double> b,
  53.             unsigned int N
  54.         );
  55.         ~Vector();                                                                              // Деструктор (++)
  56.         double NormaVec();                                                                  // Норма вектора при СЛАУ (++)
  57.     private:
  58.         unsigned int N;                                                                     // Размер вектора
  59.         vector<double> b;                                                                       // Элементы вектора правой части
  60.         //double ScalarMultipl(Vector b);
  61.     };
  62.     vector<double> CGM(Matrix A, Vector b,                                              // Метод сопряженных градиентов (+)
  63.         unsigned int maxIter, double eps);
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. ADDITIONAL.CPP
  72.  
  73. #include"Matrix.h"
  74. #include<fstream>
  75.  
  76. namespace solution
  77. {
  78.  
  79.     /******************************************
  80.     *                   ВВОД ЗНАЧЕНИЙ
  81.     ******************************************/
  82.     void InputValues(unsigned int &N, unsigned int &maxIter, double &eps)
  83.     {
  84.         try
  85.         {
  86.             ifstream fin("kuslau.txt");
  87.             fin >> N >> maxIter >> eps;
  88.             fin.close();
  89.         }
  90.         catch (ios::failure& e)
  91.         {
  92.             cerr << e.what();
  93.         }
  94.     }
  95.  
  96.     void Vector::InputVectorValues(unsigned int N)
  97.     {
  98.         try
  99.         {
  100.             ifstream fin("pr.txt");
  101.             b.resize(N);
  102.             for (size_t i(0); i < N; i++)
  103.                 fin >> b[i];
  104.             fin.close();
  105.             this->N = N;
  106.         }
  107.         catch (ios::failure& e)
  108.         {
  109.             cerr << e.what();
  110.         }
  111.     }
  112.  
  113.     void Matrix::InputMatrixValues(unsigned int N)
  114.     {
  115.         try
  116.         {
  117.             // Считываем диагональные элементы
  118.             ifstream fin("di.txt");
  119.             di.resize(N);
  120.             for (size_t i(0); i < N; i++)
  121.                 fin >> di[i];
  122.             fin.close();
  123.  
  124.             /* ACHTUNG КОСТЫЛЬ !!! */
  125.             // Считываем указатели начала строк
  126.             double elem;
  127.             fin.open("ig.txt");
  128.             fin >> elem;
  129.             ig.push_back(elem);
  130.             while (fin.get() != EOF)
  131.             {
  132.                 fin >> elem;
  133.                 ig.push_back(elem);
  134.             }
  135.             fin.close();
  136.  
  137.             /* ACHTUNG КОСТЫЛЬ !!! */
  138.             // Считываем указатель на столбцы
  139.             fin.open("jg.txt");
  140.             fin >> elem;
  141.             jg.push_back(elem);
  142.             while (fin.get() != EOF)
  143.             {
  144.                 fin >> elem;
  145.                 jg.push_back(elem);
  146.             }
  147.             fin.close();
  148.  
  149.             /* ACHTUNG КОСТЫЛЬ !!! */
  150.             // Считываем нижний треугольник
  151.             fin.open("ggl.txt");
  152.             fin >> elem;
  153.             ggl.push_back(elem);
  154.             while (fin.get() != EOF)
  155.             {
  156.                 fin >> elem;
  157.                 ggl.push_back(elem);
  158.             }
  159.             fin.close();
  160.             this->N = N;
  161.         }
  162.         catch (ios::failure e)
  163.         {
  164.             std::cerr << e.what();
  165.         }
  166.     }
  167.  
  168.     /******************************************
  169.     *                   МЕТОДЫ ВЕКТОРА
  170.     ******************************************/
  171.  
  172.     Vector::Vector()
  173.     {
  174.         cout << "Вектор создан по умолчанию" << endl;
  175.     }
  176.  
  177.     Vector::Vector(vector<double> b, unsigned int N)
  178.     {
  179.         this->b = b;
  180.         this->N = N;
  181.     }
  182.  
  183.     Vector::~Vector()
  184.     {
  185.         cout << "Вектор удален" << endl;
  186.     }
  187.  
  188.     double Vector::NormaVec()
  189.     {
  190.         return sqrt(ScalarMult(b, b));
  191.     }
  192.  
  193.     /******************************************
  194.     *                   МЕТОДЫ МАТРИЦЫ
  195.     ******************************************/
  196.  
  197.     Matrix::Matrix()
  198.     {
  199.         cout << "Матрица создана по умолчанию" << endl;
  200.     }
  201.  
  202.     Matrix::Matrix(vector<uint8_t> ig, vector<uint8_t> jg, vector<double> ggl, vector<double> ggu, vector<double> di, unsigned int N)
  203.     {
  204.         this->ig = ig;
  205.         this->jg = jg;
  206.         this->ggl = ggl;
  207.         this->ggu = ggu;
  208.         this->di = di;
  209.         this->N = N;
  210.     }
  211.  
  212.     Matrix::~Matrix()
  213.     {
  214.         cout << "Матрица удалена" << endl;
  215.     }
  216.  
  217.     /******************************************
  218.     *                   ПРОЧИЕ ФУНКЦИИ
  219.     ******************************************/
  220.  
  221.     double ScalarMult(vector<double> a, vector<double> b)
  222.     {
  223.         if (a.size() != b.size())
  224.         {
  225.             cout << "Невозможно выполнить скалярное произведение";
  226.             exit(EXIT_FAILURE);
  227.         }
  228.  
  229.         double result(0);
  230.         for (size_t i(0); i < a.size(); i++)
  231.             result += a[i] * b[i];
  232.  
  233.         return result;
  234.     }
  235.  
  236.     vector<double> operator + (vector<double> a, vector<double> b)
  237.     {
  238.         //vector<double> result;
  239.         if (a.size() != b.size())
  240.         {
  241.             cout << "Невозможно найти сумму векторов";
  242.             exit(EXIT_FAILURE);
  243.         }
  244.         for (size_t i(0); i < a.size(); i++)
  245.             a[i] += b[i];
  246.         return a;
  247.     }
  248.  
  249.     vector<double> operator - (vector<double> a, vector<double> b)
  250.     {
  251.         //vector<double> result;
  252.         if (a.size() != b.size())
  253.         {
  254.             cout << "Невозможно найти разность векторов";
  255.             exit(EXIT_FAILURE);
  256.         }
  257.         for (size_t i(0); i < a.size(); i++)
  258.             a[i] -= b[i];
  259.  
  260.         return a;
  261.     }
  262.  
  263.     vector<double> operator * (Matrix A, vector<double> b)
  264.     {
  265.         vector<double> result;
  266.         result.resize(A.N);
  267.         //for (size_t i(0); i < A.N; i++)
  268.         //  result[i] = A.di[i] * b[i];
  269.  
  270.         for (size_t i(0); i < A.N; i++)
  271.         {
  272.             result[i] = A.di[i] * b[i];
  273.             for (size_t j(A.ig[i]); j < A.ig[i + 1] - 1; j++)
  274.             {
  275.                 result[i] += A.ggl[j] * b[A.jg[j]];
  276.                 result[A.jg[j]] += A.ggu[j] * b[i];
  277.             }
  278.         }
  279.         return result;
  280.     }
  281.  
  282.     vector<double> CGM(Matrix A, Vector b, unsigned int maxIter, double eps)
  283.     {
  284.         return vector<double>();
  285.     }
  286.  
  287.     double Norma(vector<double> a)
  288.     {
  289.         return sqrt(ScalarMult(a, a));
  290.     }
  291. }
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. source.cpp
  299.  
  300. #include"Matrix.h"
  301.  
  302. using namespace solution;
  303. using namespace std;
  304.  
  305. void ShowMenu()
  306. {
  307.    cout << endl << "0. TEST" << endl
  308.       << "1. Считать данные из файлов" << endl
  309.       << "2. Запустить метод сопряженных градиентов" << endl
  310.       << "3. Запустить локально-оптимальную схему" << endl
  311.       << "Выход: любой другой символ" << endl
  312.       << ">> ";
  313. }
  314.  
  315. int main()
  316. {
  317.    setlocale(LC_ALL, "Russian");
  318.    size_t command;
  319.    unsigned int maxIter, N;
  320.    double eps;
  321.    Matrix A;
  322.    Matrix A1(vector<uint8_t> {0, 0, 1, 1, 3, 4, 6},
  323.       vector<uint8_t> {0, 0, 1, 3, 1, 2},
  324.       vector<double> {4., 10., 11., 14., 16., 17.},
  325.       vector<double> {2., 3., 6., 13., 7., 9.},
  326.       vector<double> {1., 5., 8., 12., 15., 19.},
  327.       6
  328.    );
  329.    Vector b;
  330.    ShowMenu();
  331.  
  332.    while (cin >> command)
  333.    {
  334.       switch (command)
  335.       {
  336.       case 0:
  337.       {
  338.          vector<double> res, b1{ 1.,1., 1., 1., 1., 1. };
  339.          res = A1 * b1;
  340.          for (int i = 0; i < 6; i++)
  341.             cout << res[i] << " ";
  342.          cout << endl;
  343.          break;
  344.       }
  345.  
  346.       case 1:
  347.       {
  348.          InputValues(N, maxIter, eps);
  349.          A.InputMatrixValues(N);
  350.          b.InputVectorValues(N);
  351.          cout << N << " " << maxIter << " " << eps << endl;
  352.          break;
  353.       }
  354.       case 2:
  355.       {
  356.          break;
  357.       }
  358.       case 3:
  359.       {
  360.          break;
  361.       }
  362.       default:
  363.       {
  364.          cout << "Неизвестная команда" << endl;
  365.          break;
  366.       }
  367.       }
  368.       cout << ">> ";
  369.    }
  370.    return 0;
  371. }
  372.  
  373.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement