isl_polynomial.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228
  1. /*
  2. * Copyright 2010 INRIA Saclay
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  7. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  8. * 91893 Orsay, France
  9. */
  10. #include <stdlib.h>
  11. #include <isl_ctx_private.h>
  12. #include <isl_map_private.h>
  13. #include <isl_factorization.h>
  14. #include <isl_lp_private.h>
  15. #include <isl_seq.h>
  16. #include <isl_union_map_private.h>
  17. #include <isl_constraint_private.h>
  18. #include <isl_polynomial_private.h>
  19. #include <isl_point_private.h>
  20. #include <isl_space_private.h>
  21. #include <isl_mat_private.h>
  22. #include <isl_vec_private.h>
  23. #include <isl_range.h>
  24. #include <isl_local.h>
  25. #include <isl_local_space_private.h>
  26. #include <isl_aff_private.h>
  27. #include <isl_val_private.h>
  28. #include <isl_config.h>
  29. #undef EL_BASE
  30. #define EL_BASE qpolynomial
  31. #include <isl_list_templ.c>
  32. #undef EL_BASE
  33. #define EL_BASE pw_qpolynomial
  34. #include <isl_list_templ.c>
  35. static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
  36. {
  37. switch (type) {
  38. case isl_dim_param: return 0;
  39. case isl_dim_in: return space->nparam;
  40. case isl_dim_out: return space->nparam + space->n_in;
  41. default: return 0;
  42. }
  43. }
  44. isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
  45. {
  46. if (!poly)
  47. return isl_bool_error;
  48. return isl_bool_ok(poly->var < 0);
  49. }
  50. __isl_keep isl_poly_cst *isl_poly_as_cst(__isl_keep isl_poly *poly)
  51. {
  52. if (!poly)
  53. return NULL;
  54. isl_assert(poly->ctx, poly->var < 0, return NULL);
  55. return (isl_poly_cst *) poly;
  56. }
  57. __isl_keep isl_poly_rec *isl_poly_as_rec(__isl_keep isl_poly *poly)
  58. {
  59. if (!poly)
  60. return NULL;
  61. isl_assert(poly->ctx, poly->var >= 0, return NULL);
  62. return (isl_poly_rec *) poly;
  63. }
  64. /* Compare two polynomials.
  65. *
  66. * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
  67. * than "poly2" and 0 if they are equal.
  68. */
  69. static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1,
  70. __isl_keep isl_poly *poly2)
  71. {
  72. int i;
  73. isl_bool is_cst1;
  74. isl_poly_rec *rec1, *rec2;
  75. if (poly1 == poly2)
  76. return 0;
  77. is_cst1 = isl_poly_is_cst(poly1);
  78. if (is_cst1 < 0)
  79. return -1;
  80. if (!poly2)
  81. return 1;
  82. if (poly1->var != poly2->var)
  83. return poly1->var - poly2->var;
  84. if (is_cst1) {
  85. isl_poly_cst *cst1, *cst2;
  86. int cmp;
  87. cst1 = isl_poly_as_cst(poly1);
  88. cst2 = isl_poly_as_cst(poly2);
  89. if (!cst1 || !cst2)
  90. return 0;
  91. cmp = isl_int_cmp(cst1->n, cst2->n);
  92. if (cmp != 0)
  93. return cmp;
  94. return isl_int_cmp(cst1->d, cst2->d);
  95. }
  96. rec1 = isl_poly_as_rec(poly1);
  97. rec2 = isl_poly_as_rec(poly2);
  98. if (!rec1 || !rec2)
  99. return 0;
  100. if (rec1->n != rec2->n)
  101. return rec1->n - rec2->n;
  102. for (i = 0; i < rec1->n; ++i) {
  103. int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
  104. if (cmp != 0)
  105. return cmp;
  106. }
  107. return 0;
  108. }
  109. isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1,
  110. __isl_keep isl_poly *poly2)
  111. {
  112. int i;
  113. isl_bool is_cst1;
  114. isl_poly_rec *rec1, *rec2;
  115. is_cst1 = isl_poly_is_cst(poly1);
  116. if (is_cst1 < 0 || !poly2)
  117. return isl_bool_error;
  118. if (poly1 == poly2)
  119. return isl_bool_true;
  120. if (poly1->var != poly2->var)
  121. return isl_bool_false;
  122. if (is_cst1) {
  123. isl_poly_cst *cst1, *cst2;
  124. int r;
  125. cst1 = isl_poly_as_cst(poly1);
  126. cst2 = isl_poly_as_cst(poly2);
  127. if (!cst1 || !cst2)
  128. return isl_bool_error;
  129. r = isl_int_eq(cst1->n, cst2->n) &&
  130. isl_int_eq(cst1->d, cst2->d);
  131. return isl_bool_ok(r);
  132. }
  133. rec1 = isl_poly_as_rec(poly1);
  134. rec2 = isl_poly_as_rec(poly2);
  135. if (!rec1 || !rec2)
  136. return isl_bool_error;
  137. if (rec1->n != rec2->n)
  138. return isl_bool_false;
  139. for (i = 0; i < rec1->n; ++i) {
  140. isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
  141. if (eq < 0 || !eq)
  142. return eq;
  143. }
  144. return isl_bool_true;
  145. }
  146. isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
  147. {
  148. isl_bool is_cst;
  149. isl_poly_cst *cst;
  150. is_cst = isl_poly_is_cst(poly);
  151. if (is_cst < 0 || !is_cst)
  152. return is_cst;
  153. cst = isl_poly_as_cst(poly);
  154. if (!cst)
  155. return isl_bool_error;
  156. return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d));
  157. }
  158. int isl_poly_sgn(__isl_keep isl_poly *poly)
  159. {
  160. isl_bool is_cst;
  161. isl_poly_cst *cst;
  162. is_cst = isl_poly_is_cst(poly);
  163. if (is_cst < 0 || !is_cst)
  164. return 0;
  165. cst = isl_poly_as_cst(poly);
  166. if (!cst)
  167. return 0;
  168. return isl_int_sgn(cst->n);
  169. }
  170. isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
  171. {
  172. isl_bool is_cst;
  173. isl_poly_cst *cst;
  174. is_cst = isl_poly_is_cst(poly);
  175. if (is_cst < 0 || !is_cst)
  176. return is_cst;
  177. cst = isl_poly_as_cst(poly);
  178. if (!cst)
  179. return isl_bool_error;
  180. return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d));
  181. }
  182. isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
  183. {
  184. isl_bool is_cst;
  185. isl_poly_cst *cst;
  186. is_cst = isl_poly_is_cst(poly);
  187. if (is_cst < 0 || !is_cst)
  188. return is_cst;
  189. cst = isl_poly_as_cst(poly);
  190. if (!cst)
  191. return isl_bool_error;
  192. return isl_bool_ok(isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d));
  193. }
  194. isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
  195. {
  196. isl_bool is_cst;
  197. isl_poly_cst *cst;
  198. is_cst = isl_poly_is_cst(poly);
  199. if (is_cst < 0 || !is_cst)
  200. return is_cst;
  201. cst = isl_poly_as_cst(poly);
  202. if (!cst)
  203. return isl_bool_error;
  204. return isl_bool_ok(isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d));
  205. }
  206. isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
  207. {
  208. isl_bool is_cst;
  209. isl_poly_cst *cst;
  210. int r;
  211. is_cst = isl_poly_is_cst(poly);
  212. if (is_cst < 0 || !is_cst)
  213. return is_cst;
  214. cst = isl_poly_as_cst(poly);
  215. if (!cst)
  216. return isl_bool_error;
  217. r = isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
  218. return isl_bool_ok(r);
  219. }
  220. isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
  221. {
  222. isl_bool is_cst;
  223. isl_poly_cst *cst;
  224. is_cst = isl_poly_is_cst(poly);
  225. if (is_cst < 0 || !is_cst)
  226. return is_cst;
  227. cst = isl_poly_as_cst(poly);
  228. if (!cst)
  229. return isl_bool_error;
  230. return isl_bool_ok(isl_int_is_negone(cst->n) && isl_int_is_one(cst->d));
  231. }
  232. __isl_give isl_poly_cst *isl_poly_cst_alloc(isl_ctx *ctx)
  233. {
  234. isl_poly_cst *cst;
  235. cst = isl_alloc_type(ctx, struct isl_poly_cst);
  236. if (!cst)
  237. return NULL;
  238. cst->poly.ref = 1;
  239. cst->poly.ctx = ctx;
  240. isl_ctx_ref(ctx);
  241. cst->poly.var = -1;
  242. isl_int_init(cst->n);
  243. isl_int_init(cst->d);
  244. return cst;
  245. }
  246. __isl_give isl_poly *isl_poly_zero(isl_ctx *ctx)
  247. {
  248. isl_poly_cst *cst;
  249. cst = isl_poly_cst_alloc(ctx);
  250. if (!cst)
  251. return NULL;
  252. isl_int_set_si(cst->n, 0);
  253. isl_int_set_si(cst->d, 1);
  254. return &cst->poly;
  255. }
  256. __isl_give isl_poly *isl_poly_one(isl_ctx *ctx)
  257. {
  258. isl_poly_cst *cst;
  259. cst = isl_poly_cst_alloc(ctx);
  260. if (!cst)
  261. return NULL;
  262. isl_int_set_si(cst->n, 1);
  263. isl_int_set_si(cst->d, 1);
  264. return &cst->poly;
  265. }
  266. __isl_give isl_poly *isl_poly_infty(isl_ctx *ctx)
  267. {
  268. isl_poly_cst *cst;
  269. cst = isl_poly_cst_alloc(ctx);
  270. if (!cst)
  271. return NULL;
  272. isl_int_set_si(cst->n, 1);
  273. isl_int_set_si(cst->d, 0);
  274. return &cst->poly;
  275. }
  276. __isl_give isl_poly *isl_poly_neginfty(isl_ctx *ctx)
  277. {
  278. isl_poly_cst *cst;
  279. cst = isl_poly_cst_alloc(ctx);
  280. if (!cst)
  281. return NULL;
  282. isl_int_set_si(cst->n, -1);
  283. isl_int_set_si(cst->d, 0);
  284. return &cst->poly;
  285. }
  286. __isl_give isl_poly *isl_poly_nan(isl_ctx *ctx)
  287. {
  288. isl_poly_cst *cst;
  289. cst = isl_poly_cst_alloc(ctx);
  290. if (!cst)
  291. return NULL;
  292. isl_int_set_si(cst->n, 0);
  293. isl_int_set_si(cst->d, 0);
  294. return &cst->poly;
  295. }
  296. __isl_give isl_poly *isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
  297. {
  298. isl_poly_cst *cst;
  299. cst = isl_poly_cst_alloc(ctx);
  300. if (!cst)
  301. return NULL;
  302. isl_int_set(cst->n, n);
  303. isl_int_set(cst->d, d);
  304. return &cst->poly;
  305. }
  306. __isl_give isl_poly_rec *isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
  307. {
  308. isl_poly_rec *rec;
  309. isl_assert(ctx, var >= 0, return NULL);
  310. isl_assert(ctx, size >= 0, return NULL);
  311. rec = isl_calloc(ctx, struct isl_poly_rec,
  312. sizeof(struct isl_poly_rec) +
  313. size * sizeof(struct isl_poly *));
  314. if (!rec)
  315. return NULL;
  316. rec->poly.ref = 1;
  317. rec->poly.ctx = ctx;
  318. isl_ctx_ref(ctx);
  319. rec->poly.var = var;
  320. rec->n = 0;
  321. rec->size = size;
  322. return rec;
  323. }
  324. __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
  325. __isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
  326. {
  327. qp = isl_qpolynomial_cow(qp);
  328. if (!qp || !space)
  329. goto error;
  330. isl_space_free(qp->dim);
  331. qp->dim = space;
  332. return qp;
  333. error:
  334. isl_qpolynomial_free(qp);
  335. isl_space_free(space);
  336. return NULL;
  337. }
  338. /* Reset the space of "qp". This function is called from isl_pw_templ.c
  339. * and doesn't know if the space of an element object is represented
  340. * directly or through its domain. It therefore passes along both.
  341. */
  342. __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
  343. __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
  344. __isl_take isl_space *domain)
  345. {
  346. isl_space_free(space);
  347. return isl_qpolynomial_reset_domain_space(qp, domain);
  348. }
  349. isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
  350. {
  351. return qp ? qp->dim->ctx : NULL;
  352. }
  353. /* Return the domain space of "qp".
  354. */
  355. static __isl_keep isl_space *isl_qpolynomial_peek_domain_space(
  356. __isl_keep isl_qpolynomial *qp)
  357. {
  358. return qp ? qp->dim : NULL;
  359. }
  360. /* Return a copy of the domain space of "qp".
  361. */
  362. __isl_give isl_space *isl_qpolynomial_get_domain_space(
  363. __isl_keep isl_qpolynomial *qp)
  364. {
  365. return isl_space_copy(isl_qpolynomial_peek_domain_space(qp));
  366. }
  367. #undef TYPE
  368. #define TYPE isl_qpolynomial
  369. #undef PEEK_SPACE
  370. #define PEEK_SPACE peek_domain_space
  371. static
  372. #include "isl_type_has_equal_space_bin_templ.c"
  373. static
  374. #include "isl_type_check_equal_space_templ.c"
  375. #undef PEEK_SPACE
  376. /* Return a copy of the local space on which "qp" is defined.
  377. */
  378. static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
  379. __isl_keep isl_qpolynomial *qp)
  380. {
  381. isl_space *space;
  382. if (!qp)
  383. return NULL;
  384. space = isl_qpolynomial_get_domain_space(qp);
  385. return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
  386. }
  387. __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
  388. {
  389. isl_space *space;
  390. if (!qp)
  391. return NULL;
  392. space = isl_space_copy(qp->dim);
  393. space = isl_space_from_domain(space);
  394. space = isl_space_add_dims(space, isl_dim_out, 1);
  395. return space;
  396. }
  397. /* Return the number of variables of the given type in the domain of "qp".
  398. */
  399. isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
  400. enum isl_dim_type type)
  401. {
  402. isl_space *space;
  403. isl_size dim;
  404. space = isl_qpolynomial_peek_domain_space(qp);
  405. if (!space)
  406. return isl_size_error;
  407. if (type == isl_dim_div)
  408. return qp->div->n_row;
  409. dim = isl_space_dim(space, type);
  410. if (dim < 0)
  411. return isl_size_error;
  412. if (type == isl_dim_all) {
  413. isl_size n_div;
  414. n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
  415. if (n_div < 0)
  416. return isl_size_error;
  417. dim += n_div;
  418. }
  419. return dim;
  420. }
  421. /* Given the type of a dimension of an isl_qpolynomial,
  422. * return the type of the corresponding dimension in its domain.
  423. * This function is only called for "type" equal to isl_dim_in or
  424. * isl_dim_param.
  425. */
  426. static enum isl_dim_type domain_type(enum isl_dim_type type)
  427. {
  428. return type == isl_dim_in ? isl_dim_set : type;
  429. }
  430. /* Externally, an isl_qpolynomial has a map space, but internally, the
  431. * ls field corresponds to the domain of that space.
  432. */
  433. isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
  434. enum isl_dim_type type)
  435. {
  436. if (!qp)
  437. return isl_size_error;
  438. if (type == isl_dim_out)
  439. return 1;
  440. type = domain_type(type);
  441. return isl_qpolynomial_domain_dim(qp, type);
  442. }
  443. /* Return the offset of the first variable of type "type" within
  444. * the variables of the domain of "qp".
  445. */
  446. static isl_size isl_qpolynomial_domain_var_offset(
  447. __isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
  448. {
  449. isl_space *space;
  450. space = isl_qpolynomial_peek_domain_space(qp);
  451. if (!space)
  452. return isl_size_error;
  453. switch (type) {
  454. case isl_dim_param:
  455. case isl_dim_set: return isl_space_offset(space, type);
  456. case isl_dim_div: return isl_space_dim(space, isl_dim_all);
  457. case isl_dim_cst:
  458. default:
  459. isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
  460. "invalid dimension type", return isl_size_error);
  461. }
  462. }
  463. /* Return the offset of the first coefficient of type "type" in
  464. * the domain of "qp".
  465. */
  466. unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
  467. enum isl_dim_type type)
  468. {
  469. switch (type) {
  470. case isl_dim_cst:
  471. return 0;
  472. case isl_dim_param:
  473. case isl_dim_set:
  474. case isl_dim_div:
  475. return 1 + isl_qpolynomial_domain_var_offset(qp, type);
  476. default:
  477. return 0;
  478. }
  479. }
  480. isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
  481. {
  482. return qp ? isl_poly_is_zero(qp->poly) : isl_bool_error;
  483. }
  484. isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
  485. {
  486. return qp ? isl_poly_is_one(qp->poly) : isl_bool_error;
  487. }
  488. isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
  489. {
  490. return qp ? isl_poly_is_nan(qp->poly) : isl_bool_error;
  491. }
  492. isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
  493. {
  494. return qp ? isl_poly_is_infty(qp->poly) : isl_bool_error;
  495. }
  496. isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
  497. {
  498. return qp ? isl_poly_is_neginfty(qp->poly) : isl_bool_error;
  499. }
  500. int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
  501. {
  502. return qp ? isl_poly_sgn(qp->poly) : 0;
  503. }
  504. static void poly_free_cst(__isl_take isl_poly_cst *cst)
  505. {
  506. isl_int_clear(cst->n);
  507. isl_int_clear(cst->d);
  508. }
  509. static void poly_free_rec(__isl_take isl_poly_rec *rec)
  510. {
  511. int i;
  512. for (i = 0; i < rec->n; ++i)
  513. isl_poly_free(rec->p[i]);
  514. }
  515. __isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
  516. {
  517. if (!poly)
  518. return NULL;
  519. poly->ref++;
  520. return poly;
  521. }
  522. __isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
  523. {
  524. isl_poly_cst *cst;
  525. isl_poly_cst *dup;
  526. cst = isl_poly_as_cst(poly);
  527. if (!cst)
  528. return NULL;
  529. dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
  530. if (!dup)
  531. return NULL;
  532. isl_int_set(dup->n, cst->n);
  533. isl_int_set(dup->d, cst->d);
  534. return &dup->poly;
  535. }
  536. __isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
  537. {
  538. int i;
  539. isl_poly_rec *rec;
  540. isl_poly_rec *dup;
  541. rec = isl_poly_as_rec(poly);
  542. if (!rec)
  543. return NULL;
  544. dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
  545. if (!dup)
  546. return NULL;
  547. for (i = 0; i < rec->n; ++i) {
  548. dup->p[i] = isl_poly_copy(rec->p[i]);
  549. if (!dup->p[i])
  550. goto error;
  551. dup->n++;
  552. }
  553. return &dup->poly;
  554. error:
  555. isl_poly_free(&dup->poly);
  556. return NULL;
  557. }
  558. __isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
  559. {
  560. isl_bool is_cst;
  561. is_cst = isl_poly_is_cst(poly);
  562. if (is_cst < 0)
  563. return NULL;
  564. if (is_cst)
  565. return isl_poly_dup_cst(poly);
  566. else
  567. return isl_poly_dup_rec(poly);
  568. }
  569. __isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
  570. {
  571. if (!poly)
  572. return NULL;
  573. if (poly->ref == 1)
  574. return poly;
  575. poly->ref--;
  576. return isl_poly_dup(poly);
  577. }
  578. __isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
  579. {
  580. if (!poly)
  581. return NULL;
  582. if (--poly->ref > 0)
  583. return NULL;
  584. if (poly->var < 0)
  585. poly_free_cst((isl_poly_cst *) poly);
  586. else
  587. poly_free_rec((isl_poly_rec *) poly);
  588. isl_ctx_deref(poly->ctx);
  589. free(poly);
  590. return NULL;
  591. }
  592. static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
  593. {
  594. isl_int gcd;
  595. isl_int_init(gcd);
  596. isl_int_gcd(gcd, cst->n, cst->d);
  597. if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
  598. isl_int_divexact(cst->n, cst->n, gcd);
  599. isl_int_divexact(cst->d, cst->d, gcd);
  600. }
  601. isl_int_clear(gcd);
  602. }
  603. __isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
  604. __isl_take isl_poly *poly2)
  605. {
  606. isl_poly_cst *cst1;
  607. isl_poly_cst *cst2;
  608. poly1 = isl_poly_cow(poly1);
  609. if (!poly1 || !poly2)
  610. goto error;
  611. cst1 = isl_poly_as_cst(poly1);
  612. cst2 = isl_poly_as_cst(poly2);
  613. if (isl_int_eq(cst1->d, cst2->d))
  614. isl_int_add(cst1->n, cst1->n, cst2->n);
  615. else {
  616. isl_int_mul(cst1->n, cst1->n, cst2->d);
  617. isl_int_addmul(cst1->n, cst2->n, cst1->d);
  618. isl_int_mul(cst1->d, cst1->d, cst2->d);
  619. }
  620. isl_poly_cst_reduce(cst1);
  621. isl_poly_free(poly2);
  622. return poly1;
  623. error:
  624. isl_poly_free(poly1);
  625. isl_poly_free(poly2);
  626. return NULL;
  627. }
  628. static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
  629. {
  630. struct isl_ctx *ctx;
  631. if (!poly)
  632. return NULL;
  633. ctx = poly->ctx;
  634. isl_poly_free(poly);
  635. return isl_poly_zero(ctx);
  636. }
  637. static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
  638. {
  639. isl_poly_rec *rec;
  640. isl_poly *cst;
  641. if (!poly)
  642. return NULL;
  643. rec = isl_poly_as_rec(poly);
  644. if (!rec)
  645. goto error;
  646. cst = isl_poly_copy(rec->p[0]);
  647. isl_poly_free(poly);
  648. return cst;
  649. error:
  650. isl_poly_free(poly);
  651. return NULL;
  652. }
  653. __isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
  654. __isl_take isl_poly *poly2)
  655. {
  656. int i;
  657. isl_bool is_zero, is_nan, is_cst;
  658. isl_poly_rec *rec1, *rec2;
  659. if (!poly1 || !poly2)
  660. goto error;
  661. is_nan = isl_poly_is_nan(poly1);
  662. if (is_nan < 0)
  663. goto error;
  664. if (is_nan) {
  665. isl_poly_free(poly2);
  666. return poly1;
  667. }
  668. is_nan = isl_poly_is_nan(poly2);
  669. if (is_nan < 0)
  670. goto error;
  671. if (is_nan) {
  672. isl_poly_free(poly1);
  673. return poly2;
  674. }
  675. is_zero = isl_poly_is_zero(poly1);
  676. if (is_zero < 0)
  677. goto error;
  678. if (is_zero) {
  679. isl_poly_free(poly1);
  680. return poly2;
  681. }
  682. is_zero = isl_poly_is_zero(poly2);
  683. if (is_zero < 0)
  684. goto error;
  685. if (is_zero) {
  686. isl_poly_free(poly2);
  687. return poly1;
  688. }
  689. if (poly1->var < poly2->var)
  690. return isl_poly_sum(poly2, poly1);
  691. if (poly2->var < poly1->var) {
  692. isl_poly_rec *rec;
  693. isl_bool is_infty;
  694. is_infty = isl_poly_is_infty(poly2);
  695. if (is_infty >= 0 && !is_infty)
  696. is_infty = isl_poly_is_neginfty(poly2);
  697. if (is_infty < 0)
  698. goto error;
  699. if (is_infty) {
  700. isl_poly_free(poly1);
  701. return poly2;
  702. }
  703. poly1 = isl_poly_cow(poly1);
  704. rec = isl_poly_as_rec(poly1);
  705. if (!rec)
  706. goto error;
  707. rec->p[0] = isl_poly_sum(rec->p[0], poly2);
  708. if (rec->n == 1)
  709. poly1 = replace_by_constant_term(poly1);
  710. return poly1;
  711. }
  712. is_cst = isl_poly_is_cst(poly1);
  713. if (is_cst < 0)
  714. goto error;
  715. if (is_cst)
  716. return isl_poly_sum_cst(poly1, poly2);
  717. rec1 = isl_poly_as_rec(poly1);
  718. rec2 = isl_poly_as_rec(poly2);
  719. if (!rec1 || !rec2)
  720. goto error;
  721. if (rec1->n < rec2->n)
  722. return isl_poly_sum(poly2, poly1);
  723. poly1 = isl_poly_cow(poly1);
  724. rec1 = isl_poly_as_rec(poly1);
  725. if (!rec1)
  726. goto error;
  727. for (i = rec2->n - 1; i >= 0; --i) {
  728. isl_bool is_zero;
  729. rec1->p[i] = isl_poly_sum(rec1->p[i],
  730. isl_poly_copy(rec2->p[i]));
  731. if (!rec1->p[i])
  732. goto error;
  733. if (i != rec1->n - 1)
  734. continue;
  735. is_zero = isl_poly_is_zero(rec1->p[i]);
  736. if (is_zero < 0)
  737. goto error;
  738. if (is_zero) {
  739. isl_poly_free(rec1->p[i]);
  740. rec1->n--;
  741. }
  742. }
  743. if (rec1->n == 0)
  744. poly1 = replace_by_zero(poly1);
  745. else if (rec1->n == 1)
  746. poly1 = replace_by_constant_term(poly1);
  747. isl_poly_free(poly2);
  748. return poly1;
  749. error:
  750. isl_poly_free(poly1);
  751. isl_poly_free(poly2);
  752. return NULL;
  753. }
  754. __isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
  755. isl_int v)
  756. {
  757. isl_poly_cst *cst;
  758. poly = isl_poly_cow(poly);
  759. if (!poly)
  760. return NULL;
  761. cst = isl_poly_as_cst(poly);
  762. isl_int_addmul(cst->n, cst->d, v);
  763. return poly;
  764. }
  765. __isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
  766. {
  767. isl_bool is_cst;
  768. isl_poly_rec *rec;
  769. is_cst = isl_poly_is_cst(poly);
  770. if (is_cst < 0)
  771. return isl_poly_free(poly);
  772. if (is_cst)
  773. return isl_poly_cst_add_isl_int(poly, v);
  774. poly = isl_poly_cow(poly);
  775. rec = isl_poly_as_rec(poly);
  776. if (!rec)
  777. goto error;
  778. rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
  779. if (!rec->p[0])
  780. goto error;
  781. return poly;
  782. error:
  783. isl_poly_free(poly);
  784. return NULL;
  785. }
  786. __isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
  787. isl_int v)
  788. {
  789. isl_bool is_zero;
  790. isl_poly_cst *cst;
  791. is_zero = isl_poly_is_zero(poly);
  792. if (is_zero < 0)
  793. return isl_poly_free(poly);
  794. if (is_zero)
  795. return poly;
  796. poly = isl_poly_cow(poly);
  797. if (!poly)
  798. return NULL;
  799. cst = isl_poly_as_cst(poly);
  800. isl_int_mul(cst->n, cst->n, v);
  801. return poly;
  802. }
  803. __isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
  804. {
  805. int i;
  806. isl_bool is_cst;
  807. isl_poly_rec *rec;
  808. is_cst = isl_poly_is_cst(poly);
  809. if (is_cst < 0)
  810. return isl_poly_free(poly);
  811. if (is_cst)
  812. return isl_poly_cst_mul_isl_int(poly, v);
  813. poly = isl_poly_cow(poly);
  814. rec = isl_poly_as_rec(poly);
  815. if (!rec)
  816. goto error;
  817. for (i = 0; i < rec->n; ++i) {
  818. rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
  819. if (!rec->p[i])
  820. goto error;
  821. }
  822. return poly;
  823. error:
  824. isl_poly_free(poly);
  825. return NULL;
  826. }
  827. /* Multiply the constant polynomial "poly" by "v".
  828. */
  829. static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
  830. __isl_keep isl_val *v)
  831. {
  832. isl_bool is_zero;
  833. isl_poly_cst *cst;
  834. is_zero = isl_poly_is_zero(poly);
  835. if (is_zero < 0)
  836. return isl_poly_free(poly);
  837. if (is_zero)
  838. return poly;
  839. poly = isl_poly_cow(poly);
  840. if (!poly)
  841. return NULL;
  842. cst = isl_poly_as_cst(poly);
  843. isl_int_mul(cst->n, cst->n, v->n);
  844. isl_int_mul(cst->d, cst->d, v->d);
  845. isl_poly_cst_reduce(cst);
  846. return poly;
  847. }
  848. /* Multiply the polynomial "poly" by "v".
  849. */
  850. static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
  851. __isl_keep isl_val *v)
  852. {
  853. int i;
  854. isl_bool is_cst;
  855. isl_poly_rec *rec;
  856. is_cst = isl_poly_is_cst(poly);
  857. if (is_cst < 0)
  858. return isl_poly_free(poly);
  859. if (is_cst)
  860. return isl_poly_cst_scale_val(poly, v);
  861. poly = isl_poly_cow(poly);
  862. rec = isl_poly_as_rec(poly);
  863. if (!rec)
  864. goto error;
  865. for (i = 0; i < rec->n; ++i) {
  866. rec->p[i] = isl_poly_scale_val(rec->p[i], v);
  867. if (!rec->p[i])
  868. goto error;
  869. }
  870. return poly;
  871. error:
  872. isl_poly_free(poly);
  873. return NULL;
  874. }
  875. __isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
  876. __isl_take isl_poly *poly2)
  877. {
  878. isl_poly_cst *cst1;
  879. isl_poly_cst *cst2;
  880. poly1 = isl_poly_cow(poly1);
  881. if (!poly1 || !poly2)
  882. goto error;
  883. cst1 = isl_poly_as_cst(poly1);
  884. cst2 = isl_poly_as_cst(poly2);
  885. isl_int_mul(cst1->n, cst1->n, cst2->n);
  886. isl_int_mul(cst1->d, cst1->d, cst2->d);
  887. isl_poly_cst_reduce(cst1);
  888. isl_poly_free(poly2);
  889. return poly1;
  890. error:
  891. isl_poly_free(poly1);
  892. isl_poly_free(poly2);
  893. return NULL;
  894. }
  895. __isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
  896. __isl_take isl_poly *poly2)
  897. {
  898. isl_poly_rec *rec1;
  899. isl_poly_rec *rec2;
  900. isl_poly_rec *res = NULL;
  901. int i, j;
  902. int size;
  903. rec1 = isl_poly_as_rec(poly1);
  904. rec2 = isl_poly_as_rec(poly2);
  905. if (!rec1 || !rec2)
  906. goto error;
  907. size = rec1->n + rec2->n - 1;
  908. res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
  909. if (!res)
  910. goto error;
  911. for (i = 0; i < rec1->n; ++i) {
  912. res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
  913. isl_poly_copy(rec1->p[i]));
  914. if (!res->p[i])
  915. goto error;
  916. res->n++;
  917. }
  918. for (; i < size; ++i) {
  919. res->p[i] = isl_poly_zero(poly1->ctx);
  920. if (!res->p[i])
  921. goto error;
  922. res->n++;
  923. }
  924. for (i = 0; i < rec1->n; ++i) {
  925. for (j = 1; j < rec2->n; ++j) {
  926. isl_poly *poly;
  927. poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
  928. isl_poly_copy(rec1->p[i]));
  929. res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
  930. if (!res->p[i + j])
  931. goto error;
  932. }
  933. }
  934. isl_poly_free(poly1);
  935. isl_poly_free(poly2);
  936. return &res->poly;
  937. error:
  938. isl_poly_free(poly1);
  939. isl_poly_free(poly2);
  940. isl_poly_free(&res->poly);
  941. return NULL;
  942. }
  943. __isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
  944. __isl_take isl_poly *poly2)
  945. {
  946. isl_bool is_zero, is_nan, is_one, is_cst;
  947. if (!poly1 || !poly2)
  948. goto error;
  949. is_nan = isl_poly_is_nan(poly1);
  950. if (is_nan < 0)
  951. goto error;
  952. if (is_nan) {
  953. isl_poly_free(poly2);
  954. return poly1;
  955. }
  956. is_nan = isl_poly_is_nan(poly2);
  957. if (is_nan < 0)
  958. goto error;
  959. if (is_nan) {
  960. isl_poly_free(poly1);
  961. return poly2;
  962. }
  963. is_zero = isl_poly_is_zero(poly1);
  964. if (is_zero < 0)
  965. goto error;
  966. if (is_zero) {
  967. isl_poly_free(poly2);
  968. return poly1;
  969. }
  970. is_zero = isl_poly_is_zero(poly2);
  971. if (is_zero < 0)
  972. goto error;
  973. if (is_zero) {
  974. isl_poly_free(poly1);
  975. return poly2;
  976. }
  977. is_one = isl_poly_is_one(poly1);
  978. if (is_one < 0)
  979. goto error;
  980. if (is_one) {
  981. isl_poly_free(poly1);
  982. return poly2;
  983. }
  984. is_one = isl_poly_is_one(poly2);
  985. if (is_one < 0)
  986. goto error;
  987. if (is_one) {
  988. isl_poly_free(poly2);
  989. return poly1;
  990. }
  991. if (poly1->var < poly2->var)
  992. return isl_poly_mul(poly2, poly1);
  993. if (poly2->var < poly1->var) {
  994. int i;
  995. isl_poly_rec *rec;
  996. isl_bool is_infty;
  997. is_infty = isl_poly_is_infty(poly2);
  998. if (is_infty >= 0 && !is_infty)
  999. is_infty = isl_poly_is_neginfty(poly2);
  1000. if (is_infty < 0)
  1001. goto error;
  1002. if (is_infty) {
  1003. isl_ctx *ctx = poly1->ctx;
  1004. isl_poly_free(poly1);
  1005. isl_poly_free(poly2);
  1006. return isl_poly_nan(ctx);
  1007. }
  1008. poly1 = isl_poly_cow(poly1);
  1009. rec = isl_poly_as_rec(poly1);
  1010. if (!rec)
  1011. goto error;
  1012. for (i = 0; i < rec->n; ++i) {
  1013. rec->p[i] = isl_poly_mul(rec->p[i],
  1014. isl_poly_copy(poly2));
  1015. if (!rec->p[i])
  1016. goto error;
  1017. }
  1018. isl_poly_free(poly2);
  1019. return poly1;
  1020. }
  1021. is_cst = isl_poly_is_cst(poly1);
  1022. if (is_cst < 0)
  1023. goto error;
  1024. if (is_cst)
  1025. return isl_poly_mul_cst(poly1, poly2);
  1026. return isl_poly_mul_rec(poly1, poly2);
  1027. error:
  1028. isl_poly_free(poly1);
  1029. isl_poly_free(poly2);
  1030. return NULL;
  1031. }
  1032. __isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
  1033. {
  1034. isl_poly *res;
  1035. if (!poly)
  1036. return NULL;
  1037. if (power == 1)
  1038. return poly;
  1039. if (power % 2)
  1040. res = isl_poly_copy(poly);
  1041. else
  1042. res = isl_poly_one(poly->ctx);
  1043. while (power >>= 1) {
  1044. poly = isl_poly_mul(poly, isl_poly_copy(poly));
  1045. if (power % 2)
  1046. res = isl_poly_mul(res, isl_poly_copy(poly));
  1047. }
  1048. isl_poly_free(poly);
  1049. return res;
  1050. }
  1051. __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
  1052. unsigned n_div, __isl_take isl_poly *poly)
  1053. {
  1054. struct isl_qpolynomial *qp = NULL;
  1055. isl_size total;
  1056. total = isl_space_dim(space, isl_dim_all);
  1057. if (total < 0 || !poly)
  1058. goto error;
  1059. if (!isl_space_is_set(space))
  1060. isl_die(isl_space_get_ctx(space), isl_error_invalid,
  1061. "domain of polynomial should be a set", goto error);
  1062. qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
  1063. if (!qp)
  1064. goto error;
  1065. qp->ref = 1;
  1066. qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
  1067. if (!qp->div)
  1068. goto error;
  1069. qp->dim = space;
  1070. qp->poly = poly;
  1071. return qp;
  1072. error:
  1073. isl_space_free(space);
  1074. isl_poly_free(poly);
  1075. isl_qpolynomial_free(qp);
  1076. return NULL;
  1077. }
  1078. __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
  1079. {
  1080. if (!qp)
  1081. return NULL;
  1082. qp->ref++;
  1083. return qp;
  1084. }
  1085. __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
  1086. {
  1087. struct isl_qpolynomial *dup;
  1088. if (!qp)
  1089. return NULL;
  1090. dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
  1091. isl_poly_copy(qp->poly));
  1092. if (!dup)
  1093. return NULL;
  1094. isl_mat_free(dup->div);
  1095. dup->div = isl_mat_copy(qp->div);
  1096. if (!dup->div)
  1097. goto error;
  1098. return dup;
  1099. error:
  1100. isl_qpolynomial_free(dup);
  1101. return NULL;
  1102. }
  1103. __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
  1104. {
  1105. if (!qp)
  1106. return NULL;
  1107. if (qp->ref == 1)
  1108. return qp;
  1109. qp->ref--;
  1110. return isl_qpolynomial_dup(qp);
  1111. }
  1112. __isl_null isl_qpolynomial *isl_qpolynomial_free(
  1113. __isl_take isl_qpolynomial *qp)
  1114. {
  1115. if (!qp)
  1116. return NULL;
  1117. if (--qp->ref > 0)
  1118. return NULL;
  1119. isl_space_free(qp->dim);
  1120. isl_mat_free(qp->div);
  1121. isl_poly_free(qp->poly);
  1122. free(qp);
  1123. return NULL;
  1124. }
  1125. __isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
  1126. {
  1127. int i;
  1128. isl_poly_rec *rec;
  1129. isl_poly_cst *cst;
  1130. rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
  1131. if (!rec)
  1132. return NULL;
  1133. for (i = 0; i < 1 + power; ++i) {
  1134. rec->p[i] = isl_poly_zero(ctx);
  1135. if (!rec->p[i])
  1136. goto error;
  1137. rec->n++;
  1138. }
  1139. cst = isl_poly_as_cst(rec->p[power]);
  1140. isl_int_set_si(cst->n, 1);
  1141. return &rec->poly;
  1142. error:
  1143. isl_poly_free(&rec->poly);
  1144. return NULL;
  1145. }
  1146. /* r array maps original positions to new positions.
  1147. */
  1148. static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
  1149. {
  1150. int i;
  1151. isl_bool is_cst;
  1152. isl_poly_rec *rec;
  1153. isl_poly *base;
  1154. isl_poly *res;
  1155. is_cst = isl_poly_is_cst(poly);
  1156. if (is_cst < 0)
  1157. return isl_poly_free(poly);
  1158. if (is_cst)
  1159. return poly;
  1160. rec = isl_poly_as_rec(poly);
  1161. if (!rec)
  1162. goto error;
  1163. isl_assert(poly->ctx, rec->n >= 1, goto error);
  1164. base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
  1165. res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
  1166. for (i = rec->n - 2; i >= 0; --i) {
  1167. res = isl_poly_mul(res, isl_poly_copy(base));
  1168. res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
  1169. }
  1170. isl_poly_free(base);
  1171. isl_poly_free(poly);
  1172. return res;
  1173. error:
  1174. isl_poly_free(poly);
  1175. return NULL;
  1176. }
  1177. static isl_bool compatible_divs(__isl_keep isl_mat *div1,
  1178. __isl_keep isl_mat *div2)
  1179. {
  1180. int n_row, n_col;
  1181. isl_bool equal;
  1182. isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
  1183. div1->n_col >= div2->n_col,
  1184. return isl_bool_error);
  1185. if (div1->n_row == div2->n_row)
  1186. return isl_mat_is_equal(div1, div2);
  1187. n_row = div1->n_row;
  1188. n_col = div1->n_col;
  1189. div1->n_row = div2->n_row;
  1190. div1->n_col = div2->n_col;
  1191. equal = isl_mat_is_equal(div1, div2);
  1192. div1->n_row = n_row;
  1193. div1->n_col = n_col;
  1194. return equal;
  1195. }
  1196. static int cmp_row(__isl_keep isl_mat *div, int i, int j)
  1197. {
  1198. int li, lj;
  1199. li = isl_seq_last_non_zero(div->row[i], div->n_col);
  1200. lj = isl_seq_last_non_zero(div->row[j], div->n_col);
  1201. if (li != lj)
  1202. return li - lj;
  1203. return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
  1204. }
  1205. struct isl_div_sort_info {
  1206. isl_mat *div;
  1207. int row;
  1208. };
  1209. static int div_sort_cmp(const void *p1, const void *p2)
  1210. {
  1211. const struct isl_div_sort_info *i1, *i2;
  1212. i1 = (const struct isl_div_sort_info *) p1;
  1213. i2 = (const struct isl_div_sort_info *) p2;
  1214. return cmp_row(i1->div, i1->row, i2->row);
  1215. }
  1216. /* Sort divs and remove duplicates.
  1217. */
  1218. static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
  1219. {
  1220. int i;
  1221. int skip;
  1222. int len;
  1223. struct isl_div_sort_info *array = NULL;
  1224. int *pos = NULL, *at = NULL;
  1225. int *reordering = NULL;
  1226. isl_size div_pos;
  1227. if (!qp)
  1228. return NULL;
  1229. if (qp->div->n_row <= 1)
  1230. return qp;
  1231. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  1232. if (div_pos < 0)
  1233. return isl_qpolynomial_free(qp);
  1234. array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
  1235. qp->div->n_row);
  1236. pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
  1237. at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
  1238. len = qp->div->n_col - 2;
  1239. reordering = isl_alloc_array(qp->div->ctx, int, len);
  1240. if (!array || !pos || !at || !reordering)
  1241. goto error;
  1242. for (i = 0; i < qp->div->n_row; ++i) {
  1243. array[i].div = qp->div;
  1244. array[i].row = i;
  1245. pos[i] = i;
  1246. at[i] = i;
  1247. }
  1248. qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
  1249. div_sort_cmp);
  1250. for (i = 0; i < div_pos; ++i)
  1251. reordering[i] = i;
  1252. for (i = 0; i < qp->div->n_row; ++i) {
  1253. if (pos[array[i].row] == i)
  1254. continue;
  1255. qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
  1256. pos[at[i]] = pos[array[i].row];
  1257. at[pos[array[i].row]] = at[i];
  1258. at[i] = array[i].row;
  1259. pos[array[i].row] = i;
  1260. }
  1261. skip = 0;
  1262. for (i = 0; i < len - div_pos; ++i) {
  1263. if (i > 0 &&
  1264. isl_seq_eq(qp->div->row[i - skip - 1],
  1265. qp->div->row[i - skip], qp->div->n_col)) {
  1266. qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
  1267. isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
  1268. 2 + div_pos + i - skip);
  1269. qp->div = isl_mat_drop_cols(qp->div,
  1270. 2 + div_pos + i - skip, 1);
  1271. skip++;
  1272. }
  1273. reordering[div_pos + array[i].row] = div_pos + i - skip;
  1274. }
  1275. qp->poly = reorder(qp->poly, reordering);
  1276. if (!qp->poly || !qp->div)
  1277. goto error;
  1278. free(at);
  1279. free(pos);
  1280. free(array);
  1281. free(reordering);
  1282. return qp;
  1283. error:
  1284. free(at);
  1285. free(pos);
  1286. free(array);
  1287. free(reordering);
  1288. isl_qpolynomial_free(qp);
  1289. return NULL;
  1290. }
  1291. static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
  1292. int first)
  1293. {
  1294. int i;
  1295. isl_bool is_cst;
  1296. isl_poly_rec *rec;
  1297. is_cst = isl_poly_is_cst(poly);
  1298. if (is_cst < 0)
  1299. return isl_poly_free(poly);
  1300. if (is_cst)
  1301. return poly;
  1302. if (poly->var < first)
  1303. return poly;
  1304. if (exp[poly->var - first] == poly->var - first)
  1305. return poly;
  1306. poly = isl_poly_cow(poly);
  1307. if (!poly)
  1308. goto error;
  1309. poly->var = exp[poly->var - first] + first;
  1310. rec = isl_poly_as_rec(poly);
  1311. if (!rec)
  1312. goto error;
  1313. for (i = 0; i < rec->n; ++i) {
  1314. rec->p[i] = expand(rec->p[i], exp, first);
  1315. if (!rec->p[i])
  1316. goto error;
  1317. }
  1318. return poly;
  1319. error:
  1320. isl_poly_free(poly);
  1321. return NULL;
  1322. }
  1323. static __isl_give isl_qpolynomial *with_merged_divs(
  1324. __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
  1325. __isl_take isl_qpolynomial *qp2),
  1326. __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
  1327. {
  1328. int *exp1 = NULL;
  1329. int *exp2 = NULL;
  1330. isl_mat *div = NULL;
  1331. int n_div1, n_div2;
  1332. qp1 = isl_qpolynomial_cow(qp1);
  1333. qp2 = isl_qpolynomial_cow(qp2);
  1334. if (!qp1 || !qp2)
  1335. goto error;
  1336. isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
  1337. qp1->div->n_col >= qp2->div->n_col, goto error);
  1338. n_div1 = qp1->div->n_row;
  1339. n_div2 = qp2->div->n_row;
  1340. exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
  1341. exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
  1342. if ((n_div1 && !exp1) || (n_div2 && !exp2))
  1343. goto error;
  1344. div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
  1345. if (!div)
  1346. goto error;
  1347. isl_mat_free(qp1->div);
  1348. qp1->div = isl_mat_copy(div);
  1349. isl_mat_free(qp2->div);
  1350. qp2->div = isl_mat_copy(div);
  1351. qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
  1352. qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
  1353. if (!qp1->poly || !qp2->poly)
  1354. goto error;
  1355. isl_mat_free(div);
  1356. free(exp1);
  1357. free(exp2);
  1358. return fn(qp1, qp2);
  1359. error:
  1360. isl_mat_free(div);
  1361. free(exp1);
  1362. free(exp2);
  1363. isl_qpolynomial_free(qp1);
  1364. isl_qpolynomial_free(qp2);
  1365. return NULL;
  1366. }
  1367. __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
  1368. __isl_take isl_qpolynomial *qp2)
  1369. {
  1370. isl_bool compatible;
  1371. qp1 = isl_qpolynomial_cow(qp1);
  1372. if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
  1373. goto error;
  1374. if (qp1->div->n_row < qp2->div->n_row)
  1375. return isl_qpolynomial_add(qp2, qp1);
  1376. compatible = compatible_divs(qp1->div, qp2->div);
  1377. if (compatible < 0)
  1378. goto error;
  1379. if (!compatible)
  1380. return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
  1381. qp1->poly = isl_poly_sum(qp1->poly, isl_poly_copy(qp2->poly));
  1382. if (!qp1->poly)
  1383. goto error;
  1384. isl_qpolynomial_free(qp2);
  1385. return qp1;
  1386. error:
  1387. isl_qpolynomial_free(qp1);
  1388. isl_qpolynomial_free(qp2);
  1389. return NULL;
  1390. }
  1391. __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
  1392. __isl_keep isl_set *dom,
  1393. __isl_take isl_qpolynomial *qp1,
  1394. __isl_take isl_qpolynomial *qp2)
  1395. {
  1396. qp1 = isl_qpolynomial_add(qp1, qp2);
  1397. qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
  1398. return qp1;
  1399. }
  1400. __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
  1401. __isl_take isl_qpolynomial *qp2)
  1402. {
  1403. return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
  1404. }
  1405. __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
  1406. __isl_take isl_qpolynomial *qp, isl_int v)
  1407. {
  1408. if (isl_int_is_zero(v))
  1409. return qp;
  1410. qp = isl_qpolynomial_cow(qp);
  1411. if (!qp)
  1412. return NULL;
  1413. qp->poly = isl_poly_add_isl_int(qp->poly, v);
  1414. if (!qp->poly)
  1415. goto error;
  1416. return qp;
  1417. error:
  1418. isl_qpolynomial_free(qp);
  1419. return NULL;
  1420. }
  1421. __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
  1422. {
  1423. if (!qp)
  1424. return NULL;
  1425. return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
  1426. }
  1427. __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
  1428. __isl_take isl_qpolynomial *qp, isl_int v)
  1429. {
  1430. if (isl_int_is_one(v))
  1431. return qp;
  1432. if (qp && isl_int_is_zero(v)) {
  1433. isl_qpolynomial *zero;
  1434. zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
  1435. isl_qpolynomial_free(qp);
  1436. return zero;
  1437. }
  1438. qp = isl_qpolynomial_cow(qp);
  1439. if (!qp)
  1440. return NULL;
  1441. qp->poly = isl_poly_mul_isl_int(qp->poly, v);
  1442. if (!qp->poly)
  1443. goto error;
  1444. return qp;
  1445. error:
  1446. isl_qpolynomial_free(qp);
  1447. return NULL;
  1448. }
  1449. __isl_give isl_qpolynomial *isl_qpolynomial_scale(
  1450. __isl_take isl_qpolynomial *qp, isl_int v)
  1451. {
  1452. return isl_qpolynomial_mul_isl_int(qp, v);
  1453. }
  1454. /* Multiply "qp" by "v".
  1455. */
  1456. __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
  1457. __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
  1458. {
  1459. if (!qp || !v)
  1460. goto error;
  1461. if (!isl_val_is_rat(v))
  1462. isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
  1463. "expecting rational factor", goto error);
  1464. if (isl_val_is_one(v)) {
  1465. isl_val_free(v);
  1466. return qp;
  1467. }
  1468. if (isl_val_is_zero(v)) {
  1469. isl_space *space;
  1470. space = isl_qpolynomial_get_domain_space(qp);
  1471. isl_qpolynomial_free(qp);
  1472. isl_val_free(v);
  1473. return isl_qpolynomial_zero_on_domain(space);
  1474. }
  1475. qp = isl_qpolynomial_cow(qp);
  1476. if (!qp)
  1477. goto error;
  1478. qp->poly = isl_poly_scale_val(qp->poly, v);
  1479. if (!qp->poly)
  1480. qp = isl_qpolynomial_free(qp);
  1481. isl_val_free(v);
  1482. return qp;
  1483. error:
  1484. isl_val_free(v);
  1485. isl_qpolynomial_free(qp);
  1486. return NULL;
  1487. }
  1488. /* Divide "qp" by "v".
  1489. */
  1490. __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
  1491. __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
  1492. {
  1493. if (!qp || !v)
  1494. goto error;
  1495. if (!isl_val_is_rat(v))
  1496. isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
  1497. "expecting rational factor", goto error);
  1498. if (isl_val_is_zero(v))
  1499. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  1500. "cannot scale down by zero", goto error);
  1501. return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
  1502. error:
  1503. isl_val_free(v);
  1504. isl_qpolynomial_free(qp);
  1505. return NULL;
  1506. }
  1507. __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
  1508. __isl_take isl_qpolynomial *qp2)
  1509. {
  1510. isl_bool compatible;
  1511. qp1 = isl_qpolynomial_cow(qp1);
  1512. if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
  1513. goto error;
  1514. if (qp1->div->n_row < qp2->div->n_row)
  1515. return isl_qpolynomial_mul(qp2, qp1);
  1516. compatible = compatible_divs(qp1->div, qp2->div);
  1517. if (compatible < 0)
  1518. goto error;
  1519. if (!compatible)
  1520. return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
  1521. qp1->poly = isl_poly_mul(qp1->poly, isl_poly_copy(qp2->poly));
  1522. if (!qp1->poly)
  1523. goto error;
  1524. isl_qpolynomial_free(qp2);
  1525. return qp1;
  1526. error:
  1527. isl_qpolynomial_free(qp1);
  1528. isl_qpolynomial_free(qp2);
  1529. return NULL;
  1530. }
  1531. __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
  1532. unsigned power)
  1533. {
  1534. qp = isl_qpolynomial_cow(qp);
  1535. if (!qp)
  1536. return NULL;
  1537. qp->poly = isl_poly_pow(qp->poly, power);
  1538. if (!qp->poly)
  1539. goto error;
  1540. return qp;
  1541. error:
  1542. isl_qpolynomial_free(qp);
  1543. return NULL;
  1544. }
  1545. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
  1546. __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
  1547. {
  1548. int i;
  1549. if (power == 1)
  1550. return pwqp;
  1551. pwqp = isl_pw_qpolynomial_cow(pwqp);
  1552. if (!pwqp)
  1553. return NULL;
  1554. for (i = 0; i < pwqp->n; ++i) {
  1555. pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
  1556. if (!pwqp->p[i].qp)
  1557. return isl_pw_qpolynomial_free(pwqp);
  1558. }
  1559. return pwqp;
  1560. }
  1561. __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
  1562. __isl_take isl_space *domain)
  1563. {
  1564. if (!domain)
  1565. return NULL;
  1566. return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
  1567. }
  1568. __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
  1569. __isl_take isl_space *domain)
  1570. {
  1571. if (!domain)
  1572. return NULL;
  1573. return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
  1574. }
  1575. __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
  1576. __isl_take isl_space *domain)
  1577. {
  1578. if (!domain)
  1579. return NULL;
  1580. return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
  1581. }
  1582. __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
  1583. __isl_take isl_space *domain)
  1584. {
  1585. if (!domain)
  1586. return NULL;
  1587. return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
  1588. }
  1589. __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
  1590. __isl_take isl_space *domain)
  1591. {
  1592. if (!domain)
  1593. return NULL;
  1594. return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
  1595. }
  1596. __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
  1597. __isl_take isl_space *domain,
  1598. isl_int v)
  1599. {
  1600. struct isl_qpolynomial *qp;
  1601. isl_poly_cst *cst;
  1602. qp = isl_qpolynomial_zero_on_domain(domain);
  1603. if (!qp)
  1604. return NULL;
  1605. cst = isl_poly_as_cst(qp->poly);
  1606. isl_int_set(cst->n, v);
  1607. return qp;
  1608. }
  1609. isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
  1610. isl_int *n, isl_int *d)
  1611. {
  1612. isl_bool is_cst;
  1613. isl_poly_cst *cst;
  1614. if (!qp)
  1615. return isl_bool_error;
  1616. is_cst = isl_poly_is_cst(qp->poly);
  1617. if (is_cst < 0 || !is_cst)
  1618. return is_cst;
  1619. cst = isl_poly_as_cst(qp->poly);
  1620. if (!cst)
  1621. return isl_bool_error;
  1622. if (n)
  1623. isl_int_set(*n, cst->n);
  1624. if (d)
  1625. isl_int_set(*d, cst->d);
  1626. return isl_bool_true;
  1627. }
  1628. /* Return the constant term of "poly".
  1629. */
  1630. static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
  1631. {
  1632. isl_bool is_cst;
  1633. isl_poly_cst *cst;
  1634. if (!poly)
  1635. return NULL;
  1636. while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
  1637. isl_poly_rec *rec;
  1638. rec = isl_poly_as_rec(poly);
  1639. if (!rec)
  1640. return NULL;
  1641. poly = rec->p[0];
  1642. }
  1643. if (is_cst < 0)
  1644. return NULL;
  1645. cst = isl_poly_as_cst(poly);
  1646. if (!cst)
  1647. return NULL;
  1648. return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
  1649. }
  1650. /* Return the constant term of "qp".
  1651. */
  1652. __isl_give isl_val *isl_qpolynomial_get_constant_val(
  1653. __isl_keep isl_qpolynomial *qp)
  1654. {
  1655. if (!qp)
  1656. return NULL;
  1657. return isl_poly_get_constant_val(qp->poly);
  1658. }
  1659. isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
  1660. {
  1661. isl_bool is_cst;
  1662. isl_poly_rec *rec;
  1663. if (!poly)
  1664. return isl_bool_error;
  1665. if (poly->var < 0)
  1666. return isl_bool_true;
  1667. rec = isl_poly_as_rec(poly);
  1668. if (!rec)
  1669. return isl_bool_error;
  1670. if (rec->n > 2)
  1671. return isl_bool_false;
  1672. isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
  1673. is_cst = isl_poly_is_cst(rec->p[1]);
  1674. if (is_cst < 0 || !is_cst)
  1675. return is_cst;
  1676. return isl_poly_is_affine(rec->p[0]);
  1677. }
  1678. isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
  1679. {
  1680. if (!qp)
  1681. return isl_bool_error;
  1682. if (qp->div->n_row > 0)
  1683. return isl_bool_false;
  1684. return isl_poly_is_affine(qp->poly);
  1685. }
  1686. static void update_coeff(__isl_keep isl_vec *aff,
  1687. __isl_keep isl_poly_cst *cst, int pos)
  1688. {
  1689. isl_int gcd;
  1690. isl_int f;
  1691. if (isl_int_is_zero(cst->n))
  1692. return;
  1693. isl_int_init(gcd);
  1694. isl_int_init(f);
  1695. isl_int_gcd(gcd, cst->d, aff->el[0]);
  1696. isl_int_divexact(f, cst->d, gcd);
  1697. isl_int_divexact(gcd, aff->el[0], gcd);
  1698. isl_seq_scale(aff->el, aff->el, f, aff->size);
  1699. isl_int_mul(aff->el[1 + pos], gcd, cst->n);
  1700. isl_int_clear(gcd);
  1701. isl_int_clear(f);
  1702. }
  1703. int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
  1704. {
  1705. isl_poly_cst *cst;
  1706. isl_poly_rec *rec;
  1707. if (!poly || !aff)
  1708. return -1;
  1709. if (poly->var < 0) {
  1710. isl_poly_cst *cst;
  1711. cst = isl_poly_as_cst(poly);
  1712. if (!cst)
  1713. return -1;
  1714. update_coeff(aff, cst, 0);
  1715. return 0;
  1716. }
  1717. rec = isl_poly_as_rec(poly);
  1718. if (!rec)
  1719. return -1;
  1720. isl_assert(poly->ctx, rec->n == 2, return -1);
  1721. cst = isl_poly_as_cst(rec->p[1]);
  1722. if (!cst)
  1723. return -1;
  1724. update_coeff(aff, cst, 1 + poly->var);
  1725. return isl_poly_update_affine(rec->p[0], aff);
  1726. }
  1727. __isl_give isl_vec *isl_qpolynomial_extract_affine(
  1728. __isl_keep isl_qpolynomial *qp)
  1729. {
  1730. isl_vec *aff;
  1731. isl_size d;
  1732. d = isl_qpolynomial_domain_dim(qp, isl_dim_all);
  1733. if (d < 0)
  1734. return NULL;
  1735. aff = isl_vec_alloc(qp->div->ctx, 2 + d);
  1736. if (!aff)
  1737. return NULL;
  1738. isl_seq_clr(aff->el + 1, 1 + d);
  1739. isl_int_set_si(aff->el[0], 1);
  1740. if (isl_poly_update_affine(qp->poly, aff) < 0)
  1741. goto error;
  1742. return aff;
  1743. error:
  1744. isl_vec_free(aff);
  1745. return NULL;
  1746. }
  1747. /* Compare two quasi-polynomials.
  1748. *
  1749. * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
  1750. * than "qp2" and 0 if they are equal.
  1751. */
  1752. int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
  1753. __isl_keep isl_qpolynomial *qp2)
  1754. {
  1755. int cmp;
  1756. if (qp1 == qp2)
  1757. return 0;
  1758. if (!qp1)
  1759. return -1;
  1760. if (!qp2)
  1761. return 1;
  1762. cmp = isl_space_cmp(qp1->dim, qp2->dim);
  1763. if (cmp != 0)
  1764. return cmp;
  1765. cmp = isl_local_cmp(qp1->div, qp2->div);
  1766. if (cmp != 0)
  1767. return cmp;
  1768. return isl_poly_plain_cmp(qp1->poly, qp2->poly);
  1769. }
  1770. /* Is "qp1" obviously equal to "qp2"?
  1771. *
  1772. * NaN is not equal to anything, not even to another NaN.
  1773. */
  1774. isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
  1775. __isl_keep isl_qpolynomial *qp2)
  1776. {
  1777. isl_bool equal;
  1778. if (!qp1 || !qp2)
  1779. return isl_bool_error;
  1780. if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
  1781. return isl_bool_false;
  1782. equal = isl_space_is_equal(qp1->dim, qp2->dim);
  1783. if (equal < 0 || !equal)
  1784. return equal;
  1785. equal = isl_mat_is_equal(qp1->div, qp2->div);
  1786. if (equal < 0 || !equal)
  1787. return equal;
  1788. return isl_poly_is_equal(qp1->poly, qp2->poly);
  1789. }
  1790. static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
  1791. {
  1792. int i;
  1793. isl_bool is_cst;
  1794. isl_poly_rec *rec;
  1795. is_cst = isl_poly_is_cst(poly);
  1796. if (is_cst < 0)
  1797. return isl_stat_error;
  1798. if (is_cst) {
  1799. isl_poly_cst *cst;
  1800. cst = isl_poly_as_cst(poly);
  1801. if (!cst)
  1802. return isl_stat_error;
  1803. isl_int_lcm(*d, *d, cst->d);
  1804. return isl_stat_ok;
  1805. }
  1806. rec = isl_poly_as_rec(poly);
  1807. if (!rec)
  1808. return isl_stat_error;
  1809. for (i = 0; i < rec->n; ++i)
  1810. poly_update_den(rec->p[i], d);
  1811. return isl_stat_ok;
  1812. }
  1813. __isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
  1814. {
  1815. isl_val *d;
  1816. if (!qp)
  1817. return NULL;
  1818. d = isl_val_one(isl_qpolynomial_get_ctx(qp));
  1819. if (!d)
  1820. return NULL;
  1821. if (poly_update_den(qp->poly, &d->n) < 0)
  1822. return isl_val_free(d);
  1823. return d;
  1824. }
  1825. __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
  1826. __isl_take isl_space *domain, int pos, int power)
  1827. {
  1828. struct isl_ctx *ctx;
  1829. if (!domain)
  1830. return NULL;
  1831. ctx = domain->ctx;
  1832. return isl_qpolynomial_alloc(domain, 0,
  1833. isl_poly_var_pow(ctx, pos, power));
  1834. }
  1835. __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
  1836. __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
  1837. {
  1838. if (isl_space_check_is_set(domain ) < 0)
  1839. goto error;
  1840. if (isl_space_check_range(domain, type, pos, 1) < 0)
  1841. goto error;
  1842. pos += isl_space_offset(domain, type);
  1843. return isl_qpolynomial_var_pow_on_domain(domain, pos, 1);
  1844. error:
  1845. isl_space_free(domain);
  1846. return NULL;
  1847. }
  1848. __isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
  1849. unsigned first, unsigned n, __isl_keep isl_poly **subs)
  1850. {
  1851. int i;
  1852. isl_bool is_cst;
  1853. isl_poly_rec *rec;
  1854. isl_poly *base, *res;
  1855. is_cst = isl_poly_is_cst(poly);
  1856. if (is_cst < 0)
  1857. return isl_poly_free(poly);
  1858. if (is_cst)
  1859. return poly;
  1860. if (poly->var < first)
  1861. return poly;
  1862. rec = isl_poly_as_rec(poly);
  1863. if (!rec)
  1864. goto error;
  1865. isl_assert(poly->ctx, rec->n >= 1, goto error);
  1866. if (poly->var >= first + n)
  1867. base = isl_poly_var_pow(poly->ctx, poly->var, 1);
  1868. else
  1869. base = isl_poly_copy(subs[poly->var - first]);
  1870. res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
  1871. for (i = rec->n - 2; i >= 0; --i) {
  1872. isl_poly *t;
  1873. t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
  1874. res = isl_poly_mul(res, isl_poly_copy(base));
  1875. res = isl_poly_sum(res, t);
  1876. }
  1877. isl_poly_free(base);
  1878. isl_poly_free(poly);
  1879. return res;
  1880. error:
  1881. isl_poly_free(poly);
  1882. return NULL;
  1883. }
  1884. __isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
  1885. isl_int denom, unsigned len)
  1886. {
  1887. int i;
  1888. isl_poly *poly;
  1889. isl_assert(ctx, len >= 1, return NULL);
  1890. poly = isl_poly_rat_cst(ctx, f[0], denom);
  1891. for (i = 0; i < len - 1; ++i) {
  1892. isl_poly *t;
  1893. isl_poly *c;
  1894. if (isl_int_is_zero(f[1 + i]))
  1895. continue;
  1896. c = isl_poly_rat_cst(ctx, f[1 + i], denom);
  1897. t = isl_poly_var_pow(ctx, i, 1);
  1898. t = isl_poly_mul(c, t);
  1899. poly = isl_poly_sum(poly, t);
  1900. }
  1901. return poly;
  1902. }
  1903. /* Remove common factor of non-constant terms and denominator.
  1904. */
  1905. static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
  1906. {
  1907. isl_ctx *ctx = qp->div->ctx;
  1908. unsigned total = qp->div->n_col - 2;
  1909. isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
  1910. isl_int_gcd(ctx->normalize_gcd,
  1911. ctx->normalize_gcd, qp->div->row[div][0]);
  1912. if (isl_int_is_one(ctx->normalize_gcd))
  1913. return;
  1914. isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
  1915. ctx->normalize_gcd, total);
  1916. isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
  1917. ctx->normalize_gcd);
  1918. isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
  1919. ctx->normalize_gcd);
  1920. }
  1921. /* Replace the integer division identified by "div" by the polynomial "s".
  1922. * The integer division is assumed not to appear in the definition
  1923. * of any other integer divisions.
  1924. */
  1925. static __isl_give isl_qpolynomial *substitute_div(
  1926. __isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
  1927. {
  1928. int i;
  1929. isl_size div_pos;
  1930. int *reordering;
  1931. isl_ctx *ctx;
  1932. if (!qp || !s)
  1933. goto error;
  1934. qp = isl_qpolynomial_cow(qp);
  1935. if (!qp)
  1936. goto error;
  1937. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  1938. if (div_pos < 0)
  1939. goto error;
  1940. qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
  1941. if (!qp->poly)
  1942. goto error;
  1943. ctx = isl_qpolynomial_get_ctx(qp);
  1944. reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row);
  1945. if (!reordering)
  1946. goto error;
  1947. for (i = 0; i < div_pos + div; ++i)
  1948. reordering[i] = i;
  1949. for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
  1950. reordering[i] = i - 1;
  1951. qp->div = isl_mat_drop_rows(qp->div, div, 1);
  1952. qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
  1953. qp->poly = reorder(qp->poly, reordering);
  1954. free(reordering);
  1955. if (!qp->poly || !qp->div)
  1956. goto error;
  1957. isl_poly_free(s);
  1958. return qp;
  1959. error:
  1960. isl_qpolynomial_free(qp);
  1961. isl_poly_free(s);
  1962. return NULL;
  1963. }
  1964. /* Replace all integer divisions [e/d] that turn out to not actually be integer
  1965. * divisions because d is equal to 1 by their definition, i.e., e.
  1966. */
  1967. static __isl_give isl_qpolynomial *substitute_non_divs(
  1968. __isl_take isl_qpolynomial *qp)
  1969. {
  1970. int i, j;
  1971. isl_size div_pos;
  1972. isl_poly *s;
  1973. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  1974. if (div_pos < 0)
  1975. return isl_qpolynomial_free(qp);
  1976. for (i = 0; qp && i < qp->div->n_row; ++i) {
  1977. if (!isl_int_is_one(qp->div->row[i][0]))
  1978. continue;
  1979. for (j = i + 1; j < qp->div->n_row; ++j) {
  1980. if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
  1981. continue;
  1982. isl_seq_combine(qp->div->row[j] + 1,
  1983. qp->div->ctx->one, qp->div->row[j] + 1,
  1984. qp->div->row[j][2 + div_pos + i],
  1985. qp->div->row[i] + 1, 1 + div_pos + i);
  1986. isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0);
  1987. normalize_div(qp, j);
  1988. }
  1989. s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
  1990. qp->div->row[i][0], qp->div->n_col - 1);
  1991. qp = substitute_div(qp, i, s);
  1992. --i;
  1993. }
  1994. return qp;
  1995. }
  1996. /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
  1997. * with d the denominator. When replacing the coefficient e of x by
  1998. * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
  1999. * inside the division, so we need to add floor(e/d) * x outside.
  2000. * That is, we replace q by q' + floor(e/d) * x and we therefore need
  2001. * to adjust the coefficient of x in each later div that depends on the
  2002. * current div "div" and also in the affine expressions in the rows of "mat"
  2003. * (if they too depend on "div").
  2004. */
  2005. static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
  2006. __isl_keep isl_mat **mat)
  2007. {
  2008. int i, j;
  2009. isl_int v;
  2010. unsigned total = qp->div->n_col - qp->div->n_row - 2;
  2011. isl_int_init(v);
  2012. for (i = 0; i < 1 + total + div; ++i) {
  2013. if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
  2014. isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
  2015. continue;
  2016. isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
  2017. isl_int_fdiv_r(qp->div->row[div][1 + i],
  2018. qp->div->row[div][1 + i], qp->div->row[div][0]);
  2019. *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
  2020. for (j = div + 1; j < qp->div->n_row; ++j) {
  2021. if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
  2022. continue;
  2023. isl_int_addmul(qp->div->row[j][1 + i],
  2024. v, qp->div->row[j][2 + total + div]);
  2025. }
  2026. }
  2027. isl_int_clear(v);
  2028. }
  2029. /* Check if the last non-zero coefficient is bigger that half of the
  2030. * denominator. If so, we will invert the div to further reduce the number
  2031. * of distinct divs that may appear.
  2032. * If the last non-zero coefficient is exactly half the denominator,
  2033. * then we continue looking for earlier coefficients that are bigger
  2034. * than half the denominator.
  2035. */
  2036. static int needs_invert(__isl_keep isl_mat *div, int row)
  2037. {
  2038. int i;
  2039. int cmp;
  2040. for (i = div->n_col - 1; i >= 1; --i) {
  2041. if (isl_int_is_zero(div->row[row][i]))
  2042. continue;
  2043. isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
  2044. cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
  2045. isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
  2046. if (cmp)
  2047. return cmp > 0;
  2048. if (i == 1)
  2049. return 1;
  2050. }
  2051. return 0;
  2052. }
  2053. /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
  2054. * We only invert the coefficients of e (and the coefficient of q in
  2055. * later divs and in the rows of "mat"). After calling this function, the
  2056. * coefficients of e should be reduced again.
  2057. */
  2058. static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
  2059. __isl_keep isl_mat **mat)
  2060. {
  2061. unsigned total = qp->div->n_col - qp->div->n_row - 2;
  2062. isl_seq_neg(qp->div->row[div] + 1,
  2063. qp->div->row[div] + 1, qp->div->n_col - 1);
  2064. isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
  2065. isl_int_add(qp->div->row[div][1],
  2066. qp->div->row[div][1], qp->div->row[div][0]);
  2067. *mat = isl_mat_col_neg(*mat, 1 + total + div);
  2068. isl_mat_col_mul(qp->div, 2 + total + div,
  2069. qp->div->ctx->negone, 2 + total + div);
  2070. }
  2071. /* Reduce all divs of "qp" to have coefficients
  2072. * in the interval [0, d-1], with d the denominator and such that the
  2073. * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
  2074. * The modifications to the integer divisions need to be reflected
  2075. * in the factors of the polynomial that refer to the original
  2076. * integer divisions. To this end, the modifications are collected
  2077. * as a set of affine expressions and then plugged into the polynomial.
  2078. *
  2079. * After the reduction, some divs may have become redundant or identical,
  2080. * so we call substitute_non_divs and sort_divs. If these functions
  2081. * eliminate divs or merge two or more divs into one, the coefficients
  2082. * of the enclosing divs may have to be reduced again, so we call
  2083. * ourselves recursively if the number of divs decreases.
  2084. */
  2085. static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
  2086. {
  2087. int i;
  2088. isl_ctx *ctx;
  2089. isl_mat *mat;
  2090. isl_poly **s;
  2091. unsigned o_div;
  2092. isl_size n_div, total, new_n_div;
  2093. total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
  2094. n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
  2095. o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
  2096. if (total < 0 || n_div < 0)
  2097. return isl_qpolynomial_free(qp);
  2098. ctx = isl_qpolynomial_get_ctx(qp);
  2099. mat = isl_mat_zero(ctx, n_div, 1 + total);
  2100. for (i = 0; i < n_div; ++i)
  2101. mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
  2102. for (i = 0; i < qp->div->n_row; ++i) {
  2103. normalize_div(qp, i);
  2104. reduce_div(qp, i, &mat);
  2105. if (needs_invert(qp->div, i)) {
  2106. invert_div(qp, i, &mat);
  2107. reduce_div(qp, i, &mat);
  2108. }
  2109. }
  2110. if (!mat)
  2111. goto error;
  2112. s = isl_alloc_array(ctx, struct isl_poly *, n_div);
  2113. if (n_div && !s)
  2114. goto error;
  2115. for (i = 0; i < n_div; ++i)
  2116. s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
  2117. 1 + total);
  2118. qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
  2119. for (i = 0; i < n_div; ++i)
  2120. isl_poly_free(s[i]);
  2121. free(s);
  2122. if (!qp->poly)
  2123. goto error;
  2124. isl_mat_free(mat);
  2125. qp = substitute_non_divs(qp);
  2126. qp = sort_divs(qp);
  2127. new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
  2128. if (new_n_div < 0)
  2129. return isl_qpolynomial_free(qp);
  2130. if (new_n_div < n_div)
  2131. return reduce_divs(qp);
  2132. return qp;
  2133. error:
  2134. isl_qpolynomial_free(qp);
  2135. isl_mat_free(mat);
  2136. return NULL;
  2137. }
  2138. __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
  2139. __isl_take isl_space *domain, const isl_int n, const isl_int d)
  2140. {
  2141. struct isl_qpolynomial *qp;
  2142. isl_poly_cst *cst;
  2143. qp = isl_qpolynomial_zero_on_domain(domain);
  2144. if (!qp)
  2145. return NULL;
  2146. cst = isl_poly_as_cst(qp->poly);
  2147. isl_int_set(cst->n, n);
  2148. isl_int_set(cst->d, d);
  2149. return qp;
  2150. }
  2151. /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
  2152. */
  2153. __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
  2154. __isl_take isl_space *domain, __isl_take isl_val *val)
  2155. {
  2156. isl_qpolynomial *qp;
  2157. isl_poly_cst *cst;
  2158. qp = isl_qpolynomial_zero_on_domain(domain);
  2159. if (!qp || !val)
  2160. goto error;
  2161. cst = isl_poly_as_cst(qp->poly);
  2162. isl_int_set(cst->n, val->n);
  2163. isl_int_set(cst->d, val->d);
  2164. isl_val_free(val);
  2165. return qp;
  2166. error:
  2167. isl_val_free(val);
  2168. isl_qpolynomial_free(qp);
  2169. return NULL;
  2170. }
  2171. static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
  2172. {
  2173. isl_bool is_cst;
  2174. isl_poly_rec *rec;
  2175. int i;
  2176. is_cst = isl_poly_is_cst(poly);
  2177. if (is_cst < 0)
  2178. return isl_stat_error;
  2179. if (is_cst)
  2180. return isl_stat_ok;
  2181. if (poly->var < d)
  2182. active[poly->var] = 1;
  2183. rec = isl_poly_as_rec(poly);
  2184. for (i = 0; i < rec->n; ++i)
  2185. if (poly_set_active(rec->p[i], active, d) < 0)
  2186. return isl_stat_error;
  2187. return isl_stat_ok;
  2188. }
  2189. static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
  2190. {
  2191. int i, j;
  2192. isl_size d;
  2193. isl_space *space;
  2194. space = isl_qpolynomial_peek_domain_space(qp);
  2195. d = isl_space_dim(space, isl_dim_all);
  2196. if (d < 0 || !active)
  2197. return isl_stat_error;
  2198. for (i = 0; i < d; ++i)
  2199. for (j = 0; j < qp->div->n_row; ++j) {
  2200. if (isl_int_is_zero(qp->div->row[j][2 + i]))
  2201. continue;
  2202. active[i] = 1;
  2203. break;
  2204. }
  2205. return poly_set_active(qp->poly, active, d);
  2206. }
  2207. #undef TYPE
  2208. #define TYPE isl_qpolynomial
  2209. static
  2210. #include "check_type_range_templ.c"
  2211. isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
  2212. enum isl_dim_type type, unsigned first, unsigned n)
  2213. {
  2214. int i;
  2215. int *active = NULL;
  2216. isl_bool involves = isl_bool_false;
  2217. isl_size offset;
  2218. isl_size d;
  2219. isl_space *space;
  2220. if (!qp)
  2221. return isl_bool_error;
  2222. if (n == 0)
  2223. return isl_bool_false;
  2224. if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
  2225. return isl_bool_error;
  2226. isl_assert(qp->dim->ctx, type == isl_dim_param ||
  2227. type == isl_dim_in, return isl_bool_error);
  2228. space = isl_qpolynomial_peek_domain_space(qp);
  2229. d = isl_space_dim(space, isl_dim_all);
  2230. if (d < 0)
  2231. return isl_bool_error;
  2232. active = isl_calloc_array(qp->dim->ctx, int, d);
  2233. if (set_active(qp, active) < 0)
  2234. goto error;
  2235. offset = isl_qpolynomial_domain_var_offset(qp, domain_type(type));
  2236. if (offset < 0)
  2237. goto error;
  2238. first += offset;
  2239. for (i = 0; i < n; ++i)
  2240. if (active[first + i]) {
  2241. involves = isl_bool_true;
  2242. break;
  2243. }
  2244. free(active);
  2245. return involves;
  2246. error:
  2247. free(active);
  2248. return isl_bool_error;
  2249. }
  2250. /* Remove divs that do not appear in the quasi-polynomial, nor in any
  2251. * of the divs that do appear in the quasi-polynomial.
  2252. */
  2253. static __isl_give isl_qpolynomial *remove_redundant_divs(
  2254. __isl_take isl_qpolynomial *qp)
  2255. {
  2256. int i, j;
  2257. isl_size div_pos;
  2258. int len;
  2259. int skip;
  2260. int *active = NULL;
  2261. int *reordering = NULL;
  2262. int redundant = 0;
  2263. int n_div;
  2264. isl_ctx *ctx;
  2265. if (!qp)
  2266. return NULL;
  2267. if (qp->div->n_row == 0)
  2268. return qp;
  2269. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  2270. if (div_pos < 0)
  2271. return isl_qpolynomial_free(qp);
  2272. len = qp->div->n_col - 2;
  2273. ctx = isl_qpolynomial_get_ctx(qp);
  2274. active = isl_calloc_array(ctx, int, len);
  2275. if (!active)
  2276. goto error;
  2277. if (poly_set_active(qp->poly, active, len) < 0)
  2278. goto error;
  2279. for (i = qp->div->n_row - 1; i >= 0; --i) {
  2280. if (!active[div_pos + i]) {
  2281. redundant = 1;
  2282. continue;
  2283. }
  2284. for (j = 0; j < i; ++j) {
  2285. if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j]))
  2286. continue;
  2287. active[div_pos + j] = 1;
  2288. break;
  2289. }
  2290. }
  2291. if (!redundant) {
  2292. free(active);
  2293. return qp;
  2294. }
  2295. reordering = isl_alloc_array(qp->div->ctx, int, len);
  2296. if (!reordering)
  2297. goto error;
  2298. for (i = 0; i < div_pos; ++i)
  2299. reordering[i] = i;
  2300. skip = 0;
  2301. n_div = qp->div->n_row;
  2302. for (i = 0; i < n_div; ++i) {
  2303. if (!active[div_pos + i]) {
  2304. qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
  2305. qp->div = isl_mat_drop_cols(qp->div,
  2306. 2 + div_pos + i - skip, 1);
  2307. skip++;
  2308. }
  2309. reordering[div_pos + i] = div_pos + i - skip;
  2310. }
  2311. qp->poly = reorder(qp->poly, reordering);
  2312. if (!qp->poly || !qp->div)
  2313. goto error;
  2314. free(active);
  2315. free(reordering);
  2316. return qp;
  2317. error:
  2318. free(active);
  2319. free(reordering);
  2320. isl_qpolynomial_free(qp);
  2321. return NULL;
  2322. }
  2323. __isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
  2324. unsigned first, unsigned n)
  2325. {
  2326. int i;
  2327. isl_poly_rec *rec;
  2328. if (!poly)
  2329. return NULL;
  2330. if (n == 0 || poly->var < 0 || poly->var < first)
  2331. return poly;
  2332. if (poly->var < first + n) {
  2333. poly = replace_by_constant_term(poly);
  2334. return isl_poly_drop(poly, first, n);
  2335. }
  2336. poly = isl_poly_cow(poly);
  2337. if (!poly)
  2338. return NULL;
  2339. poly->var -= n;
  2340. rec = isl_poly_as_rec(poly);
  2341. if (!rec)
  2342. goto error;
  2343. for (i = 0; i < rec->n; ++i) {
  2344. rec->p[i] = isl_poly_drop(rec->p[i], first, n);
  2345. if (!rec->p[i])
  2346. goto error;
  2347. }
  2348. return poly;
  2349. error:
  2350. isl_poly_free(poly);
  2351. return NULL;
  2352. }
  2353. __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
  2354. __isl_take isl_qpolynomial *qp,
  2355. enum isl_dim_type type, unsigned pos, const char *s)
  2356. {
  2357. qp = isl_qpolynomial_cow(qp);
  2358. if (!qp)
  2359. return NULL;
  2360. if (type == isl_dim_out)
  2361. isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
  2362. "cannot set name of output/set dimension",
  2363. return isl_qpolynomial_free(qp));
  2364. type = domain_type(type);
  2365. qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
  2366. if (!qp->dim)
  2367. goto error;
  2368. return qp;
  2369. error:
  2370. isl_qpolynomial_free(qp);
  2371. return NULL;
  2372. }
  2373. __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
  2374. __isl_take isl_qpolynomial *qp,
  2375. enum isl_dim_type type, unsigned first, unsigned n)
  2376. {
  2377. isl_size offset;
  2378. if (!qp)
  2379. return NULL;
  2380. if (type == isl_dim_out)
  2381. isl_die(qp->dim->ctx, isl_error_invalid,
  2382. "cannot drop output/set dimension",
  2383. goto error);
  2384. if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
  2385. return isl_qpolynomial_free(qp);
  2386. type = domain_type(type);
  2387. if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
  2388. return qp;
  2389. qp = isl_qpolynomial_cow(qp);
  2390. if (!qp)
  2391. return NULL;
  2392. isl_assert(qp->dim->ctx, type == isl_dim_param ||
  2393. type == isl_dim_set, goto error);
  2394. qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
  2395. if (!qp->dim)
  2396. goto error;
  2397. offset = isl_qpolynomial_domain_var_offset(qp, type);
  2398. if (offset < 0)
  2399. goto error;
  2400. first += offset;
  2401. qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
  2402. if (!qp->div)
  2403. goto error;
  2404. qp->poly = isl_poly_drop(qp->poly, first, n);
  2405. if (!qp->poly)
  2406. goto error;
  2407. return qp;
  2408. error:
  2409. isl_qpolynomial_free(qp);
  2410. return NULL;
  2411. }
  2412. /* Project the domain of the quasi-polynomial onto its parameter space.
  2413. * The quasi-polynomial may not involve any of the domain dimensions.
  2414. */
  2415. __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
  2416. __isl_take isl_qpolynomial *qp)
  2417. {
  2418. isl_space *space;
  2419. isl_size n;
  2420. isl_bool involves;
  2421. n = isl_qpolynomial_dim(qp, isl_dim_in);
  2422. if (n < 0)
  2423. return isl_qpolynomial_free(qp);
  2424. involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
  2425. if (involves < 0)
  2426. return isl_qpolynomial_free(qp);
  2427. if (involves)
  2428. isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
  2429. "polynomial involves some of the domain dimensions",
  2430. return isl_qpolynomial_free(qp));
  2431. qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
  2432. space = isl_qpolynomial_get_domain_space(qp);
  2433. space = isl_space_params(space);
  2434. qp = isl_qpolynomial_reset_domain_space(qp, space);
  2435. return qp;
  2436. }
  2437. static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
  2438. __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
  2439. {
  2440. int i, j, k;
  2441. isl_int denom;
  2442. unsigned total;
  2443. unsigned n_div;
  2444. isl_poly *poly;
  2445. if (!eq)
  2446. goto error;
  2447. if (eq->n_eq == 0) {
  2448. isl_basic_set_free(eq);
  2449. return qp;
  2450. }
  2451. qp = isl_qpolynomial_cow(qp);
  2452. if (!qp)
  2453. goto error;
  2454. qp->div = isl_mat_cow(qp->div);
  2455. if (!qp->div)
  2456. goto error;
  2457. total = isl_basic_set_offset(eq, isl_dim_div);
  2458. n_div = eq->n_div;
  2459. isl_int_init(denom);
  2460. for (i = 0; i < eq->n_eq; ++i) {
  2461. j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
  2462. if (j < 0 || j == 0 || j >= total)
  2463. continue;
  2464. for (k = 0; k < qp->div->n_row; ++k) {
  2465. if (isl_int_is_zero(qp->div->row[k][1 + j]))
  2466. continue;
  2467. isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
  2468. &qp->div->row[k][0]);
  2469. normalize_div(qp, k);
  2470. }
  2471. if (isl_int_is_pos(eq->eq[i][j]))
  2472. isl_seq_neg(eq->eq[i], eq->eq[i], total);
  2473. isl_int_abs(denom, eq->eq[i][j]);
  2474. isl_int_set_si(eq->eq[i][j], 0);
  2475. poly = isl_poly_from_affine(qp->dim->ctx,
  2476. eq->eq[i], denom, total);
  2477. qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
  2478. isl_poly_free(poly);
  2479. }
  2480. isl_int_clear(denom);
  2481. if (!qp->poly)
  2482. goto error;
  2483. isl_basic_set_free(eq);
  2484. qp = substitute_non_divs(qp);
  2485. qp = sort_divs(qp);
  2486. return qp;
  2487. error:
  2488. isl_basic_set_free(eq);
  2489. isl_qpolynomial_free(qp);
  2490. return NULL;
  2491. }
  2492. /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
  2493. */
  2494. __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
  2495. __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
  2496. {
  2497. if (!qp || !eq)
  2498. goto error;
  2499. if (qp->div->n_row > 0)
  2500. eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
  2501. return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
  2502. error:
  2503. isl_basic_set_free(eq);
  2504. isl_qpolynomial_free(qp);
  2505. return NULL;
  2506. }
  2507. /* Look for equalities among the variables shared by context and qp
  2508. * and the integer divisions of qp, if any.
  2509. * The equalities are then used to eliminate variables and/or integer
  2510. * divisions from qp.
  2511. */
  2512. __isl_give isl_qpolynomial *isl_qpolynomial_gist(
  2513. __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
  2514. {
  2515. isl_local_space *ls;
  2516. isl_basic_set *aff;
  2517. ls = isl_qpolynomial_get_domain_local_space(qp);
  2518. context = isl_local_space_lift_set(ls, context);
  2519. aff = isl_set_affine_hull(context);
  2520. return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
  2521. }
  2522. __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
  2523. __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
  2524. {
  2525. isl_space *space = isl_qpolynomial_get_domain_space(qp);
  2526. isl_set *dom_context = isl_set_universe(space);
  2527. dom_context = isl_set_intersect_params(dom_context, context);
  2528. return isl_qpolynomial_gist(qp, dom_context);
  2529. }
  2530. /* Return a zero isl_qpolynomial in the given space.
  2531. *
  2532. * This is a helper function for isl_pw_*_as_* that ensures a uniform
  2533. * interface over all piecewise types.
  2534. */
  2535. static __isl_give isl_qpolynomial *isl_qpolynomial_zero_in_space(
  2536. __isl_take isl_space *space)
  2537. {
  2538. return isl_qpolynomial_zero_on_domain(isl_space_domain(space));
  2539. }
  2540. #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
  2541. #undef PW
  2542. #define PW isl_pw_qpolynomial
  2543. #undef BASE
  2544. #define BASE qpolynomial
  2545. #undef EL_IS_ZERO
  2546. #define EL_IS_ZERO is_zero
  2547. #undef ZERO
  2548. #define ZERO zero
  2549. #undef IS_ZERO
  2550. #define IS_ZERO is_zero
  2551. #undef FIELD
  2552. #define FIELD qp
  2553. #undef DEFAULT_IS_ZERO
  2554. #define DEFAULT_IS_ZERO 1
  2555. #include <isl_pw_templ.c>
  2556. #include <isl_pw_eval.c>
  2557. #include <isl_pw_insert_dims_templ.c>
  2558. #include <isl_pw_lift_templ.c>
  2559. #include <isl_pw_morph_templ.c>
  2560. #include <isl_pw_move_dims_templ.c>
  2561. #include <isl_pw_neg_templ.c>
  2562. #include <isl_pw_opt_templ.c>
  2563. #include <isl_pw_sub_templ.c>
  2564. #undef BASE
  2565. #define BASE pw_qpolynomial
  2566. #include <isl_union_single.c>
  2567. #include <isl_union_eval.c>
  2568. #include <isl_union_neg.c>
  2569. int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
  2570. {
  2571. if (!pwqp)
  2572. return -1;
  2573. if (pwqp->n != -1)
  2574. return 0;
  2575. if (!isl_set_plain_is_universe(pwqp->p[0].set))
  2576. return 0;
  2577. return isl_qpolynomial_is_one(pwqp->p[0].qp);
  2578. }
  2579. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
  2580. __isl_take isl_pw_qpolynomial *pwqp1,
  2581. __isl_take isl_pw_qpolynomial *pwqp2)
  2582. {
  2583. return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
  2584. }
  2585. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
  2586. __isl_take isl_pw_qpolynomial *pwqp1,
  2587. __isl_take isl_pw_qpolynomial *pwqp2)
  2588. {
  2589. int i, j, n;
  2590. struct isl_pw_qpolynomial *res;
  2591. if (!pwqp1 || !pwqp2)
  2592. goto error;
  2593. isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
  2594. goto error);
  2595. if (isl_pw_qpolynomial_is_zero(pwqp1)) {
  2596. isl_pw_qpolynomial_free(pwqp2);
  2597. return pwqp1;
  2598. }
  2599. if (isl_pw_qpolynomial_is_zero(pwqp2)) {
  2600. isl_pw_qpolynomial_free(pwqp1);
  2601. return pwqp2;
  2602. }
  2603. if (isl_pw_qpolynomial_is_one(pwqp1)) {
  2604. isl_pw_qpolynomial_free(pwqp1);
  2605. return pwqp2;
  2606. }
  2607. if (isl_pw_qpolynomial_is_one(pwqp2)) {
  2608. isl_pw_qpolynomial_free(pwqp2);
  2609. return pwqp1;
  2610. }
  2611. n = pwqp1->n * pwqp2->n;
  2612. res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
  2613. for (i = 0; i < pwqp1->n; ++i) {
  2614. for (j = 0; j < pwqp2->n; ++j) {
  2615. struct isl_set *common;
  2616. struct isl_qpolynomial *prod;
  2617. common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
  2618. isl_set_copy(pwqp2->p[j].set));
  2619. if (isl_set_plain_is_empty(common)) {
  2620. isl_set_free(common);
  2621. continue;
  2622. }
  2623. prod = isl_qpolynomial_mul(
  2624. isl_qpolynomial_copy(pwqp1->p[i].qp),
  2625. isl_qpolynomial_copy(pwqp2->p[j].qp));
  2626. res = isl_pw_qpolynomial_add_piece(res, common, prod);
  2627. }
  2628. }
  2629. isl_pw_qpolynomial_free(pwqp1);
  2630. isl_pw_qpolynomial_free(pwqp2);
  2631. return res;
  2632. error:
  2633. isl_pw_qpolynomial_free(pwqp1);
  2634. isl_pw_qpolynomial_free(pwqp2);
  2635. return NULL;
  2636. }
  2637. __isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
  2638. __isl_take isl_vec *vec)
  2639. {
  2640. int i;
  2641. isl_bool is_cst;
  2642. isl_poly_rec *rec;
  2643. isl_val *res;
  2644. isl_val *base;
  2645. is_cst = isl_poly_is_cst(poly);
  2646. if (is_cst < 0)
  2647. goto error;
  2648. if (is_cst) {
  2649. isl_vec_free(vec);
  2650. res = isl_poly_get_constant_val(poly);
  2651. isl_poly_free(poly);
  2652. return res;
  2653. }
  2654. rec = isl_poly_as_rec(poly);
  2655. if (!rec || !vec)
  2656. goto error;
  2657. isl_assert(poly->ctx, rec->n >= 1, goto error);
  2658. base = isl_val_rat_from_isl_int(poly->ctx,
  2659. vec->el[1 + poly->var], vec->el[0]);
  2660. res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
  2661. isl_vec_copy(vec));
  2662. for (i = rec->n - 2; i >= 0; --i) {
  2663. res = isl_val_mul(res, isl_val_copy(base));
  2664. res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
  2665. isl_vec_copy(vec)));
  2666. }
  2667. isl_val_free(base);
  2668. isl_poly_free(poly);
  2669. isl_vec_free(vec);
  2670. return res;
  2671. error:
  2672. isl_poly_free(poly);
  2673. isl_vec_free(vec);
  2674. return NULL;
  2675. }
  2676. /* Evaluate "qp" in the void point "pnt".
  2677. * In particular, return the value NaN.
  2678. */
  2679. static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
  2680. __isl_take isl_point *pnt)
  2681. {
  2682. isl_ctx *ctx;
  2683. ctx = isl_point_get_ctx(pnt);
  2684. isl_qpolynomial_free(qp);
  2685. isl_point_free(pnt);
  2686. return isl_val_nan(ctx);
  2687. }
  2688. __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
  2689. __isl_take isl_point *pnt)
  2690. {
  2691. isl_bool is_void;
  2692. isl_vec *ext;
  2693. isl_val *v;
  2694. if (!qp || !pnt)
  2695. goto error;
  2696. isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
  2697. is_void = isl_point_is_void(pnt);
  2698. if (is_void < 0)
  2699. goto error;
  2700. if (is_void)
  2701. return eval_void(qp, pnt);
  2702. ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
  2703. v = isl_poly_eval(isl_poly_copy(qp->poly), ext);
  2704. isl_qpolynomial_free(qp);
  2705. isl_point_free(pnt);
  2706. return v;
  2707. error:
  2708. isl_qpolynomial_free(qp);
  2709. isl_point_free(pnt);
  2710. return NULL;
  2711. }
  2712. int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
  2713. {
  2714. int cmp;
  2715. isl_int t;
  2716. isl_int_init(t);
  2717. isl_int_mul(t, cst1->n, cst2->d);
  2718. isl_int_submul(t, cst2->n, cst1->d);
  2719. cmp = isl_int_sgn(t);
  2720. isl_int_clear(t);
  2721. return cmp;
  2722. }
  2723. __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
  2724. __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
  2725. unsigned first, unsigned n)
  2726. {
  2727. unsigned total;
  2728. unsigned g_pos;
  2729. int *exp;
  2730. if (!qp)
  2731. return NULL;
  2732. if (type == isl_dim_out)
  2733. isl_die(qp->div->ctx, isl_error_invalid,
  2734. "cannot insert output/set dimensions",
  2735. goto error);
  2736. if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
  2737. return isl_qpolynomial_free(qp);
  2738. type = domain_type(type);
  2739. if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
  2740. return qp;
  2741. qp = isl_qpolynomial_cow(qp);
  2742. if (!qp)
  2743. return NULL;
  2744. g_pos = pos(qp->dim, type) + first;
  2745. qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
  2746. if (!qp->div)
  2747. goto error;
  2748. total = qp->div->n_col - 2;
  2749. if (total > g_pos) {
  2750. int i;
  2751. exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
  2752. if (!exp)
  2753. goto error;
  2754. for (i = 0; i < total - g_pos; ++i)
  2755. exp[i] = i + n;
  2756. qp->poly = expand(qp->poly, exp, g_pos);
  2757. free(exp);
  2758. if (!qp->poly)
  2759. goto error;
  2760. }
  2761. qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
  2762. if (!qp->dim)
  2763. goto error;
  2764. return qp;
  2765. error:
  2766. isl_qpolynomial_free(qp);
  2767. return NULL;
  2768. }
  2769. __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
  2770. __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
  2771. {
  2772. isl_size pos;
  2773. pos = isl_qpolynomial_dim(qp, type);
  2774. if (pos < 0)
  2775. return isl_qpolynomial_free(qp);
  2776. return isl_qpolynomial_insert_dims(qp, type, pos, n);
  2777. }
  2778. static int *reordering_move(isl_ctx *ctx,
  2779. unsigned len, unsigned dst, unsigned src, unsigned n)
  2780. {
  2781. int i;
  2782. int *reordering;
  2783. reordering = isl_alloc_array(ctx, int, len);
  2784. if (!reordering)
  2785. return NULL;
  2786. if (dst <= src) {
  2787. for (i = 0; i < dst; ++i)
  2788. reordering[i] = i;
  2789. for (i = 0; i < n; ++i)
  2790. reordering[src + i] = dst + i;
  2791. for (i = 0; i < src - dst; ++i)
  2792. reordering[dst + i] = dst + n + i;
  2793. for (i = 0; i < len - src - n; ++i)
  2794. reordering[src + n + i] = src + n + i;
  2795. } else {
  2796. for (i = 0; i < src; ++i)
  2797. reordering[i] = i;
  2798. for (i = 0; i < n; ++i)
  2799. reordering[src + i] = dst + i;
  2800. for (i = 0; i < dst - src; ++i)
  2801. reordering[src + n + i] = src + i;
  2802. for (i = 0; i < len - dst - n; ++i)
  2803. reordering[dst + n + i] = dst + n + i;
  2804. }
  2805. return reordering;
  2806. }
  2807. __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
  2808. __isl_take isl_qpolynomial *qp,
  2809. enum isl_dim_type dst_type, unsigned dst_pos,
  2810. enum isl_dim_type src_type, unsigned src_pos, unsigned n)
  2811. {
  2812. unsigned g_dst_pos;
  2813. unsigned g_src_pos;
  2814. int *reordering;
  2815. if (!qp)
  2816. return NULL;
  2817. if (dst_type == isl_dim_out || src_type == isl_dim_out)
  2818. isl_die(qp->dim->ctx, isl_error_invalid,
  2819. "cannot move output/set dimension",
  2820. goto error);
  2821. if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
  2822. return isl_qpolynomial_free(qp);
  2823. if (dst_type == isl_dim_in)
  2824. dst_type = isl_dim_set;
  2825. if (src_type == isl_dim_in)
  2826. src_type = isl_dim_set;
  2827. if (n == 0 &&
  2828. !isl_space_is_named_or_nested(qp->dim, src_type) &&
  2829. !isl_space_is_named_or_nested(qp->dim, dst_type))
  2830. return qp;
  2831. qp = isl_qpolynomial_cow(qp);
  2832. if (!qp)
  2833. return NULL;
  2834. g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
  2835. g_src_pos = pos(qp->dim, src_type) + src_pos;
  2836. if (dst_type > src_type)
  2837. g_dst_pos -= n;
  2838. qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
  2839. if (!qp->div)
  2840. goto error;
  2841. qp = sort_divs(qp);
  2842. if (!qp)
  2843. goto error;
  2844. reordering = reordering_move(qp->dim->ctx,
  2845. qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
  2846. if (!reordering)
  2847. goto error;
  2848. qp->poly = reorder(qp->poly, reordering);
  2849. free(reordering);
  2850. if (!qp->poly)
  2851. goto error;
  2852. qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
  2853. if (!qp->dim)
  2854. goto error;
  2855. return qp;
  2856. error:
  2857. isl_qpolynomial_free(qp);
  2858. return NULL;
  2859. }
  2860. __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
  2861. __isl_take isl_space *space, isl_int *f, isl_int denom)
  2862. {
  2863. isl_size d;
  2864. isl_poly *poly;
  2865. space = isl_space_domain(space);
  2866. if (!space)
  2867. return NULL;
  2868. d = isl_space_dim(space, isl_dim_all);
  2869. poly = d < 0 ? NULL : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
  2870. return isl_qpolynomial_alloc(space, 0, poly);
  2871. }
  2872. __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
  2873. {
  2874. isl_ctx *ctx;
  2875. isl_poly *poly;
  2876. isl_qpolynomial *qp;
  2877. if (!aff)
  2878. return NULL;
  2879. ctx = isl_aff_get_ctx(aff);
  2880. poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
  2881. aff->v->size - 1);
  2882. qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
  2883. aff->ls->div->n_row, poly);
  2884. if (!qp)
  2885. goto error;
  2886. isl_mat_free(qp->div);
  2887. qp->div = isl_mat_copy(aff->ls->div);
  2888. qp->div = isl_mat_cow(qp->div);
  2889. if (!qp->div)
  2890. goto error;
  2891. isl_aff_free(aff);
  2892. qp = reduce_divs(qp);
  2893. qp = remove_redundant_divs(qp);
  2894. return qp;
  2895. error:
  2896. isl_aff_free(aff);
  2897. return isl_qpolynomial_free(qp);
  2898. }
  2899. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
  2900. __isl_take isl_pw_aff *pwaff)
  2901. {
  2902. int i;
  2903. isl_pw_qpolynomial *pwqp;
  2904. if (!pwaff)
  2905. return NULL;
  2906. pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
  2907. pwaff->n);
  2908. for (i = 0; i < pwaff->n; ++i) {
  2909. isl_set *dom;
  2910. isl_qpolynomial *qp;
  2911. dom = isl_set_copy(pwaff->p[i].set);
  2912. qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
  2913. pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
  2914. }
  2915. isl_pw_aff_free(pwaff);
  2916. return pwqp;
  2917. }
  2918. __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
  2919. __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
  2920. {
  2921. isl_aff *aff;
  2922. aff = isl_constraint_get_bound(c, type, pos);
  2923. isl_constraint_free(c);
  2924. return isl_qpolynomial_from_aff(aff);
  2925. }
  2926. /* For each 0 <= i < "n", replace variable "first" + i of type "type"
  2927. * in "qp" by subs[i].
  2928. */
  2929. __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
  2930. __isl_take isl_qpolynomial *qp,
  2931. enum isl_dim_type type, unsigned first, unsigned n,
  2932. __isl_keep isl_qpolynomial **subs)
  2933. {
  2934. int i;
  2935. isl_poly **polys;
  2936. if (n == 0)
  2937. return qp;
  2938. qp = isl_qpolynomial_cow(qp);
  2939. if (!qp)
  2940. return NULL;
  2941. if (type == isl_dim_out)
  2942. isl_die(qp->dim->ctx, isl_error_invalid,
  2943. "cannot substitute output/set dimension",
  2944. goto error);
  2945. if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
  2946. return isl_qpolynomial_free(qp);
  2947. type = domain_type(type);
  2948. for (i = 0; i < n; ++i)
  2949. if (!subs[i])
  2950. goto error;
  2951. for (i = 0; i < n; ++i)
  2952. if (isl_qpolynomial_check_equal_space(qp, subs[i]) < 0)
  2953. goto error;
  2954. isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
  2955. for (i = 0; i < n; ++i)
  2956. isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
  2957. first += pos(qp->dim, type);
  2958. polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
  2959. if (!polys)
  2960. goto error;
  2961. for (i = 0; i < n; ++i)
  2962. polys[i] = subs[i]->poly;
  2963. qp->poly = isl_poly_subs(qp->poly, first, n, polys);
  2964. free(polys);
  2965. if (!qp->poly)
  2966. goto error;
  2967. return qp;
  2968. error:
  2969. isl_qpolynomial_free(qp);
  2970. return NULL;
  2971. }
  2972. /* Extend "bset" with extra set dimensions for each integer division
  2973. * in "qp" and then call "fn" with the extended bset and the polynomial
  2974. * that results from replacing each of the integer divisions by the
  2975. * corresponding extra set dimension.
  2976. */
  2977. isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
  2978. __isl_keep isl_basic_set *bset,
  2979. isl_stat (*fn)(__isl_take isl_basic_set *bset,
  2980. __isl_take isl_qpolynomial *poly, void *user), void *user)
  2981. {
  2982. isl_space *space;
  2983. isl_local_space *ls;
  2984. isl_qpolynomial *poly;
  2985. if (!qp || !bset)
  2986. return isl_stat_error;
  2987. if (qp->div->n_row == 0)
  2988. return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
  2989. user);
  2990. space = isl_space_copy(qp->dim);
  2991. space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
  2992. poly = isl_qpolynomial_alloc(space, 0, isl_poly_copy(qp->poly));
  2993. bset = isl_basic_set_copy(bset);
  2994. ls = isl_qpolynomial_get_domain_local_space(qp);
  2995. bset = isl_local_space_lift_basic_set(ls, bset);
  2996. return fn(bset, poly, user);
  2997. }
  2998. /* Return total degree in variables first (inclusive) up to last (exclusive).
  2999. */
  3000. int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
  3001. {
  3002. int deg = -1;
  3003. int i;
  3004. isl_bool is_zero, is_cst;
  3005. isl_poly_rec *rec;
  3006. is_zero = isl_poly_is_zero(poly);
  3007. if (is_zero < 0)
  3008. return -2;
  3009. if (is_zero)
  3010. return -1;
  3011. is_cst = isl_poly_is_cst(poly);
  3012. if (is_cst < 0)
  3013. return -2;
  3014. if (is_cst || poly->var < first)
  3015. return 0;
  3016. rec = isl_poly_as_rec(poly);
  3017. if (!rec)
  3018. return -2;
  3019. for (i = 0; i < rec->n; ++i) {
  3020. int d;
  3021. is_zero = isl_poly_is_zero(rec->p[i]);
  3022. if (is_zero < 0)
  3023. return -2;
  3024. if (is_zero)
  3025. continue;
  3026. d = isl_poly_degree(rec->p[i], first, last);
  3027. if (poly->var < last)
  3028. d += i;
  3029. if (d > deg)
  3030. deg = d;
  3031. }
  3032. return deg;
  3033. }
  3034. /* Return total degree in set variables.
  3035. */
  3036. int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
  3037. {
  3038. unsigned ovar;
  3039. isl_size nvar;
  3040. if (!poly)
  3041. return -2;
  3042. ovar = isl_space_offset(poly->dim, isl_dim_set);
  3043. nvar = isl_space_dim(poly->dim, isl_dim_set);
  3044. if (nvar < 0)
  3045. return -2;
  3046. return isl_poly_degree(poly->poly, ovar, ovar + nvar);
  3047. }
  3048. __isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
  3049. unsigned pos, int deg)
  3050. {
  3051. int i;
  3052. isl_bool is_cst;
  3053. isl_poly_rec *rec;
  3054. is_cst = isl_poly_is_cst(poly);
  3055. if (is_cst < 0)
  3056. return NULL;
  3057. if (is_cst || poly->var < pos) {
  3058. if (deg == 0)
  3059. return isl_poly_copy(poly);
  3060. else
  3061. return isl_poly_zero(poly->ctx);
  3062. }
  3063. rec = isl_poly_as_rec(poly);
  3064. if (!rec)
  3065. return NULL;
  3066. if (poly->var == pos) {
  3067. if (deg < rec->n)
  3068. return isl_poly_copy(rec->p[deg]);
  3069. else
  3070. return isl_poly_zero(poly->ctx);
  3071. }
  3072. poly = isl_poly_copy(poly);
  3073. poly = isl_poly_cow(poly);
  3074. rec = isl_poly_as_rec(poly);
  3075. if (!rec)
  3076. goto error;
  3077. for (i = 0; i < rec->n; ++i) {
  3078. isl_poly *t;
  3079. t = isl_poly_coeff(rec->p[i], pos, deg);
  3080. if (!t)
  3081. goto error;
  3082. isl_poly_free(rec->p[i]);
  3083. rec->p[i] = t;
  3084. }
  3085. return poly;
  3086. error:
  3087. isl_poly_free(poly);
  3088. return NULL;
  3089. }
  3090. /* Return coefficient of power "deg" of variable "t_pos" of type "type".
  3091. */
  3092. __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
  3093. __isl_keep isl_qpolynomial *qp,
  3094. enum isl_dim_type type, unsigned t_pos, int deg)
  3095. {
  3096. unsigned g_pos;
  3097. isl_poly *poly;
  3098. isl_qpolynomial *c;
  3099. if (!qp)
  3100. return NULL;
  3101. if (type == isl_dim_out)
  3102. isl_die(qp->div->ctx, isl_error_invalid,
  3103. "output/set dimension does not have a coefficient",
  3104. return NULL);
  3105. if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
  3106. return NULL;
  3107. type = domain_type(type);
  3108. g_pos = pos(qp->dim, type) + t_pos;
  3109. poly = isl_poly_coeff(qp->poly, g_pos, deg);
  3110. c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
  3111. qp->div->n_row, poly);
  3112. if (!c)
  3113. return NULL;
  3114. isl_mat_free(c->div);
  3115. c->div = isl_mat_copy(qp->div);
  3116. if (!c->div)
  3117. goto error;
  3118. return c;
  3119. error:
  3120. isl_qpolynomial_free(c);
  3121. return NULL;
  3122. }
  3123. /* Homogenize the polynomial in the variables first (inclusive) up to
  3124. * last (exclusive) by inserting powers of variable first.
  3125. * Variable first is assumed not to appear in the input.
  3126. */
  3127. __isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
  3128. int target, int first, int last)
  3129. {
  3130. int i;
  3131. isl_bool is_zero, is_cst;
  3132. isl_poly_rec *rec;
  3133. is_zero = isl_poly_is_zero(poly);
  3134. if (is_zero < 0)
  3135. return isl_poly_free(poly);
  3136. if (is_zero)
  3137. return poly;
  3138. if (deg == target)
  3139. return poly;
  3140. is_cst = isl_poly_is_cst(poly);
  3141. if (is_cst < 0)
  3142. return isl_poly_free(poly);
  3143. if (is_cst || poly->var < first) {
  3144. isl_poly *hom;
  3145. hom = isl_poly_var_pow(poly->ctx, first, target - deg);
  3146. if (!hom)
  3147. goto error;
  3148. rec = isl_poly_as_rec(hom);
  3149. rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
  3150. return hom;
  3151. }
  3152. poly = isl_poly_cow(poly);
  3153. rec = isl_poly_as_rec(poly);
  3154. if (!rec)
  3155. goto error;
  3156. for (i = 0; i < rec->n; ++i) {
  3157. is_zero = isl_poly_is_zero(rec->p[i]);
  3158. if (is_zero < 0)
  3159. return isl_poly_free(poly);
  3160. if (is_zero)
  3161. continue;
  3162. rec->p[i] = isl_poly_homogenize(rec->p[i],
  3163. poly->var < last ? deg + i : i, target,
  3164. first, last);
  3165. if (!rec->p[i])
  3166. goto error;
  3167. }
  3168. return poly;
  3169. error:
  3170. isl_poly_free(poly);
  3171. return NULL;
  3172. }
  3173. /* Homogenize the polynomial in the set variables by introducing
  3174. * powers of an extra set variable at position 0.
  3175. */
  3176. __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
  3177. __isl_take isl_qpolynomial *poly)
  3178. {
  3179. unsigned ovar;
  3180. isl_size nvar;
  3181. int deg = isl_qpolynomial_degree(poly);
  3182. if (deg < -1)
  3183. goto error;
  3184. poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
  3185. poly = isl_qpolynomial_cow(poly);
  3186. if (!poly)
  3187. goto error;
  3188. ovar = isl_space_offset(poly->dim, isl_dim_set);
  3189. nvar = isl_space_dim(poly->dim, isl_dim_set);
  3190. if (nvar < 0)
  3191. return isl_qpolynomial_free(poly);
  3192. poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
  3193. if (!poly->poly)
  3194. goto error;
  3195. return poly;
  3196. error:
  3197. isl_qpolynomial_free(poly);
  3198. return NULL;
  3199. }
  3200. __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
  3201. __isl_take isl_mat *div)
  3202. {
  3203. isl_term *term;
  3204. isl_size d;
  3205. int n;
  3206. d = isl_space_dim(space, isl_dim_all);
  3207. if (d < 0 || !div)
  3208. goto error;
  3209. n = d + div->n_row;
  3210. term = isl_calloc(space->ctx, struct isl_term,
  3211. sizeof(struct isl_term) + (n - 1) * sizeof(int));
  3212. if (!term)
  3213. goto error;
  3214. term->ref = 1;
  3215. term->dim = space;
  3216. term->div = div;
  3217. isl_int_init(term->n);
  3218. isl_int_init(term->d);
  3219. return term;
  3220. error:
  3221. isl_space_free(space);
  3222. isl_mat_free(div);
  3223. return NULL;
  3224. }
  3225. __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
  3226. {
  3227. if (!term)
  3228. return NULL;
  3229. term->ref++;
  3230. return term;
  3231. }
  3232. __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
  3233. {
  3234. int i;
  3235. isl_term *dup;
  3236. isl_size total;
  3237. total = isl_term_dim(term, isl_dim_all);
  3238. if (total < 0)
  3239. return NULL;
  3240. dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
  3241. if (!dup)
  3242. return NULL;
  3243. isl_int_set(dup->n, term->n);
  3244. isl_int_set(dup->d, term->d);
  3245. for (i = 0; i < total; ++i)
  3246. dup->pow[i] = term->pow[i];
  3247. return dup;
  3248. }
  3249. __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
  3250. {
  3251. if (!term)
  3252. return NULL;
  3253. if (term->ref == 1)
  3254. return term;
  3255. term->ref--;
  3256. return isl_term_dup(term);
  3257. }
  3258. __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
  3259. {
  3260. if (!term)
  3261. return NULL;
  3262. if (--term->ref > 0)
  3263. return NULL;
  3264. isl_space_free(term->dim);
  3265. isl_mat_free(term->div);
  3266. isl_int_clear(term->n);
  3267. isl_int_clear(term->d);
  3268. free(term);
  3269. return NULL;
  3270. }
  3271. isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
  3272. {
  3273. isl_size dim;
  3274. if (!term)
  3275. return isl_size_error;
  3276. switch (type) {
  3277. case isl_dim_param:
  3278. case isl_dim_in:
  3279. case isl_dim_out: return isl_space_dim(term->dim, type);
  3280. case isl_dim_div: return term->div->n_row;
  3281. case isl_dim_all: dim = isl_space_dim(term->dim, isl_dim_all);
  3282. if (dim < 0)
  3283. return isl_size_error;
  3284. return dim + term->div->n_row;
  3285. default: return isl_size_error;
  3286. }
  3287. }
  3288. /* Return the space of "term".
  3289. */
  3290. static __isl_keep isl_space *isl_term_peek_space(__isl_keep isl_term *term)
  3291. {
  3292. return term ? term->dim : NULL;
  3293. }
  3294. /* Return the offset of the first variable of type "type" within
  3295. * the variables of "term".
  3296. */
  3297. static isl_size isl_term_offset(__isl_keep isl_term *term,
  3298. enum isl_dim_type type)
  3299. {
  3300. isl_space *space;
  3301. space = isl_term_peek_space(term);
  3302. if (!space)
  3303. return isl_size_error;
  3304. switch (type) {
  3305. case isl_dim_param:
  3306. case isl_dim_set: return isl_space_offset(space, type);
  3307. case isl_dim_div: return isl_space_dim(space, isl_dim_all);
  3308. default:
  3309. isl_die(isl_term_get_ctx(term), isl_error_invalid,
  3310. "invalid dimension type", return isl_size_error);
  3311. }
  3312. }
  3313. isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
  3314. {
  3315. return term ? term->dim->ctx : NULL;
  3316. }
  3317. void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
  3318. {
  3319. if (!term)
  3320. return;
  3321. isl_int_set(*n, term->n);
  3322. }
  3323. /* Return the coefficient of the term "term".
  3324. */
  3325. __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
  3326. {
  3327. if (!term)
  3328. return NULL;
  3329. return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
  3330. term->n, term->d);
  3331. }
  3332. #undef TYPE
  3333. #define TYPE isl_term
  3334. static
  3335. #include "check_type_range_templ.c"
  3336. isl_size isl_term_get_exp(__isl_keep isl_term *term,
  3337. enum isl_dim_type type, unsigned pos)
  3338. {
  3339. isl_size offset;
  3340. if (isl_term_check_range(term, type, pos, 1) < 0)
  3341. return isl_size_error;
  3342. offset = isl_term_offset(term, type);
  3343. if (offset < 0)
  3344. return isl_size_error;
  3345. return term->pow[offset + pos];
  3346. }
  3347. __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
  3348. {
  3349. isl_local_space *ls;
  3350. isl_aff *aff;
  3351. if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
  3352. return NULL;
  3353. ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
  3354. isl_mat_copy(term->div));
  3355. aff = isl_aff_alloc(ls);
  3356. if (!aff)
  3357. return NULL;
  3358. isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
  3359. aff = isl_aff_normalize(aff);
  3360. return aff;
  3361. }
  3362. __isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
  3363. isl_stat (*fn)(__isl_take isl_term *term, void *user),
  3364. __isl_take isl_term *term, void *user)
  3365. {
  3366. int i;
  3367. isl_bool is_zero, is_bad, is_cst;
  3368. isl_poly_rec *rec;
  3369. is_zero = isl_poly_is_zero(poly);
  3370. if (is_zero < 0 || !term)
  3371. goto error;
  3372. if (is_zero)
  3373. return term;
  3374. is_cst = isl_poly_is_cst(poly);
  3375. is_bad = isl_poly_is_nan(poly);
  3376. if (is_bad >= 0 && !is_bad)
  3377. is_bad = isl_poly_is_infty(poly);
  3378. if (is_bad >= 0 && !is_bad)
  3379. is_bad = isl_poly_is_neginfty(poly);
  3380. if (is_cst < 0 || is_bad < 0)
  3381. return isl_term_free(term);
  3382. if (is_bad)
  3383. isl_die(isl_term_get_ctx(term), isl_error_invalid,
  3384. "cannot handle NaN/infty polynomial",
  3385. return isl_term_free(term));
  3386. if (is_cst) {
  3387. isl_poly_cst *cst;
  3388. cst = isl_poly_as_cst(poly);
  3389. if (!cst)
  3390. goto error;
  3391. term = isl_term_cow(term);
  3392. if (!term)
  3393. goto error;
  3394. isl_int_set(term->n, cst->n);
  3395. isl_int_set(term->d, cst->d);
  3396. if (fn(isl_term_copy(term), user) < 0)
  3397. goto error;
  3398. return term;
  3399. }
  3400. rec = isl_poly_as_rec(poly);
  3401. if (!rec)
  3402. goto error;
  3403. for (i = 0; i < rec->n; ++i) {
  3404. term = isl_term_cow(term);
  3405. if (!term)
  3406. goto error;
  3407. term->pow[poly->var] = i;
  3408. term = isl_poly_foreach_term(rec->p[i], fn, term, user);
  3409. if (!term)
  3410. goto error;
  3411. }
  3412. term = isl_term_cow(term);
  3413. if (!term)
  3414. return NULL;
  3415. term->pow[poly->var] = 0;
  3416. return term;
  3417. error:
  3418. isl_term_free(term);
  3419. return NULL;
  3420. }
  3421. isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
  3422. isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
  3423. {
  3424. isl_term *term;
  3425. if (!qp)
  3426. return isl_stat_error;
  3427. term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
  3428. if (!term)
  3429. return isl_stat_error;
  3430. term = isl_poly_foreach_term(qp->poly, fn, term, user);
  3431. isl_term_free(term);
  3432. return term ? isl_stat_ok : isl_stat_error;
  3433. }
  3434. __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
  3435. {
  3436. isl_poly *poly;
  3437. isl_qpolynomial *qp;
  3438. int i;
  3439. isl_size n;
  3440. n = isl_term_dim(term, isl_dim_all);
  3441. if (n < 0)
  3442. term = isl_term_free(term);
  3443. if (!term)
  3444. return NULL;
  3445. poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
  3446. for (i = 0; i < n; ++i) {
  3447. if (!term->pow[i])
  3448. continue;
  3449. poly = isl_poly_mul(poly,
  3450. isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
  3451. }
  3452. qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
  3453. term->div->n_row, poly);
  3454. if (!qp)
  3455. goto error;
  3456. isl_mat_free(qp->div);
  3457. qp->div = isl_mat_copy(term->div);
  3458. if (!qp->div)
  3459. goto error;
  3460. isl_term_free(term);
  3461. return qp;
  3462. error:
  3463. isl_qpolynomial_free(qp);
  3464. isl_term_free(term);
  3465. return NULL;
  3466. }
  3467. __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
  3468. __isl_take isl_space *space)
  3469. {
  3470. int i;
  3471. int extra;
  3472. isl_size total, d_set, d_qp;
  3473. if (!qp || !space)
  3474. goto error;
  3475. if (isl_space_is_equal(qp->dim, space)) {
  3476. isl_space_free(space);
  3477. return qp;
  3478. }
  3479. qp = isl_qpolynomial_cow(qp);
  3480. if (!qp)
  3481. goto error;
  3482. d_set = isl_space_dim(space, isl_dim_set);
  3483. d_qp = isl_qpolynomial_domain_dim(qp, isl_dim_set);
  3484. extra = d_set - d_qp;
  3485. total = isl_space_dim(qp->dim, isl_dim_all);
  3486. if (d_set < 0 || d_qp < 0 || total < 0)
  3487. goto error;
  3488. if (qp->div->n_row) {
  3489. int *exp;
  3490. exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
  3491. if (!exp)
  3492. goto error;
  3493. for (i = 0; i < qp->div->n_row; ++i)
  3494. exp[i] = extra + i;
  3495. qp->poly = expand(qp->poly, exp, total);
  3496. free(exp);
  3497. if (!qp->poly)
  3498. goto error;
  3499. }
  3500. qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
  3501. if (!qp->div)
  3502. goto error;
  3503. for (i = 0; i < qp->div->n_row; ++i)
  3504. isl_seq_clr(qp->div->row[i] + 2 + total, extra);
  3505. isl_space_free(qp->dim);
  3506. qp->dim = space;
  3507. return qp;
  3508. error:
  3509. isl_space_free(space);
  3510. isl_qpolynomial_free(qp);
  3511. return NULL;
  3512. }
  3513. /* For each parameter or variable that does not appear in qp,
  3514. * first eliminate the variable from all constraints and then set it to zero.
  3515. */
  3516. static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
  3517. __isl_keep isl_qpolynomial *qp)
  3518. {
  3519. int *active = NULL;
  3520. int i;
  3521. isl_size d;
  3522. isl_size nparam;
  3523. isl_size nvar;
  3524. d = isl_set_dim(set, isl_dim_all);
  3525. if (d < 0 || !qp)
  3526. goto error;
  3527. active = isl_calloc_array(set->ctx, int, d);
  3528. if (set_active(qp, active) < 0)
  3529. goto error;
  3530. for (i = 0; i < d; ++i)
  3531. if (!active[i])
  3532. break;
  3533. if (i == d) {
  3534. free(active);
  3535. return set;
  3536. }
  3537. nparam = isl_set_dim(set, isl_dim_param);
  3538. nvar = isl_set_dim(set, isl_dim_set);
  3539. if (nparam < 0 || nvar < 0)
  3540. goto error;
  3541. for (i = 0; i < nparam; ++i) {
  3542. if (active[i])
  3543. continue;
  3544. set = isl_set_eliminate(set, isl_dim_param, i, 1);
  3545. set = isl_set_fix_si(set, isl_dim_param, i, 0);
  3546. }
  3547. for (i = 0; i < nvar; ++i) {
  3548. if (active[nparam + i])
  3549. continue;
  3550. set = isl_set_eliminate(set, isl_dim_set, i, 1);
  3551. set = isl_set_fix_si(set, isl_dim_set, i, 0);
  3552. }
  3553. free(active);
  3554. return set;
  3555. error:
  3556. free(active);
  3557. isl_set_free(set);
  3558. return NULL;
  3559. }
  3560. struct isl_opt_data {
  3561. isl_qpolynomial *qp;
  3562. int first;
  3563. isl_val *opt;
  3564. int max;
  3565. };
  3566. static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
  3567. {
  3568. struct isl_opt_data *data = (struct isl_opt_data *)user;
  3569. isl_val *val;
  3570. val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
  3571. if (data->first) {
  3572. data->first = 0;
  3573. data->opt = val;
  3574. } else if (data->max) {
  3575. data->opt = isl_val_max(data->opt, val);
  3576. } else {
  3577. data->opt = isl_val_min(data->opt, val);
  3578. }
  3579. return isl_stat_ok;
  3580. }
  3581. __isl_give isl_val *isl_qpolynomial_opt_on_domain(
  3582. __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
  3583. {
  3584. struct isl_opt_data data = { NULL, 1, NULL, max };
  3585. isl_bool is_cst;
  3586. if (!set || !qp)
  3587. goto error;
  3588. is_cst = isl_poly_is_cst(qp->poly);
  3589. if (is_cst < 0)
  3590. goto error;
  3591. if (is_cst) {
  3592. isl_set_free(set);
  3593. data.opt = isl_qpolynomial_get_constant_val(qp);
  3594. isl_qpolynomial_free(qp);
  3595. return data.opt;
  3596. }
  3597. set = fix_inactive(set, qp);
  3598. data.qp = qp;
  3599. if (isl_set_foreach_point(set, opt_fn, &data) < 0)
  3600. goto error;
  3601. if (data.first)
  3602. data.opt = isl_val_zero(isl_set_get_ctx(set));
  3603. isl_set_free(set);
  3604. isl_qpolynomial_free(qp);
  3605. return data.opt;
  3606. error:
  3607. isl_set_free(set);
  3608. isl_qpolynomial_free(qp);
  3609. isl_val_free(data.opt);
  3610. return NULL;
  3611. }
  3612. __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
  3613. __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
  3614. {
  3615. int i;
  3616. int n_sub;
  3617. isl_ctx *ctx;
  3618. isl_space *space;
  3619. isl_poly **subs;
  3620. isl_mat *mat, *diag;
  3621. qp = isl_qpolynomial_cow(qp);
  3622. space = isl_qpolynomial_peek_domain_space(qp);
  3623. if (isl_morph_check_applies(morph, space) < 0)
  3624. goto error;
  3625. ctx = isl_qpolynomial_get_ctx(qp);
  3626. n_sub = morph->inv->n_row - 1;
  3627. if (morph->inv->n_row != morph->inv->n_col)
  3628. n_sub += qp->div->n_row;
  3629. subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
  3630. if (n_sub && !subs)
  3631. goto error;
  3632. for (i = 0; 1 + i < morph->inv->n_row; ++i)
  3633. subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
  3634. morph->inv->row[0][0], morph->inv->n_col);
  3635. if (morph->inv->n_row != morph->inv->n_col)
  3636. for (i = 0; i < qp->div->n_row; ++i)
  3637. subs[morph->inv->n_row - 1 + i] =
  3638. isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
  3639. qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
  3640. for (i = 0; i < n_sub; ++i)
  3641. isl_poly_free(subs[i]);
  3642. free(subs);
  3643. diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
  3644. mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
  3645. diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
  3646. mat = isl_mat_diagonal(mat, diag);
  3647. qp->div = isl_mat_product(qp->div, mat);
  3648. isl_space_free(qp->dim);
  3649. qp->dim = isl_space_copy(morph->ran->dim);
  3650. if (!qp->poly || !qp->div || !qp->dim)
  3651. goto error;
  3652. isl_morph_free(morph);
  3653. return qp;
  3654. error:
  3655. isl_qpolynomial_free(qp);
  3656. isl_morph_free(morph);
  3657. return NULL;
  3658. }
  3659. __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
  3660. __isl_take isl_union_pw_qpolynomial *upwqp1,
  3661. __isl_take isl_union_pw_qpolynomial *upwqp2)
  3662. {
  3663. return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
  3664. &isl_pw_qpolynomial_mul);
  3665. }
  3666. /* Reorder the dimension of "qp" according to the given reordering.
  3667. */
  3668. __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
  3669. __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
  3670. {
  3671. isl_space *space;
  3672. qp = isl_qpolynomial_cow(qp);
  3673. if (!qp)
  3674. goto error;
  3675. r = isl_reordering_extend(r, qp->div->n_row);
  3676. if (!r)
  3677. goto error;
  3678. qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
  3679. if (!qp->div)
  3680. goto error;
  3681. qp->poly = reorder(qp->poly, r->pos);
  3682. if (!qp->poly)
  3683. goto error;
  3684. space = isl_reordering_get_space(r);
  3685. qp = isl_qpolynomial_reset_domain_space(qp, space);
  3686. isl_reordering_free(r);
  3687. return qp;
  3688. error:
  3689. isl_qpolynomial_free(qp);
  3690. isl_reordering_free(r);
  3691. return NULL;
  3692. }
  3693. __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
  3694. __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
  3695. {
  3696. isl_bool equal_params;
  3697. if (!qp || !model)
  3698. goto error;
  3699. equal_params = isl_space_has_equal_params(qp->dim, model);
  3700. if (equal_params < 0)
  3701. goto error;
  3702. if (!equal_params) {
  3703. isl_reordering *exp;
  3704. exp = isl_parameter_alignment_reordering(qp->dim, model);
  3705. exp = isl_reordering_extend_space(exp,
  3706. isl_qpolynomial_get_domain_space(qp));
  3707. qp = isl_qpolynomial_realign_domain(qp, exp);
  3708. }
  3709. isl_space_free(model);
  3710. return qp;
  3711. error:
  3712. isl_space_free(model);
  3713. isl_qpolynomial_free(qp);
  3714. return NULL;
  3715. }
  3716. struct isl_split_periods_data {
  3717. int max_periods;
  3718. isl_pw_qpolynomial *res;
  3719. };
  3720. /* Create a slice where the integer division "div" has the fixed value "v".
  3721. * In particular, if "div" refers to floor(f/m), then create a slice
  3722. *
  3723. * m v <= f <= m v + (m - 1)
  3724. *
  3725. * or
  3726. *
  3727. * f - m v >= 0
  3728. * -f + m v + (m - 1) >= 0
  3729. */
  3730. static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
  3731. __isl_keep isl_qpolynomial *qp, int div, isl_int v)
  3732. {
  3733. isl_size total;
  3734. isl_basic_set *bset = NULL;
  3735. int k;
  3736. total = isl_space_dim(space, isl_dim_all);
  3737. if (total < 0 || !qp)
  3738. goto error;
  3739. bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
  3740. k = isl_basic_set_alloc_inequality(bset);
  3741. if (k < 0)
  3742. goto error;
  3743. isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
  3744. isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
  3745. k = isl_basic_set_alloc_inequality(bset);
  3746. if (k < 0)
  3747. goto error;
  3748. isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
  3749. isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
  3750. isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
  3751. isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
  3752. isl_space_free(space);
  3753. return isl_set_from_basic_set(bset);
  3754. error:
  3755. isl_basic_set_free(bset);
  3756. isl_space_free(space);
  3757. return NULL;
  3758. }
  3759. static isl_stat split_periods(__isl_take isl_set *set,
  3760. __isl_take isl_qpolynomial *qp, void *user);
  3761. /* Create a slice of the domain "set" such that integer division "div"
  3762. * has the fixed value "v" and add the results to data->res,
  3763. * replacing the integer division by "v" in "qp".
  3764. */
  3765. static isl_stat set_div(__isl_take isl_set *set,
  3766. __isl_take isl_qpolynomial *qp, int div, isl_int v,
  3767. struct isl_split_periods_data *data)
  3768. {
  3769. int i;
  3770. isl_size div_pos;
  3771. isl_set *slice;
  3772. isl_poly *cst;
  3773. slice = set_div_slice(isl_set_get_space(set), qp, div, v);
  3774. set = isl_set_intersect(set, slice);
  3775. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  3776. if (div_pos < 0)
  3777. goto error;
  3778. for (i = div + 1; i < qp->div->n_row; ++i) {
  3779. if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div]))
  3780. continue;
  3781. isl_int_addmul(qp->div->row[i][1],
  3782. qp->div->row[i][2 + div_pos + div], v);
  3783. isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0);
  3784. }
  3785. cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
  3786. qp = substitute_div(qp, div, cst);
  3787. return split_periods(set, qp, data);
  3788. error:
  3789. isl_set_free(set);
  3790. isl_qpolynomial_free(qp);
  3791. return isl_stat_error;
  3792. }
  3793. /* Split the domain "set" such that integer division "div"
  3794. * has a fixed value (ranging from "min" to "max") on each slice
  3795. * and add the results to data->res.
  3796. */
  3797. static isl_stat split_div(__isl_take isl_set *set,
  3798. __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
  3799. struct isl_split_periods_data *data)
  3800. {
  3801. for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
  3802. isl_set *set_i = isl_set_copy(set);
  3803. isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
  3804. if (set_div(set_i, qp_i, div, min, data) < 0)
  3805. goto error;
  3806. }
  3807. isl_set_free(set);
  3808. isl_qpolynomial_free(qp);
  3809. return isl_stat_ok;
  3810. error:
  3811. isl_set_free(set);
  3812. isl_qpolynomial_free(qp);
  3813. return isl_stat_error;
  3814. }
  3815. /* If "qp" refers to any integer division
  3816. * that can only attain "max_periods" distinct values on "set"
  3817. * then split the domain along those distinct values.
  3818. * Add the results (or the original if no splitting occurs)
  3819. * to data->res.
  3820. */
  3821. static isl_stat split_periods(__isl_take isl_set *set,
  3822. __isl_take isl_qpolynomial *qp, void *user)
  3823. {
  3824. int i;
  3825. isl_pw_qpolynomial *pwqp;
  3826. struct isl_split_periods_data *data;
  3827. isl_int min, max;
  3828. isl_size div_pos;
  3829. isl_stat r = isl_stat_ok;
  3830. data = (struct isl_split_periods_data *)user;
  3831. if (!set || !qp)
  3832. goto error;
  3833. if (qp->div->n_row == 0) {
  3834. pwqp = isl_pw_qpolynomial_alloc(set, qp);
  3835. data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
  3836. return isl_stat_ok;
  3837. }
  3838. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  3839. if (div_pos < 0)
  3840. goto error;
  3841. isl_int_init(min);
  3842. isl_int_init(max);
  3843. for (i = 0; i < qp->div->n_row; ++i) {
  3844. enum isl_lp_result lp_res;
  3845. if (isl_seq_first_non_zero(qp->div->row[i] + 2 + div_pos,
  3846. qp->div->n_row) != -1)
  3847. continue;
  3848. lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
  3849. set->ctx->one, &min, NULL, NULL);
  3850. if (lp_res == isl_lp_error)
  3851. goto error2;
  3852. if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
  3853. continue;
  3854. isl_int_fdiv_q(min, min, qp->div->row[i][0]);
  3855. lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
  3856. set->ctx->one, &max, NULL, NULL);
  3857. if (lp_res == isl_lp_error)
  3858. goto error2;
  3859. if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
  3860. continue;
  3861. isl_int_fdiv_q(max, max, qp->div->row[i][0]);
  3862. isl_int_sub(max, max, min);
  3863. if (isl_int_cmp_si(max, data->max_periods) < 0) {
  3864. isl_int_add(max, max, min);
  3865. break;
  3866. }
  3867. }
  3868. if (i < qp->div->n_row) {
  3869. r = split_div(set, qp, i, min, max, data);
  3870. } else {
  3871. pwqp = isl_pw_qpolynomial_alloc(set, qp);
  3872. data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
  3873. }
  3874. isl_int_clear(max);
  3875. isl_int_clear(min);
  3876. return r;
  3877. error2:
  3878. isl_int_clear(max);
  3879. isl_int_clear(min);
  3880. error:
  3881. isl_set_free(set);
  3882. isl_qpolynomial_free(qp);
  3883. return isl_stat_error;
  3884. }
  3885. /* If any quasi-polynomial in pwqp refers to any integer division
  3886. * that can only attain "max_periods" distinct values on its domain
  3887. * then split the domain along those distinct values.
  3888. */
  3889. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
  3890. __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
  3891. {
  3892. struct isl_split_periods_data data;
  3893. data.max_periods = max_periods;
  3894. data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
  3895. if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
  3896. goto error;
  3897. isl_pw_qpolynomial_free(pwqp);
  3898. return data.res;
  3899. error:
  3900. isl_pw_qpolynomial_free(data.res);
  3901. isl_pw_qpolynomial_free(pwqp);
  3902. return NULL;
  3903. }
  3904. /* Construct a piecewise quasipolynomial that is constant on the given
  3905. * domain. In particular, it is
  3906. * 0 if cst == 0
  3907. * 1 if cst == 1
  3908. * infinity if cst == -1
  3909. *
  3910. * If cst == -1, then explicitly check whether the domain is empty and,
  3911. * if so, return 0 instead.
  3912. */
  3913. static __isl_give isl_pw_qpolynomial *constant_on_domain(
  3914. __isl_take isl_basic_set *bset, int cst)
  3915. {
  3916. isl_space *space;
  3917. isl_qpolynomial *qp;
  3918. if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
  3919. cst = 0;
  3920. if (!bset)
  3921. return NULL;
  3922. bset = isl_basic_set_params(bset);
  3923. space = isl_basic_set_get_space(bset);
  3924. if (cst < 0)
  3925. qp = isl_qpolynomial_infty_on_domain(space);
  3926. else if (cst == 0)
  3927. qp = isl_qpolynomial_zero_on_domain(space);
  3928. else
  3929. qp = isl_qpolynomial_one_on_domain(space);
  3930. return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
  3931. }
  3932. /* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
  3933. * "fn" is the function that is called on each factor.
  3934. * "pwpq" collects the results.
  3935. */
  3936. struct isl_multiplicative_call_data_pw_qpolynomial {
  3937. __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset);
  3938. isl_pw_qpolynomial *pwqp;
  3939. };
  3940. /* Call "fn" on "bset" and return the result,
  3941. * but first check if "bset" has any redundant constraints or
  3942. * implicit equality constraints.
  3943. * If so, there may be further opportunities for detecting factors or
  3944. * removing equality constraints, so recursively call
  3945. * the top-level isl_basic_set_multiplicative_call.
  3946. */
  3947. static __isl_give isl_pw_qpolynomial *multiplicative_call_base(
  3948. __isl_take isl_basic_set *bset,
  3949. __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
  3950. {
  3951. isl_size n1, n2, n_eq;
  3952. n1 = isl_basic_set_n_constraint(bset);
  3953. if (n1 < 0)
  3954. bset = isl_basic_set_free(bset);
  3955. bset = isl_basic_set_remove_redundancies(bset);
  3956. bset = isl_basic_set_detect_equalities(bset);
  3957. n2 = isl_basic_set_n_constraint(bset);
  3958. n_eq = isl_basic_set_n_equality(bset);
  3959. if (n2 < 0 || n_eq < 0)
  3960. bset = isl_basic_set_free(bset);
  3961. else if (n2 < n1 || n_eq > 0)
  3962. return isl_basic_set_multiplicative_call(bset, fn);
  3963. return fn(bset);
  3964. }
  3965. /* isl_factorizer_every_factor_basic_set callback that applies
  3966. * data->fn to the factor "bset" and multiplies in the result
  3967. * in data->pwqp.
  3968. */
  3969. static isl_bool multiplicative_call_factor_pw_qpolynomial(
  3970. __isl_keep isl_basic_set *bset, void *user)
  3971. {
  3972. struct isl_multiplicative_call_data_pw_qpolynomial *data = user;
  3973. isl_pw_qpolynomial *res;
  3974. bset = isl_basic_set_copy(bset);
  3975. res = multiplicative_call_base(bset, data->fn);
  3976. data->pwqp = isl_pw_qpolynomial_mul(data->pwqp, res);
  3977. if (!data->pwqp)
  3978. return isl_bool_error;
  3979. return isl_bool_true;
  3980. }
  3981. /* Factor bset, call fn on each of the factors and return the product.
  3982. *
  3983. * If no factors can be found, simply call fn on the input.
  3984. * Otherwise, construct the factors based on the factorizer,
  3985. * call fn on each factor and compute the product.
  3986. */
  3987. static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
  3988. __isl_take isl_basic_set *bset,
  3989. __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
  3990. {
  3991. struct isl_multiplicative_call_data_pw_qpolynomial data = { fn };
  3992. isl_space *space;
  3993. isl_set *set;
  3994. isl_factorizer *f;
  3995. isl_qpolynomial *qp;
  3996. isl_bool every;
  3997. f = isl_basic_set_factorizer(bset);
  3998. if (!f)
  3999. goto error;
  4000. if (f->n_group == 0) {
  4001. isl_factorizer_free(f);
  4002. return multiplicative_call_base(bset, fn);
  4003. }
  4004. space = isl_basic_set_get_space(bset);
  4005. space = isl_space_params(space);
  4006. set = isl_set_universe(isl_space_copy(space));
  4007. qp = isl_qpolynomial_one_on_domain(space);
  4008. data.pwqp = isl_pw_qpolynomial_alloc(set, qp);
  4009. every = isl_factorizer_every_factor_basic_set(f,
  4010. &multiplicative_call_factor_pw_qpolynomial, &data);
  4011. if (every < 0)
  4012. data.pwqp = isl_pw_qpolynomial_free(data.pwqp);
  4013. isl_basic_set_free(bset);
  4014. isl_factorizer_free(f);
  4015. return data.pwqp;
  4016. error:
  4017. isl_basic_set_free(bset);
  4018. return NULL;
  4019. }
  4020. /* Factor bset, call fn on each of the factors and return the product.
  4021. * The function is assumed to evaluate to zero on empty domains,
  4022. * to one on zero-dimensional domains and to infinity on unbounded domains
  4023. * and will not be called explicitly on zero-dimensional or unbounded domains.
  4024. *
  4025. * We first check for some special cases and remove all equalities.
  4026. * Then we hand over control to compressed_multiplicative_call.
  4027. */
  4028. __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
  4029. __isl_take isl_basic_set *bset,
  4030. __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
  4031. {
  4032. isl_bool bounded;
  4033. isl_size dim;
  4034. isl_morph *morph;
  4035. isl_pw_qpolynomial *pwqp;
  4036. if (!bset)
  4037. return NULL;
  4038. if (isl_basic_set_plain_is_empty(bset))
  4039. return constant_on_domain(bset, 0);
  4040. dim = isl_basic_set_dim(bset, isl_dim_set);
  4041. if (dim < 0)
  4042. goto error;
  4043. if (dim == 0)
  4044. return constant_on_domain(bset, 1);
  4045. bounded = isl_basic_set_is_bounded(bset);
  4046. if (bounded < 0)
  4047. goto error;
  4048. if (!bounded)
  4049. return constant_on_domain(bset, -1);
  4050. if (bset->n_eq == 0)
  4051. return compressed_multiplicative_call(bset, fn);
  4052. morph = isl_basic_set_full_compression(bset);
  4053. bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
  4054. pwqp = compressed_multiplicative_call(bset, fn);
  4055. morph = isl_morph_dom_params(morph);
  4056. morph = isl_morph_ran_params(morph);
  4057. morph = isl_morph_inverse(morph);
  4058. pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
  4059. return pwqp;
  4060. error:
  4061. isl_basic_set_free(bset);
  4062. return NULL;
  4063. }
  4064. /* Drop all floors in "qp", turning each integer division [a/m] into
  4065. * a rational division a/m. If "down" is set, then the integer division
  4066. * is replaced by (a-(m-1))/m instead.
  4067. */
  4068. static __isl_give isl_qpolynomial *qp_drop_floors(
  4069. __isl_take isl_qpolynomial *qp, int down)
  4070. {
  4071. int i;
  4072. isl_poly *s;
  4073. if (!qp)
  4074. return NULL;
  4075. if (qp->div->n_row == 0)
  4076. return qp;
  4077. qp = isl_qpolynomial_cow(qp);
  4078. if (!qp)
  4079. return NULL;
  4080. for (i = qp->div->n_row - 1; i >= 0; --i) {
  4081. if (down) {
  4082. isl_int_sub(qp->div->row[i][1],
  4083. qp->div->row[i][1], qp->div->row[i][0]);
  4084. isl_int_add_ui(qp->div->row[i][1],
  4085. qp->div->row[i][1], 1);
  4086. }
  4087. s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
  4088. qp->div->row[i][0], qp->div->n_col - 1);
  4089. qp = substitute_div(qp, i, s);
  4090. if (!qp)
  4091. return NULL;
  4092. }
  4093. return qp;
  4094. }
  4095. /* Drop all floors in "pwqp", turning each integer division [a/m] into
  4096. * a rational division a/m.
  4097. */
  4098. static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
  4099. __isl_take isl_pw_qpolynomial *pwqp)
  4100. {
  4101. int i;
  4102. if (!pwqp)
  4103. return NULL;
  4104. if (isl_pw_qpolynomial_is_zero(pwqp))
  4105. return pwqp;
  4106. pwqp = isl_pw_qpolynomial_cow(pwqp);
  4107. if (!pwqp)
  4108. return NULL;
  4109. for (i = 0; i < pwqp->n; ++i) {
  4110. pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
  4111. if (!pwqp->p[i].qp)
  4112. goto error;
  4113. }
  4114. return pwqp;
  4115. error:
  4116. isl_pw_qpolynomial_free(pwqp);
  4117. return NULL;
  4118. }
  4119. /* Adjust all the integer divisions in "qp" such that they are at least
  4120. * one over the given orthant (identified by "signs"). This ensures
  4121. * that they will still be non-negative even after subtracting (m-1)/m.
  4122. *
  4123. * In particular, f is replaced by f' + v, changing f = [a/m]
  4124. * to f' = [(a - m v)/m].
  4125. * If the constant term k in a is smaller than m,
  4126. * the constant term of v is set to floor(k/m) - 1.
  4127. * For any other term, if the coefficient c and the variable x have
  4128. * the same sign, then no changes are needed.
  4129. * Otherwise, if the variable is positive (and c is negative),
  4130. * then the coefficient of x in v is set to floor(c/m).
  4131. * If the variable is negative (and c is positive),
  4132. * then the coefficient of x in v is set to ceil(c/m).
  4133. */
  4134. static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
  4135. int *signs)
  4136. {
  4137. int i, j;
  4138. isl_size div_pos;
  4139. isl_vec *v = NULL;
  4140. isl_poly *s;
  4141. qp = isl_qpolynomial_cow(qp);
  4142. div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
  4143. if (div_pos < 0)
  4144. return isl_qpolynomial_free(qp);
  4145. qp->div = isl_mat_cow(qp->div);
  4146. if (!qp->div)
  4147. goto error;
  4148. v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
  4149. for (i = 0; i < qp->div->n_row; ++i) {
  4150. isl_int *row = qp->div->row[i];
  4151. v = isl_vec_clr(v);
  4152. if (!v)
  4153. goto error;
  4154. if (isl_int_lt(row[1], row[0])) {
  4155. isl_int_fdiv_q(v->el[0], row[1], row[0]);
  4156. isl_int_sub_ui(v->el[0], v->el[0], 1);
  4157. isl_int_submul(row[1], row[0], v->el[0]);
  4158. }
  4159. for (j = 0; j < div_pos; ++j) {
  4160. if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
  4161. continue;
  4162. if (signs[j] < 0)
  4163. isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
  4164. else
  4165. isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
  4166. isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
  4167. }
  4168. for (j = 0; j < i; ++j) {
  4169. if (isl_int_sgn(row[2 + div_pos + j]) >= 0)
  4170. continue;
  4171. isl_int_fdiv_q(v->el[1 + div_pos + j],
  4172. row[2 + div_pos + j], row[0]);
  4173. isl_int_submul(row[2 + div_pos + j],
  4174. row[0], v->el[1 + div_pos + j]);
  4175. }
  4176. for (j = i + 1; j < qp->div->n_row; ++j) {
  4177. if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
  4178. continue;
  4179. isl_seq_combine(qp->div->row[j] + 1,
  4180. qp->div->ctx->one, qp->div->row[j] + 1,
  4181. qp->div->row[j][2 + div_pos + i], v->el,
  4182. v->size);
  4183. }
  4184. isl_int_set_si(v->el[1 + div_pos + i], 1);
  4185. s = isl_poly_from_affine(qp->dim->ctx, v->el,
  4186. qp->div->ctx->one, v->size);
  4187. qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
  4188. isl_poly_free(s);
  4189. if (!qp->poly)
  4190. goto error;
  4191. }
  4192. isl_vec_free(v);
  4193. return qp;
  4194. error:
  4195. isl_vec_free(v);
  4196. isl_qpolynomial_free(qp);
  4197. return NULL;
  4198. }
  4199. struct isl_to_poly_data {
  4200. int sign;
  4201. isl_pw_qpolynomial *res;
  4202. isl_qpolynomial *qp;
  4203. };
  4204. /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
  4205. * We first make all integer divisions positive and then split the
  4206. * quasipolynomials into terms with sign data->sign (the direction
  4207. * of the requested approximation) and terms with the opposite sign.
  4208. * In the first set of terms, each integer division [a/m] is
  4209. * overapproximated by a/m, while in the second it is underapproximated
  4210. * by (a-(m-1))/m.
  4211. */
  4212. static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
  4213. int *signs, void *user)
  4214. {
  4215. struct isl_to_poly_data *data = user;
  4216. isl_pw_qpolynomial *t;
  4217. isl_qpolynomial *qp, *up, *down;
  4218. qp = isl_qpolynomial_copy(data->qp);
  4219. qp = make_divs_pos(qp, signs);
  4220. up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
  4221. up = qp_drop_floors(up, 0);
  4222. down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
  4223. down = qp_drop_floors(down, 1);
  4224. isl_qpolynomial_free(qp);
  4225. qp = isl_qpolynomial_add(up, down);
  4226. t = isl_pw_qpolynomial_alloc(orthant, qp);
  4227. data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
  4228. return isl_stat_ok;
  4229. }
  4230. /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
  4231. * the polynomial will be an overapproximation. If "sign" is negative,
  4232. * it will be an underapproximation. If "sign" is zero, the approximation
  4233. * will lie somewhere in between.
  4234. *
  4235. * In particular, is sign == 0, we simply drop the floors, turning
  4236. * the integer divisions into rational divisions.
  4237. * Otherwise, we split the domains into orthants, make all integer divisions
  4238. * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
  4239. * depending on the requested sign and the sign of the term in which
  4240. * the integer division appears.
  4241. */
  4242. __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
  4243. __isl_take isl_pw_qpolynomial *pwqp, int sign)
  4244. {
  4245. int i;
  4246. struct isl_to_poly_data data;
  4247. if (sign == 0)
  4248. return pwqp_drop_floors(pwqp);
  4249. if (!pwqp)
  4250. return NULL;
  4251. data.sign = sign;
  4252. data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
  4253. for (i = 0; i < pwqp->n; ++i) {
  4254. if (pwqp->p[i].qp->div->n_row == 0) {
  4255. isl_pw_qpolynomial *t;
  4256. t = isl_pw_qpolynomial_alloc(
  4257. isl_set_copy(pwqp->p[i].set),
  4258. isl_qpolynomial_copy(pwqp->p[i].qp));
  4259. data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
  4260. continue;
  4261. }
  4262. data.qp = pwqp->p[i].qp;
  4263. if (isl_set_foreach_orthant(pwqp->p[i].set,
  4264. &to_polynomial_on_orthant, &data) < 0)
  4265. goto error;
  4266. }
  4267. isl_pw_qpolynomial_free(pwqp);
  4268. return data.res;
  4269. error:
  4270. isl_pw_qpolynomial_free(pwqp);
  4271. isl_pw_qpolynomial_free(data.res);
  4272. return NULL;
  4273. }
  4274. static __isl_give isl_pw_qpolynomial *poly_entry(
  4275. __isl_take isl_pw_qpolynomial *pwqp, void *user)
  4276. {
  4277. int *sign = user;
  4278. return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
  4279. }
  4280. __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
  4281. __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
  4282. {
  4283. return isl_union_pw_qpolynomial_transform_inplace(upwqp,
  4284. &poly_entry, &sign);
  4285. }
  4286. __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
  4287. __isl_take isl_qpolynomial *qp)
  4288. {
  4289. int i, k;
  4290. isl_space *space;
  4291. isl_vec *aff = NULL;
  4292. isl_basic_map *bmap = NULL;
  4293. isl_bool is_affine;
  4294. unsigned pos;
  4295. unsigned n_div;
  4296. if (!qp)
  4297. return NULL;
  4298. is_affine = isl_poly_is_affine(qp->poly);
  4299. if (is_affine < 0)
  4300. goto error;
  4301. if (!is_affine)
  4302. isl_die(qp->dim->ctx, isl_error_invalid,
  4303. "input quasi-polynomial not affine", goto error);
  4304. aff = isl_qpolynomial_extract_affine(qp);
  4305. if (!aff)
  4306. goto error;
  4307. space = isl_qpolynomial_get_space(qp);
  4308. pos = 1 + isl_space_offset(space, isl_dim_out);
  4309. n_div = qp->div->n_row;
  4310. bmap = isl_basic_map_alloc_space(space, n_div, 1, 2 * n_div);
  4311. for (i = 0; i < n_div; ++i) {
  4312. k = isl_basic_map_alloc_div(bmap);
  4313. if (k < 0)
  4314. goto error;
  4315. isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
  4316. isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
  4317. bmap = isl_basic_map_add_div_constraints(bmap, k);
  4318. }
  4319. k = isl_basic_map_alloc_equality(bmap);
  4320. if (k < 0)
  4321. goto error;
  4322. isl_int_neg(bmap->eq[k][pos], aff->el[0]);
  4323. isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
  4324. isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
  4325. isl_vec_free(aff);
  4326. isl_qpolynomial_free(qp);
  4327. bmap = isl_basic_map_finalize(bmap);
  4328. return bmap;
  4329. error:
  4330. isl_vec_free(aff);
  4331. isl_qpolynomial_free(qp);
  4332. isl_basic_map_free(bmap);
  4333. return NULL;
  4334. }