Example #1
0
def _indexed_to_symbol(idx: sp.Indexed) -> sp.Symbol:
    base_name, _, _ = str(idx).rpartition("[")
    subscript = ",".join(map(str, idx.indices))
    if len(idx.indices) > 1:
        base_name = translate(base_name)
        subscript = "_{" + subscript + "}"
    return sp.Symbol(f"{base_name}{subscript}")
    def _print_VectorSymbol(self, expr, vec_symbol=None):
        # print("_print_VecSymbol", type(expr), expr)

        # this method is used to print:
        # 1. VectorSymbol instances, that uses the vec_symbol provided in the settings.
        # 2. The unit vector symbol (normalized vector), that uses uni_vec_symbol. In this
        #    case, the expression in a string representing the expression given into 
        #    _print_Normalize()
        if expr in self._settings['symbol_names']:
            return self._settings['symbol_names'][expr]
        if hasattr(expr, "name"):
            # this has been adapted from _deal_with_super_sub
            string = expr.name
            result = expr.name
            if not '{' in string and string != r"\nabla":
                name, supers, subs = split_super_sub(string)

                name = translate(name)
                supers = [translate(sup) for sup in supers]
                subs = [translate(sub) for sub in subs]
                if vec_symbol is None:
                    if expr._vec_symbol == "":
                        vec_symbol = self._settings["vec_symbol"]
                    else:
                        vec_symbol = expr._vec_symbol

                result = vec_symbol % name
                # glue all items together:
                if supers:
                    result += "^{%s}" % " ".join(supers)
                if subs:
                    result += "_{%s}" % " ".join(subs)

            if expr._bold:
                result = r"\mathbf{{{}}}".format(result)
            if expr._italic:
                result = r"\mathit{%s}" % result

            return result
        return vec_symbol % expr
Example #3
0
def test_translate():
    s = 'Alpha'
    assert translate(s) == 'A'
    s = 'Beta'
    assert translate(s) == 'B'
    s = 'Eta'
    assert translate(s) == 'H'
    s = 'omicron'
    assert translate(s) == 'o'
    s = 'Pi'
    assert translate(s) == r'\Pi'
    s = 'pi'
    assert translate(s) == r'\pi'
    s = 'LamdaHatDOT'
    assert translate(s) == r'\dot{\hat{\Lambda}}'
Example #4
0
def test_translate():
    s = "Alpha"
    assert translate(s) == "A"
    s = "Beta"
    assert translate(s) == "B"
    s = "Eta"
    assert translate(s) == "H"
    s = "omicron"
    assert translate(s) == "o"
    s = "Pi"
    assert translate(s) == r"\Pi"
    s = "pi"
    assert translate(s) == r"\pi"
    s = "LamdaHatDOT"
    assert translate(s) == r"\dot{\hat{\Lambda}}"
Example #5
0
def test_translate():
    s = 'Alpha'
    assert translate(s) == 'A'
    s = 'Beta'
    assert translate(s) == 'B'
    s = 'Eta'
    assert translate(s) == 'H'
    s = 'omicron'
    assert translate(s) == 'o'
    s = 'Pi'
    assert translate(s) == r'\Pi'
    s = 'pi'
    assert translate(s) == r'\pi'
Example #6
0
def test_translate():
    s = 'Alpha'
    assert translate(s) == 'A'
    s = 'Beta'
    assert translate(s) == 'B'
    s = 'Eta'
    assert translate(s) == 'H'
    s = 'omicron'
    assert translate(s) == 'o'
    s = 'Pi'
    assert translate(s) == r'\Pi'
    s = 'pi'
    assert translate(s) == r'\pi'
Example #7
0
    def _print_Function(self, expr, exp=None):
        from sympy.physics.vector.functions import dynamicsymbols
        func = expr.func.__name__
        t = dynamicsymbols._t

        if hasattr(self, '_print_' + func):
            return getattr(self, '_print_' + func)(expr, exp)
        elif isinstance(type(expr), UndefinedFunction) and (expr.args
                                                            == (t, )):

            name, supers, subs = split_super_sub(func)
            name = translate(name)
            supers = [translate(sup) for sup in supers]
            subs = [translate(sub) for sub in subs]

            if len(supers) != 0:
                supers = r"^{%s}" % "".join(supers)
            else:
                supers = r""

            if len(subs) != 0:
                subs = r"_{%s}" % "".join(subs)
            else:
                subs = r""

            if exp:
                supers += r"^{%s}" % self._print(exp)

            return r"%s" % (name + supers + subs)
        else:
            args = [str(self._print(arg)) for arg in expr.args]
            # How inverse trig functions should be displayed, formats are:
            # abbreviated: asin, full: arcsin, power: sin^-1
            inv_trig_style = self._settings['inv_trig_style']
            # If we are dealing with a power-style inverse trig function
            inv_trig_power_case = False
            # If it is applicable to fold the argument brackets
            can_fold_brackets = self._settings['fold_func_brackets'] and \
                len(args) == 1 and \
                not self._needs_function_brackets(expr.args[0])

            inv_trig_table = ["asin", "acos", "atan", "acot"]

            # If the function is an inverse trig function, handle the style
            if func in inv_trig_table:
                if inv_trig_style == "abbreviated":
                    func = func
                elif inv_trig_style == "full":
                    func = "arc" + func[1:]
                elif inv_trig_style == "power":
                    func = func[1:]
                    inv_trig_power_case = True

                    # Can never fold brackets if we're raised to a power
                    if exp is not None:
                        can_fold_brackets = False

            if inv_trig_power_case:
                name = r"\operatorname{%s}^{-1}" % func
            elif exp is not None:
                name = r"\operatorname{%s}^{%s}" % (func, exp)
            else:
                name = r"\operatorname{%s}" % func

            if can_fold_brackets:
                name += r"%s"
            else:
                name += r"\left(%s\right)"

            if inv_trig_power_case and exp is not None:
                name += r"^{%s}" % exp

            return name % ",".join(args)
Example #8
0
    def _print_Function(self, expr, exp=None):
        from sympy.physics.vector.functions import dynamicsymbols
        func = expr.func.__name__
        t = dynamicsymbols._t

        if hasattr(self, '_print_' + func):
            return getattr(self, '_print_' + func)(expr, exp)
        elif isinstance(type(expr), UndefinedFunction) and (expr.args == (t,)):

            name, supers, subs = split_super_sub(func)
            name = translate(name)
            supers = [translate(sup) for sup in supers]
            subs = [translate(sub) for sub in subs]

            if len(supers) != 0:
                supers = r"^{%s}" % "".join(supers)
            else:
                supers = r""

            if len(subs) != 0:
                subs = r"_{%s}" % "".join(subs)
            else:
                subs = r""

            if exp:
                supers += r"^{%s}" % self._print(exp)

            return r"%s" % (name + supers + subs)
        else:
            args = [str(self._print(arg)) for arg in expr.args]
            # How inverse trig functions should be displayed, formats are:
            # abbreviated: asin, full: arcsin, power: sin^-1
            inv_trig_style = self._settings['inv_trig_style']
            # If we are dealing with a power-style inverse trig function
            inv_trig_power_case = False
            # If it is applicable to fold the argument brackets
            can_fold_brackets = self._settings['fold_func_brackets'] and \
                len(args) == 1 and \
                not self._needs_function_brackets(expr.args[0])

            inv_trig_table = ["asin", "acos", "atan", "acot"]

            # If the function is an inverse trig function, handle the style
            if func in inv_trig_table:
                if inv_trig_style == "abbreviated":
                    func = func
                elif inv_trig_style == "full":
                    func = "arc" + func[1:]
                elif inv_trig_style == "power":
                    func = func[1:]
                    inv_trig_power_case = True

                    # Can never fold brackets if we're raised to a power
                    if exp is not None:
                        can_fold_brackets = False

            if inv_trig_power_case:
                name = r"\operatorname{%s}^{-1}" % func
            elif exp is not None:
                name = r"\operatorname{%s}^{%s}" % (func, exp)
            else:
                name = r"\operatorname{%s}" % func

            if can_fold_brackets:
                name += r"%s"
            else:
                name += r"\left(%s\right)"

            if inv_trig_power_case and exp is not None:
                name += r"^{%s}" % exp

            return name % ",".join(args)
Example #9
0
 def _print_Domain(self, expr):
     return translate(expr.name)