ragni-cas 0.2.2 → 0.2.3

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.
data/lib/fnc-trsc.rb DELETED
@@ -1,126 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- module CAS
4
- # ___ _
5
- # | __|_ ___ __ ___ _ _ ___ _ _| |_
6
- # | _|\ \ / '_ \/ _ \ ' \/ -_) ' \ _|
7
- # |___/_\_\ .__/\___/_||_\___|_||_\__|
8
- # |_|
9
- class Exp < CAS::Op
10
- def diff(v)
11
- if @x.depend? v
12
- return @x.diff(v) * CAS.exp(@x)
13
- else
14
- return CAS::Zero
15
- end
16
- end
17
-
18
- # Same as `CAS::Op`
19
- def call(f)
20
- CAS::Help.assert(f, Hash)
21
-
22
- Math::exp @x.call(f)
23
- end
24
-
25
- # Same as `CAS::Op`
26
- def to_s
27
- "exp(#{@x})"
28
- end
29
-
30
- # Same as `CAS::Op`
31
- def simplify
32
- super
33
- if @x == CAS::Zero
34
- return CAS::One
35
- end
36
- if @x == CAS::One
37
- return CAS::E
38
- end
39
- if @x == CAS::Infinity
40
- return CAS::Infinity
41
- end
42
- if @x.is_a? CAS::Ln
43
- return @x.x
44
- end
45
- return self
46
- end
47
-
48
- # Same as `CAS::Op`
49
- def to_code
50
- "Math::exp(#{@x.to_code})"
51
- end
52
-
53
- # Return latex representation of current Op
54
- def to_latex
55
- "e^{#{@x.to_latex}}"
56
- end
57
- end # Exp
58
-
59
- def self.exp(x)
60
- CAS::Exp.new x
61
- end
62
-
63
- # _ _ _ _
64
- # | | ___ __ _ __ _ _ _(_) |_| |_ _ __
65
- # | |__/ _ \/ _` / _` | '_| | _| ' \| ' \
66
- # |____\___/\__, \__,_|_| |_|\__|_||_|_|_|_|
67
- # |___/
68
- class Ln < CAS::Op
69
- def diff(v)
70
- if @x.depend? v
71
- return CAS::One / @x
72
- else
73
- return CAS::Zero
74
- end
75
- end
76
-
77
- # Same as `CAS::Op`
78
- def call(f)
79
- # I'm leaving to Math the honor
80
- # of handling negative values...
81
- CAS::Help.assert(f, Hash)
82
-
83
- Math::log @x.call(f)
84
- end
85
-
86
- # Same as `CAS::Op`
87
- def to_s
88
- "log(#{@x})"
89
- end
90
-
91
- # Same as `CAS::Op`
92
- def simplify
93
- super
94
- if @x == CAS::Zero
95
- return CAS.invert(CAS::Infinity)
96
- end
97
- if @x == CAS::One
98
- return CAS::Zero
99
- end
100
- if @x == CAS::E
101
- return CAS::One
102
- end
103
- if @x.is_a? CAS::Exp
104
- return @x.x
105
- end
106
- return self
107
- end
108
-
109
- # Same as `CAS::Op`
110
- def to_code
111
- "Math::log(#{@x.to_code})"
112
- end
113
-
114
- # Return latex representation of current Op
115
- def to_latex
116
- "\\log\\left( #{@x.to_latex} \\right)"
117
- end
118
- end # Ln
119
-
120
- def self.ln(x)
121
- CAS::Ln.new x
122
- end
123
- def self.log(x)
124
- CAS::Ln.new x
125
- end
126
- end
data/lib/numbers.rb DELETED
@@ -1,390 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- module CAS
4
- # ___ _ _
5
- # / __|___ _ _ __| |_ __ _ _ _| |_
6
- # | (__/ _ \ ' \(_-< _/ _` | ' \ _|
7
- # \___\___/_||_/__/\__\__,_|_||_\__|
8
- class Constant < CAS::Op
9
- def initialize(x)
10
- @x = x
11
- end
12
-
13
- # The derivative of a constant is always zero
14
- def diff(v)
15
- CAS::Zero
16
- end
17
-
18
- # Same as `CAS::Op`
19
- def call(f)
20
- @x
21
- end
22
-
23
- # Same as `CAS::Op`
24
- def depend?(v)
25
- false
26
- end
27
-
28
- # Same as `CAS::Op`
29
- def to_s
30
- "#{@x}"
31
- end
32
-
33
- # Subs for a constant is a dummy method
34
- def subs(dt)
35
- # CAS::Help.assert(dt, Hash)
36
- # if dt.keys.include? self
37
- # if dt[self].is_a? CAS::Op
38
- # return dt[self]
39
- # elsif dt[self].is_a? Numeric
40
- # return CAS::const(dt[self])
41
- # else
42
- # raise CASError, "Impossible subs. Received a #{dt[self].class} = #{dt[self]}"
43
- # end
44
- # end
45
- end
46
-
47
- # Same as `CAS::Op`
48
- def simplify
49
- case @x
50
- when 0
51
- return CAS::Zero
52
- when 1
53
- return CAS::One
54
- when Math::PI
55
- return CAS::Pi
56
- when Math::E
57
- return CAS::E
58
- when 1.0/0.0
59
- return CAS::Infinity
60
- else
61
- return self
62
- end
63
- end
64
-
65
- # Same as `CAS::Op`
66
- def args
67
- []
68
- end
69
-
70
- # Same as `CAS::Op`
71
- def ==(op)
72
- # CAS::Help.assert(op, CAS::Op)
73
-
74
- if op.is_a? CAS::Constant
75
- return @x == op.x
76
- else
77
- return false
78
- end
79
- end
80
-
81
- # Same as `CAS::Op`
82
- def inspect
83
- "Const(#{@x})"
84
- end
85
-
86
- # Same as `CAS::Op`
87
- def dot_graph(node)
88
- "#{self.class.to_s.gsub("CAS::", "")}_#{self.object_id};"
89
- end
90
-
91
- # Return latex representation of current Op
92
- def to_latex
93
- self.to_s
94
- end
95
- end
96
-
97
- # Allows to define a series of new constants.
98
- #
99
- # ``` ruby
100
- # a, b = CAS::const 1.0, 100
101
- # ```
102
- #
103
- # <- `Array` of Numeric
104
- # -> `Array` of `CAS::Contant`
105
- def self.const(*val)
106
- (return CAS::Constant.new(val[0])) if val.size == 1
107
- ret = []
108
- val.each do |n|
109
- ret << CAS::Constant.new(n)
110
- end
111
- return ret
112
- end
113
-
114
- # __ __ _ _ _
115
- # \ \ / /_ _ _ _(_)__ _| |__| |___
116
- # \ V / _` | '_| / _` | '_ \ / -_)
117
- # \_/\__,_|_| |_\__,_|_.__/_\___|
118
- class Variable < CAS::Op
119
- # Contains all define variable, in an hash. Variables are
120
- # accessible through variable name.
121
- @@vars = {}
122
-
123
- # Returns the `Hash` that contains all the variable
124
- #
125
- # -> `Hash`
126
- def self.list
127
- @@vars
128
- end
129
-
130
- # Return the number of variable defined
131
- #
132
- # -> `Fixnum`
133
- def self.size
134
- @@vars.keys.size
135
- end
136
-
137
- # Returns `true` if a variable already exists
138
- #
139
- # <- `Object` that represent the variable
140
- # -> `TrueClass` if variable exists, `FalseClass` if not
141
- def self.exist?(name)
142
- @@vars.keys.include? name
143
- end
144
-
145
- attr_reader :name
146
- # Variable is a container for an atomic simbol that becomes a number
147
- # when `CAS::Op#call` method is used.
148
- #
149
- # <- `Object` that is a identifier for the variable
150
- # -> `CAS::Variable` instance
151
- def initialize(name)
152
- raise CASError, "Variable #{name} already exists" if CAS::Variable.exist? name
153
- @name = name
154
- @@vars[@name] = self
155
- end
156
-
157
- # Returns the derivative of a variable
158
- #
159
- # ```
160
- # dx dx
161
- # -- = 1; -- = 0
162
- # dx dy
163
- # ```
164
- #
165
- # <- `CAS::Op` for the derivative denominator
166
- # -> `CAS::Constant`, 0 if not depended, 1 if dependent
167
- def diff(v)
168
- (self == v ? CAS::One : CAS::Zero)
169
- end
170
-
171
- # Same as `CAS::Op`
172
- def depend?(v)
173
- self == v
174
- end
175
-
176
- # Same as `CAS::Op`
177
- def ==(op)
178
- # CAS::Help.assert(op, CAS::Op)
179
- if op.is_a? CAS::Variable
180
- return self.inspect == op.inspect
181
- else
182
- false
183
- end
184
- end
185
-
186
- # Same as `CAS::Op`
187
- def call(f)
188
- CAS::Help.assert(f, Hash)
189
-
190
- return f[self] if f[self]
191
- return f[@name] if f[@name]
192
- end
193
-
194
- # Same as `CAS::Op`
195
- def to_s
196
- "#{@name}"
197
- end
198
-
199
- # Same as `CAS::Op`
200
- def to_code
201
- "#{@name}"
202
- end
203
-
204
- # Same as `CAS::Op`
205
- def args
206
- [self]
207
- end
208
-
209
- # Terminal substitutions for variables
210
- def subs(dt)
211
- CAS::Help.assert(dt, Hash)
212
- if dt.keys.include? self
213
- if dt[self].is_a? CAS::Op
214
- return dt[self]
215
- elsif dt[self].is_a? Numeric
216
- return CAS::const(dt[self])
217
- else
218
- raise CASError, "Impossible subs. Received a #{dt[self].class} = #{dt[self]}"
219
- end
220
- end
221
- end
222
-
223
- # Same as `CAS::Op`
224
- def inspect
225
- "Var(#{@name})"
226
- end
227
-
228
- # Same as `CAS::Op`
229
- def simplify
230
- self
231
- end
232
-
233
- # Same as `CAS::Op`
234
- def dot_graph(node)
235
- "#{@name};"
236
- end
237
-
238
- # Return latex representation of current Op
239
- def to_latex
240
- self.to_s
241
- end
242
- end # Number
243
-
244
- def self.vars(*name)
245
- (return CAS::Variable.new(name[0])) if name.size == 1
246
- ret = []
247
- name.each do |n|
248
- ret << CAS::Variable.new(n)
249
- end
250
- return ret
251
- end
252
-
253
- # _______ ___ ___
254
- # |_ / __| _ \/ _ \
255
- # / /| _|| / (_) |
256
- # /___|___|_|_\\___/
257
- class ZERO_CONSTANT < CAS::Constant
258
- def initialize
259
- @x = 0.0
260
- end
261
-
262
- def to_s
263
- "0"
264
- end
265
- end # Zero
266
- Zero = CAS::ZERO_CONSTANT.new
267
-
268
- # ___
269
- # / _ \ _ _ ___
270
- # | (_) | ' \/ -_)
271
- # \___/|_||_\___|
272
- class ONE_CONSTANT < CAS::Constant
273
- def initialize
274
- @x = 1.0
275
- end
276
-
277
- def to_s
278
- "1"
279
- end
280
- end # Zero
281
- One = CAS::ONE_CONSTANT.new
282
-
283
- # _____
284
- # |_ _|_ __ _____
285
- # | | \ V V / _ \
286
- # |_| \_/\_/\___/
287
- class TWO_CONSTANT < CAS::Constant
288
- def initialize
289
- @x = 2.0
290
- end
291
-
292
- def to_s
293
- "2"
294
- end
295
- end # Zero
296
- Two = CAS::TWO_CONSTANT.new
297
-
298
- # ___ ___
299
- # | _ \_ _|
300
- # | _/| |
301
- # |_| |___|
302
- class PI_CONSTANT < CAS::Constant
303
- def initialize
304
- @x = Math::PI
305
- end
306
-
307
- def to_s
308
- "π"
309
- end
310
-
311
- def to_latex
312
- "\\pi"
313
- end
314
- end
315
- Pi = CAS::PI_CONSTANT.new
316
-
317
- # ___
318
- # | __|
319
- # | _|
320
- # |___|
321
- class E_CONSTANT < CAS::Constant
322
- def initialize
323
- @x = Math::E
324
- end
325
-
326
- def to_s
327
- "e"
328
- end
329
-
330
- def to_latex
331
- "e"
332
- end
333
- end
334
- E = CAS::E_CONSTANT.new
335
-
336
- # ___ __ _ _ _
337
- # |_ _|_ _ / _(_)_ _ (_) |_ _ _
338
- # | || ' \| _| | ' \| | _| || |
339
- # |___|_||_|_| |_|_||_|_|\__|\_, |
340
- # |__/
341
- class INFINITY_CONSTANT < CAS::Constant
342
- def initialize
343
- @x = (1.0/0.0)
344
- end
345
-
346
- def to_s
347
- "∞"
348
- end
349
-
350
- def to_latex
351
- "\\infty"
352
- end
353
- end
354
- Infinity = CAS::INFINITY_CONSTANT.new
355
-
356
- # _ _ ___ __ _ _ _
357
- # | \| |___ __ _|_ _|_ _ / _(_)_ _ (_) |_ _ _
358
- # | .` / -_) _` || || ' \| _| | ' \| | _| || |
359
- # |_|\_\___\__, |___|_||_|_| |_|_||_|_|\__|\_, |
360
- # |___/ |__/
361
- class NEG_INFINITY_CONSTANT < CAS::Constant
362
- def initialize
363
- @x = -(1.0/0.0)
364
- end
365
-
366
- def to_s
367
- "-∞"
368
- end
369
-
370
- def to_latex
371
- "-\\infty"
372
- end
373
- end
374
- NegInfinity = CAS::NEG_INFINITY_CONSTANT.new
375
-
376
- # _
377
- # ___/ |
378
- # |___| |
379
- # |_|
380
- class MINUS_ONE_CONSTANT < CAS::Constant
381
- def initialize
382
- @x = -1.0
383
- end
384
-
385
- def to_s
386
- "-1"
387
- end
388
- end
389
- MinusOne = CAS::MINUS_ONE_CONSTANT.new
390
- end