A PowerShell module providing a Vector class and cmdlet-style wrapper
functions for common vector arithmetic: addition, subtraction, dot product,
magnitude, and normalization.
Clone the repo and import the module manifest:
git clone https://github.com/coffeyaveryon-spec/VectorOps-PS.git
Import-Module .\VectorOps-PS\VectorOps-PS.psd1| Cmdlet | Description |
|---|---|
New-Vector |
Creates a Vector from an array of numeric components |
Add-Vector |
Component-wise sum of two vectors |
Get-VectorDifference |
Component-wise difference of two vectors |
Get-DotProduct |
Scalar (dot) product of two vectors |
Get-VectorMagnitude |
Euclidean length of a vector |
Get-NormalizedVector |
Unit-length vector in the same direction |
$v1 = New-Vector -Components @(1, 2, 3)
$v2 = New-Vector -Components @(4, 5, 6)Add-Vector -Vector1 $v1 -Vector2 $v2
# (5, 7, 9)
Get-VectorDifference -Vector1 $v2 -Vector2 $v1
# (3, 3, 3)Get-DotProduct -Vector1 $v1 -Vector2 $v2
# 32$v = New-Vector -Components @(3, 4)
Get-VectorMagnitude -Vector $v
# 5$v = New-Vector -Components @(3, 4)
$unit = Get-NormalizedVector -Vector $v
$unit.Components
# 0.6
# 0.8The cmdlets are thin wrappers around a Vector class exposed by the module.
You can use the class directly if you prefer:
using module .\VectorOps-PS\VectorOps-PS.psd1
$a = [Vector]::new(@(1, 2, 3))
$b = [Vector]::new(@(4, 5, 6))
$a.Add($b) # (5, 7, 9)
$a.Subtract($b) # (-3, -3, -3)
$a.Dot($b) # 32
$a.Magnitude() # 3.7416573867739413
$a.Normalize() # unit vector in the direction of $aOperations between vectors of different dimensions throw an
ArgumentException, and normalizing a zero-length vector throws an
InvalidOperationException:
$a = New-Vector -Components @(1, 2, 3)
$b = New-Vector -Components @(1, 2)
Add-Vector -Vector1 $a -Vector2 $b
# ArgumentException: Vectors must have the same number of dimensions.
$zero = New-Vector -Components @(0, 0)
Get-NormalizedVector -Vector $zero
# InvalidOperationException: Cannot normalize a zero-length vector.Tests are written with Pester (v5+).
Invoke-Pester -Path .\Tests\VectorOps-PS.Tests.ps1