Multiplication on Multi Dimensional Arrays
Some applications where multi dimensional arrays could simplify mathematical expressions are the Taylor plane and distributions which have high order statistics. I have recently written some MATLAB code which seems to generalize the idea of matrix multiplication. I have tested my code for a 2x2x2 symbolic array multiplied by a vector. In normal linear algebra we can think of a lift multiply and a right multiply.
With a 3 dimensional array one could consider multiplication with respect to each of the 6 faces of the cube, and could consider every possible orientation of the operands. This is not the approach I took. Rather I made it so that you specify the inner dimensions in the multiplication of each operand. For instance you can do standard matrix multiplication with this algorithm as follows: Let:
A=
(:,:,1) =
[ a111, a121]
[ a211, a221]
(:,:,2) =
[ a112, a122]
[ a212, a222]
X=
x1
x2
If we wanted to represent the Right multiplication of A by X would write:
>> A.in(2)*X.in(1)
ans(:,:,1) =
a111*X1+a121*X2
a211*X1+a221*X2
ans(:,:,2) =
a112*X1+a122*X2
a212*X1+a222*X2
Before we discuss the other types of multiplication let:X2=permute(X,[2,1,3])=
[ x1, x2]
And
X3=permute(X,[3,2,1])=
(:,:,1) =
x1
(:,:,2) =
x2
Then the left multiplication of A by X is:X2.in(2)*A.in(1)
ans(:,:,1) =
[ a111*x1+x2*a211, x1*a121+a221*x2]
ans(:,:,2) =
[ a112*x1+x2*a212, x1*a122+a222*x2]
And one might define the rear multiplication of A by X as follows:
A.in(3)*X3.in(3)
ans =
[ a111*x1+a112*x2, x1*a121+a122*x2]
[ a211*x1+x2*a212, a221*x1+a222*x2]
The algorithm produces answers depending if X is on the left or right of the multiplication sign and which you specify as the inner dimensions. I will give a few other examples later and the readers can hopefully give feedback if the answers make sense. What is clear is that the algorithm can be used to simplify the expressions for things like high third order Taylor approximations to two dimensional quadratic surfaces. In general the algorithm should not be limited to two dimensional and third order quadratic surfaces. However, I have not tested the code yet beyond these surfaces.
With respect to higher order statistics I am interested to know if there is an easy extension of the joint Gaussian that can be expressed in terms of these high order statistics.
Some examples of non standard products:>> A.in(1)*X.in(2)
and get the expected result:
ans(:,:,1) =
[ a111*x1+a211*x1, x1*a121+a221*x1]
[ a111*x2+x2*a211, a121*x2+a221*x2]
ans(:,:,2) =
[ a112*x1+a212*x1, x1*a122+a222*x1]
[ a112*x2+x2*a212, a122*x2+a222*x2]
>> A.in(2)*X.in(2)
ans(:,:,1) =
a111*X1+a121*X1
a111*X2+a121*X2
a211*X1+a221*X1
a211*X2+a221*X2
ans(:,:,2) =
a112*X1+a122*X1
a112*X2+a122*X2
a212*X1+a222*X1
a212*X2+a222*X2
>> A.in(1)*X.in(1)
ans(:,:,1) =
[ a111*X1+a211*X2, a121*X1+a221*X2]
ans(:,:,2) =
[ a112*X1+a212*X2, a122*X1+a222*X2]
In the next case there are no inner dimensions and we get the Kronecker product:
>> A*X
ans(:,:,1) =
[ a111*X1, a121*X1]
[ a111*X2, a121*X2]
[ a211*X1, a221*X1]
[ a211*X2, a221*X2]
ans(:,:,2) =
[ a112*X1, a122*X1]
[ a112*X2, a122*X2]
[ a212*X1, a222*X1]
[ a212*X2, a222*X2]
function y=mtimes(A,B)
%maxdim=1;
size1=size(A.A);
size2=size(B.A);
L=max(length(size1),length(size2));
size1A=[size1 ones(1,L-length(size1))];
size1B=[size2 ones(1,L-length(size2))];
size2A=size1A;
size2B=size1B;
size2A(A.in)=1;
size2B(B.in)=1;
size_ans=size2A.*size2B;
y=zeros(size_ans);
if isa(A.A,'sym')isa(B.A,'sym')
y=sym(y);
end
pSizeAns=[1 cumprod(size_ans(1:end-1))];
%p1size1B=cumbprod(size1B);
%p2size2B=[1 p1size1B(1:end-1)];
for i=1:prod(size_ans)
K_Ans=mod(floor((i-1)./pSizeAns),size_ans)+1;
getA=cell(size(K_Ans));
getB=cell(size(K_Ans));
for j=1:length(K_Ans)
if (size2A(j)==1)
getA{j}=':';
else
getA{j}=floor((K_Ans(j)-1)/size2B(j))+1;
end
if (size2B(j)==1)
getB{j}=':';
else
getB{j}=mod(K_Ans(j)-1,size2B(j))+1;
end
end
sliceA=A.A(getA{:});
sliceB=B.A(getB{:});
Ap=1:length(size1A);
Bp=1:length(size1B);
Ap(A.in)=[];Ap=[A.in Ap];
Bp(B.in)=[];Bp=[B.in Bp];
sliceA=permute(sliceA,Ap);
sliceB=permute(sliceB,Bp);
myProd=sliceA.*sliceB;
y(i)=sum(myProd(:),1);
end
I posted DogOnPorch’s comments here because I find he has a perspective well grounded in history. I thought there was no basis for the deletion of his post from Politics Canada. Rather then have such valuable insight disappear I reposted it here. I got one rather long reply from Sean which tries to show the absurdity of collective guilt by historical references of British imperialism.
I do not agree with Sean’s concussion. I am not a historian. I appreciate other historical perspectives. My feelings are theBritish empire was not as destructive as suggested by Sean but it is certainly an interesting historical question. His post was clearly substantial enough to be a headline topic on this blog.
"'On the German home front, a collective guilt settled onto the German population who'
The problem with the collective guilt argument, s243A, is that it can be used against a *lot of* other European countries, and hit a few others much harder than Germany when you take the past couple centuries into account.
My family is Irish, for example. When we look at our family tree there's... oh, a couple surviving branches, whereas most families have a rich tapestry going back generations. Why? In the early 1800's, the British decided that we pesky Irish then resident in Corcaigh-- better known as Cork, to those on the outside-- were too much of a troublesome nuisance to the Crown to be given the same rights, even to basic survival, as the British themselves. So over a century, in a collection of military operations, hypertaxation, arrogant and brutal repression, deliberately murderous economic policies (aka pumping out Irish foodstuffs other than the potato when the potato itself was known to be hit by blight), and horridly bloody attacks on Irish civilians-- after the 1916 Easter Rebellion for example-- millions of Irish were killed, and much of my family was thereby deliberately wiped out.
Not genocidal? Oh yes, very genocidal. And if you have any doubts about Ireland, then check out British "civilization" as brought to Tasmania and Australia, where the British government gave settlers cash payments and land for hunting down and massacring the aboriginals there. Or India, where the toll stands at up to 50 million dead Indians-- a number that dwarfs Hitler and Stalin-- due to British policies of confiscating Indian croplands, brutally taxing the peasantry, manipulating prices in Indian markets and taking out desperately needed food from Indian markets, as had been done in Ireland. http://tinyurl.com/yxwr8g
Now, the British themselves were busy getting flattened like pancakes themselves (and pansies, which they were) in Afghanistan on 3 different occasions as well as in several unsuccessful battles in South America, where a bunch of militias in Uruguay and the Plata somehow defeated the British and blocked them from the region: http://tinyurl.com/y7hxgb
Yet it was precisely this British fecklessness and incompetence that led them to be so cruel in their colonies. Maybe it was partly just a matter of taking out their frustrations on the Irish, aboriginals and Indians, but a lot of it was also the desperate need to acquire resources to launch their failed wars. One could go on-- British terror bombing in eastern Africa or around Basra in the early 1900's, British slavery in Jamaica (which was especially cruel), no shortage of examples here.
Why bring this up? Because in Germany, the people and the nation's government have clearly and openly apologized for what happened during the 1930's and 1940's and made reparations. Whereas Britain in particular-- and other colonial powers to a lesser degree-- have not apologized to anywhere near the same degree for their own colonial atrocities lasting for much longer, doing much more damage, and being abetted by governments which were wealthy and didn't have the excuse of hyperinflation or revanchism (as Germany during the late Weimar years did-- and the Nazis never, ever won a majority either, despite confused claims to the contrary).
If you truly feel ashamed of your own German heritage, then I feel very sorry for you. Many Germans in the 20th century did horrible things which we justifiably condemn. The Germans also produced people like Bach, Kant, Beethoven, Brahms, Daimler, Benz, Diesel, Goethe, Siemens and who knows how many other people whose work in science, medicine, chemistry, art and music have if anything shaped the world we live in. We owe a good deal of the comforts of the modern developed world to the hard work and moral character of Germans in prior centuries-- the very computer you're writing on is a direct descendant of the information revolution that was launched with the Gutenberg printing press.
All this is to say that countries in glass houses shouldn't be throwing stones. When Western Europe's major colonial countries-- and Britain in particular, which was by far the worst colonialist in the 19th century with an atrocious record even worse than the 20th century dictators-- come clean about their own crimes, and when they return what they themselves looted from their colonies, then maybe I'll be a little more sympathetic to the moral indignation about the WWII Axis countries. But until such consistency is demonstrated, I see little more than propaganda and hypocrisy at work on this topic.
5:39 PM
Another point to add here, s243A, that may help to give a sense of the scale of the British ruin in India-- prior to British military operations both of the East India Company and the main army itself, India was the second-richest country in the world, with a civilization almost three times as old as Britain and more ancient even than the great cultures of ancient Greece.
Likewise, Britain only actually took control of around 53% of what is now India on the map-- the rest of the country stayed independent of British rule, with Indian rulers who did business equally with Russians to the north and British to the (mainly) northeast. Yet even in the subset of the Subcontinent under their control, the British did terrible damage to kill so many millions of people and cause such economic destruction. India had advanced industries before the East India came along, by 1948, eastern India (the seat of British power) was devastated, while the unconquered regions of India relatively prospered.
Even today, it is the regions in India not under British control that are the most thriving economically and culturally, while those under British imperial rule are in the worst shape. This much applies to other colonies as well. Notice also that the common claim of a "humane British decolonization"-- compared to France-- is also BS: British operations in Kenya were very brutal, read for example "Britain's Gulag" by Caroline Elkins to get a sense of the deliberate massacres, many with a genocidal tinge, that took place there. (And compare also Australia, North America-- the deliberate spreading of smallpox-- and Ireland for other examples.) The British were defeated in the Suez, Israel and southern Yemen among other places after WWII, but often in the wake of a very ugly war against the civilian population, for example.
One other example to bring up-- the Opium Wars, where the British started a war to force an addictive drug onto the Chinese population, in return for products of actual value from China. Hard to think of a more disgusting basis for launching a war, and there aren't much in the way of parallels in the 19th century-- except admittedly for France, which joined with Britain during the Second Opium War in 1860 and, along with Britain, burned down the almost incomparable cultural treasure of the Imperial Summer Gardens in their vandalism (for which the British and their partners in crime in the Second Opium War will be forever cursed, until the Brits make apologies and pay full reparations with interest for it).
Thankfully for our modern world, China was still strong enough, despite its corrupt government, to stay independent, such that China was never colonized by Britain or any other Western power in the 18th-20th centuries. (There were in fact some smaller encounters after the Second Opium War when Chinese forces, with improved weapons, were managed to thwart attacks and beat back not only the British but also the French, Dutch, the Russians, even Americans. The rest-- the Chinese managed to fend off with some well-placed bribery.)
Still, on the record, I'd say that if we're considering atrocities against civilians, genocides, war crimes and other brutality, the British and other 19th-century colonial powers have a lot to answer for and a lot of loot to return. Some Irish people I've met even cheer not only the Zeppelin attacks and the Blitz (and the Luftwaffe bombings of British factories during WWII, the V-2 and V-1 bombings) but also the brutal Irish Republican Army attacks against Britain from the 1920's onward, especially the very terrible attacks on British pubs, malls and shopping facilities from 1970 to the present. A few are so full of rage that they even cheer the more recent terrorist attacks against the British homeland (like 7/7), as justifiable retribution.
I find this to be preposterous-- British civilians today are not responsible for what their ancestors did. What Britain needs to do is to acknowledge this past fully and stop acting like a hypocritical moral authority, and pay reparations and return loot where it was stolen-- not suffer attacks against its homeland for what prior generations did. Today's Britons cannot be blamed for this, and attacks against them are terrorist atrocities, not "acts of vengeance." Yet this is the problem with collective guilt arguments-- dragged out enough, collective guilt is used to justify such modern attacks for past atrocities by prior British governments, as some of my own friends have declared. Pointing the finger of collective guilt can have deadly consequences for innocent people."