-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorGeometry.cpp
168 lines (155 loc) · 5.26 KB
/
VectorGeometry.cpp
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
168
//
// Created by Ben Roberts on 6/16/2023.
//
#include "VectorGeometry.h"
void printPoint(Point p) {
std::cout << p.x << " ";
std::cout << p.y << " ";
std::cout << std::endl;
std::cout << "" << std::endl;
}
void printVector(Vector v) {
for (int i = 0; i < v.dim; i++) {
std::cout << v.vectorArray[i] << " ";
}
std::cout << std::endl;
std::cout << "" << std::endl;
}
Matrix createMatrix(Vector* vectors, int numVectors) {
Matrix m{};
m.rows = numVectors;
int cols = 0;
// Find the vec with the highest dimension in the set of vectors.
// Then this dim will become the dim of the matrix.
for (int i = 0; i < numVectors; i++) {
if (vectors[i].dim > cols) {
cols = vectors[i].dim;
}
}
m.cols = cols;
m.rowVector = new Vector[m.rows];
// Copy the vectors into the matrix.
for (int i = 0; i < m.rows; i++) {
m.rowVector[i].dim = m.cols;
m.rowVector[i].vectorArray = new double[m.cols];
// If the vector has a lower dim then the matrix we can just fill in 0s.
for (int j = 0; j < m.cols; j++) {
if (vectors[i].dim > j) {
m.rowVector[i].vectorArray[j] = vectors[i].vectorArray[j];
} else {
m.rowVector[i].vectorArray[j] = 0.0;
}
}
}
return m;
}
void printMatrix(Matrix matrix) {
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.rowVector[i].dim; j++) {
std::cout << matrix.rowVector[i].vectorArray[j] << " ";
}
std::cout << std::endl;
}
std::cout << "" << std::endl;
}
Vector addVectors (Matrix m) {
Vector result{};
result.dim = m.cols;
result.vectorArray = new double[m.cols]();
// Add the vectors component-wise
for (int i = 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
result.vectorArray[j] += m.rowVector[i].vectorArray[j];
}
}
return result;
}
Matrix reduce(Matrix m) {
for (int piv = 0; piv < m.rows; piv++) {
double t1 = m.rowVector[piv].vectorArray[piv];
for (int j = piv + 1; j < m.rows; j++) {
if (m.rowVector[piv].vectorArray[j] != 0) {
double t2 = m.rowVector[j].vectorArray[piv];
double t3 = 0.0;
if (t2 != 0)
{
t3 = t1 / t2;
}
for (int z = piv; z < m.rows; z++) {
m.rowVector[j].vectorArray[z] = m.rowVector[piv].vectorArray[z] - (m.rowVector[j].vectorArray[z] * t3);
if (m.rowVector[j].vectorArray[z] < 1e-14 && m.rowVector[j].vectorArray[z] > -1e-14)
{
m.rowVector[j].vectorArray[z] = 0;
}
}
}
}
}
return m;
}
double det(Matrix& m) {
double det = 1.0;
for (int piv = 0; piv < m.rows - 1; piv++) {
double t1 = m.rowVector[piv].vectorArray[piv];
if (t1 == 0) {
return 0.0;
}
for (int j = piv + 1; j < m.rows; j++) {
if (m.rowVector[j].vectorArray[piv] != 0) {
double t2 = m.rowVector[j].vectorArray[piv];
double t3;
if (t2 != 0) {
t3 = t1 / t2;
if (t3 == 0) {
return 0.0;
}
det /= -t3;
} else {
return 0.0;
}
for (int z = piv; z < m.rows; z++) {
m.rowVector[j].vectorArray[z] = m.rowVector[piv].vectorArray[z] - (m.rowVector[j].vectorArray[z] * t3);
if (m.rowVector[j].vectorArray[z] < 1e-14 && m.rowVector[j].vectorArray[z] > -1e-14) {
m.rowVector[j].vectorArray[z] = 0;
}
}
}
}
}
for (int i = 0; i < m.rows; i++) {
det *= m.rowVector[i].vectorArray[i];
}
return det;
}
Matrix RREF(Matrix m) {
const int rows = m.rows; // number of rows
const int cols = m.cols; // number of columns
int pivCol = 0;
while (pivCol < rows) {
double t1, t2;
for (int r = 0; r < rows; r++) {
t1 = m.rowVector[pivCol].vectorArray[pivCol];
t2 = 0;
if (t1 != 0 && m.rowVector[r].vectorArray[pivCol] != 0) {
t2 = m.rowVector[r].vectorArray[pivCol] / m.rowVector[pivCol].vectorArray[pivCol];
}
for (int c = 0; c < cols; c++) {
if (r == pivCol) {
if (t1 != 0 && m.rowVector[r].vectorArray[c] != 0) {
m.rowVector[r].vectorArray[c] /= t1; // make pivot 1
} else {
m.rowVector[r].vectorArray[c] = 0;
}
}
else {
double t3 = m.rowVector[pivCol].vectorArray[c] * t2;
if (t3 != 0) {
m.rowVector[r].vectorArray[c] -= t3;
}
}
}
}
pivCol++;
}
return m;
}