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 #include "SundanceMultiIndex.hpp" 00043 #include "PlayaExceptions.hpp" 00044 00045 using namespace Sundance; 00046 using namespace Sundance; 00047 00048 using namespace Sundance; 00049 using namespace Teuchos; 00050 00051 MultiIndex::MultiIndex() 00052 : m_(maxDim(), 0) 00053 {;} 00054 00055 MultiIndex::MultiIndex(int x, int y, int z) 00056 : m_(maxDim(), 0) 00057 { 00058 m_[0] = x; 00059 m_[1] = y; 00060 m_[2] = z; 00061 } 00062 00063 MultiIndex MultiIndex::operator+(const MultiIndex& other) const 00064 { 00065 MultiIndex rtn; 00066 00067 for (int i=0; i<maxDim(); i++) 00068 { 00069 rtn.m_[i] = m_[i] + other[i]; 00070 } 00071 return rtn; 00072 } 00073 00074 MultiIndex MultiIndex::operator-(const MultiIndex& other) const 00075 { 00076 MultiIndex rtn; 00077 00078 for (int i=0; i<maxDim(); i++) 00079 { 00080 rtn.m_[i] = m_[i] - other[i]; 00081 } 00082 return rtn; 00083 } 00084 00085 MultiIndex MultiIndex::operator-() const 00086 { 00087 MultiIndex rtn; 00088 00089 for (int i=0; i<maxDim(); i++) 00090 { 00091 rtn.m_[i] = -m_[i]; 00092 } 00093 return rtn; 00094 } 00095 00096 string MultiIndex::toString() const 00097 { 00098 return "(" + Teuchos::toString(m_[0]) + "," 00099 + Teuchos::toString(m_[1]) + "," 00100 + Teuchos::toString(m_[2]) + ")"; 00101 } 00102 00103 XMLObject MultiIndex::toXML() const 00104 { 00105 XMLObject rtn("MultiIndex"); 00106 rtn.addAttribute("indices", toString()); 00107 return rtn; 00108 } 00109 00110 bool MultiIndex::operator==(const MultiIndex& m) const 00111 { 00112 for (int i=0; i<maxDim(); i++) 00113 { 00114 if (m_[i] != m[i]) return false; 00115 } 00116 return true; 00117 } 00118 00119 bool MultiIndex::operator<(const MultiIndex& m) const 00120 { 00121 for (int i=0; i<maxDim(); i++) 00122 { 00123 if (m_[i] > m.m_[i]) return false; 00124 if (m_[i] < m.m_[i]) return true; 00125 } 00126 return false; 00127 } 00128 00129 00130 00131 int MultiIndex::order() const 00132 { 00133 int h = 0; 00134 for (int i=0; i<maxDim(); i++) 00135 { 00136 h += m_[i]; 00137 } 00138 return h; 00139 } 00140 00141 bool MultiIndex::isValid() const 00142 { 00143 for (int i=0; i<maxDim(); i++) 00144 { 00145 if (m_[i] < 0) return false; 00146 } 00147 return true; 00148 } 00149 00150 int MultiIndex::firstOrderDirection() const 00151 { 00152 TEUCHOS_TEST_FOR_EXCEPTION(order() != 1, std::logic_error, 00153 "bad order in MultiIndex::firstOrderDirection() const"); 00154 for (int i=0; i<maxDim(); i++) 00155 { 00156 if (m_[i] == 1) return i; 00157 } 00158 return -1; 00159 } 00160 00161 00162 00163 string MultiIndex::coordForm() const 00164 { 00165 std::string rtn; 00166 00167 for (int i=0; i<m_[0]; i++) 00168 { 00169 rtn += "x"; 00170 } 00171 for (int i=0; i<m_[1]; i++) 00172 { 00173 rtn += "y"; 00174 } 00175 for (int i=0; i<m_[2]; i++) 00176 { 00177 rtn += "z"; 00178 } 00179 return rtn; 00180 } 00181 00182