1
# math.rb :the Math backend: data based on mathematical formulas.
2
# Copyright (C) 2006 - 2009 Vincent Fourmond
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
require 'Dobjects/Dvector'
20
require 'Dobjects/Function'
24
Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $')
30
class MathBackend < Backend
35
describe 'math', 'Mathematical functions', <<EOD
36
This backend returns computations of mathematical formulas.
39
# \todo make provisions for 3-D datasets. Ideas: x(t):y(t):z(t)
40
# for parametric plots ? (possibly x(t):y1(t):y2(t):...:yn(t)) ?
42
param_accessor :samples, 'samples', "Samples", 'integer',
43
"The number of points"
44
param_accessor :x_range, 'xrange', "X Range", 'float-range',
46
param_accessor :t_range, 'trange', "T Range", 'float-range',
47
"T range (a:b) (parametric plot)"
49
param_accessor :log, 'log', "Logarithmic scale", 'boolean',
50
"Space samples logarithmically"
55
@x_range = -10.0..10.0
56
@t_range = -10.0..10.0
60
# This is called by the architecture to get the data. It first
61
# splits the set name into func@range.
62
def query_dataset(set)
68
if set =~ /:/ # parametric
70
set_param_from_string(:t_range, range)
73
values = make_dvector(@t_range, @samples, @log)
76
set_param_from_string(:x_range, range)
79
values = make_dvector(@x_range, @samples, @log)
82
return Dataset.dataset_from_spec(name, set) do |b|
83
get_data_column(b, varname, values)
90
# Turns a Range and a number of points into a Dvector
91
def make_dvector(range, nb_points, log = @log)
93
a = Dvector.new(nb_points) { |i|
96
# a is in [0:1] inclusive...
98
delta = range.last/range.first
99
# delta is positive necessarily
104
delta = range.last - range.first
111
# Uses compute_formula to get data from
112
def get_data_column(column, variable, values)
113
column.gsub!(/\b#{variable}\b/, "(column[0])")
114
Dvector.compute_formula(column, [values])