-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmatrix.ps1
More file actions
301 lines (284 loc) · 9.74 KB
/
Copy pathmatrix.ps1
File metadata and controls
301 lines (284 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Scope = 'Function', Target = 'New-ComplexValue')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Scope = 'Function', Target = 'New-Matrix')]
Param()
function Format-ComplexValue {
<#
.SYNOPSIS
Utility method for rendering readable output for complex numbers
.PARAMETER WithColor
When -WithColor is used, the output will include color templates to add color (see Write-Label)
#>
[CmdletBinding()]
[OutputType([String])]
Param(
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
[System.Numerics.Complex] $Value,
[Switch] $WithColor
)
$Real = $Value.Real
$Imaginary = $Value.Imaginary
if ($Real -eq 0 -and $Imaginary -eq 0) {
'0'
} else {
$Re = if ($Real -eq 0) { '' } else { $Real }
$Sign = if ([Math]::Sign($Imaginary) -lt 0) { '-' } else { '+' }
$Op = if ($Re.Length -gt 0 -and $Imaginary -ne 0) { " $Sign " } else { '' }
$Minus = if ($Imaginary -lt 0 -and $Re.Length -eq 0) { '-' } else { '' }
$Im = if ($Imaginary -eq 0) { '' } else { [Math]::Abs($Imaginary) }
$I = if ($Imaginary -ne 0) { 'i' } else { '' }
if ($WithColor) {
$WithI = if ($Imaginary -ne 0) { "{{#cyan $I}}" } else { '' }
"${Re}${Op}${Minus}${Im}${WithI}"
} else {
"${Re}${Op}${Minus}${Im}${I}"
}
}
}
function Invoke-MatrixMap {
<#
.SYNOPSIS
Apply passed function to each element of passed matrix and return new matrix with results.
.EXAMPLE
$A = 1..4 | matrix
$AddOne = { Param($X) $X + 1 }
$B = $A | matmap $AddOne
#>
[CmdletBinding()]
[Alias('matmap')]
[OutputType([Matrix])]
Param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[Matrix] $InputMatrix,
[Parameter(Position = 0)]
[ScriptBlock] $Expression = { },
[Switch] $Strict
)
Process {
$Parameters = ($Expression | Get-ParameterList).Name
$Morphism = switch ($Parameters.Count) {
1 { [System.Func[Complex, Complex]]$Expression }
3 { [System.Func[Complex, Int, Int, Complex]]$Expression }
4 { [System.Func[Complex, Int, Int, Matrix, Complex]]$Expression }
default {
if ($Strict) {
throw 'Expression has wrong number of parameters'
}
$Identity = { Param($X) $X }
[System.Func[Complex, Complex]]$Identity
}
}
$InputMatrix.Map($Morphism)
}
}
function New-ComplexValue {
<#
.SYNOPSIS
Utility method for creating complex values
.PARAMETER Random
Return a complex value with random real and imaginary parts
.PARAMETER Bounds
Minimum and maximum values for random real and imaginary parts
.EXAMPLE
$C = New-ComplexValue 2 3
.EXAMPLE
$C = 4, -1.5 | complex
#>
[CmdletBinding(DefaultParameterSetName = 'normal')]
[Alias('complex')]
[OutputType([System.Numerics.Complex])]
Param(
[Parameter(ValueFromPipeline = $True)]
[Array] $Parts,
[Parameter(Position = 0)]
[Alias('Re')]
[Int] $Real = 0,
[Parameter(Position = 1)]
[Alias('Im')]
[Int] $Imaginary = 0,
[Parameter(ParameterSetName = 'random')]
[Switch] $Random,
[Parameter(ParameterSetName = 'random')]
[ValidateCount(2, 2)]
[Double[]] $Bounds = @(-10.0, 10.0)
)
End {
$Re, $Im = if ($Input.Count -ge 2) {
$Input[0, 1]
} else {
if ($Random) {
$Minimum, $Maximum = $Bounds
$Parameters = @{ Minimum = $Minimum; Maximum = $Maximum }
(Get-Random @Parameters), (Get-Random @Parameters)
} else {
$Real, $Imaginary
}
}
[System.Numerics.Complex]::New($Re, $Im)
}
}
function New-Matrix {
<#
.SYNOPSIS
Utility wrapper function for creating matrices
.DESCRIPTION
New-Matrix is a wrapper function for the [Matrix] class and is intended to reduce the effort required to create [Matrix] objects.
Use "New-Matrix | Get-Member" to see available methods:
Adj: Return matrix adjugate ("classical adjoint")
> Note: Available as a class method - [Matrix]::Adj
Det: Return matrix determinant (even matrices larger than 3x3
> Note: Available as a class method - [Matrix]::Det
Dot: Return dot product between matrix and one other matrix (with compatible size)
> Note: Available as a class method - [Matrix]::Dot
Clone: Return new matrix with identical values as original matrix
Cofactor: Return cofactor for given row and column index pair
> Example: $Matrix.Cofactor(0, 1)
Indexes: Return list of "ij" pairs (useful for iterating through matrix values)
> Example: (New-Matrix).Indexes() | ForEach-Object { "(i,j) = ($($_[0]),$($_[1]))" }
Inverse: Return matrix inverse (Note: Det() must return non-zero value)
> Note: Available as a class method - [Matrix]::Invert
Multiply: Return result of multiplying matrix by scalar value (ex: 42)
> Note: Available as a class method - [Matrix]::Multiply
RemoveColumn: Return matrix with selected column removed
RemoveRow: Return matrix with selected column removed
Transpose: Return matrix transpose
> Note: Available as a class method - [Matrix]::Transpose
Trace: Return matrix trace (sum of diagonal elements)
> Note: Available as a class method - [Matrix]::Trace
*** All methods that return a [Matrix] object provide a "fluent" interface and can be chained ***
*** All methods are "non destructive" and will return a clone of the original matrix (when applicable) ***
.PARAMETER Size
Size = @(number of rows, number of columns)
.PARAMETER Diagonal
Add values to matrix along diagonal
.PARAMETER Unit
Create unit matrix with size, -Size
.PARAMETER Random
Create random matrix with size, -Size
.PARAMETER Bounds
Minimum and maximum values for matrix random complex values
.EXAMPLE
$Matrix = 1..9 | matrix 3,3
#>
[CmdletBinding(DefaultParameterSetName = 'normal')]
[Alias('matrix')]
[OutputType([Matrix])]
Param(
[Parameter(ValueFromPipeline = $True)]
[Array] $Values,
[Parameter(Position = 0)]
[Array] $Size = @(2, 2),
[Switch] $Diagonal,
[Switch] $Identity,
[Switch] $Unit,
[Switch] $Custom,
[Parameter(ParameterSetName = 'random')]
[Switch] $Random,
[Parameter(ParameterSetName = 'random')]
[ValidateCount(2, 2)]
[Double[]] $Bounds = @(-10.0, 10.0)
)
Begin {
function Update-Matrix {
Param(
[Matrix] $Matrix,
[String] $MatrixType,
[Array] $Values
)
switch ($MatrixType) {
'Diagonal' {
$Values = $Values | Invoke-Flatten
$Index = 0
foreach ($Pair in $Matrix.Indexes()) {
$Row, $Column = $Pair
if ($Row -eq $Column) {
$Matrix[$Row][$Column] = $Values[$Index]
$Index++
}
}
break
}
'Custom' {
$Matrix.Rows = $Values | Invoke-Flatten
}
default {
# Do nothing
}
}
}
$M, $N = if ($Size.Count -eq 1) { $Size * 2 } else { $Size }
$Matrix = New-Object 'Matrix' @($M, $N)
$MatrixType = Find-FirstTrueVariable 'Custom', 'Diagonal', 'Identity', 'Unit', 'Random'
if ($Values.Count -gt 0) {
Update-Matrix -Values $Values -Matrix $Matrix -MatrixType $MatrixType
}
}
End {
$Values = $Input
if ($Values.Count -gt 0) {
Update-Matrix -Values $Values -Matrix $Matrix -MatrixType $MatrixType
} else {
switch ($MatrixType) {
'Unit' {
$Matrix = [Matrix]::Unit($M, $N)
break
}
'Identity' {
$Matrix = [Matrix]::Identity($M)
break
}
'Random' {
$Matrix.Rows = 1..($M * $N) | ForEach-Object {
New-ComplexValue -Random -Bounds $Bounds
}
break
}
default {
# Do nothing
}
}
}
$Matrix
}
}
function Test-Matrix {
<#
.SYNOPSIS
Test if a matrix is one or more of the following:
- Diagonal
- Square
- Symmetric
.EXAMPLE
1..4 | New-Matrix 2,2 | Test-Matrix -Square
# True
.EXAMPLE
1..4 | New-Matrix 2,2 | Test-Matrix -Square -Diagonal
# False
#>
[CmdletBinding()]
[OutputType([Bool])]
Param(
[Parameter(Position = 0, ValueFromPipeline = $True)]
$Value,
[Switch] $Diagonal,
[Switch] $Hermitian,
[Switch] $Square,
[Switch] $Symmetric
)
if ($Value.GetType().Name -eq 'Matrix') {
$Result = $True
if ($Diagonal) {
$Result = $Result -and $Value.IsDiagonal()
}
if ($Hermitian) {
$Result = $Result -and $Value.IsHermitian()
}
if ($Square) {
$Result = $Result -and $Value.IsSquare()
}
if ($Symmetric) {
$Result = $Result -and $Value.IsSymmetric()
}
return $Result
}
$False
}