0
$\begingroup$

I have physical equations of motion that describe the dependence of one vector field on the components of another vector field. Without getting too much into detail, my differential equations involve a double curl that mixes the vector components on one side of the equation. Furthermore, I would like to discretize the space on which these equations are defined. So far I was able to set up the discretized equations:

$$ \frac{d}{dt}\ \begin{pmatrix} \vdots\\ P_x(i)\\ P_y(i)\\ P_z(i)\\ \vdots \end{pmatrix} = \begin{pmatrix} \vdots\\ A_y(i)-A_y(i+1)\\ -A_x(i)+A_x(i+1)-a*A_z(i)\\ b*A_y(i)\\ \vdots \end{pmatrix}\tag{1} $$ where a and b are some arbitrary constants and $\vec{A}$ and $\vec{P}$ are the two vector fields whose relation I want to determine. The lattice coordinates in one dimension are written as i (e.g. $\frac{d}{dt} P_x(7) = A_y(7)-A_y(8)$).

Is there any way to automatically turn this into a matrix multiplication? I would want the result to look something like

$$ \frac{d}{dt}\ \begin{pmatrix} \vdots\\ P_x(i)\\ P_y(i)\\ P_z(i)\\ P_x(i+1)\\ P_y(i+1)\\ P_z(i+1)\\ \vdots \end{pmatrix} = \begin{pmatrix} \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots \\ \ddots & 0 & 1 & 0 & 0 & -1 & 0 & \ddots\\ \ddots & -1 & 0 & -a & 1 & 0 & 0 & \ddots\\ \ddots & 0 & b & 0 & 0 & 0 & 0 & \ddots\\ \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots\\ \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots\\ \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots\\ \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots\\ \end{pmatrix} \begin{pmatrix} \vdots\\ A_x(i)\\ A_y(i)\\ A_z(i)\\ A_x(i+1)\\ A_y(i+1)\\ A_z(i+1)\\ \vdots \end{pmatrix} \tag{2} $$

You can already see from this example that there is some regularity as P(i+1), P(i+2), etc. will have the same entries in the matrix as the ones for P(i), only that the entries within the matrix will be at different positions.

So the question is: If I gave you the vector on the right-hand side of equation (1), could you give me the matrix on the right-hand side of equation (2)?

$\endgroup$
  • $\begingroup$ SparseArray[{ {i_, j_} /; Mod[i, 3] == 1 && j == i + 1 :> 1, {i_, j_} /; Mod[i, 3] == 1 && j == i + 4 :> -1, {i_, j_} /; Mod[i, 3] == 2 && j == i + 1 :> 1, {i_, j_} /; Mod[i, 3] == 2 && j == i + 3 :> -a, {i_, j_} /; Mod[i, 3] == 2 && j == i + 4 :> -1, {i_, j_} /; Mod[i, 3] == 0 && j == i + 1 :> b, ...}, {n, n}]? $\endgroup$ – Michael E2 Sep 26 at 21:36
  • $\begingroup$ @MichaelE2, I tried your suggestion and must be doing something wrong because when I copy-paste what you wrote, into v12.1 on a Mac, it gives an error "Syntax::sntxf" saying SparseArray cannot be followed by what is following it. I cleared variables so I don't think that is my problem and unfortunately, it doesn't work for me. $\endgroup$ – Mark R Sep 26 at 21:54
  • $\begingroup$ @MarkR The ellipsis is an ellipsis, not a RepeatedNull. The code is about as complete as the post and is merely a hint. $\endgroup$ – Michael E2 Sep 26 at 22:44
  • $\begingroup$ @MichaelE2 - thanks for letting me know. I should have realized it was just showing the general idea. I should have looked more closely before trying to copy-paste. $\endgroup$ – Mark R Sep 27 at 19:48
3
$\begingroup$

You could use CoefficientArrays. For example:

m = CoefficientArrays[
    {
    Ay[i]-Ay[i+1],
    -Ax[i]+Ax[i+1]-a Az[i],
    b Ay[i]
    },
    {Ax[i],Ay[i],Az[i],Ax[i+1],Ay[i+1],Az[i+1]}
][[2]];
m //Normal //TeXForm

$\left( \begin{array}{cccccc} 0 & 1 & 0 & 0 & -1 & 0 \\ -1 & 0 & -a & 1 & 0 & 0 \\ 0 & b & 0 & 0 & 0 & 0 \\ \end{array} \right)$

| improve this answer | |
$\endgroup$
  • $\begingroup$ Very neat solution! $\endgroup$ – xabdax Sep 26 at 23:08

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.