如果你可以使用numpy和INF的值是該則是0的任何項目接受:
import numpy as np
a = np.array([[70,52,13,67],[90,48,57,26],[43,45,67,89],[88,65,44,0]],
dtype = np.float32)
>>>
>>> 1/a
array([[ 0.01428571, 0.01923077, 0.07692308, 0.01492537],
[ 0.01111111, 0.02083333, 0.01754386, 0.03846154],
[ 0.02325581, 0.02222222, 0.01492537, 0.01123596],
[ 0.01136364, 0.01538462, 0.02272727, inf]], dtype=float32)
>>> a**-1
array([[ 0.01428571, 0.01923077, 0.07692308, 0.01492537],
[ 0.01111111, 0.02083333, 0.01754386, 0.03846154],
[ 0.02325581, 0.02222222, 0.01492537, 0.01123596],
[ 0.01136364, 0.01538462, 0.02272727, inf]], dtype=float32)
>>> pow(a, -1)
array([[ 0.01428571, 0.01923077, 0.07692308, 0.01492537],
[ 0.01111111, 0.02083333, 0.01754386, 0.03846154],
[ 0.02325581, 0.02222222, 0.01492537, 0.01123596],
[ 0.01136364, 0.01538462, 0.02272727, inf]], dtype=float32)
>>>
>>> inverse_of_a = 1/a
>>> inverse_of_a
array([[ 0.01428571, 0.01923077, 0.07692308, 0.01492537],
[ 0.01111111, 0.02083333, 0.01754386, 0.03846154],
[ 0.02325581, 0.02222222, 0.01492537, 0.01123596],
[ 0.01136364, 0.01538462, 0.02272727, inf]], dtype=float32)
>>>
請檢查您的壓痕!由於複製和粘貼而丟失了嗎? –
@noazet'^'是按位運算符而不是運算符。您可以使用'**'操作符作爲電源操作員。 http://en.wikipedia.org/wiki/Bitwise_operation#XOR – ajay
[Sage](http://www.sagemath.org)使用Python和一些額外的語法糖。所以在這種情況下'^'實際上是指數運算(而^^'是xor)。我喜歡自己下面的列表理解方法。 – kcrisman