test_pretty.py 180 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825
  1. # -*- coding: utf-8 -*-
  2. from sympy.concrete.products import Product
  3. from sympy.concrete.summations import Sum
  4. from sympy.core.add import Add
  5. from sympy.core.basic import Basic
  6. from sympy.core.containers import (Dict, Tuple)
  7. from sympy.core.function import (Derivative, Function, Lambda, Subs)
  8. from sympy.core.mul import Mul
  9. from sympy.core import (EulerGamma, GoldenRatio, Catalan)
  10. from sympy.core.numbers import (I, Rational, oo, pi)
  11. from sympy.core.power import Pow
  12. from sympy.core.relational import (Eq, Ge, Gt, Le, Lt, Ne)
  13. from sympy.core.singleton import S
  14. from sympy.core.symbol import (Symbol, symbols)
  15. from sympy.functions.elementary.complexes import conjugate
  16. from sympy.functions.elementary.exponential import LambertW
  17. from sympy.functions.special.bessel import (airyai, airyaiprime, airybi, airybiprime)
  18. from sympy.functions.special.delta_functions import Heaviside
  19. from sympy.functions.special.error_functions import (fresnelc, fresnels)
  20. from sympy.functions.special.singularity_functions import SingularityFunction
  21. from sympy.functions.special.zeta_functions import dirichlet_eta
  22. from sympy.geometry.line import (Ray, Segment)
  23. from sympy.integrals.integrals import Integral
  24. from sympy.logic.boolalg import (And, Equivalent, ITE, Implies, Nand, Nor, Not, Or, Xor)
  25. from sympy.matrices.dense import (Matrix, diag)
  26. from sympy.matrices.expressions.slice import MatrixSlice
  27. from sympy.matrices.expressions.trace import Trace
  28. from sympy.polys.domains.finitefield import FF
  29. from sympy.polys.domains.integerring import ZZ
  30. from sympy.polys.domains.rationalfield import QQ
  31. from sympy.polys.domains.realfield import RR
  32. from sympy.polys.orderings import (grlex, ilex)
  33. from sympy.polys.polytools import groebner
  34. from sympy.polys.rootoftools import (RootSum, rootof)
  35. from sympy.series.formal import fps
  36. from sympy.series.fourier import fourier_series
  37. from sympy.series.limits import Limit
  38. from sympy.series.order import O
  39. from sympy.series.sequences import (SeqAdd, SeqFormula, SeqMul, SeqPer)
  40. from sympy.sets.contains import Contains
  41. from sympy.sets.fancysets import Range
  42. from sympy.sets.sets import (Complement, FiniteSet, Intersection, Interval, Union)
  43. from sympy.codegen.ast import (Assignment, AddAugmentedAssignment,
  44. SubAugmentedAssignment, MulAugmentedAssignment, DivAugmentedAssignment, ModAugmentedAssignment)
  45. from sympy.core.expr import UnevaluatedExpr
  46. from sympy.physics.quantum.trace import Tr
  47. from sympy.functions import (Abs, Chi, Ci, Ei, KroneckerDelta,
  48. Piecewise, Shi, Si, atan2, beta, binomial, catalan, ceiling, cos,
  49. euler, exp, expint, factorial, factorial2, floor, gamma, hyper, log,
  50. meijerg, sin, sqrt, subfactorial, tan, uppergamma, lerchphi,
  51. elliptic_k, elliptic_f, elliptic_e, elliptic_pi, DiracDelta, bell,
  52. bernoulli, fibonacci, tribonacci, lucas, stieltjes, mathieuc, mathieus,
  53. mathieusprime, mathieucprime)
  54. from sympy.matrices import (Adjoint, Inverse, MatrixSymbol, Transpose,
  55. KroneckerProduct, BlockMatrix, OneMatrix, ZeroMatrix)
  56. from sympy.matrices.expressions import hadamard_power
  57. from sympy.physics import mechanics
  58. from sympy.physics.control.lti import (TransferFunction, Feedback, TransferFunctionMatrix,
  59. Series, Parallel, MIMOSeries, MIMOParallel, MIMOFeedback)
  60. from sympy.physics.units import joule, degree
  61. from sympy.printing.pretty import pprint, pretty as xpretty
  62. from sympy.printing.pretty.pretty_symbology import center_accent, is_combining
  63. from sympy.sets.conditionset import ConditionSet
  64. from sympy.sets import ImageSet, ProductSet
  65. from sympy.sets.setexpr import SetExpr
  66. from sympy.stats.crv_types import Normal
  67. from sympy.stats.symbolic_probability import (Covariance, Expectation,
  68. Probability, Variance)
  69. from sympy.tensor.array import (ImmutableDenseNDimArray, ImmutableSparseNDimArray,
  70. MutableDenseNDimArray, MutableSparseNDimArray, tensorproduct)
  71. from sympy.tensor.functions import TensorProduct
  72. from sympy.tensor.tensor import (TensorIndexType, tensor_indices, TensorHead,
  73. TensorElement, tensor_heads)
  74. from sympy.testing.pytest import raises, _both_exp_pow, warns_deprecated_sympy
  75. from sympy.vector import CoordSys3D, Gradient, Curl, Divergence, Dot, Cross, Laplacian
  76. import sympy as sym
  77. class lowergamma(sym.lowergamma):
  78. pass # testing notation inheritance by a subclass with same name
  79. a, b, c, d, x, y, z, k, n, s, p = symbols('a,b,c,d,x,y,z,k,n,s,p')
  80. f = Function("f")
  81. th = Symbol('theta')
  82. ph = Symbol('phi')
  83. """
  84. Expressions whose pretty-printing is tested here:
  85. (A '#' to the right of an expression indicates that its various acceptable
  86. orderings are accounted for by the tests.)
  87. BASIC EXPRESSIONS:
  88. oo
  89. (x**2)
  90. 1/x
  91. y*x**-2
  92. x**Rational(-5,2)
  93. (-2)**x
  94. Pow(3, 1, evaluate=False)
  95. (x**2 + x + 1) #
  96. 1-x #
  97. 1-2*x #
  98. x/y
  99. -x/y
  100. (x+2)/y #
  101. (1+x)*y #3
  102. -5*x/(x+10) # correct placement of negative sign
  103. 1 - Rational(3,2)*(x+1)
  104. -(-x + 5)*(-x - 2*sqrt(2) + 5) - (-y + 5)*(-y + 5) # issue 5524
  105. ORDERING:
  106. x**2 + x + 1
  107. 1 - x
  108. 1 - 2*x
  109. 2*x**4 + y**2 - x**2 + y**3
  110. RELATIONAL:
  111. Eq(x, y)
  112. Lt(x, y)
  113. Gt(x, y)
  114. Le(x, y)
  115. Ge(x, y)
  116. Ne(x/(y+1), y**2) #
  117. RATIONAL NUMBERS:
  118. y*x**-2
  119. y**Rational(3,2) * x**Rational(-5,2)
  120. sin(x)**3/tan(x)**2
  121. FUNCTIONS (ABS, CONJ, EXP, FUNCTION BRACES, FACTORIAL, FLOOR, CEILING):
  122. (2*x + exp(x)) #
  123. Abs(x)
  124. Abs(x/(x**2+1)) #
  125. Abs(1 / (y - Abs(x)))
  126. factorial(n)
  127. factorial(2*n)
  128. subfactorial(n)
  129. subfactorial(2*n)
  130. factorial(factorial(factorial(n)))
  131. factorial(n+1) #
  132. conjugate(x)
  133. conjugate(f(x+1)) #
  134. f(x)
  135. f(x, y)
  136. f(x/(y+1), y) #
  137. f(x**x**x**x**x**x)
  138. sin(x)**2
  139. conjugate(a+b*I)
  140. conjugate(exp(a+b*I))
  141. conjugate( f(1 + conjugate(f(x))) ) #
  142. f(x/(y+1), y) # denom of first arg
  143. floor(1 / (y - floor(x)))
  144. ceiling(1 / (y - ceiling(x)))
  145. SQRT:
  146. sqrt(2)
  147. 2**Rational(1,3)
  148. 2**Rational(1,1000)
  149. sqrt(x**2 + 1)
  150. (1 + sqrt(5))**Rational(1,3)
  151. 2**(1/x)
  152. sqrt(2+pi)
  153. (2+(1+x**2)/(2+x))**Rational(1,4)+(1+x**Rational(1,1000))/sqrt(3+x**2)
  154. DERIVATIVES:
  155. Derivative(log(x), x, evaluate=False)
  156. Derivative(log(x), x, evaluate=False) + x #
  157. Derivative(log(x) + x**2, x, y, evaluate=False)
  158. Derivative(2*x*y, y, x, evaluate=False) + x**2 #
  159. beta(alpha).diff(alpha)
  160. INTEGRALS:
  161. Integral(log(x), x)
  162. Integral(x**2, x)
  163. Integral((sin(x))**2 / (tan(x))**2)
  164. Integral(x**(2**x), x)
  165. Integral(x**2, (x,1,2))
  166. Integral(x**2, (x,Rational(1,2),10))
  167. Integral(x**2*y**2, x,y)
  168. Integral(x**2, (x, None, 1))
  169. Integral(x**2, (x, 1, None))
  170. Integral(sin(th)/cos(ph), (th,0,pi), (ph, 0, 2*pi))
  171. MATRICES:
  172. Matrix([[x**2+1, 1], [y, x+y]]) #
  173. Matrix([[x/y, y, th], [0, exp(I*k*ph), 1]])
  174. PIECEWISE:
  175. Piecewise((x,x<1),(x**2,True))
  176. ITE:
  177. ITE(x, y, z)
  178. SEQUENCES (TUPLES, LISTS, DICTIONARIES):
  179. ()
  180. []
  181. {}
  182. (1/x,)
  183. [x**2, 1/x, x, y, sin(th)**2/cos(ph)**2]
  184. (x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  185. {x: sin(x)}
  186. {1/x: 1/y, x: sin(x)**2} #
  187. [x**2]
  188. (x**2,)
  189. {x**2: 1}
  190. LIMITS:
  191. Limit(x, x, oo)
  192. Limit(x**2, x, 0)
  193. Limit(1/x, x, 0)
  194. Limit(sin(x)/x, x, 0)
  195. UNITS:
  196. joule => kg*m**2/s
  197. SUBS:
  198. Subs(f(x), x, ph**2)
  199. Subs(f(x).diff(x), x, 0)
  200. Subs(f(x).diff(x)/y, (x, y), (0, Rational(1, 2)))
  201. ORDER:
  202. O(1)
  203. O(1/x)
  204. O(x**2 + y**2)
  205. """
  206. def pretty(expr, order=None):
  207. """ASCII pretty-printing"""
  208. return xpretty(expr, order=order, use_unicode=False, wrap_line=False)
  209. def upretty(expr, order=None):
  210. """Unicode pretty-printing"""
  211. return xpretty(expr, order=order, use_unicode=True, wrap_line=False)
  212. def test_pretty_ascii_str():
  213. assert pretty( 'xxx' ) == 'xxx'
  214. assert pretty( "xxx" ) == 'xxx'
  215. assert pretty( 'xxx\'xxx' ) == 'xxx\'xxx'
  216. assert pretty( 'xxx"xxx' ) == 'xxx\"xxx'
  217. assert pretty( 'xxx\"xxx' ) == 'xxx\"xxx'
  218. assert pretty( "xxx'xxx" ) == 'xxx\'xxx'
  219. assert pretty( "xxx\'xxx" ) == 'xxx\'xxx'
  220. assert pretty( "xxx\"xxx" ) == 'xxx\"xxx'
  221. assert pretty( "xxx\"xxx\'xxx" ) == 'xxx"xxx\'xxx'
  222. assert pretty( "xxx\nxxx" ) == 'xxx\nxxx'
  223. def test_pretty_unicode_str():
  224. assert pretty( 'xxx' ) == 'xxx'
  225. assert pretty( 'xxx' ) == 'xxx'
  226. assert pretty( 'xxx\'xxx' ) == 'xxx\'xxx'
  227. assert pretty( 'xxx"xxx' ) == 'xxx\"xxx'
  228. assert pretty( 'xxx\"xxx' ) == 'xxx\"xxx'
  229. assert pretty( "xxx'xxx" ) == 'xxx\'xxx'
  230. assert pretty( "xxx\'xxx" ) == 'xxx\'xxx'
  231. assert pretty( "xxx\"xxx" ) == 'xxx\"xxx'
  232. assert pretty( "xxx\"xxx\'xxx" ) == 'xxx"xxx\'xxx'
  233. assert pretty( "xxx\nxxx" ) == 'xxx\nxxx'
  234. def test_upretty_greek():
  235. assert upretty( oo ) == '∞'
  236. assert upretty( Symbol('alpha^+_1') ) == 'α⁺₁'
  237. assert upretty( Symbol('beta') ) == 'β'
  238. assert upretty(Symbol('lambda')) == 'λ'
  239. def test_upretty_multiindex():
  240. assert upretty( Symbol('beta12') ) == 'β₁₂'
  241. assert upretty( Symbol('Y00') ) == 'Y₀₀'
  242. assert upretty( Symbol('Y_00') ) == 'Y₀₀'
  243. assert upretty( Symbol('F^+-') ) == 'F⁺⁻'
  244. def test_upretty_sub_super():
  245. assert upretty( Symbol('beta_1_2') ) == 'β₁ ₂'
  246. assert upretty( Symbol('beta^1^2') ) == 'β¹ ²'
  247. assert upretty( Symbol('beta_1^2') ) == 'β²₁'
  248. assert upretty( Symbol('beta_10_20') ) == 'β₁₀ ₂₀'
  249. assert upretty( Symbol('beta_ax_gamma^i') ) == 'βⁱₐₓ ᵧ'
  250. assert upretty( Symbol("F^1^2_3_4") ) == 'F¹ ²₃ ₄'
  251. assert upretty( Symbol("F_1_2^3^4") ) == 'F³ ⁴₁ ₂'
  252. assert upretty( Symbol("F_1_2_3_4") ) == 'F₁ ₂ ₃ ₄'
  253. assert upretty( Symbol("F^1^2^3^4") ) == 'F¹ ² ³ ⁴'
  254. def test_upretty_subs_missing_in_24():
  255. assert upretty( Symbol('F_beta') ) == 'Fᵦ'
  256. assert upretty( Symbol('F_gamma') ) == 'Fᵧ'
  257. assert upretty( Symbol('F_rho') ) == 'Fᵨ'
  258. assert upretty( Symbol('F_phi') ) == 'Fᵩ'
  259. assert upretty( Symbol('F_chi') ) == 'Fᵪ'
  260. assert upretty( Symbol('F_a') ) == 'Fₐ'
  261. assert upretty( Symbol('F_e') ) == 'Fₑ'
  262. assert upretty( Symbol('F_i') ) == 'Fᵢ'
  263. assert upretty( Symbol('F_o') ) == 'Fₒ'
  264. assert upretty( Symbol('F_u') ) == 'Fᵤ'
  265. assert upretty( Symbol('F_r') ) == 'Fᵣ'
  266. assert upretty( Symbol('F_v') ) == 'Fᵥ'
  267. assert upretty( Symbol('F_x') ) == 'Fₓ'
  268. def test_missing_in_2X_issue_9047():
  269. assert upretty( Symbol('F_h') ) == 'Fₕ'
  270. assert upretty( Symbol('F_k') ) == 'Fₖ'
  271. assert upretty( Symbol('F_l') ) == 'Fₗ'
  272. assert upretty( Symbol('F_m') ) == 'Fₘ'
  273. assert upretty( Symbol('F_n') ) == 'Fₙ'
  274. assert upretty( Symbol('F_p') ) == 'Fₚ'
  275. assert upretty( Symbol('F_s') ) == 'Fₛ'
  276. assert upretty( Symbol('F_t') ) == 'Fₜ'
  277. def test_upretty_modifiers():
  278. # Accents
  279. assert upretty( Symbol('Fmathring') ) == 'F̊'
  280. assert upretty( Symbol('Fddddot') ) == 'F⃜'
  281. assert upretty( Symbol('Fdddot') ) == 'F⃛'
  282. assert upretty( Symbol('Fddot') ) == 'F̈'
  283. assert upretty( Symbol('Fdot') ) == 'Ḟ'
  284. assert upretty( Symbol('Fcheck') ) == 'F̌'
  285. assert upretty( Symbol('Fbreve') ) == 'F̆'
  286. assert upretty( Symbol('Facute') ) == 'F́'
  287. assert upretty( Symbol('Fgrave') ) == 'F̀'
  288. assert upretty( Symbol('Ftilde') ) == 'F̃'
  289. assert upretty( Symbol('Fhat') ) == 'F̂'
  290. assert upretty( Symbol('Fbar') ) == 'F̅'
  291. assert upretty( Symbol('Fvec') ) == 'F⃗'
  292. assert upretty( Symbol('Fprime') ) == 'F′'
  293. assert upretty( Symbol('Fprm') ) == 'F′'
  294. # No faces are actually implemented, but test to make sure the modifiers are stripped
  295. assert upretty( Symbol('Fbold') ) == 'Fbold'
  296. assert upretty( Symbol('Fbm') ) == 'Fbm'
  297. assert upretty( Symbol('Fcal') ) == 'Fcal'
  298. assert upretty( Symbol('Fscr') ) == 'Fscr'
  299. assert upretty( Symbol('Ffrak') ) == 'Ffrak'
  300. # Brackets
  301. assert upretty( Symbol('Fnorm') ) == '‖F‖'
  302. assert upretty( Symbol('Favg') ) == '⟨F⟩'
  303. assert upretty( Symbol('Fabs') ) == '|F|'
  304. assert upretty( Symbol('Fmag') ) == '|F|'
  305. # Combinations
  306. assert upretty( Symbol('xvecdot') ) == 'x⃗̇'
  307. assert upretty( Symbol('xDotVec') ) == 'ẋ⃗'
  308. assert upretty( Symbol('xHATNorm') ) == '‖x̂‖'
  309. assert upretty( Symbol('xMathring_yCheckPRM__zbreveAbs') ) == 'x̊_y̌′__|z̆|'
  310. assert upretty( Symbol('alphadothat_nVECDOT__tTildePrime') ) == 'α̇̂_n⃗̇__t̃′'
  311. assert upretty( Symbol('x_dot') ) == 'x_dot'
  312. assert upretty( Symbol('x__dot') ) == 'x__dot'
  313. def test_pretty_Cycle():
  314. from sympy.combinatorics.permutations import Cycle
  315. assert pretty(Cycle(1, 2)) == '(1 2)'
  316. assert pretty(Cycle(2)) == '(2)'
  317. assert pretty(Cycle(1, 3)(4, 5)) == '(1 3)(4 5)'
  318. assert pretty(Cycle()) == '()'
  319. def test_pretty_Permutation():
  320. from sympy.combinatorics.permutations import Permutation
  321. p1 = Permutation(1, 2)(3, 4)
  322. assert xpretty(p1, perm_cyclic=True, use_unicode=True) == "(1 2)(3 4)"
  323. assert xpretty(p1, perm_cyclic=True, use_unicode=False) == "(1 2)(3 4)"
  324. assert xpretty(p1, perm_cyclic=False, use_unicode=True) == \
  325. '⎛0 1 2 3 4⎞\n'\
  326. '⎝0 2 1 4 3⎠'
  327. assert xpretty(p1, perm_cyclic=False, use_unicode=False) == \
  328. "/0 1 2 3 4\\\n"\
  329. "\\0 2 1 4 3/"
  330. with warns_deprecated_sympy():
  331. old_print_cyclic = Permutation.print_cyclic
  332. Permutation.print_cyclic = False
  333. assert xpretty(p1, use_unicode=True) == \
  334. '⎛0 1 2 3 4⎞\n'\
  335. '⎝0 2 1 4 3⎠'
  336. assert xpretty(p1, use_unicode=False) == \
  337. "/0 1 2 3 4\\\n"\
  338. "\\0 2 1 4 3/"
  339. Permutation.print_cyclic = old_print_cyclic
  340. def test_pretty_basic():
  341. assert pretty( -Rational(1)/2 ) == '-1/2'
  342. assert pretty( -Rational(13)/22 ) == \
  343. """\
  344. -13 \n\
  345. ----\n\
  346. 22 \
  347. """
  348. expr = oo
  349. ascii_str = \
  350. """\
  351. oo\
  352. """
  353. ucode_str = \
  354. """\
  355. ∞\
  356. """
  357. assert pretty(expr) == ascii_str
  358. assert upretty(expr) == ucode_str
  359. expr = (x**2)
  360. ascii_str = \
  361. """\
  362. 2\n\
  363. x \
  364. """
  365. ucode_str = \
  366. """\
  367. 2\n\
  368. x \
  369. """
  370. assert pretty(expr) == ascii_str
  371. assert upretty(expr) == ucode_str
  372. expr = 1/x
  373. ascii_str = \
  374. """\
  375. 1\n\
  376. -\n\
  377. x\
  378. """
  379. ucode_str = \
  380. """\
  381. 1\n\
  382. ─\n\
  383. x\
  384. """
  385. assert pretty(expr) == ascii_str
  386. assert upretty(expr) == ucode_str
  387. # not the same as 1/x
  388. expr = x**-1.0
  389. ascii_str = \
  390. """\
  391. -1.0\n\
  392. x \
  393. """
  394. ucode_str = \
  395. """\
  396. -1.0\n\
  397. x \
  398. """
  399. assert pretty(expr) == ascii_str
  400. assert upretty(expr) == ucode_str
  401. # see issue #2860
  402. expr = Pow(S(2), -1.0, evaluate=False)
  403. ascii_str = \
  404. """\
  405. -1.0\n\
  406. 2 \
  407. """
  408. ucode_str = \
  409. """\
  410. -1.0\n\
  411. 2 \
  412. """
  413. assert pretty(expr) == ascii_str
  414. assert upretty(expr) == ucode_str
  415. expr = y*x**-2
  416. ascii_str = \
  417. """\
  418. y \n\
  419. --\n\
  420. 2\n\
  421. x \
  422. """
  423. ucode_str = \
  424. """\
  425. y \n\
  426. ──\n\
  427. 2\n\
  428. x \
  429. """
  430. assert pretty(expr) == ascii_str
  431. assert upretty(expr) == ucode_str
  432. #see issue #14033
  433. expr = x**Rational(1, 3)
  434. ascii_str = \
  435. """\
  436. 1/3\n\
  437. x \
  438. """
  439. ucode_str = \
  440. """\
  441. 1/3\n\
  442. x \
  443. """
  444. assert xpretty(expr, use_unicode=False, wrap_line=False,\
  445. root_notation = False) == ascii_str
  446. assert xpretty(expr, use_unicode=True, wrap_line=False,\
  447. root_notation = False) == ucode_str
  448. expr = x**Rational(-5, 2)
  449. ascii_str = \
  450. """\
  451. 1 \n\
  452. ----\n\
  453. 5/2\n\
  454. x \
  455. """
  456. ucode_str = \
  457. """\
  458. 1 \n\
  459. ────\n\
  460. 5/2\n\
  461. x \
  462. """
  463. assert pretty(expr) == ascii_str
  464. assert upretty(expr) == ucode_str
  465. expr = (-2)**x
  466. ascii_str = \
  467. """\
  468. x\n\
  469. (-2) \
  470. """
  471. ucode_str = \
  472. """\
  473. x\n\
  474. (-2) \
  475. """
  476. assert pretty(expr) == ascii_str
  477. assert upretty(expr) == ucode_str
  478. # See issue 4923
  479. expr = Pow(3, 1, evaluate=False)
  480. ascii_str = \
  481. """\
  482. 1\n\
  483. 3 \
  484. """
  485. ucode_str = \
  486. """\
  487. 1\n\
  488. 3 \
  489. """
  490. assert pretty(expr) == ascii_str
  491. assert upretty(expr) == ucode_str
  492. expr = (x**2 + x + 1)
  493. ascii_str_1 = \
  494. """\
  495. 2\n\
  496. 1 + x + x \
  497. """
  498. ascii_str_2 = \
  499. """\
  500. 2 \n\
  501. x + x + 1\
  502. """
  503. ascii_str_3 = \
  504. """\
  505. 2 \n\
  506. x + 1 + x\
  507. """
  508. ucode_str_1 = \
  509. """\
  510. 2\n\
  511. 1 + x + x \
  512. """
  513. ucode_str_2 = \
  514. """\
  515. 2 \n\
  516. x + x + 1\
  517. """
  518. ucode_str_3 = \
  519. """\
  520. 2 \n\
  521. x + 1 + x\
  522. """
  523. assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3]
  524. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  525. expr = 1 - x
  526. ascii_str_1 = \
  527. """\
  528. 1 - x\
  529. """
  530. ascii_str_2 = \
  531. """\
  532. -x + 1\
  533. """
  534. ucode_str_1 = \
  535. """\
  536. 1 - x\
  537. """
  538. ucode_str_2 = \
  539. """\
  540. -x + 1\
  541. """
  542. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  543. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  544. expr = 1 - 2*x
  545. ascii_str_1 = \
  546. """\
  547. 1 - 2*x\
  548. """
  549. ascii_str_2 = \
  550. """\
  551. -2*x + 1\
  552. """
  553. ucode_str_1 = \
  554. """\
  555. 1 - 2⋅x\
  556. """
  557. ucode_str_2 = \
  558. """\
  559. -2⋅x + 1\
  560. """
  561. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  562. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  563. expr = x/y
  564. ascii_str = \
  565. """\
  566. x\n\
  567. -\n\
  568. y\
  569. """
  570. ucode_str = \
  571. """\
  572. x\n\
  573. ─\n\
  574. y\
  575. """
  576. assert pretty(expr) == ascii_str
  577. assert upretty(expr) == ucode_str
  578. expr = -x/y
  579. ascii_str = \
  580. """\
  581. -x \n\
  582. ---\n\
  583. y \
  584. """
  585. ucode_str = \
  586. """\
  587. -x \n\
  588. ───\n\
  589. y \
  590. """
  591. assert pretty(expr) == ascii_str
  592. assert upretty(expr) == ucode_str
  593. expr = (x + 2)/y
  594. ascii_str_1 = \
  595. """\
  596. 2 + x\n\
  597. -----\n\
  598. y \
  599. """
  600. ascii_str_2 = \
  601. """\
  602. x + 2\n\
  603. -----\n\
  604. y \
  605. """
  606. ucode_str_1 = \
  607. """\
  608. 2 + x\n\
  609. ─────\n\
  610. y \
  611. """
  612. ucode_str_2 = \
  613. """\
  614. x + 2\n\
  615. ─────\n\
  616. y \
  617. """
  618. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  619. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  620. expr = (1 + x)*y
  621. ascii_str_1 = \
  622. """\
  623. y*(1 + x)\
  624. """
  625. ascii_str_2 = \
  626. """\
  627. (1 + x)*y\
  628. """
  629. ascii_str_3 = \
  630. """\
  631. y*(x + 1)\
  632. """
  633. ucode_str_1 = \
  634. """\
  635. y⋅(1 + x)\
  636. """
  637. ucode_str_2 = \
  638. """\
  639. (1 + x)⋅y\
  640. """
  641. ucode_str_3 = \
  642. """\
  643. y⋅(x + 1)\
  644. """
  645. assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3]
  646. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  647. # Test for correct placement of the negative sign
  648. expr = -5*x/(x + 10)
  649. ascii_str_1 = \
  650. """\
  651. -5*x \n\
  652. ------\n\
  653. 10 + x\
  654. """
  655. ascii_str_2 = \
  656. """\
  657. -5*x \n\
  658. ------\n\
  659. x + 10\
  660. """
  661. ucode_str_1 = \
  662. """\
  663. -5⋅x \n\
  664. ──────\n\
  665. 10 + x\
  666. """
  667. ucode_str_2 = \
  668. """\
  669. -5⋅x \n\
  670. ──────\n\
  671. x + 10\
  672. """
  673. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  674. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  675. expr = -S.Half - 3*x
  676. ascii_str = \
  677. """\
  678. -3*x - 1/2\
  679. """
  680. ucode_str = \
  681. """\
  682. -3⋅x - 1/2\
  683. """
  684. assert pretty(expr) == ascii_str
  685. assert upretty(expr) == ucode_str
  686. expr = S.Half - 3*x
  687. ascii_str = \
  688. """\
  689. 1/2 - 3*x\
  690. """
  691. ucode_str = \
  692. """\
  693. 1/2 - 3⋅x\
  694. """
  695. assert pretty(expr) == ascii_str
  696. assert upretty(expr) == ucode_str
  697. expr = -S.Half - 3*x/2
  698. ascii_str = \
  699. """\
  700. 3*x 1\n\
  701. - --- - -\n\
  702. 2 2\
  703. """
  704. ucode_str = \
  705. """\
  706. 3⋅x 1\n\
  707. - ─── - ─\n\
  708. 2 2\
  709. """
  710. assert pretty(expr) == ascii_str
  711. assert upretty(expr) == ucode_str
  712. expr = S.Half - 3*x/2
  713. ascii_str = \
  714. """\
  715. 1 3*x\n\
  716. - - ---\n\
  717. 2 2 \
  718. """
  719. ucode_str = \
  720. """\
  721. 1 3⋅x\n\
  722. ─ - ───\n\
  723. 2 2 \
  724. """
  725. assert pretty(expr) == ascii_str
  726. assert upretty(expr) == ucode_str
  727. def test_negative_fractions():
  728. expr = -x/y
  729. ascii_str =\
  730. """\
  731. -x \n\
  732. ---\n\
  733. y \
  734. """
  735. ucode_str =\
  736. """\
  737. -x \n\
  738. ───\n\
  739. y \
  740. """
  741. assert pretty(expr) == ascii_str
  742. assert upretty(expr) == ucode_str
  743. expr = -x*z/y
  744. ascii_str =\
  745. """\
  746. -x*z \n\
  747. -----\n\
  748. y \
  749. """
  750. ucode_str =\
  751. """\
  752. -x⋅z \n\
  753. ─────\n\
  754. y \
  755. """
  756. assert pretty(expr) == ascii_str
  757. assert upretty(expr) == ucode_str
  758. expr = x**2/y
  759. ascii_str =\
  760. """\
  761. 2\n\
  762. x \n\
  763. --\n\
  764. y \
  765. """
  766. ucode_str =\
  767. """\
  768. 2\n\
  769. x \n\
  770. ──\n\
  771. y \
  772. """
  773. assert pretty(expr) == ascii_str
  774. assert upretty(expr) == ucode_str
  775. expr = -x**2/y
  776. ascii_str =\
  777. """\
  778. 2 \n\
  779. -x \n\
  780. ----\n\
  781. y \
  782. """
  783. ucode_str =\
  784. """\
  785. 2 \n\
  786. -x \n\
  787. ────\n\
  788. y \
  789. """
  790. assert pretty(expr) == ascii_str
  791. assert upretty(expr) == ucode_str
  792. expr = -x/(y*z)
  793. ascii_str =\
  794. """\
  795. -x \n\
  796. ---\n\
  797. y*z\
  798. """
  799. ucode_str =\
  800. """\
  801. -x \n\
  802. ───\n\
  803. y⋅z\
  804. """
  805. assert pretty(expr) == ascii_str
  806. assert upretty(expr) == ucode_str
  807. expr = -a/y**2
  808. ascii_str =\
  809. """\
  810. -a \n\
  811. ---\n\
  812. 2\n\
  813. y \
  814. """
  815. ucode_str =\
  816. """\
  817. -a \n\
  818. ───\n\
  819. 2\n\
  820. y \
  821. """
  822. assert pretty(expr) == ascii_str
  823. assert upretty(expr) == ucode_str
  824. expr = y**(-a/b)
  825. ascii_str =\
  826. """\
  827. -a \n\
  828. ---\n\
  829. b \n\
  830. y \
  831. """
  832. ucode_str =\
  833. """\
  834. -a \n\
  835. ───\n\
  836. b \n\
  837. y \
  838. """
  839. assert pretty(expr) == ascii_str
  840. assert upretty(expr) == ucode_str
  841. expr = -1/y**2
  842. ascii_str =\
  843. """\
  844. -1 \n\
  845. ---\n\
  846. 2\n\
  847. y \
  848. """
  849. ucode_str =\
  850. """\
  851. -1 \n\
  852. ───\n\
  853. 2\n\
  854. y \
  855. """
  856. assert pretty(expr) == ascii_str
  857. assert upretty(expr) == ucode_str
  858. expr = -10/b**2
  859. ascii_str =\
  860. """\
  861. -10 \n\
  862. ----\n\
  863. 2 \n\
  864. b \
  865. """
  866. ucode_str =\
  867. """\
  868. -10 \n\
  869. ────\n\
  870. 2 \n\
  871. b \
  872. """
  873. assert pretty(expr) == ascii_str
  874. assert upretty(expr) == ucode_str
  875. expr = Rational(-200, 37)
  876. ascii_str =\
  877. """\
  878. -200 \n\
  879. -----\n\
  880. 37 \
  881. """
  882. ucode_str =\
  883. """\
  884. -200 \n\
  885. ─────\n\
  886. 37 \
  887. """
  888. assert pretty(expr) == ascii_str
  889. assert upretty(expr) == ucode_str
  890. def test_Mul():
  891. expr = Mul(0, 1, evaluate=False)
  892. assert pretty(expr) == "0*1"
  893. assert upretty(expr) == "0⋅1"
  894. expr = Mul(1, 0, evaluate=False)
  895. assert pretty(expr) == "1*0"
  896. assert upretty(expr) == "1⋅0"
  897. expr = Mul(1, 1, evaluate=False)
  898. assert pretty(expr) == "1*1"
  899. assert upretty(expr) == "1⋅1"
  900. expr = Mul(1, 1, 1, evaluate=False)
  901. assert pretty(expr) == "1*1*1"
  902. assert upretty(expr) == "1⋅1⋅1"
  903. expr = Mul(1, 2, evaluate=False)
  904. assert pretty(expr) == "1*2"
  905. assert upretty(expr) == "1⋅2"
  906. expr = Add(0, 1, evaluate=False)
  907. assert pretty(expr) == "0 + 1"
  908. assert upretty(expr) == "0 + 1"
  909. expr = Mul(1, 1, 2, evaluate=False)
  910. assert pretty(expr) == "1*1*2"
  911. assert upretty(expr) == "1⋅1⋅2"
  912. expr = Add(0, 0, 1, evaluate=False)
  913. assert pretty(expr) == "0 + 0 + 1"
  914. assert upretty(expr) == "0 + 0 + 1"
  915. expr = Mul(1, -1, evaluate=False)
  916. assert pretty(expr) == "1*-1"
  917. assert upretty(expr) == "1⋅-1"
  918. expr = Mul(1.0, x, evaluate=False)
  919. assert pretty(expr) == "1.0*x"
  920. assert upretty(expr) == "1.0⋅x"
  921. expr = Mul(1, 1, 2, 3, x, evaluate=False)
  922. assert pretty(expr) == "1*1*2*3*x"
  923. assert upretty(expr) == "1⋅1⋅2⋅3⋅x"
  924. expr = Mul(-1, 1, evaluate=False)
  925. assert pretty(expr) == "-1*1"
  926. assert upretty(expr) == "-1⋅1"
  927. expr = Mul(4, 3, 2, 1, 0, y, x, evaluate=False)
  928. assert pretty(expr) == "4*3*2*1*0*y*x"
  929. assert upretty(expr) == "4⋅3⋅2⋅1⋅0⋅y⋅x"
  930. expr = Mul(4, 3, 2, 1+z, 0, y, x, evaluate=False)
  931. assert pretty(expr) == "4*3*2*(z + 1)*0*y*x"
  932. assert upretty(expr) == "4⋅3⋅2⋅(z + 1)⋅0⋅y⋅x"
  933. expr = Mul(Rational(2, 3), Rational(5, 7), evaluate=False)
  934. assert pretty(expr) == "2/3*5/7"
  935. assert upretty(expr) == "2/3⋅5/7"
  936. expr = Mul(x + y, Rational(1, 2), evaluate=False)
  937. assert pretty(expr) == "(x + y)*1/2"
  938. assert upretty(expr) == "(x + y)⋅1/2"
  939. expr = Mul(Rational(1, 2), x + y, evaluate=False)
  940. assert pretty(expr) == "x + y\n-----\n 2 "
  941. assert upretty(expr) == "x + y\n─────\n 2 "
  942. expr = Mul(S.One, x + y, evaluate=False)
  943. assert pretty(expr) == "1*(x + y)"
  944. assert upretty(expr) == "1⋅(x + y)"
  945. expr = Mul(x - y, S.One, evaluate=False)
  946. assert pretty(expr) == "(x - y)*1"
  947. assert upretty(expr) == "(x - y)⋅1"
  948. expr = Mul(Rational(1, 2), x - y, S.One, x + y, evaluate=False)
  949. assert pretty(expr) == "1/2*(x - y)*1*(x + y)"
  950. assert upretty(expr) == "1/2⋅(x - y)⋅1⋅(x + y)"
  951. expr = Mul(x + y, Rational(3, 4), S.One, y - z, evaluate=False)
  952. assert pretty(expr) == "(x + y)*3/4*1*(y - z)"
  953. assert upretty(expr) == "(x + y)⋅3/4⋅1⋅(y - z)"
  954. expr = Mul(x + y, Rational(1, 1), Rational(3, 4), Rational(5, 6),evaluate=False)
  955. assert pretty(expr) == "(x + y)*1*3/4*5/6"
  956. assert upretty(expr) == "(x + y)⋅1⋅3/4⋅5/6"
  957. expr = Mul(Rational(3, 4), x + y, S.One, y - z, evaluate=False)
  958. assert pretty(expr) == "3/4*(x + y)*1*(y - z)"
  959. assert upretty(expr) == "3/4⋅(x + y)⋅1⋅(y - z)"
  960. def test_issue_5524():
  961. assert pretty(-(-x + 5)*(-x - 2*sqrt(2) + 5) - (-y + 5)*(-y + 5)) == \
  962. """\
  963. 2 / ___ \\\n\
  964. - (5 - y) + (x - 5)*\\-x - 2*\\/ 2 + 5/\
  965. """
  966. assert upretty(-(-x + 5)*(-x - 2*sqrt(2) + 5) - (-y + 5)*(-y + 5)) == \
  967. """\
  968. 2 \n\
  969. - (5 - y) + (x - 5)⋅(-x - 2⋅√2 + 5)\
  970. """
  971. def test_pretty_ordering():
  972. assert pretty(x**2 + x + 1, order='lex') == \
  973. """\
  974. 2 \n\
  975. x + x + 1\
  976. """
  977. assert pretty(x**2 + x + 1, order='rev-lex') == \
  978. """\
  979. 2\n\
  980. 1 + x + x \
  981. """
  982. assert pretty(1 - x, order='lex') == '-x + 1'
  983. assert pretty(1 - x, order='rev-lex') == '1 - x'
  984. assert pretty(1 - 2*x, order='lex') == '-2*x + 1'
  985. assert pretty(1 - 2*x, order='rev-lex') == '1 - 2*x'
  986. f = 2*x**4 + y**2 - x**2 + y**3
  987. assert pretty(f, order=None) == \
  988. """\
  989. 4 2 3 2\n\
  990. 2*x - x + y + y \
  991. """
  992. assert pretty(f, order='lex') == \
  993. """\
  994. 4 2 3 2\n\
  995. 2*x - x + y + y \
  996. """
  997. assert pretty(f, order='rev-lex') == \
  998. """\
  999. 2 3 2 4\n\
  1000. y + y - x + 2*x \
  1001. """
  1002. expr = x - x**3/6 + x**5/120 + O(x**6)
  1003. ascii_str = \
  1004. """\
  1005. 3 5 \n\
  1006. x x / 6\\\n\
  1007. x - -- + --- + O\\x /\n\
  1008. 6 120 \
  1009. """
  1010. ucode_str = \
  1011. """\
  1012. 3 5 \n\
  1013. x x ⎛ 6⎞\n\
  1014. x - ── + ─── + O⎝x ⎠\n\
  1015. 6 120 \
  1016. """
  1017. assert pretty(expr, order=None) == ascii_str
  1018. assert upretty(expr, order=None) == ucode_str
  1019. assert pretty(expr, order='lex') == ascii_str
  1020. assert upretty(expr, order='lex') == ucode_str
  1021. assert pretty(expr, order='rev-lex') == ascii_str
  1022. assert upretty(expr, order='rev-lex') == ucode_str
  1023. def test_EulerGamma():
  1024. assert pretty(EulerGamma) == str(EulerGamma) == "EulerGamma"
  1025. assert upretty(EulerGamma) == "γ"
  1026. def test_GoldenRatio():
  1027. assert pretty(GoldenRatio) == str(GoldenRatio) == "GoldenRatio"
  1028. assert upretty(GoldenRatio) == "φ"
  1029. def test_Catalan():
  1030. assert pretty(Catalan) == upretty(Catalan) == "G"
  1031. def test_pretty_relational():
  1032. expr = Eq(x, y)
  1033. ascii_str = \
  1034. """\
  1035. x = y\
  1036. """
  1037. ucode_str = \
  1038. """\
  1039. x = y\
  1040. """
  1041. assert pretty(expr) == ascii_str
  1042. assert upretty(expr) == ucode_str
  1043. expr = Lt(x, y)
  1044. ascii_str = \
  1045. """\
  1046. x < y\
  1047. """
  1048. ucode_str = \
  1049. """\
  1050. x < y\
  1051. """
  1052. assert pretty(expr) == ascii_str
  1053. assert upretty(expr) == ucode_str
  1054. expr = Gt(x, y)
  1055. ascii_str = \
  1056. """\
  1057. x > y\
  1058. """
  1059. ucode_str = \
  1060. """\
  1061. x > y\
  1062. """
  1063. assert pretty(expr) == ascii_str
  1064. assert upretty(expr) == ucode_str
  1065. expr = Le(x, y)
  1066. ascii_str = \
  1067. """\
  1068. x <= y\
  1069. """
  1070. ucode_str = \
  1071. """\
  1072. x ≤ y\
  1073. """
  1074. assert pretty(expr) == ascii_str
  1075. assert upretty(expr) == ucode_str
  1076. expr = Ge(x, y)
  1077. ascii_str = \
  1078. """\
  1079. x >= y\
  1080. """
  1081. ucode_str = \
  1082. """\
  1083. x ≥ y\
  1084. """
  1085. assert pretty(expr) == ascii_str
  1086. assert upretty(expr) == ucode_str
  1087. expr = Ne(x/(y + 1), y**2)
  1088. ascii_str_1 = \
  1089. """\
  1090. x 2\n\
  1091. ----- != y \n\
  1092. 1 + y \
  1093. """
  1094. ascii_str_2 = \
  1095. """\
  1096. x 2\n\
  1097. ----- != y \n\
  1098. y + 1 \
  1099. """
  1100. ucode_str_1 = \
  1101. """\
  1102. x 2\n\
  1103. ───── ≠ y \n\
  1104. 1 + y \
  1105. """
  1106. ucode_str_2 = \
  1107. """\
  1108. x 2\n\
  1109. ───── ≠ y \n\
  1110. y + 1 \
  1111. """
  1112. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1113. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1114. def test_Assignment():
  1115. expr = Assignment(x, y)
  1116. ascii_str = \
  1117. """\
  1118. x := y\
  1119. """
  1120. ucode_str = \
  1121. """\
  1122. x := y\
  1123. """
  1124. assert pretty(expr) == ascii_str
  1125. assert upretty(expr) == ucode_str
  1126. def test_AugmentedAssignment():
  1127. expr = AddAugmentedAssignment(x, y)
  1128. ascii_str = \
  1129. """\
  1130. x += y\
  1131. """
  1132. ucode_str = \
  1133. """\
  1134. x += y\
  1135. """
  1136. assert pretty(expr) == ascii_str
  1137. assert upretty(expr) == ucode_str
  1138. expr = SubAugmentedAssignment(x, y)
  1139. ascii_str = \
  1140. """\
  1141. x -= y\
  1142. """
  1143. ucode_str = \
  1144. """\
  1145. x -= y\
  1146. """
  1147. assert pretty(expr) == ascii_str
  1148. assert upretty(expr) == ucode_str
  1149. expr = MulAugmentedAssignment(x, y)
  1150. ascii_str = \
  1151. """\
  1152. x *= y\
  1153. """
  1154. ucode_str = \
  1155. """\
  1156. x *= y\
  1157. """
  1158. assert pretty(expr) == ascii_str
  1159. assert upretty(expr) == ucode_str
  1160. expr = DivAugmentedAssignment(x, y)
  1161. ascii_str = \
  1162. """\
  1163. x /= y\
  1164. """
  1165. ucode_str = \
  1166. """\
  1167. x /= y\
  1168. """
  1169. assert pretty(expr) == ascii_str
  1170. assert upretty(expr) == ucode_str
  1171. expr = ModAugmentedAssignment(x, y)
  1172. ascii_str = \
  1173. """\
  1174. x %= y\
  1175. """
  1176. ucode_str = \
  1177. """\
  1178. x %= y\
  1179. """
  1180. assert pretty(expr) == ascii_str
  1181. assert upretty(expr) == ucode_str
  1182. def test_pretty_rational():
  1183. expr = y*x**-2
  1184. ascii_str = \
  1185. """\
  1186. y \n\
  1187. --\n\
  1188. 2\n\
  1189. x \
  1190. """
  1191. ucode_str = \
  1192. """\
  1193. y \n\
  1194. ──\n\
  1195. 2\n\
  1196. x \
  1197. """
  1198. assert pretty(expr) == ascii_str
  1199. assert upretty(expr) == ucode_str
  1200. expr = y**Rational(3, 2) * x**Rational(-5, 2)
  1201. ascii_str = \
  1202. """\
  1203. 3/2\n\
  1204. y \n\
  1205. ----\n\
  1206. 5/2\n\
  1207. x \
  1208. """
  1209. ucode_str = \
  1210. """\
  1211. 3/2\n\
  1212. y \n\
  1213. ────\n\
  1214. 5/2\n\
  1215. x \
  1216. """
  1217. assert pretty(expr) == ascii_str
  1218. assert upretty(expr) == ucode_str
  1219. expr = sin(x)**3/tan(x)**2
  1220. ascii_str = \
  1221. """\
  1222. 3 \n\
  1223. sin (x)\n\
  1224. -------\n\
  1225. 2 \n\
  1226. tan (x)\
  1227. """
  1228. ucode_str = \
  1229. """\
  1230. 3 \n\
  1231. sin (x)\n\
  1232. ───────\n\
  1233. 2 \n\
  1234. tan (x)\
  1235. """
  1236. assert pretty(expr) == ascii_str
  1237. assert upretty(expr) == ucode_str
  1238. @_both_exp_pow
  1239. def test_pretty_functions():
  1240. """Tests for Abs, conjugate, exp, function braces, and factorial."""
  1241. expr = (2*x + exp(x))
  1242. ascii_str_1 = \
  1243. """\
  1244. x\n\
  1245. 2*x + e \
  1246. """
  1247. ascii_str_2 = \
  1248. """\
  1249. x \n\
  1250. e + 2*x\
  1251. """
  1252. ucode_str_1 = \
  1253. """\
  1254. x\n\
  1255. 2⋅x + ℯ \
  1256. """
  1257. ucode_str_2 = \
  1258. """\
  1259. x \n\
  1260. ℯ + 2⋅x\
  1261. """
  1262. ucode_str_3 = \
  1263. """\
  1264. x \n\
  1265. ℯ + 2⋅x\
  1266. """
  1267. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1268. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  1269. expr = Abs(x)
  1270. ascii_str = \
  1271. """\
  1272. |x|\
  1273. """
  1274. ucode_str = \
  1275. """\
  1276. │x│\
  1277. """
  1278. assert pretty(expr) == ascii_str
  1279. assert upretty(expr) == ucode_str
  1280. expr = Abs(x/(x**2 + 1))
  1281. ascii_str_1 = \
  1282. """\
  1283. | x |\n\
  1284. |------|\n\
  1285. | 2|\n\
  1286. |1 + x |\
  1287. """
  1288. ascii_str_2 = \
  1289. """\
  1290. | x |\n\
  1291. |------|\n\
  1292. | 2 |\n\
  1293. |x + 1|\
  1294. """
  1295. ucode_str_1 = \
  1296. """\
  1297. │ x │\n\
  1298. │──────│\n\
  1299. │ 2│\n\
  1300. │1 + x │\
  1301. """
  1302. ucode_str_2 = \
  1303. """\
  1304. │ x │\n\
  1305. │──────│\n\
  1306. │ 2 │\n\
  1307. │x + 1│\
  1308. """
  1309. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1310. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1311. expr = Abs(1 / (y - Abs(x)))
  1312. ascii_str = \
  1313. """\
  1314. 1 \n\
  1315. ---------\n\
  1316. |y - |x||\
  1317. """
  1318. ucode_str = \
  1319. """\
  1320. 1 \n\
  1321. ─────────\n\
  1322. │y - │x││\
  1323. """
  1324. assert pretty(expr) == ascii_str
  1325. assert upretty(expr) == ucode_str
  1326. n = Symbol('n', integer=True)
  1327. expr = factorial(n)
  1328. ascii_str = \
  1329. """\
  1330. n!\
  1331. """
  1332. ucode_str = \
  1333. """\
  1334. n!\
  1335. """
  1336. assert pretty(expr) == ascii_str
  1337. assert upretty(expr) == ucode_str
  1338. expr = factorial(2*n)
  1339. ascii_str = \
  1340. """\
  1341. (2*n)!\
  1342. """
  1343. ucode_str = \
  1344. """\
  1345. (2⋅n)!\
  1346. """
  1347. assert pretty(expr) == ascii_str
  1348. assert upretty(expr) == ucode_str
  1349. expr = factorial(factorial(factorial(n)))
  1350. ascii_str = \
  1351. """\
  1352. ((n!)!)!\
  1353. """
  1354. ucode_str = \
  1355. """\
  1356. ((n!)!)!\
  1357. """
  1358. assert pretty(expr) == ascii_str
  1359. assert upretty(expr) == ucode_str
  1360. expr = factorial(n + 1)
  1361. ascii_str_1 = \
  1362. """\
  1363. (1 + n)!\
  1364. """
  1365. ascii_str_2 = \
  1366. """\
  1367. (n + 1)!\
  1368. """
  1369. ucode_str_1 = \
  1370. """\
  1371. (1 + n)!\
  1372. """
  1373. ucode_str_2 = \
  1374. """\
  1375. (n + 1)!\
  1376. """
  1377. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1378. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1379. expr = subfactorial(n)
  1380. ascii_str = \
  1381. """\
  1382. !n\
  1383. """
  1384. ucode_str = \
  1385. """\
  1386. !n\
  1387. """
  1388. assert pretty(expr) == ascii_str
  1389. assert upretty(expr) == ucode_str
  1390. expr = subfactorial(2*n)
  1391. ascii_str = \
  1392. """\
  1393. !(2*n)\
  1394. """
  1395. ucode_str = \
  1396. """\
  1397. !(2⋅n)\
  1398. """
  1399. assert pretty(expr) == ascii_str
  1400. assert upretty(expr) == ucode_str
  1401. n = Symbol('n', integer=True)
  1402. expr = factorial2(n)
  1403. ascii_str = \
  1404. """\
  1405. n!!\
  1406. """
  1407. ucode_str = \
  1408. """\
  1409. n!!\
  1410. """
  1411. assert pretty(expr) == ascii_str
  1412. assert upretty(expr) == ucode_str
  1413. expr = factorial2(2*n)
  1414. ascii_str = \
  1415. """\
  1416. (2*n)!!\
  1417. """
  1418. ucode_str = \
  1419. """\
  1420. (2⋅n)!!\
  1421. """
  1422. assert pretty(expr) == ascii_str
  1423. assert upretty(expr) == ucode_str
  1424. expr = factorial2(factorial2(factorial2(n)))
  1425. ascii_str = \
  1426. """\
  1427. ((n!!)!!)!!\
  1428. """
  1429. ucode_str = \
  1430. """\
  1431. ((n!!)!!)!!\
  1432. """
  1433. assert pretty(expr) == ascii_str
  1434. assert upretty(expr) == ucode_str
  1435. expr = factorial2(n + 1)
  1436. ascii_str_1 = \
  1437. """\
  1438. (1 + n)!!\
  1439. """
  1440. ascii_str_2 = \
  1441. """\
  1442. (n + 1)!!\
  1443. """
  1444. ucode_str_1 = \
  1445. """\
  1446. (1 + n)!!\
  1447. """
  1448. ucode_str_2 = \
  1449. """\
  1450. (n + 1)!!\
  1451. """
  1452. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1453. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1454. expr = 2*binomial(n, k)
  1455. ascii_str = \
  1456. """\
  1457. /n\\\n\
  1458. 2*| |\n\
  1459. \\k/\
  1460. """
  1461. ucode_str = \
  1462. """\
  1463. ⎛n⎞\n\
  1464. 2⋅⎜ ⎟\n\
  1465. ⎝k⎠\
  1466. """
  1467. assert pretty(expr) == ascii_str
  1468. assert upretty(expr) == ucode_str
  1469. expr = 2*binomial(2*n, k)
  1470. ascii_str = \
  1471. """\
  1472. /2*n\\\n\
  1473. 2*| |\n\
  1474. \\ k /\
  1475. """
  1476. ucode_str = \
  1477. """\
  1478. ⎛2⋅n⎞\n\
  1479. 2⋅⎜ ⎟\n\
  1480. ⎝ k ⎠\
  1481. """
  1482. assert pretty(expr) == ascii_str
  1483. assert upretty(expr) == ucode_str
  1484. expr = 2*binomial(n**2, k)
  1485. ascii_str = \
  1486. """\
  1487. / 2\\\n\
  1488. |n |\n\
  1489. 2*| |\n\
  1490. \\k /\
  1491. """
  1492. ucode_str = \
  1493. """\
  1494. ⎛ 2⎞\n\
  1495. ⎜n ⎟\n\
  1496. 2⋅⎜ ⎟\n\
  1497. ⎝k ⎠\
  1498. """
  1499. assert pretty(expr) == ascii_str
  1500. assert upretty(expr) == ucode_str
  1501. expr = catalan(n)
  1502. ascii_str = \
  1503. """\
  1504. C \n\
  1505. n\
  1506. """
  1507. ucode_str = \
  1508. """\
  1509. C \n\
  1510. n\
  1511. """
  1512. assert pretty(expr) == ascii_str
  1513. assert upretty(expr) == ucode_str
  1514. expr = catalan(n)
  1515. ascii_str = \
  1516. """\
  1517. C \n\
  1518. n\
  1519. """
  1520. ucode_str = \
  1521. """\
  1522. C \n\
  1523. n\
  1524. """
  1525. assert pretty(expr) == ascii_str
  1526. assert upretty(expr) == ucode_str
  1527. expr = bell(n)
  1528. ascii_str = \
  1529. """\
  1530. B \n\
  1531. n\
  1532. """
  1533. ucode_str = \
  1534. """\
  1535. B \n\
  1536. n\
  1537. """
  1538. assert pretty(expr) == ascii_str
  1539. assert upretty(expr) == ucode_str
  1540. expr = bernoulli(n)
  1541. ascii_str = \
  1542. """\
  1543. B \n\
  1544. n\
  1545. """
  1546. ucode_str = \
  1547. """\
  1548. B \n\
  1549. n\
  1550. """
  1551. assert pretty(expr) == ascii_str
  1552. assert upretty(expr) == ucode_str
  1553. expr = bernoulli(n, x)
  1554. ascii_str = \
  1555. """\
  1556. B (x)\n\
  1557. n \
  1558. """
  1559. ucode_str = \
  1560. """\
  1561. B (x)\n\
  1562. n \
  1563. """
  1564. assert pretty(expr) == ascii_str
  1565. assert upretty(expr) == ucode_str
  1566. expr = fibonacci(n)
  1567. ascii_str = \
  1568. """\
  1569. F \n\
  1570. n\
  1571. """
  1572. ucode_str = \
  1573. """\
  1574. F \n\
  1575. n\
  1576. """
  1577. assert pretty(expr) == ascii_str
  1578. assert upretty(expr) == ucode_str
  1579. expr = lucas(n)
  1580. ascii_str = \
  1581. """\
  1582. L \n\
  1583. n\
  1584. """
  1585. ucode_str = \
  1586. """\
  1587. L \n\
  1588. n\
  1589. """
  1590. assert pretty(expr) == ascii_str
  1591. assert upretty(expr) == ucode_str
  1592. expr = tribonacci(n)
  1593. ascii_str = \
  1594. """\
  1595. T \n\
  1596. n\
  1597. """
  1598. ucode_str = \
  1599. """\
  1600. T \n\
  1601. n\
  1602. """
  1603. assert pretty(expr) == ascii_str
  1604. assert upretty(expr) == ucode_str
  1605. expr = stieltjes(n)
  1606. ascii_str = \
  1607. """\
  1608. stieltjes \n\
  1609. n\
  1610. """
  1611. ucode_str = \
  1612. """\
  1613. γ \n\
  1614. n\
  1615. """
  1616. assert pretty(expr) == ascii_str
  1617. assert upretty(expr) == ucode_str
  1618. expr = stieltjes(n, x)
  1619. ascii_str = \
  1620. """\
  1621. stieltjes (x)\n\
  1622. n \
  1623. """
  1624. ucode_str = \
  1625. """\
  1626. γ (x)\n\
  1627. n \
  1628. """
  1629. assert pretty(expr) == ascii_str
  1630. assert upretty(expr) == ucode_str
  1631. expr = mathieuc(x, y, z)
  1632. ascii_str = 'C(x, y, z)'
  1633. ucode_str = 'C(x, y, z)'
  1634. assert pretty(expr) == ascii_str
  1635. assert upretty(expr) == ucode_str
  1636. expr = mathieus(x, y, z)
  1637. ascii_str = 'S(x, y, z)'
  1638. ucode_str = 'S(x, y, z)'
  1639. assert pretty(expr) == ascii_str
  1640. assert upretty(expr) == ucode_str
  1641. expr = mathieucprime(x, y, z)
  1642. ascii_str = "C'(x, y, z)"
  1643. ucode_str = "C'(x, y, z)"
  1644. assert pretty(expr) == ascii_str
  1645. assert upretty(expr) == ucode_str
  1646. expr = mathieusprime(x, y, z)
  1647. ascii_str = "S'(x, y, z)"
  1648. ucode_str = "S'(x, y, z)"
  1649. assert pretty(expr) == ascii_str
  1650. assert upretty(expr) == ucode_str
  1651. expr = conjugate(x)
  1652. ascii_str = \
  1653. """\
  1654. _\n\
  1655. x\
  1656. """
  1657. ucode_str = \
  1658. """\
  1659. _\n\
  1660. x\
  1661. """
  1662. assert pretty(expr) == ascii_str
  1663. assert upretty(expr) == ucode_str
  1664. f = Function('f')
  1665. expr = conjugate(f(x + 1))
  1666. ascii_str_1 = \
  1667. """\
  1668. ________\n\
  1669. f(1 + x)\
  1670. """
  1671. ascii_str_2 = \
  1672. """\
  1673. ________\n\
  1674. f(x + 1)\
  1675. """
  1676. ucode_str_1 = \
  1677. """\
  1678. ________\n\
  1679. f(1 + x)\
  1680. """
  1681. ucode_str_2 = \
  1682. """\
  1683. ________\n\
  1684. f(x + 1)\
  1685. """
  1686. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1687. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1688. expr = f(x)
  1689. ascii_str = \
  1690. """\
  1691. f(x)\
  1692. """
  1693. ucode_str = \
  1694. """\
  1695. f(x)\
  1696. """
  1697. assert pretty(expr) == ascii_str
  1698. assert upretty(expr) == ucode_str
  1699. expr = f(x, y)
  1700. ascii_str = \
  1701. """\
  1702. f(x, y)\
  1703. """
  1704. ucode_str = \
  1705. """\
  1706. f(x, y)\
  1707. """
  1708. assert pretty(expr) == ascii_str
  1709. assert upretty(expr) == ucode_str
  1710. expr = f(x/(y + 1), y)
  1711. ascii_str_1 = \
  1712. """\
  1713. / x \\\n\
  1714. f|-----, y|\n\
  1715. \\1 + y /\
  1716. """
  1717. ascii_str_2 = \
  1718. """\
  1719. / x \\\n\
  1720. f|-----, y|\n\
  1721. \\y + 1 /\
  1722. """
  1723. ucode_str_1 = \
  1724. """\
  1725. ⎛ x ⎞\n\
  1726. f⎜─────, y⎟\n\
  1727. ⎝1 + y ⎠\
  1728. """
  1729. ucode_str_2 = \
  1730. """\
  1731. ⎛ x ⎞\n\
  1732. f⎜─────, y⎟\n\
  1733. ⎝y + 1 ⎠\
  1734. """
  1735. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1736. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1737. expr = f(x**x**x**x**x**x)
  1738. ascii_str = \
  1739. """\
  1740. / / / / / x\\\\\\\\\\
  1741. | | | | \\x /||||
  1742. | | | \\x /|||
  1743. | | \\x /||
  1744. | \\x /|
  1745. f\\x /\
  1746. """
  1747. ucode_str = \
  1748. """\
  1749. ⎛ ⎛ ⎛ ⎛ ⎛ x⎞⎞⎞⎞⎞
  1750. ⎜ ⎜ ⎜ ⎜ ⎝x ⎠⎟⎟⎟⎟
  1751. ⎜ ⎜ ⎜ ⎝x ⎠⎟⎟⎟
  1752. ⎜ ⎜ ⎝x ⎠⎟⎟
  1753. ⎜ ⎝x ⎠⎟
  1754. f⎝x ⎠\
  1755. """
  1756. assert pretty(expr) == ascii_str
  1757. assert upretty(expr) == ucode_str
  1758. expr = sin(x)**2
  1759. ascii_str = \
  1760. """\
  1761. 2 \n\
  1762. sin (x)\
  1763. """
  1764. ucode_str = \
  1765. """\
  1766. 2 \n\
  1767. sin (x)\
  1768. """
  1769. assert pretty(expr) == ascii_str
  1770. assert upretty(expr) == ucode_str
  1771. expr = conjugate(a + b*I)
  1772. ascii_str = \
  1773. """\
  1774. _ _\n\
  1775. a - I*b\
  1776. """
  1777. ucode_str = \
  1778. """\
  1779. _ _\n\
  1780. a - ⅈ⋅b\
  1781. """
  1782. assert pretty(expr) == ascii_str
  1783. assert upretty(expr) == ucode_str
  1784. expr = conjugate(exp(a + b*I))
  1785. ascii_str = \
  1786. """\
  1787. _ _\n\
  1788. a - I*b\n\
  1789. e \
  1790. """
  1791. ucode_str = \
  1792. """\
  1793. _ _\n\
  1794. a - ⅈ⋅b\n\
  1795. ℯ \
  1796. """
  1797. assert pretty(expr) == ascii_str
  1798. assert upretty(expr) == ucode_str
  1799. expr = conjugate( f(1 + conjugate(f(x))) )
  1800. ascii_str_1 = \
  1801. """\
  1802. ___________\n\
  1803. / ____\\\n\
  1804. f\\1 + f(x)/\
  1805. """
  1806. ascii_str_2 = \
  1807. """\
  1808. ___________\n\
  1809. /____ \\\n\
  1810. f\\f(x) + 1/\
  1811. """
  1812. ucode_str_1 = \
  1813. """\
  1814. ___________\n\
  1815. ⎛ ____⎞\n\
  1816. f⎝1 + f(x)⎠\
  1817. """
  1818. ucode_str_2 = \
  1819. """\
  1820. ___________\n\
  1821. ⎛____ ⎞\n\
  1822. f⎝f(x) + 1⎠\
  1823. """
  1824. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1825. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1826. expr = f(x/(y + 1), y)
  1827. ascii_str_1 = \
  1828. """\
  1829. / x \\\n\
  1830. f|-----, y|\n\
  1831. \\1 + y /\
  1832. """
  1833. ascii_str_2 = \
  1834. """\
  1835. / x \\\n\
  1836. f|-----, y|\n\
  1837. \\y + 1 /\
  1838. """
  1839. ucode_str_1 = \
  1840. """\
  1841. ⎛ x ⎞\n\
  1842. f⎜─────, y⎟\n\
  1843. ⎝1 + y ⎠\
  1844. """
  1845. ucode_str_2 = \
  1846. """\
  1847. ⎛ x ⎞\n\
  1848. f⎜─────, y⎟\n\
  1849. ⎝y + 1 ⎠\
  1850. """
  1851. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  1852. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  1853. expr = floor(1 / (y - floor(x)))
  1854. ascii_str = \
  1855. """\
  1856. / 1 \\\n\
  1857. floor|------------|\n\
  1858. \\y - floor(x)/\
  1859. """
  1860. ucode_str = \
  1861. """\
  1862. ⎢ 1 ⎥\n\
  1863. ⎢───────⎥\n\
  1864. ⎣y - ⌊x⌋⎦\
  1865. """
  1866. assert pretty(expr) == ascii_str
  1867. assert upretty(expr) == ucode_str
  1868. expr = ceiling(1 / (y - ceiling(x)))
  1869. ascii_str = \
  1870. """\
  1871. / 1 \\\n\
  1872. ceiling|--------------|\n\
  1873. \\y - ceiling(x)/\
  1874. """
  1875. ucode_str = \
  1876. """\
  1877. ⎡ 1 ⎤\n\
  1878. ⎢───────⎥\n\
  1879. ⎢y - ⌈x⌉⎥\
  1880. """
  1881. assert pretty(expr) == ascii_str
  1882. assert upretty(expr) == ucode_str
  1883. expr = euler(n)
  1884. ascii_str = \
  1885. """\
  1886. E \n\
  1887. n\
  1888. """
  1889. ucode_str = \
  1890. """\
  1891. E \n\
  1892. n\
  1893. """
  1894. assert pretty(expr) == ascii_str
  1895. assert upretty(expr) == ucode_str
  1896. expr = euler(1/(1 + 1/(1 + 1/n)))
  1897. ascii_str = \
  1898. """\
  1899. E \n\
  1900. 1 \n\
  1901. ---------\n\
  1902. 1 \n\
  1903. 1 + -----\n\
  1904. 1\n\
  1905. 1 + -\n\
  1906. n\
  1907. """
  1908. ucode_str = \
  1909. """\
  1910. E \n\
  1911. 1 \n\
  1912. ─────────\n\
  1913. 1 \n\
  1914. 1 + ─────\n\
  1915. 1\n\
  1916. 1 + ─\n\
  1917. n\
  1918. """
  1919. assert pretty(expr) == ascii_str
  1920. assert upretty(expr) == ucode_str
  1921. expr = euler(n, x)
  1922. ascii_str = \
  1923. """\
  1924. E (x)\n\
  1925. n \
  1926. """
  1927. ucode_str = \
  1928. """\
  1929. E (x)\n\
  1930. n \
  1931. """
  1932. assert pretty(expr) == ascii_str
  1933. assert upretty(expr) == ucode_str
  1934. expr = euler(n, x/2)
  1935. ascii_str = \
  1936. """\
  1937. /x\\\n\
  1938. E |-|\n\
  1939. n\\2/\
  1940. """
  1941. ucode_str = \
  1942. """\
  1943. ⎛x⎞\n\
  1944. E ⎜─⎟\n\
  1945. n⎝2⎠\
  1946. """
  1947. assert pretty(expr) == ascii_str
  1948. assert upretty(expr) == ucode_str
  1949. def test_pretty_sqrt():
  1950. expr = sqrt(2)
  1951. ascii_str = \
  1952. """\
  1953. ___\n\
  1954. \\/ 2 \
  1955. """
  1956. ucode_str = \
  1957. "√2"
  1958. assert pretty(expr) == ascii_str
  1959. assert upretty(expr) == ucode_str
  1960. expr = 2**Rational(1, 3)
  1961. ascii_str = \
  1962. """\
  1963. 3 ___\n\
  1964. \\/ 2 \
  1965. """
  1966. ucode_str = \
  1967. """\
  1968. 3 ___\n\
  1969. ╲╱ 2 \
  1970. """
  1971. assert pretty(expr) == ascii_str
  1972. assert upretty(expr) == ucode_str
  1973. expr = 2**Rational(1, 1000)
  1974. ascii_str = \
  1975. """\
  1976. 1000___\n\
  1977. \\/ 2 \
  1978. """
  1979. ucode_str = \
  1980. """\
  1981. 1000___\n\
  1982. ╲╱ 2 \
  1983. """
  1984. assert pretty(expr) == ascii_str
  1985. assert upretty(expr) == ucode_str
  1986. expr = sqrt(x**2 + 1)
  1987. ascii_str = \
  1988. """\
  1989. ________\n\
  1990. / 2 \n\
  1991. \\/ x + 1 \
  1992. """
  1993. ucode_str = \
  1994. """\
  1995. ________\n\
  1996. ╱ 2 \n\
  1997. ╲╱ x + 1 \
  1998. """
  1999. assert pretty(expr) == ascii_str
  2000. assert upretty(expr) == ucode_str
  2001. expr = (1 + sqrt(5))**Rational(1, 3)
  2002. ascii_str = \
  2003. """\
  2004. ___________\n\
  2005. 3 / ___ \n\
  2006. \\/ 1 + \\/ 5 \
  2007. """
  2008. ucode_str = \
  2009. """\
  2010. 3 ________\n\
  2011. ╲╱ 1 + √5 \
  2012. """
  2013. assert pretty(expr) == ascii_str
  2014. assert upretty(expr) == ucode_str
  2015. expr = 2**(1/x)
  2016. ascii_str = \
  2017. """\
  2018. x ___\n\
  2019. \\/ 2 \
  2020. """
  2021. ucode_str = \
  2022. """\
  2023. x ___\n\
  2024. ╲╱ 2 \
  2025. """
  2026. assert pretty(expr) == ascii_str
  2027. assert upretty(expr) == ucode_str
  2028. expr = sqrt(2 + pi)
  2029. ascii_str = \
  2030. """\
  2031. ________\n\
  2032. \\/ 2 + pi \
  2033. """
  2034. ucode_str = \
  2035. """\
  2036. _______\n\
  2037. ╲╱ 2 + π \
  2038. """
  2039. assert pretty(expr) == ascii_str
  2040. assert upretty(expr) == ucode_str
  2041. expr = (2 + (
  2042. 1 + x**2)/(2 + x))**Rational(1, 4) + (1 + x**Rational(1, 1000))/sqrt(3 + x**2)
  2043. ascii_str = \
  2044. """\
  2045. ____________ \n\
  2046. / 2 1000___ \n\
  2047. / x + 1 \\/ x + 1\n\
  2048. 4 / 2 + ------ + -----------\n\
  2049. \\/ x + 2 ________\n\
  2050. / 2 \n\
  2051. \\/ x + 3 \
  2052. """
  2053. ucode_str = \
  2054. """\
  2055. ____________ \n\
  2056. ╱ 2 1000___ \n\
  2057. ╱ x + 1 ╲╱ x + 1\n\
  2058. 4 ╱ 2 + ────── + ───────────\n\
  2059. ╲╱ x + 2 ________\n\
  2060. ╱ 2 \n\
  2061. ╲╱ x + 3 \
  2062. """
  2063. assert pretty(expr) == ascii_str
  2064. assert upretty(expr) == ucode_str
  2065. def test_pretty_sqrt_char_knob():
  2066. # See PR #9234.
  2067. expr = sqrt(2)
  2068. ucode_str1 = \
  2069. """\
  2070. ___\n\
  2071. ╲╱ 2 \
  2072. """
  2073. ucode_str2 = \
  2074. "√2"
  2075. assert xpretty(expr, use_unicode=True,
  2076. use_unicode_sqrt_char=False) == ucode_str1
  2077. assert xpretty(expr, use_unicode=True,
  2078. use_unicode_sqrt_char=True) == ucode_str2
  2079. def test_pretty_sqrt_longsymbol_no_sqrt_char():
  2080. # Do not use unicode sqrt char for long symbols (see PR #9234).
  2081. expr = sqrt(Symbol('C1'))
  2082. ucode_str = \
  2083. """\
  2084. ____\n\
  2085. ╲╱ C₁ \
  2086. """
  2087. assert upretty(expr) == ucode_str
  2088. def test_pretty_KroneckerDelta():
  2089. x, y = symbols("x, y")
  2090. expr = KroneckerDelta(x, y)
  2091. ascii_str = \
  2092. """\
  2093. d \n\
  2094. x,y\
  2095. """
  2096. ucode_str = \
  2097. """\
  2098. δ \n\
  2099. x,y\
  2100. """
  2101. assert pretty(expr) == ascii_str
  2102. assert upretty(expr) == ucode_str
  2103. def test_pretty_product():
  2104. n, m, k, l = symbols('n m k l')
  2105. f = symbols('f', cls=Function)
  2106. expr = Product(f((n/3)**2), (n, k**2, l))
  2107. unicode_str = \
  2108. """\
  2109. l \n\
  2110. ─┬──────┬─ \n\
  2111. │ │ ⎛ 2⎞\n\
  2112. │ │ ⎜n ⎟\n\
  2113. │ │ f⎜──⎟\n\
  2114. │ │ ⎝9 ⎠\n\
  2115. │ │ \n\
  2116. 2 \n\
  2117. n = k """
  2118. ascii_str = \
  2119. """\
  2120. l \n\
  2121. __________ \n\
  2122. | | / 2\\\n\
  2123. | | |n |\n\
  2124. | | f|--|\n\
  2125. | | \\9 /\n\
  2126. | | \n\
  2127. 2 \n\
  2128. n = k """
  2129. expr = Product(f((n/3)**2), (n, k**2, l), (l, 1, m))
  2130. unicode_str = \
  2131. """\
  2132. m l \n\
  2133. ─┬──────┬─ ─┬──────┬─ \n\
  2134. │ │ │ │ ⎛ 2⎞\n\
  2135. │ │ │ │ ⎜n ⎟\n\
  2136. │ │ │ │ f⎜──⎟\n\
  2137. │ │ │ │ ⎝9 ⎠\n\
  2138. │ │ │ │ \n\
  2139. l = 1 2 \n\
  2140. n = k """
  2141. ascii_str = \
  2142. """\
  2143. m l \n\
  2144. __________ __________ \n\
  2145. | | | | / 2\\\n\
  2146. | | | | |n |\n\
  2147. | | | | f|--|\n\
  2148. | | | | \\9 /\n\
  2149. | | | | \n\
  2150. l = 1 2 \n\
  2151. n = k """
  2152. assert pretty(expr) == ascii_str
  2153. assert upretty(expr) == unicode_str
  2154. def test_pretty_Lambda():
  2155. # S.IdentityFunction is a special case
  2156. expr = Lambda(y, y)
  2157. assert pretty(expr) == "x -> x"
  2158. assert upretty(expr) == "x ↦ x"
  2159. expr = Lambda(x, x+1)
  2160. assert pretty(expr) == "x -> x + 1"
  2161. assert upretty(expr) == "x ↦ x + 1"
  2162. expr = Lambda(x, x**2)
  2163. ascii_str = \
  2164. """\
  2165. 2\n\
  2166. x -> x \
  2167. """
  2168. ucode_str = \
  2169. """\
  2170. 2\n\
  2171. x ↦ x \
  2172. """
  2173. assert pretty(expr) == ascii_str
  2174. assert upretty(expr) == ucode_str
  2175. expr = Lambda(x, x**2)**2
  2176. ascii_str = \
  2177. """\
  2178. 2
  2179. / 2\\ \n\
  2180. \\x -> x / \
  2181. """
  2182. ucode_str = \
  2183. """\
  2184. 2
  2185. ⎛ 2⎞ \n\
  2186. ⎝x ↦ x ⎠ \
  2187. """
  2188. assert pretty(expr) == ascii_str
  2189. assert upretty(expr) == ucode_str
  2190. expr = Lambda((x, y), x)
  2191. ascii_str = "(x, y) -> x"
  2192. ucode_str = "(x, y) ↦ x"
  2193. assert pretty(expr) == ascii_str
  2194. assert upretty(expr) == ucode_str
  2195. expr = Lambda((x, y), x**2)
  2196. ascii_str = \
  2197. """\
  2198. 2\n\
  2199. (x, y) -> x \
  2200. """
  2201. ucode_str = \
  2202. """\
  2203. 2\n\
  2204. (x, y) ↦ x \
  2205. """
  2206. assert pretty(expr) == ascii_str
  2207. assert upretty(expr) == ucode_str
  2208. expr = Lambda(((x, y),), x**2)
  2209. ascii_str = \
  2210. """\
  2211. 2\n\
  2212. ((x, y),) -> x \
  2213. """
  2214. ucode_str = \
  2215. """\
  2216. 2\n\
  2217. ((x, y),) ↦ x \
  2218. """
  2219. assert pretty(expr) == ascii_str
  2220. assert upretty(expr) == ucode_str
  2221. def test_pretty_TransferFunction():
  2222. tf1 = TransferFunction(s - 1, s + 1, s)
  2223. assert upretty(tf1) == "s - 1\n─────\ns + 1"
  2224. tf2 = TransferFunction(2*s + 1, 3 - p, s)
  2225. assert upretty(tf2) == "2⋅s + 1\n───────\n 3 - p "
  2226. tf3 = TransferFunction(p, p + 1, p)
  2227. assert upretty(tf3) == " p \n─────\np + 1"
  2228. def test_pretty_Series():
  2229. tf1 = TransferFunction(x + y, x - 2*y, y)
  2230. tf2 = TransferFunction(x - y, x + y, y)
  2231. tf3 = TransferFunction(x**2 + y, y - x, y)
  2232. tf4 = TransferFunction(2, 3, y)
  2233. tfm1 = TransferFunctionMatrix([[tf1, tf2], [tf3, tf4]])
  2234. tfm2 = TransferFunctionMatrix([[tf3], [-tf4]])
  2235. tfm3 = TransferFunctionMatrix([[tf1, -tf2, -tf3], [tf3, -tf4, tf2]])
  2236. tfm4 = TransferFunctionMatrix([[tf1, tf2], [tf3, -tf4], [-tf2, -tf1]])
  2237. tfm5 = TransferFunctionMatrix([[-tf2, -tf1], [tf4, -tf3], [tf1, tf2]])
  2238. expected1 = \
  2239. """\
  2240. ⎛ 2 ⎞\n\
  2241. ⎛ x + y ⎞ ⎜x + y⎟\n\
  2242. ⎜───────⎟⋅⎜──────⎟\n\
  2243. ⎝x - 2⋅y⎠ ⎝-x + y⎠\
  2244. """
  2245. expected2 = \
  2246. """\
  2247. ⎛-x + y⎞ ⎛ -x - y⎞\n\
  2248. ⎜──────⎟⋅⎜───────⎟\n\
  2249. ⎝x + y ⎠ ⎝x - 2⋅y⎠\
  2250. """
  2251. expected3 = \
  2252. """\
  2253. ⎛ 2 ⎞ \n\
  2254. ⎜x + y⎟ ⎛ x + y ⎞ ⎛ -x - y x - y⎞\n\
  2255. ⎜──────⎟⋅⎜───────⎟⋅⎜─────── + ─────⎟\n\
  2256. ⎝-x + y⎠ ⎝x - 2⋅y⎠ ⎝x - 2⋅y x + y⎠\
  2257. """
  2258. expected4 = \
  2259. """\
  2260. ⎛ 2 ⎞\n\
  2261. ⎛ x + y x - y⎞ ⎜x - y x + y⎟\n\
  2262. ⎜─────── + ─────⎟⋅⎜───── + ──────⎟\n\
  2263. ⎝x - 2⋅y x + y⎠ ⎝x + y -x + y⎠\
  2264. """
  2265. expected5 = \
  2266. """\
  2267. ⎡ x + y x - y⎤ ⎡ 2 ⎤ \n\
  2268. ⎢─────── ─────⎥ ⎢x + y⎥ \n\
  2269. ⎢x - 2⋅y x + y⎥ ⎢──────⎥ \n\
  2270. ⎢ ⎥ ⎢-x + y⎥ \n\
  2271. ⎢ 2 ⎥ ⋅⎢ ⎥ \n\
  2272. ⎢x + y 2 ⎥ ⎢ -2 ⎥ \n\
  2273. ⎢────── ─ ⎥ ⎢ ─── ⎥ \n\
  2274. ⎣-x + y 3 ⎦τ ⎣ 3 ⎦τ\
  2275. """
  2276. expected6 = \
  2277. """\
  2278. ⎛⎡ x + y x - y ⎤ ⎡ x - y x + y ⎤ ⎞\n\
  2279. ⎜⎢─────── ───── ⎥ ⎢ ───── ───────⎥ ⎟\n\
  2280. ⎡ x + y x - y⎤ ⎡ 2 ⎤ ⎜⎢x - 2⋅y x + y ⎥ ⎢ x + y x - 2⋅y⎥ ⎟\n\
  2281. ⎢─────── ─────⎥ ⎢ x + y -x + y - x - y⎥ ⎜⎢ ⎥ ⎢ ⎥ ⎟\n\
  2282. ⎢x - 2⋅y x + y⎥ ⎢─────── ────── ────────⎥ ⎜⎢ 2 ⎥ ⎢ 2 ⎥ ⎟\n\
  2283. ⎢ ⎥ ⎢x - 2⋅y x + y -x + y ⎥ ⎜⎢x + y -2 ⎥ ⎢ -2 x + y ⎥ ⎟\n\
  2284. ⎢ 2 ⎥ ⋅⎢ ⎥ ⋅⎜⎢────── ─── ⎥ + ⎢ ─── ────── ⎥ ⎟\n\
  2285. ⎢x + y 2 ⎥ ⎢ 2 ⎥ ⎜⎢-x + y 3 ⎥ ⎢ 3 -x + y ⎥ ⎟\n\
  2286. ⎢────── ─ ⎥ ⎢x + y -2 x - y ⎥ ⎜⎢ ⎥ ⎢ ⎥ ⎟\n\
  2287. ⎣-x + y 3 ⎦τ ⎢────── ─── ───── ⎥ ⎜⎢-x + y -x - y⎥ ⎢ -x - y -x + y ⎥ ⎟\n\
  2288. ⎣-x + y 3 x + y ⎦τ ⎜⎢────── ───────⎥ ⎢─────── ────── ⎥ ⎟\n\
  2289. ⎝⎣x + y x - 2⋅y⎦τ ⎣x - 2⋅y x + y ⎦τ⎠\
  2290. """
  2291. assert upretty(Series(tf1, tf3)) == expected1
  2292. assert upretty(Series(-tf2, -tf1)) == expected2
  2293. assert upretty(Series(tf3, tf1, Parallel(-tf1, tf2))) == expected3
  2294. assert upretty(Series(Parallel(tf1, tf2), Parallel(tf2, tf3))) == expected4
  2295. assert upretty(MIMOSeries(tfm2, tfm1)) == expected5
  2296. assert upretty(MIMOSeries(MIMOParallel(tfm4, -tfm5), tfm3, tfm1)) == expected6
  2297. def test_pretty_Parallel():
  2298. tf1 = TransferFunction(x + y, x - 2*y, y)
  2299. tf2 = TransferFunction(x - y, x + y, y)
  2300. tf3 = TransferFunction(x**2 + y, y - x, y)
  2301. tf4 = TransferFunction(y**2 - x, x**3 + x, y)
  2302. tfm1 = TransferFunctionMatrix([[tf1, tf2], [tf3, -tf4], [-tf2, -tf1]])
  2303. tfm2 = TransferFunctionMatrix([[-tf2, -tf1], [tf4, -tf3], [tf1, tf2]])
  2304. tfm3 = TransferFunctionMatrix([[-tf1, tf2], [-tf3, tf4], [tf2, tf1]])
  2305. tfm4 = TransferFunctionMatrix([[-tf1, -tf2], [-tf3, -tf4]])
  2306. expected1 = \
  2307. """\
  2308. x + y x - y\n\
  2309. ─────── + ─────\n\
  2310. x - 2⋅y x + y\
  2311. """
  2312. expected2 = \
  2313. """\
  2314. -x + y -x - y\n\
  2315. ────── + ───────\n\
  2316. x + y x - 2⋅y\
  2317. """
  2318. expected3 = \
  2319. """\
  2320. 2 \n\
  2321. x + y x + y ⎛ -x - y⎞ ⎛x - y⎞\n\
  2322. ────── + ─────── + ⎜───────⎟⋅⎜─────⎟\n\
  2323. -x + y x - 2⋅y ⎝x - 2⋅y⎠ ⎝x + y⎠\
  2324. """
  2325. expected4 = \
  2326. """\
  2327. ⎛ 2 ⎞\n\
  2328. ⎛ x + y ⎞ ⎛x - y⎞ ⎛x - y⎞ ⎜x + y⎟\n\
  2329. ⎜───────⎟⋅⎜─────⎟ + ⎜─────⎟⋅⎜──────⎟\n\
  2330. ⎝x - 2⋅y⎠ ⎝x + y⎠ ⎝x + y⎠ ⎝-x + y⎠\
  2331. """
  2332. expected5 = \
  2333. """\
  2334. ⎡ x + y -x + y ⎤ ⎡ x - y x + y ⎤ ⎡ x + y x - y ⎤ \n\
  2335. ⎢─────── ────── ⎥ ⎢ ───── ───────⎥ ⎢─────── ───── ⎥ \n\
  2336. ⎢x - 2⋅y x + y ⎥ ⎢ x + y x - 2⋅y⎥ ⎢x - 2⋅y x + y ⎥ \n\
  2337. ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ \n\
  2338. ⎢ 2 2 ⎥ ⎢ 2 2 ⎥ ⎢ 2 2 ⎥ \n\
  2339. ⎢x + y x - y ⎥ ⎢x - y x + y ⎥ ⎢x + y x - y ⎥ \n\
  2340. ⎢────── ────── ⎥ + ⎢────── ────── ⎥ + ⎢────── ────── ⎥ \n\
  2341. ⎢-x + y 3 ⎥ ⎢ 3 -x + y ⎥ ⎢-x + y 3 ⎥ \n\
  2342. ⎢ x + x ⎥ ⎢x + x ⎥ ⎢ x + x ⎥ \n\
  2343. ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ \n\
  2344. ⎢-x + y -x - y⎥ ⎢ -x - y -x + y ⎥ ⎢-x + y -x - y⎥ \n\
  2345. ⎢────── ───────⎥ ⎢─────── ────── ⎥ ⎢────── ───────⎥ \n\
  2346. ⎣x + y x - 2⋅y⎦τ ⎣x - 2⋅y x + y ⎦τ ⎣x + y x - 2⋅y⎦τ\
  2347. """
  2348. expected6 = \
  2349. """\
  2350. ⎡ x - y x + y ⎤ ⎡-x + y -x - y ⎤ \n\
  2351. ⎢ ───── ───────⎥ ⎢────── ─────── ⎥ \n\
  2352. ⎢ x + y x - 2⋅y⎥ ⎡ -x - y -x + y⎤ ⎢x + y x - 2⋅y ⎥ \n\
  2353. ⎢ ⎥ ⎢─────── ──────⎥ ⎢ ⎥ \n\
  2354. ⎢ 2 2 ⎥ ⎢x - 2⋅y x + y ⎥ ⎢ 2 2 ⎥ \n\
  2355. ⎢x - y x + y ⎥ ⎢ ⎥ ⎢-x + y - x - y⎥ \n\
  2356. ⎢────── ────── ⎥ ⋅⎢ 2 2⎥ + ⎢─────── ────────⎥ \n\
  2357. ⎢ 3 -x + y ⎥ ⎢- x - y x - y ⎥ ⎢ 3 -x + y ⎥ \n\
  2358. ⎢x + x ⎥ ⎢──────── ──────⎥ ⎢ x + x ⎥ \n\
  2359. ⎢ ⎥ ⎢ -x + y 3 ⎥ ⎢ ⎥ \n\
  2360. ⎢ -x - y -x + y ⎥ ⎣ x + x⎦τ ⎢ x + y x - y ⎥ \n\
  2361. ⎢─────── ────── ⎥ ⎢─────── ───── ⎥ \n\
  2362. ⎣x - 2⋅y x + y ⎦τ ⎣x - 2⋅y x + y ⎦τ\
  2363. """
  2364. assert upretty(Parallel(tf1, tf2)) == expected1
  2365. assert upretty(Parallel(-tf2, -tf1)) == expected2
  2366. assert upretty(Parallel(tf3, tf1, Series(-tf1, tf2))) == expected3
  2367. assert upretty(Parallel(Series(tf1, tf2), Series(tf2, tf3))) == expected4
  2368. assert upretty(MIMOParallel(-tfm3, -tfm2, tfm1)) == expected5
  2369. assert upretty(MIMOParallel(MIMOSeries(tfm4, -tfm2), tfm2)) == expected6
  2370. def test_pretty_Feedback():
  2371. tf = TransferFunction(1, 1, y)
  2372. tf1 = TransferFunction(x + y, x - 2*y, y)
  2373. tf2 = TransferFunction(x - y, x + y, y)
  2374. tf3 = TransferFunction(y**2 - 2*y + 1, y + 5, y)
  2375. tf4 = TransferFunction(x - 2*y**3, x + y, x)
  2376. tf5 = TransferFunction(1 - x, x - y, y)
  2377. tf6 = TransferFunction(2, 2, x)
  2378. expected1 = \
  2379. """\
  2380. ⎛1⎞ \n\
  2381. ⎜─⎟ \n\
  2382. ⎝1⎠ \n\
  2383. ─────────────\n\
  2384. 1 ⎛ x + y ⎞\n\
  2385. ─ + ⎜───────⎟\n\
  2386. 1 ⎝x - 2⋅y⎠\
  2387. """
  2388. expected2 = \
  2389. """\
  2390. ⎛1⎞ \n\
  2391. ⎜─⎟ \n\
  2392. ⎝1⎠ \n\
  2393. ────────────────────────────────────\n\
  2394. ⎛ 2 ⎞\n\
  2395. 1 ⎛x - y⎞ ⎛ x + y ⎞ ⎜y - 2⋅y + 1⎟\n\
  2396. ─ + ⎜─────⎟⋅⎜───────⎟⋅⎜────────────⎟\n\
  2397. 1 ⎝x + y⎠ ⎝x - 2⋅y⎠ ⎝ y + 5 ⎠\
  2398. """
  2399. expected3 = \
  2400. """\
  2401. ⎛ x + y ⎞ \n\
  2402. ⎜───────⎟ \n\
  2403. ⎝x - 2⋅y⎠ \n\
  2404. ────────────────────────────────────────────\n\
  2405. ⎛ 2 ⎞ \n\
  2406. 1 ⎛ x + y ⎞ ⎛x - y⎞ ⎜y - 2⋅y + 1⎟ ⎛1 - x⎞\n\
  2407. ─ + ⎜───────⎟⋅⎜─────⎟⋅⎜────────────⎟⋅⎜─────⎟\n\
  2408. 1 ⎝x - 2⋅y⎠ ⎝x + y⎠ ⎝ y + 5 ⎠ ⎝x - y⎠\
  2409. """
  2410. expected4 = \
  2411. """\
  2412. ⎛ x + y ⎞ ⎛x - y⎞ \n\
  2413. ⎜───────⎟⋅⎜─────⎟ \n\
  2414. ⎝x - 2⋅y⎠ ⎝x + y⎠ \n\
  2415. ─────────────────────\n\
  2416. 1 ⎛ x + y ⎞ ⎛x - y⎞\n\
  2417. ─ + ⎜───────⎟⋅⎜─────⎟\n\
  2418. 1 ⎝x - 2⋅y⎠ ⎝x + y⎠\
  2419. """
  2420. expected5 = \
  2421. """\
  2422. ⎛ x + y ⎞ ⎛x - y⎞ \n\
  2423. ⎜───────⎟⋅⎜─────⎟ \n\
  2424. ⎝x - 2⋅y⎠ ⎝x + y⎠ \n\
  2425. ─────────────────────────────\n\
  2426. 1 ⎛ x + y ⎞ ⎛x - y⎞ ⎛1 - x⎞\n\
  2427. ─ + ⎜───────⎟⋅⎜─────⎟⋅⎜─────⎟\n\
  2428. 1 ⎝x - 2⋅y⎠ ⎝x + y⎠ ⎝x - y⎠\
  2429. """
  2430. expected6 = \
  2431. """\
  2432. ⎛ 2 ⎞ \n\
  2433. ⎜y - 2⋅y + 1⎟ ⎛1 - x⎞ \n\
  2434. ⎜────────────⎟⋅⎜─────⎟ \n\
  2435. ⎝ y + 5 ⎠ ⎝x - y⎠ \n\
  2436. ────────────────────────────────────────────\n\
  2437. ⎛ 2 ⎞ \n\
  2438. 1 ⎜y - 2⋅y + 1⎟ ⎛1 - x⎞ ⎛x - y⎞ ⎛ x + y ⎞\n\
  2439. ─ + ⎜────────────⎟⋅⎜─────⎟⋅⎜─────⎟⋅⎜───────⎟\n\
  2440. 1 ⎝ y + 5 ⎠ ⎝x - y⎠ ⎝x + y⎠ ⎝x - 2⋅y⎠\
  2441. """
  2442. expected7 = \
  2443. """\
  2444. ⎛ 3⎞ \n\
  2445. ⎜x - 2⋅y ⎟ \n\
  2446. ⎜────────⎟ \n\
  2447. ⎝ x + y ⎠ \n\
  2448. ──────────────────\n\
  2449. ⎛ 3⎞ \n\
  2450. 1 ⎜x - 2⋅y ⎟ ⎛2⎞\n\
  2451. ─ + ⎜────────⎟⋅⎜─⎟\n\
  2452. 1 ⎝ x + y ⎠ ⎝2⎠\
  2453. """
  2454. expected8 = \
  2455. """\
  2456. ⎛1 - x⎞ \n\
  2457. ⎜─────⎟ \n\
  2458. ⎝x - y⎠ \n\
  2459. ───────────\n\
  2460. 1 ⎛1 - x⎞\n\
  2461. ─ + ⎜─────⎟\n\
  2462. 1 ⎝x - y⎠\
  2463. """
  2464. expected9 = \
  2465. """\
  2466. ⎛ x + y ⎞ ⎛x - y⎞ \n\
  2467. ⎜───────⎟⋅⎜─────⎟ \n\
  2468. ⎝x - 2⋅y⎠ ⎝x + y⎠ \n\
  2469. ─────────────────────────────\n\
  2470. 1 ⎛ x + y ⎞ ⎛x - y⎞ ⎛1 - x⎞\n\
  2471. ─ - ⎜───────⎟⋅⎜─────⎟⋅⎜─────⎟\n\
  2472. 1 ⎝x - 2⋅y⎠ ⎝x + y⎠ ⎝x - y⎠\
  2473. """
  2474. expected10 = \
  2475. """\
  2476. ⎛1 - x⎞ \n\
  2477. ⎜─────⎟ \n\
  2478. ⎝x - y⎠ \n\
  2479. ───────────\n\
  2480. 1 ⎛1 - x⎞\n\
  2481. ─ - ⎜─────⎟\n\
  2482. 1 ⎝x - y⎠\
  2483. """
  2484. assert upretty(Feedback(tf, tf1)) == expected1
  2485. assert upretty(Feedback(tf, tf2*tf1*tf3)) == expected2
  2486. assert upretty(Feedback(tf1, tf2*tf3*tf5)) == expected3
  2487. assert upretty(Feedback(tf1*tf2, tf)) == expected4
  2488. assert upretty(Feedback(tf1*tf2, tf5)) == expected5
  2489. assert upretty(Feedback(tf3*tf5, tf2*tf1)) == expected6
  2490. assert upretty(Feedback(tf4, tf6)) == expected7
  2491. assert upretty(Feedback(tf5, tf)) == expected8
  2492. assert upretty(Feedback(tf1*tf2, tf5, 1)) == expected9
  2493. assert upretty(Feedback(tf5, tf, 1)) == expected10
  2494. def test_pretty_MIMOFeedback():
  2495. tf1 = TransferFunction(x + y, x - 2*y, y)
  2496. tf2 = TransferFunction(x - y, x + y, y)
  2497. tfm_1 = TransferFunctionMatrix([[tf1, tf2], [tf2, tf1]])
  2498. tfm_2 = TransferFunctionMatrix([[tf2, tf1], [tf1, tf2]])
  2499. tfm_3 = TransferFunctionMatrix([[tf1, tf1], [tf2, tf2]])
  2500. expected1 = \
  2501. """\
  2502. ⎛ ⎡ x + y x - y ⎤ ⎡ x - y x + y ⎤ ⎞-1 ⎡ x + y x - y ⎤ \n\
  2503. ⎜ ⎢─────── ───── ⎥ ⎢ ───── ───────⎥ ⎟ ⎢─────── ───── ⎥ \n\
  2504. ⎜ ⎢x - 2⋅y x + y ⎥ ⎢ x + y x - 2⋅y⎥ ⎟ ⎢x - 2⋅y x + y ⎥ \n\
  2505. ⎜I - ⎢ ⎥ ⋅⎢ ⎥ ⎟ ⋅ ⎢ ⎥ \n\
  2506. ⎜ ⎢ x - y x + y ⎥ ⎢ x + y x - y ⎥ ⎟ ⎢ x - y x + y ⎥ \n\
  2507. ⎜ ⎢ ───── ───────⎥ ⎢─────── ───── ⎥ ⎟ ⎢ ───── ───────⎥ \n\
  2508. ⎝ ⎣ x + y x - 2⋅y⎦τ ⎣x - 2⋅y x + y ⎦τ⎠ ⎣ x + y x - 2⋅y⎦τ\
  2509. """
  2510. expected2 = \
  2511. """\
  2512. ⎛ ⎡ x + y x - y ⎤ ⎡ x - y x + y ⎤ ⎡ x + y x + y ⎤ ⎞-1 ⎡ x + y x - y ⎤ ⎡ x - y x + y ⎤ \n\
  2513. ⎜ ⎢─────── ───── ⎥ ⎢ ───── ───────⎥ ⎢─────── ───────⎥ ⎟ ⎢─────── ───── ⎥ ⎢ ───── ───────⎥ \n\
  2514. ⎜ ⎢x - 2⋅y x + y ⎥ ⎢ x + y x - 2⋅y⎥ ⎢x - 2⋅y x - 2⋅y⎥ ⎟ ⎢x - 2⋅y x + y ⎥ ⎢ x + y x - 2⋅y⎥ \n\
  2515. ⎜I + ⎢ ⎥ ⋅⎢ ⎥ ⋅⎢ ⎥ ⎟ ⋅ ⎢ ⎥ ⋅⎢ ⎥ \n\
  2516. ⎜ ⎢ x - y x + y ⎥ ⎢ x + y x - y ⎥ ⎢ x - y x - y ⎥ ⎟ ⎢ x - y x + y ⎥ ⎢ x + y x - y ⎥ \n\
  2517. ⎜ ⎢ ───── ───────⎥ ⎢─────── ───── ⎥ ⎢ ───── ───── ⎥ ⎟ ⎢ ───── ───────⎥ ⎢─────── ───── ⎥ \n\
  2518. ⎝ ⎣ x + y x - 2⋅y⎦τ ⎣x - 2⋅y x + y ⎦τ ⎣ x + y x + y ⎦τ⎠ ⎣ x + y x - 2⋅y⎦τ ⎣x - 2⋅y x + y ⎦τ\
  2519. """
  2520. assert upretty(MIMOFeedback(tfm_1, tfm_2, 1)) == \
  2521. expected1 # Positive MIMOFeedback
  2522. assert upretty(MIMOFeedback(tfm_1*tfm_2, tfm_3)) == \
  2523. expected2 # Negative MIMOFeedback (Default)
  2524. def test_pretty_TransferFunctionMatrix():
  2525. tf1 = TransferFunction(x + y, x - 2*y, y)
  2526. tf2 = TransferFunction(x - y, x + y, y)
  2527. tf3 = TransferFunction(y**2 - 2*y + 1, y + 5, y)
  2528. tf4 = TransferFunction(y, x**2 + x + 1, y)
  2529. tf5 = TransferFunction(1 - x, x - y, y)
  2530. tf6 = TransferFunction(2, 2, y)
  2531. expected1 = \
  2532. """\
  2533. ⎡ x + y ⎤ \n\
  2534. ⎢───────⎥ \n\
  2535. ⎢x - 2⋅y⎥ \n\
  2536. ⎢ ⎥ \n\
  2537. ⎢ x - y ⎥ \n\
  2538. ⎢ ───── ⎥ \n\
  2539. ⎣ x + y ⎦τ\
  2540. """
  2541. expected2 = \
  2542. """\
  2543. ⎡ x + y ⎤ \n\
  2544. ⎢ ─────── ⎥ \n\
  2545. ⎢ x - 2⋅y ⎥ \n\
  2546. ⎢ ⎥ \n\
  2547. ⎢ x - y ⎥ \n\
  2548. ⎢ ───── ⎥ \n\
  2549. ⎢ x + y ⎥ \n\
  2550. ⎢ ⎥ \n\
  2551. ⎢ 2 ⎥ \n\
  2552. ⎢- y + 2⋅y - 1⎥ \n\
  2553. ⎢──────────────⎥ \n\
  2554. ⎣ y + 5 ⎦τ\
  2555. """
  2556. expected3 = \
  2557. """\
  2558. ⎡ x + y x - y ⎤ \n\
  2559. ⎢ ─────── ───── ⎥ \n\
  2560. ⎢ x - 2⋅y x + y ⎥ \n\
  2561. ⎢ ⎥ \n\
  2562. ⎢ 2 ⎥ \n\
  2563. ⎢y - 2⋅y + 1 y ⎥ \n\
  2564. ⎢──────────── ──────────⎥ \n\
  2565. ⎢ y + 5 2 ⎥ \n\
  2566. ⎢ x + x + 1⎥ \n\
  2567. ⎢ ⎥ \n\
  2568. ⎢ 1 - x 2 ⎥ \n\
  2569. ⎢ ───── ─ ⎥ \n\
  2570. ⎣ x - y 2 ⎦τ\
  2571. """
  2572. expected4 = \
  2573. """\
  2574. ⎡ x - y x + y y ⎤ \n\
  2575. ⎢ ───── ─────── ──────────⎥ \n\
  2576. ⎢ x + y x - 2⋅y 2 ⎥ \n\
  2577. ⎢ x + x + 1⎥ \n\
  2578. ⎢ ⎥ \n\
  2579. ⎢ 2 ⎥ \n\
  2580. ⎢- y + 2⋅y - 1 x - 1 -2 ⎥ \n\
  2581. ⎢────────────── ───── ─── ⎥ \n\
  2582. ⎣ y + 5 x - y 2 ⎦τ\
  2583. """
  2584. expected5 = \
  2585. """\
  2586. ⎡ x + y x - y x + y y ⎤ \n\
  2587. ⎢───────⋅───── ─────── ──────────⎥ \n\
  2588. ⎢x - 2⋅y x + y x - 2⋅y 2 ⎥ \n\
  2589. ⎢ x + x + 1⎥ \n\
  2590. ⎢ ⎥ \n\
  2591. ⎢ 1 - x 2 x + y -2 ⎥ \n\
  2592. ⎢ ───── + ─ ─────── ─── ⎥ \n\
  2593. ⎣ x - y 2 x - 2⋅y 2 ⎦τ\
  2594. """
  2595. assert upretty(TransferFunctionMatrix([[tf1], [tf2]])) == expected1
  2596. assert upretty(TransferFunctionMatrix([[tf1], [tf2], [-tf3]])) == expected2
  2597. assert upretty(TransferFunctionMatrix([[tf1, tf2], [tf3, tf4], [tf5, tf6]])) == expected3
  2598. assert upretty(TransferFunctionMatrix([[tf2, tf1, tf4], [-tf3, -tf5, -tf6]])) == expected4
  2599. assert upretty(TransferFunctionMatrix([[Series(tf2, tf1), tf1, tf4], [Parallel(tf6, tf5), tf1, -tf6]])) == \
  2600. expected5
  2601. def test_pretty_order():
  2602. expr = O(1)
  2603. ascii_str = \
  2604. """\
  2605. O(1)\
  2606. """
  2607. ucode_str = \
  2608. """\
  2609. O(1)\
  2610. """
  2611. assert pretty(expr) == ascii_str
  2612. assert upretty(expr) == ucode_str
  2613. expr = O(1/x)
  2614. ascii_str = \
  2615. """\
  2616. /1\\\n\
  2617. O|-|\n\
  2618. \\x/\
  2619. """
  2620. ucode_str = \
  2621. """\
  2622. ⎛1⎞\n\
  2623. O⎜─⎟\n\
  2624. ⎝x⎠\
  2625. """
  2626. assert pretty(expr) == ascii_str
  2627. assert upretty(expr) == ucode_str
  2628. expr = O(x**2 + y**2)
  2629. ascii_str = \
  2630. """\
  2631. / 2 2 \\\n\
  2632. O\\x + y ; (x, y) -> (0, 0)/\
  2633. """
  2634. ucode_str = \
  2635. """\
  2636. ⎛ 2 2 ⎞\n\
  2637. O⎝x + y ; (x, y) → (0, 0)⎠\
  2638. """
  2639. assert pretty(expr) == ascii_str
  2640. assert upretty(expr) == ucode_str
  2641. expr = O(1, (x, oo))
  2642. ascii_str = \
  2643. """\
  2644. O(1; x -> oo)\
  2645. """
  2646. ucode_str = \
  2647. """\
  2648. O(1; x → ∞)\
  2649. """
  2650. assert pretty(expr) == ascii_str
  2651. assert upretty(expr) == ucode_str
  2652. expr = O(1/x, (x, oo))
  2653. ascii_str = \
  2654. """\
  2655. /1 \\\n\
  2656. O|-; x -> oo|\n\
  2657. \\x /\
  2658. """
  2659. ucode_str = \
  2660. """\
  2661. ⎛1 ⎞\n\
  2662. O⎜─; x → ∞⎟\n\
  2663. ⎝x ⎠\
  2664. """
  2665. assert pretty(expr) == ascii_str
  2666. assert upretty(expr) == ucode_str
  2667. expr = O(x**2 + y**2, (x, oo), (y, oo))
  2668. ascii_str = \
  2669. """\
  2670. / 2 2 \\\n\
  2671. O\\x + y ; (x, y) -> (oo, oo)/\
  2672. """
  2673. ucode_str = \
  2674. """\
  2675. ⎛ 2 2 ⎞\n\
  2676. O⎝x + y ; (x, y) → (∞, ∞)⎠\
  2677. """
  2678. assert pretty(expr) == ascii_str
  2679. assert upretty(expr) == ucode_str
  2680. def test_pretty_derivatives():
  2681. # Simple
  2682. expr = Derivative(log(x), x, evaluate=False)
  2683. ascii_str = \
  2684. """\
  2685. d \n\
  2686. --(log(x))\n\
  2687. dx \
  2688. """
  2689. ucode_str = \
  2690. """\
  2691. d \n\
  2692. ──(log(x))\n\
  2693. dx \
  2694. """
  2695. assert pretty(expr) == ascii_str
  2696. assert upretty(expr) == ucode_str
  2697. expr = Derivative(log(x), x, evaluate=False) + x
  2698. ascii_str_1 = \
  2699. """\
  2700. d \n\
  2701. x + --(log(x))\n\
  2702. dx \
  2703. """
  2704. ascii_str_2 = \
  2705. """\
  2706. d \n\
  2707. --(log(x)) + x\n\
  2708. dx \
  2709. """
  2710. ucode_str_1 = \
  2711. """\
  2712. d \n\
  2713. x + ──(log(x))\n\
  2714. dx \
  2715. """
  2716. ucode_str_2 = \
  2717. """\
  2718. d \n\
  2719. ──(log(x)) + x\n\
  2720. dx \
  2721. """
  2722. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2723. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2724. # basic partial derivatives
  2725. expr = Derivative(log(x + y) + x, x)
  2726. ascii_str_1 = \
  2727. """\
  2728. d \n\
  2729. --(log(x + y) + x)\n\
  2730. dx \
  2731. """
  2732. ascii_str_2 = \
  2733. """\
  2734. d \n\
  2735. --(x + log(x + y))\n\
  2736. dx \
  2737. """
  2738. ucode_str_1 = \
  2739. """\
  2740. ∂ \n\
  2741. ──(log(x + y) + x)\n\
  2742. ∂x \
  2743. """
  2744. ucode_str_2 = \
  2745. """\
  2746. ∂ \n\
  2747. ──(x + log(x + y))\n\
  2748. ∂x \
  2749. """
  2750. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2751. assert upretty(expr) in [ucode_str_1, ucode_str_2], upretty(expr)
  2752. # Multiple symbols
  2753. expr = Derivative(log(x) + x**2, x, y)
  2754. ascii_str_1 = \
  2755. """\
  2756. 2 \n\
  2757. d / 2\\\n\
  2758. -----\\log(x) + x /\n\
  2759. dy dx \
  2760. """
  2761. ascii_str_2 = \
  2762. """\
  2763. 2 \n\
  2764. d / 2 \\\n\
  2765. -----\\x + log(x)/\n\
  2766. dy dx \
  2767. """
  2768. ucode_str_1 = \
  2769. """\
  2770. 2 \n\
  2771. d ⎛ 2⎞\n\
  2772. ─────⎝log(x) + x ⎠\n\
  2773. dy dx \
  2774. """
  2775. ucode_str_2 = \
  2776. """\
  2777. 2 \n\
  2778. d ⎛ 2 ⎞\n\
  2779. ─────⎝x + log(x)⎠\n\
  2780. dy dx \
  2781. """
  2782. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2783. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2784. expr = Derivative(2*x*y, y, x) + x**2
  2785. ascii_str_1 = \
  2786. """\
  2787. 2 \n\
  2788. d 2\n\
  2789. -----(2*x*y) + x \n\
  2790. dx dy \
  2791. """
  2792. ascii_str_2 = \
  2793. """\
  2794. 2 \n\
  2795. 2 d \n\
  2796. x + -----(2*x*y)\n\
  2797. dx dy \
  2798. """
  2799. ucode_str_1 = \
  2800. """\
  2801. 2 \n\
  2802. ∂ 2\n\
  2803. ─────(2⋅x⋅y) + x \n\
  2804. ∂x ∂y \
  2805. """
  2806. ucode_str_2 = \
  2807. """\
  2808. 2 \n\
  2809. 2 ∂ \n\
  2810. x + ─────(2⋅x⋅y)\n\
  2811. ∂x ∂y \
  2812. """
  2813. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2814. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2815. expr = Derivative(2*x*y, x, x)
  2816. ascii_str = \
  2817. """\
  2818. 2 \n\
  2819. d \n\
  2820. ---(2*x*y)\n\
  2821. 2 \n\
  2822. dx \
  2823. """
  2824. ucode_str = \
  2825. """\
  2826. 2 \n\
  2827. ∂ \n\
  2828. ───(2⋅x⋅y)\n\
  2829. 2 \n\
  2830. ∂x \
  2831. """
  2832. assert pretty(expr) == ascii_str
  2833. assert upretty(expr) == ucode_str
  2834. expr = Derivative(2*x*y, x, 17)
  2835. ascii_str = \
  2836. """\
  2837. 17 \n\
  2838. d \n\
  2839. ----(2*x*y)\n\
  2840. 17 \n\
  2841. dx \
  2842. """
  2843. ucode_str = \
  2844. """\
  2845. 17 \n\
  2846. ∂ \n\
  2847. ────(2⋅x⋅y)\n\
  2848. 17 \n\
  2849. ∂x \
  2850. """
  2851. assert pretty(expr) == ascii_str
  2852. assert upretty(expr) == ucode_str
  2853. expr = Derivative(2*x*y, x, x, y)
  2854. ascii_str = \
  2855. """\
  2856. 3 \n\
  2857. d \n\
  2858. ------(2*x*y)\n\
  2859. 2 \n\
  2860. dy dx \
  2861. """
  2862. ucode_str = \
  2863. """\
  2864. 3 \n\
  2865. ∂ \n\
  2866. ──────(2⋅x⋅y)\n\
  2867. 2 \n\
  2868. ∂y ∂x \
  2869. """
  2870. assert pretty(expr) == ascii_str
  2871. assert upretty(expr) == ucode_str
  2872. # Greek letters
  2873. alpha = Symbol('alpha')
  2874. beta = Function('beta')
  2875. expr = beta(alpha).diff(alpha)
  2876. ascii_str = \
  2877. """\
  2878. d \n\
  2879. ------(beta(alpha))\n\
  2880. dalpha \
  2881. """
  2882. ucode_str = \
  2883. """\
  2884. d \n\
  2885. ──(β(α))\n\
  2886. dα \
  2887. """
  2888. assert pretty(expr) == ascii_str
  2889. assert upretty(expr) == ucode_str
  2890. expr = Derivative(f(x), (x, n))
  2891. ascii_str = \
  2892. """\
  2893. n \n\
  2894. d \n\
  2895. ---(f(x))\n\
  2896. n \n\
  2897. dx \
  2898. """
  2899. ucode_str = \
  2900. """\
  2901. n \n\
  2902. d \n\
  2903. ───(f(x))\n\
  2904. n \n\
  2905. dx \
  2906. """
  2907. assert pretty(expr) == ascii_str
  2908. assert upretty(expr) == ucode_str
  2909. def test_pretty_integrals():
  2910. expr = Integral(log(x), x)
  2911. ascii_str = \
  2912. """\
  2913. / \n\
  2914. | \n\
  2915. | log(x) dx\n\
  2916. | \n\
  2917. / \
  2918. """
  2919. ucode_str = \
  2920. """\
  2921. ⌠ \n\
  2922. ⎮ log(x) dx\n\
  2923. ⌡ \
  2924. """
  2925. assert pretty(expr) == ascii_str
  2926. assert upretty(expr) == ucode_str
  2927. expr = Integral(x**2, x)
  2928. ascii_str = \
  2929. """\
  2930. / \n\
  2931. | \n\
  2932. | 2 \n\
  2933. | x dx\n\
  2934. | \n\
  2935. / \
  2936. """
  2937. ucode_str = \
  2938. """\
  2939. ⌠ \n\
  2940. ⎮ 2 \n\
  2941. ⎮ x dx\n\
  2942. ⌡ \
  2943. """
  2944. assert pretty(expr) == ascii_str
  2945. assert upretty(expr) == ucode_str
  2946. expr = Integral((sin(x))**2 / (tan(x))**2)
  2947. ascii_str = \
  2948. """\
  2949. / \n\
  2950. | \n\
  2951. | 2 \n\
  2952. | sin (x) \n\
  2953. | ------- dx\n\
  2954. | 2 \n\
  2955. | tan (x) \n\
  2956. | \n\
  2957. / \
  2958. """
  2959. ucode_str = \
  2960. """\
  2961. ⌠ \n\
  2962. ⎮ 2 \n\
  2963. ⎮ sin (x) \n\
  2964. ⎮ ─────── dx\n\
  2965. ⎮ 2 \n\
  2966. ⎮ tan (x) \n\
  2967. ⌡ \
  2968. """
  2969. assert pretty(expr) == ascii_str
  2970. assert upretty(expr) == ucode_str
  2971. expr = Integral(x**(2**x), x)
  2972. ascii_str = \
  2973. """\
  2974. / \n\
  2975. | \n\
  2976. | / x\\ \n\
  2977. | \\2 / \n\
  2978. | x dx\n\
  2979. | \n\
  2980. / \
  2981. """
  2982. ucode_str = \
  2983. """\
  2984. ⌠ \n\
  2985. ⎮ ⎛ x⎞ \n\
  2986. ⎮ ⎝2 ⎠ \n\
  2987. ⎮ x dx\n\
  2988. ⌡ \
  2989. """
  2990. assert pretty(expr) == ascii_str
  2991. assert upretty(expr) == ucode_str
  2992. expr = Integral(x**2, (x, 1, 2))
  2993. ascii_str = \
  2994. """\
  2995. 2 \n\
  2996. / \n\
  2997. | \n\
  2998. | 2 \n\
  2999. | x dx\n\
  3000. | \n\
  3001. / \n\
  3002. 1 \
  3003. """
  3004. ucode_str = \
  3005. """\
  3006. 2 \n\
  3007. ⌠ \n\
  3008. ⎮ 2 \n\
  3009. ⎮ x dx\n\
  3010. ⌡ \n\
  3011. 1 \
  3012. """
  3013. assert pretty(expr) == ascii_str
  3014. assert upretty(expr) == ucode_str
  3015. expr = Integral(x**2, (x, Rational(1, 2), 10))
  3016. ascii_str = \
  3017. """\
  3018. 10 \n\
  3019. / \n\
  3020. | \n\
  3021. | 2 \n\
  3022. | x dx\n\
  3023. | \n\
  3024. / \n\
  3025. 1/2 \
  3026. """
  3027. ucode_str = \
  3028. """\
  3029. 10 \n\
  3030. ⌠ \n\
  3031. ⎮ 2 \n\
  3032. ⎮ x dx\n\
  3033. ⌡ \n\
  3034. 1/2 \
  3035. """
  3036. assert pretty(expr) == ascii_str
  3037. assert upretty(expr) == ucode_str
  3038. expr = Integral(x**2*y**2, x, y)
  3039. ascii_str = \
  3040. """\
  3041. / / \n\
  3042. | | \n\
  3043. | | 2 2 \n\
  3044. | | x *y dx dy\n\
  3045. | | \n\
  3046. / / \
  3047. """
  3048. ucode_str = \
  3049. """\
  3050. ⌠ ⌠ \n\
  3051. ⎮ ⎮ 2 2 \n\
  3052. ⎮ ⎮ x ⋅y dx dy\n\
  3053. ⌡ ⌡ \
  3054. """
  3055. assert pretty(expr) == ascii_str
  3056. assert upretty(expr) == ucode_str
  3057. expr = Integral(sin(th)/cos(ph), (th, 0, pi), (ph, 0, 2*pi))
  3058. ascii_str = \
  3059. """\
  3060. 2*pi pi \n\
  3061. / / \n\
  3062. | | \n\
  3063. | | sin(theta) \n\
  3064. | | ---------- d(theta) d(phi)\n\
  3065. | | cos(phi) \n\
  3066. | | \n\
  3067. / / \n\
  3068. 0 0 \
  3069. """
  3070. ucode_str = \
  3071. """\
  3072. 2⋅π π \n\
  3073. ⌠ ⌠ \n\
  3074. ⎮ ⎮ sin(θ) \n\
  3075. ⎮ ⎮ ────── dθ dφ\n\
  3076. ⎮ ⎮ cos(φ) \n\
  3077. ⌡ ⌡ \n\
  3078. 0 0 \
  3079. """
  3080. assert pretty(expr) == ascii_str
  3081. assert upretty(expr) == ucode_str
  3082. def test_pretty_matrix():
  3083. # Empty Matrix
  3084. expr = Matrix()
  3085. ascii_str = "[]"
  3086. unicode_str = "[]"
  3087. assert pretty(expr) == ascii_str
  3088. assert upretty(expr) == unicode_str
  3089. expr = Matrix(2, 0, lambda i, j: 0)
  3090. ascii_str = "[]"
  3091. unicode_str = "[]"
  3092. assert pretty(expr) == ascii_str
  3093. assert upretty(expr) == unicode_str
  3094. expr = Matrix(0, 2, lambda i, j: 0)
  3095. ascii_str = "[]"
  3096. unicode_str = "[]"
  3097. assert pretty(expr) == ascii_str
  3098. assert upretty(expr) == unicode_str
  3099. expr = Matrix([[x**2 + 1, 1], [y, x + y]])
  3100. ascii_str_1 = \
  3101. """\
  3102. [ 2 ]
  3103. [1 + x 1 ]
  3104. [ ]
  3105. [ y x + y]\
  3106. """
  3107. ascii_str_2 = \
  3108. """\
  3109. [ 2 ]
  3110. [x + 1 1 ]
  3111. [ ]
  3112. [ y x + y]\
  3113. """
  3114. ucode_str_1 = \
  3115. """\
  3116. ⎡ 2 ⎤
  3117. ⎢1 + x 1 ⎥
  3118. ⎢ ⎥
  3119. ⎣ y x + y⎦\
  3120. """
  3121. ucode_str_2 = \
  3122. """\
  3123. ⎡ 2 ⎤
  3124. ⎢x + 1 1 ⎥
  3125. ⎢ ⎥
  3126. ⎣ y x + y⎦\
  3127. """
  3128. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  3129. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  3130. expr = Matrix([[x/y, y, th], [0, exp(I*k*ph), 1]])
  3131. ascii_str = \
  3132. """\
  3133. [x ]
  3134. [- y theta]
  3135. [y ]
  3136. [ ]
  3137. [ I*k*phi ]
  3138. [0 e 1 ]\
  3139. """
  3140. ucode_str = \
  3141. """\
  3142. ⎡x ⎤
  3143. ⎢─ y θ⎥
  3144. ⎢y ⎥
  3145. ⎢ ⎥
  3146. ⎢ ⅈ⋅k⋅φ ⎥
  3147. ⎣0 ℯ 1⎦\
  3148. """
  3149. assert pretty(expr) == ascii_str
  3150. assert upretty(expr) == ucode_str
  3151. unicode_str = \
  3152. """\
  3153. ⎡v̇_msc_00 0 0 ⎤
  3154. ⎢ ⎥
  3155. ⎢ 0 v̇_msc_01 0 ⎥
  3156. ⎢ ⎥
  3157. ⎣ 0 0 v̇_msc_02⎦\
  3158. """
  3159. expr = diag(*MatrixSymbol('vdot_msc',1,3))
  3160. assert upretty(expr) == unicode_str
  3161. def test_pretty_ndim_arrays():
  3162. x, y, z, w = symbols("x y z w")
  3163. for ArrayType in (ImmutableDenseNDimArray, ImmutableSparseNDimArray, MutableDenseNDimArray, MutableSparseNDimArray):
  3164. # Basic: scalar array
  3165. M = ArrayType(x)
  3166. assert pretty(M) == "x"
  3167. assert upretty(M) == "x"
  3168. M = ArrayType([[1/x, y], [z, w]])
  3169. M1 = ArrayType([1/x, y, z])
  3170. M2 = tensorproduct(M1, M)
  3171. M3 = tensorproduct(M, M)
  3172. ascii_str = \
  3173. """\
  3174. [1 ]\n\
  3175. [- y]\n\
  3176. [x ]\n\
  3177. [ ]\n\
  3178. [z w]\
  3179. """
  3180. ucode_str = \
  3181. """\
  3182. ⎡1 ⎤\n\
  3183. ⎢─ y⎥\n\
  3184. ⎢x ⎥\n\
  3185. ⎢ ⎥\n\
  3186. ⎣z w⎦\
  3187. """
  3188. assert pretty(M) == ascii_str
  3189. assert upretty(M) == ucode_str
  3190. ascii_str = \
  3191. """\
  3192. [1 ]\n\
  3193. [- y z]\n\
  3194. [x ]\
  3195. """
  3196. ucode_str = \
  3197. """\
  3198. ⎡1 ⎤\n\
  3199. ⎢─ y z⎥\n\
  3200. ⎣x ⎦\
  3201. """
  3202. assert pretty(M1) == ascii_str
  3203. assert upretty(M1) == ucode_str
  3204. ascii_str = \
  3205. """\
  3206. [[1 y] ]\n\
  3207. [[-- -] [z ]]\n\
  3208. [[ 2 x] [ y 2 ] [- y*z]]\n\
  3209. [[x ] [ - y ] [x ]]\n\
  3210. [[ ] [ x ] [ ]]\n\
  3211. [[z w] [ ] [ 2 ]]\n\
  3212. [[- -] [y*z w*y] [z w*z]]\n\
  3213. [[x x] ]\
  3214. """
  3215. ucode_str = \
  3216. """\
  3217. ⎡⎡1 y⎤ ⎤\n\
  3218. ⎢⎢── ─⎥ ⎡z ⎤⎥\n\
  3219. ⎢⎢ 2 x⎥ ⎡ y 2 ⎤ ⎢─ y⋅z⎥⎥\n\
  3220. ⎢⎢x ⎥ ⎢ ─ y ⎥ ⎢x ⎥⎥\n\
  3221. ⎢⎢ ⎥ ⎢ x ⎥ ⎢ ⎥⎥\n\
  3222. ⎢⎢z w⎥ ⎢ ⎥ ⎢ 2 ⎥⎥\n\
  3223. ⎢⎢─ ─⎥ ⎣y⋅z w⋅y⎦ ⎣z w⋅z⎦⎥\n\
  3224. ⎣⎣x x⎦ ⎦\
  3225. """
  3226. assert pretty(M2) == ascii_str
  3227. assert upretty(M2) == ucode_str
  3228. ascii_str = \
  3229. """\
  3230. [ [1 y] ]\n\
  3231. [ [-- -] ]\n\
  3232. [ [ 2 x] [ y 2 ]]\n\
  3233. [ [x ] [ - y ]]\n\
  3234. [ [ ] [ x ]]\n\
  3235. [ [z w] [ ]]\n\
  3236. [ [- -] [y*z w*y]]\n\
  3237. [ [x x] ]\n\
  3238. [ ]\n\
  3239. [[z ] [ w ]]\n\
  3240. [[- y*z] [ - w*y]]\n\
  3241. [[x ] [ x ]]\n\
  3242. [[ ] [ ]]\n\
  3243. [[ 2 ] [ 2 ]]\n\
  3244. [[z w*z] [w*z w ]]\
  3245. """
  3246. ucode_str = \
  3247. """\
  3248. ⎡ ⎡1 y⎤ ⎤\n\
  3249. ⎢ ⎢── ─⎥ ⎥\n\
  3250. ⎢ ⎢ 2 x⎥ ⎡ y 2 ⎤⎥\n\
  3251. ⎢ ⎢x ⎥ ⎢ ─ y ⎥⎥\n\
  3252. ⎢ ⎢ ⎥ ⎢ x ⎥⎥\n\
  3253. ⎢ ⎢z w⎥ ⎢ ⎥⎥\n\
  3254. ⎢ ⎢─ ─⎥ ⎣y⋅z w⋅y⎦⎥\n\
  3255. ⎢ ⎣x x⎦ ⎥\n\
  3256. ⎢ ⎥\n\
  3257. ⎢⎡z ⎤ ⎡ w ⎤⎥\n\
  3258. ⎢⎢─ y⋅z⎥ ⎢ ─ w⋅y⎥⎥\n\
  3259. ⎢⎢x ⎥ ⎢ x ⎥⎥\n\
  3260. ⎢⎢ ⎥ ⎢ ⎥⎥\n\
  3261. ⎢⎢ 2 ⎥ ⎢ 2 ⎥⎥\n\
  3262. ⎣⎣z w⋅z⎦ ⎣w⋅z w ⎦⎦\
  3263. """
  3264. assert pretty(M3) == ascii_str
  3265. assert upretty(M3) == ucode_str
  3266. Mrow = ArrayType([[x, y, 1 / z]])
  3267. Mcolumn = ArrayType([[x], [y], [1 / z]])
  3268. Mcol2 = ArrayType([Mcolumn.tolist()])
  3269. ascii_str = \
  3270. """\
  3271. [[ 1]]\n\
  3272. [[x y -]]\n\
  3273. [[ z]]\
  3274. """
  3275. ucode_str = \
  3276. """\
  3277. ⎡⎡ 1⎤⎤\n\
  3278. ⎢⎢x y ─⎥⎥\n\
  3279. ⎣⎣ z⎦⎦\
  3280. """
  3281. assert pretty(Mrow) == ascii_str
  3282. assert upretty(Mrow) == ucode_str
  3283. ascii_str = \
  3284. """\
  3285. [x]\n\
  3286. [ ]\n\
  3287. [y]\n\
  3288. [ ]\n\
  3289. [1]\n\
  3290. [-]\n\
  3291. [z]\
  3292. """
  3293. ucode_str = \
  3294. """\
  3295. ⎡x⎤\n\
  3296. ⎢ ⎥\n\
  3297. ⎢y⎥\n\
  3298. ⎢ ⎥\n\
  3299. ⎢1⎥\n\
  3300. ⎢─⎥\n\
  3301. ⎣z⎦\
  3302. """
  3303. assert pretty(Mcolumn) == ascii_str
  3304. assert upretty(Mcolumn) == ucode_str
  3305. ascii_str = \
  3306. """\
  3307. [[x]]\n\
  3308. [[ ]]\n\
  3309. [[y]]\n\
  3310. [[ ]]\n\
  3311. [[1]]\n\
  3312. [[-]]\n\
  3313. [[z]]\
  3314. """
  3315. ucode_str = \
  3316. """\
  3317. ⎡⎡x⎤⎤\n\
  3318. ⎢⎢ ⎥⎥\n\
  3319. ⎢⎢y⎥⎥\n\
  3320. ⎢⎢ ⎥⎥\n\
  3321. ⎢⎢1⎥⎥\n\
  3322. ⎢⎢─⎥⎥\n\
  3323. ⎣⎣z⎦⎦\
  3324. """
  3325. assert pretty(Mcol2) == ascii_str
  3326. assert upretty(Mcol2) == ucode_str
  3327. def test_tensor_TensorProduct():
  3328. A = MatrixSymbol("A", 3, 3)
  3329. B = MatrixSymbol("B", 3, 3)
  3330. assert upretty(TensorProduct(A, B)) == "A\u2297B"
  3331. assert upretty(TensorProduct(A, B, A)) == "A\u2297B\u2297A"
  3332. def test_diffgeom_print_WedgeProduct():
  3333. from sympy.diffgeom.rn import R2
  3334. from sympy.diffgeom import WedgeProduct
  3335. wp = WedgeProduct(R2.dx, R2.dy)
  3336. assert upretty(wp) == "ⅆ x∧ⅆ y"
  3337. assert pretty(wp) == r"d x/\d y"
  3338. def test_Adjoint():
  3339. X = MatrixSymbol('X', 2, 2)
  3340. Y = MatrixSymbol('Y', 2, 2)
  3341. assert pretty(Adjoint(X)) == " +\nX "
  3342. assert pretty(Adjoint(X + Y)) == " +\n(X + Y) "
  3343. assert pretty(Adjoint(X) + Adjoint(Y)) == " + +\nX + Y "
  3344. assert pretty(Adjoint(X*Y)) == " +\n(X*Y) "
  3345. assert pretty(Adjoint(Y)*Adjoint(X)) == " + +\nY *X "
  3346. assert pretty(Adjoint(X**2)) == " +\n/ 2\\ \n\\X / "
  3347. assert pretty(Adjoint(X)**2) == " 2\n/ +\\ \n\\X / "
  3348. assert pretty(Adjoint(Inverse(X))) == " +\n/ -1\\ \n\\X / "
  3349. assert pretty(Inverse(Adjoint(X))) == " -1\n/ +\\ \n\\X / "
  3350. assert pretty(Adjoint(Transpose(X))) == " +\n/ T\\ \n\\X / "
  3351. assert pretty(Transpose(Adjoint(X))) == " T\n/ +\\ \n\\X / "
  3352. assert upretty(Adjoint(X)) == " †\nX "
  3353. assert upretty(Adjoint(X + Y)) == " †\n(X + Y) "
  3354. assert upretty(Adjoint(X) + Adjoint(Y)) == " † †\nX + Y "
  3355. assert upretty(Adjoint(X*Y)) == " †\n(X⋅Y) "
  3356. assert upretty(Adjoint(Y)*Adjoint(X)) == " † †\nY ⋅X "
  3357. assert upretty(Adjoint(X**2)) == \
  3358. " †\n⎛ 2⎞ \n⎝X ⎠ "
  3359. assert upretty(Adjoint(X)**2) == \
  3360. " 2\n⎛ †⎞ \n⎝X ⎠ "
  3361. assert upretty(Adjoint(Inverse(X))) == \
  3362. " †\n⎛ -1⎞ \n⎝X ⎠ "
  3363. assert upretty(Inverse(Adjoint(X))) == \
  3364. " -1\n⎛ †⎞ \n⎝X ⎠ "
  3365. assert upretty(Adjoint(Transpose(X))) == \
  3366. " †\n⎛ T⎞ \n⎝X ⎠ "
  3367. assert upretty(Transpose(Adjoint(X))) == \
  3368. " T\n⎛ †⎞ \n⎝X ⎠ "
  3369. m = Matrix(((1, 2), (3, 4)))
  3370. assert upretty(Adjoint(m)) == \
  3371. ' †\n'\
  3372. '⎡1 2⎤ \n'\
  3373. '⎢ ⎥ \n'\
  3374. '⎣3 4⎦ '
  3375. assert upretty(Adjoint(m+X)) == \
  3376. ' †\n'\
  3377. '⎛⎡1 2⎤ ⎞ \n'\
  3378. '⎜⎢ ⎥ + X⎟ \n'\
  3379. '⎝⎣3 4⎦ ⎠ '
  3380. assert upretty(Adjoint(BlockMatrix(((OneMatrix(2, 2), X),
  3381. (m, ZeroMatrix(2, 2)))))) == \
  3382. ' †\n'\
  3383. '⎡ 𝟙 X⎤ \n'\
  3384. '⎢ ⎥ \n'\
  3385. '⎢⎡1 2⎤ ⎥ \n'\
  3386. '⎢⎢ ⎥ 𝟘⎥ \n'\
  3387. '⎣⎣3 4⎦ ⎦ '
  3388. def test_Transpose():
  3389. X = MatrixSymbol('X', 2, 2)
  3390. Y = MatrixSymbol('Y', 2, 2)
  3391. assert pretty(Transpose(X)) == " T\nX "
  3392. assert pretty(Transpose(X + Y)) == " T\n(X + Y) "
  3393. assert pretty(Transpose(X) + Transpose(Y)) == " T T\nX + Y "
  3394. assert pretty(Transpose(X*Y)) == " T\n(X*Y) "
  3395. assert pretty(Transpose(Y)*Transpose(X)) == " T T\nY *X "
  3396. assert pretty(Transpose(X**2)) == " T\n/ 2\\ \n\\X / "
  3397. assert pretty(Transpose(X)**2) == " 2\n/ T\\ \n\\X / "
  3398. assert pretty(Transpose(Inverse(X))) == " T\n/ -1\\ \n\\X / "
  3399. assert pretty(Inverse(Transpose(X))) == " -1\n/ T\\ \n\\X / "
  3400. assert upretty(Transpose(X)) == " T\nX "
  3401. assert upretty(Transpose(X + Y)) == " T\n(X + Y) "
  3402. assert upretty(Transpose(X) + Transpose(Y)) == " T T\nX + Y "
  3403. assert upretty(Transpose(X*Y)) == " T\n(X⋅Y) "
  3404. assert upretty(Transpose(Y)*Transpose(X)) == " T T\nY ⋅X "
  3405. assert upretty(Transpose(X**2)) == \
  3406. " T\n⎛ 2⎞ \n⎝X ⎠ "
  3407. assert upretty(Transpose(X)**2) == \
  3408. " 2\n⎛ T⎞ \n⎝X ⎠ "
  3409. assert upretty(Transpose(Inverse(X))) == \
  3410. " T\n⎛ -1⎞ \n⎝X ⎠ "
  3411. assert upretty(Inverse(Transpose(X))) == \
  3412. " -1\n⎛ T⎞ \n⎝X ⎠ "
  3413. m = Matrix(((1, 2), (3, 4)))
  3414. assert upretty(Transpose(m)) == \
  3415. ' T\n'\
  3416. '⎡1 2⎤ \n'\
  3417. '⎢ ⎥ \n'\
  3418. '⎣3 4⎦ '
  3419. assert upretty(Transpose(m+X)) == \
  3420. ' T\n'\
  3421. '⎛⎡1 2⎤ ⎞ \n'\
  3422. '⎜⎢ ⎥ + X⎟ \n'\
  3423. '⎝⎣3 4⎦ ⎠ '
  3424. assert upretty(Transpose(BlockMatrix(((OneMatrix(2, 2), X),
  3425. (m, ZeroMatrix(2, 2)))))) == \
  3426. ' T\n'\
  3427. '⎡ 𝟙 X⎤ \n'\
  3428. '⎢ ⎥ \n'\
  3429. '⎢⎡1 2⎤ ⎥ \n'\
  3430. '⎢⎢ ⎥ 𝟘⎥ \n'\
  3431. '⎣⎣3 4⎦ ⎦ '
  3432. def test_pretty_Trace_issue_9044():
  3433. X = Matrix([[1, 2], [3, 4]])
  3434. Y = Matrix([[2, 4], [6, 8]])
  3435. ascii_str_1 = \
  3436. """\
  3437. /[1 2]\\
  3438. tr|[ ]|
  3439. \\[3 4]/\
  3440. """
  3441. ucode_str_1 = \
  3442. """\
  3443. ⎛⎡1 2⎤⎞
  3444. tr⎜⎢ ⎥⎟
  3445. ⎝⎣3 4⎦⎠\
  3446. """
  3447. ascii_str_2 = \
  3448. """\
  3449. /[1 2]\\ /[2 4]\\
  3450. tr|[ ]| + tr|[ ]|
  3451. \\[3 4]/ \\[6 8]/\
  3452. """
  3453. ucode_str_2 = \
  3454. """\
  3455. ⎛⎡1 2⎤⎞ ⎛⎡2 4⎤⎞
  3456. tr⎜⎢ ⎥⎟ + tr⎜⎢ ⎥⎟
  3457. ⎝⎣3 4⎦⎠ ⎝⎣6 8⎦⎠\
  3458. """
  3459. assert pretty(Trace(X)) == ascii_str_1
  3460. assert upretty(Trace(X)) == ucode_str_1
  3461. assert pretty(Trace(X) + Trace(Y)) == ascii_str_2
  3462. assert upretty(Trace(X) + Trace(Y)) == ucode_str_2
  3463. def test_MatrixSlice():
  3464. n = Symbol('n', integer=True)
  3465. x, y, z, w, t, = symbols('x y z w t')
  3466. X = MatrixSymbol('X', n, n)
  3467. Y = MatrixSymbol('Y', 10, 10)
  3468. Z = MatrixSymbol('Z', 10, 10)
  3469. expr = MatrixSlice(X, (None, None, None), (None, None, None))
  3470. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3471. expr = X[x:x + 1, y:y + 1]
  3472. assert pretty(expr) == upretty(expr) == 'X[x:x + 1, y:y + 1]'
  3473. expr = X[x:x + 1:2, y:y + 1:2]
  3474. assert pretty(expr) == upretty(expr) == 'X[x:x + 1:2, y:y + 1:2]'
  3475. expr = X[:x, y:]
  3476. assert pretty(expr) == upretty(expr) == 'X[:x, y:]'
  3477. expr = X[:x, y:]
  3478. assert pretty(expr) == upretty(expr) == 'X[:x, y:]'
  3479. expr = X[x:, :y]
  3480. assert pretty(expr) == upretty(expr) == 'X[x:, :y]'
  3481. expr = X[x:y, z:w]
  3482. assert pretty(expr) == upretty(expr) == 'X[x:y, z:w]'
  3483. expr = X[x:y:t, w:t:x]
  3484. assert pretty(expr) == upretty(expr) == 'X[x:y:t, w:t:x]'
  3485. expr = X[x::y, t::w]
  3486. assert pretty(expr) == upretty(expr) == 'X[x::y, t::w]'
  3487. expr = X[:x:y, :t:w]
  3488. assert pretty(expr) == upretty(expr) == 'X[:x:y, :t:w]'
  3489. expr = X[::x, ::y]
  3490. assert pretty(expr) == upretty(expr) == 'X[::x, ::y]'
  3491. expr = MatrixSlice(X, (0, None, None), (0, None, None))
  3492. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3493. expr = MatrixSlice(X, (None, n, None), (None, n, None))
  3494. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3495. expr = MatrixSlice(X, (0, n, None), (0, n, None))
  3496. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3497. expr = MatrixSlice(X, (0, n, 2), (0, n, 2))
  3498. assert pretty(expr) == upretty(expr) == 'X[::2, ::2]'
  3499. expr = X[1:2:3, 4:5:6]
  3500. assert pretty(expr) == upretty(expr) == 'X[1:2:3, 4:5:6]'
  3501. expr = X[1:3:5, 4:6:8]
  3502. assert pretty(expr) == upretty(expr) == 'X[1:3:5, 4:6:8]'
  3503. expr = X[1:10:2]
  3504. assert pretty(expr) == upretty(expr) == 'X[1:10:2, :]'
  3505. expr = Y[:5, 1:9:2]
  3506. assert pretty(expr) == upretty(expr) == 'Y[:5, 1:9:2]'
  3507. expr = Y[:5, 1:10:2]
  3508. assert pretty(expr) == upretty(expr) == 'Y[:5, 1::2]'
  3509. expr = Y[5, :5:2]
  3510. assert pretty(expr) == upretty(expr) == 'Y[5:6, :5:2]'
  3511. expr = X[0:1, 0:1]
  3512. assert pretty(expr) == upretty(expr) == 'X[:1, :1]'
  3513. expr = X[0:1:2, 0:1:2]
  3514. assert pretty(expr) == upretty(expr) == 'X[:1:2, :1:2]'
  3515. expr = (Y + Z)[2:, 2:]
  3516. assert pretty(expr) == upretty(expr) == '(Y + Z)[2:, 2:]'
  3517. def test_MatrixExpressions():
  3518. n = Symbol('n', integer=True)
  3519. X = MatrixSymbol('X', n, n)
  3520. assert pretty(X) == upretty(X) == "X"
  3521. # Apply function elementwise (`ElementwiseApplyFunc`):
  3522. expr = (X.T*X).applyfunc(sin)
  3523. ascii_str = """\
  3524. / T \\\n\
  3525. (d -> sin(d)).\\X *X/\
  3526. """
  3527. ucode_str = """\
  3528. ⎛ T ⎞\n\
  3529. (d ↦ sin(d))˳⎝X ⋅X⎠\
  3530. """
  3531. assert pretty(expr) == ascii_str
  3532. assert upretty(expr) == ucode_str
  3533. lamda = Lambda(x, 1/x)
  3534. expr = (n*X).applyfunc(lamda)
  3535. ascii_str = """\
  3536. / 1\\ \n\
  3537. |x -> -|.(n*X)\n\
  3538. \\ x/ \
  3539. """
  3540. ucode_str = """\
  3541. ⎛ 1⎞ \n\
  3542. ⎜x ↦ ─⎟˳(n⋅X)\n\
  3543. ⎝ x⎠ \
  3544. """
  3545. assert pretty(expr) == ascii_str
  3546. assert upretty(expr) == ucode_str
  3547. def test_pretty_dotproduct():
  3548. from sympy.matrices.expressions.dotproduct import DotProduct
  3549. n = symbols("n", integer=True)
  3550. A = MatrixSymbol('A', n, 1)
  3551. B = MatrixSymbol('B', n, 1)
  3552. C = Matrix(1, 3, [1, 2, 3])
  3553. D = Matrix(1, 3, [1, 3, 4])
  3554. assert pretty(DotProduct(A, B)) == "A*B"
  3555. assert pretty(DotProduct(C, D)) == "[1 2 3]*[1 3 4]"
  3556. assert upretty(DotProduct(A, B)) == "A⋅B"
  3557. assert upretty(DotProduct(C, D)) == "[1 2 3]⋅[1 3 4]"
  3558. def test_pretty_Determinant():
  3559. from sympy.matrices import Determinant, Inverse, BlockMatrix, OneMatrix, ZeroMatrix
  3560. m = Matrix(((1, 2), (3, 4)))
  3561. assert upretty(Determinant(m)) == '│1 2│\n│ │\n│3 4│'
  3562. assert upretty(Determinant(Inverse(m))) == \
  3563. '│ -1│\n'\
  3564. '│⎡1 2⎤ │\n'\
  3565. '│⎢ ⎥ │\n'\
  3566. '│⎣3 4⎦ │'
  3567. X = MatrixSymbol('X', 2, 2)
  3568. assert upretty(Determinant(X)) == '│X│'
  3569. assert upretty(Determinant(X + m)) == \
  3570. '│⎡1 2⎤ │\n'\
  3571. '│⎢ ⎥ + X│\n'\
  3572. '│⎣3 4⎦ │'
  3573. assert upretty(Determinant(BlockMatrix(((OneMatrix(2, 2), X),
  3574. (m, ZeroMatrix(2, 2)))))) == \
  3575. '│ 𝟙 X│\n'\
  3576. '│ │\n'\
  3577. '│⎡1 2⎤ │\n'\
  3578. '│⎢ ⎥ 𝟘│\n'\
  3579. '│⎣3 4⎦ │'
  3580. def test_pretty_piecewise():
  3581. expr = Piecewise((x, x < 1), (x**2, True))
  3582. ascii_str = \
  3583. """\
  3584. /x for x < 1\n\
  3585. | \n\
  3586. < 2 \n\
  3587. |x otherwise\n\
  3588. \\ \
  3589. """
  3590. ucode_str = \
  3591. """\
  3592. ⎧x for x < 1\n\
  3593. ⎪ \n\
  3594. ⎨ 2 \n\
  3595. ⎪x otherwise\n\
  3596. ⎩ \
  3597. """
  3598. assert pretty(expr) == ascii_str
  3599. assert upretty(expr) == ucode_str
  3600. expr = -Piecewise((x, x < 1), (x**2, True))
  3601. ascii_str = \
  3602. """\
  3603. //x for x < 1\\\n\
  3604. || |\n\
  3605. -|< 2 |\n\
  3606. ||x otherwise|\n\
  3607. \\\\ /\
  3608. """
  3609. ucode_str = \
  3610. """\
  3611. ⎛⎧x for x < 1⎞\n\
  3612. ⎜⎪ ⎟\n\
  3613. -⎜⎨ 2 ⎟\n\
  3614. ⎜⎪x otherwise⎟\n\
  3615. ⎝⎩ ⎠\
  3616. """
  3617. assert pretty(expr) == ascii_str
  3618. assert upretty(expr) == ucode_str
  3619. expr = x + Piecewise((x, x > 0), (y, True)) + Piecewise((x/y, x < 2),
  3620. (y**2, x > 2), (1, True)) + 1
  3621. ascii_str = \
  3622. """\
  3623. //x \\ \n\
  3624. ||- for x < 2| \n\
  3625. ||y | \n\
  3626. //x for x > 0\\ || | \n\
  3627. x + |< | + |< 2 | + 1\n\
  3628. \\\\y otherwise/ ||y for x > 2| \n\
  3629. || | \n\
  3630. ||1 otherwise| \n\
  3631. \\\\ / \
  3632. """
  3633. ucode_str = \
  3634. """\
  3635. ⎛⎧x ⎞ \n\
  3636. ⎜⎪─ for x < 2⎟ \n\
  3637. ⎜⎪y ⎟ \n\
  3638. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟ \n\
  3639. x + ⎜⎨ ⎟ + ⎜⎨ 2 ⎟ + 1\n\
  3640. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟ \n\
  3641. ⎜⎪ ⎟ \n\
  3642. ⎜⎪1 otherwise⎟ \n\
  3643. ⎝⎩ ⎠ \
  3644. """
  3645. assert pretty(expr) == ascii_str
  3646. assert upretty(expr) == ucode_str
  3647. expr = x - Piecewise((x, x > 0), (y, True)) + Piecewise((x/y, x < 2),
  3648. (y**2, x > 2), (1, True)) + 1
  3649. ascii_str = \
  3650. """\
  3651. //x \\ \n\
  3652. ||- for x < 2| \n\
  3653. ||y | \n\
  3654. //x for x > 0\\ || | \n\
  3655. x - |< | + |< 2 | + 1\n\
  3656. \\\\y otherwise/ ||y for x > 2| \n\
  3657. || | \n\
  3658. ||1 otherwise| \n\
  3659. \\\\ / \
  3660. """
  3661. ucode_str = \
  3662. """\
  3663. ⎛⎧x ⎞ \n\
  3664. ⎜⎪─ for x < 2⎟ \n\
  3665. ⎜⎪y ⎟ \n\
  3666. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟ \n\
  3667. x - ⎜⎨ ⎟ + ⎜⎨ 2 ⎟ + 1\n\
  3668. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟ \n\
  3669. ⎜⎪ ⎟ \n\
  3670. ⎜⎪1 otherwise⎟ \n\
  3671. ⎝⎩ ⎠ \
  3672. """
  3673. assert pretty(expr) == ascii_str
  3674. assert upretty(expr) == ucode_str
  3675. expr = x*Piecewise((x, x > 0), (y, True))
  3676. ascii_str = \
  3677. """\
  3678. //x for x > 0\\\n\
  3679. x*|< |\n\
  3680. \\\\y otherwise/\
  3681. """
  3682. ucode_str = \
  3683. """\
  3684. ⎛⎧x for x > 0⎞\n\
  3685. x⋅⎜⎨ ⎟\n\
  3686. ⎝⎩y otherwise⎠\
  3687. """
  3688. assert pretty(expr) == ascii_str
  3689. assert upretty(expr) == ucode_str
  3690. expr = Piecewise((x, x > 0), (y, True))*Piecewise((x/y, x < 2), (y**2, x >
  3691. 2), (1, True))
  3692. ascii_str = \
  3693. """\
  3694. //x \\\n\
  3695. ||- for x < 2|\n\
  3696. ||y |\n\
  3697. //x for x > 0\\ || |\n\
  3698. |< |*|< 2 |\n\
  3699. \\\\y otherwise/ ||y for x > 2|\n\
  3700. || |\n\
  3701. ||1 otherwise|\n\
  3702. \\\\ /\
  3703. """
  3704. ucode_str = \
  3705. """\
  3706. ⎛⎧x ⎞\n\
  3707. ⎜⎪─ for x < 2⎟\n\
  3708. ⎜⎪y ⎟\n\
  3709. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟\n\
  3710. ⎜⎨ ⎟⋅⎜⎨ 2 ⎟\n\
  3711. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟\n\
  3712. ⎜⎪ ⎟\n\
  3713. ⎜⎪1 otherwise⎟\n\
  3714. ⎝⎩ ⎠\
  3715. """
  3716. assert pretty(expr) == ascii_str
  3717. assert upretty(expr) == ucode_str
  3718. expr = -Piecewise((x, x > 0), (y, True))*Piecewise((x/y, x < 2), (y**2, x
  3719. > 2), (1, True))
  3720. ascii_str = \
  3721. """\
  3722. //x \\\n\
  3723. ||- for x < 2|\n\
  3724. ||y |\n\
  3725. //x for x > 0\\ || |\n\
  3726. -|< |*|< 2 |\n\
  3727. \\\\y otherwise/ ||y for x > 2|\n\
  3728. || |\n\
  3729. ||1 otherwise|\n\
  3730. \\\\ /\
  3731. """
  3732. ucode_str = \
  3733. """\
  3734. ⎛⎧x ⎞\n\
  3735. ⎜⎪─ for x < 2⎟\n\
  3736. ⎜⎪y ⎟\n\
  3737. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟\n\
  3738. -⎜⎨ ⎟⋅⎜⎨ 2 ⎟\n\
  3739. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟\n\
  3740. ⎜⎪ ⎟\n\
  3741. ⎜⎪1 otherwise⎟\n\
  3742. ⎝⎩ ⎠\
  3743. """
  3744. assert pretty(expr) == ascii_str
  3745. assert upretty(expr) == ucode_str
  3746. expr = Piecewise((0, Abs(1/y) < 1), (1, Abs(y) < 1), (y*meijerg(((2, 1),
  3747. ()), ((), (1, 0)), 1/y), True))
  3748. ascii_str = \
  3749. """\
  3750. / 1 \n\
  3751. | 0 for --- < 1\n\
  3752. | |y| \n\
  3753. | \n\
  3754. < 1 for |y| < 1\n\
  3755. | \n\
  3756. | __0, 2 /2, 1 | 1\\ \n\
  3757. |y*/__ | | -| otherwise \n\
  3758. \\ \\_|2, 2 \\ 1, 0 | y/ \
  3759. """
  3760. ucode_str = \
  3761. """\
  3762. ⎧ 1 \n\
  3763. ⎪ 0 for ─── < 1\n\
  3764. ⎪ │y│ \n\
  3765. ⎪ \n\
  3766. ⎨ 1 for │y│ < 1\n\
  3767. ⎪ \n\
  3768. ⎪ ╭─╮0, 2 ⎛2, 1 │ 1⎞ \n\
  3769. ⎪y⋅│╶┐ ⎜ │ ─⎟ otherwise \n\
  3770. ⎩ ╰─╯2, 2 ⎝ 1, 0 │ y⎠ \
  3771. """
  3772. assert pretty(expr) == ascii_str
  3773. assert upretty(expr) == ucode_str
  3774. # XXX: We have to use evaluate=False here because Piecewise._eval_power
  3775. # denests the power.
  3776. expr = Pow(Piecewise((x, x > 0), (y, True)), 2, evaluate=False)
  3777. ascii_str = \
  3778. """\
  3779. 2\n\
  3780. //x for x > 0\\ \n\
  3781. |< | \n\
  3782. \\\\y otherwise/ \
  3783. """
  3784. ucode_str = \
  3785. """\
  3786. 2\n\
  3787. ⎛⎧x for x > 0⎞ \n\
  3788. ⎜⎨ ⎟ \n\
  3789. ⎝⎩y otherwise⎠ \
  3790. """
  3791. assert pretty(expr) == ascii_str
  3792. assert upretty(expr) == ucode_str
  3793. def test_pretty_ITE():
  3794. expr = ITE(x, y, z)
  3795. assert pretty(expr) == (
  3796. '/y for x \n'
  3797. '< \n'
  3798. '\\z otherwise'
  3799. )
  3800. assert upretty(expr) == """\
  3801. ⎧y for x \n\
  3802. ⎨ \n\
  3803. ⎩z otherwise\
  3804. """
  3805. def test_pretty_seq():
  3806. expr = ()
  3807. ascii_str = \
  3808. """\
  3809. ()\
  3810. """
  3811. ucode_str = \
  3812. """\
  3813. ()\
  3814. """
  3815. assert pretty(expr) == ascii_str
  3816. assert upretty(expr) == ucode_str
  3817. expr = []
  3818. ascii_str = \
  3819. """\
  3820. []\
  3821. """
  3822. ucode_str = \
  3823. """\
  3824. []\
  3825. """
  3826. assert pretty(expr) == ascii_str
  3827. assert upretty(expr) == ucode_str
  3828. expr = {}
  3829. expr_2 = {}
  3830. ascii_str = \
  3831. """\
  3832. {}\
  3833. """
  3834. ucode_str = \
  3835. """\
  3836. {}\
  3837. """
  3838. assert pretty(expr) == ascii_str
  3839. assert pretty(expr_2) == ascii_str
  3840. assert upretty(expr) == ucode_str
  3841. assert upretty(expr_2) == ucode_str
  3842. expr = (1/x,)
  3843. ascii_str = \
  3844. """\
  3845. 1 \n\
  3846. (-,)\n\
  3847. x \
  3848. """
  3849. ucode_str = \
  3850. """\
  3851. ⎛1 ⎞\n\
  3852. ⎜─,⎟\n\
  3853. ⎝x ⎠\
  3854. """
  3855. assert pretty(expr) == ascii_str
  3856. assert upretty(expr) == ucode_str
  3857. expr = [x**2, 1/x, x, y, sin(th)**2/cos(ph)**2]
  3858. ascii_str = \
  3859. """\
  3860. 2 \n\
  3861. 2 1 sin (theta) \n\
  3862. [x , -, x, y, -----------]\n\
  3863. x 2 \n\
  3864. cos (phi) \
  3865. """
  3866. ucode_str = \
  3867. """\
  3868. ⎡ 2 ⎤\n\
  3869. ⎢ 2 1 sin (θ)⎥\n\
  3870. ⎢x , ─, x, y, ───────⎥\n\
  3871. ⎢ x 2 ⎥\n\
  3872. ⎣ cos (φ)⎦\
  3873. """
  3874. assert pretty(expr) == ascii_str
  3875. assert upretty(expr) == ucode_str
  3876. expr = (x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  3877. ascii_str = \
  3878. """\
  3879. 2 \n\
  3880. 2 1 sin (theta) \n\
  3881. (x , -, x, y, -----------)\n\
  3882. x 2 \n\
  3883. cos (phi) \
  3884. """
  3885. ucode_str = \
  3886. """\
  3887. ⎛ 2 ⎞\n\
  3888. ⎜ 2 1 sin (θ)⎟\n\
  3889. ⎜x , ─, x, y, ───────⎟\n\
  3890. ⎜ x 2 ⎟\n\
  3891. ⎝ cos (φ)⎠\
  3892. """
  3893. assert pretty(expr) == ascii_str
  3894. assert upretty(expr) == ucode_str
  3895. expr = Tuple(x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  3896. ascii_str = \
  3897. """\
  3898. 2 \n\
  3899. 2 1 sin (theta) \n\
  3900. (x , -, x, y, -----------)\n\
  3901. x 2 \n\
  3902. cos (phi) \
  3903. """
  3904. ucode_str = \
  3905. """\
  3906. ⎛ 2 ⎞\n\
  3907. ⎜ 2 1 sin (θ)⎟\n\
  3908. ⎜x , ─, x, y, ───────⎟\n\
  3909. ⎜ x 2 ⎟\n\
  3910. ⎝ cos (φ)⎠\
  3911. """
  3912. assert pretty(expr) == ascii_str
  3913. assert upretty(expr) == ucode_str
  3914. expr = {x: sin(x)}
  3915. expr_2 = Dict({x: sin(x)})
  3916. ascii_str = \
  3917. """\
  3918. {x: sin(x)}\
  3919. """
  3920. ucode_str = \
  3921. """\
  3922. {x: sin(x)}\
  3923. """
  3924. assert pretty(expr) == ascii_str
  3925. assert pretty(expr_2) == ascii_str
  3926. assert upretty(expr) == ucode_str
  3927. assert upretty(expr_2) == ucode_str
  3928. expr = {1/x: 1/y, x: sin(x)**2}
  3929. expr_2 = Dict({1/x: 1/y, x: sin(x)**2})
  3930. ascii_str = \
  3931. """\
  3932. 1 1 2 \n\
  3933. {-: -, x: sin (x)}\n\
  3934. x y \
  3935. """
  3936. ucode_str = \
  3937. """\
  3938. ⎧1 1 2 ⎫\n\
  3939. ⎨─: ─, x: sin (x)⎬\n\
  3940. ⎩x y ⎭\
  3941. """
  3942. assert pretty(expr) == ascii_str
  3943. assert pretty(expr_2) == ascii_str
  3944. assert upretty(expr) == ucode_str
  3945. assert upretty(expr_2) == ucode_str
  3946. # There used to be a bug with pretty-printing sequences of even height.
  3947. expr = [x**2]
  3948. ascii_str = \
  3949. """\
  3950. 2 \n\
  3951. [x ]\
  3952. """
  3953. ucode_str = \
  3954. """\
  3955. ⎡ 2⎤\n\
  3956. ⎣x ⎦\
  3957. """
  3958. assert pretty(expr) == ascii_str
  3959. assert upretty(expr) == ucode_str
  3960. expr = (x**2,)
  3961. ascii_str = \
  3962. """\
  3963. 2 \n\
  3964. (x ,)\
  3965. """
  3966. ucode_str = \
  3967. """\
  3968. ⎛ 2 ⎞\n\
  3969. ⎝x ,⎠\
  3970. """
  3971. assert pretty(expr) == ascii_str
  3972. assert upretty(expr) == ucode_str
  3973. expr = Tuple(x**2)
  3974. ascii_str = \
  3975. """\
  3976. 2 \n\
  3977. (x ,)\
  3978. """
  3979. ucode_str = \
  3980. """\
  3981. ⎛ 2 ⎞\n\
  3982. ⎝x ,⎠\
  3983. """
  3984. assert pretty(expr) == ascii_str
  3985. assert upretty(expr) == ucode_str
  3986. expr = {x**2: 1}
  3987. expr_2 = Dict({x**2: 1})
  3988. ascii_str = \
  3989. """\
  3990. 2 \n\
  3991. {x : 1}\
  3992. """
  3993. ucode_str = \
  3994. """\
  3995. ⎧ 2 ⎫\n\
  3996. ⎨x : 1⎬\n\
  3997. ⎩ ⎭\
  3998. """
  3999. assert pretty(expr) == ascii_str
  4000. assert pretty(expr_2) == ascii_str
  4001. assert upretty(expr) == ucode_str
  4002. assert upretty(expr_2) == ucode_str
  4003. def test_any_object_in_sequence():
  4004. # Cf. issue 5306
  4005. b1 = Basic()
  4006. b2 = Basic(Basic())
  4007. expr = [b2, b1]
  4008. assert pretty(expr) == "[Basic(Basic()), Basic()]"
  4009. assert upretty(expr) == "[Basic(Basic()), Basic()]"
  4010. expr = {b2, b1}
  4011. assert pretty(expr) == "{Basic(), Basic(Basic())}"
  4012. assert upretty(expr) == "{Basic(), Basic(Basic())}"
  4013. expr = {b2: b1, b1: b2}
  4014. expr2 = Dict({b2: b1, b1: b2})
  4015. assert pretty(expr) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4016. assert pretty(
  4017. expr2) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4018. assert upretty(
  4019. expr) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4020. assert upretty(
  4021. expr2) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4022. def test_print_builtin_set():
  4023. assert pretty(set()) == 'set()'
  4024. assert upretty(set()) == 'set()'
  4025. assert pretty(frozenset()) == 'frozenset()'
  4026. assert upretty(frozenset()) == 'frozenset()'
  4027. s1 = {1/x, x}
  4028. s2 = frozenset(s1)
  4029. assert pretty(s1) == \
  4030. """\
  4031. 1 \n\
  4032. {-, x}
  4033. x \
  4034. """
  4035. assert upretty(s1) == \
  4036. """\
  4037. ⎧1 ⎫
  4038. ⎨─, x⎬
  4039. ⎩x ⎭\
  4040. """
  4041. assert pretty(s2) == \
  4042. """\
  4043. 1 \n\
  4044. frozenset({-, x})
  4045. x \
  4046. """
  4047. assert upretty(s2) == \
  4048. """\
  4049. ⎛⎧1 ⎫⎞
  4050. frozenset⎜⎨─, x⎬⎟
  4051. ⎝⎩x ⎭⎠\
  4052. """
  4053. def test_pretty_sets():
  4054. s = FiniteSet
  4055. assert pretty(s(*[x*y, x**2])) == \
  4056. """\
  4057. 2 \n\
  4058. {x , x*y}\
  4059. """
  4060. assert pretty(s(*range(1, 6))) == "{1, 2, 3, 4, 5}"
  4061. assert pretty(s(*range(1, 13))) == "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
  4062. assert pretty({x*y, x**2}) == \
  4063. """\
  4064. 2 \n\
  4065. {x , x*y}\
  4066. """
  4067. assert pretty(set(range(1, 6))) == "{1, 2, 3, 4, 5}"
  4068. assert pretty(set(range(1, 13))) == \
  4069. "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
  4070. assert pretty(frozenset([x*y, x**2])) == \
  4071. """\
  4072. 2 \n\
  4073. frozenset({x , x*y})\
  4074. """
  4075. assert pretty(frozenset(range(1, 6))) == "frozenset({1, 2, 3, 4, 5})"
  4076. assert pretty(frozenset(range(1, 13))) == \
  4077. "frozenset({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})"
  4078. assert pretty(Range(0, 3, 1)) == '{0, 1, 2}'
  4079. ascii_str = '{0, 1, ..., 29}'
  4080. ucode_str = '{0, 1, …, 29}'
  4081. assert pretty(Range(0, 30, 1)) == ascii_str
  4082. assert upretty(Range(0, 30, 1)) == ucode_str
  4083. ascii_str = '{30, 29, ..., 2}'
  4084. ucode_str = '{30, 29, …, 2}'
  4085. assert pretty(Range(30, 1, -1)) == ascii_str
  4086. assert upretty(Range(30, 1, -1)) == ucode_str
  4087. ascii_str = '{0, 2, ...}'
  4088. ucode_str = '{0, 2, …}'
  4089. assert pretty(Range(0, oo, 2)) == ascii_str
  4090. assert upretty(Range(0, oo, 2)) == ucode_str
  4091. ascii_str = '{..., 2, 0}'
  4092. ucode_str = '{…, 2, 0}'
  4093. assert pretty(Range(oo, -2, -2)) == ascii_str
  4094. assert upretty(Range(oo, -2, -2)) == ucode_str
  4095. ascii_str = '{-2, -3, ...}'
  4096. ucode_str = '{-2, -3, …}'
  4097. assert pretty(Range(-2, -oo, -1)) == ascii_str
  4098. assert upretty(Range(-2, -oo, -1)) == ucode_str
  4099. def test_pretty_SetExpr():
  4100. iv = Interval(1, 3)
  4101. se = SetExpr(iv)
  4102. ascii_str = "SetExpr([1, 3])"
  4103. ucode_str = "SetExpr([1, 3])"
  4104. assert pretty(se) == ascii_str
  4105. assert upretty(se) == ucode_str
  4106. def test_pretty_ImageSet():
  4107. imgset = ImageSet(Lambda((x, y), x + y), {1, 2, 3}, {3, 4})
  4108. ascii_str = '{x + y | x in {1, 2, 3}, y in {3, 4}}'
  4109. ucode_str = '{x + y │ x ∊ {1, 2, 3}, y ∊ {3, 4}}'
  4110. assert pretty(imgset) == ascii_str
  4111. assert upretty(imgset) == ucode_str
  4112. imgset = ImageSet(Lambda(((x, y),), x + y), ProductSet({1, 2, 3}, {3, 4}))
  4113. ascii_str = '{x + y | (x, y) in {1, 2, 3} x {3, 4}}'
  4114. ucode_str = '{x + y │ (x, y) ∊ {1, 2, 3} × {3, 4}}'
  4115. assert pretty(imgset) == ascii_str
  4116. assert upretty(imgset) == ucode_str
  4117. imgset = ImageSet(Lambda(x, x**2), S.Naturals)
  4118. ascii_str = '''\
  4119. 2 \n\
  4120. {x | x in Naturals}'''
  4121. ucode_str = '''\
  4122. ⎧ 2 │ ⎫\n\
  4123. ⎨x │ x ∊ ℕ⎬\n\
  4124. ⎩ │ ⎭'''
  4125. assert pretty(imgset) == ascii_str
  4126. assert upretty(imgset) == ucode_str
  4127. # TODO: The "x in N" parts below should be centered independently of the
  4128. # 1/x**2 fraction
  4129. imgset = ImageSet(Lambda(x, 1/x**2), S.Naturals)
  4130. ascii_str = '''\
  4131. 1 \n\
  4132. {-- | x in Naturals}
  4133. 2 \n\
  4134. x '''
  4135. ucode_str = '''\
  4136. ⎧1 │ ⎫\n\
  4137. ⎪── │ x ∊ ℕ⎪\n\
  4138. ⎨ 2 │ ⎬\n\
  4139. ⎪x │ ⎪\n\
  4140. ⎩ │ ⎭'''
  4141. assert pretty(imgset) == ascii_str
  4142. assert upretty(imgset) == ucode_str
  4143. imgset = ImageSet(Lambda((x, y), 1/(x + y)**2), S.Naturals, S.Naturals)
  4144. ascii_str = '''\
  4145. 1 \n\
  4146. {-------- | x in Naturals, y in Naturals}
  4147. 2 \n\
  4148. (x + y) '''
  4149. ucode_str = '''\
  4150. ⎧ 1 │ ⎫
  4151. ⎪──────── │ x ∊ ℕ, y ∊ ℕ⎪
  4152. ⎨ 2 │ ⎬
  4153. ⎪(x + y) │ ⎪
  4154. ⎩ │ ⎭'''
  4155. assert pretty(imgset) == ascii_str
  4156. assert upretty(imgset) == ucode_str
  4157. def test_pretty_ConditionSet():
  4158. ascii_str = '{x | x in (-oo, oo) and sin(x) = 0}'
  4159. ucode_str = '{x │ x ∊ ℝ ∧ (sin(x) = 0)}'
  4160. assert pretty(ConditionSet(x, Eq(sin(x), 0), S.Reals)) == ascii_str
  4161. assert upretty(ConditionSet(x, Eq(sin(x), 0), S.Reals)) == ucode_str
  4162. assert pretty(ConditionSet(x, Contains(x, S.Reals, evaluate=False), FiniteSet(1))) == '{1}'
  4163. assert upretty(ConditionSet(x, Contains(x, S.Reals, evaluate=False), FiniteSet(1))) == '{1}'
  4164. assert pretty(ConditionSet(x, And(x > 1, x < -1), FiniteSet(1, 2, 3))) == "EmptySet"
  4165. assert upretty(ConditionSet(x, And(x > 1, x < -1), FiniteSet(1, 2, 3))) == "∅"
  4166. assert pretty(ConditionSet(x, Or(x > 1, x < -1), FiniteSet(1, 2))) == '{2}'
  4167. assert upretty(ConditionSet(x, Or(x > 1, x < -1), FiniteSet(1, 2))) == '{2}'
  4168. condset = ConditionSet(x, 1/x**2 > 0)
  4169. ascii_str = '''\
  4170. 1 \n\
  4171. {x | -- > 0}
  4172. 2 \n\
  4173. x '''
  4174. ucode_str = '''\
  4175. ⎧ │ ⎛1 ⎞⎫
  4176. ⎪x │ ⎜── > 0⎟⎪
  4177. ⎨ │ ⎜ 2 ⎟⎬
  4178. ⎪ │ ⎝x ⎠⎪
  4179. ⎩ │ ⎭'''
  4180. assert pretty(condset) == ascii_str
  4181. assert upretty(condset) == ucode_str
  4182. condset = ConditionSet(x, 1/x**2 > 0, S.Reals)
  4183. ascii_str = '''\
  4184. 1 \n\
  4185. {x | x in (-oo, oo) and -- > 0}
  4186. 2 \n\
  4187. x '''
  4188. ucode_str = '''\
  4189. ⎧ │ ⎛1 ⎞⎫
  4190. ⎪x │ x ∊ ℝ ∧ ⎜── > 0⎟⎪
  4191. ⎨ │ ⎜ 2 ⎟⎬
  4192. ⎪ │ ⎝x ⎠⎪
  4193. ⎩ │ ⎭'''
  4194. assert pretty(condset) == ascii_str
  4195. assert upretty(condset) == ucode_str
  4196. def test_pretty_ComplexRegion():
  4197. from sympy.sets.fancysets import ComplexRegion
  4198. cregion = ComplexRegion(Interval(3, 5)*Interval(4, 6))
  4199. ascii_str = '{x + y*I | x, y in [3, 5] x [4, 6]}'
  4200. ucode_str = '{x + y⋅ⅈ │ x, y ∊ [3, 5] × [4, 6]}'
  4201. assert pretty(cregion) == ascii_str
  4202. assert upretty(cregion) == ucode_str
  4203. cregion = ComplexRegion(Interval(0, 1)*Interval(0, 2*pi), polar=True)
  4204. ascii_str = '{r*(I*sin(theta) + cos(theta)) | r, theta in [0, 1] x [0, 2*pi)}'
  4205. ucode_str = '{r⋅(ⅈ⋅sin(θ) + cos(θ)) │ r, θ ∊ [0, 1] × [0, 2⋅π)}'
  4206. assert pretty(cregion) == ascii_str
  4207. assert upretty(cregion) == ucode_str
  4208. cregion = ComplexRegion(Interval(3, 1/a**2)*Interval(4, 6))
  4209. ascii_str = '''\
  4210. 1 \n\
  4211. {x + y*I | x, y in [3, --] x [4, 6]}
  4212. 2 \n\
  4213. a '''
  4214. ucode_str = '''\
  4215. ⎧ │ ⎡ 1 ⎤ ⎫
  4216. ⎪x + y⋅ⅈ │ x, y ∊ ⎢3, ──⎥ × [4, 6]⎪
  4217. ⎨ │ ⎢ 2⎥ ⎬
  4218. ⎪ │ ⎣ a ⎦ ⎪
  4219. ⎩ │ ⎭'''
  4220. assert pretty(cregion) == ascii_str
  4221. assert upretty(cregion) == ucode_str
  4222. cregion = ComplexRegion(Interval(0, 1/a**2)*Interval(0, 2*pi), polar=True)
  4223. ascii_str = '''\
  4224. 1 \n\
  4225. {r*(I*sin(theta) + cos(theta)) | r, theta in [0, --] x [0, 2*pi)}
  4226. 2 \n\
  4227. a '''
  4228. ucode_str = '''\
  4229. ⎧ │ ⎡ 1 ⎤ ⎫
  4230. ⎪r⋅(ⅈ⋅sin(θ) + cos(θ)) │ r, θ ∊ ⎢0, ──⎥ × [0, 2⋅π)⎪
  4231. ⎨ │ ⎢ 2⎥ ⎬
  4232. ⎪ │ ⎣ a ⎦ ⎪
  4233. ⎩ │ ⎭'''
  4234. assert pretty(cregion) == ascii_str
  4235. assert upretty(cregion) == ucode_str
  4236. def test_pretty_Union_issue_10414():
  4237. a, b = Interval(2, 3), Interval(4, 7)
  4238. ucode_str = '[2, 3] ∪ [4, 7]'
  4239. ascii_str = '[2, 3] U [4, 7]'
  4240. assert upretty(Union(a, b)) == ucode_str
  4241. assert pretty(Union(a, b)) == ascii_str
  4242. def test_pretty_Intersection_issue_10414():
  4243. x, y, z, w = symbols('x, y, z, w')
  4244. a, b = Interval(x, y), Interval(z, w)
  4245. ucode_str = '[x, y] ∩ [z, w]'
  4246. ascii_str = '[x, y] n [z, w]'
  4247. assert upretty(Intersection(a, b)) == ucode_str
  4248. assert pretty(Intersection(a, b)) == ascii_str
  4249. def test_ProductSet_exponent():
  4250. ucode_str = ' 1\n[0, 1] '
  4251. assert upretty(Interval(0, 1)**1) == ucode_str
  4252. ucode_str = ' 2\n[0, 1] '
  4253. assert upretty(Interval(0, 1)**2) == ucode_str
  4254. def test_ProductSet_parenthesis():
  4255. ucode_str = '([4, 7] × {1, 2}) ∪ ([2, 3] × [4, 7])'
  4256. a, b = Interval(2, 3), Interval(4, 7)
  4257. assert upretty(Union(a*b, b*FiniteSet(1, 2))) == ucode_str
  4258. def test_ProductSet_prod_char_issue_10413():
  4259. ascii_str = '[2, 3] x [4, 7]'
  4260. ucode_str = '[2, 3] × [4, 7]'
  4261. a, b = Interval(2, 3), Interval(4, 7)
  4262. assert pretty(a*b) == ascii_str
  4263. assert upretty(a*b) == ucode_str
  4264. def test_pretty_sequences():
  4265. s1 = SeqFormula(a**2, (0, oo))
  4266. s2 = SeqPer((1, 2))
  4267. ascii_str = '[0, 1, 4, 9, ...]'
  4268. ucode_str = '[0, 1, 4, 9, …]'
  4269. assert pretty(s1) == ascii_str
  4270. assert upretty(s1) == ucode_str
  4271. ascii_str = '[1, 2, 1, 2, ...]'
  4272. ucode_str = '[1, 2, 1, 2, …]'
  4273. assert pretty(s2) == ascii_str
  4274. assert upretty(s2) == ucode_str
  4275. s3 = SeqFormula(a**2, (0, 2))
  4276. s4 = SeqPer((1, 2), (0, 2))
  4277. ascii_str = '[0, 1, 4]'
  4278. ucode_str = '[0, 1, 4]'
  4279. assert pretty(s3) == ascii_str
  4280. assert upretty(s3) == ucode_str
  4281. ascii_str = '[1, 2, 1]'
  4282. ucode_str = '[1, 2, 1]'
  4283. assert pretty(s4) == ascii_str
  4284. assert upretty(s4) == ucode_str
  4285. s5 = SeqFormula(a**2, (-oo, 0))
  4286. s6 = SeqPer((1, 2), (-oo, 0))
  4287. ascii_str = '[..., 9, 4, 1, 0]'
  4288. ucode_str = '[…, 9, 4, 1, 0]'
  4289. assert pretty(s5) == ascii_str
  4290. assert upretty(s5) == ucode_str
  4291. ascii_str = '[..., 2, 1, 2, 1]'
  4292. ucode_str = '[…, 2, 1, 2, 1]'
  4293. assert pretty(s6) == ascii_str
  4294. assert upretty(s6) == ucode_str
  4295. ascii_str = '[1, 3, 5, 11, ...]'
  4296. ucode_str = '[1, 3, 5, 11, …]'
  4297. assert pretty(SeqAdd(s1, s2)) == ascii_str
  4298. assert upretty(SeqAdd(s1, s2)) == ucode_str
  4299. ascii_str = '[1, 3, 5]'
  4300. ucode_str = '[1, 3, 5]'
  4301. assert pretty(SeqAdd(s3, s4)) == ascii_str
  4302. assert upretty(SeqAdd(s3, s4)) == ucode_str
  4303. ascii_str = '[..., 11, 5, 3, 1]'
  4304. ucode_str = '[…, 11, 5, 3, 1]'
  4305. assert pretty(SeqAdd(s5, s6)) == ascii_str
  4306. assert upretty(SeqAdd(s5, s6)) == ucode_str
  4307. ascii_str = '[0, 2, 4, 18, ...]'
  4308. ucode_str = '[0, 2, 4, 18, …]'
  4309. assert pretty(SeqMul(s1, s2)) == ascii_str
  4310. assert upretty(SeqMul(s1, s2)) == ucode_str
  4311. ascii_str = '[0, 2, 4]'
  4312. ucode_str = '[0, 2, 4]'
  4313. assert pretty(SeqMul(s3, s4)) == ascii_str
  4314. assert upretty(SeqMul(s3, s4)) == ucode_str
  4315. ascii_str = '[..., 18, 4, 2, 0]'
  4316. ucode_str = '[…, 18, 4, 2, 0]'
  4317. assert pretty(SeqMul(s5, s6)) == ascii_str
  4318. assert upretty(SeqMul(s5, s6)) == ucode_str
  4319. # Sequences with symbolic limits, issue 12629
  4320. s7 = SeqFormula(a**2, (a, 0, x))
  4321. raises(NotImplementedError, lambda: pretty(s7))
  4322. raises(NotImplementedError, lambda: upretty(s7))
  4323. b = Symbol('b')
  4324. s8 = SeqFormula(b*a**2, (a, 0, 2))
  4325. ascii_str = '[0, b, 4*b]'
  4326. ucode_str = '[0, b, 4⋅b]'
  4327. assert pretty(s8) == ascii_str
  4328. assert upretty(s8) == ucode_str
  4329. def test_pretty_FourierSeries():
  4330. f = fourier_series(x, (x, -pi, pi))
  4331. ascii_str = \
  4332. """\
  4333. 2*sin(3*x) \n\
  4334. 2*sin(x) - sin(2*x) + ---------- + ...\n\
  4335. 3 \
  4336. """
  4337. ucode_str = \
  4338. """\
  4339. 2⋅sin(3⋅x) \n\
  4340. 2⋅sin(x) - sin(2⋅x) + ────────── + …\n\
  4341. 3 \
  4342. """
  4343. assert pretty(f) == ascii_str
  4344. assert upretty(f) == ucode_str
  4345. def test_pretty_FormalPowerSeries():
  4346. f = fps(log(1 + x))
  4347. ascii_str = \
  4348. """\
  4349. oo \n\
  4350. ____ \n\
  4351. \\ ` \n\
  4352. \\ -k k \n\
  4353. \\ -(-1) *x \n\
  4354. / -----------\n\
  4355. / k \n\
  4356. /___, \n\
  4357. k = 1 \
  4358. """
  4359. ucode_str = \
  4360. """\
  4361. ∞ \n\
  4362. ____ \n\
  4363. ╲ \n\
  4364. ╲ -k k \n\
  4365. ╲ -(-1) ⋅x \n\
  4366. ╱ ───────────\n\
  4367. ╱ k \n\
  4368. ╱ \n\
  4369. ‾‾‾‾ \n\
  4370. k = 1 \
  4371. """
  4372. assert pretty(f) == ascii_str
  4373. assert upretty(f) == ucode_str
  4374. def test_pretty_limits():
  4375. expr = Limit(x, x, oo)
  4376. ascii_str = \
  4377. """\
  4378. lim x\n\
  4379. x->oo \
  4380. """
  4381. ucode_str = \
  4382. """\
  4383. lim x\n\
  4384. x─→∞ \
  4385. """
  4386. assert pretty(expr) == ascii_str
  4387. assert upretty(expr) == ucode_str
  4388. expr = Limit(x**2, x, 0)
  4389. ascii_str = \
  4390. """\
  4391. 2\n\
  4392. lim x \n\
  4393. x->0+ \
  4394. """
  4395. ucode_str = \
  4396. """\
  4397. 2\n\
  4398. lim x \n\
  4399. x─→0⁺ \
  4400. """
  4401. assert pretty(expr) == ascii_str
  4402. assert upretty(expr) == ucode_str
  4403. expr = Limit(1/x, x, 0)
  4404. ascii_str = \
  4405. """\
  4406. 1\n\
  4407. lim -\n\
  4408. x->0+x\
  4409. """
  4410. ucode_str = \
  4411. """\
  4412. 1\n\
  4413. lim ─\n\
  4414. x─→0⁺x\
  4415. """
  4416. assert pretty(expr) == ascii_str
  4417. assert upretty(expr) == ucode_str
  4418. expr = Limit(sin(x)/x, x, 0)
  4419. ascii_str = \
  4420. """\
  4421. /sin(x)\\\n\
  4422. lim |------|\n\
  4423. x->0+\\ x /\
  4424. """
  4425. ucode_str = \
  4426. """\
  4427. ⎛sin(x)⎞\n\
  4428. lim ⎜──────⎟\n\
  4429. x─→0⁺⎝ x ⎠\
  4430. """
  4431. assert pretty(expr) == ascii_str
  4432. assert upretty(expr) == ucode_str
  4433. expr = Limit(sin(x)/x, x, 0, "-")
  4434. ascii_str = \
  4435. """\
  4436. /sin(x)\\\n\
  4437. lim |------|\n\
  4438. x->0-\\ x /\
  4439. """
  4440. ucode_str = \
  4441. """\
  4442. ⎛sin(x)⎞\n\
  4443. lim ⎜──────⎟\n\
  4444. x─→0⁻⎝ x ⎠\
  4445. """
  4446. assert pretty(expr) == ascii_str
  4447. assert upretty(expr) == ucode_str
  4448. expr = Limit(x + sin(x), x, 0)
  4449. ascii_str = \
  4450. """\
  4451. lim (x + sin(x))\n\
  4452. x->0+ \
  4453. """
  4454. ucode_str = \
  4455. """\
  4456. lim (x + sin(x))\n\
  4457. x─→0⁺ \
  4458. """
  4459. assert pretty(expr) == ascii_str
  4460. assert upretty(expr) == ucode_str
  4461. expr = Limit(x, x, 0)**2
  4462. ascii_str = \
  4463. """\
  4464. 2\n\
  4465. / lim x\\ \n\
  4466. \\x->0+ / \
  4467. """
  4468. ucode_str = \
  4469. """\
  4470. 2\n\
  4471. ⎛ lim x⎞ \n\
  4472. ⎝x─→0⁺ ⎠ \
  4473. """
  4474. assert pretty(expr) == ascii_str
  4475. assert upretty(expr) == ucode_str
  4476. expr = Limit(x*Limit(y/2,y,0), x, 0)
  4477. ascii_str = \
  4478. """\
  4479. / /y\\\\\n\
  4480. lim |x* lim |-||\n\
  4481. x->0+\\ y->0+\\2//\
  4482. """
  4483. ucode_str = \
  4484. """\
  4485. ⎛ ⎛y⎞⎞\n\
  4486. lim ⎜x⋅ lim ⎜─⎟⎟\n\
  4487. x─→0⁺⎝ y─→0⁺⎝2⎠⎠\
  4488. """
  4489. assert pretty(expr) == ascii_str
  4490. assert upretty(expr) == ucode_str
  4491. expr = 2*Limit(x*Limit(y/2,y,0), x, 0)
  4492. ascii_str = \
  4493. """\
  4494. / /y\\\\\n\
  4495. 2* lim |x* lim |-||\n\
  4496. x->0+\\ y->0+\\2//\
  4497. """
  4498. ucode_str = \
  4499. """\
  4500. ⎛ ⎛y⎞⎞\n\
  4501. 2⋅ lim ⎜x⋅ lim ⎜─⎟⎟\n\
  4502. x─→0⁺⎝ y─→0⁺⎝2⎠⎠\
  4503. """
  4504. assert pretty(expr) == ascii_str
  4505. assert upretty(expr) == ucode_str
  4506. expr = Limit(sin(x), x, 0, dir='+-')
  4507. ascii_str = \
  4508. """\
  4509. lim sin(x)\n\
  4510. x->0 \
  4511. """
  4512. ucode_str = \
  4513. """\
  4514. lim sin(x)\n\
  4515. x─→0 \
  4516. """
  4517. assert pretty(expr) == ascii_str
  4518. assert upretty(expr) == ucode_str
  4519. def test_pretty_ComplexRootOf():
  4520. expr = rootof(x**5 + 11*x - 2, 0)
  4521. ascii_str = \
  4522. """\
  4523. / 5 \\\n\
  4524. CRootOf\\x + 11*x - 2, 0/\
  4525. """
  4526. ucode_str = \
  4527. """\
  4528. ⎛ 5 ⎞\n\
  4529. CRootOf⎝x + 11⋅x - 2, 0⎠\
  4530. """
  4531. assert pretty(expr) == ascii_str
  4532. assert upretty(expr) == ucode_str
  4533. def test_pretty_RootSum():
  4534. expr = RootSum(x**5 + 11*x - 2, auto=False)
  4535. ascii_str = \
  4536. """\
  4537. / 5 \\\n\
  4538. RootSum\\x + 11*x - 2/\
  4539. """
  4540. ucode_str = \
  4541. """\
  4542. ⎛ 5 ⎞\n\
  4543. RootSum⎝x + 11⋅x - 2⎠\
  4544. """
  4545. assert pretty(expr) == ascii_str
  4546. assert upretty(expr) == ucode_str
  4547. expr = RootSum(x**5 + 11*x - 2, Lambda(z, exp(z)))
  4548. ascii_str = \
  4549. """\
  4550. / 5 z\\\n\
  4551. RootSum\\x + 11*x - 2, z -> e /\
  4552. """
  4553. ucode_str = \
  4554. """\
  4555. ⎛ 5 z⎞\n\
  4556. RootSum⎝x + 11⋅x - 2, z ↦ ℯ ⎠\
  4557. """
  4558. assert pretty(expr) == ascii_str
  4559. assert upretty(expr) == ucode_str
  4560. def test_GroebnerBasis():
  4561. expr = groebner([], x, y)
  4562. ascii_str = \
  4563. """\
  4564. GroebnerBasis([], x, y, domain=ZZ, order=lex)\
  4565. """
  4566. ucode_str = \
  4567. """\
  4568. GroebnerBasis([], x, y, domain=ℤ, order=lex)\
  4569. """
  4570. assert pretty(expr) == ascii_str
  4571. assert upretty(expr) == ucode_str
  4572. F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
  4573. expr = groebner(F, x, y, order='grlex')
  4574. ascii_str = \
  4575. """\
  4576. /[ 2 2 ] \\\n\
  4577. GroebnerBasis\\[x - x - 3*y + 1, y - 2*x + y - 1], x, y, domain=ZZ, order=grlex/\
  4578. """
  4579. ucode_str = \
  4580. """\
  4581. ⎛⎡ 2 2 ⎤ ⎞\n\
  4582. GroebnerBasis⎝⎣x - x - 3⋅y + 1, y - 2⋅x + y - 1⎦, x, y, domain=ℤ, order=grlex⎠\
  4583. """
  4584. assert pretty(expr) == ascii_str
  4585. assert upretty(expr) == ucode_str
  4586. expr = expr.fglm('lex')
  4587. ascii_str = \
  4588. """\
  4589. /[ 2 4 3 2 ] \\\n\
  4590. GroebnerBasis\\[2*x - y - y + 1, y + 2*y - 3*y - 16*y + 7], x, y, domain=ZZ, order=lex/\
  4591. """
  4592. ucode_str = \
  4593. """\
  4594. ⎛⎡ 2 4 3 2 ⎤ ⎞\n\
  4595. GroebnerBasis⎝⎣2⋅x - y - y + 1, y + 2⋅y - 3⋅y - 16⋅y + 7⎦, x, y, domain=ℤ, order=lex⎠\
  4596. """
  4597. assert pretty(expr) == ascii_str
  4598. assert upretty(expr) == ucode_str
  4599. def test_pretty_UniversalSet():
  4600. assert pretty(S.UniversalSet) == "UniversalSet"
  4601. assert upretty(S.UniversalSet) == '𝕌'
  4602. def test_pretty_Boolean():
  4603. expr = Not(x, evaluate=False)
  4604. assert pretty(expr) == "Not(x)"
  4605. assert upretty(expr) == "¬x"
  4606. expr = And(x, y)
  4607. assert pretty(expr) == "And(x, y)"
  4608. assert upretty(expr) == "x ∧ y"
  4609. expr = Or(x, y)
  4610. assert pretty(expr) == "Or(x, y)"
  4611. assert upretty(expr) == "x ∨ y"
  4612. syms = symbols('a:f')
  4613. expr = And(*syms)
  4614. assert pretty(expr) == "And(a, b, c, d, e, f)"
  4615. assert upretty(expr) == "a ∧ b ∧ c ∧ d ∧ e ∧ f"
  4616. expr = Or(*syms)
  4617. assert pretty(expr) == "Or(a, b, c, d, e, f)"
  4618. assert upretty(expr) == "a ∨ b ∨ c ∨ d ∨ e ∨ f"
  4619. expr = Xor(x, y, evaluate=False)
  4620. assert pretty(expr) == "Xor(x, y)"
  4621. assert upretty(expr) == "x ⊻ y"
  4622. expr = Nand(x, y, evaluate=False)
  4623. assert pretty(expr) == "Nand(x, y)"
  4624. assert upretty(expr) == "x ⊼ y"
  4625. expr = Nor(x, y, evaluate=False)
  4626. assert pretty(expr) == "Nor(x, y)"
  4627. assert upretty(expr) == "x ⊽ y"
  4628. expr = Implies(x, y, evaluate=False)
  4629. assert pretty(expr) == "Implies(x, y)"
  4630. assert upretty(expr) == "x → y"
  4631. # don't sort args
  4632. expr = Implies(y, x, evaluate=False)
  4633. assert pretty(expr) == "Implies(y, x)"
  4634. assert upretty(expr) == "y → x"
  4635. expr = Equivalent(x, y, evaluate=False)
  4636. assert pretty(expr) == "Equivalent(x, y)"
  4637. assert upretty(expr) == "x ⇔ y"
  4638. expr = Equivalent(y, x, evaluate=False)
  4639. assert pretty(expr) == "Equivalent(x, y)"
  4640. assert upretty(expr) == "x ⇔ y"
  4641. def test_pretty_Domain():
  4642. expr = FF(23)
  4643. assert pretty(expr) == "GF(23)"
  4644. assert upretty(expr) == "ℤ₂₃"
  4645. expr = ZZ
  4646. assert pretty(expr) == "ZZ"
  4647. assert upretty(expr) == "ℤ"
  4648. expr = QQ
  4649. assert pretty(expr) == "QQ"
  4650. assert upretty(expr) == "ℚ"
  4651. expr = RR
  4652. assert pretty(expr) == "RR"
  4653. assert upretty(expr) == "ℝ"
  4654. expr = QQ[x]
  4655. assert pretty(expr) == "QQ[x]"
  4656. assert upretty(expr) == "ℚ[x]"
  4657. expr = QQ[x, y]
  4658. assert pretty(expr) == "QQ[x, y]"
  4659. assert upretty(expr) == "ℚ[x, y]"
  4660. expr = ZZ.frac_field(x)
  4661. assert pretty(expr) == "ZZ(x)"
  4662. assert upretty(expr) == "ℤ(x)"
  4663. expr = ZZ.frac_field(x, y)
  4664. assert pretty(expr) == "ZZ(x, y)"
  4665. assert upretty(expr) == "ℤ(x, y)"
  4666. expr = QQ.poly_ring(x, y, order=grlex)
  4667. assert pretty(expr) == "QQ[x, y, order=grlex]"
  4668. assert upretty(expr) == "ℚ[x, y, order=grlex]"
  4669. expr = QQ.poly_ring(x, y, order=ilex)
  4670. assert pretty(expr) == "QQ[x, y, order=ilex]"
  4671. assert upretty(expr) == "ℚ[x, y, order=ilex]"
  4672. def test_pretty_prec():
  4673. assert xpretty(S("0.3"), full_prec=True, wrap_line=False) == "0.300000000000000"
  4674. assert xpretty(S("0.3"), full_prec="auto", wrap_line=False) == "0.300000000000000"
  4675. assert xpretty(S("0.3"), full_prec=False, wrap_line=False) == "0.3"
  4676. assert xpretty(S("0.3")*x, full_prec=True, use_unicode=False, wrap_line=False) in [
  4677. "0.300000000000000*x",
  4678. "x*0.300000000000000"
  4679. ]
  4680. assert xpretty(S("0.3")*x, full_prec="auto", use_unicode=False, wrap_line=False) in [
  4681. "0.3*x",
  4682. "x*0.3"
  4683. ]
  4684. assert xpretty(S("0.3")*x, full_prec=False, use_unicode=False, wrap_line=False) in [
  4685. "0.3*x",
  4686. "x*0.3"
  4687. ]
  4688. def test_pprint():
  4689. import sys
  4690. from io import StringIO
  4691. fd = StringIO()
  4692. sso = sys.stdout
  4693. sys.stdout = fd
  4694. try:
  4695. pprint(pi, use_unicode=False, wrap_line=False)
  4696. finally:
  4697. sys.stdout = sso
  4698. assert fd.getvalue() == 'pi\n'
  4699. def test_pretty_class():
  4700. """Test that the printer dispatcher correctly handles classes."""
  4701. class C:
  4702. pass # C has no .__class__ and this was causing problems
  4703. class D:
  4704. pass
  4705. assert pretty( C ) == str( C )
  4706. assert pretty( D ) == str( D )
  4707. def test_pretty_no_wrap_line():
  4708. huge_expr = 0
  4709. for i in range(20):
  4710. huge_expr += i*sin(i + x)
  4711. assert xpretty(huge_expr ).find('\n') != -1
  4712. assert xpretty(huge_expr, wrap_line=False).find('\n') == -1
  4713. def test_settings():
  4714. raises(TypeError, lambda: pretty(S(4), method="garbage"))
  4715. def test_pretty_sum():
  4716. from sympy.abc import x, a, b, k, m, n
  4717. expr = Sum(k**k, (k, 0, n))
  4718. ascii_str = \
  4719. """\
  4720. n \n\
  4721. ___ \n\
  4722. \\ ` \n\
  4723. \\ k\n\
  4724. / k \n\
  4725. /__, \n\
  4726. k = 0 \
  4727. """
  4728. ucode_str = \
  4729. """\
  4730. n \n\
  4731. ___ \n\
  4732. ╲ \n\
  4733. ╲ k\n\
  4734. ╱ k \n\
  4735. ╱ \n\
  4736. ‾‾‾ \n\
  4737. k = 0 \
  4738. """
  4739. assert pretty(expr) == ascii_str
  4740. assert upretty(expr) == ucode_str
  4741. expr = Sum(k**k, (k, oo, n))
  4742. ascii_str = \
  4743. """\
  4744. n \n\
  4745. ___ \n\
  4746. \\ ` \n\
  4747. \\ k\n\
  4748. / k \n\
  4749. /__, \n\
  4750. k = oo \
  4751. """
  4752. ucode_str = \
  4753. """\
  4754. n \n\
  4755. ___ \n\
  4756. ╲ \n\
  4757. ╲ k\n\
  4758. ╱ k \n\
  4759. ╱ \n\
  4760. ‾‾‾ \n\
  4761. k = ∞ \
  4762. """
  4763. assert pretty(expr) == ascii_str
  4764. assert upretty(expr) == ucode_str
  4765. expr = Sum(k**(Integral(x**n, (x, -oo, oo))), (k, 0, n**n))
  4766. ascii_str = \
  4767. """\
  4768. n \n\
  4769. n \n\
  4770. ______ \n\
  4771. \\ ` \n\
  4772. \\ oo \n\
  4773. \\ / \n\
  4774. \\ | \n\
  4775. \\ | n \n\
  4776. ) | x dx\n\
  4777. / | \n\
  4778. / / \n\
  4779. / -oo \n\
  4780. / k \n\
  4781. /_____, \n\
  4782. k = 0 \
  4783. """
  4784. ucode_str = \
  4785. """\
  4786. n \n\
  4787. n \n\
  4788. ______ \n\
  4789. ╲ \n\
  4790. ╲ \n\
  4791. ╲ ∞ \n\
  4792. ╲ ⌠ \n\
  4793. ╲ ⎮ n \n\
  4794. ╱ ⎮ x dx\n\
  4795. ╱ ⌡ \n\
  4796. ╱ -∞ \n\
  4797. ╱ k \n\
  4798. ╱ \n\
  4799. ‾‾‾‾‾‾ \n\
  4800. k = 0 \
  4801. """
  4802. assert pretty(expr) == ascii_str
  4803. assert upretty(expr) == ucode_str
  4804. expr = Sum(k**(
  4805. Integral(x**n, (x, -oo, oo))), (k, 0, Integral(x**x, (x, -oo, oo))))
  4806. ascii_str = \
  4807. """\
  4808. oo \n\
  4809. / \n\
  4810. | \n\
  4811. | x \n\
  4812. | x dx \n\
  4813. | \n\
  4814. / \n\
  4815. -oo \n\
  4816. ______ \n\
  4817. \\ ` \n\
  4818. \\ oo \n\
  4819. \\ / \n\
  4820. \\ | \n\
  4821. \\ | n \n\
  4822. ) | x dx\n\
  4823. / | \n\
  4824. / / \n\
  4825. / -oo \n\
  4826. / k \n\
  4827. /_____, \n\
  4828. k = 0 \
  4829. """
  4830. ucode_str = \
  4831. """\
  4832. ∞ \n\
  4833. ⌠ \n\
  4834. ⎮ x \n\
  4835. ⎮ x dx \n\
  4836. ⌡ \n\
  4837. -∞ \n\
  4838. ______ \n\
  4839. ╲ \n\
  4840. ╲ \n\
  4841. ╲ ∞ \n\
  4842. ╲ ⌠ \n\
  4843. ╲ ⎮ n \n\
  4844. ╱ ⎮ x dx\n\
  4845. ╱ ⌡ \n\
  4846. ╱ -∞ \n\
  4847. ╱ k \n\
  4848. ╱ \n\
  4849. ‾‾‾‾‾‾ \n\
  4850. k = 0 \
  4851. """
  4852. assert pretty(expr) == ascii_str
  4853. assert upretty(expr) == ucode_str
  4854. expr = Sum(k**(Integral(x**n, (x, -oo, oo))), (
  4855. k, x + n + x**2 + n**2 + (x/n) + (1/x), Integral(x**x, (x, -oo, oo))))
  4856. ascii_str = \
  4857. """\
  4858. oo \n\
  4859. / \n\
  4860. | \n\
  4861. | x \n\
  4862. | x dx \n\
  4863. | \n\
  4864. / \n\
  4865. -oo \n\
  4866. ______ \n\
  4867. \\ ` \n\
  4868. \\ oo \n\
  4869. \\ / \n\
  4870. \\ | \n\
  4871. \\ | n \n\
  4872. ) | x dx\n\
  4873. / | \n\
  4874. / / \n\
  4875. / -oo \n\
  4876. / k \n\
  4877. /_____, \n\
  4878. 2 2 1 x \n\
  4879. k = n + n + x + x + - + - \n\
  4880. x n \
  4881. """
  4882. ucode_str = \
  4883. """\
  4884. ∞ \n\
  4885. ⌠ \n\
  4886. ⎮ x \n\
  4887. ⎮ x dx \n\
  4888. ⌡ \n\
  4889. -∞ \n\
  4890. ______ \n\
  4891. ╲ \n\
  4892. ╲ \n\
  4893. ╲ ∞ \n\
  4894. ╲ ⌠ \n\
  4895. ╲ ⎮ n \n\
  4896. ╱ ⎮ x dx\n\
  4897. ╱ ⌡ \n\
  4898. ╱ -∞ \n\
  4899. ╱ k \n\
  4900. ╱ \n\
  4901. ‾‾‾‾‾‾ \n\
  4902. 2 2 1 x \n\
  4903. k = n + n + x + x + ─ + ─ \n\
  4904. x n \
  4905. """
  4906. assert pretty(expr) == ascii_str
  4907. assert upretty(expr) == ucode_str
  4908. expr = Sum(k**(
  4909. Integral(x**n, (x, -oo, oo))), (k, 0, x + n + x**2 + n**2 + (x/n) + (1/x)))
  4910. ascii_str = \
  4911. """\
  4912. 2 2 1 x \n\
  4913. n + n + x + x + - + - \n\
  4914. x n \n\
  4915. ______ \n\
  4916. \\ ` \n\
  4917. \\ oo \n\
  4918. \\ / \n\
  4919. \\ | \n\
  4920. \\ | n \n\
  4921. ) | x dx\n\
  4922. / | \n\
  4923. / / \n\
  4924. / -oo \n\
  4925. / k \n\
  4926. /_____, \n\
  4927. k = 0 \
  4928. """
  4929. ucode_str = \
  4930. """\
  4931. 2 2 1 x \n\
  4932. n + n + x + x + ─ + ─ \n\
  4933. x n \n\
  4934. ______ \n\
  4935. ╲ \n\
  4936. ╲ \n\
  4937. ╲ ∞ \n\
  4938. ╲ ⌠ \n\
  4939. ╲ ⎮ n \n\
  4940. ╱ ⎮ x dx\n\
  4941. ╱ ⌡ \n\
  4942. ╱ -∞ \n\
  4943. ╱ k \n\
  4944. ╱ \n\
  4945. ‾‾‾‾‾‾ \n\
  4946. k = 0 \
  4947. """
  4948. assert pretty(expr) == ascii_str
  4949. assert upretty(expr) == ucode_str
  4950. expr = Sum(x, (x, 0, oo))
  4951. ascii_str = \
  4952. """\
  4953. oo \n\
  4954. __ \n\
  4955. \\ ` \n\
  4956. ) x\n\
  4957. /_, \n\
  4958. x = 0 \
  4959. """
  4960. ucode_str = \
  4961. """\
  4962. ∞ \n\
  4963. ___ \n\
  4964. ╲ \n\
  4965. ╲ \n\
  4966. ╱ x\n\
  4967. ╱ \n\
  4968. ‾‾‾ \n\
  4969. x = 0 \
  4970. """
  4971. assert pretty(expr) == ascii_str
  4972. assert upretty(expr) == ucode_str
  4973. expr = Sum(x**2, (x, 0, oo))
  4974. ascii_str = \
  4975. """\
  4976. oo \n\
  4977. ___ \n\
  4978. \\ ` \n\
  4979. \\ 2\n\
  4980. / x \n\
  4981. /__, \n\
  4982. x = 0 \
  4983. """
  4984. ucode_str = \
  4985. """\
  4986. ∞ \n\
  4987. ___ \n\
  4988. ╲ \n\
  4989. ╲ 2\n\
  4990. ╱ x \n\
  4991. ╱ \n\
  4992. ‾‾‾ \n\
  4993. x = 0 \
  4994. """
  4995. assert pretty(expr) == ascii_str
  4996. assert upretty(expr) == ucode_str
  4997. expr = Sum(x/2, (x, 0, oo))
  4998. ascii_str = \
  4999. """\
  5000. oo \n\
  5001. ___ \n\
  5002. \\ ` \n\
  5003. \\ x\n\
  5004. ) -\n\
  5005. / 2\n\
  5006. /__, \n\
  5007. x = 0 \
  5008. """
  5009. ucode_str = \
  5010. """\
  5011. ∞ \n\
  5012. ____ \n\
  5013. ╲ \n\
  5014. ╲ \n\
  5015. ╲ x\n\
  5016. ╱ ─\n\
  5017. ╱ 2\n\
  5018. ╱ \n\
  5019. ‾‾‾‾ \n\
  5020. x = 0 \
  5021. """
  5022. assert pretty(expr) == ascii_str
  5023. assert upretty(expr) == ucode_str
  5024. expr = Sum(x**3/2, (x, 0, oo))
  5025. ascii_str = \
  5026. """\
  5027. oo \n\
  5028. ____ \n\
  5029. \\ ` \n\
  5030. \\ 3\n\
  5031. \\ x \n\
  5032. / --\n\
  5033. / 2 \n\
  5034. /___, \n\
  5035. x = 0 \
  5036. """
  5037. ucode_str = \
  5038. """\
  5039. ∞ \n\
  5040. ____ \n\
  5041. ╲ \n\
  5042. ╲ 3\n\
  5043. ╲ x \n\
  5044. ╱ ──\n\
  5045. ╱ 2 \n\
  5046. ╱ \n\
  5047. ‾‾‾‾ \n\
  5048. x = 0 \
  5049. """
  5050. assert pretty(expr) == ascii_str
  5051. assert upretty(expr) == ucode_str
  5052. expr = Sum((x**3*y**(x/2))**n, (x, 0, oo))
  5053. ascii_str = \
  5054. """\
  5055. oo \n\
  5056. ____ \n\
  5057. \\ ` \n\
  5058. \\ n\n\
  5059. \\ / x\\ \n\
  5060. ) | -| \n\
  5061. / | 3 2| \n\
  5062. / \\x *y / \n\
  5063. /___, \n\
  5064. x = 0 \
  5065. """
  5066. ucode_str = \
  5067. """\
  5068. ∞ \n\
  5069. _____ \n\
  5070. ╲ \n\
  5071. ╲ \n\
  5072. ╲ n\n\
  5073. ╲ ⎛ x⎞ \n\
  5074. ╱ ⎜ ─⎟ \n\
  5075. ╱ ⎜ 3 2⎟ \n\
  5076. ╱ ⎝x ⋅y ⎠ \n\
  5077. ╱ \n\
  5078. ‾‾‾‾‾ \n\
  5079. x = 0 \
  5080. """
  5081. assert pretty(expr) == ascii_str
  5082. assert upretty(expr) == ucode_str
  5083. expr = Sum(1/x**2, (x, 0, oo))
  5084. ascii_str = \
  5085. """\
  5086. oo \n\
  5087. ____ \n\
  5088. \\ ` \n\
  5089. \\ 1 \n\
  5090. \\ --\n\
  5091. / 2\n\
  5092. / x \n\
  5093. /___, \n\
  5094. x = 0 \
  5095. """
  5096. ucode_str = \
  5097. """\
  5098. ∞ \n\
  5099. ____ \n\
  5100. ╲ \n\
  5101. ╲ 1 \n\
  5102. ╲ ──\n\
  5103. ╱ 2\n\
  5104. ╱ x \n\
  5105. ╱ \n\
  5106. ‾‾‾‾ \n\
  5107. x = 0 \
  5108. """
  5109. assert pretty(expr) == ascii_str
  5110. assert upretty(expr) == ucode_str
  5111. expr = Sum(1/y**(a/b), (x, 0, oo))
  5112. ascii_str = \
  5113. """\
  5114. oo \n\
  5115. ____ \n\
  5116. \\ ` \n\
  5117. \\ -a \n\
  5118. \\ ---\n\
  5119. / b \n\
  5120. / y \n\
  5121. /___, \n\
  5122. x = 0 \
  5123. """
  5124. ucode_str = \
  5125. """\
  5126. ∞ \n\
  5127. ____ \n\
  5128. ╲ \n\
  5129. ╲ -a \n\
  5130. ╲ ───\n\
  5131. ╱ b \n\
  5132. ╱ y \n\
  5133. ╱ \n\
  5134. ‾‾‾‾ \n\
  5135. x = 0 \
  5136. """
  5137. assert pretty(expr) == ascii_str
  5138. assert upretty(expr) == ucode_str
  5139. expr = Sum(1/y**(a/b), (x, 0, oo), (y, 1, 2))
  5140. ascii_str = \
  5141. """\
  5142. 2 oo \n\
  5143. ____ ____ \n\
  5144. \\ ` \\ ` \n\
  5145. \\ \\ -a\n\
  5146. \\ \\ --\n\
  5147. / / b \n\
  5148. / / y \n\
  5149. /___, /___, \n\
  5150. y = 1 x = 0 \
  5151. """
  5152. ucode_str = \
  5153. """\
  5154. 2 ∞ \n\
  5155. ____ ____ \n\
  5156. ╲ ╲ \n\
  5157. ╲ ╲ -a\n\
  5158. ╲ ╲ ──\n\
  5159. ╱ ╱ b \n\
  5160. ╱ ╱ y \n\
  5161. ╱ ╱ \n\
  5162. ‾‾‾‾ ‾‾‾‾ \n\
  5163. y = 1 x = 0 \
  5164. """
  5165. expr = Sum(1/(1 + 1/(
  5166. 1 + 1/k)) + 1, (k, 111, 1 + 1/n), (k, 1/(1 + m), oo)) + 1/(1 + 1/k)
  5167. ascii_str = \
  5168. """\
  5169. 1 \n\
  5170. 1 + - \n\
  5171. oo n \n\
  5172. _____ _____ \n\
  5173. \\ ` \\ ` \n\
  5174. \\ \\ / 1 \\ \n\
  5175. \\ \\ |1 + ---------| \n\
  5176. \\ \\ | 1 | 1 \n\
  5177. ) ) | 1 + -----| + -----\n\
  5178. / / | 1| 1\n\
  5179. / / | 1 + -| 1 + -\n\
  5180. / / \\ k/ k\n\
  5181. /____, /____, \n\
  5182. 1 k = 111 \n\
  5183. k = ----- \n\
  5184. m + 1 \
  5185. """
  5186. ucode_str = \
  5187. """\
  5188. 1 \n\
  5189. 1 + ─ \n\
  5190. ∞ n \n\
  5191. ______ ______ \n\
  5192. ╲ ╲ \n\
  5193. ╲ ╲ \n\
  5194. ╲ ╲ ⎛ 1 ⎞ \n\
  5195. ╲ ╲ ⎜1 + ─────────⎟ \n\
  5196. ╲ ╲ ⎜ 1 ⎟ 1 \n\
  5197. ╱ ╱ ⎜ 1 + ─────⎟ + ─────\n\
  5198. ╱ ╱ ⎜ 1⎟ 1\n\
  5199. ╱ ╱ ⎜ 1 + ─⎟ 1 + ─\n\
  5200. ╱ ╱ ⎝ k⎠ k\n\
  5201. ╱ ╱ \n\
  5202. ‾‾‾‾‾‾ ‾‾‾‾‾‾ \n\
  5203. 1 k = 111 \n\
  5204. k = ───── \n\
  5205. m + 1 \
  5206. """
  5207. assert pretty(expr) == ascii_str
  5208. assert upretty(expr) == ucode_str
  5209. def test_units():
  5210. expr = joule
  5211. ascii_str1 = \
  5212. """\
  5213. 2\n\
  5214. kilogram*meter \n\
  5215. ---------------\n\
  5216. 2 \n\
  5217. second \
  5218. """
  5219. unicode_str1 = \
  5220. """\
  5221. 2\n\
  5222. kilogram⋅meter \n\
  5223. ───────────────\n\
  5224. 2 \n\
  5225. second \
  5226. """
  5227. ascii_str2 = \
  5228. """\
  5229. 2\n\
  5230. 3*x*y*kilogram*meter \n\
  5231. ---------------------\n\
  5232. 2 \n\
  5233. second \
  5234. """
  5235. unicode_str2 = \
  5236. """\
  5237. 2\n\
  5238. 3⋅x⋅y⋅kilogram⋅meter \n\
  5239. ─────────────────────\n\
  5240. 2 \n\
  5241. second \
  5242. """
  5243. from sympy.physics.units import kg, m, s
  5244. assert upretty(expr) == "joule"
  5245. assert pretty(expr) == "joule"
  5246. assert upretty(expr.convert_to(kg*m**2/s**2)) == unicode_str1
  5247. assert pretty(expr.convert_to(kg*m**2/s**2)) == ascii_str1
  5248. assert upretty(3*kg*x*m**2*y/s**2) == unicode_str2
  5249. assert pretty(3*kg*x*m**2*y/s**2) == ascii_str2
  5250. def test_pretty_Subs():
  5251. f = Function('f')
  5252. expr = Subs(f(x), x, ph**2)
  5253. ascii_str = \
  5254. """\
  5255. (f(x))| 2\n\
  5256. |x=phi \
  5257. """
  5258. unicode_str = \
  5259. """\
  5260. (f(x))│ 2\n\
  5261. │x=φ \
  5262. """
  5263. assert pretty(expr) == ascii_str
  5264. assert upretty(expr) == unicode_str
  5265. expr = Subs(f(x).diff(x), x, 0)
  5266. ascii_str = \
  5267. """\
  5268. /d \\| \n\
  5269. |--(f(x))|| \n\
  5270. \\dx /|x=0\
  5271. """
  5272. unicode_str = \
  5273. """\
  5274. ⎛d ⎞│ \n\
  5275. ⎜──(f(x))⎟│ \n\
  5276. ⎝dx ⎠│x=0\
  5277. """
  5278. assert pretty(expr) == ascii_str
  5279. assert upretty(expr) == unicode_str
  5280. expr = Subs(f(x).diff(x)/y, (x, y), (0, Rational(1, 2)))
  5281. ascii_str = \
  5282. """\
  5283. /d \\| \n\
  5284. |--(f(x))|| \n\
  5285. |dx || \n\
  5286. |--------|| \n\
  5287. \\ y /|x=0, y=1/2\
  5288. """
  5289. unicode_str = \
  5290. """\
  5291. ⎛d ⎞│ \n\
  5292. ⎜──(f(x))⎟│ \n\
  5293. ⎜dx ⎟│ \n\
  5294. ⎜────────⎟│ \n\
  5295. ⎝ y ⎠│x=0, y=1/2\
  5296. """
  5297. assert pretty(expr) == ascii_str
  5298. assert upretty(expr) == unicode_str
  5299. def test_gammas():
  5300. assert upretty(lowergamma(x, y)) == "γ(x, y)"
  5301. assert upretty(uppergamma(x, y)) == "Γ(x, y)"
  5302. assert xpretty(gamma(x), use_unicode=True) == 'Γ(x)'
  5303. assert xpretty(gamma, use_unicode=True) == 'Γ'
  5304. assert xpretty(symbols('gamma', cls=Function)(x), use_unicode=True) == 'γ(x)'
  5305. assert xpretty(symbols('gamma', cls=Function), use_unicode=True) == 'γ'
  5306. def test_beta():
  5307. assert xpretty(beta(x,y), use_unicode=True) == 'Β(x, y)'
  5308. assert xpretty(beta(x,y), use_unicode=False) == 'B(x, y)'
  5309. assert xpretty(beta, use_unicode=True) == 'Β'
  5310. assert xpretty(beta, use_unicode=False) == 'B'
  5311. mybeta = Function('beta')
  5312. assert xpretty(mybeta(x), use_unicode=True) == 'β(x)'
  5313. assert xpretty(mybeta(x, y, z), use_unicode=False) == 'beta(x, y, z)'
  5314. assert xpretty(mybeta, use_unicode=True) == 'β'
  5315. # test that notation passes to subclasses of the same name only
  5316. def test_function_subclass_different_name():
  5317. class mygamma(gamma):
  5318. pass
  5319. assert xpretty(mygamma, use_unicode=True) == r"mygamma"
  5320. assert xpretty(mygamma(x), use_unicode=True) == r"mygamma(x)"
  5321. def test_SingularityFunction():
  5322. assert xpretty(SingularityFunction(x, 0, n), use_unicode=True) == (
  5323. """\
  5324. n\n\
  5325. <x> \
  5326. """)
  5327. assert xpretty(SingularityFunction(x, 1, n), use_unicode=True) == (
  5328. """\
  5329. n\n\
  5330. <x - 1> \
  5331. """)
  5332. assert xpretty(SingularityFunction(x, -1, n), use_unicode=True) == (
  5333. """\
  5334. n\n\
  5335. <x + 1> \
  5336. """)
  5337. assert xpretty(SingularityFunction(x, a, n), use_unicode=True) == (
  5338. """\
  5339. n\n\
  5340. <-a + x> \
  5341. """)
  5342. assert xpretty(SingularityFunction(x, y, n), use_unicode=True) == (
  5343. """\
  5344. n\n\
  5345. <x - y> \
  5346. """)
  5347. assert xpretty(SingularityFunction(x, 0, n), use_unicode=False) == (
  5348. """\
  5349. n\n\
  5350. <x> \
  5351. """)
  5352. assert xpretty(SingularityFunction(x, 1, n), use_unicode=False) == (
  5353. """\
  5354. n\n\
  5355. <x - 1> \
  5356. """)
  5357. assert xpretty(SingularityFunction(x, -1, n), use_unicode=False) == (
  5358. """\
  5359. n\n\
  5360. <x + 1> \
  5361. """)
  5362. assert xpretty(SingularityFunction(x, a, n), use_unicode=False) == (
  5363. """\
  5364. n\n\
  5365. <-a + x> \
  5366. """)
  5367. assert xpretty(SingularityFunction(x, y, n), use_unicode=False) == (
  5368. """\
  5369. n\n\
  5370. <x - y> \
  5371. """)
  5372. def test_deltas():
  5373. assert xpretty(DiracDelta(x), use_unicode=True) == 'δ(x)'
  5374. assert xpretty(DiracDelta(x, 1), use_unicode=True) == \
  5375. """\
  5376. (1) \n\
  5377. δ (x)\
  5378. """
  5379. assert xpretty(x*DiracDelta(x, 1), use_unicode=True) == \
  5380. """\
  5381. (1) \n\
  5382. x⋅δ (x)\
  5383. """
  5384. def test_hyper():
  5385. expr = hyper((), (), z)
  5386. ucode_str = \
  5387. """\
  5388. ┌─ ⎛ │ ⎞\n\
  5389. ├─ ⎜ │ z⎟\n\
  5390. 0╵ 0 ⎝ │ ⎠\
  5391. """
  5392. ascii_str = \
  5393. """\
  5394. _ \n\
  5395. |_ / | \\\n\
  5396. | | | z|\n\
  5397. 0 0 \\ | /\
  5398. """
  5399. assert pretty(expr) == ascii_str
  5400. assert upretty(expr) == ucode_str
  5401. expr = hyper((), (1,), x)
  5402. ucode_str = \
  5403. """\
  5404. ┌─ ⎛ │ ⎞\n\
  5405. ├─ ⎜ │ x⎟\n\
  5406. 0╵ 1 ⎝1 │ ⎠\
  5407. """
  5408. ascii_str = \
  5409. """\
  5410. _ \n\
  5411. |_ / | \\\n\
  5412. | | | x|\n\
  5413. 0 1 \\1 | /\
  5414. """
  5415. assert pretty(expr) == ascii_str
  5416. assert upretty(expr) == ucode_str
  5417. expr = hyper([2], [1], x)
  5418. ucode_str = \
  5419. """\
  5420. ┌─ ⎛2 │ ⎞\n\
  5421. ├─ ⎜ │ x⎟\n\
  5422. 1╵ 1 ⎝1 │ ⎠\
  5423. """
  5424. ascii_str = \
  5425. """\
  5426. _ \n\
  5427. |_ /2 | \\\n\
  5428. | | | x|\n\
  5429. 1 1 \\1 | /\
  5430. """
  5431. assert pretty(expr) == ascii_str
  5432. assert upretty(expr) == ucode_str
  5433. expr = hyper((pi/3, -2*k), (3, 4, 5, -3), x)
  5434. ucode_str = \
  5435. """\
  5436. ⎛ π │ ⎞\n\
  5437. ┌─ ⎜ ─, -2⋅k │ ⎟\n\
  5438. ├─ ⎜ 3 │ x⎟\n\
  5439. 2╵ 4 ⎜ │ ⎟\n\
  5440. ⎝3, 4, 5, -3 │ ⎠\
  5441. """
  5442. ascii_str = \
  5443. """\
  5444. \n\
  5445. _ / pi | \\\n\
  5446. |_ | --, -2*k | |\n\
  5447. | | 3 | x|\n\
  5448. 2 4 | | |\n\
  5449. \\3, 4, 5, -3 | /\
  5450. """
  5451. assert pretty(expr) == ascii_str
  5452. assert upretty(expr) == ucode_str
  5453. expr = hyper((pi, S('2/3'), -2*k), (3, 4, 5, -3), x**2)
  5454. ucode_str = \
  5455. """\
  5456. ┌─ ⎛π, 2/3, -2⋅k │ 2⎞\n\
  5457. ├─ ⎜ │ x ⎟\n\
  5458. 3╵ 4 ⎝3, 4, 5, -3 │ ⎠\
  5459. """
  5460. ascii_str = \
  5461. """\
  5462. _ \n\
  5463. |_ /pi, 2/3, -2*k | 2\\\n\
  5464. | | | x |\n\
  5465. 3 4 \\ 3, 4, 5, -3 | /\
  5466. """
  5467. assert pretty(expr) == ascii_str
  5468. assert upretty(expr) == ucode_str
  5469. expr = hyper([1, 2], [3, 4], 1/(1/(1/(1/x + 1) + 1) + 1))
  5470. ucode_str = \
  5471. """\
  5472. ⎛ │ 1 ⎞\n\
  5473. ⎜ │ ─────────────⎟\n\
  5474. ⎜ │ 1 ⎟\n\
  5475. ┌─ ⎜1, 2 │ 1 + ─────────⎟\n\
  5476. ├─ ⎜ │ 1 ⎟\n\
  5477. 2╵ 2 ⎜3, 4 │ 1 + ─────⎟\n\
  5478. ⎜ │ 1⎟\n\
  5479. ⎜ │ 1 + ─⎟\n\
  5480. ⎝ │ x⎠\
  5481. """
  5482. ascii_str = \
  5483. """\
  5484. \n\
  5485. / | 1 \\\n\
  5486. | | -------------|\n\
  5487. _ | | 1 |\n\
  5488. |_ |1, 2 | 1 + ---------|\n\
  5489. | | | 1 |\n\
  5490. 2 2 |3, 4 | 1 + -----|\n\
  5491. | | 1|\n\
  5492. | | 1 + -|\n\
  5493. \\ | x/\
  5494. """
  5495. assert pretty(expr) == ascii_str
  5496. assert upretty(expr) == ucode_str
  5497. def test_meijerg():
  5498. expr = meijerg([pi, pi, x], [1], [0, 1], [1, 2, 3], z)
  5499. ucode_str = \
  5500. """\
  5501. ╭─╮2, 3 ⎛π, π, x 1 │ ⎞\n\
  5502. │╶┐ ⎜ │ z⎟\n\
  5503. ╰─╯4, 5 ⎝ 0, 1 1, 2, 3 │ ⎠\
  5504. """
  5505. ascii_str = \
  5506. """\
  5507. __2, 3 /pi, pi, x 1 | \\\n\
  5508. /__ | | z|\n\
  5509. \\_|4, 5 \\ 0, 1 1, 2, 3 | /\
  5510. """
  5511. assert pretty(expr) == ascii_str
  5512. assert upretty(expr) == ucode_str
  5513. expr = meijerg([1, pi/7], [2, pi, 5], [], [], z**2)
  5514. ucode_str = \
  5515. """\
  5516. ⎛ π │ ⎞\n\
  5517. ╭─╮0, 2 ⎜1, ─ 2, π, 5 │ 2⎟\n\
  5518. │╶┐ ⎜ 7 │ z ⎟\n\
  5519. ╰─╯5, 0 ⎜ │ ⎟\n\
  5520. ⎝ │ ⎠\
  5521. """
  5522. ascii_str = \
  5523. """\
  5524. / pi | \\\n\
  5525. __0, 2 |1, -- 2, pi, 5 | 2|\n\
  5526. /__ | 7 | z |\n\
  5527. \\_|5, 0 | | |\n\
  5528. \\ | /\
  5529. """
  5530. assert pretty(expr) == ascii_str
  5531. assert upretty(expr) == ucode_str
  5532. ucode_str = \
  5533. """\
  5534. ╭─╮ 1, 10 ⎛1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1 │ ⎞\n\
  5535. │╶┐ ⎜ │ z⎟\n\
  5536. ╰─╯11, 2 ⎝ 1 1 │ ⎠\
  5537. """
  5538. ascii_str = \
  5539. """\
  5540. __ 1, 10 /1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1 | \\\n\
  5541. /__ | | z|\n\
  5542. \\_|11, 2 \\ 1 1 | /\
  5543. """
  5544. expr = meijerg([1]*10, [1], [1], [1], z)
  5545. assert pretty(expr) == ascii_str
  5546. assert upretty(expr) == ucode_str
  5547. expr = meijerg([1, 2, ], [4, 3], [3], [4, 5], 1/(1/(1/(1/x + 1) + 1) + 1))
  5548. ucode_str = \
  5549. """\
  5550. ⎛ │ 1 ⎞\n\
  5551. ⎜ │ ─────────────⎟\n\
  5552. ⎜ │ 1 ⎟\n\
  5553. ╭─╮1, 2 ⎜1, 2 4, 3 │ 1 + ─────────⎟\n\
  5554. │╶┐ ⎜ │ 1 ⎟\n\
  5555. ╰─╯4, 3 ⎜ 3 4, 5 │ 1 + ─────⎟\n\
  5556. ⎜ │ 1⎟\n\
  5557. ⎜ │ 1 + ─⎟\n\
  5558. ⎝ │ x⎠\
  5559. """
  5560. ascii_str = \
  5561. """\
  5562. / | 1 \\\n\
  5563. | | -------------|\n\
  5564. | | 1 |\n\
  5565. __1, 2 |1, 2 4, 3 | 1 + ---------|\n\
  5566. /__ | | 1 |\n\
  5567. \\_|4, 3 | 3 4, 5 | 1 + -----|\n\
  5568. | | 1|\n\
  5569. | | 1 + -|\n\
  5570. \\ | x/\
  5571. """
  5572. assert pretty(expr) == ascii_str
  5573. assert upretty(expr) == ucode_str
  5574. expr = Integral(expr, x)
  5575. ucode_str = \
  5576. """\
  5577. ⌠ \n\
  5578. ⎮ ⎛ │ 1 ⎞ \n\
  5579. ⎮ ⎜ │ ─────────────⎟ \n\
  5580. ⎮ ⎜ │ 1 ⎟ \n\
  5581. ⎮ ╭─╮1, 2 ⎜1, 2 4, 3 │ 1 + ─────────⎟ \n\
  5582. ⎮ │╶┐ ⎜ │ 1 ⎟ dx\n\
  5583. ⎮ ╰─╯4, 3 ⎜ 3 4, 5 │ 1 + ─────⎟ \n\
  5584. ⎮ ⎜ │ 1⎟ \n\
  5585. ⎮ ⎜ │ 1 + ─⎟ \n\
  5586. ⎮ ⎝ │ x⎠ \n\
  5587. ⌡ \
  5588. """
  5589. ascii_str = \
  5590. """\
  5591. / \n\
  5592. | \n\
  5593. | / | 1 \\ \n\
  5594. | | | -------------| \n\
  5595. | | | 1 | \n\
  5596. | __1, 2 |1, 2 4, 3 | 1 + ---------| \n\
  5597. | /__ | | 1 | dx\n\
  5598. | \\_|4, 3 | 3 4, 5 | 1 + -----| \n\
  5599. | | | 1| \n\
  5600. | | | 1 + -| \n\
  5601. | \\ | x/ \n\
  5602. | \n\
  5603. / \
  5604. """
  5605. assert pretty(expr) == ascii_str
  5606. assert upretty(expr) == ucode_str
  5607. def test_noncommutative():
  5608. A, B, C = symbols('A,B,C', commutative=False)
  5609. expr = A*B*C**-1
  5610. ascii_str = \
  5611. """\
  5612. -1\n\
  5613. A*B*C \
  5614. """
  5615. ucode_str = \
  5616. """\
  5617. -1\n\
  5618. A⋅B⋅C \
  5619. """
  5620. assert pretty(expr) == ascii_str
  5621. assert upretty(expr) == ucode_str
  5622. expr = C**-1*A*B
  5623. ascii_str = \
  5624. """\
  5625. -1 \n\
  5626. C *A*B\
  5627. """
  5628. ucode_str = \
  5629. """\
  5630. -1 \n\
  5631. C ⋅A⋅B\
  5632. """
  5633. assert pretty(expr) == ascii_str
  5634. assert upretty(expr) == ucode_str
  5635. expr = A*C**-1*B
  5636. ascii_str = \
  5637. """\
  5638. -1 \n\
  5639. A*C *B\
  5640. """
  5641. ucode_str = \
  5642. """\
  5643. -1 \n\
  5644. A⋅C ⋅B\
  5645. """
  5646. assert pretty(expr) == ascii_str
  5647. assert upretty(expr) == ucode_str
  5648. expr = A*C**-1*B/x
  5649. ascii_str = \
  5650. """\
  5651. -1 \n\
  5652. A*C *B\n\
  5653. -------\n\
  5654. x \
  5655. """
  5656. ucode_str = \
  5657. """\
  5658. -1 \n\
  5659. A⋅C ⋅B\n\
  5660. ───────\n\
  5661. x \
  5662. """
  5663. assert pretty(expr) == ascii_str
  5664. assert upretty(expr) == ucode_str
  5665. def test_pretty_special_functions():
  5666. x, y = symbols("x y")
  5667. # atan2
  5668. expr = atan2(y/sqrt(200), sqrt(x))
  5669. ascii_str = \
  5670. """\
  5671. / ___ \\\n\
  5672. |\\/ 2 *y ___|\n\
  5673. atan2|-------, \\/ x |\n\
  5674. \\ 20 /\
  5675. """
  5676. ucode_str = \
  5677. """\
  5678. ⎛√2⋅y ⎞\n\
  5679. atan2⎜────, √x⎟\n\
  5680. ⎝ 20 ⎠\
  5681. """
  5682. assert pretty(expr) == ascii_str
  5683. assert upretty(expr) == ucode_str
  5684. def test_pretty_geometry():
  5685. e = Segment((0, 1), (0, 2))
  5686. assert pretty(e) == 'Segment2D(Point2D(0, 1), Point2D(0, 2))'
  5687. e = Ray((1, 1), angle=4.02*pi)
  5688. assert pretty(e) == 'Ray2D(Point2D(1, 1), Point2D(2, tan(pi/50) + 1))'
  5689. def test_expint():
  5690. expr = Ei(x)
  5691. string = 'Ei(x)'
  5692. assert pretty(expr) == string
  5693. assert upretty(expr) == string
  5694. expr = expint(1, z)
  5695. ucode_str = "E₁(z)"
  5696. ascii_str = "expint(1, z)"
  5697. assert pretty(expr) == ascii_str
  5698. assert upretty(expr) == ucode_str
  5699. assert pretty(Shi(x)) == 'Shi(x)'
  5700. assert pretty(Si(x)) == 'Si(x)'
  5701. assert pretty(Ci(x)) == 'Ci(x)'
  5702. assert pretty(Chi(x)) == 'Chi(x)'
  5703. assert upretty(Shi(x)) == 'Shi(x)'
  5704. assert upretty(Si(x)) == 'Si(x)'
  5705. assert upretty(Ci(x)) == 'Ci(x)'
  5706. assert upretty(Chi(x)) == 'Chi(x)'
  5707. def test_elliptic_functions():
  5708. ascii_str = \
  5709. """\
  5710. / 1 \\\n\
  5711. K|-----|\n\
  5712. \\z + 1/\
  5713. """
  5714. ucode_str = \
  5715. """\
  5716. ⎛ 1 ⎞\n\
  5717. K⎜─────⎟\n\
  5718. ⎝z + 1⎠\
  5719. """
  5720. expr = elliptic_k(1/(z + 1))
  5721. assert pretty(expr) == ascii_str
  5722. assert upretty(expr) == ucode_str
  5723. ascii_str = \
  5724. """\
  5725. / | 1 \\\n\
  5726. F|1|-----|\n\
  5727. \\ |z + 1/\
  5728. """
  5729. ucode_str = \
  5730. """\
  5731. ⎛ │ 1 ⎞\n\
  5732. F⎜1│─────⎟\n\
  5733. ⎝ │z + 1⎠\
  5734. """
  5735. expr = elliptic_f(1, 1/(1 + z))
  5736. assert pretty(expr) == ascii_str
  5737. assert upretty(expr) == ucode_str
  5738. ascii_str = \
  5739. """\
  5740. / 1 \\\n\
  5741. E|-----|\n\
  5742. \\z + 1/\
  5743. """
  5744. ucode_str = \
  5745. """\
  5746. ⎛ 1 ⎞\n\
  5747. E⎜─────⎟\n\
  5748. ⎝z + 1⎠\
  5749. """
  5750. expr = elliptic_e(1/(z + 1))
  5751. assert pretty(expr) == ascii_str
  5752. assert upretty(expr) == ucode_str
  5753. ascii_str = \
  5754. """\
  5755. / | 1 \\\n\
  5756. E|1|-----|\n\
  5757. \\ |z + 1/\
  5758. """
  5759. ucode_str = \
  5760. """\
  5761. ⎛ │ 1 ⎞\n\
  5762. E⎜1│─────⎟\n\
  5763. ⎝ │z + 1⎠\
  5764. """
  5765. expr = elliptic_e(1, 1/(1 + z))
  5766. assert pretty(expr) == ascii_str
  5767. assert upretty(expr) == ucode_str
  5768. ascii_str = \
  5769. """\
  5770. / |4\\\n\
  5771. Pi|3|-|\n\
  5772. \\ |x/\
  5773. """
  5774. ucode_str = \
  5775. """\
  5776. ⎛ │4⎞\n\
  5777. Π⎜3│─⎟\n\
  5778. ⎝ │x⎠\
  5779. """
  5780. expr = elliptic_pi(3, 4/x)
  5781. assert pretty(expr) == ascii_str
  5782. assert upretty(expr) == ucode_str
  5783. ascii_str = \
  5784. """\
  5785. / 4| \\\n\
  5786. Pi|3; -|6|\n\
  5787. \\ x| /\
  5788. """
  5789. ucode_str = \
  5790. """\
  5791. ⎛ 4│ ⎞\n\
  5792. Π⎜3; ─│6⎟\n\
  5793. ⎝ x│ ⎠\
  5794. """
  5795. expr = elliptic_pi(3, 4/x, 6)
  5796. assert pretty(expr) == ascii_str
  5797. assert upretty(expr) == ucode_str
  5798. def test_RandomDomain():
  5799. from sympy.stats import Normal, Die, Exponential, pspace, where
  5800. X = Normal('x1', 0, 1)
  5801. assert upretty(where(X > 0)) == "Domain: 0 < x₁ ∧ x₁ < ∞"
  5802. D = Die('d1', 6)
  5803. assert upretty(where(D > 4)) == 'Domain: d₁ = 5 ∨ d₁ = 6'
  5804. A = Exponential('a', 1)
  5805. B = Exponential('b', 1)
  5806. assert upretty(pspace(Tuple(A, B)).domain) == \
  5807. 'Domain: 0 ≤ a ∧ 0 ≤ b ∧ a < ∞ ∧ b < ∞'
  5808. def test_PrettyPoly():
  5809. F = QQ.frac_field(x, y)
  5810. R = QQ.poly_ring(x, y)
  5811. expr = F.convert(x/(x + y))
  5812. assert pretty(expr) == "x/(x + y)"
  5813. assert upretty(expr) == "x/(x + y)"
  5814. expr = R.convert(x + y)
  5815. assert pretty(expr) == "x + y"
  5816. assert upretty(expr) == "x + y"
  5817. def test_issue_6285():
  5818. assert pretty(Pow(2, -5, evaluate=False)) == '1 \n--\n 5\n2 '
  5819. assert pretty(Pow(x, (1/pi))) == \
  5820. ' 1 \n'\
  5821. ' --\n'\
  5822. ' pi\n'\
  5823. 'x '
  5824. def test_issue_6359():
  5825. assert pretty(Integral(x**2, x)**2) == \
  5826. """\
  5827. 2
  5828. / / \\ \n\
  5829. | | | \n\
  5830. | | 2 | \n\
  5831. | | x dx| \n\
  5832. | | | \n\
  5833. \\/ / \
  5834. """
  5835. assert upretty(Integral(x**2, x)**2) == \
  5836. """\
  5837. 2
  5838. ⎛⌠ ⎞ \n\
  5839. ⎜⎮ 2 ⎟ \n\
  5840. ⎜⎮ x dx⎟ \n\
  5841. ⎝⌡ ⎠ \
  5842. """
  5843. assert pretty(Sum(x**2, (x, 0, 1))**2) == \
  5844. """\
  5845. 2
  5846. / 1 \\ \n\
  5847. | ___ | \n\
  5848. | \\ ` | \n\
  5849. | \\ 2| \n\
  5850. | / x | \n\
  5851. | /__, | \n\
  5852. \\x = 0 / \
  5853. """
  5854. assert upretty(Sum(x**2, (x, 0, 1))**2) == \
  5855. """\
  5856. 2
  5857. ⎛ 1 ⎞ \n\
  5858. ⎜ ___ ⎟ \n\
  5859. ⎜ ╲ ⎟ \n\
  5860. ⎜ ╲ 2⎟ \n\
  5861. ⎜ ╱ x ⎟ \n\
  5862. ⎜ ╱ ⎟ \n\
  5863. ⎜ ‾‾‾ ⎟ \n\
  5864. ⎝x = 0 ⎠ \
  5865. """
  5866. assert pretty(Product(x**2, (x, 1, 2))**2) == \
  5867. """\
  5868. 2
  5869. / 2 \\ \n\
  5870. |______ | \n\
  5871. | | | 2| \n\
  5872. | | | x | \n\
  5873. | | | | \n\
  5874. \\x = 1 / \
  5875. """
  5876. assert upretty(Product(x**2, (x, 1, 2))**2) == \
  5877. """\
  5878. 2
  5879. ⎛ 2 ⎞ \n\
  5880. ⎜─┬──┬─ ⎟ \n\
  5881. ⎜ │ │ 2⎟ \n\
  5882. ⎜ │ │ x ⎟ \n\
  5883. ⎜ │ │ ⎟ \n\
  5884. ⎝x = 1 ⎠ \
  5885. """
  5886. f = Function('f')
  5887. assert pretty(Derivative(f(x), x)**2) == \
  5888. """\
  5889. 2
  5890. /d \\ \n\
  5891. |--(f(x))| \n\
  5892. \\dx / \
  5893. """
  5894. assert upretty(Derivative(f(x), x)**2) == \
  5895. """\
  5896. 2
  5897. ⎛d ⎞ \n\
  5898. ⎜──(f(x))⎟ \n\
  5899. ⎝dx ⎠ \
  5900. """
  5901. def test_issue_6739():
  5902. ascii_str = \
  5903. """\
  5904. 1 \n\
  5905. -----\n\
  5906. ___\n\
  5907. \\/ x \
  5908. """
  5909. ucode_str = \
  5910. """\
  5911. 1 \n\
  5912. ──\n\
  5913. √x\
  5914. """
  5915. assert pretty(1/sqrt(x)) == ascii_str
  5916. assert upretty(1/sqrt(x)) == ucode_str
  5917. def test_complicated_symbol_unchanged():
  5918. for symb_name in ["dexpr2_d1tau", "dexpr2^d1tau"]:
  5919. assert pretty(Symbol(symb_name)) == symb_name
  5920. def test_categories():
  5921. from sympy.categories import (Object, IdentityMorphism,
  5922. NamedMorphism, Category, Diagram, DiagramGrid)
  5923. A1 = Object("A1")
  5924. A2 = Object("A2")
  5925. A3 = Object("A3")
  5926. f1 = NamedMorphism(A1, A2, "f1")
  5927. f2 = NamedMorphism(A2, A3, "f2")
  5928. id_A1 = IdentityMorphism(A1)
  5929. K1 = Category("K1")
  5930. assert pretty(A1) == "A1"
  5931. assert upretty(A1) == "A₁"
  5932. assert pretty(f1) == "f1:A1-->A2"
  5933. assert upretty(f1) == "f₁:A₁——▶A₂"
  5934. assert pretty(id_A1) == "id:A1-->A1"
  5935. assert upretty(id_A1) == "id:A₁——▶A₁"
  5936. assert pretty(f2*f1) == "f2*f1:A1-->A3"
  5937. assert upretty(f2*f1) == "f₂∘f₁:A₁——▶A₃"
  5938. assert pretty(K1) == "K1"
  5939. assert upretty(K1) == "K₁"
  5940. # Test how diagrams are printed.
  5941. d = Diagram()
  5942. assert pretty(d) == "EmptySet"
  5943. assert upretty(d) == "∅"
  5944. d = Diagram({f1: "unique", f2: S.EmptySet})
  5945. assert pretty(d) == "{f2*f1:A1-->A3: EmptySet, id:A1-->A1: " \
  5946. "EmptySet, id:A2-->A2: EmptySet, id:A3-->A3: " \
  5947. "EmptySet, f1:A1-->A2: {unique}, f2:A2-->A3: EmptySet}"
  5948. assert upretty(d) == "{f₂∘f₁:A₁——▶A₃: ∅, id:A₁——▶A₁: ∅, " \
  5949. "id:A₂——▶A₂: ∅, id:A₃——▶A₃: ∅, f₁:A₁——▶A₂: {unique}, f₂:A₂——▶A₃: ∅}"
  5950. d = Diagram({f1: "unique", f2: S.EmptySet}, {f2 * f1: "unique"})
  5951. assert pretty(d) == "{f2*f1:A1-->A3: EmptySet, id:A1-->A1: " \
  5952. "EmptySet, id:A2-->A2: EmptySet, id:A3-->A3: " \
  5953. "EmptySet, f1:A1-->A2: {unique}, f2:A2-->A3: EmptySet}" \
  5954. " ==> {f2*f1:A1-->A3: {unique}}"
  5955. assert upretty(d) == "{f₂∘f₁:A₁——▶A₃: ∅, id:A₁——▶A₁: ∅, id:A₂——▶A₂: " \
  5956. "∅, id:A₃——▶A₃: ∅, f₁:A₁——▶A₂: {unique}, f₂:A₂——▶A₃: ∅}" \
  5957. " ══▶ {f₂∘f₁:A₁——▶A₃: {unique}}"
  5958. grid = DiagramGrid(d)
  5959. assert pretty(grid) == "A1 A2\n \nA3 "
  5960. assert upretty(grid) == "A₁ A₂\n \nA₃ "
  5961. def test_PrettyModules():
  5962. R = QQ.old_poly_ring(x, y)
  5963. F = R.free_module(2)
  5964. M = F.submodule([x, y], [1, x**2])
  5965. ucode_str = \
  5966. """\
  5967. 2\n\
  5968. ℚ[x, y] \
  5969. """
  5970. ascii_str = \
  5971. """\
  5972. 2\n\
  5973. QQ[x, y] \
  5974. """
  5975. assert upretty(F) == ucode_str
  5976. assert pretty(F) == ascii_str
  5977. ucode_str = \
  5978. """\
  5979. ╱ ⎡ 2⎤╲\n\
  5980. ╲[x, y], ⎣1, x ⎦╱\
  5981. """
  5982. ascii_str = \
  5983. """\
  5984. 2 \n\
  5985. <[x, y], [1, x ]>\
  5986. """
  5987. assert upretty(M) == ucode_str
  5988. assert pretty(M) == ascii_str
  5989. I = R.ideal(x**2, y)
  5990. ucode_str = \
  5991. """\
  5992. ╱ 2 ╲\n\
  5993. ╲x , y╱\
  5994. """
  5995. ascii_str = \
  5996. """\
  5997. 2 \n\
  5998. <x , y>\
  5999. """
  6000. assert upretty(I) == ucode_str
  6001. assert pretty(I) == ascii_str
  6002. Q = F / M
  6003. ucode_str = \
  6004. """\
  6005. 2 \n\
  6006. ℚ[x, y] \n\
  6007. ─────────────────\n\
  6008. ╱ ⎡ 2⎤╲\n\
  6009. ╲[x, y], ⎣1, x ⎦╱\
  6010. """
  6011. ascii_str = \
  6012. """\
  6013. 2 \n\
  6014. QQ[x, y] \n\
  6015. -----------------\n\
  6016. 2 \n\
  6017. <[x, y], [1, x ]>\
  6018. """
  6019. assert upretty(Q) == ucode_str
  6020. assert pretty(Q) == ascii_str
  6021. ucode_str = \
  6022. """\
  6023. ╱⎡ 3⎤ ╲\n\
  6024. │⎢ x ⎥ ╱ ⎡ 2⎤╲ ╱ ⎡ 2⎤╲│\n\
  6025. │⎢1, ──⎥ + ╲[x, y], ⎣1, x ⎦╱, [2, y] + ╲[x, y], ⎣1, x ⎦╱│\n\
  6026. ╲⎣ 2 ⎦ ╱\
  6027. """
  6028. ascii_str = \
  6029. """\
  6030. 3 \n\
  6031. x 2 2 \n\
  6032. <[1, --] + <[x, y], [1, x ]>, [2, y] + <[x, y], [1, x ]>>\n\
  6033. 2 \
  6034. """
  6035. def test_QuotientRing():
  6036. R = QQ.old_poly_ring(x)/[x**2 + 1]
  6037. ucode_str = \
  6038. """\
  6039. ℚ[x] \n\
  6040. ────────\n\
  6041. ╱ 2 ╲\n\
  6042. ╲x + 1╱\
  6043. """
  6044. ascii_str = \
  6045. """\
  6046. QQ[x] \n\
  6047. --------\n\
  6048. 2 \n\
  6049. <x + 1>\
  6050. """
  6051. assert upretty(R) == ucode_str
  6052. assert pretty(R) == ascii_str
  6053. ucode_str = \
  6054. """\
  6055. ╱ 2 ╲\n\
  6056. 1 + ╲x + 1╱\
  6057. """
  6058. ascii_str = \
  6059. """\
  6060. 2 \n\
  6061. 1 + <x + 1>\
  6062. """
  6063. assert upretty(R.one) == ucode_str
  6064. assert pretty(R.one) == ascii_str
  6065. def test_Homomorphism():
  6066. from sympy.polys.agca import homomorphism
  6067. R = QQ.old_poly_ring(x)
  6068. expr = homomorphism(R.free_module(1), R.free_module(1), [0])
  6069. ucode_str = \
  6070. """\
  6071. 1 1\n\
  6072. [0] : ℚ[x] ──> ℚ[x] \
  6073. """
  6074. ascii_str = \
  6075. """\
  6076. 1 1\n\
  6077. [0] : QQ[x] --> QQ[x] \
  6078. """
  6079. assert upretty(expr) == ucode_str
  6080. assert pretty(expr) == ascii_str
  6081. expr = homomorphism(R.free_module(2), R.free_module(2), [0, 0])
  6082. ucode_str = \
  6083. """\
  6084. ⎡0 0⎤ 2 2\n\
  6085. ⎢ ⎥ : ℚ[x] ──> ℚ[x] \n\
  6086. ⎣0 0⎦ \
  6087. """
  6088. ascii_str = \
  6089. """\
  6090. [0 0] 2 2\n\
  6091. [ ] : QQ[x] --> QQ[x] \n\
  6092. [0 0] \
  6093. """
  6094. assert upretty(expr) == ucode_str
  6095. assert pretty(expr) == ascii_str
  6096. expr = homomorphism(R.free_module(1), R.free_module(1) / [[x]], [0])
  6097. ucode_str = \
  6098. """\
  6099. 1\n\
  6100. 1 ℚ[x] \n\
  6101. [0] : ℚ[x] ──> ─────\n\
  6102. <[x]>\
  6103. """
  6104. ascii_str = \
  6105. """\
  6106. 1\n\
  6107. 1 QQ[x] \n\
  6108. [0] : QQ[x] --> ------\n\
  6109. <[x]> \
  6110. """
  6111. assert upretty(expr) == ucode_str
  6112. assert pretty(expr) == ascii_str
  6113. def test_Tr():
  6114. A, B = symbols('A B', commutative=False)
  6115. t = Tr(A*B)
  6116. assert pretty(t) == r'Tr(A*B)'
  6117. assert upretty(t) == 'Tr(A⋅B)'
  6118. def test_pretty_Add():
  6119. eq = Mul(-2, x - 2, evaluate=False) + 5
  6120. assert pretty(eq) == '5 - 2*(x - 2)'
  6121. def test_issue_7179():
  6122. assert upretty(Not(Equivalent(x, y))) == 'x ⇎ y'
  6123. assert upretty(Not(Implies(x, y))) == 'x ↛ y'
  6124. def test_issue_7180():
  6125. assert upretty(Equivalent(x, y)) == 'x ⇔ y'
  6126. def test_pretty_Complement():
  6127. assert pretty(S.Reals - S.Naturals) == '(-oo, oo) \\ Naturals'
  6128. assert upretty(S.Reals - S.Naturals) == 'ℝ \\ ℕ'
  6129. assert pretty(S.Reals - S.Naturals0) == '(-oo, oo) \\ Naturals0'
  6130. assert upretty(S.Reals - S.Naturals0) == 'ℝ \\ ℕ₀'
  6131. def test_pretty_SymmetricDifference():
  6132. from sympy.sets.sets import SymmetricDifference
  6133. assert upretty(SymmetricDifference(Interval(2,3), Interval(3,5), \
  6134. evaluate = False)) == '[2, 3] ∆ [3, 5]'
  6135. with raises(NotImplementedError):
  6136. pretty(SymmetricDifference(Interval(2,3), Interval(3,5), evaluate = False))
  6137. def test_pretty_Contains():
  6138. assert pretty(Contains(x, S.Integers)) == 'Contains(x, Integers)'
  6139. assert upretty(Contains(x, S.Integers)) == 'x ∈ ℤ'
  6140. def test_issue_8292():
  6141. from sympy.core import sympify
  6142. e = sympify('((x+x**4)/(x-1))-(2*(x-1)**4/(x-1)**4)', evaluate=False)
  6143. ucode_str = \
  6144. """\
  6145. 4 4 \n\
  6146. 2⋅(x - 1) x + x\n\
  6147. - ────────── + ──────\n\
  6148. 4 x - 1 \n\
  6149. (x - 1) \
  6150. """
  6151. ascii_str = \
  6152. """\
  6153. 4 4 \n\
  6154. 2*(x - 1) x + x\n\
  6155. - ---------- + ------\n\
  6156. 4 x - 1 \n\
  6157. (x - 1) \
  6158. """
  6159. assert pretty(e) == ascii_str
  6160. assert upretty(e) == ucode_str
  6161. def test_issue_4335():
  6162. y = Function('y')
  6163. expr = -y(x).diff(x)
  6164. ucode_str = \
  6165. """\
  6166. d \n\
  6167. -──(y(x))\n\
  6168. dx \
  6169. """
  6170. ascii_str = \
  6171. """\
  6172. d \n\
  6173. - --(y(x))\n\
  6174. dx \
  6175. """
  6176. assert pretty(expr) == ascii_str
  6177. assert upretty(expr) == ucode_str
  6178. def test_issue_8344():
  6179. from sympy.core import sympify
  6180. e = sympify('2*x*y**2/1**2 + 1', evaluate=False)
  6181. ucode_str = \
  6182. """\
  6183. 2 \n\
  6184. 2⋅x⋅y \n\
  6185. ────── + 1\n\
  6186. 2 \n\
  6187. 1 \
  6188. """
  6189. assert upretty(e) == ucode_str
  6190. def test_issue_6324():
  6191. x = Pow(2, 3, evaluate=False)
  6192. y = Pow(10, -2, evaluate=False)
  6193. e = Mul(x, y, evaluate=False)
  6194. ucode_str = \
  6195. """\
  6196. 3\n\
  6197. 2 \n\
  6198. ───\n\
  6199. 2\n\
  6200. 10 \
  6201. """
  6202. assert upretty(e) == ucode_str
  6203. def test_issue_7927():
  6204. e = sin(x/2)**cos(x/2)
  6205. ucode_str = \
  6206. """\
  6207. ⎛x⎞\n\
  6208. cos⎜─⎟\n\
  6209. ⎝2⎠\n\
  6210. ⎛ ⎛x⎞⎞ \n\
  6211. ⎜sin⎜─⎟⎟ \n\
  6212. ⎝ ⎝2⎠⎠ \
  6213. """
  6214. assert upretty(e) == ucode_str
  6215. e = sin(x)**(S(11)/13)
  6216. ucode_str = \
  6217. """\
  6218. 11\n\
  6219. ──\n\
  6220. 13\n\
  6221. (sin(x)) \
  6222. """
  6223. assert upretty(e) == ucode_str
  6224. def test_issue_6134():
  6225. from sympy.abc import lamda, t
  6226. phi = Function('phi')
  6227. e = lamda*x*Integral(phi(t)*pi*sin(pi*t), (t, 0, 1)) + lamda*x**2*Integral(phi(t)*2*pi*sin(2*pi*t), (t, 0, 1))
  6228. ucode_str = \
  6229. """\
  6230. 1 1 \n\
  6231. 2 ⌠ ⌠ \n\
  6232. λ⋅x ⋅⎮ 2⋅π⋅φ(t)⋅sin(2⋅π⋅t) dt + λ⋅x⋅⎮ π⋅φ(t)⋅sin(π⋅t) dt\n\
  6233. ⌡ ⌡ \n\
  6234. 0 0 \
  6235. """
  6236. assert upretty(e) == ucode_str
  6237. def test_issue_9877():
  6238. ucode_str1 = '(2, 3) ∪ ([1, 2] \\ {x})'
  6239. a, b, c = Interval(2, 3, True, True), Interval(1, 2), FiniteSet(x)
  6240. assert upretty(Union(a, Complement(b, c))) == ucode_str1
  6241. ucode_str2 = '{x} ∩ {y} ∩ ({z} \\ [1, 2])'
  6242. d, e, f, g = FiniteSet(x), FiniteSet(y), FiniteSet(z), Interval(1, 2)
  6243. assert upretty(Intersection(d, e, Complement(f, g))) == ucode_str2
  6244. def test_issue_13651():
  6245. expr1 = c + Mul(-1, a + b, evaluate=False)
  6246. assert pretty(expr1) == 'c - (a + b)'
  6247. expr2 = c + Mul(-1, a - b + d, evaluate=False)
  6248. assert pretty(expr2) == 'c - (a - b + d)'
  6249. def test_pretty_primenu():
  6250. from sympy.ntheory.factor_ import primenu
  6251. ascii_str1 = "nu(n)"
  6252. ucode_str1 = "ν(n)"
  6253. n = symbols('n', integer=True)
  6254. assert pretty(primenu(n)) == ascii_str1
  6255. assert upretty(primenu(n)) == ucode_str1
  6256. def test_pretty_primeomega():
  6257. from sympy.ntheory.factor_ import primeomega
  6258. ascii_str1 = "Omega(n)"
  6259. ucode_str1 = "Ω(n)"
  6260. n = symbols('n', integer=True)
  6261. assert pretty(primeomega(n)) == ascii_str1
  6262. assert upretty(primeomega(n)) == ucode_str1
  6263. def test_pretty_Mod():
  6264. from sympy.core import Mod
  6265. ascii_str1 = "x mod 7"
  6266. ucode_str1 = "x mod 7"
  6267. ascii_str2 = "(x + 1) mod 7"
  6268. ucode_str2 = "(x + 1) mod 7"
  6269. ascii_str3 = "2*x mod 7"
  6270. ucode_str3 = "2⋅x mod 7"
  6271. ascii_str4 = "(x mod 7) + 1"
  6272. ucode_str4 = "(x mod 7) + 1"
  6273. ascii_str5 = "2*(x mod 7)"
  6274. ucode_str5 = "2⋅(x mod 7)"
  6275. x = symbols('x', integer=True)
  6276. assert pretty(Mod(x, 7)) == ascii_str1
  6277. assert upretty(Mod(x, 7)) == ucode_str1
  6278. assert pretty(Mod(x + 1, 7)) == ascii_str2
  6279. assert upretty(Mod(x + 1, 7)) == ucode_str2
  6280. assert pretty(Mod(2 * x, 7)) == ascii_str3
  6281. assert upretty(Mod(2 * x, 7)) == ucode_str3
  6282. assert pretty(Mod(x, 7) + 1) == ascii_str4
  6283. assert upretty(Mod(x, 7) + 1) == ucode_str4
  6284. assert pretty(2 * Mod(x, 7)) == ascii_str5
  6285. assert upretty(2 * Mod(x, 7)) == ucode_str5
  6286. def test_issue_11801():
  6287. assert pretty(Symbol("")) == ""
  6288. assert upretty(Symbol("")) == ""
  6289. def test_pretty_UnevaluatedExpr():
  6290. x = symbols('x')
  6291. he = UnevaluatedExpr(1/x)
  6292. ucode_str = \
  6293. """\
  6294. 1\n\
  6295. ─\n\
  6296. x\
  6297. """
  6298. assert upretty(he) == ucode_str
  6299. ucode_str = \
  6300. """\
  6301. 2\n\
  6302. ⎛1⎞ \n\
  6303. ⎜─⎟ \n\
  6304. ⎝x⎠ \
  6305. """
  6306. assert upretty(he**2) == ucode_str
  6307. ucode_str = \
  6308. """\
  6309. 1\n\
  6310. 1 + ─\n\
  6311. x\
  6312. """
  6313. assert upretty(he + 1) == ucode_str
  6314. ucode_str = \
  6315. ('''\
  6316. 1\n\
  6317. x⋅─\n\
  6318. x\
  6319. ''')
  6320. assert upretty(x*he) == ucode_str
  6321. def test_issue_10472():
  6322. M = (Matrix([[0, 0], [0, 0]]), Matrix([0, 0]))
  6323. ucode_str = \
  6324. """\
  6325. ⎛⎡0 0⎤ ⎡0⎤⎞
  6326. ⎜⎢ ⎥, ⎢ ⎥⎟
  6327. ⎝⎣0 0⎦ ⎣0⎦⎠\
  6328. """
  6329. assert upretty(M) == ucode_str
  6330. def test_MatrixElement_printing():
  6331. # test cases for issue #11821
  6332. A = MatrixSymbol("A", 1, 3)
  6333. B = MatrixSymbol("B", 1, 3)
  6334. C = MatrixSymbol("C", 1, 3)
  6335. ascii_str1 = "A_00"
  6336. ucode_str1 = "A₀₀"
  6337. assert pretty(A[0, 0]) == ascii_str1
  6338. assert upretty(A[0, 0]) == ucode_str1
  6339. ascii_str1 = "3*A_00"
  6340. ucode_str1 = "3⋅A₀₀"
  6341. assert pretty(3*A[0, 0]) == ascii_str1
  6342. assert upretty(3*A[0, 0]) == ucode_str1
  6343. ascii_str1 = "(-B + A)[0, 0]"
  6344. ucode_str1 = "(-B + A)[0, 0]"
  6345. F = C[0, 0].subs(C, A - B)
  6346. assert pretty(F) == ascii_str1
  6347. assert upretty(F) == ucode_str1
  6348. def test_issue_12675():
  6349. x, y, t, j = symbols('x y t j')
  6350. e = CoordSys3D('e')
  6351. ucode_str = \
  6352. """\
  6353. ⎛ t⎞ \n\
  6354. ⎜⎛x⎞ ⎟ j_e\n\
  6355. ⎜⎜─⎟ ⎟ \n\
  6356. ⎝⎝y⎠ ⎠ \
  6357. """
  6358. assert upretty((x/y)**t*e.j) == ucode_str
  6359. ucode_str = \
  6360. """\
  6361. ⎛1⎞ \n\
  6362. ⎜─⎟ j_e\n\
  6363. ⎝y⎠ \
  6364. """
  6365. assert upretty((1/y)*e.j) == ucode_str
  6366. def test_MatrixSymbol_printing():
  6367. # test cases for issue #14237
  6368. A = MatrixSymbol("A", 3, 3)
  6369. B = MatrixSymbol("B", 3, 3)
  6370. C = MatrixSymbol("C", 3, 3)
  6371. assert pretty(-A*B*C) == "-A*B*C"
  6372. assert pretty(A - B) == "-B + A"
  6373. assert pretty(A*B*C - A*B - B*C) == "-A*B -B*C + A*B*C"
  6374. # issue #14814
  6375. x = MatrixSymbol('x', n, n)
  6376. y = MatrixSymbol('y*', n, n)
  6377. assert pretty(x + y) == "x + y*"
  6378. ascii_str = \
  6379. """\
  6380. 2 \n\
  6381. -2*y* -a*x\
  6382. """
  6383. assert pretty(-a*x + -2*y*y) == ascii_str
  6384. def test_degree_printing():
  6385. expr1 = 90*degree
  6386. assert pretty(expr1) == '90°'
  6387. expr2 = x*degree
  6388. assert pretty(expr2) == 'x°'
  6389. expr3 = cos(x*degree + 90*degree)
  6390. assert pretty(expr3) == 'cos(x° + 90°)'
  6391. def test_vector_expr_pretty_printing():
  6392. A = CoordSys3D('A')
  6393. assert upretty(Cross(A.i, A.x*A.i+3*A.y*A.j)) == "(i_A)×((x_A) i_A + (3⋅y_A) j_A)"
  6394. assert upretty(x*Cross(A.i, A.j)) == 'x⋅(i_A)×(j_A)'
  6395. assert upretty(Curl(A.x*A.i + 3*A.y*A.j)) == "∇×((x_A) i_A + (3⋅y_A) j_A)"
  6396. assert upretty(Divergence(A.x*A.i + 3*A.y*A.j)) == "∇⋅((x_A) i_A + (3⋅y_A) j_A)"
  6397. assert upretty(Dot(A.i, A.x*A.i+3*A.y*A.j)) == "(i_A)⋅((x_A) i_A + (3⋅y_A) j_A)"
  6398. assert upretty(Gradient(A.x+3*A.y)) == "∇(x_A + 3⋅y_A)"
  6399. assert upretty(Laplacian(A.x+3*A.y)) == "∆(x_A + 3⋅y_A)"
  6400. # TODO: add support for ASCII pretty.
  6401. def test_pretty_print_tensor_expr():
  6402. L = TensorIndexType("L")
  6403. i, j, k = tensor_indices("i j k", L)
  6404. i0 = tensor_indices("i_0", L)
  6405. A, B, C, D = tensor_heads("A B C D", [L])
  6406. H = TensorHead("H", [L, L])
  6407. expr = -i
  6408. ascii_str = \
  6409. """\
  6410. -i\
  6411. """
  6412. ucode_str = \
  6413. """\
  6414. -i\
  6415. """
  6416. assert pretty(expr) == ascii_str
  6417. assert upretty(expr) == ucode_str
  6418. expr = A(i)
  6419. ascii_str = \
  6420. """\
  6421. i\n\
  6422. A \n\
  6423. \
  6424. """
  6425. ucode_str = \
  6426. """\
  6427. i\n\
  6428. A \n\
  6429. \
  6430. """
  6431. assert pretty(expr) == ascii_str
  6432. assert upretty(expr) == ucode_str
  6433. expr = A(i0)
  6434. ascii_str = \
  6435. """\
  6436. i_0\n\
  6437. A \n\
  6438. \
  6439. """
  6440. ucode_str = \
  6441. """\
  6442. i₀\n\
  6443. A \n\
  6444. \
  6445. """
  6446. assert pretty(expr) == ascii_str
  6447. assert upretty(expr) == ucode_str
  6448. expr = A(-i)
  6449. ascii_str = \
  6450. """\
  6451. \n\
  6452. A \n\
  6453. i\
  6454. """
  6455. ucode_str = \
  6456. """\
  6457. \n\
  6458. A \n\
  6459. i\
  6460. """
  6461. assert pretty(expr) == ascii_str
  6462. assert upretty(expr) == ucode_str
  6463. expr = -3*A(-i)
  6464. ascii_str = \
  6465. """\
  6466. \n\
  6467. -3*A \n\
  6468. i\
  6469. """
  6470. ucode_str = \
  6471. """\
  6472. \n\
  6473. -3⋅A \n\
  6474. i\
  6475. """
  6476. assert pretty(expr) == ascii_str
  6477. assert upretty(expr) == ucode_str
  6478. expr = H(i, -j)
  6479. ascii_str = \
  6480. """\
  6481. i \n\
  6482. H \n\
  6483. j\
  6484. """
  6485. ucode_str = \
  6486. """\
  6487. i \n\
  6488. H \n\
  6489. j\
  6490. """
  6491. assert pretty(expr) == ascii_str
  6492. assert upretty(expr) == ucode_str
  6493. expr = H(i, -i)
  6494. ascii_str = \
  6495. """\
  6496. L_0 \n\
  6497. H \n\
  6498. L_0\
  6499. """
  6500. ucode_str = \
  6501. """\
  6502. L₀ \n\
  6503. H \n\
  6504. L₀\
  6505. """
  6506. assert pretty(expr) == ascii_str
  6507. assert upretty(expr) == ucode_str
  6508. expr = H(i, -j)*A(j)*B(k)
  6509. ascii_str = \
  6510. """\
  6511. i L_0 k\n\
  6512. H *A *B \n\
  6513. L_0 \
  6514. """
  6515. ucode_str = \
  6516. """\
  6517. i L₀ k\n\
  6518. H ⋅A ⋅B \n\
  6519. L₀ \
  6520. """
  6521. assert pretty(expr) == ascii_str
  6522. assert upretty(expr) == ucode_str
  6523. expr = (1+x)*A(i)
  6524. ascii_str = \
  6525. """\
  6526. i\n\
  6527. (x + 1)*A \n\
  6528. \
  6529. """
  6530. ucode_str = \
  6531. """\
  6532. i\n\
  6533. (x + 1)⋅A \n\
  6534. \
  6535. """
  6536. assert pretty(expr) == ascii_str
  6537. assert upretty(expr) == ucode_str
  6538. expr = A(i) + 3*B(i)
  6539. ascii_str = \
  6540. """\
  6541. i i\n\
  6542. 3*B + A \n\
  6543. \
  6544. """
  6545. ucode_str = \
  6546. """\
  6547. i i\n\
  6548. 3⋅B + A \n\
  6549. \
  6550. """
  6551. assert pretty(expr) == ascii_str
  6552. assert upretty(expr) == ucode_str
  6553. def test_pretty_print_tensor_partial_deriv():
  6554. from sympy.tensor.toperators import PartialDerivative
  6555. L = TensorIndexType("L")
  6556. i, j, k = tensor_indices("i j k", L)
  6557. A, B, C, D = tensor_heads("A B C D", [L])
  6558. H = TensorHead("H", [L, L])
  6559. expr = PartialDerivative(A(i), A(j))
  6560. ascii_str = \
  6561. """\
  6562. d / i\\\n\
  6563. ---|A |\n\
  6564. j\\ /\n\
  6565. dA \n\
  6566. \
  6567. """
  6568. ucode_str = \
  6569. """\
  6570. ∂ ⎛ i⎞\n\
  6571. ───⎜A ⎟\n\
  6572. j⎝ ⎠\n\
  6573. ∂A \n\
  6574. \
  6575. """
  6576. assert pretty(expr) == ascii_str
  6577. assert upretty(expr) == ucode_str
  6578. expr = A(i)*PartialDerivative(H(k, -i), A(j))
  6579. ascii_str = \
  6580. """\
  6581. L_0 d / k \\\n\
  6582. A *---|H |\n\
  6583. j\\ L_0/\n\
  6584. dA \n\
  6585. \
  6586. """
  6587. ucode_str = \
  6588. """\
  6589. L₀ ∂ ⎛ k ⎞\n\
  6590. A ⋅───⎜H ⎟\n\
  6591. j⎝ L₀⎠\n\
  6592. ∂A \n\
  6593. \
  6594. """
  6595. assert pretty(expr) == ascii_str
  6596. assert upretty(expr) == ucode_str
  6597. expr = A(i)*PartialDerivative(B(k)*C(-i) + 3*H(k, -i), A(j))
  6598. ascii_str = \
  6599. """\
  6600. L_0 d / k k \\\n\
  6601. A *---|3*H + B *C |\n\
  6602. j\\ L_0 L_0/\n\
  6603. dA \n\
  6604. \
  6605. """
  6606. ucode_str = \
  6607. """\
  6608. L₀ ∂ ⎛ k k ⎞\n\
  6609. A ⋅───⎜3⋅H + B ⋅C ⎟\n\
  6610. j⎝ L₀ L₀⎠\n\
  6611. ∂A \n\
  6612. \
  6613. """
  6614. assert pretty(expr) == ascii_str
  6615. assert upretty(expr) == ucode_str
  6616. expr = (A(i) + B(i))*PartialDerivative(C(j), D(j))
  6617. ascii_str = \
  6618. """\
  6619. / i i\\ d / L_0\\\n\
  6620. |A + B |*-----|C |\n\
  6621. \\ / L_0\\ /\n\
  6622. dD \n\
  6623. \
  6624. """
  6625. ucode_str = \
  6626. """\
  6627. ⎛ i i⎞ ∂ ⎛ L₀⎞\n\
  6628. ⎜A + B ⎟⋅────⎜C ⎟\n\
  6629. ⎝ ⎠ L₀⎝ ⎠\n\
  6630. ∂D \n\
  6631. \
  6632. """
  6633. assert pretty(expr) == ascii_str
  6634. assert upretty(expr) == ucode_str
  6635. expr = (A(i) + B(i))*PartialDerivative(C(-i), D(j))
  6636. ascii_str = \
  6637. """\
  6638. / L_0 L_0\\ d / \\\n\
  6639. |A + B |*---|C |\n\
  6640. \\ / j\\ L_0/\n\
  6641. dD \n\
  6642. \
  6643. """
  6644. ucode_str = \
  6645. """\
  6646. ⎛ L₀ L₀⎞ ∂ ⎛ ⎞\n\
  6647. ⎜A + B ⎟⋅───⎜C ⎟\n\
  6648. ⎝ ⎠ j⎝ L₀⎠\n\
  6649. ∂D \n\
  6650. \
  6651. """
  6652. assert pretty(expr) == ascii_str
  6653. assert upretty(expr) == ucode_str
  6654. expr = PartialDerivative(B(-i) + A(-i), A(-j), A(-n))
  6655. ucode_str = """\
  6656. 2 \n\
  6657. ∂ ⎛ ⎞\n\
  6658. ───────⎜A + B ⎟\n\
  6659. ⎝ i i⎠\n\
  6660. ∂A ∂A \n\
  6661. n j \
  6662. """
  6663. assert upretty(expr) == ucode_str
  6664. expr = PartialDerivative(3*A(-i), A(-j), A(-n))
  6665. ucode_str = """\
  6666. 2 \n\
  6667. ∂ ⎛ ⎞\n\
  6668. ───────⎜3⋅A ⎟\n\
  6669. ⎝ i⎠\n\
  6670. ∂A ∂A \n\
  6671. n j \
  6672. """
  6673. assert upretty(expr) == ucode_str
  6674. expr = TensorElement(H(i, j), {i:1})
  6675. ascii_str = \
  6676. """\
  6677. i=1,j\n\
  6678. H \n\
  6679. \
  6680. """
  6681. ucode_str = ascii_str
  6682. assert pretty(expr) == ascii_str
  6683. assert upretty(expr) == ucode_str
  6684. expr = TensorElement(H(i, j), {i: 1, j: 1})
  6685. ascii_str = \
  6686. """\
  6687. i=1,j=1\n\
  6688. H \n\
  6689. \
  6690. """
  6691. ucode_str = ascii_str
  6692. assert pretty(expr) == ascii_str
  6693. assert upretty(expr) == ucode_str
  6694. expr = TensorElement(H(i, j), {j: 1})
  6695. ascii_str = \
  6696. """\
  6697. i,j=1\n\
  6698. H \n\
  6699. \
  6700. """
  6701. ucode_str = ascii_str
  6702. expr = TensorElement(H(-i, j), {-i: 1})
  6703. ascii_str = \
  6704. """\
  6705. j\n\
  6706. H \n\
  6707. i=1 \
  6708. """
  6709. ucode_str = ascii_str
  6710. assert pretty(expr) == ascii_str
  6711. assert upretty(expr) == ucode_str
  6712. def test_issue_15560():
  6713. a = MatrixSymbol('a', 1, 1)
  6714. e = pretty(a*(KroneckerProduct(a, a)))
  6715. result = 'a*(a x a)'
  6716. assert e == result
  6717. def test_print_lerchphi():
  6718. # Part of issue 6013
  6719. a = Symbol('a')
  6720. pretty(lerchphi(a, 1, 2))
  6721. uresult = 'Φ(a, 1, 2)'
  6722. aresult = 'lerchphi(a, 1, 2)'
  6723. assert pretty(lerchphi(a, 1, 2)) == aresult
  6724. assert upretty(lerchphi(a, 1, 2)) == uresult
  6725. def test_issue_15583():
  6726. N = mechanics.ReferenceFrame('N')
  6727. result = '(n_x, n_y, n_z)'
  6728. e = pretty((N.x, N.y, N.z))
  6729. assert e == result
  6730. def test_matrixSymbolBold():
  6731. # Issue 15871
  6732. def boldpretty(expr):
  6733. return xpretty(expr, use_unicode=True, wrap_line=False, mat_symbol_style="bold")
  6734. from sympy.matrices.expressions.trace import trace
  6735. A = MatrixSymbol("A", 2, 2)
  6736. assert boldpretty(trace(A)) == 'tr(𝐀)'
  6737. A = MatrixSymbol("A", 3, 3)
  6738. B = MatrixSymbol("B", 3, 3)
  6739. C = MatrixSymbol("C", 3, 3)
  6740. assert boldpretty(-A) == '-𝐀'
  6741. assert boldpretty(A - A*B - B) == '-𝐁 -𝐀⋅𝐁 + 𝐀'
  6742. assert boldpretty(-A*B - A*B*C - B) == '-𝐁 -𝐀⋅𝐁 -𝐀⋅𝐁⋅𝐂'
  6743. A = MatrixSymbol("Addot", 3, 3)
  6744. assert boldpretty(A) == '𝐀̈'
  6745. omega = MatrixSymbol("omega", 3, 3)
  6746. assert boldpretty(omega) == 'ω'
  6747. omega = MatrixSymbol("omeganorm", 3, 3)
  6748. assert boldpretty(omega) == '‖ω‖'
  6749. a = Symbol('alpha')
  6750. b = Symbol('b')
  6751. c = MatrixSymbol("c", 3, 1)
  6752. d = MatrixSymbol("d", 3, 1)
  6753. assert boldpretty(a*B*c+b*d) == 'b⋅𝐝 + α⋅𝐁⋅𝐜'
  6754. d = MatrixSymbol("delta", 3, 1)
  6755. B = MatrixSymbol("Beta", 3, 3)
  6756. assert boldpretty(a*B*c+b*d) == 'b⋅δ + α⋅Β⋅𝐜'
  6757. A = MatrixSymbol("A_2", 3, 3)
  6758. assert boldpretty(A) == '𝐀₂'
  6759. def test_center_accent():
  6760. assert center_accent('a', '\N{COMBINING TILDE}') == 'ã'
  6761. assert center_accent('aa', '\N{COMBINING TILDE}') == 'aã'
  6762. assert center_accent('aaa', '\N{COMBINING TILDE}') == 'aãa'
  6763. assert center_accent('aaaa', '\N{COMBINING TILDE}') == 'aaãa'
  6764. assert center_accent('aaaaa', '\N{COMBINING TILDE}') == 'aaãaa'
  6765. assert center_accent('abcdefg', '\N{COMBINING FOUR DOTS ABOVE}') == 'abcd⃜efg'
  6766. def test_imaginary_unit():
  6767. from sympy.printing.pretty import pretty # b/c it was redefined above
  6768. assert pretty(1 + I, use_unicode=False) == '1 + I'
  6769. assert pretty(1 + I, use_unicode=True) == '1 + ⅈ'
  6770. assert pretty(1 + I, use_unicode=False, imaginary_unit='j') == '1 + I'
  6771. assert pretty(1 + I, use_unicode=True, imaginary_unit='j') == '1 + ⅉ'
  6772. raises(TypeError, lambda: pretty(I, imaginary_unit=I))
  6773. raises(ValueError, lambda: pretty(I, imaginary_unit="kkk"))
  6774. def test_str_special_matrices():
  6775. from sympy.matrices import Identity, ZeroMatrix, OneMatrix
  6776. assert pretty(Identity(4)) == 'I'
  6777. assert upretty(Identity(4)) == '𝕀'
  6778. assert pretty(ZeroMatrix(2, 2)) == '0'
  6779. assert upretty(ZeroMatrix(2, 2)) == '𝟘'
  6780. assert pretty(OneMatrix(2, 2)) == '1'
  6781. assert upretty(OneMatrix(2, 2)) == '𝟙'
  6782. def test_pretty_misc_functions():
  6783. assert pretty(LambertW(x)) == 'W(x)'
  6784. assert upretty(LambertW(x)) == 'W(x)'
  6785. assert pretty(LambertW(x, y)) == 'W(x, y)'
  6786. assert upretty(LambertW(x, y)) == 'W(x, y)'
  6787. assert pretty(airyai(x)) == 'Ai(x)'
  6788. assert upretty(airyai(x)) == 'Ai(x)'
  6789. assert pretty(airybi(x)) == 'Bi(x)'
  6790. assert upretty(airybi(x)) == 'Bi(x)'
  6791. assert pretty(airyaiprime(x)) == "Ai'(x)"
  6792. assert upretty(airyaiprime(x)) == "Ai'(x)"
  6793. assert pretty(airybiprime(x)) == "Bi'(x)"
  6794. assert upretty(airybiprime(x)) == "Bi'(x)"
  6795. assert pretty(fresnelc(x)) == 'C(x)'
  6796. assert upretty(fresnelc(x)) == 'C(x)'
  6797. assert pretty(fresnels(x)) == 'S(x)'
  6798. assert upretty(fresnels(x)) == 'S(x)'
  6799. assert pretty(Heaviside(x)) == 'Heaviside(x)'
  6800. assert upretty(Heaviside(x)) == 'θ(x)'
  6801. assert pretty(Heaviside(x, y)) == 'Heaviside(x, y)'
  6802. assert upretty(Heaviside(x, y)) == 'θ(x, y)'
  6803. assert pretty(dirichlet_eta(x)) == 'dirichlet_eta(x)'
  6804. assert upretty(dirichlet_eta(x)) == 'η(x)'
  6805. def test_hadamard_power():
  6806. m, n, p = symbols('m, n, p', integer=True)
  6807. A = MatrixSymbol('A', m, n)
  6808. B = MatrixSymbol('B', m, n)
  6809. # Testing printer:
  6810. expr = hadamard_power(A, n)
  6811. ascii_str = \
  6812. """\
  6813. .n\n\
  6814. A \
  6815. """
  6816. ucode_str = \
  6817. """\
  6818. ∘n\n\
  6819. A \
  6820. """
  6821. assert pretty(expr) == ascii_str
  6822. assert upretty(expr) == ucode_str
  6823. expr = hadamard_power(A, 1+n)
  6824. ascii_str = \
  6825. """\
  6826. .(n + 1)\n\
  6827. A \
  6828. """
  6829. ucode_str = \
  6830. """\
  6831. ∘(n + 1)\n\
  6832. A \
  6833. """
  6834. assert pretty(expr) == ascii_str
  6835. assert upretty(expr) == ucode_str
  6836. expr = hadamard_power(A*B.T, 1+n)
  6837. ascii_str = \
  6838. """\
  6839. .(n + 1)\n\
  6840. / T\\ \n\
  6841. \\A*B / \
  6842. """
  6843. ucode_str = \
  6844. """\
  6845. ∘(n + 1)\n\
  6846. ⎛ T⎞ \n\
  6847. ⎝A⋅B ⎠ \
  6848. """
  6849. assert pretty(expr) == ascii_str
  6850. assert upretty(expr) == ucode_str
  6851. def test_issue_17258():
  6852. n = Symbol('n', integer=True)
  6853. assert pretty(Sum(n, (n, -oo, 1))) == \
  6854. ' 1 \n'\
  6855. ' __ \n'\
  6856. ' \\ ` \n'\
  6857. ' ) n\n'\
  6858. ' /_, \n'\
  6859. 'n = -oo '
  6860. assert upretty(Sum(n, (n, -oo, 1))) == \
  6861. """\
  6862. 1 \n\
  6863. ___ \n\
  6864. ╲ \n\
  6865. ╲ \n\
  6866. ╱ n\n\
  6867. ╱ \n\
  6868. ‾‾‾ \n\
  6869. n = -∞ \
  6870. """
  6871. def test_is_combining():
  6872. line = "v̇_m"
  6873. assert [is_combining(sym) for sym in line] == \
  6874. [False, True, False, False]
  6875. def test_issue_17616():
  6876. assert pretty(pi**(1/exp(1))) == \
  6877. ' / -1\\\n'\
  6878. ' \\e /\n'\
  6879. 'pi '
  6880. assert upretty(pi**(1/exp(1))) == \
  6881. ' ⎛ -1⎞\n'\
  6882. ' ⎝ℯ ⎠\n'\
  6883. 'π '
  6884. assert pretty(pi**(1/pi)) == \
  6885. ' 1 \n'\
  6886. ' --\n'\
  6887. ' pi\n'\
  6888. 'pi '
  6889. assert upretty(pi**(1/pi)) == \
  6890. ' 1\n'\
  6891. ' ─\n'\
  6892. ' π\n'\
  6893. 'π '
  6894. assert pretty(pi**(1/EulerGamma)) == \
  6895. ' 1 \n'\
  6896. ' ----------\n'\
  6897. ' EulerGamma\n'\
  6898. 'pi '
  6899. assert upretty(pi**(1/EulerGamma)) == \
  6900. ' 1\n'\
  6901. ' ─\n'\
  6902. ' γ\n'\
  6903. 'π '
  6904. z = Symbol("x_17")
  6905. assert upretty(7**(1/z)) == \
  6906. 'x₁₇___\n'\
  6907. ' ╲╱ 7 '
  6908. assert pretty(7**(1/z)) == \
  6909. 'x_17___\n'\
  6910. ' \\/ 7 '
  6911. def test_issue_17857():
  6912. assert pretty(Range(-oo, oo)) == '{..., -1, 0, 1, ...}'
  6913. assert pretty(Range(oo, -oo, -1)) == '{..., 1, 0, -1, ...}'
  6914. def test_issue_18272():
  6915. x = Symbol('x')
  6916. n = Symbol('n')
  6917. assert upretty(ConditionSet(x, Eq(-x + exp(x), 0), S.Complexes)) == \
  6918. '⎧ │ ⎛ x ⎞⎫\n'\
  6919. '⎨x │ x ∊ ℂ ∧ ⎝-x + ℯ = 0⎠⎬\n'\
  6920. '⎩ │ ⎭'
  6921. assert upretty(ConditionSet(x, Contains(n/2, Interval(0, oo)), FiniteSet(-n/2, n/2))) == \
  6922. '⎧ │ ⎧-n n⎫ ⎛n ⎞⎫\n'\
  6923. '⎨x │ x ∊ ⎨───, ─⎬ ∧ ⎜─ ∈ [0, ∞)⎟⎬\n'\
  6924. '⎩ │ ⎩ 2 2⎭ ⎝2 ⎠⎭'
  6925. assert upretty(ConditionSet(x, Eq(Piecewise((1, x >= 3), (x/2 - 1/2, x >= 2), (1/2, x >= 1),
  6926. (x/2, True)) - 1/2, 0), Interval(0, 3))) == \
  6927. '⎧ │ ⎛⎛⎧ 1 for x ≥ 3⎞ ⎞⎫\n'\
  6928. '⎪ │ ⎜⎜⎪ ⎟ ⎟⎪\n'\
  6929. '⎪ │ ⎜⎜⎪x ⎟ ⎟⎪\n'\
  6930. '⎪ │ ⎜⎜⎪─ - 0.5 for x ≥ 2⎟ ⎟⎪\n'\
  6931. '⎪ │ ⎜⎜⎪2 ⎟ ⎟⎪\n'\
  6932. '⎨x │ x ∊ [0, 3] ∧ ⎜⎜⎨ ⎟ - 0.5 = 0⎟⎬\n'\
  6933. '⎪ │ ⎜⎜⎪ 0.5 for x ≥ 1⎟ ⎟⎪\n'\
  6934. '⎪ │ ⎜⎜⎪ ⎟ ⎟⎪\n'\
  6935. '⎪ │ ⎜⎜⎪ x ⎟ ⎟⎪\n'\
  6936. '⎪ │ ⎜⎜⎪ ─ otherwise⎟ ⎟⎪\n'\
  6937. '⎩ │ ⎝⎝⎩ 2 ⎠ ⎠⎭'
  6938. def test_Str():
  6939. from sympy.core.symbol import Str
  6940. assert pretty(Str('x')) == 'x'
  6941. def test_symbolic_probability():
  6942. mu = symbols("mu")
  6943. sigma = symbols("sigma", positive=True)
  6944. X = Normal("X", mu, sigma)
  6945. assert pretty(Expectation(X)) == r'E[X]'
  6946. assert pretty(Variance(X)) == r'Var(X)'
  6947. assert pretty(Probability(X > 0)) == r'P(X > 0)'
  6948. Y = Normal("Y", mu, sigma)
  6949. assert pretty(Covariance(X, Y)) == 'Cov(X, Y)'
  6950. def test_issue_21758():
  6951. from sympy.functions.elementary.piecewise import piecewise_fold
  6952. from sympy.series.fourier import FourierSeries
  6953. x = Symbol('x')
  6954. k, n = symbols('k n')
  6955. fo = FourierSeries(x, (x, -pi, pi), (0, SeqFormula(0, (k, 1, oo)), SeqFormula(
  6956. Piecewise((-2*pi*cos(n*pi)/n + 2*sin(n*pi)/n**2, (n > -oo) & (n < oo) & Ne(n, 0)),
  6957. (0, True))*sin(n*x)/pi, (n, 1, oo))))
  6958. assert upretty(piecewise_fold(fo)) == \
  6959. '⎧ 2⋅sin(3⋅x) \n'\
  6960. '⎪2⋅sin(x) - sin(2⋅x) + ────────── + … for n > -∞ ∧ n < ∞ ∧ n ≠ 0\n'\
  6961. '⎨ 3 \n'\
  6962. '⎪ \n'\
  6963. '⎩ 0 otherwise '
  6964. assert pretty(FourierSeries(x, (x, -pi, pi), (0, SeqFormula(0, (k, 1, oo)),
  6965. SeqFormula(0, (n, 1, oo))))) == '0'
  6966. def test_diffgeom():
  6967. from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField
  6968. x,y = symbols('x y', real=True)
  6969. m = Manifold('M', 2)
  6970. assert pretty(m) == 'M'
  6971. p = Patch('P', m)
  6972. assert pretty(p) == "P"
  6973. rect = CoordSystem('rect', p, [x, y])
  6974. assert pretty(rect) == "rect"
  6975. b = BaseScalarField(rect, 0)
  6976. assert pretty(b) == "x"
  6977. def test_deprecated_prettyForm():
  6978. with warns_deprecated_sympy():
  6979. from sympy.printing.pretty.pretty_symbology import xstr
  6980. assert xstr(1) == '1'
  6981. with warns_deprecated_sympy():
  6982. from sympy.printing.pretty.stringpict import prettyForm
  6983. p = prettyForm('s', unicode='s')
  6984. with warns_deprecated_sympy():
  6985. assert p.unicode == p.s == 's'