Say I have a vector

X[n_] := Table[Subscript[x, i], {i, 1, n}]

and a matrix of connections:

W[n_, w_, \[Omega]_] := IdentityMatrix[n]*w + (1 - IdentityMatrix[n])*\[Omega]

And I define the following expressions

v = X[10]
connections = W[10, w, \[Omega]]
s = Total[v]

And now I want to calculate the sum of the matrix product Wv:

Collect[Total[connections.v], {\[Omega], w}, FullSimplify]

This will output

w (x_1+x_2+x_3+x_4+x_5+x_6+x_7+x_8+x_9+x_10)+9 \[Omega] (x_1+x_2+x_3+x_4+x_5+x_6+x_7+x_8+x_9+x_10)

However, I know that s=x_1+x_2+...+x_10... So how do I make Mathematica substitute the sum by s? Yielding

w s + 9 \[Omega] s

Try this

X[n_] := Table[Subscript[x, i], {i, 1, n}]
W[n_, w_, ω_] := IdentityMatrix[n]*w + (1 - IdentityMatrix[n])*ω
v = X[10];
connections = W[10, w, ω];
(*Carefully do NOT assign any value to s*)
Collect[Total[connections.v], {ω, w}, FullSimplify]//.Total[v]->s

which returns

s w+9 s ω

If you use a previously defined name then the whole model of Mathematica is to expand all definitions until it cannot expand them any more. There are always other ways of doing things and you can try to fight Mathematica and try to make it do things your way instead of its way, but you might want to consider whether it is worth the effort to do that. Up to you to decide.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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