2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #include "gvn/InferenceConstraint.hxx"
17 InferenceConstraint::Result SameDimsConstraint::check(const std::vector<GVN::Value *> & values) const
19 const GVN::Value & R1 = *values[0];
20 const GVN::Value & C1 = *values[1];
21 const GVN::Value & R2 = *values[2];
22 const GVN::Value & C2 = *values[3];
24 if (R1.value == R2.value)
26 if (C1.value == C2.value)
31 MultivariatePolynomial mp = *C1.poly - *C2.poly;
32 if (mp.constant != 0 && mp.isCoeffPositive(false))
39 MultivariatePolynomial mp = *R1.poly - *R2.poly;
40 if (mp.constant > 0 && mp.isCoeffPositive(false))
48 MPolyConstraintSet SameDimsConstraint::getMPConstraints(const std::vector<GVN::Value *> & values) const
50 MPolyConstraintSet set(2);
51 const GVN::Value & R1 = *values[0];
52 const GVN::Value & C1 = *values[1];
53 const GVN::Value & R2 = *values[2];
54 const GVN::Value & C2 = *values[3];
56 set.add(*R1.poly - *R2.poly, MPolyConstraint::Kind::EQ0);
57 set.add(*C1.poly - *C2.poly, MPolyConstraint::Kind::EQ0);
62 void SameDimsConstraint::applyConstraints(const std::vector<GVN::Value *> & values) const
64 GVN::Value & R1 = *values[0];
65 GVN::Value & C1 = *values[1];
66 GVN::Value & R2 = *values[2];
67 GVN::Value & C2 = *values[3];
69 applyEquality(R1, R2);
70 applyEquality(C1, C2);
73 InferenceConstraint::Result EqualConstraint::check(const std::vector<GVN::Value *> & values) const
75 const GVN::Value & x = *values[0];
76 const GVN::Value & y = *values[1];
78 if (x.value == y.value)
84 MultivariatePolynomial mp = *x.poly - *y.poly;
85 if (mp.constant > 0 && mp.isCoeffPositive(false))
93 void EqualConstraint::applyConstraints(const std::vector<GVN::Value *> & values) const
95 GVN::Value & x = *values[0];
96 GVN::Value & y = *values[1];
101 MPolyConstraintSet EqualConstraint::getMPConstraints(const std::vector<GVN::Value *> & values) const
103 MPolyConstraintSet set(1);
104 const GVN::Value & x = *values[0];
105 const GVN::Value & y = *values[1];
107 set.add(*x.poly - *y.poly, MPolyConstraint::Kind::EQ0);
112 InferenceConstraint::Result MPolyConstraint::check(const std::vector<GVN::Value *> & values) const
114 MultivariatePolynomial mp = poly.eval(InferenceConstraint::getArgs(values));
115 //std::wcerr << "MPolyConstraint=" << poly << "::" << mp << std::endl;
120 if (mp.isConstant(0))
122 // for all X, P(X) == 0
125 else if (mp.constant != 0 && mp.isCoeffPositive(false))
127 // P(X) = Q(X) + K where K != 0 and Q with positive coeffs
128 return Result::FALSE;
132 return Result::DUNNO;
136 if (mp.constant != 0 && mp.isCoeffPositive(false))
140 else if (mp.isConstant(0))
142 return Result::FALSE;
146 return Result::DUNNO;
149 if (mp.isCoeffStrictPositive())
153 else if (mp.constant < 0 && mp.isCoeffNegative(false))
155 return Result::FALSE;
159 return Result::DUNNO;
163 if (mp.isCoeffPositive())
167 else if (mp.isConstant() && mp.constant < 0)
169 return Result::FALSE;
173 return Result::DUNNO;
179 MPolyConstraintSet MPolyConstraint::getMPConstraints(const std::vector<GVN::Value *> & values) const
181 MPolyConstraintSet set(1);
182 set.add(poly.eval(InferenceConstraint::getArgs(values)), kind);
187 void MPolyConstraint::applyConstraints(const std::vector<GVN::Value *> & values) const
191 if (poly.constant == 0 && poly.polynomial.size() == 2)
193 const MultivariateMonomial & m1 = *poly.polynomial.begin();
194 const MultivariateMonomial & m2 = *std::next(poly.polynomial.begin());
196 if ((m1.coeff == 1 && m2.coeff == -1) || (m1.coeff == -1 && m2.coeff == 1) && (m1.monomial.size() == 1 && m2.monomial.size() == 1))
198 // We have a polynomial P such as P(X,Y)=X-Y
199 GVN::Value & x = *values[m1.monomial.begin()->var];
200 GVN::Value & y = *values[m2.monomial.begin()->var];
208 InferenceConstraint::Result MPolyConstraintSet::check(const std::vector<GVN::Value *> & values) const
210 for (const auto & constraint : constraints)
212 Result res = constraint.check(values);
213 if (res != Result::TRUE)
221 MPolyConstraintSet MPolyConstraintSet::getMPConstraints(const std::vector<GVN::Value *> & values) const
223 MPolyConstraintSet set(constraints.size());
224 const std::vector<const MultivariatePolynomial *> args = InferenceConstraint::getArgs(values);
225 for (const auto & constraint : constraints)
227 set.add(constraint.poly.eval(args), constraint.kind);
232 void MPolyConstraintSet::applyConstraints(const std::vector<GVN::Value *> & values) const
234 for (const auto & mpc : constraints)
236 mpc.applyConstraints(values);
240 InferenceConstraint::Result PositiveConstraint::check(const std::vector<GVN::Value *> & values) const
242 const GVN::Value & x = *values[0];
244 if (x.poly->isCoeffPositive())
248 else if (x.poly->isConstant() && x.poly->constant < 0)
250 return Result::FALSE;
253 return Result::DUNNO;
256 void PositiveConstraint::applyConstraints(const std::vector<GVN::Value *> & values) const { }
258 MPolyConstraintSet PositiveConstraint::getMPConstraints(const std::vector<GVN::Value *> & values) const
260 MPolyConstraintSet set(1);
261 const GVN::Value & x = *values[0];
263 set.add(*x.poly, MPolyConstraint::Kind::GEQ0);
268 InferenceConstraint::Result GreaterConstraint::check(const std::vector<GVN::Value *> & values) const
270 const GVN::Value & x = *values[0];
271 const GVN::Value & y = *values[1];
273 if (x.value == y.value)
275 return Result::FALSE;
278 MultivariatePolynomial mp = *x.poly - *y.poly;
279 if (mp.constant > 0 && mp.isCoeffPositive(false))
283 else if (mp.constant < 0 && mp.isCoeffNegative(false))
285 return Result::FALSE;
288 return Result::DUNNO;
291 void GreaterConstraint::applyConstraints(const std::vector<GVN::Value *> & values) const { }
293 MPolyConstraintSet GreaterConstraint::getMPConstraints(const std::vector<GVN::Value *> & values) const
295 MPolyConstraintSet set(1);
296 const GVN::Value & x = *values[0];
297 const GVN::Value & y = *values[1];
299 set.add(*x.poly - *y.poly, MPolyConstraint::Kind::GT0);