carl  24.04
Computer ARithmetic Library
Definiteness.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Degree.h"
4 #include "SoSDecomposition.h"
5 
6 #include "../MultivariatePolynomial.h"
7 #include "../UnivariatePolynomial.h"
8 
9 #include <iostream>
10 
11 namespace carl {
12 
13 /**
14  * Regarding a polynomial \f$p\f$ as a function \f$p: X \rightarrow Y\f$, its definiteness gives information about the codomain \f$Y\f$.
15  */
16 enum class Definiteness {
17  /// Indicates that \f$y < 0 \forall y \in Y\f$.
18  NEGATIVE = 0,
19  /// Indicates that \f$y \leq 0 \forall y \in Y\f$.
20  NEGATIVE_SEMI = 1,
21  /// Indicates that values may be positive and negative.
22  NON = 2,
23  /// Indicates that \f$y \geq 0 \forall y \in Y\f$.
24  POSITIVE_SEMI = 3,
25  /// Indicates that \f$y > 0 \forall y \in Y\f$.
26  POSITIVE = 4
27 };
28 
29 inline std::ostream& operator<<(std::ostream& os, Definiteness d) {
30  switch (d) {
32  return os << "negative";
34  return os << "seminegative";
35  case Definiteness::NON:
36  return os << "none";
38  return os << "semipositive";
40  return os << "positive";
41  }
42  return os;
43 }
44 
45 template<typename Coeff>
47  if (t.monomial()) {
48  if (t.monomial()->is_square()) {
50  }
51  } else if (!carl::is_zero(t.coeff())) {
53  }
54  return Definiteness::NON;
55 }
56 
57 template<typename C, typename O, typename P>
58 Definiteness definiteness(const MultivariatePolynomial<C,O,P>& p, bool full_effort = true) {
59  // Todo: handle constant polynomials
60  if (is_linear(p)) {
61  CARL_LOG_DEBUG("carl.core", "Linear and hence " << Definiteness::NON);
62  return Definiteness::NON;
63  }
64  auto term = p.rbegin();
65  if (term == p.rend()) return Definiteness::NON;
66  Definiteness result = definiteness(*term);
67  CARL_LOG_DEBUG("carl.core", "Got " << result << " from first term " << *term);
68  ++term;
69  if (term == p.rend()) return result;
70  if (result > Definiteness::NON) {
71  for (; term != p.rend(); ++term) {
72  Definiteness termDefin = definiteness(*term);
73  if (termDefin > Definiteness::NON) {
74  if( termDefin > result ) result = termDefin;
75  } else {
76  result = Definiteness::NON;
77  break;
78  }
79  }
80  } else if (result < Definiteness::NON) {
81  for (; term != p.rend(); ++term) {
82  Definiteness termDefin = definiteness(*term);
83  if (termDefin < Definiteness::NON) {
84  if( termDefin < result ) result = termDefin;
85  } else {
86  result = Definiteness::NON;
87  break;
88  }
89  }
90  }
91  CARL_LOG_DEBUG("carl.core", "Eventually got " << result);
92  if( full_effort && result == Definiteness::NON && total_degree(p) == 2 )
93  {
94  assert( !is_constant(p) );
95  bool lTermNegative = carl::is_negative( p.lterm().coeff() );
97  if( p.has_constant_term() )
98  {
99  bool constPartNegative = carl::is_negative( p.constant_part() );
100  if( constPartNegative != lTermNegative ) return Definiteness::NON;
101  result = lTermNegative ? Definiteness::NEGATIVE : Definiteness::POSITIVE;
102  tmp -= p.constant_part();
103  }
104  else
105  {
106  result = lTermNegative ? Definiteness::NEGATIVE_SEMI : Definiteness::POSITIVE_SEMI;
107  }
108  if( lTermNegative )
109  tmp = -tmp;
110  if( !sos_decomposition(tmp, true ).empty() ) return result;
111  return Definiteness::NON;
112  }
113  return result;
114 }
115 
116 }
Implements utility functions concerning the (total) degree of monomials, terms and polynomials.
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
std::vector< std::pair< C, MultivariatePolynomial< C, O, P > > > sos_decomposition(const MultivariatePolynomial< C, O, P > &p, bool not_trivial=false)
bool is_constant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
Definiteness definiteness(const Term< Coeff > &t)
Definition: Definiteness.h:46
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
typename UnderlyingNumberType< P >::type Coeff
bool is_negative(const cln::cl_I &n)
Definition: operations.h:47
Definiteness
Regarding a polynomial as a function , its definiteness gives information about the codomain .
Definition: Definiteness.h:16
@ NEGATIVE_SEMI
Indicates that .
@ NEGATIVE
Indicates that .
@ NON
Indicates that values may be positive and negative.
@ POSITIVE
Indicates that .
@ POSITIVE_SEMI
Indicates that .
bool is_linear(const ContextPolynomial< Coeff, Ordering, Policies > &p)
auto total_degree(const Monomial &m)
Gives the total degree, i.e.
Definition: Degree.h:24
The general-purpose multivariate polynomial class.
const Term< Coeff > & lterm() const
The leading term.
const Coeff & constant_part() const
Retrieve the constant term of this polynomial or zero, if there is no constant term.
bool has_constant_term() const
Check if the polynomial has a constant term that is not zero.
Coefficient & coeff()
Get the coefficient.
Definition: Term.h:80
Monomial::Arg & monomial()
Get the monomial.
Definition: Term.h:91