Ask Your Question
0

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

asked 8 years ago

daviddgl gravatar image

updated 7 years ago

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)
Preview: (hide)

3 Answers

Sort by » oldest newest most voted
1

answered 8 years ago

kcrisman gravatar image

updated 8 years ago

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.

Preview: (hide)
link

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 ( 8 years ago )
1

answered 8 years ago

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

Preview: (hide)
link
0

answered 8 years ago

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)?

Preview: (hide)
link

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: 8 years ago

Seen: 1,044 times

Last updated: Nov 30 '16