I know the function solve
can solve Ax=b
. But I want a function to solve xA=b for x?
Is there some function available?
By the way It should work like mrdivide of Matlab:
x = B/A solves the system of linear equations x*A = B for x. The matrices A and B must contain the same number of columns. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.
If A is a scalar, then B/A is equivalent to B./A.
If A is a square n-by-n matrix and B is a matrix with n columns, then x = B/A is a solution to the equation x*A = B, if it exists.
If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with n columns, then x = B/A returns a least-squares solution of the system of equations x*A = B.
Method using matrix inversion:
If xA = b, so then x * A * inv(A) = b * inv(A) <=> x = b * inv(A) <=>, so in opencv you would have:
void mrdivide(const cv::Mat &A, const cv::Mat &b, cv::Mat &x) {
x = b * A.inv();
}
However, this is not recommended due to high cost of matrix inversion and from numerical methodology point of view, loss of accuracy. Furthermore, it can solve only system of linear equations which is not overdefined and system matrix A must be invertible.
Methods using matrix transposition:
Since we have xA = b, so (xA)^T = b^T <=> A^T * x^T = b^T, so we can use cv::solve(A.t(), b.t(), x), and x.t() is an result:
void mrdivide(const cv::Mat &A, const cv::Mat &b, cv::Mat &x) {
cv::solve(A.t(), b.t(), x);
cv::transpose(x,x);
}
This is general and recommended solution. It provides all functionality that solve() does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With