c_cpp dlib矩阵运算
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp dlib矩阵运算相关的知识,希望对你有一定的参考价值。
#include "pch.h"
#include <iostream>
#include <dlib/matrix.h>
using namespace std;
int main(void) {
// 3 rows and 1 columns.
dlib::matrix<double> y(3, 1);
// 3 rows 3 columns.
dlib::matrix<double> M(3, 3);
// 3.5
// y = 1.2
// 7.8
y = 3.5,
1.2,
7.8;
// 54.2 7.4 12.1
// M = 1 2 3
// 5.9 0.05 1
M = 54.2, 7.4, 12.1,
1, 2, 3,
5.9, 0.05, 1;
// The solution to y = M*x can be obtained by multiplying the inverse of M with y.
dlib::matrix<double> x = dlib::inv(M) * y;
//std::cout << "x: \n" << x << std::endl;
// We can check that it really worked by plugging x back into the original equation
// i.e. M * x - y. It should be extremely small that closed to 0;
// run-time sized column/row vector declaration
dlib::matrix<double, 0, 1> runtime_sized_column_vector;
dlib::matrix<double, 1, 0> runtime_sized_row_vector;
// now column vector has the size of 3.
runtime_sized_column_vector.set_size(3);
// similarly to matrix x
// i.e. now it's 3 rows and 4 columns
x.set_size(3, 4);
// Matrix element at row 0 and col 1
// M(0,1) == 7.4
cout << M(0, 1) << endl;
// Column/Row vector element at position 1
// y(1) == 1.2
cout << y(1) << endl;
// Compute sum of matrix
// -------------------------
// double M_sum = 0;
// for (long r = 0; r < M.nr(); r++) {
// for (long c = 0; c < M.nc(); c++) {
// M_sum += M(r, c);
// }
// }
// -------------------------
cout << dlib::sum(M) << endl;
// print matrix
cout << M << endl;
// print matrix using comma separators
// It is also possible to read in a matrix that uses either space, tab, or comma
// separated values by uncommenting the following:
// cin >> M;
cout << dlib::csv << M << endl;
// -------------- common MATLAB functions in dlib --------------
dlib::matrix<double> A, B, C, D, E;
dlib::matrix<int> Aint;
dlib::matrix<long> Blong;
// A = eye(3)
A = dlib::identity_matrix<double>(3);
// B = ones(3,4)
B = dlib::ones_matrix<double>(3, 4);
// B = rand(3,4)
B = dlib::randm(3, 4);
// C = 1.4*A
C = 1.4*A;
// MATLAB: D = A.*C
D = dlib::pointwise_multiply(A, C);
// MATLAB: E = A * B
E = A * B;
// MATLAB: E = A + B
E = A + C;
// MATLAB: E = A + 5
E = A + 5;
// MATLAB: E = E'
E = dlib::trans(E); // Note that if you want a conjugate transpose then you need to say conj(trans(E))
// MATLAB: E = B' * B
E = dlib::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 = dlib::round(C);
// MATLAB: C = floor(C)
C = dlib::floor(C);
// MATLAB: C = ceil(C)
C = dlib::ceil(C);
// MATLAB: C = diag(B)
C = dlib::diag(B);
// MATLAB: B = cast(A, "int32")
Aint = dlib::matrix_cast<int>(A);
// MATLAB: A = B(1,:)
A = dlib::rowm(B, 0);
// MATLAB: A = B([1:2],:)
A = dlib::rowm(B, dlib::range(0, 1));
// MATLAB: A = B(:,1)
A = dlib::colm(B, 0);
// MATLAB: A = [1:5]
Blong = dlib::range(1, 5);
// MATLAB: A = [1:2:5]
Blong = dlib::range(1, 2, 5);
// MATLAB: A = B([1:3], [1:2])
A = dlib::subm(B, dlib::range(0, 2), dlib::range(0, 1));
// or equivalently
A = dlib::subm(B, dlib::rectangle(0, 0, 1, 2));
// MATLAB: A = B([1:3], [1:2:4])
A = dlib::subm(B, dlib::range(0, 2), dlib::range(0, 2, 3));
// MATLAB: B(:,:) = 5
B = 5;
// or equivalently
dlib::set_all_elements(B, 5);
// MATLAB: B([1:2],[1,2]) = 7
dlib::set_subm(B, dlib::range(0, 1), dlib::range(0, 1)) = 7;
// MATLAB: B([1:3],[2:3]) = A
dlib::set_subm(B, dlib::range(0, 2), dlib::range(1, 2)) = A;
// MATLAB: B(:,1) = 4
dlib::set_colm(B, 0) = 4;
// MATLAB: B(:,[1:2]) = 4
dlib::set_colm(B, dlib::range(0, 1)) = 4;
// MATLAB: B(:,1) = B(:,2)
dlib::set_colm(B, 0) = dlib::colm(B, 1);
// MATLAB: B(1,:) = 4
dlib::set_rowm(B, 0) = 4;
// MATLAB: B(1,:) = B(2,:)
dlib::set_rowm(B, 0) = dlib::rowm(B, 1);
// MATLAB: var = det(E' * E)
var = dlib::det(dlib::trans(E)*E);
// MATLAB: C = pinv(E)
C = dlib::pinv(E);
// MATLAB: C = inv(E)
C = dlib::inv(E);
// MATLAB: [A,B,C] = svd(E)
dlib::svd(E, A, B, C);
// MATLAB: A = chol(E,'lower')
A = dlib::chol(E);
// MATLAB: var = min(min(A))
var = dlib::min(A);
return 0;
}
以上是关于c_cpp dlib矩阵运算的主要内容,如果未能解决你的问题,请参考以下文章