lbfgsb 0.2.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 272abcfabc883d993bd0b9eae08b4e0d590524b4a5aea38bc7b1f8c92fb0a5b5
4
- data.tar.gz: 315ffe16183e0a08d236a9563cc6daa29db0931310b8d0cd2ccbd10810b2353e
3
+ metadata.gz: 771754be56f6a3954337488ed44d237a9d97899c28800916d89ff134e8d2d12d
4
+ data.tar.gz: 7db51dd7443056af4e174199752ba7ef136c7898bb9bd086b5316a784fb0a70f
5
5
  SHA512:
6
- metadata.gz: 73b05d103b55055894fec49d587c78a798b94d7cc3333324a5e1331e1dd8ce30bb0ee0280f190e111cc04e3d31c3c16e06fce51c91fe004a7f59bcc05aa8f1c5
7
- data.tar.gz: 0ebcc71411a6fd7b9339d7c9a335277ba69eb74cb83f301de3c651bc243d1daef396b84e9904a680511d7f5f07ac1ff4af469d344f68b2ad2b41ff003b2c5534
6
+ metadata.gz: a1eb043cbdd14c0db2d9bd1a74b934d1719a95ee943281f915a11dbff508cdaf55c501d6ba250cec5569b405ff0ab4702124c1388cc6e14bd7df229727ffbfcf
7
+ data.tar.gz: db4f459a6b3711415c78d8a94ff0bbc4039581f7923e0bbabd833e2222a0e5462979b2319f3284725411e6542d84f4d319003cf685ccc9c9d4d27b909d79a443
data/.clang-format ADDED
@@ -0,0 +1,149 @@
1
+ ---
2
+ Language: Cpp
3
+ # BasedOnStyle: LLVM
4
+ AccessModifierOffset: -2
5
+ AlignAfterOpenBracket: Align
6
+ AlignConsecutiveMacros: false
7
+ AlignConsecutiveAssignments: false
8
+ AlignConsecutiveBitFields: false
9
+ AlignConsecutiveDeclarations: false
10
+ AlignEscapedNewlines: Right
11
+ AlignOperands: Align
12
+ AlignTrailingComments: true
13
+ AllowAllArgumentsOnNextLine: true
14
+ AllowAllConstructorInitializersOnNextLine: true
15
+ AllowAllParametersOfDeclarationOnNextLine: true
16
+ AllowShortEnumsOnASingleLine: true
17
+ AllowShortBlocksOnASingleLine: Never
18
+ AllowShortCaseLabelsOnASingleLine: false
19
+ AllowShortFunctionsOnASingleLine: All
20
+ AllowShortLambdasOnASingleLine: All
21
+ AllowShortIfStatementsOnASingleLine: Never
22
+ AllowShortLoopsOnASingleLine: false
23
+ AlwaysBreakAfterDefinitionReturnType: None
24
+ AlwaysBreakAfterReturnType: None
25
+ AlwaysBreakBeforeMultilineStrings: false
26
+ AlwaysBreakTemplateDeclarations: MultiLine
27
+ BinPackArguments: true
28
+ BinPackParameters: true
29
+ BraceWrapping:
30
+ AfterCaseLabel: false
31
+ AfterClass: false
32
+ AfterControlStatement: Never
33
+ AfterEnum: false
34
+ AfterFunction: false
35
+ AfterNamespace: false
36
+ AfterObjCDeclaration: false
37
+ AfterStruct: false
38
+ AfterUnion: false
39
+ AfterExternBlock: false
40
+ BeforeCatch: false
41
+ BeforeElse: false
42
+ BeforeLambdaBody: false
43
+ BeforeWhile: false
44
+ IndentBraces: false
45
+ SplitEmptyFunction: true
46
+ SplitEmptyRecord: true
47
+ SplitEmptyNamespace: true
48
+ BreakBeforeBinaryOperators: None
49
+ BreakBeforeBraces: Attach
50
+ BreakBeforeInheritanceComma: false
51
+ BreakInheritanceList: BeforeColon
52
+ BreakBeforeTernaryOperators: true
53
+ BreakConstructorInitializersBeforeComma: false
54
+ BreakConstructorInitializers: BeforeColon
55
+ BreakAfterJavaFieldAnnotations: false
56
+ BreakStringLiterals: true
57
+ ColumnLimit: 128
58
+ CommentPragmas: '^ IWYU pragma:'
59
+ CompactNamespaces: false
60
+ ConstructorInitializerAllOnOneLineOrOnePerLine: false
61
+ ConstructorInitializerIndentWidth: 4
62
+ ContinuationIndentWidth: 4
63
+ Cpp11BracedListStyle: true
64
+ DeriveLineEnding: true
65
+ DerivePointerAlignment: false
66
+ DisableFormat: false
67
+ ExperimentalAutoDetectBinPacking: false
68
+ FixNamespaceComments: true
69
+ ForEachMacros:
70
+ - foreach
71
+ - Q_FOREACH
72
+ - BOOST_FOREACH
73
+ IncludeBlocks: Preserve
74
+ IncludeCategories:
75
+ - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
76
+ Priority: 2
77
+ SortPriority: 0
78
+ - Regex: '^(<|"(gtest|gmock|isl|json)/)'
79
+ Priority: 3
80
+ SortPriority: 0
81
+ - Regex: '.*'
82
+ Priority: 1
83
+ SortPriority: 0
84
+ IncludeIsMainRegex: '(Test)?$'
85
+ IncludeIsMainSourceRegex: ''
86
+ IndentCaseLabels: false
87
+ IndentCaseBlocks: false
88
+ IndentGotoLabels: true
89
+ IndentPPDirectives: None
90
+ IndentExternBlock: AfterExternBlock
91
+ IndentWidth: 2
92
+ IndentWrappedFunctionNames: false
93
+ InsertTrailingCommas: None
94
+ JavaScriptQuotes: Leave
95
+ JavaScriptWrapImports: true
96
+ KeepEmptyLinesAtTheStartOfBlocks: true
97
+ MacroBlockBegin: ''
98
+ MacroBlockEnd: ''
99
+ MaxEmptyLinesToKeep: 1
100
+ NamespaceIndentation: None
101
+ ObjCBinPackProtocolList: Auto
102
+ ObjCBlockIndentWidth: 2
103
+ ObjCBreakBeforeNestedBlockParam: true
104
+ ObjCSpaceAfterProperty: false
105
+ ObjCSpaceBeforeProtocolList: true
106
+ PenaltyBreakAssignment: 2
107
+ PenaltyBreakBeforeFirstCallParameter: 19
108
+ PenaltyBreakComment: 300
109
+ PenaltyBreakFirstLessLess: 120
110
+ PenaltyBreakString: 1000
111
+ PenaltyBreakTemplateDeclaration: 10
112
+ PenaltyExcessCharacter: 1000000
113
+ PenaltyReturnTypeOnItsOwnLine: 60
114
+ PointerAlignment: Left
115
+ ReflowComments: true
116
+ SortIncludes: true
117
+ SortUsingDeclarations: true
118
+ SpaceAfterCStyleCast: false
119
+ SpaceAfterLogicalNot: false
120
+ SpaceAfterTemplateKeyword: true
121
+ SpaceBeforeAssignmentOperators: true
122
+ SpaceBeforeCpp11BracedList: false
123
+ SpaceBeforeCtorInitializerColon: true
124
+ SpaceBeforeInheritanceColon: true
125
+ SpaceBeforeParens: ControlStatements
126
+ SpaceBeforeRangeBasedForLoopColon: true
127
+ SpaceInEmptyBlock: false
128
+ SpaceInEmptyParentheses: false
129
+ SpacesBeforeTrailingComments: 1
130
+ SpacesInAngles: false
131
+ SpacesInConditionalStatement: false
132
+ SpacesInContainerLiterals: true
133
+ SpacesInCStyleCastParentheses: false
134
+ SpacesInParentheses: false
135
+ SpacesInSquareBrackets: false
136
+ SpaceBeforeSquareBrackets: false
137
+ Standard: Latest
138
+ StatementMacros:
139
+ - Q_UNUSED
140
+ - QT_REQUIRE_VERSION
141
+ TabWidth: 8
142
+ UseCRLF: false
143
+ UseTab: Never
144
+ WhitespaceSensitiveMacros:
145
+ - STRINGIZE
146
+ - PP_STRINGIZE
147
+ - BOOST_PP_STRINGIZE
148
+ ...
149
+
@@ -1,21 +1,19 @@
1
1
  name: build
2
2
 
3
- on: [push]
3
+ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  build:
7
7
  runs-on: ubuntu-latest
8
8
  strategy:
9
9
  matrix:
10
- ruby: [ '2.5', '2.6', '2.7' ]
10
+ ruby: [ '2.6', '2.7', '3.0' ]
11
11
  steps:
12
12
  - uses: actions/checkout@v2
13
13
  - name: Set up Ruby ${{ matrix.ruby }}
14
- uses: actions/setup-ruby@v1
14
+ uses: ruby/setup-ruby@v1
15
15
  with:
16
16
  ruby-version: ${{ matrix.ruby }}
17
+ bundler-cache: true
17
18
  - name: Build and test with Rake
18
- run: |
19
- gem install bundler
20
- bundle install --jobs 4 --retry 3
21
- bundle exec rake
19
+ run: bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## 0.4.1
2
+ - Remove dependent gem's type declaration file from installation files.
3
+
4
+ ## 0.4.0
5
+ - Add type declaration file: sig/lbfgsb.rbs
6
+
7
+ ## 0.3.2
8
+ - Refactor native extension codes.
9
+ - Update documentations.
10
+
11
+ ## 0.3.1
12
+ - Add GC guard to narray given to native extension method.
13
+
14
+ ## 0.3.0
15
+ - Add library name prefix to functions derived from blas and linpack
16
+ to avoid errors due to collision with the blas functions called by numo-linalg.
17
+
1
18
  ## 0.2.0
2
19
  - Add an option to minimize method for calculating both function value and gradient vector using fnc.
3
20
  If true is given to jcb, fnc is assumed to return the function value and gardient vector as [f, g] array.
data/Gemfile CHANGED
@@ -3,6 +3,8 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in lbfgsb.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
6
+ gem "rake", "~> 13.0"
7
7
  gem "rake-compiler"
8
8
  gem "rspec", "~> 3.0"
9
+ gem "rbs", "~> 1.2"
10
+ gem "steep", "~> 0.44"
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2020 Atsushi Tatsuma
1
+ Copyright (c) 2020-2021 Atsushi Tatsuma
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://github.com/yoshoku/lbfgsb.rb/workflows/build/badge.svg)](https://github.com/yoshoku/lbfgsb.rb/actions?query=workflow%3Abuild)
4
4
  [![Gem Version](https://badge.fury.io/rb/lbfgsb.svg)](https://badge.fury.io/rb/lbfgsb)
5
- [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/suika/blob/master/LICENSE.txt)
5
+ [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/suika/blob/main/LICENSE.txt)
6
6
  [![Documentation](http://img.shields.io/badge/api-reference-blue.svg)](https://yoshoku.github.io/lbfgsb.rb/doc/)
7
7
 
8
8
  Lbfgsb.rb is a Ruby binding for [L-BFGS-B](http://users.iems.northwestern.edu/~nocedal/lbfgsb.html)
data/Steepfile ADDED
@@ -0,0 +1,20 @@
1
+ target :lib do
2
+ signature "sig", "sig-deps"
3
+ #
4
+ check "lib" # Directory name
5
+ # check "Gemfile" # File name
6
+ # check "app/models/**/*.rb" # Glob
7
+ # # ignore "lib/templates/*.rb"
8
+ #
9
+ # # library "pathname", "set" # Standard libraries
10
+ # library "numo-narray" # Gems
11
+ end
12
+
13
+ # target :spec do
14
+ # signature "sig", "sig-private"
15
+ #
16
+ # check "spec"
17
+ #
18
+ # # library "pathname", "set" # Standard libraries
19
+ # # library "rspec"
20
+ # end
@@ -2,13 +2,8 @@
2
2
 
3
3
  VALUE rb_mLbfgsb;
4
4
 
5
- static
6
- VALUE lbfgsb_min_l_bfgs_b(VALUE self,
7
- VALUE fnc, VALUE x_val, VALUE jcb, VALUE args,
8
- VALUE l_val, VALUE u_val, VALUE nbd_val,
9
- VALUE maxcor, VALUE ftol, VALUE gtol, VALUE maxiter, VALUE disp)
10
- {
11
- long i;
5
+ static VALUE lbfgsb_min_l_bfgs_b(VALUE self, VALUE fnc, VALUE x_val, VALUE jcb, VALUE args, VALUE l_val, VALUE u_val,
6
+ VALUE nbd_val, VALUE maxcor, VALUE ftol, VALUE gtol, VALUE maxiter, VALUE disp) {
12
7
  long n_iter;
13
8
  long n_fev;
14
9
  long n_jev;
@@ -19,12 +14,12 @@ VALUE lbfgsb_min_l_bfgs_b(VALUE self,
19
14
  narray_t* nbd_nary;
20
15
  long n;
21
16
  long m = NUM2LONG(maxcor);
22
- double *x_ptr;
23
- double *l_ptr;
24
- double *u_ptr;
25
- long *nbd_ptr;
17
+ double* x_ptr;
18
+ double* l_ptr;
19
+ double* u_ptr;
20
+ long* nbd_ptr;
26
21
  double f;
27
- double *g;
22
+ double* g;
28
23
  double factr = NUM2DBL(ftol);
29
24
  double pgtol = NUM2DBL(gtol);
30
25
  double* wa;
@@ -35,7 +30,6 @@ VALUE lbfgsb_min_l_bfgs_b(VALUE self,
35
30
  long lsave[4];
36
31
  long isave[44];
37
32
  double dsave[29];
38
- double* g_ptr;
39
33
  VALUE g_val;
40
34
  VALUE fg_arr;
41
35
  VALUE ret;
@@ -109,17 +103,15 @@ VALUE lbfgsb_min_l_bfgs_b(VALUE self,
109
103
  wa = ALLOC_N(double, (2 * m + 5) * n + 12 * m * m + 12 * m);
110
104
  iwa = ALLOC_N(long, 3 * n);
111
105
 
106
+ g_val = Qnil;
112
107
  f = 0.0;
113
- for (i = 0; i < n; g[i++] = 0.0);
108
+ memset(g, 0, n * sizeof(*g));
114
109
  strcpy(task, "START");
115
110
  n_fev = 0;
116
111
  n_jev = 0;
117
112
 
118
113
  for (n_iter = 0; n_iter < max_iter;) {
119
- setulb_(
120
- &n, &m, x_ptr, l_ptr, u_ptr, nbd_ptr, &f, g, &factr, &pgtol, wa, iwa,
121
- task, &iprint, csave, lsave, isave, dsave
122
- );
114
+ setulb_(&n, &m, x_ptr, l_ptr, u_ptr, nbd_ptr, &f, g, &factr, &pgtol, wa, iwa, task, &iprint, csave, lsave, isave, dsave);
123
115
  if (strncmp(task, "FG", 2) == 0) {
124
116
  if (RB_TYPE_P(jcb, T_TRUE)) {
125
117
  fg_arr = rb_funcall(self, rb_intern("fnc"), 3, fnc, x_val, args);
@@ -129,15 +121,16 @@ VALUE lbfgsb_min_l_bfgs_b(VALUE self,
129
121
  f = NUM2DBL(rb_funcall(self, rb_intern("fnc"), 3, fnc, x_val, args));
130
122
  g_val = rb_funcall(self, rb_intern("jcb"), 3, jcb, x_val, args);
131
123
  }
132
- n_fev += 1;
133
- n_jev += 1;
134
- if (CLASS_OF(g_val) != numo_cDFloat) g_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, g_val);
135
- if (!RTEST(nary_check_contiguous(g_val))) g_val = nary_dup(g_val);
136
- g_ptr = (double*)na_get_pointer_for_read(g_val);
137
- for (i = 0; i < n; i++) g[i] = g_ptr[i];
124
+ n_fev++;
125
+ n_jev++;
126
+ if (CLASS_OF(g_val) != numo_cDFloat)
127
+ g_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, g_val);
128
+ if (!RTEST(nary_check_contiguous(g_val)))
129
+ g_val = nary_dup(g_val);
130
+ memcpy(g, na_get_pointer_for_read(g_val), n * sizeof(*g));
131
+ RB_GC_GUARD(g_val);
138
132
  } else if (strncmp(task, "NEW_X", 5) == 0) {
139
- n_iter += 1;
140
- continue;
133
+ n_iter++;
141
134
  } else {
142
135
  break;
143
136
  }
@@ -157,12 +150,15 @@ VALUE lbfgsb_min_l_bfgs_b(VALUE self,
157
150
  rb_hash_aset(ret, ID2SYM(rb_intern("n_jev")), LONG2NUM(n_jev));
158
151
  rb_hash_aset(ret, ID2SYM(rb_intern("success")), strncmp(task, "CONV", 4) == 0 ? Qtrue : Qfalse);
159
152
 
153
+ RB_GC_GUARD(x_val);
154
+ RB_GC_GUARD(l_val);
155
+ RB_GC_GUARD(u_val);
156
+ RB_GC_GUARD(nbd_val);
157
+
160
158
  return ret;
161
159
  }
162
160
 
163
- void
164
- Init_lbfgsbext(void)
165
- {
161
+ void Init_lbfgsbext(void) {
166
162
  rb_mLbfgsb = rb_define_module("Lbfgsb");
167
163
  /* The value of double epsilon used in the native extension. */
168
164
  rb_define_const(rb_mLbfgsb, "DBL_EPSILON", DBL2NUM(DBL_EPSILON));
@@ -2,8 +2,10 @@
2
2
  #define LBFGSB_RB_H 1
3
3
 
4
4
  #include <float.h>
5
+ #include <string.h>
5
6
 
6
7
  #include <ruby.h>
8
+
7
9
  #include <numo/narray.h>
8
10
  #include <numo/template.h>
9
11
 
@@ -5,38 +5,7 @@
5
5
  */
6
6
  #include "blas.h"
7
7
 
8
- double dnrm2_(long *n, double *x, long *incx)
9
- {
10
- long i__1, i__2;
11
- double ret_val, d__1, d__2, d__3;
12
- static long i__;
13
- static double scale;
14
-
15
- --x;
16
-
17
- ret_val = 0.;
18
- scale = 0.;
19
- i__1 = *n;
20
- i__2 = *incx;
21
- for (i__ = 1; i__2 < 0 ? i__ >= i__1 : i__ <= i__1; i__ += i__2) {
22
- d__2 = scale, d__3 = (d__1 = x[i__], fabs(d__1));
23
- scale = d__2 >= d__3 ? d__2 : d__3;
24
- }
25
- if (scale == 0.) {
26
- return ret_val;
27
- }
28
- i__2 = *n;
29
- i__1 = *incx;
30
- for (i__ = 1; i__1 < 0 ? i__ >= i__2 : i__ <= i__2; i__ += i__1) {
31
- d__1 = x[i__] / scale;
32
- ret_val += d__1 * d__1;
33
- }
34
- ret_val = scale * sqrt(ret_val);
35
- return ret_val;
36
- }
37
-
38
- int daxpy_(long *n, double *da, double *dx, long *incx, double *dy, long *incy)
39
- {
8
+ int lbfgsb_rb_daxpy_(long* n, double* da, double* dx, long* incx, double* dy, long* incy) {
40
9
  long i__1;
41
10
  static long i__, m, ix, iy, mp1;
42
11
 
@@ -100,8 +69,7 @@ L40:
100
69
  return 0;
101
70
  }
102
71
 
103
- int dcopy_(long *n, double *dx, long *incx, double *dy, long *incy)
104
- {
72
+ int lbfgsb_rb_dcopy_(long* n, double* dx, long* incx, double* dy, long* incy) {
105
73
  long i__1;
106
74
  static long i__, m, ix, iy, mp1;
107
75
 
@@ -165,8 +133,7 @@ L40:
165
133
  return 0;
166
134
  }
167
135
 
168
- double ddot_(long *n, double *dx, long *incx, double *dy, long *incy)
169
- {
136
+ double lbfgsb_rb_ddot_(long* n, double* dx, long* incx, double* dy, long* incy) {
170
137
  long i__1;
171
138
  double ret_val;
172
139
  static long i__, m, ix, iy, mp1;
@@ -224,16 +191,15 @@ L40:
224
191
  mp1 = m + 1;
225
192
  i__1 = *n;
226
193
  for (i__ = mp1; i__ <= i__1; i__ += 5) {
227
- dtemp = dtemp + dx[i__] * dy[i__] + dx[i__ + 1] * dy[i__ + 1]
228
- + dx[i__ + 2] * dy[i__ + 2] + dx[i__ + 3] * dy[i__ + 3] + dx[i__ + 4] * dy[i__ + 4];
194
+ dtemp = dtemp + dx[i__] * dy[i__] + dx[i__ + 1] * dy[i__ + 1] + dx[i__ + 2] * dy[i__ + 2] + dx[i__ + 3] * dy[i__ + 3] +
195
+ dx[i__ + 4] * dy[i__ + 4];
229
196
  }
230
197
  L60:
231
198
  ret_val = dtemp;
232
199
  return ret_val;
233
200
  }
234
201
 
235
- int dscal_(long *n, double *da, double *dx, long *incx)
236
- {
202
+ int lbfgsb_rb_dscal_(long* n, double* da, double* dx, long* incx) {
237
203
  long i__1, i__2;
238
204
  static long i__, m, mp1, nincx;
239
205