summaryrefslogtreecommitdiff
path: root/src/theory/arith/row_vector.h
blob: 5fd471700bedd48155570a90a356a4daaf73fc99 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233


#include "cvc4_private.h"

#ifndef __CVC4__THEORY__ARITH__ROW_VECTOR_H
#define __CVC4__THEORY__ARITH__ROW_VECTOR_H

#include "theory/arith/arith_utilities.h"
#include "theory/arith/arithvar_set.h"
#include "util/rational.h"
#include <vector>

namespace CVC4 {
namespace theory {
namespace arith {

class VarCoeffPair {
private:
  ArithVar d_variable;
  Rational d_coeff;

public:
  VarCoeffPair(ArithVar v, const Rational& q): d_variable(v), d_coeff(q) {}

  ArithVar getArithVar() const { return d_variable; }
  Rational& getCoefficient() { return d_coeff; }
  const Rational& getCoefficient() const { return d_coeff; }

  bool operator<(const VarCoeffPair& other) const{
    return getArithVar() < other.getArithVar();
  }

  static bool variableLess(const VarCoeffPair& a, const VarCoeffPair& b){
    return a < b;
  }
};

/**
 * ReducedRowVector is a sparse vector representation that represents the
 * row as a strictly sorted array of "VarCoeffPair"s.
 * The row has a notion of a basic variable.
 * This is a variable that must have a coefficient of -1 in the array.
 */
class ReducedRowVector {
public:
  typedef std::vector<VarCoeffPair> VarCoeffArray;
  typedef VarCoeffArray::const_iterator const_iterator;

private:
  typedef std::vector<bool> ArithVarContainsSet;
  typedef VarCoeffArray::iterator iterator;

  /**
   * Invariants:
   * - isSorted(d_entries, true)
   * - noZeroCoefficients(d_entries)
   */
  VarCoeffArray d_entries;

  /**
   * Buffer for d_entries to reduce allocations by addRowTimesConstant.
   */
  VarCoeffArray d_buffer;

  /**
   * The basic variable associated with the row.
   * Must have a coefficient of -1.
   */
  ArithVar d_basic;


  /**
   * Invariants:
   * - This set is the same as the set maintained in d_entries.
   */
  ArithVarContainsSet d_contains;

  std::vector<uint32_t>& d_rowCount;
  std::vector<PermissiveBackArithVarSet>& d_columnMatrix;


public:

  ReducedRowVector(ArithVar basic,
                   const std::vector< ArithVar >& variables,
                   const std::vector< Rational >& coefficients,
                   std::vector<uint32_t>& count,
                   std::vector< PermissiveBackArithVarSet >& columnMatrix);
  ~ReducedRowVector();

  void enqueueNonBasicVariablesAndCoefficients(std::vector< ArithVar >& variables,
                                               std::vector< Rational >& coefficients) const;

  /** Returns the basic variable.*/
  ArithVar basic() const{
    Assert(basicIsSet());
    return d_basic;
  }

  /** Returns the number of nonzero variables in the vector. */
  uint32_t size() const {
    return d_entries.size();
  }

  /** Iterates over the nonzero entries in the vector. */
  const_iterator begin() const { return d_entries.begin(); }
  const_iterator end() const { return d_entries.end(); }

  /** Returns true if the variable is in the row. */
  bool has(ArithVar x_j) const{
    if(x_j >= d_contains.size()){
      return false;
    }else{
      return d_contains[x_j];
    }
  }

  /**
   * Returns the coefficient of a variable in the row.
   */
  const Rational& lookup(ArithVar x_j) const{
    Assert(has(x_j));
    Assert(hasInEntries(x_j));
    const_iterator lb = lower_bound(x_j);
    return (*lb).getCoefficient();
  }


  /** Prints the row to the buffer Debug("row::print"). */
  void printRow();

  /**
   * Changes the basic variable to x_j.
   * Precondition: has(x_j)
   */
  void pivot(ArithVar x_j);

  /**
   * Replaces other.basic() in the current row using the other row.
   * This assumes the other row represents an equality equal to zero.
   *
   *   \sum(this->entries) -= this->lookup(other.basic()) * (\sum(other.d_entries))
   * Precondition:
   *  has(other.basic())
   *  basic != other.basic()
   */
  void substitute(const ReducedRowVector& other);

  /**
   * Returns the reduced row as an equality with
   * the basic variable on the lhs equal to the sum of the non-basics variables.
   * The mapped from ArithVars to Nodes is specificied by map.
   */
  Node asEquality(const ArithVarToNodeMap& map) const;

private:

  /**
   * \sum(this->entries) += c * (\sum(other.d_entries) )
   *
   * Updates the current row to be the sum of itself and
   * another vector times c (c != 0).
   */
  void addRowTimesConstant(const Rational& c, const ReducedRowVector& other);


  /** Multiplies the coefficients of the RowVector by c (where c != 0). */
  void multiply(const Rational& c);

  /**
   * Adds v to d_contains.
   * This may resize d_contains.
   */
  static void addArithVar(ArithVarContainsSet& contains, ArithVar v);

  /** Removes v from d_contains. */
  static void removeArithVar(ArithVarContainsSet& contains, ArithVar v);


  /**
   * Let c be -1 if strictlySorted is true and c be 0 otherwise.
   * isSorted(arr, strictlySorted) is then equivalent to
   * If i<j, cmp(getArithVar(d_entries[i]), getArithVar(d_entries[j])) <= c.
   */
  static bool isSorted(const VarCoeffArray& arr, bool strictlySorted);

  /**
   * Zips together an array of variables and coefficients and appends
   * it to the end of an output vector.
   */
  static void zip(const std::vector< ArithVar >& variables,
                  const std::vector< Rational >& coefficients,
                  VarCoeffArray& output);

  /**
   * Debugging code.
   * noZeroCoefficients(arr) is equivalent to
   *  0 != getCoefficient(arr[i]) for all i.
   */
  static bool noZeroCoefficients(const VarCoeffArray& arr);

  /** Debugging code.*/
  bool matchingCounts() const;

  const_iterator lower_bound(ArithVar x_j) const{
    return std::lower_bound(d_entries.begin(), d_entries.end(), VarCoeffPair(x_j, 0));
  }

  /** Debugging code */
  bool wellFormed() const{
    return
      isSorted(d_entries, true) &&
      noZeroCoefficients(d_entries) &&
      basicIsSet() &&
      has(basic()) &&
      lookup(basic()) == Rational(-1);
  }

  bool basicIsSet() const { return d_basic != ARITHVAR_SENTINEL; }

  /** Debugging code. */
  bool hasInEntries(ArithVar x_j) const {
    return std::binary_search(d_entries.begin(), d_entries.end(), VarCoeffPair(x_j,0));
  }

}; /* class ReducedRowVector */


}/* namespace CVC4::theory::arith */
}/* namespace CVC4::theory */
}/* namespace CVC4 */

#endif /* __CVC4__THEORY__ARITH_ARITH_CONSTANTS_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback