Constructing a Multidimensional Differentiation Matrix

元气小坏坏 提交于 2020-01-23 17:56:05

问题


I have been trying to construct the matrix Dij, defined as

I want to plot it for points located at xi = -cos[ π (2 i + 1) / (2 N)] on the interval [-1,1] to consequentially take derivatives of a function. I am though having problems constructing the differentiating matrix Dij.

I have written a python script as:

import numpy as np 
N = 100
x = np.linspace(-1,1,N-1)
for i in range(0, N - 1):
   x[i] = -np.cos(np.pi*(2*i + 1)/2*N)

def Dmatrix(x,N):
    m_ij = np.zeros(3)
    for k in range(len(x)):
        for j in range(len(x)):
           for i in range(len(x)):
                m_ij[i,j,k] = -2/N*((k*np.sin(k*np.pi*(2*i + 1)/2*N(np.cos(k*np.pi*(2*j +1))/2*N)/(np.sin(np.pi*(2*i + 1)/2*N)))
    return m_ij

xx = Dmatrix(x,N)

This thus returns the error:

IndexError: too many indices for array

Is there a way one could more efficiently construct this and successfully compute it over all k ? The goal will be to multiply this matrix by a function and sum over j to get the first order derivative of given function.


回答1:


can be implemented as

def D(N):
    from numpy import zeros, pi, sin, cos
    D = zeros((N, N))
    for i in range(N):
        for j in range(N):
            for k in range(N):
                D[i,j] -= k*sin(k*pi*(i+i+1)/2/N)*cos(k*pi*(j+j+1)/2/N)
            D[i,j] /= sin(pi*(i+i+1)/2/N)
    return D*2/N

It could be convenient to vectorize the inner loop.

On second tought, all the procedure can be vectorized using np.einsum (at the end I have also some timing, the einsum version, of course, abysmally faster than a triple loop):

In [1]: from numpy import set_printoptions ; set_printoptions(linewidth=120)                                                             

In [2]: def D(N): 
   ...:     from numpy import zeros, pi, sin, cos 
   ...:     D = zeros((N, N)) 
   ...:     for i in range(N): 
   ...:         for j in range(N): 
   ...:             for k in range(N): 
   ...:                 D[i,j] -= k * sin(k*pi*(2*i+1)/2/N) * cos(k*pi*(2*j+1)/2/N) 
   ...:             D[i,j] /= sin(pi*(2*i+1)/2/N) 
   ...:     return D*2/N                                                                                                                 

In [3]: def E(N): 
   ...:     from numpy import arange, cos, einsum, outer, pi, sin 
   ...:     i = j = k = arange(N) 
   ...:     s_i  = sin((2*i+1)*pi/2/N) 
   ...:     s_ki = sin(outer(k,(2*i+1)*pi/2/N)) 
   ...:     c_kj = cos(outer(k,(2*j+1)*pi/2/N)) 
   ...:     return -2/N*einsum('k, ki, kj -> ij', k, s_ki, c_kj) / s_i[:,None]                                                           

In [4]: for N in (3,4,5): 
   ...:     print(D(N)) ; print(E(N)) ; print('==========') 
   ...:                                                                                                                                  
[[-1.73205081e+00  2.30940108e+00 -5.77350269e-01]
 [-5.77350269e-01  1.22464680e-16  5.77350269e-01]
 [ 5.77350269e-01 -2.30940108e+00  1.73205081e+00]]
[[-1.73205081e+00  2.30940108e+00 -5.77350269e-01]
 [-5.77350269e-01  1.22464680e-16  5.77350269e-01]
 [ 5.77350269e-01 -2.30940108e+00  1.73205081e+00]]
==========
[[-3.15432203  4.46088499 -1.84775907  0.5411961 ]
 [-0.76536686 -0.22417076  1.30656296 -0.31702534]
 [ 0.31702534 -1.30656296  0.22417076  0.76536686]
 [-0.5411961   1.84775907 -4.46088499  3.15432203]]
[[-3.15432203  4.46088499 -1.84775907  0.5411961 ]
 [-0.76536686 -0.22417076  1.30656296 -0.31702534]
 [ 0.31702534 -1.30656296  0.22417076  0.76536686]
 [-0.5411961   1.84775907 -4.46088499  3.15432203]]
==========
[[-4.97979657e+00  7.20682930e+00 -3.40260323e+00  1.70130162e+00 -5.25731112e-01]
 [-1.05146222e+00 -4.49027977e-01  2.10292445e+00 -8.50650808e-01  2.48216561e-01]
 [ 3.24919696e-01 -1.37638192e+00  2.44929360e-16  1.37638192e+00 -3.24919696e-01]
 [-2.48216561e-01  8.50650808e-01 -2.10292445e+00  4.49027977e-01  1.05146222e+00]
 [ 5.25731112e-01 -1.70130162e+00  3.40260323e+00 -7.20682930e+00  4.97979657e+00]]
[[-4.97979657e+00  7.20682930e+00 -3.40260323e+00  1.70130162e+00 -5.25731112e-01]
 [-1.05146222e+00 -4.49027977e-01  2.10292445e+00 -8.50650808e-01  2.48216561e-01]
 [ 3.24919696e-01 -1.37638192e+00  2.44929360e-16  1.37638192e+00 -3.24919696e-01]
 [-2.48216561e-01  8.50650808e-01 -2.10292445e+00  4.49027977e-01  1.05146222e+00]
 [ 5.25731112e-01 -1.70130162e+00  3.40260323e+00 -7.20682930e+00  4.97979657e+00]]
==========

In [5]: %timeit D(20)                                                                                                                    
36 ms ± 277 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [6]: %timeit E(20)                                                                                                                    
146 µs ± 777 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [7]: %timeit D(100)                                                                                                                   
4.35 s ± 30.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [8]: %timeit E(100)                                                                                                                   
7.7 ms ± 2.82 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [9]:                                                                                                                                  



回答2:


m_ij = np.zeros(3) doesn't make a three-dimensional array, it makes an array with one dimension of length 3.

In [1]: import numpy as np

In [2]: m_ij = np.zeros(3)

In [3]: print(m_ij)
[0. 0. 0.]

I suspect you want (as a simple fix)

len_x = len(x)
m_ij = np.zeros((len_x, len_x, len_x))



回答3:


Look at your x calc by itself

In [418]: N = 10 
     ...: x = np.linspace(-1,1,N-1) 
     ...: y = np.zeros(N) 
     ...: for i in range(N): 
     ...:    y[i] = -np.cos(np.pi*(2*i + 1)/2*N) 
     ...:                                                                       
In [419]: x                                                                     
Out[419]: array([-1.  , -0.75, -0.5 , -0.25,  0.  ,  0.25,  0.5 ,  0.75,  1.  ])
In [420]: y                                                                     
Out[420]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [421]: (2*np.arange(N)+1)                                                    
Out[421]: array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])
In [422]: (2*np.arange(N)+1)/2*N                                                
Out[422]: array([ 5., 15., 25., 35., 45., 55., 65., 75., 85., 95.])

I separated x and y, because otherwise it doesn't make any sense to create x and then over write it.

The y values don't look interesting because they are all just cos of odd whole multiples of pi.

Note how I use np.arange instead of looping on range.



来源:https://stackoverflow.com/questions/58792037/constructing-a-multidimensional-differentiation-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!