SundanceIntVec.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
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 

Site Contact