1
$\begingroup$

everyone! Can you please help me with the concise code?

Given a list of rules. setA =

{Subscript[Abx, 1, 1] -> 293.305, Subscript[Aby, 1, 1] -> -37.9087, 
 Subscript[Abz, 1, 1] -> -21.7755, Subscript[Abx, 1, 2] -> 306.777, 
 Subscript[Aby, 1, 2] -> -39.0271, Subscript[Abz, 1, 2] -> -17.8631, 
 Subscript[Abx, 1, 3] -> 305.999, Subscript[Aby, 1, 3] -> -20.5591, 
 Subscript[Abz, 1, 3] -> -13.3516, Subscript[Abx, 1, 4] -> 296.88, 
 Subscript[Aby, 1, 4] -> 7.90668, Subscript[Abz, 1, 4] -> -7.29044, 
 Subscript[Abx, 1, 5] -> 285.328, Subscript[Aby, 1, 5] -> 36.7819, 
 Subscript[Abz, 1, 5] -> 1.27108, Subscript[Abx, 1, 6] -> 277.254, 
 Subscript[Aby, 1, 6] -> 56.4782, Subscript[Abz, 1, 6] -> 13.2836, 
 Subscript[Abx, 2, 1] -> 329.509, Subscript[Aby, 2, 1] -> -45.9608, 
 Subscript[Abz, 2, 1] -> -7.54032, Subscript[Abx, 2, 2] -> 327.696, 
 Subscript[Aby, 2, 2] -> -34.1085, Subscript[Abz, 2, 2] -> -8.53138, 
 Subscript[Abx, 2, 3] -> 323.534, Subscript[Aby, 2, 3] -> -12.6589, 
 Subscript[Abz, 2, 3] -> -10.9313, Subscript[Abx, 2, 4] -> 317.955, 
 Subscript[Aby, 2, 4] -> 11.9279, Subscript[Abz, 2, 4] -> -11.7819, 
 Subscript[Abx, 2, 5] -> 311.89, Subscript[Aby, 2, 5] -> 33.1912, 
 Subscript[Abz, 2, 5] -> -8.12503, Subscript[Abx, 2, 6] -> 306.272, 
 Subscript[Aby, 2, 6] -> 44.6707, Subscript[Abz, 2, 6] -> 2.99749, 
 Subscript[Abx, 3, 1] -> 350.655, Subscript[Aby, 3, 1] -> -49.2378, 
 Subscript[Abz, 3, 1] -> -4.30901, Subscript[Abx, 3, 2] -> 344.377, 
 Subscript[Aby, 3, 2] -> -31.2548, Subscript[Abz, 3, 2] -> 0.0258388, 
 Subscript[Abx, 3, 3] -> 341.522, Subscript[Aby, 3, 3] -> -7.49589, 
 Subscript[Abz, 3, 3] -> -1.40915, Subscript[Abx, 3, 4] -> 339.678, 
 Subscript[Aby, 3, 4] -> 15.8155, Subscript[Abz, 3, 4] -> -3.88774, 
 Subscript[Abx, 3, 5] -> 336.432, Subscript[Aby, 3, 5] -> 32.4558, 
 Subscript[Abz, 3, 5] -> -2.68367, Subscript[Abx, 3, 6] -> 329.373, 
 Subscript[Aby, 3, 6] -> 36.2016, Subscript[Abz, 3, 6] -> 6.92931, 
 Subscript[Abx, 4, 1] -> 366.162, Subscript[Aby, 4, 1] -> -49.6521, 
 Subscript[Abz, 4, 1] -> -6.33289, Subscript[Abx, 4, 2] -> 361.216, 
 Subscript[Aby, 4, 2] -> -30.8016, Subscript[Abz, 4, 2] -> 5.72576, 
 Subscript[Abx, 4, 3] -> 360.739, Subscript[Aby, 4, 3] -> -5.8953, 
 Subscript[Abz, 4, 3] -> 7.90838, Subscript[Abx, 4, 4] -> 361.109, 
 Subscript[Aby, 4, 4] -> 17.9386, Subscript[Abz, 4, 4] -> 6.35521, 
 Subscript[Abx, 4, 5] -> 358.701, Subscript[Aby, 4, 5] -> 33.5719, 
 Subscript[Abz, 4, 5] -> 7.20654, Subscript[Abx, 4, 6] -> 349.895, 
 Subscript[Aby, 4, 6] -> 33.8762, Subscript[Abz, 4, 6] -> 16.6026, 
 Subscript[Abx, 5, 1] -> 385.45, Subscript[Aby, 5, 1] -> -49.1158, 
 Subscript[Abz, 5, 1] -> -7.8633, Subscript[Abx, 5, 2] -> 382.61, 
 Subscript[Aby, 5, 2] -> -33.0846, Subscript[Abz, 5, 2] -> 6.4856, 
 Subscript[Abx, 5, 3] -> 381.964, Subscript[Aby, 5, 3] -> -8.68214, 
 Subscript[Abz, 5, 3] -> 9.71485, Subscript[Abx, 5, 4] -> 381.309, 
 Subscript[Aby, 5, 4] -> 16.6662, Subscript[Abz, 5, 4] -> 8.9101, 
 Subscript[Abx, 5, 5] -> 378.447, Subscript[Aby, 5, 5] -> 35.5353, 
 Subscript[Abz, 5, 5] -> 11.157, Subscript[Abx, 5, 6] -> 371.175, 
 Subscript[Aby, 5, 6] -> 40.4998, Subscript[Abz, 5, 6] -> 23.541, 
 Subscript[Abx, 6, 1] -> 417.939, Subscript[Aby, 6, 1] -> -49.5409, 
 Subscript[Abz, 6, 1] -> -3.15155, Subscript[Abx, 6, 2] -> 412.956, 
 Subscript[Aby, 6, 2] -> -38.4393, Subscript[Abz, 6, 2] -> 0.222538, 
 Subscript[Abx, 6, 3] -> 405.973, Subscript[Aby, 6, 3] -> -16.6815, 
 Subscript[Abz, 6, 3] -> -3.29615, Subscript[Abx, 6, 4] -> 399.342, 
 Subscript[Aby, 6, 4] -> 10.3674, Subscript[Abz, 6, 4] -> -6.25992, 
 Subscript[Abx, 6, 5] -> 395.416, Subscript[Aby, 6, 5] -> 37.3421, 
 Subscript[Abz, 6, 5] -> -1.22105, Subscript[Abx, 6, 6] -> 396.55, 
 Subscript[Aby, 6, 6] -> 58.8777, Subscript[Abz, 6, 6] -> 19.2682}

. I need to create four functions, which should make such changes as: 1) swap the first and second index of each rule, for example, Subscript[Abx, 3, 5] should become Subscript[Abx, 5, 3]. 2) The second function should change the first index to the opposite from the end. All indexes vary from 1 to 6, so, the function has to change each item like that Subscript[Abx, 2, 3] to Subscript[Abx, 5, 3] and so on. 3) The third function should make the same changes with the second index, so Subscript[Abx, 2, 3] changes to Subscript[Abx, 2, 4]. 4) The fourth function changes simultaneously both indexes to the opposite ones, so Subscript[1, 3] becomes Subscript[6, 4].

I know ho to do this manipulating with the list like

Table[Subscript[setA[[i]][[1]][[1]], setA[[i]][[1]][[2]], 
      setA[[i]][[1]][[3]]], {i, 1, Length@setA}].

But I think it is ugly and inefficient.. Can you please help me with an elegant and efficient implementation of these functions in the style of Mathematica?

$\endgroup$

1 Answer 1

1
$\begingroup$

Define the 4 functions like:

fun1[dat_] := dat /. Subscript[x0_, x1_, x2_] :> Subscript[x0, x2, x1];
fun2[dat_] := dat /. Subscript[x0_, x1_, x2_] :> Subscript[x0, 7 - x1, x2];
fun3[dat_] := dat /. Subscript[x0_, x1_, x2_] :> Subscript[x0, x1, 7 - x2];
fun4[dat_] := dat /. Subscript[x0_, x1_, x2_] :> Subscript[x0, 7 - x1, 7 - x2];

Then test like:

fun1[Subscript[Abx, 3, 5]]
(* Subscript[Abx, 5, 3] *)

fun2[Subscript[Abx, 2, 3]]
(* Subscript[Abx, 5, 3] *)

fun3[Subscript[Abx, 2, 3]]
(* Subscript[Abx, 2, 4] *)

fun4[Subscript[Abx, 1, 3]]
(* Subscript[Abx, 6, 4] *)
$\endgroup$
5
  • $\begingroup$ Thanks a lot! And the last question a little bit off topic. In the given list how to refer to the value of a rule with a known left-hand side? I mean, how to find for example the value of Subscript[Abz, 2, 5] that is -8.12503? $\endgroup$ Nov 14, 2022 at 12:31
  • $\begingroup$ P.S. And the really last question. In the result of fun2[setA] how to sort the list like it was originally? $\endgroup$ Nov 14, 2022 at 12:34
  • $\begingroup$ Assuming lrules is the list of rules, you get the value of Subscript[Abz, 2, 5] by: Subscript[Abz, 2, 5] /. lrules $\endgroup$ Nov 14, 2022 at 12:46
  • $\begingroup$ The order of the list is not changed by the functions. $\endgroup$ Nov 14, 2022 at 12:47
  • $\begingroup$ Thank you! )))) $\endgroup$ Nov 14, 2022 at 12:49

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.