GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/util/real_algebraic_number_poly_imp.cpp Lines: 33 75 44.0 %
Date: 2021-05-22 Branches: 22 152 14.5 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Gereon Kremer, Mathias Preiner
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Implementation of RealAlgebraicNumber based on libpoly.
14
 */
15
16
#include "base/cvc5config.h"
17
#include "util/real_algebraic_number.h"
18
19
#ifndef CVC5_POLY_IMP  // Make sure this comes after base/cvc5config.h
20
#error "This source should only ever be built if CVC5_POLY_IMP is on!"
21
#endif /* CVC5_POLY_IMP */
22
23
#include <poly/polyxx.h>
24
25
#include <limits>
26
27
#include "base/check.h"
28
#include "util/poly_util.h"
29
30
namespace cvc5 {
31
32
12
RealAlgebraicNumber::RealAlgebraicNumber(poly::AlgebraicNumber&& an)
33
12
    : d_value(std::move(an))
34
{
35
12
}
36
37
8
RealAlgebraicNumber::RealAlgebraicNumber(const Integer& i)
38
8
    : d_value(poly::DyadicRational(poly_utils::toInteger(i)))
39
{
40
8
}
41
42
2
RealAlgebraicNumber::RealAlgebraicNumber(const Rational& r)
43
{
44
4
  poly::Rational pr = poly_utils::toRational(r);
45
4
  auto dr = poly_utils::toDyadicRational(r);
46
2
  if (dr)
47
  {
48
2
    d_value = poly::AlgebraicNumber(dr.value());
49
  }
50
  else
51
  {
52
    d_value = poly::AlgebraicNumber(
53
        poly::UPolynomial({numerator(pr), -denominator(pr)}),
54
        poly::DyadicInterval(floor(pr), ceil(pr)));
55
  }
56
2
}
57
58
18
RealAlgebraicNumber::RealAlgebraicNumber(const std::vector<long>& coefficients,
59
                                         long lower,
60
18
                                         long upper)
61
{
62
#ifdef CVC5_ASSERTIONS
63
72
  for (long c : coefficients)
64
  {
65
54
    Assert(std::numeric_limits<std::int32_t>::min() <= c
66
           && c <= std::numeric_limits<std::int32_t>::max())
67
        << "Coefficients need to fit within 32 bit integers. Please use the "
68
           "constructor based on Integer instead.";
69
  }
70
#endif
71
36
  d_value = poly::AlgebraicNumber(poly::UPolynomial(coefficients),
72
54
                                  poly::DyadicInterval(lower, upper));
73
18
}
74
75
RealAlgebraicNumber::RealAlgebraicNumber(
76
    const std::vector<Integer>& coefficients,
77
    const Rational& lower,
78
    const Rational& upper)
79
{
80
  *this = poly_utils::toRanWithRefinement(
81
      poly::UPolynomial(poly_utils::toInteger(coefficients)), lower, upper);
82
}
83
RealAlgebraicNumber::RealAlgebraicNumber(
84
    const std::vector<Rational>& coefficients,
85
    const Rational& lower,
86
    const Rational& upper)
87
{
88
  Integer factor = Integer(1);
89
  for (const auto& c : coefficients)
90
  {
91
    factor = factor.lcm(c.getDenominator());
92
  }
93
  std::vector<poly::Integer> coeffs;
94
  for (const auto& c : coefficients)
95
  {
96
    Assert((c * factor).getDenominator() == Integer(1));
97
    coeffs.emplace_back(poly_utils::toInteger((c * factor).getNumerator()));
98
  }
99
  *this = poly_utils::toRanWithRefinement(
100
      poly::UPolynomial(std::move(coeffs)), lower, upper);
101
}
102
103
std::ostream& operator<<(std::ostream& os, const RealAlgebraicNumber& ran)
104
{
105
  return os << ran.getValue();
106
}
107
108
8
bool operator==(const RealAlgebraicNumber& lhs, const RealAlgebraicNumber& rhs)
109
{
110
8
  return lhs.getValue() == rhs.getValue();
111
}
112
bool operator!=(const RealAlgebraicNumber& lhs, const RealAlgebraicNumber& rhs)
113
{
114
  return lhs.getValue() != rhs.getValue();
115
}
116
24
bool operator<(const RealAlgebraicNumber& lhs, const RealAlgebraicNumber& rhs)
117
{
118
24
  return lhs.getValue() < rhs.getValue();
119
}
120
bool operator<=(const RealAlgebraicNumber& lhs, const RealAlgebraicNumber& rhs)
121
{
122
  return lhs.getValue() <= rhs.getValue();
123
}
124
bool operator>(const RealAlgebraicNumber& lhs, const RealAlgebraicNumber& rhs)
125
{
126
  return lhs.getValue() > rhs.getValue();
127
}
128
bool operator>=(const RealAlgebraicNumber& lhs, const RealAlgebraicNumber& rhs)
129
{
130
  return lhs.getValue() >= rhs.getValue();
131
}
132
133
6
RealAlgebraicNumber operator+(const RealAlgebraicNumber& lhs,
134
                              const RealAlgebraicNumber& rhs)
135
{
136
6
  return lhs.getValue() + rhs.getValue();
137
}
138
RealAlgebraicNumber operator-(const RealAlgebraicNumber& lhs,
139
                              const RealAlgebraicNumber& rhs)
140
{
141
  return lhs.getValue() - rhs.getValue();
142
}
143
4
RealAlgebraicNumber operator-(const RealAlgebraicNumber& ran)
144
{
145
4
  return -ran.getValue();
146
}
147
2
RealAlgebraicNumber operator*(const RealAlgebraicNumber& lhs,
148
                              const RealAlgebraicNumber& rhs)
149
{
150
2
  return lhs.getValue() * rhs.getValue();
151
}
152
153
RealAlgebraicNumber& operator+=(RealAlgebraicNumber& lhs,
154
                                const RealAlgebraicNumber& rhs)
155
{
156
  lhs.getValue() = lhs.getValue() + rhs.getValue();
157
  return lhs;
158
}
159
RealAlgebraicNumber& operator-=(RealAlgebraicNumber& lhs,
160
                                const RealAlgebraicNumber& rhs)
161
{
162
  lhs.getValue() = lhs.getValue() - rhs.getValue();
163
  return lhs;
164
}
165
RealAlgebraicNumber& operator*=(RealAlgebraicNumber& lhs,
166
                                const RealAlgebraicNumber& rhs)
167
{
168
  lhs.getValue() = lhs.getValue() * rhs.getValue();
169
  return lhs;
170
}
171
172
6
int sgn(const RealAlgebraicNumber& ran) { return sgn(ran.getValue()); }
173
2
bool isZero(const RealAlgebraicNumber& ran) { return is_zero(ran.getValue()); }
174
4
bool isOne(const RealAlgebraicNumber& ran) { return is_one(ran.getValue()); }
175
176
28194
}  // namespace cvc5