std::complex<double>offre.
jn'est qu'une variable, on peut utiliser à la place
std::complex<double{0,1}ou bien depuis le C++14 après avoir indiqué
using namespace std::literals;on peut utiliser
1i.
Eigen:: Matrix3cd Z; const std::complex<double> j{ 0 , 1 }; Z(0,0) = res1_ohm[0] + j * res2_ohm[0]; // ou Z(0,0) = std::complex<double>{ res1_ohm[0] , res2_ohm[0] }; // ou using namespace literals; Z(0,0) = res1_ohm[0] + 1i * res2_ohm[0];
#include <string> #include <sstream> #include <iostream> #include <vector> #include <fstream> #include <clocale> #include <complex> #include <cmath> #include <algorithm> #include <Eigen/Dense> template <class charT, charT sep> class punct_facet: public std::numpunct<charT> { protected: charT do_decimal_point() const { return sep; } }; double comaStod(std::string const& s) { std::istringstream iss(s); iss.imbue(std::locale(std::cout.getloc(), new punct_facet<char, ','>)); double d; if (!(iss >> d)) throw std::invalid_argument("invalid double"); return d; } // Will convert any vector of vector to Eigen::Matrix, but it's slow... Eigen::MatrixXd convertToEigenMatrix(const std::vector<std::vector<double>>& array) { // Search max_col_size size_t max_col_size = 0; for (size_t row = 0; row < array.size(); ++row) max_col_size = std::max(max_col_size, array[row].size()); Eigen::MatrixXd matrix(array.size(), max_col_size); for (size_t row = 0; row < array.size(); ++row) { for (size_t col = 0; col < array[row].size(); ++col) matrix(row, col) = array[row][col]; // Fill any too short columns with 0. for (size_t col = array[row].size(); col < max_col_size; ++col) matrix(row, col) = 0; } return matrix; } int main() { const std::string filename = "Measurements.csv"; std::ifstream file(filename); if (!file) { std::cerr << "Can't open file " << filename << std::endl; return 1; } std::string line; std::vector<std::vector<double>> array; while (std::getline(file, line)) { // Ignore line starting with # if (line.empty() || line[0] == '#') continue; std::vector<double> v; std::string field; std::stringstream ss(line); while (std::getline(ss, field, ';')) { try { v.push_back(comaStod(field)); } catch (std::invalid_argument& e) { //std::cerr << "Can't convert " << field << " to double (" << e.what() << ")" << std::endl; continue; } } if (!v.empty()) array.push_back(v); } auto matrix = convertToEigenMatrix(array); std::cout << "Nb rows: " << matrix.rows() << ", nb cols: " << matrix.cols() << ", total size: " << matrix.size() << std::endl; std::cout << "Matrix:\n" << matrix << std::endl; return 0; }
Améliorer votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
#include <string> #include <sstream> #include <iostream> #include <vector> #include <fstream> #include <clocale> #include <cmath> #include <complex> #include "EigenResources\Eigenvalues" template <class charT, charT sep> class punct_facet: public std::numpunct<charT> { protected: charT do_decimal_point() const { return sep; } }; //conversion , to . double comaStod(std::string const& s) { std::istringstream iss(s); iss.imbue(std::locale(std::cout.getloc(), new punct_facet<char, ','>)); double d; if (!(iss >> d)) throw std::invalid_argument("invalid double"); return d; } //Eigen Matrix Eigen::MatrixXd ConvertToEigenMatrix(std::vector<std::vector<double>> data) { Eigen::MatrixXd eMatrix(data.size(), data[0].size()); for (int i = 0; i < data.size(); ++i) eMatrix.row(i) = Eigen::VectorXd::Map(&data[i][0], data[0].size()); return eMatrix; } int main() { const std::string filename = "Measurements.csv"; std::ifstream file(filename); if (!file) { std::cerr << "Can't open file " << filename << std::endl; return 1; } std::string line; std::vector<std::vector<double>> TAB1; while (std::getline(file, line)) { // Ignore line starting with # //if (line.empty() || line[0] == '#') //continue; std::vector<double> v; std::string field; std::stringstream ss(line); while (std::getline(ss, field, ';')) { try { v.push_back(comaStod(field)); } catch (std::invalid_argument& e) { std::cerr << "Can't convert " << field << " to double (" << e.what() << ")" << std::endl; continue; } } if (!v.empty()) TAB1.push_back(v); } //affichage (deux boucles for) std::cout << "Vec:\n"; for (size_t i=0; i<TAB1.size(); ++i) { for (size_t j=0; j<TAB1[i].size(); ++j) { //std::cout << TAB1[i][j] << " "; } std::cout << "\n"; } //appel de la fonction pour la conversion Eigen Matrix Eigen::MatrixXd ma_matrice; ma_matrice = ConvertToEigenMatrix (TAB1); std::cout << ma_matrice <<std::endl; return 0; }
une question bête : pourquoi t'as ajouté const ici Eigen::MatrixXd convertToEigenMatrix(const std::vector<std::vector<double>>& array)??
Améliorer votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <string> #include <sstream> #include <iostream> #include <vector> #include <fstream> #include <clocale> #include <complex> #include <cmath> #include <algorithm> #include <Eigen/Dense> template <class charT, charT sep> class punct_facet: public std::numpunct<charT> { protected: charT do_decimal_point() const { return sep; } }; double comaStod(std::string const& s) { std::istringstream iss(s); iss.imbue(std::locale(std::cout.getloc(), new punct_facet<char, ','>)); double d; if (!(iss >> d)) throw std::invalid_argument("invalid double"); return d; } // Will convert any vector of vector to Eigen::Matrix, but it's slow... Eigen::MatrixXd convertToEigenMatrix(const std::vector<std::vector<double>>& array) { // Search max_col_size size_t max_col_size = 0; for (size_t row = 0; row < array.size(); ++row) max_col_size = std::max(max_col_size, array[row].size()); Eigen::MatrixXd matrix(array.size(), max_col_size); for (size_t row = 0; row < array.size(); ++row) { for (size_t col = 0; col < array[row].size(); ++col) matrix(row, col) = array[row][col]; // Fill any too short columns with 0. for (size_t col = array[row].size(); col < max_col_size; ++col) matrix(row, col) = 0; } return matrix; } int main() { const std::string filename = "Measurements.csv"; std::ifstream file(filename); if (!file) { std::cerr << "Can't open file " << filename << std::endl; return 1; } std::string line; std::vector<std::vector<double>> array; while (std::getline(file, line)) { std::vector<double> v; std::string field; std::stringstream ss(line); while (std::getline(ss, field, ';')) { try { v.push_back(comaStod(field)); } catch (std::invalid_argument& e) { //std::cerr << "Can't convert " << field << " to double (" << e.what() << ")" << std::endl; continue; } } if (!v.empty()) array.push_back(v); } auto matrix = convertToEigenMatrix(array); std::cout << "Nb rows: " << matrix.rows() << ", nb cols: " << matrix.cols() << ", total size: " << matrix.size() << std::endl; std::cout << "Matrix:\n" << matrix << std::endl; // Vecteurs colonnes : Eigen:: VectorXd R2,r2; R2 = matrix.col(2); //2éme colonnes de la matrice //Multiplication element par element du vecteur R2: for (int k=0; k< R2.size(); ++k) { r2[k]=R2[k]*R2[k]; } return 0; }
Améliorer votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Eigen::Matrix3cd matrice_Z1; matrice_Z1.real()<<res1_ohm[0],0,0,0,res2_ohm[0],0,0,0,res3_ohm[0]; //partie réelle matrice_Z1.imag()<< pulsation[0]*induc1_henry[0],pulsation[0]*mutuelle12[0],pulsation[0]*mutuelle13[0],pulsation[0]*mutuelle12[0],pulsation[0]*induc2_henry[0],pulsation[0]*mutuelle23[0],pulsation[0]*mutuelle13[0],pulsation[0]*mutuelle23[0],pulsation[0]*induc3_henry[0]; // partie imaginaire std::cout << "la matrice Z: "<< matrice_Z1 << est::endl;
Eigen:: Matrix3cd matrice_Z; Z(0,0)= res1_ohm[0]+j * res2_ohm[0];