Advertisement
KuriGohanAndKamehaX2

31_32

Dec 19th, 2019
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int _n()
  4.  
  5. {
  6.  
  7. int n;
  8.  
  9. std::cout << "enter size and matrix: ";
  10.  
  11. std::cin >> n;
  12.  
  13. return n;
  14.  
  15. }
  16.  
  17. double** _mec2(int t)
  18.  
  19. {
  20.  
  21. double** a = new double* [t];
  22.  
  23. for (int i = 0; i < t; i++)
  24.  
  25. a[i] = new double[t];
  26.  
  27. return a;
  28.  
  29. }
  30.  
  31. double** _mec2(int t1, int t2)
  32.  
  33. {
  34.  
  35. double** a = new double* [t1];
  36.  
  37. for (int i = 0; i < t1; i++)
  38.  
  39. a[i] = new double[t2];
  40.  
  41. return a;
  42.  
  43. }
  44.  
  45. double* _mec1(int t)
  46.  
  47. {
  48.  
  49. return new double[t];
  50.  
  51. }
  52.  
  53. void _mer2(double** t1, int t2)
  54.  
  55. {
  56.  
  57. for (int i = 0; i < t2; i++)
  58.  
  59. delete[] t1[i];
  60.  
  61. delete[] t1;
  62.  
  63. }
  64.  
  65. void _mer1(double* t1)
  66.  
  67. {
  68.  
  69. delete[] t1;
  70.  
  71. }
  72.  
  73. void _cin2(double** t1, int t2)
  74.  
  75. {
  76.  
  77. for (int i = 0; i < t2; i++)
  78.  
  79. for (int j = 0; j < t2; j++)
  80.  
  81. std::cin >> t1[i][j];
  82.  
  83. }
  84.  
  85. void _cin2(double** t1, int t2, int t3)
  86.  
  87. {
  88.  
  89. for (int i = 0; i < t2; i++)
  90.  
  91. for (int j = 0; j < t3; j++)
  92.  
  93. std::cin >> t1[i][j];
  94.  
  95. }
  96.  
  97. void _cin1(double* t1, int t2)
  98.  
  99. {
  100.  
  101. for (int i = 0; i < t2; i++)
  102.  
  103. std::cin >> t1[i];
  104.  
  105. }
  106.  
  107. void _cout2(double** t1, int t2)
  108.  
  109. {
  110.  
  111. std::cout << '\n';
  112.  
  113. for (int i = 0; i < t2; i++)
  114.  
  115. {
  116.  
  117. for (int j = 0; j < t2; j++)
  118.  
  119. std::cout << t1[i][j] << ' ';
  120.  
  121. std::cout << '\n';
  122.  
  123. }
  124.  
  125. std::cout << '\n';
  126.  
  127. }
  128.  
  129. void _cout2(double** t1, int t2, int t3)
  130.  
  131. {
  132.  
  133. std::cout << '\n';
  134.  
  135. for (int i = 0; i < t2; i++)
  136.  
  137. {
  138.  
  139. for (int j = 0; j < t3; j++)
  140.  
  141. std::cout << t1[i][j] << ' ';
  142.  
  143. std::cout << '\n';
  144.  
  145. }
  146.  
  147. std::cout << '\n';
  148.  
  149. }
  150.  
  151. void _cout1(double* t1, int t2)
  152.  
  153. {
  154.  
  155. std::cout << '\n';
  156.  
  157. for (int i = 0; i < t2; i++)
  158.  
  159. std::cout << t1[i] << ' ';
  160.  
  161. std::cout << '\n';
  162.  
  163. }
  164.  
  165. double _30_a(double** a, int n)
  166.  
  167. {
  168.  
  169. double s = 0;
  170.  
  171. for (int i = 0; i < n; i++)
  172.  
  173. s += a[i][i];
  174.  
  175. return s;
  176.  
  177. }
  178.  
  179. //norma
  180.  
  181. double _30_b(double** a, int n)
  182.  
  183. {
  184.  
  185. double max;
  186.  
  187. for (int i = 0; i < n; i++)
  188.  
  189. {
  190.  
  191. double s = 0;
  192.  
  193. for (int j = 0; j < n; j++)
  194.  
  195. {
  196.  
  197. s += abs(a[i][j]);
  198.  
  199. }
  200.  
  201. if (!i || s > max)
  202.  
  203. max = s;
  204.  
  205. }
  206.  
  207. return max;
  208.  
  209. }
  210.  
  211. //transpose
  212.  
  213. void _30_v(double**& c, double** a, int n)
  214.  
  215. {
  216.  
  217. for (int i = 0; i < n; i++)
  218.  
  219. for (int j = 0; j < n; j++)
  220.  
  221. c[i][j] = a[i][j];
  222.  
  223. for (int i = 1; i < n; i++)
  224.  
  225. for (int j = 0; j < i; j++)
  226.  
  227. c[i][j] = c[j][i];
  228.  
  229. }
  230.  
  231. //mul scalar
  232.  
  233. void _30_g(double**& c, double** a, int m, int n, double l)
  234.  
  235. {
  236.  
  237. for (int i = 0; i < m; i++)
  238.  
  239. for (int j = 0; j < n; j++)
  240.  
  241. c[i][j] = l * a[i][j];
  242.  
  243. }
  244.  
  245. //sum
  246.  
  247. void _30_d(double**& c, double** a, double** b, int m, int n)
  248.  
  249. {
  250.  
  251. for (int i = 0; i < m; i++)
  252.  
  253. for (int j = 0; j < n; j++)
  254.  
  255. c[i][j] = a[i][j] + b[i][j];
  256.  
  257. }
  258.  
  259. //mul mat
  260.  
  261. void _30_e(double**& c, double** a, int m, int n, double** b, int t)
  262.  
  263. {
  264.  
  265. for (int i = 0; i < m; i++) //умножение матриц А и (В - Е)
  266.  
  267. for (int j = 0; j < t; j++)
  268.  
  269. {
  270.  
  271. c[i][j] = 0;
  272.  
  273. for (int m = 0; m < n; m++)
  274.  
  275. c[i][j] += a[i][m]*b[m][j];
  276.  
  277. }
  278.  
  279. }
  280.  
  281. double** _null_or_E(int t, bool is_identity)
  282.  
  283. {
  284.  
  285. double** c = _mec2(t);
  286.  
  287. for (int i = 0; i < t; i++)
  288.  
  289. for (int j = 0; j < t; j++)
  290.  
  291. if (is_identity && i == j)
  292.  
  293. c[i][j] = 1;
  294.  
  295. else
  296.  
  297. c[i][j] = 0;
  298.  
  299. return c;
  300.  
  301. }
  302.  
  303. //pow
  304.  
  305. void _30_j(double**& b, double** a, int n, int z)
  306.  
  307. {
  308.  
  309. b = _null_or_E(n, true);
  310.  
  311. double** c = _mec2(n);
  312.  
  313. double** t = _mec2(n);
  314.  
  315. _30_g(c, a, n, n, 1);
  316.  
  317. int m = z;
  318.  
  319. while (m > 0)
  320.  
  321. {
  322.  
  323. if (m % 2)
  324.  
  325. {
  326.  
  327. m--;
  328.  
  329. _30_g(t, b, n, n, 1);
  330.  
  331. _30_e(b, t, n, n, c, n);
  332.  
  333. }
  334.  
  335. else
  336.  
  337. {
  338.  
  339. m /= 2;
  340.  
  341. _30_g(t, c, n, n, 1);
  342.  
  343. _30_e(c, t, n, n, c, n);
  344.  
  345. }
  346.  
  347. }
  348.  
  349. _mer2(c, n);
  350.  
  351. _mer2(t, n);
  352.  
  353. }
  354.  
  355. void _31_a(double**& c, double** a, int n, int m)
  356.  
  357. {
  358.  
  359. c = _null_or_E(n, false);
  360.  
  361. double** t1 = _mec2(n);
  362.  
  363. double** t2 = _mec2(n);
  364.  
  365. for (int i = 0; i < m; i++)
  366.  
  367. {
  368.  
  369. _30_j(t1, a, n, i);
  370.  
  371. _30_g(t2, c, n, n, 1);
  372.  
  373. _30_d(c, t2, t1, n, n);
  374.  
  375. }
  376.  
  377. _mer2(t1, n);
  378.  
  379. _mer2(t2, n);
  380.  
  381. }
  382.  
  383. void _31_b(double**& c, double** a, double** b, int n, int m)
  384.  
  385. {
  386.  
  387. c = _null_or_E(n, false);
  388.  
  389. double** t1 = _mec2(n);
  390.  
  391. double** t2 = _mec2(n);
  392.  
  393. for (int i = 1; i <= 2 * m; i += 2)
  394.  
  395. {
  396.  
  397. _30_j(t1, a, n, i);
  398.  
  399. _30_g(t2, c, n, n, 1);
  400.  
  401. _30_d(c, t2, t1, n, n);
  402.  
  403. _30_j(t1, b, n, i + 1);
  404.  
  405. _30_g(t2, c, n, n, 1);
  406.  
  407. _30_d(c, t2, t1, n, n);
  408.  
  409. }
  410.  
  411. _mer2(t1, n);
  412.  
  413. _mer2(t2, n);
  414.  
  415. }
  416.  
  417. void _31_v(double**& c, double** a, double** b, int n, int m)
  418.  
  419. {
  420.  
  421. c = _null_or_E(n, false);
  422.  
  423. double** t1 = _mec2(n);
  424.  
  425. double** t2 = _mec2(n);
  426.  
  427. double** t3 = _mec2(n);
  428.  
  429. double** t4 = _mec2(n);
  430.  
  431. for (int i = 1; i <= m; i++)
  432.  
  433. {
  434.  
  435. _30_j(t1, a, n, i);
  436.  
  437. _30_j(t2, b, n, m - i + 1);
  438.  
  439. _30_e(t3, t1, n, n, t2, n);
  440.  
  441. _30_j(t4, c, n, 1);
  442.  
  443. _30_d(c, t4, t3, n, n);
  444.  
  445. }
  446.  
  447. _mer2(t1, n);
  448.  
  449. _mer2(t2, n);
  450.  
  451. _mer2(t3, n);
  452.  
  453. _mer2(t4, n);
  454.  
  455. }
  456.  
  457. void _32(double** a, double** b, double** c, double** d, int n, double**& x, double**& y)
  458.  
  459. {
  460.  
  461. double** t1 = _mec2(n);
  462.  
  463. double** t2 = _mec2(n);
  464.  
  465. double** t3 = _mec2(n);
  466.  
  467. double** t4 = _mec2(n);
  468.  
  469. _30_e(t3, a, n, n, d, n);
  470.  
  471. _30_e(t4, b, n, n, c, n);
  472.  
  473. _30_d(y, t3, t4, n, n);
  474.  
  475. _30_e(t1, a, n, n, c, n);
  476.  
  477. _30_e(t2, b, n, n, d, n);
  478.  
  479. _30_g(t2, t2, n, n, -1);
  480.  
  481. _30_d(x, t3, t4, n, n);
  482.  
  483. _mer2(t1, n);
  484.  
  485. _mer2(t2, n);
  486.  
  487. _mer2(t3, n);
  488.  
  489. _mer2(t4, n);
  490.  
  491. }
  492.  
  493. int main()
  494.  
  495. {
  496.  
  497. int m = _n();
  498.  
  499. int n = _n();
  500.  
  501. double** a = _mec2(m);
  502.  
  503. _cin2(a, m);
  504.  
  505. double** b = _mec2(m);
  506.  
  507. _cin2(b, m);
  508.  
  509. double** x = _mec2(m, m);
  510.  
  511. _30_e(x, a, m, m, b, m);
  512.  
  513. _cout2(x, m, m);
  514.  
  515. _mer2(x, m);
  516.  
  517. _mer2(a, m);
  518.  
  519. _mer2(b, m);
  520.  
  521. return 0;
  522.  
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement