# Symbolic vector addition

I'm trying to figure out a nice way to add a mix of symbolic and explicit vectors/matrices without mathematica treating the symbolic vectors as scalars and promoting them to constant arrays.

For example:

In: Assuming[x \[Element] Matrices[{2, 2}], x + IdentityMatrix[2]]

Out: {{1 + x, x}, {x, 1 + x}}

Of course I could do:

In: HoldForm[x + IdentityMatrix[2]]

and not release the hold until I replace everything with its explicit value, but I am hoping that there is a cleaner solution.

• What would be an acceptable output for your first line of code? May 31, 2016 at 13:56
• x + {{1,0},{0,1}} would be nice. Doing so in a way the was idiomatic/self-documenting would be perfect. Ideally, I don't want people reading my code to have to guess why I used a HoldForm (even without a comment :)) May 31, 2016 at 13:57
• Would Inactivate[Plus][x, IdentityMatrix[2] do what you want? You can always Activate the "frozen" operators down the road when you have values for your variables. May 31, 2016 at 14:00
• that's pretty nice, but I'm sure there must be an even more idiomatic way. May 31, 2016 at 14:48

## 1 Answer

Here are two possible approaches:

You could inactivate Plus as follows:

expr = Inactive[Plus][x, IdentityMatrix[2]]


x = {{1, 1}, {0, 0}};
expr


Activate@expr


As you can see, the inactive form will format very similarly to a "normal" plus sign, with a slight color change to indicate its inactive status.

Alternatively, you could use an operator without built-in meaning to represent your addition, only replacing it with Plus when you are ready to do so.

For instance, one could use CirclePlus, which is also formatted to a nice infix graphical representation reminiscent of a plus sign, for readability:

expr = x \[CirclePlus] IdentityMatrix[2]


formats to

and similarly CirclePlus[x, IdentityMatrix[2]] formats to the same result.

You can then assign values to x whenever you are ready in the course of your calculations:

x = {{1, 1}, {0, 0}};
expr


When you are ready to "reactivate" your operator, replace it with Plus:

expr /. CirclePlus -> Plus

(* Out: {{2, 1}, {0, 1}} *)