8
$\begingroup$

I create a symbolic matrix using the following:

mat = ToExpression[Table[StringJoin[{"s", ToString[i], ToString[j]}], {i, 1, d}, {j, 1, d}]]

for arbitrary $d$. Notice that for $d=2$, this gives

$$\begin{pmatrix} s11 & s12 \\ s21 &s22\end{pmatrix}.$$

I do this since an analytic expression is important. However, I would like to get numerics. How can I update the above define for mat to get a function of the form:

matNEW[s11_, s12_, s21_, s22_]

such that I can evaluate the matrix later in the script for specific values of the parameters.

$\endgroup$
  • 10
    $\begingroup$ Start by using indexed variables instead of constructed expressions, such as s[1, 2] etc. Then making your array is simple: mat[d_Integer] := Array[s, {d, d}]. Later on, use replacements on your mat: mat[2] /. {s[1, 1] -> 2, ...} perhaps. $\endgroup$ – MarcoB May 29 at 16:09
6
$\begingroup$

You may use Indexed with Array to create a matrix that can be switched between symbolic and numeric content.

To input the script capital R symbol enter: Esc scR Esc . See more at Notational Alphabet Characters.

indexedArray[m_Symbol, dims_?(VectorQ[#, # ∈ PositiveIntegers &] &)] :=
 Array[Indexed[m, {##}] &, dims]

indexedArray builds a symbolic matrix on the unassigned symbol provided.

ℛ = indexedArray[r, {3, 2}];
MatrixForm@ℛ

Mathematica graphics

For symbolic calculations:

Transpose@ℛ // MatrixForm

Mathematica graphics

𝓒 = indexedArray[c, {2, 3}];
𝓒.ℛ // MatrixForm

Mathematica graphics

For numeric calculations:

We need to assign values to r not .

r = Partition[Range@6, 2];
MatrixForm@ℛ

Mathematica graphics

{Range@3}.ℛ // MatrixForm

Mathematica graphics

𝓒.ℛ // MatrixForm

Mathematica graphics

To return to symbolic calculations

Clear the Indexed variable to return to symbolic calculations.

r =.
ℛ // MatrixForm

Mathematica graphics

Hope this helps.

| improve this answer | |
$\endgroup$
  • $\begingroup$ @MarcoB Thanks for the script formatting. I did not realise that the Mma.SE Editor Tools worked on script symbols. Is this from a recent update? $\endgroup$ – Edmund May 30 at 0:45
  • $\begingroup$ You’re right, the editor doesn’t. I used steampiano.net/msc , which got the script R, and finally I copy-pasted the script C from a wiki page :-) $\endgroup$ – MarcoB May 30 at 2:24
  • $\begingroup$ @MarcoB No, it does do it now. I tried it before posting the comment. It didn't when I first used it a long time ago but it does do scripts now. $\endgroup$ – Edmund May 30 at 2:27
  • $\begingroup$ Well that’s interesting! You’re right, it worked. I wonder what I did wrong the first time. Even better then! $\endgroup$ – MarcoB May 30 at 4:20
  • $\begingroup$ @Edmund, great answer. Couple of questions. 1. How does indexedArray set the indices of r as subscripts? 2. Also, how does r = Partition[Range@6, 2]; assign values to the values of r? Can we instead set the values as a function of their indices? i.e. rjk = f[j,k] for some defined function f. $\endgroup$ – Sid May 31 at 21:46

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.