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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 namespace CIMPL
00060 {
00061
00062
00063 template< class T >
00064 Vector<T>::Vector()
00065 : length(0)
00066 {
00067 data = 0;
00068 clean = 0;
00069 memoryManaged = true;
00070 }
00071
00072
00073 template< class T >
00074 Vector<T>::Vector(const int l)
00075 {
00076 if(l < 1)
00077 {
00078 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00079 Utility::RunTimeError("Vector length should be larger than 0!");
00080 }
00081
00082 length = l;
00083 data = new (std::nothrow) T[length];
00084 Utility::CheckPointer(data);
00085
00086 clean = new (std::nothrow) Cleaner<T>(data);
00087 Utility::CheckPointer(clean);
00088 memoryManaged = true;
00089
00090 }
00091
00092
00093 template< class T >
00094 Vector<T>::Vector(string str)
00095 {
00096
00097 if(str.substr(0,1) == "[")
00098 {
00099 if(str.substr(str.size()-1,1) == "]")
00100 {
00101 str = str.substr(1,str.size()-2);
00102 vector<string> elem_strs = Utility::Split(str);
00103 length = (int)elem_strs.size();
00104 if(length <= 0)
00105 {
00106 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00107 Utility::RunTimeError("Vector length should be larger than 0!");
00108 }
00109 data = new (std::nothrow) T[length];
00110 Utility::CheckPointer(data);
00111 clean = new (std::nothrow) Cleaner<T>(data);
00112 Utility::CheckPointer(clean);
00113 memoryManaged = true;
00114
00115 vector<string>::const_iterator constIterator;
00116 int i = 0;
00117 for(constIterator = elem_strs.begin(); constIterator != elem_strs.end(); constIterator++)
00118 {
00119 data[i] = (T)Utility::ToDouble((string)*constIterator);
00120 i++;
00121 }
00122 }
00123 else
00124 {
00125 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00126 Utility::RunTimeError("Incorrect initialization. String cannot be parsed!");
00127 }
00128 }
00129 else
00130 {
00131 double start, inc, end;
00132 vector<string> bounds = Utility::Split(str, ":");
00133 if(bounds.size() == 3)
00134 {
00135 start = Utility::ToDouble(bounds[0]);
00136 inc = Utility::ToDouble(bounds[1]);
00137 end = Utility::ToDouble(bounds[2]);
00138 }
00139 else if(bounds.size() == 2)
00140 {
00141 start = Utility::ToDouble(bounds[0]);
00142 inc = 1.0;
00143 end = Utility::ToDouble(bounds[1]);
00144 }
00145 else
00146 {
00147 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00148 Utility::RunTimeError("Incorrect initialization. String cannot be parsed!");
00149 }
00150
00151 length = (int)(abs(end-start)/inc+numeric_limits<float>::epsilon())+1;
00152 if(length <= 0)
00153 {
00154 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00155 Utility::RunTimeError("Vector length should be larger than 0!");
00156 }
00157 data = new (std::nothrow) T[length];
00158 Utility::CheckPointer(data);
00159 clean = new (std::nothrow) Cleaner<T>(data);
00160 Utility::CheckPointer(clean);
00161 memoryManaged = true;
00162 for(int i=0;i<length;i++)
00163 {
00164 data[i] = (T)(start+i*inc);
00165 }
00166
00167 }
00168
00169
00170
00171
00172 }
00173
00174
00175
00176 template< class T >
00177 Vector<T>::Vector(const int l, T init)
00178 {
00179 if(l < 1)
00180 {
00181 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00182 Utility::RunTimeError("Vector length should be larger than 0!");
00183 }
00184
00185 length = l;
00186 data = new (std::nothrow) T[length];
00187 Utility::CheckPointer(data);
00188
00189 clean = new (std::nothrow) Cleaner<T>(data);
00190 Utility::CheckPointer(clean);
00191 memoryManaged = true;
00192 for(int i=0;i<length;i++)
00193 {
00194 data[i] = init;
00195 }
00196
00197 }
00198
00199
00200 template< class T >
00201 Vector<T>::Vector(T* _data, const int l)
00202 {
00203 if(l < 1)
00204 {
00205 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00206 Utility::RunTimeError("Vector length should be larger than 0!");
00207 }
00208
00209 length = l;
00210 data = _data;
00211 clean = 0;
00212 memoryManaged = false;
00213
00214 }
00215
00216
00217
00218 template< class T >
00219 Vector<T>::Vector(Vector<T> &m)
00220 {
00221 length = m.length;
00222
00223
00224 if(m.memoryManaged == true)
00225 {
00226 data = m.data;
00227 clean = new (std::nothrow) Cleaner<T>(data);
00228 Utility::CheckPointer(clean);
00229 memoryManaged = true;
00230 }
00231 else
00232 {
00233
00234 data = m.data;
00235 clean = 0;
00236 memoryManaged = false;
00237 Utility::Warning("Using copy constructor from an unmanaged vector!\nCreated a new unmanaged vector pointing to the same unmanaged memory.");
00238 }
00239
00240 }
00241
00242
00243
00244 template< class T >
00245 Vector<T>::~Vector(void)
00246 {
00247
00248 if(clean != 0 && memoryManaged == true)
00249 {
00250 delete clean;
00251 }
00252 }
00253
00254
00255
00256 template< class T >
00257 void Vector<T>::Set(T* _data, const int l)
00258 {
00259 if(l < 1)
00260 {
00261 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00262 Utility::RunTimeError("Vector length should be larger than 0!");
00263 }
00264
00265 length = l;
00266 data = _data;
00267 delete clean;
00268 clean = 0;
00269 memoryManaged = false;
00270
00271 }
00272
00273
00274 template< class T >
00275 void Vector<T>::Clean()
00276 {
00277 if(clean != 0 && memoryManaged == true)
00278 {
00279 delete clean;
00280 data = 0;
00281 clean = 0;
00282 }
00283 }
00284
00285
00286
00288 template< class T >
00289 inline const T* Vector<T>::DataPtr() const
00290 {
00291 return data;
00292 }
00293
00295 template< class T >
00296 inline T* Vector<T>::Data()
00297 {
00298 return data;
00299 }
00300
00301
00302
00303 template< class T >
00304 Vector<T> Vector<T>::Clone() const
00305 {
00306 Vector<T> temp(length);
00307
00308 for(int j=0;j<temp.length;j++)
00309 {
00310 temp.data[j] = data[j];
00311 }
00312
00313 return temp;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 template< class T >
00338 Vector<T> Vector<T>::Slice(int start, int end)
00339 {
00340
00341 if(start<0 || start>=length || end<0 || end>=length)
00342 {
00343 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00344 Utility::RunTimeError("Index outside bounds!");
00345 }
00346
00347 if(start > end)
00348 {
00349 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00350 Utility::RunTimeError("Second slice parameter cannot be less than the first parameter!");
00351 }
00352
00353 Vector<T> temp(end-start+1);
00354
00355 for(int i=start; i<=end; i++)
00356 {
00357 temp[i-start] = data[i];
00358 }
00359
00360 return temp;
00361 }
00362
00363 template< class T >
00364 Vector<T> Vector<T>::Slice(string str)
00365 {
00366 int start, end;
00367 vector<string> bounds = Utility::Split(str, ":");
00368
00369 if(str == ":")
00370 {
00371 start = 0;
00372 end = length-1;
00373 }
00374 else if(bounds.size() == 2)
00375 {
00376 start = Utility::ToInt(bounds[0]);
00377 end = Utility::ToInt(bounds[1]);
00378 }
00379 else
00380 {
00381 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00382 Utility::RunTimeError("Incorrect slice argument. String cannot be parsed!");
00383 }
00384
00385 return this->Slice(start, end);
00386 }
00387
00388
00389 template< class T >
00390 inline const bool Vector<T>::IsMemoryManaged() const
00391 {
00392 return memoryManaged;
00393 }
00394
00395
00396
00397 template< class T >
00398 inline const int Vector<T>::Length() const
00399 {
00400 return length;
00401 }
00402
00403
00404 template< class T >
00405 inline const int Vector<T>::Numel() const
00406 {
00407 return length;
00408 }
00409
00410
00411 template< class T >
00412 inline void Vector<T>::Init(const T init)
00413 {
00414 for(int i=0;i<length;i++)
00415 {
00416 data[i] = init;
00417 }
00418 }
00419
00420
00421
00422 template< class T >
00423 inline Vector<T>& Vector<T>::Rand(const double max)
00424 {
00425 if(!RandomGen::Initialized())
00426 {
00427 RandomGen::Initialize();
00428 }
00429 for(int i=0;i<length;i++)
00430 {
00431 data[i] = (T)(rand()/(double)RAND_MAX*max);
00432 }
00433 return *this;
00434 }
00435
00436
00437 template< class T >
00438 Vector<T> Vector<T>::Rand(const int l, const double max)
00439 {
00440 Vector<T> m(l);
00441 if(!RandomGen::Initialized())
00442 {
00443 RandomGen::Initialize();
00444 }
00445 for(int i=0;i<l;i++)
00446 {
00447 m.data[i] = (T)(rand()/(double)RAND_MAX*max);
00448 }
00449 return m;
00450 }
00451
00452
00453
00454 template< class T >
00455 void Vector<T>::ReadFromVector(const Vector<T>& m, const int index)
00456 {
00457 if(length < m.length+index)
00458 {
00459 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00460 Utility::RunTimeError("You can't read from a larger Vector (from the copy start point) to a smaller Vector!");
00461 }
00462
00463 for(int i=0;i<m.length;i++)
00464 {
00465 data[i+index] = m.data[i];
00466 }
00467
00468 }
00469
00470
00471
00472
00473 template< class T >
00474 Vector<T> Vector<T>::Cat(Vector<T>& m1, Vector<T>& m2)
00475 {
00476 Vector<T> temp(m1.Length()+m2.Length());
00477 for(int i=0;i<m1.Length();i++)
00478 {
00479 temp.data[i] = m1.data[i];
00480 }
00481 for(int i=m1.Length();i<temp.Length();i++)
00482 {
00483 temp.data[i] = m2.data[i-m1.Length()];
00484 }
00485 return temp;
00486 }
00487
00488
00489
00490 template< class T >
00491 Vector<T> Vector<T>::Ones(int side)
00492 {
00493 Vector<T> temp(side,1);
00494 return temp;
00495 }
00496
00497 template< class T >
00498 Vector<T> Vector<T>::Zeros(int side)
00499 {
00500 Vector<T> temp(side,0);
00501 return temp;
00502 }
00503
00504
00505
00506
00507
00508 template< class T >
00509 T Vector<T>::Inner(Vector<T>& m1, Vector<T>& m2)
00510 {
00511 if(m1.length != m2.length)
00512 {
00513 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00514 Utility::RunTimeError("Vector lengths are not the same!");
00515 }
00516
00517 T inner = 0;
00518 for(int i=0; i<m1.length; i++)
00519 {
00520 inner += m1.data[i]*m2.data[i];
00521 }
00522
00523 return inner;
00524 }
00525
00526
00527
00528
00529
00530 template< class T >
00531 Vector<int> Vector<T>::And(Vector<T>& m1, Vector<T>& m2)
00532 {
00533 if(m1.length != m2.length)
00534 {
00535 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00536 Utility::RunTimeError("Vector lengths are not the same!");
00537 }
00538
00539 Vector<int> temp(m1.length);
00540 for(int i=0;i<m1.length;i++)
00541 {
00542 temp[i] = (m1.data[i] != 0 && m2.data[i] != 0) ? 1 : 0;
00543 }
00544
00545 return temp;
00546 }
00547
00548
00549
00550 template< class T >
00551 Vector<int> Vector<T>::Or(Vector<T>& m1, Vector<T>& m2)
00552 {
00553 if(m1.length != m2.length)
00554 {
00555 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00556 Utility::RunTimeError("Vector lengths are not the same!");
00557 }
00558
00559 Vector<int> temp(m1.length);
00560 for(int i=0;i<m1.length;i++)
00561 {
00562 temp[i] = (m1.data[i] == 0 && m2.data[i] == 0) ? 0 : 1;
00563 }
00564
00565 return temp;
00566 }
00567
00568 template< class T >
00569 Vector<int> Vector<T>::Lt(Vector<T>& m1, Vector<T>& m2)
00570 {
00571 if(m1.length != m2.length)
00572 {
00573 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00574 Utility::RunTimeError("Vector lengths are not the same!");
00575 }
00576
00577 Vector<int> temp(m1.length);
00578 for(int i=0;i<m1.length;i++)
00579 {
00580 temp[i] = (m1.data[i] < m2.data[i]) ? 1 : 0;
00581 }
00582
00583 return temp;
00584 }
00585
00586 template< class T >
00587 Vector<int> Vector<T>::Gt(Vector<T>& m1, Vector<T>& m2)
00588 {
00589 if(m1.length != m2.length)
00590 {
00591 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00592 Utility::RunTimeError("Vector lengths are not the same!");
00593 }
00594
00595 Vector<int> temp(m1.length);
00596 for(int i=0;i<m1.length;i++)
00597 {
00598 temp[i] = (m1.data[i] > m2.data[i]) ? 1 : 0;
00599 }
00600
00601 return temp;
00602 }
00603
00604 template< class T >
00605 Vector<int> Vector<T>::Le(Vector<T>& m1, Vector<T>& m2)
00606 {
00607 if(m1.length != m2.length)
00608 {
00609 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00610 Utility::RunTimeError("Vector lengths are not the same!");
00611 }
00612
00613 Vector<int> temp(m1.length);
00614 for(int i=0;i<m1.length;i++)
00615 {
00616 temp[i] = (m1.data[i] <= m2.data[i]) ? 1 : 0;
00617 }
00618
00619 return temp;
00620 }
00621
00622 template< class T >
00623 Vector<int> Vector<T>::Ge(Vector<T>& m1, Vector<T>& m2)
00624 {
00625 if(m1.length != m2.length)
00626 {
00627 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00628 Utility::RunTimeError("Vector lengths are not the same!");
00629 }
00630
00631 Vector<int> temp(m1.length);
00632 for(int i=0;i<m1.length;i++)
00633 {
00634 temp[i] = (m1.data[i] >= m2.data[i]) ? 1 : 0;
00635 }
00636
00637 return temp;
00638 }
00639
00640 template< class T >
00641 Vector<int> Vector<T>::Eq(Vector<T>& m1, Vector<T>& m2)
00642 {
00643 if(m1.length != m2.length)
00644 {
00645 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00646 Utility::RunTimeError("Vector lengths are not the same!");
00647 }
00648
00649 Vector<int> temp(m1.length);
00650 for(int i=0;i<m1.length;i++)
00651 {
00652 temp[i] = (m1.data[i] == m2.data[i]) ? 1 : 0;
00653 }
00654
00655 return temp;
00656 }
00657
00658 template< class T >
00659 Vector<int> Vector<T>::Ne(Vector<T>& m1, Vector<T>& m2)
00660 {
00661 if(m1.length != m2.length)
00662 {
00663 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00664 Utility::RunTimeError("Vector lengths are not the same!");
00665 }
00666
00667 Vector<int> temp(m1.length);
00668 for(int i=0;i<m1.length;i++)
00669 {
00670 temp[i] = (m1.data[i] != m2.data[i]) ? 1 : 0;
00671 }
00672
00673 return temp;
00674 }
00675
00676
00677
00678
00679
00680
00681 template< class T >
00682 Vector<int> Vector<T>::And(Vector<T>& m1, T v)
00683 {
00684 Vector<int> temp(m1.length);
00685 for(int i=0;i<m1.length;i++)
00686 {
00687 temp[i] = (m1.data[i] != 0 && v != 0) ? 1 : 0;
00688 }
00689
00690 return temp;
00691 }
00692
00693
00694 template< class T >
00695 Vector<int> Vector<T>::Or(Vector<T>& m1, T v)
00696 {
00697 Vector<int> temp(m1.length);
00698 for(int i=0;i<m1.length;i++)
00699 {
00700 temp[i] = (m1.data[i] == 0 && v == 0) ? 0 : 1;
00701 }
00702
00703 return temp;
00704 }
00705
00706
00707 template< class T >
00708 Vector<int> Vector<T>::Lt(Vector<T>& m1, T v)
00709 {
00710 Vector<int> temp(m1.length);
00711 for(int i=0;i<m1.length;i++)
00712 {
00713 temp[i] = (m1.data[i] < v) ? 1 : 0;
00714 }
00715
00716 return temp;
00717 }
00718
00719
00720 template< class T >
00721 Vector<int> Vector<T>::Gt(Vector<T>& m1, T v)
00722 {
00723 Vector<int> temp(m1.length);
00724 for(int i=0;i<m1.length;i++)
00725 {
00726 temp[i] = (m1.data[i] > v) ? 1 : 0;
00727 }
00728
00729 return temp;
00730 }
00731
00732
00733 template< class T >
00734 Vector<int> Vector<T>::Le(Vector<T>& m1, T v)
00735 {
00736 Vector<int> temp(m1.length);
00737 for(int i=0;i<m1.length;i++)
00738 {
00739 temp[i] = (m1.data[i] <= v) ? 1 : 0;
00740 }
00741
00742 return temp;
00743 }
00744
00745
00746 template< class T >
00747 Vector<int> Vector<T>::Ge(Vector<T>& m1, T v)
00748 {
00749 Vector<int> temp(m1.length);
00750 for(int i=0;i<m1.length;i++)
00751 {
00752 temp[i] = (m1.data[i] >= v) ? 1 : 0;
00753 }
00754
00755 return temp;
00756 }
00757
00758
00759 template< class T >
00760 Vector<int> Vector<T>::Eq(Vector<T>& m1, T v)
00761 {
00762 Vector<int> temp(m1.length);
00763 for(int i=0;i<m1.length;i++)
00764 {
00765 temp[i] = (m1.data[i] == v) ? 1 : 0;
00766 }
00767
00768 return temp;
00769 }
00770
00771
00772 template< class T >
00773 Vector<int> Vector<T>::Ne(Vector<T>& m1, T v)
00774 {
00775 Vector<int> temp(m1.length);
00776 for(int i=0;i<m1.length;i++)
00777 {
00778 temp[i] = (m1.data[i] != v) ? 1 : 0;
00779 }
00780
00781 return temp;
00782 }
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801 template< class T >
00802 Vector<T> Vector<T>::Add(Vector<T>& m1, Vector<T>& m2)
00803 {
00804 if(m1.length != m2.length)
00805 {
00806 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00807 Utility::RunTimeError("Vector lengths are not the same!");
00808 }
00809
00810 Vector<T> temp(m1.length);
00811 for(int i=0;i<temp.length;i++)
00812 {
00813 temp.data[i] = m1.data[i] + m2.data[i];
00814 }
00815
00816 return temp;
00817 }
00818
00819
00820 template< class T >
00821 Vector<T> Vector<T>::Subtract(Vector<T>& m1, Vector<T>& m2)
00822 {
00823 if(m1.length != m2.length)
00824 {
00825 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00826 Utility::RunTimeError("Vector lengths are not the same!");
00827 }
00828
00829 Vector<T> temp(m1.length);
00830 for(int i=0;i<temp.length;i++)
00831 {
00832 temp.data[i] = m1.data[i] - m2.data[i];
00833 }
00834
00835 return temp;
00836 }
00837
00838 template< class T >
00839 Vector<T> Vector<T>::Multiply(Vector<T>& m1, Vector<T>& m2)
00840 {
00841 if(m1.length != m2.length)
00842 {
00843 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00844 Utility::RunTimeError("Vector lengths are not the same!");
00845 }
00846
00847 Vector<T> temp(m1.length);
00848 for(int i=0;i<temp.length;i++)
00849 {
00850 temp.data[i] = m1.data[i] * m2.data[i];
00851 }
00852
00853 return temp;
00854 }
00855
00856 template< class T >
00857 Vector<T> Vector<T>::Divide(Vector<T>& m1, Vector<T>& m2)
00858 {
00859 if(m1.length != m2.length)
00860 {
00861 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00862 Utility::RunTimeError("Vector lengths are not the same!");
00863 }
00864
00865 Vector<T> temp(m1.length);
00866 for(int i=0;i<temp.length;i++)
00867 {
00868
00869 if(m2.data[i] != 0)
00870 {
00871 temp.data[i] = m1.data[i] / m2.data[i];
00872 }
00873 else
00874 {
00875 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00876 Utility::RunTimeError("Divide by zero in vector division!");
00877 }
00878 }
00879
00880 return temp;
00881 }
00882
00883
00884
00885 template< class T >
00886 Vector<T> Vector<T>::Add(Vector<T>& m1, T v2)
00887 {
00888 Vector<T> temp(m1.length);
00889 for(int i=0;i<temp.length;i++)
00890 {
00891 temp.data[i] = m1.data[i] + v2;
00892 }
00893 return temp;
00894 }
00895
00896 template< class T >
00897 Vector<T> Vector<T>::Subtract(Vector<T>& m1, T v2)
00898 {
00899 Vector<T> temp(m1.length);
00900 for(int i=0;i<temp.length;i++)
00901 {
00902 temp.data[i] = m1.data[i] - v2;
00903 }
00904 return temp;
00905 }
00906
00907 template< class T >
00908 Vector<T> Vector<T>::Subtract(T v2, Vector<T>& m1)
00909 {
00910 Vector<T> temp(m1.length);
00911 for(int i=0;i<temp.length;i++)
00912 {
00913 temp.data[i] = v2 - m1.data[i];
00914 }
00915 return temp;
00916 }
00917
00918
00919 template< class T >
00920 Vector<T> Vector<T>::Multiply(Vector<T>& m1, T v2)
00921 {
00922 Vector<T> temp(m1.length);
00923 for(int i=0;i<temp.length;i++)
00924 {
00925 temp.data[i] = m1.data[i] * v2;
00926 }
00927 return temp;
00928 }
00929
00930 template< class T >
00931 Vector<T> Vector<T>::Divide(Vector<T>& m1, T v2)
00932 {
00933 if(v2 == 0)
00934 {
00935 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00936 Utility::RunTimeError("Divide by zero in vector by value division!");
00937 }
00938
00939 Vector<T> temp(m1.length);
00940 for(int i=0;i<temp.length;i++)
00941 {
00942 temp.data[i] = m1.data[i] / v2;
00943 }
00944 return temp;
00945 }
00946
00947 template< class T >
00948 Vector<T> Vector<T>::Divide(T v2, Vector<T>& m1)
00949 {
00950 Vector<T> temp(m1.length);
00951 for(int i=0;i<temp.length;i++)
00952 {
00953 if(m1.data[i] != 0)
00954 {
00955 temp.data[i] = v2 / m1.data[i];
00956 }
00957 else
00958 {
00959 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00960 Utility::RunTimeError("Divide by zero in value by vector division!");
00961 }
00962 }
00963 return temp;
00964 }
00965
00966
00967
00968
00969 template< class T >
00970 Vector<T>& Vector<T>::Add(Vector<T>& m)
00971 {
00972 if(length != m.length)
00973 {
00974 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00975 Utility::RunTimeError("Vector lengths are not the same!");
00976 }
00977
00978 for(int i=0;i<this->length;i++)
00979 {
00980 this->data[i] += m.data[i];
00981 }
00982
00983 return *this;
00984 }
00985
00986 template< class T >
00987 Vector<T>& Vector<T>::Subtract(Vector<T>& m)
00988 {
00989 if(length != m.length)
00990 {
00991 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00992 Utility::RunTimeError("Vector lengths are not the same!");
00993 }
00994
00995 for(int i=0;i<this->length;i++)
00996 {
00997 this->data[i] -= m.data[i];
00998 }
00999
01000 return *this;
01001 }
01002
01003 template< class T >
01004 Vector<T>& Vector<T>::Multiply(Vector<T>& m)
01005 {
01006 if(length != m.length)
01007 {
01008 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01009 Utility::RunTimeError("Vector lengths are not the same!");
01010 }
01011
01012 for(int i=0;i<this->length;i++)
01013 {
01014 this->data[i] *= m.data[i];
01015 }
01016
01017 return *this;
01018 }
01019
01020 template< class T >
01021 Vector<T>& Vector<T>::Divide(Vector<T>& m)
01022 {
01023 if(length != m.length)
01024 {
01025 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01026 Utility::RunTimeError("Vector lengths are not the same!");
01027 }
01028
01029 for(int i=0;i<this->length;i++)
01030 {
01031 if(m.data[i] != 0)
01032 {
01033 this->data[i] /= m.data[i];
01034 }
01035 else
01036 {
01037 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01038 Utility::RunTimeError("Divide by zero in vector division!");
01039 }
01040 }
01041
01042 return *this;
01043 }
01044
01045
01046 template< class T >
01047 Vector<T>& Vector<T>::Add(T v)
01048 {
01049 for(int i=0;i<this->length;i++)
01050 {
01051 this->data[i] += v;
01052 }
01053 return *this;
01054 }
01055
01056 template< class T >
01057 Vector<T>& Vector<T>::Subtract(T v)
01058 {
01059 for(int i=0;i<this->length;i++)
01060 {
01061 this->data[i] -= v;
01062 }
01063 return *this;
01064 }
01065
01066 template< class T >
01067 Vector<T>& Vector<T>::Multiply(T v)
01068 {
01069 for(int i=0;i<this->length;i++)
01070 {
01071 this->data[i] *= v;
01072 }
01073 return *this;
01074 }
01075
01076 template< class T >
01077 Vector<T>& Vector<T>::Divide(T v)
01078 {
01079 if(v == 0)
01080 {
01081 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01082 Utility::RunTimeError("Divide by zero in vector by value division!");
01083 }
01084
01085 for(int i=0;i<this->length;i++)
01086 {
01087 this->data[i] /= v;
01088 }
01089 return *this;
01090 }
01091
01092
01093
01094
01095
01096
01097 template< class T >
01098 Vector<T>& Vector<T>::operator= (Vector<T>& m)
01099 {
01100
01101 if(m.memoryManaged == true && memoryManaged == true)
01102 {
01103 length = m.length;
01104 data = m.data;
01105 delete clean;
01106 clean = new (std::nothrow) Cleaner<T>(data);
01107 Utility::CheckPointer(clean);
01108 }
01109 else if(m.memoryManaged == false && memoryManaged == true)
01110 {
01111 length = m.length;
01112 data = new (std::nothrow) T[length];
01113 Utility::CheckPointer(data);
01114 clean = new (std::nothrow) Cleaner<T>(data);
01115 Utility::CheckPointer(clean);
01116 for(int j=0;j<length;j++)
01117 {
01118 data[j] = m.data[j];
01119 }
01120 Utility::Warning("Using assignment from an unmanaged vector!\nValues are copied to the new vector.");
01121
01122 }
01123 else if(memoryManaged == false)
01124 {
01125 if(length == m.length)
01126 {
01127 for(int j=0;j<length;j++)
01128 {
01129 data[j] = m.data[j];
01130 }
01131 Utility::Warning("Using assignment to an unmanaged vector!\nValues are copied.");
01132 }
01133 else
01134 {
01135 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01136 Utility::RunTimeError("Assignment to an unmanaged vector. Lengths of the vectors must match!");
01137 }
01138 }
01139 else
01140 {
01141 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01142 Utility::RunTimeError("Something is wrong. We shouldn't arrive to this branch!");
01143 }
01144
01145 return *this;
01146 }
01147
01148
01149 template< class T >
01150 Vector<T>& Vector<T>::operator= (Array<T>& m)
01151 {
01152 if(memoryManaged == true)
01153 {
01154 length = m.length;
01155
01156 data = m.data;
01157 clean = new (std::nothrow) Cleaner(data);
01158 Utility::CheckPointer(clean);
01159 }
01160 else
01161 {
01162 if(length == m.length)
01163 {
01164 for(int j=0;j<length;j++)
01165 {
01166 data[j] = m.data[j];
01167 }
01168 Utility::Warning("Using assignment to an unmanaged vector!\nValues are copied.");
01169 }
01170 else
01171 {
01172 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01173 Utility::RunTimeError("Assignment to an unmanaged vector. Number of elements in vector and array must match!");
01174 }
01175 }
01176
01177 return *this;
01178 }
01179
01180 template< class T >
01181 Vector<T>& Vector<T>::operator= (Matrix<T>& m)
01182 {
01183 if(memoryManaged == true)
01184 {
01185 length = m.length;
01186
01187 data = m.data;
01188 clean = new (std::nothrow) Cleaner(data);
01189 Utility::CheckPointer(clean);
01190 }
01191 else
01192 {
01193 if(length == m.length)
01194 {
01195 for(int j=0;j<length;j++)
01196 {
01197 data[j] = m.data[j];
01198 }
01199 Utility::Warning("Using assignment to an unmanaged vector!\nValues are copied.");
01200 }
01201 else
01202 {
01203 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01204 Utility::RunTimeError("Assignment to an unmanaged vector. Number of elements in vector and array must match!");
01205 }
01206 }
01207
01208 return *this;
01209 }
01210
01211
01212
01213 template< class T >
01214 Vector<T>& Vector<T>::operator= (string str)
01215 {
01216 Vector<T> temp(str);
01217 *this = temp;
01218 return *this;
01219 }
01220
01221
01222
01223
01224 template< class T >
01225 inline Vector<T> Vector<T>::operator+ ()
01226 {
01227 return *this;
01228 }
01229
01230
01231 template< class T >
01232 inline Vector<T> Vector<T>::operator- ()
01233 {
01234 Vector<T> temp(length);
01235 for(int i=0;i<length;i++)
01236 {
01237 temp.data[i] = - data[i];
01238 }
01239 return temp;
01240 }
01241
01242
01243 template< class T >
01244 inline Vector<int> Vector<T>::operator! ()
01245 {
01246 Vector<int> temp(yDim, xDim);
01247 for(int i=0;i<length;i++)
01248 {
01249 if(data[i] == 0)
01250 {
01251 temp.Data()[i] = 1;
01252 }
01253 else
01254 {
01255 temp.Data()[i] = 0;
01256 }
01257 }
01258 return temp;
01259 }
01260
01261
01262
01263
01264 template< class T >
01265 inline T& Vector<T>::operator() (const int i)
01266 {
01267 if(i<0 || i>=length)
01268 {
01269 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01270 Utility::RunTimeError("Index outside bounds!");
01271 }
01272 return data[i];
01273 }
01274
01275
01276 template< class T >
01277 inline T& Vector<T>::operator[] (const int i)
01278 {
01279 return data[i];
01280 }
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312 template< class T >
01313 inline Vector<T> Vector<T>::operator() (int start, int end)
01314 {
01315 if(start<0 || start>=length || end<0 || end>=length)
01316 {
01317 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01318 Utility::RunTimeError("Index outside bounds!");
01319 }
01320 if(start > end)
01321 {
01322 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01323 Utility::RunTimeError("Second slice parameter cannot be less than the first parameter!");
01324 }
01325
01326
01327 Vector<T> temp(&(data[start]), end-start+1);
01328 return temp;
01329 }
01330
01331
01332 template< class T >
01333 inline Vector<T> Vector<T>::operator() (string str)
01334 {
01335 int start, end;
01336 vector<string> bounds = Utility::Split(str, ":");
01337
01338 if(str == ":")
01339 {
01340 start = 0;
01341 end = length-1;
01342 }
01343 else if(bounds.size() == 2)
01344 {
01345 start = Utility::ToInt(bounds[0]);
01346 end = Utility::ToInt(bounds[1]);
01347 }
01348 else
01349 {
01350 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01351 Utility::RunTimeError("Incorrect slice argument. String cannot be parsed!");
01352 }
01353
01354 return this->operator()(start, end);
01355
01356
01357 }
01358
01359
01360
01361
01362 template< class T >
01363 inline Vector<T> Vector<T>::operator() (Vector<int>& ind)
01364 {
01365 Vector<T> temp(ind.Length());
01366 for(int i=0; i<ind.Length(); i++)
01367 {
01368 if(ind.ElemNC(i) >= length)
01369 {
01370 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01371 Utility::RunTimeError("Index outside bounds!");
01372 }
01373 temp.data[i] = data[ind.ElemNC(i)];
01374 }
01375 return temp;
01376 }
01377
01378
01379
01380
01381
01383 template< class T >
01384 inline T& Vector<T>::Elem(const int i)
01385 {
01386 if(i<0 || i>=length)
01387 {
01388 cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01389 Utility::RunTimeError("Index outside bounds!");
01390 }
01391 return data[i];
01392 }
01393
01395 template< class T >
01396 inline T& Vector<T>::ElemNC(const int i)
01397 {
01398 return data[i];
01399 }
01400
01401
01402
01403
01404
01405
01406
01407
01408 template< class T >
01409 Vector<T>& Vector<T>::operator+= (Vector<T>& m)
01410 {
01411 return this->Add(m);
01412 }
01413
01414 template< class T >
01415 Vector<T>& Vector<T>::operator-= (Vector<T>& m)
01416 {
01417 return this->Subtract(m);
01418 }
01419
01420 template< class T >
01421 Vector<T>& Vector<T>::operator*= (Vector<T>& m)
01422 {
01423 return this->Multiply(m);
01424 }
01425
01426 template< class T >
01427 Vector<T>& Vector<T>::operator/= (Vector<T>& m)
01428 {
01429 return this->Divide(m);
01430 }
01431
01432
01433 template< class T >
01434 Vector<T>& Vector<T>::operator+= (T v)
01435 {
01436 return this->Add(v);
01437 }
01438
01439 template< class T >
01440 Vector<T>& Vector<T>::operator-= (T v)
01441 {
01442 return this->Subtract(v);
01443 }
01444
01445 template< class T >
01446 Vector<T>& Vector<T>::operator*= (T v)
01447 {
01448 return this->Multiply(v);
01449 }
01450
01451 template< class T >
01452 Vector<T>& Vector<T>::operator/= (T v)
01453 {
01454 return this->Divide(v);
01455 }
01456
01457
01458
01459
01460
01461
01462 template< class T >
01463 Vector<T>::Vector(Matrix<T> &m)
01464 {
01465 length = m.length;
01466
01467 data = m.data;
01468
01469 clean = new (std::nothrow) Cleaner<T>(data);
01470 Utility::CheckPointer(clean);
01471
01472 memoryManaged = true;
01473
01474 }
01475
01476
01477
01478
01479 template< class T >
01480 Vector<T>::Vector(Array<T> &m)
01481 {
01482 length = m.length;
01483
01484 data = m.data;
01485
01486 clean = new (std::nothrow) Cleaner<T>(data);
01487 Utility::CheckPointer(clean);
01488
01489 memoryManaged = true;
01490
01491 }
01492
01493
01494
01495
01496
01497
01498
01499
01500 };
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518