A simple vector library written in plain C for educational purpose.
Introduction
This library is written in hope to simplify the coding process for non-professional vector computation.
This library should not be used for industrial or scientific use due to the limited precision and performance; Complex number is not supported either.
Usage
You may copy the files in
./includeto your project directory and compile it seperately;You may generate a single head file
vector.hfor your project:1
$ make single
This generate a single head file in the project root directory
./. Copy the./vector.hto your project and include it.
Example
This is a brief demonstration on the commonly used functions’ usage. The output of each statement follows in comments.
Several practical examples are available in ./example.
1 |
|
Type
vector
1 | typedef struct Vector{ |
The vector type contains components in three dimensions and the overall magnitude. All these data are in double type. Each time an operation is performed to a vector, the magnitude would update automatically.
You may get a component directly:
1 | vector V = vecSet(1.1, 2.2, 3.3); |
Functions
Arithmetic
Addition
vecAdd(A, B) returns the sum: A + B.
Subtraction
vecSub(A, B) returns the difference: A - B.
Negative
vecNeg(V) returns the negative: - V.
Modulus
vecMod(V) returns the modulus: | V |.
Unit Vector
vecUnit(V) returns the unit vector: ^ V.
Scalar Product
vecScl(V, s) returns the vector s * V.
Dot Product
vecDot(A, B) returns the dot product: A · B.
Cross Product
vecCrx(A, B) returns the Cross product: A × B.
Utilities
Initialization
There’re two ways to initialize a vector:
vecSet(x, y, z)returns a vector specify the coordinates (x,y,z).vecInit()returns a vector with coordinates all of0.0. However, you should usevecSet(0.0, 0.0, 0.0)instead for better readability.
Pretty Print
vecPrint(V)prints the vector in the form of<x, y, z> | modulus |, 2 decimal places by defaultvecnPrint(V, p)using the specified precisionp. If p is 0, print in exponential form.