VectorOps-PS.psm1

class Vector {
    [double[]] $Components

    Vector([double[]] $components) {
        if ($null -eq $components -or $components.Count -eq 0) {
            throw [System.ArgumentException]::new('Vector must have at least one component.')
        }
        $this.Components = $components
    }

    [int] Dimension() {
        return $this.Components.Count
    }

    [void] AssertSameDimension([Vector] $other) {
        if ($this.Components.Count -ne $other.Components.Count) {
            throw [System.ArgumentException]::new('Vectors must have the same number of dimensions.')
        }
    }

    [Vector] Add([Vector] $other) {
        $this.AssertSameDimension($other)
        $result = [double[]]::new($this.Components.Count)
        for ($i = 0; $i -lt $this.Components.Count; $i++) {
            $result[$i] = $this.Components[$i] + $other.Components[$i]
        }
        return [Vector]::new($result)
    }

    [Vector] Subtract([Vector] $other) {
        $this.AssertSameDimension($other)
        $result = [double[]]::new($this.Components.Count)
        for ($i = 0; $i -lt $this.Components.Count; $i++) {
            $result[$i] = $this.Components[$i] - $other.Components[$i]
        }
        return [Vector]::new($result)
    }

    [double] Dot([Vector] $other) {
        $this.AssertSameDimension($other)
        $sum = 0.0
        for ($i = 0; $i -lt $this.Components.Count; $i++) {
            $sum += $this.Components[$i] * $other.Components[$i]
        }
        return $sum
    }

    [double] Magnitude() {
        $sumOfSquares = 0.0
        foreach ($c in $this.Components) {
            $sumOfSquares += $c * $c
        }
        return [math]::Sqrt($sumOfSquares)
    }

    [Vector] Normalize() {
        $mag = $this.Magnitude()
        if ($mag -eq 0) {
            throw [System.InvalidOperationException]::new('Cannot normalize a zero-length vector.')
        }
        $result = [double[]]::new($this.Components.Count)
        for ($i = 0; $i -lt $this.Components.Count; $i++) {
            $result[$i] = $this.Components[$i] / $mag
        }
        return [Vector]::new($result)
    }

    [string] ToString() {
        return '(' + ($this.Components -join ', ') + ')'
    }
}

$publicFiles = @(Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Filter '*.ps1' -ErrorAction SilentlyContinue)

foreach ($file in $publicFiles) {
    . $file.FullName
}

Export-ModuleMember -Function $publicFiles.BaseName