日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

ndarray python 映射_在Matlab的delsq演示中,用numpy方法对向量映射进行ndarray处理?...

發布時間:2024/7/23 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ndarray python 映射_在Matlab的delsq演示中,用numpy方法对向量映射进行ndarray处理?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面復制了演示。G中的編號是不同的,但數字只是標簽(標簽網格讓我困惑)。在import numpy as np

from scipy import sparse

from scipy.sparse import linalg

import matplotlib.pyplot as plt

def numgrid(n):

"""

NUMGRID Number the grid points in a two dimensional region.

G = NUMGRID('R',n) numbers the points on an n-by-n grid in

an L-shaped domain made from 3/4 of the entire square.

adapted from C. Moler, 7-16-91, 12-22-93.

Copyright (c) 1984-94 by The MathWorks, Inc.

"""

x = np.ones((n,1))*np.linspace(-1,1,n)

y = np.flipud(x.T)

G = (x > -1) & (x < 1) & (y > -1) & (y < 1) & ( (x > 0) | (y > 0))

G = np.where(G,1,0) # boolean to integer

k = np.where(G)

G[k] = 1+np.arange(len(k[0]))

return G

def delsq(G):

"""

DELSQ Construct five-point finite difference Laplacian.

delsq(G) is the sparse form of the two-dimensional,

5-point discrete negative Laplacian on the grid G.

adapted from C. Moler, 7-16-91.

Copyright (c) 1984-94 by The MathWorks, Inc.

"""

[m,n] = G.shape

# Indices of interior points

G1 = G.flatten()

p = np.where(G1)[0]

N = len(p)

# Connect interior points to themselves with 4's.

i = G1[p]-1

j = G1[p]-1

s = 4*np.ones(p.shape)

# for k = north, east, south, west

for k in [-1, m, 1, -m]:

# Possible neighbors in k-th direction

Q = G1[p+k]

# Index of points with interior neighbors

q = np.where(Q)[0]

# Connect interior points to neighbors with -1's.

i = np.concatenate([i, G1[p[q]]-1])

j = np.concatenate([j,Q[q]-1])

s = np.concatenate([s,-np.ones(q.shape)])

# sparse matrix with 5 diagonals

return sparse.csr_matrix((s, (i,j)),(N,N))

if __name__ == '__main__':

print numgrid(6)

print delsq(numgrid(6)).todense()

G = numgrid(32)

D = delsq(G)

N = D.shape[0]

rhs = np.ones((N,1))

u = linalg.spsolve(D, rhs) # vector solution

U = np.zeros(G.shape) # map u back onto G space

U[G>0] = u[G[G>0]-1]

plt.contour(U)

plt.show()

結果:

總結

以上是生活随笔為你收集整理的ndarray python 映射_在Matlab的delsq演示中,用numpy方法对向量映射进行ndarray处理?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。