Skip to content

Instantly share code, notes, and snippets.

@blippy
Created November 19, 2016 11:52
Show Gist options
  • Select an option

  • Save blippy/89f011ba6d4e203e6c7822fc1076c391 to your computer and use it in GitHub Desktop.

Select an option

Save blippy/89f011ba6d4e203e6c7822fc1076c391 to your computer and use it in GitHub Desktop.
orginal stats sources in suckless ptar format
Metadata Encoding: utf-8
Archive Creation Date: 2016-11-19T11:49:57Z
Path: help.txt
Type: Regular File
File Size: 185
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
stats - generate statistics
Enter data separated by whitespace, including newlines. Type `g' to
go, i.e. perform the calculations.
COMMANDS:
g go - find the stats
h this help
q quit
---
Path: Makefile
Type: Regular File
File Size: 738
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
CFLAGS=-ggdb -O0 -Wall
# Needed for cygwin:
#LIBB = -L/usr/local/lib
#INC = -I/usr/local/include
#LIBS = -lgsl -lgslcblas -lblas
# -ltecla
.PHONY: clean install
stats: mcstats.o stats.o lex.yy.o
gcc stats.o mcstats.o lex.yy.o -lm -o stats
clean:
rm -f *o stats lex.yy.c help.c *~
lex.yy.c: stats.lex stats.h
flex stats.lex
lex.yy.o: lex.yy.c
gcc -c lex.yy.c
mcstats.o : mcstats.c mcstats.h
stats.o: stats.c stats.h
gcc -c stats.c
#help.c : help.txt
# # http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967
# cat help.txt | ( echo "unsigned char help_text[] = {"; xxd -i; echo ", 0x00 };" ) > help.c
#help.o: help.c
install: stats
#mkdir -p ~/.local/bin
cp stats /usr/local/bin
---
Path: mcstats.c
Type: Regular File
File Size: 1325
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
#include <alloca.h>
#include <assert.h>
#include <math.h>
#include <stdio.h>
//#include <gsl/gsl_math.h>
//#include <gsl/gsl_statistics.h>
#include "mcstats.h"
void exp_fit_y(double *y, int n)
{
int i;
double *x;
x = alloca(n * sizeof(double));
assert(x);
for(i =0; i<n; i++)
{
x[i] = i;
y[i] = log(y[i]);
}
double a, b;
least_squares(x, y, n, &a, &b);
double r = exp(b);
pstat("fexp", r);
}
/* compute a and b least squares y = a + b x
http://www.efunda.com/math/leastsquares/lstsqr1dcurve.cfm
TODO check calcs of least_squares()
*/
void least_squares(double *x, double *y, int n, double *a , double *b)
{
int i;
double sx = 0.0;
double sxx = 0.0;
double sxy = 0.0;
double sy = 0.0;
for(i = 0; i< n; i++)
{
sx += x[i];
sxx += x[i] * x[i];
sxy += x[i] * y[i];
sy += y[i];
}
double t1 = n * sxx - sx * sx ;
*a = (sy * sxx - sx * sxy)/ t1;
*b = (n * sxy - sx * sy)/t1;
}
/* sample standard deviation
Calculations verified on 05-May-2014
*/
double sstdev(double *x, int n)
{
int i;
double sx = 0.0f, sxx = 0.0f;
for(i = 0; i <n; i++) {
sx = sx + x[i];
sxx = sxx + x[i] * x[i];
}
double a = n * sxx - sx * sx;
return sqrt(a/n/(n-1));
}
void pstat(char *name, double value)
{
printf("%s: %g\n", name, value);
}
---
Path: mcstats.h
Type: Regular File
File Size: 235
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
#ifndef MCSTATS_H
#define MCSTATS_H
void exp_fit_y(double *y, int n);
void least_squares(double *x, double *y, int n, double *a , double *b);
void pstat(char *name, double value);
double sstdev(double *x, int n);
#endif // MCSTATS_H
---
Path: README
Type: Regular File
File Size: 85
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
TODO - stats no longer requires flex
sudo apt-get install libgsl0-dev libtecla1-dev
---
Path: stats.c
Type: Regular File
File Size: 4116
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <gsl/gsl_sort.h>
//#include <gsl/gsl_statistics.h>
//#include <readline/readline.h>
#ifdef USETGETLINE
//#include <libtecla.h>
#endif
extern int yylex (void);
#include "mcstats.h"
#include "stats.h"
#define BUFLEN 4096
/* NB I have used yytext directly rather than allocated string
space. This is fine so long as no look-ahead is required, but will
cause problems otheriwse.
*/
extern char *yytext;
YYSTYPE yylval;
//extern unsigned char help_text[];
//void sort_doubles(double
#define MAXELS 7500
int nels = 0;
double yarr[MAXELS]; // data in original order
double yarrs[MAXELS]; // sorted data
// #define STR_EQ(s1,s2) (strcmp(s1,s2) == 0)
// #define CMD_EQ(s1) (STR_EQ(yytext, s1))
int cmp(const void *a, const void *b)
{
double v1 = *(double *)a;
double v2 = *(double *)b;
double v3 = v1 - v2;
return ( 0 < v3) - (v3 < 0) ;
}
void go()
{
int i;
double m, min, max;
memcpy(yarrs, yarr, nels *sizeof(double));
qsort(yarrs, nels, sizeof(double), cmp);
//gsl_sort(yarrs, 1, nels);
puts("sorted array:");
for(i =0; i<nels; i++)
{
m = (double)100*i/nels;
printf("%d %lf %lf\n", i, m, yarrs[i]);
}
//gsl_stats_minmax(&min, &max, yarr,1 , nels);
min = yarrs[0];
pstat("min", min);
max = yarrs[nels-1];
pstat("max", max);
m = yarr[nels-1];
pstat("r2r", (max -m)/(m-min)); // reward-to-risk ratio
// geometric mean
m = 1;
for(i =0; i<nels; i++) { m*= yarr[i]; }
m = pow(m, 1.0/nels);
pstat("geom", m);
// harmonic mean
m = 0;
for(i =0; i<nels; i++) { m+= 1.0/yarr[i]; }
m = nels / m;
pstat("harm", m);
m = 0.0f;
for(i=0; i<nels; i++) { m += yarr[i]; }
m = m/(float)nels;
//m = gsl_stats_mean(yarrs, 1, nels);
pstat("mean", m);
m = (yarrs[nels/2] + yarrs[(nels-1)/2])/2.0f;
//m = gsl_stats_median_from_sorted_data(yarrs,1, nels);
pstat("median", m);
pstat("num", (float)nels);
pstat("sstddev", sstdev(yarr, nels));
m = 0.0;
for(i=0; i<nels; i++) { m+= yarr[i] ; }
pstat("sum", m);
//m = gsl_stats_sd(yarr, 1, nels);
//pstat("stdevs", m);
exp_fit_y(yarr, nels);
for(i=0; i<20; i++) { fputc('-', stdout); }
printf("\n");
nels = 0;
}
// http://stackoverflow.com/questions/5457608/c-remove-character-from-string
void remove_char(char *str, char garbage) {
char *src, *dst;
for (src = dst = str; *src != '\0'; src++) {
*dst = *src;
if (*dst != garbage) dst++;
}
*dst = '\0';
}
void process_token(char *token)
{
//printf("token: *%s*\n", token);
switch(token[0])
{
case 'g': go(); break;
//case 'h': puts((const char *)help_text); break;
case 'q': exit(0); break;
default:
remove_char(token, ',');
char *pend;
float f;
f = strtof(token, &pend);
if(pend[0] == '\0') // if there's junk at the end then it wont be \0
{
assert(nels< MAXELS);
yarr[nels] = f;
nels++;
}
else
{ puts("Rejected"); }
}
}
void process_line(char *line)
{
char buf[BUFLEN];
char delim[] = " \t\r\n";
char *token;
strcpy(buf, line);
token = strtok(buf, delim);
while(token != NULL)
{
process_token(token);
token = strtok(NULL, delim);
}
}
/*
void loop_using_buffer()
{
loop:
fgets(buf, BUFLEN, stdin);
token = strtok(buf, delim);
while(token != NULL)
{
process_token(token);
token = strtok(NULL, delim);
}
goto loop;
/
}
*/
void loop_using_lex()
{
int t;
loop:
t = yylex();
if (t != NEWLINE) process_token(yytext);
goto loop;
}
#ifdef USEGETLINE
/* void loop_using_getline()
{
char *line;
GetLine *gl;
gl = new_GetLine(BUFLEN, 2048);
assert(gl);
while ((line=gl_get_line(gl, "", NULL, -1)) != NULL) {process_line(line);}
gl = del_GetLine(gl);
}
*/
#endif
int main()
{
puts("stats 1.0");
puts("Common commands: gq. h for help");
//loop_using_getline();
//loop_using_buffer();
loop_using_lex();
return EXIT_SUCCESS;
}
---
Path: stats.f90
Type: Regular File
File Size: 731
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
program stat
use fgsl
implicit none
real(fgsl_double) :: data(5) = (/17.2D0, 18.1D0, 16.5D0, 18.3D0, 12.6D0 /)
real(fgsl_double) :: mean, variance, largest, smallest
mean = fgsl_stats_mean(data, 1_fgsl_size_t, 5_fgsl_size_t)
variance = fgsl_stats_variance(data, 1_fgsl_size_t, 5_fgsl_size_t)
largest = fgsl_stats_max(data, 1_fgsl_size_t, 5_fgsl_size_t)
smallest = fgsl_stats_min(data, 1_fgsl_size_t, 5_fgsl_size_t)
write(6, '(''The dataset is '',5(F9.5))') data
write(6, '(''The sample mean is '',F9.5)') mean
write(6, '(''The estimated variance is '',F9.5)') variance
write(6, '(''The largest value is '',F9.5)') largest
write(6, '(''The smallest value is '',F9.5)') smallest
end program stat
---
Path: stats.h
Type: Regular File
File Size: 320
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
/* created by mcarter
27-Oct-2013
*/
enum yytokentype {
CMD = 258,
DOUBLE,
STRING ,
NEWLINE
};
typedef union YYSTYPE
{
double dval;
char *string;
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
extern YYSTYPE yylval;
---
Path: stats.lex
Type: Regular File
File Size: 1869
User Name: mcarter
User ID: 3003
Group Name: mcarter
Group ID: 4003
Permissions: 0000644
Modification Time: 1477498122
---
/* a shlex-like scanner
created by mcarter
http://flex.sourceforge.net/manual/Patterns.html
flex toke.lex
for clues and enhancements on processing strings,
search for `quoted strings' at
http://flex.sourceforge.net/manual/Start-Conditions.html
*/
%{
//#include <readline/readline.h>
#include "stats.h"
#include <stdlib.h>
// void yyerror(char *s);
void yyerror(char *s) {
printf("%d: %s at %s\n", yylineno, s, yytext);
}
char *p;
//extern YYSTYPE yylval;
%}
DQUOTE "\""
HASH "#"
ALPHA [:alphanum:]
C1 [0-9a-zA-Z]
WHITE [ \t\r]
LW ^[ \t]*
/* VCHAR (C1|"-"|".") */
VCHAR [0-9a-zA-Z]|-|"."|"_"|"?"|":"|";"|"+"
%x str
%%
static int MAX_STR_CONST = 1024;
char string_buf[MAX_STR_CONST];
char *string_buf_ptr;
\" string_buf_ptr = string_buf; BEGIN(str);
<str>\" { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
p=(char *)calloc(strlen(string_buf)+1,sizeof(char));
strcpy(p,string_buf);
yylval.string=p;
return STRING;
}
<str>[^\"]+ {
char *yptr = yytext;
while ( *yptr )
*string_buf_ptr++ = *yptr++;
}
{HASH}[^\n]*"\n" /* eat up comments */
[+-]?[0-9]+(\.[0-9]+)? { yylval.dval = atof(yytext) ; return DOUBLE ;}
[a-z]+ { return CMD;}
{VCHAR}+ { p=(char *)calloc(strlen(yytext)+1,sizeof(char));
strcpy(p,yytext);
yylval.string=p; return STRING;}
^[ \t\r]*"\n"
{WHITE}+ /* eat up whitespace */
"£" /* drop the pound sign */
"\n" { return NEWLINE ;}
. { printf("Uncaught: %c\n", yytext[0]);
yyerror("invalid character");}
%%
int yywrap(void) {
return 1;
}
/*
int main( int argc, char **argv )
{
puts("2");
yylex() ;
}
*/
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment