2016/03/21

a numpy technique

リスト内包表記を排除してif/else入り関数を2次元リストの全要素に適用
apply function, including if/else statement, to each element in 2d-list using numpy function.

use
numpy.where
numpy.fromfunction

before

    func = lambda i,j: 1.0 if (np.sqrt(np.power((i-cent2[0]),2.)+np.power((j-cent2[1]),2.))\
 <= (0.5*fwhm*fac)) else 1e-30                                         temp2 = np.float_([[func(i,j) for i in range(size2)] for j in range(size2)])            


after                        

    func = lambda i,j: np.where((np.sqrt(np.power((i-cent2[0]),2.)+np.power((j-cent2[1]),2.)\
) <= (0.5*fwhm*fac)), 1.0, 1e-30)

    temp2 = np.fromfunction(func,(size2,size2))


cpu time remarkably improved.
めんどくさいからcpu時間の計算はやってない。

0 件のコメント: