Matrix in Python using matplotlib and mplot3d

1

Hello,

The code below has the following error:

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

Does anyone know a solution?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import itertools
A = np.matrix([[100.0,100.0,100.0],[0.0,0.0 ,64.3],[0.0 ,0.0,57.1]])
b = np.matrix([[100.0], [100.0],[100]])  # we will use the convention that a vector is a column vector
c = 0.0

def f(x, A, b, c):
    return float(0.5 * x.T * A * x - b.T * x + c)
def bowl(A, b, c):
    fig = plt.figure(figsize=(10,8))
    qf = fig.gca(projection='3d')
    size = 20
    x1 = list(np.linspace(-6, 6, size))
    x2 = list(np.linspace(-6, 6, size))
    x1, x2 = np.meshgrid(x1, x2)
    zs = np.zeros((size, size))
    for i in range(size):
        for j in range(size):
            x = np.matrix([[x1[i,j]], [x2[i,j]]])
            zs[i,j] = f(x, A, b, c)
    qf.plot_surface(x1, x2, zs, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0)
    fig.show()
    return x1, x2, zs
x1, x2, zs = bowl(A, b, c)

def contoursteps(x1, x2, zs, steps=None):
    fig = plt.figure(figsize=(6,6))
    cp = plt.contour(x1, x2, zs, 10)
    plt.clabel(cp, inline=1, fontsize=10)
    if steps is not None:
        steps = np.matrix(steps)
        plt.plot(steps[:,0], steps[:,1], '-o')
    fig.show()
contoursteps(x1, x2, zs)
x = np.matrix([[-2.0],[-2.0]])
steps = [(-2.0, -2.0)]
i = 0
imax = 10
eps = 0.01
r = b - A * x
delta = r.T * r
delta0 = delta
while i < imax and delta > eps**2 * delta0:
    alpha = float(delta / (r.T * (A * r)))
    x = x + alpha * r
    steps.append((x[0,0], x[1,0]))  # store steps for future drawing
    r = b - A * x
    delta = r.T * r
    i += 1
contoursteps(x1, x2, zs, steps)
x = np.matrix([[-2.0],[-2.0]])
steps = [(-2.0, -2.0)]
i = 0
imax = 10
eps = 0.01
print ('b:\n', b)
print ('A:\n', A)
print ('x:\n', x)
r = b - A * x
print ('r:\n', r)
contoursteps(x1, x2, zs, None)
plt.plot([0, r[0, 0] * 0.5], [0, r[1, 0] * 0.5], 'g')
plt.show()
print ('||r||^2 =', np.linalg.norm(r)**2)
delta = r.T * r
print ('r.T * r = ',  delta)
delta0 = delta

ValueError: shapes (1,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)
    
asked by anonymous 12.10.2016 / 18:17

1 answer

1

At 3 points, the script declares the array x with shape (2, 1), but uses this array in multiplication operations with shape arrays. different (incompatible, for array multiplication operation).

An example of where this error occurs in the f() function:

def f(x, A, b, c):
    return float(0.5 * x.T * A * x - b.T * x + c)
                           ^--aqui


Just as an example, if you change the 3 points of the script and add one more element to the x array, the script will work:

...
x = np.matrix([ [x1[i,j]], [x2[i,j]], [0.0] ])
...
x = np.matrix([[-2.0],[-2.0], [0.0]])
...
x = np.matrix([[-2.0],[-2.0], [0.0]])
...


Important : I have set the value 0.0 just to work , but you need to check the algorithm description, what should be the correct value to initialize the array x .

The script with the 0.0s above produces the following images:

tested with: Python 3.5.2 | Anaconda custom (64-bit)

    
12.10.2016 / 19:24