Numbers are great!
When we measure things with one number, it's technically called a scalar.
When we measure things with more than one number, it's called a vector
We can do lots of things with vectors. We can add or substract them, multiply and divide them.
Vectors are very useful.
This module helps you use Vectors in PowerShell
Vectors are actually built into PowerShell.
Because PowerShell is built atop of the .NET Framework, and the .NET Framework has had vector support for over a decade, PowerShell has had vectors for over a decade.
# Create a 2D vector
[Numerics.Vector2]::new(1,2)
# Create a 3D vector
[Numerics.Vector3]::new(1,2,3)
# Create a 4D vector
[Numerics.Vector4]::new(1,2,3,4)This module exists to make vectors a bit more useful by providing commands to construct them.
There are a few commands in this module:
Get-VectorGet-Vector2Get-Vector3Get-Vector4
Each command constructs a vector of the corresponding dimension.
A Vector with one dimension is just a list.
We can also drop the Get and just refer to them by vector number
Vector2 1 2
Vector3 1 2 3
Vector4 1 2 3 4We can be even shorter, and use V2, V3, and V4
v2 1 2
v3 1 2 3
v4 1 2 3 4We can turn anything into a series of vectors.
v2 1
v3 1
v4 1Strings can become vectors, too! (after all, each byte is already a number)
v2 "hi"
v3 "hi"
v4 "hi".NET vectors are very powerful, and overload many operators.
For example, we can add, subtract, multiply, or divide by a scalar.
# Let's start with addition.
# We can add a scalar to a vector.
(v2 1 2) + 1
(v3 1 2 3) + 1
(v4 1 2 3 4) + 1
# Let's try substraction:
(v2 1 2) - 1
(v3 1 2 3) - 1
(v4 1 2 3 4) - 1
# How about multiplication?
(v2 1 2) * 2
(v3 1 2 3) * 2
(v4 1 2 3 4) * 2
# What about division?
(v2 1 2) / 2
(v3 1 2 3) / 2
(v4 1 2 3 4) / 2We can also work with other vectors:
# Adding vectors:
(v2 1 2) + (v2 1 2)
(v3 1 2 3) + (v3 1 2 3)
(v4 1 2 3 4) + (v4 1 2 3 4)
# Subtracting vectors:
(v2 1 2) - (v2 1 2)
(v3 1 2 3) - (v3 1 2 3)
(v4 1 2 3 4) - (v4 1 2 3 4)
# Multiplying vectors:
(v2 1 2) * (v2 1 2)
(v3 1 2 3) * (v3 1 2 3)
(v4 1 2 3 4) * (v4 1 2 3 4)
# Dividing vectors:
(v2 1 2) / (v2 1 2)
(v3 1 2 3) / (v3 1 2 3)
(v4 1 2 3 4) / (v4 1 2 3 4)Vectors have a large number of methods to work with.
Let's start simple, by calculating the length of a given vector.
(v2 1 1).Length()
(v3 1 1 1).Length()
(v4 1 1 1 1).Length()Many of the most useful things we can do with a vector are exposed as a static methods:
(v2 1 1) | Get-Member -Static
(v3 1 1 1) | Get-Member -Static
(v4 1 1 1 1) | Get-Member -StaticWe can access static method with ::
For a small example, let's find the distance between vectors:
$vector1 = v2 1 2
$vector2 = v2 2 1
$vector1::Distance($vector1, $vector2)For another simple example, let's find a few point between two points, using
Linear Interpolation lerp
$vector1 = v2 1 5
$vector2 = v2 1 -5
$vector1::Lerp($vector1, $vector2, 0.25)
$vector1::Lerp($vector1, $vector2, 0.5)
$vector1::Lerp($vector1, $vector2, 0.75)All of this would not be possible without the great work of the .NET team to build such incredibly useful data structures.
Hopefully this module helps us all work with vectors!
You can install Vector from the PowerShell gallery
Install-Module Vector -Scope CurrentUser -ForceOnce installed, you can import the module with:
Import-Module Vector -PassThruYou can also clone the repo and import the module locally:
git clone https://github.com/PowerShellWeb/Vector
cd ./Vector
Import-Module ./ -PassThruVector has 1 function
Gets a vector in one, two, three, or four dimensions.
This will convert a variety of types into numbers.
Vector1will return a list of numbersVector2will return a list of[Numerics.Vector2]Vector3will return a list of[Numerics.Vector3]Vector4will return a list of[Numerics.Vector4]
Notes
This attempts to convert any type into a number.
Some types are special:
- Primitive types will be casted to float
[Numerics.Vector2],[Numerics.Vector3],[Numerics.Vector4]output each component[string]s that match a range ($start..$end) will output that range[Version]s will output each numeric component[semver]s will output each numeric component, followed by the bytes of a release type[Numerics.Matrix3x2]and[Numerics.Matrix4x4]will return the numbers in the matrix[DateTime]and[DateTimeOffset]will become a series of 12 numbersyear,month,dayhour,minute,secondmillisecond,microsecond,nanosecondoffset.hours,offset.minutes,offset.seconds
[string]swill return their bytes in the current$outputEncoding- Anything unknown will be stringified and the bytes will be returned
Examples
Create a vector out of two numbers
Vector2 1 2(Vector2 1 2) + (Vector2 2 1)(Vector2 1 2) - (Vector2 2 1)Create a thousand vectors
$vectors = Vector2 1..2kbCreate a thousand vectors in random order, using the pipeline
$vectors = 1..2kb | Get-Random -Count 2kb | Vector2Create a vector from a string
$vector = Vector2 "hi"Create a vector out of two numbers
Vector3 1 2 3(Vector3 1 2 3 ) + (Vector3 3 2 1)(Vector3 1 2 3 ) - (Vector3 3 2 1)Create a thousand vectors
$vectors = Vector3 1..3kbCreate a thousand vectors in random order, using the pipeline
$vectors = 1..3kb | Get-Random -Count 3kb | Vector3Create a vector from a string
$vector = Vector3 "hi"Create a vector out of four numbers
Vector4 1 2 3 4(Vector4 1 2 3 4 ) + (Vector4 4 3 2 1 )(Vector4 1 2 3 4 ) - (Vector4 4 3 2 1)Create a thousand vectors
$vectors = Vector4 1..4kbCreate a thousand vectors in random order, using the pipeline
$vectors = 1..4kb | Get-Random -Count 4kb | Vector4Create vectors from a string
Vector4 "hi"