I am wondering how to handle the following situation: I do have vectors of known dimension that I would like to handle symbolically. I suppose I can do something like

avec = Array[a,2];
bvec = Array[b,2];

Now in my equations there are also scalars that represent the norms of these vectors, i.e. A == Norm[avec] So suppose I write an expression like

expr = A avec.(avec + bvec)

What I would like Mathematica to do is:

  1. Whenever avec.avec is encountered, replace this by A^2
  2. Whenever avec.bvec is encountered, leave this symbolic, don't expand into the elements of the vectors.

How can I achieve this?

  • $\begingroup$ The coolest thing would actually be if I could use the same names for the different meanings, such as one would do in handwritten calculations, i.e. a for the norm, OverVector[a] for the vector, a[[1]] for one of its components or something like this. But this is not really necessary. $\endgroup$ – janitor048 Aug 25 '12 at 4:06
  • $\begingroup$ I would hope that the replacement desired is avec . avec by A^2 rather than by A! $\endgroup$ – murray Aug 26 '12 at 20:17
  • $\begingroup$ Eh, yes, of course. My bad. I've edited the question to correct this typo. $\endgroup$ – janitor048 Aug 27 '12 at 13:53

One possibility is to replace Plus[] and Dot[] with generic operators like CirclePlus[] () and CircleTimes[] (), which enables you to do things like

(Distribute[avec⊗(avec⊕bvec), CirclePlus] /. avec⊗avec :> A) /.
 Thread[{avec, bvec} -> {HoldForm[avec], HoldForm[bvec]}]

which yields A⊕avec⊗bvec. The HoldForm[] prevents avec and bvec from being turned into their corresponding values.

  • $\begingroup$ Ok, this seems in principle to do the job, however, it is somewhat unhandy. The expressions I am after are lengthy and I would like Mathematica to expand, distribute, collect, simplify, etc. them automatically - so explicitly giving instructions on which terms to expand etc. seems not to be the ideal way to go. $\endgroup$ – janitor048 Aug 25 '12 at 4:31
  • 2
    $\begingroup$ Can you give a somewhat lengthier example, then? $\endgroup$ – J. M. will be back soon Aug 25 '12 at 5:48
  • $\begingroup$ A problem already occurs with my simple example: when try to process Distribute[A^2 avec⊗(avec⊕bvec),CirclePlus] it fails to expand the expression due to the mixture of and normal scalar multiplication. The expressions I would like to process are of the form (f[A,B] avec + g[A,B] bvec)^2 where f and g are in principle simple but rather lengthy algebraic functions of the norms of the vectors. Actually my expression contains more than two terms. I would like use Simplify, Series, etc. on the expressions.. I somehow feel that Mathematica is lacking some functionally in this ascpect $\endgroup$ – janitor048 Aug 29 '12 at 16:04
  • $\begingroup$ In that case, something more involved would indeed be required... $\endgroup$ – J. M. will be back soon Aug 29 '12 at 16:26

Perhaps something like:

SetAttributes[symblNm, HoldFirst];
symblNm[x_] := SymbolName[Unevaluated[x]]

Distribute[symblNm[avec].(symblNm[avec] + symblNm[bvec])]/. {Dot[x_, x_]:> Norm[x]}
(* "avec"."bvec" + Norm["avec"] *)

To evaluate:

Map[Symbol, %, {-1}]
(* Sqrt[Abs[a[1]]^2 + Abs[a[2]]^2] + a[1] b[1] + a[2] b[2] *)


Map[ToExpression, %%, {-1}]
(*  Sqrt[Abs[a[1]]^2 + Abs[a[2]]^2] + a[1] b[1] + a[2] b[2] *)

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.