我是vector-vector-scalar multiplication。标量可以是整数类型或类型实数。我还必须允许向量与标量(v = u * scal)和反向(v = scal * u)相乘。以下代码给出了错误,因为它无法区分这两者。 一种解决方案是具有四个函数来处理整数和实数标量类型,并且每个函数都具有post和pre-multiply操作。有没有办法避免有四个功能,只有两个?
这是指定运算符(*)
!> Multiplies a vector by a scalar such that one can write
!> v = scal * u and v = u * scal.
Procedure :: vector_scalar_postmultiply
Procedure, Pass (ub) :: vector_scalar_premultiply
Generic :: Operator (*) => vector_scalar_postmultiply, &
vector_scalar_premultiply
这是后乘法函数
!> Multiplies a vector by a scalar such that one can write
!> v = scal * u and v = u * scal.
Procedure :: vector_scalar_postmultiply
Procedure, Pass (ub) :: vector_scalar_premultiply
Generic :: Operator (*) => vector_scalar_postmultiply, &
vector_scalar_premultiply
Function vector_scalar_postmultiply &
( &
u, &
scal &
) &
Result (v)
!!$ In.
Class (Vector), Intent(in) :: u
Class (*), Intent(in) :: scal
!!$ Out.
Type (Vector) :: v
Select Type (scal)
!!$ scal points to integer intrinsic type.
Type Is (Integer)
v% x = Real (scal) * u% x
v% y = Real (scal) * u% y
v% z = Real (scal) * u% z
!!$ scal points to real intrinsic type.
Type Is (Real)
v% x = scal * u% x
v% y = scal * u% y
v% z = scal * u% z
End Select
End Function vector_scalar_postmultiply
这是预乘函数
Function vector_scalar_premultiply &
( &
scalb, &
ub &
) &
Result (vb)
!!$ In.
Class (*), Intent(in) :: scalb
Class (Vector), Intent(in) :: ub
!!$ Out.
Type (Vector) :: vb
Select Type (scalb)
!!$ scal points to integer intrinsic type.
Type Is (Integer)
vb% x = Real (scalb) * ub% x
vb% y = Real (scalb) * ub% y
vb% z = Real (scalb) * ub% z
!!$ scal points to real intrinsic type.
Type Is (Real)
vb% x = scalb * ub% x
vb% y = scalb * ub% y
vb% z = scalb * ub% z
End Select
End Function vector_scalar_premultiply
错误如下
gfortran -o build/lib/foul.o -c -ffree-form -g -J./build/lib lib/foul.f
gfortran -o build/lib/vectors.o -c -ffree-form -g -J./build/lib lib/vectors.f
lib/vectors.f:214.28:
Generic :: Operator (*) => vector_scalar_postmultiply, &
1
Error: 'vector_scalar_postmultiply' and 'vector_scalar_premultiply' for GENERIC '*'
at (1) are ambiguous
scons: *** [build/lib/vectors.o] Error 1
scons: building terminated because of errors.