ragni-cas 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5050928caea86f26844bb7d526712ebbac3955bd
4
- data.tar.gz: d65f4cc064b36c402cea4dc49a7ad983130af256
3
+ metadata.gz: 37f26b41c10ea87025194d269eca06dd324ab079
4
+ data.tar.gz: db0c443f3488b0229a8d3fac2d3e891815c80fd5
5
5
  SHA512:
6
- metadata.gz: ad30cd4099786ab30fcfb02d2df868fdd2addf7ae69611c881f5cffaa2f46ba414bd66b4cb84c868582c92b1fce4c2b9a163379b194c2f717d83c5de3bb2a237
7
- data.tar.gz: d5edfc457d09f2758d18108af41e96c4107634e7dcd9aaee0ce03b0eeb85b33ccab641890e9b8e0585268bbbee5242cba8f49cc505a37cc6c9c7c8f76ad62681
6
+ metadata.gz: 83557eef91cc4384d07fa82c835a33e8351465c38a84036af6f5eda0b08281f08e4152fc88fb0f91d4ecb03bf4d181b33facdc25d54d67822589a95002a0829d
7
+ data.tar.gz: 6ad9a1a6d43df4685db8468f0fe82bf6ff82047173ed60deec848b3a2709edd99a38363a24978ffadd32bbaa983e88707f6399a3376f452ca37efa255e389140
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/fnc-branch.rb CHANGED
@@ -45,7 +45,7 @@ module CAS
45
45
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
46
46
  "#{cls} -> #{@x.dot_graph node}\n #{cls} -> #{@y.dot_graph node}\n #{cls} -> #{@condition.dot_graph node}"
47
47
  end
48
-
48
+
49
49
  def to_latex
50
50
  "\\left\\{ \\begin{array}{lr} #{@x.to_latex} & #{@condition.to_latex} \\\\ #{@y.to_latex} \\end{array} \\right."
51
51
  end
@@ -154,56 +154,192 @@ module CAS
154
154
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
155
155
  "#{cls} -> #{@x.dot_graph node}\n #{cls} -> #{@y.dot_graph node}"
156
156
  end
157
- end
157
+ end # Condition
158
158
 
159
159
  class Equal < CAS::Condition
160
160
  def initialize(x, y)
161
161
  super(:eq, x, y)
162
162
  end
163
-
163
+
164
164
  def to_latex
165
165
  "#{@x.to_latex} = #{@y.to_latex}"
166
166
  end
167
- end
167
+ end # Equal
168
168
 
169
169
  class Greater < CAS::Condition
170
170
  def initialize(x, y)
171
171
  super(:gt, x, y)
172
172
  end
173
-
173
+
174
174
  def to_latex
175
175
  "#{@x.to_latex} > #{@y.to_latex}"
176
176
  end
177
- end
177
+ end # Greater
178
178
 
179
179
  class GreaterEqual < CAS::Condition
180
180
  def initialize(x, y)
181
181
  super(:geq, x, y)
182
182
  end
183
-
183
+
184
184
  def to_latex
185
185
  "#{@x.to_latex} \\geq #{@y.to_latex}"
186
186
  end
187
- end
187
+ end # GreaterEqual
188
188
 
189
189
  class Smaller < CAS::Condition
190
190
  def initialize(x, y)
191
191
  super(:lt, x, y)
192
192
  end
193
-
193
+
194
194
  def to_latex
195
195
  "#{@x.to_latex} < #{@y.to_latex}"
196
196
  end
197
- end
197
+ end # Smaller
198
198
 
199
199
  class SmallerEqual < CAS::Condition
200
200
  def initialize(x, y)
201
201
  super(:leq, x, y)
202
202
  end
203
-
203
+
204
204
  def to_latex
205
205
  "#{@x.to_latex} \\leq #{@y.to_latex}"
206
206
  end
207
+ end # SmallerEqual
208
+
209
+ class BoxCondition < CAS::Condition
210
+ attr_reader :x, :lower, :upper
211
+
212
+ def initialize(x, lower, upper, type=:closed)
213
+ if lower.is_a? Numeric
214
+ lower = CAS::const lower
215
+ end
216
+ if upper.is_a? Numeric
217
+ upper = CAS::const upper
218
+ end
219
+ CAS::Help.assert(lower, CAS::Constant)
220
+ CAS::Help.assert(upper, CAS::Constant)
221
+ CAS::Help.assert(x, CAS::Op)
222
+ CAS::Help.assert(type, Symbol)
223
+
224
+ lower, upper = upper, lower if lower.x > upper.x
225
+
226
+ @lower = lower
227
+ @upper = upper
228
+ @x = x
229
+
230
+ case type
231
+ when :open
232
+ self.define_singleton_method :call do |fd|
233
+ (@lower.call(fd) < @x.call(fd)) and (@x.call(fd) < @upper.call(fd))
234
+ end
235
+ self.define_singleton_method :inspect do
236
+ "#{@lower.inspect} < #{@x.inspect} < #{@upper.inspect}"
237
+ end
238
+ self.define_singleton_method :to_s do
239
+ "#{@lower} < #{@x} < #{@upper}"
240
+ end
241
+ self.define_singleton_method :to_code do
242
+ "(#{@lower.to_code} < #{@x.to_code} and #{@x.to_code} < #{@upper.to_code}"
243
+ end
244
+ when :closed
245
+ self.define_singleton_method :call do |fd|
246
+ (@lower.call(fd) <= @x.call(fd)) and (@x.call(fd) <= @upper.call(fd))
247
+ end
248
+ self.define_singleton_method :inspect do
249
+ "#{@lower.inspect} ≤ #{@x.inspect} ≤ #{@upper.inspect}"
250
+ end
251
+ self.define_singleton_method :to_s do
252
+ "#{@lower} ≤ #{@x} ≤ #{@upper}"
253
+ end
254
+ self.define_singleton_method :to_code do
255
+ "(#{@lower.to_code} <= #{@x.to_code} and #{@x.to_code} <= #{@upper.to_code}"
256
+ end
257
+ when :upper_closed
258
+ self.define_singleton_method :call do |fd|
259
+ (@lower.call(fd) < @x.call(fd)) and (@x.call(fd) <= @upper.call(fd))
260
+ end
261
+ self.define_singleton_method :inspect do
262
+ "#{@lower.inspect} < #{@x.inspect} ≤ #{@upper.inspect}"
263
+ end
264
+ self.define_singleton_method :to_s do
265
+ "#{@lower} < #{@x} ≤ #{@upper}"
266
+ end
267
+ self.define_singleton_method :to_code do
268
+ "(#{@lower.to_code} < #{@x.to_code} and #{@x.to_code} <= #{@upper.to_code}"
269
+ end
270
+ when :lower_closed
271
+ self.define_singleton_method :call do |fd|
272
+ (@lower.call(fd) <= @x.call(fd)) and (@x.call(fd) < @upper.call(fd))
273
+ end
274
+ self.define_singleton_method :inspect do
275
+ "#{@lower.inspect} ≤ #{@x.inspect} < #{@upper.inspect}"
276
+ end
277
+ self.define_singleton_method :to_s do
278
+ "#{@lower} ≤ #{@x} < #{@upper}"
279
+ end
280
+ self.define_singleton_method :to_code do
281
+ "(#{@lower.to_code} <= #{@x.to_code} and #{@x.to_code} < #{@upper.to_code}"
282
+ end
283
+ else
284
+ raise ArgumentError, "Unknown type of box condition"
285
+ end
286
+ end
287
+
288
+ def depend?(v)
289
+ @x.depend? v
290
+ end
291
+
292
+ def simplify
293
+ @x.simplify
294
+ return self
295
+ end
296
+
297
+ def subs(fd)
298
+ @x = @x.subs(fd)
299
+ return self
300
+ end
301
+
302
+ def diff(v)
303
+ CAS::equal(@x.diff(x).simplify, CAS::Zero)
304
+ end
305
+
306
+ def dot_graph(node)
307
+ cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
308
+ " #{cls} -> #{@lower.dot_graph node}\n #{cls} -> #{@x.dot_graph node}\n #{cls} -> #{@upper.dot_graph node}\n"
309
+ end
310
+
311
+ def ==(cond)
312
+ return false if not self.class != cond.class
313
+ return (@x == cond.x and @lower == cond.lower and @upper == cond.upper)
314
+ end
315
+
316
+ def !=(cond)
317
+ not self == cond
318
+ end
319
+ end # BoxCondition
320
+
321
+ class BoxConditionOpen < CAS::BoxCondition
322
+ def initialize(x, a, b)
323
+ super x, a, b, :open
324
+ end
325
+ end
326
+
327
+ class BoxConditionClosed < CAS::BoxCondition
328
+ def initialize(x, a, b)
329
+ super x, a, b, :closed
330
+ end
331
+ end
332
+
333
+ class BoxConditionUpperClosed < CAS::BoxCondition
334
+ def initialize(x, a, b)
335
+ super x, a, b, :upper_closed
336
+ end
337
+ end
338
+
339
+ class BoxConditionLowerClosed < CAS::BoxCondition
340
+ def initialize(x, a, b)
341
+ super x, a, b, :lower_closed
342
+ end
207
343
  end
208
344
 
209
345
  def self.equal(x, y); CAS::Equal.new(x, y); end
@@ -212,11 +348,28 @@ module CAS
212
348
  def self.smaller(x, y); CAS::Smaller.new(x, y); end
213
349
  def self.smaller_equal(x, y); CAS::SmallerEqual.new(x, y); end
214
350
 
351
+ def self.box(x, a, b, type=:closed)
352
+ case type
353
+ when :closed
354
+ return CAS::BoxConditionClosed.new(x, a, b)
355
+ when :open
356
+ return CAS::BoxConditionOpen.new(x, a, b)
357
+ when :upper_closed
358
+ return CAS::BoxConditionUpperClosed.new(x, a, b)
359
+ when :lower_closed
360
+ return CAS::BoxConditionLowerClosed.new(x, a, b)
361
+ else
362
+ raise CAS::CASError, "Unknown box condition type"
363
+ end
364
+ end
365
+
215
366
  class Op
216
367
  def equal(v); CAS.equal(self, v); end
217
368
  def greater(v); CAS.greater(self, v); end
218
369
  def smaller(v); CAS.smaller(self, v); end
219
370
  def greater_equal(v); CAS.greater_equal(self, v); end
220
371
  def smaller_equal(v); CAS.smaller_equal(self, v); end
372
+
373
+ def limit(a, b, type=:closed); CAS.box(self, a, b, type); end
221
374
  end
222
375
  end
data/lib/op.rb CHANGED
@@ -237,8 +237,8 @@ module CAS
237
237
  def as_proc(bind=nil)
238
238
  args_ext = self.args.map { |e| "#{e} = fd[\"#{e}\"];" }
239
239
  code = "Proc.new do |fd|; #{args_ext.join " "} #{self.to_code}; end"
240
- if bind
241
- CAS::Help.assert(bind, Binding)
240
+ if bind # All objects have eval value, we bind when not nil
241
+ # CAS::Help.assert(bind, Binding)
242
242
  bind.eval(code)
243
243
  else
244
244
  eval(code)
@@ -260,7 +260,7 @@ module CAS
260
260
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
261
261
  "#{cls} -> #{@x.dot_graph node}\n"
262
262
  end
263
-
263
+
264
264
  # Returns the latex representation of the current Op.
265
265
  #
266
266
  # -> `String`
@@ -443,7 +443,7 @@ module CAS
443
443
  cls = "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id}"
444
444
  "#{cls} -> #{@x.dot_graph node}\n #{cls} -> #{@y.dot_graph node}"
445
445
  end
446
-
446
+
447
447
  # Returns the latex representation of the current Op.
448
448
  #
449
449
  # -> `String`
data/lib/ragni-cas.rb CHANGED
@@ -22,12 +22,15 @@
22
22
  # Copyright:: Copyright (c) 2016 Matteo Ragni
23
23
  # License:: Distributed under MIT license term
24
24
 
25
- require_relative 'op.rb'
26
- require_relative 'numbers.rb'
27
- require_relative 'fnc-base.rb'
28
- require_relative 'fnc-trig.rb'
29
- require_relative 'fnc-trsc.rb'
30
- require_relative 'fnc-branch.rb'
25
+ # ___ _
26
+ # | _ \___ __ _ _ _(_)_ _ ___ ___
27
+ # | / -_) _` | || | | '_/ -_|_-<
28
+ # |_|_\___\__, |\_,_|_|_| \___/__/
29
+ # |_|
30
+
31
+ %w|op.rb numbers.rb fnc-base.rb fnc-trig.rb fnc-trsc.rb fnc-branch.rb version.rb|.each do |r|
32
+ require File.expand_path(r, File.dirname(__FILE__))
33
+ end
31
34
 
32
35
  module CAS
33
36
 
@@ -118,3 +121,105 @@ digraph Op {
118
121
  end
119
122
  end
120
123
  end
124
+
125
+
126
+ # ___ _ _ _ _ _ _
127
+ # / _ \__ _____ _ _| |___ __ _ __| (_)_ _ __ _ | \| |_ _ _ __ ___ _ _(_)__
128
+ # | (_) \ V / -_) '_| / _ \/ _` / _` | | ' \/ _` | | .` | || | ' \/ -_) '_| / _|
129
+ # \___/ \_/\___|_| |_\___/\__,_\__,_|_|_||_\__, | |_|\_|\_,_|_|_|_\___|_| |_\__|
130
+ # |___/
131
+ class Fixnum
132
+ alias :overloaded_plus :+
133
+ alias :overloaded_minus :-
134
+ alias :overloaded_mul :*
135
+ alias :overloaded_div :/
136
+ alias :overloaded_pow :**
137
+
138
+ def +(a)
139
+ if a.is_a? CAS::Op
140
+ CAS::const(self) + a
141
+ else
142
+ self.overloaded_plus a
143
+ end
144
+ end
145
+
146
+ def -(a)
147
+ if a.is_a? CAS::Op
148
+ CAS::const(self) - a
149
+ else
150
+ self.overloaded_minus a
151
+ end
152
+ end
153
+
154
+ def *(a)
155
+ if a.is_a? CAS::Op
156
+ CAS::const(self) * a
157
+ else
158
+ self.overloaded_mul a
159
+ end
160
+ end
161
+
162
+ def /(a)
163
+ if a.is_a? CAS::Op
164
+ CAS::const(self) / a
165
+ else
166
+ self.overloaded_div a
167
+ end
168
+ end
169
+
170
+ def **(a)
171
+ if a.is_a? CAS::Op
172
+ CAS::const(self) ** a
173
+ else
174
+ self.overloaded_pow a
175
+ end
176
+ end
177
+ end
178
+
179
+ class Float
180
+ alias :overloaded_plus :+
181
+ alias :overloaded_minus :-
182
+ alias :overloaded_mul :*
183
+ alias :overloaded_div :/
184
+ alias :overloaded_pow :**
185
+
186
+ def +(a)
187
+ if a.is_a? CAS::Op
188
+ CAS::const(self) + a
189
+ else
190
+ self.overloaded_plus a
191
+ end
192
+ end
193
+
194
+ def -(a)
195
+ if a.is_a? CAS::Op
196
+ CAS::const(self) - a
197
+ else
198
+ self.overloaded_minus a
199
+ end
200
+ end
201
+
202
+ def *(a)
203
+ if a.is_a? CAS::Op
204
+ CAS::const(self) * a
205
+ else
206
+ self.overloaded_mul a
207
+ end
208
+ end
209
+
210
+ def /(a)
211
+ if a.is_a? CAS::Op
212
+ CAS::const(self) / a
213
+ else
214
+ self.overloaded_div a
215
+ end
216
+ end
217
+
218
+ def **(a)
219
+ if a.is_a? CAS::Op
220
+ CAS::const(self) ** a
221
+ else
222
+ self.overloaded_pow a
223
+ end
224
+ end
225
+ end
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
  #!/usr/bin/env ruby
3
3
 
4
4
  module CAS
5
- VERSION = [0, 2, 0]
5
+ VERSION = [0, 2, 1]
6
6
  end
7
7
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ragni-cas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Ragni
@@ -29,7 +29,7 @@ cert_chain:
29
29
  XorZtzkkLImvKFj35xKLFfVkv0Vd8tGQoiL8vdmQNJjAjtE+C+Y7OI4dpiZPKO4G
30
30
  R/8JOvUuk9jPbyLxjQH/sFaFqqYGX2xo1zk2CRy/A0WhJrSaXVw1r5lEi7b0W5gg
31
31
  -----END CERTIFICATE-----
32
- date: 2016-07-27 00:00:00.000000000 Z
32
+ date: 2016-07-28 00:00:00.000000000 Z
33
33
  dependencies: []
34
34
  description:
35
35
  email: info@ragni.me
@@ -68,5 +68,5 @@ rubyforge_project:
68
68
  rubygems_version: 2.5.1
69
69
  signing_key:
70
70
  specification_version: 4
71
- summary: An extremely simple CAS system for my optimizers
71
+ summary: An extremely simple CAS, for rapid prototyping and meta-programming
72
72
  test_files: []
metadata.gz.sig CHANGED
Binary file