1 # script_name = 'MatrixExample.py'
   2 
   3 # Short description:
   4 #   Some simple examples of matrix operations
   5 
   6 from Timba.TDL import *
   7 from Timba.Meq import meq
   8 
   9 # Timba.TDL.Settings.forest_state is a standard TDL name.
  10 # This is a record passed to Set.Forest.State.
  11 Settings.forest_state.cache_policy = 100;
  12 
  13 def _define_forest (ns):
  14   """define_forest() is a standard TDL name. When a forest script is
  15   loaded by, e.g., the browser, this method is automatically called to
  16   define the forest. The 'ns' argument is a NodeScope object in which
  17   the forest is to be defined, usually this is simply the global scope.
  18   """;
  19 
  20   #  create matrix with contents 2 3
  21   #                             -1 4
  22   ns.matrix << Meq.Matrix22(2, 3, -1, 4)
  23 
  24   # create inverted matrix with contents 4/11  -3/11
  25   #                                      1/11   2/11
  26   ns.invert << Meq.MatrixInvert22(ns.matrix)
  27 
  28 
  29   # the  matrix matrix1 = 2 4
  30   #                       1 2
  31   # cannot be inverted and indeed ns.invert1 has 'inf'
  32   # as its contents
  33   ns.matrix1 << Meq.Matrix22(2, 4, 1, 2)
  34   ns.invert1 << Meq.MatrixInvert22(ns.matrix1)
  35 
  36   # we define matrices  A = 1 4   B = -2 5    C = 2 3
  37   #                         6 3        1 6        4 5
  38   ns.A << Meq.Matrix22(1,4,6,3)
  39   ns.B << Meq.Matrix22(-2,5,1,6)
  40   ns.C << Meq.Matrix22(2,3,4,5)
  41   ns.multiply << Meq.MatrixMultiply(ns.A,ns.B,ns.C)
  42   # the contents of ns.multiply is the matrix  120  151
  43   #                                            174  213
  44   # which tells us that Meq.MatrixMultiply does matrix
  45   # multiplication in the order (AB)C  i.e. in the
  46   # sequence left to right
  47 
  48   # the Meq.ReqSeq (Request Sequencer) node enables us
  49   # to perform a number of separate requests in sequence.
  50   # Each child is activated only after the previous child
  51   # has returned. If this node had a parent, it would return the 
  52   # contents of the ns.multiply node, as we give the result_index
  53   # a value of 2 (zero-based) so we return the contents of the  
  54   # third node.  
  55   ns.reqseq << Meq.ReqSeq(ns.invert, ns.invert1, ns.multiply, result_index=2)
  56 
  57 def _test_forest (mqs,parent):
  58   """test_forest() is a standard TDL name. When a forest script is
  59   loaded by, e.g., the browser, and the "test" option is set to true,
  60   this method is automatically called after define_forest() to run a
  61   test on the forest. The 'mqs' argument is a meqserver proxy object.
  62   """;
  63   # create any old domain ...
  64   cells = meq.cells(meq.domain(0,2,0,1),num_freq=8,num_time=4);
  65   # create simple request
  66   request = meq.request(cells,rqtype='e1')
  67   a = mqs.meq('Node.Execute',record(name='reqseq',request=request),wait=True);
  68 
  69 
  70 # The following is the testing branch, executed when the script is run directly
  71 # via 'python script.py'
  72 
  73 if __name__ == '__main__':
  74 #  from Timba.Meq import meqds
  75   Timba.TDL._dbg.set_verbose(5);
  76   ns = NodeScope();
  77   _define_forest(ns);
  78   # resolves nodes
  79   ns.Resolve();

MatrixExample.py (last edited 2007-02-11 18:52:38 by TonyWillis)