9

Solving an equation symbolically can be achieved in R using the Ryacas library. For example

library(Ryacas)
yacas("Solve(x/(1+x) == a, x)")

gives

expression(list(x == a/(1 - a)))

Does anybody know how to (symbolically) solve a system of equations?

Thank you.

Brani
  • 6,454
  • 15
  • 46
  • 49

2 Answers2

11

Well, i use the excellent python library, sympy, for symbolic computation.

Using sympy, solving systems of equations straightforward:

>>> from sympy import *
>>> x,y = symbols('x y')
>>> solve([Eq(x + 5*y, 2), Eq(-3*x + 6*y, 15)], [x, y])
{y: 1, x: -3}

So that's how to solve a system of equations using symbolic algebra, except through a python package.

The good news is that there's an R port to sympy, called rsympy, which is available on CRAN, or Google Code, here.

I have never used rsympy, other than downloading/installing it and working through a couple of the simplest examples in the rsympy Manual. I have used the original python library a lot during the past three years and i can recommend it highly.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
doug
  • 69,080
  • 24
  • 165
  • 199
3

Try this:

yacas( "OldSolve({a*x+y==0,x+z==0},{x,y})" )
Artem Klevtsov
  • 9,193
  • 6
  • 52
  • 57
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • do you have an idea about the following? If I run `yacas( "OldSolve({x+5*y==2,-3*x+6*y==15},{x,y})" )` then I get `{{x==2-5*y,y==1}};` which is fine, but I do not know why *x* was not computed to get -3 at the end. Is it possible to make R compute exact results? – daroczig Jan 23 '11 at 15:50
  • I don't have a device with Yacas installed with me at the moment to test my solution, but after looking at the docs I would have tried: `yacas( "OldSolve({a*x+y==0,x+z==0}, x)" )` – IRTFM Jan 23 '11 at 22:15