Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I calculate an element without looping through all preceding elements in my case (see the question body)?

I have 2 arrays of Doubles of the same length. Array a is filled with some data, array b is to be calculated.

Each element of the array b equals a corresponding value from array a plus a weighted sum of all preceding elements in the array b.

A weighted sum is calculated by adding all those elements each multiplied by a coefficient which equals its distance from the current element we calculate divided by number of elements in the preceding subset.

To implement this I loop through the whole preceding subset for each element I calculate.

Can this be optimized? I have not enough maths skills, but I suspect that I could only use the first preceding element to calculate every next as every element is already derived from the preceding set and contains all the information of it already weighted. Maybe I can just adjust the weight formula and get the same result without a second level looping?

This seems to be an example in Scala (I am not sure if it is correct :-]). As the real project uses negative indices, treat a(1) and a(2) as preceding a(0) in terms of the task written above.


import scala.Double.NaN
val a = Array[Double] (8.5, 3.4, 7.1, 5.12, 0.14, 5)
val b = Array[Double] (NaN, NaN, NaN, NaN, NaN, 5)
var i = b.length - 2
while (i >= 0) {
  b(i) = a(i) + {
    var succession = 0.0
    var j = 1
    while (i + j < b.length) {
      succession += b (i+j) * (1.0-j.toDouble/(b.length - i))
      j += 1
    }
    succession
  }
  i -= 1
}
b.foreach((n : Double) => println(n))

like image 705
Ivan Avatar asked Nov 03 '10 08:11

Ivan


People also ask

Which statement along with for loop and while loop can be used to execute statements after the loop has ended?

The purpose the break statement is to break out of a loop early. For example if the following code asks a use input a integer number x. If x is divisible by 5, the break statement is executed and this causes the exit from the loop.

How do you loop over the elements of a list in Python?

You can loop through the list items by using a while loop. Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by referring to their indexes. Remember to increase the index by 1 after each iteration.

What do you mean by iteration or looping?

The looping can be defined as repeating the same process multiple times until a specific condition satisfies. It is known as iteration also.


1 Answers

I assume the distance is the absolute difference of two elements.

If I understood it correctly each element of b has to be:

b(i) = a(i) + sum(j = 1 to i-1) (a(j) * (abs(a(i) - a(j)) / i )

b(i) = a(i) + sum(j = 1 to i-1) ( abs(a(j)*a(j) - a(j)*a(i)) / i )

Now, if we could write b(i+1) in terms of b(i) we would have done it.

The problem is that each weight depends on both, a(i) and a(j) (and even worse, it is the absolute difference).

That's why we can't simplify the above anymore and can't "extract" knowledge from each sum to use it in the next one.

like image 94
George Avatar answered Oct 22 '22 07:10

George