On Fri, Nov 11, 2011 at 4:38 PM, vweber <[email protected]> wrote:
> Dear all
>
> Few questions:
>
> 1) exectuting that
>> x = symbols('x')
>> z=1.0*x**(-1.0)
>> print fcode(simplify(z))

Don't make the exponent real:

>>> fcode(1.0/x)
'      1.0d0/x'
>>> fcode(1.0*x**-1)
'      1.0d0/x'

> 2) executing that
>> x = symbols('x')
>> z=x**2
>> print fcode(simplify(diff(z)))
>
> gives
>
>>      2*x
>
> I would like to get 2.0d0*x instead, any hints?

Usually we want to go from Floats to Rational; there is no function to
go the opposite direction that I know of, but if you want to make all
coefficients Floats then you could do:

>>> Add(*[Mul(float(c) if c is not S.One else 1, m) for c, m in 
>>> [a.as_coeff_Mul() for a in Add.make_args(expr)]])
2.0*x
>>> expr = 2*x**3 + x + 4
>>> Add(*[Mul(float(c) if c is not S.One else 1, m) for c, m in 
>>> [a.as_coeff_Mul() for a in Add.make_args(expr)]])
2.0*x**3 + x + 4.0

(If you want a 1.0 in front of the x then make that

>>> Add(*[Mul(float(c), m) for c, m in [a.as_coeff_Mul() for a in 
>>> Add.make_args(expr)]])
2.0*x**3 + 1.0*x + 4.0


> 3) executing
>> x = symbols('x')
>> z=1.0*x**(1.0/3.0)
>> print fcode(simplify(z))
>

sympy never sees the ratio since python evaluates the 1.0/3.0 to 0.33;
again, don't use Floats:

>>> fcode(1.0*root(x, 3))
'      1.0d0*x**(1.0d0/3.0d0)'
>>> fcode(1.0*x**Rational(1, 3))
'      1.0d0*x**(1.0d0/3.0d0)'

HTH,
 Chris

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to