On Thu, Aug 28, 2008 at 9:14 PM, Jason Grout
<[EMAIL PROTECTED]> wrote:
>
> MB wrote:
>> Using Robert's suggestion of repr() got me pretty close.  The biggest
>> remaining issue is that Sage writes a^x whereas C needs pow(a,x).  For
>> simple cases, I was able to fix this with regular expression
>> substitution as follows:
>>
>> import re
>> p = re.compile("([a-zA-Z0-9]+?)\\^([a-zA-Z0-9]+)")
>>
>> o = open("mycode.c", "w")
>> o.write("E1 = "
>> o.write(p.subn("pow(\\1,\\2)", repr(E1))[0])
>> o.write(";\n")
>>
>> Here E1 is the expression to be written out.
>>
>> Unfortunately, my regular expression is too simple to handle cases
>> like (a+b)^2.
>
>
> For various objects and various software systems (like mathematica,
> magma, maxima, etc.), we have a _mathematica_init_, _magma_init_, etc,
> which convert an expression into syntax for the target system.  A lot of
> these are defined in calculus.py for converting symbolic expressions to
> syntax for other systems.  I don't think we have an "interface" to C
> code; can anyone think of a reason why we shouldn't?  (or do we already
> have one?)

This is a good idea.  Regarding the rest of your email, this is precisely
the same thing I would have said.

Incidentally, I believe Ginac has a C output mode for its expressions.
This isn't wrapped in pynac yet, but should be easy to do.

> That said, can you modify either the _repr_ function or the _latex_
> function for your needs?  For example, in the _latex_ function, there is
> a place in the code where it clearly does the power string (line 5081 in
> devel/sage/sage/calculus/calculus.py in my current sage files).
>
> For that matter, it looks like if you just add two lines in _sys_init_
> (line 5111 in calculus.py for me), so it looks like this:
>
>     def _sys_init_(self, system):
>         ops = self._operands
>         if self._operator is operator.neg:
>             return '-(%s)' % sys_init(ops[0], system)
>         elif self._operator is operator.pow:
>            return 'pow(%s, %s)' % (sys_init(ops[0], system),
>                                        sys_init(ops[1], system))
>        else:
>             return '(%s) %s (%s)' % (sys_init(ops[0], system),
>                              infixops[self._operator],
>                              sys_init(ops[1], system))
>
> or something like that, it would be a quick hackjob to do what you want,
> maybe.
>
>
> Thanks,
>
> Jason
>
>
> >
>



-- 
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to