-
Notifications
You must be signed in to change notification settings - Fork 88
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
Various fixes #293
Merged
Merged
Various fixes #293
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbe05a3
Merge pull request #4 from alchemyst/master
NicVDMey 971f222
added function to utils to calculate zero input and output directions…
NicVDMey 5db565f
Merge pull request #5 from alchemyst/master
NicVDMey fcae829
Modified sv_dir_plot to display bode plots of direction instead of ju…
NicVDMey 225b850
Fixed poles, zeros and mimotf_slice. Added multi_poly_lcm
NicVDMey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -383,8 +383,8 @@ def mimotf_slice(self, rows, cols): | |
result = [[] for r in range(nRows)] | ||
for r in range(nRows): | ||
for c in range(nCols): | ||
result[r].append(tf(list(self[r, c].numerator.coeffs), | ||
list(self[r, c].denominator.coeffs))) | ||
result[r].append(tf(list(self[rows[r], cols[c]].numerator.coeffs), | ||
list(self[rows[r], cols[c]].denominator.coeffs))) | ||
|
||
return mimotf(result) | ||
|
||
|
@@ -397,7 +397,7 @@ def poles(self): | |
>>> G = mimotf([[(s - 1) / (s + 2), 4 / (s + 2)], | ||
... [4.5 / (s + 2), 2 * (s - 1) / (s + 2)]]) | ||
>>> G.poles() | ||
[-2.0] | ||
array([-2.]) | ||
""" | ||
return poles(self) | ||
|
||
|
@@ -809,6 +809,39 @@ def polylcm(a, b): | |
lcm_roots = a_roots + b_roots | ||
return numpy.poly1d(lcm_roots, True) | ||
|
||
def multi_polylcm(P): | ||
roots_list = [i.r.tolist() for i in P] | ||
roots_by_mult = [] | ||
lcm_roots_by_mult = [] | ||
for roots in roots_list: | ||
root_builder = [] | ||
for root in roots: | ||
repeated = False | ||
for i in range(len(root_builder)): | ||
if abs(root_builder[i][0]-root) < 10**-3: | ||
root_builder[i][1] += 1 | ||
repeated = True | ||
break | ||
if not repeated: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern of using a flag is why Python's |
||
root_builder.append([root, 1]) | ||
for i in root_builder: | ||
roots_by_mult.append(i) | ||
for i in range(len(roots_by_mult)): | ||
in_lcm = False | ||
for j in range(len(lcm_roots_by_mult)): | ||
if abs(roots_by_mult[i][0] - lcm_roots_by_mult[j][0]) < 10**-3: | ||
in_lcm = True | ||
if lcm_roots_by_mult[j][1] < roots_by_mult[i][1]: | ||
lcm_roots_by_mult[j][1] = roots_by_mult[i][1] | ||
break | ||
if not in_lcm: | ||
lcm_roots_by_mult.append(roots_by_mult[i]) | ||
lcm_roots = [] | ||
for i in lcm_roots_by_mult: | ||
for j in range(i[1]): | ||
lcm_roots.append(i[0]) | ||
return numpy.poly1d(lcm_roots, True) | ||
|
||
def arrayfun(f, A): | ||
""" | ||
Recurses down to scalar elements in A, then applies f, returning lists | ||
|
@@ -2144,13 +2177,13 @@ def lcm_of_all_minors(G): | |
Returns the lowest common multiple of all minors of G | ||
''' | ||
Nrows, Ncols = G.shape | ||
lcm = 1 | ||
denoms = [] | ||
for i in range(1, min(Nrows, Ncols) + 1, 1): | ||
allminors = minors(G, i) | ||
for m in allminors: | ||
numer, denom = num_denom(m, symbolic_expr=True) | ||
lcm = sympy.lcm(lcm, denom) | ||
return lcm | ||
for j in allminors: | ||
if j.denominator.order > 0: | ||
denoms.append(j.denominator) | ||
return multi_polylcm(denoms) | ||
|
||
|
||
def poles(G): | ||
|
@@ -2174,18 +2207,15 @@ def poles(G): | |
>>> G = mimotf([[(s - 1) / (s + 2), 4 / (s + 2)], | ||
... [4.5 / (s + 2), 2 * (s - 1) / (s + 2)]]) | ||
>>> poles(G) | ||
[-2.0] | ||
array([-2.]) | ||
|
||
''' | ||
if not (type(G) == tf or type(G) == mimotf): | ||
G = sym2mimotf(G) | ||
|
||
lcm = lcm_of_all_minors(G) | ||
lcm_poly = sympy.Poly(lcm) | ||
lcm_coeff = [float(k) for k in lcm_poly.all_coeffs()] | ||
pole = numpy.roots(lcm_coeff) | ||
|
||
return list(set(pole)) | ||
|
||
return lcm.r | ||
|
||
|
||
def zeros(G=None, A=None, B=None, C=None, D=None): | ||
|
@@ -2241,8 +2271,12 @@ def zeros(G=None, A=None, B=None, C=None, D=None): | |
gcd = polygcd(gcd, numpy.poly1d(num_coeff)) | ||
else: | ||
gcd = poly1d(numer) | ||
zero = numpy.roots(gcd) | ||
return list(set(zero)) | ||
zero = list(set(numpy.roots(gcd))) | ||
pole = poles(G) | ||
for i in pole: | ||
if i in zero: | ||
zero.remove(i) | ||
return zero | ||
|
||
elif A is not None: | ||
M = numpy.bmat([[A, B], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
for x in range
is a bit of an antipattern in Python. Especially this formrange(len(
. Check out https://nedbatchelder.com/text/iter.html