关于matrix的样例学习,去繁留简,方便日后使用:http://dlib.net/matrix_ex.cpp.html <http://dlib.net/linear_algebra.html>
// y = M*x, 求解x
//
// 3.5
// y = 1.2
// 7.8
//
// 54.2 7.4 12.1
// M = 1 2 3
// 5.9 0.05 1
matrix<double,3,1> y; // 编译的时候固定y的大小
matrix<double> M(3,3); // M的大小在runtime时确定,也可以用M.set_size()之后resize
// Now we need to initialize the y and M matrices and we can do so like this:
M = 54.2, 7.4, 12.1,
1, 2, 3,
5.9, 0.05, 1;
y = 3.5,
1.2,
7.8;
// 不可以用auto定义x,像auto x = inv(M)*y; read the matrix_expressions_ex.cpp example program.
matrix<double> x = inv(M)*y;
cout << "x: \n" << x << endl;
// 可以用M*x - y 是否为0反向检验,some numerical error and round off可能导致值不一定准确为0
cout << "M*x - y: \n" << M*x - y << endl;
// 创建run-time sized column or row vectors like so
matrix<double,0,1> runtime_sized_column_vector;
matrix<double,1,0> runtime_sized_row_vector;
// 确定vector具体大小
runtime_sized_column_vector.set_size(3);
// 相似的,确定matrix具体大小
x.set_size(3,4); // x now has 3 rows and 4 columns.
// matrix和vector里的取出元素:
cout << M(0,1) << endl; // value 7.4.
cout << y(1) << endl; // value 1.2
// 遍历M matrix,算所有元素之和
double M_sum = 0;
// loop over all the rows
for (long r = 0; r < M.nr(); ++r)
{
// loop over all the columns
for (long c = 0; c < M.nc(); ++c)
{
M_sum += M(r,c);
}
}
cout << "sum of all elements in M is " << M_sum << endl;
// 简单版算所有元素之和
cout << "sum of all elements in M is " << sum(M) << endl;
// ostream M:
cout << M << endl;
// which will print:
// 54.2 7.4 12.1
// 1 2 3
// 5.9 0.05 1
// 用逗号分隔元素的ostream M:
cout << csv << M << endl;
// and you will instead get this as output:
// 54.2, 7.4, 12.1
// 1, 2, 3
// 5.9, 0.05, 1
// read in a matrix that uses either space, tab, or comma separated values:
cin >> M;
// ----------------------------- Comparison with MATLAB ------------------------------
matrix<double> A, B, C, D, E;
matrix<int> Aint;
matrix<long> Blong;
// MATLAB: A = eye(3)
A = identity_matrix<double>(3);
// MATLAB: B = ones(3,4)
B = ones_matrix<double>(3,4);
// MATLAB: B = rand(3,4)
B = randm(3,4);
// MATLAB: C = 1.4*A
C = 1.4*A;
// MATLAB: D = A.*C
D = pointwise_multiply(A,C);
// MATLAB: E = A * B
E = A*B;
// MATLAB: E = A + C
E = A + C;
// MATLAB: E = A + 5
E = A + 5;
// MATLAB: E = E'
E = trans(E); // Note that if you want a conjugate transpose then you need to say conj(trans(E))
// MATLAB: E = B' * B
E = trans(B)*B;
double var;
// MATLAB: var = A(1,2)
var = A(0,1); // dlib::matrix is 0 indexed rather than starting at 1 like Matlab.
// MATLAB: C = round(C)
C = round(C);
// MATLAB: C = floor(C)
C = floor(C);
// MATLAB: C = ceil(C)
C = ceil(C);
// MATLAB: C = diag(B)
C = diag(B);
// MATLAB: B = cast(A, "int32")
Aint = matrix_cast<int>(A);
// MATLAB: A = B(1,:)
A = rowm(B,0);
// MATLAB: A = B([1:2],:)
A = rowm(B,range(0,1));
// MATLAB: A = B(:,1)
A = colm(B,0);
// MATLAB: A = [1:5]
Blong = range(1,5);
// MATLAB: A = [1:2:5]
Blong = range(1,2,5);
// MATLAB: A = B([1:3], [1:2])
A = subm(B, range(0,2), range(0,1));
// or equivalently
A = subm(B, rectangle(0,0,1,2));
// MATLAB: A = B([1:3], [1:2:4])
A = subm(B, range(0,2), range(0,2,3));
// MATLAB: B(:,:) = 5
B = 5;
// or equivalently
set_all_elements(B,5);
// MATLAB: B([1:2],[1,2]) = 7
set_subm(B,range(0,1), range(0,1)) = 7;
// MATLAB: B([1:3],[2:3]) = A
set_subm(B,range(0,2), range(1,2)) = A;
// MATLAB: B(:,1) = 4
set_colm(B,0) = 4;
// MATLAB: B(:,[1:2]) = 4
set_colm(B,range(0,1)) = 4;
// MATLAB: B(:,1) = B(:,2)
set_colm(B,0) = colm(B,1);
// MATLAB: B(1,:) = 4
set_rowm(B,0) = 4;
// MATLAB: B(1,:) = B(2,:)
set_rowm(B,0) = rowm(B,1);
// MATLAB: var = det(E' * E)
var = det(trans(E)*E);
// MATLAB: C = pinv(E)
C = pinv(E);
// MATLAB: C = inv(E)
C = inv(E);
// MATLAB: [A,B,C] = svd(E)
svd(E,A,B,C);
// MATLAB: A = chol(E,'lower')
A = chol(E);
// MATLAB: var = min(min(A))
var = min(A);
}