Hacker News new | ask | show | jobs
by Sprint 4673 days ago
I'd just use octave. It's as simple as

  $ octave
  octave:1> a=load('numbers.txt');  
  octave:2> sum(a)
  ans =  55
  octave:3> mean(a)
  ans =  5.5000
  octave:4> std(a)
  ans =  3.0277
  octave:5> quantile(a)
  ans =
  
      1.0000
      3.0000
      5.5000
      8.0000
     10.0000
etc
2 comments

I like octave and R!

The reason I wrote this script was to get quick results from the command line.

For instance: I could use grep, cut and other unix tools to get the numbers from a file and make quick calculations.

Of course, for complex processing I would use octave or R.

Yeah, I was thinking about that and spent the past minutes to make me some Bash functions like:

  function mean() {
          octave -q --eval "mean = mean(load('$1'))"
  }
Then just run "mean numbers.txt".

I am sure your approach is much quicker, octave takes a good 0.5s(!) to load on my machine.

Yup, octave requires more time to warm up.

Regarding speed, for simple calculations like sum, mean and variance, the bottleneck is in I/O.

Would you be able to use Octave for reading from stdin?
Yes, Sprint suggested this:

    octave -q --eval "mean = mean(load('$1'))"
But, again, octave requires more time to warm up...
That does not read the data from stdin. You could probably get it to work with some bash wizardry. But maybe not. I did spend some time on this a few years ago and it may have changed in the interim, but my memory is that I tried load("/dev/stdin") and using a fifo etc and it doesn't work (probably because load() uses some seeks to determine matrix data shape before reading data in i.e. data is read in as columns instead of reading in rows and transposing). At least that was my takeaway. At least if you want to use the load() builtin.

Basically you just need to write an octave function that reads values from the terminal.