Cross product over a collection of vectors (vector field)
Drafted with Claude's assistance.
cross_product(a, b) in stdlib_linalg takes two size-3 vectors and returns one size-3 vector. This issue is to track the case raised in fortran-lang/stdlib#457: computing many cross products at once, for example for a vector field on a grid.
The example from that thread was nodes x(3,n) and velocities u(3,n) on a sphere, where the user wants x × u for each of the n points. Today this needs a loop over cross_product(x(:,i), u(:,i)).
Questions I do not have answers to:
- Should there be an interface for this at all, or is the loop the intended usage?
- If so, which memory layout should it support:
(3,n),(n,3), or both? - Should it be an extra specific for
cross_product, a separate function, or take an optional argument selecting the dimension that holds the components?
I have not benchmarked the loop against any alternative, and I have not looked at how other libraries handle this beyond noting that numpy.cross accepts axis arguments.
Related: fortran-lang/stdlib#457 (scalar cross_product), fortran-lang/stdlib#970 (cross product matrix form).
Here is my proposal if you want to push this further:
- both the vector and array of vectors versions shall be publicly available. The single vector version is perfectly fine as is and calling it within a loop is perfectly reasonable.
- The issue is that the vector version will not be inlined as Fortran compilers as of today do not inline when importing a function declared in a different unit scope (not the same file). So the array of vectors version has the oportunity of offering a drop-in self-inlined version if applied at once on many vectors, since it will defined within the same stdlib module and the compiler will inline from within (force inlining options exist like
-flto, but are fragile, builds become longer, and the flag is a suggestion not an order...)
So, something in the lines of the following sketched module could offer the desired capabilities:
module stdlib_cross_product
implicit none
integer, parameter :: ilp = 4
public :: cross_product, cross
private
contains
pure function cross_product(u,v) result(w)
real, intent(in) :: u(3), v(3)
real :: w(3)
w(1) = u(2) * v(3) - u(3) * v(2)
w(2) = u(3) * v(1) - u(1) * v(3)
w(3) = u(1) * v(2) - u(2) * v(1)
end function
subroutine cross(u,v,w,axis)
real, intent(in) :: u(:,:), v(:,:)
real, intent(out):: w(:,:)
integer, intent(in), optional :: axis
integer :: axis_
logical :: ok
integer(ilp) :: i, n
axis_ = 1; if(present(axis)) axis_ = axis
! verify once that all vectors have the same shape
ok = all( shape(u) == shape(v) ) .and. all( shape(u) == shape(w) )
if(.not.ok)then
! report dimensionality problem with and optional error status
return
end if
! verify that the requested axis is of size 3
if( size(u,dim=axis_) /= 3 )then
! report dimensionality problem
return
end if
select case(axis_)
case(1)
n = size(u,dim=2)
do concurrent (i=1:n)
w(1:3,i) = cross_product(u(1:3,i),v(1:3,i))
end do
case(2)
n = size(u,dim=1)
do concurrent (i=1:n)
w(i,1:3) = cross_product(u(i,1:3),v(i,1:3))
end do
end select
end subroutine
end module