diff --git a/entropy.cc b/entropy.cc index 5c23f5d..56b4cc9 100644 --- a/entropy.cc +++ b/entropy.cc @@ -1,10 +1,41 @@ #include #include #include +#include template NumericType entropy(const std::string& fileDescriptor) { - throw std::runtime_error("Implement me!"); + + std::ifstream ReadRandomData(fileDescriptor.c_str()); + int NumberEvents = Discretization; + NumericType Events[NumberEvents]; + for(int i = 0; i < NumberEvents-1; i++) + { + ReadRandomData >> Events[i]; + } + NumericType* min_ptr = std::min_element(Events,Events+NumberEvents); + NumericType* max_ptr = std::max_element(Events,Events+NumberEvents); + + NumericType min_value = *min_ptr; + NumericType max_value = *max_ptr; + + int NumberBins = max_value - min_value + 1; + + NumericType* Histogram = new NumericType [NumberBins]; + for ( int i = 0; i < NumberEvents; i++ ) + { + int idx = (int) ((Events[i] - min_value) / (ceil((max_value - min_value) / NumberBins) )); + Histogram[ idx ] ++; + } + NumericType entropy_val = 0; + for (int i = 0; i < NumberEvents; i++) + { + NumericType prob = Histogram[i] * 1 / (float)NumberEvents; + entropy_val += -prob * log2(prob); + } + delete[] Histogram; + return entropy_val; + } int main() { @@ -12,7 +43,7 @@ int main() { *Print out the entropy of a probability distribution over 8 discrete events, with event occurrences given by a random source of information. */ - //std::cout << entropy("/dev/random") << std::endl; + std::cout << entropy("random.txt") << std::endl; /* *Note: this is a suggested function signature, feel free to implement whatever you see fit! diff --git a/matching.cc b/matching.cc index 5aa6325..ab2f6e3 100644 --- a/matching.cc +++ b/matching.cc @@ -22,17 +22,64 @@ constexpr std::array, kPatchSize> patchCandidate2 {0.03114041,0.15132543,0.60837695,0.18235618,0.74499181}, {0.19450344,0.93216069,0.5751807 ,0.38489764,0.5703268}, {0.92990664,0.22307124,0.63934838,0.38695049,0.21440734}}}; + +float CalculateMean(std::array, kPatchSize> Patch) +{ + float sum = 0; + for (int i = 0; i < kPatchSize; i++) + { + for( int j = 0; j < kPatchSize; j++) + { + sum += Patch[i][j]; + } + } + return sum / (kPatchSize*kPatchSize); +} + +float CalculateSTD(std::array, kPatchSize> Patch) +{ + float sum = 0; + float mean = CalculateMean(Patch); + for (int i = 0; i < kPatchSize; i++) + { + for( int j = 0; j < kPatchSize; j++) + { + sum += (Patch[i][j]-mean)*(Patch[i][j]-mean); + } + } + return sqrt(sum/(kPatchSize*kPatchSize)); +} + +float similarity(std::array, kPatchSize> Patch1, std::array, kPatchSize> Patch2) +{ + float sum = 0; + float mean1 = CalculateMean(Patch1); + float mean2 = CalculateMean(Patch2); + float std1 = CalculateSTD(Patch1); + float std2 = CalculateSTD(Patch2); + + for (int i = 0; i < kPatchSize; i++) + { + for( int j = 0; j < kPatchSize; j++) + { + sum += (Patch1[i][j]-mean1)*(Patch2[i][j]-mean2); + } + } + return (sum/(kPatchSize*kPatchSize*std1*std2)); +} + int main() { /* *Print out the similarity between the reference patch, and candidate 1 */ - //std::cout << similarity(referencePatch, patchCandidate1) << std::endl; + std::cout << similarity(referencePatch, patchCandidate1) << std::endl; /* *Print out the similarity between the reference patch and candidate 2 */ - //std::cout << similarity(referencePatch, patchCandidate2) << std::endl; + std::cout << similarity(referencePatch, patchCandidate2) << std::endl; /* *Note: this is a suggested function signature, feel free to implement whatever you see fit! */ + } diff --git a/modeling.cc b/modeling.cc index 2aee6fc..06d5c66 100644 --- a/modeling.cc +++ b/modeling.cc @@ -25,6 +25,25 @@ constexpr std::array, kDataPoints> points = {{{3. {3.0128605724511037, 1.8897446209038709}, {1.9357873494944466, 2.9778798335036534}}}; +void ModelFit(float coeff[]) +{ + float x = 0, y = 0, xy = 0, x2 = 0; + float mean_x = 0, mean_y = 0; + for(int i = 0; i < kDataPoints; i++) + { + x += points[i][0]; + y += points[i][1]; + xy += points[i][0] * points[i][1]; + x2 += points[i][0] * points[i][0]; + } + mean_x = x/kDataPoints; + mean_y = y/kDataPoints; + float ss_xy = xy - (kDataPoints*mean_x*mean_y); + float ss_x2 = x2 - (kDataPoints*mean_x*mean_x); + coeff[0] = ss_xy/ss_x2; + coeff[1] = mean_y - coeff[0]*mean_x; +} + int main() { /* @@ -37,4 +56,8 @@ int main() { *Note: this is a suggested function signature, feel free to implement whatever you see fit! */ + float coefficients[2]; + ModelFit(coefficients); + std::cout <<"coefficients for the model: y = C0 + C1*x"< #include #include +#include + +using namespace std; +int NumberOfPalindrome(std::string DictionaryPath) +{ + std::string test; + int count = 0; + ifstream ReadDict(DictionaryPath); + while(ReadDict >> test) + { + count += std::equal(test.begin(), test.begin() + test.size() / 2, test.rbegin()); + } + return count; + ReadDict.close(); +} int main() { /* @@ -20,4 +35,7 @@ int main() { *Note: this is a suggested function signature, feel free to implement whatever you see fit! */ + std::string path = "words.txt"; + int PalindromeCount = NumberOfPalindrome(path); + cout << "The number of palindromes in the dictionary are :"<