gsl4r 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/INSTALL +22 -0
- data/LICENSE +19 -0
- data/LICENSE.LGPLv3 +165 -0
- data/README +55 -0
- data/Rakefile +97 -0
- data/TODO +1 -0
- data/lib/gsl4r.rb +18 -0
- data/lib/gsl4r/complex.rb +434 -0
- data/lib/gsl4r/f +174 -0
- data/lib/gsl4r/harness.rb +58 -0
- data/lib/gsl4r/platform.rb +8 -0
- data/lib/gsl4r/util.rb +85 -0
- data/lib/gsl4r/vector.rb +29 -0
- data/test/complex_test.rb +490 -0
- data/test/complex_tests.rb +28 -0
- data/test/gsl_complex_tests_gen +0 -0
- data/test/gsl_complex_tests_gen.c +791 -0
- metadata +73 -0
data/INSTALL
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
== Install
|
2
|
+
|
3
|
+
Options to install,
|
4
|
+
|
5
|
+
|
6
|
+
local gem file using rake:
|
7
|
+
|
8
|
+
rake install
|
9
|
+
|
10
|
+
System wide gem install using rake
|
11
|
+
|
12
|
+
sudo rake install
|
13
|
+
|
14
|
+
|
15
|
+
You must also update your environment so that GSL4r can find the gsl shared library, under Mac OS X, and using macports, this is something along the lines of:
|
16
|
+
|
17
|
+
export LD_LIBRARY_PATH=/opt/local/lib:$LD_LIBRARY_PATH
|
18
|
+
|
19
|
+
Under linux, it might look something like,
|
20
|
+
|
21
|
+
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
|
22
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
I am an employee of UC Berkeley, which therefore makes me an
|
3
|
+
employee of the State of California.
|
4
|
+
|
5
|
+
By law, all works that I create as part of my job are
|
6
|
+
owned by the University of California Regents. However, the
|
7
|
+
open nature of academic research work allows me leeway to
|
8
|
+
ascribe other open source licenses to the fruits of my labor.
|
9
|
+
|
10
|
+
Therefore, I am releasing this software under the Gnu General
|
11
|
+
Lesser Public License v3
|
12
|
+
|
13
|
+
See the file LICENSE.LGPLv3 for more information.
|
14
|
+
|
15
|
+
04 Feb 2010
|
16
|
+
Colby Gutierrez-Kraybill
|
17
|
+
colby@astro.berkeley.edu
|
18
|
+
|
19
|
+
Copyright (c) 2010 University of California Regents, Gnu LGPLv3
|
data/LICENSE.LGPLv3
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
== Description
|
2
|
+
|
3
|
+
GSL4r
|
4
|
+
Ruby GNU Scientific Library via FFI
|
5
|
+
|
6
|
+
The Foreign Function Interface allows for run-time bindings to shared libraries. Using FFI to interface between Ruby and other libraries written in other languages makes these libraries runnable across all Ruby interpreters that have FFI capabilties (MRI, JRuby, etc...).
|
7
|
+
|
8
|
+
This avoids the complications with implementation specific C extensions or Java Native Interfaces in the case of JRuby.
|
9
|
+
|
10
|
+
== Documentation
|
11
|
+
|
12
|
+
This project is currently in its alpha stages and as of 11 Feb 2010, I have only pushed a library for using Complex functions in GSL.
|
13
|
+
|
14
|
+
== Getting Started
|
15
|
+
|
16
|
+
Right now you can look at the existing project by cloning the GIT repository via:
|
17
|
+
|
18
|
+
git clone git://rubyforge.org/gsl4r.git
|
19
|
+
|
20
|
+
== Installing gsl4r
|
21
|
+
|
22
|
+
There is no installation available at this time.
|
23
|
+
|
24
|
+
== Examples
|
25
|
+
|
26
|
+
cd gsl4r/lib
|
27
|
+
irb -r gsl4r
|
28
|
+
irb(main):001:0> require 'gsl4r/complex'
|
29
|
+
=> true
|
30
|
+
irb(main):002:0> include GSL4r
|
31
|
+
=> Object
|
32
|
+
irb(main):004:0> a=GSL_Complex.create(1,1)
|
33
|
+
=> (1.0,1.0)
|
34
|
+
irb(main):005:0> b=GSL_Complex.create(2,2)
|
35
|
+
irb(main):009:0> a.abs
|
36
|
+
=> 1.4142135623731
|
37
|
+
irb(main):012:0> b.add(a)
|
38
|
+
=> (3.0,3.0)
|
39
|
+
irb(main):013:0> a.logabs
|
40
|
+
=> 0.346573590279973
|
41
|
+
irb(main):014:0> a.gsl_complex_logabs
|
42
|
+
=> 0.346573590279973
|
43
|
+
irb(main):016:0> include GSL4r::Complex::Methods
|
44
|
+
=> Object
|
45
|
+
irb(main):015:0> gsl_complex_add(a,b)
|
46
|
+
=> (3.0,3.0)
|
47
|
+
|
48
|
+
== Questions and/or Comments
|
49
|
+
|
50
|
+
Feel free to email {Colby Gutierrez-Kraybill}[mailto:colby@astro.berkeley.edu] with any questions.
|
51
|
+
|
52
|
+
== Copyright
|
53
|
+
|
54
|
+
See LICENSE and LICENSE.LPGLv3
|
55
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
|
2
|
+
# $Id$
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
require 'rubygems/specification'
|
7
|
+
require 'date'
|
8
|
+
require 'fileutils'
|
9
|
+
require 'test/unit'
|
10
|
+
|
11
|
+
GEM = "gsl4r"
|
12
|
+
GEM_VERSION = File.open("VERSION").gets.chomp
|
13
|
+
AUTHOR = "Colby Gutierrez-Kraybill"
|
14
|
+
EMAIL = "colby@astro.berkeley.edu"
|
15
|
+
HOMEPAGE = %q{http://gsl4r.rubyforge.org}
|
16
|
+
SUMMARY = "GSL4r, ruby FFI wrappers around GNU Scientific Library"
|
17
|
+
DESCRIPTION = "Wrappers around the GNU Scientific Library using Foreign Function Interface. This allows all ruby implementations that support FFI to interface to the C based GSL routines."
|
18
|
+
DIRS = [ "lib/**/*", "test/**/*" ]
|
19
|
+
|
20
|
+
spec = Gem::Specification.new do |s|
|
21
|
+
s.name = GEM
|
22
|
+
s.rubyforge_project = s.name
|
23
|
+
s.version = GEM_VERSION
|
24
|
+
s.platform = Gem::Platform::RUBY
|
25
|
+
s.has_rdoc = true
|
26
|
+
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
27
|
+
s.summary = SUMMARY
|
28
|
+
s.description = DESCRIPTION
|
29
|
+
s.author = AUTHOR
|
30
|
+
s.email = EMAIL
|
31
|
+
s.homepage = HOMEPAGE
|
32
|
+
|
33
|
+
s.requirements << 'GNU Scientific Library, version 1.13 or greater'
|
34
|
+
s.require_path = 'lib'
|
35
|
+
s.files = %w{LICENSE LICENSE.LGPLv3 README INSTALL Rakefile TODO}
|
36
|
+
s.files = s.files + DIRS.collect do |dir|
|
37
|
+
Dir.glob( dir )
|
38
|
+
end.flatten.delete_if { |f| f.include?(".git") }
|
39
|
+
end
|
40
|
+
|
41
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "install the gem locally"
|
45
|
+
task :install => [:package] do
|
46
|
+
sh %{gem install pkg/#{GEM}-#{GEM_VERSION}}
|
47
|
+
end
|
48
|
+
|
49
|
+
desc "create a gemspec file"
|
50
|
+
task :make_spec do
|
51
|
+
File.open("#{GEM}.gemspec", "w") do |file|
|
52
|
+
file.puts spec.to_ruby
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
task :test => [:gsl_config] do
|
57
|
+
$: << 'lib'
|
58
|
+
|
59
|
+
require 'gsl4r'
|
60
|
+
require 'gsl4r/complex'
|
61
|
+
|
62
|
+
complextests = GSL4r::Complex::Harness.new
|
63
|
+
complextests.write_c_tests
|
64
|
+
complextests.compile_c_tests
|
65
|
+
complextests.run_c_tests "complex_test.rb"
|
66
|
+
|
67
|
+
runner = Test::Unit::AutoRunner.new(true)
|
68
|
+
runner.to_run << 'test'
|
69
|
+
runner.pattern = [/_test.rb$/]
|
70
|
+
exit if !runner.run
|
71
|
+
end
|
72
|
+
|
73
|
+
task :gsl_config do
|
74
|
+
gsl_cflags = `gsl-config --cflags`.chomp
|
75
|
+
if ( $?.to_i != 0 )
|
76
|
+
raise "Unable to run 'gsl-config', is it in the PATH? It doesn't support --cflags?"
|
77
|
+
end
|
78
|
+
|
79
|
+
gsl_ldflags = `gsl-config --libs`.chomp
|
80
|
+
if ( $?.to_i != 0 )
|
81
|
+
raise "Unable to run 'gsl-config', it doesn't support --libs?"
|
82
|
+
end
|
83
|
+
|
84
|
+
gsl_lib_path = File.join(`gsl-config --prefix`.chomp, "lib")
|
85
|
+
if ( $?.to_i != 0 )
|
86
|
+
raise "Unable to run 'gsl-config', it doesn't support --prefix?"
|
87
|
+
end
|
88
|
+
|
89
|
+
ENV["CFLAGS"] = (ENV.has_key?("CFLAGS") ? ENV["CFLAGS"]+" " : gsl_cflags)
|
90
|
+
ENV["LDFLAGS"] = (ENV.has_key?("LDFLAGS") ? ENV["LDFLAGS"]+" " : gsl_ldflags)
|
91
|
+
ENV['LD_LIBRARY_PATH'] = (ENV.has_key?('LD_LIBRARY_PATH') ?
|
92
|
+
ENV['LD_LIBRARY_PATH']+":"+gsl_lib_path : gsl_lib_path)
|
93
|
+
end
|
94
|
+
|
95
|
+
task :default => [:gsl_config,:package] do
|
96
|
+
|
97
|
+
end
|
data/lib/gsl4r.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# == Other Info
|
4
|
+
#
|
5
|
+
# Author:: Colby Gutierrez-Kraybill
|
6
|
+
# Version:: $Id$
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'ffi'
|
11
|
+
|
12
|
+
module GSL4r
|
13
|
+
Version = '0.0.1';
|
14
|
+
GSL_LIB_PATH = File.join([`gsl-config --prefix`.chomp,
|
15
|
+
"lib","libgsl.#{FFI::Platform::LIBSUFFIX}"])
|
16
|
+
GSLCBLAS_LIB_PATH = File.join([`gsl-config --prefix`.chomp,
|
17
|
+
"lib","libgslcblas.#{FFI::Platform::LIBSUFFIX}"])
|
18
|
+
end
|
@@ -0,0 +1,434 @@
|
|
1
|
+
#
|
2
|
+
# == Other Info
|
3
|
+
#
|
4
|
+
# Author:: Colby Gutierrez-Kraybill
|
5
|
+
# Version:: $Id$
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'monitor'
|
10
|
+
require 'ffi'
|
11
|
+
|
12
|
+
require 'gsl4r/util'
|
13
|
+
require 'gsl4r/harness'
|
14
|
+
|
15
|
+
module GSL4r
|
16
|
+
module Complex
|
17
|
+
extend ::FFI::Library
|
18
|
+
# Note, according to
|
19
|
+
# gnu.org/software/gsl/manual/html_node/Representation-of-complex-numbers.html
|
20
|
+
# the layout of the complex numbers struct is supposed to be platform
|
21
|
+
# dependent and opaque to applications using GSL. It also provides
|
22
|
+
# C Macros to help with this opaqueness. Unfortunately, we don't have
|
23
|
+
# the luxury of using the macros, nor making the struct mapping opaque.
|
24
|
+
# Happily, in practice, on Linux 32 and 64bit, OSX 32 and 64 bit, and
|
25
|
+
# Solaris 64 bit, the struct appears to be identical.
|
26
|
+
# TODO: Need to check other platforms...
|
27
|
+
# TODO: Perhaps integrate use of FFI::Generator to auto-gen the layout
|
28
|
+
# for structs, based on the platform this package is installed onto?
|
29
|
+
#
|
30
|
+
# Note, long double is not implemented, as its size is dependent on
|
31
|
+
# the platform. GCC on intel turns long double into the native
|
32
|
+
# 80-bit float of the x86 architecture. Microsoft VC aliases it
|
33
|
+
# back to double. It would be nice if we could guarantee that it
|
34
|
+
# was a 128 quadruple precision value, but... no.
|
35
|
+
|
36
|
+
class GSL_Complex < ::FFI::Struct
|
37
|
+
layout :dat, [:double, 2]
|
38
|
+
|
39
|
+
$globalGSLComplexLock = Monitor.new
|
40
|
+
|
41
|
+
EPSILON = 5.0e-15
|
42
|
+
R = 0
|
43
|
+
I = 1
|
44
|
+
|
45
|
+
#def initialize( a )
|
46
|
+
#super()
|
47
|
+
#self[:dat][R] = a[:dat][R]
|
48
|
+
#self[:dat][I] = a[:dat][I]
|
49
|
+
#end
|
50
|
+
#def initialize( r, i )
|
51
|
+
#super()
|
52
|
+
#self[:dat][R] = r
|
53
|
+
#self[:dat][I] = i
|
54
|
+
#end
|
55
|
+
# Create a factory method, as the initialize functions have some
|
56
|
+
# issues when copies are created, probably in by_value specifically
|
57
|
+
# TODO: followup with Wayne Meissner to confirm this
|
58
|
+
class << self
|
59
|
+
#
|
60
|
+
def create( r=0.0, i=0.0 )
|
61
|
+
myComplex = GSL_Complex.new
|
62
|
+
myComplex.set( r, i )
|
63
|
+
end
|
64
|
+
|
65
|
+
# the r_ and c_ methods are designed to help automate the building
|
66
|
+
# of tests and not for use in general
|
67
|
+
def r_type()
|
68
|
+
"GSL_Complex"
|
69
|
+
end
|
70
|
+
|
71
|
+
def r_equals(v1,v2)
|
72
|
+
"#{v1.to_s}.equals(#{v2.to_s})"
|
73
|
+
end
|
74
|
+
|
75
|
+
def r_assignment( name )
|
76
|
+
"#{name}.set(2.0,2.0)" # these numbers should make c_assignment for the test
|
77
|
+
end
|
78
|
+
|
79
|
+
def c_to_r_assignment(v1,v2)
|
80
|
+
"printf(\\\" #{v1}.set(%.15g,%.15g)\\\\n\\\",GSL_REAL(#{v2}),GSL_IMAG(#{v2}));\\n"
|
81
|
+
end
|
82
|
+
|
83
|
+
def c_type()
|
84
|
+
"gsl_complex"
|
85
|
+
end
|
86
|
+
|
87
|
+
def c_assignment( name )
|
88
|
+
"GSL_SET_COMPLEX(&#{name}, 2.0, 2.0);"
|
89
|
+
end
|
90
|
+
|
91
|
+
def c_equals()
|
92
|
+
end
|
93
|
+
|
94
|
+
def c_value_initializer( name )
|
95
|
+
return "gsl_complex #{name}; " + c_assignment( name )
|
96
|
+
end
|
97
|
+
|
98
|
+
def c_pointer_initializer( name )
|
99
|
+
return ""
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Play nice and have these methods show up in case someone
|
104
|
+
# is digging around for them using these reflection routines
|
105
|
+
# Note: this won't show the shortened named forms that
|
106
|
+
# will automatically be generated when called.
|
107
|
+
def methods
|
108
|
+
a = super
|
109
|
+
a + ::GSL4r::Complex::Methods.methods.grep(/^gsl_complex_/)
|
110
|
+
end
|
111
|
+
|
112
|
+
def public_methods
|
113
|
+
a = super
|
114
|
+
a + ::GSL4r::Complex::Methods.methods.grep(/^gsl_complex_/)
|
115
|
+
end
|
116
|
+
|
117
|
+
def real()
|
118
|
+
return self[:dat][R]
|
119
|
+
end
|
120
|
+
|
121
|
+
def imag()
|
122
|
+
return self[:dat][I]
|
123
|
+
end
|
124
|
+
|
125
|
+
def equals( a )
|
126
|
+
return ( (a[:dat][R] - self[:dat][R]).abs < EPSILON &&
|
127
|
+
(a[:dat][I] - self[:dat][I]).abs < EPSILON )
|
128
|
+
end
|
129
|
+
|
130
|
+
def set( r, i )
|
131
|
+
self[:dat][R] = r
|
132
|
+
self[:dat][I] = i
|
133
|
+
return self
|
134
|
+
end
|
135
|
+
|
136
|
+
def set_real( r )
|
137
|
+
self[:dat][R] = r
|
138
|
+
end
|
139
|
+
|
140
|
+
def set_imag( i )
|
141
|
+
self[:dat][I] = i
|
142
|
+
end
|
143
|
+
|
144
|
+
def to_s()
|
145
|
+
return "(#{self[:dat][R]},#{self[:dat][I]})"
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
# This traps method calls intended to create shortened versions
|
150
|
+
# of the GSL function calls.
|
151
|
+
#
|
152
|
+
# This first checks if the called method matches the Module
|
153
|
+
# function call gsl_complex_#{called_method} (where called_method
|
154
|
+
# might be 'abs').
|
155
|
+
#
|
156
|
+
# If it finds a match (respond_to), it will then create a new
|
157
|
+
# method for the class as a whole (class_eval), making the method
|
158
|
+
# available to not just this instance of the class, but all
|
159
|
+
# existing instances and all those created after.
|
160
|
+
#
|
161
|
+
# Finally, the creation is wrapped up in a synchronized call
|
162
|
+
# to ensure thread safety. It is only unsafe the first time
|
163
|
+
# the method is invoked (and non-existent at that point). Every
|
164
|
+
# time the method is invoked after, it should not hit method_missing.
|
165
|
+
# TODO: Is this true for java threads too, or is it per 'vm' per
|
166
|
+
# thread?
|
167
|
+
def method_missing( called_method, *args, &block )
|
168
|
+
|
169
|
+
$globalGSLComplexLock.synchronize do
|
170
|
+
|
171
|
+
prefix = "gsl_complex_"
|
172
|
+
|
173
|
+
if ( ::GSL4r::Complex::Methods.respond_to?("#{prefix}#{called_method}") == false )
|
174
|
+
prefix = ""
|
175
|
+
if ( ::GSL4r::Complex::Methods.respond_to?("#{called_method}") == false )
|
176
|
+
super # NoMethodError
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
self.class.class_eval <<-end_eval
|
181
|
+
def #{called_method}(*args, &block)
|
182
|
+
args.insert(0, self)
|
183
|
+
::GSL4r::Complex::Methods::#{prefix}#{called_method}( *args, &block )
|
184
|
+
end
|
185
|
+
end_eval
|
186
|
+
|
187
|
+
__send__(called_method, *args, &block)
|
188
|
+
end # globalGSLComplexLock.synchronize
|
189
|
+
end # method_missing
|
190
|
+
end # GSL_Complex
|
191
|
+
|
192
|
+
|
193
|
+
class Harness
|
194
|
+
include ::GSL4r::Harness
|
195
|
+
|
196
|
+
def initialize
|
197
|
+
@c_compiler = "gcc"
|
198
|
+
@c_src_name = "gsl_complex_tests_gen.c"
|
199
|
+
@c_binary = "gsl_complex_tests_gen"
|
200
|
+
@c_includes = ["gsl/gsl_complex.h","gsl/gsl_complex_math.h"]
|
201
|
+
@c_flags = [`gsl-config --libs`.chomp,`gsl-config --cflags`.chomp]
|
202
|
+
@c_tests = ::GSL4r::Complex::Methods.methods.grep(/^c_test/)
|
203
|
+
@r_header = %Q{$: << File.join('..','lib')\\nrequire 'test/unit'\\nrequire 'test/unit/autorunner'\\nrequire 'gsl4r/complex'\\ninclude GSL4r::Complex\\nclass ComplexTests < Test::Unit::TestCase\\n EPSILON = 5.0e-15}
|
204
|
+
|
205
|
+
@r_footer = %Q{end}
|
206
|
+
|
207
|
+
end # Complex::Harness
|
208
|
+
end
|
209
|
+
|
210
|
+
class GSL_Complex_float < GSL_Complex
|
211
|
+
layout :dat, [:float, 2]
|
212
|
+
end
|
213
|
+
|
214
|
+
# Make namespaces cleaner by putting the attached functions in their
|
215
|
+
# own sub-name space
|
216
|
+
module Methods
|
217
|
+
|
218
|
+
# provides attach_gsl_function
|
219
|
+
extend ::GSL4r::Util
|
220
|
+
|
221
|
+
extend ::FFI::Library
|
222
|
+
|
223
|
+
ffi_lib GSL4r::GSL_LIB_PATH
|
224
|
+
|
225
|
+
# Returns the argument of the complex number z, arg(z), where -pi < arg(z) <= pi
|
226
|
+
attach_gsl_function :gsl_complex_arg, [ GSL_Complex.by_value ], :double,
|
227
|
+
[GSL_Complex], :double
|
228
|
+
|
229
|
+
# Returns the magnitude of the complex number z, |z|.
|
230
|
+
attach_gsl_function :gsl_complex_abs, [ GSL_Complex.by_value ], :double,
|
231
|
+
[GSL_Complex], :double
|
232
|
+
|
233
|
+
# Returns the squared magnitude of the complex number z, |z|^2
|
234
|
+
attach_gsl_function :gsl_complex_abs2, [ GSL_Complex.by_value ], :double,
|
235
|
+
[GSL_Complex], :double
|
236
|
+
|
237
|
+
# Returns the
|
238
|
+
# the natural logarithm of the magnitude of the complex number z, log|z|.
|
239
|
+
# It allows an accurate evaluation of log|z| when |z| is close to one.
|
240
|
+
# The direct evaluation of log(gsl_complex_abs(z)) would lead to a loss
|
241
|
+
# of precision in this case
|
242
|
+
attach_gsl_function :gsl_complex_logabs, [ GSL_Complex.by_value ], :double
|
243
|
+
|
244
|
+
# 5.3 Arithmetic operators
|
245
|
+
|
246
|
+
# Returns the sum of the complex numbers a and b, z=a+b
|
247
|
+
attach_gsl_function :gsl_complex_add, Array.new(2, GSL_Complex.by_value),
|
248
|
+
GSL_Complex.by_value, Array.new(2, GSL_Complex), GSL_Complex
|
249
|
+
|
250
|
+
# Returns the difference of the complex numbers a and b, z=a-b
|
251
|
+
attach_gsl_function :gsl_complex_sub, Array.new(2, GSL_Complex.by_value),
|
252
|
+
GSL_Complex.by_value, Array.new(2, GSL_Complex), GSL_Complex
|
253
|
+
|
254
|
+
# Returns the product of the complex numbers a and b, z=ab
|
255
|
+
attach_gsl_function :gsl_complex_mul, Array.new(2, GSL_Complex.by_value),
|
256
|
+
GSL_Complex.by_value, Array.new(2, GSL_Complex), GSL_Complex
|
257
|
+
|
258
|
+
# Returns the quotient of the complex numbers a and b, z=a/b
|
259
|
+
attach_gsl_function :gsl_complex_div, Array.new(2, GSL_Complex.by_value),
|
260
|
+
GSL_Complex.by_value, Array.new(2, GSL_Complex), GSL_Complex
|
261
|
+
|
262
|
+
# Returns the sum of the complex number a and the real number x, z=a+x
|
263
|
+
attach_gsl_function :gsl_complex_add_real, [ GSL_Complex.by_value, :double ],
|
264
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
265
|
+
|
266
|
+
# Returns the difference of the complex number a and the real number x, z=a-x
|
267
|
+
attach_gsl_function :gsl_complex_sub_real, [ GSL_Complex.by_value, :double ],
|
268
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
269
|
+
|
270
|
+
# Returns the product of the complex number a and the real number x, z=ax
|
271
|
+
attach_gsl_function :gsl_complex_mul_real, [ GSL_Complex.by_value, :double ],
|
272
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
273
|
+
|
274
|
+
# Returns the quotient of the complex number a and the real number x, z=a/x
|
275
|
+
attach_gsl_function :gsl_complex_div_real, [ GSL_Complex.by_value, :double ],
|
276
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
277
|
+
|
278
|
+
# Returns the sum of the complex number a and the imaginary number iy, z=a+iy
|
279
|
+
attach_gsl_function :gsl_complex_add_imag, [ GSL_Complex.by_value, :double ],
|
280
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
281
|
+
|
282
|
+
# Returns the difference of the complex number a and the imaginary number iy, z=a-iy
|
283
|
+
attach_gsl_function :gsl_complex_sub_imag, [ GSL_Complex.by_value, :double ],
|
284
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
285
|
+
|
286
|
+
# Returns the product of the complex number a and the imaginary number iy, z=a*iy
|
287
|
+
attach_gsl_function :gsl_complex_mul_imag, [ GSL_Complex.by_value, :double ],
|
288
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
289
|
+
|
290
|
+
# Returns the quotient of the complex number a and the imaginary number iy, z=a/iy
|
291
|
+
attach_gsl_function :gsl_complex_div_imag, [ GSL_Complex.by_value, :double ],
|
292
|
+
GSL_Complex.by_value, [ GSL_Complex, :double], GSL_Complex
|
293
|
+
|
294
|
+
# Returns the complex conjugate of the complex number z, z^* = x-iy
|
295
|
+
attach_gsl_function :gsl_complex_conjugate, [ GSL_Complex.by_value ],
|
296
|
+
GSL_Complex.by_value, GSL_Complex, GSL_Complex
|
297
|
+
|
298
|
+
# Returns the inverse, or reciprocal, of the complex number z, 1/z = (x-iy)/(x^2 + y^2)
|
299
|
+
attach_gsl_function :gsl_complex_inverse, [ GSL_Complex.by_value ],
|
300
|
+
GSL_Complex.by_value, GSL_Complex, GSL_Complex
|
301
|
+
|
302
|
+
# Returns the negative of the complex number z, -z = (-x) + i(-y)
|
303
|
+
attach_gsl_function :gsl_complex_negative, [ GSL_Complex.by_value ],
|
304
|
+
GSL_Complex.by_value, GSL_Complex, GSL_Complex
|
305
|
+
|
306
|
+
# 5.4 Elementary Complex Functions
|
307
|
+
|
308
|
+
# This function returns the square root of the complex number z, √z.
|
309
|
+
# The branch cut is the negative real axis. The result always lies
|
310
|
+
# in the right half of the complex plane.
|
311
|
+
attach_gsl_function :gsl_complex_sqrt, [ GSL_Complex.by_value ],
|
312
|
+
GSL_Complex.by_value, GSL_Complex, GSL_Complex
|
313
|
+
|
314
|
+
|
315
|
+
# This function returns the complex square root of the real number x,
|
316
|
+
# where x may be negative.
|
317
|
+
attach_gsl_function :gsl_complex_sqrt_real, [ :double ],
|
318
|
+
GSL_Complex.by_value, :double, GSL_Complex
|
319
|
+
|
320
|
+
|
321
|
+
# r= r e^(i theta)
|
322
|
+
attach_gsl_function :gsl_complex_polar, [ :double, :double ],
|
323
|
+
GSL_Complex.by_value, [ :double, :double ], GSL_Complex
|
324
|
+
# r=a^b
|
325
|
+
attach_gsl_function :gsl_complex_pow, [ GSL_Complex.by_value, GSL_Complex.by_value ],
|
326
|
+
GSL_Complex.by_value, [ GSL_Complex, GSL_Complex ], GSL_Complex
|
327
|
+
# r=a^b
|
328
|
+
attach_gsl_function :gsl_complex_pow_real, [ GSL_Complex.by_value, :double ],
|
329
|
+
GSL_Complex.by_value, [ GSL_Complex, :double ], GSL_Complex
|
330
|
+
# r=exp(a)
|
331
|
+
attach_gsl_function :gsl_complex_exp, [ GSL_Complex.by_value ],
|
332
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
333
|
+
# r=log(a) (base e)
|
334
|
+
attach_gsl_function :gsl_complex_log, [ GSL_Complex.by_value ],
|
335
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
336
|
+
# r=log10(a) (base 10)
|
337
|
+
attach_gsl_function :gsl_complex_log10, [ GSL_Complex.by_value ],
|
338
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
339
|
+
# r=log_b(a) (base=b)
|
340
|
+
attach_gsl_function :gsl_complex_log_b, [ GSL_Complex.by_value, GSL_Complex.by_value ],
|
341
|
+
GSL_Complex.by_value, [ GSL_Complex, GSL_Complex ], GSL_Complex
|
342
|
+
# r=sin(a)
|
343
|
+
attach_gsl_function :gsl_complex_sin, [ GSL_Complex.by_value ],
|
344
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
345
|
+
# r=cos(a)
|
346
|
+
attach_gsl_function :gsl_complex_cos, [ GSL_Complex.by_value ],
|
347
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
348
|
+
# r=sec(a)
|
349
|
+
attach_gsl_function :gsl_complex_sec, [ GSL_Complex.by_value ],
|
350
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
351
|
+
# r=csc(a)
|
352
|
+
attach_gsl_function :gsl_complex_csc, [ GSL_Complex.by_value ],
|
353
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
354
|
+
# r=tan(a)
|
355
|
+
attach_gsl_function :gsl_complex_tan, [ GSL_Complex.by_value ],
|
356
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
357
|
+
# r=cot(a)
|
358
|
+
attach_gsl_function :gsl_complex_cot, [ GSL_Complex.by_value ],
|
359
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
360
|
+
# r=arcsin(a)
|
361
|
+
attach_gsl_function :gsl_complex_arcsin, [ GSL_Complex.by_value ],
|
362
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
363
|
+
# r=arcsin(a)
|
364
|
+
attach_gsl_function :gsl_complex_arcsin_real, [ :double ],
|
365
|
+
GSL_Complex.by_value, [ :double ], GSL_Complex
|
366
|
+
# r=arccos(a)
|
367
|
+
attach_gsl_function :gsl_complex_arccos, [ GSL_Complex.by_value ],
|
368
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
369
|
+
# r=arccos(a)
|
370
|
+
attach_gsl_function :gsl_complex_arccos_real, [ :double ],
|
371
|
+
GSL_Complex.by_value, [ :double ], GSL_Complex
|
372
|
+
# r=arcsec(a)
|
373
|
+
attach_gsl_function :gsl_complex_arcsec, [ GSL_Complex.by_value ],
|
374
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
375
|
+
# r=arcsec(a)
|
376
|
+
attach_gsl_function :gsl_complex_arcsec_real, [ :double ],
|
377
|
+
GSL_Complex.by_value, [ :double ], GSL_Complex
|
378
|
+
# r=arccsc(a)
|
379
|
+
attach_gsl_function :gsl_complex_arccsc, [ GSL_Complex.by_value ],
|
380
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
381
|
+
# r=arccsc(a)
|
382
|
+
attach_gsl_function :gsl_complex_arccsc_real, [ :double ],
|
383
|
+
GSL_Complex.by_value, [ :double ], GSL_Complex
|
384
|
+
# r=arctan(a)
|
385
|
+
attach_gsl_function :gsl_complex_arctan, [ GSL_Complex.by_value ],
|
386
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
387
|
+
# r=arccot(a)
|
388
|
+
attach_gsl_function :gsl_complex_arccot, [ GSL_Complex.by_value ],
|
389
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
390
|
+
# r=sinh(a)
|
391
|
+
attach_gsl_function :gsl_complex_sinh, [ GSL_Complex.by_value ],
|
392
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
393
|
+
# r=coshh(a)
|
394
|
+
attach_gsl_function :gsl_complex_cosh, [ GSL_Complex.by_value ],
|
395
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
396
|
+
# r=sech(a)
|
397
|
+
attach_gsl_function :gsl_complex_sech, [ GSL_Complex.by_value ],
|
398
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
399
|
+
# r=csch(a)
|
400
|
+
attach_gsl_function :gsl_complex_csch, [ GSL_Complex.by_value ],
|
401
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
402
|
+
# r=tanh(a)
|
403
|
+
attach_gsl_function :gsl_complex_tanh, [ GSL_Complex.by_value ],
|
404
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
405
|
+
# r=coth(a)
|
406
|
+
attach_gsl_function :gsl_complex_coth, [ GSL_Complex.by_value ],
|
407
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
408
|
+
# r=arcsinh(a)
|
409
|
+
attach_gsl_function :gsl_complex_arcsinh, [ GSL_Complex.by_value ],
|
410
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
411
|
+
# r=arccosh(a)
|
412
|
+
attach_gsl_function :gsl_complex_arccosh, [ GSL_Complex.by_value ],
|
413
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
414
|
+
# r=arccosh(a)
|
415
|
+
attach_gsl_function :gsl_complex_arccosh_real, [ :double ],
|
416
|
+
GSL_Complex.by_value, [ :double ], GSL_Complex
|
417
|
+
# r=arcsech(a)
|
418
|
+
attach_gsl_function :gsl_complex_arcsech, [ GSL_Complex.by_value ],
|
419
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
420
|
+
# r=arccsch(a)
|
421
|
+
attach_gsl_function :gsl_complex_arccsch, [ GSL_Complex.by_value ],
|
422
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
423
|
+
# r=arctanh(a)
|
424
|
+
attach_gsl_function :gsl_complex_arctanh, [ GSL_Complex.by_value ],
|
425
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
426
|
+
# r=arctanh(a)
|
427
|
+
attach_gsl_function :gsl_complex_arctanh_real, [ :double ],
|
428
|
+
GSL_Complex.by_value, [ :double ], GSL_Complex
|
429
|
+
# r=arccoth(a)
|
430
|
+
attach_gsl_function :gsl_complex_arccoth, [ GSL_Complex.by_value ],
|
431
|
+
GSL_Complex.by_value, [ GSL_Complex ], GSL_Complex
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|