Re: Multiply 2 matrices where one contains differential operators with one that contains functions of x and y
- To: mathgroup at smc.vnet.net
- Subject: [mg104466] Re: [mg104417] Multiply 2 matrices where one contains differential operators with one that contains functions of x and y
- From: "Nasser M. Abbasi" <nma at 12000.org>
- Date: Sun, 1 Nov 2009 04:00:42 -0500 (EST)
- References: <20091029225146.K51SM.569239.imail@eastrmwml30> <200910310650.BAA13104@smc.vnet.net> <op.u2on0gpdtgfoz2@bobbys-imac.local>
- Reply-to: "Nasser M. Abbasi" <nma at 12000.org>
DrMajorBob;
Thanks for your detailed reply, I need to look at your code more closely.
But just to answer your initial point
> That doesn't look like a Dot product at all, since it operates a 3x2
> matrix on a 2x3 and gets a 3x3.
Yes. I am doing matrix products ofcourse, so 3x2 matrix when multiplied by
2x3 matrix better result in 3x3 or else something is really wrong :)
>From help on Dot
"The standard (built-in) usage still exists a.b.c or Dot[a, b, c] gives
products of vectors, matrices and tensors"
I use A.B to multiply matrices by each others? may be I am missing your
point here. How else would you multiply matrices with each others in
Mathematica other than using the Dot?
Best,
--Nasser
----- Original Message -----
Subject: [mg104466] Re: [mg104417] Multiply 2 matrices where one contains differential
operators with one that contains functions of x and y
> That doesn't look like a Dot product at all, since it operates a 3x2
> matrix on a 2x3 and gets a 3x3.
>
> So... to find out what's really happening:
>
> a = Array[f, {3, 2}]
> b = Array[g, {2, 3}]
> {rowsA, colsA} = Dimensions[a];
> {rowsB, colsB} = Dimensions[b];
> r = Table[0, {rowsA}, {colsB}];(*where the result of A.B goes*)For[
> i = 1, i <= rowsA, i++,
> For[j = 1, j <= colsB, j++,
> For[ii = 1, ii <= rowsB, ii++,
> r[[i, j]] = r[[i, j]] + a[[i, ii]] /@ {b[[ii, j]]}]]]
> r
> Dimensions@r
>
> {{f[1, 1], f[1, 2]}, {f[2, 1], f[2, 2]}, {f[3, 1], f[3, 2]}}
>
> {{g[1, 1], g[1, 2], g[1, 3]}, {g[2, 1], g[2, 2], g[2, 3]}}
>
> {{{f[1, 1][g[1, 1]] + f[1, 2][g[2, 1]]}, {f[1, 1][g[1, 2]] +
> f[1, 2][g[2, 2]]}, {f[1, 1][g[1, 3]] +
> f[1, 2][g[2, 3]]}}, {{f[2, 1][g[1, 1]] +
> f[2, 2][g[2, 1]]}, {f[2, 1][g[1, 2]] +
> f[2, 2][g[2, 2]]}, {f[2, 1][g[1, 3]] +
> f[2, 2][g[2, 3]]}}, {{f[3, 1][g[1, 1]] +
> f[3, 2][g[2, 1]]}, {f[3, 1][g[1, 2]] +
> f[3, 2][g[2, 2]]}, {f[3, 1][g[1, 3]] + f[3, 2][g[2, 3]]}}}
>
> {3, 3, 1}
>
> That looks like an outer product of the rows of a with the columns of b.
>
> Here's a somewhat similar structure (with one less low-level dimension):
>
> Outer[h, a, Transpose@b, 1, 1]
> Dimensions@%
>
> {{h[{f[1, 1], f[1, 2]}, {g[1, 1], g[2, 1]}],
> h[{f[1, 1], f[1, 2]}, {g[1, 2], g[2, 2]}],
> h[{f[1, 1], f[1, 2]}, {g[1, 3], g[2, 3]}]}, {h[{f[2, 1],
> f[2, 2]}, {g[1, 1], g[2, 1]}],
> h[{f[2, 1], f[2, 2]}, {g[1, 2], g[2, 2]}],
> h[{f[2, 1], f[2, 2]}, {g[1, 3], g[2, 3]}]}, {h[{f[3, 1],
> f[3, 2]}, {g[1, 1], g[2, 1]}],
> h[{f[3, 1], f[3, 2]}, {g[1, 2], g[2, 2]}],
> h[{f[3, 1], f[3, 2]}, {g[1, 3], g[2, 3]}]}}
>
> {3, 3}
>
> It remains to figure out what "h" should be. A simple solution is
>
> h[{a_, b_}, {c_, d_}] := a[c] + b[d]
> Outer[h, a, Transpose@b, 1, 1]
> Dimensions@%
>
> {{f[1, 1][g[1, 1]] + f[1, 2][g[2, 1]],
> f[1, 1][g[1, 2]] + f[1, 2][g[2, 2]],
> f[1, 1][g[1, 3]] + f[1, 2][g[2, 3]]}, {f[2, 1][g[1, 1]] +
> f[2, 2][g[2, 1]], f[2, 1][g[1, 2]] + f[2, 2][g[2, 2]],
> f[2, 1][g[1, 3]] + f[2, 2][g[2, 3]]}, {f[3, 1][g[1, 1]] +
> f[3, 2][g[2, 1]], f[3, 1][g[1, 2]] + f[3, 2][g[2, 2]],
> f[3, 1][g[1, 3]] + f[3, 2][g[2, 3]]}}
>
> {3, 3}
>
> To get back the low-level (probably unnecessary) dimension:
>
> h[{a_, b_}, {c_, d_}] := {a[c] + b[d]}
> Outer[h, a, Transpose@b, 1, 1]
> Dimensions@%
>
> {{{f[1, 1][g[1, 1]] + f[1, 2][g[2, 1]]}, {f[1, 1][g[1, 2]] +
> f[1, 2][g[2, 2]]}, {f[1, 1][g[1, 3]] +
> f[1, 2][g[2, 3]]}}, {{f[2, 1][g[1, 1]] +
> f[2, 2][g[2, 1]]}, {f[2, 1][g[1, 2]] +
> f[2, 2][g[2, 2]]}, {f[2, 1][g[1, 3]] +
> f[2, 2][g[2, 3]]}}, {{f[3, 1][g[1, 1]] +
> f[3, 2][g[2, 1]]}, {f[3, 1][g[1, 2]] +
> f[3, 2][g[2, 2]]}, {f[3, 1][g[1, 3]] + f[3, 2][g[2, 3]]}}}
>
> {3, 3, 1}
>
> For your actual problem, that's
>
> a = {{D[#1, x] &, 0 &}, {0 &, D[#1, y] &}, {D[#1, y] &, D[#1, x] &}};
> b = {{x*y, x^3*y, 3*x + y^2}, {2*x, x^4*y, y^2}};
> h[{a_, b_}, {c_, d_}] := {a[c] + b[d]}
> Outer[h, a, Transpose@b, 1, 1]
>
> {{{y}, {3 x^2 y}, {3}}, {{0}, {x^4}, {2 y}}, {{2 + x}, {x^3 +
> 4 x^3 y}, {2 y}}}
>
> which agrees with your nested For method.
>
> The function h could be done differently, since it's analogous to
>
> Inner[Compose, {aa, bb}, {c, d}, Plus]
>
> aa[c] + bb[d]
>
> For instance:
>
> a = {{D[#1, x] &, 0 &}, {0 &, D[#1, y] &}, {D[#1, y] &, D[#1, x] &}};
> b = {{x*y, x^3*y, 3*x + y^2}, {2*x, x^4*y, y^2}};
> h = Inner[Compose, ##, Plus] &;
> Outer[h, a, Transpose@b, 1, 1]
>
> {{y, 3 x^2 y, 3}, {0, x^4, 2 y}, {2 + x, x^3 + 4 x^3 y, 2 y}}
>
> That omits the low-level List, but it can be added in a hundred different
> ways. For instance,
>
> a = {{D[#1, x] &, 0 &}, {0 &, D[#1, y] &}, {D[#1, y] &, D[#1, x] &}};
> b = {{x*y, x^3*y, 3*x + y^2}, {2*x, x^4*y, y^2}};
> h = Inner[Compose, ##, Plus] &;
> Map[List, Outer[h, a, Transpose@b, 1, 1], {2}]
>
> {{{y}, {3 x^2 y}, {3}}, {{0}, {x^4}, {2 y}}, {{2 + x}, {x^3 +
> 4 x^3 y}, {2 y}}}
>
> Bobby
>
> On Sat, 31 Oct 2009 01:50:39 -0500, Nasser M. Abbasi <nma at 12000.org>
> wrote:
>
>> Hello,
>> Version 7
>>
>> Lets say A is a 3 by 2 matrix, which contains differential operators in
>> some
>> entries and 0 in all other entries, as in
>>
>> A= { { d/dx , 0 } , {0 , d/dy } , { d/dy , d/dx } }
>>
>> And I want to multiply the above with say a 2 by 3 matrix whose entries
>> are
>> functions of x and y as in
>>
>> B = {{x*y, x^3*y, 3*x + y^2}, {2*x, x^4*y, y^2}}
>>
>> I'd like to somehow be able to do A.B, but ofcourse here I can't, as I
>> need
>> to "apply" the operator on each function as the matrix multiplication is
>> being carried out.
>>
>> I tried to somehow integrate applying the operators in A into the matrix
>> multiplication of A by B, but could not find a short "functional" way.
>>
>> So I ended up solving this by doing the matrix multiplication by hand
>> using
>> for loops (oh no) so that I can 'get inside' the loop and be able to
>> apply
>> the operator to each entry. This is my solution:
>>
>>
>> A = {{D[#1, x] & , 0 & }, {0 & , D[#1, y] & }, {D[#1, y] & , D[#1, x]
>> }}
>> B = {{x*y, x^3*y, 3*x + y^2}, {2*x, x^4*y, y^2}}
>>
>> {rowsA, colsA} = Dimensions[A];
>> {rowsB, colsB} = Dimensions[B];
>>
>> r = Table[0, {rowsA}, {colsB}]; (*where the result of A.B goes *)
>>
>> For[i = 1, i <= rowsA, i++,
>> For[j = 1, j <= colsB, j++,
>> For[ii = 1, ii <= rowsB, ii++,
>> r[[i,j]] = r[[i,j]] + A[[i,ii]] /@ {B[[ii,j]]}
>> ]
>> ]
>> ]
>>
>> MatrixForm[r]
>>
>> The above work, but I am sure a Mathematica expert here can come up with
>> a
>> true functional solution or by using some other Mathematica function
>> which I
>> overlooked to do the above in a more elegent way.
>>
>> --Nasser
>>
>>
>
>
> --
> DrMajorBob at yahoo.com