Refactrored poly2str and poly2hstr

master
Per A Brodtkorb 8 years ago
parent dfae5bf3d7
commit 23e3c5f29b

@ -442,8 +442,8 @@ class LevelCrossings(PlotData):
max_L = max(L) max_L = max(L)
maxi = max(abs(r_[min_L, max_L])) * epsilon maxi = max(abs(r_[min_L, max_L])) * epsilon
mini = -maxi mini = -maxi
nu = 101
u = linspace(mini, maxi, 101) u = linspace(mini, maxi, nu)
G = cdfnorm(u) # (1 + erf(u / sqrt(2))) / 2 G = cdfnorm(u) # (1 + erf(u / sqrt(2))) / 2
G = G * (1 - G) G = G * (1 - G)
@ -451,7 +451,7 @@ class LevelCrossings(PlotData):
factor1 = 1. / sqrt(1 - x ** 2) factor1 = 1. / sqrt(1 - x ** 2)
factor2 = 1. / (1 + x) factor2 = 1. / (1 + x)
integral = zeros(u.shape, dtype=float) integral = zeros(u.shape, dtype=float)
for i in range(len(integral)): for i in range(nu):
y = factor1 * exp(-u[i] * u[i] * factor2) y = factor1 * exp(-u[i] * u[i] * factor2)
integral[i] = trapz(y, x) integral[i] = trapz(y, x)
# end # end

@ -629,11 +629,44 @@ def poly2hstr(p, variable='x'):
>>> import wafo.polynomial as wp >>> import wafo.polynomial as wp
>>> wp.poly2hstr([1, 1, 2], 's' ) >>> wp.poly2hstr([1, 1, 2], 's' )
'(s + 1)*s + 2' '(s + 1)*s + 2'
>>> wp.poly2hstr([2, 1, 2, 1], 's' )
'((2*s + 1)*s + 2)*s + 1'
>>> wp.poly2hstr([2, 0, 2, 1], 's' )
'(2*s**2 + 2)*s + 1'
See also See also
-------- --------
poly2str poly2str
""" """
def _append_coef(s, coef, expon, var):
# Is it the first term?
isfirst = s == ''
# Add sign, but we don't need a leading plus-sign.
if isfirst:
if coef < 0:
s = '-' # Unary minus.
else:
sgn = '-' if coef < 0 else '+'
s = '{0:s} {1:s} '.format(s, sgn)
# We need the coefficient only if it is different from 1 or -1 or
# when it is the constant term.
needcoef = ((abs(coef) != 1) | (expon == 0) & isfirst) | 1 - isfirst
# Append the coefficient if it is different from one or when it is
# the constant term.
if needcoef:
s = '{0:s}{1:.20g}'.format(s, abs(coef))
# We need the variable except in the constant term.
needvar = expon != 0
# Append variable if necessary.
if needvar:
# Append a multiplication sign if necessary.
if needcoef:
if 1 - isfirst:
s = '({0:s})'.format(s)
s = '{0:s}*'.format(s)
s = '{0:s}{1:s}'.format(s, var)
return s
var = variable var = variable
coefs = polytrim(np.atleast_1d(p)) coefs = polytrim(np.atleast_1d(p))
@ -649,44 +682,9 @@ def poly2hstr(p, variable='x'):
else: else:
# Append exponent if necessary. # Append exponent if necessary.
if ix > 1: if ix > 1:
exponstr = '%.0f' % ix s = '{0:s}**{1:d}'.format(s, ix)
s = '%s**%s' % (s, exponstr)
ix = 1 ix = 1
# Is it the first term? s = _append_coef(s, coef, expon, var)
isfirst = s == ''
# We need the coefficient only if it is different from 1 or -1 or
# when it is the constant term.
needcoef = ((abs(coef) != 1) |
(expon == 0) & isfirst) | 1 - isfirst
# We need the variable except in the constant term.
needvar = (expon != 0)
# Add sign, but we don't need a leading plus-sign.
if isfirst:
if coef < 0:
s = '-' # % Unary minus.
else:
if coef < 0:
s = '%s - ' % s # % Binary minus (subtraction).
else:
s = '%s + ' % s # % Binary plus (addition).
# Append the coefficient if it is different from one or when it is
# the constant term.
if needcoef:
coefstr = '%.20g' % abs(coef)
s = '%s%s' % (s, coefstr)
# Append variable if necessary.
if needvar:
# Append a multiplication sign if necessary.
if needcoef:
if 1 - isfirst:
s = '(%s)' % s
s = '%s*' % s
s = '%s%s' % (s, var)
# Now treat the special case where the polynomial is zero. # Now treat the special case where the polynomial is zero.
if s == '': if s == '':
@ -720,52 +718,68 @@ def poly2str(p, variable='x'):
>>> import wafo.polynomial as wp >>> import wafo.polynomial as wp
>>> wp.poly2str([1, 1, 2], 's' ) >>> wp.poly2str([1, 1, 2], 's' )
's**2 + s + 2' 's**2 + s + 2'
>>> wp.poly2str([2, 1, 2, 0, 1], 's' )
'2*s**4 + s**3 + 2*s**2 + 1'
""" """
thestr = "0"
var = variable
# Remove leading zeros
coeffs = polytrim(np.atleast_1d(p))
N = len(coeffs) - 1 def _coefstr_0(coefstr, k):
for k in range(N+1):
coefstr = '%.4g' % abs(coeffs[k])
if coefstr[-4:] == '0000':
coefstr = coefstr[:-5]
power = (N - k)
if power == 0:
if coefstr != '0': if coefstr != '0':
newstr = '%s' % (coefstr,) newstr = '{0:s}'.format(coefstr)
else: else:
newstr = '0' if k == 0 else '' newstr = '0' if k == 0 else ''
elif power == 1: return newstr
def _coefstr_1(coefstr, var):
if coefstr == '0': if coefstr == '0':
newstr = '' newstr = ''
elif coefstr in ['b', '1']: elif coefstr in ['b', '1']:
newstr = var newstr = var
else: else:
newstr = '%s*%s' % (coefstr, var) newstr = '{0:s}*{1:s}'.format(coefstr, var)
else: return newstr
def _coefstr_n(coefstr, var, power):
if coefstr == '0': if coefstr == '0':
newstr = '' newstr = ''
elif coefstr in ['b', '1']: elif coefstr in ['b', '1']:
newstr = '%s**%d' % (var, power,) newstr = '{0:s}**{1:d}'.format(var, power)
else: else:
newstr = '%s*%s**%d' % (coefstr, var, power) newstr = '{0:s}*{1:s}**{2:d}'.format(coefstr, var, power)
return newstr
def _add_strings(thestr, newstr, ck, k):
if k > 0: if k > 0:
if newstr != '': if newstr != '':
if coeffs[k] < 0: sgn = '-' if ck < 0 else '+'
thestr = "%s - %s" % (thestr, newstr) thestr = "{0:s} {1:s} {2:s}".format(thestr, sgn, newstr)
else: elif (k == 0) and (newstr != '') and (ck < 0):
thestr = "%s + %s" % (thestr, newstr) thestr = "-{0:s}".format(newstr)
elif (k == 0) and (newstr != '') and (coeffs[k] < 0):
thestr = "-%s" % (newstr,)
else: else:
thestr = newstr thestr = newstr
return thestr return thestr
thestr = "0"
var = variable
# Remove leading zeros
coeffs = polytrim(np.atleast_1d(p))
N = len(coeffs) - 1
for k, ck in enumerate(coeffs):
coefstr = '%.4g' % abs(ck)
if coefstr[-4:] == '0000':
coefstr = coefstr[:-5]
power = (N - k)
if power == 0:
newstr = _coefstr_0(coefstr, k)
elif power == 1:
newstr = _coefstr_1(coefstr, var)
else:
newstr = _coefstr_n(coefstr, var, power)
thestr = _add_strings(thestr, newstr, ck, k)
return thestr
def polyshift(py, a=-1, b=1): def polyshift(py, a=-1, b=1):
""" """

Loading…
Cancel
Save