<commit_before># -*- coding: utf-8 -*-
"""
==================================================================
Abstract data structures for math (:mod:`sknano.tools._coremath`)
==================================================================

.. currentmodule:: sknano.tools._coremath

"""
from __future__ import absolute_import, division, print_function
__docformat__ = 'restructuredtext en'

try:
    from pint import UnitRegistry
    ureg = UnitRegistry()
    Qty = ureg.Quantity
except ImportError:
    Qty = None

import numpy as np

from ._corefuncs import check_type
from ._luts import xyz

__all__ = ['Point', 'Point2D', 'Point3D',
           'Vector', 'Vector2D', 'Vector3D',
           'Quaternion']


class Point(object):
    """Create a point in :math:`R^3`

    Parameters
    ----------
    x, y, z : float, optional
        :math:`x, y, z` coordinates of point in :math:`R^3` space.
    units : {None, str}, optional
        Units of coordinates.

    """
    def __init__(self, x=None, y=None, z=None, with_units=False,
                 units=None, dtype=None):
        self._p = np.zeros(3, dtype=float)
        self._units = units
        for i, pi in enumerate((x, y, z)):
            if pi is not None:
                self._p[i] = pi

        if with_units and (Qty is None or units is None):
            with_units = False
        self._with_units = with_units
        self._units = units

        if with_units:
            self._p = Qty(self._p, units)

    def __str__(self):
        if self._with_units:
            return str(self._p)
        else:
            return repr(self)

    def __repr__(self):
        if self._with_units:
            return("Point(x={!r}, y={!r}, z={!r}, with_units={!r}, "
                   "units={!r})".format(self.x.magnitude, self.y.magnitude,
                                        self.z.magnitude, True, self._units))
        else:
            return("Point(x={!r}, y={!r}, z={!r})".format(
                self.x, self.y, self.z))

    def __iter__(self):
        return iter(self._p)

    def __len__(self):
        return len(self._p)

    @property
    def x(self):
        """:math:`x`-coordinate of `Point`.

        Returns
        -------
        float
            :math:`x`-coordinate of `Point`.

        """
        return self._p[0]

    @x.setter
    def x(self, value=float):
        """Set :math:`x`-coordinate of `Point`.

        Parameters
        ----------
        value : float
            :math:`x`-coordinate of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(int, float))
            try:
                self._p[0] = value
            except ValueError:
                self._p[0] = Qty(value, self._units)
        except TypeError as e:
            print(e)

    @property
    def y(self):
        """:math:`y`-coordinate of `Point`.

        Returns
        -------
        float
            :math:`y`-coordinate of `Point`.

        """
        return self._p[1]

    @y.setter
    def y(self, value=float):
        """Set :math:`y`-coordinate of `Point`.

        Parameters
        ----------
        value : float
            :math:`y`-coordinate of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(int, float))
            try:
                self._p[1] = value
            except ValueError:
                self._p[1] = Qty(value, self._units)
        except TypeError as e:
            print(e)

    @property
    def z(self):
        """:math:`z`-coordinate of `Point`.

        Returns
        -------
        float
            :math:`z`-coordinate of `Point`.

        """
        return self._p[2]

    @z.setter
    def z(self, value=float):
        """Set :math:`z`-coordinate of `Point`.

        Parameters
        ----------
        value : float
            :math:`z`-coordinate of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(int, float))
            try:
                self._p[2] = value
            except ValueError:
                self._p[2] = Qty(value, self._units)
        except TypeError as e:
            print(e)

    @property
    def coords(self):
        """:math:`x, y, z` coordinates of `Point`.

        Returns
        -------
        :py:class:`~numpy:numpy.ndarray`
            3-element :py:class:`~numpy:numpy.ndarray` of [:math:`x, y, z`]
            coordinates of `Point`.

        """
        return self._p

    @coords.setter
    def coords(self, value=np.ndarray):
        """Set :math:`x, y, z` coordinates of `Point`

        Parameters
        ----------
        value : :py:class:`~numpy:numpy.ndarray`
            3-element :py:class:`~numpy:numpy.ndarray` of [:math:`x, y, z`]
            coordinates of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(np.ndarray,))
            for i, pi in enumerate(value):
                try:
                    check_type(pi, allowed_types=(int, float))
                    try:
                        self._p[i] = pi
                    except ValueError:
                        self._p[i] = Qty(pi, self._units)
                except TypeError as e:
                    print(e)
        except TypeError as e:
            print(e)

    def fix_minus_zero_coords(self, epsilon=1.0e-10):
        """Set `Point` coordinates that are small, negative numbers to zero.

        Set `Point` coordinates that are negative and have absolute value
        less than `epsilon` to zero.

        Parameters
        ----------
        epsilon : float, optional
            Smallest allowed absolute value of any :math:`x,y,z` coordinate.

        """
        p = self._p.tolist()
        for i, pi in enumerate(p[:]):
            if pi < 0 and abs(pi) < epsilon:
                try:
                    self._p[i] = 0.0
                except ValueError:
                    self._p[i] = Qty(0.0, self._units)

    def rezero_coords(self, epsilon=1.0e-10):
        """Re-zero `Point` coordinates near zero.

        Set `Point` coordinates with absolute value less than `epsilon` to
        zero.

        Parameters
        ----------
        epsilon : float, optional
            Smallest allowed absolute value of any :math:`x,y,z` coordinate.

        """
        p = self._p.tolist()
        for i, pi in enumerate(p[:]):
            if abs(pi) < epsilon:
                try:
                    self._p[i] = 0.0
                except ValueError:
                    self._p[i] = Qty(0.0, self._units)


Point3D = Point


class Point2D(object):
    """Create a point in :math:`R^2`

    Parameters
    ----------
    x, y : float, optional
        :math:`x, y` coordinates of point in :math:`R^2` space.
    units : {None, str}, optional
        Units of coordinates.

    """
    def __init__(self, x=None, y=None, with_units=False, units=None,
                 dtype=None):
        self._p = np.zeros(2, dtype=float)
        self._units = units
        for i, pi in enumerate((x, y)):
            if pi is not None:
                self._p[i] = pi

        if with_units and (Qty is None or units is None):
            with_units = False
        self._with_units = with_units
        self._units = units

        if with_units:
            self._p = Qty(self._p, units)

    def __str__(self):
        if self._with_units:
            return str(self._p)
        else:
            return repr(self)

    def __repr__(self):
        if self._with_units:
            return("Point(x={!r}, y={!r}, with_units={!r}, "
                   "units={!r})".format(self.x.magnitude, self.y.magnitude,
                                        True, self._units))
        else:
            return("Point(x={!r}, y={!r})".format(self.x, self.y))

    def __iter__(self):
        return iter(self._p)

    def __len__(self):
        return len(self._p)

    @property
    def x(self):
        """:math:`x`-coordinate of `Point2D`.

        Returns
        -------
        float
            :math:`x`-coordinate of `Point2D`.

        """
        return self._p[0]

    @x.setter
    def x(self, value=float):
        """Set :math:`x`-coordinate of `Point2D`.

        Parameters
        ----------
        value : float
            :math:`x`-coordinate of `Point2D`.

        """
        try:
            value = value.magnitude<commit_msg>fixing Point2D __repr__ method<commit_after># -*- coding: utf-8 -*-
"""
==================================================================
Abstract data structures for math (:mod:`sknano.tools._coremath`)
==================================================================

.. currentmodule:: sknano.tools._coremath

"""
from __future__ import absolute_import, division, print_function
__docformat__ = 'restructuredtext en'

try:
    from pint import UnitRegistry
    ureg = UnitRegistry()
    Qty = ureg.Quantity
except ImportError:
    Qty = None

import numpy as np

from ._corefuncs import check_type
from ._luts import xyz

__all__ = ['Point', 'Point2D', 'Point3D',
           'Vector', 'Vector2D', 'Vector3D',
           'Quaternion']


class Point(object):
    """Create a point in :math:`R^3`

    Parameters
    ----------
    x, y, z : float, optional
        :math:`x, y, z` coordinates of point in :math:`R^3` space.
    units : {None, str}, optional
        Units of coordinates.

    """
    def __init__(self, x=None, y=None, z=None, with_units=False,
                 units=None, dtype=None):
        self._p = np.zeros(3, dtype=float)
        self._units = units
        for i, pi in enumerate((x, y, z)):
            if pi is not None:
                self._p[i] = pi

        if with_units and (Qty is None or units is None):
            with_units = False
        self._with_units = with_units
        self._units = units

        if with_units:
            self._p = Qty(self._p, units)

    def __str__(self):
        if self._with_units:
            return str(self._p)
        else:
            return repr(self)

    def __repr__(self):
        if self._with_units:
            return("Point(x={!r}, y={!r}, z={!r}, with_units={!r}, "
                   "units={!r})".format(self.x.magnitude, self.y.magnitude,
                                        self.z.magnitude, True, self._units))
        else:
            return("Point(x={!r}, y={!r}, z={!r})".format(
                self.x, self.y, self.z))

    def __iter__(self):
        return iter(self._p)

    def __len__(self):
        return len(self._p)

    @property
    def x(self):
        """:math:`x`-coordinate of `Point`.

        Returns
        -------
        float
            :math:`x`-coordinate of `Point`.

        """
        return self._p[0]

    @x.setter
    def x(self, value=float):
        """Set :math:`x`-coordinate of `Point`.

        Parameters
        ----------
        value : float
            :math:`x`-coordinate of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(int, float))
            try:
                self._p[0] = value
            except ValueError:
                self._p[0] = Qty(value, self._units)
        except TypeError as e:
            print(e)

    @property
    def y(self):
        """:math:`y`-coordinate of `Point`.

        Returns
        -------
        float
            :math:`y`-coordinate of `Point`.

        """
        return self._p[1]

    @y.setter
    def y(self, value=float):
        """Set :math:`y`-coordinate of `Point`.

        Parameters
        ----------
        value : float
            :math:`y`-coordinate of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(int, float))
            try:
                self._p[1] = value
            except ValueError:
                self._p[1] = Qty(value, self._units)
        except TypeError as e:
            print(e)

    @property
    def z(self):
        """:math:`z`-coordinate of `Point`.

        Returns
        -------
        float
            :math:`z`-coordinate of `Point`.

        """
        return self._p[2]

    @z.setter
    def z(self, value=float):
        """Set :math:`z`-coordinate of `Point`.

        Parameters
        ----------
        value : float
            :math:`z`-coordinate of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(int, float))
            try:
                self._p[2] = value
            except ValueError:
                self._p[2] = Qty(value, self._units)
        except TypeError as e:
            print(e)

    @property
    def coords(self):
        """:math:`x, y, z` coordinates of `Point`.

        Returns
        -------
        :py:class:`~numpy:numpy.ndarray`
            3-element :py:class:`~numpy:numpy.ndarray` of [:math:`x, y, z`]
            coordinates of `Point`.

        """
        return self._p

    @coords.setter
    def coords(self, value=np.ndarray):
        """Set :math:`x, y, z` coordinates of `Point`

        Parameters
        ----------
        value : :py:class:`~numpy:numpy.ndarray`
            3-element :py:class:`~numpy:numpy.ndarray` of [:math:`x, y, z`]
            coordinates of `Point`.

        """
        try:
            value = value.magnitude
        except AttributeError:
            pass

        try:
            check_type(value, allowed_types=(np.ndarray,))
            for i, pi in enumerate(value):
                try:
                    check_type(pi, allowed_types=(int, float))
                    try:
                        self._p[i] = pi
                    except ValueError:
                        self._p[i] = Qty(pi, self._units)
                except TypeError as e:
                    print(e)
        except TypeError as e:
            print(e)

    def fix_minus_zero_coords(self, epsilon=1.0e-10):
        """Set `Point` coordinates that are small, negative numbers to zero.

        Set `Point` coordinates that are negative and have absolute value
        less than `epsilon` to zero.

        Parameters
        ----------
        epsilon : float, optional
            Smallest allowed absolute value of any :math:`x,y,z` coordinate.

        """
        p = self._p.tolist()
        for i, pi in enumerate(p[:]):
            if pi < 0 and abs(pi) < epsilon:
                try:
                    self._p[i] = 0.0
                except ValueError:
                    self._p[i] = Qty(0.0, self._units)

    def rezero_coords(self, epsilon=1.0e-10):
        """Re-zero `Point` coordinates near zero.

        Set `Point` coordinates with absolute value less than `epsilon` to
        zero.

        Parameters
        ----------
        epsilon : float, optional
            Smallest allowed absolute value of any :math:`x,y,z` coordinate.

        """
        p = self._p.tolist()
        for i, pi in enumerate(p[:]):
            if abs(pi) < epsilon:
                try:
                    self._p[i] = 0.0
                except ValueError:
                    self._p[i] = Qty(0.0, self._units)


Point3D = Point


class Point2D(object):
    """Create a point in :math:`R^2`

    Parameters
    ----------
    x, y : float, optional
        :math:`x, y` coordinates of point in :math:`R^2` space.
    units : {None, str}, optional
        Units of coordinates.

    """
    def __init__(self, x=None, y=None, with_units=False, units=None,
                 dtype=None):
        self._p = np.zeros(2, dtype=float)
        self._units = units
        for i, pi in enumerate((x, y)):
            if pi is not None:
                self._p[i] = pi

        if with_units and (Qty is None or units is None):
            with_units = False
        self._with_units = with_units
        self._units = units

        if with_units:
            self._p = Qty(self._p, units)

    def __str__(self):
        if self._with_units:
            return str(self._p)
        else:
            return repr(self)

    def __repr__(self):
        if self._with_units:
            return("Point2D(x={!r}, y={!r}, with_units={!r}, "
                   "units={!r})".format(self.x.magnitude, self.y.magnitude,
                                        True, self._units))
        else:
            return("Point2D(x={!r}, y={!r})".format(self.x, self.y))

    def __iter__(self):
        return iter(self._p)

    def __len__(self):
        return len(self._p)

    @property
    def x(self):
        """:math:`x`-coordinate of `Point2D`.

        Returns
        -------
        float
            :math:`x`-coordinate of `Point2D`.

        """
        return self._p[0]

    @x.setter
    def x(self, value=float):
        """Set :math:`x`-coordinate of `Point2D`.

        Parameters
        ----------
        value : float
            :math:`x`-coordinate of `Point2D`.

        """
        try:
            value = value.magnitude
{'fraction_non_alphanumeric': 0.1471109333028519, 'fraction_numerical': 0.005429502200377207, 'mean_word_length': 1.4904639908909765, 'pattern_counts': {'":': 10, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4195417', 'n_tokens_mistral': 5312, 'n_tokens_neox': 5051, 'n_words': 1292}