Singular https://www.singular.uni-kl.de/forum/ |
|
read() https://www.singular.uni-kl.de/forum/viewtopic.php?f=10&t=1810 |
Page 1 of 1 |
Author: | Singuser [ Fri Mar 19, 2010 2:21 pm ] |
Post subject: | read() |
If I use the command read() with no file specified, then the standard input from the command line is read. When I do this it seems I can only enter 80 characters max at the command line. Could someone tell me how I could enter unlimited or just a lot more that 80 characters at the command line? Thank you kindly |
Author: | gorzel [ Sat Mar 20, 2010 8:54 pm ] |
Post subject: | Re: read() |
read(""); waits for input from stdin and returns a string. Indeed it accepts only the first 80 characters and ends with a control key ^P, which becomes visible, when you write the string to stdout. Code: > string s = read(""); > ? // put your very long string very > size(s); 81 > write("outfile",s); (Consider outfile in a editor, or just call less outfile on a Unix-system). ----------------------------------------------------------------------------------------------- But, why do you need such a long input? read(""); is to be used for instance when you want to delay output on screen until the user hits the return key, or if you want to build a menue where the user has to answer with y/n. If you just want to put a long string into Singular, then just put it into " " and assign it to a string variable. This even works using the mouse (copy-and-paste) at least on Unix-machines. Try: Code: > string oneword = "hello"; oneword; hello > bigint(2)^400; 2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493376 > string S; > S=" On a terminal with 80 characters, this big number spreads over twolines, Now copy with the mouse the two lines, place them immediately after the " (on a Unix-sytem you just have to mark and release it) and then close by "; Then S should contain the complete string. Check, whether nothing is lost by Code: > size(s);
121 > S==string(bigint(2)^400); 1 |
Author: | Singuser [ Mon Mar 22, 2010 1:03 pm ] |
Post subject: | Re: read() |
Thank you for your reply gorzel. I am trying run the following interactive script program which prompts the user to enter info such the number of variables in the ring and the generators of an ideal. The problem with character limits comes up the one of the polynomial generators is big ;ie, over 80 characters. Code: option(none);
LIB "primdec.lib"; ring r; string s; int n; int g; int k; list L; link l=":r"; printf("How many variables does the ring have?"); s="n="+read(l)+";"; execute(s); ring r=0,a(1..n),dp; printf("How many generators does the ideal have?"); s="g="+read(l)+";"; execute(s); printf("WARNING: The variables used must be x(1)..x(n)!!!"); for (k=1;k<=g;k=k+1) { printf("Enter generator number %s: (NO MORE THAN 80 CHARS/LINE)",k); s="poly f(k)="+read(l)+read(l)+";"; execute(s); } ideal i; for (k=1;k<=g;k=k+1) { i=i+f(k); } printf("The ideal is:"); i; printf("Its Prime Decomposition is:"); primdecGTZ(i); quit; |
Author: | Singuser [ Mon Mar 22, 2010 1:11 pm ] |
Post subject: | Re: read() |
Sorry, if you try to run that code as written the variables used should be called a(1),..,a(n) rather that x(1),..,x(n). The line Code: s="poly f(k)="+read(l)+read(l)+";"; is where the problem arises. The reason that I wrote read(l) twice was my attempt to fix the character limit problem. Is there a better way to accomplish what I am trying to do here? I really would like an interactive script. |
Author: | gorzel [ Mon Mar 22, 2010 4:26 pm ] |
Post subject: | Re: read() |
I reflected a moment about your problem. How do you want to enter the polynomials ? Copied from somewhere with the mouse, or entered by hand on keyboard? Let us assume, the user takes this ordeal to type the polys with the indexed variables a(i). (Note, that she or he has then also to enter * and ^ for multplication and expontiation.) //------------------ So what you could do is the following: Build an addditiional loop where the input is read line by line. The user will have to mark explicitly when the input of a polynomial is finished. There is one limitation: Within at most 80 characters per line, no coefficient has to be broken. A succesive input of three polynomial in variables x(1..4) should work. Code: 12121212*x(1)^2+3214843131/54641231*x(2)^3*x(4) + 2354654*x(2)^5 - 2354343*x(3) + 123*x(4) # 897*x(2) -x(3) +561/5646*x(1)^5 + x(3) # x(1) + x(2) + 123* x(1)*x(2)^3 # But it will not work if the coefficient is longer than 80 characters so that the user has to break it within the coefficient. This would result in blanks and newlines that had to be eliminated first. (May be it is a liitle less restictive: Numerators and denominators are not allowed to break, but you can break at the / ). An example which will not work. Code: 12317631817312988888888782649869168888888888888882983712379 213823012380183103810*x(1) # Here is some code. (Not tested, since at the moment I am reading the forum only on the web without access to a running Singular.) Code: for (k=1;k<=g;k++) { s="poly f(k) ="; "Enter polynomial", k; "Do not break the coefficients and "; "enter a single # when this poly is complete"; s = read(l); while(answ !="#") { s = s + answ; s = read(l); } s = s +";"; execute(s); I[k] = f(k); } Note that I wrote i[k] = f(k); instead of i=i+f(k);. The difference is that + makes automatically some simplifications: e.g. multiple and zero generators will be removed. This does not change the result of primdecGTZ, but when you ouptut Code: printf("The ideal is:"); i; then the user may see something different than the original input. |
Author: | gorzel [ Mon Mar 22, 2010 4:36 pm ] |
Post subject: | Re: read() |
As i see I have to correct my code a little bit: string answ; was neither declared nor properly used. Code: string answ;
for (k=1;k<=g;k++) { s="poly f(k) ="; "Enter polynomial", k; "Do not break the coefficients and "; "enter a single # when this poly is complete"; answ = read(l); while(answ !="#") { s = s + answ; answ = read(l); } s = s +";"; execute(s); I[k] = f(k); } |
Author: | gorzel [ Tue Mar 23, 2010 2:29 pm ] |
Post subject: | Re: read() |
Tested and improved code Note 1: answ!="#" was not correct, there is trailing newline. not(find(ans,"#")) is more relaxed. Note 2: Breaking of rational numbers at / is not allowed. Code: //option(none); LIB "primdec.lib"; string s,answ; int g,k,n; //list L; link l=":r"; printf("How many variables does the ring have?"); s="n="+read(l)+";"; execute(s); ring r=0,x(1..n),dp; ideal i; printf("How many generators does the ideal have?"); s="g="+read(l)+";"; execute(s); printf("WARNING: The variables used must be x(1)..x(n)!!!"); printf("Do not break the coefficients and enter a # on a new line"); printf("when the input of the generator is complete."+newline); for (k=1;k<=g;k=k+1) { s="poly f(k)="; printf("Enter generator number %s: (NO MORE THAN 80 CHARS/LINE)",k); answ = read(l); while(!find(answ,"#")) { s = s + answ; answ = read(l); } s = s +";"; execute(s); i[k] = f(k); } printf("The ideal is:"); i; printf("Its Prime Decomposition is:"); primdecGTZ(i); quit; Store it in a file say pdc-job, then call Singular -q pdc-job Example: Code: How many variables does the ring have?
2 How many generators does the ideal have? 3 WARNING: The variables used must be x(1)..x(n)!!! Do not break the coefficients and enter a # on a new line when the input of the generator is complete. Enter generator number 1: (NO MORE THAN 80 CHARS/LINE) x(1)* x(2)^2 # Enter generator number 2: (NO MORE THAN 80 CHARS/LINE) 3*x(1)^2-x(1)*x(2) # Enter generator number 3: (NO MORE THAN 80 CHARS/LINE) x(1)^2 - x(2)^2 # The ideal is: i[1]=x(1)*x(2)^2 i[2]=3*x(1)^2-x(1)*x(2) i[3]=x(1)^2-x(2)^2 Its Prime Decomposition is: [1]: [1]: _[1]=x(1)*x(2)-3*x(2)^2 _[2]=x(1)^2-x(2)^2 _[3]=x(2)^3 [2]: _[1]=x(1) _[2]=x(2) |
Author: | Singuser [ Sat Mar 27, 2010 8:09 am ] |
Post subject: | Re: read() |
This is excellent. Thanks a lot gorzel |
Page 1 of 1 | All times are UTC + 1 hour [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |