Friday 3 December 2010

Electronics for Dogs 6 - Our final idiot

So let's have a recap of our idiot. What can he do?
Well he has a whiteboard with boxes A to H on them.
He has a set of pigeon holes each with 8 switches and lights on them. Normally a switch will turn on the corresponding light, (but not always). Trust me when I say that it turns out to be very cheap to make these pigeon holes. Having billions of "holes" with 8 switches and lights in each hole is not a problem. Because of this we'll give each hole a number, or better yet an address.
He also has a set of instructions that we have given him. However these instructions have to be very simple. Let's have another look at these instructions. How simple can we make them? Let's take it to the limit:

Add the number in box A to the number in box B place the result in box C. If anything odd happens record the oddities with numbers in box D
Copy the value from box x to box y replacing the value in there with the new one (substitute any box designation in here)
Take a value from the pigeon hole number listed in Box D and put that value in Box A
Take the value from Box A and put it in the pigeon hole number listed in Box D
Set box A to value specified.
Test box A if the bit number as specified in Box B is a 1 then do not do the next instruction.
Stop following this instruction stream and start using a different one
And, XOR and NOT instructions. (more on these later)

And there you have it, less than a dozen instructions and capable of solving any problem you can throw at a computer.
Don't believe me? Everything I've discussed so far can be solved with those instructions. now a nice processor will make life easier for you by having more complex instructions, but let's get the basics done first.

One thing to say is with just a dozen instructions then why not have the instructions as a set of numbers? Rather than say "add this to this" then "move this to this" just say 12, 4.
When the instructions are just numbers then then the instructions can be placed into the pidgeon holes too. When our idiot turns up for work in the morning he doesn't have to think. He just looks at address 0 in the pigeon holes and sees the number 5 which tells him that he needs to start by writing 5 into box A then he looks at address 1 and sees he needs to...

Hang on though, how do you work out what numbers to put in there? That's the programming we've discussed but let me give you a final recap before we start getting into the electronics:

User Interface designer says:
We need a box that says Publish Post in it

Programmer Thinks:
First I need to draw a box, then set it's colour, then put the text publish post in it

Programmer writes - in a language I just made up, but not a million miles off:
draw_box(top_left_coordinate, top_right_coordinate, bottom_left_coordinate, bottom_right_coordinate)
Set_colour(orange)
Insert_text("Publish Post")

The Compiler (A piece of software, another program) looks at draw_box and finds the code associated with that. That code includes such things as:
create_triangle(top_left_coordinate, top_right_coordinate, bottom_left_coordinate)
create_triangle(top_right_coordinate, bottom_left_coordinate, bottom_right_coordinate)
so it then looks up create_triangle which includes code such as
vertex_x = coordinate_x + (offset*depth)

Which it translates into:
set A = offset
set B = depth
jump to multiply function
set A = coordinate_x
set B = C
add A to B and place in C
set address vertex_x to C

Which then gets passed to the assembler which can then rattle off a stream of numbers corresponding to the above instructions. e.g. set A to offset is saying set A to the value at memory address (wherever we chose to put offset in memory). so it's just a lookup that it has to do.

Now at this point I'm going to leave computers because I'm not sure more detail is necessary. If you want more detail on any of the above shout and I can always write more. However I'm not currently interested in detailing how for example the 3D rendering engine works no matter how interesting you may find that or otherwise, I just want the concepts to be clear as to what sort of processes you go through to get from a publish post button on a web form to a sequence of numbers in memory.

So next we move onto some electronics. To the point where you might be able to build one of these processors. Might...

Electronics for Dogs 5 - some loose ends

So in the first post I told you there were some things to not worry about. Some things to trust me on. Hopefully now I'll complete the picture. This should be a very short post.

Let's assume that our idiot is running a washing machine. We're going to make things a bit easier for him but in this case he, as always, has no idea what he's doing. He's just following instructions and doing as he's told.
I'll make it easy and have a number of lights on the wall labelled with numbers. In actual fact these will be connected up to sensors on the washing machine. One light will be the door open latch, another will be the half full indicator, another will be the full indicator, another will be the overflow. One will be the on switch.
There'll be another indicator that as far as he knows gives a number from 0 to 7. In fact that will be the program number. There'll also be a number of switches for him to throw. One will be connected to a pump to pump water in, one to pump water out, one to turn on the motor that runs the drum etc. But he won't know, it's just a bunch of switches to him.

So he sits there and his first instruction says wait for light 1 to switch on. When it does look at the number on indicator 1 and use that to determine which set of instructions to use next.
Having picked instruction sheet number 3 he sees that his first job is to see if light number 4 is turned on (is the door closed) if it is then turn on switch number 6.
And on and on...

So far so good, but what does this have to do with a processor? Did you notice how all of that up there was to do with numbers? If light number 3 is on could very easily be is value x 1 or 0. Throw switch number 6 could be set value y to 1.

Time for a new concept. Let's make this idiot's job even more basic and simple. Let's get rid of the lights and switches Let's just give him a wall of numbers. Think of it as a wall of pigeon holes if you like, so that instead of a light coming on the number in box x changes from being 0 to being 1. Instead of throwing a switch he puts a value into that pigeon hole.
This makes things simpler for him because now everything is a number, if he's solving the budget or operating a washing machine or running the phone exchange all he has is a wall of numbers and a set of instructions. In the real world getting things like the status of a door latch into a number in a box on the wall would be very difficult. For the computer however it's easy!

I said previously that everything in a computer is a switch. Numbers are stored by the state of a number of switches. The difference between a switch that stores the last value you told it, and one that tells you the state of the washing machine door really isn't that much. Going back to the pigeon hole analogy instead of numbers on a slip of paper let's say we had 8 switches. Each switch had a light associated with it. You'll remember from the last post that 8 switches means that you can story a value between 0 and 255. So that's what our idiot is doing, you tell him to store the number 4 and actually he's not even aware he's doing that (I did tell you he was very stupid didn't I) he's setting the 8 switches to 00000100. That is just flick on the switch of bit 2 (we start numbering at 0) and make sure all the others are off.
Sometimes these switches will be just connected to the light associated with it. Sometimes they'll go and do other things. Sometimes the light associated with the switch won't be connected to the switch at all and will be connected to (for example) a door latch. It doesn't matter he doesn't care that's a separate problem.

Believe it or not that is what your keyboard and mouse are doing. When you push a button it closes a switch. This switch closure is detected and presented as a number to the computer. But if it's a USB keyboard or mouse then the only way your computer knows about this is that somewhere in its memory a value has changed and it has to occasionally go and look at it to see if it has.
Having said that I'll immediately retract it because USB doesn't work quite like that. In most implementations it's more a case of the processor will regularly check memory and be presented with a list of devices that have had updates, then it will have to go and look at those updates to see what they are; however the principle is still valid that the only way the processor knows that something has happened is because it goes and looks at some memory and sees what value it is then goes and does something else based upon that value. Once again it's all just numbers at the end of the day.
Oh and if anyone is old enough here to chirp in and start talking about I/O address space and the like then yes, well done, have a cookie, but often it's not done that way anymore for good reasons, stop trying to confuse the issue, that's my job!

So next time we'll take our last detailed look at our idiot making him the most stupid ignorant individual you'll probably meet. But that's okay because the dumbed down version is easy to copy.

Electronics for Dogs 4 - On the nature of numbers

We all know what numbers are. If I want to write 10 I write 10 on a piece of paper. I can keep writing forever as big a number as I fancy - or at least as big as will fit on the paper.
Computers aren't like that. Not only do they have limited amount of memory - the size of the notepad as it were, but the size of the number they can represent is limited. Before I say why this is the case I want to say why it normally isn't a problem.

Let's say for the sake of argument that the largest number you could represent was 256. Actually no, let's say 100 because we're still thinking in decimal at the moment. What would you do if you needed to represent the fact that you owned 101 sheep? Simple you'd say you had 100 sheep plus 1 sheep.
Hang on a minute, that's exactly what our decimal system does. We only have 10 numbers 0,1,2,3,4,5,6,7,8 & 9. If you want to represent 10 then you say you have 1 lot of 10 plus 0 lots of 1. 23 is 2 lots of ten plus 3 lots of 1. 101 is 1 lot of 100 plus 1 lot of 1. We call this the base system, that adding numbers to the left represents increments of the base. Most of us deal in base 10. Computers deal in base 2. Why this is and what it means we'll get to later, but don't forget that we tell the time in base 12. Imperial measures are in whatever base they fancy at that particular time. You're used to different bases whether you realise it or not.
But let's say that even with this base system, this system of adding numbers to the left to represent ever larger numbers you still only have limited space. Let's say you're a bureaucrat making a form and you want to make it easy for yourself. So you make a form where the person has to put each letter of their name in a different box. You're not doing this to be annoying as such, you're just trying to stop people from using cursive handwriting to make sure you can get their names right. Of course as soon as someone tries to fill out your form who has 3 middle names then your system breaks, but hey that's not your fault is it?
Anyway this is exactly the sort of problem we deal with all the time in computing and electronics. because you physically have to build something you have to chose how big to make it in the first place, you can't just add a few extra bits on at the end because there is no space for them because you haven't built that bit of the computer.

So what do you do? First you make things big, normally you make them way too big. Second you don't just have fixed space allowance for things. Let me once again explain by analogy,
Imagine an authoritarian government states that every family shall have a home. Every family shall have at most three children. All houses are identical. Every family shall have an address of city number and house number and occupant number. Their name shall be formed by sticking all three together.
Sounds great/terrible but what happens when the supreme leader wants 4 children. He's not allowed to break the rules as such, but he really wants that extra child. No problem he thinks I'll create a new house number that the extra children will stay in. Child number 3 will be reassigned to the new house along with child 4 and the child 3 designation at the original house will be a pointer, a reference to this new house. Using this system of continually pointing to further houses he can have as many children as he wants without breaking the system.
Yes it's a mess, yes it's often confusing, yes there's a long trail of investigation to find what you need to know but this way he could have as many children as he wanted yet staying within the scheme.

This is the sort of problem we have all the time in electronics. Many processors in everyday devices, the one that probably runs your washing machine or burgler alarm, your calculator even are 8 bit devices. This means that they can't represent any number larger than 256. Ask it to add 3 to 255 and it will tell you the answer is 2.
Wait a minute you say, I just told my calculator to add 255 to 3 and it said 258 clear as day! Well yes, that's because it cheats, or perhaps the person who was designing it knew about this problem and worked around it. So in the case of the washing machine, it has 8 programs, has a delay timer of up to 120 minutes, has 3 sensors reporting nothing more complex than door open or closed so it doesn't need anything more sophisticated.
In the case of the calculator it's be a good question to ask, well why not make it bigger? Why not make it understand numbers larger than 256 rather than always working around the problem.
Well this comes back to my very fast idiot again. To get an idiot that can deal with bigger numbers is a little bit harder. And even if I got an idiot that could deal with the concept of 4 billion, what if I needed to deal with 5 billion? No first of all I need to come up with a set of instructions that can cope with anything I throw at the idiot/calculator then go from there.
So back to the problem, the calculator is trying to add 255 to 3 and telling me the answer is 2. How do I work around that? Well fortunately when it does the add it remembers that it ran out of space so while it tells you the answer is 2 you can then go and say "Did you run out of space" in this case it would answer yes. most of the time it would answer no. You as the programmer writing the instructions for this idiot can then put something in to keep track of this overflow and deal with it appropriatly. The advantage is if you write these instructions correctly the user of the calculator will never notice this problem.
Except that that's not entirely true either. On the average calculator try the calculation (9999999999 *100000) +1 - (9999999999 *100000) and you'll get an answer of 0 rather than 1, but hey nothing's perfect.

So anyway back to the whole point of this. I've been trying to hold this off in these articles but I can wait no longer. Binary and Hexadecimal. Bases and all their glory.
The building blocks of everything we've talked about is a switch, for the purposes of discussion almost all electronics is digital these days. The analogue stuff is I would argue more interesting, but the digital stuff is really what gets most of the work done. Why that's the case is not important right now, but it is simpler to understand so let's run with that. The switch can be on or off. Unless you have dimmer switches this is no surprise. How can you express numbers then. If you have only light bulbs then how do you represent 7? Exactly the same way we represent 15, or 23. 0 is 0 and 1 is 1. So it hopefully follows that 10 is 2 and 11 is 3. 100 then must be 4 which means 7 is 111.
Here comes the old joke that there are 10 types of people in the world, those who understand binary and those who don't. Now you can count yourself amongst the type that does.

This was where the 256 came from earlier. If you have 8 switches then you can have 256 values. This is why I say that while most people can count to 10 on their hands an engineer can count to over 1000, 1023 to be precise. Why? Because I still want a value for zero and with 1024 states that means I can count to 1023. How do I calculate 1024 for 10 fingers? Easy. With 1 finger I have 0 or 1. With two fingers I have 0, 1, 2 & 3. So I have 2*2 states. With 3 fingers 2*2*2. etc. Also known as 2^3, that is 2 raised to the power of 3. so 10 fingers gives you 2^10, 2 times 2 ten times, 2 to the power of 10 or however else you want to phrase it.

And that I promise you is the most complex maths you'll need for this series. Well unless I get as far as digital coding theory, but that's just long division. Oh and analogue electronics is full of calculus, but I can't remember much of that anymore so you should be fine. Control theory is much worse but I'll be glossing over that because I never really understood it anyway; it's a great case of Someone Else's Problem; and unless you're interested in switched mode power supplies (the thing that powers your computer) then you're fine.

Right so by convention we call each of those individual switches a bit. If we have 8 of them that is a byte. Normally if we need more of them we tend to use multiple bytes to give us 16, 32 or 64 bits. While 24 bit processors do exist they are not common for most domestic purposes. I have used 10 and 14 bit processors in my time, but they were really specialist so let's not worry about those for the moment.

All this is well and good provided you just want to deal with whole numbers or integers as they are often known. What if i want to represent 1.2 or 34.382476235076?
As always there are many ways you can solve this problem, but the one commonly used is known as floating point. Again if you're interested in why other systems aren't used then let me know, but otherwise trust me floating point solves most problems well enough. This is not how it actually works in most implementations - look here if you want to know some specifics, as usual this is about the concepts.
Because you want a fixed storage space for a number that could take a very large range you break the problem up.
Take the number 32.76256
in floating point form we store 3.276256 and 1. That is 3.276256 * 10. We could also have 327.6256 as 3.276256 and 2.
What's the advantage here? Simply put it's that often we only care about the number of significant digits. That is, to use an earlier example if we have (9999999999 *100000) then we often don't care about that last 1 that is added to it. Or to put it another way we'd often rather sacrifice the precision of an answer in order to be able to deal with the full range of the problem. That is it's better to lose that 1 in that calculation than to not be able to perform the calculation at all. If you have gone through life not knowing about this problem then that just shows how true it is. If you have experienced it before then you know how to deal with it I'm sure ;-)

Now one last word on these numbers before we go. 32 bits gives you just over 4 billion separate values. That's an awful lot you say. Indeed for most things we need to do it's an excessively large number. However with more than that many people on earth it's clearly not enough to assign a separate number to each device attached to the global network. This is exactly the problem the internet will face soon, but that's a whole other issue...

Next time there's a few loose ends to tie up. I mentioned how things get into and out of a processor and I need to deal with that first before we can then go on and look at some of how we would build a processor.
Feedback needed as always by the way, what is obvious and what is still a blur please do comment...

Electronics for Dogs 3 - everything is a number

So I mentioned last time how everything is a number. Many people might get this straight away, for others just how the web page they are currently reading isn't actually letters as you normally think of them but in fact an awful lot of numbers is a bit of a strange concept.

So let me start with it's all about context. The numbers 62, 76, 111 are equally good as letters on a page, values of sound level on an answer phone message or polygon locations on Buzz lightyears helmet; like everything in life it's what you do with them.
Again let's take a simple example. Text that I'm typing now. If you only have numbers then what do you do? Well you could start by saying 0=a, 1=b, 2=c. As it happens the most common standard is known as ASCII and a is represented by the number 97, b by 98 and so on. Lower values being used for UPPER CASE characters, punctuation, and numbers themselves. Wait, what? I hear you say, numbers for numbers?
Yes in an ASCII text file the number 12 is represented by the number 49 then the number 50. The space character is the number 32 and so on. If you want the full list have a look here.

So what about things like Microsoft Word. That allows you to do lots of things with text such as BOLD and italics, different colours and so on. Wait a minute I just did that on the web too. How does that work? Are there further numbers used for bold, maybe a bold letter a is stored as the number 165342...
The short answer is it is up to the writer of the program what they do but the web chooses a very interesting solution. (Disclaimer in the modern days of CSS and Java and the like this is far from the full story, but it'll do for now.)
Imagine a program that read in the text file as before, but if I typed some text then the brackets would be taken as an instruction to display some text. So a file containing only text (that was composed of numbers) would be displayed as something much richer.
Again don't worry too much here is a good starting place if the details interest you.

What else? Well the MP3s you listen to on your music device of choice? I'm sure you're aware that music and sound is just the movements of air. A speaker moves itself back and forth to move air back and forth. Why not represent the movement of the speaker, it's position from millisecond to millisecond with a lot of numbers? You may have heard of 20 kHz 40khz or 44 or even 48kHz mp3s. This is exactly it. In a 48kHz mp3 there are 48000 numbers used per second to represent the position of each of the speakers. Sure that's a lot of numbers but I did mention that these computers were quite fast...

What about one of the wonders of the modern age where reality and computers mix? CGI, watching Beowulf it can be hard to believe it's not real never mind that it's all a lot of numbers.
Let's start with something much simpler. Remember Terminator 2? Actually no let's go even simpler. Luxo Jr. The first feature pixar did and still a classic. How on earth do you describe that as a lot of numbers?
I assume you're happy with the concept of Cartesian co-ordinates. You know that thing from maths where a line starts at 3 across 4 up and continues to 7 across 3 up.
Well that's what you're doing with 3d graphics, and instead of line you have triangles. Corner 1 of the tiangle is at 3 across 4 up 2 deep. Corner 2 is at 7 across 3 up 24 deep. Corner 3 is at etc.
Fine you say, that describes a single triangle. That movie had spheres and cones. It had light sources and the ball had patterns on it. The short answer is again that if you use enough triangles then like you can make a football out of hexagons then you can make a sphere out of triangles. If you use enough then you can make any shape. As for the patterns on the ball, well then you just give each triangle a number to represent its colour. Or you have a picture that the rendering engine (the thing that takes this list of triangles and produces a picture) paints around the shape.
Yes it's an awful lot of numbers, yes it's a lot of work to build up a character's face from a collection of spheres cones boxes etc, but now you might have an inkling as to why CGI is so very expensive.

So now you know. Your keyboard is just sending a sequence of numbers to your computer. Your mouse is just sending numbers saying how fast and in what direction it is moving. The webpage is just a whole bunch of numbers saying the letter to use and how it should be displayed. Your webcam is just sending a stream of numbers saying what colour each pixel it sees should be at. That youTube video you watched, again more numbers saying how the pixels on your screen should change. It's all numbers and that's all the computer does; and it's all you need.
Next though we need to look at the numbers themselves because it's never as simple as you'd first hope it to be...

Electronics for Dogs 2 - The very fast idiot

So moving on after those initial thoughts lets jump into what that processor at the heart of every computer is. I mentioned it before, the very fast idiot.
So imagine you have an employee working for you. He's as thick as two short planks but he's eager. Let's assume that he always follows instructions given to him, but don't ask him to make any judgement calls himself because he'll normally get it wrong. So what do you do, this guy is so stupid he can't even use a calculator to add up numbers and struggles to answer the phone correctly.
Never mind you are a thoughtful employer so you decide to write out long lists of instructions for him. It goes something like:

if the phone rings then lift the receiver. Then place the bit with the cord coming out of it next to your mouth and the other end of the receiver next to your right ear.

Ask the person at the other end who they are and who they want to speak to. Write this down on a piece of paper you find here using a pen here. Then replace the paper there and the pen there so you'll be able to find them again. Next consult this diagram for where you might find that that person. Follow the instructions on that diagram then give it to that person.
If that person is nor available consult this diagram provided here to work out who else to ask about it. Once you have done all that wait for the phone to ring again and start from the beginning.

This may sound over the top, but your processor at the heart of your computer is even more stupid than this. Hearing something and writing it down is still after decades of work still a problem for computers. Why? Because they are monumentally stupid (in the traditional numan sense) but they are very fast and very very reliable. It's often said that the only time a computer genuinely makes a mistake is because someone has told it to make that mistake.
So let me strip this down further the average processor can do the following things:

Add one number to another
Copy a value from one place to another.
Test if one value is equal, greater than or smaller to another number. Then "jump" to a different place in the instructions depending on the result.
And, Xor, Or two numbers together (for those of you thinking in decimal this doesn't make much sense. For the following discussion lets assume that it can do a number select that is if I asked for the 3rd digit in 5467 I would report the number 4)
Bit shifts (basically multiply or divide by the base. So in decimal terms the ability to multiply or divide by 10. i.e. take the number and add a zero to it or remove the first digit. Wow 56 * 10 is 560 - clever computer...)

And that's about it. Everything your computer does can be accomplished with those few basic abilities.
Let's go back to our very reliable idiot. You give him a whiteboard with 8 boxes on it labelled A to H. You've given up trying to teach him how to multiply numbers so instead you're going to write him some instructions. You're going to teach him long multiplication, but not tell him you're doing that.
The instructions might go something like this:

When writing a number in a box always replace the value there with the new one.
Write 0 into box H
Write the first number in box A
Write the second number in box B
Write the number 1 in box C
Write the number 1 in box D
* Using the number in box C look at the value in box A and select the number correlating to that
Put this value in box E
Using the number in box D look at the value in box B and select the number correlating to that
Put this value in box F
In the Table go across E rows and down F rows Select that number and place it in box G
add one less than the number of zeroes indicated in box C (this is the bit shift we talked about before)
add this to the number in box H and place the result in box H
Is the number in box C the same as the number of digits (Yes a computer can't do this, but it's a small cheat.)
If it is then you're finished
If it isn't then
add 1 to the number in box C
add 1 to the number in box D
Go back to *

Ok there's a small cheat there but you get the idea of how to build something like multiplication up from smaller steps. If we go back to the first lesson you'll see the opposite of this where we tried to break down something into simple steps. At some point we meet in the middle and where we have to have things like displays and keyboards and mice. But that is a subject for the next post.
What I'm trying to get at here is that if your tame idiot was fast enough he'd be able to multiply perfectly fine just using very simple operations.

Let me just sneak in an extra concept here while you're not looking. Now that I have instructions on how to multiply two numbers I never have to tell the idiot how to do it again. If for example later on I was writing instructions on how to do a balance sheet then I'd just say multiply these two numbers using these instruction. To use computing parlance that would be a subroutine or a function call. But that's a bit unwieldy I'd rather say something like multiply number A by number B. If I was getting really generic and lazy I might prefer to write multiply(A, B). A suitably instructed idiot would know to look for a set of instructions labeled multiply and then apply those instructions to A and B. This way of expression has the advantage that it works for square root, sqrt(A); divide (A, B), or anything else I might want to do with two numbers, print (A); perhaps I'm writing a computer game and I need to draw a circle of diameter A of colour B at location on the screen X and Y, draw_circle (A, B, X, Y)

But that really is a topic for the next post. I keep saying all a computer knows is numbers but how can everything a computer does be just numbers? Another one of those things that's really simple once you get it, but if you don't then it all seems so impenetrable. So lets get that out of the way next time.

Monday 15 November 2010

My problems with Quantum Mechanics

I've often struggled with Quantum Mechanics, like many I never really accepted it into my world view because the conclusions of to contradicted with my everyday experience. More than that though I used to reject relativity because again it contradicted my world view. It's important to say that I'm not saying that they were wrong, just that they were too weird to be right.
To be fair it's normally a good measure of the validity of a theory, at least in the rest of life that it if it feels right then it is probably good. Just like a fast race car normally looks nice, and a good engine sounds good. (or is that just because we have trained ourselves to look for the right structures? That's another discussion though).

Anyway I'll stay on relativity for the moment, I was taught it by any number of thought experiments. The man running on top of a train, the spaceship to the next start system with a clock on each. In each one of these they reached a set of conclusions about the way that things had to work and from this concluded that the speed of light must be constant and that all frames of reference are equally valid. This left me cold and not believing it. This is let's not argue a question of belief because i could never experience them and because it didn't match my existing world view I discarded the new belief.
Now where I think this was wrong was that I missed the first bit. What I should have done is started from the "let's pretend that we had a world where the speed of light is constant for all viewers and all reference frames are equally vaild. Now how would this world have to behave if you were to push the limits of the rules..." From that you then discuss all these relativity thought experiments. Now if you'd happened to say that 'actually if we do this real world experiment then this ties in with this thought experiment'. etc etc.. Unfortunatly that never happened and it took a long time for me to tie up the loose ends by myself.
At the same time I was going through this with Quantum Mechanics. I was smugly superior that there must have been something deeper. What I hadn't realised was that I had come up with the hidden variable theory. Had wikipedia existed at this point or had I actually been studying physics I might have come across this, but I didn't. Of course the outcome of this is that there are experiments possible to determine if QMs probabilities are real or if there are some hidden variables. They of course did them and QM stands. This blew my mind.

Now let's fast forward slightly I'm trying to defend QM specifically and science in general on a random discussion board and this whole thing comes back around again so here is a somewhat re-written version of what I posted.

There's a difference between science and religion, or at least religion as it is often practised. I admit that science can be seen to be a religion to many looking at it, but all that can happen is that people try and explain their point of view and hopefully the truth will become evident. I guess that is at the end of the day the view of all rational parties on both sides: that if they could just find a way to explain themselves properly then the other side will come around. So let me try for the side of science.
Science doesn't care what you think, it doesn't care about your past it just cares if you are correct, or at least interesting. This is not to say that there are not those in authority. If Richard Feynman were to say "You know I've been thinking about it and maybe the maths behind electron behaviour is a bit wrong" then people would probably listen. If some random user on a web forum says the same thing then there would be much more scepticism. This is the right way for things to be though; there are many more insufficiently educated people who don't understand enough of the relevant field than there are experts or even well informed outsiders with a valuable perspective to offer. The outsider is not to know that most things have already been considered by thousands of others previously and the chances are that they have made a error someone has made before. They might not have even made an error, they just may not be aware of certain experimental evidence. At first glance (to me) the hidden variable theory makes much more sense than QM, but reality nor the scientific community doesn't care what you think or what makes sense if it doesn't match experiments and only once you have proved you are aware of the vast range of experimental data can you start to question those who are.

Taking a step back, many before have commented on how Newton was wrong, Einstein could/must be wrong.
Well no, let me use a bad analogy that I find reassuring: Suppose you grew up in the Yorkshire Dales. Let's say you had never left Skipton. As far as you're concerned, water falls from the sky, when there's a lot of it you get a river. When it gets cold it freezes, then you leave it in a cup it evaporates. From this you could determine many properties of water. You could maybe even work out the basics of the water cycle and predict huge amounts of its behaviour. But could you predict deserts? Or would you reject it as just not possible because almost every other day if not every single day water falls from the sky. What about oceans? Salty water would be ludicrous to you. While you might be able to get what they were thinking when someone told you about it, water that you couldn't drink might be just impossible for you to accept.
I think the same applies for QM and relativity, that I can't accept that the world could be governed by probability, that all of this highly predictable world I am used to could be the result of lots of randomness. I can do that maths and use it, but I don't really accept it because it is outside my experience. Who was it who said that you don't understand $advanced_science you just get used to it.

The post would end here but I came across something the other say that blew my mind again. As I understand it, edge effects on a 2d plane could cause the behaviour of the universe as we observe it. I really like this, i could accept that as part of my world view, not because it is complex, but because it is complexity from something very simple; and that really appeals to my aesthetic.
Then again we could all be living in a simulation and the more I learn about string theory the more that seems probable. Although AFAIAC the simulation argument is the new Godwin's law of physics discussions so on that note...

Friday 12 November 2010

Electronics for Dogs

Okay so I've not posted in over a year. I've had nothing to say, so I've not said it.
Anyway I remembered this morning a project I started years ago and never quite finished. So I thought I'd have another go at it on here.

The challenge came from the idea that I think engineering and computers are actually really simple, people just think they're complex because what you have is a great number of very simple concepts working together and you have to understand most of then to understand any of them.
For me it came as a revelation about 3/4 of the way into my electronics BTEC that ever since then the concepts have been easy even if the maths and execution is difficult. Therefore I wanted to explain the easy bit to help people understand this fundamental part of the modern world even if you do leave the complexity up to those who have studied.
The project aim was expressed at the time as "To explain electronics in such a way that my girlfriend could understand it". Which goes to show how much I have learned over the past few years because I should have known how bad an idea it was to express the concept in such an insulting form. To then go on and tell her that I was naming said project "Electronics for Dogs" in a tribute to Wallace and Gromit goes on to show just how badly I could get things wrong and explains why I spent almost all of University single.

So here we go, I present the first chapter, if this proves successful to me then I'll post more later.

Computers are complex and a pillar of modern society. They're clearly designed by really clever people. Rubbish. I design them and that should be enough justification for you that they're not that hard.
To start geeky as I mean to go on Tom Barker in his first Doctor Who story expressed it best "A computer is a very fast idiot. They do exactly what you tell them to at amazing speed. Which becomes a bit of a problem if you've told them to do something you don't want them to."
I don't think the average person would believe just how simple a computer actually is at its heart but it'll be a while before we can get to that, let's start off with a simple problem. A key code door lock. It's a simple device really, everyone can understand how to use one, but how hard is it to make one? Very simple actually, provided you break the problem down. This is going to be the key to everything we do if you'll pardon the pun. It's crucial to say that at each stage you may ask, "but how do you do that?" to which I could explain and hopefully will later on in further chapters but the most vitally important part to getting your head around all this stuff is the ability to say to yourself "I won't worry about that now, that's someone else's problem" Up until now, how a computer works or how to build an electronic door lock might have been someone else's problem, well let's try and peel the onion a bit, but don't worry yet about what lies at the heart of the onion we'll get there, but it may take a while. All I will say is that if you can follow this first article you too can understand the depths of your computer because this one covers what I believe is the hardest concept.
However just so you know here's the plan I assume you know nothing about the internals of computers or electronics or anything above basic physics, so you'll have to bear with me otherwise it will go:
Student: How does a computer work?
Me: Erm I need to explain programming languages [explanation follows]
Student: well how does that run on a computer
Me: Well I have to explain what a processor is [explanation follows]
Student: Well what is a logic gate?
Me: Well I have to explain what a transistor is [explanation follows]
Student: But I don't understand quantum mechanics
Me: Neither do I, just trust me on this. (In fact there is the quote that "If you are not completely confused by quantum mechanics, you do not understand it")

So for our first discussion we talk about programming languages and only programming languages. Trust me that next time we'll talk about how you could implement a programming language in a way that it would run on a machine. It doesn't matter yet that you understand how that machine would look, just that you follow the basic concepts in the programming language bit.

So let's take that door lock, lets break the problem down into a few steps.

1) If the user presses a button, is it the clear button? If it is then clear everything otherwise:
2) Does the button match the first digit in the code? If it is then wait for the second button.
3) Does the button match the second digit in the code? If it is then wait for the third button
etc until
4) have all buttons been pressed in the correct order? If yes then unlock the door.

That does help us break the problem down and so is helpful, but it's a bit confused, but often your first attempts are a bit confused.
Let's introduce some pseudo-code, that is a way of writing some of these things in a way that while still ambiguous is more precise than the English language. Over time we'll develop this pseudo code into a more precise language.

What I'll say is "if {something or other} then {something we want to do} otherwise {something we'll do instead}.

So let's try that door lock again shall we, hopefully this time a little bit more precise.
if {someone has pressed a button} then {does it match the first digit of the code? } otherwise {it's the incorrect code so do nothing until someone presses clear}

Hang on though before we go any further and finish that off notice that our first thing to do is another question. so let's take that and break it down again, we could re-phrase that as:
if {button pressed is equal to first code digit} then {first digit is pressed so we need to test for next code digit}

which means the thing to do could again be written as
if {button pressed is equal to second code digit} then {second digit is pressed so we need to test for next code digit} otherwise {it's the incorrect code so do nothing until someone presses clear}

This is all getting quite confusing now, having these statements inside statements means things get awefully complex very quickly. What we need is a way to remember what button press we're up to. So what about having seen what I've written above can we produce something more clean? Well have this thing, this variable, I'll call it anything I want, let's say last_correct_code_pressed that I can set to a value and when i reference it in other parts of the program the pseudocode will know that I'm referencing this thing I set earlier. It doesn't matter what it actually is, or how the code does it, as long as the reader (and therefore later the machine) knows that I'm referencing this thing.
Let's try restating the problem as

set last_correct_code_pressed to 0
if {a button is pressed} then
{
if {button pressed is equal to the next code in the sequence} then {
set last_correct_code_pressed to last_correct_code_pressed +1
if {last_correct_code_pressed is the last code in the sequence} then {open the door }
}
otherwise
{
set last_correct_code_pressed to 0
}
}
Okay it's quite a different way of stating the problem but hopefully you were able to follow what we're trying to say, by breaking the code across multiple lines and using the brackets to keep the code together we can break the problem down. Now the above isn't a full solution to the problem neither will it run on any computer in existence. That is not the point of this exercise. There are lots of books on how to write software code that will take you from the concept to the implementation. The point I'm trying to start with is that you have the concepts.

To push the concept further now you might ask, "well how does it know a button is pressed?" Well, to give you a quick answer, assume there are the numbers 0 to 9 on a keypad. in a typical implementation you would have a bit of code that every few milliseconds (that is thousands of times a second) does the following:

button_to_check = 0
while {button to check is less than or equal to 9} then {
if {the button to be check is telling me that it is pressed} then {tell who needs to be told} otherwise {button_to_check = button_to_check +1}
}

The important part here being this while statement will keep going round and round within itself until button_to_check is equal to 10. if it detects a button pressed it will go and "call" whoever needs to do something about it. Given that this is happening in our example 1000 times a second you can understand it's probably for the best that nothing much happens a lot of the time.

"Ah, but" you say "You still haven't answered how a button can tell me it is pressed"
To which I will say, "good point". Now this is the really weird bit, or the really trivial bit depending on your point of view. Assuming you followed the above with the button_to_check then you already know the answer. In the same way there was a variable called button_to_check that you could store a number in there can be a variable called (for example) button_pressed that tells you what button is pressed. How that piece of magic happens will for the moment remain magic, but trust me it will become clear later.

Now I'm not saying that's all there is to programming, far far far from it, you wouldn't spend 3 years at university learning computer science if that was all there was to it. That is however the start and almost enough. Let's jump a few stages from key locks to something much more complex, let's try a web browser. In our pseudocode parts of a web browser's code might look something like:

if {user presses back button} then {switch address to previous page}
if {user presses refresh button} then {reload this page}
if {user presses home button} then {set address to recorded home page}

Hang on we've yet to actually draw the web browser on screen yet, okay so lets take a step back; after firefox loads, code equivalent to this might run:

Draw a generic window this_wide by this_high at this_location on the screen
put the firefox logo at this_location
Set the window title text to Blogger: Double you - Create Post - Mozilla Firefox
Add a file menu
Add an edit menu
Add a view menu
etc

Hang on though, you ask how do you draw a window?
Simple, you ask the operating system to do it for you. Most people don't care because it is someone else's problem but let's assume you're curious; as always break the problem down:

Draw a grey box this_wide by this_high at this_location on the screen
Draw the minimise, maximise and close icons
Draw the title bar from here to here in this colour.
etc

Fine you ask, how do you draw a grey box on the screen, (answer: you ask the graphics card driver to do it for you), then knowing that you ask how does the graphics card make those pixels light up etc.

Hopefully you can see that this is all about taking what appears to be a very complex problem and breaking it down. As soon as you can break it down into logical steps you can then break each of those steps down into more and more steps until you end up with something so simple even an idiot like a computer could do it. The only thing a computer does is these very very simple things very very very fast.
What those simple things a computer does is a topic for next chapter.