| 346 | // ---------------------------------------------------------------------------------------- |
| 347 | |
| 348 | void custom_matrix_expressions_example( |
| 349 | ) |
| 350 | { |
| 351 | matrix<double> x(2,3); |
| 352 | x = 1, 1, 1, |
| 353 | 2, 2, 2; |
| 354 | |
| 355 | cout << x << endl; |
| 356 | |
| 357 | // Finally, let's use the matrix expressions we defined above. |
| 358 | |
| 359 | // prints the transpose of x |
| 360 | cout << example_trans(x) << endl; |
| 361 | |
| 362 | // prints this: |
| 363 | // 11 11 11 |
| 364 | // 12 12 12 |
| 365 | cout << add_scalar(x, 10) << endl; |
| 366 | |
| 367 | |
| 368 | // matrix expressions can be nested, even the user defined ones. |
| 369 | // the following statement prints this: |
| 370 | // 11 12 |
| 371 | // 11 12 |
| 372 | // 11 12 |
| 373 | cout << example_trans(add_scalar(x, 10)) << endl; |
| 374 | |
| 375 | // Since we setup the alias detection correctly we can even do this: |
| 376 | x = example_trans(add_scalar(x, 10)); |
| 377 | cout << "new x:\n" << x << endl; |
| 378 | |
| 379 | cout << "Do some testing with the example_vector_to_matrix() function: " << endl; |
| 380 | std::vector<float> vect; |
| 381 | vect.push_back(1); |
| 382 | vect.push_back(3); |
| 383 | vect.push_back(5); |
| 384 | |
| 385 | // Now let's treat our std::vector like a matrix and print some things. |
| 386 | cout << example_vector_to_matrix(vect) << endl; |
| 387 | cout << add_scalar(example_vector_to_matrix(vect), 10) << endl; |
| 388 | |
| 389 | |
| 390 | |
| 391 | /* |
| 392 | As an aside, note that dlib contains functions equivalent to the ones we |
| 393 | defined above. They are: |
| 394 | - dlib::trans() |
| 395 | - dlib::mat() (converts things into matrices) |
| 396 | - operator+ (e.g. you can say my_mat + 1) |
| 397 | |
| 398 | |
| 399 | Also, if you are going to be creating your own matrix expression you should also |
| 400 | look through the matrix code in the dlib/matrix folder. There you will find |
| 401 | many other examples of matrix expressions. |
| 402 | */ |
| 403 | } |
| 404 | |
| 405 | // ---------------------------------------------------------------------------------------- |
no test coverage detected