Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "SundanceIntVec.hpp"
00044 #include "SundanceDebug.hpp"
00045 #include "SundanceCombinatorialUtils.hpp"
00046
00047 namespace Sundance
00048 {
00049 using Teuchos::Array;
00050
00051 IntVec::IntVec(int n)
00052 : data_(n)
00053 {
00054 for (int i=0; i<n; i++) data_[i] = 0;
00055 }
00056
00057 IntVec IntVec::operator+(const IntVec& other) const
00058 {
00059 TEUCHOS_TEST_FOR_EXCEPT(size() != other.size());
00060
00061 IntVec rtn(size());
00062 for (int i=0; i<size(); i++) rtn[i] = data_[i] + other[i];
00063
00064 return rtn;
00065 }
00066
00067
00068 IntVec IntVec::operator*(int a) const
00069 {
00070 IntVec rtn(size());
00071 for (int i=0; i<size(); i++) rtn[i] = a*data_[i];
00072
00073 return rtn;
00074 }
00075
00076 int IntVec::factorial() const
00077 {
00078 int rtn=1;
00079
00080 for (int i=0; i<size(); i++)
00081 {
00082 int n_i = data_[i];
00083 for (int j=1; j<=n_i; j++) rtn *= j;
00084 }
00085 return rtn;
00086 }
00087
00088
00089 int IntVec::pow(const IntVec& other) const
00090 {
00091 TEUCHOS_TEST_FOR_EXCEPT(size() != other.size());
00092 int rtn=1;
00093
00094 for (int i=0; i<size(); i++)
00095 {
00096 int n_i = data_[i];
00097 int p_i = other[i];
00098 for (int j=1; j<=p_i; j++) rtn *= n_i;
00099 }
00100 return rtn;
00101 }
00102
00103 int IntVec::abs() const
00104 {
00105 int rtn = 0;
00106 for (int i=0; i<size(); i++)
00107 {
00108 rtn += ::abs(data_[i]);
00109 }
00110 return rtn;
00111 }
00112
00113 int IntVec::norm() const
00114 {
00115 int rtn = 0;
00116 for (int i=0; i<size(); i++)
00117 {
00118 if (::abs(data_[i]) > rtn) rtn = ::abs(data_[i]);
00119 }
00120 return rtn;
00121 }
00122
00123 bool IntVec::operator==(const IntVec& other) const
00124 {
00125 if (size() != other.size()) return false;
00126 for (int i=0; i<size(); i++)
00127 {
00128 if (data_[i] != other.data_[i]) return false;
00129 }
00130 return true;
00131 }
00132
00133 bool IntVec::operator<(const IntVec& other) const
00134 {
00135 if (size() < other.size()) return true;
00136 if (size() > other.size()) return false;
00137 if (abs() < other.abs()) return true;
00138 if (abs() > other.abs()) return false;
00139
00140 for (int i=0; i<size(); i++)
00141 {
00142 if (data_[i] < other.data_[i]) return true;
00143 if (data_[i] > other.data_[i]) return false;
00144 }
00145
00146 return false;
00147 }
00148
00149 void IntVec::print(std::ostream& os) const
00150 {
00151 os << "IVec[" << data_ << "]";
00152 }
00153
00154
00155 void IntVec::getPartitions(int M, Array<Array<IntVec> >& parts) const
00156 {
00157 Array<Array<Array<int> > > rComp(size());
00158 Array<int> radix(size());
00159
00160 for (int i=0; i<size(); i++)
00161 {
00162 restrictedCompositions(data_[i], M, rComp[i]);
00163 radix[i] = rComp[i].size();
00164 }
00165
00166 Array<int> pick(size(), 0);
00167 bool workLeft = true;
00168 while (workLeft)
00169 {
00170 Array<IntVec> part(M);
00171 for (int j=0; j<M; j++) part[j] = IntVec(size());
00172 for (int i=0; i<size(); i++)
00173 {
00174 TEUCHOS_TEST_FOR_EXCEPT(pick[i] >= rComp[i].size());
00175 const Array<int>& p = rComp[i][pick[i]];
00176 TEUCHOS_TEST_FOR_EXCEPT(p.size() != M);
00177 for (int j=0; j<M; j++) part[j][i] = p[j];
00178 }
00179 bool isNonzero = true;
00180 for (int i=0; i<part.size(); i++)
00181 {
00182 if (part[i].abs()==0) {isNonzero = false; break;}
00183 }
00184 if (isNonzero) parts.append(part);
00185 workLeft = nextNum(pick, radix);
00186 }
00187 }
00188
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198