23 #ifndef O2SCL_VECTOR_H
24 #define O2SCL_VECTOR_H
56 #include <o2scl/misc.h>
57 #include <o2scl/uniform_grid.h>
72 template<
class vec_t,
class vec2_t>
75 if (dest.size()<N) dest.resize(N);
101 template<
class vec_t,
class vec2_t>
107 for(i=m;i+3<N;i+=4) {
124 template<
class mat_t,
class mat2_t>
126 size_t m=src.size1();
127 size_t n=src.size2();
128 if (dest.size1()<m || dest.size2()<n) dest.resize(m,n);
129 for(
size_t i=0;i<m;i++) {
130 for(
size_t j=0;j<n;j++) {
149 template<
class mat_t,
class mat2_t>
151 for(
size_t i=0;i<M;i++) {
152 for(
size_t j=0;j<N;j++) {
166 template<
class vec_t,
class vec2_t,
class data_t>
175 for(i=m;i+3<N;i+=4) {
198 template<
class vec_t,
class vec2_t>
200 return vector_swap<vec_t,vec2_t,double>(N,v1,v2);
208 template<
class mat_t,
class mat2_t,
class data_t>
211 for(
size_t i=0;i<M;i++) {
212 for(
size_t j=0;j<N;j++) {
226 template<
class mat_t,
class mat2_t,
class data_t>
228 return matrix_swap<mat_t,mat2_t,double>(M,N,m1,m2);
236 template<
class vec_t,
class data_t>
249 template<
class vec_t>
251 return vector_swap<vec_t,double>(v,i,j);
259 template<
class mat_t,
class data_t>
260 void matrix_swap(mat_t &m,
size_t i1,
size_t j1,
size_t i2,
size_t j2) {
261 data_t temp=m(i1,j1);
272 template<
class mat_t>
274 size_t i2,
size_t j2) {
275 return matrix_swap<mat_t,double>(m,i1,j1,i2,j2);
283 template<
class mat_t,
class data_t>
286 for(
size_t i=0;i<M;i++) {
299 template<
class mat_t>
301 return matrix_swap_cols<mat_t,double>(M,m,j1,j2);
309 template<
class mat_t,
class data_t>
312 for(
size_t j=0;j<N;j++) {
325 template<
class mat_t>
327 return matrix_swap_rows<mat_t,double>(N,m,i1,i2);
335 template<
class vec_t,
class data_t>
343 if (j<n && data[j] < data[j+1]) j++;
344 if (!(v < data[j]))
break;
388 template<
class vec_t,
class data_t>
401 sort_downheap<vec_t,data_t>(data,N,k);
409 sort_downheap<vec_t,data_t>(data,N,0);
417 template<
class vec_t,
class vec_
size_t>
421 const size_t pki = order[k];
426 if (j < N && data[order[j]] < data[order[j + 1]]) {
431 if (!(data[pki] < data[order[j]])) {
481 template<
class vec_t,
class vec_
size_t>
490 for (i = 0 ; i < n ; i++) {
504 sort_index_downheap<vec_t,vec_size_t>(N,data,order,k);
510 size_t tmp = order[0];
517 sort_index_downheap<vec_t,vec_size_t>(N,data,order,0);
532 template<
class vec_t>
534 return vector_sort<vec_t,double>(n,data);
555 template<
class vec_t,
class data_t>
558 O2SCL_ERR2(
"Subset length greater than size in ",
568 data_t xbound=data[0];
572 for(
size_t i=1;i<n;i++) {
576 }
else if (xi>=xbound) {
580 for(i1=j-1;i1>0;i1--) {
581 if (xi>smallest[i1-1])
break;
582 smallest[i1]=smallest[i1-1];
585 xbound=smallest[j-1];
605 template<
class vec_t,
class data_t>
608 O2SCL_ERR2(
"Subset length greater than size in ",
618 data_t xbound=data[0];
622 for(
size_t i=1;i<n;i++) {
626 }
else if (xi<=xbound) {
630 for(i1=j-1;i1>0;i1--) {
631 if (xi<largest[i1-1])
break;
632 largest[i1]=largest[i1-1];
645 template<
class vec_t,
class data_t>
652 for(
size_t i=1;i<n;i++) {
663 template<
class vec_t,
class data_t>
671 for(
size_t i=1;i<n;i++) {
682 template<
class vec_t,
class data_t>
691 for(
size_t i=1;i<n;i++) {
702 template<
class vec_t,
class data_t>
709 for(
size_t i=1;i<n;i++) {
720 template<
class vec_t,
class data_t>
728 for(
size_t i=1;i<n;i++) {
739 template<
class vec_t,
class data_t>
748 for(
size_t i=1;i<n;i++) {
760 template<
class vec_t,
class data_t>
762 data_t &min, data_t &max) {
769 for(
size_t i=1;i<n;i++) {
783 template<
class vec_t,
class data_t>
785 size_t &ix_min,
size_t &ix_max) {
794 for(
size_t i=1;i<n;i++) {
810 template<
class vec_t,
class data_t>
812 size_t &ix_min, data_t &min,
813 size_t &ix_max, data_t &max) {
822 for(
size_t i=1;i<n;i++) {
840 template<
class vec_t,
class data_t>
842 size_t ix=vector_max_index<vec_t,data_t>(n,data);
844 return quadratic_extremum_y<data_t>(0,1,2,data[0],data[1],data[2]);
845 }
else if (ix==n-1) {
846 return quadratic_extremum_y<data_t>
847 (n-3,n-2,n-1,data[n-3],data[n-2],data[n-1]);
849 return quadratic_extremum_y<data_t>
850 (ix-1,ix,ix+1,data[ix-1],data[ix],data[ix+1]);
855 template<
class vec_t,
class data_t>
857 size_t ix=vector_max_index<vec_t,data_t>(n,y);
858 if (ix==0 || ix==n-1)
return y[ix];
859 return quadratic_extremum_y<data_t>(x[ix-1],x[ix],x[ix+1],
860 y[ix-1],y[ix],y[ix+1]);
865 template<
class vec_t,
class data_t>
867 size_t ix=vector_max_index<vec_t,data_t>(n,y);
868 if (ix==0 || ix==n-1)
return y[ix];
869 return quadratic_extremum_x<data_t>(x[ix-1],x[ix],x[ix+1],
870 y[ix-1],y[ix],y[ix+1]);
875 template<
class vec_t,
class data_t>
877 size_t ix=vector_min_index<vec_t,data_t>(n,data);
879 return quadratic_extremum_y<data_t>(0,1,2,data[0],data[1],data[2]);
880 }
else if (ix==n-1) {
881 return quadratic_extremum_y<data_t>
882 (n-3,n-2,n-1,data[n-3],data[n-2],data[n-1]);
884 return quadratic_extremum_y<data_t>
885 (ix-1,ix,ix+1,data[ix-1],data[ix],data[ix+1]);
890 template<
class vec_t,
class data_t>
892 size_t ix=vector_min_index<vec_t,data_t>(n,y);
893 if (ix==0 || ix==n-1)
return y[ix];
894 return quadratic_extremum_y<data_t>(x[ix-1],x[ix],x[ix+1],
895 y[ix-1],y[ix],y[ix+1]);
900 template<
class vec_t,
class data_t>
902 size_t ix=vector_min_index<vec_t,data_t>(n,y);
903 if (ix==0 || ix==n-1)
return y[ix];
904 return quadratic_extremum_x<data_t>(x[ix-1],x[ix],x[ix+1],
905 y[ix-1],y[ix],y[ix+1]);
913 template<
class mat_t,
class data_t>
914 data_t
matrix_max(
size_t m,
const size_t n,
const mat_t &data) {
919 data_t max=data(0,0);
920 for(
size_t i=0;i<m;i++) {
921 for(
size_t j=0;j<n;j++) {
932 template<
class mat_t,
class data_t> data_t
matrix_max(
const mat_t &data) {
933 size_t m=data.size1();
934 size_t n=data.size2();
938 data_t max=data(0,0);
939 for(
size_t i=0;i<n;i++) {
940 for(
size_t j=0;j<m;j++) {
952 template<
class mat_t,
class data_t>
954 size_t &i_max,
size_t &j_max, data_t &max) {
962 for(
size_t i=0;i<n;i++) {
963 for(
size_t j=0;j<m;j++) {
976 template<
class mat_t,
class data_t>
977 data_t
matrix_min(
size_t n,
const size_t m,
const mat_t &data) {
982 data_t min=data(0,0);
983 for(
size_t i=0;i<n;i++) {
984 for(
size_t j=0;j<m;j++) {
996 template<
class mat_t,
class data_t>
998 size_t &i_min,
size_t &j_min, data_t &min) {
1006 for(
size_t i=0;i<n;i++) {
1007 for(
size_t j=0;j<m;j++) {
1008 if (data(i,j)<min) {
1020 template<
class mat_t,
class data_t>
1022 data_t &min, data_t &max) {
1029 for(
size_t i=0;i<n;i++) {
1030 for(
size_t j=0;j<m;j++) {
1031 if (data(i,j)<min) {
1033 }
else if (data(i,j)>max) {
1044 template<
class mat_t,
class data_t>
1046 size_t &i_min,
size_t &j_min, data_t &min,
1047 size_t &i_max,
size_t &j_max, data_t &max) {
1059 for(
size_t i=0;i<n;i++) {
1060 for(
size_t j=0;j<m;j++) {
1061 if (data(i,j)<min) {
1065 }
else if (data(i,j)>max) {
1090 template<
class vec_t>
1093 O2SCL_ERR(
"Empty vector in function vector_lookup().",
1104 double best=x[i], bdiff=fabs(x[i]-x0);
1109 bdiff=fabs(x[i]-x0);
1120 template<
class mat_t>
1122 double x0,
size_t &i,
size_t &j) {
1124 O2SCL_ERR(
"Empty matrix in matrix_lookup().",
1128 bool found_one=
false;
1129 for(
size_t i2=0;i2<m;i2++) {
1130 for(
size_t j2=0;j2<n;j2++) {
1132 if (found_one==
false) {
1133 dist=fabs(A(i,j)-x0);
1138 if (fabs(A(i,j)-x0)<dist) {
1139 dist=fabs(A(i,j)-x0);
1147 if (found_one==
false) {
1187 template<
class vec_t,
class data_t>
1189 size_t lo,
size_t hi) {
1191 O2SCL_ERR2(
"Low and high indexes backwards in ",
1192 "function vector_bsearch_inc().",
exc_einval);
1232 template<
class vec_t,
class data_t>
1234 size_t lo,
size_t hi) {
1236 O2SCL_ERR2(
"Low and high indexes backwards in ",
1237 "function vector_bsearch_dec().",
exc_einval);
1258 template<
class vec_t,
class data_t>
1260 size_t lo,
size_t hi) {
1261 if (x[lo]<x[hi-1]) {
1262 return vector_bsearch_inc<vec_t,data_t>(x0,x,lo,hi);
1264 return vector_bsearch_dec<vec_t,data_t>(x0,x,lo,hi);
1275 template<
class vec_t,
class data_t>
1279 for(
size_t i=0;i<n;i++) {
1290 template<
class vec_t,
class data_t> data_t
vector_sum(vec_t &data) {
1292 for(
size_t i=0;i<data.size();i++) {
1304 template<
class vec_t,
class data_t>
1312 }
else if (n == 1) {
1316 for (
size_t i = 0; i < n; i++) {
1317 const data_t xx = x[i];
1320 const data_t ax = fabs(xx);
1323 ssq = 1.0 + ssq * (scale / ax) * (scale / ax);
1326 ssq += (ax / scale) * (ax / scale);
1332 return scale * sqrt(ssq);
1351 template<
class vec_t,
class data_t>
1354 data_t *tmp=
new data_t[n];
1355 for(
size_t i=0;i<n;i++) {
1356 tmp[i]=data[(i+k)%n];
1358 for(
size_t i=0;i<n;i++) {
1370 template<
class vec_t,
class data_t>
1374 for(
size_t i=0;i<n/2;i++) {
1376 data[n-1-i]=data[i];
1386 template<
class vec_t,
class data_t>
1389 size_t n=data.size();
1391 for(
size_t i=0;i<n/2;i++) {
1393 data[n-1-i]=data[i];
1415 template<
class mat_t,
class mat_row_t>
1417 return mat_row_t(M,row);
1430 double &operator[](
size_t i) {
1433 const double &operator[](
size_t i)
const {
1454 template<
class mat_t,
class mat_column_t>
1456 return mat_column_t(M,column);
1469 double &operator[](
size_t i) {
1470 return m_(i,column_);
1472 const double &operator[](
size_t i)
const {
1473 return m_(i,column_);
1491 template<
class vec_t>
1493 bool endline=
false) {
1498 for(
size_t i=0;i<n-1;i++) os << v[i] <<
" ";
1500 if (endline) os << std::endl;
1518 template<
class vec_t>
1519 void vector_out(std::ostream &os,
const vec_t &v,
bool endline=
false) {
1526 for(
size_t i=0;i<n-1;i++) os << v[i] <<
" ";
1528 if (endline) os << std::endl;
1534 template<
class vec_t,
class data_t>
1536 g.template vector<vec_t>(v);
1541 template<
class mat_t>
1543 for(
size_t i=0;i<M;i++) {
1544 for(
size_t j=0;j<N;j++) {
1545 if (i==j) m(i,j)=1.0;
1554 #if defined (O2SCL_COND_FLAG) || defined (DOXYGEN)
1556 #if defined (O2SCL_ARMA) || defined (DOXYGEN)
1557 #include <armadillo>
1566 template<> arma::subview_row<double>
1567 matrix_row<arma::mat,arma::subview_row<double> >
1568 (arma::mat &M,
size_t row);
1571 template<> arma::subview_col<double>
1572 matrix_column<arma::mat,arma::subview_col<double> >
1573 (arma::mat &M,
size_t column);
1580 #if defined (O2SCL_EIGEN) || defined (DOXYGEN)
1581 #include <Eigen/Dense>
1588 double matrix_max(
const Eigen::MatrixXd &data);
1591 template<> Eigen::MatrixXd::RowXpr
1592 matrix_row<Eigen::MatrixXd,Eigen::MatrixXd::RowXpr>
1593 (Eigen::MatrixXd &M,
size_t row);
1596 template<> Eigen::MatrixXd::ColXpr
1597 matrix_column<Eigen::MatrixXd,Eigen::MatrixXd::ColXpr>
1598 (Eigen::MatrixXd &M,
size_t column);
1607 #include <o2scl/vector_special.h>
1654 template<
class T,
class F,
class A>
class matrix {
data_t vector_min_quad(size_t n, const vec_t &data)
Minimum of vector by quadratic fit.
size_t vector_lookup(size_t n, const vec_t &x, double x0)
Lookup element x0 in vector x of length n.
size_t vector_min_index(size_t n, const vec_t &data)
Compute the index which holds the minimum of the first n elements of a vector.
data_t vector_min_value(size_t n, const vec_t &data)
Compute the minimum of the first n elements of a vector.
data_t vector_max_quad_loc(size_t n, const vec_t &x, const vec_t &y)
Location of vector maximum by quadratic fit.
data_t vector_min_quad_loc(size_t n, const vec_t &x, const vec_t &y)
Location of vector minimum by quadratic fit.
void matrix_swap_rows(size_t N, mat_t &m, size_t i1, size_t i2)
Generic swap two columns in a matrix.
size_t vector_bsearch_dec(const data_t x0, const vec_t &x, size_t lo, size_t hi)
Binary search a part of an decreasing vector for x0.
Generic object which represents a column of a matrix.
void matrix_minmax(size_t n, const size_t m, const mat_t &data, data_t &min, data_t &max)
Compute the minimum and maximum of a matrix.
size_t vector_bsearch_inc(const data_t x0, const vec_t &x, size_t lo, size_t hi)
Binary search a part of an increasing vector for x0.
bool is_finite(double x)
Return false if x is infinite or not a number.
invalid argument supplied by user
Generic object which represents a row of a matrix.
void vector_reverse(size_t n, vec_t &data)
Reverse a vector.
void vector_rotate(size_t n, vec_t &data, size_t k)
"Rotate" a vector so that the kth element is now the beginning
void vector_swap_double(size_t N, vec_t &v1, vec2_t &v2)
Generic swap of of the first N elements of two double-precision vectors.
The default matrix type from uBlas.
void vector_min(size_t n, const vec_t &data, size_t &index, data_t &val)
Compute the minimum of the first n elements of a vector.
data_t vector_sum(size_t n, vec_t &data)
Compute the sum of the first n elements of a vector.
void vector_swap(size_t N, vec_t &v1, vec2_t &v2)
Swap the first N elements of two vectors.
void vector_largest(size_t n, vec_t &data, size_t k, vec_t &largest)
Find the k largest entries of a vector.
void matrix_swap_double(size_t M, size_t N, mat_t &m1, mat2_t &m2)
Generic swap of two matrices.
void vector_sort_index(size_t n, const vec_t &data, vec_size_t &order)
Create a permutation which sorts a vector (in increasing order)
void matrix_swap_cols(size_t M, mat_t &m, size_t j1, size_t j2)
Generic swap two columns in a matrix.
void matrix_copy(mat_t &src, mat2_t &dest)
Simple generic matrix copy.
data_t vector_max_value(size_t n, const vec_t &data)
Compute the maximum of the first n elements of a vector.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
data_t matrix_min(size_t n, const size_t m, const mat_t &data)
Compute the minimum of a matrix.
void vector_minmax_value(size_t n, vec_t &data, data_t &min, data_t &max)
Compute the minimum and maximum of the first n elements of a vector.
void matrix_set_identity(size_t M, size_t N, mat_t &m)
Set a matrix to unity on the diagonal and zero otherwise.
data_t vector_max_quad(size_t n, const vec_t &data)
Maximum of vector by quadratic fit.
mat_column_t matrix_column(mat_t &M, size_t column)
Construct a column of a matrix.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
void matrix_max_index(size_t n, const size_t m, const mat_t &data, size_t &i_max, size_t &j_max, data_t &max)
Compute the maximum of a matrix and return the indices of the maximum element.
mat_row_t matrix_row(mat_t &M, size_t row)
Construct a row of a matrix.
void vector_out(std::ostream &os, size_t n, const vec_t &v, bool endline=false)
Output a vector to a stream.
void vector_smallest(size_t n, vec_t &data, size_t k, vec_t &smallest)
Find the k smallest entries of a vector.
void vector_grid(uniform_grid< data_t > g, vec_t &v)
Fill a vector with a specified grid.
void vector_minmax(size_t n, vec_t &data, size_t &ix_min, data_t &min, size_t &ix_max, data_t &max)
Compute the minimum and maximum of the first n elements of a vector.
data_t vector_norm(size_t n, const vec_t &x)
Compute the norm of a vector of floating-point (single or double precision) numbers.
size_t vector_max_index(size_t n, const vec_t &data)
Compute the index which holds the maximum of the first n elements of a vector.
void vector_max(size_t n, const vec_t &data, size_t &index, data_t &val)
Compute the maximum of the first n elements of a vector.
void vector_minmax_index(size_t n, vec_t &data, size_t &ix_min, size_t &ix_max)
Compute the minimum and maximum of the first n elements of a vector.
data_t matrix_max(size_t m, const size_t n, const mat_t &data)
Compute the maximum of the lower-left part of a matrix.
size_t vector_bsearch(const data_t x0, const vec_t &x, size_t lo, size_t hi)
Binary search a part of a monotonic vector for x0.
void sort_index_downheap(size_t N, const vec_t &data, vec_size_t &order, size_t k)
Provide a downheap() function for vector_sort_index()
void matrix_minmax_index(size_t n, const size_t m, const mat_t &data, size_t &i_min, size_t &j_min, data_t &min, size_t &i_max, size_t &j_max, data_t &max)
Compute the minimum and maximum of a matrix and return their locations.
void matrix_swap_cols_double(size_t M, mat_t &m, size_t j1, size_t j2)
Generic swap two elements in a matrix.
void vector_copy(vec_t &src, vec2_t &dest)
Simple generic vector copy.
void matrix_swap(size_t M, size_t N, mat_t &v1, mat2_t &v2)
Generic swap of two matrices.
void matrix_swap_rows_double(size_t N, mat_t &m, size_t i1, size_t i2)
Generic swap two elements in a matrix.
void matrix_min_index(size_t n, const size_t m, const mat_t &data, size_t &i_min, size_t &j_min, data_t &min)
Compute the minimum of a matrix and return the indices of the minimum element.
void matrix_lookup(size_t m, size_t n, const mat_t &A, double x0, size_t &i, size_t &j)
Lookup an element in a matrix.
void vector_sort_double(size_t n, vec_t &data)
Sort a vector of doubles (in increasing order)
The default vector type from uBlas.
void vector_sort(size_t n, vec_t &data)
Sort a vector (in increasing order)
void sort_downheap(vec_t &data, size_t n, size_t k)
Provide a downheap() function for vector_sort()