PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] how to print derivatives more compactly (subscript notation)

 August 18, 2022     latex, output, printing, python, sympy     No comments   

Issue

currently I'm struggeling a little bit with the output from SymPy. By default, by executing the following (assuming using Jupyter Notebook):

from sympy import *

t, x = symbols('t x')
u    = Function('u')(t, x)

display(Eq(I*u.diff(t) + u.diff(x,x) + abs(u)**2*u))

it prints

Default output

I want to have it like this, however

Desired output

in order to increase readability. Has anyone a clue how to achieve this? I'm rather new to SymPy and really would like to get this output.

Looking forward to your answers!

EDIT1:

I took the suggestions from @smichr, tweaked it a little and wrote it into a function. Hopefully I covered everything important. Here is the function

# Assuming that symbols and functions with greek letters are defined like this
# omega = Function('\\omega')(t, x)

def show(expr):
    functions = expr.atoms(Function)
    reps = {}

    for fun in functions:
        # Consider the case that some functions won't have the name
        # attribute e.g. Abs of an elementary function
        try:            
            reps[fun] = Symbol(fun.name) # Otherwise functions with greek symbols aren't replaced
        except AttributeError:
            continue

    dreps = [(deriv, Symbol(deriv.expr.subs(reps).name + "_{," + 
                            ''.join(par.name for par in deriv.variables) + "}"))  \
             for deriv in expr.atoms(Derivative)]

    # Ensure that higher order derivatives are replaced first, then lower ones. 
    # Otherwise you get d/dr w_r instead of w_rr
    dreps.sort(key=lambda x: len(x[0].variables), reverse=True)
    output = expr.subs(dreps).subs(reps)

    display(output)

zeta, eta = symbols('\\zeta \\eta')
psi       = Function('\\psi')(zeta, eta)

eq = Eq(I*psi.diff(zeta) + psi.diff(eta, eta) + abs(psi)**2*psi, 0)
show(eq)

which shows Edited functiuons output


Solution

The Derivative (like all SymPy objects) has arguments -- sometimes named -- and you can do with those whatever is useful to you. In this case it looks like replacing the function with a letter and the derivative variables as a composite symbol is what you want. Here is an attempt at that that can be tweaked as necessary:

>>> reps={u:'u'}
>>> dreps = [(i,i.expr.subs(reps).name+"_"+''.join(v.name for v in i.variables)
    for i in eq.atoms(Derivative)]
>>> eq.subs(dreps).subs(reps)
u*Abs(u)**2 + I*u_t + u_xx

Getting them is a certain order, however, requires changing the associated Printer. See, for example, here.



Answered By - smichr
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing