Array.inl

Go to the documentation of this file.
00001 //Copyright (c) 2004-2005, Baris Sumengen
00002 //All rights reserved.
00003 //
00004 // CIMPL Matrix Performance Library
00005 //
00006 //Redistribution and use in source and binary
00007 //forms, with or without modification, are
00008 //permitted provided that the following
00009 //conditions are met:
00010 //
00011 //    * No commercial use is allowed. 
00012 //    This software can only be used
00013 //    for non-commercial purposes. This 
00014 //    distribution is mainly intended for
00015 //    academic research and teaching.
00016 //    * Redistributions of source code must
00017 //    retain the above copyright notice, this
00018 //    list of conditions and the following
00019 //    disclaimer.
00020 //    * Redistributions of binary form must
00021 //    mention the above copyright notice, this
00022 //    list of conditions and the following
00023 //    disclaimer in a clearly visible part 
00024 //    in associated product manual, 
00025 //    readme, and web site of the redistributed 
00026 //    software.
00027 //    * Redistributions in binary form must
00028 //    reproduce the above copyright notice,
00029 //    this list of conditions and the
00030 //    following disclaimer in the
00031 //    documentation and/or other materials
00032 //    provided with the distribution.
00033 //    * The name of Baris Sumengen may not be
00034 //    used to endorse or promote products
00035 //    derived from this software without
00036 //    specific prior written permission.
00037 //
00038 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
00039 //HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00040 //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
00041 //NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00042 //MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00043 //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00044 //CONTRIBUTORS BE LIABLE FOR ANY
00045 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00046 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00047 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00048 //OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00049 //DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00050 //HOWEVER CAUSED AND ON ANY THEORY OF
00051 //LIABILITY, WHETHER IN CONTRACT, STRICT
00052 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00053 //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00054 //OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00055 //POSSIBILITY OF SUCH DAMAGE.
00056 
00057 
00058 
00059 // Template Implementation
00060 
00061 
00062 template< class T >
00063 Array<T>::Array() 
00064     : ndims(0), length(0)
00065 {
00066     dims = 0;
00067     data = 0;
00068     clean = 0;
00069 
00070 }
00071 
00072 
00073 template< class T >
00074 Array<T>::Array(const int xdim)
00075 {
00076     if(xdim < 1)
00077     {
00078         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00079         Utility::RunTimeError("All array dimensions should be larger than 0!");
00080     }
00081 
00082     ndims = 1;
00083     length = xdim;
00084     dims = new int[1];
00085     Utility::CheckPointer(dims);
00086     dims[0] = xdim;
00087     data = new T[length];
00088     Utility::CheckPointer(data);
00089 
00090     clean = new Cleaner<T>(data);
00091     
00092 }
00093 
00094 
00095 template< class T >
00096 Array<T>::Array(const int xdim, const int ydim)
00097 {
00098     if(xdim < 1 || ydim < 1)
00099     {
00100         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00101         Utility::RunTimeError("All array dimensions should be larger than 0!");
00102     }
00103 
00104     ndims = 2;
00105     length = xdim*ydim;
00106     dims = new int[2];
00107     Utility::CheckPointer(dims);
00108     dims[0] = xdim;
00109     dims[1] = ydim;
00110     data = new T[length];
00111     Utility::CheckPointer(data);
00112 
00113     clean = new Cleaner<T>(data);
00114     
00115 }
00116 
00117 
00118 template< class T >
00119 Array<T>::Array(const int xdim, const int ydim, const int zdim)
00120 {
00121     if(xdim < 1 || ydim < 1 || zdim < 1)
00122     {
00123         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00124         Utility::RunTimeError("All array dimensions should be larger than 0!");
00125     }
00126     ndims = 3;
00127     length = xdim*ydim*zdim;
00128     dims = new int[3];
00129     Utility::CheckPointer(dims);
00130     dims[0] = xdim;
00131     dims[1] = ydim;
00132     dims[2] = zdim;
00133     data = new T[length];
00134     Utility::CheckPointer(data);
00135 
00136     clean = new Cleaner<T>(data);
00137 
00138 }
00139 
00140 
00141 template< class T >
00142 Array<T>::Array(const int xdim, const int ydim, const int zdim, const int extradim, ...)
00143 {
00144     int *temp = new int[10];
00145     Utility::CheckPointer(temp);
00146     
00147     temp[0] = xdim;
00148     temp[1] = ydim;
00149     temp[2] = zdim;
00150     temp[3] = extradim;
00151 
00152     va_list argptr;
00153     va_start(argptr, extradim);
00154     int i = 4;
00155     int a;
00156     while((a = va_arg(argptr, int)) > 0 && i<10)
00157     {
00158         temp[i] = a;
00159         i++;
00160     }
00161     va_end(argptr);
00162 
00163     if(a>=0)
00164     {
00165         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00166         Utility::RunTimeError("To create multidimensional arrays of dimension 4 or higher, an extra dimension should be entered as -1 at the end. Dimensionality of arrays cannot exceed 10 !");
00167     }
00168 
00169     ndims = i;
00170     length = 1;
00171     dims = new int[ndims];
00172     Utility::CheckPointer(dims);
00173 
00174     for(int j=0;j<ndims;j++)
00175     {
00176         if(temp[j] < 1)
00177         {
00178             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00179             Utility::RunTimeError("All array dimensions should be larger than 1!");
00180         }
00181 
00182         length *= temp[j];
00183         dims[j] = temp[j];
00184     }
00185     data = new T[length];
00186     Utility::CheckPointer(data);
00187 
00188     clean = new Cleaner<T>(data);
00189     
00190 }
00191 
00192 
00193 template< class T >
00194 Array<T>::Array(const int _ndims, const int* _dims) 
00195     : ndims(_ndims)
00196 {
00197     dims = new int[ndims];
00198     Utility::CheckPointer(dims);
00199     
00200     length = 1;
00201     for(int j=0;j<ndims;j++)
00202     {
00203         dims[j] = _dims[j];
00204         length *= _dims[j];
00205     }
00206     data = new T[length];
00207     Utility::CheckPointer(data);
00208 
00209     clean = new Cleaner<T>(data);
00210 
00211 }
00212 
00213 
00214 // copies only the data pointer.
00215 template< class T >
00216 Array<T>::Array(Array<T> &m) 
00217     : ndims(m.ndims), length(m.length)
00218 {
00219     dims = new int[ndims];
00220     Utility::CheckPointer(dims);
00221     
00222     for(int j=0;j<ndims;j++)
00223     {
00224         dims[j] = m.dims[j];
00225     }
00226 
00227     data = m.data;
00228 
00229     clean = new Cleaner<T>(data);
00230 
00231 }
00232 
00233 
00234 template< class T >
00235 Array<T>::~Array()
00236 {
00237 //  cout << "Array destroyed" << endl;
00238     delete [] dims;
00239     
00240     // Collect garbage
00241     delete clean;
00242 }
00243 
00244 
00245 // Useful for passing data to third party libraries.
00246 template< class T >
00247 inline const T* Array<T>::DataPtr()
00248 {
00249     return data;
00250 }
00251 
00252 template< class T >
00253 inline T* Array<T>::Data()
00254 {
00255     return data;
00256 }
00257 
00258 
00259 
00260 template< class T >
00261 Array<T> Array<T>::Clone() const
00262 {
00263     Array<T> temp(this->ndims, this->dims);
00264 
00265     // memcopy here...
00266     for(int j=0;j<temp.length;j++)
00267     {
00268         temp.data[j] = this->data[j];
00269     }
00270 
00271 
00272     return temp;
00273 }
00274 
00275 
00276 
00277 
00278 
00279 
00280 template< class T >
00281 inline const int Array<T>::XDim() const
00282 {
00283     return dims[0];
00284 }
00285 
00286 template< class T >
00287 inline const int Array<T>::YDim() const
00288 {
00289     if(ndims < 2)
00290     {
00291         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00292         Utility::RunTimeError("YDim(): Number of dimensions is less than 2!");
00293     }
00294     return dims[1];
00295 }
00296 
00297 template< class T >
00298 inline const int Array<T>::ZDim() const
00299 {
00300     if(ndims < 3)
00301     {
00302         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00303         Utility::RunTimeError("ZDim(): Number of dimensions is less than 3!");
00304     }
00305     return dims[2];
00306 }
00307 
00308 template< class T >
00309 inline const int* Array<T>::Dims() const
00310 {
00311     return dims;
00312 }
00313 
00314 template< class T >
00315 inline const int* Array<T>::Size() const
00316 {
00317     return dims;
00318 }
00319 
00320 template< class T >
00321 inline int Array<T>::Length() const
00322 {
00323     return length;
00324 }
00325 
00326 template< class T >
00327 inline int Array<T>::NDims() const
00328 {
00329     return ndims;
00330 }
00331 
00332 template< class T >
00333 inline void Array<T>::Init(const T init)
00334 {
00335     for(int i=0;i<length;i++)
00336     {
00337         data[i] = init;
00338     }
00339 }
00340 
00341 
00342 
00343 
00344 
00345 
00346 
00347 
00348 
00349 
00350 
00351 
00352 
00353 template< class T >
00354 bool Array<T>::IsCompatible(Array<T>& m1, Array<T>& m2)
00355 {
00356     bool temp = true;
00357     if(m1.ndims != m2.ndims)
00358     {
00359         temp = false;
00360     }
00361     else
00362     {
00363         for(int i=0; i<m1.ndims; i++)
00364         {
00365             if(m1.dims[i] != m2.dims[i])
00366             {
00367                 temp = false;
00368                 break;
00369             }
00370         }
00371     }
00372 
00373     return temp;
00374 }
00375 
00376 
00377 
00378 
00379 
00380 // Boolean Operations...
00381 template< class T >
00382 Array<int> Array<T>::And(Array<T>& m1, Array<T>& m2)
00383 {
00384     if(!Array<T>::IsCompatible(m1, m2))
00385     {
00386         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00387         Utility::RunTimeError("Array sizes are not compatible!");
00388     }
00389 
00390     Array<int> temp(m1.ndims, m1.dims);
00391     for(int i=0;i<temp.Length();i++)
00392     {
00393         temp[i] = (m1.data[i] != 0 && m2.data[i] != 0) ? 1 : 0;
00394     }
00395     
00396     return temp;
00397 }
00398 
00399 
00400 template< class T >
00401 Array<int> Array<T>::Or(Array<T>& m1, Array<T>& m2)
00402 {
00403     if(!Array<T>::IsCompatible(m1, m2))
00404     {
00405         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00406         Utility::RunTimeError("Array sizes are not compatible!");
00407     }
00408 
00409     Array<int> temp(m1.ndims, m1.dims);
00410     for(int i=0;i<temp.Length();i++)
00411     {
00412         temp[i] = (m1.data[i] == 0 && m2.data[i] == 0) ? 0 : 1;
00413     }
00414     
00415     return temp;
00416 }
00417 
00418 
00419 template< class T >
00420 Array<int> Array<T>::Lt(Array<T>& m1, Array<T>& m2)
00421 {
00422     if(!Array<T>::IsCompatible(m1, m2))
00423     {
00424         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00425         Utility::RunTimeError("Array sizes are not compatible!");
00426     }
00427 
00428     Array<int> temp(m1.ndims, m1.dims);
00429     for(int i=0;i<temp.Length();i++)
00430     {
00431         temp[i] = (m1.data[i] < m2.data[i]) ? 1 : 0;
00432     }
00433     
00434     return temp;
00435 }
00436 
00437 
00438 template< class T >
00439 Array<int> Array<T>::Gt(Array<T>& m1, Array<T>& m2)
00440 {
00441     if(!Array<T>::IsCompatible(m1, m2))
00442     {
00443         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00444         Utility::RunTimeError("Array sizes are not compatible!");
00445     }
00446 
00447     Array<int> temp(m1.ndims, m1.dims);
00448     for(int i=0;i<temp.Length();i++)
00449     {
00450         temp[i] = (m1.data[i] > m2.data[i]) ? 1 : 0;
00451     }
00452     
00453     return temp;
00454 }
00455 
00456 
00457 template< class T >
00458 Array<int> Array<T>::Le(Array<T>& m1, Array<T>& m2)
00459 {
00460     if(!Array<T>::IsCompatible(m1, m2))
00461     {
00462         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00463         Utility::RunTimeError("Array sizes are not compatible!");
00464     }
00465 
00466     Array<int> temp(m1.ndims, m1.dims);
00467     for(int i=0;i<temp.Length();i++)
00468     {
00469         temp[i] = (m1.data[i] <= m2.data[i]) ? 1 : 0;
00470     }
00471     
00472     return temp;
00473 }
00474 
00475 
00476 template< class T >
00477 Array<int> Array<T>::Ge(Array<T>& m1, Array<T>& m2)
00478 {
00479     if(!Array<T>::IsCompatible(m1, m2))
00480     {
00481         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00482         Utility::RunTimeError("Array sizes are not compatible!");
00483     }
00484 
00485     Array<int> temp(m1.ndims, m1.dims);
00486     for(int i=0;i<temp.Length();i++)
00487     {
00488         temp[i] = (m1.data[i] >= m2.data[i]) ? 1 : 0;
00489     }
00490     
00491     return temp;
00492 }
00493 
00494 
00495 template< class T >
00496 Array<int> Array<T>::Eq(Array<T>& m1, Array<T>& m2)
00497 {
00498     if(!Array<T>::IsCompatible(m1, m2))
00499     {
00500         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00501         Utility::RunTimeError("Array sizes are not compatible!");
00502     }
00503 
00504     Array<int> temp(m1.ndims, m1.dims);
00505     for(int i=0;i<temp.Length();i++)
00506     {
00507         temp[i] = (m1.data[i] == m2.data[i]) ? 1 : 0;
00508     }
00509     
00510     return temp;
00511 }
00512 
00513 
00514 template< class T >
00515 Array<int> Array<T>::Ne(Array<T>& m1, Array<T>& m2)
00516 {
00517     if(!Array<T>::IsCompatible(m1, m2))
00518     {
00519         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00520         Utility::RunTimeError("Array sizes are not compatible!");
00521     }
00522 
00523     Array<int> temp(m1.ndims, m1.dims);
00524     for(int i=0;i<temp.Length();i++)
00525     {
00526         temp[i] = (m1.data[i] != m2.data[i]) ? 1 : 0;
00527     }
00528     
00529     return temp;
00530 }
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538 
00539 // Boolean Operations with value types...
00540 template< class T >
00541 Array<int> Array<T>::And(Array<T>& m1, T v)
00542 {
00543     Array<int> temp(m1.ndims, m1.dims);
00544     for(int i=0;i<temp.Length();i++)
00545     {
00546         temp[i] = (m1.data[i] != 0 && v != 0) ? 1 : 0;
00547     }
00548     
00549     return temp;
00550 }
00551 
00552 
00553 template< class T >
00554 Array<int> Array<T>::Or(Array<T>& m1, T v)
00555 {
00556     Array<int> temp(m1.ndims, m1.dims);
00557     for(int i=0;i<temp.Length();i++)
00558     {
00559         temp[i] = (m1.data[i] == 0 && v == 0) ? 0 : 1;
00560     }
00561     
00562     return temp;
00563 }
00564 
00565 
00566 template< class T >
00567 Array<int> Array<T>::Lt(Array<T>& m1, T v)
00568 {
00569     Array<int> temp(m1.ndims, m1.dims);
00570     for(int i=0;i<temp.Length();i++)
00571     {
00572         temp[i] = (m1.data[i] < v) ? 1 : 0;
00573     }
00574     
00575     return temp;
00576 }
00577 
00578 
00579 template< class T >
00580 Array<int> Array<T>::Gt(Array<T>& m1, T v)
00581 {
00582     Array<int> temp(m1.ndims, m1.dims);
00583     for(int i=0;i<temp.Length();i++)
00584     {
00585         temp[i] = (m1.data[i] > v) ? 1 : 0;
00586     }
00587     
00588     return temp;
00589 }
00590 
00591 
00592 template< class T >
00593 Array<int> Array<T>::Le(Array<T>& m1, T v)
00594 {
00595     Array<int> temp(m1.ndims, m1.dims);
00596     for(int i=0;i<temp.Length();i++)
00597     {
00598         temp[i] = (m1.data[i] <= v) ? 1 : 0;
00599     }
00600     
00601     return temp;
00602 }
00603 
00604 
00605 template< class T >
00606 Array<int> Array<T>::Ge(Array<T>& m1, T v)
00607 {
00608     Array<int> temp(m1.ndims, m1.dims);
00609     for(int i=0;i<temp.Length();i++)
00610     {
00611         temp[i] = (m1.data[i] >= v) ? 1 : 0;
00612     }
00613     
00614     return temp;
00615 }
00616 
00617 
00618 template< class T >
00619 Array<int> Array<T>::Eq(Array<T>& m1, T v)
00620 {
00621     Array<int> temp(m1.ndims, m1.dims);
00622     for(int i=0;i<temp.Length();i++)
00623     {
00624         temp[i] = (m1.data[i] == v) ? 1 : 0;
00625     }
00626     
00627     return temp;
00628 }
00629 
00630 
00631 template< class T >
00632 Array<int> Array<T>::Ne(Array<T>& m1, T v)
00633 {
00634     Array<int> temp(m1.ndims, m1.dims);
00635     for(int i=0;i<temp.Length();i++)
00636     {
00637         temp[i] = (m1.data[i] != v) ? 1 : 0;
00638     }
00639     
00640     return temp;
00641 }
00642 
00643 
00644 
00645 
00646 
00647 
00648 
00649 
00650 
00651 
00652 // Add matrix to another matrix
00653 template< class T >
00654 Array<T> Array<T>::Add(Array<T>& m1, Array<T>& m2)
00655 {
00656     if(!Array<T>::IsCompatible(m1, m2))
00657     {
00658         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00659         Utility::RunTimeError("Array sizes are not compatible!");
00660     }
00661 
00662     Array<T> temp(m1.ndims, m1.dims);
00663     for(int i=0;i<temp.length;i++)
00664     {
00665         temp.data[i] = m1.data[i] + m2.data[i];
00666     }
00667     
00668     return temp;
00669 }
00670 
00671 
00672 template< class T >
00673 Array<T> Array<T>::Subtract(Array<T>& m1, Array<T>& m2)
00674 {
00675     if(!Array<T>::IsCompatible(m1, m2))
00676     {
00677         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00678         Utility::RunTimeError("Array sizes are not compatible!");
00679     }
00680 
00681     Array<T> temp(m1.ndims, m1.dims);
00682     for(int i=0;i<temp.length;i++)
00683     {
00684         temp.data[i] = m1.data[i] - m2.data[i];
00685     }
00686     
00687     return temp;
00688 }
00689 
00690 
00691 template< class T >
00692 Array<T> Array<T>::Multiply(Array<T>& m1, Array<T>& m2)
00693 {
00694     if(!Array<T>::IsCompatible(m1, m2))
00695     {
00696         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00697         Utility::RunTimeError("Array sizes are not compatible!");
00698     }
00699 
00700     Array<T> temp(m1.ndims, m1.dims);
00701     for(int i=0;i<temp.length;i++)
00702     {
00703         temp.data[i] = m1.data[i] * m2.data[i];
00704     }
00705     
00706     return temp;
00707 }
00708 
00709 
00710 template< class T >
00711 Array<T> Array<T>::Divide(Array<T>& m1, Array<T>& m2)
00712 {
00713     if(!Array<T>::IsCompatible(m1, m2))
00714     {
00715         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00716         Utility::RunTimeError("Array sizes are not compatible!");
00717     }
00718 
00719     Array<T> temp(m1.ndims, m1.dims);
00720     for(int i=0;i<temp.length;i++)
00721     {
00722         //catch division exception instead... Future work
00723         if(m2.data[i] != 0)
00724         {
00725             temp.data[i] = m1.data[i] / m2.data[i];
00726         }
00727         else
00728         {
00729             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00730             Utility::RunTimeError("Divide by zero in matrix division!");
00731         }
00732     }
00733     
00734     return temp;
00735 }
00736 
00737 
00738 // Add value to another matrix
00739 template< class T >
00740 Array<T> Array<T>::Add(Array<T>& m1, T v2)
00741 {
00742     Array<T> temp(m1.ndims, m1.dims);
00743     for(int i=0;i<temp.length;i++)
00744     {
00745         temp.data[i] = m1.data[i] + v2;
00746     }
00747     return temp;
00748 }
00749 
00750 
00751 template< class T >
00752 Array<T> Array<T>::Subtract(Array<T>& m1, T v2)
00753 {
00754     Array<T> temp(m1.ndims, m1.dims);
00755     for(int i=0;i<temp.length;i++)
00756     {
00757         temp.data[i] = m1.data[i] - v2;
00758     }
00759     return temp;
00760 }
00761 
00762 
00763 template< class T >
00764 Array<T> Array<T>::Subtract(T v2, Array<T>& m1)
00765 {
00766     Array<T> temp(m1.ndims, m1.dims);
00767     for(int i=0;i<temp.length;i++)
00768     {
00769         temp.data[i] = v2 - m1.data[i];
00770     }
00771     return temp;
00772 }
00773 
00774 
00775 template< class T >
00776 Array<T> Array<T>::Multiply(Array<T>& m1, T v2)
00777 {
00778     Array<T> temp(m1.ndims, m1.dims);
00779     for(int i=0;i<temp.length;i++)
00780     {
00781         temp.data[i] = m1.data[i] * v2;
00782     }
00783     return temp;
00784 }
00785 
00786 
00787 template< class T >
00788 Array<T> Array<T>::Divide(Array<T>& m1, T v2)
00789 {
00790     if(v2 == 0)
00791     {
00792         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00793         Utility::RunTimeError("Divide by zero in matrix by value division!");
00794     }
00795 
00796     Array<T> temp(m1.ndims, m1.dims);
00797     for(int i=0;i<temp.length;i++)
00798     {
00799         temp.data[i] = m1.data[i] / v2;
00800     }
00801     return temp;
00802 }
00803 
00804 
00805 template< class T >
00806 Array<T> Array<T>::Divide(T v2, Array<T>& m1)
00807 {
00808     Array<T> temp(m1.ndims, m1.dims);
00809     for(int i=0;i<temp.length;i++)
00810     {
00811         if(m1.data[i] != 0)
00812         {
00813             temp.data[i] = v2 / m1.data[i];
00814         }
00815         else
00816         {
00817             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00818             Utility::RunTimeError("Divide by zero in value by matrix division!");
00819         }
00820     }
00821     return temp;
00822 }
00823 
00824 
00825 
00826 // Add matrix to "this" matrix
00827 template< class T >
00828 Array<T>& Array<T>::Add(Array<T>& m)
00829 {
00830     if(!Array<T>::IsCompatible(*this, m))
00831     {
00832         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00833         Utility::RunTimeError("Array sizes are not compatible!");
00834     }
00835 
00836     for(int i=0;i<this->length;i++)
00837     {
00838         this->data[i] += m.data[i];
00839     }
00840     
00841     return *this;
00842 }
00843 
00844 
00845 template< class T >
00846 Array<T>& Array<T>::Subtract(Array<T>& m)
00847 {
00848     if(!Array<T>::IsCompatible(*this, m))
00849     {
00850         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00851         Utility::RunTimeError("Array sizes are not compatible!");
00852     }
00853 
00854     for(int i=0;i<this->length;i++)
00855     {
00856         this->data[i] -= m.data[i];
00857     }
00858     
00859     return *this;
00860 }
00861 
00862 
00863 template< class T >
00864 Array<T>& Array<T>::Multiply(Array<T>& m)
00865 {
00866     if(!Array<T>::IsCompatible(*this, m))
00867     {
00868         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00869         Utility::RunTimeError("Array sizes are not compatible!");
00870     }
00871 
00872     for(int i=0;i<this->length;i++)
00873     {
00874         this->data[i] *= m.data[i];
00875     }
00876     
00877     return *this;
00878 }
00879 
00880 
00881 template< class T >
00882 Array<T>& Array<T>::Divide(Array<T>& m)
00883 {
00884     if(!Array<T>::IsCompatible(*this, m))
00885     {
00886         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00887         Utility::RunTimeError("Array sizes are not compatible!");
00888     }
00889 
00890     for(int i=0;i<this->length;i++)
00891     {
00892         if(m.data[i] != 0)
00893         {
00894             this->data[i] /= m.data[i];
00895         }
00896         else
00897         {
00898             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00899             Utility::RunTimeError("Divide by zero in matrix division!");
00900         }
00901     }
00902     
00903     return *this;
00904 }
00905 
00906 
00907 // Add value to "this" matrix
00908 template< class T >
00909 Array<T>& Array<T>::Add(T v)
00910 {
00911     for(int i=0;i<this->length;i++)
00912     {
00913         this->data[i] += v;
00914     }
00915     return *this;
00916 }
00917 
00918 
00919 template< class T >
00920 Array<T>& Array<T>::Subtract(T v)
00921 {
00922     for(int i=0;i<this->length;i++)
00923     {
00924         this->data[i] -= v;
00925     }
00926     return *this;
00927 }
00928 
00929 
00930 template< class T >
00931 Array<T>& Array<T>::Multiply(T v)
00932 {
00933     for(int i=0;i<this->length;i++)
00934     {
00935         this->data[i] *= v;
00936     }
00937     return *this;
00938 }
00939 
00940 
00941 template< class T >
00942 Array<T>& Array<T>::Divide(T v)
00943 {
00944     if(v == 0)
00945     {
00946         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00947         Utility::RunTimeError("Divide by zero in matrix by value division!");
00948     }
00949     
00950     for(int i=0;i<this->length;i++)
00951     {
00952         this->data[i] /= v;
00953     }
00954     return *this;
00955 }
00956 
00957 
00958 
00959 
00960 
00961 
00962 
00963 
00964 
00965 
00966 
00967 
00968 // OPERATORS
00969 
00970 
00971 template< class T >
00972 Array<T>& Array<T>::operator= (Array<T>& m)
00973 {
00974     ndims = m.ndims;
00975     length = m.length;
00976     dims = new int[ndims];
00977     Utility::CheckPointer(dims);
00978 
00979     for(int j=0;j<ndims;j++)
00980     {
00981         dims[j] = m.dims[j];
00982     }
00983     data = m.data;
00984 
00985     delete clean;
00986     clean = new Cleaner<T>(data);
00987     
00988     
00989     return *this;
00990 }
00991 
00992 template< class T >
00993 Array<T>& Array<T>::operator= (Matrix<T>& m)
00994 {
00995     ndims = m.ndims;
00996     length = m.length;
00997     dims = new int[ndims];
00998     Utility::CheckPointer(dims);
00999 
01000     dims[0] = m.xDim;
01001     dims[1] = m.yDim;
01002     data = m.data;
01003     
01004     delete clean;
01005     clean = new Cleaner<T>(data);
01006     
01007     
01008     return *this;
01009 }
01010 
01011 
01012 
01013 
01014 
01015 // Unary +-!
01016 template< class T >
01017 inline Array<T> Array<T>::operator+ ()
01018 {
01019     return *this;
01020 }
01021 
01022 template< class T >
01023 inline Array<T> Array<T>::operator- ()
01024 {
01025     Array<T> temp(this->ndims, this->dims);
01026     for(int i=0;i<length;i++)
01027     {
01028         temp.data[i] = - data[i];
01029     }
01030     return temp;
01031 }
01032 
01033 template< class T >
01034 inline Array<int> Array<T>::operator! ()
01035 {
01036     Array<int> temp(this->ndims, this->dims);
01037     for(int i=0;i<length;i++)
01038     {
01039         if(data[i] == 0)
01040         {
01041             temp[i] = 1;
01042         }
01043         else
01044         {
01045             temp[i] = 0;
01046         }
01047     }
01048     return temp;
01049 }
01050 
01051 
01052 
01053 
01054 
01055 
01056 // Access to the elements
01057 template< class T >
01058 inline T& Array<T>::operator() (const int i)
01059 {
01060     if(i<0 || i>=length)
01061     {
01062         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01063         Utility::RunTimeError("Index outside bounds!");
01064     }
01065     return data[i];
01066 }
01067 
01068 
01069 
01070 
01071     // no bounds checking...
01072 template< class T >
01073 inline T& Array<T>::operator[] (const int i)
01074 {
01075     return data[i];
01076 }
01077 
01078 
01079     
01080 
01081 template< class T >
01082 inline T& Array<T>::operator() (const int i, const int j, ...)
01083 {
01084     int *temp = new int[ndims];
01085     temp[0] = i;
01086     temp[1] = j;
01087 
01088     va_list argptr;
01089     va_start(argptr, j);
01090     int lim = 2;
01091     int a;
01092     while(lim<ndims)
01093     {
01094         a = va_arg(argptr, int);
01095         if(a<0 || a>=dims[lim])
01096         {
01097             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01098             Utility::RunTimeError("Index outside bounds!");
01099         }
01100 
01101         temp[lim] = a;
01102         lim++;
01103     }
01104     va_end(argptr);
01105 
01106     int ind = 0;
01107     for(int it=0;it<lim;it++)
01108     {
01109         int tempDim = 1;
01110         for(int it2=0;it2<it;it2++)
01111         {
01112             tempDim *= dims[it2];
01113         }
01114 
01115         ind += temp[it]*tempDim;
01116     }
01117 
01118     return data[ind];
01119 }
01120 
01121 
01122 
01124 template< class T >
01125 inline T& Array<T>::Elem(const int i)
01126 {
01127     if(i<0 || i>=length)
01128     {
01129         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01130         Utility::RunTimeError("Index outside bounds!");
01131     }
01132     return data[i];
01133 }
01134 
01136 template< class T >
01137 inline T& Array<T>::ElemNC(const int i)
01138 {
01139     return data[i];
01140 }
01141 
01142 
01144 template< class T >
01145 inline T& Array<T>::Elem(const int i, const int j, ...)
01146 {
01147     int *temp = new int[ndims];
01148     temp[0] = i;
01149     temp[1] = j;
01150 
01151     va_list argptr;
01152     va_start(argptr, j);
01153     int lim = 2;
01154     int a;
01155     while(lim<ndims)
01156     {
01157         a = va_arg(argptr, int);
01158         if(a<0 || a>=dims[lim])
01159         {
01160             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01161             Utility::RunTimeError("Index outside bounds!");
01162         }
01163 
01164         temp[lim] = a;
01165         lim++;
01166     }
01167     va_end(argptr);
01168 
01169     int ind = 0;
01170     for(int it=0;it<lim;it++)
01171     {
01172         int tempDim = 1;
01173         for(int it2=0;it2<it;it2++)
01174         {
01175             tempDim *= dims[it2];
01176         }
01177 
01178         ind += temp[it]*tempDim;
01179     }
01180 
01181     return data[ind];
01182 }
01183 
01184 
01186 template< class T >
01187 inline T& Array<T>::ElemNC(const int i, const int j, ...)
01188 {
01189     int *temp = new int[ndims];
01190     temp[0] = i;
01191     temp[1] = j;
01192 
01193     va_list argptr;
01194     va_start(argptr, j);
01195     int lim = 2;
01196     int a;
01197     while(lim<ndims)
01198     {
01199         a = va_arg(argptr, int);
01200         temp[lim] = a;
01201         lim++;
01202     }
01203     va_end(argptr);
01204 
01205     int ind = 0;
01206     for(int it=0;it<lim;it++)
01207     {
01208         int tempDim = 1;
01209         for(int it2=0;it2<it;it2++)
01210         {
01211             tempDim *= dims[it2];
01212         }
01213 
01214         ind += temp[it]*tempDim;
01215     }
01216 
01217     return data[ind];
01218 }
01219 
01220 
01221 
01222 
01223 
01224 // Add inline
01225 template< class T >
01226 Array<T>& Array<T>::operator+= (Array<T>& m)
01227 {
01228     return this->Add(m);
01229 }
01230 
01231 template< class T >
01232 Array<T>& Array<T>::operator-= (Array<T>& m)
01233 {
01234     return this->Subtract(m);
01235 }
01236 
01237 template< class T >
01238 Array<T>& Array<T>::operator*= (Array<T>& m)
01239 {
01240     return this->Multiply(m);
01241 }
01242 
01243 template< class T >
01244 Array<T>& Array<T>::operator/= (Array<T>& m)
01245 {
01246     return this->Divide(m);
01247 }
01248 
01249 
01250 template< class T >
01251 Array<T>& Array<T>::operator+= (T v)
01252 {
01253     return this->Add(v);
01254 }
01255 
01256 template< class T >
01257 Array<T>& Array<T>::operator-= (T v)
01258 {
01259     return this->Subtract(v);
01260 }
01261 
01262 template< class T >
01263 Array<T>& Array<T>::operator*= (T v)
01264 {
01265     return this->Multiply(v);
01266 }
01267 
01268 template< class T >
01269 Array<T>& Array<T>::operator/= (T v)
01270 {
01271     return this->Divide(v);
01272 }
01273 
01274 
01275 
01276 template< class T >
01277 Array<T>::Array(Matrix<T> &m) 
01278 {
01279     ndims = m.ndims;
01280     length = m.length;
01281     dims = new int[ndims];
01282     Utility::CheckPointer(dims);
01283     
01284     dims[0] = m.xDim;
01285     dims[1] = m.yDim;
01286     data = m.data;
01287     
01288     clean = new Cleaner<T>(data);
01289     
01290 }
01291 
01292 
01293 
01294 
01295 
01296 
01297 
01298 
01299 
01300 
01301 
01302 

CIMPL 0.1 Code Reference. Copyright © 2004, Baris Sumengen. All rights reserved.