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