Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VectorOps-PS

A PowerShell module providing a Vector class and cmdlet-style wrapper functions for common vector arithmetic: addition, subtraction, dot product, magnitude, and normalization.

Installation

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

Cmdlets

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

Usage

Create vectors

$v1 = New-Vector -Components @(1, 2, 3)
$v2 = New-Vector -Components @(4, 5, 6)

Add and subtract

Add-Vector -Vector1 $v1 -Vector2 $v2
# (5, 7, 9)

Get-VectorDifference -Vector1 $v2 -Vector2 $v1
# (3, 3, 3)

Dot product

Get-DotProduct -Vector1 $v1 -Vector2 $v2
# 32

Magnitude

$v = New-Vector -Components @(3, 4)
Get-VectorMagnitude -Vector $v
# 5

Normalize

$v = New-Vector -Components @(3, 4)
$unit = Get-NormalizedVector -Vector $v
$unit.Components
# 0.6
# 0.8

Using the Vector class directly

The 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 $a

Errors

Operations 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.

Running the tests

Tests are written with Pester (v5+).

Invoke-Pester -Path .\Tests\VectorOps-PS.Tests.ps1

License

MIT

About

PowerShell module providing a Vector class and cmdlets for vector arithmetic: add, subtract, dot product, magnitude, and normalization.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages