%mathpiper,def="ChineseRemainderPoly"

ChineseRemainderPoly(mlist_IsList,vlist_IsList) <--
[
  Local(m,i,nr,result,msub,euclid,clist);
  clist:={};
  m:=Product(mlist);
  result:=0;

/* Echo({mlist,m}); */


  nr:=Length(mlist);
  For(i:=1,i<=nr,i++)
  [
    msub:=Quotient(m,mlist[i]);

/* Echo({Factor(msub)}); */

    euclid := ExtendedEuclideanMonic(msub,mlist[i]);
    Local(c);

    c:=vlist[i] * euclid[2];

    c:=Modulo(c, mlist[i]);

    DestructiveAppend(clist,c);
    result:=result + msub * c;
  ];
  {Expand(result),clist};
];

%/mathpiper