在 powershell 中有一种方法可以不用乘法将正数变成负数吗?

It there a way in powershell to make a positive number to a negative number whitout using multiplication?

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

-编辑 在这一点上,我正在使用它,但看起来计算成本非常高:

    $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

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

 PS> $v = 10; -$v
 -10

适用于您的情况:

 $digit = -$this.data[$i,$j]

顺便说一句:如果性能很重要,您可以 通过使用 ..range operator 来创建要迭代的索引来加快循环,尽管以内存消耗为代价:

$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