Module: Rucas::Symbolic::Expr
- Defined in:
- lib/rucas/symbolic.rb,
lib/rucas/rewrite.rb,
lib/rucas/simplify.rb,
lib/rucas/symbolic.rb,
lib/rucas/symbolic.rb
Overview
Symbolic expression; subclasses represent various kinds of expressions.
Class Method Summary collapse
Instance Method Summary collapse
-
#children ⇒ Object
Children in the expression tree, if any.
-
#match(expr, bindings = {}) ⇒ Object
Non-recursive matching.
-
#precedence ⇒ Object
Operator precedence; this reflects Ruby’s built-in order-of-operations rules.
-
#rewrite(pattern, output) ⇒ Object
If this expression matches
pattern
, returnoutput
(with appropriate bindings of variables in pattern applied to output); otherwise, return self (or an object == to self). -
#simplify ⇒ Object
Return expression after algebraic simplification.
-
#to_s_paren ⇒ Object
String representation including all parentheses; by default,
to_s
omits parentheses when operator precedence rules allow. -
#value ⇒ Object
If this expression involves only constants, evaluate it and return the result; if it is not, return nil.
Class Method Details
.make(e) ⇒ Object
38 39 40 41 42 |
# File 'lib/rucas/symbolic.rb', line 38 def self.make e return e if e.is_a?(Expr) return ConstExpr.new(e) if CONST_CLASSES.any? {|t| e.is_a?(t)} raise "#{e} is not a symbolic expression" end |
Instance Method Details
#children ⇒ Object
Children in the expression tree, if any. For example, an AddExpr returns its left and right operands (x and y in x + y).
27 |
# File 'lib/rucas/symbolic.rb', line 27 def children; [] end |
#match(expr, bindings = {}) ⇒ Object
Non-recursive matching. Here, self is a pattern and expr
is the input to be matched against this pattern. Returns bindings for the free variables in self that make the match succeed, or nil if there is no such match.
54 55 56 |
# File 'lib/rucas/rewrite.rb', line 54 def match expr, bindings = {} nil # see subclasses end |
#precedence ⇒ Object
Operator precedence; this reflects Ruby’s built-in order-of-operations rules. You should not rely on the particular values; only the ordering they define is guaranteed.
32 |
# File 'lib/rucas/symbolic.rb', line 32 def precedence; 0 end |
#rewrite(pattern, output) ⇒ Object
If this expression matches pattern
, return output
(with appropriate bindings of variables in pattern applied to output); otherwise, return self (or an object == to self).
42 43 44 45 46 |
# File 'lib/rucas/rewrite.rb', line 42 def rewrite pattern, output bindings = pattern.match(self) return output.with(bindings) if bindings self end |
#simplify ⇒ Object
Return expression after algebraic simplification. Note that this isn’t a very smart simplifier.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rucas/simplify.rb', line 88 def simplify new_self = self changed = false for pattern, output in Simplify::RULES new_self = new_self.rewrite(pattern, output) #puts "#{pattern}\t#{new_self.to_s_paren}" changed = (new_self != self) break if changed end if changed then new_self.simplify else self end end |
#to_s_paren ⇒ Object
String representation including all parentheses; by default, to_s
omits parentheses when operator precedence rules allow.
36 |
# File 'lib/rucas/symbolic.rb', line 36 def to_s_paren; to_s end |
#value ⇒ Object
If this expression involves only constants, evaluate it and return the result; if it is not, return nil.
62 63 64 |
# File 'lib/rucas/rewrite.rb', line 62 def value nil # see subclasses end |