Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday, May 12, 2015

Linear Regression

Hi there, Long time no see, sorry I have been rather quite taken with various business ventures and so I have not been working on my projects as much as I should have. As I have had a lot of downtime recently as the semester winds down I have been taking my free time at work to learn some celestial mechanics.

The notes I am using are great and so I will link to them here.  It starts off with a primer on numerical methods, one of the things he asks you to do is create a program which will compute the  least squares solution to an overdetermined system, then apply that program to create a least squares fit to a set of data. This was the method Gauss used to create tables of ephermrides for asteroids, but it presents and interesting set of computational problems,

1) how does one represent a matrix in memory
2) how does one solve a system of linear equations in a finite number of steps
3) how does one then use that solution as a polynomial to evaluate a coefficent of determination.

This has occupied my time on and off for the past couple of weeks. It suffices to say I learned a lot about representing mathematical objects in a computer. Polynomials are really quite facinating actually. For example it is possible to compute a polynomial at any unknown value in Ω(n) time where n is the degree of the polynomial.

Another interesting question is how does one test a linear least squares code once its been written. The answers are found in my github repository. Here I will only present my results:

The test data and fit

Polynomial model: 
Degree: 1
Coefficients:
a_1: 7.939715
a_0: 1438.362123
r: 0.997310
r2: 0.994628


Wednesday, February 19, 2014

Be the Change

The plan is still on, I am attempting to ramp up my business in order to get to the point where I can purchase everything that I will need. But I am restless, the problem is that I have a need to tinker. To this end I have bought an Aurduino Uno board as a platform for building a Temperature compensating focuser. The focuser software will run under Linux Mint.

The thing that has always bothered me about Amateur astronomy is that the tools we need for our hobby cost so much. Software which should not be expensive is either hundreds or thousands of dollars. This needs to change, which is why I started OpenAIP. However this project will never get finished as long as all of the tools needed to go around it are gestating. The good news is that there are good hardware platforms on Windows and Linux, ASCOM and INDI respectively. The next step is to build the tools on top of these platforms for image processing.

GIMP should do the job of Photoshop, and to a certain extent it does.  But we have no equivalent for Maxim DL (hence OpenAIP). My personal opinion is that a version of linux dedicated to astronomy should exist. One where you can simply boot your computer up and try it or install it and make it work for you. But this is a goal far off in the future, for now I will start with my focuser and eventually get back to OpenAIP.

Look forward to tutorials for installing INDI on Linux Mint/Ubuntu.

Ed. 
Of course after I wrote this Cinnamon inexplicably crashed so hard I had to re-image the machine overnight.


Friday, January 24, 2014

Factoring integers for fun and profit

Sorry for the time between posts. I have been working on a project which has been monopolizing my time. Anyway we had a lull at work and there is a problem which crops up periodically which is I get a 3-5 digit number which I think might be prime. While my TI-89 will factor integer I also thought it would be relatively easy to write a program which is determines primality simply by searching for a divisor. Once a divisor greater than 1 and less than the number is found the number is composite.

Hence you simple use the mod operator in C to find a divisor and you only need to check integer less in the desired range if you do not find one then the number is prime. This program is a strait forward exersise I am thinking of giving to some of my programming tutoring students. Now I told one of the other tutors about this and they asked me if 2013 was prime, which I responded it was not. They asked me what its factorization was, which I did not know. They seemed upset, so challenge accepted. Next I was extending my program to provide the prime factorization of a number less than UINT_MAX which happens to be the same as ULONG_MAX. The result is this file.

The interesting thing is that trial division is highly inefficient method of doing prime factorization. However for integers up to 2^32-1 computers are fast enough for this method to give results in a reasonable amount of time. (1-2 secs) Now keep in mind that this is purely an interesting side project for me, as it has no practical use, i.e. code-breaking.

What was amazing was that when I went to ULLONG_MAX which is 2^64-1, this program broke. Not only is it too slow but the sheer size of integers causes segmentation faults. The first solution I thought of was using libpari to generate a list of primes. However libpari could only do this on a 64-bit native system, so the next step is to create a class which generates at its instantiation the list of primes from 2 to 2^64-1 (well 2^64-59 inclusive) This will require implementation of a decent prime sieve, since I have written Sieve of Eratosthenes before I am thinking that I will implement the Sieve of Atkin.

My hope is that this process will yield an algorithm that is O(n/log(n)) at detecting primes after the list has been generated. Doing this for arbitrary primes is a much harder question. <-massive understatement! If you have not done this and are interested in programming I highly suggest doing it once, its a wonderful afternoon of work.

Ed.  Do the back of the envelope calculation on how much memory you would need to hold all of the primes less than 2^64-1. Its a steep hill to climb.

Tuesday, November 5, 2013

Netbeans on Windows

I am more than a little bit irritable since I have started my all liquid diet. We are two weeks out from surgery as of today. The major aggravation is MiKTeX, let me do a stack trace to the beginning, after extolling the virtues of Netbeans, I decided to run it on my windows machine.  This was a mistake as I am using Avast anti-virus software and it sees compilers and IDEs not made by Microsoft as malware. In order to install Netbeans I had to disable Avast. So the procedure for that is:

  1. Download Netbeans for windows 
  2. Disable Avast
  3. Install Netbeans for windows. 

Next I installed MinGW, the minimalist gnu software distribution for windows. (Here is a link to the MinGW installer.) After all of this Avast decided that any attempt to scan the MinGW exes by Netbeans was an attack. So after adding the following file system exceptions:

Then adding the following exceptions to the "Deep Screen" feature:


I was finally able to compile C++ programs. Before I got this far I needed to do something even more annoying. It turns out that MiKTeX comes with its own version of libstdc++, and thus causes your programs to bail, so you have to statically link to libstdc++.a in the MinGW lib directory in order for the damn thing to work. So yeah fun times all around for my first day without food since I was a toddler. It should be noted that on Linux this ish just works. 

Friday, November 1, 2013

The Final Countdown (and thoughts on Cross Platform IDEs *finally*)

Hi all,

As I write this I prepare for my last weekend of eating "normally." On Monday I go in for preoperative testing, that also happens to be the day I start a two week diet of specially formulated protein shakes from Bariatric Advantage.  This is both good and bad, good in the sense that I have my meals planned for the next two weeks, bad in the sense that my stomach is full sized and so these shakes will not in any way satisfy me.

Now, I have been promising a blog post on cross platform IDEs (Integrated Development Environments) for awhile so here it is. To start, there are no good cross platform IDEs, because there are no "good" IDEs. All IDEs are about trade-offs, all of them are idiosyncratic and we must accept this because an IDE is the most general programming tool available. With that out of the way the next problem has to do with language. Oddly the most useful IDEs around are for JAVA.

Eclipse leads the pack of course, and it fits the bill for a cross platform IDE, the problem is that production JAVA code is pretty much an in-house affair, most of the code that escapes into the wild, for better or worse is, C/C++ or Python. This is where Eclipse falls down, it is the ultimate in shitshowing when it comes to C/C++ because CDT is terrible, terrible to set up, terrible to use on a daily basis. The next cross platform IDE isn't cross platform but maybe if your clever, the code it produces is, I am talking about Visual Studio, which is the standard coding platform for any professional outfit. Hopefully your code is ANSI C++ avoiding any of the proprietary stuff you could get some cross platform code, I have done this and it is a monumental bitch.

Finally I have found a solution I think, Netbeans is the best IDE I can find so far. It is truly cross platform it does pretty much every language imaginable and it has a feature for selecting available libraries installed on your system, which is much better than Visual Studio where you have to go through a metric tonne of windows just to add a single DLL to your project. However this a compromise choice, it is not as good for JAVA as Eclipse, it is not a fully featured as Visual Studio but I think it may be the sweet spot.

Monday, September 9, 2013

FFTW for fun (and maybe profit)

Hi all,

As many of you may know, for many years I have wanted to create an open source image processing suite. The core of which is a modular image processing engine. The problem with this is of course that I usually start work on it a week before the semester then all development time is swallowed whole. The first kernel of any image processing engine is the ability to identify a star on the image plane, a process known as centroiding. In order to centroid you need something like a Fourier Transform of the image (in order to compute the point spread function of the star), I say something like because there is too much noise associated with the Fourier Transform to help with very faint objects. So my first baby step was to be able to take an image from disk and get its Fourier Transform.

To do this I needed FFTW, and cfitsio. Oddly cfitsio does not compile well for 64-bit windows so a year or so ago I spent a whole night fucking around getting it to compile in VS 2010. (If you are interested in that source code it is in my git hub.) Once that was done it was a matter of figuring out how to do a real to real 2D Fourier transform using FFTW. So here we are, the image is a few seconds of noise buildup taken from my Meade DSI II:


And now its Fourier Transform:


Most students usually do this in Matlab before trying it in C++, I guess I just like to learn things the hard way.