Home > other >  How can I display variables on the same line with SymPy?
How can I display variables on the same line with SymPy?

Time:05-16

I have this code and i want to display it on the same line with text.

from sympy import *

c, x, L, y, a, b, E, xi, A, R, D, nu, q0, h, eta = symbols("c,x,L,y,a,b,E,xi,A,R,D,nu,q0,h,eta")
w = c * x / a * (1 - cos(2 * pi * y / b))

wx = diff(w, x)
wxx = diff(w, x, 2)

wy = diff(w, y)
wyy = diff(w, y, 2)


q = q0 * (1 - x / a)

display(w, q, wy)

When i display this it displays:

w

q

wy

I Want it to display w,q,wy such that i also can put in text. Does anyone know how to do that?

CodePudding user response:

Try this: first use init_printing(), the don't use display:

from sympy import *
init_printing()

c, x, L, y, a, b, E, xi, A, R, D, nu, q0, h, eta = symbols("c,x,L,y,a,b,E,xi,A,R,D,nu,q0,h,eta")
w = c * x / a * (1 - cos(2 * pi * y / b))

wx = diff(w, x)
wxx = diff(w, x, 2)

wy = diff(w, y)
wyy = diff(w, y, 2)


q = q0 * (1 - x / a)

w, q, wy

this will display them into a tupl, inline.

EDIT to accomodate comment. I personally wouldn't use it, as it might creates more trouble than it's worth down the road:

import inspect
from IPython.display import display, Math

def edisplay(var):
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    var_name = [var_name for var_name, var_val in callers_local_vars if var_val is var]
    var_name.sort()
    var_name = [v for v in var_name if v[0] != "_"]
    if len(var_name) == 0:
        raise ValueError
    lat = var_name[0]   "="   latex(var)
    display(Math(lat))

edisplay(q)
# out: q = q0*(1 - x/a)
edisplay(w)
# out: w = c*x*(1 - cos(2*pi*y/b))/a
  • Related