2 #include <symengine/ntheory_funcs.h>
3 #include <symengine/prime_sieve.h>
8 PrimePi::PrimePi(
const RCP<const Basic> &arg) : OneArgFunction(arg)
10 SYMENGINE_ASSIGN_TYPEID()
11 SYMENGINE_ASSERT(is_canonical(arg))
14 bool PrimePi::is_canonical(const RCP<const Basic> &arg)
const
23 RCP<const Basic> PrimePi::create(
const RCP<const Basic> &arg)
const
28 RCP<const Basic> primepi(
const RCP<const Basic> &arg)
31 if (is_a<NaN>(*arg)) {
33 }
else if (is_a<Infty>(*arg)) {
34 if (down_cast<const Infty &>(*arg).is_negative_infinity()) {
35 return make_rcp<const Integer>(integer_class(0));
39 }
else if (down_cast<const Number &>(*arg).is_complex()) {
40 throw SymEngineException(
"Complex can't be passed to primepi!");
41 }
else if (down_cast<const Number &>(*arg).is_negative()) {
42 return make_rcp<const Integer>(integer_class(0));
49 Sieve::iterator pi(num);
50 unsigned long int p = 0;
51 while ((pi.next_prime()) <= num) {
54 return make_rcp<const Integer>(integer_class(p));
56 return make_rcp<const PrimePi>(arg);
59 Primorial::Primorial(
const RCP<const Basic> &arg) : OneArgFunction(arg)
61 SYMENGINE_ASSIGN_TYPEID()
62 SYMENGINE_ASSERT(is_canonical(arg))
65 bool Primorial::is_canonical(const RCP<const
Basic> &arg)
const
76 return primorial(arg);
79 RCP<const Basic> primorial(
const RCP<const Basic> &arg)
82 if (is_a<NaN>(*arg)) {
85 if (down_cast<const Number &>(*arg).is_positive()) {
86 if (is_a<Infty>(*arg)) {
90 throw SymEngineException(
91 "Only positive numbers are allowed for primorial!");
97 return make_rcp<const Integer>(mp_primorial(n));
99 return make_rcp<const Primorial>(arg);
111 const RCP<const Basic> &n)
114 if (not is_a<Integer>(*s)
115 or not down_cast<const Integer &>(*
sub(s,
integer(2)))
117 throw DomainError(
"The number of sides of the polygon must be an "
118 "integer greater than 2");
122 if (not is_a<Integer>(*n)
123 or not down_cast<const Integer &>(*n).is_positive()) {
124 throw DomainError(
"n must be an integer greater than 0");
130 auto s_int = down_cast<const Integer &>(*s).as_integer_class();
131 auto n_int = down_cast<const Integer &>(*n).as_integer_class();
133 return make_rcp<const Integer>(res);
136 RCP<const Integer> m1 =
integer(-1);
137 RCP<const Integer> m2 =
integer(-2);
138 RCP<const Integer> p2 =
integer(2);
139 RCP<const Integer> p4 =
integer(4);
140 RCP<const Basic> x =
div(
154 const RCP<const Basic> &x)
157 if (not is_a<Integer>(*s)
158 or not down_cast<const Integer &>(*
sub(s,
integer(2)))
160 throw DomainError(
"The number of sides of the polygon must be an "
161 "integer greater than 2");
165 if (not is_a<Integer>(*x)
166 or not down_cast<const Integer &>(*x).is_positive()) {
167 throw DomainError(
"x must be an integer greater than 0");
173 auto s_int = down_cast<const Integer &>(*s).as_integer_class();
174 auto x_int = down_cast<const Integer &>(*x).as_integer_class();
176 return make_rcp<const Integer>(res);
179 RCP<const Integer> m2 =
integer(-2);
180 RCP<const Integer> m4 =
integer(-4);
181 RCP<const Integer> p2 =
integer(2);
182 RCP<const Integer> p8 =
integer(8);
183 RCP<const Basic> root
RCP< const Basic > create(const RCP< const Basic > &arg) const override
Method to construct classes with canonicalization.
Main namespace for SymEngine package.
bool is_a_Number(const Basic &b)
integer_class mp_polygonal_number(const integer_class &s, const integer_class &n)
Numeric calculation of the n:th s-gonal number.
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
std::enable_if< std::is_integral< T >::value, RCP< const Integer > >::type integer(T i)
RCP< const Basic > principal_polygonal_root(const RCP< const Basic > &s, const RCP< const Basic > &x)
The principal s-gonal root of x.
RCP< const Basic > sub(const RCP< const Basic > &a, const RCP< const Basic > &b)
Substracts b from a.
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
RCP< const Basic > floor(const RCP< const Basic > &arg)
Canonicalize Floor:
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
integer_class mp_principal_polygonal_root(const integer_class &s, const integer_class &x)
Numeric calculation of the principal s-gonal root of x.
RCP< const Basic > polygonal_number(const RCP< const Basic > &s, const RCP< const Basic > &n)
The n:th s-gonal number.