-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mr.CAS.rb
96 lines (89 loc) · 3.44 KB
/
Mr.CAS.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
# Copyright (c) 2016 Matteo Ragni
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
##
# Mr.CAS
# A minmalistic CAS engine with encapsuled graph
# representation. This will make impossible to
# perform complex high level simplifications, but
# it is powerful enough to define simple algorithm
# in a symbolic way.
#
# Mathematically, this is an implementation of the
# forward chain rule for automatic differentiaition.
# Each function is a container of function and the
# derivation is in the form:
#
# ```
# d(f(g(x))
# --------- = g'(x) * f'(g(x))
# dx
# ```
#
# Author:: Matteo Ragni (mailto:[email protected])
# Copyright:: Copyright (c) 2016 Matteo Ragni
# License:: Distributed under MIT license terms
module CAS
# Support functions are in this separate Helper class
module Help
# Check input `obj.class` against a `type` class
# raises an `ArgumentError` if check fails
#
# * **argument**: object to be cecked
# * **argument**: type to be checked against
# * **returns**: `TrueClass`, or raises an `ArgumentError`
def self.assert(obj, type)
raise ArgumentError, "required #{type}, received #{obj.class}" unless obj.is_a? type
return true
end
# Check if input object is feasible to be a name of a `CAS::Variable` or a `CAS::Function`
# raise an `ArgumentError` if the check fails. To be feasible the object must be a `String`
# instance or `Symbol` instance
#
# * **argument**: object to be checked
# * **returns**: `TrueClass` or raises `ArgumentError`
def self.assert_name(obj)
raise ArgumentError, "Input name must be a String/Symbol" unless [Symbol, String].include? obj.class
return true
end
end
end
# ___ _
# | _ \___ __ _ _ _(_)_ _ ___ ___
# | / -_) _` | || | | '_/ -_|_-<
# |_|_\___\__, |\_,_|_|_| \___/__/
# |_|
%w|operators/op.rb operators/bary-op.rb operators/nary-op.rb
numbers/constants.rb numbers/variables.rb numbers/functions.rb
functions/fnc-sum.rb functions/fnc-prod.rb
functions/fnc-base.rb functions/fnc-trig.rb functions/fnc-trsc.rb
functions/fnc-conditions.rb functions/fnc-box-conditions.rb functions/fnc-piecewise.rb
overloading/fixnum.rb overloading/float.rb
version.rb|.each do |r|
require File.expand_path(r, File.dirname(__FILE__))
end
module CAS
CAS::NumericToConst[-Math::PI] = (-CAS::Pi)
CAS::NumericToConst[-Math::E] = (-CAS::E)
CAS::NumericToConst[(-1.0/0.0)] = (CAS::NegInfinity)
end