PPL  1.2
Linear_Expression_defs.hh
Go to the documentation of this file.
1 /* Linear_Expression class declaration.
2  Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3  Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_Linear_Expression_defs_hh
25 #define PPL_Linear_Expression_defs_hh 1
26 
28 
29 #include "Constraint_types.hh"
30 #include "Generator_types.hh"
31 #include "Congruence_types.hh"
32 #include "Grid_Generator_types.hh"
33 #include "Linear_System_types.hh"
36 #include "Coefficient_types.hh"
37 #include "Polyhedron_types.hh"
38 #include "Grid_types.hh"
39 #include "PIP_Problem_types.hh"
41 #include "Scalar_Products_types.hh"
42 #include "MIP_Problem_types.hh"
43 #include "Box_types.hh"
44 #include "BD_Shape_types.hh"
45 #include "Octagonal_Shape_types.hh"
46 #include "termination_types.hh"
47 
51 
53 #include "Variable_defs.hh"
54 #include <cstddef>
55 
56 namespace Parma_Polyhedra_Library {
57 
58 // Put them in the namespace here to declare them friend later.
59 
61 
62 Linear_Expression
63 operator+(const Linear_Expression& e1, const Linear_Expression& e2);
64 
66 
67 Linear_Expression
68 operator+(Variable v, Variable w);
69 
71 
72 Linear_Expression
73 operator+(Variable v, const Linear_Expression& e);
74 
76 
77 Linear_Expression
78 operator+(const Linear_Expression& e, Variable v);
79 
81 
82 Linear_Expression
83 operator+(Coefficient_traits::const_reference n, const Linear_Expression& e);
84 
86 
87 Linear_Expression
88 operator+(const Linear_Expression& e, Coefficient_traits::const_reference n);
89 
91 
92 Linear_Expression
93 operator+(const Linear_Expression& e);
94 
96 
97 Linear_Expression
98 operator-(const Linear_Expression& e);
99 
101 
102 Linear_Expression
103 operator-(const Linear_Expression& e1, const Linear_Expression& e2);
104 
106 
107 Linear_Expression
108 operator-(Variable v, Variable w);
109 
111 
112 Linear_Expression
113 operator-(Variable v, const Linear_Expression& e);
114 
116 
117 Linear_Expression
118 operator-(const Linear_Expression& e, Variable v);
119 
121 
122 Linear_Expression
123 operator-(Coefficient_traits::const_reference n, const Linear_Expression& e);
124 
126 
127 Linear_Expression
128 operator-(const Linear_Expression& e, Coefficient_traits::const_reference n);
129 
131 
132 Linear_Expression
133 operator*(Coefficient_traits::const_reference n, const Linear_Expression& e);
134 
136 
137 Linear_Expression
138 operator*(const Linear_Expression& e, Coefficient_traits::const_reference n);
139 
141 
142 Linear_Expression&
143 operator+=(Linear_Expression& e1, const Linear_Expression& e2);
144 
146 
151 Linear_Expression&
152 operator+=(Linear_Expression& e, Variable v);
153 
155 
156 Linear_Expression&
157 operator+=(Linear_Expression& e, Coefficient_traits::const_reference n);
158 
160 
161 Linear_Expression&
162 operator-=(Linear_Expression& e1, const Linear_Expression& e2);
163 
165 
170 Linear_Expression&
171 operator-=(Linear_Expression& e, Variable v);
172 
174 
175 Linear_Expression&
176 operator-=(Linear_Expression& e, Coefficient_traits::const_reference n);
177 
179 
180 Linear_Expression&
181 operator*=(Linear_Expression& e, Coefficient_traits::const_reference n);
182 
184 
185 Linear_Expression&
186 operator/=(Linear_Expression& e, Coefficient_traits::const_reference n);
187 
189 
190 void
191 neg_assign(Linear_Expression& e);
192 
194 
195 Linear_Expression&
196 add_mul_assign(Linear_Expression& e,
197  Coefficient_traits::const_reference n, Variable v);
198 
200 
201 void add_mul_assign(Linear_Expression& e1,
202  Coefficient_traits::const_reference factor,
203  const Linear_Expression& e2);
204 
206 
207 void sub_mul_assign(Linear_Expression& e1,
208  Coefficient_traits::const_reference factor,
209  const Linear_Expression& e2);
210 
212 
213 Linear_Expression&
214 sub_mul_assign(Linear_Expression& e,
215  Coefficient_traits::const_reference n, Variable v);
216 
217 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
218 
229 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
230 int compare(const Linear_Expression& x, const Linear_Expression& y);
231 
232 namespace IO_Operators {
233 
235 
236 std::ostream& operator<<(std::ostream& s, const Linear_Expression& e);
237 
238 } // namespace IO_Operators
239 
240 } // namespace Parma_Polyhedra_Library
241 
243 
290 public:
292 
294  explicit Linear_Expression(Representation r = default_representation);
295 
302 
305 
306  // Queried by expression adapters.
309 
315  template <typename LE_Adapter>
316  explicit
317  Linear_Expression(const LE_Adapter& e,
318  typename
320  LE_Adapter>::value,
321  void*>::type = 0);
322 
326  template <typename LE_Adapter>
327  Linear_Expression(const LE_Adapter& e,
328  Representation r,
329  typename
331  LE_Adapter>::value,
332  void*>::type = 0);
333 
341  template <typename LE_Adapter>
342  explicit
343  Linear_Expression(const LE_Adapter& e,
344  dimension_type space_dim,
345  typename
347  LE_Adapter>::value,
348  void*>::type = 0);
349 
354  template <typename LE_Adapter>
355  Linear_Expression(const LE_Adapter& e,
356  dimension_type space_dim,
357  Representation r,
358  typename
360  LE_Adapter>::value,
361  void*>::type = 0);
362 
365 
368 
373  explicit Linear_Expression(Coefficient_traits::const_reference n,
374  Representation r = default_representation);
375 
377 
382  Linear_Expression(Variable v, Representation r = default_representation);
383 
386 
389 
392 
396  private:
397  public:
398  typedef std::bidirectional_iterator_tag iterator_category;
399  typedef const Coefficient value_type;
400  typedef std::ptrdiff_t difference_type;
401  typedef value_type* pointer;
402  typedef Coefficient_traits::const_reference reference;
403 
405 
408  explicit const_iterator();
409 
411 
417  const_iterator(const const_iterator& i);
418 
419  ~const_iterator();
420 
422 
428  void m_swap(const_iterator& i);
429 
431 
438 
440 
445 
447 
452 
454  reference operator*() const;
455 
457 
460  Variable variable() const;
461 
463 
467  bool operator==(const const_iterator& i) const;
468 
470 
474  bool operator!=(const const_iterator& i) const;
475 
476  private:
480 
482 
483  friend class Linear_Expression;
484  };
485 
488  const_iterator begin() const;
489 
492  const_iterator end() const;
493 
497 
500 
503 
506 
508  Coefficient_traits::const_reference coefficient(Variable v) const;
509 
511  void set_coefficient(Variable v,
512  Coefficient_traits::const_reference n);
513 
515  Coefficient_traits::const_reference inhomogeneous_term() const;
516 
518  void set_inhomogeneous_term(Coefficient_traits::const_reference n);
519 
522 
535  void linear_combine(const Linear_Expression& y, Variable v);
536 
539  void linear_combine(const Linear_Expression& y,
540  Coefficient_traits::const_reference c1,
541  Coefficient_traits::const_reference c2);
542 
546  Coefficient_traits::const_reference c1,
547  Coefficient_traits::const_reference c2);
548 
551 
553 
558  void remove_space_dimensions(const Variables_Set& vars);
559 
563 
565 
576  void permute_space_dimensions(const std::vector<Variable>& cycle);
577 
579  bool is_zero() const;
580 
585  bool all_homogeneous_terms_are_zero() const;
586 
588  static void initialize();
589 
591  static void finalize();
592 
594  static const Linear_Expression& zero();
595 
601 
604 
606  bool OK() const;
607 
609 
615  bool ascii_load(std::istream& s);
616 
618  void m_swap(Linear_Expression& y);
619 
622 
625  Representation r);
626 
629  bool is_equal_to(const Linear_Expression& x) const;
630 
633 
637  void normalize();
638 
641  void sign_normalize();
642 
647  bool all_zeroes(const Variables_Set& vars) const;
648 
649 private:
654  static const Linear_Expression* zero_p;
655 
657 
659 
663  Linear_Expression(dimension_type space_dim, bool,
664  Representation r = default_representation);
665 
666  // NOTE: This method is public, but it's not exposed in Linear_Expression,
667  // so that it can be used internally in the PPL, by friends of
668  // Linear_Expression.
670  Coefficient_traits::const_reference get(dimension_type i) const;
671 
672  // NOTE: This method is public, but it's not exposed in Linear_Expression,
673  // so that it can be used internally in the PPL, by friends of
674  // Linear_Expression.
676  void set(dimension_type i, Coefficient_traits::const_reference n);
677 
678  // NOTE: This method is public, but it's not exposed in Linear_Expression,
679  // so that it can be used internally in the PPL, by friends of
680  // Linear_Expression.
682  Coefficient_traits::const_reference get(Variable v) const;
683 
684  // NOTE: This method is public, but it's not exposed in Linear_Expression,
685  // so that it can be used internally in the PPL, by friends of
686  // Linear_Expression.
688  void set(Variable v, Coefficient_traits::const_reference n);
689 
694  bool all_zeroes(dimension_type start, dimension_type end) const;
695 
700 
706 
707  void exact_div_assign(Coefficient_traits::const_reference c,
709 
712 
726 
729  void linear_combine(const Linear_Expression& y,
730  Coefficient_traits::const_reference c1,
731  Coefficient_traits::const_reference c2,
733 
737  Coefficient_traits::const_reference c1,
738  Coefficient_traits::const_reference c2,
740 
742  void mul_assign(Coefficient_traits::const_reference n,
744 
748 
752 
756 
761  bool all_zeroes_except(const Variables_Set& vars,
762  dimension_type start, dimension_type end) const;
763 
765  void scalar_product_assign(Coefficient& result,
766  const Linear_Expression& y) const;
767 
769  void scalar_product_assign(Coefficient& result, const Linear_Expression& y,
770  dimension_type start, dimension_type end) const;
771 
773  int scalar_product_sign(const Linear_Expression& y) const;
774 
778  dimension_type start, dimension_type end) const;
779 
781  void has_a_free_dimension_helper(std::set<dimension_type>& x) const;
782 
784  bool is_equal_to(const Linear_Expression& x,
785  dimension_type start, dimension_type end) const;
786 
789  bool is_equal_to(const Linear_Expression& x,
790  Coefficient_traits::const_reference c1,
791  Coefficient_traits::const_reference c2,
792  dimension_type start, dimension_type end) const;
793 
795  void get_row(Dense_Row& r) const;
796 
798  void get_row(Sparse_Row& r) const;
799 
806  Variable first, Variable last) const;
807 
812  void negate(dimension_type first, dimension_type last);
813 
814  template <typename Row>
816 
817  // NOTE: The following classes are friends of Linear_Expression in order
818  // to access its private methods.
819  // Since they are *not* friend of Linear_Expression_Impl, they can only
820  // access its public methods so they cannot break the class invariant of
821  // Linear_Expression_Impl.
822  friend class Grid;
823  friend class Congruence;
824  friend class Polyhedron;
825  friend class PIP_Tree_Node;
826  friend class Grid_Generator;
827  friend class Generator;
828  friend class Constraint;
829  friend class Constraint_System;
830  friend class PIP_Problem;
831  friend class BHRZ03_Certificate;
832  friend class Scalar_Products;
833  friend class MIP_Problem;
834  friend class Box_Helpers;
835  friend class Congruence_System;
836  friend class BD_Shape_Helpers;
838  friend class Termination_Helpers;
839  template <typename T>
840  friend class BD_Shape;
841  template <typename T>
842  friend class Octagonal_Shape;
843  template <typename T>
844  friend class Linear_System;
845  template <typename T>
846  friend class Box;
847  template <typename T>
848  friend class Expression_Adapter;
849  template <typename T>
851  template <typename T>
852  friend class Expression_Hide_Last;
853 
854  friend Linear_Expression
855  operator+(const Linear_Expression& e1, const Linear_Expression& e2);
856  friend Linear_Expression
857  operator+(Coefficient_traits::const_reference n, const Linear_Expression& e);
858  friend Linear_Expression
859  operator+(const Linear_Expression& e, Coefficient_traits::const_reference n);
860  friend Linear_Expression
861  operator+(Variable v, const Linear_Expression& e);
862  friend Linear_Expression
864 
865  friend Linear_Expression
866  operator-(const Linear_Expression& e);
867 
868  friend Linear_Expression
869  operator-(const Linear_Expression& e1, const Linear_Expression& e2);
870  friend Linear_Expression
872  friend Linear_Expression
873  operator-(Coefficient_traits::const_reference n, const Linear_Expression& e);
874  friend Linear_Expression
875  operator-(const Linear_Expression& e, Coefficient_traits::const_reference n);
876  friend Linear_Expression
877  operator-(Variable v, const Linear_Expression& e);
878  friend Linear_Expression
879  operator-(const Linear_Expression& e, Variable v);
880 
881  friend Linear_Expression
882  operator*(Coefficient_traits::const_reference n, const Linear_Expression& e);
883  friend Linear_Expression
884  operator*(const Linear_Expression& e, Coefficient_traits::const_reference n);
885 
886  friend Linear_Expression&
888  friend Linear_Expression&
890  friend Linear_Expression&
891  operator+=(Linear_Expression& e, Coefficient_traits::const_reference n);
892 
893  friend Linear_Expression&
895  friend Linear_Expression&
897  friend Linear_Expression&
898  operator-=(Linear_Expression& e, Coefficient_traits::const_reference n);
899 
900  friend Linear_Expression&
901  operator*=(Linear_Expression& e, Coefficient_traits::const_reference n);
902  friend Linear_Expression&
903  operator/=(Linear_Expression& e, Coefficient_traits::const_reference n);
904 
905  friend void
907 
908  friend Linear_Expression&
910  Coefficient_traits::const_reference n, Variable v);
911  friend Linear_Expression&
913  Coefficient_traits::const_reference n, Variable v);
914 
915  friend void
917  Coefficient_traits::const_reference factor,
918  const Linear_Expression& e2);
919  friend void
921  Coefficient_traits::const_reference factor,
922  const Linear_Expression& e2);
923 
924  friend int
925  compare(const Linear_Expression& x, const Linear_Expression& y);
926 
927  friend std::ostream&
929  ::operator<<(std::ostream& s, const Linear_Expression& e);
930 };
931 
932 namespace Parma_Polyhedra_Library {
933 
935 
937 
939 
942 
943 } // namespace Parma_Polyhedra_Library
944 
946 
947 #endif // !defined(PPL_Linear_Expression_defs_hh)
Enable_If< Is_Singleton< T >::value, Interval< B, Info > >::type operator*(const Interval< B, Info > &x, const T &y)
bool operator==(const const_iterator &i) const
Compares *this with i.
bool all_zeroes_except(const Variables_Set &vars, dimension_type start, dimension_type end) const
Returns true if all coefficients in [start,end), except those corresponding to variables in vars...
A linear equality or inequality.
void swap(CO_Tree &x, CO_Tree &y)
void set_space_dimension(dimension_type n)
Sets the dimension of the vector space enclosing *this to n .
The base class for systems of constraints and generators.
A finite sequence of coefficients.
size_t dimension_type
An unsigned integral type for representing space dimensions.
An std::set of variables' indexes.
An adapter for Linear_Expression objects.
void set(dimension_type i, Coefficient_traits::const_reference n)
Sets the i-th coefficient to n.
A line, ray, point or closure point.
Variable variable() const
Returns the variable of the coefficient pointed to by *this.
bool ascii_load(std::istream &s)
Loads from s an ASCII representation (as produced by ascii_dump(std::ostream&) const) and sets *this ...
Coefficient_traits::const_reference inhomogeneous_term() const
Returns the inhomogeneous term of *this.
friend Linear_Expression operator+(const Linear_Expression &e1, const Linear_Expression &e2)
friend Linear_Expression & operator+=(Linear_Expression &e1, const Linear_Expression &e2)
void add_mul_assign(GMP_Integer &x, const GMP_Integer &y, const GMP_Integer &z)
Linear_Expression & operator=(const Linear_Expression &e)
Assignment operator.
std::ostream & operator<<(std::ostream &s, const Ask_Tell< D > &x)
A finite sparse sequence of coefficients.
friend Linear_Expression & operator/=(Linear_Expression &e, Coefficient_traits::const_reference n)
Linear_Expression_Interface::const_iterator_interface * itr
dimension_type space_dimension() const
Returns the dimension of the vector space enclosing *this.
bool OK() const
Checks if all the invariants are satisfied.
void set_coefficient(Variable v, Coefficient_traits::const_reference n)
Sets the coefficient of v in *this to n.
void set_inhomogeneous_term(Coefficient_traits::const_reference n)
Sets the inhomogeneous term of *this to n.
const_iterator & operator--()
Navigates to the previous nonzero coefficient.
A Parametric Integer (linear) Programming problem.
void get_row(Dense_Row &r) const
Sets r to a copy of the row that implements *this.
static void finalize()
Finalizes the class.
Coefficient gcd(dimension_type start, dimension_type end) const
Returns the gcd of the nonzero coefficients in [start,end). If all the coefficients in this range are...
bool is_equal_to(const Linear_Expression &x) const
Coefficient_traits::const_reference coefficient(Variable v) const
Returns the coefficient of v in *this.
friend Linear_Expression & operator*=(Linear_Expression &e, Coefficient_traits::const_reference n)
A dimension of the vector space.
friend Linear_Expression operator*(Coefficient_traits::const_reference n, const Linear_Expression &e)
bool have_a_common_variable(const Linear_Expression &x, Variable first, Variable last) const
Returns true if there is a variable from index first (included) to index last (excluded) whose coeffi...
The base class for convex polyhedra.
void negate(dimension_type first, dimension_type last)
Negates the elements from index first (included) to index last (excluded).
static dimension_type max_space_dimension()
Returns the maximum space dimension a Linear_Expression can handle.
#define PPL_OUTPUT_DECLARATIONS
void permute_space_dimensions(const std::vector< Variable > &cycle)
Permutes the space dimensions of the expression.
bool all_zeroes(const Variables_Set &vars) const
Returns true if the coefficient of each variable in vars[i] is .
static void initialize()
Initializes the class.
friend Linear_Expression & sub_mul_assign(Linear_Expression &e, Coefficient_traits::const_reference n, Variable v)
void set_representation(Representation r)
Converts *this to the specified representation.
memory_size_type total_memory_in_bytes() const
Returns a lower bound to the total size in bytes of the memory occupied by *this. ...
A class holding a constant called value that evaluates to true if and only if Base is the same type a...
const_iterator & operator=(const const_iterator &i)
Assigns i to *this .
A Mixed Integer (linear) Programming problem.
friend int compare(const Linear_Expression &x, const Linear_Expression &y)
Coefficient value
Definition: PIP_Tree.cc:618
A not necessarily closed, iso-oriented hyperrectangle.
Definition: Box_defs.hh:299
const_iterator & operator++()
Navigates to the next nonzero coefficient.
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
void swap_space_dimensions(Variable v1, Variable v2)
Swaps the coefficients of the variables v1 and v2 .
bool all_homogeneous_terms_are_zero() const
Returns true if and only if all the homogeneous terms of *this are .
friend void neg_assign(Linear_Expression &e)
int compare(const Linear_Expression &x, const Linear_Expression &y)
void linear_combine_lax(const Linear_Expression &y, Coefficient_traits::const_reference c1, Coefficient_traits::const_reference c2)
void remove_space_dimensions(const Variables_Set &vars)
Removes all the specified dimensions from the expression.
void has_a_free_dimension_helper(std::set< dimension_type > &x) const
Removes from the set x all the indexes of nonzero elements of *this.
void neg_assign(GMP_Integer &x)
The convergence certificate for the BHRZ03 widening operator.
The entire library is confined to this namespace.
Definition: version.hh:61
Representation representation() const
Returns the current representation of *this.
Enable_If< Is_Singleton< T >::value, Interval< B, Info > >::type operator+(const Interval< B, Info > &x, const T &y)
friend Linear_Expression & operator-=(Linear_Expression &e1, const Linear_Expression &e2)
An adapter for Linear_Expression that hides the inhomogeneous term.
A bounded difference shape.
static const Linear_Expression * zero_p
Holds (between class initialization and finalization) a pointer to the (zero-dimension space) constan...
int scalar_product_sign(const Linear_Expression &y) const
Computes the sign of the sum of (*this)[i]*y[i], for each i.
bool is_zero() const
Returns true if and only if *this is .
Adapters' base type (for template meta-programming).
void sub_mul_assign(GMP_Integer &x, const GMP_Integer &y, const GMP_Integer &z)
Sparse representation: only the nonzero coefficient are stored. If there are many nonzero coefficient...
void exact_div_assign(Coefficient_traits::const_reference c, dimension_type start, dimension_type end)
Enable_If< Is_Singleton< T >::value, Interval< B, Info > >::type operator-(const Interval< B, Info > &x, const T &y)
size_t memory_size_type
An unsigned integral type for representing memory size in bytes.
void mul_assign(Coefficient_traits::const_reference n, dimension_type start, dimension_type end)
Equivalent to (*this)[i] *= n, for each i in [start, end).
Coefficient c
Definition: PIP_Tree.cc:64
void scalar_product_assign(Coefficient &result, const Linear_Expression &y) const
Sets results to the sum of (*this)[i]*y[i], for each i.
void linear_combine(const Linear_Expression &y, Variable v)
dimension_type first_nonzero(dimension_type first, dimension_type last) const
static const Linear_Expression & zero()
Returns the (zero-dimension space) constant 0.
A class that provides a type member called type equivalent to T if and only if b is true...
A node of the PIP solution tree.
dimension_type num_zeroes(dimension_type start, dimension_type end) const
Returns the number of zero coefficient in [start, end).
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
friend Linear_Expression operator-(const Linear_Expression &e)
reference operator*() const
Returns the current element.
void m_swap(Linear_Expression &y)
Swaps *this with y.
An adapter for Linear_Expression that maybe hides the last coefficient.
friend Linear_Expression & add_mul_assign(Linear_Expression &e, Coefficient_traits::const_reference n, Variable v)
A grid line, parameter or grid point.
Linear_Expression(Representation r=default_representation)
Default constructor: returns a copy of Linear_Expression::zero().
A class implementing various scalar product functions.
bool operator!=(const const_iterator &i) const
Compares *this with i .
void shift_space_dimensions(Variable v, dimension_type n)