Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MatrixVectorConversion
//g++ 7.4.0 //this code is created by Rezaul Hoque on September 20,21;contact:jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> #include <iostream> #include <cmath> class Matrix { friend std::ostream& operator<<(std::ostream &, const Matrix &);//stream extraction operator friend std::istream& operator>>(std::istream &, const Matrix &);//stream insertion operator friend Matrix operator+(const Matrix &, const Matrix&);//binary addition operator public: Matrix (double a=0, double b=0,double c=0, double d=0) : r11(a), r12(b),r21(c), r22(d) {} Matrix (const Matrix& s) : r11(s.r11),r12(s.r12),r21(s.r21),r22(s.r22) {} double det() { return (r11*r22 - r12*r21);} int isSingular() { return det() == 0;} Matrix inverse(); void print (); double r11,r12,r21,r22; }; Matrix Matrix :: inverse() { double k = 1/det(); Matrix temp (k*r22,-k*r12,-k*r21,k*r11); return temp; } std::ostream& operator<<(std::ostream& ost,const Matrix& m) { ost<<m.r11;std::cout<<" ";ost<<m.r12;std::cout<<'\n';ost<<m.r21;std::cout<<" ";ost<<m.r22;std::cout<<'\n'; return ost; } std::istream& operator>>(std::istream& ist,const Matrix& m) { std::cout<<"r11:\n"; ist>>m.r11; std::cout<<"r11:\n"; ist>>m.r12; std::cout<<"r21:\n"; ist>>m.r21; std::cout<<"r22:\n"; ist>>m.r22; return ist; } Matrix operator+(const Matrix & v, const Matrix& w){ Matrix z(4); z.r11= v.r11+w.r11; z.r12= v.r12+w.r12; z.r21= v.r21+w.r21; z.r22= v.r22+w.r22; return z; } class Vector { friend Vector operator*(const Vector&, double t); friend std::ostream& operator<<(std::ostream &, const Vector &);//stream extraction operator friend std::istream& operator>>(std::istream &, Vector &);//stream insertion operator friend Vector operator+(const Vector &, const Vector&);//binary addition operator friend Vector operator-(const Vector &, const Vector&);//binary subtraction operator friend Vector operator&(Vector&, Vector&); public: Vector (int ,double );//default constructor Vector(double a,double b,double c,double d) { w=a;x=b;y=c;z=d; } Vector (const Vector &);//copy constructor Vector(Matrix & m) { data = new double[4]; data[0]=m.r11; data[1]=m.r12; data[2]=m.r21; data[3]=m.r22; } ~Vector();//destructor const Vector & operator=(const Vector &);//assignment operator double& operator[](int) const;//subscript operator Vector norm(); void print(){std::cout<<" ("<<w; std::cout<<","<<x; std::cout<<","<<y; std::cout<<","<<z<<")";} int size; double w, x,y,z; double * data; }; std::ostream& operator<<(std::ostream& ostr,const Vector& v) { int l; ostr<<"( "; for(l=0;l<v.size-1;l++){ ostr<<v.data[l]<<","; if((l+1)%4 == 0) std::cout<<"\n"; } return ostr<<v.data[l]<<")\n"; } std::istream& operator>>(std::istream& istr,const Vector& v) { for(int l=0;l<=v.size-1;l++) { std::cout<<l<<":"; istr>>v[l]; } return istr; } Vector::Vector(int sz=1,double t=0.0): size(sz) { data = new double[size]; for(int l=0;l<size;l++) data[l]=t; } Vector::Vector(const Vector & v): size(v.size) { data = new double[v.size]; for(int l=0;l<v.size;l++) data[l]=v.data[l]; } Vector::~Vector() { delete [] data; data = NULL; size=0; } const Vector& Vector::operator=(const Vector & v) { if(&v != this) { delete [] data; size = v.size; data = new double[size]; for(int l=0;l<size;l++) data[l]=v.data[l]; } return *this; } Vector operator& (Vector& v,Vector & w) { Vector z(3); z.data[0]=v.data[1]*w.data[2]-v.data[2]*w.data[1]; z.data[1]=v.data[2]*w.data[0]-v.data[0]*w.data[2]; z.data[2]=v.data[0]*w.data[1]-v.data[1]*w.data[0]; return z; } double& Vector:: operator[](int I) const { return data[I]; } Vector operator+(const Vector & v,const Vector& w) { Vector z(4,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] + w.data[l];} return z; } Vector operator-(const Vector & v,const Vector& w) { Vector z(3,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] - w.data[l];} return z; } Vector operator*(const Vector& v,double b) { Vector z(3); for(int l=0; l<z.size; l++) z.data[l] += v.data[l]*b; return z; } Vector Vector:: norm() { Vector z(3); for(int l=0; l<size; l++) z.data[l] +=sqrt(data[l]*data[l]); return z; } int main() { Matrix m(1,2,3,4); std::cout<<"The matrix looks like:\n"; std::cout<<m<<"\n"; if(!m.isSingular()) std::cout<<"The matrix is nonsingular.\n"; std::cout<<"After the inversion the matrix looks like:\n"; Matrix p; p=m.inverse(); std::cout<<p; Matrix o,q(1,2,3,4),r(5,6,7,8); std::cout<<"o=q+r:\n"; o=q+r; std::cout<<o; Vector k; k=m; k.print(); return 0; }
run
|
edit
|
history
|
help
0
Get all anagrams from given words
ignat
FloydTriangle
MCM
C++ Inheritance Example
newwork
Test
ClassQuiz2
Scemo
Test 5(2020)