# Identity operator of unspecified dimensions

I would like to create a function which can can take a symbolic square matrix $$m$$ and returns $$m \otimes I$$. Where $$I$$ is the idenity operator of same dimensions as $$m$$. The actual function I am interested in is more complicated but this is the minimal functionality that I am interested in.

An implementation that would work for non-symbolic (I just mean concrete) matrices m could for example be the following

mKronI[m_] := KroneckerProduct[m, IdentityMatrix[Length[m]]]


Then, inserting some specific matrix, say $$m = \left( \begin{matrix} 1 & 0 \\ 0 & 0 \end{matrix} \right)$$ should return $$\left( \begin{matrix} 1 & 0 \\ 0 & 0 \end{matrix} \right) \otimes \left( \begin{matrix} 1 & 0 \\ 0 & 1 \end{matrix} \right) = \left( \begin{matrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{matrix} \right)$$ Indeed:

In[]:m = {{1, 0}, {0, 0}};
In[]:mKronI[m]

Out[]:{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}


However, such a function returns an error when a symbolic variable is passed as it is unable to determine the Length of this variable.

Would it be possible to let the function simply return 'its definition' in such a case? Such that we would have

In[]:answ = mKronI[m]

Out[]:KroneckerProduct[m, IdentityMatrix[Length[m]]]


which then would also ensure that whenever m will be specified later, answ will automatically be evaluated to the correct value.

In simple terms I would like similar behavior for the function as we have for the following trivial example which mixes symbolic variables and variables for which the value is specified

In[]:c = a + b
Out[]:a + b


Then, if we specify b

In[]:b = 1


And check the value for c, we get

In[]:c

Out[]:1+a


I hope my question is clear. I have looked into a number of posts Implementing the symbolic identity matrix and Symbolic tensor simplifications and the identity matrix but I don't understand the answers given there well and also I doubt if they would help me. I also fiddled around with some things as Assuming[m \[Element] Matrices[{n, n}], KroneckerProduct[m, IdentityMatrix[n]]]) but so far without luck.

Thanks in advance for any help.

• Make a conditional definition like: mKronI[m_] /; MatrixQ[m] := KroneckerProduct[m, IdentityMatrix[Length[m]]] May 22, 2021 at 16:43
• Why not just use SetDelayed for answ? Or not invoke mKronI until you have an actual matrix set for your variable you use in mKronI? Are you aware that you don’t have to use only m in your mKronI? You could set something like matrixThatIsNotm to a matrix and invoke mKronI[matrixThatIsNotm]. I apologize if this is a misunderstanding of your problem, but it seems to me that you could simply change slightly your method of programming to not generate this problem? May 22, 2021 at 18:52
• Most importantly I want Mathematica to not give an error when using the function with a symbolic input so that the result can be used in further calculations. Your solution @DanielHuber does that so thank you! I didn't know about Condition. @CA Trevillian, if I use SetDelayed for answ it will not directly yield an error, but it will the first time I use it somewhere. But I don't understand your suggestion. What do you mean by setting something to a matrix? Similar to Element[m, Matrices[{n, n}]]? But you may be right about my rogramming methods,maybe I should reconsider them. May 22, 2021 at 19:42