]> www.ginac.de Git - cln.git/blob - src/complex/cl_C.h
* src/complex/transcendental/cl_C_expt_C.cc (expt): fix logic for
[cln.git] / src / complex / cl_C.h
1 // cl_C internals
2
3 #ifndef _CL_C_H
4 #define _CL_C_H
5
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 "cl_macros.h"
13 #include "cln/malloc.h"
14
15 namespace cln {
16
17 struct cl_heap_complex : cl_heap {
18         cl_R realpart;
19         cl_R imagpart;
20 };
21
22 inline cl_heap_complex* TheComplex (cl_heap_complex* p)
23         { return p; }
24 inline cl_heap_complex* TheComplex (const cl_number& obj)
25         { return (cl_heap_complex*)(obj.pointer); }
26
27 inline cl_heap_complex* allocate_complex (const cl_R& real, const cl_R& imag)
28 {
29         cl_heap_complex* p = (cl_heap_complex*) malloc_hook(sizeof(cl_heap_complex));
30         p->refcount = 1;
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);
34         return p;
35 }
36
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) {}
41
42 // Both work, but the first definition results in less compiler-generated
43 // temporaries.
44 #if 1
45   #define Complex  cl_heap_complex*
46 #else
47   #define Complex  cl_N
48 #endif
49
50 // Type tests
51 inline cl_boolean realp (const cl_N& x)
52 {
53         if (x.pointer_p())
54                 if (x.heappointer->type == &cl_class_complex)
55                         return cl_false;
56         return cl_true;
57 }
58 inline cl_boolean complexp (const cl_N& x)
59 {
60         if (x.pointer_p())
61                 if (x.heappointer->type == &cl_class_complex)
62                         return cl_true;
63         return cl_false;
64 }
65
66 // Comparison with a fixnum.
67 inline cl_boolean eq (const cl_N& x, sint32 y)
68 {
69         return (cl_boolean)(x.word == cl_combine(cl_FN_tag,y));
70 }
71
72 inline cl_boolean exact_zerop (const cl_N& x)
73 {
74         return eq(x,0);
75 }
76
77
78 // A complex (cl_C) is a number which is not a real number (cl_R).
79
80 // typedef
81 class cl_C : public cl_N {
82 public:
83 };
84
85 inline cl_boolean realp (const cl_C& x)
86         { unused x; return cl_false; }
87 inline cl_boolean complexp (const cl_C& x)
88         { unused x; return cl_true; }
89
90
91 // Liefert zu reellen Zahlen a und b /= Fixnum 0 die komplexe Zahl a+bi.
92 // complex_C(a,b)
93 extern const cl_N complex_C (const cl_R& a, const cl_R& b);
94
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)
98 {
99         return TheComplex(x)->realpart;
100 }
101 inline const cl_R& imagpart (const cl_C& x)
102 {
103         return TheComplex(x)->imagpart;
104 }
105
106
107 // Primitive forms of complex numbers with restricted part type.
108
109 // typedef
110 struct cl_C_SF {
111         cl_SF realpart;
112         cl_SF imagpart;
113         cl_C_SF (const cl_SF& re, const cl_SF& im) : realpart(re), imagpart(im) {}
114 };
115 inline const cl_N complex_C (const cl_C_SF& c)
116         { return complex_C(c.realpart,c.imagpart); }
117
118 // typedef
119 struct cl_C_FF {
120         cl_FF realpart;
121         cl_FF imagpart;
122         cl_C_FF (const cl_FF& re, const cl_FF& im) : realpart(re), imagpart(im) {}
123 };
124 inline const cl_N complex_C (const cl_C_FF& c)
125         { return complex_C(c.realpart,c.imagpart); }
126
127 // typedef
128 struct cl_C_DF {
129         cl_DF realpart;
130         cl_DF imagpart;
131         cl_C_DF (const cl_DF& re, const cl_DF& im) : realpart(re), imagpart(im) {}
132 };
133 inline const cl_N complex_C (const cl_C_DF& c)
134         { return complex_C(c.realpart,c.imagpart); }
135
136 // typedef
137 struct cl_C_LF {
138         cl_LF realpart;
139         cl_LF imagpart;
140         cl_C_LF (const cl_LF& re, const cl_LF& im) : realpart(re), imagpart(im) {}
141 };
142 inline const cl_N complex_C (const cl_C_LF& c)
143         { return complex_C(c.realpart,c.imagpart); }
144
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);
147
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);
150
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);
153
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);
156
157
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);
160
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);
163
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);
166
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);
169
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);
172
173 // Liefert (abs x), wo x eine nicht-reelle Zahl ist.
174 extern const cl_R abs (const cl_C& x);
175
176
177 // typedef
178 struct cl_C_R {
179         cl_R realpart;
180         cl_R imagpart;
181         cl_C_R () : realpart(0), imagpart(0) {}
182         cl_C_R (const cl_R& re, const cl_R& im) : realpart(re), imagpart(im) {}
183 };
184
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);
187
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);
190
191 }  // namespace cln
192
193 #endif /* _CL_C_H */