-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utilities.h
205 lines (171 loc) · 4.85 KB
/
Utilities.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*************************************************************************************************
* This file contains functions that are used to perform transformations on matrices and vectors.*
* *
*************************************************************************************************/
#ifndef _UTILITIES_h
#define _UTILITIES_h
#include "Vertex.h"
#include "Matrices/DenseMatrix1D.h"
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stack>
/*
* returns the sum of the values in array arr
* @pram: pointer to an array of floats
* @pram: size of the array
*/
template <typename DT>
DT sum_array(DT* arr,int arr_size){
DT sum=0;
for(int j=0;j<arr_size;j++){
sum+=arr[j];
}
return sum;
}
/*
* returns the minimum value in the array arr
* @pram: pointer to array of floats
* @pram: size of the array
*/
template <typename DT>
DT min(DT* arr, int arr_size){
DT min_so_far=arr[0];
for(int i=1;i<arr_size;i++){
if(arr[i]<min_so_far){
min_so_far=arr[i];
}
}
return min_so_far;
}
/*
* returns the maximum value in the array arr
* @pram: pointer to array
* @pram: size of the array
*/
template <typename DT>
DT max(DT* arr, int arr_size){
DT max_so_far=arr[0];
for(int i=1;i<arr_size;i++){
if(arr[i]>max_so_far){
max_so_far=arr[i];
}
}
return max_so_far;
}
/*
* returns the maximum value in the std::vector vec
* @pram: pointer to std::vector
*/
template<typename DT>
std::vector<DT>* vector_max(std::vector<DT>* vec){
DT max_so_far= (*vec)[0];
std::vector<int>* ret_vec= new std::vector<int>();
int vec_counter=0;
for(int i=0;i<vec->size();i++){
if((*vec)[i]>max_so_far)
max_so_far=(*vec)[i];
}
for(int i=0;i<vec->size();i++){
if((*vec)[i]==max_so_far){
ret_vec->insert(ret_vec->begin()+vec_counter,i);
vec_counter++;
}
}
return ret_vec;
}
/*
* returns the mean of all the floats in an array
* @pram: pointer to array of floats
* @pram: size of array
*/
template <typename DT>
DT mean(DT *arr,int arr_size){
return sum_array(arr,arr_size)/arr_size;
}
/*
* returns the standard deviation of the values in arr
* @pram: pointer to array of floats
* @pram: size of the array
*/
template <typename DT>
DT std_dev(DT *arr, int arr_size){
DT arr_mean=mean(arr,arr_size);
DT variance=0;
for(int index=0;index<arr_size;index++){
DT hold =arr_mean-arr[index];
variance+= hold*hold;
}
variance=variance/arr_size;
return sqrtf(variance);
}
/*
* returns an array of integers which indicate
* the indices where the value val is located in
* the array
* @param: array we are traversing through
* @param: the value we are looking for
* @param: the size of the array
*/
template <typename DT>
int* find_in_arr(DT* arr,DT val, int arr_size){
int counter=0;
//count the number of times val shows up in arr
for(int i=0;i<arr_size;i++){
if(arr[i]==val)
counter++;
}
//create and return the array with indices of where
//value is located
int* ret_arr= new int[counter];
counter=0;
for(int i=0;i<arr_size;i++){
if(arr[i]==val){
ret_arr[counter]=i;
counter++;
}
}
return ret_arr;
}
/*
* multiples a std::vector by a scalar
* @pram: pointer to the array returned
* @pram: pointer to array used to represent the std::vector
* @pram: the number by which the std::vector gets scaled
*/
template <typename DT>
DT* scalar_multiplication(DT *old_row,int size, DT scaling_factor){
DT* ret_row= new DT[size];
//multiply each value in array old_row with value scaling_factor
for(int i=0;i<size;i++){
ret_row[i]=scaling_factor*old_row[i];
}
return ret_row;
}
/*
* takes an array of vertex objects vertices and an integer
* signifying a component of the graph. The function returns
* an integer array where vertices that don't belong to the
* component are masked out.
* @param: std::vector of pointers to vertex objects
* @param: the component we wish to mask
*/
std::vector<int>* component_mask(std::vector<vertex*>& vertices, int component){
std::vector<int>* comp_mask = NULL;
bool hasConnectedComponeents = false;
for(int i=0;i< vertices.size();i++)
{
vertex* curr_vertex= vertices[i];
if(curr_vertex->get_low_link()==component){
if (!hasConnectedComponeents)
{
comp_mask = new std::vector<int>(vertices.size(),0);
hasConnectedComponeents = true;
}
(*comp_mask)[i] = 1;
}
}
return comp_mask;
}
#endif