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