/test/language/expanded/aux_ac_complex.e
Specman e | 166 lines | 117 code | 25 blank | 24 comment | 2 complexity | f3ea8fbc6c284c129a545e8a537eb126 MD5 | raw file
1-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries. 2-- See the Copyright notice at the end of this file. 3-- 4expanded class AUX_AC_COMPLEX 5 6insert 7 NUMERIC 8 redefine out 9 end 10 11feature {ANY} 12 is_equal (other: like Current): BOOLEAN 13 do 14 Result := standard_is_equal(other) 15 end 16 17 is_near_equal (other: like Current): BOOLEAN 18 do 19 Result := (re - other.re).abs * 2.0 ^ 45 < re.abs and (im - other.im).abs * 2.0 ^ 45 < im.abs 20 end 21 22 re: REAL 23 24 im: REAL 25 26 make (r, i: REAL): like Current 27 do 28 Result.set_item(r, i) 29 end 30 31 set_item (r, i: REAL) 32 do 33 re := r 34 im := i 35 end 36 37 infix "+" (other: like Current): like Current 38 do 39 Result.set_item(re + other.re, im + other.im) 40 end 41 42 infix "-" (other: like Current): like Current 43 do 44 Result.set_item(re - other.re, im - other.im) 45 end 46 47 infix "*" (other: like Current): like Current 48 do 49 Result.set_item(re * other.re - im * other.im, re * other.im + im * other.re) 50 end 51 52 infix "/" (other: like Current): like Current 53 -- The ratio of self and `other'. 54 -- From Numerical Recipes in C, 2nd ed. p. 949. 55 local 56 r, den: REAL 57 do 58 if other.re.abs >= other.im.abs then 59 r := other.im / other.re 60 den := other.re + r * other.im 61 Result.set_item((re + r * im) / den, (im - r * re) / den) 62 else 63 r := other.re / other.im 64 den := other.im + r * other.re 65 Result.set_item((re * r + im) / den, (im * r - re) / den) 66 end 67 end 68 69 prefix "-": like Current 70 do 71 Result.set_item(-re, -im) 72 end 73 74 prefix "+": like Current 75 do 76 Result.set_item(re, im) 77 end 78 79 valid_divisor (other: like Current): BOOLEAN 80 do 81 Result := other /= zero 82 end 83 84 sign: INTEGER_8 85 do 86 check 87 False 88 end 89 end 90 91 divisible (other: like Current): BOOLEAN 92 do 93 Result := re /= 0 or else im /= 0 94 end 95 96 one: like Current 97 do 98 Result.set_item(re.one, im.zero) 99 end 100 101 zero: like Current 102 do 103 Result.set_item(re.zero, im.zero) 104 end 105 106 plus_scalar (scalar: REAL): like Current 107 do 108 Result.set_item(re + scalar, im) 109 end 110 111 times_scalar (scalar: REAL): like Current 112 do 113 Result.set_item(re * scalar, im * scalar) 114 end 115 116 exp: like Current 117 -- The complex exponential `e^self'. 118 local 119 rp: REAL 120 do 121 rp := re.exp 122 Result.set_item(rp * im.cos, rp * im.sin) 123 end 124 125 out: STRING 126 do 127 create Result.make(24) 128 Result.append("(") 129 Result.append(re.out) 130 Result.append(", ") 131 Result.append(im.out) 132 Result.append(")") 133 ensure 134 not Result.is_empty 135 end 136 137 set_item_numeric (other: like Current) 138 do 139 set_item(other.re, other.im) 140 end 141 142feature {ANY} 143 hash_code: INTEGER 144 do 145 Result := re.hash_code + im.hash_code 146 end 147 148end -- class AUX_AC_COMPLEX 149-- 150-- ------------------------------------------------------------------------------------------------------------------------------ 151-- Copyright notice below. Please read. 152-- 153-- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, 154-- as published by the Free Software Foundation; either version 2, or (at your option) any later version. 155-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty 156-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have 157-- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free 158-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 159-- 160-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE 161-- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE 162-- 163-- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN 164-- 165-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr 166-- ------------------------------------------------------------------------------------------------------------------------------