Programming algorithm expressed in math notation

AI Thread Summary
The discussion focuses on converting a computer algorithm into mathematical notation. The original code snippet calculates an index based on 2D coordinates (x, y) for a 1D array. Initial suggestions included using summation notation, but it was clarified that the operation is not a summation. The correct mathematical expression for the index is given as index_{x,y} = x + y * width, with the constraints 0 ≤ x ≤ Int(Text3.Text) - 1 and 0 ≤ y ≤ Int(Text2.Text) - 1. The conversation also addressed a perceived bug in the code, which was ultimately deemed unnecessary as the code's purpose was correctly identified. The participants emphasized that the calculation is straightforward and does not require summation.
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 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 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 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 elusiveshame

Similar threads

Replies
0
Views
390
Replies
0
Views
856
Replies
0
Views
381
Replies
1
Views
4K
Replies
4
Views
1K
Replies
30
Views
6K
Replies
1
Views
2K
Back
Top