嘗試用一些運算式替換矩陣的第一行,類似于我的 MATLAB 代碼:
%Matrix A defined and Ny
A(1,:) = (-1).^(1:Ny 1).*(0:Ny).^2;
A(Ny 1,:) = (0:Ny).^2;
我寫的 C 代碼是:
static const int ny = 10;
Eigen::VectorXd i = VectorXd(ny 1);
std::iota(i.begin(), i.end(), 0);
//matrix A is also defined correctly here
A.row(0) = pow(-1,(i))*(pow(i,2)); //error here
std::cout << A.row(0) << "\n";
我可以在不需要在 for 回圈中重寫它的情況下完成這項作業嗎?我猜這需要我更改 i 的初始化
完整的錯誤資訊:
Test.cpp:329:25: error: no matching function for call to ‘pow(int, Eigen::VectorXd&)’
329 | dv1.row(0) = pow(-1,(a))*(pow(a,2));
| ^~~
/usr/include/c /9/complex:1885:5: note: template argument deduction/substitution failed:
Test.cpp:329:25: note: ‘Eigen::VectorXd’ {aka ‘Eigen::Matrix<double, -1, 1>’} is not derived from ‘const std::complex<_Up>’
329 | dv1.row(0) = pow(-1,(a))*(pow(a,2));
/mnt/c/Users/J/Documents/eigen-3.4.0/eigen-3.4.0/Eigen/src/Core/GlobalFunctions.h:175:3: note: template argument deduction/substitution failed:
Test.cpp:329:35: note: mismatched types ‘const Eigen::ArrayBase<ExponentDerived>’ and ‘int’
329 | dv1.row(0) = pow(-1,(a))*(pow(a,2));
它不斷重復類似的錯誤。我嘗試用初始化 i ,Eigen::Matrix< double, 1, ny 1>但我得到了同樣的錯誤。%%%%%%%%%%%%%%%%%%%% 編輯:目前我有這個 C 代碼作業,但我想利用運算子.row()和.col():
for (int i = 0; i < ny 1; i ){
for (int j = 0; j < ny 1; j ){
A(0,j) = -1. * pow(-1,(j))*(pow(j,2));
A((ny),j) = 1. * pow(j,2);
}
}
std::cout << A << "\n";
這將替換矩陣 A 的第一行和最后一行
uj5u.com熱心網友回復:
這就是我在評論中@Sedenion 的幫助下解決問題的方法:
Eigen::ArrayXi exponents((ny 1));
exponents = Eigen::ArrayXi::LinSpaced((ny 1), 0, (ny 1));
A.row(0) = -1. * (Eigen::pow(-1., exponents.cast<double>())) * (Eigen::pow(exponents.cast<double>(),2)) ; //first row
A.row((ny)) = 1. * (Eigen::pow(exponents.cast<double>(),2)) ; //last row
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/494101.html
