首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成d维I阶偏导数的一个模式

生成d维I阶偏导数的一个模式
EN

Stack Overflow用户
提问于 2016-12-27 16:24:14
回答 2查看 194关注 0票数 0

我试图计算一个有限元程序,在这里我需要计算d维的偏导数。在有限元中,基函数N(x,y,z)=N(x)N(y)N(z),因此一阶导数是:

代码语言:javascript
运行
复制
N(x)'N(y)N(z) N(x)N(y)'N(z) N(x)N(y)N(z)'

二阶导数是

代码语言:javascript
运行
复制
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)的函数,以便在下表中告诉我这些模式:

我认为必须有一个简单的算法来实现这一目标。有人能帮我吗?THx

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-27 22:09:27

这可以通过嵌套循环来解决:

代码语言:javascript
运行
复制
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块解析输出的数组。

票数 0
EN

Stack Overflow用户

发布于 2016-12-28 02:00:15

我通过调用一个函数来实现这一点。

代码语言:javascript
运行
复制
#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的结果:

代码语言:javascript
运行
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41348642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档