Showing ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

Question:

I am trying to use the following matrices and perform a dot product as shown in the code. I checked the size of the matrices and all are (3, 1) but it is throwing me error for the last two dot products.

coordinate1 = [-7.173, -2.314, 2.811] 
coordinate2 = [-5.204, -3.598, 3.323] 
coordinate3 = [-3.922, -3.881, 4.044] 
coordinate4 = [-2.734, -3.794, 3.085] 

import numpy as np 
from numpy import matrix
coordinate1i=matrix(coordinate1)
coordinate2i=matrix(coordinate2)
coordinate3i=matrix(coordinate3)
coordinate4i=matrix(coordinate4)

b0 = coordinate1i - coordinate2i
b1 = coordinate3i - coordinate2i
b2 = coordinate4i - coordinate3i

n1 = np.cross(b0, b1)
n2 = np.cross(b2, b1)

n12cross = np.cross(n1,n2)
x1= np.cross(n1,b1)/np.linalg.norm(b1)
print np.shape(x1)
print np.shape(n2)
np.asarray(x1)
np.asarray(n2)

y = np.dot(x1,n2)
x = np.dot(n1,n2)

return np.degrees(np.arctan2(y, x))
Asked By: shome

||

Answers:

By converting the matrix to array by using

n12 = np.squeeze(np.asarray(n2))

X12 = np.squeeze(np.asarray(x1))

solved the issue.

Answered By: shome

Unlike standard arithmetic, which desires matching dimensions, dot products require that the dimensions are one of:

  • (X..., A, B) dot (Y..., B, C) -> (X..., Y..., A, C), where ... means “0 or more different values
  • (B,) dot (B, C) -> (C,)
  • (A, B) dot (B,) -> (A,)
  • (B,) dot (B,) -> ()

Your problem is that you are using np.matrix, which is totally unnecessary in your code – the main purpose of np.matrix is to translate a * b into np.dot(a, b). As a general rule, np.matrix is probably not a good choice.

Answered By: Eric
numpy.dot(a, b, out=None)

Dot product of two arrays.

For N dimensions it is a sum product over the last axis of a and the second-to-last of b.

Documentation: numpy.dot.

Answered By: Theon.Soong

The column of the first matrix and the row of the second matrix should be equal and the order should be like this only

column of first matrix = row of second matrix

and do not follow the below step

row of first matrix  = column of second matrix

it will throw an error

Answered By: Shinto Joseph

Solution: Input the transpose of n2 into dot product:

np.dot(x1, n2.T)

For a dot product to work, the dimensions of the input matrices must meet a certain criteria: the size of the column of the first matrix = the size of the row of the second matrix.

You have both matrices with dimensions (1,3):
x1.shape => (1,3), n2.shape => (1,3).
Following the above rule, you need x1.shape => (1,3), n2.shape => (3,1).
This is easily accomplished by taking the transpose of n2, shown above.

Answered By: T Law
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.