Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- // Auxiliary Functions
- // 1. Display the vector
- void displayVector(vector <double> v){
- cout << "{";
- for(unsigned int i=0; i<v.size(); i++){
- if(i != v.size()-1){
- cout << v[i] << ", ";
- }
- else{
- cout << v[i] << "}";
- }
- }
- cout << endl;
- }
- // 2. Calculate the norm of a vector
- double norm(vector <double> v){
- double sum = 0;
- for(unsigned int i=0; i<v.size(); i++){
- sum += v[i] * v[i];
- }
- return sqrt(sum);
- }
- // 3. Add 2 vectors
- vector <double> add(vector <double> v1, vector <double> v2){
- vector <double> result = vector <double> ();
- if(v1.size() == v2.size()){
- for(unsigned int i=0; i<v1.size(); i++){
- result.push_back(v1[i] + v2[i]);
- }
- return result;
- }
- else{
- cout << "Cannot add 2 vectors with different size/dimension." << endl;
- }
- }
- // 4. Subtract 2 vectors
- vector <double> subtract(vector <double> v1, vector <double> v2){
- vector <double> result = vector <double> ();
- if(v1.size() == v2.size()){
- for(unsigned int i=0; i<v1.size(); i++){
- result.push_back(v1[i] - v2[i]);
- }
- return result;
- }
- else{
- cout << "Cannot add 2 vectors with different size/dimension." << endl;
- }
- }
- // 5. Multiply 2 vectors
- double multiply(vector <double> v1, vector <double> v2){
- double sum = 0;
- if(v1.size() == v2.size()){
- for(unsigned int i=0; i<v1.size(); i++){
- sum += v1[i] * v2[i];
- }
- return sum;
- }
- else{
- cout << "Cannot multiply 2 vectors with different size/dimension." << endl;
- }
- }
- // 6. Multiply all the elements of a vector by a number
- vector <double> multiplyVectorByNumber(vector <double> v, double number){
- for(unsigned int i=0; i<v.size(); i++){
- v[i] *= number;
- }
- return v;
- }
- // 7. Divide all the elements of a vector by a number
- vector <double> divideVectorByNumber(vector <double> v, double number){
- for(unsigned int i=0; i<v.size(); i++){
- v[i] /= number;
- }
- return v;
- }
- // 8. Gram - Schmidt
- vector < vector <double> > gramSchmidt(vector < vector <double> > sVector){
- // Create the basic variable
- vector < vector <double> > bases = vector < vector <double> > ();
- // Starting the session
- vector <double> g1 = sVector[0];
- vector <double> base1 = divideVectorByNumber(g1, norm(g1));
- bases.push_back(base1);
- // For-loop
- for(int i=1; i<sVector.size(); i++){
- vector <double> gi = sVector[i];
- for(int j=0; j<i; j++){
- gi = subtract( gi , multiplyVectorByNumber(bases[j], multiply(bases[j], gi)) );
- }
- bases.push_back(divideVectorByNumber(gi, norm(gi)));
- }
- return bases;
- }
- int main()
- {
- // 1. Display the problem, ask the user for data
- cout << "******************" << endl;
- cout << "* GRAM - SCHMIDT *" << endl;
- cout << "******************" << endl << endl;
- cout << "First, give me how many vectors you have: ";
- int n;
- cin >> n;
- while(n < 2){
- cout << "First, give me how many vectors you have: ";
- cin >> n;
- }
- cout << endl;
- cout << "Now, I want you give me these vectors' dimension (how many coordinates each vector has): ";
- int m;
- cin >> m;
- while(m < 2){
- cout << "Now, I want you give me these vectors' dimension (how many coordinates each vector has): ";
- cin >> m;
- }
- cout << endl << endl;
- // 2. Create the "n" vectors
- vector < vector <double> > sVector = vector < vector <double> > ();
- for(int i=0; i<n; i++){
- cout << "Vector " << (i+1) << ": " << endl;
- vector <double> si = vector <double> ();
- for(int j=0; j<m; j++){
- cout << "Coordinate " << (j+1) << ": ";
- double coordinate;
- cin >> coordinate;
- si.push_back(coordinate);
- }
- sVector.push_back(si);
- cout << endl;
- }
- // 3. Show the user the "n" vector he has created
- for(unsigned int i=0; i<sVector.size(); i++){
- cout << "s" << (i+1) << " = ";
- displayVector(sVector[i]);
- }
- cout << endl;
- /*
- // Checking the functions
- cout << "Norm of s1 = ||s1|| = " << norm(sVector[0]) << endl;
- cout << "Norm of s2 = ||s2|| = " << norm(sVector[1]) << endl;
- vector <double> sumS1S2 = add(sVector[0], sVector[1]);
- cout << "s1 + s2 = ";
- displayVector(sumS1S2);
- vector <double> subtractS1S2 = subtract(sVector[0], sVector[1]);
- cout << "s1 - s2 = ";
- displayVector(subtractS1S2);
- double productS1S2 = multiply(sVector[0], sVector[1]);
- cout << "s1 * s2 = " << productS1S2;
- vector <double> prod = multiplyVectorByNumber(sVector[0], 8);
- cout << "\n8 * s1 = ";
- displayVector(prod);
- vector <double> normalizationOfS1 = divideVectorByNumber(sVector[0], norm(sVector[0]));
- cout << "Normalizing s1 ----> (s1/ ||s1||) = ";
- displayVector(normalizationOfS1);
- */
- cout << "~~~~ Gram - Schmidt having the vectors ";
- for(int i=0; i<sVector.size(); i++){
- if(i != sVector.size()-1){
- cout << "s" << (i+1) << ", ";
- }
- else{
- cout << "s" << (i+1) << " ~~~~" << endl;
- }
- }
- vector < vector <double> > bases = gramSchmidt(sVector);
- for(unsigned int i=0; i<sVector.size(); i++){
- cout << "base" << (i+1) << " = ";
- displayVector(bases[i]);
- }
- cout << endl << endl;
- cout << "~~~~ Confirmation ~~~~" << endl;
- for(unsigned int i=0; i<sVector.size(); i++){
- cout << "s" << (i+1) << " = ";
- for(unsigned int j=0; j<bases.size(); j++){
- if(j != bases.size()-1){
- cout << multiply(sVector[i], bases[j]) << "*";
- displayVector(bases[j]);
- cout << " + ";
- }
- else{
- cout << multiply(sVector[i], bases[j]) << "*";
- displayVector(bases[j]);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement