3
$\begingroup$

I have a list BB={3, 5, 10} and a list FF = {1/23 (9 + 2 Sqrt[3]), 1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))}

Each element of the list FF I can return like this FF[[i]] where i = 1, 2, 3 (for instance FF[[1]] = 1/23 (9 + 2 Sqrt[3]))

But I would like to return each element of the list FF not by index running through the values i=1,2,3 but by using the list BB elements that are related to the list i in the following way: 1 associated with 3, 2 associated with 5 and 3 associated with 10

So I would like to receive the element FF[[1]] like this FF[3] (and for the remaining elements FF[[2]] like this FF[5] and FF[[3]] like this FF[10])

Could you show how this can be done?

ClearAll["Global`*"]
BB={3, 5, 10};
FF = {1/23 (9 + 2 Sqrt[3]), 1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))};
$\endgroup$

5 Answers 5

6
$\begingroup$

I think what you need can be achieved simply by using AssociationThread

BB = {3, 5, 10};
FF = AssociationThread[BB,
   {1/23 (9 + 2 Sqrt[3]), 
    1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))}];
(*<|3 -> 1/23 (9 + 2 Sqrt[3]), 5 -> 1/23 (9 - 2 Sqrt[3]), 
 10 -> -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))|>*)

FF[3]
(*1/23 (9 + 2 Sqrt[3])*)

Let me know if that works for you.

$\endgroup$
4
  • 1
    $\begingroup$ Might be worth nothing that the resulting FF even supports FF[[1]] (which will return the same as FF[3]) $\endgroup$
    – Lukas Lang
    Apr 23, 2023 at 12:10
  • $\begingroup$ @alex, thanks for the answer! $\endgroup$
    – Mam Mam
    Apr 23, 2023 at 12:25
  • $\begingroup$ @LukasLang it seems that OP understands that your suggested syntax works. They wanted to see if they can have a different notation which is more akin to a functional call. $\endgroup$
    – alex
    Apr 23, 2023 at 12:50
  • $\begingroup$ Ah sorry, you were referring to the Association! Yes, indeed. You can still use index notation with Associations. Absolutely! :) $\endgroup$
    – alex
    Apr 23, 2023 at 12:54
4
$\begingroup$

You can also do this using straightforward (though somewhat complicated) indexing, rather than using Associations. Define

FFF[n_] := FF[[Position[BB, n][[1, 1]]]];

Then FFF[3] and FFF[5] are as you expect. Position finds the index in BB that you want, and the [[1,1]] strips away a couple of extra levels of { }.

$\endgroup$
1
  • $\begingroup$ Thanks for the answer! $\endgroup$
    – Mam Mam
    Apr 23, 2023 at 14:56
3
$\begingroup$
BB = {3, 5, 10};
FF = {1/23 (9 + 2 Sqrt[3]), 
   1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))};

lut = Thread[BB -> FF];

ff[n_Integer] := Lookup[lut, n, "NA"];

Test the lookup table:

ff /@ Range[10]

{"NA", "NA", 1/23 (9 + 2 Sqrt[3]), "NA", 1/23 (9 - 2 Sqrt[3]), "NA", "NA", "NA", "NA", -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))}

$\endgroup$
1
  • $\begingroup$ Thanks for the answer! $\endgroup$
    – Mam Mam
    Apr 23, 2023 at 14:57
2
$\begingroup$

It is not possible to define FF as a list and then write FF[1]. This evaluates to: {FF-list}[1] what is nonsense. But what you can do is to use a new name and use "AssociationThread" as already proposed like e.g.:

FF = {1/23 (9 + 2 Sqrt[3]), 
   1/23 (9 - 2 Sqrt[3]), -((-3 + 7 Sqrt[3])/(-7 + Sqrt[3]))};
BB = {3, 5, 10};
ind = AssociationThread[BB -> {1, 2, 3}];
FFF[i_] := FF[[ind[i]]]

Now you can e.g. write:

FFF[5]

1/23 (9 - 2 Sqrt[3])
$\endgroup$
2
  • $\begingroup$ Thanks for the answer! $\endgroup$
    – Mam Mam
    Apr 23, 2023 at 12:25
  • $\begingroup$ Hi @Daniel, I don't think it is nonsense. I'm sure that if OP knew about associations then they would't have used a list! :) $\endgroup$
    – alex
    Apr 23, 2023 at 12:53
2
$\begingroup$

If you just want to extract elements of FF using the values of BB (rather than their indices):

Extract[FF,PositionIndex[BB][3]]

$$ \frac{1}{23} \left(9+2 \sqrt{3}\right) $$

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