Discussion:
Matrix: finding x in A.x=b
Mansur Marvanov
2007-11-21 22:29:28 UTC
Permalink
Hello!

Is there any way to find vector x in A.x=b simply?

I have:
(%i105) A:matrix([1,2],[3,4])$
(%i107) b:matrix([1],[2])$

(%i115) solve(A.x=b,[x1,x2]);
(%o115) []
^^^ Doesn't work:(

How can I get vector x?

There is function lsolve(A,b) in MathCAD. Is there the same in Maxima?
--
Mansur Marvanov <***@gmail.com>
Peter Danenberg
2007-11-21 23:25:01 UTC
Permalink
> Is there any way to find vector x in A.x=b simply?

What about something like this:

(%i10) load(linearalgebra)$
(%i11) A:matrix([1,2],[3,4])$
(%i12) b:matrix([1],[2])$
(%i13) lu_backsub(lu_factor(A, floatfield), b);
Barton Willis
2007-11-22 00:32:12 UTC
Permalink
This method only works for square matrices :( For square matrices,
you can try linsolve_by_lu. Example:

(%i1) linsolve_by_lu(matrix([1,2],[3,4]), matrix([1],[2]));
(%o1) [matrix([0],[1/2]),false]

For rational numbers, the second argument is false; for floats the
second argument is an estimate of the condition number. Multiple
right-hand-sides are OK

(%i2) linsolve_by_lu(matrix([1,2],[3,4]), matrix([1,0],[0,1]));
(%o2) [matrix([-2,1],[3/2,-1/2]),false]

And a double-float example

(%i3) linsolve_by_lu(matrix([1,2],[3,4]),
matrix([1,0],[0,1]),'floatfield);
(%o3) [matrix([-2.0,1.0],[1.5,-0.5]),21.77777777777778]

And, for fun:

(%i1) f : lambda([i,j], random(10))$
(%i2) m2(m,n) := genmatrix(f,m,n)$

(%i3) m1 : matrix([m2(2,2),m2(2,2)],[m2(2,2), m2(2,2)]);
(%o3)
matrix([matrix([2,9],[8,0]),matrix([4,5],[7,3])],[matrix([3,9],[5,5]),matrix([2,5],[1,0])])


(%i4) b : identfor(m1);
(%o4)
matrix([matrix([1,0],[0,1]),matrix([0,0],[0,0])],[matrix([0,0],[0,0]),matrix([1,0],[0,1])])


(%i5) matrix_element_mult : "."$
(%i6) m1_inv : first(linsolve_by_lu(m1,b, 'noncommutingring));

(%o6) matrix([matrix([-43/188,25/376],[57/376,-55
/752]),matrix([71/376,27/376],[-81/752,91/752])],
[matrix([145/376,25/752],[-109
/376,59/752]),matrix([-305/752,27/752],[333/752,-207/752])])

(%i7) m1.m1_inv;
(%o7)
matrix([matrix([1,0],[0,1]),matrix([0,0],[0,0])],[matrix([0,0],[0,0]),matrix([1,0],[0,1])])



BW

-----maxima-***@math.utexas.edu wrote: -----

>To: ***@math.utexas.edu
>From: Peter Danenberg <***@wikitex.org>
>Sent by: maxima-***@math.utexas.edu
>Date: 11/21/2007 05:25PM
>Subject: Re: [Maxima] Matrix: finding x in A.x=b
>
>> Is there any way to find vector x in A.x=b simply?
>
>What about something like this:
>
> (%i10) load(linearalgebra)$
> (%i11) A:matrix([1,2],[3,4])$
> (%i12) b:matrix([1],[2])$
> (%i13) lu_backsub(lu_factor(A, floatfield), b);
>_______________________________________________
>Maxima mailing list
>***@math.utexas.edu
>http://www.math.utexas.edu/mailman/listinfo/maxima
Paul Smith
2007-11-21 23:24:00 UTC
Permalink
On Nov 21, 2007 10:29 PM, Mansur Marvanov <***@gmail.com> wrote:
> Is there any way to find vector x in A.x=b simply?
>
> I have:
> (%i105) A:matrix([1,2],[3,4])$
> (%i107) b:matrix([1],[2])$
>
> (%i115) solve(A.x=b,[x1,x2]);
> (%o115) []
> ^^^ Doesn't work:(
>
> How can I get vector x?
>
> There is function lsolve(A,b) in MathCAD. Is there the same in Maxima?

A possible way:

(%i1) A:matrix([1,2],[3,4])$

(%i2) b:matrix([1],[2])$

(%i3) invert(A).b;
[ 0 ]
[ ]
(%o3) [ 1 ]
[ - ]
[ 2 ]
(%i4)

Paul
Mansur Marvanov
2007-11-23 07:36:54 UTC
Permalink
I'm sorry, I forget mention, that I need to solve A.x=b by Gauss method.
LU-method - it's another method with what I should work:)

invert(A).b; <<< it's work, but is it close to Gauss?

On Thu, 2007-11-22 at 12:00 -0600, maxima-***@math.utexas.edu wrote:
> Message: 3
> Date: Wed, 21 Nov 2007 23:24:00 +0000
> From: "Paul Smith" <***@gmail.com>
> Subject: Re: [Maxima] Matrix: finding x in A.x=b
> To: ***@math.utexas.edu
> Message-ID:
> <***@mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On Nov 21, 2007 10:29 PM, Mansur Marvanov <***@gmail.com> wrote:
> > Is there any way to find vector x in A.x=b simply?
> >
> > I have:
> > (%i105) A:matrix([1,2],[3,4])$
> > (%i107) b:matrix([1],[2])$
> >
> > (%i115) solve(A.x=b,[x1,x2]);
> > (%o115) []
> > ^^^ Doesn't work:(
> >
> > How can I get vector x?
> >
> > There is function lsolve(A,b) in MathCAD. Is there the same in Maxima?
>
> A possible way:
>
> (%i1) A:matrix([1,2],[3,4])$
>
> (%i2) b:matrix([1],[2])$
>
> (%i3) invert(A).b;
> [ 0 ]
> [ ]
> (%o3) [ 1 ]
> [ - ]
> [ 2 ]
> (%i4)
>
> Paul
>
>
> ------------------------------
>
> Message: 4
> Date: Wed, 21 Nov 2007 17:25:01 -0600
> From: Peter Danenberg <***@wikitex.org>
> Subject: Re: [Maxima] Matrix: finding x in A.x=b
> To: ***@math.utexas.edu
> Message-ID: <***@klutometis.wikitex.org>
> Content-Type: text/plain; charset=us-ascii
>
> > Is there any way to find vector x in A.x=b simply?
>
> What about something like this:
>
> (%i10) load(linearalgebra)$
> (%i11) A:matrix([1,2],[3,4])$
> (%i12) b:matrix([1],[2])$
> (%i13) lu_backsub(lu_factor(A, floatfield), b);
>
>
> ------------------------------
>
> Message: 6
> Date: Wed, 21 Nov 2007 18:32:12 -0600
> From: Barton Willis <***@unk.edu>
> Subject: Re: [Maxima] Matrix: finding x in A.x=b
> To: Peter Danenberg <***@wikitex.org>
> Cc: ***@math.utexas.edu
> Message-ID:
> <OFD88101A8.880719B8-ON8625739B.0002F2CB-***@unk.edu>
> Content-Type: text/plain; charset=US-ASCII
>
> This method only works for square matrices :( For square matrices,
> you can try linsolve_by_lu. Example:
>
> (%i1) linsolve_by_lu(matrix([1,2],[3,4]), matrix([1],[2]));
> (%o1) [matrix([0],[1/2]),false]
>
> For rational numbers, the second argument is false; for floats the
> second argument is an estimate of the condition number. Multiple
> right-hand-sides are OK
>
> (%i2) linsolve_by_lu(matrix([1,2],[3,4]), matrix([1,0],[0,1]));
> (%o2) [matrix([-2,1],[3/2,-1/2]),false]
>
> And a double-float example
>
> (%i3) linsolve_by_lu(matrix([1,2],[3,4]),
> matrix([1,0],[0,1]),'floatfield);
> (%o3) [matrix([-2.0,1.0],[1.5,-0.5]),21.77777777777778]
>
> And, for fun:
>
> (%i1) f : lambda([i,j], random(10))$
> (%i2) m2(m,n) := genmatrix(f,m,n)$
>
> (%i3) m1 : matrix([m2(2,2),m2(2,2)],[m2(2,2), m2(2,2)]);
> (%o3)
> matrix([matrix([2,9],[8,0]),matrix([4,5],[7,3])],[matrix([3,9],[5,5]),matrix([2,5],[1,0])])
>
>
> (%i4) b : identfor(m1);
> (%o4)
> matrix([matrix([1,0],[0,1]),matrix([0,0],[0,0])],[matrix([0,0],[0,0]),matrix([1,0],[0,1])])
>
>
> (%i5) matrix_element_mult : "."$
> (%i6) m1_inv : first(linsolve_by_lu(m1,b, 'noncommutingring));
>
> (%o6) matrix([matrix([-43/188,25/376],[57/376,-55
> /752]),matrix([71/376,27/376],[-81/752,91/752])],
> [matrix([145/376,25/752],[-109
> /376,59/752]),matrix([-305/752,27/752],[333/752,-207/752])])
>
> (%i7) m1.m1_inv;
> (%o7)
> matrix([matrix([1,0],[0,1]),matrix([0,0],[0,0])],[matrix([0,0],[0,0]),matrix([1,0],[0,1])])
>
>
>
> BW
>
> -----maxima-***@math.utexas.edu wrote: -----
>
> >To: ***@math.utexas.edu
> >From: Peter Danenberg <***@wikitex.org>
> >Sent by: maxima-***@math.utexas.edu
> >Date: 11/21/2007 05:25PM
> >Subject: Re: [Maxima] Matrix: finding x in A.x=b
> >
> >> Is there any way to find vector x in A.x=b simply?
> >
> >What about something like this:
> >
> > (%i10) load(linearalgebra)$
> > (%i11) A:matrix([1,2],[3,4])$
> > (%i12) b:matrix([1],[2])$
> > (%i13) lu_backsub(lu_factor(A, floatfield), b);
> >_______________________________________________
> >Maxima mailing list
> >***@math.utexas.edu
> >http://www.math.utexas.edu/mailman/listinfo/maxima
>
>
> Maxima mailing list
> ***@math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
>
> End of Maxima Digest, Vol 16, Issue 35
> **************************************
Alexey Beshenov
2007-11-24 00:12:33 UTC
Permalink
On Friday 23 November 2007 10:36, Mansur Marvanov wrote:

> I'm sorry, I forget mention, that I need to solve A.x=b by Gauss method.

> invert(A).b; <<< it's work, but is it close to Gauss?

As for Maxima, invert(A) uses the adjoint method. Gaussian methods involve
elimination scheme (partial---in classic method with substitution---or
full---in Gauss-Jordan algorithm), which is not the case of using matrix
inverse explicitly.

> LU-method - it's another method with what I should work:)

LU decomposition is more convenient and useful than Gaussian methods, so it's
not a big fault for Maxima if there are no implementation of classic
elimination algorithms.

It's better to write your own code if you need to use Gaussian methods in
educational works.

--
Alexey Beshenov <***@beshenov.ru>
http://beshenov.ru/
Continue reading on narkive:
Loading...