0

I have a quadratic equation generating 2 solutions x1,x2 and each solution generates two new solutions again. This continues to infinity.

The way I tried to implement this is using a routine with vector r=[x1,x2] as an input parameter. And use the parallel for loop to compute the next 2.

Unfortunately the loop only continues to execute the first solution of r[].

The code looks as follows:

r=[1,2];
axyz=fileopen("myfile","w");
test(r)={
 parfor(i=1,2,r[i],c,
  if(c<>1,filewrite(axyz,c); 
   test([x1(c),x2(c)]))
 );
}

test(r);
The IF statement is to prevent saving a solution when it is unity. The file write is not necessary and can be replaced by a print.

Question: Is it possible to make sure that the second solution is also processed? So that the function expands into an infinite solution tree generating all solutions.

1 Answers1

0

Using parfor will not achieve what you want. I would suggest using a depth first search. Your code is incomplete (missing definitions for x1,x2), so the following is untested - intended to show changes necessary.

r=[1,2];
axyz=fileopen("myfile","w");
test(r,level)={
for(i=1,2,my(c=r[i]);
   if(c<>1,if(level==0, filewrite(axyz,c),
   test([x1(c),x2(c)], level-1))
 );
}
for(level=0, oo, test(r, level));
Andrew
  • 873
  • 4
  • 10
  • Thank you I am looking for a parallel solution. The level is a good idea but using it this way there are 2 for loops. – GerryMrt Jan 25 '19 at 21:13