1 // This file is part of the FXT library.
2 // Copyright (C) 2010, 2012 Joerg Arndt
3 // License: GNU General Public License version 3 or later,
4 // see the file COPYING.txt in the main directory.
5 
6 #include "aux0/swap.h"
7 #include "aux1/aux1double.h"  // norm()
8 #include "sort/minmaxmed23.h"  // max2()
9 #include "sort/minmax.h"  // min_max()
10 
11 #include "fxtalloca.h"
12 #include "fxttypes.h"
13 
14 #include "fxtio.h"
15 
16 #define CHOP(x,eps)  (fabs(x)<(eps) ? 0 : (x))
17 
18 #include <cmath> // fabs()
19 
20 
21 void
graph_print(const char * bla,const double * f,ulong n,ulong width,double eps)22 graph_print(const char *bla, const double *f, ulong n, ulong width/*=60*/, double eps/*=0.0*/)
23 {
24     cout << endl;
25     if ( bla )  cout << bla;
26 
27     ALLOCA(char, line, width+1);
28 
29     cout << "   norm = " << norm(f, n);
30     cout << endl;
31 
32     double mi, ma;
33     min_max(f, n, &mi, &ma);
34     double mx = max2( fabs(mi), fabs(ma));
35     ma =   mx * 1.1;
36     mi = - mx * 1.1;
37     double d = ma - mi;
38     double v = ( d>1e-9 ? 1.0/d * (double)width : 0.0 );
39     ulong z = ulong( -mi * v );  // zero position
40     if ( z>width )  z = 0;
41     line[width] = 0;
42 
43     for (ulong k=0; k<n; ++k)
44     {
45         double r = f[k];
46 
47         for (ulong j=0; j<=width; ++j)  line[j] = ' ';
48         ulong g1 = z;
49         ulong val = (ulong)( (r-mi)*v+0.01 );
50         ulong g2 = val;
51         if ( g1>g2 )  ::swap2(g1, g2);
52         for (ulong j=g1; j<=g2; ++j)  line[j] = '-';
53 
54         if ( z )  line[z] = '|';
55         line[val] = '*';
56         cout << line;
57 
58         r = CHOP(r,eps);
59         cout << "   ";
60         cout.width( 12 );
61         cout << r;
62         cout << "   " << k;
63         cout << endl;
64     }
65     cout << endl;
66 }
67 // -------------------------
68 
69