<filename>concept/variables/function.py
"""
   Function objects behave as variables.

.. module:: function
    :platform: Unix, Windows
    :synopis: represents functions behaving like variables

.. moduleauthor:: <NAME> <<EMAIL>>

   =======
   License
   =======
   Copyright (c) 2017 <NAME>

   Permission is hereby granted, free of charge, to any person obtaining a copy of this
   software and associated documentation files (the "Software"), to deal in the Software
   without restriction, including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
   to whom the Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all copies
   or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import math
from concept.variables.variable import Var


class Function(Var):
    """class as base for function classes keeping a decorated function or variable calculating a value."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        if not (isinstance(value, Var) or issubclass(value.__class__, Var)):
            raise TypeError("wrong type")

        super(Function, self).__init__(value)

    def __repr__(self):
        """String representation of this or derived class."""
        return "%s(%s)" % (self.__class__.__name__, self.value)


class Square(Function):
    """Class that calculates the square of the decorated function or variable."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        super(Square, self).__init__(value)

    def get(self):
        """Return f(x) = x*x."""
        return self.value.get() ** 2


class Sqrt(Function):
    """Class that calculates the square root of the decorated function or variable."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        super(Sqrt, self).__init__(value)

    def get(self):
        """Return f(x) = sqrt(x)."""
        return math.sqrt(self.value.get())


class Sin(Function):
    """Class that calculates the sin of the decorated function or variable."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        super(Sin, self).__init__(value)

    def get(self):
        """Return f(x) = x*x."""
        return math.sin(self.value.get())

<filename>concept/variables/function.py
"""
   Function objects behave as variables.

.. module:: function
    :platform: Unix, Windows
    :synopis: represents functions behaving like variables

.. moduleauthor:: <NAME> <<EMAIL>>

   =======
   License
   =======
   Copyright (c) 2017 <NAME>

   Permission is hereby granted, free of charge, to any person obtaining a copy of this
   software and associated documentation files (the "Software"), to deal in the Software
   without restriction, including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
   to whom the Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all copies
   or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import math
from concept.variables.variable import Var


class Function(Var):
    """class as base for function classes keeping a decorated function or variable calculating a value."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        if not (isinstance(value, Var) or issubclass(value.__class__, Var)):
            raise TypeError("wrong type")

        super(Function, self).__init__(value)

    def __repr__(self):
        """String representation of this or derived class."""
        return "%s(%s)" % (self.__class__.__name__, self.value)


class Square(Function):
    """Class that calculates the square of the decorated function or variable."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        super(Square, self).__init__(value)

    def get(self):
        """Return f(x) = x*x."""
        return self.value.get() ** 2


class Sqrt(Function):
    """Class that calculates the square root of the decorated function or variable."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        super(Sqrt, self).__init__(value)

    def get(self):
        """Return f(x) = sqrt(x)."""
        return math.sqrt(self.value.get())


class Sin(Function):
    """Class that calculates the sin of the decorated function or variable."""

    def __init__(self, value):
        """Initializing with a variable or function (like this class)."""
        super(Sin, self).__init__(value)

    def get(self):
        """Return f(x) = x*x."""
        return math.sin(self.value.get())

Function objects behave as variables.  .. module:: function     :platform: Unix, Windows     :synopis: represents functions behaving like variables  .. moduleauthor:: <NAME> <<EMAIL>>     =======    License    =======    Copyright (c) 2017 <NAME>     Permission is hereby granted, free of charge, to any person obtaining a copy of this    software and associated documentation files (the "Software"), to deal in the Software    without restriction, including without limitation the rights to use, copy, modify, merge,    publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons    to whom the Software is furnished to do so, subject to the following conditions:     The above copyright notice and this permission notice shall be included in all copies    or substantial portions of the Software.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    DAMAGES OR OTHER LIABILITY,    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class as base for function classes keeping a decorated function or variable calculating a value. Initializing with a variable or function (like this class). String representation of this or derived class. Class that calculates the square of the decorated function or variable. Initializing with a variable or function (like this class). Return f(x) = x*x. Class that calculates the square root of the decorated function or variable. Initializing with a variable or function (like this class). Return f(x) = sqrt(x). Class that calculates the sin of the decorated function or variable. Initializing with a variable or function (like this class). Return f(x) = x*x.