What’s the best way to play a Loop in math?

Asked

Viewed 559 times

9

I needed to represent and document an arithmetic algorithm in a mathematical notation, the problem is I can’t find the best way to represent a compound loop for that.

Suppose we have the simple algorithm And:

public int E () {
    return n / (m + 200);
}

The mathematical representation would be:

inserir a descrição da imagem aqui

And I’m having second thoughts about how to represent a slightly more composed function, like:

public int E () {
     int e = 0;
     for(int i = 0; i < 5; i++) {
        e += i;
        e *= i + 2;
     }
     return e;
}

And representing in a sum:

inserir a descrição da imagem aqui

In the above formula, there is no implementation of how far the loop starts, jumps and must go (whereas in the code, it is defined that the loop will travel until i < 5, being initially i = 0 and by loop, i += 1).

Is this scientific representation right? Or is there another mathematical way to represent a loop of the genre?

3 answers

14


The way it is:

public int E () {
     int e = 0;
     for(int i = 0; i < 5; i++) {
        e += i;
        e *= i + 2;
     }
     return e;
}

The function E will always return the value 714, then the mathematical representation would be:

inserir a descrição da imagem aqui

But if we generalize considering an input n:

public int E (int n) {
     int e = 0;
     for(int i = 0; i < n; i++) {
        e += i;
        e *= i + 2;
     }
     return e;
}

Then we have a disguised recursive function. Recursive because the value of each iteration will depend on the value passed; the only difference is that this value is stored in a local variable and is calculated iteratively from 0 to n, while in the recursive version the call is made from n up to 0, being n = 0 our stop condition, since the loop would not execute and be returned to zero.

public int E (int n) {
    if (n == 0) {
        return 0;
    }

    return (n+2) * (E(n-1) + n)
}

For comparison with the original version, when calculating E(5) we will have the same result, 714. In this way, we can represent our function E(n) as:

inserir a descrição da imagem aqui

  • This does not answer the question. Where the Loop was applied in the expression?

  • 2

    @Cypherpotato In recursion

  • Where can I specify the beginning, step, and end of recursion in the cited example?

  • @Cypherpotato What you mean by step and end?

  • for inicio = 0; inicio < fim; inicio += passo

  • @Cypherpotato Hum, that level of detail should be in the question already

  • But it’s just below the last equation. I’ll edit it to make it clear.

  • 5

    @Cypherpotato, mathematically there is no distinction between loop and recursion. So much so that the functional paradigm, the one that derives directly from mathematics (lambda calculus, being more specific), does not foresee iterations, only recursions.

Show 3 more comments

11

Specifically about summation and productive operations, not in general and can not represent your specific case because the order of the operation is important.

There are operations that are done in collections. They are unary functions based on binary operations (vine binary operation), as long as the binary operation itself defines, with the collection of elements, an abelian group (not to confuse group and ensemble, are distinct mathematical objects). And summation and productive are examples of such operations.

I do not have the guarantee of the necessity that the group needs to be abelian, however, as a group alone has no ordination, it seems to me intuitive that op(a,b) = op(b,a) must be true

The definition of a collection operator is recursive. Be a collection S (vine bag) an element of C* (vine kleene star), and C a set belonging to the group G = {C, op, e}, where op defines a commutative operation in C and e is the neutral element of op. We can create a set operator opX which receives a collection and returns an elmento of C. Your signature is like this (in Latex it is more elegant, but for the next moment, let’s focus on ASCII):

opX: C* -> C

It means that opX receives a variable amount of C (therefore, a collection with repetitions, aka bag) and returns an element of C. Your return is to climb on C.

This is the signature of opX, identifying the types of inputs and their output. The definition of opX, however, so it is:

opX(S) = e, se S = {}
opX(S) = op(a, opX(S\{a})), a em S, S != {}

The sum is defined this way:

Σ(S) = 0, se S = {}
Σ(S) = +(a, Σ(S\{a})), se a em S, S != {}

And the one about production:

Π(S) = 1, se S = {}
Π(S) = *(a, Π(S\{a})), se a em S, S != {}

It happens that often does not interest to describe all the elements in S for these operations. Then a set inference is made. To make this inference, you must pass a unary function on X for mapping (if the domain passed is distinct from the basic operation domain). That is, you need to describe a function of the following signature:

map: X -> C

So you define a collection S' based on S applying the function of mastering map:

S' = {map(s), s em S}

Then, by convention, the following was defined:

Σ(S, map) = Σ(S'), S' = {map(s), s em S}

However, as in most cases the range of integers consists of consecutive numbers, it would be enough to inform the beginning and the end of this range. It is also agreed that it is elegant to show which iteration variable, why it is put i = 0 below the sigma symbol (or pi in the product) and only an arbitrary number above. Example of this notation: here.

In ASCII notation, it would be equivalent to this:

Σ(ini, fim, map) = Σ(S, map) = Σ(S')
S = [ini, fim], S' = {map(s), s em S}

Where [ini, fim] is the closed interval of the integers between ini and fim.

I particularly sometimes write only that the iteration variable is contained in a set. Look at the products.


The algorithm defined in its pseudo-code loop, however, cannot make it an operation in any relevant numerical set (natural/integer/rational/real/complex).

2

That sounds like a recurrence relationship to me.

The mathematical representation could be

E₀ = 0
Eₙ₊₁ = (Eₙ + n) * (n + 2) (para n > 0)

In the Wikipedia.

Obs. I am putting Wikipedia as a reference because it has been a long time since I studied mathematics.

  • 1

    That’s not the @Andersoncarloswoss response in a well-summarized way?

  • think that this more summarized notation is the most used when working me with recurrence relations...the notation he used seems to me to be a more generic notation to denote functions, not only functions defined recursively, but also, for example, discontinuous functions that we see when we learn differential calculus

Browser other questions tagged

You are not signed in. Login or sign up in order to post.