powershell中有没有一种方法可以将正数变为负数而不使用乘法?

hex*_*mal 5 powershell negative-number

我想知道是否有一种方法可以不使用乘法将正数变成负数,就像$b = $a * -1
我正在寻找最成本合理的方法一样,因为我会在脚本中多次这样做。

-edit 此时我正在使用它,但看起来计算成本非常高:

    $temp_array = New-Object 'object[,]' $this.row,$this.col

    for ($i=0;$i -le $this.row -1 ; $i++) {
        for ($j=0;$j -le $this.col -1 ; $j++) {
            $digit = $this.data[$i,$j] * -1
            $temp_array[$i,$j] = 1 / ( 1 + [math]::exp( $digit ) )
            #[math]::Round( $digit ,3)
        }
    }
    $this.data = $temp_array
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 5

要无条件地将正数转换为其等价的负数(或者更一般地说,翻转数字的符号),只需使用一元运算-

 PS> $v = 10; -$v
 -10
Run Code Online (Sandbox Code Playgroud)

适用于您的案例:

 $digit = -$this.data[$i,$j]
Run Code Online (Sandbox Code Playgroud)

顺便说一句:如果性能很重要,您可以通过使用范围运算符创建要迭代的索引来加速循环:..

$temp_array = New-Object 'object[,]' $this.row,$this.col

for ($i in 0..($this.row-1)) {
    for ($j in 0..($this.col-1)) {
        $digit = - $this.data[$i,$j]
        $temp_array[$i,$j] = 1 / ( 1 + [math]::exp( $digit ) )
    }
}
$this.data = $temp_array
Run Code Online (Sandbox Code Playgroud)