# smath.rb :multi-dimensional math backend # Copyright (C) 2014 Vincent Fourmond # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA require 'Dobjects/Dvector' require 'Dobjects/Function' module CTioga2 module Data module Backends class SMathBackend < Backend include Dobjects include Math describe 'smath', 'Mathematical functions (multi-D)', < 1) set = "u:v:#{set}" end val_u = Dvector.new val_v = Dvector.new for u in u_values for v in v_values val_u << u val_v << v end end return Dataset.dataset_from_spec(name, set) do |b| get_data_column(b, [val_u, val_v]) end end protected # Turns a Range and a number of points into a Dvector def make_dvector(range, nb_points, log = @log) n = nb_points -1 a = Dvector.new(nb_points) { |i| i.to_f/(n.to_f) } # a is in [0:1] inclusive... if log delta = range.last/range.first # delta is positive necessarily a *= delta.log a.exp! a *= range.first else delta = range.last - range.first a *= delta a += range.first end return a end # Uses compute_formula to get data from def get_data_column(column, values) column.gsub!(/\b(u|v)\b/) do cl = if $1 == 'u' 0 else 1 end "(column[#{cl}])" end return Ruby.compute_formula(column, values) end end end end end