Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Inclusion-Exclusion Principle in Mathematica

  • To: mathgroup at
  • Subject: [mg91666] Re: Inclusion-Exclusion Principle in Mathematica
  • From: dh <dh at>
  • Date: Wed, 3 Sep 2008 06:45:31 -0400 (EDT)
  • References: <g88v23$grc$>


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:





this gives: 


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 







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):


a[Sequence@@ vars]

,Evaluate[Sequence@@ bounds]]

this gives e.g.:


this looks fine and we replace sum by Sum and get:


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:




> 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>


  • Prev by Date: Programmatic headers and footers
  • Next by Date: Controlling the size of the integral sign
  • Previous by thread: Programmatic headers and footers
  • Next by thread: Controlling the size of the integral sign