1. First vectors
probe starts by describing its body: the four vertices of a tetrahedron, one at the origin and three at the tips of the Cartesian axes, and a point q it will inspect in the next chapters.
f90
program probe
!< Tutorial, chapter 1: the four vertices of a tetrahedron, and a point.
use penf, only : R8P
use vecfor, only : ex, ey, ez, vector
implicit none
type(vector) :: o ! the origin
type(vector) :: p(4) ! the vertices
type(vector) :: q ! a point, set component by component
p(1) = o ! a new vector is (0, 0, 0)
p(2) = ex ! the Cartesian versors: unit vectors along x, y and z
p(3) = ey
p(4) = ez
q%x = 0.8_R8P ! the components are real(R8P)
q%y = 0.6_R8P
q%z = 0.8_R8P
call p(1)%printf(prefix='p1 = ')
call p(2)%printf(prefix='p2 = ')
call p(3)%printf(prefix='p3 = ')
call p(4)%printf(prefix='p4 = ')
call q%printf(prefix='q = ')
print '(A,F4.2)', 'the x of q is ', q%x
endprogram probeconsole
$ probe
p1 = +0.0, +0.0, +0.0
p2 = +1.0, +0.0, +0.0
p3 = +0.0, +1.0, +0.0
p4 = +0.0, +0.0, +1.0
q = +0.8, +0.6, +0.8
the x of q is 0.80use vecforbrings the typevectorand the versorsex,ey,ez, the unit vectors along x, y and z. Import withonlywhat you use: the module exports many names (one set for each precision).- A
vectoris three real components,x,yandz, of kindR8P(double precision). A new vector is(0, 0, 0): the components have default values, soois the origin without being set. - Vectors are assigned with
=and built from expressions of vectors:2 * ex + 3 * eyis the point(2, 3, 0). The next chapter has all of them. - The components are ordinary variables: read them, write them. They are
real(R8P), so write the literals with the kind_R8P(R8Pcomes from PENF, the library of kinds VecFor depends on): a plain0.8is a single precision number, andq%x = 0.8would store0.800000011920929. printfprints a vector, its components separated by commas, after an optionalprefix. Each number is written with the shortest digits that read back as the same value:+0.8is the double precision number nearest to 0.8.
Three ways to build a vector
From the versors as here, component by component as q, or with the structure constructor, vector(0.8_R8P, 0.6_R8P, 0.8_R8P): the same vector (see A vector from its components). A number assigned to a vector sets all three components: v = 1 is (1, 1, 1) (see Every component set to one number).
What you learned
use vecfor, type(vector), the versors ex, ey, ez, the components x, y, z of kind R8P, printf. Reference: The vector type.
Next: 2. Arithmetic.