Class Apc
In: lib/rapfp/Apc.rb
Parent: Object
                                                              ###

   File:     Apc.rb

   Subject:  Class for arbitrary precision complex.

   Author:   Dennis J. Darland

   Date:     April 2, 2007

Copyright (C) 2007 Dennis J. Darland#

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Methods

*   **   +   -   /   ==   abs   acos   acosh   asin   asinh   atan   atanh   cos   cosh   display_val   eq   exp   im   log   log10   neg   new   re   setcconst   setrconst   sin   sinh   sqrt   tan   tanh   to_s  

Public Class methods

@re is real comonent @im is imaginary component

[Source]

    # File lib/rapfp/Apc.rb, line 34
34:   def initialize(re,im)
35:     @re = re
36:     @im = im
37:   end

Public Instance methods

[Source]

    # File lib/rapfp/Apc.rb, line 69
69:   def *(other)
70:     Apc.new(@re*other.re-@im*other.im,@im*other.re+@re*other.im)
71:   end

[Source]

     # File lib/rapfp/Apc.rb, line 141
141:   def **(other)
142:     a = self.clone
143:     unless other.kind_of?(Apc) #other is integer
144:       b = @@cconst.one
145:       if other > 0 then
146:         other.times do
147:           b = b.clone * a
148:         end
149:       elsif other < 0 then
150:         (-other).times do
151:           b = b.clone * a
152:         end
153:         b = @@cconst.one/b.clone
154:       end
155:       return b
156:     else
157:       if other == @@cconst.zero then
158:         return @@cconst.one
159:       elsif other == @@cconst.one then
160:         return a
161:       elsif a == @@cconst.zero then
162:         return a
163:       elsif
164:         a == @@cconst.one then
165:         return a
166:       else
167:         lg = a.log
168:         lgt = lg * other
169:         b = lgt.exp
170:         return b
171:       end
172:     end 
173:   end

[Source]

    # File lib/rapfp/Apc.rb, line 63
63:   def +(other)
64:     Apc.new(@re+other.re,@im+other.im)
65:   end

[Source]

    # File lib/rapfp/Apc.rb, line 66
66:   def -(other)
67:     Apc.new(@re-other.re,@im-other.im)
68:   end

[Source]

    # File lib/rapfp/Apc.rb, line 72
72:   def /(other)
73:     de = other.re * other.re + other.im * other.im
74:     Apc.new((@re * other.re + @im * other.im)/de ,(@im * other.re - @re * other.im)/de)
75:   end

[Source]

    # File lib/rapfp/Apc.rb, line 76
76:   def ==(other)
77:     (@re == other.re) && (@im == other.im)
78:   end

[Source]

    # File lib/rapfp/Apc.rb, line 82
82:    def abs
83:     if self == @@cconst.zero
84:       return @@rconst.zero
85:     end
86:     (@re * @re + @im * @im).sqrt
87:   end

[Source]

     # File lib/rapfp/Apc.rb, line 205
205:   def acos
206:     Apc.new(@@rconst.pi / @@rconst.two,@@rconst.zero) - self.asin
207:   end

[Source]

     # File lib/rapfp/Apc.rb, line 214
214:   def acosh
215:     (self + (self * self - @@cconst.one).sqrt).log
216:   end

def **(other)

  a = self.clone
  unless other.kind_of?(Apc)
    b = @@cconst.one.clone
    if other > 0 then
      other.times do
        b = a * b.clone
      end
      return b
    elsif other < 0
      (-other).times do
        b = a * b.clone
      end
      c = @@cconst.one.clone/b.clone
      return c
    end
    return b
  else
    b = other
    if b == (@@cconst.zero) then return @@cconst.one end
    if b == @@cconst.one then return a end
    if a == @@cconst.zero then return @@cconst.zero end
    return (a.log*b).exp
  end

end

[Source]

     # File lib/rapfp/Apc.rb, line 202
202:   def asin
203:     @@cconst.minus_i * (@@cconst.i * self+(@@cconst.one - self*self).sqrt).log
204:   end

[Source]

     # File lib/rapfp/Apc.rb, line 211
211:   def asinh
212:     (self + (self * self + @@cconst.one).sqrt).log
213:   end

[Source]

     # File lib/rapfp/Apc.rb, line 208
208:   def atan
209:     (@@cconst.i / @@cconst.two) * ((@@cconst.one - self * @@cconst.i).log - (@@cconst.one + self * @@cconst.i).log)
210:   end

[Source]

     # File lib/rapfp/Apc.rb, line 217
217:   def atanh
218:     @@cconst.half * ((@@cconst.one + self).log - (@@cconst.one - self).log)
219:   end

[Source]

     # File lib/rapfp/Apc.rb, line 97
 97:   def cos
 98:     y1 = (self * @@cconst.i).exp
 99:     y2 = (self * @@cconst.minus_i).exp
100:     (y1+y2) / @@cconst.two
101:   end

[Source]

     # File lib/rapfp/Apc.rb, line 110
110:   def cosh
111:     y1 = self.exp
112:     y2 = (self * @@cconst.minus_one).exp
113:     (y1+y2) / @@cconst.two
114:   end

[Source]

    # File lib/rapfp/Apc.rb, line 60
60:   def display_val(label)
61: #    puts "#{label} = #{self.to_s}"
62:   end

[Source]

    # File lib/rapfp/Apc.rb, line 79
79:    def eq(other)
80:      (@re.eq(other.re)) && (@im.eq(other.im))
81:    end

[Source]

    # File lib/rapfp/Apc.rb, line 88
88:   def exp
89:     w = @re.exp
90:     Apc.new(w*@im.cos,w*@im.sin)
91:   end

[Source]

    # File lib/rapfp/Apc.rb, line 41
41:   def im
42:     @im
43:   end

[Source]

     # File lib/rapfp/Apc.rb, line 118
118:   def log
119:     dd = self.abs
120: # puts "cx log dd = " + dd.to_s
121:     if dd > @@rconst.zero then
122:       lg = dd.log
123: # puts "cx log lg = dd.log = " + lg.to_s
124:     else
125:       lg = @@rconst.zero #really error!
126:     end
127:     if self.re == @@rconst.zero then 
128:       if self.im. > @@rconst.zero then 
129:         ang = @@rconst.pi/@@rconst.two
130:       else  
131:         ang = @@rconst.pi/@@rconst.minus_two 
132:       end
133:     else 
134:       ang = (self.im / self.re).atan 
135:     end
136:     return Apc.new(lg,ang)
137:   end

[Source]

     # File lib/rapfp/Apc.rb, line 138
138:   def log10
139:     self.log/@@cconst.ten.log
140:   end

[Source]

    # File lib/rapfp/Apc.rb, line 50
50:   def neg
51:     Apc.new(@re.neg,@im.neg)
52:   end

[Source]

    # File lib/rapfp/Apc.rb, line 38
38:   def re
39:     @re
40:   end

[Source]

    # File lib/rapfp/Apc.rb, line 44
44:   def setcconst(c)
45:     @@cconst = c
46:   end

[Source]

    # File lib/rapfp/Apc.rb, line 47
47:   def setrconst(c)
48:     @@rconst = c
49:   end

[Source]

    # File lib/rapfp/Apc.rb, line 92
92:   def sin
93:     y1 = (self * @@cconst.i).exp
94:     y2 = (self * @@cconst.minus_i).exp
95:     (y1-y2) / @@cconst.two_i
96:   end

[Source]

     # File lib/rapfp/Apc.rb, line 105
105:   def sinh
106:     y1 = self.exp
107:     y2 = (self * @@cconst.minus_one).exp
108:     (y1-y2) / @@cconst.two
109:   end

[Source]

     # File lib/rapfp/Apc.rb, line 174
174:   def sqrt
175:     self ** @@cconst.half
176:   end

[Source]

     # File lib/rapfp/Apc.rb, line 102
102:   def tan
103:     self.sin/self.cos
104:   end

[Source]

     # File lib/rapfp/Apc.rb, line 115
115:   def tanh
116:     self.sinh/self.cosh
117:   end

[Source]

    # File lib/rapfp/Apc.rb, line 53
53:   def to_s
54:     if @im > @@rconst.zero then
55:       "#{@re.to_s} + #{@im.to_s}I"
56:     else
57:       "#{@re.to_s} - #{@im.neg.to_s}I"
58:     end
59:   end

[Validate]