Advertisement
osipyonok

MMEEP 2

Feb 2nd, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. // MMEEP2.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <armadillo>
  7.  
  8. using namespace std;
  9. using namespace arma;
  10.  
  11. struct node
  12. {
  13.     int ind;//виробничих потреб промисловості;
  14.     int agr;//виробничих потреб сільського господарства;
  15.     int con;//споживання
  16.     int gross_output;
  17.     int Calc_Gross_Output() { return gross_output = ind + agr + con; }
  18. };
  19.  
  20. inline void GrossOutput(node & nd , string str) {
  21.     cout << "Gross output of " + str + ": " << nd.Calc_Gross_Output() << endl;
  22. }
  23.  
  24. inline mat BuildTechMat(const node & a, const node & b) {
  25.     mat A = zeros(2, 2);
  26.     A(0, 0) = 1.0 * a.ind / a.gross_output;
  27.     A(0, 1) = 1.0 * a.agr / a.gross_output;
  28.  
  29.     A(1, 0) = 1.0 * b.ind / b.gross_output;
  30.     A(1, 1) = 1.0 * b.agr / b.gross_output;
  31.  
  32.     return A;
  33. }
  34.  
  35. void Step_1() {
  36.     node industry, agriculture;
  37.  
  38.     industry.ind = 1150;
  39.     industry.agr = 920;
  40.     industry.con = 1575;
  41.  
  42.     agriculture.ind = 820;
  43.     agriculture.agr = 650;
  44.     agriculture.con = 1230;
  45.  
  46.     GrossOutput(industry, "industry");
  47.     GrossOutput(agriculture, "agriculture");
  48.     cout << endl;
  49.  
  50.     mat A = BuildTechMat(industry, agriculture);
  51.     cout << "A = \n" << A << endl;
  52.  
  53.     // Модель міжгалузевої залежності цін (модель врівноважених цін) p = pA + s
  54.     // (p1 p2) = (a11 * p1 + a21 * p2     a12 * p1 + a22 * p2) + (s1 s2)
  55.     //
  56.     // p1 = a11 * p1 + a21 * p2 + s1
  57.     // p2 = a12 * p1 + a22 * p2 + s2
  58.     //
  59.     // (1 - a11) * p1 - a21 * p2 = s1
  60.     // - a12 * p1 + (1 - a22) * p2 = s2
  61.     //
  62.     // Bp = s
  63.     //
  64.  
  65.     mat B = zeros(2, 2);
  66.     B(0, 0) = 1 - A(0, 0), B(0, 1) = -A(1, 0);
  67.     B(1, 0) = -A(0, 1), B(1, 1) = 1 - A(1, 1);
  68.  
  69.     vec s = zeros(2);
  70.     s(0) = 0.15;
  71.     s(1) = 0.4;
  72.  
  73.     vec ans = solve(B, s);
  74.  
  75.     cout << "Price of industry product: " << ans(0) << endl;
  76.     cout << "Price of agriculture product: " << ans(1) << endl;
  77.     cout << endl;
  78. }
  79.  
  80. vec Coefficients_of_characteristic_poly(mat33 A) {
  81.     vec res = zeros(4);
  82.     res(0) = -1; // л^3
  83.     res(1) = A(0, 0) + A(1, 1) + A(2, 2); // л^2
  84.     res(2) = A(2, 0) * A(0, 2) + A(2, 1) * A(1, 2) + A(1, 0) * A(0, 1) - A(0, 0) * A(1, 1) - A(0, 0) * A(2, 2) - A(1, 1) * A(2, 2); // л^1
  85.     res(3) = A(0, 0) * A(1, 1) * A(2, 2) + A(1, 0) * A(2, 1) * A(0, 2) + A(0, 1) * A(1, 2) * A(2, 0)
  86.             - A(2, 0) * A(0, 2) * A(1, 1) - A(2, 1) * A(1, 2) * A(0, 0) - A(1, 0) * A(0, 1) * A(2, 2); // л^0
  87.     return res *= -1;
  88. }
  89.  
  90.  
  91. void Step_2() {
  92.     mat A = zeros(3, 3);
  93.  
  94.     A(0, 0) = 0.65, A(0, 1) = 0.1, A(0, 2) = 0.25;
  95.     A(1, 0) = 0.35, A(1, 1) = 0.35, A(1, 2) = 0.2;
  96.     A(2, 0) = 0.1, A(2, 1) = 0.45, A(2, 2) = 0.1;
  97.  
  98.     vec coef = Coefficients_of_characteristic_poly(A);
  99.     cout << "Coefficients of the characteristic polynomial:\n" << coef << endl;
  100.  
  101.     cx_vec eigval;
  102.     cx_mat eigvec;
  103.     eig_gen(eigval, eigvec, A);
  104.     cout << "Eigenvalues:\n" << eigval << endl;
  105.  
  106.     double frob = eigval(0).real();
  107.     int frob_i = 0;
  108.     for (int i = 1; i < eigval.n_rows ; ++i) {
  109.         if (eigval(i).real() > frob) {
  110.             frob = eigval(i).real();
  111.             frob_i = i;
  112.         }
  113.     }
  114.     cout << "Frobenius number: " << frob << endl << endl;
  115.  
  116.     cout << "Right Frobenius vector:\n" << eigvec.col(frob_i) << endl;
  117.  
  118.     mat At = A.t();
  119.     eig_gen(eigval, eigvec, At);
  120.     frob = eigval(0).real();
  121.     frob_i = 0;
  122.     for (int i = 1; i < eigval.n_rows; ++i) {
  123.         if (eigval(i).real() > frob) {
  124.             frob = eigval(i).real();
  125.             frob_i = i;
  126.         }
  127.     }
  128.     cout << "Left Frobenius vector:\n" << eigvec.col(frob_i) << endl;
  129.    
  130.     mat B = inv(eye(A.n_rows, A.n_cols) - A);
  131.     cout << "B:\n" << B << endl;
  132.  
  133.  
  134.  
  135.     mat C = eye(A.n_rows, A.n_cols) - B;
  136.     mat P = eye(A.n_rows, A.n_cols);
  137.     for (int i = 1; ; ++i, P *= A, C += P) {
  138.         if (i > 100000){
  139.             cout << "The series is divergent (N <= 100000)\n";
  140.             break;
  141.         }
  142.         if (-C.min() < 0.01) {
  143.             cout << "The series is convergence for N = " << i << endl;
  144.             cout << "B - E - A - ... = " << endl << -C << endl;
  145.             break;
  146.         }
  147.     }
  148.  
  149.     vec y = zeros(3);
  150.     y(0) = 900, y(1) = 1500, y(2) = 1000;
  151.     vec x = solve(eye(A.n_rows, A.n_cols) - A, y);
  152.     cout << "(E - A)x = y" << endl << "x = " << endl;
  153.     cout << x << endl;
  154.     cout << (x.min() > 0 ? "Matrix A is productive" : "Matrix A is non-productive") << endl;
  155. }
  156.  
  157. int main(int argc, const char **argv) {
  158.     Step_1();
  159.     Step_2();
  160.  
  161.     system("pause");
  162.     return 0;  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement