mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2011-02-04, 15:06   #2124
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

I was also thinking of making a Cartesian product making script but my main problem is I don't know how to allow for infinite arguments in the function. I guess I could make one that worked for 2 at a time, and puts the parentheses around it with a complete argument. what am i saying I can make it so the person has to give a collection of sets doh.

Last fiddled with by science_man_88 on 2011-02-04 at 15:11
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 16:19   #2125
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Code:
(12:17)>p=[1,2];c=[1,2];d=[];for(i=1,#p,for(j=1,#c,x=[p[i],c[j]];d=concat(d,[x])))
(12:17)>d
%201 = [[1, 1], [1, 2], [2, 1], [2, 2]]
(12:17)>p=[1,2];e=[];for(i=1,#p,for(j=1,#d,x=[d[j],p[i]];e=concat(e,[x])))
(12:17)>e
%202 = [[[1, 1], 1], [[1, 2], 1], [[2, 1], 1], [[2, 2], 1], [[1, 1], 2], [[1, 2], 2], [[2, 1], 2], [[2, 2], 2]]
found the basics but the problem I have is how to make this more like tuples. I think I have a way but it would take forever with the method I'm thinking of.
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 16:46   #2126
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Code:
(12:34)>Cp(p,c)= d=[];for(i=1,#p,for(j=1,#c,x=[p[i],c[j]];d=concat(d,[x])))
%205 = (p,c)->d=[];for(i=1,#p,for(j=1,#c,x=[p[i],c[j]];d=concat(d,[x])))
(12:39)>Cp([1,3],[1,2])
(12:39)>d
%206 = [[1, 1], [1, 2], [3, 1], [3, 2]]
(12:39)>CPC(C)= A=Cp(C[1],C[2]);B=[];for(i=3,#C,if(i%2==1,B=Cp(A,C[i]),A=Cp(B,C[i])));if(#C%2==1,return(B),return(A))
%207 = (C)->A=Cp(C[1],C[2]);B=[];for(i=3,#C,if(i%2==1,B=Cp(A,C[i]),A=Cp(B,C[i])));if(#C%2==1,return(B),return(A))
(12:43)>C=[v,c]
%208 = [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2]]
(12:44)>CPC(C)
%209 = 0
not sure why the last result and I know I forgot the empty set.
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 18:00   #2127
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
(12:34)>Cp(p,c)= d=[];for(i=1,#p,for(j=1,#c,x=[p[i],c[j]];d=concat(d,[x])))
%205 = (p,c)->d=[];for(i=1,#p,for(j=1,#c,x=[p[i],c[j]];d=concat(d,[x])))
(12:39)>Cp([1,3],[1,2])
(12:39)>d
%206 = [[1, 1], [1, 2], [3, 1], [3, 2]]
(12:39)>CPC(C)= A=Cp(C[1],C[2]);B=[];for(i=3,#C,if(i%2==1,B=Cp(A,C[i]),A=Cp(B,C[i])));if(#C%2==1,return(B),return(A))
%207 = (C)->A=Cp(C[1],C[2]);B=[];for(i=3,#C,if(i%2==1,B=Cp(A,C[i]),A=Cp(B,C[i])));if(#C%2==1,return(B),return(A))
(12:43)>C=[v,c]
%208 = [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2]]
(12:44)>CPC(C)
%209 = 0
not sure why the last result and I know I forgot the empty set.
got it working:

Code:
CPC(C)= Cp(C[1],C[2]);A=d;for(i=3,#C,Cp(A,C[i]);A=d);A;
Now I'd still like it to be more proper output (using notation).
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 18:35   #2128
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Code:
(14:31)>intersection(x,y) = C=[];for(i=1,#x,for(j=1,#y,if(x[i]==y[j],C=concat(C,x[i]))));C;
(14:33)>intersection([1,2,3,1],[2,3,1])
%259 = [1, 2, 3, 1]
I think this is good.
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 21:08   #2129
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

10111010110112 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
got it working
Yes. You should be returning the result,though, not storing it in a variable. What if you needed to call it twice, like

dostuff(Cp(v1, v2), Cp(v3, v4))

?

Quote:
Originally Posted by science_man_88 View Post
Now I'd still like it to be more proper output (using notation).
Once you have a working function (as you do!), the right way -- what you seem to be doing -- is to have a second function that formats it "nicely". (An alternate, also good, method is to have an argument that, if set to a particular value, "nicens" the result -- though in this case that wouldn't be as good, I think.)

Maybe this:
Code:
printVectorAsSet(v)={
  if(type(v) != "t_VEC",
    print1(v)
  ,
    print1("{");
    if (#v, printVectorAsSet(v[1]));
    for(i=2,#v,
      print1(", ");
      printVectorAsSet(v[i])
    );
    print1("}")
  )
}
So after fixing your function Cp to return the value, Cp([10,20,30],[1,2]) returns
Code:
[[10, 1], [10, 2], [20, 1], [20, 2], [30, 1], [30, 2]]
and printVectorAsSet(Cp([10,20,30],[1,2])) writes
Code:
{{10, 1}, {10, 2}, {20, 1}, {20, 2}, {30, 1}, {30, 2}}
Quote:
Originally Posted by science_man_88 View Post
Code:
(14:31)>intersection(x,y) = C=[];for(i=1,#x,for(j=1,#y,if(x[i]==y[j],C=concat(C,x[i]))));C;
(14:33)>intersection([1,2,3,1],[2,3,1])
%259 = [1, 2, 3, 1]
I think this is good.
Yep. It could be made faster, but that works fine.

I'll admit, I don't like the repetition. I suppose for both functions you could add vecsort(__,,8) in the appropriate places to fix that.
CRGreathouse is offline   Reply With Quote
Old 2011-02-04, 21:23   #2130
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Yes. You should be returning the result,though, not storing it in a variable. What if you needed to call it twice, like

dostuff(Cp(v1, v2), Cp(v3, v4))

?



Once you have a working function (as you do!), the right way -- what you seem to be doing -- is to have a second function that formats it "nicely". (An alternate, also good, method is to have an argument that, if set to a particular value, "nicens" the result -- though in this case that wouldn't be as good, I think.)

Maybe this:
Code:
printVectorAsSet(v)={
  if(type(v) != "t_VEC",
    print1(v)
  ,
    print1("{");
    if (#v, printVectorAsSet(v[1]));
    for(i=2,#v,
      print1(", ");
      printVectorAsSet(v[i])
    );
    print1("}")
  )
}
So after fixing your function Cp to return the value, Cp([10,20,30],[1,2]) returns
Code:
[[10, 1], [10, 2], [20, 1], [20, 2], [30, 1], [30, 2]]
and printVectorAsSet(Cp([10,20,30],[1,2])) writes
Code:
{{10, 1}, {10, 2}, {20, 1}, {20, 2}, {30, 1}, {30, 2}}


Yep. It could be made faster, but that works fine.

I'll admit, I don't like the repetition. I suppose for both functions you could add vecsort(__,,8) in the appropriate places to fix that.
I couldn't remember if I knew that or not lol.
Code:
intersection(x,y) = C=[];for(i=1,#x,for(j=1,#y,if(x[i]==y[j],C=concat(C,x[i]))));printVectorAsSet(vecsort(C,,8));

Last fiddled with by science_man_88 on 2011-02-04 at 21:44
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 21:55   #2131
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

applying your make to a set function to the CPC gives back:

Code:
{{{1, 1}, 1}, {{1, 1}, 2}, {{1, 2}, 1}, {{1, 2}, 2}, {{2, 1}, 1}, {{2, 1}, 2}, {{2, 2}, 1}, {{2, 2}, 2}, {{3, 1}, 1}, {{3, 1}, 2}, {{3, 2}, 1}, {{3, 2}, 2}, {{4, 1}, 1}, {{4, 1}, 2}, {{4, 2}, 1}, {{4, 2}, 2}, {{5, 1}, 1}, {{5, 1}, 2}, {{5, 2}, 1}, {{5, 2}, 2}, {{6, 1}, 1}, {{6, 1}, 2}, {{6, 2}, 1}, {{6, 2}, 2}, {{7, 1}, 1}, {{7, 1}, 2}, {{7, 2}, 1}, {{7, 2}, 2}, {{8, 1}, 1}, {{8, 1}, 2}, {{8, 2}, 1}, {{8, 2}, 2}}
but this comes from the fact that I never got each index to be one vector. on restart of PARI I got:

Quote:
*** syntax error, unexpected $end, expecting KPARROW or ',' or ')': ...=[],B=[])=if(#A!=0&&#B!=0,
I don't understand why this is in a function called aredisjoint .

Code:
aredisjoint(A=[],B=[]) = 
if(#A!=0 && #B!=0,
   a=0;
   for(i=1,#A,
       for(j=1,#B,
	       if(A[i]==B[j],
		      return(0),
			  a=a+1;
			  if(a==#A*#B,
			     return(1)
				)
			)
		)
	),
return(0)
)
addhelp(aredisjoint,"a function to tell when 2 sets (vectors(which are actually sequences)) are considered disjoint.")
not sure why it's happened.

Last fiddled with by science_man_88 on 2011-02-04 at 22:04
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 23:47   #2132
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Looks like you missed a parenthesis.
CRGreathouse is offline   Reply With Quote
Old 2011-02-04, 23:50   #2133
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

20C016 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Looks like you missed a parenthesis.
5 opening 5 closing for the for and if statements and I can't see anything else not closed.
science_man_88 is offline   Reply With Quote
Old 2011-02-04, 23:59   #2134
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Quote:
Originally Posted by science_man_88 View Post

Code:
aredisjoint(A=[],B=[]) = 
if(#A!=0 && #B!=0,
   a=0;
   for(i=1,#A,
       for(j=1,#B,
	       if(A[i]==B[j],
		      return(0),
			  a=a+1;
			  if(a==#A*#B,
			     return(1)
				)
			)
		)
	),
return(0)
)
addhelp(aredisjoint,"a function to tell when 2 sets (vectors(which are actually sequences)) are considered disjoint.")
color coded

Last fiddled with by science_man_88 on 2011-02-05 at 00:00
science_man_88 is offline   Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do I sometimes see all the <> formatting commands when I quote or edit? cheesehead Forum Feedback 3 2013-05-25 12:56
Passing commands to PARI on Windows James Heinrich Software 2 2012-05-13 19:19
Ubiquity commands Mini-Geek Aliquot Sequences 1 2009-09-22 19:33
64-bit Pari? CRGreathouse Software 2 2009-03-13 04:22
Are these commands correct? jasong Linux 2 2007-10-18 23:40

All times are UTC. The time now is 15:31.


Fri Aug 6 15:31:38 UTC 2021 up 14 days, 10 hrs, 1 user, load averages: 2.36, 2.72, 2.83

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.