# How to make function return True if two functions and domains are same?

(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]]]


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


Now if I use Equal function:

expr1 == expr2


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]]]}


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.

• 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. Commented Apr 12, 2022 at 13:11
• @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.
– hana
Commented Apr 12, 2022 at 13:17
• You can remove the domain by simply taking only the first part of the answer: (expr1 == expr2)[[1]] Commented Apr 12, 2022 at 13:21
• Let me update my question to make it more clear.
– hana
Commented Apr 12, 2022 at 13:24

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}} *)


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


Then

conditionalExprMatch[expr1, expr2]

True

conditionalExprMatch[expr2, expr3]


conditionalExprMatch[expr3, expr4]


conditionalExprMatch[expr4, expr5]

True


Hope this helps.