1
$\begingroup$

I got confused working with list of functions of two variables. I construct few lists of functions, one is vector of functions List1 and another is 2D list of functions List2:

FLista = MapThread[F1[#1, b] &, {{a1, a2, a3, a4, a5}}];
FListb = MapThread[F2[#1, b] &, {{a1, a2, a3, a4, a5}}];
FListc = MapThread[F3[#1, b] &, {{a1, a2, a3, a4, a5}}];

List1 = Transpose[{FLista, FListb, FListc}]

{
{F1[a1, b], F2[a1, b], F3[a1, b]},\
{F1[a2, b], F2[a2, b], F3[a2, b]},\
{F1[a3, b], F2[a3, b], F3[a3, b]},\
{F1[a4, b], F2[a4, b], F3[a4, b]},\
{F1[a5, b], F2[a5, b], F3[a5, b]}\
}

List2= FLista + FListb + FListc

{F1[a1, b] + F2[a1, b] + F3[a1, b], F1[a2, b] + F2[a2, b] + F3[a2, b],\
F1[a3, b] + F2[a3, b] + F3[a3, b], F1[a4, b] + F2[a4, b] + F3[a4, b],\
F1[a5, b] + F2[a5, b] + F3[a5, b]}

where {a1, a2, a3, a4, a5} are given numbers.

Now I would like to define the second argument for each function in List1 and List2. Let it be b={b1, b2, b3}.

And I would like to obtain the following:

(*Desired output for List1:*)
{
{F1[a1, b1], F2[a1, b1], F3[a1, b1]},\
{F1[a1, b2], F2[a1, b2], F3[a1, b2]},\
{F1[a1, b3], F2[a1, b3], F3[a1, b3]},\
{F1[a2, b1], F2[a2, b1], F3[a2, b1]},\
...
{F1[a4, b3], F2[a4, b3], F3[a4, b3]}\
{F1[a5, b1], F2[a5, b1], F3[a5, b1]}\
{F1[a5, b2], F2[a5, b2], F3[a5, b2]}\
{F1[a5, b3], F2[a5, b3], F3[a5, b3]}\
}

(*Desired output for List2:*)
{F1[a1, b1] + F2[a1, b1] + F3[a1, b1], F1[a1, b2] + F2[a1, b2] + F3[a1, b2],F1[a1, b3] + F2[a1, b3] + F3[a1, b3], ...
F1[a5, b1] + F2[a5, b1] + F3[a5, b1], F1[a5, b2] + F2[a5, b2] + F3[a5, b2], F1[a5, b3] + F2[a5, b3] + F3[a5, b3]}

I would expect, there is a solution based on Map or Thread functions, but, I have no idea how to tell Mathematica that the first argument in Fn[arg1,arg2] is already defined with {a1, a2, a3, a4, a5}, and they have ignore first index or assume it to be blind.

I still have lack of expertise in this topic, and the paradigm I am implementing such constructions might be wrong. Any suggestions are warmly invited.

UPD Whit the solution given by @lercir below, I have to emphasize that I am looking for the solution of sequential assignments of arg1 and arg2. The procedure I would like to implement is the sequential definition of arguments: 1) Define arg1s 2) work with this functions (what I omitted in the post thinking it is not necessary to be noted) 3) Define arg2s to the final lists.

$\endgroup$
1
  • 2
    $\begingroup$ Simply use List1 /. Thread[b -> {b1, b2, b3}] $\endgroup$ Commented May 11, 2024 at 7:30

1 Answer 1

1
$\begingroup$

One way might be

Outer[Construct, {F1, F2, F3}, {a1, a2, a3, a4, a5}, {b1, b2, b3}]
(* 
  {{{F1[a1, b1], F1[a1, b2], F1[a1, b3]}, 
    {F1[a2, b1], F1[a2, b2], F1[a2, b3]}, 
    {F1[a3, b1], F1[a3, b2], F1[a3, b3]}, 
    {F1[a4, b1], F1[a4, b2], F1[a4, b3]}, 
    {F1[a5, b1], F1[a5, b2], F1[a5, b3]}}, 
    <...etc...>} 
*)

If you want the literal value you provided for your desired List1, then you can rearrange the list:

List1 = Flatten[Transpose[Outer[Construct, {F1, F2, F3}, {a1, a2, a3, a4, a5}, {b1, b2, b3}], {3, 1, 2}], 1]

Then you can just do

List2 = Plus @@@ List1

You could also just use Table

Table[{F1[a, b], F2[a, b], F3[a, b]}, {a, {a1, a2, a3, a4, a5}}, {b, {b1, b2, b3}}]

and do the similar restructuring/mapping

$\endgroup$
6
  • $\begingroup$ I agree it is working option, what you described. But the issue is that I need to define the first argument well prior to define the second one. The suggestion you gave well works for the case when I do not care about the sequence of the assignment of arg1 and arg2 and DOES NOT work when I need to do it sequentially. I will make an UPD in the head post. $\endgroup$ Commented May 10, 2024 at 17:03
  • $\begingroup$ I'm sorry, I don't understand. Can you provide maybe an example of this sequential process? Right now it sounds like b is just a dummy argument in an expression like F1[a,b], but if that's the case, then my suggestion still works. But regardless, would currying work for you? $\endgroup$ Commented May 10, 2024 at 20:00
  • $\begingroup$ To make my suggestion more specific, you said that you "need to define the fist argument well prior to the second one", but that's not a problem as far as I can tell. You compute all of your as and store them in a list, then you compute all your bs and store them in a list. Then you use those lists as I demonstrated. Why wouldn't that work? $\endgroup$ Commented May 10, 2024 at 20:02
  • $\begingroup$ Thank you for your participation and patience, answering to your question, It relates to my current implementation of the nested integration, way to long to explain, although I plan to ask the community for advice to optimize it in future. Well, to my current needs, I believe, I found the solution, I will switch to the list of the expressions after defining of the arg1s, and then will turn them back to a list of function with only one argument arg2. $\endgroup$ Commented May 10, 2024 at 20:34
  • 2
    $\begingroup$ And to make the curry suggestion more specific... You could define F1[a_][b_]:=F1[a,b]. Then you could build up a list of F1[a] type expressions and then apply them to b. Or you could use CurryApplied or OperatorApplied. For example, once you have your F type expressions you could wrap them in OperatorApplied: OperatorApplied[F1]. Then once you have your as, you build up OperatorApplied[F1][a1]. That will remain unevaluated because there aren't yet enough arguments. Then you apply those to your bs: OperatorApplied[F1][a1][b1] which will finally resolve to F1[a1,b1]. $\endgroup$ Commented May 10, 2024 at 20:38

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.