-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMath.cpp
More file actions
163 lines (148 loc) · 3.43 KB
/
ArrayMath.cpp
File metadata and controls
163 lines (148 loc) · 3.43 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "ArrayMath.h"
#include <iostream>
using namespace std;
void ArrayMath::DotProduct(float* multiplier, float* product, float** multiplicand, int sizeX, int sizeY)
{
int newSize = sizeX;
int length = sizeY;
for(int x=0; x<sizeX; x++)
product[x]=0;
for(int x=0; x<sizeX; x++)
{
for(int y=0; y<sizeY; y++)
{
if(sizeX==newSize)
{
product[x] += multiplicand[x][y] * multiplier[y];
}else if(sizeY==newSize)
{
cout << "Error" << endl;
}
}
}
}
void ArrayMath::Multiply(float* multiplicand, int length, float multiplier)
{
for(int i=0; i<length; i++)
{
multiplicand[i] = multiplicand[i]*multiplier;
}
}
void ArrayMath::Add(float* a, float* b, int length)
{
for(int i=0; i<length; i++)
{
a[i]+=b[i];
}
}
void ArrayMath::Sub(float* a, float* b, int length)
{
for(int i=0; i<length; i++)
{
a[i]-=b[i];
}
}
void ArrayMath::CrossProduct(float** multiplicand, int sizeX, int sizeY, float* multiplier, int length)
{
if(sizeX!=length && sizeY!=length)
{
cout << "INCOMPATIBLE SIZES: " << length << " crossProduct(" << sizeX << "x" << sizeY << endl;
return;
}
for(int x=0; x<sizeX; x++)
{
for(int y=0; y<sizeY; y++)
{
if(sizeX==length)
{
multiplicand[x][y] = multiplicand[x][y] * multiplier[x];
}else if(sizeY==length)
{
multiplicand[x][y] = multiplicand[x][y] * multiplier[y];
}
}
}
}
void ArrayMath::Multiply(float** multiplicand, int sizeX, int sizeY, float multiplier)
{
for(int x=0; x<sizeX; x++)
{
for(int y=0; y<sizeY; y++)
{
multiplicand[x][y] = multiplicand[x][y] * multiplier;
}
}
}
void ArrayMath::Add(float** a, float** b, int sizeX, int sizeY)
{
for(int x=0; x<sizeX; x++)
{
for(int y=0; y<sizeY; y++)
{
a[x][y]+=b[x][y];
}
}
}
float** ArrayMath::Copy(float** toCopy, int sizeX, int sizeY)
{
float** copy = new float*[sizeX];
for(int x=0; x<sizeX; x++)
{
copy[x] = new float[sizeY];
for(int y=0; y<sizeY; y++)
{
copy[x][y] = toCopy[x][y];
}
}
return copy;
}
float* ArrayMath::Copy(float* toCopy, int length)
{
float* copy = new float[length];
for(int i=0; i<length; i++)
{
copy[i] = toCopy[i];
}
return copy;
}
void ArrayMath::displayProgress(int aProgress, int maxProgress)
{
int barWidth = 70;
float progress = ((float) aProgress+1) / (float) maxProgress;
cout << "[";
int pos = barWidth * progress;
for (int i = 0; i < barWidth; ++i) {
if (i < pos) cout << "=";
else if (i == pos) cout << ">";
else cout << " ";
}
cout << "] " << int(progress * 100.0) << " %\r";
cout.flush();
}
void ArrayMath::print(float** array, int sizeX, int sizeY)
{
for(int x=0; x<sizeX; x++)
{
print(array[x], sizeY);
}
}
void ArrayMath::print(float* array, int length)
{
for(int i=0; i<length; i++)
{
cout << array[i] << ",";
}
cout << endl;
}
void ArrayMath::deleteArray(float** array, int sizeX)
{
for(int i=0; i<sizeX; i++)
{
deleteArray(array[i]);
}
delete[] array;
}
void ArrayMath::deleteArray(float* array)
{
delete[] array;
}