Doug Kerr
Well-known member
If we delve into the processing of images in a photographic chain, we may well run into calculations done by matrix multiplication. An important example would be the transformation of colors represented by the coordinates of the CIE XYZ color space into their r,g,b coordinates (the linear coordinates that are the precursors of the R,G,B coordinates of the sRGB color space). Or we may be working with the transformation of the three outputs of a sensor into (an approximation of) r, g, and b, often done by simple matrix multiplication.
Its very easy to get confused about matrix multiplication, which has regularly happened to me. In fact, just a few months ago, I needed the help of a colleague, the eminent color scientist Bruce Lindbloom, to straighten me on just how it worked in a certain matter I was looking into.
So I thought I would pass on my new understanding of this so that, if you should have to deal with the matter, you won't have to ask Bruce how it works.
I am not here going to speak of how the matrix multiplication is actually calculated. Rather, I will limit this note to the often-baffling matter of just how a matrix multiplication is set up, and how we speak of the operation.
In our work such as I mentioned above, we most often run into matrix multiplication involving a source matrix that is really a 1 × 3 "vector" of three values , a 3 × 3 matrix that does our "transformation", and a result matrix that is a 1 × 3 vector of three values - again, as when transforming X, Y, Z to r, g, b.
But the symmetry of a 3 × 3 matrix can make it hard to clearly see how the "rules" of these operations work, so I will at first use an example with an asymmetrical matrix.
Figure 1a
Here the two matrixes we are multiplying are identified as A and B (never mind for now "which is multiplied by which", a matter that is rather tricky, and of which I will speak in due time.)
Matrix A is a "3 × 4" matrix (we always mention the number of rows first and then the number of columns; this is contrary to the notation for indexes and coordinates, just to keep us off-balance!) Matrix B is a "4 × 2" matrix, and matrix C is a "3 × 2" matrix. Why did I choose that particular set of sizes? Well, as you will see in a minute, once I got started there was little choice as to how to proceed.
Note that I have put the "result" (and the equals sign) on the left side of this equation. That is the way we most find it done in mathematics. But it does not matter at all. If I were to move the equals sign to the right of A and B, and then put C to its right, nothing else would change. So I wont waste your time showing that variation in presentation.
There are some brackets and arrows that show how the dimensions of these three matrices are related. The relationship labeled "mandatory" is just that: unless the number of columns in the first factor is the same as the number of rows in the second factor, the multiplication is not defined. (In that statement and in similar ones to follow, "first" and "second" mean exactly that, and only that.) But the number of columns in the second factor can be whatever is needed to fit the overall calculation we need to make.
Th other thinner arrows show how the result, C, gets its dimensions. Its number of rows will be the same as the number of rows in the first factor; its number of columns will be the same as the number of columns in the second factor.
Got that?
Now that we've got those rules down pat (!), I'll move to examples involving a 3 × 3 matrix as one of the factors, since that is the case we will most often encounter, and in addition that case illuminates some further tricky aspects of this matter.
Figure 1b
This might be the situation in which B is the "vector" comprising the three outputs of a sensor, A is the transform matrix, and matrix C is the result (the r, g, and b values).
Now in normal "scalar" mathematics, if one way to set up a calculation is
we might (if only as a matter of "style") want to instead write it as:
The two are exactly equivalent, so in fact which we do is just a matter of "style".
But with matrix multiplication, we can't be nearly so sanguine. Here we see how we might, for some reason, rearrange our calculation see just above:
Figure 1c
The first thing we see is that the source matrix, B, and the result matrix , C, are now "laid on their sides". This is an unavoidable consequence of the "rules" I discussed above regarding the dimensions of the various matrixes. The primes on their designations are just to recognize this seemingly-trivial, but vital, difference.
Not visible, given the rather generic way I showed the matrixes, is that A, the transform matrix, must also be rearranged. In particular, it is flipped over its upper-left to lower right diagonal axis. (Technically, it is said to be "the transpose" of the original A matrix. The double prime on its designation alerts us tom this difference.)
We can often get into trouble when we speak of something being multiplied by something else.
Consider regular scalar multiplication. We might, in a narrative description of some calculation, say that "we then take the number of towers (10) and multiply it by the number of antennas on each tower (4) to get the total number of antennas needed (40).
Now does that narrative description mean:
or
Well, that verbal description doesn't tell us. But that really doesn't matter, since the two formulations are exactly equivalent.
But as you can guess for my discussion above, with matrix multiplication, that ambiguity can't just be blown off. So if we say "we take the set of three outputs from the sensor, B, and multiply it by the transformation matrix, A", does that mean in particular what we see in figure 1b or what we see in figure 1c? As we've seen, those are not the same thing, so we have to understand which is meant.
But fortunately there is terminology that can be used to describe exactly what we mean.
For example, if the specific calculation we have in mind is that seen in figure 1b, we can say, "we take the set of three outputs of the sensor, B, and left-multiply it by the transformation matrix, A.
Now do people often do that? No.
Best regards,
Doug
Its very easy to get confused about matrix multiplication, which has regularly happened to me. In fact, just a few months ago, I needed the help of a colleague, the eminent color scientist Bruce Lindbloom, to straighten me on just how it worked in a certain matter I was looking into.
So I thought I would pass on my new understanding of this so that, if you should have to deal with the matter, you won't have to ask Bruce how it works.
I am not here going to speak of how the matrix multiplication is actually calculated. Rather, I will limit this note to the often-baffling matter of just how a matrix multiplication is set up, and how we speak of the operation.
In our work such as I mentioned above, we most often run into matrix multiplication involving a source matrix that is really a 1 × 3 "vector" of three values , a 3 × 3 matrix that does our "transformation", and a result matrix that is a 1 × 3 vector of three values - again, as when transforming X, Y, Z to r, g, b.
But the symmetry of a 3 × 3 matrix can make it hard to clearly see how the "rules" of these operations work, so I will at first use an example with an asymmetrical matrix.
Figure 1a
Here the two matrixes we are multiplying are identified as A and B (never mind for now "which is multiplied by which", a matter that is rather tricky, and of which I will speak in due time.)
Matrix A is a "3 × 4" matrix (we always mention the number of rows first and then the number of columns; this is contrary to the notation for indexes and coordinates, just to keep us off-balance!) Matrix B is a "4 × 2" matrix, and matrix C is a "3 × 2" matrix. Why did I choose that particular set of sizes? Well, as you will see in a minute, once I got started there was little choice as to how to proceed.
Note that I have put the "result" (and the equals sign) on the left side of this equation. That is the way we most find it done in mathematics. But it does not matter at all. If I were to move the equals sign to the right of A and B, and then put C to its right, nothing else would change. So I wont waste your time showing that variation in presentation.
There are some brackets and arrows that show how the dimensions of these three matrices are related. The relationship labeled "mandatory" is just that: unless the number of columns in the first factor is the same as the number of rows in the second factor, the multiplication is not defined. (In that statement and in similar ones to follow, "first" and "second" mean exactly that, and only that.) But the number of columns in the second factor can be whatever is needed to fit the overall calculation we need to make.
Th other thinner arrows show how the result, C, gets its dimensions. Its number of rows will be the same as the number of rows in the first factor; its number of columns will be the same as the number of columns in the second factor.
Got that?
Now that we've got those rules down pat (!), I'll move to examples involving a 3 × 3 matrix as one of the factors, since that is the case we will most often encounter, and in addition that case illuminates some further tricky aspects of this matter.
Figure 1b
This might be the situation in which B is the "vector" comprising the three outputs of a sensor, A is the transform matrix, and matrix C is the result (the r, g, and b values).
Now in normal "scalar" mathematics, if one way to set up a calculation is
C = A • B
we might (if only as a matter of "style") want to instead write it as:
C = B • A
The two are exactly equivalent, so in fact which we do is just a matter of "style".
But with matrix multiplication, we can't be nearly so sanguine. Here we see how we might, for some reason, rearrange our calculation see just above:
Figure 1c
The first thing we see is that the source matrix, B, and the result matrix , C, are now "laid on their sides". This is an unavoidable consequence of the "rules" I discussed above regarding the dimensions of the various matrixes. The primes on their designations are just to recognize this seemingly-trivial, but vital, difference.
Not visible, given the rather generic way I showed the matrixes, is that A, the transform matrix, must also be rearranged. In particular, it is flipped over its upper-left to lower right diagonal axis. (Technically, it is said to be "the transpose" of the original A matrix. The double prime on its designation alerts us tom this difference.)
We can often get into trouble when we speak of something being multiplied by something else.
Consider regular scalar multiplication. We might, in a narrative description of some calculation, say that "we then take the number of towers (10) and multiply it by the number of antennas on each tower (4) to get the total number of antennas needed (40).
Now does that narrative description mean:
<number of antennas> = 4 • 10
or
<number of antennas> = 10 • 4
Well, that verbal description doesn't tell us. But that really doesn't matter, since the two formulations are exactly equivalent.
But as you can guess for my discussion above, with matrix multiplication, that ambiguity can't just be blown off. So if we say "we take the set of three outputs from the sensor, B, and multiply it by the transformation matrix, A", does that mean in particular what we see in figure 1b or what we see in figure 1c? As we've seen, those are not the same thing, so we have to understand which is meant.
But fortunately there is terminology that can be used to describe exactly what we mean.
For example, if the specific calculation we have in mind is that seen in figure 1b, we can say, "we take the set of three outputs of the sensor, B, and left-multiply it by the transformation matrix, A.
Now do people often do that? No.
Best regards,
Doug