Programming algorithm expressed in math notation

In summary, the conversation discusses how to properly write a computer algorithm in mathematical notation. A code snippet is provided and it is determined that the mathematical formula for it is ## index_{x,y} = x + y * width##. A bug in the code is pointed out and fixed, and the conversation ends with the agreement that there is no cleaner way to express the formula.
  • #1
elusiveshame
169
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
  • #2
##\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
  • #3
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
  • #4
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.
 
  • #5
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
 
  • #6
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
  • #7
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!
 
  • #8
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

1. What is a programming algorithm expressed in math notation?

A programming algorithm expressed in math notation is a set of step-by-step instructions for solving a problem or completing a task, written using mathematical symbols and equations. It is a way to describe a computer program using mathematical concepts and formulas.

2. How is math notation used in programming algorithms?

Math notation is used in programming algorithms to provide a precise and concise way to describe the steps necessary to solve a problem. It allows for clear communication and can help reduce errors in the code.

3. What are the benefits of using math notation in programming algorithms?

Using math notation in programming algorithms can make the code more readable and easier to understand. It also allows for easier translation between different programming languages and can help identify and correct errors more quickly.

4. Are there any drawbacks to using math notation in programming algorithms?

One potential drawback of using math notation in programming algorithms is that it may be more difficult for those without a strong mathematical background to understand. It also may not be suitable for describing more complex algorithms.

5. How can I learn to read and write programming algorithms expressed in math notation?

There are many resources available for learning how to read and write programming algorithms expressed in math notation. These include online tutorials, textbooks, and practice problems. It is also helpful to have a basic understanding of mathematical concepts such as algebra, calculus, and discrete mathematics.

Similar threads

  • Programming and Computer Science
Replies
2
Views
886
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
22
Views
728
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
967
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top