Skip to content

VecForVector algebra for Fortran

One vector type, written as you write the maths: a + b, 2 * a, a .dot. b, a .cross. b. Lengths, angles, face normals, distances, projections, rotations and mirrors, in single, double or quadruple precision, on whole arrays at once, and inside an OpenACC kernel.

VecFor

a tetrahedron with its outward unit normals, a point q outside it and its foot on the slanted face

The tetrahedron of the tutorial: its outward face normals, a point q and its foot on the slanted face. Every figure of these pages is computed by VecFor, projected onto the page with its own dot product, and drawn by foresight.

Quick start ​

A triangle in space, its normal and area, an angle, a point above it, its distance and its foot: this is a whole program.

f90
program quickstart
!< Quick start: a triangle in space, its normal and area, a point above it, its distance and its foot.
use penf, only : R8P
use vecfor, only : angle, ex, ey, ez, face_normal3, vector
implicit none
type(vector) :: a, b, c ! the corners of a triangle
type(vector) :: n       ! its normal
type(vector) :: q       ! a point
type(vector) :: foot    ! its projection onto the plane of the triangle

a = ex
b = ey
c = ez
n = face_normal3(pt1=a, pt2=b, pt3=c)                 ! as long as the triangle is large
call n%printf(prefix='normal   = ')
print '(A,F7.4)', 'area     = ', n%normL2()
print '(A,F7.4)', 'angle    = ', angle(b - a, c - a) * 180 / acos(-1._R8P)
q = 0.8_R8P * ex + 0.6_R8P * ey + 0.8_R8P * ez
print '(A,F7.4)', 'distance = ', q%distance_to_plane(pt1=a, pt2=b, pt3=c)
foot = q%projection_onto_plane(pt1=a, pt2=b, pt3=c)
print '(A,3F7.4)', 'foot     = ', foot%x, foot%y, foot%z
endprogram quickstart
console
$ quickstart
normal   = +0.5, +0.5, +0.5
area     =  0.8660
angle    = 60.0000
distance =  0.6928
foot     =  0.4000 0.2000 0.4000

Each figure is the output of a program of the docs; the title leads to the recipe or the chapter that computes it.

Where next ​

Learn VecFor step by step in the tutorial, find quick answers in the cookbook, look up every operator and method in the reference.

Authors ​

Contributions are welcome — see the Contributing page.

Released under the GPL v3, BSD 2-Clause, BSD 3-Clause or MIT License.