1
$\begingroup$

Say a function is set:

a := Sum[b[i], {i, 8}]

As can be seen that b[i] is a scalar variable.

Now I want to transform the scalar variables of b[i] to a vector variable t:={b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]}, and it can be used in the function mentioned above.

And more, t should be able to replace by specific vector, say {1,2,3,4,5,6,7,8}, which can be substituted into a, and get the desired result 36.

And that means b[1]->1 b[2]->2 b[3]->3 b[4]->4 b[5]->5 b[6]->6 b[7]->7 b[8]->8, and they can be substituted into a, so a=Sum[i,{i,8}]=36. But it is done in one step, something like this {b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]}->{1,2,3,4,5,6,7,8}. How should I accomplish such a procedure?

$\endgroup$

closed as unclear what you're asking by Jens, m_goldberg, Itai Seggev, yohbs, MarcoB Jul 26 '17 at 5:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Do you just want to create a function a[b_]:=Total@b and then evaluate a[{1,2,3,4}]? $\endgroup$ – Edmund Jul 25 '17 at 1:00
  • $\begingroup$ No, I need transform scalar variables b[i] to a vector variable {b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]}. And it can be replaced by another vector with the same length. NOT just summation.@Edmund $\endgroup$ – Robin_Lyn Jul 25 '17 at 1:13
  • $\begingroup$ Maybe you're looking for Indexed? Using it we get: a = Sum[Indexed[t, i], {i, 8}] (* Indexed[t, {1}] + Indexed[t, {2}] + Indexed[t, {3}] + Indexed[t, {4}] + Indexed[t, {5}] + Indexed[t, {6}] + Indexed[t, {7}] + Indexed[t, {8}] *), then we can do: a /. t -> Range@8 (* 36 *). $\endgroup$ – jkuczm Jul 25 '17 at 11:30
  • $\begingroup$ When defining a function, you should specify what variables it depends on. Do you know about Total? How is your function different from it? $\endgroup$ – Jens Jul 25 '17 at 15:13
1
$\begingroup$

I am not sure if I understand you correctly, but this may help. To convert a into a list (or "vector") you can use Apply, i.e.

t = List@@a
(* {b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]} *)

You can substitute values into a using

a /. Thread[t -> {1, 2, 3, 4, 5, 6, 7, 8}]
(* 36 *)

Addendum

Following up your comment. I am still unsure what you are trying to achieve. As others have suggested, Total is pretty much the thing that does what your a may be supposed to do. You could write

a[vec_] := Total[vec]

and

t = {b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8]};

Then

a[t]
(* b[1] + b[2] + b[3] + b[4] + b[5] + b[6] + b[7] + b[8] *)

and

a[{1, 2, 3, 4, 5, 6, 7, 8}]
(* 36 *)
$\endgroup$
  • $\begingroup$ It does solve the problem mentioned above, but not the way I want. Thanks anyway! $\endgroup$ – Robin_Lyn Jul 25 '17 at 11:04
  • $\begingroup$ I am afraid I do not understand what you are looking for, then. Why do you specify a := Sum... in this way, as an assignment without any variables? $\endgroup$ – JEM_Mosig Jul 25 '17 at 21:07
  • $\begingroup$ Thank you for your help. I have found a alternate way to achieve what I am looking for. $\endgroup$ – Robin_Lyn Jul 26 '17 at 1:44
0
$\begingroup$

Make a list:

c = Table[b[i], {i, 8}];

a[c_List] := blah-blah
$\endgroup$
  • $\begingroup$ Thank you for your help. The List created should be able to be assigned to a specific vector and substituted into function a $\endgroup$ – Robin_Lyn Jul 25 '17 at 0:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.