Title: Change to code for ctrb and obsv by billtubbs · Pull Request #300 · python-control/python-control · GitHub
Open Graph Title: Change to code for ctrb and obsv by billtubbs · Pull Request #300 · python-control/python-control
X Title: Change to code for ctrb and obsv by billtubbs · Pull Request #300 · python-control/python-control
Description: This is not a bug fix. It's a minor speed improvement to statefbk.ctrb and statefbk.obsv. [It's the first time I have contributed to someone else's project so welcome feedback and advice if I'm not doing this correctly] Summary I noticed that you can avoid multiple hstack operations by creating a list first. I've done some testing and I think the behaviour of these methods is unchanged. See my test outputs below. I realise this is a very minor improvement so feel free to ignore. 1. Testing ctrb() Python 3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 15:43:19) Type 'copyright', 'credits' or 'license' for more information IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import numpy as np In [2]: import control In [3]: np.random.seed(1) In [4]: cum = 0 In [5]: for i in range(10): ...: A = np.random.randn(i, i) ...: B = np.random.randn(i, 1) ...: cum += control.ctrb(A, B).sum() ...: In [6]: cum Out[6]: -10066.24518631401 In [7]: control.__file__ Out[7]: '/Users/billtubbs/python-control/python-control/control/__init__.py' Results with official control package for comparison Python 3.6.7 | packaged by conda-forge | (default, Feb 28 2019, 02:16:08) Type 'copyright', 'credits' or 'license' for more information IPython 7.3.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import numpy as np In [2]: import control In [3]: np.random.seed(1) In [4]: cum = 0. In [5]: for i in range(10): ...: A = np.random.randn(i, i) ...: B = np.random.randn(i, 1) ...: cum += control.ctrb(A, B).sum() ...: In [6]: cum Out[6]: -10066.245186314005 In [7]: control.__file__ Out[7]: '/Users/billtubbs/anaconda3/envs/torch/lib/python3.6/site-packages/control/__init__.py' 2. Testing obsv() In [1]: import numpy as np In [2]: import control In [3]: control.__file__ Out[3]: '/Users/billtubbs/python-control/python-control/control/__init__.py' In [4]: np.random.seed(1) In [5]: cum = 0. In [6]: for i in range(10): ...: A = np.random.randn(i, i) ...: C = np.random.randn(1, i) ...: cum += control.obsv(A, C).sum() ...: In [7]: cum Out[7]: -7589.3515984634905 Results with official control package for comparison In [12]: np.random.seed(1) In [13]: cum = 0. In [14]: for i in range(10): ...: A = np.random.randn(i, i) ...: C = np.random.randn(1, i) ...: cum += control.obsv(A, C).sum() ...: In [15]: cum Out[15]: -7589.351598463492 In [16]: control.__file__ Out[16]: '/Users/billtubbs/anaconda3/envs/torch/lib/python3.6/site-packages/control/__init__.py' 3. Speed Tests (In Python 3.7 on a 2013 MacBook Pro) In [8]: def ctrb1(A, B): ...: amat = np.mat(A) ...: bmat = np.mat(B) ...: n = np.shape(amat)[0] ...: ctrb = bmat ...: for i in range(1, n): ...: ctrb = np.hstack((ctrb, amat**i*bmat)) ...: return ctrb ...: In [9]: def ctrb2(A, B): ...: amat = np.mat(A) ...: bmat = np.mat(B) ...: n = np.shape(amat)[0] ...: ctrb = np.hstack([bmat] + [amat**i*bmat for i in range(1, n)]) ...: return ctrb ...: In [11]: np.random.seed(1) In [12]: A = np.random.randn(8, 8) In [13]: B = np.random.randn(8, 1) In [14]: %timeit ctrb1(A, B) 456 µs ± 18 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) In [15]: %timeit ctrb2(A, B) 378 µs ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) In [16]: 1-378/456 Out[16]: 0.17105263157894735
Open Graph Description: This is not a bug fix. It's a minor speed improvement to statefbk.ctrb and statefbk.obsv. [It's the first time I have contributed to someone else's project so welcome feedback and advi...
X Description: This is not a bug fix. It's a minor speed improvement to statefbk.ctrb and statefbk.obsv. [It's the first time I have contributed to someone else's project so welcome feedb...
Opengraph URL: https://github.com/python-control/python-control/pull/300
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:6d4e45c5-2aef-8ec8-0177-60192b72420f |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | DA2E:F5F4F:2713F3:364D87:697B4053 |
| html-safe-nonce | 040b63db699ffa6057c9649a60ba4f8670b95c82b08c4040e41d4852dde76559 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQTJFOkY1RjRGOjI3MTNGMzozNjREODc6Njk3QjQwNTMiLCJ2aXNpdG9yX2lkIjoiNjI5ODU4ODcwOTc5MjcyNzEyMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 20870e388197e5ea54545bef18217eeac3d40ac7a63849422a78402318a9da00 |
| hovercard-subject-tag | pull_request:280131488 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python-control/python-control/pull/300/files |
| twitter:image | https://avatars.githubusercontent.com/u/7958850?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/7958850?s=400&v=4 |
| og:image:alt | This is not a bug fix. It's a minor speed improvement to statefbk.ctrb and statefbk.obsv. [It's the first time I have contributed to someone else's project so welcome feedback and advi... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6fe64d94889230be19a900846cfc91707e7dd4206878d6c208922b46a6b0735d |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python-control/python-control git https://github.com/python-control/python-control.git |
| octolytics-dimension-user_id | 2285872 |
| octolytics-dimension-user_login | python-control |
| octolytics-dimension-repository_id | 22791752 |
| octolytics-dimension-repository_nwo | python-control/python-control |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 22791752 |
| octolytics-dimension-repository_network_root_nwo | python-control/python-control |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 15736a47b83581130a149aa1b928696bea06adde |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width