home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sources / hp48 / 285 < prev    next >
Encoding:
Text File  |  1992-11-17  |  18.5 KB  |  380 lines

  1. Newsgroups: comp.sources.hp48
  2. Path: sparky!uunet!seq!spell
  3. From: Salvatore A. Colomba <flynn@sabrina.dei.unipd.it>
  4. Subject:  v07i036:  smhl.sac - Symbolic Matrix Handling Library v1.1, Part01/02
  5. Message-ID: <cshp48-v07i036=smhl.sac.225537@seq.uncwil.edu>
  6. Followup-To: comp.sys.hp48
  7. Sender: spell@seq.uncwil.edu (Chris Spell)
  8. Organization: Univ. of North Carolina @ Wilmington
  9. Date: Wed, 18 Nov 1992 03:56:58 GMT
  10. Approved: spell@seq.uncwil.edu
  11. Lines: 371
  12.  
  13. Checksum: 2231066675 (verify with brik -cv)
  14. Submitted-by: Salvatore A. Colomba <flynn@sabrina.dei.unipd.it>
  15. Posting-number: Volume 7, Issue 36
  16. Archive-name: smhl.sac/part01
  17.  
  18. BEGIN_DOC smhl1_1.doc
  19.                          Symbolic Matrix Handling Library
  20.                              Version 1.1  16 Nov 1992
  21.                                  BETA TEST RELEASE
  22.                                         by
  23.                                Salvatore A. Colomba
  24.  
  25.  
  26.        0 - Warnings
  27.  
  28.        This is a  BETA TEST  RELEASE. This means  that I've not  tried it in
  29.      real operative situations;  I've just tested the  routines. If some bug
  30.      is still present,  any report will be  very appreciated and immediately
  31.      followed by a fixed release.
  32.  
  33.  
  34.        1 - Introduction
  35.  
  36.        This is version  1.1 of  SMHL, an update  to version 1.0.  I've fixed
  37.      some bugs, speeded  up most of the routines  (with a look to symmat_cf;
  38.      thanks to  Carlos  Ferraro for  a  couple of  good ideas),  added some;
  39.      moreover, most of the routines aren't  anymore hidden, in order to make
  40.      it easier to develop more optimized SMHL-based programs.
  41.  
  42.        SMHL is a collection  of routines to handle  symbolic matrices almost
  43.      exactly as they were numerical ones. Almost every standard command that
  44.      usually operates on  either algebraics or matrices  is extended to deal
  45.      with the new matrix type, and  a TRUE linear system solver is provided.
  46.      SMHL has  been developed  in User RPL,  so it's  not very fast,  but it
  47.      still should be useful for many purposes...
  48.  
  49.        SMHL is a Cardware: if you like it, I would be very happy if you send
  50.      me  a   picture  card  of  your   country...  Thanks!!!  Non-commercial
  51.      distribution of SMHL  with this document (unchanged)  is encouraged. My
  52.      address is:
  53.  
  54.                                Salvatore A. Colomba
  55.                                 C.so del Popolo 14
  56.                                 35100 Padova Italy
  57.  
  58.        Comments, suggestions, bug  reports, NeXT computers  welcome... My e-
  59.      mail address is the following:
  60.  
  61.                             Flynn@sabrina.dei.unipd.it
  62.                                (Flynn@147.162.2.106)
  63.  
  64.        NOTE: I take no responsibility for any  damage caused by any possible
  65.      bug in  the SMHL  library. Although  in SMHL1.0  people all  around the
  66.      world has discovered 'just' two bugs, version 1.1 is mostly re-written,
  67.      so I can't  be sure that it's totally  bug-free... And, as someone once
  68.      said: "There is always one more bug".
  69.  
  70.  
  71.        2 - Installation
  72.  
  73.        Well I'm  sure  that all  you out  there  know the  way to  install a
  74.      library, however here are the required steps:
  75.  
  76.           1)   UUdecode  or ASC-> SMHL and download it in your HP48 (or
  77.                vice-versa).
  78.           2)   Recall  it and store in your favorite port; if you don't
  79.                have  enough  memory, disable  LAST_STACK  and LAST_ARG,
  80.                recall  it on the stack, PURGE the original variable and
  81.                then  press <port  number> STO;  by doing this  way, you
  82.                avoid  to get  more than one  copy of SMHL  in memory at
  83.                the same time.
  84.           3)   Turn off and then on your calculator.
  85.           4)   Now  SMHL  is  installed; enter  in  the SMHL  directory
  86.                under  the LIBRARY menu and  press BlueShift-NXT (PREV);
  87.                choose  KEYS  to remap  the new  functions in  your user
  88.                keyboard  (DKEY  gets rid  of them),  and enter  in USER
  89.                mode.
  90.  
  91.  
  92.        3 - Conventions
  93.  
  94.        Symbolic matrices are delimited by the  {} characters, using the same
  95.      convention as the numerical matrices, i.e.:
  96.  
  97.            [ 1 2 3 ]     <-->     { a b c }     -     Row vectors
  98.  
  99.           [[ 1 2 3 ]             {{ a b c }
  100.            [ 4 5 6 ]     <-->     { d E f }     -     Matrices
  101.            [ 7 8 9 ]]             { g h I }
  102.  
  103.        Note that symbols  'e' and  'i' represent two  constants, and without
  104.      apostrophes they  are  'built-in commands',  and  HP48 DOESN'T  care to
  105.      convert them in computation, so keep in mind:
  106.  
  107.                    -  You can't use them like any other symbol.
  108.                    -  If  you meant to use  them for what they
  109.                       are,    you   MUST   enclose   them   in
  110.                       apostrophes to allow HP48 to handle them
  111.                       correctly.
  112.  
  113.        Symbolic and numerical matrices  or arrays are fully interchangeable:
  114.      where a  matrix  can stay,  there  a list  can do.  So, you  haven't to
  115.      perform  boring conversions  before,  e.g., summing  two matrices,  one
  116.      numerical and  the other symbolic;  result, in general,  is symbolic if
  117.      one of  the arguments was  symbolic. However, you  cannot form matrices
  118.      like:
  119.                                     {[ 1 2 3 ]
  120.                                      { 4 5 6 }
  121.                                      { 7 8 9 }}
  122.  
  123.        The first row is not always accepted,  so don't use it; no checks are
  124.      performed, so it's due to you to avoid such situations.
  125.  
  126.  
  127.        4 - Notes
  128.  
  129.        From now  on, when I'll  say 'matrix',  I'll mean both  numerical and
  130.      symbolic matrix or vector.
  131.  
  132.        I'm sorry, but my  routines don't have almost  any error managing and
  133.      there isn't a true arguments checking;  I think it would have slow down
  134.      calculations  too  much. Maybe  in  a  system-rpl  version  (or just  a
  135.      compiled one)...  Anyway, the linear  system solver will tell  you if a
  136.      set of linear equation has no solution and Det will refuse to work on a
  137.      non-square matrix.  Note that  if arguments  aren't correct,  you won't
  138.      always incur into  an error: sometimes you'll  simply get wrong or bad-
  139.      formed results.
  140.  
  141.        Flag -28 is used to keep track  of an internal sort of 'sort by type'
  142.      routine; if you ABSOLUTELY need that flag for any other program, unpack
  143.      the SMHL  library, STOre in 'SWAPPED'  the flag number you  wish to use
  144.      and pack  again the  library, or  e-mail me  the flag number  you would
  145.      prefer and I'll send you a modified version.
  146.  
  147.        As far as regards  the keys, a user keymap  is provided to extend the
  148.      arithmetic commands  to the new  matrix type,  so keep the  keyboard in
  149.      USER mode; note that when you  are in algebraic mode, you don't need to
  150.      exit USER mode;  everything that can be  put in an algebraic expression
  151.      will be as the usual.
  152.  
  153.  
  154.        5 - Basic commands
  155.  
  156.        Name     [Key]    What's more
  157.        ---------------------------------------------------------------------
  158.        Eval     [EVAL]   E-valuates each matrix element.
  159.        ->q      [->Q]    Applies  ->Q  to  all the  elements;  if the  input
  160.                          matrix  is a  numerical one, it's  obviously turned
  161.                          into a symbolic one.
  162.        ->Num    [->NUM]  Transforms  a  symbolic matrix  (or vector)  into a
  163.                          numerical one.
  164.        Sq       [x^2]    If  argument is an array,  it returns the square of
  165.                          its  modulus, that  is, it performs a  Dot with the
  166.                          array itself.
  167.        \^|      [y^x]    Returns  the  n-th power  of  a matrix.  This is  a
  168.                          highly   optimized   routine.  In   the   range  of
  169.                          exponents  from  1 to  100,  the maximum  number of
  170.                          multiplications   is   10,  corresponding   to  the
  171.                          exponent   value  87...   Nice,  isn't  it?     The
  172.                          exponent,  must be a _relative_  integer other than
  173.                          zero.
  174.        PInv     [INV] |\
  175.        Inv      INV   |->PInv  is a Partial Inverse: the inverse is given in
  176.                          terms  of a matrix in level 2, and a coefficient in
  177.                          level  one;  to  get  the  inverse  matrix,  you'll
  178.                          simply   have   to  divide   the  matrix   by  that
  179.                          coefficient.   Useful  you  you   have  to  operate
  180.                          further  on the  inverse. Inv,  on the  other hand,
  181.                          gives  directly  the inverse  matrix. Note  that if
  182.                          the  input matrix was a  numerical one, the outputs
  183.                          of  PInv and Inv  will be the same,  i.e. you won't
  184.                          get the determinant in level one.
  185.        Div      [/]      Performs  linear  system solving;  see below  for a
  186.                          more detailed description.
  187.        Mul      [*]      If   arguments  are  two  vectors,  it  computes  a
  188.                          symbolic Dot.
  189.  
  190.        Neg      [+/-]    |
  191.        Sub      [-]      |
  192.        Add      [+]      |
  193.        Con      CON      |
  194.        Trn      TRN      |
  195.        Rdm      RDM      |----> Simply extended to handle symbolic matrices
  196.        Det      DET      |
  197.        Abs      ABS      |
  198.        Arry->   ARRY->   |
  199.        ->Arry   ->ARRY   |
  200.        Size     SIZE     |
  201.  
  202.        VIEW     ...      Shows  the matrix in level  1 using the small font;
  203.                          it'll  leave you  in scroll  mode, so that  you can
  204.                          view  matrices larger than display.  I've written a
  205.                          version  of this routine that shows the elements in
  206.                          the  Equation Writer  format; nice,  but it  eats a
  207.                          lot  of  memory... If  you are  one of  those lucky
  208.                          people  with a  memory expansion,  feel free  to e-
  209.                          mail me to obtain a modified SMHL.
  210.        EXCT     ...      EXpands and CollecTs each single element.
  211.        MAT->    ...      Given  a matrix  in level  1, it  will split  it in
  212.                          rows and leave the  number of rows in level 1.
  213.        ->MAT    ...      It's  quite the opposite than  MAT->; given, say, n
  214.                          arrays  and the number n in level 1, it returns the
  215.                          formed matrix. Note that:
  216.                                 1: Arrays MUST all be  of the same kind, you
  217.                                    can't mix symbolic and numerical ones.
  218.                                 2: They  MUST   have  the  same   number  of
  219.                                    elements.
  220.                          If  you  don't  follow these  simple rules,  you'll
  221.                          surely  incur in  some error,  or get a  bad formed
  222.                          matrix.
  223.        Type     ...      Divides its input in five types:
  224.                                0:  Numbers, complex
  225.                                1:  Symbolic, expressions
  226.                                2:  Numerical arrays (eventually complex)
  227.                                3:  Symbolic arrays
  228.                                4:  Numerical matrices (eventually complex)
  229.                                5:  Symbolic matrices
  230.        Apply    ...      Applies  program in level 1  to the SYMBOLIC matrix
  231.                          or vector in level 2.
  232.        M->L     ...      Turn a numerical matrix into a symbolic one.
  233.  
  234.        Note:   most of  the  above commands  can  be put  in algebraic  form
  235.                (p.e.: 'Inv(X)'); this to give you maximum compatibility with
  236.                the standard functions.
  237.  
  238.  
  239.        6 - Symbolic linear system solving
  240.  
  241.        System solving  needs  the same  arguments than  the  built-in system
  242.      solver; but it works with any possible matrix; when the solution is not
  243.      unique, it  will  give the  GENERAL solution,  in terms  of independent
  244.      variables xk (p.e.: x4,x5,x6,...). Let's see some example:
  245.  
  246.        _________________________
  247.        | {HOME}                |
  248.        |-----------------------|        The rank of this matrix is 2; it
  249.        |2:            [ 0 0 0 ]|        means that the  solution will be
  250.        |1: [[ 1 2 3 ]          |        a 1-dimension subspace.  In more
  251.        |    [ 4 5 6 ]          |        mathematical terms, the solution
  252.        |    [ 7 8 9 ]          |        will be  Ker(A),  where A is the
  253.        |### ### ### ### ### ###|        matrix in level 1.
  254.        `-----------------------'
  255.        Pressing [/], after about 20 seconds, in level 1 you'll get:
  256.  
  257.                            { { x3 } { '2*-x3' } { x3 } }
  258.  
  259.        Trying to  multiply  the original  matrix for  this  solution, you'll
  260.      always obtain a  three-elements null vector. The  same principle can be
  261.      applied to  solve  liner systems  with  vector unknown  quantities; for
  262.      example:
  263.  
  264.                              _________________________
  265.                              | {HOME}                |
  266.                              |-----------------------|
  267.                              |2: [[ 1 2 3 ] [ 4 5 6..|
  268.                              |1: [[ 1 2 3 ]          |
  269.                              |    [ 4 5 6 ]          |
  270.                              |    [ 7 8 9 ]          |
  271.                              |### ### ### ### ### ###|
  272.                              `-----------------------'
  273.  
  274.        Pressing [/], after about  20 seconds (SMHL1.0 took  30!), you'll get
  275.      the solution of the following linear system:
  276.  
  277.                               x1+2*x2+3*x3 = ( 1,2,3 )
  278.                             4*x1+5*x2+6*x3 = ( 4,5,6 )
  279.                             7*x1+8*x2+9*x3 = ( 7,8,9 )
  280.  
  281.                                      that is:
  282.  
  283.                        x1= { 'x31+1'  'x32'     'x33-1'   }
  284.                        x2= { '2*-x31' '2*x32+1' '2*-x33+2 }
  285.                        x3= { 'x31'    'x32'     'x33'     }
  286.  
  287.        Call VIEW to see it better; note that the identity matrix is only one
  288.      of the oo^3  possible matrices and corresponds  to x31=0, x32=0, x33=1.
  289.      Nice, eh? Symbolic matrices are _obviously_ supported, but note that in
  290.      this case  the  solutions found  won't _always_  be REALLY  general: my
  291.      routines assume  that the coefficient  matrix rank doesn't  depend upon
  292.      symbolic elements values; some other  times, you'll get a 'Incompatible
  293.      system' error, although  system has some solution  if some condition is
  294.      verified...
  295.        Let's see some examples:
  296.  
  297.                                    / a 2 3 | 0 \
  298.                                   (  4 5 6 | 0  )
  299.                                    \ 7 8 9 | 0 /
  300.  
  301.        When you try  to solve  this system with  my package, you'll  get the
  302.      null solution, although the number of  solution varies as a changes; if
  303.      a!=1 the solution is actually the  null one, but if a=1 the rank of the
  304.      coefficient  matrix becomes  2,  and  you'll have  oo^1 solutions.  The
  305.      problem resides in the PIVOT routine itself: it's not so smart to avoid
  306.      taking  symbolics  as special  elements;  I'm  sorry,  but I'll  hardly
  307.      improve PIVOT in this direction, because  it would have to solve a sort
  308.      of 'assignment problem', and I think it's not a practicable way in User
  309.      RPL. Another example:
  310.  
  311.         / 1 2 3 | a \     PIVOT     / 1 0 -1 | 0               \
  312.        (  4 5 6 | b  )  ---------> (  0 1  2 | 0                )
  313.         \ 7 8 9 | c /               \ 0 0  0 | c-7*a-2*(b-4*a) /
  314.  
  315.        As you  can see,  this system  has no  solution for ALL  the possible
  316.      values of  a,b,c, and solving  it by using  Div will lead  to an error,
  317.      although the system with:
  318.                                  c-7*a-2*(b-4*a)=0
  319.        has the solutions:
  320.                                  { x1 '-2*x1' x3 }
  321.  
  322.  
  323.        7 - Program development oriented routines
  324.  
  325.        The following routines  constitute under certain points  of view, the
  326.      very heart  of SMHL;  they are  very stupid  routines, in  general they
  327.      DON'T make ANY  check on their arguments,  but intensive mail exchanges
  328.      with some people has convinced me to make them visible.
  329.  
  330.        det     Computes a raw determinant on SYMBOLIC matrices only.
  331.        Dot     Performs a dot between two vectors.
  332.        MINOR   Given  a  matrix in  level  2 and  a list  containig  row and
  333.                column, it returns the minor of the matrix.
  334.        ->qu    It's equivalent to ->Q, but it's extended to unit objects.
  335.        V\:-K   Divides a vector by a constant; result is ALWAYS a list.
  336.        M\:-K   Same as above for matrices.
  337.        SYS     Solves a linear system; same syntax as the built-in solver.
  338.        K\*V    Multiplies a vector by a constant; result is ALWAYS a list.
  339.        K\*M    Same as above for matrices.
  340.        M\*M    Multiplies two SYMBOLIC matrices.
  341.        V\+-V   Sums two vectors
  342.        M\+-M   Sums two SYMBOLIC matrices.
  343.        L->M    Turns a  symbolic matrix into  a numerical one;  note that if
  344.                argument is  a symbolic  vector, it  turns it in  a numerical
  345.                column vector.
  346.        PIVOT   Pivotizes, as much as possible, a matrix.
  347.        STICK   Given  a  known  terms  matrix  or  vector  in  level  2  and
  348.                coefficent matrix in level  one, returns the complete matrix,
  349.                that is, it puts them side to side.
  350.        SPLIT   Is the inverse  operation. Note that if  a positive integer n
  351.                is given, a matrix of n columns will be extracted.
  352.        TSRT    Sorts by  Type two arguments  and returns a  two digit number
  353.                whose first  digit is  the first  argument's Type  and second
  354.                digit is the second one.
  355.        SWAP?   Turns TSRTed objects in the right order.
  356.        SWAP\?  Turns TSRTed objects in the inverse order.
  357.        MULX    Equivalent to the HP's MULTI
  358.        XMAKE   Given two positive integers m and  n, creates a mxn matrix in
  359.                the form:
  360.                                 / x11 x12 ... x1n \
  361.                                 | x21 x22 ... x2n |
  362.                                 | ... ... ... ... |
  363.                                 \ xm1 xm2 ... xmn /
  364.  
  365.                Else, if m is 1, it will return a vectror in the form:
  366.                                   ( x1 x2 .. xn )
  367.        NULL    Returns a null  vector whose size is  given by the integer in
  368.                level 1; if integer in level 2 is other than zero, a symbolic
  369.                vector will be returned.
  370.  
  371.        VER     Call  it and follow the instructions, or a lighting will melt
  372.                down your HP!!!  !8^) !8^) !8^)
  373.        KEYS    Maps the new functions on the keyboard.
  374.        DKEY    Restores the standard key definitions.
  375.  
  376.        Well, I think that's all folks. Hope  that your teacher allows you to
  377.      bring your HP48 at the exam...
  378.  
  379. END_DOC
  380.