> next up previous contents index
: Variable values : Variable sets : Print names generation   目次   索引

Multiple type variable sets

Multiple type variable sets are those variable sets containing unidimensional variables of different types and are defined by joining previously defined variables sets. Joining variables sets is possible by using the ``$\verb+^+$'' operator. Program 5 illustrates the definition of multiple type variable sets.



Program 5: Multiple type variables sets.

     1  /*=============================================================================
     2   * File           : multipleTypes.cpp
     3   *=============================================================================
     4   *
     5   *------------------------- Description ---------------------------------------
     6   * This file gives an example to define multiple type variable sets   
     7   *   
     8   *-----------------------------------------------------------------------------
     9  */
    10
    11  #include <pl.h>
    12
    13  main() 
    14  {
    15    //Definig types
    16    plIntegerType rank_type(0,10);
    17    plIntegerType age_type(0,120);
    18    plRealType weight_type(40.0,140.0);
    19    plRealType height_type(0.2,2.30, 200);
    20
    21    //Defining single type variable sets
    22    plSymbol rank("rank",rank_type);
    23    plSymbol age("age",age_type);
    24    plSymbol height("height",height_type);
    25    plSymbol weight("weight",weight_type);
    26    plArray  ancestors_height("anc_height",height_type,1,2);
    27
    28    //Defining a multiple type variable 
    29    plVariable physical_info(weight^height^ancestors_height);
    30    cout<<"physical_info = "<<physical_info<<"\n";
    31    
    32    //Defining a multiple type variable 
    33    plVariable athlet_info(physical_info^age);
    34    cout<<"athlet_info = "<<athlet_info<<"\n\n";
    35
    36    //The previous two variables can also be constructed as follows
    37    plVariable physical_info2;
    38    physical_info2 = weight^height^ancestors_height;
    39    cout<<"physical_info2 = "<<physical_info2<<"\n";
    40
    41    plVariable athlet_info2;
    42    athlet_info2 = physical_info2;
    43    athlet_info2 ^= age;
    44    cout<<"athlet_info2 = "<<athlet_info2<<"\n";
    45
    46  }

The output of the previous code is then as follows:

  physical_info = {weight height anc_height0 anc_height1}
  athlet_info = {weight height anc_height0 anc_height1 age}

Multiple type variable sets are dynamic, that is they can be affected by using the ``$^=$'' and ``$\verb+^=+$'' operators. Once you have created a variable set by means of the constructor plVariable you can assign it the value of another variable set or join it with another variable sets. plSymbols and plArrays are no dynamic, objects of these classes can not appear at the left side of ``='' or ``$\verb+^=+$'' operators.

The variable sets $physical\_info$ and $athlete\_info$ defined in Program 5 could also be constructed as follows:

  plVariable physical_info;
  physical_info = weight^height^ancestors_height;

  plVariable athlet_info;
  athlet_info = physical_info;
  athlet_info ^= age;

The ``^'' and ``^=`` operators are particularly useful for grouping variables of different types. Program 6 shows this situation. The example consists in a suppositious collection of $n$ sensors placed on a circular device. Particularly, it poses the problem of constructing two categories of variable sets. The first category, sensor readings sets, are composed of an $angle$, a $distance$ and a $velocity$ where velocity is decomposed in an $x$ and $y$ component. The second category, side readings sets, are divide all the readings (angles, distances and velocities) on left and right data. For simplicity we assume that $n$ is an even number of sensors. The odd numbered sensors $S_1, S_3,...,S_{n-1}$ are on the left and the even numbered sensors $S_2, S_4,...,S_{n}$ are on the right. In essence, we would like to construct the following sets:


\begin{displaymath}
\begin{array}{l}
reading_i = \{angle_i, distance_i, velocity...
...ding_2\cup reading_4\cup ... \cup reading_{n-1} \\
\end{array}\end{displaymath}

Program 6 is a version for the construction of these two categories of sets.



Program 6: Grouping variables.

     1  /*=============================================================================
     2   * File           : groupingVars.cpp
     3   *=============================================================================
     4   *
     5   *------------------------- Description ---------------------------------------
     6   * This is a simple example showing how to group a set of variables. We assume  
     7   * having n sensors giving a reading of a moving object. A readings is composed 
     8   * by an angle, a distance and a velocity in x and y. A set of Left an right 
     9   * readings is also constructed. We assume that odd sensors are left sensors
    10   * and that even sensors are right sensors
    11   *-----------------------------------------------------------------------------
    12  */
    13
    14  #include <pl.h>
    15
    16  main ()
    17  {
    18
    19    /**********************************************************************
    20     Defining types and variables
    21    ***********************************************************************/
    22
    23    //Defining types
    24    plIntegerType angle_reading(0,360);
    25    plRealType distance_reading(0,100.0,50);
    26    plRealType velocity_reading(-50,50,20);
    27
    28    const int n_sensors = 6;
    29
    30    //Defining a plArrays angle(n_sensors) for angles 
    31    plArray angle("T",angle_reading,1,n_sensors);
    32
    33    //Defining a plArrays distance(n_sensors) for distances
    34    plArray distance("D",distance_reading,1,n_sensors);
    35
    36    //Defining a plArrays velocity(n_sensors, 2). The "2" corresponds to
    37    //the x and y components
    38    plArray velocity("V",velocity_reading,2,n_sensors,2);
    39
    40    /**********************************************************************
    41     Joining variable sets by means of the ^ operator 
    42    ***********************************************************************/
    43
    44    unsigned int i;
    45    vector <plVariable> reading(n_sensors);
    46
    47    for(i=0;i<n_sensors;i++)
    48      {
    49        //Lectire_i = {angle_i, distance_i, velocity_x; velocity_y}
    50        reading[i] = angle(i)^distance(i)^velocity(i,0)^velocity(i,1);
    51        cout<<"reading "<<i<<" set = "<<reading[i]<<"\n";
    52      }
    53
    54    /**********************************************************************
    55     Joining variables sets by means of the ^= operator 
    56    ***********************************************************************/
    57
    58    plVariable left_readings;
    59    plVariable right_readings;
    60    
    61    // constructing the sets of left and right readings
    62    for(i=0;i<n_sensors-1;i+=2)
    63      {
    64        left_readings ^= reading[i];
    65        right_readings ^= reading[i+1];
    66      }
    67    
    68    cout<<"\nleft readings set = "<<left_readings<<"\n\n";
    69    cout<<"right readings set = "<<right_readings<<"\n";
    70    
    71  }

Lines 31, 33 and 38 defines single type variable sets. Lines 31 defines the set angle with print name ``T'' and type angle_reading. angle contains the elements of a one dimensional symbol array of size n_sensors, each of the element can be referred by angle(i) were i<n_sensors. Likewise, the set distance, has print name ``D'' is of type distance_reading and contains n_sensors variables. Line 38 defines the set velocity containing a n_sensors$\times 2$ array of Symbols. Again, each of the symbols on distance_reading can be refereed by distance_reading(i, j) were i<n_sensors and j$\in\{0, 2\}$.

Here is the output of the program:

reading 0 set = {T0 D0 V(0,0) V(0,1)}
reading 1 set = {T1 D1 V(1,0) V(1,1)}
reading 2 set = {T2 D2 V(2,0) V(2,1)}
reading 3 set = {T3 D3 V(3,0) V(3,1)}
reading 4 set = {T4 D4 V(4,0) V(4,1)}
reading 5 set = {T5 D5 V(5,0) V(5,1)}

left readings set = {T0 D0 V(0,0) V(0,1) T2 D2 V(2,0) V(2,1) 
T4 D4 V(4,0) V(4,1)}

right readings set = {T1 D1 V(1,0) V(1,1) T3 D3 V(3,0) V(3,1) 
T5 D5 V(5,0) V(5,1)}


next up previous contents index
: Variable values : Variable sets : Print names generation   目次   索引
Juan-Manuel Ahuactzin 平成17年3月31日