Example #1
0
    def shape(self):
        """Returns a list with dimensions of each index.

        Dimensions is a property of the array, not of the indices.  Still, if
        the IndexedBase does not define a shape attribute, it is assumed that
        the ranges of the indices correspond to the shape of the array.

        >>> from sympy.tensor.indexed import IndexedBase, Idx
        >>> from sympy import symbols
        >>> n, m = symbols('n m', integer=True)
        >>> i = Idx('i', m)
        >>> j = Idx('j', m)
        >>> A = IndexedBase('A', shape=(n, n))
        >>> B = IndexedBase('B')
        >>> A[i, j].shape
        (n, n)
        >>> B[i, j].shape
        (m, m)
        """
        from sympy.solvers.solvers import _filldedent

        if self.base.shape:
            return self.base.shape
        try:
            return Tuple(*[i.upper - i.lower + 1 for i in self.indices])
        except AttributeError:
            raise IndexException(
                _filldedent("""
                Range is not defined for all indices in: %s""" % self))
        except TypeError:
            raise IndexException(
                _filldedent("""
                Shape cannot be inferred from Idx with
                undefined range: %s""" % self))
Example #2
0
    def __new__(cls, label, range=None, **kw_args):
        from sympy.solvers.solvers import _filldedent

        if isinstance(label, basestring):
            label = Symbol(label, integer=True)
        label, range = map(sympify, (label, range))

        if not label.is_integer:
            raise TypeError("Idx object requires an integer label.")

        elif is_sequence(range):
            if len(range) != 2:
                raise ValueError(
                    _filldedent("""
                    Idx range tuple must have length 2, but got %s""" %
                                len(range)))
            for bound in range:
                if not (bound.is_integer or abs(bound) is S.Infinity):
                    raise TypeError("Idx object requires integer bounds.")
            args = label, Tuple(*range)
        elif isinstance(range, Expr):
            if not (range.is_integer or range is S.Infinity):
                raise TypeError("Idx object requires an integer dimension.")
            args = label, Tuple(0, range - 1)
        elif range:
            raise TypeError(
                _filldedent("""
                The range must be an ordered iterable or
                integer SymPy expression."""))
        else:
            args = label,

        obj = Expr.__new__(cls, *args, **kw_args)
        return obj
Example #3
0
    def __new__(cls, label, range=None, **kw_args):
        from sympy.solvers.solvers import _filldedent

        if isinstance(label, basestring):
            label = Symbol(label, integer=True)
        label, range = map(sympify, (label, range))

        if not label.is_integer:
            raise TypeError("Idx object requires an integer label.")

        elif is_sequence(range):
            if len(range) != 2:
                raise ValueError(_filldedent("""
                    Idx range tuple must have length 2, but got %s""" % len(range)))
            for bound in range:
                if not (bound.is_integer or abs(bound) is S.Infinity):
                    raise TypeError("Idx object requires integer bounds.")
            args = label, Tuple(*range)
        elif isinstance(range, Expr):
            if not (range.is_integer or range is S.Infinity):
                raise TypeError("Idx object requires an integer dimension.")
            args = label, Tuple(0, range - 1)
        elif range:
            raise TypeError(_filldedent("""
                The range must be an ordered iterable or
                integer SymPy expression."""))
        else:
            args = label,

        obj = Expr.__new__(cls, *args, **kw_args)
        return obj
Example #4
0
    def shape(self):
        """Returns a list with dimensions of each index.

        Dimensions is a property of the array, not of the indices.  Still, if
        the IndexedBase does not define a shape attribute, it is assumed that
        the ranges of the indices correspond to the shape of the array.

        >>> from sympy.tensor.indexed import IndexedBase, Idx
        >>> from sympy import symbols
        >>> n, m = symbols('n m', integer=True)
        >>> i = Idx('i', m)
        >>> j = Idx('j', m)
        >>> A = IndexedBase('A', shape=(n, n))
        >>> B = IndexedBase('B')
        >>> A[i, j].shape
        (n, n)
        >>> B[i, j].shape
        (m, m)
        """
        from sympy.solvers.solvers import _filldedent

        if self.base.shape:
            return self.base.shape
        try:
            return Tuple(*[i.upper - i.lower + 1 for i in self.indices])
        except AttributeError:
            raise IndexException(_filldedent("""
                Range is not defined for all indices in: %s""" % self))
        except TypeError:
            raise IndexException(_filldedent("""
                Shape cannot be inferred from Idx with
                undefined range: %s""" % self))
Example #5
0
def conjugate_gauss_beams(wavelen, waist_in, waist_out, **kwargs):
    """
    Find the optical setup conjugating the object/image waists.

    Parameters
    ==========

    wavelen: the wavelength of the beam
    waist_in and waist_out: the waists to be conjugated
    f: the focal distance of the element used in the conjugation

    Returns
    =======

    A tuple containing (s_in, s_out, f)
     - s_in - distance before the optical element
     - s_out - distance after the optical element
     - f -  focal distance of the optical element

    Examples
    ========

    >>> from sympy.physics.gaussopt import conjugate_gauss_beams
    >>> from sympy import symbols, factor
    >>> l, w_i, w_o, f = symbols('l w_i w_o f')

    >>> conjugate_gauss_beams(l, w_i, w_o, f=f)[0]
    f*(-sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)) + 1)

    >>> factor(conjugate_gauss_beams(l, w_i, w_o, f=f)[1])
    f*w_o**2*(w_i**2/w_o**2 - sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)))/w_i**2

    >>> conjugate_gauss_beams(l, w_i, w_o, f=f)[2]
    f
    """
    #TODO add the other possible arguments
    wavelen, waist_in, waist_out = sympify((wavelen, waist_in, waist_out))
    m = waist_out / waist_in
    z = waist2rayleigh(waist_in, wavelen)
    if len(kwargs) != 1:
        raise ValueError("The function expects only one named argument")
    elif 'dist' in kwargs:
        raise NotImplementedError(
            _filldedent('''
            Currently only focal length is supported as a parameter'''))
    elif 'f' in kwargs:
        f = sympify(kwargs['f'])
        s_in = f * (1 - sqrt(1 / m**2 - z**2 / f**2))
        s_out = gaussian_conj(s_in, z, f)[0]
    elif 's_in' in kwargs:
        raise NotImplementedError(
            _filldedent('''
            Currently only focal length is supported as a parameter'''))
    else:
        raise ValueError(
            _filldedent('''
            The functions expects the focal length as a named argument'''))
    return (s_in, s_out, f)
Example #6
0
def conjugate_gauss_beams(wavelen, waist_in, waist_out, **kwargs):
    """
    Find the optical setup conjugating the object/image waists.

    Parameters
    ==========

    wavelen: the wavelength of the beam
    waist_in and waist_out: the waists to be conjugated
    f: the focal distance of the element used in the conjugation

    Returns
    =======

    A tuple containing (s_in, s_out, f)
     - s_in - distance before the optical element
     - s_out - distance after the optical element
     - f -  focal distance of the optical element

    Examples
    ========

    >>> from sympy.physics.gaussopt import conjugate_gauss_beams
    >>> from sympy import symbols, factor
    >>> l, w_i, w_o, f = symbols('l w_i w_o f')

    >>> conjugate_gauss_beams(l, w_i, w_o, f=f)[0]
    f*(-sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)) + 1)

    >>> factor(conjugate_gauss_beams(l, w_i, w_o, f=f)[1])
    f*w_o**2*(w_i**2/w_o**2 - sqrt(w_i**2/w_o**2 - pi**2*w_i**4/(f**2*l**2)))/w_i**2

    >>> conjugate_gauss_beams(l, w_i, w_o, f=f)[2]
    f
    """
    #TODO add the other possible arguments
    wavelen, waist_in, waist_out = sympify((wavelen, waist_in, waist_out))
    m = waist_out / waist_in
    z = waist2rayleigh(waist_in, wavelen)
    if len(kwargs) != 1:
        raise ValueError("The function expects only one named argument")
    elif 'dist' in kwargs:
        raise NotImplementedError(_filldedent('''
            Currently only focal length is supported as a parameter'''))
    elif 'f' in kwargs:
        f = sympify(kwargs['f'])
        s_in = f * (1 - sqrt(1/m**2 - z**2/f**2))
        s_out = gaussian_conj(s_in, z, f)[0]
    elif 's_in' in kwargs:
        raise NotImplementedError(_filldedent('''
            Currently only focal length is supported as a parameter'''))
    else:
        raise ValueError(_filldedent('''
            The functions expects the focal length as a named argument'''))
    return (s_in, s_out, f)
Example #7
0
 def __init__(self, *args):
     if len(args) == 1 and isinstance(args[0], Matrix) \
                       and args[0].shape == (2, 1):
         temp = args[0]
     elif len(args) == 2:
         temp = ((args[0],), (args[1],))
     else:
         raise ValueError(_filldedent('''
             Expecting 2x1 Matrix or the 2 elements of
             the Matrix but got %s''' % str(args)))
     Matrix.__init__(self, temp)
Example #8
0
    def __new__(cls, base, *args, **kw_args):
        from sympy.solvers.solvers import _filldedent

        if not args:
            raise IndexException("Indexed needs at least one index.")
        if isinstance(base, (basestring, Symbol)):
            base = IndexedBase(base)
        elif not isinstance(base, IndexedBase):
            raise TypeError(_filldedent("""
                Indexed expects string, Symbol or IndexedBase as base."""))
        return Expr.__new__(cls, base, *args, **kw_args)
Example #9
0
 def __init__(self, *args):
     if len(args) == 1 and isinstance(args[0], Matrix) \
                       and args[0].shape == (2, 1):
         temp = args[0]
     elif len(args) == 2:
         temp = ((args[0], ), (args[1], ))
     else:
         raise ValueError(
             _filldedent('''
             Expecting 2x1 Matrix or the 2 elements of
             the Matrix but got %s''' % str(args)))
     Matrix.__init__(self, temp)