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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling an on-the-fly created variable name: Is it possible?

Status
Not open for further replies.

JohannesRS

Programmer
Jun 9, 2005
15
BR
Hi all.

Well, this is the most difficult task I've seen, and probably the answer is "it can't be done". But I need to be sure about it.

In order to avoid an extremely big and heavily zeroed matrix, whose size is fully dependent of input parameters, I came out with the idea of a looping code that would generate, according to the input needs, N matrixes of optimal size, whose names would be generated in order by the same looping. It would be something like allocating matrix1(a,b,c), matrix2(d,e,f), matrix3(g,h,i), etc, according to optimal needs.

In order to implement this, I can already see two problems: first, how could (or CAN'T) I declare this variables, if the names would be generated on the fly? and second, how could (again, or CAN'T) I call the values from this variable in the code?

I'm almost sure it just CAN'T be done. But I would like to be sure about it. If it's possible (or impossible), or anyone knows about any (probably huge) workaround for this kind of problem, it will be really helpfull.

Thanks a lot in advance,

Jones
 
You can't generate random names but you can create an array of pointers to point to all the matrices you create. In OO programming similar things are done. Very often we just work on a collection of objects. Each object will be similar to your matrix of arbitrary size.
 
Hi xwb.

Thanks for the help, but I really could not properly understand your answer this time. :) Despite not being as used to pointers as to matrices, I don't see how they could help me to avoid useless usage of memory and keep the free input defined arrays size.

To put in a real example: let's say I get matrices 1, 2 and 3 (which could freely inputed as less or more matrices) in the optimal sizes 4x4x4, 6x6x6 and 10x10x10. By optimal, I mean that with this size a minimum (possibly none) of zeroes will be needed.

If I make all them become one single matrix, the most human way of doing it would be to create a supermatrix 10x10x10x4, which would waste with inutile zeroes at least 2720 positions in memory.

That would be nothing, but my problem is that the number of matrices, sizes and differences among their sizes will easily become much greater than that. :( Despite that, the optimal "sub"-matrices sizes are easily calculated. :p

If I have wrongly understood your suggestion, xwb, please forgive me. I really need some help in trying to sort out this problem, but I could not properly understand your suggestion and decided to clarify a bit more the problem.

Please, forgive my dumbness, and thanks a lot for any extra help in advance. :)
 
I'll see if I can knock up a quick example to illustrate what I mean. May be a day or two.
 
Thanks a lot! I'll be waiting here for that. :)

Really thanks, and sorry for taking so much of your time (in all question I've already made here. :D ).
 
Something like this
Code:
module ModSparse
   type TSparse
      character*16:: ident ! something to identify when we dump
      integer:: xmax       ! max sizes
      integer:: ymax
      real, dimension(:,:), allocatable:: p
   end type
contains
   ! Allocate the space for the array
   subroutine SparseCreate(this, inIdent, inX, inY)
      type (TSparse), intent(inout):: this
      character*(*), intent(in):: inIdent
      integer, intent(in):: inX
      integer, intent(in):: inY

      this%xmax = inX
      this%ymax = inY
      this%ident = inIdent
      allocate (this%p(inX,inY))
   end subroutine

   ! Dellocate the space
   subroutine SparseDelete(this)
      type (TSparse), intent(inout):: this

      deallocate (this%p)
   end subroutine

   ! Set to sequential values starting at val
   subroutine SparseSet (this, val)
      type (TSparse), intent (inout):: this
      real, intent(in):: val
      integer:: x, y
      real:: v
      v = val
      do x = 1, this%xmax
         do y = 1, this%ymax
            this%p(x,y) = v
            v = v + 1.0
         enddo
      enddo
   end subroutine

   ! Dump the array
   subroutine SparseDump (this)
      type (TSparse), intent(in):: this
      integer:: x, y, xoff
      character*32:: fmt
      write (fmt, 10) this%ymax
10    format ('(',I2,'F8.2)')
      write (*, '(A)') this%ident
      do x = 1, this%xmax
         write(*, fmt) (this%p(x,y), y = 1, this%ymax)
      enddo
   end subroutine
end module

program main
   use ModSparse

   type (TSparse):: sss(4)
   integer:: ii

   ! Create the arrays
   call SparseCreate (sss(1), 'fiftyfive', 5, 5)
   call SparseCreate (sss(2), 'b', 3, 7)
   call SparseCreate (sss(3), 'c', 9, 2)
   call SparseCreate (sss(4), 'lastone', 3, 3)

   call SparseSet (sss(1), 10.1)
   call SparseSet (sss(2), 20.2)
   call SparseSet (sss(3), 30.3)
   call SparseSet (sss(4), 40.5)

   ! Print it
   do ii = 1, 4
      call SparseDump (sss(ii))
   enddo

   ! Tidy up
   do ii = 1, 4
      call SparseDelete (sss(ii))
   enddo
   stop
end program
 
You can also use pointers with a minor mod. Note that this does not work on the earlier versions of g95 (pre 2005). It will work on the later versions.
Code:
program main
   use ModSparse

   type (TSparse), target:: sss(4)
   type (TSparse), pointer:: ppp
   integer:: ii

   ! Create the arrays
   call SparseCreate (sss(1), 'fiftyfive', 5, 5)
   call SparseCreate (sss(2), 'b', 3, 7)
   call SparseCreate (sss(3), 'c', 9, 2)
   call SparseCreate (sss(4), 'lastone', 3, 3)

   ptr => sss(2)

   call SparseSet (sss(1), 10.1)
   call SparseSet (sss(2), 20.2)
   call SparseSet (sss(3), 30.3)
   call SparseSet (sss(4), 40.5)

   ! Print it
   do ii = 1, 4
      call SparseDump (sss(ii))
   enddo

   call SparseSet (ptr, 5.2)
   call SparseDump (ptr)

   ! Tidy up
   do ii = 1, 4
      call SparseDelete (sss(ii))
   enddo
   stop
end program
 
Thanks, xwb!

Sorry for not answering before, But really thanks. I've never thought about independent allocation of arrays into a type structure. That's a marvelous idea!

I'll make some trials on this, and let you know when I have some extra results. But this is the tip of the year! :)

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top