tioga 1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Tioga_README +372 -0
- data/lgpl.txt +504 -0
- data/split/Dtable/defs.h +33 -0
- data/split/Dtable/dtable.c +1928 -0
- data/split/Dtable/dtable_intern.h +144 -0
- data/split/Dtable/dvector.h +61 -0
- data/split/Dtable/extconf.rb +4 -0
- data/split/Dtable/include/dtable.h +35 -0
- data/split/Dtable/lib/Dtable_extras.rb +90 -0
- data/split/Dtable/namespace.h +47 -0
- data/split/Dtable/safe_double.h +104 -0
- data/split/Dtable/symbols.c +92 -0
- data/split/Dtable/symbols.h +52 -0
- data/split/Dvector/defs.h +33 -0
- data/split/Dvector/dvector.c +5486 -0
- data/split/Dvector/dvector_intern.h +142 -0
- data/split/Dvector/extconf.rb +4 -0
- data/split/Dvector/include/dvector.h +61 -0
- data/split/Dvector/lib/Dvector_extras.rb +328 -0
- data/split/Dvector/lib/Numeric_extras.rb +134 -0
- data/split/Dvector/namespace.h +47 -0
- data/split/Dvector/safe_double.h +104 -0
- data/split/Dvector/symbols.c +92 -0
- data/split/Dvector/symbols.h +52 -0
- data/split/Flate/defs.h +33 -0
- data/split/Flate/extconf.rb +19 -0
- data/split/Flate/flate.c +156 -0
- data/split/Flate/flate_intern.h +97 -0
- data/split/Flate/include/flate.h +98 -0
- data/split/Flate/namespace.h +47 -0
- data/split/Flate/safe_double.h +104 -0
- data/split/Flate/symbols.c +92 -0
- data/split/Flate/symbols.h +52 -0
- data/split/Function/defs.h +33 -0
- data/split/Function/dvector.h +61 -0
- data/split/Function/extconf.rb +4 -0
- data/split/Function/function.c +988 -0
- data/split/Function/joint_qsort.c +258 -0
- data/split/Function/lib/Function_extras.rb +44 -0
- data/split/Function/namespace.h +47 -0
- data/split/Function/safe_double.h +104 -0
- data/split/Function/symbols.c +92 -0
- data/split/Function/symbols.h +52 -0
- data/split/Tioga/axes.c +774 -0
- data/split/Tioga/defs.h +33 -0
- data/split/Tioga/dtable.h +35 -0
- data/split/Tioga/dvector.h +61 -0
- data/split/Tioga/extconf.rb +4 -0
- data/split/Tioga/figures.c +672 -0
- data/split/Tioga/figures.h +855 -0
- data/split/Tioga/flate.h +98 -0
- data/split/Tioga/init.c +524 -0
- data/split/Tioga/lib/Arcs_and_Circles.rb +64 -0
- data/split/Tioga/lib/ColorConstants.rb +274 -0
- data/split/Tioga/lib/Colorbars.rb +10 -0
- data/split/Tioga/lib/Colormaps.rb +105 -0
- data/split/Tioga/lib/Coordinate_Conversions.rb +194 -0
- data/split/Tioga/lib/Creating_Paths.rb +94 -0
- data/split/Tioga/lib/Doc.rb +91 -0
- data/split/Tioga/lib/Executive.rb +515 -0
- data/split/Tioga/lib/FigMkr.rb +2224 -0
- data/split/Tioga/lib/FigureConstants.rb +125 -0
- data/split/Tioga/lib/Figures_and_Plots.rb +268 -0
- data/split/Tioga/lib/Images.rb +278 -0
- data/split/Tioga/lib/Legends.rb +190 -0
- data/split/Tioga/lib/MarkerConstants.rb +122 -0
- data/split/Tioga/lib/Markers.rb +129 -0
- data/split/Tioga/lib/Page_Frame_Bounds.rb +567 -0
- data/split/Tioga/lib/Rectangles.rb +94 -0
- data/split/Tioga/lib/Shading.rb +100 -0
- data/split/Tioga/lib/Special_Paths.rb +307 -0
- data/split/Tioga/lib/Strokes.rb +129 -0
- data/split/Tioga/lib/TeX_Text.rb +454 -0
- data/split/Tioga/lib/TexPreamble.rb +358 -0
- data/split/Tioga/lib/Titles_and_Labels.rb +306 -0
- data/split/Tioga/lib/Transparency.rb +89 -0
- data/split/Tioga/lib/Using_Paths.rb +164 -0
- data/split/Tioga/lib/Utils.rb +74 -0
- data/split/Tioga/lib/X_and_Y_Axes.rb +749 -0
- data/split/Tioga/lib/irb_tioga.rb +122 -0
- data/split/Tioga/lib/tioga.rb +1 -0
- data/split/Tioga/lib/tioga_ui.rb +5 -0
- data/split/Tioga/lib/tioga_ui_cmds.rb +793 -0
- data/split/Tioga/makers.c +989 -0
- data/split/Tioga/mk_tioga_sty.rb +53 -0
- data/split/Tioga/namespace.h +47 -0
- data/split/Tioga/pdf_font_dicts.c +18253 -0
- data/split/Tioga/pdfcolor.c +486 -0
- data/split/Tioga/pdfcoords.c +505 -0
- data/split/Tioga/pdffile.c +342 -0
- data/split/Tioga/pdfimage.c +536 -0
- data/split/Tioga/pdfpath.c +914 -0
- data/split/Tioga/pdfs.h +229 -0
- data/split/Tioga/pdftext.c +443 -0
- data/split/Tioga/safe_double.h +104 -0
- data/split/Tioga/symbols.c +92 -0
- data/split/Tioga/symbols.h +52 -0
- data/split/Tioga/texout.c +380 -0
- data/split/defs.h +33 -0
- data/split/extconf.rb +107 -0
- data/split/mkmf2.rb +1612 -0
- data/split/namespace.h +47 -0
- data/split/safe_double.h +104 -0
- data/split/scripts/tioga +4 -0
- data/split/symbols.c +92 -0
- data/split/symbols.h +52 -0
- data/tests/dtable_test.data +6 -0
- data/tests/dvector_read_test.data +1 -0
- data/tests/dvector_test.data +101 -0
- data/tests/tc_Dtable.rb +221 -0
- data/tests/tc_Dvector.rb +791 -0
- data/tests/tc_FMkr.rb +162 -0
- data/tests/tc_Flate.rb +45 -0
- data/tests/tc_Function.rb +111 -0
- data/tests/ts_Tioga.rb +38 -0
- metadata +163 -0
@@ -0,0 +1,142 @@
|
|
1
|
+
/* Dvector.h */
|
2
|
+
/*
|
3
|
+
Copyright (C) 2005 Bill Paxton
|
4
|
+
|
5
|
+
Dvector is free software; you can redistribute it and/or modify
|
6
|
+
it under the terms of the GNU General Library Public License as published
|
7
|
+
by the Free Software Foundation; either version 2 of the License, or
|
8
|
+
(at your option) any later version.
|
9
|
+
|
10
|
+
Dvector is distributed in the hope that it will be useful,
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
GNU Library General Public License for more details.
|
14
|
+
|
15
|
+
You should have received a copy of the GNU Library General Public License
|
16
|
+
along with Dvector; if not, write to the Free Software
|
17
|
+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
*/
|
19
|
+
|
20
|
+
#ifndef __Dvector_H__
|
21
|
+
#define __Dvector_H__
|
22
|
+
|
23
|
+
#include "ruby.h"
|
24
|
+
#include <stdbool.h>
|
25
|
+
#include <namespace.h>
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
/*======================================================================*/
|
30
|
+
|
31
|
+
PUBLIC void Init_Dvector();
|
32
|
+
/* dirty hack to prevent namespace conflicts */
|
33
|
+
PRIVATE bool Is_Dvector(VALUE obj);
|
34
|
+
PRIVATE double *Dvector_Data_for_Read(VALUE dvector, long *len_ptr); /* returns pointer to the dvector's data (which may be shared) */
|
35
|
+
PRIVATE double *Dvector_Data_Copy(VALUE dvector, long *len_ptr); /* like Dvector_Data_for_Read, but returns pointer to a copy of the data */
|
36
|
+
PRIVATE double *Dvector_Data_for_Write(VALUE dvector, long *len_ptr);
|
37
|
+
PRIVATE double *Dvector_Data_Resize(VALUE dvector, long new_len);
|
38
|
+
PRIVATE double *Dvector_Data_Replace(VALUE dvector, long len, double *data); /* copies the data into the dvector */
|
39
|
+
PRIVATE VALUE Dvector_Create(void);
|
40
|
+
PRIVATE void Dvector_Push_Double(VALUE ary, double val);
|
41
|
+
PRIVATE void Dvector_Store_Double(VALUE ary, long idx, double val);
|
42
|
+
|
43
|
+
PRIVATE VALUE Read_Dvectors(char *filename, VALUE destinations, int first_row_of_file, int number_of_rows);
|
44
|
+
PRIVATE VALUE Read_Row(char *filename, int row, VALUE row_ary);
|
45
|
+
|
46
|
+
/* implementations for Ruby methods */
|
47
|
+
|
48
|
+
PRIVATE VALUE dvector_apply_math_op(VALUE source, double (*op)(double));
|
49
|
+
PRIVATE VALUE dvector_apply_math_op2(VALUE ary1, VALUE ary2, double (*op)(double, double));
|
50
|
+
PRIVATE VALUE dvector_apply_math_op_bang(VALUE ary, double (*op)(double));
|
51
|
+
PRIVATE VALUE dvector_apply_math_op2_bang(VALUE ary1, VALUE ary2, double (*op)(double, double));
|
52
|
+
|
53
|
+
PRIVATE VALUE dvector_freeze(VALUE ary);
|
54
|
+
PRIVATE VALUE dvector_frozen_p(VALUE ary);
|
55
|
+
PRIVATE VALUE make_new_dvector(VALUE klass, long len, long capa);
|
56
|
+
PRIVATE VALUE dvector_new2(long len, long capa);
|
57
|
+
PRIVATE VALUE dvector_new();
|
58
|
+
PRIVATE VALUE dvector_new4(long len, VALUE *data);
|
59
|
+
PRIVATE VALUE dvector_new4_dbl(long len, double *data);
|
60
|
+
PRIVATE VALUE dvector_check_array_type(VALUE ary);
|
61
|
+
PRIVATE VALUE dvector_initialize(int argc, VALUE *argv, VALUE ary);
|
62
|
+
PRIVATE VALUE dvector_push(VALUE ary, VALUE item);
|
63
|
+
PRIVATE VALUE dvector_push_m(int argc, VALUE *argv, VALUE ary);
|
64
|
+
PRIVATE VALUE dvector_pop(VALUE ary);
|
65
|
+
PRIVATE VALUE dvector_shift(VALUE ary);
|
66
|
+
PRIVATE VALUE dvector_unshift_m(int argc, VALUE *argv, VALUE ary);
|
67
|
+
PRIVATE VALUE dvector_entry(VALUE ary, long offset);
|
68
|
+
PRIVATE VALUE dvector_subseq(VALUE ary, long beg, long len);
|
69
|
+
PRIVATE VALUE dvector_aref(int argc, VALUE *argv, VALUE ary);
|
70
|
+
PRIVATE VALUE dvector_at(VALUE ary, VALUE pos);
|
71
|
+
PRIVATE VALUE dvector_first(int argc, VALUE *argv, VALUE ary);
|
72
|
+
PRIVATE VALUE dvector_last(int argc, VALUE *argv, VALUE ary);
|
73
|
+
PRIVATE VALUE dvector_uniq_bang(VALUE ary);
|
74
|
+
PRIVATE VALUE dvector_uniq(VALUE ary);
|
75
|
+
PRIVATE VALUE dvector_fetch(int argc, VALUE *argv, VALUE ary);
|
76
|
+
PRIVATE VALUE dvector_index(VALUE ary, VALUE val);
|
77
|
+
PRIVATE VALUE dvector_rindex(VALUE ary, VALUE val);
|
78
|
+
PRIVATE VALUE dvector_aset(int argc, VALUE *argv, VALUE ary);
|
79
|
+
PRIVATE VALUE dvector_insert(int argc, VALUE *argv, VALUE ary);
|
80
|
+
PRIVATE VALUE dvector_each(VALUE ary);
|
81
|
+
PRIVATE VALUE dvector_each2(VALUE ary, VALUE ary2);
|
82
|
+
PRIVATE VALUE dvector_each_index(VALUE ary);
|
83
|
+
PRIVATE VALUE dvector_each_with_index(VALUE ary);
|
84
|
+
PRIVATE VALUE dvector_each2_with_index(VALUE ary, VALUE ary2);
|
85
|
+
PRIVATE VALUE dvector_reverse_each(VALUE ary);
|
86
|
+
PRIVATE VALUE dvector_reverse_each2(VALUE ary, VALUE ary2);
|
87
|
+
PRIVATE VALUE dvector_reverse_each_index(VALUE ary);
|
88
|
+
PRIVATE VALUE dvector_reverse_each_with_index(VALUE ary);
|
89
|
+
PRIVATE VALUE dvector_reverse_each2_with_index(VALUE ary, VALUE ary2);
|
90
|
+
PRIVATE VALUE dvector_length(VALUE ary);
|
91
|
+
PRIVATE VALUE dvector_empty_p(VALUE ary);
|
92
|
+
PRIVATE VALUE dvector_dup(VALUE ary);
|
93
|
+
PRIVATE VALUE dvector_join(VALUE ary, VALUE sep);
|
94
|
+
PRIVATE VALUE dvector_join_m(int argc, VALUE *argv, VALUE ary);
|
95
|
+
PRIVATE VALUE dvector_to_s(VALUE ary);
|
96
|
+
PRIVATE VALUE dvector_to_a(VALUE ary);
|
97
|
+
PRIVATE VALUE dvector_read(int argc, VALUE *argv, VALUE klass);
|
98
|
+
PRIVATE VALUE dvector_read_row(int argc, VALUE *argv, VALUE klass);
|
99
|
+
PRIVATE VALUE dvector_read_rows(int argc, VALUE *argv, VALUE klass);
|
100
|
+
PRIVATE VALUE dvector_reverse(VALUE ary);
|
101
|
+
PRIVATE VALUE dvector_reverse_bang(VALUE ary);
|
102
|
+
PRIVATE VALUE dvector_reverse_m(VALUE ary);
|
103
|
+
PRIVATE VALUE dvector_sort_bang(VALUE ary);
|
104
|
+
PRIVATE VALUE dvector_sort(VALUE ary);
|
105
|
+
PRIVATE VALUE dvector_collect(VALUE ary);
|
106
|
+
PRIVATE VALUE dvector_collect2(VALUE ary, VALUE ary2);
|
107
|
+
PRIVATE VALUE dvector_collect_bang(VALUE ary);
|
108
|
+
PRIVATE VALUE dvector_collect2_bang(VALUE ary, VALUE ary2);
|
109
|
+
PRIVATE VALUE dvector_values_at(int argc, VALUE *argv, VALUE ary);
|
110
|
+
PRIVATE VALUE dvector_select(VALUE ary);
|
111
|
+
PRIVATE VALUE dvector_delete(VALUE ary, VALUE item);
|
112
|
+
PRIVATE VALUE dvector_delete_at(VALUE ary, long pos);
|
113
|
+
PRIVATE VALUE dvector_slice_bang(int argc, VALUE *argv, VALUE ary);
|
114
|
+
PRIVATE VALUE dvector_reject_bang(VALUE ary);
|
115
|
+
PRIVATE VALUE dvector_reject(VALUE ary);
|
116
|
+
PRIVATE VALUE dvector_delete_if(VALUE ary);
|
117
|
+
PRIVATE VALUE dvector_replace(VALUE dest, VALUE orig);
|
118
|
+
PRIVATE VALUE dvector_clear(VALUE ary);
|
119
|
+
PRIVATE VALUE dvector_fill(int argc, VALUE *argv, VALUE ary);
|
120
|
+
PRIVATE VALUE dvector_eql(VALUE ary1, VALUE ary2);
|
121
|
+
PRIVATE VALUE dvector_includes(VALUE ary, VALUE item);
|
122
|
+
PRIVATE VALUE dvector_cmp(VALUE ary1, VALUE ary2);
|
123
|
+
PRIVATE VALUE dvector_mod(VALUE ary, VALUE arg);
|
124
|
+
PRIVATE VALUE dvector_min(int argc, VALUE *argv, VALUE ary);
|
125
|
+
PRIVATE VALUE dvector_max(int argc, VALUE *argv, VALUE ary);
|
126
|
+
|
127
|
+
PRIVATE void c_dvector_create_spline_interpolant(int n_pts_data, double *Xs, double *Ys,
|
128
|
+
bool start_clamped, double start_slope, bool end_clamped, double end_slope,
|
129
|
+
double *Bs, double *Cs, double *Ds);
|
130
|
+
PRIVATE VALUE dvector_create_spline_interpolant(int argc, VALUE *argv, VALUE klass);
|
131
|
+
|
132
|
+
PRIVATE double c_dvector_spline_interpolate(double x, int n_pts_data,
|
133
|
+
double *Xs, double *Ys, double *Bs, double *Cs, double *Ds);
|
134
|
+
PRIVATE VALUE dvector_spline_interpolate(int argc, VALUE *argv, VALUE klass);
|
135
|
+
|
136
|
+
PRIVATE double c_dvector_linear_interpolate(int num_pts, double *xs, double *ys, double x);
|
137
|
+
PRIVATE VALUE dvector_linear_interpolate(int argc, VALUE *argv, VALUE klass);
|
138
|
+
|
139
|
+
/* end of dirty hack */
|
140
|
+
|
141
|
+
#endif /* __Dvector_H__ */
|
142
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
/* Dvector.h */
|
2
|
+
/*
|
3
|
+
Copyright (C) 2005 Bill Paxton
|
4
|
+
|
5
|
+
Dvector is free software; you can redistribute it and/or modify
|
6
|
+
it under the terms of the GNU General Library Public License as published
|
7
|
+
by the Free Software Foundation; either version 2 of the License, or
|
8
|
+
(at your option) any later version.
|
9
|
+
|
10
|
+
Dvector is distributed in the hope that it will be useful,
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
GNU Library General Public License for more details.
|
14
|
+
|
15
|
+
You should have received a copy of the GNU Library General Public License
|
16
|
+
along with Dvector; if not, write to the Free Software
|
17
|
+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
*/
|
19
|
+
|
20
|
+
#ifndef __Dvector_H__
|
21
|
+
#define __Dvector_H__
|
22
|
+
|
23
|
+
/* this file has been heavily modified by Vincent Fourmond to take care
|
24
|
+
of the 'RCR330' scheme of exporting symbols
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include <symbols.h>
|
28
|
+
#include <stdbool.h>
|
29
|
+
|
30
|
+
/*======================================================================*/
|
31
|
+
|
32
|
+
/* functions for handling Dvectors: */
|
33
|
+
|
34
|
+
DECLARE_SYMBOL(double *, Dvector_Data_for_Read, (VALUE dvector, long *len_ptr));
|
35
|
+
/* returns pointer to the dvector's data (which may be shared) */
|
36
|
+
DECLARE_SYMBOL(double *, Dvector_Data_Copy, (VALUE dvector, long *len_ptr));
|
37
|
+
/* like Dvector_Data_for_Read, but returns pointer to a copy of the data */
|
38
|
+
DECLARE_SYMBOL(double *, Dvector_Data_for_Write,
|
39
|
+
(VALUE dvector, long *len_ptr));
|
40
|
+
DECLARE_SYMBOL(double *, Dvector_Data_Resize, (VALUE dvector, long new_len));
|
41
|
+
DECLARE_SYMBOL(double *, Dvector_Data_Replace,
|
42
|
+
(VALUE dvector, long len, double *data));
|
43
|
+
/* copies the data into the dvector */
|
44
|
+
DECLARE_SYMBOL(VALUE, Dvector_Create, (void));
|
45
|
+
DECLARE_SYMBOL(void, Dvector_Store_Double, (VALUE ary, long idx, double val));
|
46
|
+
/* pushes one element onto the vector */
|
47
|
+
DECLARE_SYMBOL(void, Dvector_Push_Double, (VALUE ary, double val));
|
48
|
+
|
49
|
+
/* functions for interpolation */
|
50
|
+
DECLARE_SYMBOL(double, c_dvector_spline_interpolate,
|
51
|
+
(double x, int n_pts_data, double *Xs, double *Ys,
|
52
|
+
double *Bs, double *Cs, double *Ds));
|
53
|
+
DECLARE_SYMBOL(double, c_dvector_linear_interpolate,
|
54
|
+
(int num_pts, double *xs, double *ys, double x));
|
55
|
+
DECLARE_SYMBOL(void, c_dvector_create_spline_interpolant,
|
56
|
+
(int n_pts_data, double *Xs, double *Ys,
|
57
|
+
bool start_clamped, double start_slope,
|
58
|
+
bool end_clamped, double end_slope,
|
59
|
+
double *As, double *Bs, double *Cs));
|
60
|
+
#endif /* __Dvector_H__ */
|
61
|
+
|
@@ -0,0 +1,328 @@
|
|
1
|
+
# Tioga::Dvector_extras.rb
|
2
|
+
|
3
|
+
module Dobjects
|
4
|
+
|
5
|
+
# MathEvaluator enables one to evaluate a simple mathematical expression
|
6
|
+
# such as "x[0] + cos(x[1])", where the array x is given at each call to
|
7
|
+
# compute, or "x + y**z", or...
|
8
|
+
#
|
9
|
+
# This class acts as a backend for Dvector.compute_formula, to make sure
|
10
|
+
# that the Math module is included, without the drawback of cluttering
|
11
|
+
# all Math functions in Dvector, which would admittedly be quite stupid.
|
12
|
+
|
13
|
+
class MathEvaluator
|
14
|
+
include Math
|
15
|
+
|
16
|
+
# Creates an evaluator for a formula. +formula+ is the formula. It is
|
17
|
+
# transformed into a block that takes +argname+ as an argument --
|
18
|
+
# +argname+ can be whatever you want. +mods+ are the modules you would
|
19
|
+
# like the formula to include. Math is included by default, but you
|
20
|
+
# can include other ones to make other kinds of functions available.
|
21
|
+
#
|
22
|
+
# MathEvaluator.new("col[0] + col[1]", "col")
|
23
|
+
# MathEvaluator.new("x*cos(y)", "x,y")
|
24
|
+
def initialize(formula, argname, mods = [])
|
25
|
+
for mod in mods
|
26
|
+
self.extend mod
|
27
|
+
end
|
28
|
+
@block = eval "proc { |#{argname}| #{formula} }"
|
29
|
+
end
|
30
|
+
|
31
|
+
# This function does the actual evaluation with the blocks
|
32
|
+
# given.
|
33
|
+
#
|
34
|
+
# e = MathEvaluator.new("x*y", "x,y")
|
35
|
+
# e.compute(1,2) -> 2
|
36
|
+
def compute(*args)
|
37
|
+
return @block.call(*args)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Dvector
|
42
|
+
|
43
|
+
def to_dvector
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
# Dvector.fancy_read's defaults options. See that function for
|
48
|
+
# more details
|
49
|
+
FANCY_READ_DEFAULTS = {
|
50
|
+
'sep' => /\s+/,
|
51
|
+
'comments' => /^\s*\#/,
|
52
|
+
'skip_first' => 0,
|
53
|
+
'index_col' => false,
|
54
|
+
'headers' => nil, # not used for now.
|
55
|
+
'default' => 0.0/0.0, # defaults to NaN
|
56
|
+
'remove_space' => true ,# removes spaces at the beginning of the lines
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
# This function reads in +stream+ (can an IO object or a String,
|
61
|
+
# in which case it represents the name of a file to be opened)
|
62
|
+
# the columns specified by +cols+ and returns them. column 0 is the
|
63
|
+
# first column. If +cols+ is +nil+, then fancy_read attempts to find
|
64
|
+
# all the columns in the file, while filling absent data with NaN.
|
65
|
+
#
|
66
|
+
# +opts+ is a hash for tuning the behavior of the reading. It can hold
|
67
|
+
# the following keys:
|
68
|
+
# 'sep':: the record separator
|
69
|
+
# 'comments':: a regular expression matching comment lines
|
70
|
+
# 'skip_first':: how many lines to skip at the beginning of the file,
|
71
|
+
# 'default':: the value taken for missing elements
|
72
|
+
# 'index_col':: if set to true, the first column contains the
|
73
|
+
# indices of the corresponding lines (0 for first and so on)
|
74
|
+
|
75
|
+
def Dvector.fancy_read(stream, cols = nil, opts = {}) # :doc:
|
76
|
+
# first, we turn the stream into a real IO stream
|
77
|
+
if stream.is_a?(String)
|
78
|
+
stream = File.open(stream)
|
79
|
+
end
|
80
|
+
raise ArgumentError.new("'stream' should have a gets method") unless
|
81
|
+
stream.respond_to? :gets
|
82
|
+
|
83
|
+
# we take default options and override them with opts
|
84
|
+
o = FANCY_READ_DEFAULTS.merge(opts)
|
85
|
+
|
86
|
+
# strip off the first lines.
|
87
|
+
while o["skip_first"] > 0
|
88
|
+
stream.gets
|
89
|
+
o["skip_first"] -= 1
|
90
|
+
end
|
91
|
+
|
92
|
+
# then, parsing the lines. We store the results in an array. We read up
|
93
|
+
# all columns, regardless of what is asked (it doesn't slow that much
|
94
|
+
# the process -- or does it ?)
|
95
|
+
|
96
|
+
columns = []
|
97
|
+
line_number = 0 # the number of the significant lines read so far
|
98
|
+
|
99
|
+
while line = stream.gets
|
100
|
+
next if line =~ o["comments"]
|
101
|
+
next if line =~ /^\s*$/ # skip empty lines
|
102
|
+
if o["remove_space"]
|
103
|
+
line.gsub!(/^\s+/,'')
|
104
|
+
end
|
105
|
+
|
106
|
+
elements = line.split(o["sep"])
|
107
|
+
# now, the fun: the actual reading.
|
108
|
+
# we first turn this elements into floats:
|
109
|
+
numbers = elements.collect do |s|
|
110
|
+
begin
|
111
|
+
a = Float(s)
|
112
|
+
rescue
|
113
|
+
o["default"]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
if numbers.size < columns.size
|
118
|
+
# we pad it with default values
|
119
|
+
while numbers.size < columns.size
|
120
|
+
numbers << o["default"]
|
121
|
+
end
|
122
|
+
else
|
123
|
+
# in that case, we need to create new Dvectors to match the
|
124
|
+
# size of numbers
|
125
|
+
while columns.size < numbers.size
|
126
|
+
columns << Dvector.new(line_number, o["default"])
|
127
|
+
end
|
128
|
+
end
|
129
|
+
# now, we should have the same number of elements both
|
130
|
+
# in numbers and in columns
|
131
|
+
columns.size.times do |i|
|
132
|
+
columns[i] << numbers[i]
|
133
|
+
end
|
134
|
+
# and it's done ;-) !
|
135
|
+
|
136
|
+
line_number += 1
|
137
|
+
end
|
138
|
+
# Adding the index columns if necessary
|
139
|
+
if o["index_col"]
|
140
|
+
columns.unshift(Dvector.new(columns[0].length) { |i| i})
|
141
|
+
end
|
142
|
+
|
143
|
+
return columns unless cols
|
144
|
+
return cols.collect { |i|
|
145
|
+
columns[i]
|
146
|
+
}
|
147
|
+
end
|
148
|
+
|
149
|
+
# This function is a rudimentary formula computing stuff. Give it
|
150
|
+
# a text _formula_ and an array of Dvectors (_a_), and it returns a
|
151
|
+
# Dvector with the result. The formula should contain the following;
|
152
|
+
# column[n]:: represents the current element of the n th
|
153
|
+
# Dvector of the array
|
154
|
+
#
|
155
|
+
# This is just a try, and should be implemented in C rather than in
|
156
|
+
# Ruby. But if you're looking for simplicity, here you go ;-) !
|
157
|
+
#
|
158
|
+
# _modules_ are the modules you would wish the evaluator to +include+.
|
159
|
+
# This feature enables one to make sure custom functions are included
|
160
|
+
|
161
|
+
def Dvector.compute_formula(formula, a, modules = []) # :doc:
|
162
|
+
|
163
|
+
evaluator = MathEvaluator.new(formula, "column", modules)
|
164
|
+
# if we reach this place, it means that there a no big syntax errors ;-)
|
165
|
+
|
166
|
+
# we now need to inspect the array given, and make sure that there is
|
167
|
+
# and transform it into a clean stuff (an array with only Dvectors and
|
168
|
+
# nil elements).
|
169
|
+
|
170
|
+
target = []
|
171
|
+
last = nil
|
172
|
+
a.each { |elem|
|
173
|
+
if elem.is_a? Dvector
|
174
|
+
target << elem
|
175
|
+
last = elem
|
176
|
+
else
|
177
|
+
target << nil
|
178
|
+
end
|
179
|
+
}
|
180
|
+
|
181
|
+
raise "No Dvector found" unless last
|
182
|
+
|
183
|
+
# we check all the vectors have the same length
|
184
|
+
target.each {|x|
|
185
|
+
if x && x.length != last.length
|
186
|
+
raise "Dvectors should have all the same length !"
|
187
|
+
end
|
188
|
+
}
|
189
|
+
|
190
|
+
res = Dvector.new
|
191
|
+
|
192
|
+
last.each_index { |i|
|
193
|
+
args = target.collect { |val|
|
194
|
+
if val
|
195
|
+
val[i]
|
196
|
+
else
|
197
|
+
nil
|
198
|
+
end
|
199
|
+
}
|
200
|
+
# we add the index at the beginning:
|
201
|
+
# args.unshift(i)
|
202
|
+
# Commented out for simplicity
|
203
|
+
|
204
|
+
# then we call the block:
|
205
|
+
elem = evaluator.compute(args)
|
206
|
+
res << elem
|
207
|
+
}
|
208
|
+
|
209
|
+
return res
|
210
|
+
end
|
211
|
+
|
212
|
+
WRITE_DEFAULTS = {
|
213
|
+
'sep' => "\t",
|
214
|
+
'write_mode' => "a",
|
215
|
+
}
|
216
|
+
|
217
|
+
# Writes an array of Dvectors into a text _file_
|
218
|
+
def Dvector.write(file, cols, options = {})
|
219
|
+
ops = WRITE_DEFAULTS.update(options)
|
220
|
+
if file.is_a?(String)
|
221
|
+
file = File.open(file, ops["write_mode"])
|
222
|
+
end
|
223
|
+
nb = cols.map {|d| d.size}.max # The number of lines
|
224
|
+
nb.times do |i|
|
225
|
+
file.puts(cols.map {|d| d[i].to_s }.join(ops["sep"]))
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
# Modified by Vincent Fourmond to have a nice Dvector module
|
233
|
+
# instead of the Tioga module.
|
234
|
+
# add arithmetic operators for Float and Fixnum with Dvec
|
235
|
+
|
236
|
+
class Array # :nodoc: all
|
237
|
+
def to_dvector
|
238
|
+
Dobjects::Dvector[*self]
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
class Float # :nodoc: all
|
243
|
+
alias :pre_dvec_add :+
|
244
|
+
def +(a)
|
245
|
+
if a.class == Dobjects::Dvector
|
246
|
+
a+self
|
247
|
+
else
|
248
|
+
pre_dvec_add(a)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
alias :pre_dvec_sub :-
|
252
|
+
def -(a)
|
253
|
+
if a.class == Dobjects::Dvector
|
254
|
+
a.neg+self
|
255
|
+
else
|
256
|
+
pre_dvec_sub(a)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
alias :pre_dvec_mult :*
|
260
|
+
def *(a)
|
261
|
+
if a.class == Dobjects::Dvector
|
262
|
+
a*self
|
263
|
+
else
|
264
|
+
pre_dvec_mult(a)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
alias :pre_dvec_div :/
|
268
|
+
def /(a)
|
269
|
+
if a.class == Dobjects::Dvector
|
270
|
+
a.inv*self
|
271
|
+
else
|
272
|
+
pre_dvec_div(a)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
alias :pre_dvec_pow :**
|
276
|
+
def **(a)
|
277
|
+
if a.class == Dobjects::Dvector
|
278
|
+
a.as_exponent_of(self)
|
279
|
+
else
|
280
|
+
pre_dvec_pow(a)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
|
286
|
+
class Fixnum # :nodoc: all
|
287
|
+
alias :pre_dvec_add :+
|
288
|
+
def +(a)
|
289
|
+
if a.class == Dobjects::Dvector
|
290
|
+
a+self
|
291
|
+
else
|
292
|
+
pre_dvec_add(a)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
alias :pre_dvec_sub :-
|
296
|
+
def -(a)
|
297
|
+
if a.class == Dobjects::Dvector
|
298
|
+
a.neg+self
|
299
|
+
else
|
300
|
+
pre_dvec_sub(a)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
alias :pre_dvec_mult :*
|
304
|
+
def *(a)
|
305
|
+
if a.class == Dobjects::Dvector
|
306
|
+
a*self
|
307
|
+
else
|
308
|
+
pre_dvec_mult(a)
|
309
|
+
end
|
310
|
+
end
|
311
|
+
alias :pre_dvec_div :/
|
312
|
+
def /(a)
|
313
|
+
if a.class == Dobjects::Dvector
|
314
|
+
a.inv*self
|
315
|
+
else
|
316
|
+
pre_dvec_div(a)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
alias :pre_dvec_pow :**
|
320
|
+
def **(a)
|
321
|
+
if a.class == Dobjects::Dvector
|
322
|
+
a.as_exponent_of(self)
|
323
|
+
else
|
324
|
+
pre_dvec_pow(a)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|