'It there a way in powershell to make a positive number to a negative number whitout using multiplication?
I was wondering if there is a way to make a positive number into a negative number whitout using a multiplication like $b = $a * -1
I'm looking for the most cost sensible way because I'm gonna do this a lot of times in a script.
-edit At this point I'm using this, but lookes very costly computation wise:
$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
Solution 1:[1]
To unconditionally turn a positive number into its negative equivalent (or, more generally, flip a number's sign), simply use the unary -
operator:
PS> $v = 10; -$v
-10
Applied to your case:
$digit = -$this.data[$i,$j]
As an aside: If performance matters, you can speed up your loops by using ..
, the range operator to create the indices to iterate over, albeit at the expense of memory consumption:
$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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |