ilayn / harold

An open-source systems and controls toolbox for Python3
MIT License
173 stars 19 forks source link

Getting exception when adding two discrete MISO transfer functions #68

Closed twmacro closed 2 years ago

twmacro commented 4 years ago

Hi again! :-)

I think the following code should work, but I'm getting an exception:

import harold
print(harold.__version__)
tf = (harold.Transfer([[[0.0], [1.0]]], [[[1.0], [1.0]]], 0.02)
    + harold.Transfer([[[1.0], [0.5]]], [[[1.0], [1.0]]], 0.02))
print(tf.polynomials)

Here is the output:

1.0.2.dev0+90a785b
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/envs/py38/lib/python3.8/site-packages/harold-1.0.2.dev0+90a785b-py3.8.egg/harold/_classes.py in __add__(self, other)
    404                 try:
--> 405                     return Transfer(self.to_array() + other.to_array(),
    406                                     dt=self._dt)

~/anaconda3/envs/py38/lib/python3.8/site-packages/harold-1.0.2.dev0+90a785b-py3.8.egg/harold/_classes.py in __init__(self, num, den, dt)
     64         (self._num, self._den,
---> 65          self._shape, self._isgain) = self.validate_arguments(num, den)
     66         self._p, self._m = self._shape

~/anaconda3/envs/py38/lib/python3.8/site-packages/harold-1.0.2.dev0+90a785b-py3.8.egg/harold/_classes.py in validate_arguments(num, den, verbose)
   1504             if returned_numden_list[0].size > returned_numden_list[1].size:
-> 1505                 raise ValueError('Noncausal transfer functions are not '
   1506                                  'allowed.')

ValueError: Noncausal transfer functions are not allowed.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-1-fe5c19b20276> in <module>
      1 import harold
      2 print(harold.__version__)
----> 3 tf = (harold.Transfer([[[0.0], [1.0]]], [[[1.0], [1.0]]], 0.02)
      4     + harold.Transfer([[[1.0], [0.5]]], [[[1.0], [1.0]]], 0.02))
      5 print(tf.polynomials)

~/anaconda3/envs/py38/lib/python3.8/site-packages/harold-1.0.2.dev0+90a785b-py3.8.egg/harold/_classes.py in __add__(self, other)
    406                                     dt=self._dt)
    407                 except ValueError:
--> 408                     raise ValueError('Shapes are not compatible for '
    409                                      'addition. Model shapes are {0} and'
    410                                      ' {1}'.format(self._shape, other.shape))

ValueError: Shapes are not compatible for addition. Model shapes are (1, 2) and (1, 2)
ilayn commented 4 years ago

Yeah probably again some static gain detection failure. Let me check quickly

ilayn commented 4 years ago

By the way you can directly create static gains like the following

F = harold.Transfer([[1, 0.5]], 0.02)
twmacro commented 4 years ago

Yep! That worked like a charm:

In [3]: harold.Transfer([[1, 0.5]], 0.02).polynomials
Out[3]: ([[array([[1.]]), array([[0.5]])]], [[array([[0.02]]), array([[0.02]])]])
ilayn commented 4 years ago

Hi Tim, this revealed yet another symptom of #30 so it will take a bit longer for me to revamp the Transfer class. So to let you know :)

twmacro commented 4 years ago

Understood. Thanks @ilayn! :)

ilayn commented 2 years ago

Well at least I wasn't lying about the time needed 😃 Sincere apologies for the ridiculous delay.