MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Matrices with Mathematica 5.1

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63328] Re: [mg63315] Matrices with Mathematica 5.1
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sat, 24 Dec 2005 07:18:54 -0500 (EST)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200512231008.FAA25936@smc.vnet.net>
  • Reply-to: murray at math.umass.edu
  • Sender: owner-wri-mathgroup at wolfram.com

If you want to maintain your entire 4-by-6 matrix intact, you need to 
write the matrix using Mathematica notation -- as a list of lists, using 
{ and }, where the several lists are the rows of your matrix:

   A = {{1, 7, -2, 0, -8, -3}, {0, 0, 1, 1, 6, 5},
         {0,  0, 0, 1, 3, 9}, {0, 0, 0, 0, 0, 0}}

(If you don't like to type all those braces, you may use the entry 
Create Matrix/Table/Palette on the Input menu to provide a framework for 
typing the individual entries in a 2-dimensional grid.

But, as the documentation for LinearSolve says, you need to use the form 
LinearSolve[mat,vec].  So you need to extract the first 5 columns to get 
the matrix, and the last column to get the "constants".

Perhaps the quickest way to do that is to thing of applying the 
appropriate function on each row, e.g.:

   Most[{1, 7, -2, 0, -8, -3}]
{1, 7, -2, 0, -8}

   Last[{1, 7, -2, 0, -8, -3}]
-3

Now to do the same thing to all rows at once, use Map:

   Map[Most, A]
{{1, 7, -2, 0, -8}, {0, 0, 1, 1, 6}, {0, 0, 0, 1, 3}, {0, 0, 0, 0, 0}}
   Map[Last, A]
{-3, 5, 9, 0}

Now you can either do those operations separately, assigning the results 
to two new names, e.g.,

   coefficients = Map[Most, A]
   constants = Map[Last, A]

and then just evaluate:

   LinearSolve[coefficients, constants]
{-11, 0, -4, 9, 0}

Or, probably, you would prefer to do the whole thing together, in one 
step.  If so, to avoid distracting nested brackets, it's probably better 
to use the ... /@... input form for the Map function, that is,

   Most /@ A
   Last /@ A

which mean the same thing as Map[Most, A] and Map[Last, A].  Then the 
whole thing, after you have your A, is done in one simple step as:

   LinearSolve[Most /@ A, Last /@ A]
{-11, 0, -4, 9, 0}

There are other, more awkward, means to extract all but the first column 
and to extract the last column.  For example:

1. Keeping, still, with a "functional" approach in which you use Map, or 
its abbreviation /@ , to do the same operation to all rows in parallel, 
use the pure functions Drop[#, -1] & and Take[#, -1]& with Map to get 
the two arguments to LinearSolve.

2. Use indexing to get the coefficients and the constants (then use the 
results as the two arguments to LinearSolve).  If you are coming from a 
more traditional and less expressive language than Mathematica, this is 
the version you might at first prefer:

    A[[ All, Range[5] ]]
{{1, 7, -2, 0, -8}, {0, 0, 1, 1, 6}, {0, 0, 0, 1, 3}, {0, 0, 0, 0, 0}}

    A[[All, 6]]
{-3, 5, 9, 0}

In the Mathematica Front End, when typing those double brackets you'd 
probably want to precede and follow each with an Esc character, so that 
then they'd look like the tightly-spaced double brackets that more 
visibly indicate indexing and are not confused with ordinary brackets 
surrounding arguments.




drizkol wrote:
> I would like to solve the system given by:
> 
> [1  7  -2  0  -8  -3]
> [0  0  1  1    6   5]
> [0  0  0  1    3   9]
> [0  0  0  0    0   0]
> 
> Where the matrix is a typical matrix in the form x1+x2+...+xn = b where
> b is the last item in the row.  For example, row 1 could be written as
> x1 + 7x2 - 2x3 -8x4 = -3.  How could I get mathematica to solve this
> matrix?  I understand how to build a matrix, I just need to know the
> operation to run on it.  I tried to use LinearSolve but I did something
> wrong.  Is there a built-in operation to solve these matrices?  If so,
> please tell.  Thanks.
> 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: What we from 1^Infinity, Infinity^0, and similar stuff
  • Next by Date: Re: Replacement equivalence?
  • Previous by thread: Re: Matrices with Mathematica 5.1
  • Next by thread: Re: Matrices with Mathematica 5.1