问题
What's the most succinct, fastest, most numerically stable, most R-idiomatic way to do left and right matrix division in R? I understand left division inv(A)*B
is usually done with solve(a,b)
, but how about B*inv(A)
? Is the best way really to compute t(solve(t(A),t(B)))
?
回答1:
I don't have a solution better than B %*% solve(A)
, but I did want to point out that in general solve(A,B)
is faster and more numerically stable than solve(A) %*% B
.
> A = matrix(rnorm(10000),100,100)
> B = matrix(rnorm(10000),100,100)
> microbenchmark(solve(A,B), solve(A) %*% B, t(solve(t(B),t(A))), B %*% solve(A))
Unit: microseconds
expr min lq mean median uq max neval
solve(A, B) 481.695 604.2435 722.2512 677.2455 761.735 1280.888 100
solve(A) %*% B 628.243 830.2095 1056.3947 927.0130 1204.682 5275.030 100
t(solve(t(B), t(A))) 603.855 792.1360 1164.7210 924.0895 1122.184 10351.307 100
B %*% solve(A) 645.119 784.1990 1070.4751 927.9400 1097.601 7866.591 100
回答2:
It is B %*% solve(A)
, because solve(A)
finds the inverse of A
.
来源:https://stackoverflow.com/questions/20116607/r-right-matrix-division