A little cheat-sheet, on how to wrap Python mpmath procedurals inside regular SageMath expressions.

The following link should not be taken, as the complete solution to anybody else’s computing task, in which my readers might want to wrap Python code for use in SageMath. In fact, I don’t even consider myself an expert in either of these subjects.

For people who do not know, “SageMath” is a combination of “Computer Algebra System” – ‘CAS’ – and ‘Numerical Toolbox’. SageMath is written using multiple languages, mainly Python. And, the ‘Maxima’ CAS back-end was written in LISP. ‘mpmath’ is a specific Python package, that allows Multi-Precision Arithmetic.

The integration of ‘mpmath’ is particularly straightforward, because Sage already uses this package. But the principles that would be used for other Python packages are similar. An interface must be established, by which Sage objects can be translated into the objects specific to the external Python packages, and back into objects that Sage can recognize again. Objects that Sage finds particularly useful, are ‘Symbolic Functions’ – that are to be manipulated algebraically – and ditto for ‘Symbolic Expressions’.

If worse comes to worst, then the data generated by the external, ‘wrapped’ code, may be converted into native Python objects such as ~Strings~. However, Sage does not recognize strings as valid Symbolic Expressions. So, one way around that could be, to call Sage’s ‘sage_eval()‘ function on the strings. (:1)

http://dirkmittler.homeip.net/LambertW%20Test%203%20–%20Sage.html

(Updated 7/28/2021, 12h15… )

Some perceivable inconsistencies, about whether the embedded worksheets can be viewed.

One of the facts which I did mention in an earlier posting, concern worksheets which I sometimes embedded into my postings as ‘<iframe>s’. In reality, there could be two reasons, why such worksheets fail to display in any one person’s browser:

1. I could have embedded them, specifying (insecure) URLs that begin with ‘http://’, even though the reader may be visiting my blog, using a (secure) URL that begins with ‘httpS://’. Or,
2. The posting might suggest that the reader “may need to” enable JavaScript from the domain ‘mathjax.org’, but, in certain cases this is not needed, while in other cases, it is.

‘Problem 1′ above is harder for the reader to fix at their end. It can be solved by fetching my posting using the insecure ‘http://’ URL, or, as I’ve done in a very recent posting, I could decide to embed the ‘<iframe>’ using a URL, which does not specify my domain-name, and just assumes that it’s to be the same, as the domain-name of my site.

‘Problem 2′ above represents a contradiction which may confuse some readers. Sometimes, when I generated an HTML version of a worksheet, I did this by simply clicking on a button, in the GUI of my software. In such cases, the HTML will require that the JavaScript be enabled. But, in certain other cases, all I really did was, to output the worksheet as a LaTeX document, and then to run a custom script on it, to generate other types of documents, one of which would have been an HTML document.

If I ran my custom script on some generic LaTeX document, that generates ‘HTML with equations’, it will do so in the form of ‘a hybrid document’. The resulting document is hybrid, because it can then be viewed in one of two ways:

1. Using the built-in support for ‘MathML’, that some readers’ browsers have. This is less likely, because I only know of one browser that actually support it: ‘Firefox’. If the reader is using ‘MS Edge’ or the ‘Chrome’ browser to view my posting, I know that they do not support MathML as a built-in feature. But,
2. Such a hybrid HTML document can also be viewed, by enabling the JavaScript that gets referred to as ‘MathJax’, and which essentially allows other browsers, including ‘MS Edge’ and ‘Chrome’, to view the equations.

I try to accommodate as many possible configurations of the readers’ browsers as I can, but the unfortunate reality is, that merely pressing the GUI button within my application, may generate HTML which is highlighted more nicely, but which is not a hybrid HTML document.

Dirk