]> www.ginac.de Git - ginac.git/blobdiff - ginac/numeric.cpp
- changed behaviour of numeric::is_rational() and added numeric::is_cinteger()
[ginac.git] / ginac / numeric.cpp
index 4877dae702c908c740a0133a7fd4246cff8323d2..9f7cb6ec22f6b74d7bec46f11f576c64a6bb2398 100644 (file)
@@ -40,7 +40,9 @@
 #include <cln.h>
 #endif
 
+#ifndef NO_GINAC_NAMESPACE
 namespace GiNaC {
+#endif // ndef NO_GINAC_NAMESPACE
 
 // linker has no problems finding text symbols for numerator or denominator
 //#define SANE_LINKER
@@ -326,7 +328,7 @@ ex numeric::evalf(int level) const
 
 int numeric::compare_same_type(basic const & other) const
 {
-    ASSERT(is_exactly_of_type(other, numeric));
+    GINAC_ASSERT(is_exactly_of_type(other, numeric));
     numeric const & o = static_cast<numeric &>(const_cast<basic &>(other));
 
     if (*value == *o.value) {
@@ -338,7 +340,7 @@ int numeric::compare_same_type(basic const & other) const
 
 bool numeric::is_equal_same_type(basic const & other) const
 {
-    ASSERT(is_exactly_of_type(other,numeric));
+    GINAC_ASSERT(is_exactly_of_type(other,numeric));
     numeric const *o = static_cast<numeric const *>(&other);
     
     return is_equal(*o);
@@ -592,7 +594,7 @@ bool numeric::is_negative(void) const
 /** True if object is a non-complex integer. */
 bool numeric::is_integer(void) const
 {
-    return (bool)instanceof(*value, cl_I_ring);  // -> CLN
+    return instanceof(*value, cl_I_ring);  // -> CLN
 }
 
 /** True if object is an exact integer greater than zero. */
@@ -636,20 +638,13 @@ bool numeric::is_prime(void) const
  *  (denominator may be unity). */
 bool numeric::is_rational(void) const
 {
-    if (instanceof(*value, cl_RA_ring)) {
-        return true;
-    } else if (!is_real()) {  // complex case, handle Q(i):
-        if ( instanceof(realpart(*value), cl_RA_ring) &&
-             instanceof(imagpart(*value), cl_RA_ring) )
-            return true;
-    }
-    return false;
+    return instanceof(*value, cl_RA_ring);
 }
 
 /** True if object is a real integer, rational or float (but not complex). */
 bool numeric::is_real(void) const
 {
-    return (bool)instanceof(*value, cl_R_ring);  // -> CLN
+    return instanceof(*value, cl_R_ring);  // -> CLN
 }
 
 bool numeric::operator==(numeric const & other) const
@@ -662,6 +657,34 @@ bool numeric::operator!=(numeric const & other) const
     return (*value != *other.value);  // -> CLN
 }
 
+/** True if object is element of the domain of integers extended by I, i.e. is
+ *  of the form a+b*I, where a and b are integers. */
+bool numeric::is_cinteger(void) const
+{
+    if (instanceof(*value, cl_I_ring))
+        return true;
+    else if (!is_real()) {  // complex case, handle n+m*I
+        if (instanceof(realpart(*value), cl_I_ring) &&
+            instanceof(imagpart(*value), cl_I_ring))
+            return true;
+    }
+    return false;
+}
+
+/** True if object is an exact rational number, may even be complex
+ *  (denominator may be unity). */
+bool numeric::is_crational(void) const
+{
+    if (instanceof(*value, cl_RA_ring))
+        return true;
+    else if (!is_real()) {  // complex case, handle Q(i):
+        if (instanceof(realpart(*value), cl_RA_ring) &&
+            instanceof(imagpart(*value), cl_RA_ring))
+            return true;
+    }
+    return false;
+}
+
 /** Numerical comparison: less.
  *
  *  @exception invalid_argument (complex inequality) */ 
@@ -714,7 +737,7 @@ bool numeric::operator>=(numeric const & other) const
  *  if the number is really an integer before calling this method. */
 int numeric::to_int(void) const
 {
-    ASSERT(is_integer());
+    GINAC_ASSERT(is_integer());
     return cl_I_to_int(The(cl_I)(*value));
 }
 
@@ -722,7 +745,7 @@ int numeric::to_int(void) const
  *  if the number is really not complex before calling this method. */
 double numeric::to_double(void) const
 {
-    ASSERT(is_real());
+    GINAC_ASSERT(is_real());
     return cl_double_approx(realpart(*value));
 }
 
@@ -753,7 +776,8 @@ inline cl_heap_ratio* TheRatio (const cl_N& obj)
 
 /** Numerator.  Computes the numerator of rational numbers, rationalized
  *  numerator of complex if real and imaginary part are both rational numbers
- *  (i.e numer(4/3+5/6*I) == 8+5*I), the number itself in all other cases. */
+ *  (i.e numer(4/3+5/6*I) == 8+5*I), the number carrying the sign in all other
+ *  cases. */
 numeric numeric::numer(void) const
 {
     if (is_integer()) {
@@ -1083,6 +1107,14 @@ numeric gamma(numeric const & x)
 
 /** The psi function (aka polygamma function).
  *  This is only a stub! */
+numeric psi(numeric const & x)
+{
+    clog << "psi(): Does anybody know good way to calculate this numerically?" << endl;
+    return numeric(0);
+}
+
+/** The psi functions (aka polygamma functions).
+ *  This is only a stub! */
 numeric psi(numeric const & n, numeric const & x)
 {
     clog << "psi(): Does anybody know good way to calculate this numerically?" << endl;
@@ -1444,4 +1476,6 @@ bool _numeric_digits::too_late = false;
  *  assignment from C++ unsigned ints and evaluated like any built-in type. */
 _numeric_digits Digits;
 
+#ifndef NO_GINAC_NAMESPACE
 } // namespace GiNaC
+#endif // ndef NO_GINAC_NAMESPACE