2
$\begingroup$

I have two lists of linear equations:

list1 = {x - y == 0, x + 2y == 0, 3x - y == 0}  
list2 = {-x + y == 0, -x - 2y == 0, 3x - y == 0,  3x - 2y == 0} 

I want to check for intersections between these two lists of equations.
Clearly, x - y == 0 and -x + y == 0 represent the same equation, and x + 2y == 0 and -x - 2y == 0 also represent the same equation. However, if I compare them directly, they don't appear to be equal.

So, how can I find the intersecting equations?
The expected result would be something like this: {x - y == 0, x + 2y == 0, 3x - y == 0} or their equivalent forms.

$\endgroup$
8
  • $\begingroup$ I assume that if you apply Simplify to all of the equations you'll end up with each of them being in a canonical form. From there you could do direct comparisons. I'm not entirely sure if this is robust for all the cases you might need. $\endgroup$
    – lericr
    Commented Aug 22 at 4:34
  • $\begingroup$ @lericr I tried ChatGPT, and it did the same thing. I'm worried about it too. $\endgroup$
    – internet
    Commented Aug 22 at 4:36
  • $\begingroup$ Since all of your equations are ... == 0, the problem would seem to be reducible to determining whether the polynomials on the LHSs are equivalent. For that, I think you can use CoefficientRules. I'm not super familiar with CoefficientRules, so there might be gotchas there as well. $\endgroup$
    – lericr
    Commented Aug 22 at 4:42
  • $\begingroup$ @lericr The equations in my case differ only by flipping the sign of the entire equation and are quite simple, though they involve more variables. Do you think that simplifying them is reliable in this case? $\endgroup$
    – internet
    Commented Aug 22 at 5:04
  • 1
    $\begingroup$ SubtractSides /@ Intersection @@ Map[Simplify, {list1, list2}, 2] $\endgroup$
    – Bob Hanlon
    Commented Aug 22 at 5:08

5 Answers 5

4
$\begingroup$

Can intersect using Intersection once all right-hand-sides are zero. Just use Together on the quotient of left-hand-sides as the SameTest. The result will contain an element from each equivalence class that occurs in both lists.

list1 = {x - y == 0, x + 2 y == 0, 3 x - y == 0};
list2 = {-x + y == 0, -x - 2 y == 0, 3 x - y == 0, 3 x - 2 y == 0};
newl1 = Map[SubtractSides, list1];
newl2 = Map[SubtractSides, list2];
Intersection[newl1, newl2, 
 SameTest -> (NumberQ[Together[#1[[1]]/#2[[1]]]] &)]

(* Out[12]= {x - y == 0, 3 x - y == 0, x + 2 y == 0} *)
$\endgroup$
3
$\begingroup$

To get an overview, draw a picture:

list1 = {x - y == 0, x + 2 y == 0, 3 x - y == 0}
list2 = {-x + y == 0, -x - 2 y == 0, 3 x - y == 0, 3 x - 2 y == 0}

ContourPlot[list1, {x, -d, d}, {y, -1, 1}]
ContourPlot[list2, {x, -d, d}, {y, -1, 1}]

enter image description here

It is clear that list2 contains an additional equation as last item. Therefore to prove this:

list1-Most[list2] //Simplify

{0, 0, 0

And the intersection point is:

Solve[list1[[1 ;; 2]], {x, y}]

{{x -> 0, y -> 0}}
$\endgroup$
3
$\begingroup$

Reduce the list

SubtractSides/@List@@FullSimplify[Reduce[Or@@Join[list1,list2]]]
(* {x + 2 y == 0, 3 x - y == 0, 3 x - 2 y == 0, x - y == 0}  *)

Sort by slope

SortBy[%,D[#[[1]],x]/D[#[[1]],y] &]
(* {3 x - y == 0, 3 x - 2 y == 0, x - y == 0, x + 2 y == 0} *)

Plot

Plot[
   Evaluate[
      ReplaceAll[y,First@Solve[#,y]]& /@ %
   ]
   , {x, -10,10}
   , PlotLegends->%
]

enter image description here

$\endgroup$
3
$\begingroup$
  • Follow the idea by @Daniel Lichtblau ( +1 );
  • Using RegionEqual( or ForAll[{x, y}, Equivalent[expr1, expr2]] // Resolve);
Clear["Global`*"];
list1 = {x - y == 0, x + 2 y == 0, 3 x - y == 0};
list2 = {-x + y == 0, -x - 2 y == 0, 3 x - y == 0, 3 x - 2 y == 0};
list1 = ImplicitRegion[#, {x, y}] & /@ list1;
list2 = ImplicitRegion[#, {x, y}] & /@ list2;
Intersection[list1, list2, SameTest -> RegionEqual ][[;; , 1]]

{x - y == 0, 3 x - y == 0, x + 2 y == 0}

  • test another example.
Clear["Global`*"];
list1 = {x - y == 0, x + 2 y == 0, 
   3 x - y == 0, (x - 1)^2 + 2 (y - 3)^2 - 1 == 0, 
   Abs[x] + Abs[y] == 1, x == 3 || x == 2};
list2 = {-x + y == 0, -x - 2 y == 0, 3 x - y == 0, 3 x - 2 y == 0, 
   18 - 2 x + x^2 - 12 y + 2 y^2 == 0, (Abs[x] + Abs[y])^2 == 
    1, (x - 3) (x - 2) == 0};
list1 = ImplicitRegion[#, {x, y}] & /@ list1;
list2 = ImplicitRegion[#, {x, y}] & /@ list2;
Intersection[list1, list2, SameTest -> RegionEqual][[;; , 1]]

{-1 + (-1 + x)^2 + 2 (-3 + y)^2 == 0, x - y == 0, 3 x - y == 0, x + 2 y == 0, Abs[x] + Abs[y] == 1, x == 3 || x == 2}

$\endgroup$
2
$\begingroup$

Something bland (as equations are linear 2D:

list1 = {x - y == 0, x + 2 y == 0, 3 x - y == 0};
list2 = {-x + y == 0, -x - 2 y == 0, 3 x - y == 0, 3 x - 2 y == 0};
Intersection[list1, list2, 
 SameTest -> (SolveValues[#1, y][[1]] === SolveValues[#2, y][[1]] &)]

yields: {x - y == 0, 3 x - y == 0, x + 2 y == 0}

$\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.