# split display of long function on few lines

HI

Sorry in advance for this question rather basic pythonic than SageMath. I would like to split display of f(x) to appear on more than one line of code, to be more readable. I have been trying without success for half an hour ;-(

W10 SageMath 9.2 notebook

code on SageCell Server

(surely I'm gonna loose lot of karma points with this question !)

var('x')

f(x)=1/2*x^2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi)))-1/2*x^2*arcsin(1/2*sqrt(-3*x^2 + 3)/sqrt(-x^2 + 1))- 1/2*sqrt(-x^2 - x^2*sin(1/9*pi)^2/cos(1/9*pi)^2 + 1)*x*sin(1/9*pi)/cos(1/9*pi)+ 1/4*sqrt(-1/4*x^2 + 1/4)*sqrt(-3*x^2 + 3)- 1/2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi)))+ 1/2*arcsin(1/2*sqrt(-3*x^2 + 3)/sqrt(-x^2 + 1))

#f(x)=1/2*x^2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi)))-
#1/2*x^2*arcsin(1/2*sqrt(-3*x^2 + 3)/sqrt(-x^2 + 1))-
#1/2*sqrt(-x^2 - x^2*sin(1/9*pi)^2/cos(1/9*pi)^2 + 1)*x*sin(1/9*pi)/cos(1/9*pi)+
#1/4*sqrt(-1/4*x^2 + 1/4)*sqrt(-3*x^2 + 3)-
#1/2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi)))+
#1/2*arcsin(1/2*sqrt(-3*x^2 + 3)/sqrt(-x^2 + 1))

#f(x)=1/2*x^2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi)))
#- 1/2*x^2*arcsin(1/2*sqrt(-3*x^2 + 3)/sqrt(-x^2 + 1))
#- 1/2*sqrt(-x^2 - x^2*sin(1/9*pi)^2/cos(1/9*pi)^2 + 1)*x*sin(1/9*pi)/cos(1/9*pi)
#+ 1/4*sqrt(-1/4*x^2 + 1/4)*sqrt(-3*x^2 + 3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)^4 + 2*tan(1/18*pi)^4 - 2*sqrt(2)*tan(1/18*pi)^2 - 2*tan(1/18*pi)^2 + sqrt(2) + 2)*tan(1/18*pi)^2 + 2*tan(1/18*pi)^2 + 2*sqrt(2)*sqrt(sqrt(2)*tan(1/18*pi)^4 + 2*tan(1/18*pi)^4 - 2*sqrt(2)*tan(1/18*pi)^2 - 2*tan(1/18*pi)^2 + sqrt(2) + 2) - 2*sqrt(2) - 4*sqrt(sqrt(2)*tan(1/18*pi)^4 + 2*tan(1/18*pi)^4 - 2*sqrt(2)*tan(1/18*pi)^2 - 2*tan(1/18*pi)^2 + sqrt(2) + 2) + 5)/((tan(1/18*pi)^2 + 1)*sqrt(-2*sqrt(2) + 3))))^2 + 1)*sqrt(-sqrt(2) + 2)

yUp=cos(2*arctan(1/4*sqrt(2)*sqrt(3*tan(1/18*pi)^4 + 2*tan(1/18*pi)^2 - sqrt(9*tan(1/18*pi)^8 + 12*tan(1/18*pi)^6 - 42*tan(1/18*pi)^4 + 12*tan(1/18*pi)^2 + 9) + 3)/tan(1/18*pi)))

intZXYAboveFirstTrianglePiece1=numerical_integral(f,yLow,yUp, max_points=100)
show("numerical integration : ",intZXYAboveFirstTrianglePiece1)

expression1=f
operandsList=[]
def print_tree(expr,k=5):
op = expr.operator()
operands = expr.operands()
if op:
#print k*' ---',op, operands
k += 1
for operand in operands:
#print_tree(operand,k)
operandsList.append(operand)
print_tree(expression1)
#show(operandsList)
index=0
for o in operandsList :
show("i :\t",index," :\t"," :\t",o)
index+=1

plt=plot(f,(x,yLow,yUp))
#show(plt,xmin=yLow,xmax=yUp,ymin=-0.8,ymax=0.8)

for o in operandsList :
plt+=plot(o,(x,yLow,yUp),color='grey',linestyle='dashed')
show(plt,xmin=yLow,xmax=yUp,ymin=-0.8,ymax=0.8)

edit retag close merge delete

Sort by ยป oldest newest most voted

This horror is a start... which can be quite certainly enhanced. Unfortunately, it doesn't display in ask.sagemath.org's implementation of Mathjax ; follow the link.

show(LatexExpr("f(x)=\\displaystyle\\begin{array}{l}%s\\end{array}."%"\\\\".join([str(latex(u)) for u in f(x).operands()])))

more

@Emmanuel Charpentier , horror is an understatement !. and it does not answer my question because you still have to write f on a single line of code.

do you know the reason to not be able to write f(x) in six code lines?

( 2021-01-13 17:16:10 +0100 )edit

I would like to split display of f(x) to appear on more than one line of code

That's what I did. Now, if you want to write (i. e. input) f(x) on more than one line, you need to use a backslash (\) as a continuation character just before a newline :

sage: foo = 1/2*x^2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi))) \
....:     -1/2*x^2*arcsin(1/2*sqrt(-3*x^2 + 3)/sqrt(-x^2 + 1)) \
....:      -1/2*sqrt(-x^2 - x^2*sin(1/9*pi)^2/cos(1/9*pi)^2 + 1)*x*sin(1/9*pi)/cos(1/9*pi) \
....:      + 1/4*sqrt(-1/4*x^2 + 1/4)*sqrt(-3*x^2 + 3) \
....:      -1/2*arcsin(x*sin(1/9*pi)/(sqrt(-x^2 + 1)*cos(1/9*pi))) \
....:       + 1/2*arcsin(1 ...
(more)
( 2021-01-13 17:54:47 +0100 )edit

Note :

show(LatexExpr("f(x)=\\begin{array}{l}%s\\end{array}."%"\\\\".join(["\\displaystyle%s"%str(latex(u)) for u in f(x).operands()])))


may be considered slightly less horrible.

( 2021-01-13 17:56:54 +0100 )edit

Yes I should have written:I would like to split the code display of f(x) to appear on more than one line of code.

I tried backslash (look at my code all which is commented has been tested) in notebook without success. try it in SageCell: comment f(x) on one line (line number 3) and un-comment line 19 to 24. you will get: SyntaxError: invalid syntax

but what I dont understand is why when I copy the code from SageCell server with the modification of comment I told just above (click on the link I provide above in the question ) into a sage shell i run into lots of errors !!

( 2021-01-14 08:04:20 +0100 )edit

That's it I just understood !, I don't have the right to write f(x) = but if I just write f= (as you write with foo=) it's okay. thank you @Emmanuel Charpentier

( 2021-01-14 09:29:44 +0100 )edit