mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   PARI/GP (https://www.mersenneforum.org/forumdisplay.php?f=155)
-   -   PARI's commands (https://www.mersenneforum.org/showthread.php?t=13636)

science_man_88 2011-02-04 15:06

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.

science_man_88 2011-02-04 16:19

[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]][/CODE]

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 2011-02-04 16:46

[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[/CODE]

not sure why the last result and I know I forgot the empty set.

science_man_88 2011-02-04 18:00

[QUOTE=science_man_88;251276][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[/CODE]

not sure why the last result and I know I forgot the empty set.[/QUOTE]

got it working:

[CODE]CPC(C)= Cp(C[1],C[2]);A=d;for(i=3,#C,Cp(A,C[i]);A=d);A;[/CODE] Now I'd still like it to be more proper output (using notation).

science_man_88 2011-02-04 18:35

[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][/CODE]

I think this is good.

CRGreathouse 2011-02-04 21:08

[QUOTE=science_man_88;251292]got it working[/QUOTE]

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=science_man_88;251292]Now I'd still like it to be more proper output (using notation).[/QUOTE]

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("}")
)
}[/code]

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]][/code]
and printVectorAsSet(Cp([10,20,30],[1,2])) writes
[code]{{10, 1}, {10, 2}, {20, 1}, {20, 2}, {30, 1}, {30, 2}}[/code]

[QUOTE=science_man_88;251298][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][/CODE]

I think this is good.[/QUOTE]

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.

science_man_88 2011-02-04 21:23

[QUOTE=CRGreathouse;251322]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("}")
)
}[/code]

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]][/code]
and printVectorAsSet(Cp([10,20,30],[1,2])) writes
[code]{{10, 1}, {10, 2}, {20, 1}, {20, 2}, {30, 1}, {30, 2}}[/code]



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.[/QUOTE]

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));[/CODE]

science_man_88 2011-02-04 21:55

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}}[/CODE]

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,[/QUOTE] 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.")[/CODE]

not sure why it's happened.

CRGreathouse 2011-02-04 23:47

Looks like you missed a parenthesis.

science_man_88 2011-02-04 23:50

[QUOTE=CRGreathouse;251346]Looks like you missed a parenthesis.[/QUOTE]

5 opening 5 closing for the for and if statements and I can't see anything else not closed.

science_man_88 2011-02-04 23:59

[QUOTE=science_man_88;251329]

[CODE]aredisjoint[COLOR="Lime"]([/COLOR]A=[],B=[][COLOR="lime"])[/COLOR] =
if[COLOR="Red"]([/COLOR]#A!=0 && #B!=0,
a=0;
for[COLOR="DarkOrange"]([/COLOR]i=1,#A,
for[COLOR="Purple"]([/COLOR]j=1,#B,
if[COLOR="Blue"]([/COLOR]A[i]==B[j],
return[COLOR="Silver"]([/COLOR]0[COLOR="Silver"])[/COLOR],
a=a+1;
if[COLOR="DarkRed"]([/COLOR]a==#A*#B,
return[COLOR="Gray"]([/COLOR]1[COLOR="Gray"])[/COLOR]
[COLOR="DarkRed"])[/COLOR]
[COLOR="blue"])[/COLOR]
[COLOR="Purple"])[/COLOR]
[COLOR="DarkOrange"])[/COLOR],
return[COLOR="Magenta"]([/COLOR]0[COLOR="Magenta"])[/COLOR]
[COLOR="red"])[/COLOR]
addhelp(aredisjoint,"a function to tell when 2 sets (vectors(which are actually sequences)) are considered disjoint.")[/CODE][/QUOTE]

color coded


All times are UTC. The time now is 23:03.

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