# Programming algorithm expressed in math notation

#### elusiveshame

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?

Related Programming and Computer Science News on Phys.org

#### anorlunda

Mentor
Gold Member
$\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.
index = x + (y * width)
It should be
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.

#### Ibix

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.

#### elusiveshame

$\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 be

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.
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.

...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.

#### elusiveshame

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

#### Ibix

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.

#### elusiveshame

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!

#### FactChecker

Gold Member
2018 Award
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$

"Programming algorithm expressed in math notation"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving