# Differences between 'Solve' and 'LinearSolve' [closed]

Using the function 'Solve' I can do the following:

Solve[a x + y == 7 && b x - y == 1, {x, y}]

I get the following output:

I want to get the same output using 'LinearSolve' instead. How is this possible and also what's the difference between the two?

-

## closed as off-topic by Öskå, m_goldberg, ciao, Mike Honeychurch, JensJun 16 '14 at 5:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Öskå, m_goldberg, ciao, Mike Honeychurch, Jens
If this question can be reworded to fit the rules in the help center, please edit the question.

Open up the documentation and check out the differences between the two :) –  Sektor Jun 15 '14 at 11:35
Would you just answer the question properly, or don't answer at all please if you don't know it. –  user152356 Jun 15 '14 at 11:38
@user152356: a kind hint from Sektor does not seem to motivate you to press F1, and learn that the analogous result can be found using LinearSolve[{{a, 1}, {b, -1}}, {7, 1}] –  Wouter Jun 15 '14 at 11:49
It doesn't assign 'x' and 'y' to the result, it just gives a vector of a solution. –  user152356 Jun 15 '14 at 12:05
would you care to elaborate on your motivation? Right now I interpret your question to read, "Solve does what I want it to, but I want LinearSolve to do what Solve does." –  bobthechemist Jun 15 '14 at 12:29

## 1 Answer

s = {x, y} /. Solve[a x + y == 7 && b x - y == 1, {x, y}][[1]]


{8/(a + b), -((a - 7 b)/(a + b))}

lsa = LinearSolve[{{a, 1}, {b, -1}}, {7, 1}]


{8/(a + b), (-a + 7 b)/(a + b)}

f = LinearSolve[{{a, 1}, {b, -1}}];

lsb = f[{7, 1}] // Simplify


{8/(a + b), -((a - 7 b)/(a + b))}

s == lsa == lsb // Simplify


True

Solve can handle a broader class of problems, can accept constraints and has more options available. However, given a problem appropriate for LinearSolve, it should be much faster.

Comparing the timings

n = 200;

coef = RandomInteger[{-20, 20}, {n, n}];

b = RandomInteger[{-20, 20}, n];

var = Array[x, n];

eqns = coef.var == b;

t1 = Timing[sol1 = var /. Solve[eqns, var][[1]];][[1]]


0.355262

t2 = Timing[sol2 = LinearSolve[coef, b];][[1]]


0.177116

{sol1 == sol2, t1/t2}


{True, 2.0058}

LinearSolve is about twice as fast as Solve for this example (200 equations with 200 unknowns).

-