2
$\begingroup$

How to gather functions which intersect at the same point (real solution)?

func = {2 x, x^2 + 1, (x^3 + 3)/2};

enter image description here

For example, with the list of functions above I want it to be divided into 3 groups as follows:
(I'm not sure if I missed any, I just looked at the plot to group them)

out= {{2 x, x^2 + 1, (x^3 + 3)/2}, {2 x, (x^3 + 3)/2}, { x^2 + 1, (x^3 + 3)/2}}

EDIT: rhermans gave a nice answer. However, I didn't know that the number of functions would change the approach. I should have mentioned that in my case, the number of functions is quite large, probably 100 or more. So some apporach may work very nice for small number of functions but cause insufficient memory with large number of functions.

$\endgroup$
11
  • 2
    $\begingroup$ Why not group like this: sb2 = Subsets[func, {2}] ? Then subtract, set to zero, solve and find that the common point of intersection for each pair is at x->1. $\endgroup$
    – Syed
    Jan 19, 2023 at 12:26
  • $\begingroup$ @Syed In the example above, I just gave a simple example of 3 functions but there are probably more than 2 functions intersect. $\endgroup$
    – emnha
    Jan 19, 2023 at 12:27
  • $\begingroup$ @Syed made a nice suggestion. You can use new=Equal @@@ Subsets[func, {2}] to get all possible equations. Then, Solve@new gives one solution which is where all three meet and Solve /@ new gives the remaining solutions. Then you can group them. Is this roughly what you meant? $\endgroup$
    – bmf
    Jan 19, 2023 at 12:44
  • $\begingroup$ @bmf yes, I think so. Thinking about Syed suggestion, I think I can make every subsets of two functions, solve them and save the intersection point. Then I can check intersection points for all subsets to combine them to get groups of 3 functions or more. One problem is that they normally intersect at more than one points so make it more complicated but I think it should work. $\endgroup$
    – emnha
    Jan 19, 2023 at 12:50
  • 2
    $\begingroup$ The number of Subsets of length two or larger is $2^n-n -1$, for $n=100$ that is more than $10^{30}$ Subsets. Too many to be manageble, a different approach is needed. $\endgroup$
    – rhermans
    Jan 19, 2023 at 13:32

2 Answers 2

4
$\begingroup$

This goes through all $2^n -n -1$ subsets of length 2 or more. An overkill that allows very simple code, but it's not practical for large $n$. See other answer(s).

func = {2 x, x^2 + 1, (x^3 + 3)/2};
GroupBy[
    Subsets[func, {2,Infinity}]
    , N[x/.Solve[Equal@@#, Reals]]&
]
<|
    {1., 1.} -> {{2 x, 1 + x^2}},
    {1., -2.30278, 1.30278} -> {{2 x, 1/2 (3 + x^3)}},
    {1., -0.618034, 1.61803} -> {{1 + x^2, 1/2 (3 + x^3)}},
    {1.} -> {{2 x, 1 + x^2, 1/2 (3 + x^3)}}
|> 

This should be interpretes as

  • Functions {2 x, 1 + x^2} all intercept only at {1., 1.}
  • Functions {2 x, 1/2 (3 + x^3)} all intercept only at {1., -2.30278, 1.30278}
  • Functions {1 + x^2, 1/2 (3 + x^3)} all intercept only at {1., -0.618034, 1.61803}
  • Functions {2 x, 1 + x^2, 1/2 (3 + x^3)} all intercept only at {1.}

Or probably, depending what you mean

Union@@@GroupBy[
    Subsets[func, {2,Infinity}]
    , Union[N[x/.Solve[Equal@@#, Reals]]]&
]
<|
    {1.} -> {2 x, 1 + x^2, 1/2 (3 + x^3)},
    {-2.30278, 1., 1.30278} -> {2 x, 1/2 (3 + x^3)}, 
    {-0.618034, 1., 1.61803} -> {1 + x^2, 1/2 (3 + x^3)}
|> 
$\endgroup$
4
  • $\begingroup$ nice solution, somehow I thought that we need one equation for one variable so two or equations for one variable would make no solution. $\endgroup$
    – emnha
    Jan 19, 2023 at 12:54
  • $\begingroup$ One problem with this is that it requires too much memory for a large number of functions. I just tried with 48 functions and the computation was aborted due to insufficient memory. $\endgroup$
    – emnha
    Jan 19, 2023 at 13:01
  • 1
    $\begingroup$ Well, this is just a first approach, you didn't mentioned complexity of expressions or any other constraints before. $\endgroup$
    – rhermans
    Jan 19, 2023 at 13:03
  • $\begingroup$ Yes, you're right. The code is nice for small number of functions. I'll edit my question. $\endgroup$
    – emnha
    Jan 19, 2023 at 13:05
2
$\begingroup$

This goes only through all $(n^2-n)/2$ Subsets[func, {2}] as suggested by @Syed. For $n=100$ that is only $4950$ cases.

It calculates the intersection points of all equation pairs. Then it checks if a particular point liven in each function. Then it groups the functions based on which intersection points they share.

ClearAll[check];
check[expr_][ pnt_] := Equal[expr/.x->First[pnt], Last[pnt]]

With[
    {
        pntlist = Union@Flatten[{x, First[#]}/.Solve[Equal@@#,x,Reals]& /@Subsets[func, {2}],1]
    },
    GroupBy[Last->First]@
    Flatten[
        Table[
            {e,#}& /@ Select[pntlist, check[e]]
            , {e, func}]
        ,1
    ]
]

enter image description here

$\endgroup$
1
  • $\begingroup$ Looking forward reading more efficient solutions! $\endgroup$
    – rhermans
    Jan 19, 2023 at 16:26

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.