Understanding sum/product algorithms

  • Thread starter Thread starter Hobold
  • Start date Start date
  • Tags Tags
    Algorithms
AI Thread Summary
The discussion revolves around the challenges of understanding sum and product algorithms, particularly when dealing with combined summations and products. The user expresses difficulty in translating mathematical notation into algorithmic form, especially with nested loops. They provide examples of their attempts but struggle to achieve the correct initialization and updating of variables for both summation and product operations. Suggestions include manually calculating terms for clarity and referencing specific algorithms from their textbook, which lacks detailed explanations. The conversation emphasizes the importance of understanding the relationship between mathematical notation and algorithmic implementation.
Hobold
Messages
82
Reaction score
1
Understading sum/product algorthms

Hello there,

I'm having a little trouble understanding sum/product algorithms. I can do single sum/product algorithms, but when it comes to multiple/combined summations or products I just can't follow.

Ex.:

\sum_{i=0}^n (a_i x + c_i)

Ok, this one was pretty simple, I have no problem in doing that. Though when it comes to things like

\sum_{i=0}^n \left ( \prod_{j=0}^i (a_i x + c_i) \right )

it gets hard. I have made a few algorithms, though I have no idea if they are right. I assume you can write using only one loop, but I have no idea how. Here's what I'd write for that one:

Code:
z = 0; y = 1;

for i = 0 to n do

   for j = 0 to i do
      z = z + a[i] * x + c[i];
   end for

   y = y*z;

end for

I can't work the other way around as well. When I notice some algorithm results in combined product/sum notation, I can't write it right, such as

Code:
v = a[0];
z = x;

for i = 1 to n do
   v = v +z*a[0];
   z = x*z;
end for

Could anyone help me understand? Or at least suggest any book or text on these?
 
Technology news on Phys.org


The inner loop is a product loop, you should initialize z = 1, and then each step should be z = z * ... or the alternate form z *= ... . The outer loop is a summation, so initialze y = 0, and each step should be y = y + ... or y += ...
 


Code:
sum:=0
for i:=0 to n do
	prod=1
	for j:=0 to i do
		prod:= prod * (a[i]*x + c[i])  // i?
	sum:= sum + prod
 


Thanks, I got that...

What would

Code:
v = a[0];
z = x;

for i = 1 to n do
   v = v +z*a[0];
   z = x*z;
end for

be in sum/product notation?

I am curretly using "Numerical Mathematics and Computing" by W. Cheney and D. Kincaid. Though it doesn't explain anything about those algorithms, there are some exercises in which the authors ask you to transform from notation -> algorithm and vice-versa. In the answer section, there are answers I cannot interpret such as:

Code:
z = b[n] + 1;

for k = 1 to n-2 do
   z = z*b[n-k] + 1;
end for

stands for z = 1 + \sum_{i=2}^n \prod_{j=2}^i b_j

I have really no idea on how the author got to that answer.
 


Hobold said:
I have really no idea on how the author got to that answer.
Try manually writing out the calculated terms for the first few loops, and you'll see why it works. As to how he figured it out, either he's clever or he copied the technique from yet another clever person.

z = b[n] + 1
z = (b[n] + 1) (b[n-1]) + 1 = b[n] b[n-1] + b[n-1] + 1
z = (b[n] b[n-1] + b[n-1] + 1) (b[n-2) + 1 = b[n] b[n-1] b[n-2] + b[n-1] b[n-2] + b[n-2] + 1
z = ...
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top