[go: up one dir, main page]

Menu

[e5459b]: / lib / finita / equation.rb  Maximize  Restore  History

Download this file

139 lines (127 with data), 3.7 kB

  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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require "symbolic"
require "finita/common"
require "finita/symbolic"
require "finita/domain"
module Finita
class Decomposition < Hash
def initialize(expression, unknowns)
super()
@expression = expression
@unknowns = unknowns
decompose
end
def linear?; @linear end
private
def decompose
map = {}
map.default = 0
e = Symbolic.expand(@expression)
(e.is_a?(Symbolic::Add) ? e.args : [e]).each do |t|
unknown_refs = Collector.new.apply!(t).refs.delete_if {|r| !@unknowns.include?(r.arg)}
if unknown_refs.size == 1
r = unknown_refs.to_a.first
term = ProductExtractor.new.apply!(t, r)
if term.nil?
map[nil] += t
else
map[r] += term
end
else
map[nil] += t
end
end
fc = ObjectCollector.new(Field)
map.each do |r,e|
fc.apply!(self[r] = e)
end
@linear = true
fc.objects.each do |f|
if @unknowns.include?(f)
@linear = false
break
end
end
end
end # Decomposition
class Binding
class Algebraic < Binding
def type
TypeInferer.new.apply!(unknown)
end
def decomposition(unknowns)
@decomposition[unknowns].nil? ? @decomposition[unknowns] = Decomposition.new(equation, unknowns) : @decomposition[unknowns]
end
# def equation()
# def assignment()
def code(problem_code)
Code.new(self, problem_code)
end
class Code < Finita::Code
def entities
super.concat([unknown_code, domain_code])
end
def initialize(binding, problem_code)
@binding = Finita.check_type(binding, Binding)
@problem_code = Finita.check_type(problem_code, Problem::Code)
super("#{problem_code.type}Binding")
@unknown_code = @binding.unknown.code(problem_code)
@domain_code = @binding.domain.code(problem_code)
end
attr_reader :problem_code
def hash
@binding.hash # TODO
end
def ==(other)
equal?(other) || self.class == other.class && @binding == other.instance_variable_get(:@binding)
end
alias :eql? :==
attr_reader :unknown_code
attr_reader :domain_code
end
end # Algebraic
attr_reader :expression, :unknown, :domain
def merge?; @merge end
def initialize(expression, unknown, domain, merge)
raise "unknown must be a Field instance" unless unknown.is_a?(Field)
@expression = Symbolic.coerce(expression)
@unknown = unknown
@domain = domain # TODO type check
@merge = merge
@decomposition = {}
end
def new_algebraic(expression, domain)
self.class::Algebraic.new(expression, unknown, domain, merge?)
end
end # Binding
# Equation of form expression=field, where expression might be a function of unknown
class Assignment < Binding
class Algebraic < Binding::Algebraic
def assignment
expression
end
def equation
expression - Ref.new(unknown)
end
end # Algebraic
def initialize(expression, unknown, domain)
System.current.equations << self
# unknown <- expression
super(expression, unknown, domain, false)
end
end # Assignment
# Equation of form expression=0 with expression being a function of unknown
class Equation < Binding
class Algebraic < Binding::Algebraic
def assignment
raise "conversion equation --> assignment is not yet implemented"
end
def equation
expression
end
end # Algebraic
def initialize(lhs, unknown, domain, merge = false)
System.current.equations << self
super
end
end # Equation
end # Finita