5
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ What would be an acceptable output for your first line of code? $\endgroup$
    – MarcoB
    May 31, 2016 at 13:56
  • $\begingroup$ 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 :)) $\endgroup$
    – Reid Hayes
    May 31, 2016 at 13:57
  • $\begingroup$ 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. $\endgroup$
    – MarcoB
    May 31, 2016 at 14:00
  • $\begingroup$ that's pretty nice, but I'm sure there must be an even more idiomatic way. $\endgroup$
    – Reid Hayes
    May 31, 2016 at 14:48

1 Answer 1

1
$\begingroup$

Here are two possible approaches:

You could inactivate Plus as follows:

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

formatted output

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

inactive sum

Activate@expr

after reactivation

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

formatted circleplus

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

no meaning for circle plus

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

expr /. CirclePlus -> Plus

(* Out: {{2, 1}, {0, 1}} *)
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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