Commit a5d30218 authored by Davis King's avatar Davis King

Added the mat() function. It rolls the array_to_matrix(), vector_to_matrix(),

pointer_to_column_vector(), and pointer_to_matrix() methods all into one
convenient interface.  Also made stddev() slightly more general.

This change also deprecates the previous matrix conversion functions.
parent 30097efb
This diff is collapsed.
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_MATRIx_MAT_ABSTRACT_H__
#ifdef DLIB_MATRIx_MAT_ABSTRACT_H__
#include "matrix_abstract.h"
#inclue <vector>
#include "../array/array_kernel_abstract.h"
#include "../array2d/array2d_kernel_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
const matrix_exp<EXP>& mat (
const matrix_exp<EXP>& m
);
/*!
ensures
- returns m
(i.e. this function just returns the input matrix without any modifications)
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename MM
>
const matrix_exp mat (
const array2d<T,MM>& array
);
/*!
ensures
- returns a matrix R such that:
- R.nr() == array.nr()
- R.nc() == array.nc()
- for all valid r and c:
R(r, c) == array[r][c]
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename MM
>
const matrix_exp mat (
const array<T,MM>& m
);
/*!
ensures
- returns a matrix R such that:
- is_col_vector(R) == true
- R.size() == m.size()
- for all valid r:
R(r) == m[r]
!*/
// ----------------------------------------------------------------------------------------
template <
typename value_type,
typename alloc
>
const matrix_exp mat (
const std::vector<value_type,alloc>& vector
);
/*!
ensures
- returns a matrix R such that:
- is_col_vector(R) == true
- R.size() == vector.size()
- for all valid r:
R(r) == vector[r]
!*/
// ----------------------------------------------------------------------------------------
template <
typename value_type,
typename alloc
>
const matrix_exp mat (
const std_vector_c<value_type,alloc>& vector
);
/*!
ensures
- returns a matrix R such that:
- is_col_vector(R) == true
- R.size() == vector.size()
- for all valid r:
R(r) == vector[r]
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp mat (
const T* ptr,
long nr
);
/*!
requires
- nr > 0
- ptr == a pointer to at least nr T objects
ensures
- returns a matrix M such that:
- M.nr() == nr
- m.nc() == 1
- for all valid i:
M(i) == ptr[i]
- Note that the returned matrix doesn't take "ownership" of
the pointer and thus will not delete or free it.
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp mat (
const T* ptr,
long nr,
long nc
);
/*!
requires
- nr > 0
- nc > 0
- ptr == a pointer to at least nr*nc T objects
ensures
- returns a matrix M such that:
- M.nr() == nr
- m.nc() == nc
- for all valid r and c:
M(r,c) == ptr[r*nc + c]
(i.e. the pointer is interpreted as a matrix laid out in memory
in row major order)
- Note that the returned matrix doesn't take "ownership" of
the pointer and thus will not delete or free it.
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_MATRIx_MAT_ABSTRACT_H__
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "matrix_math_functions.h" #include "matrix_math_functions.h"
#include "matrix_op.h" #include "matrix_op.h"
#include "../general_hash/random_hashing.h" #include "../general_hash/random_hashing.h"
#include "matrix_mat.h"
namespace dlib namespace dlib
...@@ -280,246 +281,6 @@ namespace dlib ...@@ -280,246 +281,6 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename array_type
>
const typename enable_if<is_matrix<array_type>,array_type>::type&
array_to_matrix (
const array_type& array
)
{
return array;
}
// ----------------------------------------------------------------------------------------
template <typename T>
struct op_array2d_to_mat : does_not_alias
{
op_array2d_to_mat( const T& array_) : array(array_){}
const T& array;
const static long cost = 1;
const static long NR = 0;
const static long NC = 0;
typedef typename T::type type;
typedef const typename T::type& const_ret_type;
typedef typename T::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long c ) const { return array[r][c]; }
long nr () const { return array.nr(); }
long nc () const { return array.nc(); }
};
template <
typename array_type
>
const typename disable_if<is_matrix<array_type>,matrix_op<op_array2d_to_mat<array_type> > >::type
array_to_matrix (
const array_type& array
)
{
typedef op_array2d_to_mat<array_type> op;
return matrix_op<op>(op(array));
}
// ----------------------------------------------------------------------------------------
template <typename T>
struct op_array_to_mat : does_not_alias
{
op_array_to_mat( const T& vect_) : vect(vect_){}
const T& vect;
const static long cost = 1;
const static long NR = 0;
const static long NC = 1;
typedef typename T::type type;
typedef const typename T::type& const_ret_type;
typedef typename T::mem_manager_type mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long ) const { return vect[r]; }
long nr () const { return vect.size(); }
long nc () const { return 1; }
};
template <
typename vector_type
>
const typename disable_if<is_matrix<vector_type>, matrix_op<op_array_to_mat<vector_type> > >::type
vector_to_matrix (
const vector_type& vector
)
{
typedef op_array_to_mat<vector_type> op;
return matrix_op<op>(op(vector));
}
// ----------------------------------------------------------------------------------------
template <
typename vector_type
>
const typename enable_if<is_matrix<vector_type>,vector_type>::type& vector_to_matrix (
const vector_type& vector
)
/*!
This overload catches the case where the argument to this function is
already a matrix.
!*/
{
return vector;
}
// ----------------------------------------------------------------------------------------
template <typename T>
struct op_std_vect_to_mat : does_not_alias
{
op_std_vect_to_mat( const T& vect_) : vect(vect_){}
const T& vect;
const static long cost = 1;
const static long NR = 0;
const static long NC = 1;
typedef typename T::value_type type;
typedef const typename T::value_type& const_ret_type;
typedef default_memory_manager mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long ) const { return vect[r]; }
long nr () const { return vect.size(); }
long nc () const { return 1; }
};
template <
typename value_type,
typename alloc
>
const matrix_op<op_std_vect_to_mat<std::vector<value_type,alloc> > > vector_to_matrix (
const std::vector<value_type,alloc>& vector
)
{
typedef op_std_vect_to_mat<std::vector<value_type,alloc> > op;
return matrix_op<op>(op(vector));
}
// ----------------------------------------------------------------------------------------
template <
typename value_type,
typename alloc
>
const matrix_op<op_std_vect_to_mat<std_vector_c<value_type,alloc> > > vector_to_matrix (
const std_vector_c<value_type,alloc>& vector
)
{
typedef op_std_vect_to_mat<std_vector_c<value_type,alloc> > op;
return matrix_op<op>(op(vector));
}
// ----------------------------------------------------------------------------------------
template <typename T>
struct op_pointer_to_col_vect : does_not_alias
{
op_pointer_to_col_vect(
const T* ptr_,
const long size_
) : ptr(ptr_), size(size_){}
const T* ptr;
const long size;
const static long cost = 1;
const static long NR = 0;
const static long NC = 1;
typedef T type;
typedef const T& const_ret_type;
typedef default_memory_manager mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long ) const { return ptr[r]; }
long nr () const { return size; }
long nc () const { return 1; }
};
template <
typename T
>
const matrix_op<op_pointer_to_col_vect<T> > pointer_to_column_vector (
const T* ptr,
long nr
)
{
DLIB_ASSERT(nr > 0 ,
"\tconst matrix_exp pointer_to_column_vector(ptr, nr)"
<< "\n\t nr must be bigger than 0"
<< "\n\t nr: " << nr
);
typedef op_pointer_to_col_vect<T> op;
return matrix_op<op>(op(ptr, nr));
}
// ----------------------------------------------------------------------------------------
template <typename T>
struct op_pointer_to_mat : does_not_alias
{
op_pointer_to_mat(
const T* ptr_,
const long nr_,
const long nc_
) : ptr(ptr_), rows(nr_), cols(nc_){}
const T* ptr;
const long rows;
const long cols;
const static long cost = 1;
const static long NR = 0;
const static long NC = 0;
typedef T type;
typedef const T& const_ret_type;
typedef default_memory_manager mem_manager_type;
typedef row_major_layout layout_type;
const_ret_type apply (long r, long c) const { return ptr[r*cols + c]; }
long nr () const { return rows; }
long nc () const { return cols; }
};
template <
typename T
>
const matrix_op<op_pointer_to_mat<T> > pointer_to_matrix (
const T* ptr,
long nr,
long nc
)
{
DLIB_ASSERT(nr > 0 && nc > 0 ,
"\tconst matrix_exp pointer_to_matrix(ptr, nr, nc)"
<< "\n\t nr and nc must be bigger than 0"
<< "\n\t nr: " << nr
<< "\n\t nc: " << nc
);
typedef op_pointer_to_mat<T> op;
return matrix_op<op>(op(ptr,nr,nc));
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template <typename M> template <typename M>
...@@ -1776,7 +1537,9 @@ namespace dlib ...@@ -1776,7 +1537,9 @@ namespace dlib
const matrix_exp<EXP>& m const matrix_exp<EXP>& m
) )
{ {
return std::sqrt(variance(m)); using std::sqrt;
using dlib::sqrt;
return sqrt(variance(m));
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -456,102 +456,6 @@ namespace dlib ...@@ -456,102 +456,6 @@ namespace dlib
M(r,c) == m(m.nr()-r-1, m.nc()-c-1) M(r,c) == m(m.nr()-r-1, m.nc()-c-1)
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename vector_type
>
const matrix_exp vector_to_matrix (
const vector_type& vector
);
/*!
requires
- vector_type is an implementation of array/array_kernel_abstract.h or
std::vector or dlib::std_vector_c or dlib::matrix
ensures
- if (vector_type is a dlib::matrix) then
- returns a reference to vector
- else
- returns a matrix R such that:
- is_col_vector(R) == true
- R.size() == vector.size()
- for all valid r:
R(r) == vector[r]
!*/
// ----------------------------------------------------------------------------------------
template <
typename array_type
>
const matrix_exp array_to_matrix (
const array_type& array
);
/*!
requires
- array_type is an implementation of array2d/array2d_kernel_abstract.h
or dlib::matrix
ensures
- if (array_type is a dlib::matrix) then
- returns a reference to array
- else
- returns a matrix R such that:
- R.nr() == array.nr()
- R.nc() == array.nc()
- for all valid r and c:
R(r, c) == array[r][c]
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp pointer_to_matrix (
const T* ptr,
long nr,
long nc
);
/*!
requires
- nr > 0
- nc > 0
- ptr == a pointer to at least nr*nc T objects
ensures
- returns a matrix M such that:
- M.nr() == nr
- m.nc() == nc
- for all valid r and c:
M(r,c) == ptr[r*nc + c]
(i.e. the pointer is interpreted as a matrix laid out in memory
in row major order)
- Note that the returned matrix doesn't take "ownership" of
the pointer and thus will not delete or free it.
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
const matrix_exp pointer_to_column_vector (
const T* ptr,
long nr
);
/*!
requires
- nr > 0
- ptr == a pointer to at least nr T objects
ensures
- returns a matrix M such that:
- M.nr() == nr
- m.nc() == 1
- for all valid i:
M(i) == ptr[i]
- Note that the returned matrix doesn't take "ownership" of
the pointer and thus will not delete or free it.
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
const matrix_exp reshape ( const matrix_exp reshape (
...@@ -1548,7 +1452,7 @@ namespace dlib ...@@ -1548,7 +1452,7 @@ namespace dlib
); );
/*! /*!
ensures ensures
- returns std::sqrt(variance(m)) - returns sqrt(variance(m))
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment