Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using std::vector;
  7.  
  8. using std::count;
  9. using std::cin;
  10. using std::endl;
  11.  
  12. class System
  13. {
  14. private:
  15.  
  16. short size = 0;
  17. vector<double> numbers;
  18.  
  19.  
  20. public:
  21.  
  22. System(short size)
  23. {
  24. this->size = size;
  25. }
  26.  
  27. bool lineAdd(vector<double> line)
  28. {
  29. if (this->size < 1) { /*std::cout << 'C';*/ return false; }
  30. if(line.size() != this->size) { /*std::cout << 'S';*/ return false; }
  31. if (this->numbers.size() % this->size != 0) { /*std::cout << 'V';*/ return false; }
  32.  
  33. for (short i = 0; i < this->size; i++)
  34. {
  35. this->numbers.push_back(line[i]);
  36. }
  37.  
  38. return true;
  39. }
  40.  
  41. void output()
  42. {
  43. short size = this->size;
  44.  
  45. short ss = this->numbers.size();
  46.  
  47. std::cout << std::endl;
  48. for (short i = 0; i < ss; i++)
  49. {
  50. std::cout << "\t" << this->numbers[i];
  51. if ((i + 1) % size == 0) std::cout << std::endl;
  52. }
  53.  
  54. }
  55.  
  56. vector<double> getLine(short n)
  57. {
  58. vector<double> res_line;
  59. short start = (n - 1) * this->size;
  60. short finish = (n - 1) * this->size + (this->size - 1);
  61. if (start < finish && this->numbers.size() > finish)
  62. {
  63. for (short i = start; i <= finish; i++)
  64. {
  65. res_line.push_back(this->numbers[i]);
  66. }
  67. }
  68. return res_line;
  69. }
  70.  
  71. // k1 * (n1) + k2 * (n2)
  72. vector<double> calculateLines(double k1, short n1, double k2, short n2)
  73. {
  74. vector<double> line1 = this->getLine(n1);
  75. vector<double> line2 = this->getLine(n2);
  76. vector<double> res_line;
  77. if (line1.size() == line2.size())
  78. {
  79. for (short i = 0; i < line1.size(); i++)
  80. {
  81. res_line.push_back( k1 * line1[i] + k2 * line2[i] );
  82. }
  83. }
  84. return res_line;
  85. }
  86.  
  87. bool replaceLine(short n, vector<double> line)
  88. {
  89. double lsize = line.size();
  90. if (lsize != this->size) return false;
  91.  
  92. short c_start = (n - 1) * lsize;
  93. short c_finish = n * lsize - 1;
  94.  
  95. for (short i = 0; i < lsize; i++)
  96. {
  97. this->numbers[c_start + i] = line[i];
  98. }
  99.  
  100. return true;
  101. }
  102.  
  103. void swapLines(short n1, short n2)
  104. {
  105. if (n1 <= this->size && n2 <= this->size)
  106. {
  107. vector<double> line1 = this->getLine(n1);
  108. vector<double> line2 = this->getLine(n2);
  109.  
  110. this->replaceLine(n1, line2);
  111. this->replaceLine(n2, line1);
  112. }
  113. }
  114.  
  115. bool isNullLine(short n)
  116. {
  117. bool success = true;
  118. for (short i = 1; i <= size; i++)
  119. {
  120. success = success && (abs(this->getElement(n, i)) < 0.1);
  121. }
  122. return success;
  123. }
  124.  
  125. double getElement(short line, short column)
  126. {
  127. if (line >= 1 && line <= this->size)
  128. {
  129. if (column >= 1 && column <= this->size)
  130. {
  131. return this->numbers[ (line - 1) * this->size + (column - 1) ];
  132. }
  133. }
  134. }
  135.  
  136. bool isUpperTriangleForm()
  137. {
  138. bool result = true;
  139.  
  140. for (short i = 1; i < this->size; i++) // Столбец
  141. {
  142. for(short j = i + 1; j <= this->size; j++) // Строка
  143. {
  144. result = result && ( this->isNullElement(j, i) );
  145. }
  146. }
  147. return result;
  148. }
  149.  
  150. void setUpperTriangleForm() // (i, j) заменяем
  151. {
  152. if (this->isUpperTriangleForm()) return;
  153. for (short i = 1; i < this->size; i++) // Столбец
  154. {
  155. for (short j = i + 1; j <= this->size; j++) // Строка
  156. {
  157. if (this->isNullElement(j, i)) continue;
  158. if (this->isNullElement(i, i)) continue;
  159.  
  160. else
  161. {
  162. double k = -(this->getElement(j, i) / this->getElement(i, i));
  163. this->replaceLine(j, this->calculateLines(k, i, 1, j));
  164. }
  165. }
  166. }
  167. for (short i = 1; i < this->size; i++)
  168. {
  169. short line = this->getMaxNullLine(this->size - (i - 1));
  170. if (line != 0)
  171. {
  172. this->swapLines(line, this->size - i);
  173. }
  174. }
  175. if (!(this->isUpperTriangleForm()))
  176. {
  177. this->setUpperTriangleForm();
  178. return;
  179. }
  180. }
  181.  
  182. bool isNullElement(short line, short column)
  183. {
  184. return abs( this->getElement(line, column) ) < 0.1;
  185. }
  186.  
  187. short getMaxNullLine(short inv)
  188. {
  189. short result = 0;
  190. short count = 0;
  191. for (short i = 1; i < inv; i++) // строка
  192. {
  193. short temp = 0;
  194. for (short j = 1; j <= this->size; j++) // столбец
  195. {
  196. if (this->isNullElement(i, j)) temp++;
  197. }
  198. if (temp > count) {
  199. result = i; count = temp;
  200. }
  201. }
  202. return result;
  203. }
  204.  
  205. double determinant()
  206. {
  207. if (this->isUpperTriangleForm())
  208. {
  209. double result = 1;
  210. for (short i = 1; i <= this->size; i++)
  211. {
  212. result = result * this->getElement(i, i);
  213. }
  214. return result;
  215. }
  216. return -1;
  217. }
  218.  
  219. vector<double> roots()
  220. {
  221. double d = this->determinant();
  222. vector<float> roots; // x4 x3 x2 x1
  223. if (d == 0)
  224. {
  225.  
  226. }
  227. else
  228. {
  229. vector<double> result;
  230. for (short i = this->size - 1; i >= 1; i--)
  231. {
  232. double sum = 0.0;
  233. if (i != this->size - 1)
  234. {
  235. for (short k = i + 1; k <= (size - 1); k++)
  236. {
  237. sum += (result[ k - (i + 1) ] * this->getElement(i, k));
  238. }
  239. }
  240. result.push_back( (this->getElement(i, this->size) - sum) / this->getElement(i, i) );
  241. }
  242. return result;
  243. }
  244. }
  245. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement