Search code examples
pythonrsolverequivalent

Python equivalent function of solve() in R


What equivalent function can I can in Python that is equivalent to solve() in R?

In R, if I call solve(a, b), it will return me the x as in a*x = b where a is my covariance matrix.


Solution

  • Try np.linalg.solve:

    >>> a = np.array([[1, 2], [3, 5]])
    >>> b = np.array([1, 2])
    >>> x = np.linalg.solve(a, b)
    >>> x
    array([-1.,  1.])
    
    > solve(matrix(c(1,2,3,5), 2, 2, byrow = TRUE), c(1, 2))
    [1] -1  1