Home > Back-end >  Solving linear system of equations containing matrices and vectors
Solving linear system of equations containing matrices and vectors

Time:02-08

I want to solve the following linear system of equations in Python:

enter image description here

with

A is a matrix of size 3 x 3

b is a vector of ones of length 3

b' stands for the transpose of b

c is a vector of zeros of length 3

d is a vector of length 3

My Python code is as follows:

A = np.array(([1,2,3], [1,2,3], [1,2,3])) 
b = np.ones(3)
c = np.zeros(3)
d = np.array([4,5,6]) 

matrix1 = np.array(([A,b], [b.T, c]))
matrix2 = np.array([d, b])
        
[alpha, beta] = np.linalg.solve(matrix1, matrix2)

I am obtaining the error for matrix 1: could not broadcast input array from shape (3,3) into shape (3).

Any help will be very appreciated!

CodePudding user response:

In [2]: A = np.array(([1,2,3], [1,2,3], [1,2,3]))
   ...: b = np.ones(3)
   ...: c = np.zeros(3)
   ...: d = np.array([4,5,6])

Look at b and b.T. See any difference? A (3,) shape has been changed to (3,) shape. Surprised? Then you haven't taken time to read the np.transpose docs.

In [3]: b
Out[3]: array([1., 1., 1.])
In [4]: b.T
Out[4]: array([1., 1., 1.])

So lets try to make your array:

In [5]: np.array(([A,b], [b.T, c]))
<ipython-input-5-45ec84398f1d>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  np.array(([A,b], [b.T, c]))
Traceback (most recent call last):
  File "<ipython-input-5-45ec84398f1d>", line 1, in <module>
    np.array(([A,b], [b.T, c]))
ValueError: could not broadcast input array from shape (3,3) into shape (3,)

Or how about just the first part:

In [6]: np.array([A,b])
<ipython-input-6-dff0caaab877>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  np.array([A,b])
Traceback (most recent call last):
  File "<ipython-input-6-dff0caaab877>", line 1, in <module>
    np.array([A,b])
ValueError: could not broadcast input array from shape (3,3) into shape (3,)

Sometimes np.array(...) with inconsistent shaped inputs produces an object dtype array; other times, such as this, it raises an error.

The basic point is, we can't make a new array by simply combining a (3,3) and (3,).

We could make b into a (3,1) shape, and concatenate that with A:

In [8]: b[:,None]
Out[8]: 
array([[1.],
       [1.],
       [1.]])
In [9]: np.hstack((A,b[:,None]))
Out[9]: 
array([[1., 2., 3., 1.],
       [1., 2., 3., 1.],
       [1., 2., 3., 1.]])

We could try the same with the 2nd row

In [11]: np.hstack((b,c))
Out[11]: array([1., 1., 1., 0., 0., 0.])

but this joins a (3,) and (3,) to make a (6,) (surprised? Do the math! ). But joining a (3,) with (1,) produces a (4,)

In [12]: np.hstack((b,[0]))
Out[12]: array([1., 1., 1., 0.])

That in turn can be joined to the (3,4) to produce a (4,4):

In [14]: np.vstack((np.hstack((A,b[:,None])), np.hstack((b,[0]))))
Out[14]: 
array([[1., 2., 3., 1.],
       [1., 2., 3., 1.],
       [1., 2., 3., 1.],
       [1., 1., 1., 0.]])

There may be more streamlined ways of constructing this array, but I'm going through all the details because you don't have much a understanding yet of numpy array dimensions.

Don't casually import terms like matrix and vector into numpy. numpy has arrays, which may be 1d, 2d or more (even 0d). They aren't exactly like school book linear algebra objects.

  •  Tags:  
  • Related