Module: SyMath::Operation::Differential

Includes:
SyMath::Operation
Included in:
Value
Defined in:
lib/symath/operation/differential.rb

Defined Under Namespace

Classes: DifferentialError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SyMath::Operation

#iterate, #recurse

Class Method Details

.initializeObject

Module initialization



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
# File 'lib/symath/operation/differential.rb', line 20

def self.initialize()
  # Map of single argument functions to their derivative.
  # FIXME: Check whether this still works if the symbol a is defined?
  @@functions = {
    # Exponential and trigonometric functions
    :exp => definition(:exp),
    :ln  => lmd(1.to_m/:a.to_m, :a),
    # Trigonometric functions
    :sin => definition(:cos),
    :cos => lmd(- fn(:sin, :a), :a),
    :tan => lmd(1.to_m + fn(:tan, :a)**2, :a),
    :cot => lmd(- (1.to_m + fn(:cot, :a)**2), :a),
    :sec => lmd(fn(:sec, :a)*fn(:tan, :a), :a),
    :csc => lmd(- fn(:cot, :a.to_m)*fn(:csc, :a.to_m), :a),
    # Inverse trigonometric functions
    :arcsin => lmd(1.to_m/fn(:sqrt, 1.to_m - :a.to_m**2), :a),
    :arccos => lmd(- 1.to_m/fn(:sqrt, 1.to_m - :a.to_m**2), :a),
    :arctan => lmd(1.to_m/fn(:sqrt, 1.to_m + :a.to_m**2), :a),
    :arcsec => lmd(1.to_m/(fn(:abs, :a)*fn(:sqrt, :a.to_m**2 - 1)), :a),
    :arccsc => lmd(- 1.to_m/(fn(:abs, :a)*fn(:sqrt, :a.to_m**2 - 1)), :a),
    :arccot => lmd(- 1.to_m/(1.to_m + :a.to_m**2), :a),
    # Hyperbolic functions
    :sinh => definition(:cosh),
    :cosh => definition(:sinh),
    :tanh => lmd(fn(:sech, :a)**2, :a),
    :sech => lmd(- fn(:tanh, :a)*fn(:sech, :a), :a),
    :csch => lmd(- fn(:coth, :a)*fn(:csch, :a), :a),
    :coth => lmd(- fn(:csch, :a)**2, :a),
    # Inverse hyperbolic functions
    :arsinh => lmd(1.to_m/fn(:sqrt, :a.to_m**2 + 1), :a),
    :arcosh => lmd(1.to_m/fn(:sqrt, :a.to_m**2 - 1), :a),
    :artanh => lmd(1.to_m/(1.to_m - :a.to_m**2), :a),
    :arsech => lmd(- 1.to_m/(:a.to_m*fn(:sqrt, 1.to_m - :a.to_m**2)), :a),
    :arcsch => lmd(- 1.to_m/(fn(:abs, :a.to_m)*fn(:sqrt, :a.to_m**2 + 1)), :a),
    :arcoth => lmd(1.to_m/(1.to_m - :a.to_m**2), :a),
  }
end

Instance Method Details

#_d_wedge(exp1, exp2) ⇒ Object

Apply wedge product or ordinary product between two expressions, depending on whether or not they have vector parts.



162
163
164
165
166
# File 'lib/symath/operation/differential.rb', line 162

def _d_wedge(exp1, exp2)
  # The product operator will determine whether this is a scalar
  # or a wedge product.
  return (exp1.factors.to_a + exp2.factors.to_a).inject(:*)
end

#d(vars) ⇒ Object



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
# File 'lib/symath/operation/differential.rb', line 58

def d(vars)
  if self.is_a?(SyMath::Definition::Function)
    return d_function_def(vars)
  end

  # d(c) = 0 for constant c
  if is_constant?(vars)
    return 0.to_m
  end

  # d(v) = dv for variable v
  if vars.member?(self)
    return to_d
  end

  # d(a + b + ...) = d(a) + d(b) + ...
  if is_a?(SyMath::Sum)
    return term1.d(vars) + term2.d(vars)
  end

  # d(-a) = -d(a)
  if is_a?(SyMath::Minus)
    return -argument.d(vars)
  end

  # Product rule
  if is_a?(SyMath::Product)
    return d_product(vars)
  end

  # Fraction rule
  if is_a?(SyMath::Fraction)
    return d_fraction(vars)
  end

  # Power rule
  if is_a?(SyMath::Power)
    return d_power(vars)
  end

  # Derivative of function
  return d_function(vars)
end

#d_failureObject

Raises:



102
103
104
# File 'lib/symath/operation/differential.rb', line 102

def d_failure()
  raise DifferentialError, 'Cannot calculate differential of expression ' + to_s
end

#d_fraction(vars) ⇒ Object



113
114
115
116
117
# File 'lib/symath/operation/differential.rb', line 113

def d_fraction(vars)
  return (_d_wedge(dividend.d(vars), divisor) -
          _d_wedge(dividend, divisor.d(vars))) /
         (divisor**2)
end

#d_function(vars) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/symath/operation/differential.rb', line 142

def d_function(vars)
  if !self.is_a?SyMath::Operator
    d_failure
  end

  if name != '' and @@functions.key?(name.to_sym)
    df = @@functions[name.to_sym]
    dfcall = df.(args[0]).evaluate
    return _d_wedge(dfcall, args[0].d(vars))
  end

  if !definition.exp.nil?
    return definition.(*args).evaluate.d(vars)
  end

  d_failure
end

#d_function_def(vars) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/symath/operation/differential.rb', line 128

def d_function_def(vars)
  if name != '' and @@functions.key?(name.to_sym)
    df = @@functions[name.to_sym]
    dfcall = df.(args[0]).evaluate
    return _d_wedge(dfcall, args[0].d(vars))
  end

  if !exp.nil?
    return self.(*args).evaluate.d(vars)
  end

  d_failure
end

#d_power(vars) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/symath/operation/differential.rb', line 119

def d_power(vars)
  if (exponent.is_constant?(vars))
    return _d_wedge(_d_wedge(exponent, base**(exponent - 1)), base.d(vars))
  else
    return _d_wedge(_d_wedge(self, fn(:ln, base)), exponent.d(vars)) +
      _d_wedge(_d_wedge(exponent, base**(exponent - 1)), base.d(vars))
  end
end

#d_product(vars) ⇒ Object

For simplicity, just use wedge products all the time. They will be normalized to scalar products afterwards.



108
109
110
111
# File 'lib/symath/operation/differential.rb', line 108

def d_product(vars)
  return (_d_wedge(factor1.d(vars), factor2) +
          _d_wedge(factor1, factor2.d(vars)))
end