GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/nl/ext/monomial.h Lines: 2 2 100.0 %
Date: 2021-09-18 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Tim King, Gereon Kremer
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
 * Utilities for monomials.
14
 */
15
16
#ifndef CVC5__THEORY__ARITH__NL__EXT__MONOMIAL_H
17
#define CVC5__THEORY__ARITH__NL__EXT__MONOMIAL_H
18
19
#include <map>
20
#include <vector>
21
22
#include "expr/node.h"
23
24
namespace cvc5 {
25
namespace theory {
26
namespace arith {
27
namespace nl {
28
29
class MonomialDb;
30
class NlModel;
31
32
typedef std::map<Node, unsigned> NodeMultiset;
33
typedef std::map<Node, NodeMultiset> MonomialExponentMap;
34
35
/** An index data structure for node multisets (monomials) */
36
18126
class MonomialIndex
37
{
38
 public:
39
  /**
40
   * Add term to this trie. The argument status indicates what the status
41
   * of n is with respect to the current node in the trie, where:
42
   *   0 : n is equal, -1 : n is superset, 1 : n is subset
43
   * of the node described by the current path in the trie.
44
   */
45
  void addTerm(Node n,
46
               const std::vector<Node>& reps,
47
               MonomialDb* nla,
48
               int status = 0,
49
               unsigned argIndex = 0);
50
51
 private:
52
  /** The children of this node */
53
  std::map<Node, MonomialIndex> d_data;
54
  /** The monomials at this node */
55
  std::vector<Node> d_monos;
56
}; /* class MonomialIndex */
57
58
/** Context-independent database for monomial information */
59
class MonomialDb
60
{
61
 public:
62
  MonomialDb();
63
5201
  ~MonomialDb() {}
64
  /** register monomial */
65
  void registerMonomial(Node n);
66
  /**
67
   * Register monomial subset. This method is called when we infer that b is
68
   * a subset of monomial a, e.g. x*y^2 is a subset of x^3*y^2*z.
69
   */
70
  void registerMonomialSubset(Node a, Node b);
71
  /**
72
   * returns true if the multiset containing the
73
   * factors of monomial a is a subset of the multiset
74
   * containing the factors of monomial b.
75
   */
76
  bool isMonomialSubset(Node a, Node b) const;
77
  /** Returns the NodeMultiset for a registered monomial. */
78
  const NodeMultiset& getMonomialExponentMap(Node monomial) const;
79
  /** Returns the exponent of variable v in the given monomial */
80
  unsigned getExponent(Node monomial, Node v) const;
81
  /** Get the list of unique variables is the monomial */
82
  const std::vector<Node>& getVariableList(Node monomial) const;
83
  /** Get degree of monomial, e.g. the degree of x^2*y^2 = 4 */
84
  unsigned getDegree(Node monomial) const;
85
  /** Sort monomials in ms by their degree
86
   *
87
   * Updates ms so that degree(ms[i]) <= degree(ms[j]) for i <= j.
88
   */
89
  void sortByDegree(std::vector<Node>& ms) const;
90
  /** Sort the variable lists based on model values
91
   *
92
   * This updates the variable lists of monomials in ms based on the absolute
93
   * value of their current model values in m.
94
   *
95
   * In other words, for each i, getVariableList(ms[i]) returns
96
   * v1, ..., vn where |m(v1)| <= ... <= |m(vn)| after this method is invoked.
97
   */
98
  void sortVariablesByModel(std::vector<Node>& ms, NlModel& m);
99
  /** Get monomial contains children map
100
   *
101
   * This maps monomials to other monomials that are contained in them, e.g.
102
   * x^2 * y may map to { x, x^2, y } if these three terms exist have been
103
   * registered to this class.
104
   */
105
  const std::map<Node, std::vector<Node> >& getContainsChildrenMap();
106
  /** Get monomial contains parent map, reverse of above */
107
  const std::map<Node, std::vector<Node> >& getContainsParentMap();
108
  /**
109
   * Get contains difference. Return the difference of a and b or null if it
110
   * does not exist. In other words, this returns a term equivalent to a/b
111
   * that does not contain division.
112
   */
113
  Node getContainsDiff(Node a, Node b) const;
114
  /**
115
   * Get contains difference non-linear. Same as above, but stores terms of kind
116
   * NONLINEAR_MULT instead of MULT.
117
   */
118
  Node getContainsDiffNl(Node a, Node b) const;
119
  /** Make monomial remainder factor */
120
  Node mkMonomialRemFactor(Node n, const NodeMultiset& n_exp_rem) const;
121
122
 private:
123
  /** commonly used terms */
124
  Node d_one;
125
  /** list of all monomials */
126
  std::vector<Node> d_monomials;
127
  /** Map from monomials to var^index. */
128
  MonomialExponentMap d_m_exp;
129
  /**
130
   * Mapping from monomials to the list of variables that occur in it. For
131
   * example, x*x*y*z -> { x, y, z }.
132
   */
133
  std::map<Node, std::vector<Node> > d_m_vlist;
134
  /** Degree information */
135
  std::map<Node, unsigned> d_m_degree;
136
  /** monomial index, by sorted variables */
137
  MonomialIndex d_m_index;
138
  /** containment ordering */
139
  std::map<Node, std::vector<Node> > d_m_contain_children;
140
  std::map<Node, std::vector<Node> > d_m_contain_parent;
141
  std::map<Node, std::map<Node, Node> > d_m_contain_mult;
142
  std::map<Node, std::map<Node, Node> > d_m_contain_umult;
143
};
144
145
}  // namespace nl
146
}  // namespace arith
147
}  // namespace theory
148
}  // namespace cvc5
149
150
#endif /* CVC5__THEORY__ARITH__NL_MONOMIAL_H */