Class: Plurimath::Math::Function::Frac

Inherits:
BinaryFunction show all
Defined in:
lib/plurimath/math/function/frac.rb

Constant Summary collapse

FUNCTION =
{
  name: "fraction",
  first_value: "numerator",
  second_value: "denominator",
}.freeze

Constants inherited from Core

Core::ALL_PARAMETERS, Core::REPLACABLES

Instance Attribute Summary collapse

Attributes inherited from BinaryFunction

#hide_function_name, #parameter_one, #parameter_two

Instance Method Summary collapse

Methods inherited from BinaryFunction

#all_values_exist?, #any_value_exist?, #to_asciimath_math_zone, #to_html, #to_latex_math_zone, #to_mathml_math_zone, #to_omml_math_zone, #to_unicodemath_math_zone

Methods inherited from Core

#ascii_fields_to_print, #class_name, #cloned_objects, #common_math_zone_conversion, descendants, #dump_mathml, #dump_nodes, #dump_omml, #dump_ox_nodes, #empty_tag, #extract_class_name_from_text, #extractable?, #filtered_values, #font_style_t_tag, #get, #gsub_spacing, inherited, #insert_t_tag, #invert_unicode_symbols, #is_binary_function?, #is_mrow?, #is_mstyle?, #is_nary_function?, #is_nary_symbol?, #is_ternary_function?, #is_unary?, #latex_fields_to_print, #linebreak?, #mathml_fields_to_print, #mathml_nodes, #mini_sized?, #msty_tag_with_attrs, #nary_attr_value, #nary_intent_name, #omml_fields_to_print, #omml_nodes, #omml_parameter, #omml_tag_name, #ox_element, #paren?, #pretty_print_instance_variables, #prime_unicode?, #r_element, #replacable_values, #result, #separate_table, #set, #symbol?, #tag_name, #to_ms_value, #unicodemath_fields_to_print, #unicodemath_parens, #updated_object_values, #validate_function_formula, #validate_mathml_fields, #variable_value, #variables

Constructor Details

#initialize(parameter_one = nil, parameter_two = nil, options = {}) ⇒ Frac

Returns a new instance of Frac.



15
16
17
18
19
20
# File 'lib/plurimath/math/function/frac.rb', line 15

def initialize(parameter_one = nil,
               parameter_two = nil,
               options = {})
  super(parameter_one, parameter_two)
  @options = options if options && !options&.empty?
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/plurimath/math/function/frac.rb', line 7

def options
  @options
end

Instance Method Details

#==(object) ⇒ Object



22
23
24
25
# File 'lib/plurimath/math/function/frac.rb', line 22

def ==(object)
  super &&
    object.options == options
end

#choose_frac(options:) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/plurimath/math/function/frac.rb', line 111

def choose_frac(options:)
  if parameter_one
    first_value = unicodemath_parens(parameter_one,
                                     options: options)
  end
  if parameter_two
    second_value = unicodemath_parens(parameter_two,
                                      options: options)
  end
  "#{first_value}#{second_value}"
end

#intent_namesObject



123
124
125
126
127
128
# File 'lib/plurimath/math/function/frac.rb', line 123

def intent_names
  {
    derivative: ":derivative",
    partial_derivative: ":partial-derivative",
  }
end

#line_breaking(obj) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/plurimath/math/function/frac.rb', line 92

def line_breaking(obj)
  parameter_one&.line_breaking(obj)
  if obj.value_exist?
    frac = self.class.new(Utility.filter_values(obj.value),
                          parameter_two)
    frac.hide_function_name = true
    obj.update(frac)
    self.parameter_two = nil
    return
  end

  parameter_two&.line_breaking(obj)
  if obj.value_exist?
    frac = self.class.new(nil, Utility.filter_values(obj.value))
    frac.hide_function_name = true
    obj.update(frac)
  end
end

#to_asciimath(options:) ⇒ Object



27
28
29
30
31
# File 'lib/plurimath/math/function/frac.rb', line 27

def to_asciimath(options:)
  first_value = "(#{parameter_one&.to_asciimath(options: options)})" if parameter_one
  second_value = "(#{parameter_two&.to_asciimath(options: options)})" if parameter_two
  "frac#{first_value}#{second_value}"
end

#to_latex(options:) ⇒ Object



53
54
55
56
57
# File 'lib/plurimath/math/function/frac.rb', line 53

def to_latex(options:)
  first_value = parameter_one&.to_latex(options: options)
  two_value = parameter_two&.to_latex(options: options)
  "\\frac{#{first_value}}{#{two_value}}"
end

#to_mathml_without_math_tag(intent, options:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/plurimath/math/function/frac.rb', line 33

def to_mathml_without_math_tag(intent, options:)
  tag_name = hide_function_name ? "mrow" : "mfrac"
  mathml_value = [
    parameter_one&.to_mathml_without_math_tag(intent, options: options),
    parameter_two&.to_mathml_without_math_tag(intent, options: options),
  ]
  frac_tag = ox_element(tag_name)
  if tag_name == "mfrac" && self.options
    frac_tag.set_attr(self.options.reject do |opt|
      opt == :choose
    end)
  end
  Utility.update_nodes(frac_tag, mathml_value)
  if intent
    update_derivative(frac_tag, mathml_value[0],
                      mathml_value[1])
  end
  intentify(frac_tag, intent, func_name: :frac, options: intent_names)
end

#to_omml_without_math_tag(display_style, options:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/plurimath/math/function/frac.rb', line 59

def to_omml_without_math_tag(display_style, options:)
  f_element = Utility.ox_element("f", namespace: "m")
  Utility.update_nodes(
    f_element,
    [
      fpr_element,
      omml_parameter(parameter_one, display_style, tag_name: "num",
                                                   options: options),
      omml_parameter(parameter_two, display_style, tag_name: "den",
                                                   options: options),
    ],
  )
end

#to_unicodemath(options:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/plurimath/math/function/frac.rb', line 73

def to_unicodemath(options:)
  return unicodemath_fraction if self.options&.dig(:unicodemath_fraction)

  if parameter_one
    first_value = unicodemath_parens(parameter_one,
                                     options: options)
  end
  if parameter_two
    second_value = unicodemath_parens(parameter_two,
                                      options: options)
  end
  return "#{first_value}/#{second_value}" unless self.options

  return "#{first_value}¦#{second_value}" if self.options&.key?(:linethickness)
  return "#{parameter_one.to_unicodemath(options: options)}#{parameter_two.to_unicodemath(options: options)}" if self.options&.key?(:displaystyle)

  "#{first_value}#{second_value}" if self.options&.key?(:ldiv)
end