Am Dienstag, 8. Mai 2012 10:44:04 UTC+2 schrieb smichr:
>
> I don't know how best to ask this question. If I have a equation with more 
> unknowns that knowns and want to know the possible solutions and get 
> something like this,
>
> >>> solve(a*x+a+b*y-c-d, a, b)
> [(0, (c + d)/y), (c/(x + 1), d/y), (d/(x + 1), c/y), ((c + d)/(x + 1), 0)]
>

In this case, you have an underdetermined linear system, so you can solve 
this problem using linear algebra.

Suppose you want to solve 

ax + a + by = z = c + d 

for a and b. Written in matrix notation:

>>> A = Matrix([[x+1, y]])
>>> b = Matrix([z])

We want to find x, such that A*x == b.

Using Gaußian elimination, we obtain the reduced row echelon form of (A|b):

>>> A.row_join(b).rref()[0]
⎡     y      z  ⎤
⎢1  ─────  ─────⎥
⎣   x + 1  x + 1⎦

(Which is trivial in this case.)

Now we make the A part quadratic by inserting rows of the form (0...0, -1, 
0...0) such that only 1 and -1 are on the diagonal:

>>> _.col_join(Matrix([[0, -1, 0]]))
⎡     y      z  ⎤
⎢1  ─────  ─────⎥
⎢   x + 1  x + 1⎥
⎢               ⎥
⎣0   -1      0  ⎦

This is all we need to obtain the solution space: The last column is a 
special solution to the inhomogeneous case (A*x == b). The linear 
combinations of columns which contain a -1 we added are all possible 
solutions to the homogeneous case (A*x == 0). So our solution is just the 
sum of the special inhomogeneous solution and the general homogeneous 
solution:

x = alpha * Matrix([y/(x + 1), -1]) + Matrix([z/(x +1), 0])

where alpha is any real number. (In this case our solution is a 
1-dimensional vector space, in general it can have a higher dimension of 
course.)

Please note that you can obtain the homogeneous solution directly using 
A.nullspace(). I don't know whether there is already some function for the 
inhomogeneous solution. 
 

> Is there some significance of these 4 solutions? Are these solutions 
> analogous to the case of `solve(a + b, a)` giving a = -b? The problem that 
> I see is that if you write 
>

I get something else:

>>> solve(a*x + a + b*y - c - d, a, b)
⎡⎧   -b⋅y + c + d⎫⎤
⎢⎨a: ────────────⎬⎥
⎣⎩      x + 1    ⎭⎦

It does not really make sense to me to return exactly 4 solutions. However, 
you can use two special solutions to get the general one (constructing a 
line):

>>> x1 = Matrix([0, z/y])
>>> x2 = Matrix([z/(x + 1), 0])
>>> x2 - x1
⎡  z  ⎤
⎢─────⎥
⎢x + 1⎥
⎢     ⎥
⎢ -z  ⎥
⎢ ──  ⎥
⎣ y   ⎦
>>> _ / (z/y)
⎡  y  ⎤
⎢─────⎥
⎢x + 1⎥
⎢     ⎥
⎣ -1  ⎦

As you can see, alpha * (x2 - x1) + x2 is exactly the same result as above.
 

> >>> solve(a*x+a+b*y-4, a, b)
> [(0, 4/y), (4/(x + 1), 0)]
>
> Those are only 2 of an infinite number of solutions since there are an 
> infinite number of ways to partition up the 4. If we partition 4 as c + d 
> (which then gives a solution that we obtained above) then substituting in 
> different values of c and d gives a variety of solutions:
>
> >>> ans=solve(a*x+a+b*y-c-d,a, b)
> >>> [Tuple(*ai).subs({c:1,d:3}) for ai in ans]
> [(0, 4/y), (1/(x + 1), 3/y), (3/(x + 1), 1/y), (4/(x + 1), 0)]
> >>> [Tuple(*ai).subs({c:2,d:2}) for ai in ans]
> [(0, 4/y), (2/(x + 1), 2/y), (2/(x + 1), 2/y), (4/(x + 1), 0)]
> >>> [Tuple(*ai).subs({c:4,d:0}) for ai in ans]
> [(0, 4/y), (4/(x + 1), 0), (0, 4/y), (4/(x + 1), 0)]
>
> (Solving this type of equation arose in doing the 
> multinomial_coefficients-without-expansion problem.)
>

If you always get linear systems, it is doable using the algorithm I 
described above.

Vinzent

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sympy/-/_53Xd505Xn8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to