summaryrefslogtreecommitdiff
path: root/src/theory/arith/row_vector.cpp
blob: 01131c4c98326a01d3715803de6ca8fdcef4fe62 (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

#include "theory/arith/row_vector.h"

using namespace CVC4;
using namespace CVC4::theory;
using namespace CVC4::theory::arith ;

bool RowVector::isSorted(const VarCoeffArray& arr, bool strictlySorted) {
  if(arr.size() >= 2){
    NonZeroIterator curr = arr.begin();
    NonZeroIterator end = arr.end();
    ArithVar prev = getArithVar(*curr);
    ++curr;
    for(;curr != end; ++curr){
      ArithVar v = getArithVar(*curr);
      if(strictlySorted && prev > v) return false;
      if(!strictlySorted && prev >= v) return false;
      prev = v;
    }
  }
  return true;
}

bool RowVector::noZeroCoefficients(const VarCoeffArray& arr){
  for(NonZeroIterator curr = arr.begin(), end = arr.end();
      curr != end; ++curr){
    const Rational& coeff = getCoefficient(*curr);
    if(coeff == 0) return false;
  }
  return true;
}

void RowVector::zip(const std::vector< ArithVar >& variables,
                    const std::vector< Rational >& coefficients,
                    VarCoeffArray& output){

  Assert(coefficients.size() == variables.size() );

  vector<Rational>::const_iterator coeffIter = coefficients.begin();
  vector<Rational>::const_iterator coeffEnd = coefficients.end();
  vector<ArithVar>::const_iterator varIter = variables.begin();

  for(; coeffIter != coeffEnd; ++coeffIter, ++varIter){
    const Rational& coeff = *coeffIter;
    ArithVar var_i = *varIter;

    output.push_back(make_pair(var_i, coeff));
  }
}

RowVector::RowVector(const std::vector< ArithVar >& variables,
                     const std::vector< Rational >& coefficients,
                     std::vector<uint32_t>& counts):
  d_rowCount(counts)
{
  zip(variables, coefficients, d_entries);

  std::sort(d_entries.begin(), d_entries.end(), cmp);

  for(NonZeroIterator i=beginNonZero(), end=endNonZero(); i != end; ++i){
    ++d_rowCount[getArithVar(*i)];
  }

  Assert(isSorted(d_entries, true));
  Assert(noZeroCoefficients(d_entries));
}

void RowVector::merge(VarCoeffArray& arr,
                      const VarCoeffArray& other,
                      const Rational& c,
                      std::vector<uint32_t>& counts){
  VarCoeffArray copy = arr;
  arr.clear();

  iterator curr1 = copy.begin();
  iterator end1 = copy.end();

  NonZeroIterator curr2 = other.begin();
  NonZeroIterator end2 = other.end();

  while(curr1 != end1 && curr2 != end2){
    if(getArithVar(*curr1) < getArithVar(*curr2)){
      arr.push_back(*curr1);
      ++curr1;
    }else if(getArithVar(*curr1) > getArithVar(*curr2)){
      ++counts[getArithVar(*curr2)];

      arr.push_back( make_pair(getArithVar(*curr2), c * getCoefficient(*curr2)));
      ++curr2;
    }else{
      Rational res = getCoefficient(*curr1) + c * getCoefficient(*curr2);
      if(res != 0){
        ++counts[getArithVar(*curr2)];

        arr.push_back(make_pair(getArithVar(*curr1), res));
      }else{
        --counts[getArithVar(*curr2)];
      }
      ++curr1;
      ++curr2;
    }
  }
  while(curr1 != end1){
    arr.push_back(*curr1);
    ++curr1;
  }
  while(curr2 != end2){
    ++counts[getArithVar(*curr2)];

    arr.push_back(make_pair(getArithVar(*curr2), c * getCoefficient(*curr2)));
    ++curr2;
  }
}

void RowVector::multiply(const Rational& c){
  Assert(c != 0);

  for(iterator i = d_entries.begin(), end = d_entries.end(); i != end; ++i){
    getCoefficient(*i) *= c;
  }
}

void RowVector::addRowTimesConstant(const Rational& c, const RowVector& other){
  Assert(c != 0);

  merge(d_entries, other.d_entries, c, d_rowCount);
}

void RowVector::printRow(){
  for(NonZeroIterator i = beginNonZero(); i != endNonZero(); ++i){
    ArithVar nb = getArithVar(*i);
    Debug("tableau") << "{" << nb << "," << getCoefficient(*i) << "}";
  }
  Debug("tableau") << std::endl;
}

ReducedRowVector::ReducedRowVector(ArithVar basic,
                                   const std::vector<ArithVar>& variables,
                                   const std::vector<Rational>& coefficients,
                                   std::vector<uint32_t>& count):
  RowVector(variables, coefficients, count), d_basic(basic){


  VarCoeffArray justBasic;
  justBasic.push_back(make_pair(basic, Rational(-1)));

  merge(d_entries,justBasic, Rational(1), d_rowCount);

  Assert(wellFormed());
}

void ReducedRowVector::substitute(const ReducedRowVector& row_s){
  ArithVar x_s = row_s.basic();

  Assert(has(x_s));
  Assert(x_s != basic());

  Rational a_rs = lookup(x_s);
  Assert(a_rs != 0);

  addRowTimesConstant(a_rs, row_s);

  Assert(!has(x_s));
  Assert(wellFormed());
}

void ReducedRowVector::pivot(ArithVar x_j){
  Assert(has(x_j));
  Assert(basic() != x_j);
  Rational negInverseA_rs = -(lookup(x_j).inverse());
  multiply(negInverseA_rs);
  d_basic = x_j;

  Assert(wellFormed());
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback