ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 15 Feb 2017 05:13:49 -0600Export to C codehttp://ask.sagemath.org/question/7922/export-to-c-code/Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.
Mon, 07 Feb 2011 01:26:21 -0600http://ask.sagemath.org/question/7922/export-to-c-code/Answer by louisgag for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=36624#post-id-36624Just to add to the list of answers, I'm linking a post I've made which shows how to expand Benjamin Jones' solution to a system of functions and their gradients: http://louisgagnon.com/scBlog/sageCpp.htmlWed, 15 Feb 2017 05:13:49 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?answer=36624#post-id-36624Answer by benjaminfjones for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=14574#post-id-14574Just to chime in here on this long abandoned question, the code generation module that DSM mentions is now included in Sage. Here's a short example:
sage: x, y = var('x, y')
sage: f = x**2 + sin(y)/sqrt(5*x+9*y)
sage: fs = f._sympy_() # convert to a sympy symbolic expression
sage: [(c_name, c_code), (h_name, c_header)] = codegen(("f", fs), "C", "test")
sage: print c_code
/******************************************************************************
* Code generated with sympy 0.7.1 *
* *
* See http://www.sympy.org/ for more information. *
* *
* This file is part of 'project' *
******************************************************************************/
#include "test.h"
#include <math.h>
double f(double x, double y) {
return pow(x, 2) + sin(y)/sqrt(5*x + 9*y);
}
Thu, 21 Feb 2013 19:58:00 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?answer=14574#post-id-14574Comment by louisgag for <p>Just to chime in here on this long abandoned question, the code generation module that DSM mentions is now included in Sage. Here's a short example:</p>
<pre><code>sage: x, y = var('x, y')
sage: f = x**2 + sin(y)/sqrt(5*x+9*y)
sage: fs = f._sympy_() # convert to a sympy symbolic expression
sage: [(c_name, c_code), (h_name, c_header)] = codegen(("f", fs), "C", "test")
sage: print c_code
/******************************************************************************
* Code generated with sympy 0.7.1 *
* *
* See http://www.sympy.org/ for more information. *
* *
* This file is part of 'project' *
******************************************************************************/
#include "test.h"
#include <math.h>
double f(double x, double y) {
return pow(x, 2) + sin(y)/sqrt(5*x + 9*y);
}
</code></pre>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=36049#post-id-36049Another comment @benjaminfjones : this line ` fs = f._sympy_()` is really necessary ? I've noticed that even if I exclude it the conversion gets done and the code returned is identical... ANSWERING MY OWN COMMENT: yes sympy is necessary, in some cases I would get an error if not converting to sympy, so you can ignore my comment...Mon, 19 Dec 2016 03:38:39 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=36049#post-id-36049Comment by louisgag for <p>Just to chime in here on this long abandoned question, the code generation module that DSM mentions is now included in Sage. Here's a short example:</p>
<pre><code>sage: x, y = var('x, y')
sage: f = x**2 + sin(y)/sqrt(5*x+9*y)
sage: fs = f._sympy_() # convert to a sympy symbolic expression
sage: [(c_name, c_code), (h_name, c_header)] = codegen(("f", fs), "C", "test")
sage: print c_code
/******************************************************************************
* Code generated with sympy 0.7.1 *
* *
* See http://www.sympy.org/ for more information. *
* *
* This file is part of 'project' *
******************************************************************************/
#include "test.h"
#include <math.h>
double f(double x, double y) {
return pow(x, 2) + sin(y)/sqrt(5*x + 9*y);
}
</code></pre>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=35085#post-id-35085Thanks for this great solution. Just a detail, this line is also required before calling codegen:
from sympy.utilities.codegen import codegenFri, 07 Oct 2016 06:47:15 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?comment=35085#post-id-35085Comment by DSM for <p>Just to chime in here on this long abandoned question, the code generation module that DSM mentions is now included in Sage. Here's a short example:</p>
<pre><code>sage: x, y = var('x, y')
sage: f = x**2 + sin(y)/sqrt(5*x+9*y)
sage: fs = f._sympy_() # convert to a sympy symbolic expression
sage: [(c_name, c_code), (h_name, c_header)] = codegen(("f", fs), "C", "test")
sage: print c_code
/******************************************************************************
* Code generated with sympy 0.7.1 *
* *
* See http://www.sympy.org/ for more information. *
* *
* This file is part of 'project' *
******************************************************************************/
#include "test.h"
#include <math.h>
double f(double x, double y) {
return pow(x, 2) + sin(y)/sqrt(5*x + 9*y);
}
</code></pre>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=18192#post-id-18192Hooray! This is one of the things which you seldom need, but when you do need, it's uber-handy.Fri, 22 Feb 2013 06:04:20 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=18192#post-id-18192Answer by kemelli for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12407#post-id-12407Hi Hennes,
Did you ever figure out how to translate Sage code into C code?
I have the exact same problem and can't find a way to do that. Besides, the link cswiercz gave is now broken.
Thu, 02 Jun 2011 12:14:34 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12407#post-id-12407Comment by cswiercz for <p>Hi Hennes,</p>
<p>Did you ever figure out how to translate Sage code into C code?
I have the exact same problem and can't find a way to do that. Besides, the link cswiercz gave is now broken.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=21632#post-id-21632Which link are you talking about? They work fine for me.Thu, 02 Jun 2011 12:48:44 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?comment=21632#post-id-21632Answer by DSM for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12399#post-id-12399It looks like sympy 0.6.7 has a [codegen package](http://docs.sympy.org/dev/modules/utilities/codegen.html), but the version in Sage (0.6.4 in Sage 4.7) doesn't. We should upgrade and make sure it works with ._sympy_(), this is a very useful feature.Thu, 02 Jun 2011 15:16:19 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12399#post-id-12399Comment by cswiercz for <p>It looks like sympy 0.6.7 has a <a href="http://docs.sympy.org/dev/modules/utilities/codegen.html">codegen package</a>, but the version in Sage (0.6.4 in Sage 4.7) doesn't. We should upgrade and make sure it works with ._sympy_(), this is a very useful feature.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=21628#post-id-21628Very interesting. I know that some of the Sympy folks have been involved with f2py, a Fortran to Python wrapping utility. Quite interesting that they have a C codegen as well!Fri, 03 Jun 2011 06:27:27 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?comment=21628#post-id-21628Answer by Shashank for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12350#post-id-12350If you just add %cython to the beginning of the notebook it gives a c file. The file is a bit messy but you can always include it in other c files and call functions.Thu, 02 Jun 2011 13:14:20 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12350#post-id-12350Answer by cswiercz for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12069#post-id-12069Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to write code that is easy to use like Python but is optimized to use fast C data types instead of slower Python data structures. However, one could certainly just write Python without any optimizations and have Cython generate the C equivalent.
Create a `.pyx` file containing your code and execute
$ sage -cython -a mycythonfile.pyx
This will output a corresponding `.c` file which can be compiled into a shared object library and `import`ed in Python along with an`.html` file that neatly shows the C equivalent of each line of code.
Be warned that the corresponding C code, though optimized, looks really messy.
For more information on compiling Cython code, take a look at the [Cython compilation documentation](http://docs.cython.org/src/reference/compilation.html).Mon, 07 Feb 2011 02:29:00 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12069#post-id-12069Comment by hennes for <p>Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to write code that is easy to use like Python but is optimized to use fast C data types instead of slower Python data structures. However, one could certainly just write Python without any optimizations and have Cython generate the C equivalent.</p>
<p>Create a <code>.pyx</code> file containing your code and execute</p>
<pre><code>$ sage -cython -a mycythonfile.pyx
</code></pre>
<p>This will output a corresponding <code>.c</code> file which can be compiled into a shared object library and <code>import</code>ed in Python along with an<code>.html</code> file that neatly shows the C equivalent of each line of code.</p>
<p>Be warned that the corresponding C code, though optimized, looks really messy.</p>
<p>For more information on compiling Cython code, take a look at the <a href="http://docs.cython.org/src/reference/compilation.html">Cython compilation documentation</a>.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22183#post-id-22183Is there a way to do this from a Sage notebook? To make myself more clear: I want to feed the result of a (symbolic) Taylor expansion from a Sage notebook into a C program, which I have already written.Mon, 07 Feb 2011 04:08:00 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22183#post-id-22183Comment by hennes for <p>Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to write code that is easy to use like Python but is optimized to use fast C data types instead of slower Python data structures. However, one could certainly just write Python without any optimizations and have Cython generate the C equivalent.</p>
<p>Create a <code>.pyx</code> file containing your code and execute</p>
<pre><code>$ sage -cython -a mycythonfile.pyx
</code></pre>
<p>This will output a corresponding <code>.c</code> file which can be compiled into a shared object library and <code>import</code>ed in Python along with an<code>.html</code> file that neatly shows the C equivalent of each line of code.</p>
<p>Be warned that the corresponding C code, though optimized, looks really messy.</p>
<p>For more information on compiling Cython code, take a look at the <a href="http://docs.cython.org/src/reference/compilation.html">Cython compilation documentation</a>.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22181#post-id-22181I somehow don't get to grips with Cython. Could you point me to a minimal example of using it from within a Sage notebook? I tried something like
%cython
x=0;
x^2;
, but it doesn't convert x^2 to pow(x,2).Mon, 07 Feb 2011 08:01:56 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22181#post-id-22181Comment by cswiercz for <p>Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to write code that is easy to use like Python but is optimized to use fast C data types instead of slower Python data structures. However, one could certainly just write Python without any optimizations and have Cython generate the C equivalent.</p>
<p>Create a <code>.pyx</code> file containing your code and execute</p>
<pre><code>$ sage -cython -a mycythonfile.pyx
</code></pre>
<p>This will output a corresponding <code>.c</code> file which can be compiled into a shared object library and <code>import</code>ed in Python along with an<code>.html</code> file that neatly shows the C equivalent of each line of code.</p>
<p>Be warned that the corresponding C code, though optimized, looks really messy.</p>
<p>For more information on compiling Cython code, take a look at the <a href="http://docs.cython.org/src/reference/compilation.html">Cython compilation documentation</a>.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22182#post-id-22182Type `%cython` at the top of a notebook cell to have Cython convert and compile the contents of the cell. (It will automatically link the .so to the currently running notebook session.) Two links will appear as the cell output. One is a link to the C-source. The other is the handy html guide to what happened in the Cython-C conversion process.Mon, 07 Feb 2011 04:25:00 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22182#post-id-22182Comment by cswiercz for <p>Cython (included with Sage) converts Python-like code into C which can then be compiled into a shared-object library and used by Python / Sage. It's intended use is to write code that is easy to use like Python but is optimized to use fast C data types instead of slower Python data structures. However, one could certainly just write Python without any optimizations and have Cython generate the C equivalent.</p>
<p>Create a <code>.pyx</code> file containing your code and execute</p>
<pre><code>$ sage -cython -a mycythonfile.pyx
</code></pre>
<p>This will output a corresponding <code>.c</code> file which can be compiled into a shared object library and <code>import</code>ed in Python along with an<code>.html</code> file that neatly shows the C equivalent of each line of code.</p>
<p>Be warned that the corresponding C code, though optimized, looks really messy.</p>
<p>For more information on compiling Cython code, take a look at the <a href="http://docs.cython.org/src/reference/compilation.html">Cython compilation documentation</a>.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22180#post-id-22180Take a look at http://uw.sagenb.org/home/pub/49 and follow the instructions. I hope this is what you're looking for and not me misinterpreting your question.Mon, 07 Feb 2011 10:16:24 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22180#post-id-22180Answer by DSM for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12076#post-id-12076If I understand you, then you're looking for the equivalent of maple's CodeGeneration package, which nicely turns Maple functions into fortran or C code which you can then include in your own code (done this many times myself). Unfortunately I couldn't find what you're looking for.
(1) I tried looking at the existing expression conversions (for sympy, maxima, etc.) but none of them seemed to need to do this.
(2) Apparently there are tricks to do it from maxima (using the "subst" command), but I couldn't get any of them to work, at least not via the Sage-Maxima interface. My Maxima-fu is pretty weak, though. Unfortunately Fortran has the "**" operator, so using maxima.fortran wouldn't help!
(3) I don't think Cython will be that useful here, because the C code that it generates is pretty messy, and parsing it to recover something that you could insert in a non-Cython C code -- though doable -- would be much harder and more error-prone than simply turning (a)^(b) into pow((a), (b)) in the first place!
This would be a very useful feature, though; doing algebra in a CAS and then moving it to code used to be one of the main things I used Maple for.Tue, 08 Feb 2011 12:45:31 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12076#post-id-12076Comment by cswiercz for <p>If I understand you, then you're looking for the equivalent of maple's CodeGeneration package, which nicely turns Maple functions into fortran or C code which you can then include in your own code (done this many times myself). Unfortunately I couldn't find what you're looking for.</p>
<p>(1) I tried looking at the existing expression conversions (for sympy, maxima, etc.) but none of them seemed to need to do this.</p>
<p>(2) Apparently there are tricks to do it from maxima (using the "subst" command), but I couldn't get any of them to work, at least not via the Sage-Maxima interface. My Maxima-fu is pretty weak, though. Unfortunately Fortran has the "**" operator, so using maxima.fortran wouldn't help!</p>
<p>(3) I don't think Cython will be that useful here, because the C code that it generates is pretty messy, and parsing it to recover something that you could insert in a non-Cython C code -- though doable -- would be much harder and more error-prone than simply turning (a)^(b) into pow((a), (b)) in the first place! </p>
<p>This would be a very useful feature, though; doing algebra in a CAS and then moving it to code used to be one of the main things I used Maple for.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22151#post-id-22151I'm upvoting this since I think I misunderstood the original question.Wed, 09 Feb 2011 11:23:02 -0600http://ask.sagemath.org/question/7922/export-to-c-code/?comment=22151#post-id-22151Answer by kemelli for <p>Is it possible to export the result of a calculation to C source code? To be more specific, I have a very long equation emerging from a Sage calculation which I need to feed into a C simulation program, but C doesn't understand the x^2, x^3, ... syntax. I ended up using the re module to manually replace the terms with the proper pow function calls, but I thought maybe there is a more elegant solution to this.</p>
http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12337#post-id-12337Hi Hennes,
Did you ever figure out how to translate Sage code into C code?
I have the exact same problem and can't find a way to do that. Besides, the link cswiercz gave is now broken.
Thu, 02 Jun 2011 12:14:49 -0500http://ask.sagemath.org/question/7922/export-to-c-code/?answer=12337#post-id-12337