1 /*
2 *_________________________________________________________________________*
3 * POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE *
4 * DESCRIPTION: SEE READ-ME *
5 * FILE NAME: colmatrix.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 "colmatrix.h"
19 #include "rowmatrix.h"
20 #include <iostream>
21 #include <cmath>
22 #include <cstdlib>
23
24 using namespace std;
25
ColMatrix()26 ColMatrix::ColMatrix(){
27 numrows = 0;
28 elements = 0;
29 }
30
~ColMatrix()31 ColMatrix::~ColMatrix(){
32 delete [] elements;
33 }
34
ColMatrix(const ColMatrix & A)35 ColMatrix::ColMatrix(const ColMatrix& A){ // copy constructor
36 numrows = 0;
37 elements = 0;
38 Dim(A.numrows);
39 for(int i=0;i<numrows;i++)
40 elements[i] = A.elements[i];
41 }
42
ColMatrix(const VirtualColMatrix & A)43 ColMatrix::ColMatrix(const VirtualColMatrix& A){ // copy constructor
44 numrows = 0;
45 elements = 0;
46 Dim(A.GetNumRows());
47 for(int i=0;i<numrows;i++)
48 elements[i] = A.BasicGet(i);
49 }
50
ColMatrix(const VirtualMatrix & A)51 ColMatrix::ColMatrix(const VirtualMatrix& A){ // copy constructor
52 if( A.GetNumCols() != 1 ){
53 cerr << "error trying to write a 2D matrix to a collumn" << endl;
54 exit(1);
55 }
56 numrows = 0;
57 elements = 0;
58 Dim(A.GetNumRows());
59 for(int i=0;i<numrows;i++)
60 elements[i] = A.BasicGet(i,0);
61 }
62
ColMatrix(int m)63 ColMatrix::ColMatrix(int m){ // size constructor
64 numrows = 0;
65 elements = 0;
66 Dim(m);
67 }
68
Dim(int m)69 void ColMatrix::Dim(int m){
70 delete [] elements;
71 numrows = m;
72 elements = new double [m];
73 }
74
Const(double value)75 void ColMatrix::Const(double value){
76 for(int i=0;i<numrows;i++)
77 elements[i] = value;
78 }
79
GetType() const80 MatrixType ColMatrix::GetType() const{
81 return COLMATRIX;
82 }
83
ReadData(istream & c)84 istream& ColMatrix::ReadData(istream& c){
85 int n;
86 c >> n;
87 Dim(n);
88 for(int i=0;i<n;i++)
89 c >> elements[i];
90 return c;
91 }
92
WriteData(ostream & c) const93 ostream& ColMatrix::WriteData(ostream& c) const{ //output
94 c << numrows << ' ';
95 for(int i=0;i<numrows;i++)
96 c << elements[i] << ' ';
97 return c;
98 }
99
operator_1int(int i)100 double& ColMatrix::operator_1int (int i){ // array access
101 if((i>numrows) || (i<1)){
102 cerr << "matrix index invalid in operator ()" << endl;
103 exit(1);
104 }
105 return elements[i-1];
106 }
107
Get_1int(int i) const108 double ColMatrix::Get_1int(int i) const{
109 if((i>numrows) || (i<1)){
110 cerr << "matrix index exceeded in Get" << endl;
111 exit(1);
112 }
113 return elements[i-1];
114 }
115
Set_1int(int i,double value)116 void ColMatrix::Set_1int(int i, double value){
117 if((i>numrows) || (i<1)){
118 cerr << "matrix index exceeded in Set" << endl;
119 exit(1);
120 }
121 elements[i-1] = value;
122 }
123
BasicGet_1int(int i) const124 double ColMatrix::BasicGet_1int(int i) const{
125 return elements[i];
126 }
127
GetElementPointer(int i)128 double* ColMatrix::GetElementPointer(int i){
129 return &(elements[i]);
130 }
131
BasicSet_1int(int i,double value)132 void ColMatrix::BasicSet_1int(int i, double value){
133 elements[i] = value;
134 }
135
BasicIncrement_1int(int i,double value)136 void ColMatrix::BasicIncrement_1int(int i, double value){
137 elements[i] += value;
138 }
139
AssignVM(const VirtualMatrix & A)140 void ColMatrix::AssignVM(const VirtualMatrix& A){
141 if( A.GetNumCols() != 1 ){
142 cerr << "error trying to write a 2D matrix to a collumn" << endl;
143 exit(1);
144 }
145 Dim( A.GetNumRows() );
146 for(int i=0;i<numrows;i++)
147 elements[i] = A.BasicGet(i,0);
148 }
149
operator =(const ColMatrix & A)150 ColMatrix& ColMatrix::operator=(const ColMatrix& A){ // assignment operator
151 Dim(A.numrows);
152 for(int i=0;i<numrows;i++)
153 elements[i] = A.elements[i];
154 return *this;
155 }
156
operator =(const VirtualColMatrix & A)157 ColMatrix& ColMatrix::operator=(const VirtualColMatrix& A){ // overloaded =
158 Dim( A.GetNumRows() );
159 for(int i=0;i<numrows;i++)
160 elements[i] = A.BasicGet(i);
161 return *this;
162 }
163
operator =(const VirtualMatrix & A)164 ColMatrix& ColMatrix::operator=(const VirtualMatrix& A){ // overloaded =
165 if( A.GetNumCols() != 1 ){
166 cerr << "error trying to write a 2D matrix to a collumn" << endl;
167 exit(1);
168 }
169 Dim( A.GetNumRows() );
170 for(int i=0;i<numrows;i++)
171 elements[i] = A.BasicGet(i,0);
172 return *this;
173 }
174
operator *=(double b)175 ColMatrix& ColMatrix::operator*=(double b){
176 for(int i=0;i<numrows;i++)
177 elements[i] *= b;
178 return *this;
179 }
180
Abs()181 void ColMatrix::Abs(){
182 for(int i=0;i<numrows;i++)
183 elements[i] = std::abs(elements[i]);
184 }
185
BasicMax(double & value,int & index)186 void ColMatrix::BasicMax(double& value, int& index){
187 value = elements[0];
188 index = 0;
189 for(int i=1;i<numrows;i++)
190 if(elements[i] > value){
191 value = elements[i];
192 index = i;
193 }
194 }
195
BasicMin(double & value,int & index)196 void ColMatrix::BasicMin(double& value, int& index){
197 value = elements[0];
198 index = 0;
199 for(int i=1;i<numrows;i++)
200 if(elements[i] < value){
201 value = elements[i];
202 index = i;
203 }
204 }
205
206