Vector Calculus Rust Library


Guide

Vectors

Vectors here represent a vector in either $\R^2$ or $\R^3$, depending which object you are using. Vectors can be obtained as the result from a vector function, a parametric curve, an operation, etc.

To create a vector, you can use the vector! macro. The type of vector that comes from this depends on if you input 2 or 3 numbers. However, the type for both of this is Vector.

let u:Vector = vector!(2, 3);
let v:Vector = vector!(3.5, PI, 2);

If you want to access the $x$ and $y$ coordinates, you can do it as v.x() or v.y() , although the $z$ coordinate in 2D vectors is always 0.

There’s also operations you can do, for example the dot product between two vectors, expressed as u*v .

Along side scalar product, there’s also the cross product written as u%v , which always returns a Vector in $\R^3$.

To take the modulus of a vector, there are 3 ways:

  1. modulus(&v) the function by reference
  2. md!(v) the macro
  3. !v , the not operator !, which when applied to vectors it returns the modulus.

Scalar Functions

You can also create scalar functions $f(x,y)$ or $f(x,y,z)$. For this, use the f! macro, which depending on the arguments you pass, the type of the function: $\R^2$ or $\R^3$.

let f:Function = f!(x, y, x*y);
let g:Function = f!(x, y, z, x.powi(2)*y + z);
let _:f64 = g(1., 2., 2.);

When you create a scalar function, the expression is automatically saved as a string, which you can access as f.expression() , and functions can be cloned with f.clone(). To evaluate a function, run f(1.,2.), for example, which means these evaluate like regular Rust functions — it’s worth noting that aside from that, you can also pass a Vector as argument to a scalar function and it will evaluate perfectly as long as the dimensions match.

Derivatives