0

I am trying to solve a matrix equation such as A.B = C. The A is the unknown matrix and i must find it. I have B(n*1) and C(n*1), so A must be n*n.

I used the BT* A.T =C.T method (numpy.linalg.solve(B.T, C.T)). But it produces an error:

LinAlgError: Last 2 dimensions of the array must be square.

So the problem is that B isn't square.

Rabih Assaf
  • 95
  • 1
  • 1
  • 8
  • 1
    are both `A` and `B` vectors with one column? If so, then there's no possible solution (you need more variables, specifcally you need `A` to be a square matrix). – Barranka Sep 01 '16 at 21:35
  • 3
    You have two `n`-dimensional vectors and you're trying to solve for an `n`-by-`n` matrix? Unless `n` is 1 or `A` is the zero vector, that's underspecified and there are infinitely many correct answers. – Tanner Swett Sep 01 '16 at 21:37
  • Sorry, i changed the question to be more correct.@Barranka @Tanner Swett – Rabih Assaf Sep 02 '16 at 11:03
  • The "problem" is that your equation has infinitely many solutions. Do you just want an arbitrary solution or do you need a *good* solution? What are you trying to accomplish by solving this equation? – Tanner Swett Sep 02 '16 at 13:33

2 Answers2

0

Here's a little example for you:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
x = np.linalg.solve(a, b)

print "A={0}".format(a)
print "B={0}".format(b)
print "x={0}".format(x)

For more information, please read the docs

BPL
  • 9,632
  • 9
  • 59
  • 117
0

If you're solving for the matrix, there is an infinite number of solutions (assuming that B is nonzero). Here's one of the possible solutions:

Choose an nonzero element of B, Bi. Now construct a matrix A such that the ith column is C / Bi, and the other columns are zero.

It should be easy to verify that multiplying this matrix by B gives C.

interjay
  • 107,303
  • 21
  • 270
  • 254