Skip to content

Cross Product #457

Open
Open
@St-Maxwell

Description

@St-Maxwell

Hello, I tried to implement cross_product function as a component of linalg module. And a brief description is presented below. If we can have an agreement on the interface of cross_product, I will create a pull request.

Cross Product

Overview

Cross product is a binary operation on two vectors in space, denoted by . The result of is a vector that is perpendicular to both and . Let , , and being the orthonormal basis vectors in space, and can be represented by the following expressions


Then has a determinant form:

API

interface
    function cross_product(a, b) result(c)
        real, intent(in) :: a(:)
        real, intent(in) :: b(:)
        real :: c(3)
    end function
end interface

error stop when the size of a or b is not equal to 3.

Discussion

I think the vectors a and b should be passed as assumed-shape array. There is no need to explicitly define a and b as fixed-size array, i.e. real, intent(in) :: a(3), b(3). Compilers are not able to check whether the size is reasonable at compile-time since in many cases, allocatable arrays are used. Therefore, the assertion for array size is placed in the body of cross_product function, where error_stop is called. As a result, cross_product is no longer a pure function.

Based on the above discussion and consideration of simplicity, I chose an analogous way to Julia to implement cross_product.

Reference Implementations

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideaProposition of an idea and opening an issue to discuss ittopic: mathematicslinear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ...

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions