| 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
# 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
# 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
# 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
# File lib/rapfp/Apc.rb, line 76
76: def ==(other)
77: (@re == other.re) && (@im == other.im)
78: end
# 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
# File lib/rapfp/Apc.rb, line 205
205: def acos
206: Apc.new(@@rconst.pi / @@rconst.two,@@rconst.zero) - self.asin
207: end
# 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
# 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
# File lib/rapfp/Apc.rb, line 211
211: def asinh
212: (self + (self * self + @@cconst.one).sqrt).log
213: end
# 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
# File lib/rapfp/Apc.rb, line 217
217: def atanh
218: @@cconst.half * ((@@cconst.one + self).log - (@@cconst.one - self).log)
219: end
# 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
# 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
# File lib/rapfp/Apc.rb, line 60
60: def display_val(label)
61: # puts "#{label} = #{self.to_s}"
62: end
# File lib/rapfp/Apc.rb, line 79
79: def eq(other)
80: (@re.eq(other.re)) && (@im.eq(other.im))
81: end
# 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
# 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
# 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