Conditionals applied to choose one function rather than the other?

Hi, everyone!
I am dealing with a problem that requires the use of conditionals applied to choose one function rather than the other, depending on the value assumed by a third function. For example, there are two functions F1[x,y] and F2[x,y], and also the third function G[y]. If G[y] is higher than a constant value (for instance 2000), F1[x,y] must be chosen; otherwise, F2[x,y] is used for computation. I wrote a code to do that, but unfortunately it didn’t work out.

G[y_]:=10*y

F1[x_,y_]:=(x-x^2)*(1-2*x)+0.1*y^2*(1-2*x)/(x-x^2)^2 if G[y]>=2000

F2[x_,y_]:=(x-x^2)*(1-2*x)+5*y*(1-2*x)/(x-x^2)^2 if G[y]<2000

The aim is to choose F1[x,y] for computation if G[y]>=2000 or F2[x,y] if G[y]<2000.
In fact, a generic function F[x,y] will become F1[x,y] or F2[x,y] depending on G[y].
Thus, F[x,y] is used for computation.

How can F[x,y] be written then?

I defined the function F[x,y] using the command Piecewise as a typical mathematical expression.

F[x,y] = F1[x,y] for G[y]>=2000 and F2[x,y]=F2[x,y] for G[y]<2000

Is this really the most appropriate command to do that?
How is another way of doing this in Mathematica?

Thank you in advance for your response.

F1[x_,y_]:=x*y
F2[x_,y_]:=x/y
G[x_,y_]:=x-y
F[x_,y_]:=Which[G[x,y]>20,F1[x,y],G[x,y]<20,F2[x,y]]

Then

F[40,10]

returns 400 and

F[40,30]

returns

4/3

Hi, Bill Nelson!
I have just edited the problem making it more interesting. Sorry for that!
I will see if your idea works out.
Thank you.

You could use Condition:

G[y_] := 10*y;
F[x_, y_] := x^2 + y /; G[y] >= 2000
F[x_, y_] := -(x + y) /; G[y] < 2000

(I simplified the functions for clarity.)

F[1, 1]
(* -2 *)

F[1, 2000]
(* 2001 *)

Hi, Eric!
I am having trouble with the command Transpose.
There are two lists of different levels, e.g.

L1={a1,a2,a3}

and

L2={{b1,b2,b3},{c1,c2,c3}}

I want to obtain a list like that:

L3={{a1,b1},{a1,c1},{a2,b2},{a2,c2},{a3,b3},{a3,c3}}

What should I do?
Please help me out. Thank you in advance for your help.

Transpose just restructures things, it won’t actually change the contents of the things. In your case, you need to duplicate some of the elements (e.g. you want a1 to appear in both {a1,b1} and {a1,c1}).

But we can work up to what you want (there are probably several other ways to do this):

Transpose[L2]
(* {{b1, c1}, {b2, c2}, {b3, c3}} *)

That kinda looks like it’s going in the right direction. We just want to somehow “thread” the as through these.

Thread[{L1, Transpose[L2]}]
(* {{a1, {b1, c1}}, {a2, {b2, c2}}, {a3, {b3, c3}}} *)

Still headed in the right direction. What if we thread each of the three sublists?

Thread /@ Thread[{L1, Transpose[L2]}]
(* {{{a1, b1}, {a1, c1}}, {{a2, b2}, {a2, c2}}, {{a3, b3}, {a3, c3}}} *)

That’s very close. We just need to remove a layer of List:

Flatten[Thread /@ Thread[{L1, Transpose[L2]}], 1]
(* {{a1, b1}, {a1, c1}, {a2, b2}, {a2, c2}, {a3, b3}, {a3, c3}} *)

Bingo!

All this sequence of commands reveals that I must study more.
By the way, you are extraordinary! Thank you once again.

Eric, to be honest, I haven’t understood clearly the following step:

Thread /@ Thread[{L1, Transpose[L2]}]

The shorthand /@ refers to the command Map. How would you write this step without the shorthand?
Thank you for your patience with me.

Map[Thread, Thread[{L1, Transpose[L2]}]]

Hi, Eric Rimbey!
You are helping me a lot. Thank you. Can you help me again, please?

What if the result is like that:

{{{a1, {b1}}, {a1, {c1}}, {a2, {b2}}, {a2, {c2}}, {a3, {b3}}, {a3, {c3}}}

This is happening because L2 is like that:

L2={{{b1},{b2},{b3}},{{c1},{c2},{c3}}}

I realize that the command Flatten does not do what I want, which is that:

L2={{b1,b2,b3},{c1,c2,c3}} 

and also finally that

{{a1, b1}, {a1, c1}, {a2, b2}, {a2, c2}, {a3, b3}, {a3, c3}}

How would you deal with that?

Thank you in advance for your response and for your kind attention.

Hi,Eric!
I managed to solve the problem.

L1={{a1,{b1}},{a1,{c1}},{a2,{b2}},{a2,{c2}}}

Using this command:

L1/.{a_,{b_}}:>{a,b}

Do you have another way to solve this?

In addition, I bumped into another problem, because I have two sets of points for each a, since c1>b1. How can I extract only the pairs {ai,c1} referring to the points with the maximum values of the cs.

I’m thinking how to do that. Can you help me, please?
Thank you.

L1 = {a1, a2, a3};
L2 = {{{b1}, {b2}, {b3}}, {{c1}, {c2}, {c3}}};
Flatten[Map[Thread, Thread[{L1, Transpose[Flatten /@ L2]}]], 1]

This is getting a bit convoluted. There are probably better ways to manage your data structures elsewhere in your workflow.

Can you please start with a new question? I’m not understanding your latest question, and I think you’re probably not providing all the context.

Hi, Eric!
I have the following list:

L1={{a1,b1},{a1,c1},{a2,b2},{a2,c2}}

I want to take the pair {a1,c1} rather than {a1,b1}, because c1>b1.

In other words, the result should be two lists L2 and L3 like:

L2={{a1,b1},{a2,b2}}

and

L3={{a1,c1},(a2,c2}}

I believe I should define a function for that, shouldn’t I?

Thank you in advance for your response.

This comment

I want to take the pair {a1,c1} rather than {a1,b1}, because c1>b1.

seems to contradict this comment

In other words, the result should be two lists L2 and L3 like:

You can’t take one thing and end up with two things. Are you actually wanting to sort? Or maybe just group things a certain way?

I think maybe we’ve gotten too far away from your actual question. I suspect that you’ve tried to simplify your question, which is generally a good thing to do, but I fear we may be so far away from your actual need that you’re doing a lot of unnecessary work.

To me, this is starting to sound like maybe you need some stronger structures to support some sort of analysis. Like, maybe you need associations. Starting with some dummy data and your data as pairs…

b1 = 5;
b2 = 10;
c1 = 7;
c2 = 14;
L1 = {{a1, c1}, {a1, b1}, {a2, b2}, {a2, c2}} (* I re-ordered so we can see how things change when we sort later *)

maybe we should add Association structure to it…

data1 = Merge[Rule @@@ L1, Identity]
(* <|a1 -> {7, 5}, a2 -> {10, 14}|> *)

Now you can do some queries or whatever. Since you’re doing comparisons, you can sort this:

Sort /@ data1
(* <|a1 -> {5, 7}, a2 -> {10, 14}|> *)

Or if you knew you were going to need the data sorted, you could have done this when you created the association:

data2 = Merge[Rule @@@ L1, Sort]
(* <|a1 -> {5, 7}, a2 -> {10, 14}|> *)

Anyway, now we can split this data based on Min or Max:

Through[{Map[Min], Map[Max]}[data2]]
(* {<|a1 -> 5, a2 -> 10|>, <|a1 -> 7, a2 -> 14|>} *)

Anyway, this is all speculation. I don’t really know how to answer your question, because it seems like it’s missing important context.

Hi, Eric Rimbey!
You are so knowledgeable that every post of yours is full of insights and impresses me.
Well. I must apologize for my lack of clarity and for not defining the context.
English is not my mother language and in fact I am not good at programming, but I am always thinking to improve my knowledge and this discussion forum has been an oasis.
Recently, I am dealing with a Fluid Mechanics problem whose solution is a big set of data from a complex algebraic equation. Despite that, you must find easy to answer my question.To solve the problem, I wrote a code in Mathematica using SolveValues. The output is a set of data points like that:

{{0.95,0.001},{0.38,0.001},... and many other points of the type {number,0.001}
{{1.2,0.006},{0.52,0.006},... and many other points of the type {number,0.006}
{{0.76,0.011},{0.29,0.011},... and many other points of the type {number,0.011}

and so on. Note that each element of the list is a pair {x,y} where y is varying from 0.001 up to 1 in increments of 0.005. Therefore, I want to sort the elements in which x has the highest value for each value of y.
Does it require association? Well, I haven’t done the programming course about associations yet.
As you notice, I am a newcomer. Anyway, I am thinking how to separate this list into two lists using Mathematica. If you can help me do that, I will be grateful to you once again. Maybe the command Max could be useful for that.

Also, I want to take this opportunity to ask you another question about the differences between Plot, ListLinePlot and ListPlot. Plot is used for a continuous domain of variables, while ListPlot is for discrete points. Usually, the solution of algebraic equations is sets of discrete points that must be sorted. For this reason, I cannot use Plot, but ListPlot for plotting. However, my plots are weird with only points. It is common to see solid continuous boundary lines in the literature. Then, I thought of ListLinePlot. However, the command ListLinePlot creates lines between points in such a way that the plot of points turns into a hatched region, messing up the plot. Honestly, I am really struggling with the use of these commands for what I want to produce. Sorry for being far away from the main question of this forum. Anyway, my questions might serve to someone.
Overall, I am definitely learning a lot by using Mathematica and also from you. Thank you, really.

Here’s some dummy data to make it easier to demonstrate (this would be just one set of results from SolveValues, i.e. the results where y=5):

data = {{72, 5}, {59, 5}, {38, 5}, {51, 5}, {55, 5}, {16, 5}, {93, 5}, {96, 5}, {67, 5}, {8, 5}}

To find the pair whose x value is largest:

MaximalBy[data, First]
(* {{96, 5}} *)

Notice that it’s a list of pairs (just one pair in this case). That’s because there may have been several pairs whose first element was the max.

I want to sort the elements in which x has the highest value for each value of y

Okay, if you want them sorted:

SortBy[data, First]
(* {{8, 5}, {16, 5}, {38, 5}, {51, 5}, {55, 5}, {59, 5}, {67, 5}, {72, 5}, {93, 5}, {96, 5}} *)

ReverseSortBy[data, First]
(* {{96, 5}, {93, 5}, {72, 5}, {67, 5}, {59, 5}, {55, 5}, {51, 5}, {38, 5}, {16, 5}, {8, 5}} *)

how to separate this list into two lists

Again, I don’t understand this. What are the two lists you want?

For the plotting stuff, you need to provide concrete examples. I can’t tell what the problem is just by your description.

Hi, Eric!
I am sending you my notebook (attached) that contains two questions.
To be honest, I have attempted many times to get what I want and your help can illuminate my work.
Thank you in advance for your help.

Example.nb (160 KB)