[GiNaC-devel] Patch for base of natural logarithm

Jan Rheinländer jrheinlaender at gmx.de
Sun Feb 9 15:51:06 CET 2025


Hi,

this is a patch to add the base of the natural logarithm as a new 
constant EulerNum, and ensure that log(EulerNum) evaluates to 1.

Of course, exp(1) will do fine for calculations. But for printing, it is 
nicer to see e^x instead of exp(1)^x or 2.718^x.

Hope this is useful.

Jan

---
  ginac/constant.cpp      | 5 +++++
  ginac/constant.h        | 1 +
  ginac/inifcns_trans.cpp | 3 +++
  ginac/numeric.cpp       | 5 +++++
  ginac/numeric.h         | 1 +
  ginsh/ginsh_lexer.lpp   | 1 +
  6 files changed, 16 insertions(+)

diff --git a/ginac/constant.cpp b/ginac/constant.cpp
index a89711ef..60af44bc 100644
--- a/ginac/constant.cpp
+++ b/ginac/constant.cpp
@@ -91,6 +91,8 @@ void constant::read_archive(const archive_node &n, lst 
&sym_lst)
              *this = Catalan;
          else if (s == Euler.name)
              *this = Euler;
+        else if (s == EulerNum.name)
+            *this = EulerNum;
          else
              throw (std::runtime_error("unknown constant '" + s + "' in 
archive"));
      } else
@@ -254,4 +256,7 @@ const constant Euler("Euler", EulerEvalf, 
"\\gamma_E", domain::positive);
  /** Catalan's constant. (0.91597...)  Diverts straight into CLN for 
evalf(). */
  const constant Catalan("Catalan", CatalanEvalf, "G", domain::positive);

+/** Base of the natural logarithm */
+const constant EulerNum("EulerNum", EulerNumEvalf, "\\mathit{e}", 
domain::positive);
+
  } // namespace GiNaC
diff --git a/ginac/constant.h b/ginac/constant.h
index 92d07941..24839d46 100644
--- a/ginac/constant.h
+++ b/ginac/constant.h
@@ -82,6 +82,7 @@ GINAC_DECLARE_UNARCHIVER(constant);
  extern const constant Pi;
  extern const constant Catalan;
  extern const constant Euler;
+extern const constant EulerNum;

  } // namespace GiNaC

diff --git a/ginac/inifcns_trans.cpp b/ginac/inifcns_trans.cpp
index 6f577bae..ef5f52f1 100644
--- a/ginac/inifcns_trans.cpp
+++ b/ginac/inifcns_trans.cpp
@@ -202,6 +202,9 @@ static ex log_eval(const ex & x)
              return log(ex_to<numeric>(x));
      }

+    if (x.is_equal(EulerNum))
+            return *_num1_p;
+
      // log(exp(t)) -> t (if -Pi < t.imag() <= Pi):
      if (is_ex_the_function(x, exp)) {
          const ex &t = x.op(0);
diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp
index 6da79977..addc012d 100644
--- a/ginac/numeric.cpp
+++ b/ginac/numeric.cpp
@@ -2515,6 +2515,11 @@ ex CatalanEvalf()
      return numeric(cln::catalanconst(cln::default_float_format));
  }

+/** Floating point evaluation of base of natural logarithm. */
+ex EulerNumEvalf()
+{
+    return exp(*_num1_p); // Suggestion by Vladimir Kisil on ginac-devel
+}

  /** _numeric_digits default ctor, checking for singleton invariance. */
  _numeric_digits::_numeric_digits()
diff --git a/ginac/numeric.h b/ginac/numeric.h
index 0673d9a5..d25670f9 100644
--- a/ginac/numeric.h
+++ b/ginac/numeric.h
@@ -325,6 +325,7 @@ inline const numeric denom(const numeric &x)
  ex PiEvalf();
  ex EulerEvalf();
  ex CatalanEvalf();
+ex EulerNumEvalf();


  } // namespace GiNaC
diff --git a/ginsh/ginsh_lexer.lpp b/ginsh/ginsh_lexer.lpp
index 6e0732e7..bcfecd10 100644
--- a/ginsh/ginsh_lexer.lpp
+++ b/ginsh/ginsh_lexer.lpp
@@ -75,6 +75,7 @@ AN    [0-9a-zA-Z_]
  Pi            yylval = Pi; return T_LITERAL;
  Euler            yylval = Euler; return T_LITERAL;
  Catalan            yylval = Catalan; return T_LITERAL;
+EulerNum        yylval = EulerNum; return T_LITERAL;
  FAIL            yylval = *new fail(); return T_LITERAL;
  I            yylval = I; return T_NUMBER;
  Digits            yylval = (long)Digits; return T_DIGITS;
-- 
2.43.0


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.ginac.de/pipermail/ginac-devel/attachments/20250209/6141c633/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 321 bytes
Desc: OpenPGP digital signature
URL: <http://www.ginac.de/pipermail/ginac-devel/attachments/20250209/6141c633/attachment.sig>


More information about the GiNaC-devel mailing list