GAMES101:Assignment 0:Linear Algebra Eigen
EigenEigen是一个用于线性代数运算的C模板库重载了-*等运算符实现了特殊方法如dot()cross()对于Matrix class,运算符仅重载以支持线性代数运算加减法运算符左右侧的行列数必须分别相同它们的标量类型必须相同-#includeiostream#includeEigen/Denseintmain(){Eigen::Matrix2d a;//2*2矩阵a1,2,3,4;Eigen::MatrixXdb(2,2);//行列数自定义为2*2的矩阵b2,3,1,4;std::couta b \nabstd::endl;Eigen::Vector3dv(1,2,3);//3*1向量Eigen::Vector3dw(1,0,0);std::cout-v w - v \n-vw-vstd::endl;}输出结果a b 3 5 4 8 -v w - v -1 -4 -6数乘/除*/转置共轭共轭转置对于矩阵aa.transpose()转置矩阵a.conjugate()共轭矩阵a.adjoint()共轭转置对每个元素取复共轭再转置对于实数矩阵conjugate()共轭矩阵不做任何操作即adjoint()与transpose()等价transpose()与adjoint()返回的矩阵不会被记录可以用b a.transpose()但不能用a a.transpose()因为transpose()返回的表达式持有对a的引用赋值时会边读取a边写入a会导致未定义行为解决方法是使用a.transposeInPlace()将a的转置存储在a中同样有a.adjointInPlace()表达式模板Eigen使用表达式模板上述操作返回的是表达式对象而非实际矩阵如auto t a.transpose();t引用a的数据如果之后a被修改t的行为可能不符合预期解决方法是显式声明类型Eigen::Matrix2d t a.transpose();矩阵乘法 矩阵向量乘法*注意矩阵乘法中m m*m会被特殊处理成tmp m*m; m tmp;所以不会有混叠效应点乘和叉乘v.dot(w)返回值为标量v.cross(w)叉乘方向右手定则四指螺旋从a绕向b,大拇指方向为叉乘结果向量方向对于2*1向量的cross()v[v1,v2],w[w1,w2]v [v1,v2],w [w1,w2]v[v1,v2],w[w1,w2]的叉乘v.cross(w)的结果是v1w2−v2w1v1w2 - v2w1v1w2−v2w1基本算术运算#includeiostream#includeEigen/Denseusingnamespacestd;intmain(){Eigen::Matrix2d mat;mat1,2,3,4;coutHere is mat.sum(): mat.sum()endl;//求和sum 10coutHere is mat.prod(): mat.prod()endl;//乘积prod 24coutHere is mat.mean(): mat.mean()endl;//均值mean 2.5coutHere is mat.minCoeff(): mat.minCoeff()endl;//最小值minCoeff 1coutHere is mat.maxCoeff(): mat.maxCoeff()endl;//最大值maxCoeff 4coutHere is mat.trace(): mat.trace()endl;//矩阵的迹trace 5主对角线元素之和}minCoeff()与maxCoeff()存在变体可以返回最小/最大值的位置坐标Matrix3f mMatrix3f::Random();std::ptrdiff_t i,j;floatminOfMm.minCoeff(i,j);//返回最小值的位置i,jcoutHere is the matrix m:\nmendl;coutIts minimum coefficient (minOfM) is at position (i,j)\n\n;RowVector4i vRowVector4i::Random();intmaxOfVv.maxCoeff(i);//返回最大值的位置icoutHere is the vector v: vendl;coutIts maximum coefficient (maxOfV) is at position iendl;Here is the matrix m: -0.824 -0.199 0.634 0.924 -0.237 0.334 -0.0532 0.146 -0.779 Its minimum coefficient (-0.824) is at position (0,0) Here is the vector v: 1189641421 1350490027 -1044963589 1967513926 Its maximum coefficient (1967513926) is at position 3访问向量元素使用()运算符Eigen::Vector3dQ(1.7,4.1,1.0);doublexQ(0);// 第1个元素x坐标doubleyQ(1);// 第2个元素y坐标doublezQ(2);// 第3个元素齐次坐标中的1
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2559027.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!