Programming algorithm expressed in math notation

Click For Summary

Discussion Overview

The discussion revolves around how to express a computer algorithm in mathematical notation, specifically focusing on converting a code snippet that calculates an index from (x, y) coordinates in a 2D array to a mathematical formula. The conversation includes aspects of programming, mathematical representation, and potential bugs in the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to convert a code snippet into mathematical notation and proposes an initial formula that may not accurately represent the algorithm.
  • Another participant suggests that the original code contains a bug and proposes an alternative formula, emphasizing the need to initialize the index variable.
  • Some participants argue that the code is correctly converting (x, y) coordinates into a 1D index and that the mathematical representation should reflect this without implying summation.
  • A later reply clarifies that the calculation is simply ##x + y \times width## for given values of ##x## and ##y##, rather than a summation.
  • Further contributions refine the mathematical expression to ##index_{x,y} = x + y \times width##, specifying the ranges for ##x## and ##y##.

Areas of Agreement / Disagreement

Participants express differing views on whether the original code contains a bug, with some asserting it does and others defending its correctness. The discussion on the mathematical representation also shows a lack of consensus on the appropriate notation, though a clearer expression is eventually proposed.

Contextual Notes

There are unresolved assumptions regarding the initialization of variables and the interpretation of the algorithm's intent. The discussion also highlights the potential for different mathematical representations based on varying interpretations of the code.

elusiveshame
Messages
170
Reaction score
35
Hey everyone,
I wasn't sure if this belonged in the general math forum or not, so I posted it here instead (mods - feel free to move if it belongs elsewhere).

What I want to know is how to properly write out a computer algorithm in proper math notation. Take this code for example:

Code:
    Height = 4
    Width = 10
    For X = 0 to Width - 1
        For Y = 0 to Height - 1
            index = x + (y * width)
        Next
    Next

To put it into a mathematical formula, how would you do it? The highest level of math completed was Single Variable Calculus, so going by what I know, it would look like:

index = x∑y∑ x + (y * x)

Is that even close?
 
Technology news on Phys.org
##\sum_y\sum_x x + (y * width)##
or
##\sum_{y,x} x + (y * width)##

To do that in LateX here on PF, write the following except use # instead of @.

@@\sum_y\sum_x x + (y * width)@@
or
@@\sum_{y,x} x + (y * width)@@

But first you have to fix a bug in your code. As written is it not a sum at all.
elusiveshame said:
index = x + (y * width)

It should be
elusiveshame said:
index = index + x + (y * width)

You also need to initialize index=0 before the loops.

Note that at the bottom of the post edit window, is a button called LateX. That shows you how to do it.
 
  • Like
Likes   Reactions: berkeman, FactChecker and elusiveshame
anorlunda said:
But first you have to fix a bug in your code. As written is it not a sum at all.
It should be
You also need to initialize index=0 before the loops.
...well, it should be that if you want the code to do what the maths says. Since the code looks to me like it's converting (x,y) coordinates into an index in a 1d array, I'd say the maths that has the bug.
 
  • Like
Likes   Reactions: elusiveshame
anorlunda said:
##\sum_y\sum_x x + (y * width)##
or
##\sum_{y,x} x + (y * width)##

To do that in LateX here on PF, write the following except use # instead of @.
But first you have to fix a bug in your code. As written is it not a sum at all.It should beYou also need to initialize index=0 before the loops.

Note that at the bottom of the post edit window, is a button called LateX. That shows you how to do it.
Thanks for the LaTeX tutorial =) There's no bug in the code, though, and it was just a small snippet of a much larger routine.

Ibix said:
...well, it should be that if you want the code to do what the maths says. Since the code looks to me like it's converting (x,y) coordinates into an index in a 1d array, I'd say the maths that has the bug.

That's exactly what it's doing. Basically it's taking an array of a data type that has its own 2D (x,y) coordinate system. This just identifies the index of that data type.
 
Here's the full loop if you're curious:

Code:
            idx = 0
            ix = 0
            iy = 0
            For X = 0 To Int(Text3.Text) - 1
                For Y = 0 To Int(Text2.Text) - 1
                    tc = X + (Int(Text3.Text) * Y)
                    For Index = 0 To 31
                        t$ = Hex(b(idx + Index))
                        If Len(t$) = 1 Then
                            t$ = "0" + t$
                        End If
                        Extract(tc).pixels(ix, iy) = CInt(CLng("&h" & Left(t$, 1)))
                        ix = ix + 1
                        Extract(tc).pixels(ix, iy) = CInt(CLng("&h" & Right(t$, 1)))
                        ix = ix + 1
                        If ix = 8 Then
                            ix = 0
                            iy = iy + 1
                        End If
                        If iy = 8 Then
                            iy = 0
                            idx = idx + 32
                        End If
                    Next
                Next
            Next
 
elusiveshame said:
That's exactly what it's doing. Basically it's taking an array of a data type that has its own 2D (x,y) coordinate system. This just identifies the index of that data type.
So you aren't summing anything. You are just calculating ##x+y\times width## for a sequence of ##x## and ##y## values. I don't think there's a way to express that any more cleanly.
 
  • Like
Likes   Reactions: FactChecker and elusiveshame
Ibix said:
So you aren't summing anything. You are just calculating ##x+y\times width## for a sequence of ##x## and ##y## values. I don't think there's a way to express that any more cleanly.
Ah, yeah, you're right.

Thanks!
 
Ibix said:
So you aren't summing anything. You are just calculating ##x+y\times width## for a sequence of ##x## and ##y## values. I don't think there's a way to express that any more cleanly.
So that would be ## index_{x,y} = x + y * width##, for ##0 \le x \le Int(Text3.Text) - 1, 0 \le y \le Int(Text2.Text) - 1 ##
 
  • Like
Likes   Reactions: elusiveshame

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
7K
Replies
3
Views
3K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K