1

I am doing some basic linear algebra solving, and I'm using np.linalg.solve() to do so. np.linalg.solve() requires that b consist of the right side of the equation, so with an augmented matrix, the augmentation (ahem) needs to be removed. Right now the method I'm using to do that is as follows:

np_exercise1 = np.array([[1,5,7],[-2,-7,-5]])
a = np_exercise1.T[0:2].T
b = np_exercise1.T[2]
solution1 = np.linalg.solve(a,b)
print('x1 = {}\nx2 = {}'.format(solution1[0],solution1[1]))

Is there a more elegant way to do this than than a double-transpose of the matrix?

1 Answer 1

1

You can do multidimensional indexing in numpy, where : selects all the data in a dimension:

>>> np_exercise1[:, 0:2]
array([[ 1,  5],
       [-2, -7]])
>>> np_exercise1[:, 2]
array([ 7, -5])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.