~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/data/backends/backends/math.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# math.rb :the Math backend: data based on mathematical formulas.
 
2
# Copyright (C) 2006 - 2009 Vincent Fourmond
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
 
 
18
 
 
19
require 'Dobjects/Dvector'
 
20
require 'Dobjects/Function'
 
21
 
 
22
module CTioga2
 
23
 
 
24
  Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $')
 
25
 
 
26
  module Data
 
27
 
 
28
    module Backends
 
29
 
 
30
      class MathBackend < Backend
 
31
        
 
32
        include Dobjects
 
33
        include Math
 
34
      
 
35
        describe 'math', 'Mathematical functions', <<EOD
 
36
This backend returns computations of mathematical formulas.
 
37
EOD
 
38
 
 
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)) ?
 
41
 
 
42
        param_accessor :samples, 'samples', "Samples", 'integer', 
 
43
        "The number of points"
 
44
        param_accessor :x_range, 'xrange',  "X Range", 'float-range', 
 
45
        "X range (a:b)"
 
46
        param_accessor :t_range, 'trange',  "T Range", 'float-range', 
 
47
        "T range (a:b) (parametric plot)"
 
48
 
 
49
        param_accessor :log, 'log',  "Logarithmic scale", 'boolean', 
 
50
        "Space samples logarithmically"
 
51
        
 
52
        def initialize
 
53
          super()
 
54
          @samples = 100
 
55
          @x_range = -10.0..10.0
 
56
          @t_range = -10.0..10.0
 
57
          @log = false
 
58
        end
 
59
 
 
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)
 
63
          if set =~ /(.*)@(.*)/
 
64
            set = $1
 
65
            range = $2
 
66
          end          
 
67
          name = "math: #{set}"
 
68
          if set =~ /:/         # parametric
 
69
            if range
 
70
              set_param_from_string(:t_range, range)
 
71
            end
 
72
            varname = "t"
 
73
            values = make_dvector(@t_range, @samples, @log)
 
74
          else
 
75
            if range
 
76
              set_param_from_string(:x_range, range)
 
77
            end
 
78
            varname = "x"
 
79
            values = make_dvector(@x_range, @samples, @log)
 
80
            set = "x:#{set}"
 
81
          end
 
82
          return Dataset.dataset_from_spec(name, set) do |b|
 
83
            get_data_column(b, varname, values)
 
84
          end
 
85
        end
 
86
 
 
87
 
 
88
        protected
 
89
        
 
90
        # Turns a Range and a number of points into a Dvector
 
91
        def make_dvector(range, nb_points, log = @log)
 
92
          n = nb_points -1
 
93
          a = Dvector.new(nb_points) { |i| 
 
94
            i.to_f/(n.to_f)
 
95
          }
 
96
          # a is in [0:1] inclusive...
 
97
          if log
 
98
            delta = range.last/range.first
 
99
            # delta is positive necessarily
 
100
            a *= delta.log
 
101
            a.exp!
 
102
            a *= range.first
 
103
          else
 
104
            delta = range.last - range.first
 
105
            a *= delta
 
106
            a += range.first
 
107
          end
 
108
          return a
 
109
        end
 
110
 
 
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])
 
115
        end
 
116
 
 
117
 
 
118
      end
 
119
    end
 
120
  end
 
121
end