Ask Your Question
0

Why show() is not working when we are using with print?

asked 2016-11-30 03:34:53 +0100

daviddgl gravatar image

updated 2017-08-01 12:17:24 +0100

FrédéricC gravatar image

Why show() is not working when we are using with print?

I have written a program to find a matrix as a result.

When I have finished and was tring to polish the output, I ended up the situation to use,

print " The matrix is ", show(A)

The output is just

The matrix is

its not giving the matrix.

if I use show() without print function, its giving the matrix.

How to fix this issue??

Thanks for helping..

Workdone:

As William Stein has given in his answer, I have used,

sys.stdout.flush()

after print function. But it didnt help me.

I will give my subroutine, since if there is any bug in the code also give a bug in show() funciton.

A=matrix(Integers(12),[[1,0,3,1],[0,2,0,3],[0,0,6,1],[0,0,0,2]])
S24=[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]
C2=zero_matrix(Integers(12),6,6)
for i in range(6):
    for j in range(6):
        C2[i,j]=A.matrix_from_rows_and_columns(S24[i],S24[j]).det()
print "The compound matrix C2(A)=", '\n'
sys.stdout.flush()
show(C2)
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2016-11-30 03:56:39 +0100

kcrisman gravatar image

updated 2016-11-30 17:16:31 +0100

Note that in the question you reference, the show() functions are not in the same line as the print statements. The return value of show is what would go in the print statement, but what you want is what the show function does, which depends on whether you are in a notebook, etc. Embedded in a print statement I wouldn't know what to expect in general.

See this example in the Sage cell server to see what I mean:

a = 1
print a
print show(latex(a))
show(latex(a))

Note that this one does seem to do something for the print, but probably this depends on the frontend. Also notice the None printed at the end - that is what show returns.

Edit: For what you want to do, you might want something along the lines of this:

a = matrix(2,[1,2,3,4])
pretty_print(html("This is the matrix $%s$"%(latex(a))))

Sorry that it wasn't clear to me initially what you actually desired as output.

edit flag offensive delete link more

Comments

@kcrisman Is there anyway to achieve the requirement.. I wanted to show the output in matrix form not the output given by sage usually.

print "the matrix is"; show(A)

is working in one cell but not in the other..

I was not able to identify the bug(or mistake) in the front end.

daviddgl gravatar imagedaviddgl ( 2016-11-30 04:42:54 +0100 )edit
1

answered 2016-11-30 05:25:41 +0100

If you're in a notebook cell, why use print? Just show("The matrix is", A).

edit flag offensive delete link more
0

answered 2016-11-30 05:16:06 +0100

slelievre gravatar image

Probably you have a comma instead of a semi-colon.

Try replacing print " The matrix is ", show(A) by print " The matrix is "; show(A)?

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-11-30 03:34:53 +0100

Seen: 908 times

Last updated: Nov 30 '16