Skip to content

Commit

Permalink
Check if rational function
Browse files Browse the repository at this point in the history
  • Loading branch information
mph- committed Jun 3, 2024
1 parent d9b1e8e commit 30a9df5
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions lcapy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,10 +884,11 @@ def is_strictly_proper(self):
This will throw an exception if the expression is not a
rational function."""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return False

return self._ratfun.is_strictly_proper
return ratfun.is_strictly_proper

@property
def is_phase(self):
Expand Down Expand Up @@ -1812,6 +1813,13 @@ def _ratfun(self):
self.__ratfun = None
return self.__ratfun

def _ratfun_check(self):

ratfun = self._ratfun
if ratfun is None:
raise ValueError('Not a rational function: %s' % self)
return ratfun

@property
def ba(self):
"""Return lists of numerator and denominator coefficients."""
Expand Down Expand Up @@ -3032,10 +3040,11 @@ def poles(self, aslist=False, damping=None, pairs=False):

def _as_ZPK(self):

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return None, None, None, None

zeros, poles, K, undef = self._ratfun.as_ZPK()
zeros, poles, K, undef = ratfun.as_ZPK()

return zeros, poles, K, undef

Expand Down Expand Up @@ -3192,9 +3201,11 @@ def canonical(self, factor_const=False):
"""
if not self.expr.has(self.var):
return self
if self._ratfun is None:

ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.canonical(factor_const),
return self.__class__(ratfun.canonical(factor_const),
**self.assumptions)

def general(self):
Expand All @@ -3204,9 +3215,10 @@ def general(self):
See also canonical, partfrac, standard, timeconst, and ZPK."""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.general(), **self.assumptions)
return self.__class__(ratfun.general(), **self.assumptions)

def partfrac(self, combine_conjugates=False, pairs=False, damping=None,
method=None):
Expand All @@ -3232,9 +3244,10 @@ def partfrac(self, combine_conjugates=False, pairs=False, damping=None,
pairs = pairs or combine_conjugates

try:
if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.partfrac(pairs, damping, method),
return self.__class__(ratfun.partfrac(pairs, damping, method),
**self.assumptions)
except ValueError:
return self.as_sum().partfrac(pairs, damping, method)
Expand Down Expand Up @@ -3289,9 +3302,10 @@ def standard(self):
"""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.standard(), **self.assumptions)
return self.__class__(ratfun.standard(), **self.assumptions)

def mixedfrac(self):
"""This is an alias for standard and may be deprecated."""
Expand All @@ -3306,9 +3320,10 @@ def timeconst(self):
See also timeconst_terms, canonical, general, standard,
partfrac and ZPK."""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.timeconst(), **self.assumptions)
return self.__class__(ratfun.timeconst(), **self.assumptions)

def timeconst_terms(self):
"""Convert each term of expression into time constant form."""
Expand All @@ -3335,9 +3350,10 @@ def ZPK(self, pairs=False, combine_conjugates=False):
"""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.ZPK(combine_conjugates or pairs),
return self.__class__(ratfun.ZPK(combine_conjugates or pairs),
**self.assumptions)

def factored(self, pairs=False):
Expand All @@ -3354,9 +3370,10 @@ def factored(self, pairs=False):
"""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.ZPK(pairs), **self.assumptions)
return self.__class__(ratfun.ZPK(pairs), **self.assumptions)

def expandcanonical(self):
"""Expand in terms for different powers with each term
Expand All @@ -3366,9 +3383,10 @@ def expandcanonical(self):
See also canonical, general, partfrac, timeconst, and ZPK."""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return self.copy()
return self.__class__(self._ratfun.expandcanonical(), **self.assumptions)
return self.__class__(ratfun.expandcanonical(), **self.assumptions)

def expand_functions(self):
"""Expand functions in expression.
Expand Down Expand Up @@ -3442,10 +3460,11 @@ def degree(self):
This the maximum of the numerator and denominator degrees.
Note zero has a degree of -inf."""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return 1

return self._ratfun.degree
return ratfun.degree

@property
def Ndegree(self):
Expand All @@ -3457,10 +3476,11 @@ def Ndegree(self):
"""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return 1

return self._ratfun.Ndegree
return ratfun.Ndegree

@property
def Ddegree(self):
Expand All @@ -3470,10 +3490,11 @@ def Ddegree(self):
Note zero has a degree of -inf."""

if self._ratfun is None:
ratfun = self._ratfun
if ratfun is None:
return 1

return self._ratfun.Ddegree
return ratfun.Ddegree

def prune_HOT(self, degree):
"""Prune higher order terms if expression is a polynomial
Expand Down Expand Up @@ -3765,15 +3786,17 @@ def foo(coeffs):

def as_ratfun_delay(self):

B, A, delay, undef = self._ratfun.as_B_A_delay_undef()
ratfun = self._ratfun_check()
B, A, delay, undef = ratfun.as_B_A_delay_undef()
if undef != 1:
raise ValueError('Have undefined expression %s' % undef)

return self.__class__(B / A, **self.assumptions), delay

def _as_B_A_delay_undef(self):

return self._ratfun.as_B_A_delay_undef()
ratfun = self._ratfun_check()
return ratfun.as_B_A_delay_undef()

def continued_fraction_inverse_coeffs(self):
"""Convert expression into a continued fraction with inverse
Expand Down Expand Up @@ -3970,10 +3993,7 @@ def as_QRF(self, pairs=False, damping=None, method=None):
`method` can be 'sub' (substitution method, the default) or
'ec' (equating cofficients method)."""

ratfun = self._ratfun
if ratfun is None:
raise ValueError('Not a rational function', self)

ratfun = self._ratfun_check()
return ratfun.as_QRF(pairs, damping, method)

def as_QRPO(self, damping=None, method=None):
Expand All @@ -3986,10 +4006,7 @@ def as_QRPO(self, damping=None, method=None):
"""

ratfun = self._ratfun
if ratfun is None:
raise ValueError('Not a rational function', self)

ratfun = self._ratfun_check()
return ratfun.as_QRPO(damping, method)

def zero_initial_conditions(self):
Expand Down

0 comments on commit 30a9df5

Please sign in to comment.