Jump to content

Mathematician's guide to Programming. The great divide.


Recommended Posts

Posted (edited)

When algebraic maths and programming code are written as examples they look very similar and confusing one for the other leads to all sorts of confusion.

 

In programming symbols may be multiple letters long

 

aa = ab + ghp

 

This does not mean

 

[math]a^2 = a*b + g*h*p[/math]

 

but instead could be written in algebra as

 

a = b + g

 

In OO (object oriented) programming most of the program concerns object. An example of an object could be a "dog". If I want my dog to bark for a set length of time at a set frequency I could use the code as follows.

 

dog.bark(4, 2)

 

In this line there are 3 symbols, one of which is an object called "dog", the other two are literals. These are objects that require no setting up as everything that needs to be known about them can be shown in just one symbol (for example "4").

 

I could tell the dog to do a few things.

 

length = 4

freq = 6

dog.sleep()

dog.wake()

dog.bark(length, freq)

 

In most good programming environments the top two lines wouldn't work as in giving the numeric symbols a name they would need to be set up with a greater description of what they are, but for simplicity I am ignoring this for now.

 

A string is another good example of an object.

 

sound = "woof, growl ..."

dog.say(sound)

 

The most fundamental thing in the language could for example be a single character. An object of type string is a little more complicated as it probably has a length and at least a few characters.

 

string = "testing"

dog.say(string.length())

 

In the above example the dog would say 7. A string has a default property that it returns and is set by equals and needs to be told specifically if something different it needed, otherwise it would have had to be written.

 

string.set("testing")

dog.say(string.length())

 

In programming lines are always implemented in order whereas in algebra this is not always the case.

 

n = b * y + z

b = n - z - z * y

 

Above is an example of a simultaneous equation. Given n starts at 10, b at 4 and y and z at 1 then in coding this is seen as meaning.

 

set the value of n to b * y + z

n is now 5

then set the value of b to n - z - z * y

b is now 3

 

In this example the simultaneous equation could resolve algebraically to the same answer but it will certainly not always do so.

 

A simultaneous equation that may give the correct answer could be represented like this.

 

newN = oldB * y + z

newB = oldN - z - z * y

oldB = newB

oldN = newN

 

Take the part

 

a - z - z * y

 

This could be algebraically simplified to

 

[math]a - 2z * y[/math]

 

but in code this would be written

 

a - (z * 2) * y

 

additionally in code

 

* = times

2^3 = 2 to the power of 3

2^(3 + 1) = 2 to the power of 4

2^3 + 1 = 2 to the power of 3 then add 1

 

and specifically in Java

 

|| = or

&& = and

= means "set"

== means "is it equal" and returns a boolean

 

whereas in algebra

 

[math]*[/math] or x = times

[math]2^3[/math] = 2 to the power of 3

[math]2\stackrel{3 + 1}{}[/math] = 2 to the power of 4

[math]2^3 + 1[/math] = 2 to the power of 3 then add 1

[math]\wedge[/math] and [math]\vee[/math] = "and" and "or"

[math]\neg[/math] = not

 

If b starts as 2 then this

 

b = b + 1

 

Is invalid in algebra, however in code the above line causes b to become 3. The real goings on are hidden less if one looks at what would need to be written if nothing was hidden though default behaviors for objects and if there were no exceptions built into the language for simplicity.

 

a.set(maths.add(a.get(), 1))

 

The word "number" seems to be an appropriate name for the type of the object "a". There is nothing fundamental about the words set and get, they are simply names of methods designed to be a description of what is happening and made up by the person who designed "number". "Maths" is also an object and both these objects would have had to be designed further up, perhaps something like this although please note that the exact syntax as in many of my examples is not correct for any one specific language.

 

new Number a

new Calculator maths

a.set(maths.add(a.get(), 1))

 

The full description of number and calculator would be elsewhere in the program.

 

a = 0

while (a < 20)

{

a = a + 1

dog.bark()

}

 

The part in the middle is looped 20 times and so the dog barks 20 times

 

while (a < dog.getEnergy())

{

dog.useEnergy()

dog.bark()

}

 

If the dog barking always results in him using up energy then useEngergy could be called from bark. Also there would need to be some sort of pause in this loop, probably in bark to prevent all the barks seeming to happen at once.

 

Arrays are like sets in mathematics

 

dog[1].sleep()

dog[2].sleep()

dog[1].bark()

dog[2].wake()

 

or more usefully

 

new Player player
new Boolean oops = false
new Number a = 0
newArray Dog[25] dogs

while (a < dogs.getSize())
{
 a++
 dogs[a].sleep()
}
a = 0
while (a < dogs.getSize())
{
 a++
 if (dogs[a].barkedRecently())
 {
   oops = true
 }
}
a = 0
if (oops)
{
 while (a < dogs.getSize())
 {
   a++
   dogs[a].bark()
 }
 player.loose()
}

 

In the above example I have used a++ to mean as in many languages a = a + 1 and also used indenting.

 

If(boolean) happens if the value if true and not if the value is false.

 

The program shows some code to simulate a game where the objective is to not wake any of the dogs up and cause them to bark or they will all bark you and you will loose.

 

Another possibility could be

 

new Integer counter
new Integer number
new Integer number2 = 1
new Integer number3 = 1

for (new counter = 1; counter < 10; counter++)
{
 number = number2 + number3
 number3 = number2
 number2 = number
 screen.write(number)
}

 

The above code uses a for loop, they are sometimes called for next loops as they looked like this in Basic based languages.

 

for counter = 1 to 10
 [inside the loop]
Next

 

They allow you to make loops easily. The program calculates the Fibonacci series and could with a bit of tweaking be made to store it into an array and refer to the array for the last two values.

 

Similar functions are possible with sets and series and logic in mathematics and they are of course restricted with the same sorts of rules as in algebra and a different "set" class in the above example might implement a way to ask a question like "are all the values in this set identical?" in just one line. However when you do need to understand code you will see a lot of the above sort of program. It is perfectly possible of course to make an algebraic set that doesn't contain enough details about how it is done to code it or work it out mathematically how to find all the values in the set but is still algebraically legitimate whereas a program however is little use to anyone unless it is entirely defined as otherwise it won't compile.

Edited by alan2here

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.