The problem is that the integrals for the second one are harder to do.
Add hint="1st_linear_Integral" to see what integrals they each must
do. There are a few open issues that would help solve this.

The first is http://code.google.com/p/sympy/issues/detail?id=2276,
which would make these kinds of integrals much faster.  The idea here
is to use the undetermined coefficients solver from dsolve() in
integrate(), which would be much faster than the current heurisch()
method for integrals of this type.

The second is http://code.google.com/p/sympy/issues/detail?id=2235,
which is to rearrange the default hint order to dsolve.  In
particular, the hint
nth_linear_constant_coeff_undetermined_coefficients should go first,
as this is always the fastest (as it involves no integration).  These
would actually be essentially the same solution from the users point
of view.

Until this is fixed, you can work around it by manually passing this
hint to dsolve:

In [18]: dsolve(de1, y(t),
'nth_linear_constant_coeff_undetermined_coefficients')
Out[18]:
           -2⋅t   2⋅sin(t)   4⋅cos(t)
y(t) = C₁⋅ℯ     + ──────── + ────────
                     5          5

In [19]: %timeit dsolve(de1, y(t),
'nth_linear_constant_coeff_undetermined_coefficients')
10 loops, best of 3: 69.3 ms per loop

In [20]: dsolve(de2, y(t),
'nth_linear_constant_coeff_undetermined_coefficients')
Out[20]:
       ⎛      2⎞
       ⎜     t ⎟  -2⋅t   2⋅sin(t)   4⋅cos(t)
y(t) = ⎜C₁ + ──⎟⋅ℯ     + ──────── + ────────
       ⎝     2 ⎠            5          5

In [21]: %timeit dsolve(de2, y(t),
'nth_linear_constant_coeff_undetermined_coefficients')
10 loops, best of 3: 166 ms per loop

You'll notice that this also tends to produce nicer looking results,
as the form of the solution is very structured with this method.

Aaron Meurer

On Mon, Sep 19, 2011 at 5:37 AM, David Joyner <[email protected]> wrote:
> Hi:
>
> Sympy's dsolve solves
>
> t = Symbol("t")
> y = Function("y")
> de = y(t).diff(t)+2*y(t) - t*exp(-2*t)
> dsolve(de,y(t))
> #y(t) == (C1 + t**2/2)*exp(-2*t)
>
> and
>
> t = Symbol("t")
> y = Function("y")
> de = y(t).diff(t)+2*y(t) - 2*cos(t)
> dsolve(de,y(t))
> #y(t) == (C1 + 2*exp(2*t)*sin(t)/5 + 4*exp(2*t)*cos(t)/5)*exp(-2*t)
>
> instantly, but when I try the sum
>
> t = Symbol("t")
> y = Function("y")
> de = y(t).diff(t)+2*y(t) - t*exp(-2*t) - 2*cos(t)
> dsolve(de,y(t))
> #y(t) == (C1 + t**2/2 + 2*exp(2*t)*sin(t)/5 + 4*exp(2*t)*cos(t)/5)*exp(-2*t)
>
> Sympy takes 5 seconds (or more). I tested this both
> with the debian sympy and with the latest spkg of Sympy in
> Sage (the spkg version was much slower). I'm also on
> a fairly old ubuntu machine.
>
> Not a complaint really, just curious why this happens.
>
> - David Joyner
>
> --
> 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.
>
>

-- 
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