carl  24.04
Computer ARithmetic Library
operations_generic.h
Go to the documentation of this file.
1 /**
2  * @file: operations_generic.h
3  * @author: Sebastian Junges
4  *
5  * @since November 6, 2014
6  */
7 
8 #pragma once
9 
10 #ifndef INCLUDED_FROM_NUMBERS_H
11 static_assert(false, "This file may only be included indirectly by numbers.h");
12 #endif
13 
15 #include "constants.h"
16 
17 namespace carl {
18 
19 template<typename T>
20 inline bool is_zero(const T& t) {
21  return t == 0;
22 }
23 
24 template<typename T>
25 inline bool is_one(const T& t) {
26  return t == 1;
27 }
28 
29 template<typename T, EnableIf<has_is_positive<T>>>
30 inline bool is_positive(const T& t) {
31  return t.is_positive();
32 }
33 
34 template<typename T, DisableIf<has_is_positive<T>>>
35 inline bool is_positive(const T& t) {
36  return t > 0;
37 }
38 
39 template<typename T, EnableIf<has_is_negative<T>>>
40 inline bool is_negative(const T& t) {
41  return t.is_negative();
42 }
43 
44 template<typename T, DisableIf<has_is_negative<T>>>
45 inline bool is_negative(const T& t) {
46  return t < 0;
47 }
48 
49 /**
50  * Implements a fast exponentiation on an arbitrary type T.
51  * To use carl::pow() on a type T, the following must be defined:
52  * - carl::constant_one<T>,
53  * - T::operator=(const T&) and
54  * - operator*(const T&, const T&).
55  * Alternatively, carl::pow() can be specialized for T explicitly.
56  * @param basis A number.
57  * @param exp The exponent.
58  * @return `basis` to the power of `exp`.
59  */
60 template<typename T, DisableIf<is_interval_type<T>> = dummy>
61 T pow(const T& basis, std::size_t exp) {
62  T res = carl::constant_one<T>().get();
63  T mult = basis;
64  for (std::size_t e = exp; e > 0; e /= 2) {
65  if (e & static_cast<std::size_t>(1)) {
66  res *= mult;
67  }
68  mult *= mult;
69  }
70  return res;
71 }
72 
73 /**
74  * Implements a fast exponentiation on an arbitrary type T.
75  * The result is stored in the given number.
76  * To use carl::pow_assign() on a type T, the following must be defined:
77  * - carl::constant_one<T>,
78  * - T::operator=(const T&) and
79  * - operator*(const T&, const T&).
80  * Alternatively, carl::pow() can be specialized for T explicitly.
81  * @param t A number.
82  * @param exp The exponent.
83  */
84 template<typename T>
85 void pow_assign(T& t, std::size_t exp) {
86  T mult = t;
87  t = carl::constant_one<T>().get();
88  for (std::size_t e = exp; e > 0; e /= 2) {
89  if (e & static_cast<std::size_t>(1)) {
90  t *= mult;
91  }
92  mult *= mult;
93  }
94 }
95 
96 } // namespace carl
carl is the main namespace for the library.
bool is_positive(const cln::cl_I &n)
Definition: operations.h:39
void pow_assign(Interval< Number > &i, Integer exp)
Definition: Power.h:46
Interval< Number > exp(const Interval< Number > &i)
Definition: Exponential.h:10
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
bool is_negative(const cln::cl_I &n)
Definition: operations.h:47
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
Definition: Interval.h:1462
static const T & get()
Definition: constants.h:51