Higher dimensional vectors
We can extend the 3D vector to higher dimensions, in which case, we will need to use an array to store the vector’s components and pass the number of dimensions as a second parameter.
As an example, we will use a secondary package, StaticArrays
, which provides an SVector
structure to hold the components. The following is not a full definition, just defining sufficient operations to calculate the distance between two N-Vectors and give yet another estimate of pi:
module VNX using StaticArrays import Base: +, *, /, ==, <, > import LinearAlgebra: norm, dot export VecN, norm, dist struct VecN sv::SVector; end sizeof(a::VecN) = length(a.sv) sOK(a::VecN, b::VecN) = (sizeof(a) == sizeof(b)) ? true : throw(BoundsError("Vector of different lengths")); (+)(a::VecN, b::VecN) = [a.sv[i] + b.sv[i] ...