Advertisement
makispaiktis

Gram - Schmidt

Mar 25th, 2020 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. // Auxiliary Functions
  11. // 1. Display the vector
  12. void displayVector(vector <double> v){
  13.     cout << "{";
  14.     for(unsigned int i=0; i<v.size(); i++){
  15.         if(i != v.size()-1){
  16.             cout << v[i] << ", ";
  17.         }
  18.         else{
  19.             cout << v[i] << "}";
  20.         }
  21.     }
  22.     cout << endl;
  23. }
  24.  
  25. // 2. Calculate the norm of a vector
  26. double norm(vector <double> v){
  27.     double sum = 0;
  28.     for(unsigned int i=0; i<v.size(); i++){
  29.         sum += v[i] * v[i];
  30.     }
  31.     return sqrt(sum);
  32. }
  33.  
  34. // 3. Add 2 vectors
  35. vector <double> add(vector <double> v1, vector <double> v2){
  36.     vector <double> result = vector <double> ();
  37.     if(v1.size() == v2.size()){
  38.         for(unsigned int i=0; i<v1.size(); i++){
  39.             result.push_back(v1[i] + v2[i]);
  40.         }
  41.         return result;
  42.     }
  43.     else{
  44.         cout << "Cannot add 2 vectors with different size/dimension." << endl;
  45.     }
  46. }
  47.  
  48. // 4. Subtract 2 vectors
  49. vector <double> subtract(vector <double> v1, vector <double> v2){
  50.     vector <double> result = vector <double> ();
  51.     if(v1.size() == v2.size()){
  52.         for(unsigned int i=0; i<v1.size(); i++){
  53.             result.push_back(v1[i] - v2[i]);
  54.         }
  55.         return result;
  56.     }
  57.     else{
  58.         cout << "Cannot add 2 vectors with different size/dimension." << endl;
  59.     }
  60. }
  61.  
  62. // 5. Multiply 2 vectors
  63. double multiply(vector <double> v1, vector <double> v2){
  64.     double sum = 0;
  65.     if(v1.size() == v2.size()){
  66.         for(unsigned int i=0; i<v1.size(); i++){
  67.             sum += v1[i] * v2[i];
  68.         }
  69.         return sum;
  70.     }
  71.     else{
  72.         cout << "Cannot multiply 2 vectors with different size/dimension." << endl;
  73.     }
  74. }
  75.  
  76. // 6. Multiply all the elements of a vector by a number
  77. vector <double> multiplyVectorByNumber(vector <double> v, double number){
  78.     for(unsigned int i=0; i<v.size(); i++){
  79.         v[i] *= number;
  80.     }
  81.     return v;
  82. }
  83.  
  84. // 7. Divide all the elements of a vector by a number
  85. vector <double> divideVectorByNumber(vector <double> v, double number){
  86.     for(unsigned int i=0; i<v.size(); i++){
  87.         v[i] /= number;
  88.     }
  89.     return v;
  90. }
  91.  
  92. // 8. Gram - Schmidt
  93. vector < vector <double> > gramSchmidt(vector < vector <double> > sVector){
  94.     // Create the basic variable
  95.     vector < vector <double> > bases = vector < vector <double> > ();
  96.     // Starting the session
  97.     vector <double> g1 = sVector[0];
  98.     vector <double> base1 = divideVectorByNumber(g1, norm(g1));
  99.     bases.push_back(base1);
  100.     // For-loop
  101.     for(int i=1; i<sVector.size(); i++){
  102.         vector <double> gi = sVector[i];
  103.         for(int j=0; j<i; j++){
  104.             gi = subtract( gi , multiplyVectorByNumber(bases[j], multiply(bases[j], gi)) );
  105.         }
  106.         bases.push_back(divideVectorByNumber(gi, norm(gi)));
  107.     }
  108.     return bases;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. int main()
  115. {
  116.     // 1. Display the problem, ask the user for data
  117.     cout << "******************" << endl;
  118.     cout << "* GRAM - SCHMIDT *" << endl;
  119.     cout << "******************" << endl << endl;
  120.     cout << "First, give me how many vectors you have: ";
  121.     int n;
  122.     cin >> n;
  123.     while(n < 2){
  124.         cout << "First, give me how many vectors you have: ";
  125.         cin >> n;
  126.     }
  127.     cout << endl;
  128.     cout << "Now, I want you give me these vectors' dimension (how many coordinates each vector has): ";
  129.     int m;
  130.     cin >> m;
  131.     while(m < 2){
  132.         cout << "Now, I want you give me these vectors' dimension (how many coordinates each vector has): ";
  133.         cin >> m;
  134.     }
  135.     cout << endl << endl;
  136.  
  137.     // 2. Create the "n" vectors
  138.     vector < vector <double> > sVector = vector < vector <double> > ();
  139.     for(int i=0; i<n; i++){
  140.         cout << "Vector " << (i+1) << ": " << endl;
  141.         vector <double> si = vector <double> ();
  142.         for(int j=0; j<m; j++){
  143.             cout << "Coordinate " << (j+1) << ": ";
  144.             double coordinate;
  145.             cin >> coordinate;
  146.             si.push_back(coordinate);
  147.         }
  148.         sVector.push_back(si);
  149.         cout << endl;
  150.     }
  151.  
  152.     // 3. Show the user the "n" vector he has created
  153.     for(unsigned int i=0; i<sVector.size(); i++){
  154.         cout << "s" << (i+1) << " = ";
  155.         displayVector(sVector[i]);
  156.     }
  157.     cout << endl;
  158.     /*
  159.     // Checking the functions
  160.     cout << "Norm of s1 = ||s1|| = " << norm(sVector[0]) << endl;
  161.     cout << "Norm of s2 = ||s2|| = " << norm(sVector[1]) << endl;
  162.     vector <double> sumS1S2 = add(sVector[0], sVector[1]);
  163.     cout << "s1 + s2 = ";
  164.     displayVector(sumS1S2);
  165.     vector <double> subtractS1S2 = subtract(sVector[0], sVector[1]);
  166.     cout << "s1 - s2 = ";
  167.     displayVector(subtractS1S2);
  168.     double productS1S2 = multiply(sVector[0], sVector[1]);
  169.     cout << "s1 * s2 = " << productS1S2;
  170.     vector <double> prod = multiplyVectorByNumber(sVector[0], 8);
  171.     cout << "\n8 * s1 = ";
  172.     displayVector(prod);
  173.     vector <double> normalizationOfS1 = divideVectorByNumber(sVector[0], norm(sVector[0]));
  174.     cout << "Normalizing s1 ----> (s1/ ||s1||) = ";
  175.     displayVector(normalizationOfS1);
  176.     */
  177.     cout << "~~~~ Gram - Schmidt having the vectors ";
  178.     for(int i=0; i<sVector.size(); i++){
  179.         if(i != sVector.size()-1){
  180.             cout << "s" << (i+1) << ", ";
  181.         }
  182.         else{
  183.             cout << "s" << (i+1) << " ~~~~" << endl;
  184.         }
  185.     }
  186.     vector < vector <double> > bases = gramSchmidt(sVector);
  187.     for(unsigned int i=0; i<sVector.size(); i++){
  188.         cout << "base" << (i+1) << " = ";
  189.         displayVector(bases[i]);
  190.     }
  191.     cout << endl << endl;
  192.     cout << "~~~~ Confirmation ~~~~" << endl;
  193.     for(unsigned int i=0; i<sVector.size(); i++){
  194.         cout << "s" << (i+1) << " = ";
  195.         for(unsigned int j=0; j<bases.size(); j++){
  196.             if(j != bases.size()-1){
  197.                 cout << multiply(sVector[i], bases[j]) << "*";
  198.                 displayVector(bases[j]);
  199.                 cout << " + ";
  200.             }
  201.             else{
  202.                 cout << multiply(sVector[i], bases[j]) << "*";
  203.                 displayVector(bases[j]);
  204.             }
  205.         }
  206.     }
  207.  
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement