中矩阵运算_VS 2017中添加Eigen库-c++矩阵运算工具
图1 选中项目 图2 将文件路径添加到C/C++
发布日期:2021-06-24 15:49:59
浏览次数:2
分类:技术文章
本文共 4230 字,大约阅读时间需要 14 分钟。
此方法仅限于在Windows平台下使用。
- 下载 Eigen 库。官方下载途径:
下载时,选择最新版本的.zip 文件格式,解压缩。以Eigen 3.3.7为例,解压后得到一个文件夹名为:eigen-eigen-323c052e1731。将整个文件夹拷贝到你希望存放的位置,例如“C:”。(PS:不用去CSDN上下载,跟这个压缩包一模一样,还费钱)
2. 在VS 2017中新建一个空项目,取名为“Matrix-test”。输入以下测试代码(官方测试代码):
#include#include using namespace Eigen;using namespace std;int main(){#pragma region Addition and subtraction Matrix2d a; a << 1, 2, 3, 4; MatrixXd b(2, 2); b << 2, 3, 1, 4; std::cout << "a + b =n" << a + b << std::endl; std::cout << "a - b =n" << a - b << std::endl; std::cout << "Doing a += b;" << std::endl; a += b; std::cout << "Now a =n" << a << std::endl; Vector3d v(1, 2, 3); Vector3d w(1, 0, 0); std::cout << "-v + w - v =n" << -v + w - v << std::endl;#pragma endregion#pragma region Scalar multiplication and division// Matrix2d a; //duplicate definition a << 1, 2, 3, 4;// Vector3d v(1, 2, 3); //duplicate definition std::cout << "a * 2.5 =n" << a * 2.5 << std::endl; std::cout << "0.1 * v =n" << 0.1 * v << std::endl; std::cout << "Doing v *= 2;" << std::endl; v *= 2; std::cout << "Now v =n" << v << std::endl;#pragma endregion#pragma region Transposition and conjugation MatrixXcf a_matrix = MatrixXcf::Random(2, 2); cout << "Here is the matrix a_matrixn" << a_matrix << endl; cout << "Here is the matrix a_matrix^Tn" << a_matrix.transpose() << endl; cout << "Here is the conjugate of a_matrixn" << a_matrix.conjugate() << endl; cout << "Here is the matrix a_matrix^*n" << a_matrix.adjoint() << endl; //This is the so-called aliasing issue Matrix2i a_matrix2; a_matrix2 << 1, 2, 3, 4; cout << "Here is the matrix a_matrix2:n" << a_matrix2 << endl; // a_matrix2 = a_matrix2.transpose(); // !!! do NOT do this !!! cout << "and the result of the aliasing effect:n" << a_matrix2 << endl;#pragma endregion#pragma region Matrix-matrix and matrix-vector multiplication Matrix2d mat; mat << 1, 2, 3, 4; Vector2d u_1(-1, 1), v_1(2, 0); std::cout << "Here is mat*mat:n" << mat * mat << std::endl; std::cout << "Here is mat*u_1:n" << mat * u_1 << std::endl; std::cout << "Here is u_1^T*mat:n" << u_1.transpose()*mat << std::endl; std::cout << "Here is u_1^T*v:n" << u_1.transpose()*v_1 << std::endl; std::cout << "Here is u_1*v_1^T:n" << u_1 * v_1.transpose() << std::endl; std::cout << "Let's multiply mat by itself" << std::endl; mat = mat * mat; std::cout << "Now mat is mat:n" << mat << std::endl;#pragma endregion #pragma region Dot product and cross product Vector3d v_2(1, 2, 3); Vector3d w_2(0, 1, 2); cout << "Dot product: " << v_2.dot(w_2) << endl; double dp = v_2.adjoint()*w_2; // automatic conversion of the inner product to a scalar cout << "Dot product via a matrix product: " << dp << endl; cout << "Cross product:n" << v_2.cross(w_2) << endl;#pragma endregion#pragma region Basic arithmetic reduction operations Eigen::Matrix2d mat_3; mat_3 << 1, 2, 3, 4; cout << "Here is mat_3.sum(): " << mat_3.sum() << endl; cout << "Here is mat_3.prod(): " << mat_3.prod() << endl; cout << "Here is mat_3.mean(): " << mat_3.mean() << endl; cout << "Here is mat_3.minCoeff(): " << mat_3.minCoeff() << endl; cout << "Here is mat_3.maxCoeff(): " << mat_3.maxCoeff() << endl; cout << "Here is mat_3.trace(): " << mat_3.trace() << endl; Matrix3f m = Matrix3f::Random(); std::ptrdiff_t i, j; float minOfM = m.minCoeff(&i, &j); cout << "Here is the matrix m:n" << m << endl; cout << "Its minimum coefficient (" << minOfM << ") is at position (" << i << "," << j << ")nn"; RowVector4i v_4 = RowVector4i::Random(); int maxOfV = v_4.maxCoeff(&i); cout << "Here is the vector v_4: " << v_4 << endl; cout << "Its maximum coefficient (" << maxOfV << ") is at position " << i << endl;#pragma endregion/**************This is the end of example codes in Eigen3 online document. **********************/ system("pause");}
注意,此时项目默认为“DEBUG”模式,活动平台为“Win32"。如果后面更改了模式或平台,均要重新执行全部以下步骤。
3. 选择该项目(图1位置),右键选择“属性”->"C/C++"->"常规"->“附加包含目录,将路径“C:eigen-eigen-323c052e1731”添加进去,再点击“应用”。如图2。
3. 继续在此页面,“链接器”->“常规”->“附加库目录”,将路径“C:eigen-eigen-323c052e1731”添加进去,再点击“应用”。如图3。
4. 按“F7”生成文件,再按“Ctrl+F5”执行文件,即可看到正确运行的输出,表示Eigen库已经链接成功。接下来就可以自己写新的测试了。
Eigen的online-document : Eigen: Getting started
转载地址:https://blog.csdn.net/weixin_33642922/article/details/112610173 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月13日 20时00分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
数据库在运行的job kill掉进程后又自动启动?
2019-04-28
你肯定没做过的oracle9i到11g的增量传输表空间
2019-04-28
查看rman中list backup对应的sql
2019-04-28
Oracle RAC添加服务名实现单节点访问
2019-04-28
记录一则完整的SPA(10g->11g)测试过程
2019-04-28
Oracle查询存在外键约束但对应列索引缺失
2019-04-28
通过拷贝block实现system文件大量坏块恢复
2019-04-28
impdp TABLE_EXISTS_ACTION=REPLACE覆盖后怎么恢复?
2019-04-28
oracle11g修改用户名
2019-04-28
WRI$_ADV_OBJECTS表过大,导致PDB的SYSAUX表空间不足
2019-04-28
tfactl diagcollect -srdc ora600收集错误信息
2019-04-28
vsftp快速配置
2019-04-28
asm rebalance 三个阶段
2019-04-28
12c以后ASM单盘大小可超2TB
2019-04-28
Pin-point安装参考
2019-04-28
在线快速清理listener.log日志
2019-04-28
oracle的字段类型number,number(*),number(*,0)
2019-04-28