Class: SyMath::Definition::Function

Inherits:
Operator show all
Defined in:
lib/symath/definition/function.rb

Direct Known Subclasses

Abs, Arccos, Arccot, Arccsc, Arcsec, Arcsin, Arctan, Exp, Fact, Lmd, Ln, Sqrt, Trig

Constant Summary collapse

@@not_mentioned_funcs =
{
  :+   => true,
  :-   => true,
  :*   => true,
  :/   => true,
  :**  => true,
  :'=' => true,
}

Instance Attribute Summary

Attributes inherited from Operator

#args, #exp

Attributes inherited from SyMath::Definition

#description, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operator

#<=>, #==, #arity, #call, #compose_with_simplify, #dump, #evaluate_call, #initialize, #is_operator?, operators, #replace, #to_latex, #to_s, #validate_args

Methods inherited from SyMath::Definition

#<=>, #==, #arity, define, defined?, definitions, get, #hash, #initialize, #inspect, #is_constant?, #is_operator?, #replace, #to_latex, #to_s, undefine, #variables

Methods inherited from Value

#*, #**, #+, #-, #-@, #/, #<, #<=, #<=>, #>, #>=, #^, #add, #base, compose_with_simplify, create, #deep_clone, #div, #dump, #evaluate, #exponent, #factors, #inspect, #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

This class inherits a constructor from SyMath::Definition::Operator

Class Method Details

.functionsObject



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

def self.functions()
  return self.definitions.select { |f|
    f.is_function? and !@@not_mentioned_funcs[f.name]
  }
end

.init_builtinObject



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
# File 'lib/symath/definition/function.rb', line 5

def self.init_builtin()
  # Define the builtin functions
  SyMath::Definition::Sqrt.new
  SyMath::Definition::Sin.new
  SyMath::Definition::Cos.new
  SyMath::Definition::Tan.new
  SyMath::Definition::Sec.new
  SyMath::Definition::Csc.new
  SyMath::Definition::Cot.new
  SyMath::Definition::Arcsin.new
  SyMath::Definition::Arccos.new
  SyMath::Definition::Arctan.new
  SyMath::Definition::Arcsec.new
  SyMath::Definition::Arccsc.new
  SyMath::Definition::Arccot.new
  SyMath::Definition::Ln.new
  SyMath::Definition::Exp.new
  SyMath::Definition::Abs.new
  SyMath::Definition::Fact.new

  # Functions defined by an expression
  expressions = [
    { :name => 'sinh',
      :exp  => '(e**x - e**-x)/2',
      :desc => 'hyperbolic sine',
    },
    { :name => 'cosh',
      :exp  => '(e**x + e**-x)/2',
      :desc => 'hyperbolic cosine',
    },
    { :name => 'tanh',
      :exp  => '(e**x - e**-x)/(e**x + e**-x)',
      :desc => 'hyperbolic tangent',
    },
    { :name => 'coth',
      :exp  => '(e**x + e**-x)/(e**x - e**-x)',
      :desc => 'hyperbolic cotangent',
    },
    { :name => 'sech',
      :exp  => '2/(e**x + e**-x)',
      :desc => 'hyperbolic secant',
    },
    { :name => 'csch',
      :exp  => '2/(e**x - e**-x)',
      :desc => 'hyperbolic cosecant',
    },
    { :name => 'arsinh',
      :exp  => 'ln(x + sqrt(x**2 + 1))',
      :desc => 'inverse hyperbolic sine',
    },
    { :name => 'arcosh',
      :exp  => 'ln(x + sqrt(x**2 - 1))',
      :desc => 'inverse hyperbolic cosine',
    },
    { :name => 'artanh',
      :exp  => 'ln((1 + x)/(1 - x))/2',
      :desc => 'inverse hyperbolic tangent',
    },
    { :name => 'arcoth',
      :exp  => 'ln((x + 1)/(x - 1))/2',
      :desc => 'inverse hyperbolic cotangent',
    },
    { :name => 'arsech',
      :exp  => 'ln((1/x + sqrt(x**-2 - 1)))',
      :desc => 'inverse hyperbolic secant',
    },
    { :name => 'arcsch',
      :exp  => 'ln((1/x + sqrt(x**-2 + 1)))',
      :desc => 'inverse hyperbolic cosecant',
    },
  ]

  expressions.each do |e|
    self.new(e[:name], args: [:x], exp: e[:exp],
             description: "#{e[:name]}(x) - #{e[:desc]}")
  end
end

Instance Method Details

#check_pi_fraction(e, im) ⇒ Object

Check if expression is a constant fraction of pi and optionally i (imaginary unit)



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/symath/definition/function.rb', line 114

def check_pi_fraction(e, im)
  gotpi = false
  gotim = !im
  c = 1
  dc = 1

  # Check that factors are only constant, divisor constant and pi.
  # Note: This code is similar to 'reduce_constant_factors'. Refactor?
  e.factors.each do |f|
    if f.is_divisor_factor?
      if f.base.is_number?
        dc *= f.base.value**f.exponent.argument.value
        next
      end
    end

    if f.is_negative_number?
      c *= - f.argument.value
      next
    end

    if f.is_number?
      c *= f.value
      next
    end

    if !gotpi and f == :pi
      gotpi = true
      next
    end

    if !gotim and f == :i
      gotim = true
      next
    end

    return nil
  end

  return nil if !gotpi and !gotim and c != 0

  return c, dc
end

#is_function?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/symath/definition/function.rb', line 158

def is_function?()
  return true
end

#latex_formatObject



162
163
164
# File 'lib/symath/definition/function.rb', line 162

def latex_format()
  return "#{name}(%s)"
end

#reduce_call(c, reductions = nil) ⇒ Object



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

def reduce_call(c, reductions = nil)
  if reductions.nil?
    reductions = @reductions
  end

  if reductions.has_key?(c.args[0])
    return reductions[c.args[0]]
  end

  return c
end