Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REXX: define vector with matrices as elements 1

Status
Not open for further replies.

dietmar0609

Programmer
Nov 25, 2008
8
FR
Hi,

I have a number of 2 dimesional elements a.i.k

How do a define a vector with the a.i.k as elements.

thx for your help.

Dietmar
 
Hi Dietmar,

As I undesrtand you mean a vector which elements are matrices.
It's simple, in stem you can use so many indexes as you want, i.e. v.1. would be the substem which contains the 1.matrix and v.1.i.j (i=1,..; j=1,..) is the i-j-th element of matrix 1 and so on ...

Here is the example which shows how to do what you want:
Code:
/*initialize all stem elements*/
v.=0
v.0 = 2
/* 1. Matrix*/
v.1.0   =2 /*number of rows*/
v.1.0.0 =2 /*number of columns*/ 
/* matrix elements */
v.1.1.1 = 1; v.1.1.2=2
v.1.2.1 = 2; v.1.2.2=1

/* 1. Matrix*/
v.2.0  =2  /*number of rows*/
v.2.0.0=3  /*number of columns*/ 
/* matrix elements */
v.2.1.1 = 1; v.2.1.2=2; v.2.1.3=3
v.2.2.1 = 3; v.2.2.2=2; v.2.2.3=1

/* Main program */
do n=1 to v.0
  call print_matrix
  say
end 
exit

/* Procedures */
print_matrix: procedure expose n v.
  rows=v.n.0
  cols=v.n.0.0
  say n||".Matrix has dimensions "||rows||"x"||cols||" :"
  do i=1 to rows
    matrixrow="" 
    do j=1 to cols
       /* compose one row of the n-th matrix*/ 
       matrixrow = matrixrow||"M"||n||"["||i||","||j"]= "||v.n.i.j||", "
    end
    say matrixrow
  end
return
The output is:
Code:
1.Matrix has dimensions 2x2 :
M1[1,1]= 1, M1[1,2]= 2,
M1[2,1]= 2, M1[2,2]= 1,

2.Matrix has dimensions 2x3 :
M2[1,1]= 1, M2[1,2]= 2, M2[1,3]= 3,
M2[2,1]= 3, M2[2,2]= 2, M2[2,3]= 1,

To make a stem v. available to the produre I used EXPOSE, i.e. the procedure recognizes the stem as a global variable. However in ooREXX you can pass stem to the procedure as an argument using USE ARG, but this is not supported by other REXXes, so I rather used here the universal approach.
:)
 
To better undestarnd add to the above source these lines
Code:
[red]/* Number of matrices */[/red]
v.0 = 2
...
...
[red]/* 2. Matrix*/[/red]
v.2.0  =2  /*number of rows*/
...
...
 
I'am on finalizing a script which helps me to do the syntax-highlighting for posting source code here.

So here is the final version of the above REXX-program with syntax highlighting:
Code:
[COLOR=#0000ff]/* initialize all stem elements */[/color]
v.[COLOR=#804040][b]=[/b][/color]0

[COLOR=#0000ff]/* Number of matrices in the vector */[/color]
v.0 [COLOR=#804040][b]=[/b][/color] 2

[COLOR=#0000ff]/* 1. Matrix */[/color]
v.1.0.rows [COLOR=#804040][b]=[/b][/color] 2 [COLOR=#0000ff]/* number of rows */[/color]
v.1.0.cols [COLOR=#804040][b]=[/b][/color] 2 [COLOR=#0000ff]/* number of columns */[/color] 
[COLOR=#0000ff]/* matrix elements */[/color]
v.1.1.1 [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b];[/b][/color] v.1.1.2[COLOR=#804040][b]=[/b][/color]2
v.1.2.1 [COLOR=#804040][b]=[/b][/color] 2[COLOR=#804040][b];[/b][/color] v.1.2.2[COLOR=#804040][b]=[/b][/color]1

[COLOR=#0000ff]/* 2. Matrix */[/color]
v.2.0.rows [COLOR=#804040][b]=[/b][/color] 2  [COLOR=#0000ff]/* number of rows */[/color]
v.2.0.cols [COLOR=#804040][b]=[/b][/color] 3  [COLOR=#0000ff]/* number of columns */[/color] 
[COLOR=#0000ff]/* matrix elements */[/color]
v.2.1.1 [COLOR=#804040][b]=[/b][/color] 1[COLOR=#804040][b];[/b][/color] v.2.1.2[COLOR=#804040][b]=[/b][/color]2[COLOR=#804040][b];[/b][/color] v.2.1.3[COLOR=#804040][b]=[/b][/color]3
v.2.2.1 [COLOR=#804040][b]=[/b][/color] 3[COLOR=#804040][b];[/b][/color] v.2.2.2[COLOR=#804040][b]=[/b][/color]2[COLOR=#804040][b];[/b][/color] v.2.2.3[COLOR=#804040][b]=[/b][/color]1

[COLOR=#0000ff]/* Main program */[/color]
[COLOR=#804040][b]do[/b][/color] n[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]v.0
  [COLOR=#804040][b]call[/b][/color][COLOR=#008080] print_matrix[/color]
  [COLOR=#804040][b]say[/b][/color]
[COLOR=#804040][b]end[/b][/color] 
[COLOR=#804040][b]exit[/b][/color]

[COLOR=#0000ff]/* Procedures */[/color]
[COLOR=#008080]print_matrix[/color]: [COLOR=#804040][b]procedure expose[/b][/color] v. n
  [COLOR=#0000ff]/* stem V. and number N are global variables */[/color]
  rows[COLOR=#804040][b]=[/b][/color]v.n.0.rows
  cols[COLOR=#804040][b]=[/b][/color]v.n.0.cols
  [COLOR=#804040][b]say[/b][/color] n[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]".Matrix has dimensions "[/color][COLOR=#804040][b]||[/b][/color]rows[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]" x "[/color][COLOR=#804040][b]||[/b][/color]cols[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]" :"[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]rows
    matrixrow[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]""[/color] 
    [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]cols
      [COLOR=#0000ff]/* compose one row of the n-th matrix */[/color] 
      matrixrow [COLOR=#804040][b]=[/b][/color] matrixrow[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"M"[/color][COLOR=#804040][b]||[/b][/color]n[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]"["[/color][COLOR=#804040][b]||[/b][/color]i[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]","[/color][COLOR=#804040][b]||[/b][/color]j[COLOR=#ff00ff]"]= "[/color][COLOR=#804040][b]||[/b][/color]v.n.i.j
      [COLOR=#804040][b]if[/b][/color] j [COLOR=#804040][b]<[/b][/color] cols[COLOR=#804040][b] then[/b][/color]
        matrixrow [COLOR=#804040][b]=[/b][/color] matrixrow[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]", "[/color] [COLOR=#0000ff]/* add comma */[/color]
    [COLOR=#804040][b]end[/b][/color]
    [COLOR=#804040][b]say[/b][/color] matrixrow
  [COLOR=#804040][b]end[/b][/color]
[COLOR=#804040][b]return[/b][/color]
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top