簡體   English   中英

為d維的i階偏導數生成模式

[英]Generate a pattern for i-th order partial derivative in d-dimension

我正在嘗試制定一個有限元代碼,在這里我需要計算d維的偏導數。 在有限元中,基函數N(x,y,z)= N(x)N(y)N(z),因此一階導數為:

N(x)'N(y)N(z) N(x)N(y)'N(z) N(x)N(y)N(z)'

二階導數是

N(x)''N(y)N(z) N(x)'N(y)'N(z) N(x)'N(y)N(z)' N(x)N(y)''N(z) N(x)N(y)N(z)' N(x)N(y)N(z)''

我想要一個帶有輸入(i,d)的函數來告訴我下表中的這些模式:

在此處輸入圖片說明

我認為必須有一個簡單的算法來實現這一目標。 有人可以給我些幫助嗎? 謝謝

將第i個導數的模式視為基i+1整數。 漂亮的序列出現了。 例如,在二維情況下,它們是

0
2, 1
6, 4, 2
12, 9, 6, 3
20, 16, 12, 8, 4

等等

這可以通過嵌套循環來解決:

int * part_deriv(int i, int d){
    int *res;
    int *curr;
    int num_el = fact(i+d-1) / ( fact(i) * fact(d-1) ); //fact() is the factorial function
    int el_size = d*sizeof(int);
    int el;

    res = calloc(num_el,el_size);
    curr = calloc(d,sizeof(int));
    *curr = i;
    memcpy(res,curr,el_size); //put the first element in the array

    el = 0;
    while(el<num_el){
       if(*curr != 0){
           for( d_idx = 1 ; d_idx<d ; d_idx++, *cur++){
              *curr--; // "move" one derivative from the first variable to 'd_idx' variable
              *(curr+d_idx)++;
              el++;
              memcpy(res+(el*el_size),curr,el_size); //put the element in the array
           }
           *curr--;
       } else{
           break; //shouldn't be reached, but added to be sure
       }
    }
    return res;
}

我還沒有完全理解您要如何輸出結果,因此可以將我輸出的數組解析為d塊。

我通過調用一個函數以遞歸的方式實現了它。

#include <vector>
#include <iostream>
using namespace std;


void func (int d, int i, vector<int> &k, int n, int start, vector<vector<int>> &a){
    if (n==i)
    {
        vector<int> m;
        int it=0;
            for(int it1=0;it1<d;++it1){
                int amount=0;
                while(find(k.begin(),k.end(),it)!= k.end()){
                    amount++;
                    it++;
                }
                m.push_back(amount);
                it++;
            }
        a.push_back(m);
    }
    else{
        for(int jj=start;jj<d+i-(i-n);++jj){
            k[n]=jj;
            func(d,i,k,n+1,jj+1, a
                 );
        }
    }
}
vector<vector<int>> test(int d, int i){
    vector<int> kk(i);
    vector<vector<int>> a;
    func(d,i,kk,0,0, a);
    return a;
}
int main(){
    auto f = test(4,2);
    for(auto it1=f.begin();it1!=f.end();++it1){
        for(auto it2= it1->begin();it2!=it1->end();++it2)
            cout<<*it2<<" ";
         cout<<endl;
    }

}

這是我對i = 2,d = 4的結果:

2 0 0 0 
1 1 0 0 
1 0 1 0 
1 0 0 1 
0 2 0 0 
0 1 1 0 
0 1 0 1 
0 0 2 0 
0 0 1 1 
0 0 0 2 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM