« Archives in May, 2013

A New Toy: Raspberry Pi

I am playing around with a new toy tonight:

What you see here is a $35 computer on a board called the “Raspberry Pi”. The ‘hard drive’ feeding this thing it’s instructions is a microSD card (with an SD adapter) smaller than my fingernail. It has 8GB though, so it can store a special fork of the Debian Linux operating system (called “Raspbian”), plus a whole lot of other utilities (such as Apache, PHP, MySQL to form a LAMMP server), python, etc. There are libraries available to access the pinouts via python and C, so you can use a full computer environment to bit-bang electronics. The computer comes with two USB ports (currently plugged up with my keyboard and mouse, but if I stick a wireless network antenna in there, I should be able to control it remotely over a wireless network via ssh/sshd/sshfs.)

Honestly, that HDMI cable (what a ripoff) was almost as expensive as the computer itself! You have $35 to play with, don’t you? Why not try it?

Here you can see my first attempt at messing with the pins through a small python program. I am blinking an LED.

I can see all sorts of uses for this thing. You could have an always-on battery/usb powered internet-LAMMPserver-controller that, on recieving some socket connection or webpage-command pings microcontrollers and sends them commands. The microcontrollers could then move motors, turn things on and off, I could turn on my coffee maker, automatically drop a hammer on my alarm clock after detecting a noise, arm the trapdoor over the shark-tank, etc.

 

Quicksort implementation

I’ve recently coded a q-sort template function implementation. It’s part of another project, but it’s sort of stand-alone.

Author: Aaron M. Schinder
Date: 4 May 2013
Purpose: ams_qsort is an implementation of quicksort, the popular O(n*log(n)) sorting algorithm. There are millions of q-sort implementations out there, but this one is mine. This was programmed partially for the ability to sort multiple lists, but mostly for the exercise in converting a recursive program to a procedural loop.

It has some desirable features over most implementations of qsort:
1. It doesn’t use recursion of the function calls. Instead, it implements recursive behavior through the use of a tree data-structure. This means it won’t blow your call-stack when sorting large lists (which is the point of having an O(n*log(n)) sorting algorithm in the first place!).
2. What the actual sorting function returns is not a single sorted list, but a permutation map. The permutation map can be applied not just to this list, but to other related lists as well to sort each according to the first. First you acquire the permutation map that will sort the target list, then you apply it to lists.

Main functions of interest:
bool quicksort(std::vector<T> *V, std::vector<long> *map, bool (*comparator)(T, T));
bool rearrange(std::vector<T> *V, std::vector<long> *map);
void invertmap(std::vector<long > *map, std::vector<long > *inv);
void mapcompose(std::vector<long > *map_in_main, std::vector<long > *map_in_sub, long indstart);

Typical use:
quicksort(&myvector,&map,&comparatorfn);
rearrange(&myvector,&map); //rearranges myvector (sorts it)
rearrange(&myothervector, &map); //rearranges some other vector (sorts it according to myvector)

invertmap(&map,&othermap); //creates a map which, when composed with map, de-permutes it.
rearrange(&myothervector,&othermap); //unsorts the myothervector

bool comparator(T A,T B)
comparator is a pointer to a bool function that returns true if A>B, according to whatever evaluation of A and B you want.

ams-qsort.hpp

ams-qsort.cpp

 

Websurfing

I’m surfing the internet – from my python shell 😛

#!/usr/bin/python

import os,sys,math,socket

print “Hello world!”

print “Socket test”

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM);
s.settimeout(10);
s.connect((“www.amssolarempire.com”,80));
#s.send(“HTTP REQUEST”);
msg = “GET / HTTP/1.1\n”;
msg = msg + “Host: amssolarempire.com\n”;
msg = msg + “Connection: close\n”;
msg = msg + “User-Agent: PythonExperiment\n”;
msg = msg + “\n”;
s.send(msg);
t = ” “;
ret = “”;
while(t!=””):
t = s.recv(1);
ret=ret+t;

print ret;

s.shutdown(0);
s.close();