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/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:2720b523-15ab-37f6-2d7d-ee707dc7f415 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | D530:A0D5B:ECEDA5:13A4E94:697C0957 |
| html-safe-nonce | a937f0749bcd151549bc50698b25d67359fb2f839e6d479ee6894d36a772e7bf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTMwOkEwRDVCOkVDRURBNToxM0E0RTk0OjY5N0MwOTU3IiwidmlzaXRvcl9pZCI6IjUzMzU5Nzc4MTE2NjIwODAzNDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 49aaae40a433049308083eeba75fb1169ffee18aa4f529b2488f07d8fea96389 |
| hovercard-subject-tag | pull_request:280131488 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,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/checks |
| 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 | da4f0ee56809799586f8ee546b27f94fe9b5893edfbf87732e82be45be013b52 |
| turbo-cache-control | no-preview |
| 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 full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3bd97d62125e07907af495e8aff5afc2a9f2a1a6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width