real_univar_taylor.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # real_univar_taylor.py: class implementing Taylor expansions of a unique real variable
  2. # Copyright 2022 Romain Serra
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import List, Optional
  15. from swiftt.taylor.real_multivar_taylor import RealMultivarTaylor
  16. from swiftt.taylor.complex_univar_taylor import ComplexUnivarTaylor
  17. from swiftt.interval import Interval
  18. from swiftt.taylor.taylor_expans_abstract import default_unknown_name
  19. class RealUnivarTaylor(ComplexUnivarTaylor, RealMultivarTaylor):
  20. """Class for Taylor expansions of a single real variable.
  21. """
  22. def __init__(self, order: int, var_name: List[str] = default_unknown_name) -> None:
  23. """Constructor for Taylor expansions of a single real variable. Result has no assigned coefficients.
  24. Args:
  25. order (int): order of expansion.
  26. var_name (List[str]): name of variable.
  27. """
  28. RealMultivarTaylor.__init__(self, 1, order, var_name)
  29. def __str__(self) -> str:
  30. """Method to cast Taylor expansion as a string. Overwrites parent implementation assuming more than one
  31. variable.
  32. Returns:
  33. str: string representing the Taylor expansion.
  34. """
  35. string = str(self.const)
  36. if not self.is_trivial():
  37. var_name = self._var_names[0]
  38. if self._coeff[1] != 0.:
  39. sign = " + " if self._coeff[1] > 0. else " - "
  40. string += sign + str(abs(self._coeff[1])) + " * " + var_name
  41. for j, el in enumerate(self._coeff[2:], 2):
  42. if el != 0.:
  43. sign = " + " if el > 0. else " - "
  44. string += sign + str(abs(el)) + " * " + var_name + "**" + str(j)
  45. return string + " + " + self.remainder_term
  46. def bounder(self, domain: Interval) -> Interval:
  47. """Method to evaluate the polynomial part of the Taylor expansion on a segment via interval arithmetic.
  48. Args:
  49. domain (Interval): input interval for expansion's variable.
  50. Returns:
  51. Interval: image of inputted interval through polynomial part.
  52. """
  53. if self.is_trivial():
  54. return Interval.singleton(self.const)
  55. # Horner's evaluation is not performed on purpose with intervals
  56. output = self.const + self._coeff[1] * domain
  57. for i, el in enumerate(self._coeff[2:], 2):
  58. output += el * domain**i
  59. return output
  60. def var_inserted(self, index_new_var: int, unknown_name: Optional[str] = None) -> RealMultivarTaylor:
  61. """Method for the addition of a new variable. Output lives in another algebra, of higher dimension. All its
  62. terms associated with the new variable are zero and the other ones are identical to original expansion.
  63. Overwrites the parent implementation to make sure the correct object is created (real expansion rather than
  64. complex).
  65. Args:
  66. index_new_var (int): index of new variable to be added.
  67. unknown_name (str): name of new variable.
  68. Returns:
  69. RealMultivarTaylor: Taylor expansion with an additional variable.
  70. """
  71. complex_expansion = ComplexUnivarTaylor.var_inserted(self, index_new_var, unknown_name)
  72. return RealMultivarTaylor.from_complex_expansion(complex_expansion)