Ask Your Question
0

variables get deleted after calulation, overflow?

asked 2012-01-11 08:43:10 +0100

god.one gravatar image

updated 2012-01-11 09:26:55 +0100

DSM gravatar image

Hello and happy 1000 questions,

I tried to solve an equation and wanted to reinsert the solutions so I can check weather everything worked correctly. But after the calculations are finished, all variables and their values are gone. Here is the code

var('n,R,T')
var('test,test2,test3')
eq1 = ((n-1)^2/(n+1)^2)==(-1-2*R+R^2-T^2+sqrt(4*T^2+(1-2*R+R^2-T^2)^2))/(2*R-4)
sol=solve(eq1,n,solution_dict=true)
test = (eq1.substitute(n=sol[0][n]))
test2 = test*(test.lhs().denominator())*(test.rhs().denominator())
test3 = expand(test2).full_simplify()
show(test3)

Errorcode:

Traceback (click to the left of this block for traceback)
File "<stdin>", line 1, in <module>
File "_sage_input_3.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZXEx"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpDYArBS/___code___.py", line 2, in <module>
    exec compile(u'eq1
File "", line 1, in <module>   
NameError: name 'eq1' is not defined

I'm working with sage SageVersion 4.7.2 under win7 with Oracle VirtualBox as recommended. Thanks in advance.

edit retag flag offensive close merge delete

Comments

Rather than using ">", to get the formatting for code right it's easiest to select the pasted code and hit the button above the text box which reads "101 010". Alternatively you can indent by four spaces.

DSM gravatar imageDSM ( 2012-01-11 09:28:12 +0100 )edit

Thank you. And it's a lot easier than the way I did it.

god.one gravatar imagegod.one ( 2012-01-11 09:39:16 +0100 )edit
1

When I tried "show(test3)", I got a different error. The latex string for test3 is *very* long, long enough that a closing brace "}" is too far away from the corresponding starting one, so latex (or jsmath) fails. I'm not sure what you expect "show" to do with such long output, anyway. For what it's worth, "show(test2)" works fine for me.

John Palmieri gravatar imageJohn Palmieri ( 2012-01-11 11:04:16 +0100 )edit

Theoretically, I expect show(test3) to give me zero. But even when I just write test3 instead of show(test3) I get the same error.

god.one gravatar imagegod.one ( 2012-01-11 11:40:28 +0100 )edit

I have a question - how long does it take for you to run this? It's been running for QUITE a while for me now and I don't even have test3 done yet. Maybe you are running out of memory or something...

kcrisman gravatar imagekcrisman ( 2012-01-11 22:20:54 +0100 )edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2012-01-11 13:07:34 +0100

DSM gravatar image

updated 2012-01-11 13:09:29 +0100

I'm not sure what's causing the problem. eq1 shouldn't be blown away.

So since I can't help on that front, maybe we can approach it in another way. I think you're trying to check to see that the n that solve returns really is a solution, which is a good idea. solve has been known to return solutions which aren't really solutions in the past. [If I'm off-base, please disregard the following.]

I'm not sure I know what your code is doing, though. I mean, if test were something like

R 2/3 == R^2 4/9

i.e. some yet-unsimplified expression but one that's true, then your procedure would give a test3 of

18*R == 12*R^2

which isn't even a number, much less 0; it'll still be an expression. At best it might give 0 == 0. In your particular case, several options work for sanity checks:

bool(test)

which should return True,

(test.lhs()/test.rhs()).full_simplify()

which should give 1, and

(test.lhs()-test.rhs()).full_simplify()

which should give 0.

edit flag offensive delete link more

Comments

Ok, you are right. It has to be something like 0==0. I rewrote the program as you proposed (one minus two equals zero) but even this yields no solution for me.

god.one gravatar imagegod.one ( 2012-01-12 06:39:59 +0100 )edit

Could you post your code? Trying the above techniques on your "test" worked for me.

DSM gravatar imageDSM ( 2012-01-12 12:47:08 +0100 )edit
0

answered 2012-01-11 10:59:39 +0100

kcrisman gravatar image

A small point; you don't need to (shouldn't?) declare testx because you assign them to stuff when you first use them.

edit flag offensive delete link more

Comments

Thanks for the tip. I deleted the var declaration of testx but same error.

god.one gravatar imagegod.one ( 2012-01-11 11:47:15 +0100 )edit
0

answered 2012-01-13 05:07:49 +0100

god.one gravatar image

updated 2012-01-13 05:09:49 +0100

Hi, here is my code

%time
var('n,R,T')
eq1 = ((n-1)^2/(n+1)^2)==(-1-2*R+R^2-T^2+sqrt(4*T^2+(1-2*R+R^2-T^2)^2))/(2*R-4)
sol=solve(eq1,n,solution_dict=true)
test1 = (eq1.substitute(n=sol[0][n]))
test2 = test1.lhs()-test1.rhs()
test3 = expand(test2).full_simplify()
test3

and the glorious result

0
CPU time: 4575.08 s,  Wall time: 4601.30

I would like to thank DSM for pointing out that my previous attempt would not result into an integer and John Palmieri to extend the usable memory for the virtual machine.

Since this is a very special question and I got an answer now, should I delete it or which answer should get the answer check?

edit flag offensive delete link more

Comments

I still don't understand why you're expanding it, though. It'll take much less time if you don't, and it doesn't really work as an independent check.

DSM gravatar imageDSM ( 2012-01-13 12:36:55 +0100 )edit

I thought if I expand the whole thing it is easier for full_simplify to collect all terms and do the simplification. And I don't get what you mean with <it doesn't="" really="" work="" as="" an="" independent="" check="">. To what are you refering with 'it'.

god.one gravatar imagegod.one ( 2012-01-13 14:48:01 +0100 )edit

Never delete a question that isn't spam or a duplicate! You might be surprised who finds this interesting, and both issues are important.

kcrisman gravatar imagekcrisman ( 2012-01-13 15:08:38 +0100 )edit

Check whichever answer you like! It seems like John's is a little more important, given that you wouldn't have gotten an answer at all otherwise. Wow, over an hour...

kcrisman gravatar imagekcrisman ( 2012-01-13 15:09:45 +0100 )edit

What I meant by it doesn't really work as an independent check is that "expand(y).full_simplify()" doesn't give you any extra information over y.full_simplify() here (unless there's a bug in Sage). As for thinking that it might be easier, you might want to try full_simplify without expanding to check..

DSM gravatar imageDSM ( 2012-01-13 15:13:25 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-01-11 08:43:10 +0100

Seen: 705 times

Last updated: Jan 13 '12