Increase number of columns filled in as row number increases - c++

I am writing a program to fill in a matrix (I use the dlib library but not relevant to the question. In the following my goal is for rows 1-19 (row indices 0-18) for one additional column to fill in with my formula. For example row 1 has the first column filled in, row 2 has the first two columns filled in. The first column for every row is preset to my initial value as required. What can I do to the nested for loops indicated by comment my lmm() function to get my desired output?
#include <random>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath>
#include <limits>
#include <cstdlib>
#include <chrono>
#include <iterator>
#include <algorithm>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/kurtosis.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/skewness.hpp>
#include <dlib/optimization.h>
#include <dlib/matrix.h>
#include "mleFunctor.h"
#include "mleDerFunctor.h"
using namespace boost::accumulators;
using namespace dlib;
//Generate Gaussian via Box-Muller from Mark Joshi's C++ Design Patterns and Derivatives Pricing
double GetOneGaussianByBoxMuller()
{
double result;
double x;
double y;
double sizeSquared;
do
{
x = 2.0*std::rand() / static_cast<double>(RAND_MAX) - 1;
y = 2.0*std::rand() / static_cast<double>(RAND_MAX) - 1;
sizeSquared = x*x + y*y;
} while (sizeSquared >= 1.0);
result = x*sqrt(-2 * log(sizeSquared) / sizeSquared);
return result;
}
double libSum(matrix<double,20,20> v, matrix<double, 20, 20> lib, int r,int c , double d, int index,std::vector<double> W)
{
double sum = 0.0;
for (auto k = index + 1; k < lib.nr()-1; ++k)
{
sum += ((d*v(k,c-1)*lib(k,c-1))/(1+d*lib(k,c-1)))*v(k,c-1) * lib(r, c-1)*(W[c] - W[c-1]);
}
return sum;
}
void lmm()
{
double dt = .25;
std::vector<double> W(20);
std::vector<double> T;
matrix<double, 20, 20> L;
W[0] = 0;
for (auto c = 1; c < W.size(); ++c)
{
W[c] = W[c - 1] + sqrt(dt)*GetOneGaussianByBoxMuller();
}
for (auto i = 0; i < 20; ++i)
{
T.push_back(i*.25);
}
set_all_elements(L, 0);
set_colm(L, 0) = .003641; //3M Libor Rate on November 16,2015
matrix<double,20,20> vol;
set_all_elements(vol,.15);
//Loop that should fill in one more column each (ie 0 indexed row has one column filled in,
//row index 1 should have 2 columns filled in etc
for (auto c = 1; c < L.nc(); ++c)
{
for (auto r = 1; r < c; ++r)
{
L(r, c) = L(r, c-1) + libSum(vol, L,r,c, .25, c,W) + vol(r,c-1) * L(r, c-1 )*(W[c] - W[c-1]);
}
}
std::ofstream outfile("LMMFlatVol.csv");
outfile << L << std :: endl;
}
int main()
{
lmm();
return 0;
}
As of right now my output is just the preset first columns with the rest of the matrix zeroes as I initialized.

Your primary problem seems to be your loop condition. Note that the inner for loop should start at the first active row. For example, in the third column (c==2) the first time you calculate a value is row three (r==2). After that all rows until the bottom of the matrix have a calculated value.
Your previous logic did not even calculate any value for the second column (c==1) because r was set to 1 and (r < c) evaluated to false!
for (auto c = 1; c < L.nc(); ++c)
{
for (auto r = c; r < L.nc(); ++r)
{
L(r, c) = L(r, c-1) + libSum(vol, L,r,c, .25, c,W) + vol(r,c-1) * L(r, c-1 )*(W[c] - W[c-1]);
}
}

Related

why does my code get segment fault on SPOJ for Sum of Digits problem? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
When I submitted the solution of a dp problem on spoj for this problem I always get a segment fault. But my solution works on other platforms like visual studio and Ideone.
I do not know why I am getting this error, Can you help?
My code:
#include <iostream>
#include <cmath>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <vector>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <set>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <numeric>
#include <array>
#include <tuple>
#include <stdexcept>
#include <utility>
#include <functional>
#include <locale>
#define mp make_pair
#define pb push_back
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define sz size()
#define len length()
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define all(v) ((v).begin()), ((v).end())
#define mms(Arr, Value) memset(Arr, Value, sizeof(Arr))
#define printl(ans) cout << ans << endl
#define vpii vector<pair<int, int> >
#define vpll vector<pair<ll, ll> >
#define pll pair<ll, ll>
#define re return
#define fri(x,n) for(int i = x ; i < n ; ++i)
#define frj(x,n) for(int j = x ; j < n ; ++j)
typedef long long int ll;
const int oo = INT_MAX;
const ll OO = 1e18;
using namespace std;
ll GCD(ll a, ll b) { return((!b) ? a : GCD(b, a % b)); }
ll LCM(ll a, ll b) { return a / (GCD(a, b)) * b; }
bool isPrime(ll n) {
if (n == 2)re 1;
if (n < 2 || n % 2 == 0)re 0;
for (ll i = 3; i * i <= n; i += 2)
if (n % i == 0)re 0;
re 1;
}
// take it as string
string a, b;
int ar[11];
ll dp[11][100][2]; // if max input is 1e9, so max pos is 10 and max sum of a one number is 9*9.. did not put a dimension for n as it is constant for all states
ll fun(int pos, ll sum, int flag, int n) {
if (pos > n) re dp[pos][sum][flag] = sum;
if (dp[pos][sum][flag] != -1) re dp[pos][sum][flag];
// if flag is 0 then this state is limited by ar[pos] value.
int limit = 9;
if (flag == 0) limit = ar[pos];
// determine next state: put next flag not limited (1) when curr flag is not limited (1) OR i is still under (smaller than) limit
// put next flag limited (0) when curr flag is limited AND i equals the limit .. you can NOT put OR as : the flag of curr state may be limited but the next state
// would be limited only if i==limit, as if i<limit the next state is always free whether flag is 1 or 0.
// if i==limit, the next state would only be limited if flag is 0. as if curr flag was free so limit of curr state was 9 and now i is 9, the next state can not be limited because flag is 1 even if i==limit.. so you must put them both !flag , i==limit and you must put AND
ll res = 0;
fri(0, limit + 1) {
if (!flag && i == limit)
res += fun(pos + 1, sum + i, 0, n); // limited
else
res += fun(pos + 1, sum + i, 1, n); // free
}
re dp[pos][sum][flag] = res;
}
int NumDigitSum(string s) {
// takes the num as string and return the sum of its digits
int sum = 0;
fri(0, s.sz) {
sum += s[i] - '0';
}
re sum;
}
int main() {
IO;
cin >> a >> b;
while (a != "-1") {
mms(dp, -1);
// ar is one indexed
fri(1, a.sz + 1) {
ar[i] = a[i - 1] - '0'; // convert to int
}
ll aans = fun(1, 0, 0, a.sz);
mms(dp, -1);
// ar is one indexed
fri(1, b.sz + 1) {
ar[i] = b[i - 1] - '0'; // convert to int
}
ll bans = fun(1, 0, 0, b.sz);
cout << bans - aans + NumDigitSum(a) << endl;
cin >> a >> b;
}
return 0;
}
Well, this kind of spoonfeeding is pretty discouraged, but here I go:
#include <iostream>
#include <string>
//number of headers = 3
//no use of using namespace std;
int main() {
int n = 100;
long sum = 0;
for (int i = 1; i <= n; i++) {
std::string num_as_string = std::to_string(i);
for(const auto& digit_as_char : num_as_string) {
sum = sum + digit_as_char - '0';
}
}
std::cout << sum;
return 0;
}
Notice some things in the code:
It is short and concise
It uses the standard string library to separate digits which is way better than a custom logic
It is very much readable and does not get on anyone's nerves

define and filling a sparse matrix using Eigen Library in C++

I am trying to build a spars Matrix using a Eigen or Armadillo library in C++ to solve a system of linear equations Ax=b. A is the coefficient matrix with a dimension of n*n, and B is a vector of right hand side with a dimension of n
the Spars Matrix A is like this, see the figure
I had a look though the Eigen document but I have a problem with defining and filling the Spars Matrix in C++.
could you please give me an example code to define the spars matrix and how to fill the values into the matrix using Eigen library in c++?
consider for example a simple spars matrix A:
1 2 0 0
0 3 0 0
0 0 4 5
0 0 6 7
int main()
{
SparseMatrix<double> A;
// fill the A matrix ????
VectorXd b, x;
SparseCholesky<SparseMatrix<double> > solver;
solver.compute(A);
x = solver.solve(b);
return 0;
}
The sparse matrix could be filled with the values mentioned in the post by using the .coeffRef() member function, as shown in this routine:
SparseMatrix<double> fillMatrix() {
int N = 4;
int M = 4;
SparseMatrix<double> m1(N,M);
m1.reserve(VectorXi::Constant(M, 4)); // 4: estimated number of non-zero enties per column
m1.coeffRef(0,0) = 1;
m1.coeffRef(0,1) = 2.;
m1.coeffRef(1,1) = 3.;
m1.coeffRef(2,2) = 4.;
m1.coeffRef(2,3) = 5.;
m1.coeffRef(3,2) = 6.;
m1.coeffRef(3,3) = 7.;
m1.makeCompressed();
return m1;
}
However, the SparseCholesky module (SimplicialCholesky<SparseMatrix<double> >) won't work in this case because the matrix is not Hermitian. The system could be solved with a LU or BiCGStab solver. Also note that sizes ofx and b need to be defined:
VectorXd b(A.rows()), x(A.cols());
In case of larger sparse matrices you may also want to look at the .reserve() function in order to allocate memory before filling the elements. The .reserve() function can be used to provide an estimate of the number of non-zero entries per column (or row, depending on the storage order. The default is comumn-major). In the example above that estimate is 4, but it does not make sense in such a small matrix. The documentation states that it is preferable to overestimate the number of non-zeros per column.
Since this question also asks about Armadillo, here is the corresponding Armadillo-based code. Best to use Armadillo version 9.100+ or later, and link with SuperLU.
#include <armadillo>
using namespace arma;
int main()
{
sp_mat A(4,4); // don't need to explicitly reserve the number of non-zeros
// fill with direct element access
A(0,0) = 1.0;
A(0,1) = 2.0;
A(1,1) = 3.0;
A(2,2) = 4.0;
A(2,3) = 5.0;
A(3,2) = 6.0;
A(3,3) = 7.0; // etc
// or load the sparse matrix from a text file with the data stored in coord format
sp_mat AA;
AA.load("my_sparse_matrix.txt", coord_ascii)
vec b; // ... fill b here ...
vec x = spsolve(A,b); // solve sparse system
return 0;
}
See also the documentation for SpMat, element access, .load(), spsolve().
The coord file format is simple. It stores non-zeros values.
Each line contains:
row col value
The row and column counts start at zero. Example:
0 0 1.0
0 1 2.0
1 1 3.0
2 2 4.0
2 3 5.0
3 2 6.0
3 3 7.0
1000 2000 9.0
Values not explicitly listed are assumed to be zero.
#include <vector>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <Eigen/Core>
#include <cstdlib>
using namespace Eigen;
using namespace std;
int main()
{
double L = 5; // Length
const int N = 120; // No of cells
double L_cell = L / N;
double k = 100; // Thermal Conductivity
double T_A = 100.;
double T_B = 200.;
double S = 1000.;
Vector<double, N> d, D, A, aL, aR, aP, S_u, S_p;
vector<double> xp;
xp.push_back((0 + L_cell) / 2.0);
double xm = xp[0];
for (int i = 0; i < N - 1; i++)
{
xm = xm + L_cell;
xp.push_back(xm);
}
for (int i = 0; i < N; i++)
{
A(i) = .1;
d(i) = L_cell;
D(i) = k / d(i);
}
aL(0) = 0;
aR(0) = D(0) * A(0);
S_p(0) = -2 * D(0) * A(0);
aP(0) = aL(0) + aR(0) - S_p(0);
S_u(0) = 2 * D(0) * A(0) * T_A + S * L_cell * A(0);
for (int i = 1; i < N - 1; i++)
{
aL(i) = D(i) * A(i);
aR(i) = D(i) * A(i);
S_p(i) = 0;
aP(i) = aL(i) + aR(i) - S_p(i);
S_u(i) = S * A(i) * L_cell;
}
aL(N - 1) = D(N - 1) * A(N - 1);
aR(N - 1) = 0;
S_p(N - 1) = -2 * D(N - 1) * A(N - 1);
aP(N - 1) = aL(N - 1) + aR(N - 1) - S_p(N - 1);
S_u(N - 1) = 2 * D(N - 1) * A(N - 1) * T_B + S * L_cell * A(N - 1);
typedef Eigen::Triplet<double> T;
std::vector<T> tripletList;
tripletList.reserve(N * 3);
Matrix<double, N, 3> v; // v is declared here
v << (-1) * aL, aP, (-1) * aR;
for (int i = 0, j = 0; i < N && j < N; i++, j++)
{
tripletList.push_back(T(i, j, v(i, 1)));
if (i + 1 < N && j + 1 < N)
{
tripletList.push_back(T(i + 1, j, v(i + 1, 0)));
tripletList.push_back(T(i, j + 1, v(i, 2)));
}
}
SparseMatrix<double> coeff(N, N);
coeff.setFromTriplets(tripletList.begin(), tripletList.end());
SimplicialLDLT<SparseMatrix<double> > solver;
solver.compute(coeff);
if (solver.info() != Success) {
cout << "decomposition failed" << endl;
return;
}
Vector<double, N> temparature;
temparature = solver.solve(S_u);
if (solver.info() != Success)
{
cout << "Solving failed" << endl;
return;
}
vector<double> Te = {}, x = {};
Te.push_back(T_A);
x.push_back(0);
for (int i = 0; i < N; i++)
{
Te.push_back(temparature(i));
x.push_back(xp[i]);
}
Te.push_back(T_B);
x.push_back(L);
for (int i = 0; i < N + 2; i++)
{
cout << x[i] << " " << Te[i] << endl;
}
return 0;
}
Here is a full code of a solution to numerical problem which uses SparseMatrix. Look at the matrix v. It has the values of all the nonzero elements of coeff matrix yet to be defined. In the next loop I made a series of tripletList.push_back(...) adding a triplet consisting of row and column index and corresponding value taken from v for each non-zero element of coeff. Now declare a Sparse Matrix coeff with appropriate size and use the method setFromTriplets (documentation) to set its non-zero elements from tripletList triplets.

A really weired issue in (c++) code

I have defined a variable in my code:
double R0;
When I set the variable less than 0.9, the code doesn’t run without no error! I have also written cout<<2; exactly after main(){ but the program doesn’t even show that! I am very confused :( what is the problem? When I change R0 to 0.9 or bigger than it, the code runs. This is the most minimal example I could provide:
#include <iostream>
#include <math.h>
#include <vector>
#include <functional>
#include <string>
#include <sstream>
using namespace std;
vector<double> getBoxCoords(int boxID, double L, double R0, int nbox)
{
vector<double> coords(4);
int yID = ceil((double)(boxID+1)/nbox);
int xID = fmod((boxID+1-(yID-1)*nbox), nbox);
if(xID==0)
xID = nbox;
coords[0] = (xID-1) * R0; // minX
coords[1] = (yID-1) * R0; // minY
coords[2] = min(xID * R0, L); // maxX
coords[3] = min(yID * R0, L); // maxY
return coords;
}
double PBC(double pos, double L)
{
if(fabs(pos) > L / 2.0)
return L-fabs(pos);
else
return fabs(pos);
}
int main()
{
std::cout << 2;
int N=100;
double rho=4.0, v0=0.03, eta=0.2, R0=0.03;
double L = pow(N/rho,0.5);
int nbox = (int)ceil(L/R0);
vector<vector<int>> box_neighbors;
vector<int> indices;
for(int i = 0; i < nbox * nbox; i++) //NUMBER OF BOX
{
vector<double> ci = getBoxCoords(i, L, R0, nbox);
indices.clear();
for(int j = 0; j < nbox * nbox; j++)
{
vector<double> cj = getBoxCoords(j, L, R0, nbox);
bool xflag=false,
yflag=false;
if (PBC(ci[0]-cj[0],L)<R0 || PBC(ci[0]-cj[2],L)<R0 || PBC(ci[2]-cj[0],L)<R0 || PBC(ci[2]-cj[2],L)<R0)
xflag=true;
if (PBC(ci[1]-cj[1],L)<R0 || PBC(ci[1]-cj[3],L)<R0 || PBC(ci[3]-cj[1],L)<R0 || PBC(ci[3]-cj[3],L)<R0)
yflag=true;
if(xflag && yflag)
indices.push_back(j);
}
box_neighbors.push_back(indices);
}
return 0;
}
How can I remove the problem? Could anyone provide a runnable answer?
First thing first, the debug std::cout << 2; is now shown because you do not end the stream, proper way of doing it is
std::cout << 2 << std::endl;
then you will be able to see the debugging message.
Secondly, Your program runs, but takes too much time to finish, when R0 is small. For the given value, that is, 0.03, both layers of the loop will execute nbox * nbox times which is 27889, thus, 777796321 in total.

Image processing: luminance weighted

I would like to weigh values of luminance.
Example:
I have a vector of luminance values:
vector <int> lum {50,100,150,200,250);
I have a vector of coefficients:
vector <float> coef {5.1 , 2.55 , 1.7 , 1.275, 1.02 }
I create an empty image:
Mat1 m(15):
So, my first value of luminance is 50 (lum[0]=50) and I want it to be applied on the 5.1 (coef[0]=5.1) first pixel of my matrix. To do that, I need to weight the 6th pixel with the first and the second value of luminance. In my case, the luminance of my 6th pixel will be 95 because (0.1*50)+ (0.9*100)=95
At the moment, for the second coefficient (coef[1]=2.55), I have used 0.9 on 2.55 for the previous calcul. It remains 1,65 on this coefficient so the 7th pixel will have 100 of luminance and the 8th will have (0.65*100)+ (0.35*150) = 117,5.
And so on...
Actually I have this:
//Blibliothèque Opencv
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
// cpp include
#include <iostream>
#include <cmath>
#include <math.h>
#include <string.h>
#include <vector>
#include <opencv2/opencv.hpp>
#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)
using namespace cv;
using namespace std;
vector <int> lum{ 50, 100, 150, 200, 250 };
vector <float> coef (5,0);
vector <int> newv(15, 0);
float pixelRef = 255, angle = 0, coeff = 0;
int main()
{
for (int n = 0; n < lum.size(); ++n)
{
//get angle
angle = ((acos(lum[n] / pixelRef)));
cout << "angle :" << angle*RAD2DEG << endl;
// get coefficient
coef [n] = (1 / (cos(angle)));
cout << "coeff :" << coef [n] << endl;
// try to weighted my pixels
newv[n] = (coef*lum[n]) + ((1 - coeff)*lum[n + 1]);
}
return 0;
}
I modified the last element of coef to 3.02f to show that this code handles well also the last element. The result sequence is:
50, 50, 50, 50, 50, 95, 100, 117.5, 150, 182.5, 218.75, 250, 250,
The code could be probably re-written better, but I'll leave this to you:
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
vector <int> lum{ 50, 100, 150, 200, 250 };
vector <float> coef{ 5.1f, 2.55f, 1.7f, 1.275f, 3.02f };
vector<float> v;
int idx_lum = 0;
int idx_coef = 0;
while (true)
{
int c = int(coef[idx_coef]);
for (int i = 0; i < c; ++i)
{
v.push_back(float(lum[idx_lum]));
}
float alpha = fmod(coef[idx_coef], 1.f);
float beta = 1.f - alpha;
v.push_back(alpha * lum[idx_lum] + beta * lum[idx_lum + 1]);
idx_lum++;
idx_coef++;
coef[idx_coef] = coef[idx_coef] - beta;
if (idx_lum >= lum.size() - 1 || idx_coef >= coef.size() - 1)
{
int cc = int(coef[idx_coef]);
for (int i = 0; i < cc; ++i)
{
v.push_back(float(lum[idx_lum]));
}
// Only if the last remainder is needed
//float alpha = fmod(coef[idx_coef], 1.f);
//v.push_back(alpha * lum[idx_lum]);
break;
}
}
// Print out the values
copy(v.begin(), v.end(), ostream_iterator<float>(cout, ", "));
// Get a cv::Mat from the std::vector
Mat1f m = Mat1f(v).t();
return 0;
}

Reading 2 CSV files and using vectors to store the values and then calculate the coefficient. Returns -1.#IND

This is my code I have, which when I build it works and creates the .exe file, however throughout the process I print the function (i.e. mean, covarience, coefficient) and they all come back as -1#IND. Think it may not be pulling in the data from the CSV files correctly?
// basic file operations
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
typedef vector<double> Prices;
Prices parse_csv_line(string& line)
{
Prices result;
string datum;
stringstream ss(line);
int count=0;
while(getline(ss,datum,','))
{
// convert string to
count++;
if (count%2 == 0)
result.push_back(atof(datum.c_str()));
}
return result;
}
Prices parse_csv_file(const char* filename)
{
ifstream file(filename);
Prices prices;
string line;
// This will discard the header line
getline(file, line);
// This will get each line in the file, and collate its values
while (getline(file, line))
{
Prices v = parse_csv_line(line);
prices.insert(prices.end(), v.begin(), v.end());
}
for(Prices::iterator it=prices.begin(); it != prices.end(); it++)
cout << " " << *it;
return prices;
}
//Calculate Correlation of series A and B, then return
/* Calculatethe mean averages for A and B.
(For each series, add each sample and then divide by the number of samples.) */
double CalculateMean(Prices x)
{
double sum = 0;
for(size_t i = 0; i < x.size(); i++)
sum += x[i];
return (sum / x.size());
}
/* Calculate the variance for A and B.
(First calculate the difference from the mean for each sample number. Square each number then divide by the number of samples (n).
If the numbers you are calculating represent a sample of a larger group, then you would divide by n – 1.) */
double CalculateVariance(Prices x)
{
double mean = CalculateMean(x);
double temp = 0;
for(size_t i = 0; i < x.size(); i++)
{
temp += (x[i] - mean) * (x[i] - mean) ;
}
return temp / x.size();
}
/* calculate the standard deviation for A and B, which is the square root of the variance.
(This number will tell you how closely your samples are located to the mean.) */
double Calculate_StandardDeviation(Prices x)
{
return sqrt(CalculateVariance(x));
}
/* Lastly, calculate the Covariance of the 2 series.
(This value can be used to represent the linear relationship between two variables.) */
double Calculate_Covariance(Prices x, Prices y)
{
double meanX = CalculateMean(x);
double meanY = CalculateMean(y);
cout << "mean x = " << meanX << "\n";
cout << "mean y = " << meanY << "\n";
double total = 0;
for(size_t i = 0; i < x.size(); i++)
{
total += (x[i] - meanX) * (y[i] - meanY);
}
return total / x.size();
}
// Using the calculated values, these can then be inputted into the Correlation Coefficient formula to find the correlation of series A and B.
double Calculate_Correlation(Prices x, Prices y)
{
double covariance = Calculate_Covariance(x, y);
cout << "covariance =" << covariance << "\n";
double correlation = covariance / (Calculate_StandardDeviation(x) * Calculate_StandardDeviation(y));
return correlation;
};
int main()
{
Prices a = parse_csv_file("PC1_A.CSV");
Prices b = parse_csv_file("PC1_B.CSV");
double correlation = Calculate_Correlation(a, b);
cout << "Correlation is: " << correlation;
cin.get();
}