Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
number.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//number.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//number.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with number.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2023 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// James Collins
24// West Texas A&M University
25// Spring, Summer 2015
26//
27//
28// silviana amethyst
29// University of Wisconsin - Eau Claire
30//
31// Created by Collins, James B. on 4/30/15.
32//
33//
34// number.hpp: Declares the class Number.
35
42
43
44#ifndef BERTINI_NODE_NUMBER_HPP
45#define BERTINI_NODE_NUMBER_HPP
46
47
49
50
51
52namespace bertini {
53namespace node{
54
55
62 class Number : public virtual Symbol
63 {
64 public:
65
66 virtual ~Number() = default;
67
68
69
70 void Reset() const override;
71
72
73
74
75
76
77
83 inline
84 int Degree(std::shared_ptr<Variable> const& v = nullptr) const override
85 {
86 return 0;
87 }
88
94 inline
95 int Degree(VariableGroup const& vars) const override
96 {
97 return 0;
98 }
99
100
106 inline
107 std::vector<int> MultiDegree(VariableGroup const& vars) const override
108 {
109 return std::vector<int>(vars.size(), 0);
110 }
111
117 void Homogenize(VariableGroup const& vars, std::shared_ptr<Variable> const& homvar) override
118 {
119
120 }
121
127 bool IsHomogeneous(std::shared_ptr<Variable> const& v = nullptr) const override
128 {
129 return true;
130 }
131
135 bool IsHomogeneous(VariableGroup const& vars) const override
136 {
137 return true;
138 }
139
140
146 void precision(unsigned int prec) const override;
147
151 std::shared_ptr<Node> Differentiate(std::shared_ptr<Variable> const& v = nullptr) const override;
152
153 protected:
154
155 private:
156 friend class boost::serialization::access;
157
158 template <typename Archive>
159 void serialize(Archive& ar, const unsigned version) {
160 ar & boost::serialization::base_object<Symbol>(*this);
161 }
162
163
164 };
165
166
172 class Integer : public virtual Number, public virtual EnableSharedFromThisVirtual<Integer>
173 {
174 public:
176
177
178
179 Integer(Integer const&) = default;
180
181 ~Integer() = default;
182
183
184
185
186 void print(std::ostream & target) const override;
187
188 template<typename... Ts>
189 static
190 std::shared_ptr<Integer> Make(Ts&& ...ts){
191 return std::shared_ptr<Integer>( new Integer(ts...) );
192 }
193
194 private:
195 // https://stackoverflow.com/questions/33933550/exception-bad-weak-ptr-while-shared-from-this
196 // struct MakeConstructorPublic;
197
198 explicit
199 Integer(int val) : true_value_(val)
200 {}
201
202 explicit
203 Integer(mpz_int val) : true_value_(val)
204 {}
205
206 explicit
207 Integer(std::string const& val) : true_value_(val)
208 {}
209
210
211
212 // Return value of constant
213 dbl FreshEval_d(std::shared_ptr<Variable> const& diff_variable) const override;
214
215 void FreshEval_d(dbl& evaluation_value, std::shared_ptr<Variable> const& diff_variable) const override;
216
217
218 mpfr_complex FreshEval_mp(std::shared_ptr<Variable> const& diff_variable) const override;
219
220 void FreshEval_mp(mpfr_complex& evaluation_value, std::shared_ptr<Variable> const& diff_variable) const override;
221
222
223 mpz_int true_value_;
224
225 friend class boost::serialization::access;
226
227 Integer() = default;
228
229 template <typename Archive>
230 void serialize(Archive& ar, const unsigned version) {
231 ar & boost::serialization::base_object<Number>(*this);
232 ar & true_value_;
233 }
234 };
235
236 // struct Integer::MakeConstructorPublic : public Integer {
237 // template <typename... Args>
238 // MakeConstructorPublic(Args&& ...args) : Integer(std::forward<Args>(args)...)
239 // {}
240 // };
241
242
243
249 class Float : public virtual Number, public virtual EnableSharedFromThisVirtual<Float>
250 {
251 public:
252 BERTINI_DEFAULT_VISITABLE()
253
254
255
256 ~Float() = default;
257
258
259
260
261
262 void print(std::ostream & target) const override;
263
264
265 template<typename... Ts>
266 static
267 std::shared_ptr<Float> Make(Ts&& ...ts){
268 return std::shared_ptr<Float>( new Float(ts...) );
269 }
270
271
272
273 private:
274
275 explicit
276 Float(mpfr_complex const& val) : highest_precision_value_(val)
277 {}
278
279 explicit
280 Float(mpfr_float const& rval, mpfr_float const& ival = 0) : highest_precision_value_(rval,ival)
281 {}
282
283 explicit
284 Float(std::string const& val) : highest_precision_value_(val)
285 {}
286
287 explicit
288 Float(std::string const& rval, std::string const& ival) : highest_precision_value_(rval,ival)
289 {}
290
291 dbl FreshEval_d(std::shared_ptr<Variable> const& diff_variable) const override;
292
293 void FreshEval_d(dbl& evaluation_value, std::shared_ptr<Variable> const& diff_variable) const override;
294
295
296 mpfr_complex FreshEval_mp(std::shared_ptr<Variable> const& diff_variable) const override;
297
298 void FreshEval_mp(mpfr_complex& evaluation_value, std::shared_ptr<Variable> const& diff_variable) const override;
299
300
301 mpfr_complex highest_precision_value_;
302
303 friend class boost::serialization::access;
304 Float() = default;
305 template <typename Archive>
306 void serialize(Archive& ar, const unsigned version) {
307 ar & boost::serialization::base_object<Number>(*this);
308 ar & const_cast<mpfr_complex &>(highest_precision_value_);
309 }
310 };
311
312
313
314
315
316
322 class Rational : public virtual Number, public virtual EnableSharedFromThisVirtual<Rational>
323 {
324 public:
325 BERTINI_DEFAULT_VISITABLE()
326
327 using mpq_rational = bertini::mpq_rational;
328
329
330
331
332 Rational(int, int) = delete;
333
334 ~Rational() = default;
335
341 template<unsigned long Digits = 50>
342 static
343 Rational Rand()
344 {
345 return Rational(RandomRat<Digits>(),RandomRat<Digits>());
346 }
347
353 template<int Digits = 50>
354 static
355 Rational RandReal()
356 {
357 return Rational(RandomRat<Digits>(),0);
358 }
359
360 void print(std::ostream & target) const override;
361
362
363
364
365
366 template<typename... Ts>
367 static
368 std::shared_ptr<Rational> Make(Ts&& ...ts){
369 return std::shared_ptr<Rational>( new Rational(ts...) );
370 }
371
372
373 private:
374
375 explicit
376 Rational(int val) : true_value_real_(val), true_value_imag_(0)
377 {}
378
379 explicit
380 Rational(int val_real_numerator, int val_real_denomenator,
381 int val_imag_numerator, int val_imag_denomenator)
382 :
383 true_value_real_(val_real_numerator,val_real_denomenator), true_value_imag_(val_imag_numerator,val_imag_denomenator)
384 {}
385
386 explicit
387 Rational(std::string val) : true_value_real_(val), true_value_imag_(0)
388 {}
389
390 explicit
391 Rational(std::string val_real, std::string val_imag) : true_value_real_(val_real), true_value_imag_(val_imag)
392 {}
393
394 explicit
395 Rational(mpq_rational const& val_real, mpq_rational const& val_imag = 0) : true_value_real_(val_real), true_value_imag_(val_imag)
396 {}
397
398
399 // Return value of constant
400 dbl FreshEval_d(std::shared_ptr<Variable> const& diff_variable) const override;
401
402 void FreshEval_d(dbl& evaluation_value, std::shared_ptr<Variable> const& diff_variable) const override;
403
404
405 mpfr_complex FreshEval_mp(std::shared_ptr<Variable> const& diff_variable) const override;
406
407 void FreshEval_mp(mpfr_complex& evaluation_value, std::shared_ptr<Variable> const& diff_variable) const override;
408
409
410 mpq_rational true_value_real_, true_value_imag_;
411 Rational() = default;
412 friend class boost::serialization::access;
413
414 template <typename Archive>
415 void serialize(Archive& ar, const unsigned version) {
416 ar & boost::serialization::base_object<Number>(*this);
417 ar & const_cast<mpq_rational &>(true_value_real_);
418 ar & const_cast<mpq_rational &>(true_value_imag_);
419 }
420 };
421
422
423
424} // re: namespace node
425} // re: namespace bertini
426
427#endif
void print(std::ostream &target) const override
Definition number.cpp:97
void print(std::ostream &target) const override
Definition number.cpp:62
Abstract Number type from which other Numbers derive.
Definition number.hpp:63
std::vector< int > MultiDegree(VariableGroup const &vars) const override
Get the multidegree of this node.
Definition number.hpp:107
int Degree(std::shared_ptr< Variable > const &v=nullptr) const override
Get the degree of this node.
Definition number.hpp:84
bool IsHomogeneous(std::shared_ptr< Variable > const &v=nullptr) const override
Is this node homogeneous?
Definition number.hpp:127
void Reset() const override
Definition number.cpp:36
void Homogenize(VariableGroup const &vars, std::shared_ptr< Variable > const &homvar) override
Homogenize this node.
Definition number.hpp:117
int Degree(VariableGroup const &vars) const override
Get the degree of this node.
Definition number.hpp:95
std::shared_ptr< Node > Differentiate(std::shared_ptr< Variable > const &v=nullptr) const override
Differentiate a number.
Definition number.cpp:50
bool IsHomogeneous(VariableGroup const &vars) const override
Definition number.hpp:135
The Rational number type for Bertini2 expression trees.
Definition number.hpp:323
void print(std::ostream &target) const override
Definition number.cpp:131
static Rational Rand()
Get a random complex rational node. Numerator and denominator will have about 50 digits.
Definition number.hpp:343
static Rational RandReal()
Get a random real rational node. Numerator and denominator will have about 50 digits.
Definition number.hpp:355
Abstract symbol class.
Definition symbol.hpp:57
mpq_rational RandomRat()
Definition random.hpp:61
Defines the abstract Symbol and NamedSymbol classes,.
#define BERTINI_DEFAULT_VISITABLE()
macro for classes which want default Accept implementation, having nothing fancy to do when accepting...
Definition visitable.hpp:121