2
$\begingroup$

(Please see the edit part for the real problem)

I have two expressions which are same but written in different ways.

expr1 = ConditionalExpression[Plus[1, Times[-1, x]], 
  Inequality[0, LessEqual, x, LessEqual, Rational[1, 2]]]

enter image description here

expr2 = ConditionalExpression[Plus[1, Times[-1, x]], 
  LessEqual[0, x, Rational[1, 2]]]

enter image description here

Now if I use Equal function:

expr1 == expr2

enter image description here

It returns True together with domains combining of the two functions. However, I want it to return True only (no domain) if the functions are same and domains are same as well. How can I do that?
SameQ doesn't work here as they are same mathematically but the underlying representations are different.

EDIT: Basically I have a list like this.

list = {ConditionalExpression[Plus[1, Times[-1, x]], 
   Inequality[0, LessEqual, x, LessEqual, Rational[1, 2]]], 
  ConditionalExpression[Plus[1, Times[-1, x]], LessEqual[1, x, 2]], 
  ConditionalExpression[Plus[1, Times[-1, x]], 
   LessEqual[0, x, Rational[1, 2]]]}

enter image description here

I want to return the position of elements in the list that same as reffunc as follows:

reffunc = 
  ConditionalExpression[Plus[1, Times[-1, x]], 
   LessEqual[0, x, Rational[1, 2]]];
Position[list, reffunc]

I expected the positions of the first and third are returned but only the third is returned.

enter image description here

$\endgroup$
4
  • $\begingroup$ Simplify[expr1 == expr2, 0 <= x <= 1/2]? That simply returns True. If that's not what you want, though, then I am not sure that I understand what you would like to happen. $\endgroup$
    – MarcoB
    Commented Apr 12, 2022 at 13:11
  • $\begingroup$ @MarcoB yes, kind of work for that example when they have the same domain that would need me to separate the function expression and domains. For example I have two functions defined with conditional expression (or include domains). Now I want to make a function returns True if they're same. Like above, you see that expr1 and expr2 are mathematically same but using SameQ doesn't work as their representations are different. $\endgroup$
    – hana
    Commented Apr 12, 2022 at 13:17
  • $\begingroup$ You can remove the domain by simply taking only the first part of the answer: (expr1 == expr2)[[1]] $\endgroup$ Commented Apr 12, 2022 at 13:21
  • $\begingroup$ Let me update my question to make it more clear. $\endgroup$
    – hana
    Commented Apr 12, 2022 at 13:24

2 Answers 2

2
$\begingroup$
list = {ConditionalExpression[Plus[1, Times[-1, x]], 
    Inequality[0, LessEqual, x, LessEqual, Rational[1, 2]]], 
   ConditionalExpression[Plus[1, Times[-1, x]], LessEqual[1, x, 2]], 
   ConditionalExpression[Plus[1, Times[-1, x]], 
    LessEqual[0, x, Rational[1, 2]]]};

reffunc = 
  ConditionalExpression[Plus[1, Times[-1, x]], 
   LessEqual[0, x, Rational[1, 2]]];

pos = Assuming[reffunc[[-1]], Position[list // Simplify, reffunc[[1]]]]

(* {{1}, {3}} *)
$\endgroup$
2
$\begingroup$

You can use the RegionMeasure of the ImplicitRegion of the domains to test if they are equal.

ClearAll[conditionalExprMatch]
conditionalExprMatch[expr1_ConditionalExpression, expr2_ConditionalExpression] :=
 With[
  {
   res = expr1 == expr2
   , vars = Variables[First /@ {expr1, expr2}]
   }
  , If[First@res 
    && RegionMeasure@
      ImplicitRegion[
       And @@ MapAt[Not, Map[Last, {expr1, expr2}] , -1]
       , vars
       ] == 0
   , First@res
   , res
   , res
   ]
  ]

With

{
  expr1 = 
   ConditionalExpression[Plus[1, Times[-1, x]], 
    Inequality[0, LessEqual, x, LessEqual, Rational[1, 2]]]
  , expr2 = 
   ConditionalExpression[Plus[1, Times[-1, x]], 
    LessEqual[0, x, Rational[1, 2]]]
  , expr3 = 
   ConditionalExpression[Plus[1, Times[-1, x]], 
    LessEqual[0, x, Rational[1, 3]]]
  , expr4 = 
   ConditionalExpression[Plus[y, Times[-1, x]], 
    Inequality[0, LessEqual, x, LessEqual, Rational[1, 2]]]
  , expr5 = 
   ConditionalExpression[Plus[y, Times[-1, x]], 
    LessEqual[0, x, Rational[1, 2]]]
  } // Multicolumn

Mathematica graphics

Then

conditionalExprMatch[expr1, expr2]
True
conditionalExprMatch[expr2, expr3]

enter image description here

conditionalExprMatch[expr3, expr4]

enter image description here

conditionalExprMatch[expr4, expr5]
True

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.