![]() |
|
|
#2146 |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Code:
interCol(C) ={ IC=intersection(C[1],C[2]);for(i=1,#C,IC=intersection(IC,C[i]));IC;}
Last fiddled with by science_man_88 on 2011-02-06 at 01:46 |
|
|
|
|
|
#2147 | |
|
Aug 2006
3·1,993 Posts |
Quote:
Code:
union(A,B)=my(U=concat(A,B));vecsort(U,,8) |
|
|
|
|
|
|
#2148 | |
|
Aug 2006
3×1,993 Posts |
Quote:
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. |
|
|
|
|
|
|
#2149 | |
|
Aug 2006
3·1,993 Posts |
Quote:
![]() I don't know what this is trying to do. |
|
|
|
|
|
|
#2150 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
Quote:
Code:
vecsort(concat(A,B),,8) |
|
|
|
|
|
|
#2151 |
|
Aug 2006
3·1,993 Posts |
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. |
|
|
|
|
|
#2152 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
Quote:
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)) Code:
aredisjoint(A=[],B=[]) ={
a=0;
for(i=1,#A,
for(j=1,#B,
if(A[i]==B[j],
return(0),
return(1)
)
)
)};
Last fiddled with by science_man_88 on 2011-02-06 at 22:19 |
|
|
|
|
|
|
#2153 | ||
|
Aug 2006
135338 Posts |
Quote:
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:
Code:
aredisjoint(A=[],B=[]) ={
a=0;
if(#A & #B, A[1] != B[1])
};
1. It clobbers the global a variable. That is, Code:
a=1;aredisjoint();print(a) 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 |
||
|
|
|
|
|
#2154 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
Quote:
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))
}
Last fiddled with by science_man_88 on 2011-02-06 at 23:44 |
|
|
|
|
|
|
#2155 |
|
Aug 2006
3×1,993 Posts |
You should remove that entire if() test on the size of the vectors, it doesn't need to be there.
|
|
|
|
|
|
#2156 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
|
|
|
|
![]() |
| 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 |