3

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)
EmielBoss
  • 133
  • 7
  • 22

2 Answers2

3

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.

blue_note
  • 22,656
  • 6
  • 48
  • 67
  • Thanks for the clarification! But then what is the benefit of defining matrices over two dimensional arrays? – EmielBoss Oct 24 '17 at 14:13
  • @EmielBoss: Mathematical strictness, *if* you need it. Many algorithms are defined in terms of *1Xn* vectors, for example. In that case, you want to somehow ensure that your size doesn't change depending on the values. However, for most people in most cases, arrays is easier to use. – blue_note Oct 24 '17 at 14:18
  • @EmielBoss have a look at [numpy.matrix vs 2D numpy.ndarray](https://docs.scipy.org/doc/scipy/reference/tutorial/linalg.html). I agree with you the translation from a lin algebra textbook to numpy is not always perfect, this is one case I would say. – Brad Solomon Oct 24 '17 at 14:25
0

This simply seems to be the way numpy.dot() functions. It does a simple array multiplication which, since one of the parameters is two dimensional, returns a two dimensional array. dot() is not a smart method, it just does what it's told without sanity checks from what I can gather in the documentation here. Note that this is not an error in your code, but you will have to extract the inner list yourself.

Alex Eggers
  • 188
  • 1
  • 14
  • Extracting the inner list is indeed an option, but I am still wondering why it behaves like this in my specific case, while it normally returns a vector just fine. – EmielBoss Oct 24 '17 at 14:06