boyfarrell.com

6 November 2005

Objective-C Vector Class

Here is a custom Vector class I have been working on: Vector.h and Vector.m. Please find the archived Xcode 2.1 project file. The Vector class is a wrapper class for a standard C array but with many methods which you might want to use on your own data. It makes it easy to handle large column of numbers oh and using gnuplot makes them pretty…

beautiful

It basically allows you to handle arrays as one object; the instance variable is just an array. I might get a bit fancy with version 2 of this code, by adding AltiVec capability etc. If you know how get in touch!

To use the plot features you will need to have gnuplot installed and also include the gnuplot_i .h and .m files in you project. For the conversion methods you will also need the values of certain physical constants defined here. Additionally, if you want to use the -readDataFrom method you will need the very rudimentary read data from text file c functions. Currently they only only work on text files with single column of numbers. Here is some such data, wavelength column and a data column. The #define ptsF is the number of element in the Vector and is declared in the abstract class ‘Model’ from which Vector inherits (Model.h and Model.m).

The above should work with the following main.m. Please pass on any advice or comments I’d be interested to find out what you think.

int main(int argc, char *argv[])
{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//address of data files
const char *wavAddress = “/Users/Daniel/Data/wavelength201.txt”;
const char *incAddress = “/Users/Daniel/Data/i1201.txt”;

//alloc and init then assign Vector instance values from text file data
Vector *wavelength = [[Vector alloc] init];
[wavelength readFromFile: wavAddress];

Vector *solarSpectrumData = [[Vector alloc] init];
[solarSpectrumData readFromFile: incAddress];

//plot solarSpectrumData (y-axis) against wavelength (x-axis)
[solarSpectrumData plotAgainstVector: wavelength];

[wavelength release];
[solarSpectrumData release];

[pool release];

return 0;

}

No Comments currently posted.

Post a comment on this entry: