1
votes

Here's my code:

def obj_function(x):
    val= 8*(x[0]**4) + 3*(x[1]**2)- 6*x[0]*x[1] + 2*x[1]
    return np.asmatrix(val)

def obj_function_prime(x):
    val=[32*(x[0]**3)-6*x[1], 6*x[1]-6*x[0]+2]
    return np.asmatrix(val)

def hess_obj_function(x):
    a= 96*(x[0]**2)
    b= -6
    c= -6
    d=  6
    val=[[a,b], [c,d]]
    return np.asmatrix(val)

tol= 10**(-6)
mu= 0.2
xk= np.asmatrix([1,1])
xold= np.asmatrix([0,0])

while np.linalg.norm(obj_function_prime(xk))> tol:
    xold=xk
    alpha=1.0
    while obj_function(xk- alpha*obj_function_prime(xk))> (obj_function(xk)- mu*alpha*np.dot(obj_function_prime(xk), obj_function(xk))):
        alpha=0.5* alpha
    xk= xk-alpha*obj_function_prime(xk)
    
print(" The minimum of the function occurs at", xk)

And this gives me error:

*

LinAlgError Traceback (most recent call last) in 20 xold= np.matrix([0,0]) 21 ---> 22 while np.linalg.norm(obj_function_prime(xk))> tol: 23 xold=xk 24 alpha=1.0

<ipython-input-3-c0704e734bdd> in obj_function_prime(x)
      4 
      5 def obj_function_prime(x):
----> 6     val=[32*(x[0]**3)-6*x[1], 6*x[1]-6*x[0]+2]
      7     return np.matrix(val)
      8 

~\anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in __pow__(self, other)
    231 
    232     def __pow__(self, other):
--> 233         return matrix_power(self, other)
    234 
    235     def __ipow__(self, other):

<__array_function__ internals> in matrix_power(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in matrix_power(a, n)
    620     a = asanyarray(a)
    621     _assert_stacked_2d(a)
--> 622     _assert_stacked_square(a)
    623 
    624     try:

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in _assert_stacked_square(*arrays)
    211         m, n = a.shape[-2:]
    212         if m != n:
--> 213             raise LinAlgError('Last 2 dimensions of the array must be square')
    214 
    215 def _assert_finite(*arrays):

LinAlgError: Last 2 dimensions of the array must be square

What does this error mean, and how can I resolve it? How do I convert a matrix to a square one, when it is not defined that way?

1
It means that the last two dimensions must be the same. A full traceback could help identify exactly where the problem is. Also, I'd suggest getting rid of the asmatrix calls. Try to make this work with regular numpy arrays (they can be 2d like np.matrix). - hpaulj
@hpaulj I have edited the question and added the stack trace. Please check, thanks! - huy
What is x[0]**3 supposed to be doing? What do you think x[0] is (shape, type, dtype?). Does that match with what it really is? - hpaulj

1 Answers

-1
votes

Using np.matrix is causing problems.

Lets make a small matrix:

In [273]: x = np.asmatrix([0,0])
In [274]: x
Out[274]: matrix([[0, 0]])

note that when you index it, it doesn't change. It selects the first 'row', but being np.matrix is still 2d.

In [275]: x[0]
Out[275]: matrix([[0, 0]])

And ** of np.matrix is matrix square, effectively x@x.

In [276]: x[0]**2
Traceback (most recent call last):
  File "<ipython-input-276-3908b7334115>", line 1, in <module>
    x[0]**2
  File "/usr/local/lib/python3.8/dist-packages/numpy/matrixlib/defmatrix.py", line 231, in __pow__
    return matrix_power(self, other)
  File "<__array_function__ internals>", line 5, in matrix_power
  File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 621, in matrix_power
    _assert_stacked_square(a)
  File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 204, in _assert_stacked_square
    raise LinAlgError('Last 2 dimensions of the array must be square')
LinAlgError: Last 2 dimensions of the array must be square

I'm not going try to figure out what you are trying to do, but clearly using asmatrix is not the way to go - at least not without understanding how such an array behaves.