Advertisement
AlezM

Untitled

Feb 18th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <vector>
  4. #include <stdio.h>
  5.  
  6. void extended_euclid(long a, long b, long *x, long *y, long *d) {/* calculates a * *x + b * *y = gcd(a, b) = *d */
  7.     long q, r, x1, x2, y1, y2;
  8.  
  9.     if (b == 0) {
  10.         *d = a, *x = 1, *y = 0;
  11.         return;
  12.     }
  13.     x2 = 1, x1 = 0, y2 = 0, y1 = 1;
  14.  
  15.     while (b > 0) {
  16.         q = a / b, r = a - q * b;
  17.         *x = x2 - q * x1, *y = y2 - q * y1;
  18.         a = b, b = r;
  19.         x2 = x1, x1 = *x, y2 = y1, y1 = *y;
  20.     }
  21.     *d = a, *x = x2, *y = y2;
  22. }
  23.  
  24.  
  25. long inverse(long a, long n) { /* computes the inverse of a modulo n */
  26.     long d, x, y;
  27.     extended_euclid(a, n, &x, &y, &d);
  28.     if (d == 1) return x;
  29.     return 0;
  30. }
  31.  
  32.  
  33.  
  34. class ZpnVector {
  35. public:
  36.     int p, n;
  37.     std::vector <int> X;
  38.     ZpnVector() : n(1), p(1) {
  39.         X.clear();
  40.         X.push_back(0);
  41.     };
  42.  
  43.     ZpnVector(std::vector <int> _v, int _p) : X(_v), p(_p) {
  44.         n = _v.size();
  45.         for each (int x in X) {
  46.             x = x % p;
  47.         }
  48.     };
  49.  
  50.     ZpnVector(const ZpnVector& _v) {
  51.         p = _v.p;
  52.         n = _v.n;
  53.         X.clear();
  54.         for (int i = 0; i < _v.n; i++) {
  55.             X.push_back(_v.X[i]);
  56.         }
  57.     };
  58.  
  59.     ZpnVector& operator= (const ZpnVector& _v) {
  60.         X = _v.X; n = _v.n; p = _v.p;
  61.         return *this;
  62.     };
  63.  
  64.     ZpnVector& operator+ (const ZpnVector& _v) const {
  65.         std::vector<int> cache;
  66.         for (int i = 0; i < n; i++) {
  67.             cache[i] = X[i] + _v.X[i];
  68.         }
  69.         return ZpnVector(cache, p);
  70.     };
  71.  
  72.     ZpnVector& operator- (const ZpnVector& _v) const {
  73.         std::vector<int> cache;
  74.         for (int i = 0; i < n; i++) {
  75.             cache.push_back(X[i] - _v.X[i]);
  76.         }
  77.         return ZpnVector(cache, p);
  78.     };
  79.  
  80.     ZpnVector& operator* (const int& k) const {
  81.         std::vector<int> cache;
  82.         for (int i = 0; i < n; i++) {
  83.             cache.push_back(X[i] * k);
  84.         }
  85.         return ZpnVector(cache, p);
  86.     };
  87.  
  88.     ZpnVector& operator/ (const int& k) const {
  89.         std::vector<int> cache;
  90.         for (int i = 0; i < n; i++) {
  91.             cache.push_back( X[i] * inverse(k, p) );
  92.         }
  93.         return ZpnVector(cache, p);
  94.     };
  95.  
  96.     int dotProduct(const ZpnVector& _v) const { //Scalar
  97.         int dP = 0;
  98.         for (int i = 0; i < n; i++) {
  99.             dP += X[i] * _v.X[i];
  100.         }
  101.         return (dP % p);
  102.     };
  103.  
  104.     double magnitude() const {
  105.         int m = 0;
  106.         for (int i = 0; i < n; i++) {
  107.             m += X[i] * X[i];
  108.         }
  109.         return sqrt(m);
  110.     }
  111.     ~ZpnVector() {};
  112. };
  113.  
  114. void GaussianElimination(std::vector< ZpnVector > matrix) {
  115.     int rows = matrix.size();
  116.     int cols = matrix[0].X.size();
  117.  
  118.     if (rows > cols) { //если кол-во векторов больше чем их размерность
  119.         std::cout << "Linear Independent!\n";
  120.         return;
  121.     }
  122.  
  123.     int curRow = 0; //строка
  124.     int curCol = 0; //колонка
  125.    
  126.     while (curRow < rows) { // i - vector number
  127.         int nonZeroRow = curRow;
  128.         bool zeroCol = false;
  129.         while (matrix[nonZeroRow].X[curCol] == 0) { //проходимся по curCol элементам векторов начиная с curRow-го
  130.             nonZeroRow++;
  131.             if (curRow == rows) {
  132.                 zeroCol = true;
  133.                 break;
  134.             }
  135.         }
  136.  
  137.         if (!zeroCol) { //нашли вектор с ненулевым curCol элементом
  138.             if (nonZeroRow != curRow)
  139.                 std::swap(matrix[curRow], matrix[nonZeroRow]);
  140.         }
  141.         else {
  142.             curCol++;
  143.             continue;
  144.         }
  145.  
  146.         ZpnVector cacheV = matrix[curRow] / matrix[curRow].X[curCol];
  147.         matrix[curRow] = cacheV; //нормализуем вектор по curCol элемету <- PROBLEM!!!
  148.  
  149.         for (int j = curRow + 1; j < rows; j++) {
  150.             matrix[j] = matrix[j] - matrix[curRow] * matrix[j].X[curCol];
  151.         }
  152.         curCol++;
  153.         curRow++;
  154.     }
  155.  
  156.     int zeroVectors = 0;
  157.     for (int i = 0; i < rows; i++) {
  158.         if (matrix[i].magnitude() == 0)
  159.             zeroVectors++;
  160.     }
  161.  
  162.     if (zeroVectors > 0 && rows - zeroVectors < cols)
  163.         std::cout << "Linear Independent!\n";
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement