I have the following:
import numpy as np
XYZ_to_sRGB_mat_D50 = np.asarray([
[3.1338561, -1.6168667, -0.4906146],
[-0.9787684, 1.9161415, 0.0334540],
[0.0719453, -0.2289914, 1.4052427],
])
XYZ_1 = np.asarray([0.25, 0.4, 0.1])
XYZ_2 = np.random.rand(100,100,3)
np.matmul(XYZ_to_sRGB_mat_D50, XYZ_1) # valid operation
np.matmul(XYZ_to_sRGB_mat_D50, XYZ_2) # makes no sense mathematically
How do I perform the same operation on XYZ_2 that I would on XYZ_2? Do I somehow reshape the array first?
It seems you are trying to
sum-reduce
the last axis ofXYZ_to_sRGB_mat_D50
(axis=1)
with the last one ofXYZ_2
(axis=2)
. So, you can usenp.tensordot
like so -Related post to understand
tensordot
.For completeness, we can surely use
np.matmul
too after swappping last two axes ofXYZ_2
, like so -This won't be as efficient as
tensordot
one.Runtime test -
Generally speaking, when it comes to
sum-reductions
on tensors,tensordot
is much more efficient. Since, the axis ofsum-reduction
is just one, we can make the tensor a2D
array by reshaping, usenp.dot
, get the result and reshape back to3D
.