mersenneforum.org  

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

Reply
 
Thread Tools
Old 2011-02-06, 01:43   #2146
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Code:
interCol(C) ={ IC=intersection(C[1],C[2]);for(i=1,#C,IC=intersection(IC,C[i]));IC;}
problem is intersection makes it pretty which then makes it useless here. I think I'll have to add a control to pretty it or not.

Last fiddled with by science_man_88 on 2011-02-06 at 01:46
science_man_88 is offline   Reply With Quote
Old 2011-02-06, 02:18   #2147
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I looked over the other thread and I realized it's simple:

Code:
union(A,B)=concat(A,B);vecsort(union,,8)
so simple I messed it up:

Code:
union(A,B)= U=concat(A,B);vecsort(U,,8)
Yes... though you should *only* use variables that have been declared with my() or local() -- and use my() unless you have a good reason to use local(). So
Code:
union(A,B)=my(U=concat(A,B));vecsort(U,,8)
Of course in this case you don't even need a variable at all -- can you see how to avoid it?
CRGreathouse is offline   Reply With Quote
Old 2011-02-06, 02:21   #2148
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
okay I added a new thing to that and on my end it works it's and if loop checking if #a and #b are = 0 but that shouldn't make a difference in that:

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)
)};
Still wrong.

The check that neither are empty is not only needless but actually harmful here. Also, there's no need to count the number of times you find disjoint pairs -- if you find any identical elements they're not disjoint, otherwise they are.
CRGreathouse is offline   Reply With Quote
Old 2011-02-06, 02:23   #2149
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Just to update I've changed one thing in Cp and fixed the output for CPC.

hopefully I can program more of this it may become very useful.
When you get all of them working maybe you can post them all in one swell foop.

Quote:
Originally Posted by science_man_88 View Post
Code:
interCol(C) ={ IC=intersection(C[1],C[2]);for(i=1,#C,IC=intersection(IC,C[i]));IC;}
problem is intersection makes it pretty which then makes it useless here. I think I'll have to add a control to pretty it or not.
I don't know what this is trying to do.
CRGreathouse is offline   Reply With Quote
Old 2011-02-06, 12:19   #2150
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... though you should *only* use variables that have been declared with my() or local() -- and use my() unless you have a good reason to use local(). So
Code:
union(A,B)=my(U=concat(A,B));vecsort(U,,8)
Of course in this case you don't even need a variable at all -- can you see how to avoid it?
Code:
vecsort(concat(A,B),,8)
science_man_88 is offline   Reply With Quote
Old 2011-02-06, 18:24   #2151
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Yep, you win.

As for the disjoint program: If you have a working intersection program you can just use that. If the sets are disjoint, what (by definition!) will their intersection be? That makes for a very short (albeit potentially slow) disjoint function.
CRGreathouse is offline   Reply With Quote
Old 2011-02-06, 22:05   #2152
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
Yep, you win.

As for the disjoint program: If you have a working intersection program you can just use that. If the sets are disjoint, what (by definition!) will their intersection be? That makes for a very short (albeit potentially slow) disjoint function.
yeah well I tried this:

Code:
(17:57)>aredisjoint(v,c)
%9 = 1
(17:57)>##
  ***   last result computed in 0 ms.
(17:57)>aredisjoint2(v,c)
%10 = 1
(17:57)>##
  ***   last result computed in 328 ms.
(17:57)>?aredisjoint2
aredisjoint2(A,B)=if(intersection(A,B)==[],return(1),return(0))
the aredisjoint is:

Code:
aredisjoint(A=[],B=[]) ={
a=0;
for(i=1,#A,
    for(j=1,#B,
	    if(A[i]==B[j],
		   return(0),
		      return(1)
			 )
		)
	)};
the reason the aredisjoint2 is slower is because my intersection script doesn't check if they have a intersection but goes through both sets creating a new vector for their intersection. my disjoint script aredisjoint only has to check the whole of both sets if no exception is found. never mind I'm an idiot.

Last fiddled with by science_man_88 on 2011-02-06 at 22:19
science_man_88 is offline   Reply With Quote
Old 2011-02-06, 22:37   #2153
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
the reason the aredisjoint2 is slower is because my intersection script doesn't check if they have a intersection but goes through both sets creating a new vector for their intersection. my disjoint script aredisjoint only has to check the whole of both sets if no exception is found. never mind I'm an idiot.
Certainly you can check whether two sets are disjoint faster than you can compute their intersection in most cases. But your current code is broken and I was suggesting a way to fix it.

The whole idea of breaking programs into different functions is that you can change the internal workings of one without affecting the others. Generally I recommend making a function that gives the correct answer, even if it runs very slowly, and using that to check if your newer, fast versions are correct.


Quote:
Originally Posted by science_man_88 View Post
Code:
aredisjoint(A=[],B=[]) ={
a=0;
for(i=1,#A,
    for(j=1,#B,
	    if(A[i]==B[j],
		   return(0),
		      return(1)
			 )
		)
	)};
This function is equivalent to
Code:
aredisjoint(A=[],B=[]) ={
  a=0;
  if(#A & #B, A[1] != B[1])
};
That is, the function does this:
1. It clobbers the global a variable. That is,
Code:
a=1;aredisjoint();print(a)
prints 0 instead of 1, a bad side-effect. (This is why you should always use my() or local().)
2. It checks if either vector is of length 0. If so, it does nothing -- internally it returns a value called gnil which is interpreted as 0 if needed but which is not displayed in gp.
3. Otherwise, it looks at A[1] and B[1]. If the values are the same it returns 0, otherwise it returns 1. It never looks at any other values.

Last fiddled with by CRGreathouse on 2011-02-06 at 22:37
CRGreathouse is offline   Reply With Quote
Old 2011-02-06, 23:39   #2154
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
Certainly you can check whether two sets are disjoint faster than you can compute their intersection in most cases. But your current code is broken and I was suggesting a way to fix it.

The whole idea of breaking programs into different functions is that you can change the internal workings of one without affecting the others. Generally I recommend making a function that gives the correct answer, even if it runs very slowly, and using that to check if your newer, fast versions are correct.




This function is equivalent to
Code:
aredisjoint(A=[],B=[]) ={
  a=0;
  if(#A & #B, A[1] != B[1])
};
That is, the function does this:
1. It clobbers the global a variable. That is,
Code:
a=1;aredisjoint();print(a)
prints 0 instead of 1, a bad side-effect. (This is why you should always use my() or local().)
2. It checks if either vector is of length 0. If so, it does nothing -- internally it returns a value called gnil which is interpreted as 0 if needed but which is not displayed in gp.
3. Otherwise, it looks at A[1] and B[1]. If the values are the same it returns 0, otherwise it returns 1. It never looks at any other values.
I suck at this:

Code:
(19:37)>aredisjoint([],[])
%1 = 0
(19:38)>aredisjoint([],[1,2,3])
%2 = 0
(19:38)>aredisjoint([4,5,6],[1,2,3])
%3 = 1
(19:38)>aredisjoint([3,5,6],[1,2,3])
%4 = 0
(19:38)>
Code:
aredisjoint(A=[],B=[]) ={
if(#A!=0 && #B!=0,
for(i=1,#A,
    for(j=1,#B,
	    if(A[i]==B[j],
		   return(0)
			 )
		)
	);return(1)
	,return(0))
	}
I think this could be changed to || because [] is a subset of every other set and hence no set can be disjoint from it so if it appears even once we know it's 0.

Last fiddled with by science_man_88 on 2011-02-06 at 23:44
science_man_88 is offline   Reply With Quote
Old 2011-02-07, 00:36   #2155
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

You should remove that entire if() test on the size of the vectors, it doesn't need to be there.
CRGreathouse is offline   Reply With Quote
Old 2011-02-07, 12:11   #2156
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
You should remove that entire if() test on the size of the vectors, it doesn't need to be there.
well it will return 1 for aredisjoint([],[]) it would also do that if only one is empty as we know the empty set is not disjoint from any set.
science_man_88 is offline   Reply With Quote
Reply

Thread Tools


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:42 UTC 2021 up 14 days, 10 hrs, 1 user, load averages: 2.41, 2.73, 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.