File: Vector.cc

package info (click to toggle)
topcom 1.1.2%2Bds-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,788 kB
  • sloc: cpp: 37,616; sh: 4,262; makefile: 497; ansic: 49
file content (156 lines) | stat: -rw-r--r-- 3,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
////////////////////////////////////////////////////////////////////////////////
// 
// Vector.cc
//
//    produced: 13/03/98 jr
// last change: 13/03/98 jr
// 
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>

#include "Vector.hh"

namespace topcom {

  const char Vector::start_char = '[';
  const char Vector::end_char   = ']';
  const char Vector::delim_char = ',';

  const parameter_type Vector::capacity = 0;

  bool Vector::is_zero() const {
    for (parameter_type index = 0; index < size(); ++index) {
      if ((*this)(index) != FieldConstants::ZERO) {
	return false;
      }
    }
    return true;
  }
  Vector& Vector::canonicalize() {
    for (parameter_type index = 0; index < size(); ++index) {
      (*this)(index).canonicalize();
    }
    return *this;
  }

  Vector& Vector::add(const Vector& vector) {
#ifdef INDEX_CHECK
    assert(size() == vector.size());
#endif
    for (parameter_type index = 0; index < size(); ++index) {
      (*this)(index) += vector(index);
    }
    return *this;
  }
  Vector& Vector::scale(const Field& factor) {
    for (parameter_type index = 0; index < size(); ++index) {
      (*this)(index) *= factor;
    }
    return *this;  
  }
  Vector& Vector::stack(const Vector& vector) {
    const parameter_type current_maxindex(size());
    resize(size() + vector.size());
    for (parameter_type i = 0; i < vector.size(); ++i) {
      (*this)(i + current_maxindex) = vector(i);
    }
    return *this;
  }

  const Vector unit_vector(const parameter_type dim, const parameter_type pos) {
    Vector result(dim, FieldConstants::ZERO);
    result(pos) = FieldConstants::ONE;
    return result;
  }

  Field inner_product(const Vector& vector1, const Vector& vector2) {
#ifdef INDEX_CHECK
    assert(vector2.size() == vector1.size());
#endif
    Field result(FieldConstants::ZERO);
    for (parameter_type i = 0; i < vector1.size(); ++i) {
      result += (vector1(i) * vector2(i));
    }
    return result;
  }
  bool lex_abs_compare(const Vector& vector1, 
		       const Vector& vector2, 
		       const parameter_type start) {
#ifdef INDEX_CHECK
    assert(vector1.size() == vector2.size());
#endif
    if (start == vector1.size()) {
      return false;
    }
    if (abs(vector1(start)) > abs(vector2(start))) {
      return false;
    }
    else if (abs(vector1(start)) == abs(vector2(start))) {
      return lex_abs_compare(vector1, vector2, start + 1);
    }
    else {
      return true;
    }
  }

  // stream input/output:
  std::istream& Vector::read(std::istream& ist) {
    char c;
    Field elem;

    vector_data::clear();
    ist >> std::ws >> c;
    if (c == start_char) {
      while (ist >> std::ws >> c) {
	if (c == end_char) {
	  break;
	}
	if (c == delim_char) {
	  continue;
	}
	ist.putback(c);
	if (ist >> elem) {
	  vector_data::push_back(elem);
	}
	else {
#ifdef READ_DEBUG
	  std::cerr << "Vector::read(std::istream& ist):"
		    << c << " not of appropriate type." << std::endl;
#endif
	  ist.clear(std::ios::failbit);
	  return ist;
	}
      }
    }
    else {
#ifdef READ_DEBUG
      std::cerr << "Vector::read(std::istream& ist):"
		<< "missing `" << start_char << "'." << std::endl;
#endif
      ist.clear(std::ios::failbit);
      return ist;
    }
    ist.clear(std::ios::goodbit);
    this->canonicalize();
    return ist;
  }

  std::ostream& Vector::write(std::ostream& ost) const {
    ost << start_char;
    if (dim() > 0) {
      for (parameter_type i = 0; i < dim() - 1; ++i) {
	ost << (*this)[i] << delim_char;
      }
      if (dim() > 0) {
	ost << (*this)[dim() - 1];
      }
    }
    ost << end_char;
    return ost;
  }

}; // namespace topcom

// eof Vector.cc