MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/matrix_ex.cpp:17–272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15// ----------------------------------------------------------------------------------------
16
17int main()
18{
19 // Let's begin this example by using the library to solve a simple
20 // linear system.
21 //
22 // We will find the value of x such that y = M*x where
23 //
24 // 3.5
25 // y = 1.2
26 // 7.8
27 //
28 // and M is
29 //
30 // 54.2 7.4 12.1
31 // M = 1 2 3
32 // 5.9 0.05 1
33
34
35 // First let's declare these 3 matrices.
36 // This declares a matrix that contains doubles and has 3 rows and 1 column.
37 // Moreover, its size is a compile time constant since we put it inside the <>.
38 matrix<double,3,1> y;
39 // Make a 3 by 3 matrix of doubles for the M matrix. In this case, M is
40 // sized at runtime and can therefore be resized later by calling M.set_size().
41 matrix<double> M(3,3);
42
43 // You may be wondering why someone would want to specify the size of a
44 // matrix at compile time when you don't have to. The reason is two fold.
45 // First, there is often a substantial performance improvement, especially
46 // for small matrices, because it enables a number of optimizations that
47 // otherwise would be impossible. Second, the dlib::matrix object checks
48 // these compile time sizes to ensure that the matrices are being used
49 // correctly. For example, if you attempt to compile the expression y*y you
50 // will get a compiler error since that is not a legal matrix operation (the
51 // matrix dimensions don't make sense as a matrix multiplication). So if
52 // you know the size of a matrix at compile time then it is always a good
53 // idea to let the compiler know about it.
54
55
56
57
58 // Now we need to initialize the y and M matrices and we can do so like this:
59 M = 54.2, 7.4, 12.1,
60 1, 2, 3,
61 5.9, 0.05, 1;
62
63 y = 3.5,
64 1.2,
65 7.8;
66
67
68 // The solution to y = M*x can be obtained by multiplying the inverse of M
69 // with y. As an aside, you should *NEVER* use the auto keyword to capture
70 // the output from a matrix expression. So don't do this: auto x = inv(M)*y;
71 // To understand why, read the matrix_expressions_ex.cpp example program.
72 matrix<double> x = inv(M)*y;
73
74 cout << "x: \n" << x << endl;

Callers

nothing calls this directly

Calls 15

yFunction · 0.85
pointwise_multiplyFunction · 0.85
roundFunction · 0.85
submFunction · 0.85
set_all_elementsFunction · 0.85
set_submFunction · 0.85
set_colmFunction · 0.85
set_rowmFunction · 0.85
detFunction · 0.85
pinvFunction · 0.85
svdFunction · 0.85
cholFunction · 0.85

Tested by

no test coverage detected