Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // gauss.cpp
- // Doprowadza macierze do postaci rref + Horner gratis!
- // autor: Trivial, 2013-10-13
- #include <cassert>
- #include <cmath>
- #include <iostream>
- #include <cstdlib>
- #include <iomanip>
- #include <vector>
- #include <set>
- using namespace std;
- int gcd(int a, int b) {
- int r;
- while (b != 0) {
- r = a % b;
- a = b;
- b = r;
- }
- return a;
- }
- class Rational {
- int p, q; /* p/q, q > 0 */
- public:
- Rational(int p, int q = 1) {
- assert(q != 0);
- if (q < 0) {
- p = -p;
- q = -q;
- }
- int g = gcd(abs(p), q);
- this->p = p/g;
- this->q = q/g;
- }
- friend Rational operator+(const Rational &a, const Rational &b) {
- return Rational(a.p*b.q + b.p*a.q, a.q*b.q);
- }
- friend Rational operator-(const Rational &a, const Rational &b) {
- return Rational(a.p*b.q - b.p*a.q, a.q*b.q);
- }
- friend Rational operator*(const Rational &a, const Rational &b) {
- return Rational(a.p*b.p, a.q*b.q);
- }
- friend Rational operator/(const Rational &a, const Rational &b) {
- return Rational(a.p*b.q, a.q*b.p);
- }
- friend ostream & operator<<(ostream &out, const Rational &a) {
- out << a.p;
- if (a.q != 1) {
- out << "/" << a.q;
- }
- return out;
- }
- friend bool operator==(const Rational &a, const Rational &b) {
- return a.p == b.p && a.q == b.q;
- }
- friend bool operator!=(const Rational &a, const Rational &b) {
- return a.p != b.p || a.q != b.q;
- }
- friend bool operator<(const Rational &a, const Rational &b) {
- return a.p*b.q < b.p*a.q;
- }
- Rational operator-() const { return Rational(-p, q); }
- int denom() const { return q; }
- int numer() const { return p; }
- };
- void printMatrix(Rational **A, int m, int n) {
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- cout << A[i][j] << "\t";
- }
- cout << "\n";
- }
- cout << endl;
- }
- inline void swap(Rational &a, Rational &b) {
- Rational c = a;
- a = b;
- b = c;
- }
- void gauss(Rational **A, int m, int n) {
- cout << "Eliminacja Gaussa dla macierzy:\n";
- printMatrix(A,m,n);
- int min = (m < n) ? m : n;
- for (int k = 0; k < min; k++) {
- if (A[k][k] == 0) {
- for (int s = k+1; s < m; s++) {
- if (A[s][k] != 0) {
- cout << "w" << k+1 << " <=> w" << s+1 << "\n";
- for (int j = k; j < n; j++) {
- swap(A[k][j], A[s][j]);
- }
- break;
- }
- }
- if (A[k][k] == 0) {
- cout << "Macierz jest osobliwa." << endl;
- return;
- }
- printMatrix(A, m, n);
- }
- bool hasChanged = false;
- for (int i = k+1; i < m; i++) {
- if (A[i][k] != 0) {
- Rational mult = A[i][k] / A[k][k];
- cout << "w" << i+1 << " := w" << i+1
- << " - (" << mult << ")*w" << k+1 << "\n";
- for (int j = k; j < n; j++) {
- A[i][j] = A[i][j] - mult * A[k][j];
- }
- hasChanged = true;
- }
- }
- if (hasChanged) {
- cout << endl;
- printMatrix(A, m, n);
- }
- }
- cout << "Eliminacja zakonczona sukcesem!" << endl;
- //~
- //~ Rational det = 1;
- //~ for (int k = 0; k < n; k++) {
- //~ det = det * A[k][k];
- //~ }
- //~
- //~ return sign*det;
- }
- void rref_jordan(Rational **A, int m, int n) {
- int *fs = (int*) malloc(n*sizeof(int));
- int fi = 0;
- printMatrix(A, m, n);
- for (int k = 0; k < n; k++) {
- int r = k - fi;
- if (r >= m) {
- for (int c = k; c < n; c++) {
- cout << "Kolumna " << c+1 << " oznaczona jako wolna" << endl;
- }
- break;
- }
- if (A[r][k] == 0) {
- for (int s = r+1; s < m; s++) {
- if (A[s][k] != 0) {
- cout << "w" << r+1 << " <=> w" << s+1 << "\n";
- for (int j = k; j < n; j++) {
- swap(A[r][j], A[s][j]);
- }
- break;
- }
- }
- if (A[r][k] == 0) {
- fs[fi++] = k;
- cout << "Kolumna " << k+1 << " oznaczona jako wolna" << endl;
- continue;
- }
- printMatrix(A, m, n);
- }
- if (A[r][k] != 1) {
- cout << "w" << r+1 << " := " << "w" << r+1
- << "/(" << A[r][k] << ")\n" << endl;
- for (int j = k+1; j < n; j++) {
- A[r][j] = A[r][j] / A[r][k];
- }
- for (int c = 0; c < fi; c++) {
- if (fs[c] < k) {
- A[r][fs[c]] = A[r][fs[c]] / A[r][k];
- }
- }
- A[r][k] = 1;
- printMatrix(A, m, n);
- }
- bool hasChanged = false;
- for (int i = 0; i < m; i++) {
- if (i == r) { continue; }
- if (A[i][k] != 0) {
- Rational p = A[i][k];
- for (int j = k; j < n; j++) {
- A[i][j] = A[i][j] - p*A[r][j];
- }
- for (int c = 0; c < fi; c++) {
- int j = fs[c];
- if (j < k) {
- A[i][j] = A[i][j] - p*A[r][j];
- }
- }
- cout << "w" << i+1 << " := w" << i+1
- << " - (" << p << ")*w" << r+1 << "\n";
- hasChanged = true;
- }
- }
- if (hasChanged) {
- cout << endl;
- printMatrix(A, m, n);
- }
- }
- free(fs);
- }
- void rref(Rational **A, int m, int n) {
- int *fs = 1 + (int*) malloc((n+1)*sizeof(int));
- int fi = 0;
- fs[-1] = -1;
- printMatrix(A, m, n);
- for (int k = 0; k < n; k++) {
- int r = k - fi;
- if (r >= m) {
- for (int c = k; c < n; c++) {
- fs[fi++] = c;
- cout << "Kolumna " << c+1 << " oznaczona jako wolna" << endl;
- }
- break;
- }
- if (A[r][k] == 0) {
- for (int s = r+1; s < m; s++) {
- if (A[s][k] != 0) {
- cout << "w" << r+1 << " <=> w" << s+1 << "\n";
- for (int j = k; j < n; j++) {
- swap(A[r][j], A[s][j]);
- }
- break;
- }
- }
- if (A[r][k] == 0) {
- fs[fi++] = k;
- cout << "Kolumna " << k+1 << " oznaczona jako wolna" << endl;
- continue;
- }
- printMatrix(A, m, n);
- }
- bool hasChanged = false;
- for (int i = r+1; i < m; i++) {
- if (A[i][k] != 0) {
- Rational p = A[i][k] / A[r][k];
- for (int j = k; j < n; j++) {
- A[i][j] = A[i][j] - p*A[r][j];
- }
- for (int c = 0; c < fi; c++) {
- int j = fs[c];
- if (j < k) {
- A[i][j] = A[i][j] - p*A[r][j];
- }
- }
- cout << "w" << i+1 << " := w" << i+1
- << " - (" << p << ")*w" << r+1 << "\n";
- hasChanged = true;
- }
- }
- if (hasChanged) {
- cout << endl;
- printMatrix(A, m, n);
- }
- }
- int fsize = fi;
- for (int k = n-1; k >= 0; k--) {
- if (k == fs[fi-1]) {
- fi--;
- continue;
- }
- int r = k - fi;
- if (A[r][k] != 1) {
- cout << "w" << r+1 << " := " << "w" << r+1
- << "/(" << A[r][k] << ")\n" << endl;
- for (int j = k+1; j < n; j++) {
- A[r][j] = A[r][j] / A[r][k];
- }
- A[r][k] = 1;
- printMatrix(A, m, n);
- }
- bool hasChanged = false;
- for (int i = r-1; i >= 0; i--) {
- if (A[i][k] != 0) {
- Rational p = A[i][k];
- A[i][k] = A[i][k] - p*A[r][k];
- for (int a = fi; a < fsize; a++) {
- int j = fs[a];
- A[i][j] = A[i][j] - p*A[r][j];
- }
- cout << "w" << i+1 << " := w" << i+1
- << " - (" << p << ")*w" << r+1 << "\n";
- hasChanged = true;
- }
- }
- if (hasChanged) {
- cout << endl;
- printMatrix(A, m, n);
- }
- }
- free(fs-1);
- }
- void horner(const Rational *as, int n, Rational x0) {
- assert(n >= 0);
- Rational *bs = (Rational*) malloc((n+1)*sizeof(Rational));
- Rational r = 0;
- for (int i = n; i >= 0; i--) {
- cout << "\t" << as[i];
- }
- cout << endl;
- cout << x0;
- for (int i = n; i >= 0; i--) {
- cout << "\t" << r*x0;
- r = r*x0 + as[i];
- bs[i] = r;
- }
- cout << endl;
- for (int i = n; i >= 0; i--) {
- cout << "\t" << bs[i];
- }
- cout << endl;
- free(bs);
- }
- void divisors(vector<int> *xs, int n) {
- int i;
- for (i = 1; i < sqrt(n); i++) {
- if (n%i == 0) {
- xs->push_back(i);
- xs->push_back(n/i);
- }
- }
- if (i*i == n) {
- xs->push_back(i);
- }
- }
- void printPolynomial(Rational *as, int n) {
- cout << "\n\nWielomian: ";
- for (int i = n; i >= 2; i--) {
- if (as[i] != 0) {
- cout << " ";
- if (as[i] != 1) {
- cout << showpos << as[i];
- }
- cout << "x^" << noshowpos << i;
- }
- }
- if (n >= 1 && as[1] != 0) {
- cout << " " << showpos << as[1] << "x" << noshowpos;
- }
- if (as[0] != 0) {
- cout << " " << showpos << as[0] << noshowpos;
- }
- cout << "\n" << endl;
- }
- void tryRational(Rational *as, int n) {
- assert(n >= 0);
- int m = 1;
- for (int i = 0; i <= n; i++) {
- m *= as[i].denom();
- }
- for (int i = 0; i <= n; i++) {
- as[i] = as[i] * m;
- }
- int k = abs(as[0].numer());
- for (int i = 1; i <= n && k != 1; i++) {
- k = gcd(k, abs(as[i].numer()));
- }
- if (k != 1) {
- for (int i = 0; i <= n; i++) {
- as[i] = as[i].numer() / k;
- }
- }
- printPolynomial(as, n);
- vector<int> ns;
- vector<int> ds;
- divisors(&ns, abs(as[0].numer()));
- divisors(&ds, abs(as[n].numer()));
- set<Rational> cs;
- for (unsigned int j = 0; j < ds.size(); j++) {
- for (unsigned int i = 0; i < ns.size(); i++) {
- for (int s = 1; s > -2; s -= 2) {
- cs.insert(Rational(s*ns[i],ds[j]));
- }
- }
- }
- for (set<Rational>::iterator it = cs.begin(); it != cs.end(); it++) {
- Rational x0 = *it;
- cout << "dla x = " << x0 << ":" << "\n";
- horner(as, n, x0);
- cout << endl;
- }
- }
- int main() {
- Rational A[4][4] = {
- { 2, Rational(2,3), 3, 4, },
- { 1, 3, 1, Rational(3,2), },
- { 4, 2, 3, 5, },
- { -1, 5, 2, -3, },
- };
- Rational *As[4] = { A[0], A[1], A[2], A[3] };
- rref(As, 4, 4); // tu można użyć różnych metod
- // Rational as[6] = { -2, 2, 1, 5, 3, 1 };
- // tryRational(as, 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment