Suppose I have expressions that involve terms like:


p1, p2, p3, and p4 are all elements of an array:

p={p1, p2, p3, p4}

I would like Mathematica to replace this term with simply "p^2" anytime it sees it. How do I tell it to do this?

  • 1
    $\begingroup$ So p1 ... p4 are all scalars? Why do you have an expression involving them in a dot product then? That seems an issue in and of itself... Have you tried {p1, p2, p3, p4}.{p1, p2, p3, p4} instead? That is equivalent to p.p with your definition of p, and it returns p1^2 + p2^2 + p3^2 + p4^2, which seems to be what you want. $\endgroup$ – MarcoB Mar 8 '16 at 22:48
  • $\begingroup$ Define p as a vector: then the symbol p represents the vector {p[[1]], p[[2]], p[[3]], p[[4]]}. Or define p as a function with designated values p[1], p[2], p[3] and p[4]. $\endgroup$ – bill s Mar 9 '16 at 2:41

Your Answer

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

Browse other questions tagged or ask your own question.