void plotInterp(const Vec_DP &xdata, const Vec_DP &ydata, const Vec_DP &interpx, const Vec_DP &interpy) { int pointnum = 100; double start = xdata[0]; double end = xdata[xdata.size() - 1]; myplot::data_set container; myplot::plot_data realPoints("k+2"); myplot::plot_data interpPoints("r."); myplot::plot_data curve("c-2"); for (int i = 0; i < pointnum; i++) { double x = start + ((double) i * (end - start)) / (pointnum - 1); double y, dy; NR::polint(xdata, ydata, x, y, dy); curve.add_point(x, y); } for (int i = 0; i < xdata.size(); i++) { realPoints.add_point(xdata[i], ydata[i]); } for (int i = 0; i < interpx.size(); i++) { interpPoints.add_point(interpx[i], interpy[i]); } container.push_back(curve); container.push_back(realPoints); container.push_back(interpPoints); myplot::plot(container); }
void generateNoisyData(Vec_DP& xdata, Vec_DP& ydata, Vec_DP& coeffs, double low, double high){ for (int i = 0; i < coeffs.size(); i++) { coeffs[i] = rdm(low, high); } for (int i = 0; i < xdata.size(); i++) { ydata[i] = (coeffs[0]*pow(xdata[i],2) + coeffs[1]*xdata[i] + coeffs[2]) * rdm(0.8, 1.2); } }
Vec_DP operator-(Vec_DP const& a, Vec_DP const& b) { if (a.size() != b.size()) throw runtime_error("Dimensions mismatch in vector subtraction!"); Vec_DP x(a.size()); for (int i = 0; i < a.size(); i++) { x[i] = a[i] - b[i]; } return x; }
void spline_interp(Vec_DP const& xdata, Vec_DP const& ydata, double yp1, double ypn, Vec_DP const& x, Vec_DP& y) { Vec_DP y2(xdata.size()); NR::spline(xdata, ydata, yp1, ypn, y2); for (int i = 0; i < x.size(); i++) { NR::splint(xdata, ydata, y2, x[i], y[i]); } double err, errx; get_error(x, y, err, errx); cout << "Maximum error in spline interpolation: " << err << ", at x = " << errx << endl; }
void poly_interp(Vec_DP const& xdata, Vec_DP const& ydata, Vec_DP const& x, Vec_DP& y) { Vec_DP c(xdata.size()); NR::polcoe(xdata, ydata, c); for (int i = 0; i < x.size(); i++) { y[i] = poly_val(x[i], c); } double err, errx; get_error(x, y, err, errx); cout << "Maximum error in polynomial interpolation: " << err << ", at x = " << errx << endl; }
double interp_spline_periodic(const Vec_DP &xdata, const Vec_DP &ydata, double deriv, Vec_DP &interpx, Vec_DP &interpy, Vec_DP &error) { Vec_DP y2(xdata.size()); NR::spline(xdata, ydata, deriv, deriv, y2); for (int i = 0; i < interpx.size(); i++) { double y, x = i * (M_PI / 200); NR::splint(xdata, ydata, y2, x, y); interpx[i] = x; interpy[i] = y; error[i] = abs(sin(x) - y); } return get_max_error(error); }
void solve_system(Vec_DP xdata, Vec_DP ydata, Vec_DP zdata){ Mat_DP A(xdata.size(), 3); Vec_DP b(xdata.size()); for (int i = 0; i < xdata.size(); i++) { double x = xdata[i]; double y = ydata[i]; A[i][0] = x; A[i][1] = y; A[i][2] = 1; b[i] = zdata[i]; } SVDsolve(A, b, coeffs); }
void construct_data(Vec_DP &xdata, Vec_DP &ydata){ for (int i = 0; i < xdata.size(); i++) { double x = i * (M_PI / 10); xdata[i] = x; ydata[i] = sin(x); } }
double get_max_error(const Vec_DP &error){ double max = 0; for (int i = 0; i < error.size(); i++) { max = (error[i] > max) ? error[i] : max; } return max; }
double poly_val(double x, Vec_DP const& c) { double y = c[0]; for (int n = 1; n < c.size(); n++) { y += c[n] * pow(x, n); } return y; }
Vec_DP subst(Vec_DP const& w) { if (w.size() != 2) throw runtime_error("Wrong dimension for input in subst!"); Vec_DP x(2); x[0] = -2 + 2.8 * pow(sin(w[0]), 2); x[1] = w[1]; return x; }
void constructData_B(Vec_DP &xdata, Vec_DP &ydata) { double ylow = -10; double yhigh = 10; for (int i = 0; i < xdata.size(); i++) { xdata[i] = i + 1; ydata[i] = rdm(ylow, yhigh); } }
void interp(const Vec_DP &xdata, const Vec_DP &ydata, Vec_DP &interpx, Vec_DP &interpy, Vec_DP &error) { for (int i = 0; i < interpx.size(); i++) { double y, errory, x = 2 + i * 0.25; NR::polint(xdata, ydata, x, y, errory); interpx[i] = x; interpy[i] = y; error[i] = errory; } }
void GSsolve_iter(Mat_DP &a, Vec_DP &b, Vec_DP &x, Vec_DP &xold) { int i, j, rows = a.nrows(); if ((rows != a.ncols()) || (rows != b.size()) || (rows != x.size()) || (rows != xold.size())) { cout << "Argument error in GaussSeidel_iter\n" << endl; abort(); } double s; for (i = 0; i < rows; i++) { s = 0; for (j = 0; j < rows; j++) { s += (j <= i - 1) ? a[i][j] * x[j] : (j >= i + 1) ? a[i][j] * xold[j] : 0; } xold[i] = x[i]; x[i] = (b[i] - s) / a[i][i]; } }
double interp_polint(const Vec_DP &xdata, const Vec_DP &ydata, Vec_DP &interpx, Vec_DP &interpy, Vec_DP &error) { for (int i = 0; i < interpx.size(); i++) { double y, errory, x = i * (M_PI / 200); NR::polint(xdata, ydata, x, y, errory); interpx[i] = x; interpy[i] = y; error[i] = abs(sin(x) - y); } return get_max_error(error); }
void MATHEMATICS::math_deriv(Vec_DP &v1, Vec_DP v0, Vec_DP v2, double t0, double t2) { int n=v1.size(); for (int i=0; i<n; i++) { v1[i]=(v2[i]-v0[i])/(t2-t0); } }
void get_error(Vec_DP const& x, Vec_DP y, double& maxerr, double& errx) { maxerr = -INFINITY; double yy, err; for (int i = 0; i < x.size(); i++) { yy = sin(x[i]); err = abs(y[i] - yy); if (err > maxerr) { maxerr = err; errx = x[i]; } } }
double MATHEMATICS::math_vecmag(Vec_DP a) { double c=0; int x=a.size(); for(int i=0; i<x; i++) { c+=a[i]*a[i]; } c=sqrt(c); return c; }
void construct_A(Mat_DP& A, Vec_DP& xdata){ Vec_DP sums(4); for (int i = 0; i < sums.size(); i++) { sums[i] = 0; } for (int i = 0; i < xdata.size(); i++) { sums[0] += xdata[i]; sums[1] += pow(xdata[i], 2); sums[2] += pow(xdata[i], 3); sums[3] += pow(xdata[i], 4); } A[0][0] = sums[3]; A[0][1] = sums[2]; A[0][2] = sums[1]; A[1][0] = sums[2]; A[1][1] = sums[1]; A[1][2] = sums[0]; A[2][0] = sums[1]; A[2][1] = sums[0]; A[2][2] = xdata.size(); }
void construct_b(Vec_DP& b, Vec_DP& xdata, Vec_DP& ydata){ Vec_DP sums(3); for (int i = 0; i < sums.size(); i++) { sums[i] = 0; } for (int i = 0; i < xdata.size(); i++) { sums[0] += ydata[i] * pow(xdata[i], 2); sums[1] += ydata[i] * xdata[i]; sums[2] += ydata[i]; } b[0] = sums[0]; b[1] = sums[1]; b[2] = sums[2]; }
void plotInterp(const Vec_DP &xdata, const Vec_DP &ydata, const Vec_DP &interpx, const Vec_DP &interpy) { int pointnum = 100; double start = xdata[0]; double end = xdata[xdata.size() - 1]; myplot::data_set container; myplot::plot_data realPoints("k+2"); myplot::plot_data interp_curve("c"); myplot::plot_data real_curve("b"); for (int i = 0; i < interpx.size(); i++) { interp_curve.add_point(interpx[i], interpy[i]); } for (int i = 0; i < interpx.size(); i++) { real_curve.add_point(interpx[i], sin(interpx[i])); } for (int i = 0; i < xdata.size(); i++) { realPoints.add_point(xdata[i], ydata[i]); } container.push_back(real_curve); container.push_back(interp_curve); container.push_back(realPoints); myplot::plot(container); }
void int_matrix(Vec_DP &k, double &mh, double &me, Vec_DP &nh, Vec_DP &ne, double &conc, double &Tempr, double &eps, int ind, string gway, Mat_DP &V){ //----------------Constantes------------------------------ //double h=1.05e-34; // Planck constant double e=1.602e-19; // elementary charge //double m0=9.1e-31; // electron mass double pi=3.14; // pi double eps0=8.85e-12; // dielectric constant complex<double> ij(0,1); // imaginary unit //----------------Material parameters--------------------- double epss; // dielectric constant int j1; int j2; int j; int l_k=k.size(); //length of k array Vec_DP k1(k); // k-space (2nd particle) double phi[10002]; double q; //----------------Definition of constants----------------- if (ind==11){ Vec_DP p(9); std::stringstream sstm; sstm << gway<<"f_factor.dat"; //valence band data reading string flnm=sstm.str(); std::ifstream in(flnm.c_str(),std::ios::out); if(!in){ std::cout<<"Could not open file"<<sstm.str()<<std::endl; exit(0); } j=0; while (!in.eof() && j<l_k) { in>>p[j]; j++; } in.close(); }
Vec_DP MATHEMATICS::math_qnormalise(Vec_DP a) { int x=a.size(); double norm; Vec_DP b(4); for (int i=0; i<4; i++) {b[i]=0;} if(x!=4) { cout << "Quaternion dimensions are invalid for making it a unit quaternion"; return b; } else { norm=MATHEMATICS::math_qnorm(a); for (int i=0; i<4; i++) { b[i]=a[i]/norm; } return b; } }
void plotFitted(const Vec_DP &xdata, const Vec_DP &ydata, const Vec_DP &coeffs){ int start = 0; int end = 5; int pointnum = 100; myplot::data_set dataContainer; myplot::plot_data poly("c-2"); myplot::plot_data points("k+2"); for (int i = 0; i < pointnum; i++) { double x = start + ((double) i * (end - start) / (pointnum - 1)); poly.add_point(x, coeffs[0]*pow(x,2) + coeffs[1]*x + coeffs[2]); } for (int i = 0; i < xdata.size(); i++) { points.add_point(xdata[i], ydata[i]); } dataContainer.push_back(poly); dataContainer.push_back(points); plot(dataContainer); }
Vec_DP MATHEMATICS::math_mxmply(Mat_DP a, Vec_DP b) { int x=a.nrows(), y=b.size(); double initial=0; Vec_DP c(initial, y); if(x!=y) { cout << "Matrix dimensions are invalid for multiplication"; return c; } else { int i, j; for(i=0; i<x; i++) { for(j=0; j<y; j++) { c[i]+=a[i][j]*b[j]; } } return c; } }
double g(Vec_DP const& x) { if (x.size() != 2) throw runtime_error("Wrong dimension for input in function g!"); Vec_DP w = subst(x); return f(w); }
void ones(Vec_DP& v) { for (int i = 0; i < v.size(); i++) { v[i] = 1; } }
void print_results(string vecname, const Vec_DP &vec){ cout << vecname << " = " << endl; for (int i = 0; i < vec.size(); i++) { cout << vec[i] << endl; } }
void printResults(const Vec_DP &interpx, const Vec_DP &interpy, const Vec_DP &error){ printf("x\tf(x)\terror\n"); for (int i = 0; i < interpx.size(); i++) { printf("%-8.2f%-8.2f%-8.2f\n", interpx[i], interpy[i], error[i]); } }