Commit e9621937 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

moved functions

parent cf2dcf86
......@@ -4,4 +4,4 @@ add_library(BASICS STATIC
PlotMap.cpp
Utilities.cpp
)
target_link_libraries(BASICS)
\ No newline at end of file
target_link_libraries(BASICS fftw3)
\ No newline at end of file
#include "Utilities.hpp"
template<>
std::string vec2str<int>(std::vector<int> vec) {
std::string str = "[";
......@@ -56,4 +57,115 @@ double calc_var(const std::vector<double> &vec) {
for (auto const &value: vec)
sqrs += pow(value, 2);
return sqrs / vec.size() - pow(calc_mean(vec), 2);
}
#define REAL 0
#define IMAG 1
void fft(int N, fftw_complex *in, fftw_complex *out) {
fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
fftw_cleanup();
}
void fft(std::vector<std::complex<double>> &in, std::vector<std::complex<double>> &out) {
int N = in.size();
fftw_complex in_arr[N], out_arr[N];
for (int i = 0; i < N; i++) {
in_arr[i][REAL] = in[i].real();
in_arr[i][IMAG] = in[i].imag();
}
fft(N, in_arr, out_arr);
out.resize(N);
for (int i = 0; i < N; i++) {
out[i] = {out_arr[i][REAL], out_arr[i][IMAG]};
}
}
void fft(const std::vector<double> &in, std::vector<double> &out) {
int N = in.size();
std::vector<std::complex<double>> in_complex;
std::vector<std::complex<double>> out_complex;
in_complex.resize(N);
out_complex.resize(N);
for (int i = 0; i < N; i++) {
in_complex[i] = {in[i], 0.0};
}
fft(in_complex, out_complex);
out.resize(N);
for (int i = 0; i < N; i++) {
out[i] = out_complex[i].real();
}
}
void fft2(int N2, int N1, fftw_complex *in, fftw_complex *out) {
fftw_plan plan = fftw_plan_dft_2d(N2, N1, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
fftw_cleanup();
}
void fft2(const std::vector<std::vector<std::complex<double>>> &in,
std::vector<std::vector<std::complex<double>>> &out) {
int N2 = in.size();
int N1 = in[0].size();
auto in_arr = new fftw_complex[N2 * N1];
auto out_arr = new fftw_complex[N2 * N1];
for (int i = 0; i < N2; i++) {
for (int j = 0; j < N1; j++) {
in_arr[i * N2 + j][REAL] = in[i][j].real();
in_arr[i * N2 + j][IMAG] = in[i][j].imag();
}
}
fft2(N2, N1, in_arr, out_arr);
out.resize(N2);
for (int i = 0; i < N2; i++) {
out[i].resize(N1);
for (int j = 0; j < N1; j++) {
out[i][j] = {out_arr[i * N2 + j][REAL], out_arr[i * N2 + j][IMAG]};
}
}
delete[] in_arr;
delete[] out_arr;
}
void fft2(const std::vector<std::vector<double>> &in,
std::vector<std::vector<double>> &out) {
int N2 = in.size();
int N1 = in[0].size();
std::vector<std::vector<std::complex<double>>> in_complex;
std::vector<std::vector<std::complex<double>>> out_complex;
in_complex.resize(N2);
out_complex.resize(N2);
for (int i = 0; i < N2; i++) {
in_complex[i].resize(N1);
out_complex[i].resize(N2);
for (int j = 0; j < N1; j++) {
in_complex[i][j] = {in[i][j], 0.0};
}
}
fft2(in_complex, out_complex);
out.resize(N2);
for (int i = 0; i < N2; i++) {
out[i].resize(N1);
for (int j = 0; j < N1; j++) {
out[i][j] = out_complex[i][j].real();
}
}
}
std::vector<double> linspace(const double &start, const double &end, int num) {
std::vector<double> linspaced;
if (num == 0) { return linspaced; }
if (num == 1) {
linspaced.push_back(start);
return linspaced;
}
double delta = (end - start) / (num - 1);
for (int i = 0; i < num - 1; i++) {
linspaced.push_back(start + delta * i);
}
linspaced.push_back(end);
return linspaced;
}
\ No newline at end of file
......@@ -4,6 +4,8 @@
#include <vector>
#include <string>
#include <math.h>
#include <complex>
#include <fftw3.h>
template<typename T>
......@@ -25,4 +27,23 @@ double calc_var(const std::vector<int> &vec);
double calc_var(const std::vector<std::vector<int>> &vec);
void fft(int N, fftw_complex *in, fftw_complex *out);
void fft(std::vector<std::complex<double>> &in,
std::vector<std::complex<double>> &out);
void fft(const std::vector<double> &in,
std::vector<double> &out);
void fft2(int N2, int N1, fftw_complex *in, fftw_complex *out);
void fft2(const std::vector<std::vector<std::complex<double>>> &in,
std::vector<std::vector<std::complex<double>>> &out);
void fft2(const std::vector<std::vector<double>> &in,
std::vector<std::vector<double>> &out);
std::vector<double> linspace(const double &start,
const double &end, int num);
#endif //TOOLS_HPP
......@@ -7,4 +7,4 @@ add_library(PROBLEMS STATIC
generators/NormalDistribution.cpp
generators/UniformDistribution.cpp
)
target_link_libraries(PROBLEMS BASICS fftw3 sprng ${MPP_LIBRARIES})
target_link_libraries(PROBLEMS BASICS sprng ${MPP_LIBRARIES})
......@@ -377,114 +377,3 @@ vector<vector<double>> CirculantEmbedding2D::getSqrtEV() {
}
return sqrt_ev_return;
}
#define REAL 0
#define IMAG 1
void fft(int N, fftw_complex *in, fftw_complex *out) {
fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
fftw_cleanup();
}
void fft(vector<std::complex<double>> &in, vector<std::complex<double>> &out) {
int N = in.size();
fftw_complex in_arr[N], out_arr[N];
for (int i = 0; i < N; i++) {
in_arr[i][REAL] = in[i].real();
in_arr[i][IMAG] = in[i].imag();
}
fft(N, in_arr, out_arr);
out.resize(N);
for (int i = 0; i < N; i++) {
out[i] = {out_arr[i][REAL], out_arr[i][IMAG]};
}
}
void fft(const vector<double> &in, vector<double> &out) {
int N = in.size();
vector<std::complex<double>> in_complex;
vector<std::complex<double>> out_complex;
in_complex.resize(N);
out_complex.resize(N);
for (int i = 0; i < N; i++) {
in_complex[i] = {in[i], 0.0};
}
fft(in_complex, out_complex);
out.resize(N);
for (int i = 0; i < N; i++) {
out[i] = out_complex[i].real();
}
}
void fft2(int N2, int N1, fftw_complex *in, fftw_complex *out) {
fftw_plan plan = fftw_plan_dft_2d(N2, N1, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
fftw_cleanup();
}
void fft2(const vector<vector<std::complex<double>>> &in,
vector<vector<std::complex<double>>> &out) {
int N2 = in.size();
int N1 = in[0].size();
auto in_arr = new fftw_complex[N2 * N1];
auto out_arr = new fftw_complex[N2 * N1];
for (int i = 0; i < N2; i++) {
for (int j = 0; j < N1; j++) {
in_arr[i * N2 + j][REAL] = in[i][j].real();
in_arr[i * N2 + j][IMAG] = in[i][j].imag();
}
}
fft2(N2, N1, in_arr, out_arr);
out.resize(N2);
for (int i = 0; i < N2; i++) {
out[i].resize(N1);
for (int j = 0; j < N1; j++) {
out[i][j] = {out_arr[i * N2 + j][REAL], out_arr[i * N2 + j][IMAG]};
}
}
delete[] in_arr;
delete[] out_arr;
}
void fft2(const vector<vector<double>> &in, vector<vector<double>> &out) {
int N2 = in.size();
int N1 = in[0].size();
vector<vector<std::complex<double>>> in_complex;
vector<vector<std::complex<double>>> out_complex;
in_complex.resize(N2);
out_complex.resize(N2);
for (int i = 0; i < N2; i++) {
in_complex[i].resize(N1);
out_complex[i].resize(N2);
for (int j = 0; j < N1; j++) {
in_complex[i][j] = {in[i][j], 0.0};
}
}
fft2(in_complex, out_complex);
out.resize(N2);
for (int i = 0; i < N2; i++) {
out[i].resize(N1);
for (int j = 0; j < N1; j++) {
out[i][j] = out_complex[i][j].real();
}
}
}
vector<double> linspace(const double &start, const double &end, int num) {
vector<double> linspaced;
if (num == 0) { return linspaced; }
if (num == 1) {
linspaced.push_back(start);
return linspaced;
}
double delta = (end - start) / (num - 1);
for (int i = 0; i < num - 1; i++) {
linspaced.push_back(start + delta * i);
}
linspaced.push_back(end);
return linspaced;
}
\ No newline at end of file
......@@ -216,26 +216,6 @@ public:
string Name() const override { return "Circulant Embedding"; }
};
#include <complex>
#include <fftw3.h>
void fft(int N, fftw_complex *in, fftw_complex *out);
void fft(std::vector<std::complex<double>> &in,
std::vector<std::complex<double>> &out);
void fft(const std::vector<double> &in,
std::vector<double> &out);
void fft2(int N2, int N1, fftw_complex *in, fftw_complex *out);
void fft2(const std::vector<std::vector<std::complex<double>>> &in,
std::vector<std::vector<std::complex<double>>> &out);
void fft2(const std::vector<std::vector<double>> &in,
std::vector<std::vector<double>> &out);
std::vector<double> linspace(const double &start,
const double &end, int num);
#endif //M_CIRCULANTEMBEDDING_H
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment