-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
145 lines (114 loc) · 2.23 KB
/
matrix.cpp
File metadata and controls
145 lines (114 loc) · 2.23 KB
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
/*
* matrix.cpp
* RVM-Speed
*
* Created by Robert Lowe on 27/08/2010.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "matrix.h"
matrix::matrix(int a, int b){
rows=a;
cols=b;
data = new double[a*b];
}
matrix::matrix(){
rows=0;
cols=0;
data = new double[1];
};
void matrix::reset(int a ,int b){
if(data)delete[] data;
rows=a;
cols=b;
data = new double[a*b];
}
void matrix::resize(int a ,int b){
double *tmpcpy=new double[a*b];
for (int i=0; i<rows; i++) {
for (int k=0; k<cols; k++) {
tmpcpy[i*cols+k]=data[i*cols+k];
}
}
rows=a;
cols=b;
delete[] data;
data = tmpcpy;
}
void matrix::AddColumn(const matrix &A,int column){
if (rows!=0 && cols!=0){
double *tmpcpy=new double[rows*(cols+1)];
for (int i=0; i<rows; i++) {
for (int k=0; k<cols; k++) {
tmpcpy[i*(cols+1)+k]=data[i*cols+k];
}
tmpcpy[i*(cols+1)+(cols)]=A.data[i*A.cols+column];
}
cols+=1;
delete[] data;
data=tmpcpy;
}
else{
delete[] data;
rows=A.rows;
cols=1;
data = new double[rows*cols];
for (int i=0; i<A.rows; i++){
data[i]=A.data[(i*A.cols)+column];
}
}
}
void matrix::RemoveColumn(int a){
double *tmpcpy=new double[rows*(cols-1)];
for (int i=0; i<rows; i++) {
for (int k=0; k<cols-1; k++){
if(k<a)
tmpcpy[i*(cols-1)+k]=data[i*cols+k];
else
tmpcpy[i*(cols-1)+k]=data[i*cols+(k+1)];
}
}
cols-=1;
delete[] data;
data=tmpcpy;
}
void matrix::RemoveRow(int a){
double *tmpcpy=new double[(rows-1)*cols];
for (int i=0; i<(rows-1); i++) {
for (int k=0; k<cols; k++){
if(i<a)
tmpcpy[i*cols+k]=data[i*cols+k];
else
tmpcpy[i*cols+k]=data[(i+1)*cols+k];
}
}
rows-=1;
delete[] data;
data=tmpcpy;
}
/*matrix matrix::diag(){
matrix out(rows,rows);
for (unsigned i = 0; i < rows; ++ i){
for (unsigned j = 0; j <rows; ++ j){
if(i==j)
out.data[(i*rows)+j]=data[i];
else
out.data[(i*rows)+j]=0.0;
}
}
return out;
}
*/
/*
matrix matrix::operator=(const matrix &a){
matrix temp(a.rows,a.cols);
for(int i=0; i<a.rows; i++){
for(int k=0; k<a.cols; k++){
temp.data[i*a.cols+k]=a.data[i*a.cols+k];
std:: cout << i << " " <<k << std::endl;
}
}
std::cout << "USING OVERLOAD" << std::endl;
return(temp);
}
*/