Skip to content

Instantly share code, notes, and snippets.

@innat
Last active September 6, 2024 21:38
Show Gist options
  • Save innat/4553cbdee9f937694202b1b174530bdf to your computer and use it in GitHub Desktop.
Save innat/4553cbdee9f937694202b1b174530bdf to your computer and use it in GitHub Desktop.
In Python np.dot and np.multiply with np.sum

Briefly:

np.dot is the dot product of two matrices.

|A B| . |E F| = |A*E+B*G A*F+B*H|
|C D|   |G H|   |C*E+D*G C*F+D*H|

Whereas np.multiply does an element-wise multiplication of two matrices.

|A B| ⊙ |E F| = |A*E B*F|
|C D|   |G H|   |C*G D*H|

When used with np.sum, the result being equal is merely a coincidence.

    >>> np.dot([[1,2], [3,4]], [[1,2], [2,3]])
    array([[ 5,  8],
           [11, 18]])
    >>> np.multiply([[1,2], [3,4]], [[1,2], [2,3]])
    array([[ 1,  4],
           [ 6, 12]])

    >>> np.sum(np.dot([[1,2], [3,4]], [[1,2], [2,3]]))
    42
    >>> np.sum(np.multiply([[1,2], [3,4]], [[1,2], [2,3]]))
    23
    
@zlatnaspirala
Copy link

A ->
[
[ 0.80146943 -0.21554566 -0.55784122]
[ 0.42798962 0.85823727 0.28329079]
[ 0.41769803 -0.46579915 0.78010224]
]

B -> [-0.54362 -0. -0. ]

=======================================================
M_parent.dot(joint.offset) ->

[-0.43569481 -0.23266371 -0.227069 ]

  Please what is the math of this case ?

By docs :

If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

Any suggestion ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment