gsl4r 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ # Generated by gsl4r/bin/generate_consts.rb
2
+ # from /opt/local/include/gsl/gsl_const_mksa.h
3
+ module GSL4r
4
+ module PhysicalConstants
5
+ module MKSA
6
+ SPEED_OF_LIGHT = (2.99792458e8) # m / s
7
+ GRAVITATIONAL_CONSTANT = (6.673e-11) # m^3 / kg s^2
8
+ PLANCKS_CONSTANT_H = (6.62606896e-34) # kg m^2 / s
9
+ PLANCKS_CONSTANT_HBAR = (1.05457162825e-34) # kg m^2 / s
10
+ ASTRONOMICAL_UNIT = (1.49597870691e11) # m
11
+ LIGHT_YEAR = (9.46053620707e15) # m
12
+ PARSEC = (3.08567758135e16) # m
13
+ GRAV_ACCEL = (9.80665e0) # m / s^2
14
+ ELECTRON_VOLT = (1.602176487e-19) # kg m^2 / s^2
15
+ MASS_ELECTRON = (9.10938188e-31) # kg
16
+ MASS_MUON = (1.88353109e-28) # kg
17
+ MASS_PROTON = (1.67262158e-27) # kg
18
+ MASS_NEUTRON = (1.67492716e-27) # kg
19
+ RYDBERG = (2.17987196968e-18) # kg m^2 / s^2
20
+ BOLTZMANN = (1.3806504e-23) # kg m^2 / K s^2
21
+ MOLAR_GAS = (8.314472e0) # kg m^2 / K mol s^2
22
+ STANDARD_GAS_VOLUME = (2.2710981e-2) # m^3 / mol
23
+ MINUTE = (6e1) # s
24
+ HOUR = (3.6e3) # s
25
+ DAY = (8.64e4) # s
26
+ WEEK = (6.048e5) # s
27
+ INCH = (2.54e-2) # m
28
+ FOOT = (3.048e-1) # m
29
+ YARD = (9.144e-1) # m
30
+ MILE = (1.609344e3) # m
31
+ NAUTICAL_MILE = (1.852e3) # m
32
+ FATHOM = (1.8288e0) # m
33
+ MIL = (2.54e-5) # m
34
+ POINT = (3.52777777778e-4) # m
35
+ TEXPOINT = (3.51459803515e-4) # m
36
+ MICRON = (1e-6) # m
37
+ ANGSTROM = (1e-10) # m
38
+ HECTARE = (1e4) # m^2
39
+ ACRE = (4.04685642241e3) # m^2
40
+ BARN = (1e-28) # m^2
41
+ LITER = (1e-3) # m^3
42
+ US_GALLON = (3.78541178402e-3) # m^3
43
+ QUART = (9.46352946004e-4) # m^3
44
+ PINT = (4.73176473002e-4) # m^3
45
+ CUP = (2.36588236501e-4) # m^3
46
+ FLUID_OUNCE = (2.95735295626e-5) # m^3
47
+ TABLESPOON = (1.47867647813e-5) # m^3
48
+ TEASPOON = (4.92892159375e-6) # m^3
49
+ CANADIAN_GALLON = (4.54609e-3) # m^3
50
+ UK_GALLON = (4.546092e-3) # m^3
51
+ MILES_PER_HOUR = (4.4704e-1) # m / s
52
+ KILOMETERS_PER_HOUR = (2.77777777778e-1) # m / s
53
+ KNOT = (5.14444444444e-1) # m / s
54
+ POUND_MASS = (4.5359237e-1) # kg
55
+ OUNCE_MASS = (2.8349523125e-2) # kg
56
+ TON = (9.0718474e2) # kg
57
+ METRIC_TON = (1e3) # kg
58
+ UK_TON = (1.0160469088e3) # kg
59
+ TROY_OUNCE = (3.1103475e-2) # kg
60
+ CARAT = (2e-4) # kg
61
+ UNIFIED_ATOMIC_MASS = (1.660538782e-27) # kg
62
+ GRAM_FORCE = (9.80665e-3) # kg m / s^2
63
+ POUND_FORCE = (4.44822161526e0) # kg m / s^2
64
+ KILOPOUND_FORCE = (4.44822161526e3) # kg m / s^2
65
+ POUNDAL = (1.38255e-1) # kg m / s^2
66
+ CALORIE = (4.1868e0) # kg m^2 / s^2
67
+ BTU = (1.05505585262e3) # kg m^2 / s^2
68
+ THERM = (1.05506e8) # kg m^2 / s^2
69
+ HORSEPOWER = (7.457e2) # kg m^2 / s^3
70
+ BAR = (1e5) # kg / m s^2
71
+ STD_ATMOSPHERE = (1.01325e5) # kg / m s^2
72
+ TORR = (1.33322368421e2) # kg / m s^2
73
+ METER_OF_MERCURY = (1.33322368421e5) # kg / m s^2
74
+ INCH_OF_MERCURY = (3.38638815789e3) # kg / m s^2
75
+ INCH_OF_WATER = (2.490889e2) # kg / m s^2
76
+ PSI = (6.89475729317e3) # kg / m s^2
77
+ POISE = (1e-1) # kg m^-1 s^-1
78
+ STOKES = (1e-4) # m^2 / s
79
+ STILB = (1e4) # cd / m^2
80
+ LUMEN = (1e0) # cd sr
81
+ LUX = (1e0) # cd sr / m^2
82
+ PHOT = (1e4) # cd sr / m^2
83
+ FOOTCANDLE = (1.076e1) # cd sr / m^2
84
+ LAMBERT = (1e4) # cd sr / m^2
85
+ FOOTLAMBERT = (1.07639104e1) # cd sr / m^2
86
+ CURIE = (3.7e10) # 1 / s
87
+ ROENTGEN = (2.58e-4) # A s / kg
88
+ RAD = (1e-2) # m^2 / s^2
89
+ SOLAR_MASS = (1.98892e30) # kg
90
+ BOHR_RADIUS = (5.291772083e-11) # m
91
+ NEWTON = (1e0) # kg m / s^2
92
+ DYNE = (1e-5) # kg m / s^2
93
+ JOULE = (1e0) # kg m^2 / s^2
94
+ ERG = (1e-7) # kg m^2 / s^2
95
+ STEFAN_BOLTZMANN_CONSTANT = (5.67040047374e-8) # kg / K^4 s^3
96
+ THOMSON_CROSS_SECTION = (6.65245893699e-29) # m^2
97
+ BOHR_MAGNETON = (9.27400899e-24) # A m^2
98
+ NUCLEAR_MAGNETON = (5.05078317e-27) # A m^2
99
+ ELECTRON_MAGNETIC_MOMENT = (9.28476362e-24) # A m^2
100
+ PROTON_MAGNETIC_MOMENT = (1.410606633e-26) # A m^2
101
+ FARADAY = (9.64853429775e4) # A s / mol
102
+ ELECTRON_CHARGE = (1.602176487e-19) # A s
103
+ VACUUM_PERMITTIVITY = (8.854187817e-12) # A^2 s^4 / kg m^3
104
+ VACUUM_PERMEABILITY = (1.25663706144e-6) # kg m / A^2 s^2
105
+ DEBYE = (3.33564095198e-30) # A s^2 / m^2
106
+ GAUSS = (1e-4) # kg / A s^2
107
+ def all_names
108
+ return ["SPEED_OF_LIGHT","GRAVITATIONAL_CONSTANT","PLANCKS_CONSTANT_H","PLANCKS_CONSTANT_HBAR","ASTRONOMICAL_UNIT","LIGHT_YEAR","PARSEC","GRAV_ACCEL","ELECTRON_VOLT","MASS_ELECTRON","MASS_MUON","MASS_PROTON","MASS_NEUTRON","RYDBERG","BOLTZMANN","MOLAR_GAS","STANDARD_GAS_VOLUME","MINUTE","HOUR","DAY","WEEK","INCH","FOOT","YARD","MILE","NAUTICAL_MILE","FATHOM","MIL","POINT","TEXPOINT","MICRON","ANGSTROM","HECTARE","ACRE","BARN","LITER","US_GALLON","QUART","PINT","CUP","FLUID_OUNCE","TABLESPOON","TEASPOON","CANADIAN_GALLON","UK_GALLON","MILES_PER_HOUR","KILOMETERS_PER_HOUR","KNOT","POUND_MASS","OUNCE_MASS","TON","METRIC_TON","UK_TON","TROY_OUNCE","CARAT","UNIFIED_ATOMIC_MASS","GRAM_FORCE","POUND_FORCE","KILOPOUND_FORCE","POUNDAL","CALORIE","BTU","THERM","HORSEPOWER","BAR","STD_ATMOSPHERE","TORR","METER_OF_MERCURY","INCH_OF_MERCURY","INCH_OF_WATER","PSI","POISE","STOKES","STILB","LUMEN","LUX","PHOT","FOOTCANDLE","LAMBERT","FOOTLAMBERT","CURIE","ROENTGEN","RAD","SOLAR_MASS","BOHR_RADIUS","NEWTON","DYNE","JOULE","ERG","STEFAN_BOLTZMANN_CONSTANT","THOMSON_CROSS_SECTION","BOHR_MAGNETON","NUCLEAR_MAGNETON","ELECTRON_MAGNETIC_MOMENT","PROTON_MAGNETIC_MOMENT","FARADAY","ELECTRON_CHARGE","VACUUM_PERMITTIVITY","VACUUM_PERMEABILITY","DEBYE","GAUSS"]
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,29 @@
1
+ # Generated by gsl4r/bin/generate_consts.rb
2
+ # from /opt/local/include/gsl/gsl_const_num.h
3
+ module GSL4r
4
+ module PhysicalConstants
5
+ module NUM
6
+ FINE_STRUCTURE = (7.297352533e-3) # 1
7
+ AVOGADRO = (6.02214199e23) # 1 / mol
8
+ YOTTA = (1e24) # 1
9
+ ZETTA = (1e21) # 1
10
+ EXA = (1e18) # 1
11
+ PETA = (1e15) # 1
12
+ TERA = (1e12) # 1
13
+ GIGA = (1e9) # 1
14
+ MEGA = (1e6) # 1
15
+ KILO = (1e3) # 1
16
+ MILLI = (1e-3) # 1
17
+ MICRO = (1e-6) # 1
18
+ NANO = (1e-9) # 1
19
+ PICO = (1e-12) # 1
20
+ FEMTO = (1e-15) # 1
21
+ ATTO = (1e-18) # 1
22
+ ZEPTO = (1e-21) # 1
23
+ YOCTO = (1e-24) # 1
24
+ def all_names
25
+ return ["FINE_STRUCTURE","AVOGADRO","YOTTA","ZETTA","EXA","PETA","TERA","GIGA","MEGA","KILO","MILLI","MICRO","NANO","PICO","FEMTO","ATTO","ZEPTO","YOCTO"]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,6 +1,7 @@
1
1
  #
2
- # This file is automatically modified at the
3
- # time of gem installation
2
+ # TODO:
3
+ # Have this file automatically modified at the
4
+ # time of gem installation?
4
5
  #
5
6
 
6
7
  module GSL4r
data/lib/gsl4r/util.rb CHANGED
@@ -7,18 +7,26 @@
7
7
  #
8
8
 
9
9
  require 'rubygems'
10
+ require 'monitor'
10
11
 
11
12
  module GSL4r
12
13
  module Util
13
14
 
14
15
  $c_var_num = 0
15
16
 
16
- def attach_gsl_function( method_name, args, return_var, args_type=nil, return_type=nil )
17
+ def attach_gsl_function( method_name, args, return_var, args_type=nil, return_type=nil,
18
+ add_self=true )
17
19
 
18
20
  # This function is attached to the extended ::FFI::Library
19
21
  # module from the calling namespace, e.g. ::GSL4r::Complex::Methods
20
22
  attach_function method_name, args, return_var
21
23
 
24
+ # Give a hint to the current module if this method should add a copy
25
+ # of itself to the calling list to make calls convienent e.g. a.abs
26
+ self.class.class_eval <<-end_eval
27
+ ::#{self}::#{method_name.to_s.upcase}_ADD_SELF = #{add_self}
28
+ end_eval
29
+
22
30
  if ( args_type != nil )
23
31
  # prepare c and ruby args code
24
32
  c_src = ""
@@ -72,6 +80,7 @@ module GSL4r
72
80
 
73
81
  c_src << " puts(" << %Q{\\"end\\"} << ");"
74
82
 
83
+ # TODO, create unit test for aliased/shorthand versions of methods
75
84
 
76
85
  eval <<-end_eval
77
86
  def c_test_#{method_name}
@@ -80,6 +89,63 @@ module GSL4r
80
89
  end
81
90
  end_eval
82
91
  end
83
- end
84
- end
85
- end
92
+ end # attach_gsl_function
93
+
94
+ module AutoPrefix
95
+
96
+ $prefixLock = Monitor.new
97
+
98
+ # This traps method calls intended to create shortened versions
99
+ # of the GSL function calls.
100
+ #
101
+ # This first checks if the called method matches the Module
102
+ # function call gsl_complex_#{called_method} (where called_method
103
+ # might be 'abs').
104
+ #
105
+ # If it finds a match (respond_to), it will then create a new
106
+ # method for the class as a whole (class_eval), making the method
107
+ # available to not just this instance of the class, but all
108
+ # existing instances and all those created after.
109
+ #
110
+ # Finally, the creation is wrapped up in a synchronized call
111
+ # to ensure thread safety. It is only unsafe the first time
112
+ # the method is invoked (and non-existent at that point). Every
113
+ # time the method is invoked after, it should not hit method_missing.
114
+ # TODO: Is this true for java threads too, or is it per 'vm' per
115
+ # thread?
116
+ def method_missing( called_method, *args, &block )
117
+
118
+ $prefixLock.synchronize do
119
+
120
+ prefix = self.class::GSL_PREFIX
121
+
122
+ if ( self.class::GSL_MODULE::Methods.respond_to?("#{prefix}#{called_method}") == false )
123
+ prefix = ""
124
+ if ( self.class::GSL_MODULE::Methods.respond_to?("#{called_method}") == false )
125
+ super # NoMethodError
126
+ end
127
+ end
128
+
129
+ # TODO: this could be smoothed out with the #args/#parameters parts of
130
+ # Ruby 1.9.
131
+ # This could inspect the definition of the parameter and if the
132
+ # first argument in the definition were of the same type as self
133
+ # then self could be inserted into the args list per below
134
+ # rather than requiring the #{called_method.to_s.upcase}_ADD_SELF
135
+ # boolean definition and check
136
+ self.class.class_eval <<-end_eval
137
+ def #{called_method}(*args, &block)
138
+ if ::#{self.class::GSL_MODULE.to_s}::Methods::#{prefix.to_s.upcase}#{called_method.to_s.upcase}_ADD_SELF
139
+ args.insert(0, self)
140
+ end
141
+ ::#{self.class::GSL_MODULE.to_s}::Methods::#{prefix}#{called_method}( *args, &block )
142
+ end
143
+ end_eval
144
+
145
+ __send__(called_method, *args, &block)
146
+ end # prefixLock.synchronize
147
+ end # method_missing
148
+ end # AutoPrefix
149
+
150
+ end # Util
151
+ end # GSL4r
data/lib/gsl4r/vector.rb CHANGED
@@ -10,20 +10,25 @@ require 'rubygems'
10
10
  require 'ffi'
11
11
 
12
12
  module GSL4r
13
+ module Vector
13
14
 
14
- extend ::FFI::Library
15
+ extend ::FFI::Library
15
16
 
16
- ffi_lib 'gsl'
17
+ ffi_lib ::GSL4r::GSL_LIB_PATH
17
18
 
18
- class Vector
19
- attach_function :gsl_vector_alloc, [ :size_t ], :pointer
20
- attach_function :gsl_vector_calloc, [ :size_t ], :pointer
21
- attach_function :gsl_vector_free, [ :pointer ], :void
19
+ class GSL_Vector < ::FFI::Struct
20
+ layout :size, :size_t,
21
+ :stride, :size_t,
22
+ :data, :pointer,
23
+ :block, :pointer,
24
+ :owner, :int
22
25
 
23
- # initializing
24
- attach_function :gsl_vector_set_all, [ :pointer, :double ], :void
25
- attach_function :gsl_vector_set_zero, [ :pointer ], :void
26
- attach_function :gsl_vector_set_basis, [ :pointer, :size_t ], :int
27
- end
26
+ include ::GSL4r::Util::AutoPrefix
27
+ module ::GSL4r::Util::AutoPrefix
28
+ GSL_PREFIX = "gsl_vector_"
29
+ GSL_MODULE = ::GSL4r::Vector
30
+ end
28
31
 
29
- end
32
+ end # class GSL_Vector
33
+ end # module Vector
34
+ end # module GSL4r
data/lib/t.rb ADDED
@@ -0,0 +1,10 @@
1
+ include ::GSL4r::Complex
2
+ a=GSL_Complex.create(1,1)
3
+
4
+ a.gsl_complex_arccsc_real(5.0)
5
+ a.arccsc_real(5.0)
6
+
7
+ #a.gsl_complex_abs(a)
8
+ #a.gsl_complex_abs()
9
+ #a.abs()
10
+
data/test/README ADDED
@@ -0,0 +1,8 @@
1
+ This directory is empty for now, as all tests are generated automatically by running:
2
+
3
+ rake test
4
+
5
+ This will create a C programs that make calls to GSL functions, then writing the
6
+ results as Ruby programs that cross check the FFI based wrapper answers against
7
+ the C programs' results using Test::Unit.
8
+