New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GSOC] Constant coefficient non-homogeneous system of ODEs solver #19341
[GSOC] Constant coefficient non-homogeneous system of ODEs solver #19341
Conversation
Added the constant coefficient non-homogeneous solver.
✅ Hi, I am the SymPy bot (v160). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.7. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
Updated the matching function to classify constant coefficient non-homogeneous functions as type2 and dsolve uses the new solver.
Moved the test cases from test_ode.py to test_systems.py
Some of the test cases from type1 were moved from test_ode.py to test_systems.py
The matching code for the removed solvers was deleted.
@oscarbenjamin @moorepants Except the final travis CI build and adding of few more test cases, the PR is almost completed. Please review and have a look. @namannimmo10 I hope this helps for your gsoc project. |
This commit may be squashed later.
Some of the matching function code was incorrectly removed and it was rectified in this PR.
@oscarbenjamin I don't know for what reason but the latex build is failing and I wasn't able to find out whats the problem. |
Have you tried building the latex docs yourself? This is the end of the build log:
Searching for that in the diff here I find: sympy/sympy/solvers/ode/systems.py Lines 434 to 435 in f93d88d
I don't think that the \right should be there. The way to use \right is like \left( \cos{x} \right) .
|
I did but I wasn't able to figure the error out.
I see, when I separately generated the equation, it worked and gave the right answer. Thanks for this help. |
|
||
e1, e2, e3 = symbols("e1:4", real=True) | ||
b1, b2, b3 = symbols("b1:4", real=True) | ||
v1, v2, v3 = symbols("v1:4", cls=Function, real=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than duplicating this code here and above I think that it would be better to make a function that returns the system and expected solution and then call that in each of the two test functions here.
eq1 = r1*c1*Derivative(x1(t), t) + x1(t) - x2(t) - r1*i | ||
eq2 = r2*c1*Derivative(x1(t), t) + r2*c2*Derivative(x2(t), t) + x2(t) - r2*i | ||
eq = [eq1, eq2] | ||
sol = [Eq(x1(t), -2*C1*c2*r2*exp(-t/(2*c2*r2) - t/(2*c2*r1) - t/(2*c1*r1) - t*sqrt(c1**2*r1**2 + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified using the script.
Some of the test cases used repeated code, this was rectified in this commit.
The big test cases were updated with smaller representations of their solutions.
I = Function("I") | ||
system = [Eq(V(t).diff(t), -1 / RC * V(t) + I(t) / C), Eq(I(t).diff(t), -R1 / L * I(t) - 1 / L * V(t) + Vs / L)] | ||
|
||
return system, dsolve(system), [RC, t, C, Vs, L, R1, V0, I0, V, I] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to define the expected solution here and return system, funcs, sol
and let the caller call dsolve
. The way this is set up dsolve
gets called twice because of the two tests that call this function.
|
||
eqs, dsolve_sol, symbols = _de_lorentz_solution() | ||
m, q, t, e1, e2, e3, b1, b2, b3, v1, v2, v3 = symbols | ||
x_1 = Integral(b1**3*b2*e2*q*exp(q*t*sqrt(-b1**2 - b2**2 - b3**2)/m)/(2*b1**3*b3*m + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This long expression for x_1
can be simplified further by running printsol
on it.
@@ -795,16 +1120,23 @@ def test_linear_3eq_order1_type4_skip(): | |||
Eq(diff(y(t), t), 2 * f * x(t) + (f + g) * y(t) - 2 * f * z(t)), Eq(diff(z(t), t), 5 * f * x(t) + f * y( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function shouldn't be called _skip
if it's not being skipped.
Updated one of the big test cases so that we call the dsolve method less number of times.
x_3 = exp(-t / (2 * c2 * r2) - t / (2 * c2 * r1) - t / (2 * c1 * r1) + t * sqrt(x_1) / (2 * c1 * c2 * r1 * r2)) | ||
x_4 = Integral(-i * r2 * exp( | ||
t / (2 * c2 * r2) + t / (2 * c2 * r1) + t / (2 * c1 * r1) + t * sqrt(x_1) / (2 * c1 * c2 * r1 * r2)) / sqrt( | ||
x_1), t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these lines are too long. They wouldn't need to be so long though if you didn't put all the spaces in:
x_4 = Integral(-i*r2*exp(t/(2*c2*r2) + t/(2*c2*r1) + t/(2*c1*r1) + t*sqrt(x_1)/(2*c1*c2*r1*r2))/sqrt(x_1), t)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example can be written as
_x1 = c1**2*r1**2 + 2*c1**2*r1*r2 + c1**2*r2**2 - 2*c1*c2*r1*r2 + 2*c1*c2*r2**2 + c2**2*r2**2
_x2 = Integral(i*r2*exp(-sqrt(_x1)*t/(2*c1*c2*r1*r2) + t/(2*c2*r2)
+ t/(2*c2*r1) + t/(2*c1*r1))/sqrt(_x1), t)
_x3 = Integral(-i*r2*exp(sqrt(_x1)*t/(2*c1*c2*r1*r2) + t/(2*c2*r2)
+ t/(2*c2*r1) + t/(2*c1*r1))/sqrt(_x1), t)
_x4 = exp(sqrt(_x1)*t/(2*c1*c2*r1*r2) - t/(2*c2*r2) - t/(2*c2*r1) - t/(2*c1*r1))
_x5 = exp(-sqrt(_x1)*t/(2*c1*c2*r1*r2) - t/(2*c2*r2) - t/(2*c2*r1) - t/(2*c1*r1))
sol = [
Eq(x1(t),
- 2*C1*_x5*c2*r2/(sqrt(_x1) + c1*r1 + c1*r2 - c2*r2)
- 2*C2*_x4*c2*r2/(-sqrt(_x1) + c1*r1 + c1*r2 - c2*r2)
- 2*_x2*_x4*c2*r2/(-sqrt(_x1) + c1*r1 + c1*r2 - c2*r2)
- 2*_x3*_x5*c2*r2/(sqrt(_x1) + c1*r1 + c1*r2 - c2*r2)),
Eq(x2(t), C1*_x5 + C2*_x4 + _x2*_x4 + _x3*_x5),
]
x_2 = 1/(2*b1**2*m*exp(q*t*sqrt(-b1**2 - b2**2 - b3**2)/m) + 2*b2**2*m*exp(q*t*sqrt(-b1**2 - b2**2 - b3**2)/m) + | ||
2*b3**2*m*exp(q*t*sqrt(-b1**2 - b2**2 - b3**2)/m)) | ||
x_3 = exp(q*t*sqrt(-b1**2 - b2**2 - b3**2)/m) | ||
x_4 = sqrt(b1**2 + b2**2 + b3**2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example can be written as
x1 = sqrt(b1**2 + b2**2 + b3**2)
x2 = exp(q*t*sqrt(-b1**2 - b2**2 - b3**2)/m)
x3 = 1/(b1**2*x1*x2 + b2**2*x1*x2)
x4 = 1/(2*b1**2*m*x2 + 2*b2**2*m*x2 + 2*b3**2*m*x2)
x5 = Integral(
b1*b3*e1*q/(b1**2*m + b2**2*m + b3**2*m)
+ b2*b3*e2*q/(b1**2*m + b2**2*m + b3**2*m)
+ b3**2*e3*q/(b1**2*m + b2**2*m + b3**2*m), t)
x6 = 1/(2*b1**3*b3*m + 2*I*b1**2*b2*m*x1 + 2*b1*b2**2*b3*m
+ 2*b1*b3**3*m + 2*I*b2**3*m*x1 + 2*I*b2*b3**2*m*x1)
x7 = Integral(
b1**2*e3*q*x4 - b1*b3*e1*q*x4 + I*b1*e2*q*x1*x4
+ b2**2*e3*q*x4 - b2*b3*e2*q*x4 - I*b2*e1*q*x1*x4, t)
x8 = Integral(
b1**3*b2*e2*q*x2*x6 - b1**2*b2**2*e1*q*x2*x6 - b1**2*b3**2*e1*q*x2*x6
- I*b1**2*b3*e2*q*x1*x2*x6 + b1**2*e3*q*x2/(2*b1**2*m + 2*b2**2*m + 2*b3**2*m)
+ b1*b2**3*e2*q*x2*x6 - b2**4*e1*q*x2*x6 - b2**2*b3**2*e1*q*x2*x6
- I*b2**2*b3*e2*q*x1*x2*x6 + b2**2*e3*q*x2/(2*b1**2*m + 2*b2**2*m + 2*b3**2*m), t)
sol = [
Eq(v1(t),
C1*b1/b3
- I*C2*b1**2*b2*x3 - C2*b1*b3*x1*x3 - I*C2*b2**3*x3 - I*C2*b2*b3**2*x3
+ I*C3*b1**2*b2*x2/(b1**2*x1 + b2**2*x1) - C3*b1*b3*x1*x2/(b1**2*x1 + b2**2*x1)
+ I*C3*b2**3*x2/(b1**2*x1 + b2**2*x1) + I*C3*b2*b3**2*x2/(b1**2*x1 + b2**2*x1)
+ I*b1**2*b2*x2*x7/(b1**2*x1 + b2**2*x1) - I*b1**2*b2*x3*x8
- b1*b3*x1*x2*x7/(b1**2*x1 + b2**2*x1) - b1*b3*x1*x3*x8 + b1*x5/b3
+ I*b2**3*x2*x7/(b1**2*x1 + b2**2*x1) - I*b2**3*x3*x8
+ I*b2*b3**2*x2*x7/(b1**2*x1 + b2**2*x1) - I*b2*b3**2*x3*x8),
Eq(v2(t),
C1*b2/b3
+ C2*b1*sqrt(-b1**2 - b2**2 - b3**2)/(b1**2*x2 + b2**2*x2)
- C2*b2*b3/(b1**2*x2 + b2**2*x2) - I*C3*b1*x1*x2/(b1**2 + b2**2)
- C3*b2*b3*x2/(b1**2 + b2**2) - I*b1*x1*x2*x7/(b1**2 + b2**2)
+ b1*x8*sqrt(-b1**2 - b2**2 - b3**2)/(b1**2*x2 + b2**2*x2)
- b2*b3*x2*x7/(b1**2 + b2**2) - b2*b3*x8/(b1**2*x2 + b2**2*x2) + b2*x5/b3),
Eq(v3(t), C1 + C3*x2 + x2*x7 + x5 + (C2 + x8)*exp(-q*t*sqrt(-b1**2 - b2**2 - b3**2)/m)),
]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I will update this.
I've put the simplifying script in #19574 and improved it a bit. |
Updated those test case solutions with better and efficient substitutions.
b76beb1
to
fac6758
Compare
@oscarbenjamin All checks have passed. |
+ 2 * b1 * b3 ** 3 * m + 2 * I * b2 ** 3 * m * x1 + 2 * I * b2 * b3 ** 2 * m * x1) | ||
x7 = Integral( | ||
b1 ** 2 * e3 * q * x4 - b1 * b3 * e1 * q * x4 + I * b1 * e2 * q * x1 * x4 | ||
+ b2 ** 2 * e3 * q * x4 - b2 * b3 * e2 * q * x4 - I * b2 * e1 * q * x1 * x4, t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you've added all of these spaces. Is that something that your editor does automatically? It's easier to read with the spacing from str and careful line breaks:
x7 = Integral(b1**2*e3*q*x4 - b1*b3*e1*q*x4 + I*b1*e2*q*x1*x4
+ b2**2*e3*q*x4 - b2*b3*e2*q*x4 - I*b2*e1*q*x1*x4, t)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most probably it's the problem with my editor, I would get it fixed.
I would merge this but github won't let me. It hasn't updated since Travis passed the tests. |
I've edited the release note and closed/reopened to restart the tests. Hopefully github will detect when the tests are complete on Travis (#19589) |
I'm still unable to merge this even though the tests have passed (again)... |
Apparently commenting updated the PR and made it mergeable! |
References to other Issues or PRs
Fixes #9244
Fixes #8859
Fixes #8567
Fixes #19150
Brief description of what is fixed or changed
Added linear first-order constant-coefficient non-homogeneous system of ODEs solver.
Other comments
The SymPy's ode module will be able to solve a number of linear first-order constant coefficient non-homogenous systems of ODEs
For example:
Release Notes
dsolve
there is no a general solver that can solve systems of constant coefficient non-homogeneous first order ODEs of any size in terms of integrals.