1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include<vector>
#include<iostream>
#include<string>
using namespace std;
#define MATRIX vector<vector<double>>
#define VECTOR vector<double>
VECTOR operator *(MATRIX m1,VECTOR m2)
{
int rows=m1.size(),r,k;
if (m1.size() != m2.size()) {printf(" uncompatible indices nothing done \n ");return m2;}
vector<double>ans;
double rxc;
ans.resize(rows);
rows=rows-1;
for(r=0;r<=rows;r++)
{
rxc=0;
for(k=0;k<=rows;k++)
{
rxc=rxc+m1[r][k]*m2[k];
}
ans[r]=rxc;
}
return ans;
}
MATRIX operator *( MATRIX m1,MATRIX m2)
{
//overload to be done if required (mat*mat) with print function below
}
void print(VECTOR v)
{
int n=v.size(),i;
string sp;
for(i=0;i<n;i++)
{
if(v[i]>=0) sp="+"; else sp="";
cout<<sp<<v[i]<<endl;
}
cout<<endl;
}
int main()
{
MATRIX unit
{
{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1}
};
MATRIX d
{
{-150, -2,-3,9},
{-4, 5, 6,2},
{7, 8.5,-9,8},
{2, 0, -3,8}
};
VECTOR r
{
{1.1,2.2,3.3,4.4}
};
//==============//
VECTOR answer=d*r;
print(answer);
print(unit*r);
cout <<" "<<endl;
cout <<"Press return to end . . ."<<endl;
cin.get();
}
|