Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions entropy.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>

template <typename NumericType, int Discretization>
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() {
/*
*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<float, 8>("/dev/random") << std::endl;
std::cout << entropy<float, 8>("random.txt") << std::endl;
/*
*Note: this is a suggested function signature, feel free to implement
whatever you see fit!
Expand Down
51 changes: 49 additions & 2 deletions matching.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,64 @@ constexpr std::array<std::array<float, kPatchSize>, 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<std::array<float, kPatchSize>, 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<std::array<float, kPatchSize>, 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<std::array<float, kPatchSize>, kPatchSize> Patch1, std::array<std::array<float, kPatchSize>, 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!
*/

}
23 changes: 23 additions & 0 deletions modeling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ constexpr std::array<std::array<float, kDimensions>, 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() {
/*
Expand All @@ -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"<<std::endl;
std::cout <<"coefficient C0:"<<coefficients[1]<<"\n"<<"coefficient C1:"<<coefficients[0]<<std::endl;
}
9 changes: 9 additions & 0 deletions random.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1
2
3
4
5
6
7
8

18 changes: 18 additions & 0 deletions strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
#include <fstream>
#include <vector>
#include <assert.h>
#include <string>

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() {
/*
Expand All @@ -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 :"<<PalindromeCount<<endl;
}
7 changes: 7 additions & 0 deletions words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
malayalam
noon
relax
very
cop
pop