Example #1
0
def scriptedsymbol( base, superscripts=(), subscripts=(), make_symbolic=False ):
    try:
        try:
            base.expand() # see if it's an expression
        except AttributeError: # if not
            base = SR( base ) # it is now
        name = str( base )
        latex_name = curly_protect( base )
    except ValueError: # SR fails if it's 'lambda'
        name = base
        latex_name = base
    if make_symbolic:
        subscripts = [ SR(s) for s in subscripts ]
        superscripts = [ SR(s) for s in superscripts ]
    #name, latex_name = str(base), curly_protect(base)
    import sys
    #print base, 'sub', subscripts, 'super', superscripts; sys.stdout.flush()
    if len(superscripts) > 0:
        name += '^' + '^'.join(str(s) for s in superscripts)
        if len(superscripts) > 1:
            latex_name += '^{%s}' % ''.join(curly_protect(s) for s in superscripts)
        else:
            latex_name += '^{%s}' % latex(superscripts[0])
    if len(subscripts) > 0:
        name += '_' + '_'.join(str(s) for s in subscripts)
        if len(subscripts) > 1:
            latex_name += '_{%s}' % ''.join(curly_protect(s) for s in subscripts)
        else:
            latex_name += '_{%s}' % latex(subscripts[0])
    return SR.symbol( name, latex_name=latex_name )
Example #2
0
def curly_protect( s ):
    if isinstance(s, basestring):
        lx = s
    else:
        lx = latex(s)
    while lx[0] == '{' and lx[-1] == '}':
        lx = lx[1:-1]
    return '{%s}'%lx
Example #3
0
    def arithmetic( self, ex, operator ):
	if operator == (2*SR.symbol('x')).operator():
	    ## too simple? sort factors so that things whose latex string
	    ## starts with '\\' are before the pure alphabetical ones.
	    ll = sorted( ex.operands(), key=lambda v: latex(v).replace('\\',' ') )
	    ## 1 ??
	    if len( ll ) > 1: ll = [ l for l in ll if l != 1 ]
	    ## -1 ?? this is weird, guess I don't understand signs in pynac
	    lm = [ l for l in ll if l == -1 ]
	    if lm:
		ll = [ l for l in ll if l != -1 ]
		ll[0] *= reduce( lambda x,y:x*y, lm, 1 )
	    ## don't know a way to enforce order of arguments to multiply
	    ## operator, so create a fake variable whose latex string is
	    ## the desired product.
	    ## thus the expression returned by this converter is suitable
	    ## only for printing in latex, not for doing math or anything
	    ## else with.
	    Msym = SR.symbol( 'M_{}'.format( ZZ.random_element(1e+10) ), latex_name=' '.join(latex(v) for v in ll) )
	    print latex(ex), ' ==> ', latex(Msym)
	    return Msym
	    ##
	    factors = set( ex.operands() )
	    greek_factors = set( [ v for v in factors if
		( latex(v) == '\\'+str(v) or latex(v) == '\\lambda' )
	    ] )
	    return wrap_latex( ' '.join(
		[ latex(v) for v in ( sorted( greek_factors ) + sorted( factors - greek_factors ) ) ]
	    ), 'math' )
	else:
	    return operator( *map(self, ex.operands()) )
Example #4
0
    def _latex_(self):
        r"""
        LaTeX representation of self.

        EXAMPLES::

            sage: latex(CuspForms(3, 24).hecke_algebra()) # indirect doctest
            \mathbf{T}_{\text{\texttt{Cuspidal...Gamma0(3)...24...}
        """
        from sage.misc.latex import latex
        return "\\mathbf{T}_{%s}" % latex(self.__M)
Example #5
0
def xform_symbol(v, xform_str, xform_latex):
    """little utility function to do things like add a hat or similar thing
    to a Sage variable, e.g. make X_i into \hat{X}_i."""
    try:
        # is it a symbol?
        if not v.is_symbol():
            # no, it's a more complex expression
            raise ValueError( str(v) + ' is not a symbol' )
    except AttributeError:
        # it's a string
        v = SR.symbol(v)
    import re
    name = str(v)
    mn = re.match( '^([^^_]*)(.*?)$', name )
    if mn: name = xform_str( mn.group(1), mn.group(2) )
    #name = re.sub( '^([^_]*)(.*?)$', r'\g<1>'+flair+'\g<2>', str(v) )
    latex_name = latex(v)
    #latex_name = re.sub( r'^([^_]*)(.*?)$', r'\\'+flair+'{\g<1>}\g<2>', latex(v) )
    ml = re.match( r'^([^^_]*)(.*?)$', latex_name )
    latex_name = xform_latex( ml.group(1), ml.group(2) )
    return SR.symbol( name, latex_name=latex_name )
Example #6
0
def display_object(sage_object, link=True):
    """
    EXAMPLES::

        sage: from reproducible_object import ReproducibleObject
        sage: display_object(ReproducibleObject("1"), link=True)
        {'url': 'http:/1', 'style': 'latex', 'data': '1'}
        sage: display_object(ReproducibleObject("1"), link=False)
        {'style': 'latex', 'data': '1'}
        sage: display_object(ReproducibleObject("Partition([1])"))
        {'url': 'http:/Partition([1])', 'style': 'text', 'data': '[1]'}
        sage: display_object(ReproducibleObject("[1,2,3]"))
        {'style': 'list', 'data': [{'url': 'http:/[1,2,3][0]', 'style': 'latex', 'data': '1'},
                                   {'url': 'http:/[1,2,3][1]', 'style': 'latex', 'data': '2'},
                                   {'url': 'http:/[1,2,3][2]', 'style': 'latex', 'data': '3'}]}
    """
    if isinstance(sage_object.value, (list, tuple)):
        return {
            "style": "list",
            "data" : [display_object(sage_object[i], link = True) for i in range(len(sage_object.value))]
            }
    s = str(latex(sage_object.value))
    # This logic is about limitations of mathjax; should this it in the template?
    if any(forbidden in s for forbidden in sage.misc.latex.latex.mathjax_avoid_list()):
        s = repr(sage_object.value)
        result = {
            "style": "2Dtext" if "\n" in s else "text",
            "data" : s,
            }
    else:
        result = {
            "style": "latex",
            "data" : s,
            }
    if link:
        result["url"] = sage_object.url()
    return result
Example #7
0
def greek_first_latex(ex):
    return latex( greek_first_latex_ex(ex) )
Example #8
0
 def _latex_(self):
     return ( '\\left(\\begin{array}{c}\n%s\n\\end{array}\\right)' %
       '\\\\\n'.join( '  %s' % latex(e) for e in self.vector ) )
Example #9
0
def latex_math( o ):
    if isinstance(o, basestring): return o
    try: return o.latex_math()
    except AttributeError: return latex( o )