Vector<Pointf> DataSource::FFT(Getdatafun getdata, double tSample, bool frequency, int type, bool window) { int numData = int(GetCount()); VectorXd timebuf(numData); int num = 0; for (int i = 0; i < numData; ++i) { double data = Membercall(getdata)(i); if (!IsNull(data)) { timebuf[i] = Membercall(getdata)(i); num++; } } Vector<Pointf> res; if (num < 3) return res; timebuf.resize(num); double windowSum = 0; if (window) { for (int i = 0; i < numData; ++i) { double windowDat = 0.54 - 0.46*cos(2*M_PI*i/numData); windowSum += windowDat; timebuf[i] *= windowDat; // Hamming window } } else windowSum = numData; VectorXcd freqbuf; try { Eigen::FFT<double> fft; fft.SetFlag(fft.HalfSpectrum); fft.fwd(freqbuf, timebuf); } catch(...) { return res; } if (frequency) { for (int i = 0; i < int(freqbuf.size()); ++i) { double xdata = i/(tSample*numData); switch (type) { case T_PHASE: res << Pointf(xdata, std::arg(freqbuf[i])); break; case T_FFT: res << Pointf(xdata, 2*std::abs(freqbuf[i])/windowSum); break; case T_PSD: res << Pointf(xdata, 2*sqr(std::abs(freqbuf[i]))/(windowSum/tSample)); } } } else { for (int i = int(freqbuf.size()) - 1; i > 0; --i) { double xdata = (tSample*numData)/i; switch (type) { case T_PHASE: res << Pointf(xdata, std::arg(freqbuf[i])); break; case T_FFT: res << Pointf(xdata, 2*std::abs(freqbuf[i])/windowSum); break; case T_PSD: res << Pointf(xdata, 2*sqr(std::abs(freqbuf[i]))/(windowSum/tSample)); } } } return res; }
void EigsGen::sortDesc(VectorXcd &values) { if(values.imag().isZero()) std::sort(values.data(), values.data() + values.size(), compare_complex_real); else std::sort(values.data(), values.data() + values.size(), compare_complex_mod); }
VectorXd GLMObjective::coordinate_descent(RegFunction *regfunc, int idx) { VectorXd gr = X[idx].transpose() * -R / n; VectorXd tmp; MatrixXd wXX(p, p); wXX.setZero(); for (int i = 0; i < n; i++) wXX += W[i] * X[idx].row(i).transpose() * X[idx].row(i); wXX /= n; VectorXcd eigenvalues = wXX.eigenvalues(); double step_size = 0; for (int i = 0; i < eigenvalues.size(); i++) { step_size = std::max(step_size, eigenvalues[i].real()); } assert(step_size >= 0); tmp = regfunc->threshold_p((model_param.beta[idx] - gr / step_size), step_size); VectorXd delta_beta = tmp - model_param.beta[idx]; tmp = (wXX*model_param.beta[idx] - gr); VectorXd old_beta = model_param.beta[idx]; model_param.beta[idx] = regfunc->threshold(tmp)/step_size; delta_beta = model_param.beta[idx] - old_beta; if (calc_norm(delta_beta) > 1e-8) { Xb += X[idx] * delta_beta; R -= W.cwiseProduct(X[idx] * delta_beta); } return model_param.beta[idx]; }
VectorTypeHost eigenToCusp(const VectorXcd& psiEigen) { int length = psiEigen.size(); VectorTypeHost psiCusp(length, 0.0); for (int i = 0; i < length; ++i) psiCusp[i] = eigenToCusp(psiEigen[i]); return psiCusp; }
PyObject* calc_Gavg(PyObject *pyw, const double &delta, const double &mu, PyObject *pySE, PyObject *pyTB, const double &Hf, PyObject *py_bp, PyObject *py_wf, const int nthreads) { try { if (nthreads > 0) omp_set_num_threads(nthreads); MatrixXcd SE; VectorXcd w; VectorXd bp, wf; VectorXd SlaterKosterCoeffs; numpy::from_numpy(pySE, SE); numpy::from_numpy(pyTB, SlaterKosterCoeffs); numpy::from_numpy(py_bp, bp); numpy::from_numpy(py_wf, wf); numpy::from_numpy(pyw, w); assert(w.size() == SE.rows()); double t = SlaterKosterCoeffs(0); VectorXd xl(DIM), xh(DIM); xl << -2*t; xh << 2*t; double BZ1 = 1.; MatrixXcd result(SE.rows(), MSIZE*MSIZE); VectorXcd tmp(MSIZE); GreenIntegrand green_integrand(w, mu, SE, SlaterKosterCoeffs, Hf); result.setZero(); for (int n = 0; n < w.size(); ++n) { green_integrand.set_data(n); tmp = md_int::Integrate(xl, xh, green_integrand, bp, wf); for (int i = 0; i < MSIZE; ++i) result(n, MSIZE*i + i) = tmp(i); } result /= BZ1; return numpy::to_numpy(result); } catch (const char *str) { std::cerr << str << std::endl; return Py_None; } }
double GLMObjective::get_local_change(const VectorXd &old, int idx) { VectorXd delta_beta = old - model_param.beta[idx]; VectorXd delta_Xb = X[idx] * delta_beta; MatrixXd wXX(p, p); for (int i = 0; i < p; i++) for (int j = 0; j < p; j++) wXX(i, j) = 0; for (int i = 0; i < n; i++) wXX += W[i] * X[idx].row(i).transpose() * X[idx].row(i); VectorXcd eigenvalues = wXX.eigenvalues(); double max_eigen = 0; for (int i = 0; i < eigenvalues.size(); i++) { max_eigen = std::max(max_eigen, eigenvalues[i].real()); } return max_eigen * (delta_beta.dot(delta_beta)) / (2 * n); }
void getRealValues(std::vector<double> &realValues, VectorXcd &complexValues, double &a, double &b) { double threshold = 10; threshold = pow(10, -20); for (int i = 0; i < complexValues.size(); ++i) { double imagValue = fabs(complexValues(i).imag()); if(imagValue < threshold) { if(a <= complexValues(i).real() && b >= complexValues(i).real()) { realValues.push_back(complexValues(i).real()); } } } std::sort(realValues.begin(), realValues.end()); }
void EigsGen::eigenvalueSchur(const MatrixXd &Rm, VectorXcd &result) { int m = Rm.cols(); if(result.size() != m) result.resize(m); for(int i = 0; i < m; i++) { if(i == m - 1 || fabs(Rm(i + 1, i)) < 1e-16) { result[i] = std::complex<double>(Rm(i, i), 0); } else { result[i] = eigenvalue2x2(Rm(i, i), Rm(i, i + 1), Rm(i + 1, i), Rm(i + 1, i + 1)); i++; result[i] = conj(result[i - 1]); } } }
VectorXcd _LC_EXPs<M>::DAtR(const VectorXcd& rs) const { int num_rs(rs.size()); VectorXcd ys = VectorXcd::Zero(num_rs); for(int i_r = 0; i_r < num_rs; i_r++) { dcomplex r(rs[i_r]); dcomplex y(0); for(int j_b = 0; j_b < this->size(); j_b++) { dcomplex c(this->cs[j_b]); int n(this->ns[j_b]); dcomplex z(this->zs[j_b]); y += Exp_DAtR<M>(r, c, n, z); } ys[i_r] = y; } return ys; }
void EigsGen::sortDescPair(VectorXcd &values, VectorXi &index) { int len = values.size(); if(len != index.size()) return; std::vector<SortPair> v(len); for(int i = 0; i < len; i++) { v[i].first = values[i]; v[i].second = index[i]; } if(values.imag().isZero()) std::sort(v.begin(), v.end(), compare_pair<COMPREAL>); else std::sort(v.begin(), v.end(), compare_pair<COMPMOD>); for(int i = 0; i < len; i++) { values[i] = v[i].first; index[i] = v[i].second; } }
VectorXcd _LC_EXPs<M>::AtR(const VectorXcd& rs) const { int num_rs(rs.size()); VectorXcd ys = VectorXcd::Zero(num_rs); for(int i_r = 0; i_r < num_rs; i_r++) { dcomplex r(rs[i_r]); dcomplex rr(pow(r, M)); dcomplex y(0); for(int j_b = 0; j_b < this->size(); j_b++) { dcomplex c(this->cs[j_b]); int n(this->ns[j_b]); dcomplex z(this->zs[j_b]); y += c * pow(r, n) * exp(-z * rr); } ys[i_r] = y; } return ys; }