matlab - How to compute a function on a set of natural numbers using recursion -


i working on property of given set of natural numbers , seems difficult compute. build function 'fun' takes 2 inputs, 1 cardinal value , set. if set empty fun should return 0 because fun depends on product of set , fun on subsets of complement set.

for clarification here example:

s set given s={1,2,3,4}. function fun(2,s) defined

fun(2,s)=prod({1,2})*[fun(1,{3}) + fun(1,{4}) + fun(2,{3,4})] +           prod({1,3})*[fun(1,{2}) + fun(1,{4}) + fun(2,{2,4})] +           prod({1,4})*[fun(1,{3}) + fun(1,{2}) + fun(2,{2,3})] +          prod({2,3})*[fun(1,{4}) + fun(1,{1}) + fun(2,{1,4})] +          prod({2,4})*[fun(1,{1}) + fun(1,{3}) + fun(2,{3,1})] +          prod({3,4})*[fun(1,{1}) + fun(1,{2}) + fun(2,{1,2})] 

prod defined product of elements in set, example

prod({1,2})=2;  prod({3,2})=6  

i trying compute function fun using recursive method in matlab it's not working. base case cardinal value should more 0 means there should @ least 1 element in set other wise prod 0 , fun return zero.

update pseudo code:

fun(i,s) if |s|=1 && i!=0    return prod(s) else if i==0    return 0 else   prod(subset s', s' subset of s , |s'|=i)*(sum on fun((for i=1 m),{s-s'}), m=|s-s'|) //i don't know how write code part , need help. end if end fun   prod(s) n=|s| temp=1 i=1 n     temp *=s(i) //s(1) 1st element of s end return temp end prod 

thanks.

with pseudo code added question it's impossible implement function. put 1 line incomplete (at least outer sum missing).

1) formalize algorithm in way can used implement. following pseudo code not correct because don't know want, should give idea how it.

fun(i,s) if i==0    return 0 else if |s|=1    return s else   r=0   s1 in subsets of s size       f=0       s2 in subsets of setdiff(s,s') size <=i          f=f+fun(s2,|s2|)       end       r=r+prod(s1)*f   end end if end fun  

2) use arrays [1,2,3,4] instead of cells {1,2,3,4}

3) prod built-in function, no need reimplement it.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -