1 /* 2 *_________________________________________________________________________* 3 * POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE * 4 * DESCRIPTION: SEE READ-ME * 5 * FILE NAME: rowmatrix.cpp * 6 * AUTHORS: See Author List * 7 * GRANTS: See Grants List * 8 * COPYRIGHT: (C) 2005 by Authors as listed in Author's List * 9 * LICENSE: Please see License Agreement * 10 * DOWNLOAD: Free at www.rpi.edu/~anderk5 * 11 * ADMINISTRATOR: Prof. Kurt Anderson * 12 * Computational Dynamics Lab * 13 * Rensselaer Polytechnic Institute * 14 * 110 8th St. Troy NY 12180 * 15 * CONTACT: anderk5@rpi.edu * 16 *_________________________________________________________________________*/ 17 18 #include "rowmatrix.h" 19 #include "colmatrix.h" 20 #include <iostream> 21 #include <cstdlib> 22 23 using namespace std; 24 RowMatrix()25RowMatrix::RowMatrix(){ 26 numcols = 0; 27 elements = 0; 28 } 29 ~RowMatrix()30RowMatrix::~RowMatrix(){ 31 delete [] elements; 32 } 33 RowMatrix(const RowMatrix & A)34RowMatrix::RowMatrix(const RowMatrix& A){ // copy constructor 35 numcols = 0; 36 elements = 0; 37 Dim(A.numcols); 38 for(int i=0;i<numcols;i++) 39 elements[i] = A.elements[i]; 40 } 41 RowMatrix(const VirtualRowMatrix & A)42RowMatrix::RowMatrix(const VirtualRowMatrix& A){ // copy constructor 43 numcols = 0; 44 elements = 0; 45 Dim(A.GetNumCols()); 46 for(int i=0;i<numcols;i++) 47 elements[i] = A.BasicGet(i); 48 } 49 RowMatrix(const VirtualMatrix & A)50RowMatrix::RowMatrix(const VirtualMatrix& A){ // copy constructor 51 if( A.GetNumRows() != 1 ){ 52 cerr << "error trying to write a 2D matrix to a collumn" << endl; 53 exit(1); 54 } 55 numcols = 0; 56 elements = 0; 57 Dim(A.GetNumCols()); 58 for(int i=0;i<numcols;i++) 59 elements[i] = A.BasicGet(i,0); 60 } 61 RowMatrix(int n)62RowMatrix::RowMatrix(int n){ // size constructor 63 numcols = 0; 64 elements = 0; 65 Dim(n); 66 } 67 Dim(int n)68void RowMatrix::Dim(int n){ 69 delete [] elements; 70 numcols = n; 71 elements = new double [n]; 72 } 73 Const(double value)74void RowMatrix::Const(double value){ 75 for(int i=0;i<numcols;i++) 76 elements[i] = value; 77 } 78 operator_1int(int i)79double& RowMatrix::operator_1int (int i){ // array access 80 if((i>numcols) || (i<1)){ 81 cerr << "matrix index invalid in operator ()" << endl; 82 exit(1); 83 } 84 return elements[i-1]; 85 } 86 Get_1int(int i) const87double RowMatrix::Get_1int(int i) const{ 88 if((i>numcols) || (i<1)){ 89 cerr << "matrix index exceeded in Get" << endl; 90 exit(1); 91 } 92 return elements[i-1]; 93 } 94 Set_1int(int i,double value)95void RowMatrix::Set_1int(int i, double value){ 96 if((i>numcols) || (i<1)){ 97 cerr << "matrix index exceeded in Set" << endl; 98 exit(1); 99 } 100 elements[i-1] = value; 101 } 102 BasicGet_1int(int i) const103double RowMatrix::BasicGet_1int(int i) const{ 104 return elements[i]; 105 } 106 BasicSet_1int(int i,double value)107void RowMatrix::BasicSet_1int(int i, double value){ 108 elements[i] = value; 109 } 110 BasicIncrement_1int(int i,double value)111void RowMatrix::BasicIncrement_1int(int i, double value){ 112 elements[i] += value; 113 } 114 GetType() const115MatrixType RowMatrix::GetType() const{ 116 return ROWMATRIX; 117 } 118 ReadData(istream & c)119istream& RowMatrix::ReadData(istream& c){ 120 int n; 121 c >> n; 122 Dim(n); 123 for(int i=0;i<n;i++) 124 c >> elements[i]; 125 126 return c; 127 } 128 WriteData(ostream & c) const129ostream& RowMatrix::WriteData(ostream& c) const{ //output 130 c << numcols << ' '; 131 for(int i=0;i<numcols;i++) 132 c << elements[i] << ' '; 133 return c; 134 } 135 AssignVM(const VirtualMatrix & A)136void RowMatrix::AssignVM(const VirtualMatrix& A){ 137 if( A.GetNumRows() != 1 ){ 138 cerr << "error trying to write a 2D matrix to a collumn" << endl; 139 exit(1); 140 } 141 Dim( A.GetNumCols() ); 142 for(int i=0;i<numcols;i++) 143 elements[i] = A.BasicGet(0,i); 144 } 145 operator =(const RowMatrix & A)146RowMatrix& RowMatrix::operator=(const RowMatrix& A){ // assignment operator 147 Dim(A.numcols); 148 for(int i=0;i<numcols;i++) 149 elements[i] = A.elements[i]; 150 return *this; 151 } 152 operator =(const VirtualRowMatrix & A)153RowMatrix& RowMatrix::operator=(const VirtualRowMatrix& A){ // overloaded = 154 Dim( A.GetNumCols() ); 155 for(int i=0;i<numcols;i++) 156 elements[i] = A.BasicGet(i); 157 return *this; 158 } 159 operator =(const VirtualMatrix & A)160RowMatrix& RowMatrix::operator=(const VirtualMatrix& A){ // overloaded = 161 if( A.GetNumRows() != 1 ){ 162 cerr << "error trying to write a 2D matrix to a collumn" << endl; 163 exit(1); 164 } 165 Dim( A.GetNumCols() ); 166 for(int i=0;i<numcols;i++) 167 elements[i] = A.BasicGet(0,i); 168 return *this; 169 } 170 operator *=(double b)171RowMatrix& RowMatrix::operator*=(double b){ 172 for(int i=0;i<numcols;i++) 173 elements[i] *= b; 174 return *this; 175 } 176 177