#include<iostream> #include<Eigen/Dense> usingnamespace std; usingnamespace Eigen; intmain() { Matrix2f A; A << 1, 2, 2, 3; cout << "Here is the matrix A:\n" << A << endl; SelfAdjointEigenSolver<Matrix2f> eigensolver(A); if (eigensolver.info() != Success) abort(); cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl; cout << "Here's a matrix whose columns are eigenvectors of A \n" << "corresponding to these eigenvalues:\n" << eigensolver.eigenvectors() << endl; }
运算结果如下:
1 2 3 4 5 6 7 8 9 10
Here is the matrix A: 1 2 2 3 The eigenvalues of A are: -0.236 4.24 Here's a matrix whose columns are eigenvectors of A corresponding to these eigenvalues: -0.851 -0.526 0.526 -0.851
#include<iostream> #include<Eigen/Dense> usingnamespace std; usingnamespace Eigen; intmain() { Matrix2f A, b; LLT<Matrix2f> llt; A << 2, -1, -1, 3; b << 1, 2, 3, 1; cout << "Here is the matrix A:\n" << A << endl; cout << "Here is the right hand side b:\n" << b << endl; cout << "Computing LLT decomposition..." << endl; llt.compute(A); cout << "The solution is:\n" << llt.solve(b) << endl; A(1,1)++; cout << "The matrix A is now:\n" << A << endl; cout << "Computing LLT decomposition..." << endl; llt.compute(A); cout << "The solution is now:\n" << llt.solve(b) << endl; }
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Here is the matrix A: 2 -1 -1 3 Here is the right hand side b: 1 2 3 1 Computing LLT decomposition... The solution is: 1.2 1.4 1.4 0.8 The matrix A is now: 2 -1 -1 4 Computing LLT decomposition... The solution is now: 1 1.29 1 0.571
也可以通过向构造函数中传入矩阵大小来预分配分解矩阵所需要的内存空间大小:
1 2 3
HouseholderQR<MatrixXf> qr(50,50); MatrixXf A = MatrixXf::Random(50,50); qr.compute(A); // no dynamic memory allocation
#include<iostream> #include<Eigen/Dense> usingnamespace std; usingnamespace Eigen; intmain() { Matrix3f A; A << 1, 2, 5, 2, 1, 4, 3, 0, 3; cout << "Here is the matrix A:\n" << A << endl; FullPivLU<Matrix3f> lu_decomp(A); cout << "The rank of A is " << lu_decomp.rank() << endl; cout << "Here is a matrix whose columns form a basis of the null-space of A:\n" << lu_decomp.kernel() << endl; cout << "Here is a matrix whose columns form a basis of the column-space of A:\n" << lu_decomp.image(A) << endl; // yes, have to pass the original A }
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13
Here is the matrix A: 1 2 5 2 1 4 3 0 3 The rank of A is 2 Here is a matrix whose columns form a basis of the null-space of A: 0.5 1 -0.5 Here is a matrix whose columns form a basis of the column-space of A: 5 1 4 2 3 3
#include<iostream> #include<Eigen/Dense> usingnamespace std; usingnamespace Eigen; intmain() { Matrix2d A; A << 2, 1, 2, 0.9999999999; FullPivLU<Matrix2d> lu(A); cout << "By default, the rank of A is found to be " << lu.rank() << endl; lu.setThreshold(1e-5); cout << "With threshold 1e-5, the rank of A is found to be " << lu.rank() << endl; }
输出:
1 2
By default, the rank of A is found to be 2 With threshold 1e-5, the rank of A is found to be 1
Matrix2i a; a << 1, 2, 3, 4; cout << "Matrix a:\n" << a << endl; a = a.transpose(); // !!! do NOT do this !!! cout << "Transposed a:\n" << a << endl;
输出:
1 2 3 4 5 6
Matrix a: 1 2 3 4 Transposed a: 1 2 2 4
与上面相同的解决方法,即使用 a = a.transpose().eval() 代替。此外,Eigen提供了一种专门针对这种情况的特殊函数 transposeInPlace() 以实现将当前矩阵替换为其转置矩阵:
1 2 3 4 5 6
MatrixXf a(2,3); a << 1, 2, 3, 4, 5, 6; cout << "Here is the initial matrix a:\n" << a << endl; a.transposeInPlace(); cout << "and after being transposed:\n" << a << endl;
输出:
1 2 3 4 5 6 7
Here is the initial matrix a: 1 2 3 4 5 6 and after being transposed: 1 4 2 5 3 6
不过Eigen对矩阵乘法进行的这种处理是需要付出一定计算资源的代价的。在进行上述乘法的过程中,Eigen首先将乘积计算入一个临时矩阵,结束计算后再将这个临时矩阵赋值给 matA ,在当前情况下这是合理的。但相同的操作也会对 matB = matA * matA 进行,这就会导致一定程度上的计算资源浪费。在这种情况下,Eigen提供了 noalias() 函数进行直接赋值而不用经过中间的临时矩阵:matB.noalias() = matA * matA 。这种操作一定要慎用!!!
可以使用C++的 placement new 语法实现在生命Map对象后修改其数组。简而言之,placement new 语法实现的是从给定指针所指向的地址开始创建一个新的对象,一般来说该指针指向的是一片提前申请好的内存,所以也不会进行内存分配。例如:
1 2 3 4 5
int data[] = {1,2,3,4,5,6,7,8,9}; Map<RowVectorXi> v(data,4); cout << "The mapped vector v is: " << v << "\n"; new (&v) Map<RowVectorXi>(data+4,5); cout << "Now v is: " << v << "\n";
输出:
1 2
The mapped vector v is: 1 2 3 4 Now v is: 5 6 7 8 9
用这种方法可以在不知道映射数组在内存中位置的情况下构造一个Map对象:
1 2 3 4 5 6 7
Map<Matrix3f> A(NULL); // don't try to use this matrix yet! VectorXf b(n_matrices); for (int i = 0; i < n_matrices; i++) { new (&A) Map<Matrix3f>(get_matrix_pointer(i)); b(i) = A.trace(); }
# 将行采样点按输入图像高度放缩 if h != 288: scale_f = lambda x : int((x * 1.0/288) * h) sample_tmp = list(map(scale_f,culane_row_anchor)) # 根据提供的函数对指定序列做映射
lines = "" for i,r inenumerate(sample_tmp): label_r = np.asarray(label)[int(round(r))] # 取出label图像中行坐标为int(round(r))的一行 for lane_idx inrange(1, 5): line = "" pos = np.where(label_r == lane_idx)[0] iflen(pos) == 0: continue pos = np.mean(pos) line = line + str(pos) + " " + str(r) + " " # print(line) # cv2.circle(image, (int(round(pos)), int(round(r))), 1, (0,0,255),2) lines = lines + line + "\n" withopen(lines_file_path, 'w') as f: f.write(lines)
根据 label.png 生成 *_gt.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14
lines = "" for label_path in os.listdir(label_file_path): label = Image.open(label_path) label_data = np.asarray(label).copy() np.unique(label_data) line = gt_image_path + ' ' + label_path for i inrange(1, 5): if i in label_data: line = line + ' 1' else: line = line + ' 0' lines = lines + line + '\n' withopen(gt_file_path, 'w') as f: f.write(lines)
intmain() { Eigen::MatrixXf mat(2,4); Eigen::VectorXf v(2); mat << 1, 2, 6, 9, 3, 1, 7, 2; v << 0, 1; //add v to each column of m mat.colwise() += v; cout << "Broadcasting result: " << endl; cout << mat << endl; Eigen::VectorXf w(4); w << 0,1,2,3; //add w to each row of m mat.rowwise() += v.transpose(); cout << "Broadcasting result: " << endl; cout << mat << endl; }
usingnamespace std; usingnamespace Eigen; intmain() { 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 << ")\n\n";
RowVector4i v = RowVector4i::Random(); int maxOfV = v.maxCoeff(&i); cout << "Here is the vector v: " << v << endl; cout << "Its maximum coefficient (" << maxOfV << ") is at position " << i << endl; return0; }
输出:
1 2 3 4 5 6 7 8
Here is the matrix m: 0.680375 0.59688 -0.329554 -0.211234 0.823295 0.536459 0.566198 -0.604897 -0.444451 Its minimum coefficient (-0.604897) is at position (2,1)
Here is the vector v: 115899597 -48539462 276748203 -290373134 Its maximum coefficient (276748203) is at position 2