Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
vector.h
1/******************************************************************************
2 * Copyright (C) 2007-2024 by the GIMLi development team *
3 * Carsten Rücker carsten@resistivity.net *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************/
18//** Idea taken from
19//
20// @Article{Veldhuizen95b,
21// author = "Todd L. Veldhuizen",
22// title = "Expression templates",
23// journal = "C++ Report",
24// volume = "7",
25// number = "5",
26// pages = "26--31",
27// month = jun,
28// year = "1995",
29// note = "Reprinted in C++ Gems, ed. Stanley Lippman"
30// }
31
32#ifndef GIMLI_VECTOR__H
33#define GIMLI_VECTOR__H
34
35#define EXPRVEC_USE_TEMPORARY_EXPRESSION
36
37//#define EXPRVEC_USE_BOOST_THREAD // case 1
38//#define EXPRVEC_USE_STD_ALGORITHM // case 2
39#define EXPRVEC_USE_INDIRECTION // case 3 #current default
40
41#include "gimli.h"
42#include "expressions.h"
43
44#include <string>
45#include <vector>
46#include <set>
47#include <algorithm>
48#include <numeric>
49#include <cmath>
50#include <cstring>
51#include <fstream>
52#include <cerrno>
53#include <iterator>
54
55#ifdef USE_BOOST_BIND
56 // deprecated
57 #include <boost/bind.hpp>
58#else
59 #include <functional>
60#endif
61
62namespace GIMLI{
63
64template < class ValueType, class A > class __VectorExpr;
65
66IndexArray find(const BVector & v);
67
68DLLEXPORT IndexArray range(Index start, Index stop, Index step=1);
69DLLEXPORT IndexArray range(Index stop);
70
71
72#ifndef PYGIMLI_CAST
73inline void Dump(const void * mem, unsigned int n) {
74 const char * p = reinterpret_cast< const char *>(mem);
75 for (unsigned int i = 0; i < n; i++) {
76 std::cout << std::hex << int(p[i]) << " ";
77 }
78 std::cout << std::endl;
79}
80#endif
81
82template < class ValueType > class DLLEXPORT VectorIterator {
83public:
84 typedef ValueType value_type;
85
86 #ifdef _MSC_VER
87 // basic iterator traits needed by msvc
88 typedef Index difference_type;
89 typedef value_type & pointer;
90 typedef value_type & reference;
91 typedef std::random_access_iterator_tag iterator_category;
92 bool operator < (const VectorIterator< ValueType > & a) const { return val_ < a.val_; }
93 #endif
94
95 VectorIterator()
96 : val_(0), maxSize_(0), end_(0){
97 }
98
99 VectorIterator(ValueType * v, Index size)
100 : val_(v), maxSize_(size), end_(v + size){
101 }
102
103 VectorIterator(const VectorIterator < ValueType > & iter)
104 : val_(iter.val_), maxSize_(iter.maxSize_), end_(iter.val_ + iter.maxSize_){
105 }
106
107 VectorIterator < ValueType > & operator = (const VectorIterator < ValueType > & iter){
108 if (this != & iter){
109// __M
110 val_ = iter.val_;
111 maxSize_ = iter.maxSize_ ;
112 end_ = iter.end_;
113 }
114 return *this;
115 }
116
117 inline const ValueType & operator * () const { return * val_; }
118
119 inline ValueType & operator * () { return * val_; }
120
121 inline const ValueType & operator [] (const Index i) const { //__MS(this) __MS(val_) __MS(*val_) Dump(val_, sizeof(ValueType));
122 return val_[i]; }
123
124 inline ValueType & operator [] (const Index i) { //__MS(this) __MS(val_) __MS(*val_) Dump(val_, sizeof(ValueType));
125 return val_[i]; }
126
127 inline VectorIterator< ValueType > & operator ++ () { // prefix ++A
128 ++val_; return *this;
129 }
130
131 inline VectorIterator< ValueType > & operator -- () { // prefix ++A
132 --val_; return *this;
133 }
134
135 inline VectorIterator< ValueType > operator ++ (int) { // postfix A++
136 VectorIterator< ValueType > old(*this); // make a copy for result
137// TO_IMPL
138 ++(*this); // Now use the prefix version to do the work
139 return old; // return the copy (the old) value.
140 }
141
142 inline VectorIterator< ValueType > operator -- (int) { // postfix A++
143 VectorIterator< ValueType > old(*this); // make a copy for result
144// TO_IMPL
145 --(*this); // Now use the prefix version to do the work
146 return old; // return the copy (the old) value.
147 }
148
149 inline bool operator == (const VectorIterator< ValueType > & a) const { return val_ == a.val_; }
150 inline bool operator != (const VectorIterator< ValueType > & a) const { return val_ != a.val_; }
151
152 inline Index size() const { return maxSize_; }
153
154 inline ValueType * ptr() const { return val_; }
155 inline ValueType * ptr() { return val_; }
156
157 inline bool hasMore() const { return val_ != end_; }
158
160 inline ValueType nextVal(){
161 // __MS(this) __MS(val_) __MS(*val_)
162 // __MS(this->hasMore())
163 return *val_++; }
164
165 inline ValueType nextForPy(){// __MS(val_) __MS(*val_)
166 if(!hasMore()){
167#if PYGIMLI
168#include "boost/python.hpp"
169 boost::python::objects::stop_iteration_error();
170#endif //will not come here
171 }
172 return nextVal();
173 }
174
175 ValueType * val_;
176 Index maxSize_;
177 ValueType * end_;
178
179};
180
182
185
186template< class ValueType > class DLLEXPORT Vector {
187public:
188 typedef ValueType ValType;
189// typedef VectorIterator< ValueType const > const_iterator;
190 typedef VectorIterator< ValueType > iterator;
191
195// #ifndef PYGIMLI_CAST
196// this constructor is dangerous for IndexArray in pygimli ..
197// there is an autocast from int -> IndexArray(int)
199 : size_(0), data_(0), capacity_(0){
200 // explicit Vector(Index n = 0) : data_(NULL), begin_(NULL), end_(NULL) {
201 resize(0);
202 clean();
203 }
204 Vector(Index n)
205 : size_(0), data_(0), capacity_(0){
206 // explicit Vector(Index n = 0) : data_(NULL), begin_(NULL), end_(NULL) {
207 resize(n);
208 clean();
209 }
210// #endif
214 Vector(Index n, const ValueType & val)
215 : size_(0), data_(0), capacity_(0){
216 resize(n);
217 fill(val);
218 }
219
223 Vector(const std::string & filename, IOFormat format=Ascii)
224 : size_(0), data_(0), capacity_(0){
225 this->load(filename, format);
226 }
227
232 : size_(0), data_(0), capacity_(0){
233 resize(v.size());
234 copy_(v);
235 }
236
240 Vector(const Vector< ValueType > & v, Index start, Index end)
241 : size_(0), data_(0), capacity_(0){
242 resize(end - start);
243 std::copy(&v[start], &v[end], data_);
244 }
245
249 template < class A > Vector(const __VectorExpr< ValueType, A > & v)
250 : size_(0), data_(0), capacity_(0){
251 resize(v.size());
252 assign_(v);
253 }
254
255#ifndef PYGIMLI_CAST
259 Vector(const std::vector< ValueType > & v)
260 : size_(0), data_(0), capacity_(0){
261 resize(v.size());
262 for (Index i = 0; i < v.size(); i ++) data_[i] = v[i];
263 //std::copy(&v[0], &v[v.size()], data_);
264 }
265
266 template < class ValueType2 > Vector(const Vector< ValueType2 > & v)
267 : size_(0), data_(0), capacity_(0){
268 resize(v.size());
269 for (Index i = 0; i < v.size(); i ++) data_[i] = ValueType(v[i]);
270 //std::copy(&v[0], &v[v.size()], data_);
271 }
272#endif
273
275 ~Vector() { free_(); }
276
278 Vector< ValueType > & operator = (const Vector< ValueType > & v) {
279 if (this != &v) {
280 resize(v.size());
281 copy_(v);
282 }
283 return *this;
284 }
285
287 template < class A > Vector< ValueType > & operator = (const __VectorExpr< ValueType, A > & v) {
288 assign_(v);
289 return *this;
290 }
291
293 Vector< ValueType > & operator = (const ValueType & val) {
294 fill(val);
295 return *this;
296 }
297
299 inline Vector < ValueType > copy() const {
300 return *this;
301 }
302
303 inline const ValueType & operator[](const Index i) const {
304 // ASSERT_THIS_SIZE(i) // will not work for std::copy, std::sort etc.
305 return data_[i];
306 }
307
308 inline ValueType & operator[](const Index i) {
309 // ASSERT_THIS_SIZE(i) // will not work for std::copy, std::sort etc.
310 return data_[i];
311 }
312
313 inline const Vector < ValueType > operator[](const IndexArray & i) const { return this->get_(i); }
314
315 inline Vector < ValueType > operator[](const IndexArray & i) { return this->get_(i); }
316
317 inline Vector < ValueType > operator[](const BVector & b) { return this->get_(b); }
318
324 inline Vector < ValueType > operator () (Index start, SIndex end) const {
325 return getVal(start, end);
326 }
327
328 inline Vector < ValueType > operator () (const std::pair< Index, SIndex > & pair) const {
329 return getVal(pair.first, pair.second);
330 }
331
332// inline Vector < ValueType > operator () (const std::vector < int > & idx) const {
333// return get_(idx);
334// }
339 inline Vector < ValueType > operator () (const IndexArray & iArray) const {
340 return get_(iArray);
341 }
342
346 inline Vector < ValueType > operator () (const SIndexArray & siArray) const {
347 return get_(siArray);
348 }
349 inline Vector < ValueType > operator () (const IVector & iVec) const {
350 return get_(iVec);
351 }
352 template < class IndexContainer > Vector < ValueType > get_(const IndexContainer & idx) const {
353// __MS(&idx)
354// __MS(idx.size())
355// //__MS(idx)
356 Vector < ValueType > v(idx.size());
357 Index id;
358 for (Index i = 0; i < idx.size(); i ++){
359 id = idx[i];
360 if (id >= 0 && id < size_){
361 v[i] = data_[(Index)id];
362 } else {
363 throwLengthError(WHERE_AM_I + " idx out of range " +
364 str(id) + " [" + str(0) + " " + str(size_) + ")");
365 }
366 }
367 return v;
368 }
369
371 Vector < ValueType > get_(const BVector & bv) const {
372 return this->get_(GIMLI::find(bv));
373 }
374 Vector < ValueType > getVUI_(const IndexArray & iA) const {
375 return this->get_(iA);
376 }
377 Vector < ValueType > getVSI_(const IVector & iA) const {
378 return this->get_(iA);
379 }
380
381#ifndef PYGIMLI_CAST
385// template < class T > operator Vector< T >() const {
386// //COUTMARKER
387// Vector< T > f(this->size());
388// for (Index i = 0; i < this->size(); i ++){ f[i] = T(data_[i]); }
389// return f;
390// }
391
395 template < class T > operator std::vector< T >() {
396 std::vector< T > f(this->size());
397 for (Index i = 0; i < this->size(); i ++){ f[i] = T(data_[i]); }
398 return f;
399 }
400#endif
401
402// template < class T > operator const Vector< T >(){
403// //COUTMARKER
404// Vector< T > f(this->size());
405// for (Index i = 0; i < this->size(); i ++){ f[i] = T(data_[i]); }
406// return f;
407// }
408
409// template < > operator Vector< double >(){
410// COUTMARKER
411// Vector< double > f(this->size());
412// for (Index i = 0; i < this->size(); i ++){ f[i] = std::real(data_[i]); }
413// return f;
414// }
415
417 inline Vector< ValueType > & setVal(const ValueType & val) {
418 this->fill(val);
419 return *this;
420 }
421
425 inline Vector< ValueType > & setVal(const ValueType & val, const BVector & bv) {
426 ASSERT_EQUAL(this->size(), bv.size())
427 for (Index i = 0; i < bv.size(); i ++ ) if (bv[i]) data_[i] = val;
428 return *this;
429 }
430
433 inline Vector< ValueType > & setVal(const ValueType & val, Index i) {
434 ASSERT_RANGE(i, 0, this->size())
435 data_[i] = val;
436 return *this;
437 }
438
439
443 inline Vector< ValueType > & setVal(const ValueType & val,
444 Index start, SIndex end) {
445 Index e = (Index)end;
446 if (e > this->size()) e = this->size();
447 if (start > e) start = e;
448
449 std::fill(data_+ start, data_ + e, val);
450 return *this;
451 }
452
454 inline Vector< ValueType > & setVal(const ValueType & val,
455 const std::pair< Index, SIndex > & pair) {
456 return setVal(val, pair.first, pair.second);
457 }
458
460 inline Vector< ValueType > & setVal(const ValueType & val,
461 const IndexArray & ids) {
462 for (Index i = 0; i < ids.size(); i ++) setVal(val, ids[i]);
463 return *this;
464 }
465
468 inline Vector< ValueType > & setVal(const Vector < ValueType > & vals,
469 const IndexArray & ids) {
470 ASSERT_EQUAL(vals.size(), ids.size())
471 for (Index i = 0; i < ids.size(); i ++){
472// data_[iArray[i]] = vals[i];
473 setVal(vals[i], ids[i]);
474 }
475 return *this;
476 }
477
479 inline Vector< ValueType > & setVal(const Vector < ValueType > & vals,
480 Index start) {
481 Index newS = start + vals.size();
482 if (this->size() < newS) this->resize(newS);
483 this->setVal(vals, start, newS);
484
485 return *this;
486 }
487
488
492 inline Vector< ValueType > & setVal(const Vector < ValueType > & vals,
493 Index start, Index end) {
494 if (start > this->size()){
495 throwLengthError(WHERE_AM_I + " vals.size() < start " +
496 str(vals.size()) + " " + str(start) + " " + str(end)) ;
497 }
498
499 if (end > this->size()) end = this->size();
500 if (start > end) start = end;
501
502 if (vals.size() < (end - start)){
503 throwLengthError( WHERE_AM_I + " vals.size() < (end-start) " +
504 str(vals.size()) + " " + str(start) + " " + str(end)) ;
505 }
506
507 if (this->size() == vals.size()){
508 std::copy(&vals[start], &vals[end], &data_[start]);
509 } else {
510 std::copy(&vals[0], &vals[end - start], &data_[start]);
511 }
512 return *this;
513 }
514
516 inline Vector< ValueType > & setVal(const Vector < ValueType > & vals,
517 const std::pair< Index, SIndex > & pair) {
518 return setVal(vals, pair.first, pair.second);
519 }
520
521 inline Vector< ValueType > & push_back(const ValueType & v) {
522 resize(size_ + 1);
523 return setVal(v, size_ -1);
524 }
525
527 inline Vector< ValueType > & addVal(const Vector < ValueType > & vals,
528 Index start, Index end) {
529 if (end > this->size()) end = this->size();
530 if (start > end) start = end;
531
532 if (vals.size() < end - start){
533 throwLengthError(WHERE_AM_I + " vals.size() < (end-start) " +
534 str(vals.size()) + " " + str(start) + " " + str(end)) ;
535 }
536
537 if (this->size() == vals.size()){
538 for (Index i = start; i < end; i ++) data_[i] += vals[i];
539 } else {
540 for (Index i = start; i < end; i ++) data_[i] += vals[i - start];
541 }
542
543 return *this;
544 }
545
546 inline Vector< ValueType > & addVal(const Vector < ValueType > & vals,
547 const std::pair< Index, SIndex > & pair) {
548 return addVal(vals, pair.first, pair.second);
549 }
550
553 inline Vector< ValueType > & addVal(const Vector < ValueType > & vals,
554 const IndexArray & idx) {
555 ASSERT_EQUAL(idx.size(), vals.size())
556 //ASSERT_THIS_SIZE(max(idx))
557 for (Index i = 0; i < idx.size(); i ++) data_[idx[i]] += vals[i];
558
559 return *this;
560 }
561
563 inline Vector< ValueType > & addVal(const ValueType & val, Index i) {
564 ASSERT_RANGE(i, 0, this->size())
565 data_[i] += val;
566 return *this;
567 }
568
570 void add(const ElementMatrix < double > & A);
571
573 void add(const ElementMatrix < double > & A, const double & scale);
574
576 void add(const ElementMatrix < double > & A, const Pos & scale);
577
579 void add(const ElementMatrix < double > & A, const RMatrix & scale);
580
582 void add(const ElementMatrix < double > & A,
583 const Vector < double > & scale);
584
587 inline const ValueType & getVal(Index i) const {
588 ASSERT_RANGE(i, 0, this->size())
589 return data_[i];
590 }
591
592 Vector < ValueType > getVal(Index start, SIndex end) const {
593 Index e = (Index) end;
594 if (end < 0){
595 e = max(start, size_ + end);
596 }
597
598 Vector < ValueType > v(e-start);
599
600 if (start == e) return v;
601
602 if (start >= 0 && start < e){
603 std::copy(& data_[start], & data_[e], &v[0]);
604 } else {
605 throwLengthError(WHERE_AM_I + " bounds out of range " +
606 str(start) + " " + str(end) + " " + str(size_));
607 }
608 return v;
609 }
610
611 Vector < ValueType > getVal(const std::pair< Index, SIndex > & pair) const {
612 return getVal(pair.first, pair.second);
613 }
614
615#ifdef PYGIMLI
616// needed for: /usr/include/boost/python/def_visitor.hpp
617 bool operator < (const Vector< ValueType > & v) const { return false; }
618#else
619 BVector operator < (const Vector< ValueType > & v) const {
620 ASSERT_EQUAL(this->size(), v.size())
621
622 BVector ret(this->size(), 0);
623
624 std::less<ValueType> l;
625 for (Index i = 0; i < v.size(); i ++) ret[i] = l(data_[i], v[i]);
626 return ret;
627 }
628#endif
629
630#define DEFINE_COMPARE_OPERATOR_VEC__(OP, FUNCT) \
631 BVector operator OP (const Vector< ValueType > & v) const { \
632 ASSERT_EQUAL(this->size(), v.size()) \
633 BVector ret(this->size(), 0); \
634 FUNCT<ValueType> f; \
635 for (Index i = 0; i < v.size(); i ++) ret[i] = f(data_[i], v[i]); \
636 return ret; \
637 } \
638
639DEFINE_COMPARE_OPERATOR_VEC__(<=, std::less_equal)
640DEFINE_COMPARE_OPERATOR_VEC__(>=, std::greater_equal)
641DEFINE_COMPARE_OPERATOR_VEC__(>, std::greater)
642
643#undef DEFINE_COMPARE_OPERATOR_VEC__
644
645#define DEFINE_COMPARE_OPERATOR__(OP, FUNCT) \
646 inline BVector operator OP (const int & v) const { \
647 BVector ret(this->size(), 0); \
648 FUNCT<ValueType> f; \
649 for (Index i = 0; i < this->size(); i ++){ ret[i] = f(data_[i], ValueType(v)); } \
650 return ret;\
651 } \
652 inline BVector operator OP (const uint & v) const { \
653 BVector ret(this->size(), 0); \
654 FUNCT<ValueType> f; \
655 for (Index i = 0; i < this->size(); i ++){ ret[i] = f(data_[i], ValueType(v)); } \
656 return ret;\
657 } \
658 inline BVector operator OP (const ValueType & v) const { \
659 BVector ret(this->size(), 0); \
660 FUNCT<ValueType> f; \
661 for (Index i = 0; i < this->size(); i ++){ ret[i] = f(data_[i], v); } \
662 return ret;\
663 } \
664
665DEFINE_COMPARE_OPERATOR__(<, std::less)
666DEFINE_COMPARE_OPERATOR__(<=, std::less_equal)
667DEFINE_COMPARE_OPERATOR__(>=, std::greater_equal)
668DEFINE_COMPARE_OPERATOR__(==, std::equal_to)
669DEFINE_COMPARE_OPERATOR__(!=, std::not_equal_to)
670DEFINE_COMPARE_OPERATOR__(>, std::greater)
671
672#undef DEFINE_COMPARE_OPERATOR__
673
674// inline BVector operator > (const ValueType & v) const {
675// BVector ret(this->size(), 0);
676// std::transform(data_, data_ + size_, &ret[0], boost::bind(isGreater< ValueType >, _1, v));
677// return ret;
678// }
679
680#define DEFINE_UNARY_MOD_OPERATOR__(OP, FUNCT) \
681 inline Vector< ValueType > & operator OP##= (const Vector < ValueType > & v) { \
682 ASSERT_EQUAL_SIZE((*this), v) \
683 std::transform(data_, data_ + size_, &v[0], data_, FUNCT()); return *this; } \
684 inline Vector< ValueType > & operator OP##= (const ValueType & val) { \
685 for (Index i = 0; i < size_; i ++) data_[i] OP##= val; return *this; } \
686
687DEFINE_UNARY_MOD_OPERATOR__(+, PLUS)
688DEFINE_UNARY_MOD_OPERATOR__(-, MINUS)
689DEFINE_UNARY_MOD_OPERATOR__(/, DIVID)
690DEFINE_UNARY_MOD_OPERATOR__(*, MULT)
691
692#undef DEFINE_UNARY_MOD_OPERATOR__
693
695 inline Vector < ValueType > operator - () const { return *this * -1.0; }
696
698 void resize(Index n, ValueType fill){
699 if (n != size_){
700 reserve(n);
701 for (Index i = size_; i < n; i ++) data_[i]=fill;
702 size_ = n;
703 }
704 }
705 // default args lead win64 pygimli segfault .. WTF??
706 void resize(Index n){
707 resize(n, 0);
708 }
709
711 void reserve(Index n){
712
713 Index newCapacity = max(1, n);
714 if (capacity_ != 0){
715 int exp;
716 frexp(n, &exp);
717 newCapacity = pow(2, exp);
718 }
719// __MS(n << " " << capacity_ << " " << newCapacity)
720
721 if (newCapacity != capacity_) {
722 ValueType * buffer = new ValueType[newCapacity];
723
724 std::memcpy(buffer, data_,
725 sizeof(ValueType) * min(capacity_, newCapacity));
726 // std::destroy_at(data_);
727 if (data_) delete [] data_;
728 data_ = buffer;
729 capacity_ = newCapacity;
730 //std::copy(&tmp[0], &tmp[min(tmp.size(), n)], data_);
731 }
732 }
733
737 template< class V > Vector< ValueType > & fill(V * val) {
738 for (Index i = 0; i < size_; i ++) data_[i] = ValueType(val[i]);
739 //std::copy(val, val + size_, data_);
740 return *this;
741 }
742
744 Vector< ValueType > & fill(const ValueType & val) {
745 std::fill(data_, data_ + size_, val); return *this; }
746
748 template< class Ex > Vector< ValueType > & fill(Expr< Ex > expr){
749 for (Index i = 0; i < size_; i ++){
750 data_[i] = expr((ValueType)i);
751 }
752 return *this;
753 }
754
755 inline void assign(const Vector< ValueType > & v){
756 this->copy_(v);
757 }
758 template < class ExprOP > inline void assign(const ExprOP & v){
759 if (v.size()) {
760 resize(v.size());
761 v.assign(*this);
762 }
763 }
764 // /*! Fill the whole vector with function expr(i) */
765// template< class V > void fill(const V & val){
766// for (Index i = 0; i < size_; i ++) data_[i] = ValueType(val);
767// }
768
770 void clean(){
771 if (size_ > 0) std::memset(data_, '\0', sizeof(ValueType) * size_);
772 }
773
775 void clear(){ free_(); }
776
778 Vector< ValueType > & round(const ValueType & tolerance){
779 THROW_TO_IMPL
780 return *this;
781 }
782
783 //VectorIterator< ValueType > * end() { return end_; }
784
785 inline bool empty() const { return size() == 0; }
786
787 inline Index size() const { return size_; }
788
789 inline Index capacity() const { return capacity_; }
790
791 inline Index nThreads() const { return nThreads_; }
792
793 inline Index singleCalcCount() const { return singleCalcCount_; }
794
795 ValueType * data() { return data_; }
796
809 bool save(const std::string & filename, IOFormat format = Ascii) const {
810
811 if (filename.rfind(VECTORASCSUFFIX) != std::string::npos) format = Ascii;
812 else if (filename.rfind(VECTORBINSUFFIX) != std::string::npos) format = Binary;
813 std::string fname(filename);
814
815 if (format == Ascii){
816 if (fname.rfind(".") == std::string::npos) fname += VECTORASCSUFFIX;
817
818 std::ofstream file; file.open(fname.c_str());
819 if (!file) {
820 throwError(filename + ": " + strerror(errno));
821 }
822
823 file.setf(std::ios::scientific, std::ios::floatfield);
824 file.precision(14);
825
826 for (Index i = 0, imax = size_; i < imax; i ++) file << data_[i] << std::endl;
827 file.close();
828 } else {
829 if (fname.rfind(".") == std::string::npos) fname += VECTORBINSUFFIX;
830 // so haett ich das gern //
831 // std::ofstream file(filename.c_str(), std::ofstream::binary);
832 // std::copy(&a[0], &a[a.size()-1], ostream_iterator< double >(&file));
833 // file.close();
834 FILE *file; file = fopen(fname.c_str(), "w+b");
835 if (!file) {
836 throwError(filename + ": " + strerror(errno));
837 }
838
839 int64 count = (int64)size_;
840 Index ret = 0; ret = fwrite((char*)&count, sizeof(int64), 1, file);
841 if (ret == 0) {
842 fclose(file);
843 return false;
844 }
845 for (Index i = 0; i < size_; i++) ret = fwrite((char*)&data_[i], sizeof(ValueType), 1, file);
846 fclose(file);
847 }
848 return true;
849 }
850
857 bool load(const std::string & filename, IOFormat format = Ascii){
858
859 if (filename.rfind(VECTORASCSUFFIX) != std::string::npos) format = Ascii;
860 else if (filename.rfind(VECTORBINSUFFIX) != std::string::npos) format = Binary;
861
862 if (!fileExist(filename)){
863 if (fileExist(filename + VECTORBINSUFFIX))
864 return this->load(filename + VECTORBINSUFFIX, Binary);
865 if (fileExist(filename + VECTORASCSUFFIX))
866 return this->load(filename + VECTORASCSUFFIX, Ascii);
867 }
868
869 if (format == Ascii){
870 std::vector < ValueType > tmp;
871
872 std::fstream file; openInFile(filename.c_str(), &file);
873 ValueType val; while(file >> val) {
874 tmp.push_back(val);
875 }
876
877 //so haett ich das gern
878// std::ifstream file(filename.c_str());
879// std::copy( std::istream_iterator<double>(file),
880// std::istream_iterator<double>(),
881// std::back_inserter(tmp));
882
883//std::back_inserter< double > (tmp));
884 //std::copy(file.begin(), file.end(), back_inserter< double >(& tmp[0]));
885
886 this->resize(tmp.size());
887 std::copy(tmp.begin(), tmp.end(), &data_[0]);
888 file.close();
889
890 } else {
891 FILE *file;
892 file = fopen(filename.c_str(), "r+b");
893 if (!file) {
894 throwError(filename + ": " + strerror(errno));
895 }
896 Index ret = 0;
897 int64 size; ret = fread(&size, sizeof(int64), 1, file);
898 if (ret) this->resize(size);
899
900 ret = fread(&data_[0], sizeof(ValueType), size, file);
901 if (!ret) {
902 }
903 fclose(file);
904 }
905 return true;
906 }
907
908 VectorIterator< ValueType > beginPyIter() const { return VectorIterator< ValueType >(data_, size_); }
909 VectorIterator< ValueType > begin() const { return VectorIterator< ValueType >(data_, size_); }
910 VectorIterator< ValueType > end() const { return VectorIterator< ValueType >(data_ + size_, 0); }
911
912 Index hash() const {
913 Index seed = 0;
914 for (Index i = 0; i < this->size_; ++i) {
915 hashCombine(seed, this->data_[i]);
916 }
917 return seed;
918 }
919protected:
920
921 void free_(){
922 size_ = 0;
923 capacity_ = 0;
924 if (data_) delete [] data_;
925 data_ = NULL;
926 }
927
928 void copy_(const Vector< ValueType > & v){
929 if (v.size()) {
930 resize(v.size());
931 //"" check speed for memcpy here
932 //std::memcpy(data_, v.data_, sizeof(ValType)*v.size());
933 // memcpy is approx 0.3% faster but copy is extensively testet
934 // cleanest solution needs iterator rewriting:
935 // std::copy(v.begin(), v.end(), this->begin());
936 std::copy(&v[0], &v[v.size()], data_); // only works without bound check in subscription operator
937 }
938// __MS(data_)
939// __MS(*this)
940 }
941
942 template < class ExprOP > inline void assign_(const ExprOP & v) {
943 if (v.size()) {
944 //std::cout << "assign_" << v.size() << std::endl;
945 resize(v.size());
946 v.assign(*this);
947 }
948 }
949
950 Index size_;
951 ValueType * data_;
952 Index capacity_;
953
954 static const Index minSizePerThread = 10000;
955 static const int maxThreads = 8;
956 int nThreads_;
957 Index singleCalcCount_;
958};
959
960template <> DLLEXPORT void Vector< Pos >::clean();
961
962// /*! Implement specialized type traits in vector.cpp */
963template <> DLLEXPORT void Vector<double>::add(
964 const ElementMatrix < double >& A);
965template <> DLLEXPORT void Vector<double>::add(
966 const ElementMatrix < double >& A, const double & a);
967template <> DLLEXPORT void Vector<double>::add(
968 const ElementMatrix < double >& A, const Pos & a);
969template <> DLLEXPORT void Vector<double>::add(
970 const ElementMatrix < double >& A, const RMatrix & a);
971
972template< typename ValueType > void Vector< ValueType >::add(
973 const ElementMatrix < double >& A){ THROW_TO_IMPL}
974template< typename ValueType > void Vector< ValueType >::add(
975 const ElementMatrix < double >& A, const double & a){THROW_TO_IMPL}
976template< typename ValueType > void Vector< ValueType >::add(
977 const ElementMatrix < double >& A, const Pos & a){THROW_TO_IMPL}
978template< typename ValueType > void Vector< ValueType >::add(
979 const ElementMatrix < double >& A, const RMatrix & a){THROW_TO_IMPL}
980
981// removeme in V1.2, 20200727
982template <> DLLEXPORT void Vector<double>::add(
983 const ElementMatrix < double > & A, const RVector & a);
984// removeme in V1.2, 20200727
985template< typename ValueType > void Vector< ValueType >::add(
986 const ElementMatrix < double >& A, const Vector< double> & a){THROW_TO_IMPL}
987
988
989template < > inline
990Vector< double > & Vector< double >::round(const double & tolerance){
991#ifdef USE_BOOST_BIND
992 std::transform(data_, data_ + size_, data_, boost::bind(roundTo< double >, _1, tolerance));
993#else
994 for (Index i = 0; i < size_; i ++) data_[i] = roundTo(data_[i], tolerance);
995#endif
996 return *this;
997}
998
999template< class ValueType, class Iter > class AssignResult{
1000public:
1001 AssignResult(Vector< ValueType > & a, const Iter & result, Index start, Index end)
1002 : a_(&a), iter_(result), start_(start), end_(end){
1003 }
1004 void operator()() {
1005 ValueType * iter = a_->begin().ptr();
1006 //std::cout << start_ << " " << end_ << std::endl;
1007 for (Index i = start_; i < end_; i++) iter[i] = iter_[i];
1008 }
1009
1011 Iter iter_;
1012 Index start_;
1013 Index end_;
1014};
1015
1016struct BINASSIGN { template < class T > inline T operator()(const T & a, const T & b) const { return b; } };
1017
1018template< class ValueType, class Iter > void assignResult(Vector< ValueType > & v, const Iter & result) {
1019#ifdef EXPRVEC_USE_TEMPORARY_EXPRESSION
1020 // Make a temporary copy of the iterator. This is faster on segmented
1021 // architectures, since all the iterators are in the same segment.
1022 Iter result2 = result;
1023#else
1024 // Otherwise, cast away const (eek!). No harmful side effects.
1025 Iter& result2 = (Iter&)result;
1026#endif
1027
1028
1029#ifdef EXPRVEC_USE_BOOST_THREAD
1030//DEPRECATED
1031#error DONT USE THIS
1032 if (v.nThreads() == 1) {
1033 AssignResult< ValueType, Iter >(v, result, 0, v.size())();
1034 } else {
1035 boost::thread_group threads;
1036 for (Index i = 0; i < v.nThreads(); i ++){
1037 Index start = v.singleCalcCount() * i;
1038 Index end = v.singleCalcCount() * (i + 1);
1039 if (i == v.nThreads() -1) end = v.size();
1040 threads.create_thread(AssignResult< ValueType, Iter >(v, result, start, end));
1041 }
1042 threads.join_all();
1043 }
1044#else // no boost thread
1045 #ifdef EXPRVEC_USE_STD_ALGORITHM
1046
1047 std::transform(v.begin().ptr(),
1048 v.end().ptr(),
1049 result,
1050 v.begin().ptr(), BINASSIGN());
1051
1052 #else // no std algo
1053 #ifdef EXPRVEC_USE_INDIRECTION
1054 ValueType * iter = v.begin().ptr();
1055
1056 // Inlined expression
1057 for (Index i = v.size(); i--;) iter[i] = result2[i];
1058 #else
1059 ValueType * iter = v.begin().ptr();
1060 ValueType * end = v.end().ptr();
1061
1062 do {
1063 *iter = *result2; // Inlined expression
1064 ++result2;
1065 } while (++iter != end);
1066 #endif
1067 #endif
1068#endif
1069}
1070
1071template< class ValueType, class A > class __VectorExpr {
1072public:
1073 __VectorExpr(const A & a) : iter_(a) { }
1074
1075 inline ValueType operator [] (Index i) const { return iter_[i]; }
1076
1077 inline ValueType operator * () const { return *iter_; }
1078
1079 inline void operator ++ () { ++iter_; }
1080
1081 void assign(Vector< ValueType > & x) const { assignResult(x, *this); }
1082
1083 inline Index size() const { return iter_.size(); }
1084
1085 A * begin() { return iter_.begin(); }
1086 A * end() { return iter_.end(); }
1087
1088private:
1089 A iter_;
1090};
1091
1092template< class ValueType, class A, class Op > class __VectorUnaryExprOp {
1093public:
1094 __VectorUnaryExprOp(const A & a) : iter_(a) { }
1095
1096 inline ValueType operator [] (Index i) const { return Op()(iter_[i]); }
1097
1098 inline ValueType operator * () const { return Op()(*iter_); }
1099
1100 inline void operator ++ () { ++iter_; }
1101
1102 inline Index size() const { return iter_.size(); }
1103
1104private:
1105 A iter_;
1106};
1107
1108template< class ValueType, class A, class B, class Op > class __VectorBinaryExprOp {
1109public:
1110 __VectorBinaryExprOp(const A & a, const B & b) : iter1_(a), iter2_(b) { }
1111
1112 inline ValueType operator [] (Index i) const { return Op()(iter1_[i], iter2_[i]); }
1113
1114 inline ValueType operator * () const { return Op()(*iter1_, *iter2_); }
1115
1116 inline void operator ++ () { ++iter1_; ++iter2_; }
1117
1118 inline Index size() const { return iter2_.size(); }
1119
1120private:
1121 A iter1_;
1122 B iter2_;
1123};
1124
1125template< class ValueType, class A, class Op > class __VectorValExprOp {
1126public:
1127 __VectorValExprOp(const A & a, const ValueType & val) : iter_(a), val_(val) { }//__DS(val << " " << &val)}
1128
1129 inline ValueType operator [] (Index i) const { return Op()(iter_[i], val_); }
1130
1131 inline ValueType operator * () const { return Op()(*iter_, val_); }
1132
1133 inline void operator ++ () { ++iter_; }
1134
1135 inline Index size() const { return iter_.size(); }
1136
1137private:
1138 A iter_;
1139 ValueType val_;
1140};
1141
1142template< class ValueType, class A, class Op > class __ValVectorExprOp {
1143public:
1144 __ValVectorExprOp(const ValueType & val, const A & a) : iter_(a), val_(val) { }
1145
1146 inline ValueType operator [] (Index i) const { return Op()(val_, iter_[i]); }
1147
1148 inline ValueType operator * () const { return Op()(val_, *iter_); }
1149
1150 inline void operator ++ () { ++iter_; }
1151
1152 inline Index size() const { return iter_.size(); }
1153
1154private:
1155 A iter_;
1156 ValueType val_;
1157};
1158
1159#define DEFINE_UNARY_EXPR_OPERATOR__(OP, FUNCT)\
1160\
1161template < class T > \
1162__VectorExpr< T, __VectorUnaryExprOp< T, VectorIterator< T >, FUNCT > > \
1163OP(const Vector< T > & a){ \
1164 typedef __VectorUnaryExprOp< T, VectorIterator< T >, FUNCT > ExprT; \
1165 return __VectorExpr< T, ExprT >(ExprT(a.begin())); } \
1166\
1167template < class T, class A > \
1168__VectorExpr< T, __VectorUnaryExprOp< T, __VectorExpr< T, A >, FUNCT > > \
1169OP(const __VectorExpr< T, A > & a){ \
1170 typedef __VectorUnaryExprOp< T, __VectorExpr< T, A >, FUNCT > ExprT; \
1171 return __VectorExpr< T, ExprT >(ExprT(a)); \
1172} \
1173
1174DEFINE_UNARY_EXPR_OPERATOR__(abs, ABS_)
1175DEFINE_UNARY_EXPR_OPERATOR__(acot, ACOT)
1176DEFINE_UNARY_EXPR_OPERATOR__(atan, ATAN)
1177DEFINE_UNARY_EXPR_OPERATOR__(cos, COS)
1178DEFINE_UNARY_EXPR_OPERATOR__(cot, COT)
1179DEFINE_UNARY_EXPR_OPERATOR__(exp, EXP)
1180DEFINE_UNARY_EXPR_OPERATOR__(exp10, EXP10)
1181DEFINE_UNARY_EXPR_OPERATOR__(fabs, ABS_)
1182DEFINE_UNARY_EXPR_OPERATOR__(log, LOG)
1183DEFINE_UNARY_EXPR_OPERATOR__(log10, LOG10)
1184DEFINE_UNARY_EXPR_OPERATOR__(sign, SIGN)
1185DEFINE_UNARY_EXPR_OPERATOR__(sin, SIN)
1186DEFINE_UNARY_EXPR_OPERATOR__(sqrt, SQRT)
1187DEFINE_UNARY_EXPR_OPERATOR__(square, SQR)
1188DEFINE_UNARY_EXPR_OPERATOR__(tan, TAN)
1189DEFINE_UNARY_EXPR_OPERATOR__(tanh, TANH)
1190
1191#undef DEFINE_UNARY_EXPR_OPERATOR__
1192
1193#define DEFINE_EXPR_OPERATOR__(OP, FUNCT) \
1194template < class T > \
1195__VectorExpr< T, __VectorBinaryExprOp< T, VectorIterator< T >, VectorIterator< T >, FUNCT > > \
1196operator OP (const Vector< T > & a, const Vector< T > & b){ \
1197 typedef __VectorBinaryExprOp< T, VectorIterator< T >, VectorIterator< T >, FUNCT > ExprT; \
1198 return __VectorExpr< T, ExprT >(ExprT(a.begin(), b.begin())); \
1199} \
1200 \
1201template < class T > \
1202__VectorExpr< T, __VectorValExprOp< T, VectorIterator< T >, FUNCT > > \
1203operator OP (const Vector< T > & a, const T & val){ \
1204 typedef __VectorValExprOp< T, VectorIterator< T >, FUNCT > ExprT; \
1205 return __VectorExpr< T, ExprT >(ExprT(a.begin(), val)); \
1206} \
1207 \
1208template < class T > \
1209__VectorExpr< T, __ValVectorExprOp< T, VectorIterator< T >, FUNCT > > \
1210operator OP (const T & val, const Vector< T > & a){ \
1211 typedef __ValVectorExprOp< T, VectorIterator< T >, FUNCT > ExprT; \
1212 return __VectorExpr< T, ExprT >(ExprT(val, a.begin())); \
1213} \
1214 \
1215template< class T, class A > \
1216__VectorExpr< T, __VectorBinaryExprOp< T, __VectorExpr< T, A >, VectorIterator< T >, FUNCT > > \
1217operator OP (const __VectorExpr< T, A > & a, const Vector< T > & b){ \
1218 typedef __VectorBinaryExprOp< T, __VectorExpr< T, A >, VectorIterator< T >, FUNCT > ExprT; \
1219 return __VectorExpr< T, ExprT >(ExprT(a, b.begin())); \
1220} \
1221 \
1222template< class T, class A > \
1223__VectorExpr< T, __VectorBinaryExprOp< T, VectorIterator< T >, __VectorExpr< T, A >, FUNCT > > \
1224operator OP (const Vector< T > & a, const __VectorExpr< T, A > & b){ \
1225 typedef __VectorBinaryExprOp< T, VectorIterator< T >, __VectorExpr< T, A >, FUNCT > ExprT; \
1226 return __VectorExpr< T, ExprT >(ExprT(a.begin(), b)); \
1227} \
1228 \
1229template< class T, class A > \
1230__VectorExpr< T, __VectorValExprOp< T, __VectorExpr< T, A >, FUNCT > > \
1231 operator OP (const __VectorExpr< T, A > & a, const T & val){ \
1232 typedef __VectorValExprOp< T, __VectorExpr< T, A >, FUNCT > ExprT; \
1233 return __VectorExpr< T, ExprT >(ExprT(a, val)); \
1234} \
1235 \
1236template< class T, class A > \
1237__VectorExpr< T, __ValVectorExprOp< T, __VectorExpr< T, A >, FUNCT > > \
1238operator OP (const T & val, const __VectorExpr< T, A > & a){ \
1239 typedef __ValVectorExprOp< T, __VectorExpr< T, A >, FUNCT > ExprT; \
1240 return __VectorExpr< T, ExprT >(ExprT(val, a)); \
1241} \
1242 \
1243template< class T, class A, class B > \
1244__VectorExpr< T, __VectorBinaryExprOp< T, __VectorExpr< T, A >, __VectorExpr< T, B >, FUNCT > > \
1245operator OP (const __VectorExpr< T, A > & a, const __VectorExpr< T, B > & b){ \
1246 typedef __VectorBinaryExprOp< T, __VectorExpr< T, A >, __VectorExpr< T, B >, FUNCT > ExprT; \
1247 return __VectorExpr< T, ExprT >(ExprT(a, b)); \
1248} \
1249\
1250template< class T, class T2, class A > \
1251 __VectorExpr< T, __VectorValExprOp< T, __VectorExpr< T, A >, FUNCT > > \
1252 operator OP (const __VectorExpr< T, A > & a, const T2 & val){ \
1253 typedef __VectorValExprOp< T, __VectorExpr< T, A >, FUNCT > ExprT; \
1254 return __VectorExpr< T, ExprT >(ExprT(a, (T)val)); \
1255} \
1256 \
1257template< class T, class T2, class A > \
1258 __VectorExpr< T, __ValVectorExprOp< T, __VectorExpr< T, A >, FUNCT > > \
1259 operator OP (const T2 & val, const __VectorExpr< T, A > & a){ \
1260 typedef __ValVectorExprOp< T, __VectorExpr< T, A >, FUNCT > ExprT; \
1261 return __VectorExpr< T, ExprT >(ExprT((T)val, a)); \
1262} \
1263 \
1264template < class T, class T2 > \
1265 __VectorExpr< T, __ValVectorExprOp< T, VectorIterator< T >, FUNCT > > \
1266 operator OP (const T2 & val, const Vector< T > & a){ \
1267 typedef __ValVectorExprOp< T, VectorIterator< T >, FUNCT > ExprT; \
1268 return __VectorExpr< T, ExprT >(ExprT((T)val, a.begin())); \
1269} \
1270 \
1271template < class T, class T2 > \
1272 __VectorExpr< T, __VectorValExprOp< T, VectorIterator< T >, FUNCT > > \
1273 operator OP (const Vector< T > & a, const T2 & val){ \
1274 typedef __VectorValExprOp< T, VectorIterator< T >, FUNCT > ExprT; \
1275 return __VectorExpr< T, ExprT >(ExprT(a.begin(), (T)val)); \
1276} \
1277 \
1278
1279DEFINE_EXPR_OPERATOR__(+, PLUS)
1280DEFINE_EXPR_OPERATOR__(-, MINUS)
1281DEFINE_EXPR_OPERATOR__(*, MULT)
1282DEFINE_EXPR_OPERATOR__(/, DIVID)
1283
1284#undef DEFINE_EXPR_OPERATOR__
1285
1286//********************************************************************************
1287//** define some utility functions
1288
1289// inline bool operator < (const GIMLI::Vector<double>&a, const GIMLI::Vector<double> &b) {
1290// return false;
1291// }
1292
1293
1294template < class ValueType >
1295bool operator == (const Vector< ValueType > & v1, const Vector< ValueType > & v2){
1296 if (v1.size() != v2.size()) return false;
1297 for (Index i = 0; i < v1.size(); i ++){
1298 if (!isEqual(v1[i], v2[i])) {
1299 // for (Index j = 0; j < v1.size(); j ++){
1300 // __MS(j<< " " << v1[j] << " " << v2[j] << " " << v1[j]-v2[j] )
1301 // }
1302 return false;
1303 }
1304 }
1305 return true;
1306}
1307
1308template < class ValueType, class A >
1309bool operator == (const Vector< ValueType > & v1, const __VectorExpr< ValueType, A > & v2){
1310 return v1 == Vector< ValueType >(v2);
1311}
1312
1314template < class ValueType >
1316 return (max(abs(v)) < TOLERANCE);
1317}
1318
1320template < class ValueType >
1322 return !zero(v);
1323}
1324
1325template < class ValueType >
1326bool operator != (const Vector< ValueType > & v1, const Vector< ValueType > & v2){
1327 return !(v1==v2);
1328}
1329
1331template < class ValueType >
1332ValueType mult(const Vector< ValueType > & v1, const Vector< ValueType > & v2){
1333 return sum(v1*v2);
1334}
1335
1337template < class ValueType >
1338ValueType dot(const Vector< ValueType > & v1, const Vector< ValueType > & v2){
1339 return mult(v1,v2);
1340}
1341
1342//template double dot(const RVector & v1, const RVector & v2);
1343//template double mult(const RVector & v1, const RVector & v2);
1344
1346inline IndexArray find(const BVector & v){
1347 IndexArray idx;
1348 idx.reserve(v.size());
1349 for (Index i = 0; i < v.size(); i ++){
1350 if (v[i]) idx.push_back(i);
1351 }
1352 return idx;
1353}
1354
1356inline BVector operator ~ (const BVector & a){
1357 BVector ret(a.size());
1358 for (Index i = 0; i < ret.size(); i ++) ret[i] = !a[i];
1359 return ret;
1360}
1361
1363inline BVector inv(const BVector & a){
1364 return ~a;
1365}
1366
1368inline BVector operator & (const BVector & a, const BVector & b){
1369 BVector ret(a.size());
1370 for (Index i = 0; i < ret.size(); i ++) ret[i] = a[i] && b[i];
1371 return ret;
1372}
1373
1375inline BVector operator | (const BVector & a, const BVector & b){
1376 BVector ret(a.size());
1377 for (Index i = 0; i < ret.size(); i ++) ret[i] = a[i] || b[i];
1378 return ret;
1379}
1380
1381inline RVector operator * (const BVector & a, const RVector & b){
1382 RVector ret(a.size());
1383 for (Index i = 0; i < ret.size(); i ++) ret[i] = a[i] * b[i];
1384 return ret;
1385}
1386
1387inline RVector operator * (const RVector & a, const BVector & b){
1388 RVector ret(a.size());
1389 for (Index i = 0; i < ret.size(); i ++) ret[i] = a[i] * b[i];
1390 return ret;
1391}
1392
1393// /*! Refactor for speed */
1394// template < class ValueType, class A > BVector
1395// operator == (const __VectorExpr< ValueType, A > & vec, const ValueType & v){
1396// return Vector< ValueType >(vec) == v;
1397// }
1398
1399#define DEFINE_COMPARE_OPERATOR__(OP) \
1400template < class ValueType, class A > BVector \
1401operator OP (const __VectorExpr< ValueType, A > & vec, const ValueType & v){ \
1402 BVector ret(vec.size(), 0); \
1403 for (Index i = 0; i < ret.size(); i ++) ret[i] = vec[i] OP v; \
1404 return ret;\
1405} \
1406template < class ValueType > BVector \
1407operator OP (const std::vector < ValueType > & vec, const ValueType & v){ \
1408 BVector ret(vec.size(), 0); \
1409 for (Index i = 0; i < ret.size(); i ++) ret[i] = vec[i] OP v; \
1410 return ret;\
1411} \
1412
1413DEFINE_COMPARE_OPERATOR__(<)
1414DEFINE_COMPARE_OPERATOR__(<=)
1415DEFINE_COMPARE_OPERATOR__(>=)
1416DEFINE_COMPARE_OPERATOR__(==)
1417DEFINE_COMPARE_OPERATOR__(!=)
1418DEFINE_COMPARE_OPERATOR__(>)
1419
1420#undef DEFINE_COMPARE_OPERATOR__
1421
1422#define DEFINE_UNARY_COMPARE_OPERATOR__(OP, FUNCT) \
1423template < class ValueType, class A > \
1424BVector OP(const __VectorExpr< ValueType, A > & vec){ \
1425 BVector ret(vec.size(), 0); \
1426 for (Index i = 0; i < ret.size(); i ++) ret[i] = FUNCT()(vec[i]); \
1427 return ret; \
1428}\
1429template < class ValueType > \
1430BVector OP (const Vector< ValueType > & vec){ \
1431 BVector ret(vec.size(), 0); \
1432 std::transform(vec.begin().ptr(), vec.end().ptr(), ret.begin().ptr(), FUNCT()); \
1433 return ret; \
1434} \
1435
1436DEFINE_UNARY_COMPARE_OPERATOR__(isInf, ISINF)
1437DEFINE_UNARY_COMPARE_OPERATOR__(isNaN, ISNAN)
1438DEFINE_UNARY_COMPARE_OPERATOR__(isInfNaN, ISINFNAN)
1439
1440#undef DEFINE_UNARY_COMPARE_OPERATOR__
1441
1442template < class T > Vector < T > cat(const Vector< T > & a, const Vector< T > & b){
1443 Vector < T > c (a.size() + b.size());
1444 std::copy(&a[0], &a[a.size()], &c[0]);
1445 std::copy(&b[0], &b[b.size()], &c[a.size()]);
1446 return c;
1447}
1448
1449template < class T, class A > T sum(const __VectorExpr< T, A > & a){
1450 //std::cout << "sum(vectorExpr)" << std::endl;
1451 T tmp(0.0);
1452 for (Index i = 0, imax = a.size(); i < imax; i++) tmp += a[i];
1453 return tmp;
1454
1455// T tmp(0.0);
1456// __VectorExpr< T, A > al = a;
1457// for (Index i = 0; i < a.size(); i++, ++al) {
1458// tmp += *al;
1459// }
1460// return tmp;
1461
1462// T tmp(0.0);
1463// __VectorExpr< T, A > al = a;
1464// for (; al != a.end(); ++al){
1465// tmp += *al;
1466// }
1467// return tmp;
1468
1469
1470//return std::accumulate(a[0], a[a.size()], T());
1471}
1472
1473//** Templates argue with python bindings
1474inline Complex sum(const CVector & c){
1475 return std::accumulate(c.begin(), c.end(), Complex(0));
1476}
1477inline double sum(const RVector & r){
1478 return std::accumulate(r.begin(), r.end(), double(0));
1479}
1480inline SIndex sum(const IVector & i){
1481 return std::accumulate(i.begin(), i.end(), SIndex(0));
1482}
1483
1484
1485template < class T, class A > T min(const __VectorExpr< T, A > & a){ return min(Vector< T >(a)); }
1486template < class T, class A > T max(const __VectorExpr< T, A > & a){ return max(Vector< T >(a)); }
1487
1488inline Complex max(const CVector & v){
1489 ASSERT_EMPTY(v)
1490 Complex ret=v[0];
1491 for (Index i = 1; i < v.size(); i ++ ) if (v[i] > ret) ret = v[i];
1492 return ret;
1493}
1494
1495inline Complex min(const CVector & v){
1496 ASSERT_EMPTY(v)
1497 Complex ret=v[0];
1498 for (Index i = 1; i < v.size(); i ++ ) if (v[i] < ret) ret = v[i];
1499 return ret;
1500}
1501
1502template < class T > T min(const Vector < T > & v){
1503 ASSERT_EMPTY(v)
1504 return *std::min_element(&v[0], &v[0] + v.size());
1505}
1506template < class T > T max(const Vector < T > & v){
1507 ASSERT_EMPTY(v)
1508 return *std::max_element(&v[0], &v[0] + v.size());
1509}
1510
1511template < class T > void capMax(Vector < T > & v, T max){
1512 ASSERT_EMPTY(v)
1513 for (Index i = 0; i < v.size(); i ++ ) v[i] = min(v[i], max);
1514}
1515
1516template < class T > void capMin(Vector < T > & v, T min){
1517 ASSERT_EMPTY(v)
1518 for (Index i = 0; i < v.size(); i ++ ) v[i] = max(v[i], min);
1519}
1520
1521template < class ValueType >
1522 ValueType mean(const Vector < ValueType > & a){
1523 return sum(a) / ValueType(a.size());
1524}
1525
1526template < class ValueType, class A>
1527 ValueType mean(const __VectorExpr< ValueType, A > & a){
1528 return sum(a) / a.size();
1529}
1530
1531template < class ValueType > ValueType stdDev(const Vector < ValueType > & a){
1532 return std::sqrt(sum(square(a - mean(a))) / (double)(a.size() - 1));
1533}
1534
1535template < class ValueType > bool haveInfNaN(const Vector < ValueType > & v){
1536 for (VectorIterator < ValueType > it = v.begin(); it != v.end(); ++it){
1537 if (isInfNaN(*it)) return true;
1538 }
1539 return false;
1540}
1541
1542template < class ValueType > Vector < ValueType > fixZero(const Vector < ValueType > & v, const ValueType tol = TOLERANCE){
1543 Vector < ValueType > ret(v);
1544 for (VectorIterator < ValueType > it = ret.begin(); it != ret.end(); ++it){
1545 if (::fabs(*it) < TOLERANCE) *it = tol;
1546 }
1547 return ret;
1548}
1549
1550template < class ValueType > Vector < ValueType > round(const Vector < ValueType > & v, ValueType tol){
1551 return Vector< ValueType >(v).round(tol);
1552}
1553
1554template < class T >
1555Vector < T > fliplr(const Vector < T > & v){
1556 Vector < T > n(v.size());
1557 for (Index i = 0; i < v.size(); i ++) n[i] = v[v.size() - 1 - i];
1558 return n;
1559}
1560
1561template < class T, class A, class T2 > Vector < T > pow(const __VectorExpr< T, A > & a, T2 power){
1562 return pow(Vector< T >(a), power);
1563}
1564
1565template < class T > Vector < T > pow(const Vector < T > & v, const Vector < T > & npower){
1566 ASSERT_EQUAL(v.size(), npower.size())
1567
1568 Vector < T > r(v.size());
1569 for (Index i = 0; i < v.size(); i ++) r[i] = std::pow(v[i], T(npower[i]));
1570 return r;
1571}
1572
1573template < class T > Vector < T > pow(const Vector < T > & v, double npower){
1574 Vector < T > r(v.size());
1575 for (Index i = 0; i < v.size(); i ++){
1576 r[i] = std::pow(v[i], T(npower));
1577 }
1578 return r;
1579}
1580
1581// no template < int|double > since castxml interprets it as pow(vec,vec(int))
1582template < class T > Vector < T > pow(const Vector < T > & v, int npower){
1583 return pow(v, (double)npower);
1584}
1585
1586template < class T > Vector< T > sort(const Vector < T > & a){
1587 #ifndef PYGIMLI_CAST
1588 std::vector < T > tmp(a.size(), 0.0) ;
1589 for (Index i = 0; i < a.size(); i ++) tmp[i] = a[i];
1590 std::sort(tmp.begin(), tmp.end());
1591
1592 Vector < T > ret(tmp);
1593 return ret;
1594#endif // fixme .. implement me without std::vector
1595// Vector < T > t(a);
1596// std::sort(t.begin(), t.end());
1597 return Vector < T > (0);
1598}
1599
1604template < class T > Vector< T > unique(const Vector < T > & a){
1605#ifndef PYGIMLI_CAST
1606 std::vector < T > tmp(a.size()), u;
1607 for (Index i = 0; i < a.size(); i ++) tmp[i] = a[i];
1608 std::unique_copy(tmp.begin(), tmp.end(), back_inserter(u));
1609
1610 Vector < T > ret(u);
1611 return ret;
1612 #endif // fixme .. implement me without std::vector
1613 return Vector < T >(0);
1614}
1615
1616
1617//** Beware! this is not thread safe.
1618template< class ValueType > struct indexCmp {
1619 indexCmp(const Vector < ValueType > & arr) : arr_(arr) {}
1620 bool operator()(const Index a, const Index b) const {
1621 return arr_[a] < arr_[b];
1622 }
1623 const Vector < ValueType > & arr_;
1624};
1625
1626template < class ValueType >
1627void sort(const Vector < ValueType > & unsorted,
1628 Vector < ValueType > & sorted,
1629 IndexArray & indexMap){
1630
1631 indexMap.resize(unsorted.size());
1632
1633 for (Index i=0; i < unsorted.size(); i++) indexMap[i] = i;
1634
1635 // Sort the index map, using unsorted for comparison
1636
1637 std::vector < Index > tmp(indexMap.size(), 0.0) ;
1638 for (Index i = 0; i < indexMap.size(); i ++) tmp[i] = indexMap[i];
1639 std::sort(tmp.begin(), tmp.end(), indexCmp< ValueType >(unsorted));
1640 for (Index i = 0; i < indexMap.size(); i ++) indexMap[i] = tmp[i];
1641
1642 sorted = unsorted(indexMap);
1643}
1644
1645template < class ValueType >
1646IndexArray sortIdx(const Vector < ValueType > & unsorted){
1647 IndexArray indexMap;
1648 Vector < ValueType > sorted;
1649 sort(unsorted, sorted, indexMap);
1650 return indexMap;
1651}
1652
1653
1654// template < template < class T > class Vec, class T >
1655// std::ostream & operator << (std::ostream & str, const Vec < T > & vec){
1656// for (Index i = 0; i < vec.size(); i ++) str << vec[i] << " ";
1657// return str;
1658// }
1659
1660template < class T >
1661std::ostream & operator << (std::ostream & str, const std::vector < T > & vec){
1662 for (Index i = 0; i < vec.size(); i ++) str << vec[i] << " ";
1663 return str;
1664}
1665
1666template < class T >
1667std::ostream & operator << (std::ostream & str, const Vector < T > & vec){
1668 for (Index i = 0; i < vec.size(); i ++) str << vec[i] << " ";
1669 return str;
1670}
1671
1675template < class ValueType >
1677 const ValueType & last, Index n){
1678 if (abs(a) < 1e-12){
1679 throwError("Can't create increasing range for start value of: " +
1680 str(a) );
1681 }
1682
1683 if (a == last){
1684 throwError("start and end need to be different: " +
1685 str(a) + " " + str(last));
1686 }
1687
1688 if (sign(a) != sign(last)){
1689 throwError("Can't create increasing range from [0 " + str(a) + " to " + str(last) + "]");
1690 }
1691
1692 if (n < 3){
1693 throwError("need at least n > 2" + str(a) + " n(" + str(n) +") "+ str(last));
1694 }
1695
1696
1697 ValueType x = (last- (n-1) * a) / (n-2);
1698
1699 Placeholder x__;
1700 RVector y(n); y.fill(x__ * a);
1701 for (Index i = 2; i < n; i ++ ) y[i] += x*(i-1);
1702
1703 return y;
1704}
1705
1709template < class ValueType >
1710Vector< ValueType > increasingRange(const ValueType & first,
1711 const ValueType & last, Index n){
1712 if (sign(first) != sign(last)){
1713 throwError("cant increase range from [0 " + str(first) + " to " + str(last) + "]");
1714 }
1715
1716 Placeholder x__;
1717 RVector y(n + 1); y.fill(x__);
1718
1719 ValueType dy = (last - first * n) / (sum(y) - ValueType(n));
1720
1721 if (dy < 0.0){
1722// std::cout << "decreasing number of layers: " << n << " " << dy << std::endl;
1723 return increasingRange(first, last, n-1);
1724 }
1725
1726 ValueType yval = 0.0;
1727 for (Index i = 0; i < n; i ++){
1728 yval = yval + first + dy * i;
1729 y[i + 1] = yval;
1730 }
1731 //y = fliplr(y) * -1.0 //#+ g.max(y)
1732 return y;
1733 //return fliplr(y) * -1.0;
1734}
1735
1736template < class ValueType >
1737Vector < std::complex < ValueType > > toComplex(const Vector < ValueType > & re,
1738 const Vector < ValueType > & im){
1739 Vector < std::complex < ValueType > > cv(re.size());
1740 for (Index i = 0; i < cv.size(); i ++)
1741 cv[i] = std::complex < ValueType >(re[i], im[i]);
1742 return cv;
1743}
1744
1745// template < class ValueType >
1746// Vector < std::complex < ValueType > > toComplex(ValueType re, const Vector < ValueType > & im){
1747// return toComplex(Vector < ValueType > (im.size(), re), im);
1748// }
1749
1750// template < class ValueType >
1751// Vector < std::complex < ValueType > > toComplex(const Vector < ValueType > & re, ValueType im=0){
1752// return toComplex(re, Vector < ValueType > (re.size(), im));
1753// }
1754
1755inline CVector toComplex(const RVector & re, double im=0.){
1756 return toComplex(re, RVector(re.size(), im));
1757}
1758inline CVector toComplex(double re, const RVector & im){
1759 return toComplex(RVector(im.size(), re), im);
1760}
1761
1764inline CVector polarToComplex(const RVector & mag, const RVector & phi,
1765 bool mRad=false){
1766 log(Warning, "polarToComplex .. Do not use me" );
1767 ASSERT_EQUAL(mag.size(), phi.size())
1768 if (mRad){
1769 return polarToComplex(mag, phi / 1000.0, false);
1770 } else {
1771 return toComplex(RVector(mag * cos(phi)), RVector(-mag * sin(phi)));
1772 }
1773}
1774
1775template < class ValueType >
1776Vector < std::complex < ValueType > > operator * (const Vector < std::complex< ValueType > > & cv,
1777 const Vector < ValueType > & v){
1778 return cv * toComplex(v);
1779}
1780
1781template < class ValueType >
1782Vector < std::complex < ValueType > > operator * (const Vector < ValueType > & v,
1783 const Vector < std::complex< ValueType > > & cv){
1784 return cv * toComplex(v);
1785}
1786
1787template < class ValueType >
1788Vector < std::complex < ValueType > > operator / (const std::complex< ValueType > & v,
1789 const Vector < std::complex< ValueType > > & cv){
1790 Vector < std::complex< ValueType > > ret(cv.size());
1791 for (Index i = 0; i < ret.size(); i ++ ) {
1792 ret[i] = v / cv[i];
1793 }
1794 return ret;
1795}
1796
1797template < class ValueType >
1798Vector < std::complex < ValueType > > operator / (const ValueType & v,
1799 const Vector < std::complex< ValueType > > & cv){
1800 return std::complex< ValueType >(v) / cv;
1801}
1802
1803template < class ValueType, class A >
1804Vector < ValueType > real(const __VectorExpr< std::complex< ValueType >, A > & a){
1805 return real(Vector < std::complex< ValueType > >(a));
1806}
1807
1808template < class ValueType >
1809Vector < ValueType > real(const Vector < std::complex< ValueType > > & cv){
1810 Vector < ValueType > v(cv.size());
1811 for (Index i = 0; i < cv.size(); i ++) v[i] = cv[i].real();
1812 return v;
1813}
1814
1815template < class ValueType, class A >
1816Vector < ValueType > imag(const __VectorExpr< std::complex< ValueType >, A > & a){
1817 return imag(Vector < std::complex< ValueType > >(a));
1818}
1819
1820template < class ValueType >
1821Vector < ValueType > imag(const Vector < std::complex< ValueType > > & cv){
1822 Vector < ValueType > v(cv.size());
1823 for (Index i = 0; i < cv.size(); i ++) v[i] = cv[i].imag();
1824 return v;
1825}
1826
1827template < class ValueType, class A >
1828Vector < ValueType > angle(const __VectorExpr< std::complex< ValueType >, A > & a){
1829 return angle(Vector < std::complex< ValueType > >(a));
1830}
1831
1832template < class ValueType >
1833Vector < ValueType > angle(const Vector < std::complex< ValueType > > & z){
1834 Vector < ValueType > v(z.size());
1835 for (Index i = 0; i < z.size(); i ++) v[i] = std::atan2(imag(z[i]), real(z[i]));
1836 return v;
1837}
1838
1839inline RVector angle(const RVector & b, const RVector & a){
1840 ASSERT_EQUAL_SIZE(b, a)
1841 RVector v(b.size());
1842 for (Index i = 0; i < b.size(); i ++) v[i] = std::atan2(b[i], a[i]);
1843 return v;
1844}
1845
1846template < class ValueType >
1847Vector < ValueType > phase(const Vector < std::complex< ValueType > > & z){
1848 Vector < ValueType > v(z.size());
1849 for (Index i = 0; i < z.size(); i ++) v[i] = std::arg(z[i]);
1850 return v;
1851}
1852
1853
1854template < class ValueType, class A >
1855Vector < ValueType > abs(const __VectorExpr< std::complex< ValueType >, A > & a){
1856 return abs(Vector < std::complex< ValueType > >(a));
1857}
1858
1859template < class ValueType >
1860Vector < ValueType > abs(const Vector < std::complex< ValueType > > & cv){
1861 return sqrt(real(cv * conj(cv)));
1862}
1863
1864template < class ValueType, class A >
1865Vector < std::complex< ValueType > > conj(const __VectorExpr< std::complex< ValueType >, A > & a){
1866 return conj(Vector < std::complex< ValueType > >(a));
1867}
1868
1869template < class ValueType >
1870Vector < std::complex< ValueType > > conj(const Vector < std::complex< ValueType > > & cv){
1871 Vector < std::complex< ValueType > > v(cv.size());
1872 for (Index i = 0; i < cv.size(); i ++) v[i] = conj(cv[i]);
1873 return v;
1874}
1875
1876inline RVector TmpToRealHACK(const RVector & v){ return v; }
1877inline RVector TmpToRealHACK(const CVector & v){ __M return real(v); }
1878
1879#define DEFINE_SCALAR_COMPLEX_BINARY_OPERATOR(OP) \
1880template <class T, class U > \
1881inline std::complex< T > operator OP (const std::complex< T > & lhs, const U & rhs) { \
1882 std::complex< T > ret; \
1883 return ret OP##= rhs; \
1884} \
1885
1886DEFINE_SCALAR_COMPLEX_BINARY_OPERATOR(*)
1887DEFINE_SCALAR_COMPLEX_BINARY_OPERATOR(/)
1888DEFINE_SCALAR_COMPLEX_BINARY_OPERATOR(-)
1889DEFINE_SCALAR_COMPLEX_BINARY_OPERATOR(+)
1890
1891#undef DEFINE_SCALAR_COMPLEX_BINARY_OPERATOR
1892
1893inline IVector toIVector(const RVector & v){
1894 IVector ret(v.size());
1895 for (Index i = 0; i < ret.size(); i ++) ret[i] = int(v[i]);
1896 return ret;
1897}
1898
1899template < class ValueType >
1900bool save(const Vector< ValueType > & a, const std::string & filename,
1901 IOFormat format=Ascii){
1902 return saveVec(a, filename, format);
1903}
1904
1905template < class ValueType >
1906bool load(Vector< ValueType > & a, const std::string & filename,
1907 IOFormat format = Ascii,
1908 bool verbose=true){
1909 return loadVec(a, filename, format, verbose);
1910}
1911
1915template < class ValueType >
1916bool saveVec(const Vector< ValueType > & a, const std::string & filename,
1917 IOFormat format=Ascii){
1918 return a.save(filename, format);
1919}
1920
1924template < class ValueType >
1925bool loadVec(Vector < ValueType > & a,
1926 const std::string & filename,
1927 IOFormat format = Ascii){
1928 return a.load(filename, format);
1929}
1930
1931} // namespace GIMLI
1932
1933#ifndef PYGIMLI_CAST
1934namespace std {
1935 template<> struct hash< std::complex < double > > {
1936 GIMLI::Index operator()(const std::complex < double > & p) const noexcept {
1937 return GIMLI::hash(p.real(), p.imag());
1938 }
1939 };
1940 template<> struct hash< GIMLI::RVector > {
1941 GIMLI::Index operator()(const GIMLI::RVector & p) const noexcept {
1942 return p.hash();
1943 }
1944 };
1945 template<> struct hash< GIMLI::IndexArray > {
1946 GIMLI::Index operator()(const GIMLI::IndexArray & p) const noexcept {
1947 return p.hash();
1948 }
1949 };
1950 template<> struct hash< GIMLI::IVector > {
1951 GIMLI::Index operator()(const GIMLI::IVector & p) const noexcept {
1952 return p.hash();
1953 }
1954 };
1955 template<> struct hash< std::map< std::string, GIMLI::RVector > > {
1956 GIMLI::Index operator()(const std::map< std::string, GIMLI::RVector > & p) const noexcept {
1957 GIMLI::Index seed = 0;
1958 for (auto & x: p) {
1959 hashCombine(seed, x.first, x.second);
1960 }
1961 return seed;
1962 }
1963 };
1964}
1965#endif // PYGIMLI_CAST
1966
1967#endif
Definition vector.h:999
Definition expressions.h:214
3 dimensional vector
Definition pos.h:73
Definition vector.h:82
ValueType nextVal()
Definition vector.h:160
One dimensional array aka Vector of limited size.
Definition vector.h:186
Vector(Index n, const ValueType &val)
Definition vector.h:214
void reserve(Index n)
Definition vector.h:711
Vector< ValueType > & setVal(const Vector< ValueType > &vals, const IndexArray &ids)
Definition vector.h:468
bool save(const std::string &filename, IOFormat format=Ascii) const
Definition vector.h:809
const double & getVal(Index i) const
Definition vector.h:587
void add(const ElementMatrix< double > &A, const Vector< double > &scale)
Definition vector.h:985
Vector(const std::vector< ValueType > &v)
Definition vector.h:259
Vector< ValueType > & addVal(const Vector< ValueType > &vals, Index start, Index end)
Definition vector.h:527
Vector< ValueType > & setVal(const ValueType &val, const std::pair< Index, SIndex > &pair)
Definition vector.h:454
Vector< ValueType > & setVal(const ValueType &val, Index start, SIndex end)
Definition vector.h:443
Vector(const Vector< ValueType > &v)
Definition vector.h:231
Vector< ValueType > & setVal(const Vector< ValueType > &vals, const std::pair< Index, SIndex > &pair)
Definition vector.h:516
void clean()
Definition vector.h:770
Vector< ValueType > & setVal(const Vector< ValueType > &vals, Index start, Index end)
Definition vector.h:492
~Vector()
Definition vector.h:275
void add(const ElementMatrix< double > &A, const Pos &scale)
Definition vector.h:976
Vector< ValueType > & setVal(const ValueType &val, const IndexArray &ids)
Definition vector.h:460
Vector< ValueType > & setVal(const Vector< ValueType > &vals, Index start)
Definition vector.h:479
Vector< ValueType > copy() const
Definition vector.h:299
Vector(const Vector< ValueType > &v, Index start, Index end)
Definition vector.h:240
Vector(const std::string &filename, IOFormat format=Ascii)
Definition vector.h:223
Vector< ValueType > & fill(const ValueType &val)
Definition vector.h:744
Vector< ValueType > & round(const ValueType &tolerance)
Definition vector.h:778
Vector< double > & fill(V *val)
Definition vector.h:737
void add(const ElementMatrix< double > &A, const double &scale)
Definition vector.h:974
Vector< ValueType > & addVal(const Vector< ValueType > &vals, const IndexArray &idx)
Definition vector.h:553
void clear()
Definition vector.h:775
Vector< ValueType > & setVal(const ValueType &val)
Definition vector.h:417
Vector< ValueType > & addVal(const ValueType &val, Index i)
Definition vector.h:563
Vector(const __VectorExpr< ValueType, A > &v)
Definition vector.h:249
void add(const ElementMatrix< double > &A, const RMatrix &scale)
Definition vector.h:978
void resize(Index n, double fill)
Definition vector.h:698
Vector< ValueType > & setVal(const ValueType &val, Index i)
Definition vector.h:433
Vector()
Definition vector.h:198
void add(const ElementMatrix< double > &A)
Definition vector.h:972
bool load(const std::string &filename, IOFormat format=Ascii)
Definition vector.h:857
Vector< ValueType > & setVal(const ValueType &val, const BVector &bv)
Definition vector.h:425
Vector< ValueType > & fill(Expr< Ex > expr)
Definition vector.h:748
Definition vector.h:1071
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
Vector< ValueType > increasingRange(const ValueType &first, const ValueType &last, Index n)
Definition vector.h:1710
void hashCombine(Index &seed, const T &val)
Definition gimli.h:655
Matrix3< ValueType > inv(const Matrix3< ValueType > &A)
Definition matrix.h:1051
RVector y(const R3Vector &rv)
Definition pos.cpp:114
RVector x(const R3Vector &rv)
Definition pos.cpp:107
RVector z(const R3Vector &rv)
Definition pos.cpp:120
bool nonZero(const Vector< ValueType > &v)
Definition vector.h:1321
CVector polarToComplex(const RVector &mag, const RVector &phi, bool mRad=false)
Definition vector.h:1764
bool loadVec(Vector< ValueType > &a, const std::string &filename, IOFormat format=Ascii)
Definition vector.h:1925
Vector< T > unique(const Vector< T > &a)
Definition vector.h:1604
bool zero(const Vector< ValueType > &v)
Definition vector.h:1315
bool saveVec(const Vector< ValueType > &a, const std::string &filename, IOFormat format=Ascii)
Definition vector.h:1916
IndexArray find(const BVector &v)
Definition vector.h:1346
Vector< ValueType > increasingRange2(const ValueType &a, const ValueType &last, Index n)
Definition vector.h:1676
IOFormat
Definition gimli.h:289
bool load(Matrix< ValueType > &A, const std::string &filename)
Definition matrix.h:828
Definition vector.h:1016
Definition vector.h:1618