The Library

- Algorithms
- API Wrappers
- Bayesian Nets
- Compression
- Containers
- Graph Tools
- Image Processing
- Linear Algebra
- Machine Learning
- Metaprogramming
- Miscellaneous
- Networking
- Optimization
- Parsing

Help/Info

- Examples: C++
  - 3D_Point_Cloud
  - Assignment_Learning
  - Base64_Encoder
  - Bayesian_Network
  - Bayesian_Network_From_Disk
  - Bayesian_Network_GUI
  - Bridge
  - BSP
  - C-Support_Vector_Machine
  - Cmd_Line_Parser
  - Compress_Stream
  - Config_File_Reader
  - Custom_Trainers
  - Directory_Navigation
  - Empirical_Kernel_Map
  - Face_Detection
  - Face_Landmark_Detection
  - FHOG_Feature_Extraction
  - FHOG_Object_Detection
  - Graph_Labeling
  - GUI
  - Hough_Transform
  - HTTP_Server
  - Image
  - IO Socket Streams
  - IO Streams Server
  - Kernel_Centroid
  - Kernel_K-Means_Clustering
  - Kernel_Ridge_Regression
  - Kernel_RLS_Filtering
  - Kernel_RLS_Regression
  - KRR_Classification
  - Learning_to_Track
  - Linear_Assignment_Problems
  - Linear_Manifold_Regularizer
  - Logger
  - Logger_Advanced
  - Logger_Custom_Output
  - Matrix
  - Matrix_Expressions
  - Member_Function_Pointer
  - Model_Selection
  - Multiclass_Classification
  - Multithreaded_Object
  - Neural_Network
  - Non-Linear Least Squares
  - Nu-Support_Vector_Machine
  - Numerical_Integration
  - Object_Detector
  - Object_Detector_Advanced
  - One_Class_Classifiers
  - Online_SVM
  - Optimization
  - Parallel_For_Loops
  - Pipe
  - Pipe_2
  - Quantum_Computing
  - Queue
  - Rank_Features
  - Relevance_Vector_Classification
  - Relevance_Vector_Regression
  - Running_Stats
  - Sequence_Labeling
  - Sequence_Segmentation
  - Sockets
  - Sockstreambuf
  - Sparse_Vectors
  - SQLite
  - Std_C++_Allocator
  - Structural_Support_Vector_Machines
  - Support_Vector_Regression
  - SURF
  - SVM-Rank
  - Threaded_Object
  - Threads
  - Thread_Function
  - Thread_Pool
  - Timer
  - Train_Object_Detector
  - Train_Shape_Predictor
  - Using_Custom_Kernels
  - Video_Object_Tracking
  - Webcam_Face_Pose_Estimation
  - XML_Parser
- Examples: Python
  - Face_Detector
  - Face_Landmark_Detection
  - find_candidate_object_locations
  - Linear_Assignment_Problems
  - Sequence_Segmenter
  - Structural_Support_Vector_Machines
  - SVM-Rank
  - Train_Object_Detector
  - Train_Shape_Predictor
- FAQ
- Home
- How to compile
- How to contribute
- Index
- Introduction
- License
- Python API
- Suggested Books

Current Release

- Change Log
- Release Notes
- Version: 18.15

Last Modified: Mar 21, 2015

# Linear Algebra

This page documents the core linear algebra tools included in dlib. In particular, the three most important objects in this part of the library are the matrix, vector, and rectangle. All the other tools on this page are functions for manipulating these three objects. A good example and introduction can be found in the matrix example program.

Most of the linear algebra tools deal with dense matrices. However, there is also a limited amount of support for working with sparse matrices and vectors. In particular, the dlib tools represent sparse vectors using the containers in the C++ STL. For details, see the notes at the top of dlib/svm/sparse_vector_abstract.h.

Finally, note that all the dense matrix tools can be obtained by #including `<dlib/matrix.h>` while the sparse vector tools can be obtained by #including `<dlib/sparse_vector.h>`. The geometry tools can be used by #including `<dlib/geometry.h>`.

Dense Matrix Tools

- Basic_Math_Operators
  - abs
  - acos
  - asin
  - atan
  - ceil
  - complex_matrix
  - conj
  - cos
  - cosh
  - cubed
  - exp
  - floor
  - imag
  - log10
  - log
  - normalize
  - norm
  - pow
  - real
  - reciprocal
  - reciprocal_max
  - round
  - round_zeros
  - sigmoid
  - sinh
  - sin
  - sqrt
  - squared
  - tanh
  - tan
- Conversions
  - mat
  - matrix_cast
  - pixel_to_vector
  - vector_to_pixel
- Linear_Algebra
  - chol
  - cholesky_decomposition
  - det
  - diag
  - diagm
  - dot
  - eigenvalue_decomposition
  - inv
  - inv_lower_triangular
  - inv_upper_triangular
  - length
  - length_squared
  - lowerm
  - lu_decomposition
  - orthogonalize
  - pinv
  - qr_decomposition
  - real_eigenvalues
  - svd2
  - svd3
  - svd
  - svd_fast
  - trace
  - trans
  - upperm
- matrix
- Other_Utilities
  - cartesian_product
  - clamp
  - const_temp_matrix
  - conv
  - conv_same
  - conv_valid
  - equal
  - fft
  - flip
  - fliplr
  - flipud
  - hash
  - identity_matrix
  - ifft
  - is_col_vector
  - is_finite
  - is_row_vector
  - is_vector
  - join_cols
  - join_rows
  - linpiece
  - linspace
  - logspace
  - lowerbound
  - make_symmetric
  - ones_matrix
  - pointwise_multiply
  - removerc
  - remove_col
  - remove_row
  - reshape
  - reshape_to_column_vector
  - rotate
  - rsort_columns
  - scale_columns
  - scale_rows
  - set_all_elements
  - sort_columns
  - symmetric_matrix_cache
  - tensor_product
  - tmp
  - uniform_matrix
  - upperbound
  - xcorr
  - xcorr_same
  - xcorr_valid
  - zeros_matrix
- Statistics
  - covariance
  - find_min_and_max
  - gaussian_randm
  - index_of_max
  - index_of_min
  - max
  - max_point
  - max_point_interpolated
  - mean
  - min
  - min_point
  - prod
  - randm
  - stddev
  - sum
  - sum_cols
  - sum_rows
  - variance
- Sub_Matrix_Expressions
  - colm
  - range
  - rowm
  - set_colm
  - set_rowm
  - set_subm
  - subm

2D/3D Geometry

- border_enumerator
- camera_transform
- center
- centered_rect
- clip_line_to_rectangle
- dcenter
- distance_to_line
- distance_to_rect_edge
- drectangle
- find_affine_transform
- find_projective_transform
- find_similarity_transform
- get_rect
- grow_rect
- move_rect
- nearest_point
- point
- point_rotator
- point_transform
- point_transform_affine
- point_transform_affine3d
- point_transform_projective
- rectangle
- resize_rect
- resize_rect_height
- resize_rect_width
- rotate_around_x
- rotate_around_y
- rotate_around_z
- rotate_point
- rotation_matrix
- set_aspect_ratio
- shrink_rect
- translate_point
- translate_rect
- vector

Sparse Vector Tools

- add
- add_to
- assign
- distance
- distance_squared
- dot
- length
- length_squared
- make_sparse_vector
- make_sparse_vector_inplace
- max
- max_index_plus_one
- min
- scale_by
- sparse_matrix_vector_multiply
- sparse_to_dense
- subtract
- subtract_from

[top]

# border_enumerator

This object is an enumerator over the border points of a rectangle.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# camera_transform

This object maps 3D points into the image plane of a camera. Therefore, you can use it to compute 2D representations of 3D data from the point of view of some camera in 3D space.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# center

Returns the center point of a rectangle.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# centered_rect

There are various overloads of this function but the basic idea is that it returns a rectangle with a given width and height and centered about a given point.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# clip_line_to_rectangle

This function takes a rectangle and a line segment and returns the part of the line segment that is entirely contained within the rectangle.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# dcenter

Returns the center point of a rectangle. This is a version of center() which returns a double version of the point rather than one which uses integers to represent the result. Therefore, it is slightly more accurate.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# distance_to_line

This function takes a line and a point and returns the distance from the line to the point.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# distance_to_rect_edge

This function takes a rectangle and a point and returns the Manhattan distance between the rectangle's edge and the point.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# drectangle

This object represents a rectangular region inside a Cartesian coordinate system. It is very similar to the rectangle except that it uses double variables instead of longs to represent the location of the rectangle. Therefore, it can position rectangles with sub-pixel accuracy.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# find_affine_transform

This is a routine that takes in two sets of points and finds the best affine transformation that maps between them.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# find_projective_transform

This is a routine that takes in two sets of points and finds the best projective transformation that maps between them.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# find_similarity_transform

This is a routine that takes in two sets of points and finds the best affine transformation that maps between them. However, it considers only rotations, translations, and uniform scale changes in finding the mapping. Therefore, it finds a similarity transformation rather than a general affine transform.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# get_rect

This is a simple template function that returns a rectangle representing the size of a 2D container (e.g. matrix or array2d).
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# grow_rect

This function takes a rectangle object, grows its borders by a given amount, and returns the result.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# mat

This is a set of simple functions that take objects like std::vector or array2d and convert them into matrix objects. Note that the conversion is done using template expressions so there is no runtime cost associated with calling mat().
`#include <dlib/matrix.h>`
Detailed Documentation
[top]

# matrix

This is a 2D matrix object that enables you to write code that deals with matrices using a simple syntax similar to what can be written in MATLAB. It is implemented using the expression templates technique which allows it to eliminate the temporary matrix objects that would normally be returned from expressions such as M = A+B+C+D; Normally each invocation of the + operator would construct and return a temporary matrix object but using this technique we can avoid creating all these temporary objects and receive a large speed boost.

This object is also capable of using BLAS and LAPACK libraries such as ATLAS or the Intel MKL when available. To enable BLAS support all you have to do is #define DLIB_USE_BLAS and then make sure you link your application with your BLAS library. Similarly, to enable LAPACK support just #define DLIB_USE_LAPACK and link to your LAPACK library. Finally, the use of BLAS and LAPACK is transparent to the user, that is, the dlib matrix object uses BLAS and LAPACK internally to optimize various operations while still allowing the user to use a simple MATLAB like syntax.

Note that the cmake files that come with dlib will automatically link with ATLAS or the Intel MKL if they are installed. So using cmake makes this easy, but by no means are you required to use cmake or the dlib cmake files.

It is also worth noting that all the preconditions of every function related to the matrix object are checked by DLIB_ASSERT statements and thus can be enabled by #defining ENABLE_ASSERTS or DEBUG. Doing this will cause your program to run slower but should catch any usage errors.

`#include <dlib/matrix.h>`
Detailed Documentation
C++ Example Programs: matrix_ex.cpp, matrix_expressions_ex.cpp

# Extensions to matrix

matrix_la
This extension contains linear algebra functions to calculate QR, LU, Cholesky, eigenvalue, and singular value decompositions. It also contains a few other miscellaneous functions that solve systems of equations or calculate values derived from the above decompositions.
Detailed Documentation
matrix_math_functions
This extension contains mathematical functions that operate on each element of a matrix independently.
Detailed Documentation
matrix_sub_expressions
This extension contains a number of functions for dealing with sub-matrices.
Detailed Documentation
matrix_utilities
This extension contains miscellaneous utility functions for manipulating matrix objects.
Detailed Documentation
[top]

# move_rect

This function takes a rectangle and moves it so that it's upper left corner occupies the given location.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# nearest_point

This function takes a rectangle and a point and returns the point in the given rectangle that is nearest to the given point.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# point

This object represents a point inside a Cartesian coordinate system. Note that a point is simply a typedef for a vector that is 2D and uses longs to represent coordinate values.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# point_rotator

This is an object that rotates a 2D vector or point object about the origin.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# point_transform

This is an object that rotates a 2D vector or point object about the origin and then adds a displacement vector.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# point_transform_affine

This is an object that applies a 2D affine transformation to a vector or point. Note that you can use find_affine_transform to easily create affine transforms from sets of point correspondences.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# point_transform_affine3d

This is an object that applies a 3D affine transformation to a vector.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# point_transform_projective

This is an object that applies a projective transformation to a vector or point. Note that you can use find_projective_transform to easily create projective transforms from sets of point correspondences.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# rectangle

This object represents a rectangular region inside a Cartesian coordinate system. It allows you to easily represent and manipulate rectangles.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# resize_rect

This function takes a rectangle and returns a new rectangle with the given size but with the same upper left corner as the original rectangle.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# resize_rect_height

This function takes a rectangle and returns a new rectangle with the given height but otherwise with the same edge points as the original rectangle.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# resize_rect_width

This function takes a rectangle and returns a new rectangle with the given width but otherwise with the same edge points as the original rectangle.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# rotate_around_x

This is a method for creating a point_transform_affine3d that rotates points around the x-axis.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# rotate_around_y

This is a method for creating a point_transform_affine3d that rotates points around the y-axis.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# rotate_around_z

This is a method for creating a point_transform_affine3d that rotates points around the z-axis.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# rotate_point

This is a function that rotates a 2D vector or point object about a given point.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# rotation_matrix

This is a method for creating 2D rotation matrices.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# set_aspect_ratio

This function reshapes a rectangle so that it has a user specified aspect ratio.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# shrink_rect

This function takes a rectangle object, shrinks its borders by a given amount, and returns the result.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# sparse_to_dense

This is a set of simple functions that take sparse vectors and converts them into equivalent dense vectors.
`#include <dlib/sparse_vector.h>`
Detailed Documentation
[top]

# translate_point

This is a method for creating a point_transform_affine3d that just translates points.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# translate_rect

This function takes a rectangle and moves it by a given number of units along the x and y axis relative to where it was before the move.
`#include <dlib/geometry.h>`
Detailed Documentation
[top]

# vector

This object represents a two or three dimensional vector.

If you want to work with general N-dimensional column vectors then you should the matrix object. In particular, you should usually use a matrix with this type: `dlib::matrix<double,0,1>`.

`#include <dlib/geometry.h>`
Detailed Documentation