Class: SyMath::Definition

Inherits:
Value
  • Object
show all
Defined in:
lib/symath/definition.rb

Direct Known Subclasses

Constant, Number, Operator, Variable

Defined Under Namespace

Classes: Abs, Arccos, Arccot, Arccsc, Arcsec, Arcsin, Arctan, Bounds, CoDiff, Constant, Cos, Cot, Csc, Curl, D, Div, Exp, Fact, Flat, Function, Grad, Hodge, Int, Laplacian, Lmd, Ln, Number, Operator, Sec, Sharp, Sin, Sqrt, Tan, Trig, Variable, Xd

Constant Summary collapse

@@definitions =
{}
@@skip_method_def =
{
  :+   => true,
  :-   => true,
  :*   => true,
  :/   => true,
  :**  => true,
  :'=' => true,
  :op  => true,
  :fn  => true,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#*, #**, #+, #-, #-@, #/, #<, #<=, #>, #>=, #^, #add, #base, compose_with_simplify, create, #deep_clone, #div, #dump, #evaluate, #exponent, #factors, #inv, #is_divisor_factor?, #is_finite?, #is_nan?, #is_negative?, #is_negative_number?, #is_number?, #is_positive?, #is_prod_exp?, #is_sum_exp?, #is_unit_quaternion?, #is_zero?, #mul, #neg, #power, #reduce, #reduce_modulo_sign, #sign, #sub, #terms, #to_m, #type, #wedge

Methods included from Operation::Exterior

#flat, #hodge, #sharp

Methods included from Operation::Integration

#anti_derivative, #get_linear_constants, initialize, #int_constant, #int_failure, #int_function, #int_inv, #int_pattern, #int_power, #int_product, #int_sum, #integral_bounds

Methods included from Operation::Differential

#_d_wedge, #d, #d_failure, #d_fraction, #d_function, #d_function_def, #d_power, #d_product, initialize

Methods included from Operation

#iterate, #recurse

Methods included from Operation::DistributiveLaw

#combfrac_add_term, #combfrac_sum, #combine_fractions, #expand, #expand_product, #expand_single_pass, #factorize, #factorize_integer_poly, #factorize_simple, #has_fractional_terms?

Methods included from Operation::Normalization

#combine_factors, #compare_factors_and_swap, #normalize, #normalize_matrix, #normalize_power, #normalize_product, #normalize_single_pass, #normalize_sum, #order_product, #product_on_fraction_form, #reduce_constant_factors, #replace_combined_factors, #swap_factors

Methods included from Operation::Match

#build_assoc_op, #match, #match_assoc, #match_replace

Constructor Details

#initialize(name, define_symbol: true, description: nil) ⇒ Definition

Returns a new instance of Definition.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/symath/definition.rb', line 98

def initialize(name, define_symbol: true, description: nil)
  @name = name.to_sym

  # Create a method for the definition if it's not a number or lambda
  if define_symbol
    self.class.define(name, self)
  end

  if description.nil?
    @description = self.to_s
  else
    @description = description
  end
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



18
19
20
# File 'lib/symath/definition.rb', line 18

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/symath/definition.rb', line 17

def name
  @name
end

Class Method Details

.define(name, s) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/symath/definition.rb', line 57

def self.define(name, s)
  if @@definitions.has_key?(name.to_sym)
    raise "#{name} is already defined."
  end

  @@definitions[name.to_sym] = s

  # Create a method for the definition. Without arguments, the method
  # returns the definition object itself. With arguments, it returns
  # the operator/function applied to a list of arguments.
  if !SyMath::Definitions.private_method_defined?(name) and
    !SyMath::Definitions.method_defined?(name) and
    !@@skip_method_def[name.to_sym]

    SyMath::Definitions.define_method :"#{name}" do |*args|
      sym = s
      if args.length > 0
        return sym.call(*args)
      else
        return sym
      end
    end
  end
end

.defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/symath/definition.rb', line 90

def self.defined?(name)
  return @@definitions.has_key?(name.to_sym)
end

.definitionsObject



94
95
96
# File 'lib/symath/definition.rb', line 94

def self.definitions()
  return @@definitions.values
end

.get(name) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/symath/definition.rb', line 49

def self.get(name)
  if !@@definitions.has_key?(name.to_sym)
    raise "#{name} is not defined."
  end

  return @@definitions[name.to_sym]
end

.init_builtinObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/symath/definition.rb', line 33

def self.init_builtin()
  # Create the builtin algebraic functions. The constructor will
  # define the functions so they can be used in expressions.
  SyMath::Definition::Function.new(:+)
  SyMath::Definition::Function.new(:-)
  SyMath::Definition::Function.new(:*)
  SyMath::Definition::Function.new(:/)
  SyMath::Definition::Function.new(:**)
  SyMath::Definition::Function.new(:^)
  SyMath::Definition::Function.new(:'=')

  SyMath::Definition::Constant.init_builtin
  SyMath::Definition::Function.init_builtin
  SyMath::Definition::Operator.init_builtin
end

.undefine(name) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/symath/definition.rb', line 82

def self.undefine(name)
  if !@@definitions.has_key?(name.to_sym)
    raise "#{name} is not undefined."
  end

  @@definitions.delete(name.to_sym)
end

Instance Method Details

#<=>(other) ⇒ Object

FIXME: Identical to operator comparison



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/symath/definition.rb', line 152

def <=>(other)
  if self.class.name != other.class.name
    return super(other)
  end

  if name != other.name
    return name.to_s <=> other.name.to_s
  end

  return 0
end

#==(other) ⇒ Object Also known as: eql?



141
142
143
144
145
146
# File 'lib/symath/definition.rb', line 141

def ==(other)
  o = other.to_m
  return false if self.class.name != o.class.name
  return false if @name.to_s != o.name.to_s
  return true
end

#arityObject



133
134
135
# File 'lib/symath/definition.rb', line 133

def arity()
  return 0
end

#hashObject



137
138
139
# File 'lib/symath/definition.rb', line 137

def hash()
  return @name.hash
end

#inspectObject



183
184
185
186
187
188
189
# File 'lib/symath/definition.rb', line 183

def inspect()
  if SyMath.setting(:inspect_to_s)
    return to_s
  else
    return super.inspect
  end
end

#is_constant?(vars = nil) ⇒ Boolean

is_self_adjoint is_additive is_homogenous is_conjugate_homogenous is_linear (additive + homogenous) is_antilinear (additive + conjugate_homogenous)

Returns:

  • (Boolean)


171
172
173
# File 'lib/symath/definition.rb', line 171

def is_constant?(vars = nil)
  return true
end

#is_function?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/symath/definition.rb', line 125

def is_function?()
  return false
end

#is_operator?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/symath/definition.rb', line 129

def is_operator?()
  return false
end

#reduce_call(c) ⇒ Object



113
114
115
# File 'lib/symath/definition.rb', line 113

def reduce_call(c)
  return c
end

#replace(map) ⇒ Object



121
122
123
# File 'lib/symath/definition.rb', line 121

def replace(map)
  return self
end

#to_latexObject



179
180
181
# File 'lib/symath/definition.rb', line 179

def to_latex()
  return "#{name}"
end

#to_sObject



175
176
177
# File 'lib/symath/definition.rb', line 175

def to_s()
  return @name.to_s
end

#variablesObject



117
118
119
# File 'lib/symath/definition.rb', line 117

def variables()
  return []
end