3
$\begingroup$

I have 3 tables ( which represents values of functions ) of the form:

 tb1={r, f1(r)}
 tb2={r, f2(r)}
 tb3={r, f3(r)}

I need to obtain a table of the form:

    tb4={r1,f1(r1)+f2(r1)+f3(r1)}
        {r2,f1(r2)+f2(r2)+f3(r2)}
               .
               .
               .

I think it should be pretty simple but I can not figure it out. Thanks in advance.

As an example, for r=0.1 , I have :

  tb1={0.1,78}
  tb2={0.1,-43}
  tb3={0.1,5}

for r=0.2:

  tb1={0.2,68}
  tb2={0.2,-41}
  tb3={0.2,4}

and so on in r=0.2,0.3... so I need to get

  tb4={0.1,78-43+5}
      {0.2,68-41+4}
$\endgroup$

4 Answers 4

2
$\begingroup$

Here is an example of your tables, since you didn't provide one:

tb1 = Table[{r,f1[r]}, {r, {r1,r2,r3}}]
tb2 = Table[{r,f2[r]}, {r, {r1,r2,r3}}]
tb3 = Table[{r,f3[r]}, {r, {r1,r2,r3}}]

{{r1, f1[r1]}, {r2, f1[r2]}, {r3, f1[r3]}}

{{r1, f2[r1]}, {r2, f2[r2]}, {r3, f2[r3]}}

{{r1, f3[r1]}, {r2, f3[r2]}, {r3, f3[r3]}}

You can use GroupBy to get your desired output:

summary = GroupBy[Join[tb1, tb2, tb3], First->Last, Total]

<|r1 -> f1[r1] + f2[r1] + f3[r1], r2 -> f1[r2] + f2[r2] + f3[r2], r3 -> f1[r3] + f2[r3] + f3[r3]|>

If you don't like associations:

List @@@ Normal @ summary

{{r1, f1[r1] + f2[r1] + f3[r1]}, {r2, f1[r2] + f2[r2] + f3[r2]}, {r3, f1[r3] + f2[r3] + f3[r3]}}

$\endgroup$
2
$\begingroup$

There are any number of ways you could go here depending on what your exact requirements are and where exactly you're starting from. If you're not absolutely constrained to start with your tb1,..., tb3 lists, you can define

tbthrough[r_] := {r, Through[(f1 + f2 + f3)[r]]}

or

tbsum[r_] := {r, Sum[f[r], {f, {f1, f2, f3}}]}

Then both of these

tbthrough /@ {r1, r2, r3, r4}
tbsum /@ {r1, r2, r3, r4}

produce

(* {{r1, f1[r1] + f2[r1] + f3[r1]}, 
    {r2, f1[r2] + f2[r2] + f3[r2]}, 
    {r3, f1[r3] + f2[r3] + f3[r3]}, 
    {r4, f1[r4] + f2[r4] + f3[r4]}} *)

Or, if you can put your lists tb1,..., tb3 into one meta-list, tb,

tblist[r_] := {r, #[r]} & /@ {f1, f2, f3}

tb = tblist /@ {r1, r2, r3, r4}

(* {{{r1, f1[r1]}, {r1, f2[r1]}, {r1, f3[r1]}}, 
    {{r2, f1[r2]}, {r2, f2[r2]}, {r2, f3[r2]}}, 
    {{r3, f1[r3]}, {r3, f2[r3]}, {r3, f3[r3]}}, 
    {{r4, f1[r4]}, {r4, f2[r4]}, {r4, f3[r4]}}} *)

Then

{#[[1, 1, 1]], Total @@ #[[;; , ;; , 2]]} & /@ GatherBy[tb, First]

Edit: You could also define tb = Join[tb1, tb2, tb3] (so, losing a level compared to tb created above), and then use the modified

{#[[1, 1]], Total@#[[;; , 2]]} & /@ GatherBy[tb, First]

Both of these yield

(* {{r1, f1[r1] + f2[r1] + f3[r1]}, 
    {r2, f1[r2] + f2[r2] + f3[r2]},
    {r3, f1[r3] + f2[r3] + f3[r3]}, 
    {r4, f1[r4] + f2[r4] + f3[r4]}} *)
$\endgroup$
5
  • $\begingroup$ Thank you very much for your help. One more thing, how can I make the sum if what I have are numbers? $\endgroup$ Aug 24, 2017 at 4:11
  • $\begingroup$ If your fs are well-defined functions and f[r] is a number then Total, Sum, Through and whatever will all work as you'd expect. So you shouldn't have to change a thing to get them working. $\endgroup$ Aug 24, 2017 at 4:15
  • $\begingroup$ Sorry but it is not working. I have 3 lists of numbers, no more. I do not have the functions, just the results of evaluations of three functions in 200 points . I want to sum the result of those evaluation , I mean, sum the second column of the three tables in each point, for example: for r=0.1 , I get the respective value in each table. I want a new table that is the sum of those three values for r=0.1 and so on with r=0.2,0.3,0.4...., I'm sorry if I have not been clear , just thought that the solution to my problem was general :D $\endgroup$ Aug 24, 2017 at 4:37
  • $\begingroup$ Ah, okay. If you don't have the functions then {#[[1, 1]], Total@#[[;; , 2]]} & /@ GatherBy[Join[tb1, tb2, tb3], First]` should work. (Slightly modified from my answer.) $\endgroup$ Aug 24, 2017 at 4:42
  • $\begingroup$ Thank you very much; you help me a lot ! Now I will go to understand the code for future calculations :D . Thank you again :D $\endgroup$ Aug 24, 2017 at 5:03
1
$\begingroup$

Answer (not optimized) only based on the question statement:

list = {r1, r2, r3};
tb = Table[{list[[i]], ToExpression["f" <> ToString[i] <> "[r]"]}, {i,
1, Length[list]}]

prints:

{{r1, f1[r]}, {r2, f2[r]}, {r3, f3[r]}}

Then,

tbfinal = Table[{list[[i]],Sum[tb[[j]][[2]] /. r -> list[[i]], {j, 1,Length[list]}]}, {i, 1, Length[list]}]

prints:

{{r1, f1[r1] + f2[r1] + f3[r1]}, {r2, f1[r2] + f2[r2] + f3[r2]}, {r3, f1[r3] + f2[r3] + f3[r3]}}
$\endgroup$
0
$\begingroup$

Merge

You can also use Merge[Total] on the list of tables converted to Associations.

Using @Carl's setup

tb1 = Table[{r, f1[r]}, {r, {r1, r2, r3}}];
tb2 = Table[{r, f2[r]}, {r, {r1, r2, r3}}];
tb3 = Table[{r, f3[r]}, {r, {r1, r2, r3}}];

Convert the list of tables into a list of Associations:

assoc = Association/@(Rule@@@#&/@{tb1,tb2,tb3});

Merge[Total] the associations and convert back to List:

List @@@ Normal @ Merge[Total][assoc]

{{r1, f1[r1] + f2[r1] + f3[r1]}, {r2, f1[r2] + f2[r2] + f3[r2]}, {r3, f1[r3] + f2[r3] + f3[r3]}}

Alternatively, define mergeTotal to compose all the steps in to a single one:

mergeTotal = Composition[List @@@ # &, Normal , Merge[Total]];
mergeTotal[assoc]

{{r1, f1[r1] + f2[r1] + f3[r1]}, {r2, f1[r2] + f2[r2] + f3[r2]}, {r3, f1[r3] + f2[r3] + f3[r3]}}

Total + Through + Map

funcs = {f1, f2, f3};
vars = {r1, r2, r3};
{#, Total @ Through @ funcs @ #} & /@ vars

{{r1, f1[r1] + f2[r1] + f3[r1]}, {r2, f1[r2] + f2[r2] + f3[r2]}, {r3, f1[r3] + f2[r3] + f3[r3]}}

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