Arithmetic/Complex: Difference between revisions

no edit summary
(Added PowerShell)
No edit summary
Line 3,497:
 
</lang>
 
=={{header|PowerShell}}==
{{works with|PowerShell|5}}
<lang PowerShell>
class Complex {
[Double]$x
[Double]$y
Complex() {
$this.x = 0
$this.y = 0
}
Complex([Double]$x, [Double]$y) {
$this.x = $x
$this.y = $y
}
[Double]abs2() {return $this.x*$this.x + $this.y*$this.y}
[Double]abs() {return [math]::sqrt($this.abs2())}
static [Complex]add([Complex]$m,[Complex]$n) {return [Complex]::new($m.x+$n.x, $m.y+$n.y)}
static [Complex]mul([Complex]$m,[Complex]$n) {return [Complex]::new($m.x*$n.x - $m.y*$n.y, $m.x*$n.y + $n.x*$m.y)}
[Complex]mul([Double]$k) {return [Complex]::new($k*$this.x, $k*$this.y)}
[Complex]negate() {return $this.mul(-1)}
[Complex]conjugate() {return [Complex]::new($this.x, -$this.y)}
[Complex]inverse() {return $this.conjugate().mul(1/$this.abs2())}
[String]show() {
if(0 -le $this.y) {
return "$($this.x)+$($this.y)i"
} else {
return "$($this.x)$($this.y)i"
}
}
static [String]show([Complex]$other) {
return $other.show()
}
}
$m = [complex]::new(3, 4)
$n = [complex]::new(7, 6)
"`$m: $($m.show())"
"`$n: $($n.show())"
"`$m + `$n: $([complex]::show([complex]::add($m,$n)))"
"`$m * `$n: $([complex]::show([complex]::mul($m,$n)))"
"negate `$m: $($m.negate().show())"
"1/`$m: $([complex]::show($m.inverse()))"
"conjugate `$m: $([complex]::show($m.conjugate()))"
</lang>
<b>Output:</b>
<pre>
$m: 3+4i
$n: 7+6i
$m + $n: 10+10i
$m * $n: -3+46i
negate $m: -3-4i
1/$m: 0.12-0.16i
conjugate $m: 3-4i
</pre>
=={{header|PowerShell}}==
{{works with|PowerShell|5}}
678

edits