1
# bijection.rb: a bijection representing a reversible coordinate transformation
2
# copyright (c) 2009 by 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 (in the COPYING file).
14
require 'ctioga2/utils'
19
Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $')
25
# This class represents a *reversible* arbitrary coordinate
26
# transformation, such as the ones that could be desirable for
27
# alternative axes. Characterized by two Block objects, #from
28
# and #to, that converts respectively from and to the target
32
# A Block converting from the target coordinates
35
# A Block converting to the target coordinates
38
# Creates a new Bijection with the given blocks.
39
def initialize(from, to = nil)
44
# Converts a vector to the target coordinates
46
return vect.map do |x|
51
# Converts a vector from the target coordinates
52
def convert_from(vect)
53
return vect.map do |x|
58
# Creates a Bijection from a text representation.
60
# Takes functions of _x_. Takes two blocks _from_ _to_
61
# separated by :: -- or only one block in the case of an
62
# involution (very common, actually, all 1/x transforms).
64
# \todo few things around here to change... in particular,
65
# I should try to find a way to include Math...
67
# \todo add very common cases ?
68
def self.from_text(spec)
69
blocks = spec.split(/::/).map do |code|
70
eval("proc do |x|\n#{code}\nend")
72
return Bijection.new(*blocks)