# Multiply differential operator matrix and shape function matrix

I'm new to Mathematica.

Say $$N(\xi, \eta)$$ (NSHAPE in code below) is a 2x6 matrix:

$$N(\xi,\eta) = \left[ \begin{array}{cccccc} 1 -\xi -\eta & 0 & \xi & 0 & \eta & 0 \\ 0 & 1 -\xi -\eta & 0 & \xi & 0 & \eta \\ \end{array} \right]$$

I wanna get a 4x6 matrix

$$Q= \left[ \begin{array}{cc} \frac{\partial }{\partial \xi } & 0 \\ \frac{\partial }{\partial \eta } & 0 \\ 0 & \frac{\partial }{\partial \xi } \\ 0 & \frac{\partial }{\partial \eta } \\ \end{array} \right] N(\xi, \eta)$$

I tried

NSHAPE = {{1 - ξ - η, 0, ξ, 0, η, 0}, {0, 1 - ξ - η, 0, ξ, 0, η}};
Q = Map[D[#1, #2] &, {NSHAPE, {ξ, η}}]


but it doesn't give the expected result.

• Please show us NSHAPE in valid Mathematica code. Commented Oct 20, 2022 at 13:18
• Something like this? Clear[e, n, matN, matD]; matN[e_, n_] := { {1 - e - n, 0, e, 0, n, 0}, {0, 1 - e - n, 0, e, 0, n} }; matD = { {D[#, e] &, 0}, {D[#, n] &, 0}, {0, D[#, e] &}, {0, D[#, n] &} }; matD . matN[e, n] // MatrixForm  Commented Oct 20, 2022 at 13:22
• Since Syed has done you a favor, I added the missing NSHAPE for you. Do remember complete code sample is almost always necessary for an on-topic question in this site. Commented Oct 20, 2022 at 14:42
• @xzczd Thank you very much! The NSHAPE in code is actually N(xi, eta) mat in my description, because N is a function name in Mathematica, so I replaced the name.
– Mike
Commented Oct 21, 2022 at 1:57

One can use Inner like this:

mydot[a_, b_] := Inner[If[Head[#1] === Function, #1[#2], #1*#2] &, a, b, Plus];


This multiplies a and b as matrices, but if an entry of a is a function, it will apply the function.

Example:

mydot[{{1/# &, 3}}, {{a, b}, {c, d}}]
(* {{1/a + 3 c, 1/b + 3 d}} *)

• One could also use Inner with Construct if there are only functions in the matrix on the left. For both cases of function application and multiplication another possibility is mydot[a_, b_] := Inner[Switch[#1, _Function, Construct[##], _, Times[##]] &, a, b, Plus] Commented Oct 21, 2022 at 13:18
• Thanks. I did not know about Construct, I must have typed #1[#2]& a million times in my life so far. Commented Oct 21, 2022 at 13:25
• #1[#2] is faster to type or about the same speed as Construct[##] (with autocompletion) but Construct is nice when no Slot is needed as in the case where the left matrix elements are functions (so without the If testing) as it's just Inner[Construct,a,b,Plus]. Your answer is the only case where I found it could be useful. Commented Oct 21, 2022 at 13:36
• Actually, Inner[Construct, a, b] since Plus is assumed when it is not included. Commented Oct 21, 2022 at 13:44

Just combine 5th syntax of D and 4th syntax of Flatten in the document:

Flatten[D[NSHAPE, {{ξ, η}}], {{1, 3}, {2}}]
(* {{-1, 0, 1, 0, 0, 0}, {-1, 0, 0, 0, 1, 0},
{0, -1, 0, 1, 0, 0}, {0, -1, 0, 0, 0, 1}} *)


If you prefer the differential operator method shown in traditional math formula, make use of UpSetDelayed (^:=):

d[x_] expr_ ^:= D[expr, x]
{{d[ξ], 0}, {d[η], 0}, {0, d[ξ]}, {0, d[η]}} . NSHAPE
(* {{-1, 0, 1, 0, 0, 0}, {-1, 0, 0, 0, 1, 0},
{0, -1, 0, 1, 0, 0}, {0, -1, 0, 0, 0, 1}} *)

nmat[ξ_, η_] := {{1 - ξ - η, 0, ξ, 0, η,
0}, {0, 1 - ξ - η, 0, ξ, 0, η}}

opmat = {{D[#, ξ] &, 0}, {D[#, η] &, 0}, {0,
D[#, ξ] &}, {0, D[#, η] &}}

opmat . nmat[ξ, η] /. Times[a_, b_Function] :> Composition[b][a]


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