0

2 개의 배열 사이의 스칼라 곱을 계산하는 프로그램을 수행하고 있습니다. 결과는 스칼라 실제 값입니다. 내 솔루션 차원을 (1,1)입니다 매트릭스를 사용하는 것입니다. 이 문제는 없지만 줄을 추가하기 만하면됩니다.FORTRAN : 전달하는 동안 실수가 실수 (:, :

res=tmp_2(1,1) 

세그먼트 결함이 있습니다. 라는

call product(u, v, m0, 3, res) 

print*, "The result is ", res 

call product(u, u, m0, 3, umod) 

umod=sqrt(umod) 

call product(v, v, m0, 3, vmod) 

vmod=sqrt(vmod) 

angle=acos(res/(umod*vmod)) 

print*, "The angle is: ", angle 

contains 

subroutine product(v, u, metr, n, res) 

real, dimension(:,:), intent(in)  :: metr 
real, dimension(:), intent(in)   :: u, v 
integer, intent(in)      :: n 

real, optional, intent(out)    :: res 

real, dimension(:,:), allocatable  :: v_t, tmp, tmp_1, tmp_2 
integer         :: i, j 

allocate(tmp(n,1)) 
allocate(tmp_1(n,1)) 
allocate(tmp_2(1,1)) 
allocate(v_t(1,n)) 

call vec_mat(v, tmp, n)   !convert vector(n) to matrix(n,1) to be able to 
           !operate with my multiplication subroutine 
call vec_mat(u, tmp_1, n) 

call mattrans(tmp, n, 1, v_t) !matrix transposition 

deallocate(tmp) 
allocate(tmp(1,n)) 

call matmul(v_t, metr, 1, n, n, tmp)  !matrixes moltiplication 

call matmul(tmp, tmp_1, 1, n, 1, tmp_2) 

res=tmp_2(1,1) !!!!!!!!!HERE IS THE PROBLEM!!!    <--------------- 

end subroutine product 

모든 서브 루틴 모듈에 있지만, 모두의 테스트 및 작품 : 코드입니다.

P. 내 서버는 스페인어이므로 실제 오류는 "세그먼트 오류 (Violacion de segmento ('core'generado) ')입니다.

+2

은 'res'가 실제로 존재합니까? –

+0

내가 선언했다는 뜻입니까? 그래, 내가 했어. –

+1

아니요, 선택적 인수입니다. 인수 목록에 있어야합니다. –

답변

2

코드의 문제가있는 줄을

이 변경이됩니다
if (present(res)) res = tmp_2(1,1) 

로 변경해야합니다, 그래서 당신은 그것을가 있는지 테스트없이 선택적 인수를 호출해서는 안, 문제가 계속 발생합니까?

+0

그게 문제를 해결했습니다! 선택적 인수를 사용하는 것은 처음이므로 컨트롤에 대해 알지 못했습니다! 고마워요! –