- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When I use a array in a function with fortran,if i use teo array at a statement,sometimes it doesn't.
function f
integer,intent(in)::n
real(rkind)::f(n,n)
real(rkind),intent(in)::a(n,n)
integer::i,j
real(rkind)::b(n,n),c(n),d
do j=2,n
do i=1,j-1
d=b(i,j)/b(j,j)
b(i,:)=b(i,:)-d*b(j,:)
f(i,:)=f(i,:)-d*f(j,:) !This code Works properly
end do
end do
do j=2,n
do i=1,j-1
b(i,:)=b(i,:)-b(i,j)/b(j,j)*b(j,:)
f(i,:)=f(i,:)-d*f(j,:) !This code doesn't work
end do
end do
end function f
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In your code, the value of b(I,j)/b(j,j) is not the same on line 47 as on line 46 because you change b(I,j) on line 46.
You're using d on lines 20 and 21, which is the same on both lines.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can we assume the arrays b and f are local variables?
You dont show any initialization for b or f so they could have any value.
Exactly what do you mean by it working or not working?
Please supply a self contained example we could compile.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is the code,in this code,variable 'b' and 'c' should have the same value,but it dosen't.And the correct one should be 'b'. Function 'f1' and 'f2' just have one statement which is different,and I have note that place.The different statement should have same effect,but they are not now.(my English is poor,If I have some wrong in my reply ,plseae forgive me)
module test
contains
function f1(a,n)
integer,intent(in)::n
real(16)::f1(n,n)
real(16),intent(in)::a(n,n)
integer::i,j,m
real(16)::b(n,n),c(n),d
b=a
f1=0
do i=1,n
f1(i,i)=1
end do
do j=1,n
do i=j+1,n
d=b(i,j)/b(j,j)
b(i,j:)=b(i,j:)-d*b(j,j:) !different place
f1(i,:)=f1(i,:)-d*f1(j,:)
end do
end do
end function f1
function f2(a,n)
integer,intent(in)::n
real(16)::f2(n,n)
real(16),intent(in)::a(n,n)
integer::i,j,m
real(16)::b(n,n),c(n),d
b=a
f2=0
do i=1,n
f2(i,i)=1
end do
do j=1,n
do i=j+1,n
b(i,j:)=b(i,j:)-b(i,j)/b(j,j)*b(j,j:) !different place,and just replace 'd'with'b(i,j)/b(j,j)'
f2(i,:)=f2(i,:)-b(i,j)/b(j,j)*f2(j,:)
end do
end do
end function f2
end module test
program main
use test
implicit none
integer,parameter::n=3
real(16)::x(n),y(n),a(n,n),b(n,n),c(n,n)
a=reshape([2,4,-2,1,1,2,1,0,1],[3,3])
b=f1(a,n)
c=f2(a,n)
write(*,*)b
write(*,*)
write(*,*)
write(*,*)
write(*,*)
write(*,*)
write(*,*)c
read(*,*)
end program main
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I ran your example and B /= C. A changed the function f1 and f2 to subroutines. The results are exactly the same.
I changed real(16) to real(8). The results are the same.
I think the logic of calculation in the loops is incorrect. I don't think this is a compiler problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In your code, the value of b(I,j)/b(j,j) is not the same on line 47 as on line 46 because you change b(I,j) on line 46.
You're using d on lines 20 and 21, which is the same on both lines.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page