Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my matrix vector multiplication in NumPy yield a two dimensional array instead of a one dimensional vector?

I have a matrix called inverseJ, which is a 2x2 matrix ([[0.07908312, 0.03071918], [-0.12699082, -0.0296126]]), and a one dimensional vector deltaT of length two ([-31.44630082, -16.9922145]). In NumPy, multiplying these should yield a one dimensional vector again, as in this example. However, when I multiply these using inverseJ.dot(deltaT), I get a two dimensional array ([[-3.00885838, 4.49657509]]) with the only element being the vector I am actually looking for. Does anyone know why I am not simply getting a vector? Any help is greatly appreciated!

Whole script for reference

from __future__ import division
import sys
import io
import os
from math import *
import numpy as np

if __name__ == "__main__":
    # Fingertip position
    x = float(sys.argv[1])
    y = float(sys.argv[2])
    # Initial guesses
    q = np.array([0., 0.])
    q[0] = float(sys.argv[3])
    q[1] = float(sys.argv[4])

    error = 0.01
    while(error > 0.001):
        # Configuration matrix
        T = np.array([17.3*cos(q[0] + (5/3)*q[1])+25.7*cos(q[0] + q[1])+41.4*cos(q[0]),
                        17.3*sin(q[0] + (5/3)*q[1])+25.7*sin(q[0] + q[1])+41.4*sin(q[0])])

        # Deviation
        deltaT = np.subtract(np.array([x,y]), T)
        error = deltaT[0]**2 + deltaT[1]**2
        # Jacobian
        J = np.matrix([ [-25.7*sin(q[0]+q[1])-17.3*sin(q[0]+(5/3)*q[1])-41.4*sin(q[0]), -25.7*sin(q[0]+q[1])-28.8333*sin(q[0]+(5/3)*q[1])],
                        [25.7*cos(q[0]+q[1])+17.3*cos(q[0]+(5/3)*q[1])+41.4*cos(q[0]),      25.7*cos(q[0]+q[1])+28.8333*cos(q[0]+(5/3)*q[1])]])
        #Inverse of the Jacobian
        det = J.item((0,0))*J.item((1,1)) - J.item((0,1))*J.item((1,0))
        inverseJ = 1/det * np.matrix([  [J.item((1,1)),     -J.item((0,1))],
                                        [-J.item((1,0)),    J.item((0,0))]])
        ### THE PROBLEMATIC MATRIX VECTOR MULTIPLICATION IN QUESTION
        q = q + inverseJ.dot(deltaT)

1 Answers

When a matrix is involved in an operation, the output is another matrix. matrix object are matrices in the strict linear algebra sense. They are always 2D, even if they have only one element.

On the contrary, the example you mention uses arrays, not matrices. Arrays are more "loosely behaved". One of the differences is that "useless" dimensions are removed, yielding a 1D vector in this example.

like image 93
blue_note Avatar answered Jan 20 '26 05:01

blue_note



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!