Re: Inclusion-Exclusion Principle in Mathematica
- To: mathgroup at smc.vnet.net
 - Subject: [mg91666] Re: Inclusion-Exclusion Principle in Mathematica
 - From: dh <dh at metrohm.ch>
 - Date: Wed, 3 Sep 2008 06:45:31 -0400 (EDT)
 - References: <g88v23$grc$1@smc.vnet.net>
 
Hi,
you want to calculate telescopic sums: Sum(a[i1,i2,..]) where 0<i1<n, 
i1<i2<=n, ...
Let's start small with two netsed sums:
nsum=5;
Sum[
a[i,j]
,{i,1,nsum},{j,i+1,nsum}]
this gives: 
a[1,2]+a[1,3]+a[1,4]+a[1,5]+a[2,3]+a[2,4]+a[2,5]+a[3,4]+a[3,5]+a[4,5]
Obviously, we need to create the iterators automatically. We will do 
this outside sum and then "insert" the iterators into Sum using 
"Sequence". Therefore, what we need to create looks like:
{ {x1,1,n}, {x2,x1+1,n},.. }
It is easier to use unique identifiers (using Unique[]) than x1,x2... 
The following creates a list "iters" of 3 iterators and a list "vars" of 
variables:
nsum=5;
niters=3;
last1=0;
vars={};
iters=Table[{last2=Unique[],last1+1,last1=last2;AppendTo[vars,last1];nsum},{niters}]
the variables are e.g.: {$7,$8,$9} and the corresponding ieterators: 
{{$7,1,5},{$8,1+$7,5},{$9,1+$8,5}}. Now we insert this into the sum (use 
lowercase sum to prevent evaluation in order to see what we get. If this 
is o.k. change to uppercase and reevaluate):
sum[
a[Sequence@@ vars]
,Evaluate[Sequence@@ bounds]]
this gives e.g.:
sum[a[$7,$8,$9],{$7,1,5},{$8,1+$7,5},{$9,1+$8,5}]
this looks fine and we replace sum by Sum and get:
a[1,2,3]+a[1,2,4]+a[1,2,5]+a[1,3,4]+a[1,3,5]+a[1,4,5]+a[2,3,4]+a[2,3,5]+a[2,4,5]+a[3,4,5]
hope this helps, Daniel
steaxauce wrote:
> Hey guys, first post!
> I believe I've just solved a problem, and I'm trying to use mathematica to test my solution for known results, but I haven't been able to write my expression into mathematica. It looks a lot like the inclusion-exclusion principle, which is equation (3) on the following mathworld page:
> 
> http://mathworld.wolfram.com/Inclusion-ExclusionPrinciple.html
> 
> My expression is similar in that it contains alternating series of multiple series, where the multiple series are of increasing "depth." I hope that's clear. I can enter it in the way it's written on that page, but that wouldn't be practical for me. I can't have it in the form sum1[] - sum2[] + sum3[] - sum4[], etc. It needs to be in the form sum[-1^(j-1)sumj[]], because I'm working with a very large and varying number of sets and I can't type in all of the series manually. To do this, I need to know how to specify the "depth," or number of series in a multiple series, with a number. I've gone through the documentation, contacted mathematica tech support and posted in the student support forum, but I haven't gotten a solution yet. Can anyone who's more familiar with mathematica that me help me out? I just downloaded the program a few days ago!
> 
> Thanks guys!
> 
-- 
Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh at metrohm.com>
Internet:<http://www.metrohm.com>