# Symbolically associate vectors and their norms

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?

-
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. –  janitor048 Aug 25 '12 at 4:06
I would hope that the replacement desired is avec . avec by A^2 rather than by A! –  murray Aug 26 '12 at 20:17
Eh, yes, of course. My bad. I've edited the question to correct this typo. –  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) /.


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

-
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. –  janitor048 Aug 25 '12 at 4:31
Can you give a somewhat lengthier example, then? –  Ｊ. Ｍ. Aug 25 '12 at 5:48
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 –  janitor048 Aug 29 '12 at 16:04
In that case, something more involved would indeed be required... –  Ｊ. Ｍ. Aug 29 '12 at 16:26

Perhaps something like:

ClearAll[symblNm];
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] *)


or

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

-