File: matrix.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (167 lines) | stat: -rw-r--r-- 4,234 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 *_________________________________________________________________________*
 *      POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE     *
 *      DESCRIPTION: SEE READ-ME                                           *
 *      FILE NAME: matrix.cpp                                              *
 *      AUTHORS: See Author List                                           *
 *      GRANTS: See Grants List                                            *
 *      COPYRIGHT: (C) 2005 by Authors as listed in Author's List          *
 *      LICENSE: Please see License Agreement                              *
 *      DOWNLOAD: Free at www.rpi.edu/~anderk5                             *
 *      ADMINISTRATOR: Prof. Kurt Anderson                                 *
 *                     Computational Dynamics Lab                          *
 *                     Rensselaer Polytechnic Institute                    *
 *                     110 8th St. Troy NY 12180                           *
 *      CONTACT:        anderk5@rpi.edu                                    *
 *_________________________________________________________________________*/


#include "matrix.h"
#include <iostream>
#include <cstdlib>

using namespace std;

Matrix::Matrix(){
  numrows = numcols = 0;
  rows = 0;
  elements = 0;
}

Matrix::~Matrix(){
  delete [] rows;
  delete [] elements;
}

Matrix::Matrix(const Matrix& A){
  numrows = numcols = 0;
  rows = 0;
  elements = 0;
  Dim(A.numrows,A.numcols);
  for(int i=0;i<numrows*numcols;i++)
    elements[i] = A.elements[i];
}

Matrix::Matrix(const VirtualMatrix& A){
  numrows = numcols = 0;
  rows = 0;
  elements = 0;
  Dim(A.GetNumRows(),A.GetNumCols());
  for(int i=0;i<numrows;i++)
    for(int j=0;j<numcols;j++)
      rows[i][j] = A.BasicGet(i,j);
}

Matrix::Matrix(int m, int n){
  numrows = numcols = 0;
  rows = 0;
  elements = 0;
  this->Dim(m,n);
}

double& Matrix::operator_2int(int i, int j){
  if((i>numrows) || (j>numcols) || (i*j==0)){
    cerr << "matrix index exceeded in operator ()" << endl;
    exit(1);
  }
  return rows[i-1][j-1];
}

double Matrix::Get_2int(int i, int j) const {
  if((i>numrows) || (j>numcols) || (i*j==0)){
    cerr << "matrix index exceeded in Get" << endl;
    exit(1);
  }
  return rows[i-1][j-1];
}

void Matrix::Set_2int(int i, int j, double value){
  if((i>numrows) || (j>numcols) || (i*j==0)){
    cerr << "matrix index exceeded in Set" << endl;
    exit(1);
  }
  rows[i-1][j-1] = value;
}

double Matrix::BasicGet_2int(int i, int j) const {
  return rows[i][j];
}

void Matrix::BasicSet_2int(int i, int j, double value){
  rows[i][j] = value;
}

void Matrix::BasicIncrement_2int(int i, int j, double value){
  rows[i][j] += value;
}

void Matrix::Const(double value){
  int num = numrows*numcols;
  for(int i=0;i<num;i++) elements[i] = value;
}

MatrixType Matrix::GetType() const{
  return MATRIX;
}

istream& Matrix::ReadData(istream& c){
  int n,m;
  c >> n >> m;
  Dim(n,m);
  for(int i=0;i<numrows;i++)
    for(int j=0;j<numcols;j++){
      c >> rows[i][j];
    }
  return c;
}

ostream& Matrix::WriteData(ostream& c) const{  //output
  c << numrows << ' ' << numcols << ' ';
  for(int i=0;i<numrows;i++)
    for(int j=0;j<numcols;j++){
      c << rows[i][j] << ' ';
    }
  return c;
}

Matrix& Matrix::Dim(int m, int n){  // allocate size
  numrows = m;
  numcols = n;
  delete [] rows;
  delete [] elements;
  elements = new double[n*m];
  rows = new double*[m];
  for(int i=0;i<m;i++)
    rows[i] = &elements[i*numcols];
  return *this;
}

void Matrix::AssignVM(const VirtualMatrix& A){
  Dim( A.GetNumRows(), A.GetNumCols() );
  for(int i=0;i<numrows;i++)
    for(int j=0;j<numcols;j++)
      rows[i][j] = A.BasicGet(i,j);
}

Matrix& Matrix::operator=(const Matrix& A){
  Dim(A.numrows,A.numcols);
  for(int i=0;i<numrows*numcols;i++)
    elements[i] = A.elements[i];
  return *this;
}

Matrix& Matrix::operator=(const VirtualMatrix& A){
  Dim( A.GetNumRows(), A.GetNumCols() );
  for(int i=0;i<numrows;i++)
    for(int j=0;j<numcols;j++)
      rows[i][j] = A.BasicGet(i,j);
  return *this;
}

Matrix& Matrix::operator*=(double b){
  for(int i=0;i<numrows;i++)
    for(int j=0;j<numcols;j++)
      rows[i][j] *= b;
  return *this;
}