Matrix

Hi guys

Is it relatively simple to create a maplet which allows a user to fill in the entries of a matrix, and then to take the elements of that matrix and put it through a procedure. So to clariffy say i make a window appear with 4 empty boxes, this would correspond to the entries of a matrix.

Scott03's picture

Simple solution

Here is some simple code that may do what you are looking for.  This Maplet will give four boxes for a 2x2 matrix, a button to get the determinant and a box to the left of the button for the result.

This is a simple exampel and can be procedure could be rewritten to be more efficient.

 

Edit: Please see the post below for the corrected worksheet


Scott

Strange results

On my Maple10 (both classic and standard) I get wrong determinants: for the default matrix [[1,-3],[1,2]] it should be 5 but says 4.

The reason: the line with tempMat[2,2] should be

tempMat[2, 2] := Maplets:-Tools:-Get('TB22'::algebraic, corrections);

 

Scott03's picture

My mistake

Oops.  I am sorry that I didn't double check the Get statements before posting.

 Here is the updated worksheet

View 185_Maplet_det_updated.mw on MapleNet or Download 185_Maplet_det_updated.mw
View file details

 

Scott

Wow thanks scott. Where did

Wow thanks scott. Where did you learn the coding from e.g. from a specific website?

My problem is to create that input form for a matrix of dimension nxm, where n<10 and m<22. The way this program has been coded that doesn't seem like an easy job, but if i could use a for loop some how to create the required number of input boxes when the user runs the program, that should be good. After the user inputs the value, i just want the results output in a matrix P of nxm dimensions, does that sound straightforward?

John Fredsted's picture

m x n matrix

You can create a Maplet corresponding to an m x n matrix by having a procedure return the Maplet:

with(Maplets:-Elements):
makeMaplet := proc(m::posint,n::posint)
   Maplet([
      ["Enter the matrix, please"],
      seq([seq(TextBox[entry||i||j](width = 5),j = 1..n)],i = 1..m),
      [Button("Return the matrix",Shutdown())]
   ])
end proc:
Maplets[Display](makeMaplet(3,5));

So far, though, I have struggled in vain to have the Maplet itself return a matrix with the entered values, when shutting down. My problem is not that I don't know how to obtain the entries themselves, the problem is that I cannot figure out the correct format of the code to put in Shutdown().

Robert Israel's picture

Matrix Maplet

Try this:

> matrixMaplet:= proc(m::posint, n::posint)
     local maplet;
     uses Maplets[Elements];
     maplet:= Maplet([
      ["Enter the matrix, please"],
      seq([seq(TextBox[_entry||i||j](width = 5),j = 1..n)],
                i = 1..m),
      [Button("Return the matrix",
          Shutdown([seq(seq(_entry||i||j,j=1..n),i=1..m)]))]
   ]);
   Matrix(m,n,map(parse,Maplets[Display](maplet)));
  end proc:
  matrixMaplet(3,4);

  

 

 

That is amazing thanks guys,

That is amazing thanks guys, 2 questions though.

Where did you learn the about functions to create this? In the help section of maple 11?

Instead of just outputting the matrix for aesthetics, how can i then pass those values on to a matrix P?

I will use the maplet within a procedure, before i had the user entering 1 intput at a time, have the procedure loop through the asking process, now i want to say address the input box of the form in the top left hand corner to P[1,1], then the next one across P[1,2], all the way to the bottom one which will be P[m,n]. thanks for the assistance so far.

Robert Israel's picture

Passing those values

I'm not sure what you mean by

"Instead of just outputting the matrix for aesthetics, how can i then pass those values on to a matrix P?"

My procedure matrixMaplet returns the Matrix, and you can do whatever you wish with the result, for example assign it to P:

P := matrixMaplet(3,4);

A useful addition to matrixMaplet might be an optional third argument that provides initial values for the entries.  Thus:

matrixMaplet:= proc(m::posint, n::posint, 
   M::{Matrix,matrix,listlist})
     local maplet, M0;
     uses Maplets[Elements];
     if nargs = 3 then M0 := M
     else M0 := Matrix(m,n)
     end if;   
     maplet:= Maplet([
         ["Edit the matrix, please"],
         seq([seq(TextBox[_entry||i||j]('width' = 5,
              'value'=M0[i,j]),j = 1..n)],
             i = 1..m),
         [Button("Done",
           Shutdown([seq(seq(_entry||i||j,j=1..n),i=1..m)]))]
        ]);
   Matrix(m,n,map(parse,Maplets[Display](maplet)));
  end proc:

Q:= <<1,2>|<3,4>|<5,6>>;
Q:= matrixMaplet(2, 3, Q);

 

John Fredsted's picture

Help pages

I learned about the functions reading the help pages. But, as I wrote in my previous post, I had trouble figuring out how to return a matrix, and here the help pages seemed of little help, but maybe that is my own fault, as I only read the help pages cursorily. Obviously, Robert Israel has resolved this issue, using a slightly different structure of the procedure, and using parse which I didn't know.

Doug Meade's picture

one approach

Here is one way to get a matrix returned from your maplet. This uses invisible fields to handle the communication. I tend to do this since I seem to have trouble when I try to pass arguments.

with(Maplets:-Elements):
makeMaplet := proc(m::posint,n::posint)
   Maplet([
     ["Enter the matrix, please"],
     seq([seq(TextBox[entry||i||j](width = 5),j = 1..n)],i = 1..m),
     [TextBox['TBm']('visible'='false', 'width'=1, 'value'=m ),
     TextBox['TBn']('visible'='false', 'width'=1, 'value'=n ),
     Button("Return the matrix",'onclick'='A1'),
     TextBox['TBM']('visible'='false', 'width'=1, 'value'="" ),
     TextBox['TBd']('visible'='false', 'width'=1, 'value'="" )]
     ],
     Action['A1']( Evaluate('function' = "GetMatrix"),
       Shutdown(['TBM']) )
  )
end proc:

GetMatrix := proc()
   local m, n, M;
   uses Maplets:-Tools;
   m := Get( 'TBm'::posint, corrections=true );
   n := Get( 'TBn'::posint, corrections=true );
   M := Matrix( m,n, (i,j)->Get(entry||i||j) );
   Set( 'TBM' = M );
end proc:
AA:=eval( parse( Maplets[Display](makeMaplet(3,5))[] ) );

I would be interested in other solutions that might be a little more elegant than this brute force solution. (The last TextBox is inserted to keep the Button centered.)

Is this helpful?

Doug

--------------------------------------------------------------------- Douglas B. Meade <>< Math, USC, Columbia, SC 29208 E-mail: mailto:meade@math.sc.edu Phone: (803) 777-6183 URL: http://www.math.sc.edu/~meade/

 

Hi Doug That is exactly what

Hi Doug

That is exactly what i am looking for, except the entries of the matrix can't be directly used, because they are surrounded in speech marks i think? Also where did you learn about these maplet commands? Thanks very much

Doug Meade's picture

forcing entries to be numeric

Here are two alternatives to make the entries numeric. First, you can use the previous maplet with the entries "parsed":

BB := map( parse, eval( parse( Maplets[Display](makeMaplet(1,5))[] ) ) );

Second, you can change the definition of M in GetMatrix to:

M := Matrix( m,n, (i,j)->Get(entry||i||j::numeric,corrections=true) );

With this approach all entries of the matrix must be numeric. If they are not, then a window will appear in which a numeric value must be entered before going further.

If you adopt the second approach, then maybe you want to put a default value of 0 in every entry in the matrix. You can force the entries to be returned as numeric by changing the double loop creating the entries to:

      seq([seq(TextBox[entry||i||j](width = 5,value=0),j = 1..n)],i = 1..m),

That is, insert ",value=0" to set 0 as the default value in each entry. Here is the current version I have:

restart;
with(Maplets:-Elements):
makeMaplet := proc(m::posint,n::posint)
   Maplet([
      ["Enter the matrix, please"],
      seq([seq(TextBox[entry||i||j](width = 5,value=0),j = 1..n)],i = 1..m),
      [TextBox['TBm']('visible'='false', 'width'=1, 'value'=m ),
       TextBox['TBn']('visible'='false', 'width'=1, 'value'=n ),
       Button("Return the matrix",'onclick'='A1'),
       TextBox['TBM']('visible'='false', 'width'=1, 'value'="" ),
       TextBox['TBd']('visible'='false', 'width'=1, 'value'="" )]
   ],
   Action['A1']( Evaluate('function' = "GetMatrix"),
                 Shutdown(['TBM']) )
)
end proc:

GetMatrix := proc()
  local m, n, M;
  uses Maplets:-Tools;
  sprintf( "in GetMatrix with m=%a and n=%a", m,n );
  m := Get( 'TBm'::posint, corrections=true );
  n := Get( 'TBn'::posint, corrections=true );
  M := Matrix( m,n, (i,j)->Get(entry||i||j::numeric,corrections=true) );
  Set( 'TBM' = M );
end proc:

AA:=eval( parse( Maplets[Display](makeMaplet(3,5))[] ) );

How did I learn this? Experience. Lots of trial and error. Reading the help. Re-reading the help. Asking questions of Maple Technical Support. I did all of this while creating Maplets for Calculus, a collection of now 94 maplets for single variable calculus. Take a look at http://m4c.math.sc.edu/. The actual maplets are on a secure site. I can give temporary access to anyone who would like to take them for a test drive.

Doug

---------------------------------------------------------------------
Douglas B. Meade  <><
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu/~meade/
Scott03's picture

Removing quotes

To get rid of the quotes you will need to run another parse over the result.  The following line will fix it

map(parse AA);

 

As for your other question you have asked a few times on how do people find out on these commands, you could check out both the User Manual and Introductory Programming Guide.  Both of these are available as pdfs on the Maplesoft.com website.  Personally I jsut go to the webpages and try to find examples that are solve a similar problem or could be changed to do what I want.

 

Scott

Excellent, thanks for all

Excellent, thanks for all the help.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}