python - Numpy, dot products on multidimensional arrays -
i have doubts on numpy.dot product.
i define matrix 6x6 like:
c=np.zeros((6,6)) c[0,0], c[1,1], c[2,2] = 129.5, 129.5, 129.5 c[3,3], c[4,4], c[5,5] = 25, 25, 25 c[0,1], c[0,2] = 82, 82 c[1,0], c[1,2] = 82, 82 c[2,0], c[2,1] = 82, 82
then recast in 4-rank tensor using multidimensional array
def long2short(m, n): """ given 2 indices m , n of stiffness tensor function return index of voigt matrix = long2short(m,n) """ if m == n: = m elif (m == 1 , n == 2) or (m == 2 , n == 1): = 3 elif (m == 0 , n == 2) or (m == 2 , n == 0): = 4 elif (m == 0 , n == 1) or (m == 1 , n == 0): = 5 return c=np.zeros((3,3,3,3)) m in range(3): n in range(3): o in range(3): p in range(3): = long2short(m, n) j = long2short(o, p) c[m, n, o, p] = c[i, j]
and change coordinate reference system of tensor using rotation matrix define like:
q=np.array([[sqrt(2.0/3), 0, 1.0/sqrt(3)], [-1.0/sqrt(6), 1.0/sqrt(2), 1.0/sqrt(3)], [-1.0/sqrt(6), -1.0/sqrt(2), 1.0/sqrt(3)]]) qt = q.transpose()
the matrix orthogonal (althought numerical precision not perfect):
in [157]: np.dot(q, qt) out[157]: array([[ 1.00000000e+00, 4.28259858e-17, 4.28259858e-17], [ 4.28259858e-17, 1.00000000e+00, 2.24240114e-16], [ 4.28259858e-17, 2.24240114e-16, 1.00000000e+00]])
but why if perform:
in [158]: a=np.dot(q,qt) in [159]: c_mat=np.dot(a, c) in [160]: a1 = np.dot(qt, c) in [161]: c_mat1=np.dot(q, a1)
i expected value c_mat (=c) not c_mat1? there subtility use dot on multidimensional arrays?
the issue np.dot(a,b)
multidimensional arrays makes dot product of last dimension of a
second-to-last dimension of b
:
np.dot(a,b) == np.tensordot(a, b, axes=([-1],[2]))
as see, not work matrix multiplication multidimensional arrays. using np.tensordot()
allows control in axes
each input want perform dot product. example, same result in c_mat1
can do:
c_mat1 = np.tensordot(q, a1, axes=([-1],[0]))
which forcing matrix multiplication-like behavior.
Comments
Post a Comment