6 #include "cln/number.h"
7 #include "cln/complex.h"
8 #include "cln/sfloat_class.h"
9 #include "cln/ffloat_class.h"
10 #include "cln/dfloat_class.h"
11 #include "cln/lfloat_class.h"
12 #include "base/cl_macros.h"
13 #include "cln/malloc.h"
17 struct cl_heap_complex : cl_heap {
22 inline cl_heap_complex* TheComplex (cl_heap_complex* p)
24 inline cl_heap_complex* TheComplex (const cl_number& obj)
25 { return (cl_heap_complex*)(obj.pointer); }
27 inline cl_heap_complex* allocate_complex (const cl_R& real, const cl_R& imag)
29 cl_heap_complex* p = (cl_heap_complex*) malloc_hook(sizeof(cl_heap_complex));
31 p->type = &cl_class_complex;
32 p->realpart.pointer = real.pointer; cl_inc_refcount(real);
33 p->imagpart.pointer = imag.pointer; cl_inc_refcount(imag);
37 // Private constructor.
38 // ptr should be the result of some allocate_complex() call.
39 inline cl_N::cl_N (cl_heap_complex* ptr)
40 : cl_number ((cl_private_thing) ptr) {}
42 // Both work, but the first definition results in less compiler-generated
45 #define Complex cl_heap_complex*
51 inline bool realp (const cl_N& x)
54 if (x.heappointer->type == &cl_class_complex)
58 inline bool complexp (const cl_N& x)
61 if (x.heappointer->type == &cl_class_complex)
66 // Comparison with a fixnum.
67 inline bool eq (const cl_N& x, sint32 y)
69 return x.word == cl_combine(cl_FN_tag,y);
72 inline bool exact_zerop (const cl_N& x)
78 // A complex (cl_C) is a number which is not a real number (cl_R).
81 class cl_C : public cl_N {
85 inline bool realp (const cl_C& x)
86 { cl_unused x; return false; }
87 inline bool complexp (const cl_C& x)
88 { cl_unused x; return true; }
91 // Liefert zu reellen Zahlen a und b /= Fixnum 0 die komplexe Zahl a+bi.
93 extern const cl_N complex_C (const cl_R& a, const cl_R& b);
95 // realpart(x) liefert den Realteil der Zahl x.
96 // imagpart(x) liefert den Imaginärteil der Zahl x.
97 inline const cl_R& realpart (const cl_C& x)
99 return TheComplex(x)->realpart;
101 inline const cl_R& imagpart (const cl_C& x)
103 return TheComplex(x)->imagpart;
107 // Primitive forms of complex numbers with restricted part type.
113 cl_C_SF (const cl_SF& re, const cl_SF& im) : realpart(re), imagpart(im) {}
115 inline const cl_N complex_C (const cl_C_SF& c)
116 { return complex_C(c.realpart,c.imagpart); }
122 cl_C_FF (const cl_FF& re, const cl_FF& im) : realpart(re), imagpart(im) {}
124 inline const cl_N complex_C (const cl_C_FF& c)
125 { return complex_C(c.realpart,c.imagpart); }
131 cl_C_DF (const cl_DF& re, const cl_DF& im) : realpart(re), imagpart(im) {}
133 inline const cl_N complex_C (const cl_C_DF& c)
134 { return complex_C(c.realpart,c.imagpart); }
140 cl_C_LF (const cl_LF& re, const cl_LF& im) : realpart(re), imagpart(im) {}
142 inline const cl_N complex_C (const cl_C_LF& c)
143 { return complex_C(c.realpart,c.imagpart); }
145 // cl_C_recip(a,b) liefert 1/(a+bi), wo a und b Short-Floats sind.
146 extern const cl_C_SF cl_C_recip (const cl_SF& a, const cl_SF& b);
148 // cl_C_recip(a,b) liefert 1/(a+bi), wo a und b Single-Floats sind.
149 extern const cl_C_FF cl_C_recip (const cl_FF& a, const cl_FF& b);
151 // cl_C_recip(a,b) liefert 1/(a+bi), wo a und b Double-Floats sind.
152 extern const cl_C_DF cl_C_recip (const cl_DF& a, const cl_DF& b);
154 // cl_C_recip(a,b) liefert 1/(a+bi), wo a und b gleichlange Long-Floats sind.
155 extern const cl_C_LF cl_C_recip (const cl_LF& a, const cl_LF& b);
158 // cl_C_hypot(a,b) liefert abs(a+bi), wo a und b Short-Floats sind.
159 extern const cl_SF cl_hypot (const cl_SF& a, const cl_SF& b);
161 // cl_C_hypot(a,b) liefert abs(a+bi), wo a und b Single-Floats sind.
162 extern const cl_FF cl_hypot (const cl_FF& a, const cl_FF& b);
164 // cl_C_hypot(a,b) liefert abs(a+bi), wo a und b Double-Floats sind.
165 extern const cl_DF cl_hypot (const cl_DF& a, const cl_DF& b);
167 // cl_C_hypot(a,b) liefert abs(a+bi), wo a und b gleichlange Long-Floats sind.
168 extern const cl_LF cl_hypot (const cl_LF& a, const cl_LF& b);
170 // cl_C_hypot(a,b) liefert abs(a+bi), wo a und b reelle Zahlen sind.
171 extern const cl_R cl_hypot (const cl_R& a, const cl_R& b);
173 // Liefert (abs x), wo x eine nicht-reelle Zahl ist.
174 extern const cl_R abs (const cl_C& x);
181 cl_C_R () : realpart(0), imagpart(0) {}
182 cl_C_R (const cl_R& re, const cl_R& im) : realpart(re), imagpart(im) {}
185 // Hilfsfunktion für atanh und atan: u+iv := artanh(x+iy). Liefert cl_C_R(u,v).
186 extern const cl_C_R atanh (const cl_R& x, const cl_R& y);
188 // Hilfsfunktion für asinh und asin: u+iv := arsinh(x+iy). Liefert cl_C_R(u,v).
189 extern const cl_C_R asinh (const cl_R& x, const cl_R& y);