TMB Documentation  v1.9.11
order.hpp
1 // Copyright (C) 2013-2015 Kasper Kristensen
2 // License: GPL-2
3 
8 namespace tmbutils {
9 
19 template <class Type>
20 class order{
21 public:
22  vector<Type> iperm;
23  matrix<Type> P;
24  int n;
26  Type Zero=0;
27  Type One=1;
28  n=x.size();
29  iperm.resize(n);
30  iperm.setZero();
31  P.resize(n,n);
32  P.setZero();
33  /* Permutation vector - n^2 comparisons */
34  for(int i=0;i<n;i++)
35  for(int j=0;j<n;j++)
36  iperm[i]+=CppAD::CondExpLt(x[j], x[i], One, Zero );
37  /* Ties correction - n^2/2 comparisons */
38  for(int i=0;i<n;i++)
39  for(int j=0;j<i;j++)
40  iperm[i]+=CppAD::CondExpEq(x[j], x[i], One, Zero );
41  /* Corresponding permutation matrix - n^2 comparisons */
42  for(int i=0;i<n;i++)
43  for(int j=0;j<n;j++)
44  P(j,i)=CppAD::CondExpEq(Type(j), iperm[i], One, Zero );
45  }
46  /* Apply permutation on other vector */
47  vector<Type> operator()(vector<Type> x){
48  vector<Type> y(x.size());
49  y.setZero();
50  /* y=P%*%x - n^2 flops */
51  for(int i=0;i<n;i++)
52  for(int j=0;j<n;j++)
53  y[i]+=P(i,j)*x[j];
54  return y;
55  }
56  /* Apply permutation on outer dimension of array */
57  array<Type> operator()(array<Type> x){
58  array<Type> y(x);
59  y.setZero();
60  /* y=P%*%x - n^2 flops */
61  for(int i=0;i<n;i++)
62  for(int j=0;j<n;j++)
63  y.row(i)+=x(j)*P(i,j);
64  return y;
65  }
66 
67 };
68 
69 }
Array class used by TMB.
Definition: tmbutils.hpp:23
Taped sorting of a vector.
Definition: order.hpp:20
Matrix class used by TMB.
Definition: tmbutils.hpp:102
Vector class used by TMB.
Definition: tmbutils.hpp:18
Utility functions for TMB (automatically included)
Definition: concat.hpp:5
License: GPL v2