0

配列を動的に割り当てる方法についてサポートが必要です。

各ステップでサイズが変化し続ける配列があります。行ベクトルから始めて、サイクルの各ステップで別の行ベクトルを追加します。今後の計算には、この新しい配列を使用する必要があります。

配列を割り当て可能として宣言し、do-loop内でそれらを割り当てようとしましたが、正しく機能させることができませんでした。

その特定のアレイのメモリの割り当てを解除するとどうなるかはまだよくわかりません。データが失われた場合、割り当てを解除する前にデータを保存するために、別の配列を使用しますか?その配列のサイズを指定するにはどうすればよいですか?

どんな助けでも大歓迎です。

! Program to generate a matrix whose rows are linearly independent vectors. 
! The elements of the matrix are either 1 or -1

   program trial

   implicit none 
   integer, parameter ::n=10,p=5   !where 'p' gives the no. of rows, & 'n' gives the no. of columns
   real(kind=8),dimension(p,n) ::z1 
! The array to be generated - this needs to be made allocatable
! I'll need the values of this array for further calculations, so should I store
! them in another array?

   integer::i,j,k,s1  !counters used
   real(kind=8),dimension(n)::x

   do i=1,p
15  call random_number(x) !generate random row vectors 
    z1(i,:)=x

! To make the array elements 1's and -1's
    do j=1,n
        if (z1(i,j).ge.0.5) then
            z1(i,j)=1.d0
!           write(*,*),z1(i,j)
        else
            z1(i,j)=-1.d0
!           write(*,*),z1(i,j)
        endif
    enddo

! Checking the output so far
    print*,'Z1'
    do k=1,i
        write(*,*)(z1(k,j),j=1,n)
    enddo

! To ckeck for linear independence, get the reduced row echelon form. Check the rows
! of the rref array, if any of the rows contains only zeros, the vectors are not
! linearly independent. Hence, regenerate that particular row of the array. 

! Check for linear independence as each row is added

    call to_rref(z1)
        do k=1,i
            s1=0
            do j=1,n
            if(z1(k,j)==0)then
                s1=s1+1
            endif
            if (s1==n) then
                print *,'THE VECTORS ARE NOT LINEARLY INDEPENDENT'
            goto 15
            endif
            enddo
        enddo

    do k=1,i
        write(*,*)(z1(k,j),j=1,n)
    enddo
   enddo

   end

! Subroutine for getting the reduced row echelon form 
subroutine to_rref(matrix)
    implicit none
    real, dimension(:,:), intent(inout) :: matrix

    integer :: pivot, norow, nocolumn
    integer :: r, i
    real, dimension(:), allocatable :: trow

    pivot = 1
    norow = size(matrix, 1)
    nocolumn = size(matrix, 2)

    allocate(trow(nocolumn))

    do r = 1, norow
       if ( nocolumn <= pivot ) exit
       i = r
       do while ( matrix(i, pivot) == 0 )
          i = i + 1
          if ( norow == i ) then
             i = r
             pivot = pivot + 1
             if ( nocolumn == pivot ) return
          end if
       end do
       trow = matrix(i, :)
       matrix(i, :) = matrix(r, :)
       matrix(r, :) = trow
       matrix(r, :) = matrix(r, :) / matrix(r, pivot)
       do i = 1, norow
          if ( i /= r ) matrix(i, :) = matrix(i, :) - matrix(r, :) * matrix(i, pivot) 
       end do
       pivot = pivot + 1
    end do
    deallocate(trow)
  end subroutine to_rref
4

1 に答える 1

2

はい、メモリの割り当てを解除するときは、メモリがもう存在しないと見なす必要があります。Fortran 95以前では、配列を拡張する場合は、データを別の配列にコピーし、配列の割り当てを解除し、その部分を再割り当てしてコピーし直す必要があります。Fortran 2003は、プロセスを単純化する組み込みのプロシージャー、move_allocを提供します。

于 2012-09-19T01:38:54.363 に答える