Class: Flt::ComplexContext

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/flt/complex.rb

Overview

consider: < Num::ContextBase

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ComplexContext

Returns a new instance of ComplexContext.



67
68
69
# File 'lib/flt/complex.rb', line 67

def initialize(context)
  @context = context
end

Class Method Details

.math_function(mth, negative_arg_to_complex = false, extra_prec = 3, &blk) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/flt/complex.rb', line 91

def self.math_function(mth, negative_arg_to_complex=false, extra_prec=3, &blk)
  class_eval do
    define_method mth do |*args|
      is_complex = args.detect{|z| z.kind_of?(Complex)}
      if is_complex || (negative_arg_to_complex && args.first<0)
        num_class.context(:extra_precision=>extra_prec) do |mth|
          #Complex.rectangular *blk[mth, *args].map{|v| @context.plus(v)}
          Complex.rectangular *instance_exec(mth,*args,&blk).map{|v| @context.plus(v)}
        end
      else
        @context.send(mth, *args) # Num(@context.send(mth, *args)) ?
      end
    end
  end
end

Instance Method Details

#abs(z) ⇒ Object



87
88
89
# File 'lib/flt/complex.rb', line 87

def abs(z)
  z.abs
end

#acos(z) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/flt/complex.rb', line 214

def acos(z)
  z_is_complex = z.kind_of?(Complex)
  if z_is_complex || z.abs>1
    # z = Complex(1) unless z_is_complex
    i = Complex(0,@context.num_class[1])
    fix num_class.context(:extra_precision=>3).cmath {
      -i*ln(z + i*sqrt(1-z*z))
    }
  else
    @context.acos(z)
  end
end

#acosh(z) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/flt/complex.rb', line 227

def acosh(z)
  z_is_complex = z.kind_of?(Complex)
  if z_is_complex || z<=1
    # z = Complex(1) unless z_is_complex
    fix num_class.context(:extra_precision=>3).cmath{ ln(z + sqrt(z*z-1)) }
  else
    @context.acosh(z)
  end
end

#asin(z) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/flt/complex.rb', line 201

def asin(z)
  z_is_complex = z.kind_of?(Complex)
  if z_is_complex || z.abs>1
    # z = Complex(1) unless z_is_complex
    i = Complex(0,@context.num_class[1])
    fix num_class.context(:extra_precision=>3).cmath {
      -i*ln(i*z + sqrt(1-z*z))
    }
  else
    @context.asin(z)
  end
end

#atanh(z) ⇒ Object



237
238
239
240
241
242
243
244
245
246
# File 'lib/flt/complex.rb', line 237

def atanh(z)
  z_is_complex = z.kind_of?(Complex)
  if z_is_complex || z.abs>1
    # z = Complex(1) unless z_is_complex
    i = Complex(0,@context.num_class[1])
    fix num_class.context(:extra_precision=>3).cmath{ num_class.one_half*ln((1+z)/(1-z)) }
  else
    @context.atanh(z)
  end
end

#mathObject



75
76
77
# File 'lib/flt/complex.rb', line 75

def math
  num_class.context
end

#Num(z) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/flt/complex.rb', line 79

def Num(z)
  if z.kind_of?(Complex)
    Complex.rectangular(*z.rectangular.map{|v| @context.num_class[v]})
  else
    Complex.rectangular(@context.num_class[z])
  end
end

#num_classObject



71
72
73
# File 'lib/flt/complex.rb', line 71

def num_class
  @context.num_class
end

#sqrt(z) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/flt/complex.rb', line 146

def sqrt(z)
  z_is_complex = z.kind_of?(Complex)
  if z_is_complex || z<0
    if z_is_complex
      re = im = nil
      num_class.context(:extra_precision=>3) do |mth|
        z = Num(z)
        i_sign = z.imag.sign
        r = abs(z)
        x = z.real
        re = ((r+x)/2).sqrt
        im = ((r-x)/2).sqrt.copy_sign(i_sign)
      end
      fix_rect(re, im)
    else
      Complex(0, @context.sqrt(-z))
    end
  else
    @context.sqrt(z)
  end
end