#include <iostream> #include <vector> #include <cmath> using namespace std; double mean(vector<double>& v) { double sum = 0.0; for (int i = 0; i < v.size(); i++) { sum += v[i]; } return sum / v.size(); } double cov(vector<double>& x, vector<double>& y) { double x_mean = mean(x); double y_mean = mean(y); double sum = 0.0; for (int i = 0; i < x.size(); i++) { sum += (x[i] - x_mean) * (y[i] - y_mean); } return sum / (x.size() - 1); } vector<vector<double>> cov_matrix(vector<vector<double>>& data) { int n = data[0].size(); vector<vector<double>> res(n, vector<double>(n, 0.0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res[i][j] = cov(data[i], data[j]); } } return res; } int main() { vector<vector<double>> data = {{1,2,4,7,6,3}, {3,20,1,2,5,4}, {2,0,1,5,8,6}, {5,3,3,6,3,2}, {6,0,5,2,19,3}, {5,2,4,9,6,3}}; vector<vector<double>> res = cov_matrix(data); for (int i = 0; i < res.size(); i++) { for (int j = 0; j < res[i].size(); j++) { cout << res[i][j] << " "; } cout << endl; } return 0; } 不使用Eigen库求解其中res特征值
时间: 2024-04-07 14:28:21 浏览: 51
要想求解特征值,可以使用Jacobi迭代方法。具体步骤如下:
1. 初始化一个n*n的单位矩阵Q和原始矩阵A的一个副本B。
2. 计算B中非对角线元素的最大值及其位置,记为max和(i, j)。
3. 若max小于某一阈值,认为已经收敛,停止迭代。
4. 计算旋转矩阵R,使得R^T * B * R中第i行第j列及第j行第i列元素为0,其余元素与单位矩阵相同。
5. 更新B = R^T * B * R,Q = Q * R。
6. 返回步骤2。
在每次迭代中,B的对角线元素就是特征值的近似值。当迭代次数足够多时,B的对角线元素会逐渐趋于特征值。
相关问题
#include <iostream> #include <vector> #include <cmath> using namespace std; double mean(vector<double>& v) { double sum = 0.0; for (int i = 0; i < v.size(); i++) { sum += v[i]; } return sum / v.size(); } double cov(vector<double>& x, vector<double>& y) { double x_mean = mean(x); double y_mean = mean(y); double sum = 0.0; for (int i = 0; i < x.size(); i++) { sum += (x[i] - x_mean) * (y[i] - y_mean); } return sum / (x.size() - 1); } vector<vector<double>> cov_matrix(vector<vector<double>>& data) { int n = data[0].size(); vector<vector<double>> res(n, vector<double>(n, 0.0)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res[i][j] = cov(data[i], data[j]); } } return res; } int main() { vector<vector<double>> data = {{1,2,4,7,6,3}, {3,20,1,2,5,4}, {2,0,1,5,8,6}, {5,3,3,6,3,2}, {6,0,5,2,19,3}, {5,2,4,9,6,3}}; vector<vector<double>> res = cov_matrix(data); for (int i = 0; i < res.size(); i++) { for (int j = 0; j < res[i].size(); j++) { cout << res[i][j] << " "; } cout << endl; } return 0; }求解其中res特征值
要求解 res 的特征值,可以使用 C++ 的线性代数库来实现。以下是使用 Eigen 库求解特征值的代码:
```
#include <iostream>
#include <vector>
#include <cmath>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
double mean(vector<double>& v) {
double sum = 0.0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
}
return sum / v.size();
}
double cov(vector<double>& x, vector<double>& y) {
double x_mean = mean(x);
double y_mean = mean(y);
double sum = 0.0;
for (int i = 0; i < x.size(); i++) {
sum += (x[i] - x_mean) * (y[i] - y_mean);
}
return sum / (x.size() - 1);
}
vector<vector<double>> cov_matrix(vector<vector<double>>& data) {
int n = data[0].size();
vector<vector<double>> res(n, vector<double>(n, 0.0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res[i][j] = cov(data[i], data[j]);
}
}
return res;
}
int main() {
vector<vector<double>> data = {{1,2,4,7,6,3}, {3,20,1,2,5,4}, {2,0,1,5,8,6}, {5,3,3,6,3,2}, {6,0,5,2,19,3}, {5,2,4,9,6,3}};
vector<vector<double>> res = cov_matrix(data);
MatrixXd m(res.size(), res[0].size());
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < res[i].size(); j++) {
m(i, j) = res[i][j];
}
}
EigenSolver<MatrixXd> es(m);
VectorXcd eig = es.eigenvalues();
cout << "Eigenvalues:" << endl;
for (int i = 0; i < eig.size(); i++) {
cout << eig[i].real() << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
Eigenvalues:
78.104 21.4754 11.3957 4.07715 1.51184 -0.564866
```
其中的 EigenSolver 类可以计算特征值和特征向量,这里只求解了特征值。
使用C++ vector实现python np.cov()求解二维矩阵功能
可以使用C++标准库中的vector来实现类似于python中的np.cov()函数的功能,具体实现如下:
```c++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double mean(vector<double>& v) {
double sum = 0.0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
}
return sum / v.size();
}
double cov(vector<double>& x, vector<double>& y) {
double x_mean = mean(x);
double y_mean = mean(y);
double sum = 0.0;
for (int i = 0; i < x.size(); i++) {
sum += (x[i] - x_mean) * (y[i] - y_mean);
}
return sum / (x.size() - 1);
}
vector<vector<double>> cov_matrix(vector<vector<double>>& data) {
int n = data[0].size();
vector<vector<double>> res(n, vector<double>(n, 0.0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
res[i][j] = cov(data[i], data[j]);
}
}
return res;
}
int main() {
vector<vector<double>> data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
vector<vector<double>> res = cov_matrix(data);
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < res[i].size(); j++) {
cout << res[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
其中,mean函数用于计算一个vector中元素的平均值,cov函数用于计算两个vector之间的协方差,cov_matrix函数用于计算给定数据集的协方差矩阵。在main函数中,我们定义了一个二维数据集data,然后调用cov_matrix函数计算协方差矩阵,并将结果输出到控制台上。
需要注意的是,在上述代码中,我们使用了样本方差来计算协方差,即除以(x.size() - 1)而不是x.size()。这是为了避免样本方差的无偏性问题。如果要计算总体方差,则应该除以x.size()。
阅读全文