I was wondering whether there exist any packages or other pre-built solutions for R that are capable of translating sets of linear equations into matrix form (e.g. for solution via the Gauss Seidel algorithm), similar to the equationsToMatrix(eqns,vars)
function in Matlab?
An example from Matlab:
[A, b] = equationsToMatrix([x - y == 0, x + 2*y == 3, [x, y])
A =
[ 1, -1]
[ 1, 2]
b =
0
3
Suggestions for building blocks would be very helpful, too.
1) This is not exactly what you are asking for but maybe it will help anyways:
giving:
2) If we know these are linear equations exactly of the form shown in the question then try this. The two
strapply
calls perform matches of the regular expression against the components ofargs
, capture the strings matched by the portions of the regular expressions within parentheses and call the function specified as the third argument with those captured strings as arguments. We combine thestrapply
outputs usingrbind.fill
and replace any NAs it generates with zero.giving:
Update: Generalized so that now not all variables need to be in each equation and also variables can be in different orders in different equations.