test_pretty.py 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972
  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, polylog,
  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, StateSpace)
  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, center
  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. ────── + ───────
  2316. x + y x - 2⋅y\
  2317. """
  2318. expected3 = \
  2319. """\
  2320. 2 \n\
  2321. x + y x + y ⎛-x - y ⎞ ⎛x - y⎞
  2322. ────── + ─────── + ⎜───────⎟⋅⎜─────⎟
  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_StateSpace():
  2602. ss1 = StateSpace(Matrix([a]), Matrix([b]), Matrix([c]), Matrix([d]))
  2603. A = Matrix([[0, 1], [1, 0]])
  2604. B = Matrix([1, 0])
  2605. C = Matrix([[0, 1]])
  2606. D = Matrix([0])
  2607. ss2 = StateSpace(A, B, C, D)
  2608. ss3 = StateSpace(Matrix([[-1.5, -2], [1, 0]]),
  2609. Matrix([[0.5, 0], [0, 1]]),
  2610. Matrix([[0, 1], [0, 2]]),
  2611. Matrix([[2, 2], [1, 1]]))
  2612. expected1 = \
  2613. """\
  2614. ⎡[a] [b]⎤\n\
  2615. ⎢ ⎥\n\
  2616. ⎣[c] [d]⎦\
  2617. """
  2618. expected2 = \
  2619. """\
  2620. ⎡⎡0 1⎤ ⎡1⎤⎤\n\
  2621. ⎢⎢ ⎥ ⎢ ⎥⎥\n\
  2622. ⎢⎣1 0⎦ ⎣0⎦⎥\n\
  2623. ⎢ ⎥\n\
  2624. ⎣[0 1] [0]⎦\
  2625. """
  2626. expected3 = \
  2627. """\
  2628. ⎡⎡-1.5 -2⎤ ⎡0.5 0⎤⎤\n\
  2629. ⎢⎢ ⎥ ⎢ ⎥⎥\n\
  2630. ⎢⎣ 1 0 ⎦ ⎣ 0 1⎦⎥\n\
  2631. ⎢ ⎥\n\
  2632. ⎢ ⎡0 1⎤ ⎡2 2⎤ ⎥\n\
  2633. ⎢ ⎢ ⎥ ⎢ ⎥ ⎥\n\
  2634. ⎣ ⎣0 2⎦ ⎣1 1⎦ ⎦\
  2635. """
  2636. assert upretty(ss1) == expected1
  2637. assert upretty(ss2) == expected2
  2638. assert upretty(ss3) == expected3
  2639. def test_pretty_order():
  2640. expr = O(1)
  2641. ascii_str = \
  2642. """\
  2643. O(1)\
  2644. """
  2645. ucode_str = \
  2646. """\
  2647. O(1)\
  2648. """
  2649. assert pretty(expr) == ascii_str
  2650. assert upretty(expr) == ucode_str
  2651. expr = O(1/x)
  2652. ascii_str = \
  2653. """\
  2654. /1\\\n\
  2655. O|-|\n\
  2656. \\x/\
  2657. """
  2658. ucode_str = \
  2659. """\
  2660. ⎛1⎞\n\
  2661. O⎜─⎟\n\
  2662. ⎝x⎠\
  2663. """
  2664. assert pretty(expr) == ascii_str
  2665. assert upretty(expr) == ucode_str
  2666. expr = O(x**2 + y**2)
  2667. ascii_str = \
  2668. """\
  2669. / 2 2 \\\n\
  2670. O\\x + y ; (x, y) -> (0, 0)/\
  2671. """
  2672. ucode_str = \
  2673. """\
  2674. ⎛ 2 2 ⎞\n\
  2675. O⎝x + y ; (x, y) → (0, 0)⎠\
  2676. """
  2677. assert pretty(expr) == ascii_str
  2678. assert upretty(expr) == ucode_str
  2679. expr = O(1, (x, oo))
  2680. ascii_str = \
  2681. """\
  2682. O(1; x -> oo)\
  2683. """
  2684. ucode_str = \
  2685. """\
  2686. O(1; x → ∞)\
  2687. """
  2688. assert pretty(expr) == ascii_str
  2689. assert upretty(expr) == ucode_str
  2690. expr = O(1/x, (x, oo))
  2691. ascii_str = \
  2692. """\
  2693. /1 \\\n\
  2694. O|-; x -> oo|\n\
  2695. \\x /\
  2696. """
  2697. ucode_str = \
  2698. """\
  2699. ⎛1 ⎞\n\
  2700. O⎜─; x → ∞⎟\n\
  2701. ⎝x ⎠\
  2702. """
  2703. assert pretty(expr) == ascii_str
  2704. assert upretty(expr) == ucode_str
  2705. expr = O(x**2 + y**2, (x, oo), (y, oo))
  2706. ascii_str = \
  2707. """\
  2708. / 2 2 \\\n\
  2709. O\\x + y ; (x, y) -> (oo, oo)/\
  2710. """
  2711. ucode_str = \
  2712. """\
  2713. ⎛ 2 2 ⎞\n\
  2714. O⎝x + y ; (x, y) → (∞, ∞)⎠\
  2715. """
  2716. assert pretty(expr) == ascii_str
  2717. assert upretty(expr) == ucode_str
  2718. def test_pretty_derivatives():
  2719. # Simple
  2720. expr = Derivative(log(x), x, evaluate=False)
  2721. ascii_str = \
  2722. """\
  2723. d \n\
  2724. --(log(x))\n\
  2725. dx \
  2726. """
  2727. ucode_str = \
  2728. """\
  2729. d \n\
  2730. ──(log(x))\n\
  2731. dx \
  2732. """
  2733. assert pretty(expr) == ascii_str
  2734. assert upretty(expr) == ucode_str
  2735. expr = Derivative(log(x), x, evaluate=False) + x
  2736. ascii_str_1 = \
  2737. """\
  2738. d \n\
  2739. x + --(log(x))\n\
  2740. dx \
  2741. """
  2742. ascii_str_2 = \
  2743. """\
  2744. d \n\
  2745. --(log(x)) + x\n\
  2746. dx \
  2747. """
  2748. ucode_str_1 = \
  2749. """\
  2750. d \n\
  2751. x + ──(log(x))\n\
  2752. dx \
  2753. """
  2754. ucode_str_2 = \
  2755. """\
  2756. d \n\
  2757. ──(log(x)) + x\n\
  2758. dx \
  2759. """
  2760. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2761. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  2762. # basic partial derivatives
  2763. expr = Derivative(log(x + y) + x, x)
  2764. ascii_str_1 = \
  2765. """\
  2766. d \n\
  2767. --(log(x + y) + x)\n\
  2768. dx \
  2769. """
  2770. ascii_str_2 = \
  2771. """\
  2772. d \n\
  2773. --(x + log(x + y))\n\
  2774. dx \
  2775. """
  2776. ucode_str_1 = \
  2777. """\
  2778. ∂ \n\
  2779. ──(log(x + y) + x)\n\
  2780. ∂x \
  2781. """
  2782. ucode_str_2 = \
  2783. """\
  2784. ∂ \n\
  2785. ──(x + log(x + y))\n\
  2786. ∂x \
  2787. """
  2788. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  2789. assert upretty(expr) in [ucode_str_1, ucode_str_2], upretty(expr)
  2790. # Multiple symbols
  2791. expr = Derivative(log(x) + x**2, x, y)
  2792. ascii_str_1 = \
  2793. """\
  2794. 2 \n\
  2795. d / 2\\\n\
  2796. -----\\log(x) + x /\n\
  2797. dy dx \
  2798. """
  2799. ascii_str_2 = \
  2800. """\
  2801. 2 \n\
  2802. d / 2 \\\n\
  2803. -----\\x + log(x)/\n\
  2804. dy dx \
  2805. """
  2806. ascii_str_3 = \
  2807. """\
  2808. 2 \n\
  2809. d / 2 \\\n\
  2810. -----\\x + log(x)/\n\
  2811. dy dx \
  2812. """
  2813. ucode_str_1 = \
  2814. """\
  2815. 2 \n\
  2816. d ⎛ 2⎞\n\
  2817. ─────⎝log(x) + x ⎠\n\
  2818. dy dx \
  2819. """
  2820. ucode_str_2 = \
  2821. """\
  2822. 2 \n\
  2823. d ⎛ 2 ⎞\n\
  2824. ─────⎝x + log(x)⎠\n\
  2825. dy dx \
  2826. """
  2827. ucode_str_3 = \
  2828. """\
  2829. 2 \n\
  2830. d ⎛ 2 ⎞\n\
  2831. ─────⎝x + log(x)⎠\n\
  2832. dy dx \
  2833. """
  2834. assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3]
  2835. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  2836. expr = Derivative(2*x*y, y, x) + x**2
  2837. ascii_str_1 = \
  2838. """\
  2839. 2 \n\
  2840. d 2\n\
  2841. -----(2*x*y) + x \n\
  2842. dx dy \
  2843. """
  2844. ascii_str_2 = \
  2845. """\
  2846. 2 \n\
  2847. 2 d \n\
  2848. x + -----(2*x*y)\n\
  2849. dx dy \
  2850. """
  2851. ascii_str_3 = \
  2852. """\
  2853. 2 \n\
  2854. 2 d \n\
  2855. x + -----(2*x*y)\n\
  2856. dx dy \
  2857. """
  2858. ucode_str_1 = \
  2859. """\
  2860. 2 \n\
  2861. ∂ 2\n\
  2862. ─────(2⋅x⋅y) + x \n\
  2863. ∂x ∂y \
  2864. """
  2865. ucode_str_2 = \
  2866. """\
  2867. 2 \n\
  2868. 2 ∂ \n\
  2869. x + ─────(2⋅x⋅y)\n\
  2870. ∂x ∂y \
  2871. """
  2872. ucode_str_3 = \
  2873. """\
  2874. 2 \n\
  2875. 2 ∂ \n\
  2876. x + ─────(2⋅x⋅y)\n\
  2877. ∂x ∂y \
  2878. """
  2879. assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3]
  2880. assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3]
  2881. expr = Derivative(2*x*y, x, x)
  2882. ascii_str = \
  2883. """\
  2884. 2 \n\
  2885. d \n\
  2886. ---(2*x*y)\n\
  2887. 2 \n\
  2888. dx \
  2889. """
  2890. ucode_str = \
  2891. """\
  2892. 2 \n\
  2893. ∂ \n\
  2894. ───(2⋅x⋅y)\n\
  2895. 2 \n\
  2896. ∂x \
  2897. """
  2898. assert pretty(expr) == ascii_str
  2899. assert upretty(expr) == ucode_str
  2900. expr = Derivative(2*x*y, x, 17)
  2901. ascii_str = \
  2902. """\
  2903. 17 \n\
  2904. d \n\
  2905. ----(2*x*y)\n\
  2906. 17 \n\
  2907. dx \
  2908. """
  2909. ucode_str = \
  2910. """\
  2911. 17 \n\
  2912. ∂ \n\
  2913. ────(2⋅x⋅y)\n\
  2914. 17 \n\
  2915. ∂x \
  2916. """
  2917. assert pretty(expr) == ascii_str
  2918. assert upretty(expr) == ucode_str
  2919. expr = Derivative(2*x*y, x, x, y)
  2920. ascii_str = \
  2921. """\
  2922. 3 \n\
  2923. d \n\
  2924. ------(2*x*y)\n\
  2925. 2 \n\
  2926. dy dx \
  2927. """
  2928. ucode_str = \
  2929. """\
  2930. 3 \n\
  2931. ∂ \n\
  2932. ──────(2⋅x⋅y)\n\
  2933. 2 \n\
  2934. ∂y ∂x \
  2935. """
  2936. assert pretty(expr) == ascii_str
  2937. assert upretty(expr) == ucode_str
  2938. # Greek letters
  2939. alpha = Symbol('alpha')
  2940. beta = Function('beta')
  2941. expr = beta(alpha).diff(alpha)
  2942. ascii_str = \
  2943. """\
  2944. d \n\
  2945. ------(beta(alpha))\n\
  2946. dalpha \
  2947. """
  2948. ucode_str = \
  2949. """\
  2950. d \n\
  2951. ──(β(α))\n\
  2952. dα \
  2953. """
  2954. assert pretty(expr) == ascii_str
  2955. assert upretty(expr) == ucode_str
  2956. expr = Derivative(f(x), (x, n))
  2957. ascii_str = \
  2958. """\
  2959. n \n\
  2960. d \n\
  2961. ---(f(x))\n\
  2962. n \n\
  2963. dx \
  2964. """
  2965. ucode_str = \
  2966. """\
  2967. n \n\
  2968. d \n\
  2969. ───(f(x))\n\
  2970. n \n\
  2971. dx \
  2972. """
  2973. assert pretty(expr) == ascii_str
  2974. assert upretty(expr) == ucode_str
  2975. def test_pretty_integrals():
  2976. expr = Integral(log(x), x)
  2977. ascii_str = \
  2978. """\
  2979. / \n\
  2980. | \n\
  2981. | log(x) dx\n\
  2982. | \n\
  2983. / \
  2984. """
  2985. ucode_str = \
  2986. """\
  2987. ⌠ \n\
  2988. ⎮ log(x) dx\n\
  2989. ⌡ \
  2990. """
  2991. assert pretty(expr) == ascii_str
  2992. assert upretty(expr) == ucode_str
  2993. expr = Integral(x**2, x)
  2994. ascii_str = \
  2995. """\
  2996. / \n\
  2997. | \n\
  2998. | 2 \n\
  2999. | x dx\n\
  3000. | \n\
  3001. / \
  3002. """
  3003. ucode_str = \
  3004. """\
  3005. ⌠ \n\
  3006. ⎮ 2 \n\
  3007. ⎮ x dx\n\
  3008. ⌡ \
  3009. """
  3010. assert pretty(expr) == ascii_str
  3011. assert upretty(expr) == ucode_str
  3012. expr = Integral((sin(x))**2 / (tan(x))**2)
  3013. ascii_str = \
  3014. """\
  3015. / \n\
  3016. | \n\
  3017. | 2 \n\
  3018. | sin (x) \n\
  3019. | ------- dx\n\
  3020. | 2 \n\
  3021. | tan (x) \n\
  3022. | \n\
  3023. / \
  3024. """
  3025. ucode_str = \
  3026. """\
  3027. ⌠ \n\
  3028. ⎮ 2 \n\
  3029. ⎮ sin (x) \n\
  3030. ⎮ ─────── dx\n\
  3031. ⎮ 2 \n\
  3032. ⎮ tan (x) \n\
  3033. ⌡ \
  3034. """
  3035. assert pretty(expr) == ascii_str
  3036. assert upretty(expr) == ucode_str
  3037. expr = Integral(x**(2**x), x)
  3038. ascii_str = \
  3039. """\
  3040. / \n\
  3041. | \n\
  3042. | / x\\ \n\
  3043. | \\2 / \n\
  3044. | x dx\n\
  3045. | \n\
  3046. / \
  3047. """
  3048. ucode_str = \
  3049. """\
  3050. ⌠ \n\
  3051. ⎮ ⎛ x⎞ \n\
  3052. ⎮ ⎝2 ⎠ \n\
  3053. ⎮ x dx\n\
  3054. ⌡ \
  3055. """
  3056. assert pretty(expr) == ascii_str
  3057. assert upretty(expr) == ucode_str
  3058. expr = Integral(x**2, (x, 1, 2))
  3059. ascii_str = \
  3060. """\
  3061. 2 \n\
  3062. / \n\
  3063. | \n\
  3064. | 2 \n\
  3065. | x dx\n\
  3066. | \n\
  3067. / \n\
  3068. 1 \
  3069. """
  3070. ucode_str = \
  3071. """\
  3072. 2 \n\
  3073. ⌠ \n\
  3074. ⎮ 2 \n\
  3075. ⎮ x dx\n\
  3076. ⌡ \n\
  3077. 1 \
  3078. """
  3079. assert pretty(expr) == ascii_str
  3080. assert upretty(expr) == ucode_str
  3081. expr = Integral(x**2, (x, Rational(1, 2), 10))
  3082. ascii_str = \
  3083. """\
  3084. 10 \n\
  3085. / \n\
  3086. | \n\
  3087. | 2 \n\
  3088. | x dx\n\
  3089. | \n\
  3090. / \n\
  3091. 1/2 \
  3092. """
  3093. ucode_str = \
  3094. """\
  3095. 10 \n\
  3096. ⌠ \n\
  3097. ⎮ 2 \n\
  3098. ⎮ x dx\n\
  3099. ⌡ \n\
  3100. 1/2 \
  3101. """
  3102. assert pretty(expr) == ascii_str
  3103. assert upretty(expr) == ucode_str
  3104. expr = Integral(x**2*y**2, x, y)
  3105. ascii_str = \
  3106. """\
  3107. / / \n\
  3108. | | \n\
  3109. | | 2 2 \n\
  3110. | | x *y dx dy\n\
  3111. | | \n\
  3112. / / \
  3113. """
  3114. ucode_str = \
  3115. """\
  3116. ⌠ ⌠ \n\
  3117. ⎮ ⎮ 2 2 \n\
  3118. ⎮ ⎮ x ⋅y dx dy\n\
  3119. ⌡ ⌡ \
  3120. """
  3121. assert pretty(expr) == ascii_str
  3122. assert upretty(expr) == ucode_str
  3123. expr = Integral(sin(th)/cos(ph), (th, 0, pi), (ph, 0, 2*pi))
  3124. ascii_str = \
  3125. """\
  3126. 2*pi pi \n\
  3127. / / \n\
  3128. | | \n\
  3129. | | sin(theta) \n\
  3130. | | ---------- d(theta) d(phi)\n\
  3131. | | cos(phi) \n\
  3132. | | \n\
  3133. / / \n\
  3134. 0 0 \
  3135. """
  3136. ucode_str = \
  3137. """\
  3138. 2⋅π π \n\
  3139. ⌠ ⌠ \n\
  3140. ⎮ ⎮ sin(θ) \n\
  3141. ⎮ ⎮ ────── dθ dφ\n\
  3142. ⎮ ⎮ cos(φ) \n\
  3143. ⌡ ⌡ \n\
  3144. 0 0 \
  3145. """
  3146. assert pretty(expr) == ascii_str
  3147. assert upretty(expr) == ucode_str
  3148. def test_pretty_matrix():
  3149. # Empty Matrix
  3150. expr = Matrix()
  3151. ascii_str = "[]"
  3152. unicode_str = "[]"
  3153. assert pretty(expr) == ascii_str
  3154. assert upretty(expr) == unicode_str
  3155. expr = Matrix(2, 0, lambda i, j: 0)
  3156. ascii_str = "[]"
  3157. unicode_str = "[]"
  3158. assert pretty(expr) == ascii_str
  3159. assert upretty(expr) == unicode_str
  3160. expr = Matrix(0, 2, lambda i, j: 0)
  3161. ascii_str = "[]"
  3162. unicode_str = "[]"
  3163. assert pretty(expr) == ascii_str
  3164. assert upretty(expr) == unicode_str
  3165. expr = Matrix([[x**2 + 1, 1], [y, x + y]])
  3166. ascii_str_1 = \
  3167. """\
  3168. [ 2 ]
  3169. [1 + x 1 ]
  3170. [ ]
  3171. [ y x + y]\
  3172. """
  3173. ascii_str_2 = \
  3174. """\
  3175. [ 2 ]
  3176. [x + 1 1 ]
  3177. [ ]
  3178. [ y x + y]\
  3179. """
  3180. ucode_str_1 = \
  3181. """\
  3182. ⎡ 2 ⎤
  3183. ⎢1 + x 1 ⎥
  3184. ⎢ ⎥
  3185. ⎣ y x + y⎦\
  3186. """
  3187. ucode_str_2 = \
  3188. """\
  3189. ⎡ 2 ⎤
  3190. ⎢x + 1 1 ⎥
  3191. ⎢ ⎥
  3192. ⎣ y x + y⎦\
  3193. """
  3194. assert pretty(expr) in [ascii_str_1, ascii_str_2]
  3195. assert upretty(expr) in [ucode_str_1, ucode_str_2]
  3196. expr = Matrix([[x/y, y, th], [0, exp(I*k*ph), 1]])
  3197. ascii_str = \
  3198. """\
  3199. [x ]
  3200. [- y theta]
  3201. [y ]
  3202. [ ]
  3203. [ I*k*phi ]
  3204. [0 e 1 ]\
  3205. """
  3206. ucode_str = \
  3207. """\
  3208. ⎡x ⎤
  3209. ⎢─ y θ⎥
  3210. ⎢y ⎥
  3211. ⎢ ⎥
  3212. ⎢ ⅈ⋅k⋅φ ⎥
  3213. ⎣0 ℯ 1⎦\
  3214. """
  3215. assert pretty(expr) == ascii_str
  3216. assert upretty(expr) == ucode_str
  3217. unicode_str = \
  3218. """\
  3219. ⎡v̇_msc_00 0 0 ⎤
  3220. ⎢ ⎥
  3221. ⎢ 0 v̇_msc_01 0 ⎥
  3222. ⎢ ⎥
  3223. ⎣ 0 0 v̇_msc_02⎦\
  3224. """
  3225. expr = diag(*MatrixSymbol('vdot_msc',1,3))
  3226. assert upretty(expr) == unicode_str
  3227. def test_pretty_ndim_arrays():
  3228. x, y, z, w = symbols("x y z w")
  3229. for ArrayType in (ImmutableDenseNDimArray, ImmutableSparseNDimArray, MutableDenseNDimArray, MutableSparseNDimArray):
  3230. # Basic: scalar array
  3231. M = ArrayType(x)
  3232. assert pretty(M) == "x"
  3233. assert upretty(M) == "x"
  3234. M = ArrayType([[1/x, y], [z, w]])
  3235. M1 = ArrayType([1/x, y, z])
  3236. M2 = tensorproduct(M1, M)
  3237. M3 = tensorproduct(M, M)
  3238. ascii_str = \
  3239. """\
  3240. [1 ]\n\
  3241. [- y]\n\
  3242. [x ]\n\
  3243. [ ]\n\
  3244. [z w]\
  3245. """
  3246. ucode_str = \
  3247. """\
  3248. ⎡1 ⎤\n\
  3249. ⎢─ y⎥\n\
  3250. ⎢x ⎥\n\
  3251. ⎢ ⎥\n\
  3252. ⎣z w⎦\
  3253. """
  3254. assert pretty(M) == ascii_str
  3255. assert upretty(M) == ucode_str
  3256. ascii_str = \
  3257. """\
  3258. [1 ]\n\
  3259. [- y z]\n\
  3260. [x ]\
  3261. """
  3262. ucode_str = \
  3263. """\
  3264. ⎡1 ⎤\n\
  3265. ⎢─ y z⎥\n\
  3266. ⎣x ⎦\
  3267. """
  3268. assert pretty(M1) == ascii_str
  3269. assert upretty(M1) == ucode_str
  3270. ascii_str = \
  3271. """\
  3272. [[1 y] ]\n\
  3273. [[-- -] [z ]]\n\
  3274. [[ 2 x] [ y 2 ] [- y*z]]\n\
  3275. [[x ] [ - y ] [x ]]\n\
  3276. [[ ] [ x ] [ ]]\n\
  3277. [[z w] [ ] [ 2 ]]\n\
  3278. [[- -] [y*z w*y] [z w*z]]\n\
  3279. [[x x] ]\
  3280. """
  3281. ucode_str = \
  3282. """\
  3283. ⎡⎡1 y⎤ ⎤\n\
  3284. ⎢⎢── ─⎥ ⎡z ⎤⎥\n\
  3285. ⎢⎢ 2 x⎥ ⎡ y 2 ⎤ ⎢─ y⋅z⎥⎥\n\
  3286. ⎢⎢x ⎥ ⎢ ─ y ⎥ ⎢x ⎥⎥\n\
  3287. ⎢⎢ ⎥ ⎢ x ⎥ ⎢ ⎥⎥\n\
  3288. ⎢⎢z w⎥ ⎢ ⎥ ⎢ 2 ⎥⎥\n\
  3289. ⎢⎢─ ─⎥ ⎣y⋅z w⋅y⎦ ⎣z w⋅z⎦⎥\n\
  3290. ⎣⎣x x⎦ ⎦\
  3291. """
  3292. assert pretty(M2) == ascii_str
  3293. assert upretty(M2) == ucode_str
  3294. ascii_str = \
  3295. """\
  3296. [ [1 y] ]\n\
  3297. [ [-- -] ]\n\
  3298. [ [ 2 x] [ y 2 ]]\n\
  3299. [ [x ] [ - y ]]\n\
  3300. [ [ ] [ x ]]\n\
  3301. [ [z w] [ ]]\n\
  3302. [ [- -] [y*z w*y]]\n\
  3303. [ [x x] ]\n\
  3304. [ ]\n\
  3305. [[z ] [ w ]]\n\
  3306. [[- y*z] [ - w*y]]\n\
  3307. [[x ] [ x ]]\n\
  3308. [[ ] [ ]]\n\
  3309. [[ 2 ] [ 2 ]]\n\
  3310. [[z w*z] [w*z w ]]\
  3311. """
  3312. ucode_str = \
  3313. """\
  3314. ⎡ ⎡1 y⎤ ⎤\n\
  3315. ⎢ ⎢── ─⎥ ⎥\n\
  3316. ⎢ ⎢ 2 x⎥ ⎡ y 2 ⎤⎥\n\
  3317. ⎢ ⎢x ⎥ ⎢ ─ y ⎥⎥\n\
  3318. ⎢ ⎢ ⎥ ⎢ x ⎥⎥\n\
  3319. ⎢ ⎢z w⎥ ⎢ ⎥⎥\n\
  3320. ⎢ ⎢─ ─⎥ ⎣y⋅z w⋅y⎦⎥\n\
  3321. ⎢ ⎣x x⎦ ⎥\n\
  3322. ⎢ ⎥\n\
  3323. ⎢⎡z ⎤ ⎡ w ⎤⎥\n\
  3324. ⎢⎢─ y⋅z⎥ ⎢ ─ w⋅y⎥⎥\n\
  3325. ⎢⎢x ⎥ ⎢ x ⎥⎥\n\
  3326. ⎢⎢ ⎥ ⎢ ⎥⎥\n\
  3327. ⎢⎢ 2 ⎥ ⎢ 2 ⎥⎥\n\
  3328. ⎣⎣z w⋅z⎦ ⎣w⋅z w ⎦⎦\
  3329. """
  3330. assert pretty(M3) == ascii_str
  3331. assert upretty(M3) == ucode_str
  3332. Mrow = ArrayType([[x, y, 1 / z]])
  3333. Mcolumn = ArrayType([[x], [y], [1 / z]])
  3334. Mcol2 = ArrayType([Mcolumn.tolist()])
  3335. ascii_str = \
  3336. """\
  3337. [[ 1]]\n\
  3338. [[x y -]]\n\
  3339. [[ z]]\
  3340. """
  3341. ucode_str = \
  3342. """\
  3343. ⎡⎡ 1⎤⎤\n\
  3344. ⎢⎢x y ─⎥⎥\n\
  3345. ⎣⎣ z⎦⎦\
  3346. """
  3347. assert pretty(Mrow) == ascii_str
  3348. assert upretty(Mrow) == ucode_str
  3349. ascii_str = \
  3350. """\
  3351. [x]\n\
  3352. [ ]\n\
  3353. [y]\n\
  3354. [ ]\n\
  3355. [1]\n\
  3356. [-]\n\
  3357. [z]\
  3358. """
  3359. ucode_str = \
  3360. """\
  3361. ⎡x⎤\n\
  3362. ⎢ ⎥\n\
  3363. ⎢y⎥\n\
  3364. ⎢ ⎥\n\
  3365. ⎢1⎥\n\
  3366. ⎢─⎥\n\
  3367. ⎣z⎦\
  3368. """
  3369. assert pretty(Mcolumn) == ascii_str
  3370. assert upretty(Mcolumn) == ucode_str
  3371. ascii_str = \
  3372. """\
  3373. [[x]]\n\
  3374. [[ ]]\n\
  3375. [[y]]\n\
  3376. [[ ]]\n\
  3377. [[1]]\n\
  3378. [[-]]\n\
  3379. [[z]]\
  3380. """
  3381. ucode_str = \
  3382. """\
  3383. ⎡⎡x⎤⎤\n\
  3384. ⎢⎢ ⎥⎥\n\
  3385. ⎢⎢y⎥⎥\n\
  3386. ⎢⎢ ⎥⎥\n\
  3387. ⎢⎢1⎥⎥\n\
  3388. ⎢⎢─⎥⎥\n\
  3389. ⎣⎣z⎦⎦\
  3390. """
  3391. assert pretty(Mcol2) == ascii_str
  3392. assert upretty(Mcol2) == ucode_str
  3393. def test_tensor_TensorProduct():
  3394. A = MatrixSymbol("A", 3, 3)
  3395. B = MatrixSymbol("B", 3, 3)
  3396. assert upretty(TensorProduct(A, B)) == "A\u2297B"
  3397. assert upretty(TensorProduct(A, B, A)) == "A\u2297B\u2297A"
  3398. def test_diffgeom_print_WedgeProduct():
  3399. from sympy.diffgeom.rn import R2
  3400. from sympy.diffgeom import WedgeProduct
  3401. wp = WedgeProduct(R2.dx, R2.dy)
  3402. assert upretty(wp) == "ⅆ x∧ⅆ y"
  3403. assert pretty(wp) == r"d x/\d y"
  3404. def test_Adjoint():
  3405. X = MatrixSymbol('X', 2, 2)
  3406. Y = MatrixSymbol('Y', 2, 2)
  3407. assert pretty(Adjoint(X)) == " +\nX "
  3408. assert pretty(Adjoint(X + Y)) == " +\n(X + Y) "
  3409. assert pretty(Adjoint(X) + Adjoint(Y)) == " + +\nX + Y "
  3410. assert pretty(Adjoint(X*Y)) == " +\n(X*Y) "
  3411. assert pretty(Adjoint(Y)*Adjoint(X)) == " + +\nY *X "
  3412. assert pretty(Adjoint(X**2)) == " +\n/ 2\\ \n\\X / "
  3413. assert pretty(Adjoint(X)**2) == " 2\n/ +\\ \n\\X / "
  3414. assert pretty(Adjoint(Inverse(X))) == " +\n/ -1\\ \n\\X / "
  3415. assert pretty(Inverse(Adjoint(X))) == " -1\n/ +\\ \n\\X / "
  3416. assert pretty(Adjoint(Transpose(X))) == " +\n/ T\\ \n\\X / "
  3417. assert pretty(Transpose(Adjoint(X))) == " T\n/ +\\ \n\\X / "
  3418. assert upretty(Adjoint(X)) == " †\nX "
  3419. assert upretty(Adjoint(X + Y)) == " †\n(X + Y) "
  3420. assert upretty(Adjoint(X) + Adjoint(Y)) == " † †\nX + Y "
  3421. assert upretty(Adjoint(X*Y)) == " †\n(X⋅Y) "
  3422. assert upretty(Adjoint(Y)*Adjoint(X)) == " † †\nY ⋅X "
  3423. assert upretty(Adjoint(X**2)) == \
  3424. " †\n⎛ 2⎞ \n⎝X ⎠ "
  3425. assert upretty(Adjoint(X)**2) == \
  3426. " 2\n⎛ †⎞ \n⎝X ⎠ "
  3427. assert upretty(Adjoint(Inverse(X))) == \
  3428. " †\n⎛ -1⎞ \n⎝X ⎠ "
  3429. assert upretty(Inverse(Adjoint(X))) == \
  3430. " -1\n⎛ †⎞ \n⎝X ⎠ "
  3431. assert upretty(Adjoint(Transpose(X))) == \
  3432. " †\n⎛ T⎞ \n⎝X ⎠ "
  3433. assert upretty(Transpose(Adjoint(X))) == \
  3434. " T\n⎛ †⎞ \n⎝X ⎠ "
  3435. m = Matrix(((1, 2), (3, 4)))
  3436. assert upretty(Adjoint(m)) == \
  3437. ' †\n'\
  3438. '⎡1 2⎤ \n'\
  3439. '⎢ ⎥ \n'\
  3440. '⎣3 4⎦ '
  3441. assert upretty(Adjoint(m+X)) == \
  3442. ' †\n'\
  3443. '⎛⎡1 2⎤ ⎞ \n'\
  3444. '⎜⎢ ⎥ + X⎟ \n'\
  3445. '⎝⎣3 4⎦ ⎠ '
  3446. assert upretty(Adjoint(BlockMatrix(((OneMatrix(2, 2), X),
  3447. (m, ZeroMatrix(2, 2)))))) == \
  3448. ' †\n'\
  3449. '⎡ 𝟙 X⎤ \n'\
  3450. '⎢ ⎥ \n'\
  3451. '⎢⎡1 2⎤ ⎥ \n'\
  3452. '⎢⎢ ⎥ 𝟘⎥ \n'\
  3453. '⎣⎣3 4⎦ ⎦ '
  3454. def test_Transpose():
  3455. X = MatrixSymbol('X', 2, 2)
  3456. Y = MatrixSymbol('Y', 2, 2)
  3457. assert pretty(Transpose(X)) == " T\nX "
  3458. assert pretty(Transpose(X + Y)) == " T\n(X + Y) "
  3459. assert pretty(Transpose(X) + Transpose(Y)) == " T T\nX + Y "
  3460. assert pretty(Transpose(X*Y)) == " T\n(X*Y) "
  3461. assert pretty(Transpose(Y)*Transpose(X)) == " T T\nY *X "
  3462. assert pretty(Transpose(X**2)) == " T\n/ 2\\ \n\\X / "
  3463. assert pretty(Transpose(X)**2) == " 2\n/ T\\ \n\\X / "
  3464. assert pretty(Transpose(Inverse(X))) == " T\n/ -1\\ \n\\X / "
  3465. assert pretty(Inverse(Transpose(X))) == " -1\n/ T\\ \n\\X / "
  3466. assert upretty(Transpose(X)) == " T\nX "
  3467. assert upretty(Transpose(X + Y)) == " T\n(X + Y) "
  3468. assert upretty(Transpose(X) + Transpose(Y)) == " T T\nX + Y "
  3469. assert upretty(Transpose(X*Y)) == " T\n(X⋅Y) "
  3470. assert upretty(Transpose(Y)*Transpose(X)) == " T T\nY ⋅X "
  3471. assert upretty(Transpose(X**2)) == \
  3472. " T\n⎛ 2⎞ \n⎝X ⎠ "
  3473. assert upretty(Transpose(X)**2) == \
  3474. " 2\n⎛ T⎞ \n⎝X ⎠ "
  3475. assert upretty(Transpose(Inverse(X))) == \
  3476. " T\n⎛ -1⎞ \n⎝X ⎠ "
  3477. assert upretty(Inverse(Transpose(X))) == \
  3478. " -1\n⎛ T⎞ \n⎝X ⎠ "
  3479. m = Matrix(((1, 2), (3, 4)))
  3480. assert upretty(Transpose(m)) == \
  3481. ' T\n'\
  3482. '⎡1 2⎤ \n'\
  3483. '⎢ ⎥ \n'\
  3484. '⎣3 4⎦ '
  3485. assert upretty(Transpose(m+X)) == \
  3486. ' T\n'\
  3487. '⎛⎡1 2⎤ ⎞ \n'\
  3488. '⎜⎢ ⎥ + X⎟ \n'\
  3489. '⎝⎣3 4⎦ ⎠ '
  3490. assert upretty(Transpose(BlockMatrix(((OneMatrix(2, 2), X),
  3491. (m, ZeroMatrix(2, 2)))))) == \
  3492. ' T\n'\
  3493. '⎡ 𝟙 X⎤ \n'\
  3494. '⎢ ⎥ \n'\
  3495. '⎢⎡1 2⎤ ⎥ \n'\
  3496. '⎢⎢ ⎥ 𝟘⎥ \n'\
  3497. '⎣⎣3 4⎦ ⎦ '
  3498. def test_pretty_Trace_issue_9044():
  3499. X = Matrix([[1, 2], [3, 4]])
  3500. Y = Matrix([[2, 4], [6, 8]])
  3501. ascii_str_1 = \
  3502. """\
  3503. /[1 2]\\
  3504. tr|[ ]|
  3505. \\[3 4]/\
  3506. """
  3507. ucode_str_1 = \
  3508. """\
  3509. ⎛⎡1 2⎤⎞
  3510. tr⎜⎢ ⎥⎟
  3511. ⎝⎣3 4⎦⎠\
  3512. """
  3513. ascii_str_2 = \
  3514. """\
  3515. /[1 2]\\ /[2 4]\\
  3516. tr|[ ]| + tr|[ ]|
  3517. \\[3 4]/ \\[6 8]/\
  3518. """
  3519. ucode_str_2 = \
  3520. """\
  3521. ⎛⎡1 2⎤⎞ ⎛⎡2 4⎤⎞
  3522. tr⎜⎢ ⎥⎟ + tr⎜⎢ ⎥⎟
  3523. ⎝⎣3 4⎦⎠ ⎝⎣6 8⎦⎠\
  3524. """
  3525. assert pretty(Trace(X)) == ascii_str_1
  3526. assert upretty(Trace(X)) == ucode_str_1
  3527. assert pretty(Trace(X) + Trace(Y)) == ascii_str_2
  3528. assert upretty(Trace(X) + Trace(Y)) == ucode_str_2
  3529. def test_MatrixSlice():
  3530. n = Symbol('n', integer=True)
  3531. x, y, z, w, t, = symbols('x y z w t')
  3532. X = MatrixSymbol('X', n, n)
  3533. Y = MatrixSymbol('Y', 10, 10)
  3534. Z = MatrixSymbol('Z', 10, 10)
  3535. expr = MatrixSlice(X, (None, None, None), (None, None, None))
  3536. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3537. expr = X[x:x + 1, y:y + 1]
  3538. assert pretty(expr) == upretty(expr) == 'X[x:x + 1, y:y + 1]'
  3539. expr = X[x:x + 1:2, y:y + 1:2]
  3540. assert pretty(expr) == upretty(expr) == 'X[x:x + 1:2, y:y + 1:2]'
  3541. expr = X[:x, y:]
  3542. assert pretty(expr) == upretty(expr) == 'X[:x, y:]'
  3543. expr = X[:x, y:]
  3544. assert pretty(expr) == upretty(expr) == 'X[:x, y:]'
  3545. expr = X[x:, :y]
  3546. assert pretty(expr) == upretty(expr) == 'X[x:, :y]'
  3547. expr = X[x:y, z:w]
  3548. assert pretty(expr) == upretty(expr) == 'X[x:y, z:w]'
  3549. expr = X[x:y:t, w:t:x]
  3550. assert pretty(expr) == upretty(expr) == 'X[x:y:t, w:t:x]'
  3551. expr = X[x::y, t::w]
  3552. assert pretty(expr) == upretty(expr) == 'X[x::y, t::w]'
  3553. expr = X[:x:y, :t:w]
  3554. assert pretty(expr) == upretty(expr) == 'X[:x:y, :t:w]'
  3555. expr = X[::x, ::y]
  3556. assert pretty(expr) == upretty(expr) == 'X[::x, ::y]'
  3557. expr = MatrixSlice(X, (0, None, None), (0, None, None))
  3558. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3559. expr = MatrixSlice(X, (None, n, None), (None, n, None))
  3560. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3561. expr = MatrixSlice(X, (0, n, None), (0, n, None))
  3562. assert pretty(expr) == upretty(expr) == 'X[:, :]'
  3563. expr = MatrixSlice(X, (0, n, 2), (0, n, 2))
  3564. assert pretty(expr) == upretty(expr) == 'X[::2, ::2]'
  3565. expr = X[1:2:3, 4:5:6]
  3566. assert pretty(expr) == upretty(expr) == 'X[1:2:3, 4:5:6]'
  3567. expr = X[1:3:5, 4:6:8]
  3568. assert pretty(expr) == upretty(expr) == 'X[1:3:5, 4:6:8]'
  3569. expr = X[1:10:2]
  3570. assert pretty(expr) == upretty(expr) == 'X[1:10:2, :]'
  3571. expr = Y[:5, 1:9:2]
  3572. assert pretty(expr) == upretty(expr) == 'Y[:5, 1:9:2]'
  3573. expr = Y[:5, 1:10:2]
  3574. assert pretty(expr) == upretty(expr) == 'Y[:5, 1::2]'
  3575. expr = Y[5, :5:2]
  3576. assert pretty(expr) == upretty(expr) == 'Y[5:6, :5:2]'
  3577. expr = X[0:1, 0:1]
  3578. assert pretty(expr) == upretty(expr) == 'X[:1, :1]'
  3579. expr = X[0:1:2, 0:1:2]
  3580. assert pretty(expr) == upretty(expr) == 'X[:1:2, :1:2]'
  3581. expr = (Y + Z)[2:, 2:]
  3582. assert pretty(expr) == upretty(expr) == '(Y + Z)[2:, 2:]'
  3583. def test_MatrixExpressions():
  3584. n = Symbol('n', integer=True)
  3585. X = MatrixSymbol('X', n, n)
  3586. assert pretty(X) == upretty(X) == "X"
  3587. # Apply function elementwise (`ElementwiseApplyFunc`):
  3588. expr = (X.T*X).applyfunc(sin)
  3589. ascii_str = """\
  3590. / T \\\n\
  3591. (d -> sin(d)).\\X *X/\
  3592. """
  3593. ucode_str = """\
  3594. ⎛ T ⎞\n\
  3595. (d ↦ sin(d))˳⎝X ⋅X⎠\
  3596. """
  3597. assert pretty(expr) == ascii_str
  3598. assert upretty(expr) == ucode_str
  3599. lamda = Lambda(x, 1/x)
  3600. expr = (n*X).applyfunc(lamda)
  3601. ascii_str = """\
  3602. / 1\\ \n\
  3603. |x -> -|.(n*X)\n\
  3604. \\ x/ \
  3605. """
  3606. ucode_str = """\
  3607. ⎛ 1⎞ \n\
  3608. ⎜x ↦ ─⎟˳(n⋅X)\n\
  3609. ⎝ x⎠ \
  3610. """
  3611. assert pretty(expr) == ascii_str
  3612. assert upretty(expr) == ucode_str
  3613. def test_pretty_dotproduct():
  3614. from sympy.matrices.expressions.dotproduct import DotProduct
  3615. n = symbols("n", integer=True)
  3616. A = MatrixSymbol('A', n, 1)
  3617. B = MatrixSymbol('B', n, 1)
  3618. C = Matrix(1, 3, [1, 2, 3])
  3619. D = Matrix(1, 3, [1, 3, 4])
  3620. assert pretty(DotProduct(A, B)) == "A*B"
  3621. assert pretty(DotProduct(C, D)) == "[1 2 3]*[1 3 4]"
  3622. assert upretty(DotProduct(A, B)) == "A⋅B"
  3623. assert upretty(DotProduct(C, D)) == "[1 2 3]⋅[1 3 4]"
  3624. def test_pretty_Determinant():
  3625. from sympy.matrices import Determinant, Inverse, BlockMatrix, OneMatrix, ZeroMatrix
  3626. m = Matrix(((1, 2), (3, 4)))
  3627. assert upretty(Determinant(m)) == '│1 2│\n│ │\n│3 4│'
  3628. assert upretty(Determinant(Inverse(m))) == \
  3629. '│ -1│\n'\
  3630. '│⎡1 2⎤ │\n'\
  3631. '│⎢ ⎥ │\n'\
  3632. '│⎣3 4⎦ │'
  3633. X = MatrixSymbol('X', 2, 2)
  3634. assert upretty(Determinant(X)) == '│X│'
  3635. assert upretty(Determinant(X + m)) == \
  3636. '│⎡1 2⎤ │\n'\
  3637. '│⎢ ⎥ + X│\n'\
  3638. '│⎣3 4⎦ │'
  3639. assert upretty(Determinant(BlockMatrix(((OneMatrix(2, 2), X),
  3640. (m, ZeroMatrix(2, 2)))))) == \
  3641. '│ 𝟙 X│\n'\
  3642. '│ │\n'\
  3643. '│⎡1 2⎤ │\n'\
  3644. '│⎢ ⎥ 𝟘│\n'\
  3645. '│⎣3 4⎦ │'
  3646. def test_pretty_piecewise():
  3647. expr = Piecewise((x, x < 1), (x**2, True))
  3648. ascii_str = \
  3649. """\
  3650. /x for x < 1\n\
  3651. | \n\
  3652. < 2 \n\
  3653. |x otherwise\n\
  3654. \\ \
  3655. """
  3656. ucode_str = \
  3657. """\
  3658. ⎧x for x < 1\n\
  3659. ⎪ \n\
  3660. ⎨ 2 \n\
  3661. ⎪x otherwise\n\
  3662. ⎩ \
  3663. """
  3664. assert pretty(expr) == ascii_str
  3665. assert upretty(expr) == ucode_str
  3666. expr = -Piecewise((x, x < 1), (x**2, True))
  3667. ascii_str = \
  3668. """\
  3669. //x for x < 1\\\n\
  3670. || |\n\
  3671. -|< 2 |\n\
  3672. ||x otherwise|\n\
  3673. \\\\ /\
  3674. """
  3675. ucode_str = \
  3676. """\
  3677. ⎛⎧x for x < 1⎞\n\
  3678. ⎜⎪ ⎟\n\
  3679. -⎜⎨ 2 ⎟\n\
  3680. ⎜⎪x otherwise⎟\n\
  3681. ⎝⎩ ⎠\
  3682. """
  3683. assert pretty(expr) == ascii_str
  3684. assert upretty(expr) == ucode_str
  3685. expr = x + Piecewise((x, x > 0), (y, True)) + Piecewise((x/y, x < 2),
  3686. (y**2, x > 2), (1, True)) + 1
  3687. ascii_str = \
  3688. """\
  3689. //x \\ \n\
  3690. ||- for x < 2| \n\
  3691. ||y | \n\
  3692. //x for x > 0\\ || | \n\
  3693. x + |< | + |< 2 | + 1\n\
  3694. \\\\y otherwise/ ||y for x > 2| \n\
  3695. || | \n\
  3696. ||1 otherwise| \n\
  3697. \\\\ / \
  3698. """
  3699. ucode_str = \
  3700. """\
  3701. ⎛⎧x ⎞ \n\
  3702. ⎜⎪─ for x < 2⎟ \n\
  3703. ⎜⎪y ⎟ \n\
  3704. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟ \n\
  3705. x + ⎜⎨ ⎟ + ⎜⎨ 2 ⎟ + 1\n\
  3706. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟ \n\
  3707. ⎜⎪ ⎟ \n\
  3708. ⎜⎪1 otherwise⎟ \n\
  3709. ⎝⎩ ⎠ \
  3710. """
  3711. assert pretty(expr) == ascii_str
  3712. assert upretty(expr) == ucode_str
  3713. expr = x - Piecewise((x, x > 0), (y, True)) + Piecewise((x/y, x < 2),
  3714. (y**2, x > 2), (1, True)) + 1
  3715. ascii_str = \
  3716. """\
  3717. //x \\ \n\
  3718. ||- for x < 2| \n\
  3719. ||y | \n\
  3720. //x for x > 0\\ || | \n\
  3721. x - |< | + |< 2 | + 1\n\
  3722. \\\\y otherwise/ ||y for x > 2| \n\
  3723. || | \n\
  3724. ||1 otherwise| \n\
  3725. \\\\ / \
  3726. """
  3727. ucode_str = \
  3728. """\
  3729. ⎛⎧x ⎞ \n\
  3730. ⎜⎪─ for x < 2⎟ \n\
  3731. ⎜⎪y ⎟ \n\
  3732. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟ \n\
  3733. x - ⎜⎨ ⎟ + ⎜⎨ 2 ⎟ + 1\n\
  3734. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟ \n\
  3735. ⎜⎪ ⎟ \n\
  3736. ⎜⎪1 otherwise⎟ \n\
  3737. ⎝⎩ ⎠ \
  3738. """
  3739. assert pretty(expr) == ascii_str
  3740. assert upretty(expr) == ucode_str
  3741. expr = x*Piecewise((x, x > 0), (y, True))
  3742. ascii_str = \
  3743. """\
  3744. //x for x > 0\\\n\
  3745. x*|< |\n\
  3746. \\\\y otherwise/\
  3747. """
  3748. ucode_str = \
  3749. """\
  3750. ⎛⎧x for x > 0⎞\n\
  3751. x⋅⎜⎨ ⎟\n\
  3752. ⎝⎩y otherwise⎠\
  3753. """
  3754. assert pretty(expr) == ascii_str
  3755. assert upretty(expr) == ucode_str
  3756. expr = Piecewise((x, x > 0), (y, True))*Piecewise((x/y, x < 2), (y**2, x >
  3757. 2), (1, True))
  3758. ascii_str = \
  3759. """\
  3760. //x \\\n\
  3761. ||- for x < 2|\n\
  3762. ||y |\n\
  3763. //x for x > 0\\ || |\n\
  3764. |< |*|< 2 |\n\
  3765. \\\\y otherwise/ ||y for x > 2|\n\
  3766. || |\n\
  3767. ||1 otherwise|\n\
  3768. \\\\ /\
  3769. """
  3770. ucode_str = \
  3771. """\
  3772. ⎛⎧x ⎞\n\
  3773. ⎜⎪─ for x < 2⎟\n\
  3774. ⎜⎪y ⎟\n\
  3775. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟\n\
  3776. ⎜⎨ ⎟⋅⎜⎨ 2 ⎟\n\
  3777. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟\n\
  3778. ⎜⎪ ⎟\n\
  3779. ⎜⎪1 otherwise⎟\n\
  3780. ⎝⎩ ⎠\
  3781. """
  3782. assert pretty(expr) == ascii_str
  3783. assert upretty(expr) == ucode_str
  3784. expr = -Piecewise((x, x > 0), (y, True))*Piecewise((x/y, x < 2), (y**2, x
  3785. > 2), (1, True))
  3786. ascii_str = \
  3787. """\
  3788. //x \\\n\
  3789. ||- for x < 2|\n\
  3790. ||y |\n\
  3791. //x for x > 0\\ || |\n\
  3792. -|< |*|< 2 |\n\
  3793. \\\\y otherwise/ ||y for x > 2|\n\
  3794. || |\n\
  3795. ||1 otherwise|\n\
  3796. \\\\ /\
  3797. """
  3798. ucode_str = \
  3799. """\
  3800. ⎛⎧x ⎞\n\
  3801. ⎜⎪─ for x < 2⎟\n\
  3802. ⎜⎪y ⎟\n\
  3803. ⎛⎧x for x > 0⎞ ⎜⎪ ⎟\n\
  3804. -⎜⎨ ⎟⋅⎜⎨ 2 ⎟\n\
  3805. ⎝⎩y otherwise⎠ ⎜⎪y for x > 2⎟\n\
  3806. ⎜⎪ ⎟\n\
  3807. ⎜⎪1 otherwise⎟\n\
  3808. ⎝⎩ ⎠\
  3809. """
  3810. assert pretty(expr) == ascii_str
  3811. assert upretty(expr) == ucode_str
  3812. expr = Piecewise((0, Abs(1/y) < 1), (1, Abs(y) < 1), (y*meijerg(((2, 1),
  3813. ()), ((), (1, 0)), 1/y), True))
  3814. ascii_str = \
  3815. """\
  3816. / 1 \n\
  3817. | 0 for --- < 1\n\
  3818. | |y| \n\
  3819. | \n\
  3820. < 1 for |y| < 1\n\
  3821. | \n\
  3822. | __0, 2 /1, 2 | 1\\ \n\
  3823. |y*/__ | | -| otherwise \n\
  3824. \\ \\_|2, 2 \\ 0, 1 | y/ \
  3825. """
  3826. ucode_str = \
  3827. """\
  3828. ⎧ 1 \n\
  3829. ⎪ 0 for ─── < 1\n\
  3830. ⎪ │y│ \n\
  3831. ⎪ \n\
  3832. ⎨ 1 for │y│ < 1\n\
  3833. ⎪ \n\
  3834. ⎪ ╭─╮0, 2 ⎛1, 2 │ 1⎞ \n\
  3835. ⎪y⋅│╶┐ ⎜ │ ─⎟ otherwise \n\
  3836. ⎩ ╰─╯2, 2 ⎝ 0, 1 │ y⎠ \
  3837. """
  3838. assert pretty(expr) == ascii_str
  3839. assert upretty(expr) == ucode_str
  3840. # XXX: We have to use evaluate=False here because Piecewise._eval_power
  3841. # denests the power.
  3842. expr = Pow(Piecewise((x, x > 0), (y, True)), 2, evaluate=False)
  3843. ascii_str = \
  3844. """\
  3845. 2\n\
  3846. //x for x > 0\\ \n\
  3847. |< | \n\
  3848. \\\\y otherwise/ \
  3849. """
  3850. ucode_str = \
  3851. """\
  3852. 2\n\
  3853. ⎛⎧x for x > 0⎞ \n\
  3854. ⎜⎨ ⎟ \n\
  3855. ⎝⎩y otherwise⎠ \
  3856. """
  3857. assert pretty(expr) == ascii_str
  3858. assert upretty(expr) == ucode_str
  3859. def test_pretty_ITE():
  3860. expr = ITE(x, y, z)
  3861. assert pretty(expr) == (
  3862. '/y for x \n'
  3863. '< \n'
  3864. '\\z otherwise'
  3865. )
  3866. assert upretty(expr) == """\
  3867. ⎧y for x \n\
  3868. ⎨ \n\
  3869. ⎩z otherwise\
  3870. """
  3871. def test_pretty_seq():
  3872. expr = ()
  3873. ascii_str = \
  3874. """\
  3875. ()\
  3876. """
  3877. ucode_str = \
  3878. """\
  3879. ()\
  3880. """
  3881. assert pretty(expr) == ascii_str
  3882. assert upretty(expr) == ucode_str
  3883. expr = []
  3884. ascii_str = \
  3885. """\
  3886. []\
  3887. """
  3888. ucode_str = \
  3889. """\
  3890. []\
  3891. """
  3892. assert pretty(expr) == ascii_str
  3893. assert upretty(expr) == ucode_str
  3894. expr = {}
  3895. expr_2 = {}
  3896. ascii_str = \
  3897. """\
  3898. {}\
  3899. """
  3900. ucode_str = \
  3901. """\
  3902. {}\
  3903. """
  3904. assert pretty(expr) == ascii_str
  3905. assert pretty(expr_2) == ascii_str
  3906. assert upretty(expr) == ucode_str
  3907. assert upretty(expr_2) == ucode_str
  3908. expr = (1/x,)
  3909. ascii_str = \
  3910. """\
  3911. 1 \n\
  3912. (-,)\n\
  3913. x \
  3914. """
  3915. ucode_str = \
  3916. """\
  3917. ⎛1 ⎞\n\
  3918. ⎜─,⎟\n\
  3919. ⎝x ⎠\
  3920. """
  3921. assert pretty(expr) == ascii_str
  3922. assert upretty(expr) == ucode_str
  3923. expr = [x**2, 1/x, x, y, sin(th)**2/cos(ph)**2]
  3924. ascii_str = \
  3925. """\
  3926. 2 \n\
  3927. 2 1 sin (theta) \n\
  3928. [x , -, x, y, -----------]\n\
  3929. x 2 \n\
  3930. cos (phi) \
  3931. """
  3932. ucode_str = \
  3933. """\
  3934. ⎡ 2 ⎤\n\
  3935. ⎢ 2 1 sin (θ)⎥\n\
  3936. ⎢x , ─, x, y, ───────⎥\n\
  3937. ⎢ x 2 ⎥\n\
  3938. ⎣ cos (φ)⎦\
  3939. """
  3940. assert pretty(expr) == ascii_str
  3941. assert upretty(expr) == ucode_str
  3942. expr = (x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  3943. ascii_str = \
  3944. """\
  3945. 2 \n\
  3946. 2 1 sin (theta) \n\
  3947. (x , -, x, y, -----------)\n\
  3948. x 2 \n\
  3949. cos (phi) \
  3950. """
  3951. ucode_str = \
  3952. """\
  3953. ⎛ 2 ⎞\n\
  3954. ⎜ 2 1 sin (θ)⎟\n\
  3955. ⎜x , ─, x, y, ───────⎟\n\
  3956. ⎜ x 2 ⎟\n\
  3957. ⎝ cos (φ)⎠\
  3958. """
  3959. assert pretty(expr) == ascii_str
  3960. assert upretty(expr) == ucode_str
  3961. expr = Tuple(x**2, 1/x, x, y, sin(th)**2/cos(ph)**2)
  3962. ascii_str = \
  3963. """\
  3964. 2 \n\
  3965. 2 1 sin (theta) \n\
  3966. (x , -, x, y, -----------)\n\
  3967. x 2 \n\
  3968. cos (phi) \
  3969. """
  3970. ucode_str = \
  3971. """\
  3972. ⎛ 2 ⎞\n\
  3973. ⎜ 2 1 sin (θ)⎟\n\
  3974. ⎜x , ─, x, y, ───────⎟\n\
  3975. ⎜ x 2 ⎟\n\
  3976. ⎝ cos (φ)⎠\
  3977. """
  3978. assert pretty(expr) == ascii_str
  3979. assert upretty(expr) == ucode_str
  3980. expr = {x: sin(x)}
  3981. expr_2 = Dict({x: sin(x)})
  3982. ascii_str = \
  3983. """\
  3984. {x: sin(x)}\
  3985. """
  3986. ucode_str = \
  3987. """\
  3988. {x: sin(x)}\
  3989. """
  3990. assert pretty(expr) == ascii_str
  3991. assert pretty(expr_2) == ascii_str
  3992. assert upretty(expr) == ucode_str
  3993. assert upretty(expr_2) == ucode_str
  3994. expr = {1/x: 1/y, x: sin(x)**2}
  3995. expr_2 = Dict({1/x: 1/y, x: sin(x)**2})
  3996. ascii_str = \
  3997. """\
  3998. 1 1 2 \n\
  3999. {-: -, x: sin (x)}\n\
  4000. x y \
  4001. """
  4002. ucode_str = \
  4003. """\
  4004. ⎧1 1 2 ⎫\n\
  4005. ⎨─: ─, x: sin (x)⎬\n\
  4006. ⎩x y ⎭\
  4007. """
  4008. assert pretty(expr) == ascii_str
  4009. assert pretty(expr_2) == ascii_str
  4010. assert upretty(expr) == ucode_str
  4011. assert upretty(expr_2) == ucode_str
  4012. # There used to be a bug with pretty-printing sequences of even height.
  4013. expr = [x**2]
  4014. ascii_str = \
  4015. """\
  4016. 2 \n\
  4017. [x ]\
  4018. """
  4019. ucode_str = \
  4020. """\
  4021. ⎡ 2⎤\n\
  4022. ⎣x ⎦\
  4023. """
  4024. assert pretty(expr) == ascii_str
  4025. assert upretty(expr) == ucode_str
  4026. expr = (x**2,)
  4027. ascii_str = \
  4028. """\
  4029. 2 \n\
  4030. (x ,)\
  4031. """
  4032. ucode_str = \
  4033. """\
  4034. ⎛ 2 ⎞\n\
  4035. ⎝x ,⎠\
  4036. """
  4037. assert pretty(expr) == ascii_str
  4038. assert upretty(expr) == ucode_str
  4039. expr = Tuple(x**2)
  4040. ascii_str = \
  4041. """\
  4042. 2 \n\
  4043. (x ,)\
  4044. """
  4045. ucode_str = \
  4046. """\
  4047. ⎛ 2 ⎞\n\
  4048. ⎝x ,⎠\
  4049. """
  4050. assert pretty(expr) == ascii_str
  4051. assert upretty(expr) == ucode_str
  4052. expr = {x**2: 1}
  4053. expr_2 = Dict({x**2: 1})
  4054. ascii_str = \
  4055. """\
  4056. 2 \n\
  4057. {x : 1}\
  4058. """
  4059. ucode_str = \
  4060. """\
  4061. ⎧ 2 ⎫\n\
  4062. ⎨x : 1⎬\n\
  4063. ⎩ ⎭\
  4064. """
  4065. assert pretty(expr) == ascii_str
  4066. assert pretty(expr_2) == ascii_str
  4067. assert upretty(expr) == ucode_str
  4068. assert upretty(expr_2) == ucode_str
  4069. def test_any_object_in_sequence():
  4070. # Cf. issue 5306
  4071. b1 = Basic()
  4072. b2 = Basic(Basic())
  4073. expr = [b2, b1]
  4074. assert pretty(expr) == "[Basic(Basic()), Basic()]"
  4075. assert upretty(expr) == "[Basic(Basic()), Basic()]"
  4076. expr = {b2, b1}
  4077. assert pretty(expr) == "{Basic(), Basic(Basic())}"
  4078. assert upretty(expr) == "{Basic(), Basic(Basic())}"
  4079. expr = {b2: b1, b1: b2}
  4080. expr2 = Dict({b2: b1, b1: b2})
  4081. assert pretty(expr) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4082. assert pretty(
  4083. expr2) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4084. assert upretty(
  4085. expr) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4086. assert upretty(
  4087. expr2) == "{Basic(): Basic(Basic()), Basic(Basic()): Basic()}"
  4088. def test_print_builtin_set():
  4089. assert pretty(set()) == 'set()'
  4090. assert upretty(set()) == 'set()'
  4091. assert pretty(frozenset()) == 'frozenset()'
  4092. assert upretty(frozenset()) == 'frozenset()'
  4093. s1 = {1/x, x}
  4094. s2 = frozenset(s1)
  4095. assert pretty(s1) == \
  4096. """\
  4097. 1 \n\
  4098. {-, x}
  4099. x \
  4100. """
  4101. assert upretty(s1) == \
  4102. """\
  4103. ⎧1 ⎫
  4104. ⎨─, x⎬
  4105. ⎩x ⎭\
  4106. """
  4107. assert pretty(s2) == \
  4108. """\
  4109. 1 \n\
  4110. frozenset({-, x})
  4111. x \
  4112. """
  4113. assert upretty(s2) == \
  4114. """\
  4115. ⎛⎧1 ⎫⎞
  4116. frozenset⎜⎨─, x⎬⎟
  4117. ⎝⎩x ⎭⎠\
  4118. """
  4119. def test_pretty_sets():
  4120. s = FiniteSet
  4121. assert pretty(s(*[x*y, x**2])) == \
  4122. """\
  4123. 2 \n\
  4124. {x , x*y}\
  4125. """
  4126. assert pretty(s(*range(1, 6))) == "{1, 2, 3, 4, 5}"
  4127. assert pretty(s(*range(1, 13))) == "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
  4128. assert pretty({x*y, x**2}) == \
  4129. """\
  4130. 2 \n\
  4131. {x , x*y}\
  4132. """
  4133. assert pretty(set(range(1, 6))) == "{1, 2, 3, 4, 5}"
  4134. assert pretty(set(range(1, 13))) == \
  4135. "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
  4136. assert pretty(frozenset([x*y, x**2])) == \
  4137. """\
  4138. 2 \n\
  4139. frozenset({x , x*y})\
  4140. """
  4141. assert pretty(frozenset(range(1, 6))) == "frozenset({1, 2, 3, 4, 5})"
  4142. assert pretty(frozenset(range(1, 13))) == \
  4143. "frozenset({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})"
  4144. assert pretty(Range(0, 3, 1)) == '{0, 1, 2}'
  4145. ascii_str = '{0, 1, ..., 29}'
  4146. ucode_str = '{0, 1, …, 29}'
  4147. assert pretty(Range(0, 30, 1)) == ascii_str
  4148. assert upretty(Range(0, 30, 1)) == ucode_str
  4149. ascii_str = '{30, 29, ..., 2}'
  4150. ucode_str = '{30, 29, …, 2}'
  4151. assert pretty(Range(30, 1, -1)) == ascii_str
  4152. assert upretty(Range(30, 1, -1)) == ucode_str
  4153. ascii_str = '{0, 2, ...}'
  4154. ucode_str = '{0, 2, …}'
  4155. assert pretty(Range(0, oo, 2)) == ascii_str
  4156. assert upretty(Range(0, oo, 2)) == ucode_str
  4157. ascii_str = '{..., 2, 0}'
  4158. ucode_str = '{…, 2, 0}'
  4159. assert pretty(Range(oo, -2, -2)) == ascii_str
  4160. assert upretty(Range(oo, -2, -2)) == ucode_str
  4161. ascii_str = '{-2, -3, ...}'
  4162. ucode_str = '{-2, -3, …}'
  4163. assert pretty(Range(-2, -oo, -1)) == ascii_str
  4164. assert upretty(Range(-2, -oo, -1)) == ucode_str
  4165. def test_pretty_SetExpr():
  4166. iv = Interval(1, 3)
  4167. se = SetExpr(iv)
  4168. ascii_str = "SetExpr([1, 3])"
  4169. ucode_str = "SetExpr([1, 3])"
  4170. assert pretty(se) == ascii_str
  4171. assert upretty(se) == ucode_str
  4172. def test_pretty_ImageSet():
  4173. imgset = ImageSet(Lambda((x, y), x + y), {1, 2, 3}, {3, 4})
  4174. ascii_str = '{x + y | x in {1, 2, 3}, y in {3, 4}}'
  4175. ucode_str = '{x + y │ x ∊ {1, 2, 3}, y ∊ {3, 4}}'
  4176. assert pretty(imgset) == ascii_str
  4177. assert upretty(imgset) == ucode_str
  4178. imgset = ImageSet(Lambda(((x, y),), x + y), ProductSet({1, 2, 3}, {3, 4}))
  4179. ascii_str = '{x + y | (x, y) in {1, 2, 3} x {3, 4}}'
  4180. ucode_str = '{x + y │ (x, y) ∊ {1, 2, 3} × {3, 4}}'
  4181. assert pretty(imgset) == ascii_str
  4182. assert upretty(imgset) == ucode_str
  4183. imgset = ImageSet(Lambda(x, x**2), S.Naturals)
  4184. ascii_str = '''\
  4185. 2 \n\
  4186. {x | x in Naturals}'''
  4187. ucode_str = '''\
  4188. ⎧ 2 │ ⎫\n\
  4189. ⎨x │ x ∊ ℕ⎬\n\
  4190. ⎩ │ ⎭'''
  4191. assert pretty(imgset) == ascii_str
  4192. assert upretty(imgset) == ucode_str
  4193. # TODO: The "x in N" parts below should be centered independently of the
  4194. # 1/x**2 fraction
  4195. imgset = ImageSet(Lambda(x, 1/x**2), S.Naturals)
  4196. ascii_str = '''\
  4197. 1 \n\
  4198. {-- | x in Naturals}
  4199. 2 \n\
  4200. x '''
  4201. ucode_str = '''\
  4202. ⎧1 │ ⎫\n\
  4203. ⎪── │ x ∊ ℕ⎪\n\
  4204. ⎨ 2 │ ⎬\n\
  4205. ⎪x │ ⎪\n\
  4206. ⎩ │ ⎭'''
  4207. assert pretty(imgset) == ascii_str
  4208. assert upretty(imgset) == ucode_str
  4209. imgset = ImageSet(Lambda((x, y), 1/(x + y)**2), S.Naturals, S.Naturals)
  4210. ascii_str = '''\
  4211. 1 \n\
  4212. {-------- | x in Naturals, y in Naturals}
  4213. 2 \n\
  4214. (x + y) '''
  4215. ucode_str = '''\
  4216. ⎧ 1 │ ⎫
  4217. ⎪──────── │ x ∊ ℕ, y ∊ ℕ⎪
  4218. ⎨ 2 │ ⎬
  4219. ⎪(x + y) │ ⎪
  4220. ⎩ │ ⎭'''
  4221. assert pretty(imgset) == ascii_str
  4222. assert upretty(imgset) == ucode_str
  4223. # issue 23449 centering issue
  4224. assert upretty([Symbol("ihat") / (Symbol("i") + 1)]) == '''\
  4225. ⎡ î ⎤
  4226. ⎢─────⎥
  4227. ⎣i + 1⎦\
  4228. '''
  4229. assert upretty(Matrix([Symbol("ihat"), Symbol("i") + 1])) == '''\
  4230. ⎡ î ⎤
  4231. ⎢ ⎥
  4232. ⎣i + 1⎦\
  4233. '''
  4234. def test_pretty_ConditionSet():
  4235. ascii_str = '{x | x in (-oo, oo) and sin(x) = 0}'
  4236. ucode_str = '{x │ x ∊ ℝ ∧ (sin(x) = 0)}'
  4237. assert pretty(ConditionSet(x, Eq(sin(x), 0), S.Reals)) == ascii_str
  4238. assert upretty(ConditionSet(x, Eq(sin(x), 0), S.Reals)) == ucode_str
  4239. assert pretty(ConditionSet(x, Contains(x, S.Reals, evaluate=False), FiniteSet(1))) == '{1}'
  4240. assert upretty(ConditionSet(x, Contains(x, S.Reals, evaluate=False), FiniteSet(1))) == '{1}'
  4241. assert pretty(ConditionSet(x, And(x > 1, x < -1), FiniteSet(1, 2, 3))) == "EmptySet"
  4242. assert upretty(ConditionSet(x, And(x > 1, x < -1), FiniteSet(1, 2, 3))) == "∅"
  4243. assert pretty(ConditionSet(x, Or(x > 1, x < -1), FiniteSet(1, 2))) == '{2}'
  4244. assert upretty(ConditionSet(x, Or(x > 1, x < -1), FiniteSet(1, 2))) == '{2}'
  4245. condset = ConditionSet(x, 1/x**2 > 0)
  4246. ascii_str = '''\
  4247. 1 \n\
  4248. {x | -- > 0}
  4249. 2 \n\
  4250. x '''
  4251. ucode_str = '''\
  4252. ⎧ │ ⎛1 ⎞⎫
  4253. ⎪x │ ⎜── > 0⎟⎪
  4254. ⎨ │ ⎜ 2 ⎟⎬
  4255. ⎪ │ ⎝x ⎠⎪
  4256. ⎩ │ ⎭'''
  4257. assert pretty(condset) == ascii_str
  4258. assert upretty(condset) == ucode_str
  4259. condset = ConditionSet(x, 1/x**2 > 0, S.Reals)
  4260. ascii_str = '''\
  4261. 1 \n\
  4262. {x | x in (-oo, oo) and -- > 0}
  4263. 2 \n\
  4264. x '''
  4265. ucode_str = '''\
  4266. ⎧ │ ⎛1 ⎞⎫
  4267. ⎪x │ x ∊ ℝ ∧ ⎜── > 0⎟⎪
  4268. ⎨ │ ⎜ 2 ⎟⎬
  4269. ⎪ │ ⎝x ⎠⎪
  4270. ⎩ │ ⎭'''
  4271. assert pretty(condset) == ascii_str
  4272. assert upretty(condset) == ucode_str
  4273. def test_pretty_ComplexRegion():
  4274. from sympy.sets.fancysets import ComplexRegion
  4275. cregion = ComplexRegion(Interval(3, 5)*Interval(4, 6))
  4276. ascii_str = '{x + y*I | x, y in [3, 5] x [4, 6]}'
  4277. ucode_str = '{x + y⋅ⅈ │ x, y ∊ [3, 5] × [4, 6]}'
  4278. assert pretty(cregion) == ascii_str
  4279. assert upretty(cregion) == ucode_str
  4280. cregion = ComplexRegion(Interval(0, 1)*Interval(0, 2*pi), polar=True)
  4281. ascii_str = '{r*(I*sin(theta) + cos(theta)) | r, theta in [0, 1] x [0, 2*pi)}'
  4282. ucode_str = '{r⋅(ⅈ⋅sin(θ) + cos(θ)) │ r, θ ∊ [0, 1] × [0, 2⋅π)}'
  4283. assert pretty(cregion) == ascii_str
  4284. assert upretty(cregion) == ucode_str
  4285. cregion = ComplexRegion(Interval(3, 1/a**2)*Interval(4, 6))
  4286. ascii_str = '''\
  4287. 1 \n\
  4288. {x + y*I | x, y in [3, --] x [4, 6]}
  4289. 2 \n\
  4290. a '''
  4291. ucode_str = '''\
  4292. ⎧ │ ⎡ 1 ⎤ ⎫
  4293. ⎪x + y⋅ⅈ │ x, y ∊ ⎢3, ──⎥ × [4, 6]⎪
  4294. ⎨ │ ⎢ 2⎥ ⎬
  4295. ⎪ │ ⎣ a ⎦ ⎪
  4296. ⎩ │ ⎭'''
  4297. assert pretty(cregion) == ascii_str
  4298. assert upretty(cregion) == ucode_str
  4299. cregion = ComplexRegion(Interval(0, 1/a**2)*Interval(0, 2*pi), polar=True)
  4300. ascii_str = '''\
  4301. 1 \n\
  4302. {r*(I*sin(theta) + cos(theta)) | r, theta in [0, --] x [0, 2*pi)}
  4303. 2 \n\
  4304. a '''
  4305. ucode_str = '''\
  4306. ⎧ │ ⎡ 1 ⎤ ⎫
  4307. ⎪r⋅(ⅈ⋅sin(θ) + cos(θ)) │ r, θ ∊ ⎢0, ──⎥ × [0, 2⋅π)⎪
  4308. ⎨ │ ⎢ 2⎥ ⎬
  4309. ⎪ │ ⎣ a ⎦ ⎪
  4310. ⎩ │ ⎭'''
  4311. assert pretty(cregion) == ascii_str
  4312. assert upretty(cregion) == ucode_str
  4313. def test_pretty_Union_issue_10414():
  4314. a, b = Interval(2, 3), Interval(4, 7)
  4315. ucode_str = '[2, 3] ∪ [4, 7]'
  4316. ascii_str = '[2, 3] U [4, 7]'
  4317. assert upretty(Union(a, b)) == ucode_str
  4318. assert pretty(Union(a, b)) == ascii_str
  4319. def test_pretty_Intersection_issue_10414():
  4320. x, y, z, w = symbols('x, y, z, w')
  4321. a, b = Interval(x, y), Interval(z, w)
  4322. ucode_str = '[x, y] ∩ [z, w]'
  4323. ascii_str = '[x, y] n [z, w]'
  4324. assert upretty(Intersection(a, b)) == ucode_str
  4325. assert pretty(Intersection(a, b)) == ascii_str
  4326. def test_ProductSet_exponent():
  4327. ucode_str = ' 1\n[0, 1] '
  4328. assert upretty(Interval(0, 1)**1) == ucode_str
  4329. ucode_str = ' 2\n[0, 1] '
  4330. assert upretty(Interval(0, 1)**2) == ucode_str
  4331. def test_ProductSet_parenthesis():
  4332. ucode_str = '([4, 7] × {1, 2}) ∪ ([2, 3] × [4, 7])'
  4333. a, b = Interval(2, 3), Interval(4, 7)
  4334. assert upretty(Union(a*b, b*FiniteSet(1, 2))) == ucode_str
  4335. def test_ProductSet_prod_char_issue_10413():
  4336. ascii_str = '[2, 3] x [4, 7]'
  4337. ucode_str = '[2, 3] × [4, 7]'
  4338. a, b = Interval(2, 3), Interval(4, 7)
  4339. assert pretty(a*b) == ascii_str
  4340. assert upretty(a*b) == ucode_str
  4341. def test_pretty_sequences():
  4342. s1 = SeqFormula(a**2, (0, oo))
  4343. s2 = SeqPer((1, 2))
  4344. ascii_str = '[0, 1, 4, 9, ...]'
  4345. ucode_str = '[0, 1, 4, 9, …]'
  4346. assert pretty(s1) == ascii_str
  4347. assert upretty(s1) == ucode_str
  4348. ascii_str = '[1, 2, 1, 2, ...]'
  4349. ucode_str = '[1, 2, 1, 2, …]'
  4350. assert pretty(s2) == ascii_str
  4351. assert upretty(s2) == ucode_str
  4352. s3 = SeqFormula(a**2, (0, 2))
  4353. s4 = SeqPer((1, 2), (0, 2))
  4354. ascii_str = '[0, 1, 4]'
  4355. ucode_str = '[0, 1, 4]'
  4356. assert pretty(s3) == ascii_str
  4357. assert upretty(s3) == ucode_str
  4358. ascii_str = '[1, 2, 1]'
  4359. ucode_str = '[1, 2, 1]'
  4360. assert pretty(s4) == ascii_str
  4361. assert upretty(s4) == ucode_str
  4362. s5 = SeqFormula(a**2, (-oo, 0))
  4363. s6 = SeqPer((1, 2), (-oo, 0))
  4364. ascii_str = '[..., 9, 4, 1, 0]'
  4365. ucode_str = '[…, 9, 4, 1, 0]'
  4366. assert pretty(s5) == ascii_str
  4367. assert upretty(s5) == ucode_str
  4368. ascii_str = '[..., 2, 1, 2, 1]'
  4369. ucode_str = '[…, 2, 1, 2, 1]'
  4370. assert pretty(s6) == ascii_str
  4371. assert upretty(s6) == ucode_str
  4372. ascii_str = '[1, 3, 5, 11, ...]'
  4373. ucode_str = '[1, 3, 5, 11, …]'
  4374. assert pretty(SeqAdd(s1, s2)) == ascii_str
  4375. assert upretty(SeqAdd(s1, s2)) == ucode_str
  4376. ascii_str = '[1, 3, 5]'
  4377. ucode_str = '[1, 3, 5]'
  4378. assert pretty(SeqAdd(s3, s4)) == ascii_str
  4379. assert upretty(SeqAdd(s3, s4)) == ucode_str
  4380. ascii_str = '[..., 11, 5, 3, 1]'
  4381. ucode_str = '[…, 11, 5, 3, 1]'
  4382. assert pretty(SeqAdd(s5, s6)) == ascii_str
  4383. assert upretty(SeqAdd(s5, s6)) == ucode_str
  4384. ascii_str = '[0, 2, 4, 18, ...]'
  4385. ucode_str = '[0, 2, 4, 18, …]'
  4386. assert pretty(SeqMul(s1, s2)) == ascii_str
  4387. assert upretty(SeqMul(s1, s2)) == ucode_str
  4388. ascii_str = '[0, 2, 4]'
  4389. ucode_str = '[0, 2, 4]'
  4390. assert pretty(SeqMul(s3, s4)) == ascii_str
  4391. assert upretty(SeqMul(s3, s4)) == ucode_str
  4392. ascii_str = '[..., 18, 4, 2, 0]'
  4393. ucode_str = '[…, 18, 4, 2, 0]'
  4394. assert pretty(SeqMul(s5, s6)) == ascii_str
  4395. assert upretty(SeqMul(s5, s6)) == ucode_str
  4396. # Sequences with symbolic limits, issue 12629
  4397. s7 = SeqFormula(a**2, (a, 0, x))
  4398. raises(NotImplementedError, lambda: pretty(s7))
  4399. raises(NotImplementedError, lambda: upretty(s7))
  4400. b = Symbol('b')
  4401. s8 = SeqFormula(b*a**2, (a, 0, 2))
  4402. ascii_str = '[0, b, 4*b]'
  4403. ucode_str = '[0, b, 4⋅b]'
  4404. assert pretty(s8) == ascii_str
  4405. assert upretty(s8) == ucode_str
  4406. def test_pretty_FourierSeries():
  4407. f = fourier_series(x, (x, -pi, pi))
  4408. ascii_str = \
  4409. """\
  4410. 2*sin(3*x) \n\
  4411. 2*sin(x) - sin(2*x) + ---------- + ...\n\
  4412. 3 \
  4413. """
  4414. ucode_str = \
  4415. """\
  4416. 2⋅sin(3⋅x) \n\
  4417. 2⋅sin(x) - sin(2⋅x) + ────────── + …\n\
  4418. 3 \
  4419. """
  4420. assert pretty(f) == ascii_str
  4421. assert upretty(f) == ucode_str
  4422. def test_pretty_FormalPowerSeries():
  4423. f = fps(log(1 + x))
  4424. ascii_str = \
  4425. """\
  4426. oo \n\
  4427. ____ \n\
  4428. \\ ` \n\
  4429. \\ -k k \n\
  4430. \\ -(-1) *x \n\
  4431. / -----------\n\
  4432. / k \n\
  4433. /___, \n\
  4434. k = 1 \
  4435. """
  4436. ucode_str = \
  4437. """\
  4438. ∞ \n\
  4439. ____ \n\
  4440. ╲ \n\
  4441. ╲ -k k \n\
  4442. ╲ -(-1) ⋅x \n\
  4443. ╱ ───────────\n\
  4444. ╱ k \n\
  4445. ╱ \n\
  4446. ‾‾‾‾ \n\
  4447. k = 1 \
  4448. """
  4449. assert pretty(f) == ascii_str
  4450. assert upretty(f) == ucode_str
  4451. def test_pretty_limits():
  4452. expr = Limit(x, x, oo)
  4453. ascii_str = \
  4454. """\
  4455. lim x\n\
  4456. x->oo \
  4457. """
  4458. ucode_str = \
  4459. """\
  4460. lim x\n\
  4461. x─→∞ \
  4462. """
  4463. assert pretty(expr) == ascii_str
  4464. assert upretty(expr) == ucode_str
  4465. expr = Limit(x**2, x, 0)
  4466. ascii_str = \
  4467. """\
  4468. 2\n\
  4469. lim x \n\
  4470. x->0+ \
  4471. """
  4472. ucode_str = \
  4473. """\
  4474. 2\n\
  4475. lim x \n\
  4476. x─→0⁺ \
  4477. """
  4478. assert pretty(expr) == ascii_str
  4479. assert upretty(expr) == ucode_str
  4480. expr = Limit(1/x, x, 0)
  4481. ascii_str = \
  4482. """\
  4483. 1\n\
  4484. lim -\n\
  4485. x->0+x\
  4486. """
  4487. ucode_str = \
  4488. """\
  4489. 1\n\
  4490. lim ─\n\
  4491. x─→0⁺x\
  4492. """
  4493. assert pretty(expr) == ascii_str
  4494. assert upretty(expr) == ucode_str
  4495. expr = Limit(sin(x)/x, x, 0)
  4496. ascii_str = \
  4497. """\
  4498. /sin(x)\\\n\
  4499. lim |------|\n\
  4500. x->0+\\ x /\
  4501. """
  4502. ucode_str = \
  4503. """\
  4504. ⎛sin(x)⎞\n\
  4505. lim ⎜──────⎟\n\
  4506. x─→0⁺⎝ x ⎠\
  4507. """
  4508. assert pretty(expr) == ascii_str
  4509. assert upretty(expr) == ucode_str
  4510. expr = Limit(sin(x)/x, x, 0, "-")
  4511. ascii_str = \
  4512. """\
  4513. /sin(x)\\\n\
  4514. lim |------|\n\
  4515. x->0-\\ x /\
  4516. """
  4517. ucode_str = \
  4518. """\
  4519. ⎛sin(x)⎞\n\
  4520. lim ⎜──────⎟\n\
  4521. x─→0⁻⎝ x ⎠\
  4522. """
  4523. assert pretty(expr) == ascii_str
  4524. assert upretty(expr) == ucode_str
  4525. expr = Limit(x + sin(x), x, 0)
  4526. ascii_str = \
  4527. """\
  4528. lim (x + sin(x))\n\
  4529. x->0+ \
  4530. """
  4531. ucode_str = \
  4532. """\
  4533. lim (x + sin(x))\n\
  4534. x─→0⁺ \
  4535. """
  4536. assert pretty(expr) == ascii_str
  4537. assert upretty(expr) == ucode_str
  4538. expr = Limit(x, x, 0)**2
  4539. ascii_str = \
  4540. """\
  4541. 2\n\
  4542. / lim x\\ \n\
  4543. \\x->0+ / \
  4544. """
  4545. ucode_str = \
  4546. """\
  4547. 2\n\
  4548. ⎛ lim x⎞ \n\
  4549. ⎝x─→0⁺ ⎠ \
  4550. """
  4551. assert pretty(expr) == ascii_str
  4552. assert upretty(expr) == ucode_str
  4553. expr = Limit(x*Limit(y/2,y,0), x, 0)
  4554. ascii_str = \
  4555. """\
  4556. / /y\\\\\n\
  4557. lim |x* lim |-||\n\
  4558. x->0+\\ y->0+\\2//\
  4559. """
  4560. ucode_str = \
  4561. """\
  4562. ⎛ ⎛y⎞⎞\n\
  4563. lim ⎜x⋅ lim ⎜─⎟⎟\n\
  4564. x─→0⁺⎝ y─→0⁺⎝2⎠⎠\
  4565. """
  4566. assert pretty(expr) == ascii_str
  4567. assert upretty(expr) == ucode_str
  4568. expr = 2*Limit(x*Limit(y/2,y,0), x, 0)
  4569. ascii_str = \
  4570. """\
  4571. / /y\\\\\n\
  4572. 2* lim |x* lim |-||\n\
  4573. x->0+\\ y->0+\\2//\
  4574. """
  4575. ucode_str = \
  4576. """\
  4577. ⎛ ⎛y⎞⎞\n\
  4578. 2⋅ lim ⎜x⋅ lim ⎜─⎟⎟\n\
  4579. x─→0⁺⎝ y─→0⁺⎝2⎠⎠\
  4580. """
  4581. assert pretty(expr) == ascii_str
  4582. assert upretty(expr) == ucode_str
  4583. expr = Limit(sin(x), x, 0, dir='+-')
  4584. ascii_str = \
  4585. """\
  4586. lim sin(x)\n\
  4587. x->0 \
  4588. """
  4589. ucode_str = \
  4590. """\
  4591. lim sin(x)\n\
  4592. x─→0 \
  4593. """
  4594. assert pretty(expr) == ascii_str
  4595. assert upretty(expr) == ucode_str
  4596. def test_pretty_ComplexRootOf():
  4597. expr = rootof(x**5 + 11*x - 2, 0)
  4598. ascii_str = \
  4599. """\
  4600. / 5 \\\n\
  4601. CRootOf\\x + 11*x - 2, 0/\
  4602. """
  4603. ucode_str = \
  4604. """\
  4605. ⎛ 5 ⎞\n\
  4606. CRootOf⎝x + 11⋅x - 2, 0⎠\
  4607. """
  4608. assert pretty(expr) == ascii_str
  4609. assert upretty(expr) == ucode_str
  4610. def test_pretty_RootSum():
  4611. expr = RootSum(x**5 + 11*x - 2, auto=False)
  4612. ascii_str = \
  4613. """\
  4614. / 5 \\\n\
  4615. RootSum\\x + 11*x - 2/\
  4616. """
  4617. ucode_str = \
  4618. """\
  4619. ⎛ 5 ⎞\n\
  4620. RootSum⎝x + 11⋅x - 2⎠\
  4621. """
  4622. assert pretty(expr) == ascii_str
  4623. assert upretty(expr) == ucode_str
  4624. expr = RootSum(x**5 + 11*x - 2, Lambda(z, exp(z)))
  4625. ascii_str = \
  4626. """\
  4627. / 5 z\\\n\
  4628. RootSum\\x + 11*x - 2, z -> e /\
  4629. """
  4630. ucode_str = \
  4631. """\
  4632. ⎛ 5 z⎞\n\
  4633. RootSum⎝x + 11⋅x - 2, z ↦ ℯ ⎠\
  4634. """
  4635. assert pretty(expr) == ascii_str
  4636. assert upretty(expr) == ucode_str
  4637. def test_GroebnerBasis():
  4638. expr = groebner([], x, y)
  4639. ascii_str = \
  4640. """\
  4641. GroebnerBasis([], x, y, domain=ZZ, order=lex)\
  4642. """
  4643. ucode_str = \
  4644. """\
  4645. GroebnerBasis([], x, y, domain=ℤ, order=lex)\
  4646. """
  4647. assert pretty(expr) == ascii_str
  4648. assert upretty(expr) == ucode_str
  4649. F = [x**2 - 3*y - x + 1, y**2 - 2*x + y - 1]
  4650. expr = groebner(F, x, y, order='grlex')
  4651. ascii_str = \
  4652. """\
  4653. /[ 2 2 ] \\\n\
  4654. GroebnerBasis\\[x - x - 3*y + 1, y - 2*x + y - 1], x, y, domain=ZZ, order=grlex/\
  4655. """
  4656. ucode_str = \
  4657. """\
  4658. ⎛⎡ 2 2 ⎤ ⎞\n\
  4659. GroebnerBasis⎝⎣x - x - 3⋅y + 1, y - 2⋅x + y - 1⎦, x, y, domain=ℤ, order=grlex⎠\
  4660. """
  4661. assert pretty(expr) == ascii_str
  4662. assert upretty(expr) == ucode_str
  4663. expr = expr.fglm('lex')
  4664. ascii_str = \
  4665. """\
  4666. /[ 2 4 3 2 ] \\\n\
  4667. GroebnerBasis\\[2*x - y - y + 1, y + 2*y - 3*y - 16*y + 7], x, y, domain=ZZ, order=lex/\
  4668. """
  4669. ucode_str = \
  4670. """\
  4671. ⎛⎡ 2 4 3 2 ⎤ ⎞\n\
  4672. GroebnerBasis⎝⎣2⋅x - y - y + 1, y + 2⋅y - 3⋅y - 16⋅y + 7⎦, x, y, domain=ℤ, order=lex⎠\
  4673. """
  4674. assert pretty(expr) == ascii_str
  4675. assert upretty(expr) == ucode_str
  4676. def test_pretty_UniversalSet():
  4677. assert pretty(S.UniversalSet) == "UniversalSet"
  4678. assert upretty(S.UniversalSet) == '𝕌'
  4679. def test_pretty_Boolean():
  4680. expr = Not(x, evaluate=False)
  4681. assert pretty(expr) == "Not(x)"
  4682. assert upretty(expr) == "¬x"
  4683. expr = And(x, y)
  4684. assert pretty(expr) == "And(x, y)"
  4685. assert upretty(expr) == "x ∧ y"
  4686. expr = Or(x, y)
  4687. assert pretty(expr) == "Or(x, y)"
  4688. assert upretty(expr) == "x ∨ y"
  4689. syms = symbols('a:f')
  4690. expr = And(*syms)
  4691. assert pretty(expr) == "And(a, b, c, d, e, f)"
  4692. assert upretty(expr) == "a ∧ b ∧ c ∧ d ∧ e ∧ f"
  4693. expr = Or(*syms)
  4694. assert pretty(expr) == "Or(a, b, c, d, e, f)"
  4695. assert upretty(expr) == "a ∨ b ∨ c ∨ d ∨ e ∨ f"
  4696. expr = Xor(x, y, evaluate=False)
  4697. assert pretty(expr) == "Xor(x, y)"
  4698. assert upretty(expr) == "x ⊻ y"
  4699. expr = Nand(x, y, evaluate=False)
  4700. assert pretty(expr) == "Nand(x, y)"
  4701. assert upretty(expr) == "x ⊼ y"
  4702. expr = Nor(x, y, evaluate=False)
  4703. assert pretty(expr) == "Nor(x, y)"
  4704. assert upretty(expr) == "x ⊽ y"
  4705. expr = Implies(x, y, evaluate=False)
  4706. assert pretty(expr) == "Implies(x, y)"
  4707. assert upretty(expr) == "x → y"
  4708. # don't sort args
  4709. expr = Implies(y, x, evaluate=False)
  4710. assert pretty(expr) == "Implies(y, x)"
  4711. assert upretty(expr) == "y → x"
  4712. expr = Equivalent(x, y, evaluate=False)
  4713. assert pretty(expr) == "Equivalent(x, y)"
  4714. assert upretty(expr) == "x ⇔ y"
  4715. expr = Equivalent(y, x, evaluate=False)
  4716. assert pretty(expr) == "Equivalent(x, y)"
  4717. assert upretty(expr) == "x ⇔ y"
  4718. def test_pretty_Domain():
  4719. expr = FF(23)
  4720. assert pretty(expr) == "GF(23)"
  4721. assert upretty(expr) == "ℤ₂₃"
  4722. expr = ZZ
  4723. assert pretty(expr) == "ZZ"
  4724. assert upretty(expr) == "ℤ"
  4725. expr = QQ
  4726. assert pretty(expr) == "QQ"
  4727. assert upretty(expr) == "ℚ"
  4728. expr = RR
  4729. assert pretty(expr) == "RR"
  4730. assert upretty(expr) == "ℝ"
  4731. expr = QQ[x]
  4732. assert pretty(expr) == "QQ[x]"
  4733. assert upretty(expr) == "ℚ[x]"
  4734. expr = QQ[x, y]
  4735. assert pretty(expr) == "QQ[x, y]"
  4736. assert upretty(expr) == "ℚ[x, y]"
  4737. expr = ZZ.frac_field(x)
  4738. assert pretty(expr) == "ZZ(x)"
  4739. assert upretty(expr) == "ℤ(x)"
  4740. expr = ZZ.frac_field(x, y)
  4741. assert pretty(expr) == "ZZ(x, y)"
  4742. assert upretty(expr) == "ℤ(x, y)"
  4743. expr = QQ.poly_ring(x, y, order=grlex)
  4744. assert pretty(expr) == "QQ[x, y, order=grlex]"
  4745. assert upretty(expr) == "ℚ[x, y, order=grlex]"
  4746. expr = QQ.poly_ring(x, y, order=ilex)
  4747. assert pretty(expr) == "QQ[x, y, order=ilex]"
  4748. assert upretty(expr) == "ℚ[x, y, order=ilex]"
  4749. def test_pretty_prec():
  4750. assert xpretty(S("0.3"), full_prec=True, wrap_line=False) == "0.300000000000000"
  4751. assert xpretty(S("0.3"), full_prec="auto", wrap_line=False) == "0.300000000000000"
  4752. assert xpretty(S("0.3"), full_prec=False, wrap_line=False) == "0.3"
  4753. assert xpretty(S("0.3")*x, full_prec=True, use_unicode=False, wrap_line=False) in [
  4754. "0.300000000000000*x",
  4755. "x*0.300000000000000"
  4756. ]
  4757. assert xpretty(S("0.3")*x, full_prec="auto", use_unicode=False, wrap_line=False) in [
  4758. "0.3*x",
  4759. "x*0.3"
  4760. ]
  4761. assert xpretty(S("0.3")*x, full_prec=False, use_unicode=False, wrap_line=False) in [
  4762. "0.3*x",
  4763. "x*0.3"
  4764. ]
  4765. def test_pprint():
  4766. import sys
  4767. from io import StringIO
  4768. fd = StringIO()
  4769. sso = sys.stdout
  4770. sys.stdout = fd
  4771. try:
  4772. pprint(pi, use_unicode=False, wrap_line=False)
  4773. finally:
  4774. sys.stdout = sso
  4775. assert fd.getvalue() == 'pi\n'
  4776. def test_pretty_class():
  4777. """Test that the printer dispatcher correctly handles classes."""
  4778. class C:
  4779. pass # C has no .__class__ and this was causing problems
  4780. class D:
  4781. pass
  4782. assert pretty( C ) == str( C )
  4783. assert pretty( D ) == str( D )
  4784. def test_pretty_no_wrap_line():
  4785. huge_expr = 0
  4786. for i in range(20):
  4787. huge_expr += i*sin(i + x)
  4788. assert xpretty(huge_expr ).find('\n') != -1
  4789. assert xpretty(huge_expr, wrap_line=False).find('\n') == -1
  4790. def test_settings():
  4791. raises(TypeError, lambda: pretty(S(4), method="garbage"))
  4792. def test_pretty_sum():
  4793. from sympy.abc import x, a, b, k, m, n
  4794. expr = Sum(k**k, (k, 0, n))
  4795. ascii_str = \
  4796. """\
  4797. n \n\
  4798. ___ \n\
  4799. \\ ` \n\
  4800. \\ k\n\
  4801. / k \n\
  4802. /__, \n\
  4803. k = 0 \
  4804. """
  4805. ucode_str = \
  4806. """\
  4807. n \n\
  4808. ___ \n\
  4809. ╲ \n\
  4810. ╲ k\n\
  4811. ╱ k \n\
  4812. ╱ \n\
  4813. ‾‾‾ \n\
  4814. k = 0 \
  4815. """
  4816. assert pretty(expr) == ascii_str
  4817. assert upretty(expr) == ucode_str
  4818. expr = Sum(k**k, (k, oo, n))
  4819. ascii_str = \
  4820. """\
  4821. n \n\
  4822. ___ \n\
  4823. \\ ` \n\
  4824. \\ k\n\
  4825. / k \n\
  4826. /__, \n\
  4827. k = oo \
  4828. """
  4829. ucode_str = \
  4830. """\
  4831. n \n\
  4832. ___ \n\
  4833. ╲ \n\
  4834. ╲ k\n\
  4835. ╱ k \n\
  4836. ╱ \n\
  4837. ‾‾‾ \n\
  4838. k = ∞ \
  4839. """
  4840. assert pretty(expr) == ascii_str
  4841. assert upretty(expr) == ucode_str
  4842. expr = Sum(k**(Integral(x**n, (x, -oo, oo))), (k, 0, n**n))
  4843. ascii_str = \
  4844. """\
  4845. n \n\
  4846. n \n\
  4847. ______ \n\
  4848. \\ ` \n\
  4849. \\ oo \n\
  4850. \\ / \n\
  4851. \\ | \n\
  4852. \\ | n \n\
  4853. ) | x dx\n\
  4854. / | \n\
  4855. / / \n\
  4856. / -oo \n\
  4857. / k \n\
  4858. /_____, \n\
  4859. k = 0 \
  4860. """
  4861. ucode_str = \
  4862. """\
  4863. n \n\
  4864. n \n\
  4865. ______ \n\
  4866. ╲ \n\
  4867. ╲ \n\
  4868. ╲ ∞ \n\
  4869. ╲ ⌠ \n\
  4870. ╲ ⎮ n \n\
  4871. ╱ ⎮ x dx\n\
  4872. ╱ ⌡ \n\
  4873. ╱ -∞ \n\
  4874. ╱ k \n\
  4875. ╱ \n\
  4876. ‾‾‾‾‾‾ \n\
  4877. k = 0 \
  4878. """
  4879. assert pretty(expr) == ascii_str
  4880. assert upretty(expr) == ucode_str
  4881. expr = Sum(k**(
  4882. Integral(x**n, (x, -oo, oo))), (k, 0, Integral(x**x, (x, -oo, oo))))
  4883. ascii_str = \
  4884. """\
  4885. oo \n\
  4886. / \n\
  4887. | \n\
  4888. | x \n\
  4889. | x dx \n\
  4890. | \n\
  4891. / \n\
  4892. -oo \n\
  4893. ______ \n\
  4894. \\ ` \n\
  4895. \\ oo \n\
  4896. \\ / \n\
  4897. \\ | \n\
  4898. \\ | n \n\
  4899. ) | x dx\n\
  4900. / | \n\
  4901. / / \n\
  4902. / -oo \n\
  4903. / k \n\
  4904. /_____, \n\
  4905. k = 0 \
  4906. """
  4907. ucode_str = \
  4908. """\
  4909. ∞ \n\
  4910. ⌠ \n\
  4911. ⎮ x \n\
  4912. ⎮ x dx \n\
  4913. ⌡ \n\
  4914. -∞ \n\
  4915. ______ \n\
  4916. ╲ \n\
  4917. ╲ \n\
  4918. ╲ ∞ \n\
  4919. ╲ ⌠ \n\
  4920. ╲ ⎮ n \n\
  4921. ╱ ⎮ x dx\n\
  4922. ╱ ⌡ \n\
  4923. ╱ -∞ \n\
  4924. ╱ k \n\
  4925. ╱ \n\
  4926. ‾‾‾‾‾‾ \n\
  4927. k = 0 \
  4928. """
  4929. assert pretty(expr) == ascii_str
  4930. assert upretty(expr) == ucode_str
  4931. expr = Sum(k**(Integral(x**n, (x, -oo, oo))), (
  4932. k, x + n + x**2 + n**2 + (x/n) + (1/x), Integral(x**x, (x, -oo, oo))))
  4933. ascii_str = \
  4934. """\
  4935. oo \n\
  4936. / \n\
  4937. | \n\
  4938. | x \n\
  4939. | x dx \n\
  4940. | \n\
  4941. / \n\
  4942. -oo \n\
  4943. ______ \n\
  4944. \\ ` \n\
  4945. \\ oo \n\
  4946. \\ / \n\
  4947. \\ | \n\
  4948. \\ | n \n\
  4949. ) | x dx\n\
  4950. / | \n\
  4951. / / \n\
  4952. / -oo \n\
  4953. / k \n\
  4954. /_____, \n\
  4955. 2 2 1 x \n\
  4956. k = n + n + x + x + - + - \n\
  4957. x n \
  4958. """
  4959. ucode_str = \
  4960. """\
  4961. ∞ \n\
  4962. ⌠ \n\
  4963. ⎮ x \n\
  4964. ⎮ x dx \n\
  4965. ⌡ \n\
  4966. -∞ \n\
  4967. ______ \n\
  4968. ╲ \n\
  4969. ╲ \n\
  4970. ╲ ∞ \n\
  4971. ╲ ⌠ \n\
  4972. ╲ ⎮ n \n\
  4973. ╱ ⎮ x dx\n\
  4974. ╱ ⌡ \n\
  4975. ╱ -∞ \n\
  4976. ╱ k \n\
  4977. ╱ \n\
  4978. ‾‾‾‾‾‾ \n\
  4979. 2 2 1 x \n\
  4980. k = n + n + x + x + ─ + ─ \n\
  4981. x n \
  4982. """
  4983. assert pretty(expr) == ascii_str
  4984. assert upretty(expr) == ucode_str
  4985. expr = Sum(k**(
  4986. Integral(x**n, (x, -oo, oo))), (k, 0, x + n + x**2 + n**2 + (x/n) + (1/x)))
  4987. ascii_str = \
  4988. """\
  4989. 2 2 1 x \n\
  4990. n + n + x + x + - + - \n\
  4991. x n \n\
  4992. ______ \n\
  4993. \\ ` \n\
  4994. \\ oo \n\
  4995. \\ / \n\
  4996. \\ | \n\
  4997. \\ | n \n\
  4998. ) | x dx\n\
  4999. / | \n\
  5000. / / \n\
  5001. / -oo \n\
  5002. / k \n\
  5003. /_____, \n\
  5004. k = 0 \
  5005. """
  5006. ucode_str = \
  5007. """\
  5008. 2 2 1 x \n\
  5009. n + n + x + x + ─ + ─ \n\
  5010. x n \n\
  5011. ______ \n\
  5012. ╲ \n\
  5013. ╲ \n\
  5014. ╲ ∞ \n\
  5015. ╲ ⌠ \n\
  5016. ╲ ⎮ n \n\
  5017. ╱ ⎮ x dx\n\
  5018. ╱ ⌡ \n\
  5019. ╱ -∞ \n\
  5020. ╱ k \n\
  5021. ╱ \n\
  5022. ‾‾‾‾‾‾ \n\
  5023. k = 0 \
  5024. """
  5025. assert pretty(expr) == ascii_str
  5026. assert upretty(expr) == ucode_str
  5027. expr = Sum(x, (x, 0, oo))
  5028. ascii_str = \
  5029. """\
  5030. oo \n\
  5031. __ \n\
  5032. \\ ` \n\
  5033. ) x\n\
  5034. /_, \n\
  5035. x = 0 \
  5036. """
  5037. ucode_str = \
  5038. """\
  5039. ∞ \n\
  5040. ___ \n\
  5041. ╲ \n\
  5042. ╲ \n\
  5043. ╱ x\n\
  5044. ╱ \n\
  5045. ‾‾‾ \n\
  5046. x = 0 \
  5047. """
  5048. assert pretty(expr) == ascii_str
  5049. assert upretty(expr) == ucode_str
  5050. expr = Sum(x**2, (x, 0, oo))
  5051. ascii_str = \
  5052. """\
  5053. oo \n\
  5054. ___ \n\
  5055. \\ ` \n\
  5056. \\ 2\n\
  5057. / x \n\
  5058. /__, \n\
  5059. x = 0 \
  5060. """
  5061. ucode_str = \
  5062. """\
  5063. ∞ \n\
  5064. ___ \n\
  5065. ╲ \n\
  5066. ╲ 2\n\
  5067. ╱ x \n\
  5068. ╱ \n\
  5069. ‾‾‾ \n\
  5070. x = 0 \
  5071. """
  5072. assert pretty(expr) == ascii_str
  5073. assert upretty(expr) == ucode_str
  5074. expr = Sum(x/2, (x, 0, oo))
  5075. ascii_str = \
  5076. """\
  5077. oo \n\
  5078. ___ \n\
  5079. \\ ` \n\
  5080. \\ x\n\
  5081. ) -\n\
  5082. / 2\n\
  5083. /__, \n\
  5084. x = 0 \
  5085. """
  5086. ucode_str = \
  5087. """\
  5088. ∞ \n\
  5089. ____ \n\
  5090. ╲ \n\
  5091. ╲ \n\
  5092. ╲ x\n\
  5093. ╱ ─\n\
  5094. ╱ 2\n\
  5095. ╱ \n\
  5096. ‾‾‾‾ \n\
  5097. x = 0 \
  5098. """
  5099. assert pretty(expr) == ascii_str
  5100. assert upretty(expr) == ucode_str
  5101. expr = Sum(x**3/2, (x, 0, oo))
  5102. ascii_str = \
  5103. """\
  5104. oo \n\
  5105. ____ \n\
  5106. \\ ` \n\
  5107. \\ 3\n\
  5108. \\ x \n\
  5109. / --\n\
  5110. / 2 \n\
  5111. /___, \n\
  5112. x = 0 \
  5113. """
  5114. ucode_str = \
  5115. """\
  5116. ∞ \n\
  5117. ____ \n\
  5118. ╲ \n\
  5119. ╲ 3\n\
  5120. ╲ x \n\
  5121. ╱ ──\n\
  5122. ╱ 2 \n\
  5123. ╱ \n\
  5124. ‾‾‾‾ \n\
  5125. x = 0 \
  5126. """
  5127. assert pretty(expr) == ascii_str
  5128. assert upretty(expr) == ucode_str
  5129. expr = Sum((x**3*y**(x/2))**n, (x, 0, oo))
  5130. ascii_str = \
  5131. """\
  5132. oo \n\
  5133. ____ \n\
  5134. \\ ` \n\
  5135. \\ n\n\
  5136. \\ / x\\ \n\
  5137. ) | -| \n\
  5138. / | 3 2| \n\
  5139. / \\x *y / \n\
  5140. /___, \n\
  5141. x = 0 \
  5142. """
  5143. ucode_str = \
  5144. """\
  5145. ∞ \n\
  5146. _____ \n\
  5147. ╲ \n\
  5148. ╲ \n\
  5149. ╲ n\n\
  5150. ╲ ⎛ x⎞ \n\
  5151. ╱ ⎜ ─⎟ \n\
  5152. ╱ ⎜ 3 2⎟ \n\
  5153. ╱ ⎝x ⋅y ⎠ \n\
  5154. ╱ \n\
  5155. ‾‾‾‾‾ \n\
  5156. x = 0 \
  5157. """
  5158. assert pretty(expr) == ascii_str
  5159. assert upretty(expr) == ucode_str
  5160. expr = Sum(1/x**2, (x, 0, oo))
  5161. ascii_str = \
  5162. """\
  5163. oo \n\
  5164. ____ \n\
  5165. \\ ` \n\
  5166. \\ 1 \n\
  5167. \\ --\n\
  5168. / 2\n\
  5169. / x \n\
  5170. /___, \n\
  5171. x = 0 \
  5172. """
  5173. ucode_str = \
  5174. """\
  5175. ∞ \n\
  5176. ____ \n\
  5177. ╲ \n\
  5178. ╲ 1 \n\
  5179. ╲ ──\n\
  5180. ╱ 2\n\
  5181. ╱ x \n\
  5182. ╱ \n\
  5183. ‾‾‾‾ \n\
  5184. x = 0 \
  5185. """
  5186. assert pretty(expr) == ascii_str
  5187. assert upretty(expr) == ucode_str
  5188. expr = Sum(1/y**(a/b), (x, 0, oo))
  5189. ascii_str = \
  5190. """\
  5191. oo \n\
  5192. ____ \n\
  5193. \\ ` \n\
  5194. \\ -a \n\
  5195. \\ ---\n\
  5196. / b \n\
  5197. / y \n\
  5198. /___, \n\
  5199. x = 0 \
  5200. """
  5201. ucode_str = \
  5202. """\
  5203. ∞ \n\
  5204. ____ \n\
  5205. ╲ \n\
  5206. ╲ -a \n\
  5207. ╲ ───\n\
  5208. ╱ b \n\
  5209. ╱ y \n\
  5210. ╱ \n\
  5211. ‾‾‾‾ \n\
  5212. x = 0 \
  5213. """
  5214. assert pretty(expr) == ascii_str
  5215. assert upretty(expr) == ucode_str
  5216. expr = Sum(1/y**(a/b), (x, 0, oo), (y, 1, 2))
  5217. ascii_str = \
  5218. """\
  5219. 2 oo \n\
  5220. ____ ____ \n\
  5221. \\ ` \\ ` \n\
  5222. \\ \\ -a\n\
  5223. \\ \\ --\n\
  5224. / / b \n\
  5225. / / y \n\
  5226. /___, /___, \n\
  5227. y = 1 x = 0 \
  5228. """
  5229. ucode_str = \
  5230. """\
  5231. 2 ∞ \n\
  5232. ____ ____ \n\
  5233. ╲ ╲ \n\
  5234. ╲ ╲ -a\n\
  5235. ╲ ╲ ──\n\
  5236. ╱ ╱ b \n\
  5237. ╱ ╱ y \n\
  5238. ╱ ╱ \n\
  5239. ‾‾‾‾ ‾‾‾‾ \n\
  5240. y = 1 x = 0 \
  5241. """
  5242. expr = Sum(1/(1 + 1/(
  5243. 1 + 1/k)) + 1, (k, 111, 1 + 1/n), (k, 1/(1 + m), oo)) + 1/(1 + 1/k)
  5244. ascii_str = \
  5245. """\
  5246. 1 \n\
  5247. 1 + - \n\
  5248. oo n \n\
  5249. _____ _____ \n\
  5250. \\ ` \\ ` \n\
  5251. \\ \\ / 1 \\ \n\
  5252. \\ \\ |1 + ---------| \n\
  5253. \\ \\ | 1 | 1 \n\
  5254. ) ) | 1 + -----| + -----\n\
  5255. / / | 1| 1\n\
  5256. / / | 1 + -| 1 + -\n\
  5257. / / \\ k/ k\n\
  5258. /____, /____, \n\
  5259. 1 k = 111 \n\
  5260. k = ----- \n\
  5261. m + 1 \
  5262. """
  5263. ucode_str = \
  5264. """\
  5265. 1 \n\
  5266. 1 + ─ \n\
  5267. ∞ n \n\
  5268. ______ ______ \n\
  5269. ╲ ╲ \n\
  5270. ╲ ╲ \n\
  5271. ╲ ╲ ⎛ 1 ⎞ \n\
  5272. ╲ ╲ ⎜1 + ─────────⎟ \n\
  5273. ╲ ╲ ⎜ 1 ⎟ 1 \n\
  5274. ╱ ╱ ⎜ 1 + ─────⎟ + ─────\n\
  5275. ╱ ╱ ⎜ 1⎟ 1\n\
  5276. ╱ ╱ ⎜ 1 + ─⎟ 1 + ─\n\
  5277. ╱ ╱ ⎝ k⎠ k\n\
  5278. ╱ ╱ \n\
  5279. ‾‾‾‾‾‾ ‾‾‾‾‾‾ \n\
  5280. 1 k = 111 \n\
  5281. k = ───── \n\
  5282. m + 1 \
  5283. """
  5284. assert pretty(expr) == ascii_str
  5285. assert upretty(expr) == ucode_str
  5286. def test_units():
  5287. expr = joule
  5288. ascii_str1 = \
  5289. """\
  5290. 2\n\
  5291. kilogram*meter \n\
  5292. ---------------\n\
  5293. 2 \n\
  5294. second \
  5295. """
  5296. unicode_str1 = \
  5297. """\
  5298. 2\n\
  5299. kilogram⋅meter \n\
  5300. ───────────────\n\
  5301. 2 \n\
  5302. second \
  5303. """
  5304. ascii_str2 = \
  5305. """\
  5306. 2\n\
  5307. 3*x*y*kilogram*meter \n\
  5308. ---------------------\n\
  5309. 2 \n\
  5310. second \
  5311. """
  5312. unicode_str2 = \
  5313. """\
  5314. 2\n\
  5315. 3⋅x⋅y⋅kilogram⋅meter \n\
  5316. ─────────────────────\n\
  5317. 2 \n\
  5318. second \
  5319. """
  5320. from sympy.physics.units import kg, m, s
  5321. assert upretty(expr) == "joule"
  5322. assert pretty(expr) == "joule"
  5323. assert upretty(expr.convert_to(kg*m**2/s**2)) == unicode_str1
  5324. assert pretty(expr.convert_to(kg*m**2/s**2)) == ascii_str1
  5325. assert upretty(3*kg*x*m**2*y/s**2) == unicode_str2
  5326. assert pretty(3*kg*x*m**2*y/s**2) == ascii_str2
  5327. def test_pretty_Subs():
  5328. f = Function('f')
  5329. expr = Subs(f(x), x, ph**2)
  5330. ascii_str = \
  5331. """\
  5332. (f(x))| 2\n\
  5333. |x=phi \
  5334. """
  5335. unicode_str = \
  5336. """\
  5337. (f(x))│ 2\n\
  5338. │x=φ \
  5339. """
  5340. assert pretty(expr) == ascii_str
  5341. assert upretty(expr) == unicode_str
  5342. expr = Subs(f(x).diff(x), x, 0)
  5343. ascii_str = \
  5344. """\
  5345. /d \\| \n\
  5346. |--(f(x))|| \n\
  5347. \\dx /|x=0\
  5348. """
  5349. unicode_str = \
  5350. """\
  5351. ⎛d ⎞│ \n\
  5352. ⎜──(f(x))⎟│ \n\
  5353. ⎝dx ⎠│x=0\
  5354. """
  5355. assert pretty(expr) == ascii_str
  5356. assert upretty(expr) == unicode_str
  5357. expr = Subs(f(x).diff(x)/y, (x, y), (0, Rational(1, 2)))
  5358. ascii_str = \
  5359. """\
  5360. /d \\| \n\
  5361. |--(f(x))|| \n\
  5362. |dx || \n\
  5363. |--------|| \n\
  5364. \\ y /|x=0, y=1/2\
  5365. """
  5366. unicode_str = \
  5367. """\
  5368. ⎛d ⎞│ \n\
  5369. ⎜──(f(x))⎟│ \n\
  5370. ⎜dx ⎟│ \n\
  5371. ⎜────────⎟│ \n\
  5372. ⎝ y ⎠│x=0, y=1/2\
  5373. """
  5374. assert pretty(expr) == ascii_str
  5375. assert upretty(expr) == unicode_str
  5376. def test_gammas():
  5377. assert upretty(lowergamma(x, y)) == "γ(x, y)"
  5378. assert upretty(uppergamma(x, y)) == "Γ(x, y)"
  5379. assert xpretty(gamma(x), use_unicode=True) == 'Γ(x)'
  5380. assert xpretty(gamma, use_unicode=True) == 'Γ'
  5381. assert xpretty(symbols('gamma', cls=Function)(x), use_unicode=True) == 'γ(x)'
  5382. assert xpretty(symbols('gamma', cls=Function), use_unicode=True) == 'γ'
  5383. def test_beta():
  5384. assert xpretty(beta(x,y), use_unicode=True) == 'Β(x, y)'
  5385. assert xpretty(beta(x,y), use_unicode=False) == 'B(x, y)'
  5386. assert xpretty(beta, use_unicode=True) == 'Β'
  5387. assert xpretty(beta, use_unicode=False) == 'B'
  5388. mybeta = Function('beta')
  5389. assert xpretty(mybeta(x), use_unicode=True) == 'β(x)'
  5390. assert xpretty(mybeta(x, y, z), use_unicode=False) == 'beta(x, y, z)'
  5391. assert xpretty(mybeta, use_unicode=True) == 'β'
  5392. # test that notation passes to subclasses of the same name only
  5393. def test_function_subclass_different_name():
  5394. class mygamma(gamma):
  5395. pass
  5396. assert xpretty(mygamma, use_unicode=True) == r"mygamma"
  5397. assert xpretty(mygamma(x), use_unicode=True) == r"mygamma(x)"
  5398. def test_SingularityFunction():
  5399. assert xpretty(SingularityFunction(x, 0, n), use_unicode=True) == (
  5400. """\
  5401. n\n\
  5402. <x> \
  5403. """)
  5404. assert xpretty(SingularityFunction(x, 1, n), use_unicode=True) == (
  5405. """\
  5406. n\n\
  5407. <x - 1> \
  5408. """)
  5409. assert xpretty(SingularityFunction(x, -1, n), use_unicode=True) == (
  5410. """\
  5411. n\n\
  5412. <x + 1> \
  5413. """)
  5414. assert xpretty(SingularityFunction(x, a, n), use_unicode=True) == (
  5415. """\
  5416. n\n\
  5417. <-a + x> \
  5418. """)
  5419. assert xpretty(SingularityFunction(x, y, n), use_unicode=True) == (
  5420. """\
  5421. n\n\
  5422. <x - y> \
  5423. """)
  5424. assert xpretty(SingularityFunction(x, 0, n), use_unicode=False) == (
  5425. """\
  5426. n\n\
  5427. <x> \
  5428. """)
  5429. assert xpretty(SingularityFunction(x, 1, n), use_unicode=False) == (
  5430. """\
  5431. n\n\
  5432. <x - 1> \
  5433. """)
  5434. assert xpretty(SingularityFunction(x, -1, n), use_unicode=False) == (
  5435. """\
  5436. n\n\
  5437. <x + 1> \
  5438. """)
  5439. assert xpretty(SingularityFunction(x, a, n), use_unicode=False) == (
  5440. """\
  5441. n\n\
  5442. <-a + x> \
  5443. """)
  5444. assert xpretty(SingularityFunction(x, y, n), use_unicode=False) == (
  5445. """\
  5446. n\n\
  5447. <x - y> \
  5448. """)
  5449. def test_deltas():
  5450. assert xpretty(DiracDelta(x), use_unicode=True) == 'δ(x)'
  5451. assert xpretty(DiracDelta(x, 1), use_unicode=True) == \
  5452. """\
  5453. (1) \n\
  5454. δ (x)\
  5455. """
  5456. assert xpretty(x*DiracDelta(x, 1), use_unicode=True) == \
  5457. """\
  5458. (1) \n\
  5459. x⋅δ (x)\
  5460. """
  5461. def test_hyper():
  5462. expr = hyper((), (), z)
  5463. ucode_str = \
  5464. """\
  5465. ┌─ ⎛ │ ⎞\n\
  5466. ├─ ⎜ │ z⎟\n\
  5467. 0╵ 0 ⎝ │ ⎠\
  5468. """
  5469. ascii_str = \
  5470. """\
  5471. _ \n\
  5472. |_ / | \\\n\
  5473. | | | z|\n\
  5474. 0 0 \\ | /\
  5475. """
  5476. assert pretty(expr) == ascii_str
  5477. assert upretty(expr) == ucode_str
  5478. expr = hyper((), (1,), x)
  5479. ucode_str = \
  5480. """\
  5481. ┌─ ⎛ │ ⎞\n\
  5482. ├─ ⎜ │ x⎟\n\
  5483. 0╵ 1 ⎝1 │ ⎠\
  5484. """
  5485. ascii_str = \
  5486. """\
  5487. _ \n\
  5488. |_ / | \\\n\
  5489. | | | x|\n\
  5490. 0 1 \\1 | /\
  5491. """
  5492. assert pretty(expr) == ascii_str
  5493. assert upretty(expr) == ucode_str
  5494. expr = hyper([2], [1], x)
  5495. ucode_str = \
  5496. """\
  5497. ┌─ ⎛2 │ ⎞\n\
  5498. ├─ ⎜ │ x⎟\n\
  5499. 1╵ 1 ⎝1 │ ⎠\
  5500. """
  5501. ascii_str = \
  5502. """\
  5503. _ \n\
  5504. |_ /2 | \\\n\
  5505. | | | x|\n\
  5506. 1 1 \\1 | /\
  5507. """
  5508. assert pretty(expr) == ascii_str
  5509. assert upretty(expr) == ucode_str
  5510. expr = hyper((pi/3, -2*k), (3, 4, 5, -3), x)
  5511. ucode_str = \
  5512. """\
  5513. ⎛ π │ ⎞\n\
  5514. ┌─ ⎜ ─, -2⋅k │ ⎟\n\
  5515. ├─ ⎜ 3 │ x⎟\n\
  5516. 2╵ 4 ⎜ │ ⎟\n\
  5517. ⎝-3, 3, 4, 5 │ ⎠\
  5518. """
  5519. ascii_str = \
  5520. """\
  5521. \n\
  5522. _ / pi | \\\n\
  5523. |_ | --, -2*k | |\n\
  5524. | | 3 | x|\n\
  5525. 2 4 | | |\n\
  5526. \\-3, 3, 4, 5 | /\
  5527. """
  5528. assert pretty(expr) == ascii_str
  5529. assert upretty(expr) == ucode_str
  5530. expr = hyper((pi, S('2/3'), -2*k), (3, 4, 5, -3), x**2)
  5531. ucode_str = \
  5532. """\
  5533. ┌─ ⎛2/3, π, -2⋅k │ 2⎞\n\
  5534. ├─ ⎜ │ x ⎟\n\
  5535. 3╵ 4 ⎝-3, 3, 4, 5 │ ⎠\
  5536. """
  5537. ascii_str = \
  5538. """\
  5539. _ \n\
  5540. |_ /2/3, pi, -2*k | 2\\
  5541. | | | x |
  5542. 3 4 \\ -3, 3, 4, 5 | /"""
  5543. assert pretty(expr) == ascii_str
  5544. assert upretty(expr) == ucode_str
  5545. expr = hyper([1, 2], [3, 4], 1/(1/(1/(1/x + 1) + 1) + 1))
  5546. ucode_str = \
  5547. """\
  5548. ⎛ │ 1 ⎞\n\
  5549. ⎜ │ ─────────────⎟\n\
  5550. ⎜ │ 1 ⎟\n\
  5551. ┌─ ⎜1, 2 │ 1 + ─────────⎟\n\
  5552. ├─ ⎜ │ 1 ⎟\n\
  5553. 2╵ 2 ⎜3, 4 │ 1 + ─────⎟\n\
  5554. ⎜ │ 1⎟\n\
  5555. ⎜ │ 1 + ─⎟\n\
  5556. ⎝ │ x⎠\
  5557. """
  5558. ascii_str = \
  5559. """\
  5560. \n\
  5561. / | 1 \\\n\
  5562. | | -------------|\n\
  5563. _ | | 1 |\n\
  5564. |_ |1, 2 | 1 + ---------|\n\
  5565. | | | 1 |\n\
  5566. 2 2 |3, 4 | 1 + -----|\n\
  5567. | | 1|\n\
  5568. | | 1 + -|\n\
  5569. \\ | x/\
  5570. """
  5571. assert pretty(expr) == ascii_str
  5572. assert upretty(expr) == ucode_str
  5573. def test_meijerg():
  5574. expr = meijerg([pi, pi, x], [1], [0, 1], [1, 2, 3], z)
  5575. ucode_str = \
  5576. """\
  5577. ╭─╮2, 3 ⎛π, π, x 1 │ ⎞\n\
  5578. │╶┐ ⎜ │ z⎟\n\
  5579. ╰─╯4, 5 ⎝ 0, 1 1, 2, 3 │ ⎠\
  5580. """
  5581. ascii_str = \
  5582. """\
  5583. __2, 3 /pi, pi, x 1 | \\\n\
  5584. /__ | | z|\n\
  5585. \\_|4, 5 \\ 0, 1 1, 2, 3 | /\
  5586. """
  5587. assert pretty(expr) == ascii_str
  5588. assert upretty(expr) == ucode_str
  5589. expr = meijerg([1, pi/7], [2, pi, 5], [], [], z**2)
  5590. ucode_str = \
  5591. """\
  5592. ⎛ π │ ⎞\n\
  5593. ╭─╮0, 2 ⎜1, ─ 2, 5, π │ 2⎟\n\
  5594. │╶┐ ⎜ 7 │ z ⎟\n\
  5595. ╰─╯5, 0 ⎜ │ ⎟\n\
  5596. ⎝ │ ⎠\
  5597. """
  5598. ascii_str = \
  5599. """\
  5600. / pi | \\\n\
  5601. __0, 2 |1, -- 2, 5, pi | 2|\n\
  5602. /__ | 7 | z |\n\
  5603. \\_|5, 0 | | |\n\
  5604. \\ | /\
  5605. """
  5606. assert pretty(expr) == ascii_str
  5607. assert upretty(expr) == ucode_str
  5608. ucode_str = \
  5609. """\
  5610. ╭─╮ 1, 10 ⎛1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1 │ ⎞\n\
  5611. │╶┐ ⎜ │ z⎟\n\
  5612. ╰─╯11, 2 ⎝ 1 1 │ ⎠\
  5613. """
  5614. ascii_str = \
  5615. """\
  5616. __ 1, 10 /1, 1, 1, 1, 1, 1, 1, 1, 1, 1 1 | \\\n\
  5617. /__ | | z|\n\
  5618. \\_|11, 2 \\ 1 1 | /\
  5619. """
  5620. expr = meijerg([1]*10, [1], [1], [1], z)
  5621. assert pretty(expr) == ascii_str
  5622. assert upretty(expr) == ucode_str
  5623. expr = meijerg([1, 2, ], [4, 3], [3], [4, 5], 1/(1/(1/(1/x + 1) + 1) + 1))
  5624. ucode_str = \
  5625. """\
  5626. ⎛ │ 1 ⎞\n\
  5627. ⎜ │ ─────────────⎟\n\
  5628. ⎜ │ 1 ⎟\n\
  5629. ╭─╮1, 2 ⎜1, 2 3, 4 │ 1 + ─────────⎟\n\
  5630. │╶┐ ⎜ │ 1 ⎟\n\
  5631. ╰─╯4, 3 ⎜ 3 4, 5 │ 1 + ─────⎟\n\
  5632. ⎜ │ 1⎟\n\
  5633. ⎜ │ 1 + ─⎟\n\
  5634. ⎝ │ x⎠\
  5635. """
  5636. ascii_str = \
  5637. """\
  5638. / | 1 \\\n\
  5639. | | -------------|\n\
  5640. | | 1 |\n\
  5641. __1, 2 |1, 2 3, 4 | 1 + ---------|\n\
  5642. /__ | | 1 |\n\
  5643. \\_|4, 3 | 3 4, 5 | 1 + -----|\n\
  5644. | | 1|\n\
  5645. | | 1 + -|\n\
  5646. \\ | x/\
  5647. """
  5648. assert pretty(expr) == ascii_str
  5649. assert upretty(expr) == ucode_str
  5650. expr = Integral(expr, x)
  5651. ucode_str = \
  5652. """\
  5653. ⌠ \n\
  5654. ⎮ ⎛ │ 1 ⎞ \n\
  5655. ⎮ ⎜ │ ─────────────⎟ \n\
  5656. ⎮ ⎜ │ 1 ⎟ \n\
  5657. ⎮ ╭─╮1, 2 ⎜1, 2 3, 4 │ 1 + ─────────⎟ \n\
  5658. ⎮ │╶┐ ⎜ │ 1 ⎟ dx\n\
  5659. ⎮ ╰─╯4, 3 ⎜ 3 4, 5 │ 1 + ─────⎟ \n\
  5660. ⎮ ⎜ │ 1⎟ \n\
  5661. ⎮ ⎜ │ 1 + ─⎟ \n\
  5662. ⎮ ⎝ │ x⎠ \n\
  5663. ⌡ \
  5664. """
  5665. ascii_str = \
  5666. """\
  5667. / \n\
  5668. | \n\
  5669. | / | 1 \\ \n\
  5670. | | | -------------| \n\
  5671. | | | 1 | \n\
  5672. | __1, 2 |1, 2 3, 4 | 1 + ---------| \n\
  5673. | /__ | | 1 | dx\n\
  5674. | \\_|4, 3 | 3 4, 5 | 1 + -----| \n\
  5675. | | | 1| \n\
  5676. | | | 1 + -| \n\
  5677. | \\ | x/ \n\
  5678. | \n\
  5679. / \
  5680. """
  5681. assert pretty(expr) == ascii_str
  5682. assert upretty(expr) == ucode_str
  5683. def test_noncommutative():
  5684. A, B, C = symbols('A,B,C', commutative=False)
  5685. expr = A*B*C**-1
  5686. ascii_str = \
  5687. """\
  5688. -1\n\
  5689. A*B*C \
  5690. """
  5691. ucode_str = \
  5692. """\
  5693. -1\n\
  5694. A⋅B⋅C \
  5695. """
  5696. assert pretty(expr) == ascii_str
  5697. assert upretty(expr) == ucode_str
  5698. expr = C**-1*A*B
  5699. ascii_str = \
  5700. """\
  5701. -1 \n\
  5702. C *A*B\
  5703. """
  5704. ucode_str = \
  5705. """\
  5706. -1 \n\
  5707. C ⋅A⋅B\
  5708. """
  5709. assert pretty(expr) == ascii_str
  5710. assert upretty(expr) == ucode_str
  5711. expr = A*C**-1*B
  5712. ascii_str = \
  5713. """\
  5714. -1 \n\
  5715. A*C *B\
  5716. """
  5717. ucode_str = \
  5718. """\
  5719. -1 \n\
  5720. A⋅C ⋅B\
  5721. """
  5722. assert pretty(expr) == ascii_str
  5723. assert upretty(expr) == ucode_str
  5724. expr = A*C**-1*B/x
  5725. ascii_str = \
  5726. """\
  5727. -1 \n\
  5728. A*C *B\n\
  5729. -------\n\
  5730. x \
  5731. """
  5732. ucode_str = \
  5733. """\
  5734. -1 \n\
  5735. A⋅C ⋅B\n\
  5736. ───────\n\
  5737. x \
  5738. """
  5739. assert pretty(expr) == ascii_str
  5740. assert upretty(expr) == ucode_str
  5741. def test_pretty_special_functions():
  5742. x, y = symbols("x y")
  5743. # atan2
  5744. expr = atan2(y/sqrt(200), sqrt(x))
  5745. ascii_str = \
  5746. """\
  5747. / ___ \\\n\
  5748. |\\/ 2 *y ___|\n\
  5749. atan2|-------, \\/ x |\n\
  5750. \\ 20 /\
  5751. """
  5752. ucode_str = \
  5753. """\
  5754. ⎛√2⋅y ⎞\n\
  5755. atan2⎜────, √x⎟\n\
  5756. ⎝ 20 ⎠\
  5757. """
  5758. assert pretty(expr) == ascii_str
  5759. assert upretty(expr) == ucode_str
  5760. def test_pretty_geometry():
  5761. e = Segment((0, 1), (0, 2))
  5762. assert pretty(e) == 'Segment2D(Point2D(0, 1), Point2D(0, 2))'
  5763. e = Ray((1, 1), angle=4.02*pi)
  5764. assert pretty(e) == 'Ray2D(Point2D(1, 1), Point2D(2, tan(pi/50) + 1))'
  5765. def test_expint():
  5766. expr = Ei(x)
  5767. string = 'Ei(x)'
  5768. assert pretty(expr) == string
  5769. assert upretty(expr) == string
  5770. expr = expint(1, z)
  5771. ucode_str = "E₁(z)"
  5772. ascii_str = "expint(1, z)"
  5773. assert pretty(expr) == ascii_str
  5774. assert upretty(expr) == ucode_str
  5775. assert pretty(Shi(x)) == 'Shi(x)'
  5776. assert pretty(Si(x)) == 'Si(x)'
  5777. assert pretty(Ci(x)) == 'Ci(x)'
  5778. assert pretty(Chi(x)) == 'Chi(x)'
  5779. assert upretty(Shi(x)) == 'Shi(x)'
  5780. assert upretty(Si(x)) == 'Si(x)'
  5781. assert upretty(Ci(x)) == 'Ci(x)'
  5782. assert upretty(Chi(x)) == 'Chi(x)'
  5783. def test_elliptic_functions():
  5784. ascii_str = \
  5785. """\
  5786. / 1 \\\n\
  5787. K|-----|\n\
  5788. \\z + 1/\
  5789. """
  5790. ucode_str = \
  5791. """\
  5792. ⎛ 1 ⎞\n\
  5793. K⎜─────⎟\n\
  5794. ⎝z + 1⎠\
  5795. """
  5796. expr = elliptic_k(1/(z + 1))
  5797. assert pretty(expr) == ascii_str
  5798. assert upretty(expr) == ucode_str
  5799. ascii_str = \
  5800. """\
  5801. / | 1 \\\n\
  5802. F|1|-----|\n\
  5803. \\ |z + 1/\
  5804. """
  5805. ucode_str = \
  5806. """\
  5807. ⎛ │ 1 ⎞\n\
  5808. F⎜1│─────⎟\n\
  5809. ⎝ │z + 1⎠\
  5810. """
  5811. expr = elliptic_f(1, 1/(1 + z))
  5812. assert pretty(expr) == ascii_str
  5813. assert upretty(expr) == ucode_str
  5814. ascii_str = \
  5815. """\
  5816. / 1 \\\n\
  5817. E|-----|\n\
  5818. \\z + 1/\
  5819. """
  5820. ucode_str = \
  5821. """\
  5822. ⎛ 1 ⎞\n\
  5823. E⎜─────⎟\n\
  5824. ⎝z + 1⎠\
  5825. """
  5826. expr = elliptic_e(1/(z + 1))
  5827. assert pretty(expr) == ascii_str
  5828. assert upretty(expr) == ucode_str
  5829. ascii_str = \
  5830. """\
  5831. / | 1 \\\n\
  5832. E|1|-----|\n\
  5833. \\ |z + 1/\
  5834. """
  5835. ucode_str = \
  5836. """\
  5837. ⎛ │ 1 ⎞\n\
  5838. E⎜1│─────⎟\n\
  5839. ⎝ │z + 1⎠\
  5840. """
  5841. expr = elliptic_e(1, 1/(1 + z))
  5842. assert pretty(expr) == ascii_str
  5843. assert upretty(expr) == ucode_str
  5844. ascii_str = \
  5845. """\
  5846. / |4\\\n\
  5847. Pi|3|-|\n\
  5848. \\ |x/\
  5849. """
  5850. ucode_str = \
  5851. """\
  5852. ⎛ │4⎞\n\
  5853. Π⎜3│─⎟\n\
  5854. ⎝ │x⎠\
  5855. """
  5856. expr = elliptic_pi(3, 4/x)
  5857. assert pretty(expr) == ascii_str
  5858. assert upretty(expr) == ucode_str
  5859. ascii_str = \
  5860. """\
  5861. / 4| \\\n\
  5862. Pi|3; -|6|\n\
  5863. \\ x| /\
  5864. """
  5865. ucode_str = \
  5866. """\
  5867. ⎛ 4│ ⎞\n\
  5868. Π⎜3; ─│6⎟\n\
  5869. ⎝ x│ ⎠\
  5870. """
  5871. expr = elliptic_pi(3, 4/x, 6)
  5872. assert pretty(expr) == ascii_str
  5873. assert upretty(expr) == ucode_str
  5874. def test_RandomDomain():
  5875. from sympy.stats import Normal, Die, Exponential, pspace, where
  5876. X = Normal('x1', 0, 1)
  5877. assert upretty(where(X > 0)) == "Domain: 0 < x₁ ∧ x₁ < ∞"
  5878. D = Die('d1', 6)
  5879. assert upretty(where(D > 4)) == 'Domain: d₁ = 5 ∨ d₁ = 6'
  5880. A = Exponential('a', 1)
  5881. B = Exponential('b', 1)
  5882. assert upretty(pspace(Tuple(A, B)).domain) == \
  5883. 'Domain: 0 ≤ a ∧ 0 ≤ b ∧ a < ∞ ∧ b < ∞'
  5884. def test_PrettyPoly():
  5885. F = QQ.frac_field(x, y)
  5886. R = QQ.poly_ring(x, y)
  5887. expr = F.convert(x/(x + y))
  5888. assert pretty(expr) == "x/(x + y)"
  5889. assert upretty(expr) == "x/(x + y)"
  5890. expr = R.convert(x + y)
  5891. assert pretty(expr) == "x + y"
  5892. assert upretty(expr) == "x + y"
  5893. def test_issue_6285():
  5894. assert pretty(Pow(2, -5, evaluate=False)) == '1 \n--\n 5\n2 '
  5895. assert pretty(Pow(x, (1/pi))) == \
  5896. ' 1 \n'\
  5897. ' --\n'\
  5898. ' pi\n'\
  5899. 'x '
  5900. def test_issue_6359():
  5901. assert pretty(Integral(x**2, x)**2) == \
  5902. """\
  5903. 2
  5904. / / \\ \n\
  5905. | | | \n\
  5906. | | 2 | \n\
  5907. | | x dx| \n\
  5908. | | | \n\
  5909. \\/ / \
  5910. """
  5911. assert upretty(Integral(x**2, x)**2) == \
  5912. """\
  5913. 2
  5914. ⎛⌠ ⎞ \n\
  5915. ⎜⎮ 2 ⎟ \n\
  5916. ⎜⎮ x dx⎟ \n\
  5917. ⎝⌡ ⎠ \
  5918. """
  5919. assert pretty(Sum(x**2, (x, 0, 1))**2) == \
  5920. """\
  5921. 2\n\
  5922. / 1 \\ \n\
  5923. |___ | \n\
  5924. |\\ ` | \n\
  5925. | \\ 2| \n\
  5926. | / x | \n\
  5927. |/__, | \n\
  5928. \\x = 0 / \
  5929. """
  5930. assert upretty(Sum(x**2, (x, 0, 1))**2) == \
  5931. """\
  5932. 2
  5933. ⎛ 1 ⎞ \n\
  5934. ⎜ ___ ⎟ \n\
  5935. ⎜ ╲ ⎟ \n\
  5936. ⎜ ╲ 2⎟ \n\
  5937. ⎜ ╱ x ⎟ \n\
  5938. ⎜ ╱ ⎟ \n\
  5939. ⎜ ‾‾‾ ⎟ \n\
  5940. ⎝x = 0 ⎠ \
  5941. """
  5942. assert pretty(Product(x**2, (x, 1, 2))**2) == \
  5943. """\
  5944. 2
  5945. / 2 \\ \n\
  5946. |______ | \n\
  5947. | | | 2| \n\
  5948. | | | x | \n\
  5949. | | | | \n\
  5950. \\x = 1 / \
  5951. """
  5952. assert upretty(Product(x**2, (x, 1, 2))**2) == \
  5953. """\
  5954. 2
  5955. ⎛ 2 ⎞ \n\
  5956. ⎜─┬──┬─ ⎟ \n\
  5957. ⎜ │ │ 2⎟ \n\
  5958. ⎜ │ │ x ⎟ \n\
  5959. ⎜ │ │ ⎟ \n\
  5960. ⎝x = 1 ⎠ \
  5961. """
  5962. f = Function('f')
  5963. assert pretty(Derivative(f(x), x)**2) == \
  5964. """\
  5965. 2
  5966. /d \\ \n\
  5967. |--(f(x))| \n\
  5968. \\dx / \
  5969. """
  5970. assert upretty(Derivative(f(x), x)**2) == \
  5971. """\
  5972. 2
  5973. ⎛d ⎞ \n\
  5974. ⎜──(f(x))⎟ \n\
  5975. ⎝dx ⎠ \
  5976. """
  5977. def test_issue_6739():
  5978. ascii_str = \
  5979. """\
  5980. 1 \n\
  5981. -----\n\
  5982. ___\n\
  5983. \\/ x \
  5984. """
  5985. ucode_str = \
  5986. """\
  5987. 1 \n\
  5988. ──\n\
  5989. √x\
  5990. """
  5991. assert pretty(1/sqrt(x)) == ascii_str
  5992. assert upretty(1/sqrt(x)) == ucode_str
  5993. def test_complicated_symbol_unchanged():
  5994. for symb_name in ["dexpr2_d1tau", "dexpr2^d1tau"]:
  5995. assert pretty(Symbol(symb_name)) == symb_name
  5996. def test_categories():
  5997. from sympy.categories import (Object, IdentityMorphism,
  5998. NamedMorphism, Category, Diagram, DiagramGrid)
  5999. A1 = Object("A1")
  6000. A2 = Object("A2")
  6001. A3 = Object("A3")
  6002. f1 = NamedMorphism(A1, A2, "f1")
  6003. f2 = NamedMorphism(A2, A3, "f2")
  6004. id_A1 = IdentityMorphism(A1)
  6005. K1 = Category("K1")
  6006. assert pretty(A1) == "A1"
  6007. assert upretty(A1) == "A₁"
  6008. assert pretty(f1) == "f1:A1-->A2"
  6009. assert upretty(f1) == "f₁:A₁——▶A₂"
  6010. assert pretty(id_A1) == "id:A1-->A1"
  6011. assert upretty(id_A1) == "id:A₁——▶A₁"
  6012. assert pretty(f2*f1) == "f2*f1:A1-->A3"
  6013. assert upretty(f2*f1) == "f₂∘f₁:A₁——▶A₃"
  6014. assert pretty(K1) == "K1"
  6015. assert upretty(K1) == "K₁"
  6016. # Test how diagrams are printed.
  6017. d = Diagram()
  6018. assert pretty(d) == "EmptySet"
  6019. assert upretty(d) == "∅"
  6020. d = Diagram({f1: "unique", f2: S.EmptySet})
  6021. assert pretty(d) == "{f2*f1:A1-->A3: EmptySet, id:A1-->A1: " \
  6022. "EmptySet, id:A2-->A2: EmptySet, id:A3-->A3: " \
  6023. "EmptySet, f1:A1-->A2: {unique}, f2:A2-->A3: EmptySet}"
  6024. assert upretty(d) == "{f₂∘f₁:A₁——▶A₃: ∅, id:A₁——▶A₁: ∅, " \
  6025. "id:A₂——▶A₂: ∅, id:A₃——▶A₃: ∅, f₁:A₁——▶A₂: {unique}, f₂:A₂——▶A₃: ∅}"
  6026. d = Diagram({f1: "unique", f2: S.EmptySet}, {f2 * f1: "unique"})
  6027. assert pretty(d) == "{f2*f1:A1-->A3: EmptySet, id:A1-->A1: " \
  6028. "EmptySet, id:A2-->A2: EmptySet, id:A3-->A3: " \
  6029. "EmptySet, f1:A1-->A2: {unique}, f2:A2-->A3: EmptySet}" \
  6030. " ==> {f2*f1:A1-->A3: {unique}}"
  6031. assert upretty(d) == "{f₂∘f₁:A₁——▶A₃: ∅, id:A₁——▶A₁: ∅, id:A₂——▶A₂: " \
  6032. "∅, id:A₃——▶A₃: ∅, f₁:A₁——▶A₂: {unique}, f₂:A₂——▶A₃: ∅}" \
  6033. " ══▶ {f₂∘f₁:A₁——▶A₃: {unique}}"
  6034. grid = DiagramGrid(d)
  6035. assert pretty(grid) == "A1 A2\n \nA3 "
  6036. assert upretty(grid) == "A₁ A₂\n \nA₃ "
  6037. def test_PrettyModules():
  6038. R = QQ.old_poly_ring(x, y)
  6039. F = R.free_module(2)
  6040. M = F.submodule([x, y], [1, x**2])
  6041. ucode_str = \
  6042. """\
  6043. 2\n\
  6044. ℚ[x, y] \
  6045. """
  6046. ascii_str = \
  6047. """\
  6048. 2\n\
  6049. QQ[x, y] \
  6050. """
  6051. assert upretty(F) == ucode_str
  6052. assert pretty(F) == ascii_str
  6053. ucode_str = \
  6054. """\
  6055. ╱ ⎡ 2⎤╲\n\
  6056. ╲[x, y], ⎣1, x ⎦╱\
  6057. """
  6058. ascii_str = \
  6059. """\
  6060. 2 \n\
  6061. <[x, y], [1, x ]>\
  6062. """
  6063. assert upretty(M) == ucode_str
  6064. assert pretty(M) == ascii_str
  6065. I = R.ideal(x**2, y)
  6066. ucode_str = \
  6067. """\
  6068. ╱ 2 ╲\n\
  6069. ╲x , y╱\
  6070. """
  6071. ascii_str = \
  6072. """\
  6073. 2 \n\
  6074. <x , y>\
  6075. """
  6076. assert upretty(I) == ucode_str
  6077. assert pretty(I) == ascii_str
  6078. Q = F / M
  6079. ucode_str = \
  6080. """\
  6081. 2 \n\
  6082. ℚ[x, y] \n\
  6083. ─────────────────\n\
  6084. ╱ ⎡ 2⎤╲\n\
  6085. ╲[x, y], ⎣1, x ⎦╱\
  6086. """
  6087. ascii_str = \
  6088. """\
  6089. 2 \n\
  6090. QQ[x, y] \n\
  6091. -----------------\n\
  6092. 2 \n\
  6093. <[x, y], [1, x ]>\
  6094. """
  6095. assert upretty(Q) == ucode_str
  6096. assert pretty(Q) == ascii_str
  6097. ucode_str = \
  6098. """\
  6099. ╱⎡ 3⎤ ╲\n\
  6100. │⎢ x ⎥ ╱ ⎡ 2⎤╲ ╱ ⎡ 2⎤╲│\n\
  6101. │⎢1, ──⎥ + ╲[x, y], ⎣1, x ⎦╱, [2, y] + ╲[x, y], ⎣1, x ⎦╱│\n\
  6102. ╲⎣ 2 ⎦ ╱\
  6103. """
  6104. ascii_str = \
  6105. """\
  6106. 3 \n\
  6107. x 2 2 \n\
  6108. <[1, --] + <[x, y], [1, x ]>, [2, y] + <[x, y], [1, x ]>>\n\
  6109. 2 \
  6110. """
  6111. def test_QuotientRing():
  6112. R = QQ.old_poly_ring(x)/[x**2 + 1]
  6113. ucode_str = \
  6114. """\
  6115. ℚ[x] \n\
  6116. ────────\n\
  6117. ╱ 2 ╲\n\
  6118. ╲x + 1╱\
  6119. """
  6120. ascii_str = \
  6121. """\
  6122. QQ[x] \n\
  6123. --------\n\
  6124. 2 \n\
  6125. <x + 1>\
  6126. """
  6127. assert upretty(R) == ucode_str
  6128. assert pretty(R) == ascii_str
  6129. ucode_str = \
  6130. """\
  6131. ╱ 2 ╲\n\
  6132. 1 + ╲x + 1╱\
  6133. """
  6134. ascii_str = \
  6135. """\
  6136. 2 \n\
  6137. 1 + <x + 1>\
  6138. """
  6139. assert upretty(R.one) == ucode_str
  6140. assert pretty(R.one) == ascii_str
  6141. def test_Homomorphism():
  6142. from sympy.polys.agca import homomorphism
  6143. R = QQ.old_poly_ring(x)
  6144. expr = homomorphism(R.free_module(1), R.free_module(1), [0])
  6145. ucode_str = \
  6146. """\
  6147. 1 1\n\
  6148. [0] : ℚ[x] ──> ℚ[x] \
  6149. """
  6150. ascii_str = \
  6151. """\
  6152. 1 1\n\
  6153. [0] : QQ[x] --> QQ[x] \
  6154. """
  6155. assert upretty(expr) == ucode_str
  6156. assert pretty(expr) == ascii_str
  6157. expr = homomorphism(R.free_module(2), R.free_module(2), [0, 0])
  6158. ucode_str = \
  6159. """\
  6160. ⎡0 0⎤ 2 2\n\
  6161. ⎢ ⎥ : ℚ[x] ──> ℚ[x] \n\
  6162. ⎣0 0⎦ \
  6163. """
  6164. ascii_str = \
  6165. """\
  6166. [0 0] 2 2\n\
  6167. [ ] : QQ[x] --> QQ[x] \n\
  6168. [0 0] \
  6169. """
  6170. assert upretty(expr) == ucode_str
  6171. assert pretty(expr) == ascii_str
  6172. expr = homomorphism(R.free_module(1), R.free_module(1) / [[x]], [0])
  6173. ucode_str = \
  6174. """\
  6175. 1\n\
  6176. 1 ℚ[x] \n\
  6177. [0] : ℚ[x] ──> ─────\n\
  6178. <[x]>\
  6179. """
  6180. ascii_str = \
  6181. """\
  6182. 1\n\
  6183. 1 QQ[x] \n\
  6184. [0] : QQ[x] --> ------\n\
  6185. <[x]> \
  6186. """
  6187. assert upretty(expr) == ucode_str
  6188. assert pretty(expr) == ascii_str
  6189. def test_Tr():
  6190. A, B = symbols('A B', commutative=False)
  6191. t = Tr(A*B)
  6192. assert pretty(t) == r'Tr(A*B)'
  6193. assert upretty(t) == 'Tr(A⋅B)'
  6194. def test_pretty_Add():
  6195. eq = Mul(-2, x - 2, evaluate=False) + 5
  6196. assert pretty(eq) == '5 - 2*(x - 2)'
  6197. def test_issue_7179():
  6198. assert upretty(Not(Equivalent(x, y))) == 'x ⇎ y'
  6199. assert upretty(Not(Implies(x, y))) == 'x ↛ y'
  6200. def test_issue_7180():
  6201. assert upretty(Equivalent(x, y)) == 'x ⇔ y'
  6202. def test_pretty_Complement():
  6203. assert pretty(S.Reals - S.Naturals) == '(-oo, oo) \\ Naturals'
  6204. assert upretty(S.Reals - S.Naturals) == 'ℝ \\ ℕ'
  6205. assert pretty(S.Reals - S.Naturals0) == '(-oo, oo) \\ Naturals0'
  6206. assert upretty(S.Reals - S.Naturals0) == 'ℝ \\ ℕ₀'
  6207. def test_pretty_SymmetricDifference():
  6208. from sympy.sets.sets import SymmetricDifference
  6209. assert upretty(SymmetricDifference(Interval(2,3), Interval(3,5), \
  6210. evaluate = False)) == '[2, 3] ∆ [3, 5]'
  6211. with raises(NotImplementedError):
  6212. pretty(SymmetricDifference(Interval(2,3), Interval(3,5), evaluate = False))
  6213. def test_pretty_Contains():
  6214. assert pretty(Contains(x, S.Integers)) == 'Contains(x, Integers)'
  6215. assert upretty(Contains(x, S.Integers)) == 'x ∈ ℤ'
  6216. def test_issue_8292():
  6217. from sympy.core import sympify
  6218. e = sympify('((x+x**4)/(x-1))-(2*(x-1)**4/(x-1)**4)', evaluate=False)
  6219. ucode_str = \
  6220. """\
  6221. 4 4 \n\
  6222. 2⋅(x - 1) x + x\n\
  6223. - ────────── + ──────\n\
  6224. 4 x - 1 \n\
  6225. (x - 1) \
  6226. """
  6227. ascii_str = \
  6228. """\
  6229. 4 4 \n\
  6230. 2*(x - 1) x + x\n\
  6231. - ---------- + ------\n\
  6232. 4 x - 1 \n\
  6233. (x - 1) \
  6234. """
  6235. assert pretty(e) == ascii_str
  6236. assert upretty(e) == ucode_str
  6237. def test_issue_4335():
  6238. y = Function('y')
  6239. expr = -y(x).diff(x)
  6240. ucode_str = \
  6241. """\
  6242. d \n\
  6243. -──(y(x))\n\
  6244. dx \
  6245. """
  6246. ascii_str = \
  6247. """\
  6248. d \n\
  6249. - --(y(x))\n\
  6250. dx \
  6251. """
  6252. assert pretty(expr) == ascii_str
  6253. assert upretty(expr) == ucode_str
  6254. def test_issue_8344():
  6255. from sympy.core import sympify
  6256. e = sympify('2*x*y**2/1**2 + 1', evaluate=False)
  6257. ucode_str = \
  6258. """\
  6259. 2 \n\
  6260. 2⋅x⋅y \n\
  6261. ────── + 1\n\
  6262. 2 \n\
  6263. 1 \
  6264. """
  6265. assert upretty(e) == ucode_str
  6266. def test_issue_6324():
  6267. x = Pow(2, 3, evaluate=False)
  6268. y = Pow(10, -2, evaluate=False)
  6269. e = Mul(x, y, evaluate=False)
  6270. ucode_str = \
  6271. """\
  6272. 3 \n\
  6273. 2 \n\
  6274. ───\n\
  6275. 2\n\
  6276. 10 \
  6277. """
  6278. assert upretty(e) == ucode_str
  6279. def test_issue_7927():
  6280. e = sin(x/2)**cos(x/2)
  6281. ucode_str = \
  6282. """\
  6283. ⎛x⎞\n\
  6284. cos⎜─⎟\n\
  6285. ⎝2⎠\n\
  6286. ⎛ ⎛x⎞⎞ \n\
  6287. ⎜sin⎜─⎟⎟ \n\
  6288. ⎝ ⎝2⎠⎠ \
  6289. """
  6290. assert upretty(e) == ucode_str
  6291. e = sin(x)**(S(11)/13)
  6292. ucode_str = \
  6293. """\
  6294. 11\n\
  6295. ──\n\
  6296. 13\n\
  6297. (sin(x)) \
  6298. """
  6299. assert upretty(e) == ucode_str
  6300. def test_issue_6134():
  6301. from sympy.abc import lamda, t
  6302. phi = Function('phi')
  6303. 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))
  6304. ucode_str = \
  6305. """\
  6306. 1 1 \n\
  6307. 2 ⌠ ⌠ \n\
  6308. λ⋅x ⋅⎮ 2⋅π⋅φ(t)⋅sin(2⋅π⋅t) dt + λ⋅x⋅⎮ π⋅φ(t)⋅sin(π⋅t) dt\n\
  6309. ⌡ ⌡ \n\
  6310. 0 0 \
  6311. """
  6312. assert upretty(e) == ucode_str
  6313. def test_issue_9877():
  6314. ucode_str1 = '(2, 3) ∪ ([1, 2] \\ {x})'
  6315. a, b, c = Interval(2, 3, True, True), Interval(1, 2), FiniteSet(x)
  6316. assert upretty(Union(a, Complement(b, c))) == ucode_str1
  6317. ucode_str2 = '{x} ∩ {y} ∩ ({z} \\ [1, 2])'
  6318. d, e, f, g = FiniteSet(x), FiniteSet(y), FiniteSet(z), Interval(1, 2)
  6319. assert upretty(Intersection(d, e, Complement(f, g))) == ucode_str2
  6320. def test_issue_13651():
  6321. expr1 = c + Mul(-1, a + b, evaluate=False)
  6322. assert pretty(expr1) == 'c - (a + b)'
  6323. expr2 = c + Mul(-1, a - b + d, evaluate=False)
  6324. assert pretty(expr2) == 'c - (a - b + d)'
  6325. def test_pretty_primenu():
  6326. from sympy.functions.combinatorial.numbers import primenu
  6327. ascii_str1 = "nu(n)"
  6328. ucode_str1 = "ν(n)"
  6329. n = symbols('n', integer=True)
  6330. assert pretty(primenu(n)) == ascii_str1
  6331. assert upretty(primenu(n)) == ucode_str1
  6332. def test_pretty_primeomega():
  6333. from sympy.functions.combinatorial.numbers import primeomega
  6334. ascii_str1 = "Omega(n)"
  6335. ucode_str1 = "Ω(n)"
  6336. n = symbols('n', integer=True)
  6337. assert pretty(primeomega(n)) == ascii_str1
  6338. assert upretty(primeomega(n)) == ucode_str1
  6339. def test_pretty_Mod():
  6340. from sympy.core import Mod
  6341. ascii_str1 = "x mod 7"
  6342. ucode_str1 = "x mod 7"
  6343. ascii_str2 = "(x + 1) mod 7"
  6344. ucode_str2 = "(x + 1) mod 7"
  6345. ascii_str3 = "2*x mod 7"
  6346. ucode_str3 = "2⋅x mod 7"
  6347. ascii_str4 = "(x mod 7) + 1"
  6348. ucode_str4 = "(x mod 7) + 1"
  6349. ascii_str5 = "2*(x mod 7)"
  6350. ucode_str5 = "2⋅(x mod 7)"
  6351. x = symbols('x', integer=True)
  6352. assert pretty(Mod(x, 7)) == ascii_str1
  6353. assert upretty(Mod(x, 7)) == ucode_str1
  6354. assert pretty(Mod(x + 1, 7)) == ascii_str2
  6355. assert upretty(Mod(x + 1, 7)) == ucode_str2
  6356. assert pretty(Mod(2 * x, 7)) == ascii_str3
  6357. assert upretty(Mod(2 * x, 7)) == ucode_str3
  6358. assert pretty(Mod(x, 7) + 1) == ascii_str4
  6359. assert upretty(Mod(x, 7) + 1) == ucode_str4
  6360. assert pretty(2 * Mod(x, 7)) == ascii_str5
  6361. assert upretty(2 * Mod(x, 7)) == ucode_str5
  6362. def test_issue_11801():
  6363. assert pretty(Symbol("")) == ""
  6364. assert upretty(Symbol("")) == ""
  6365. def test_pretty_UnevaluatedExpr():
  6366. x = symbols('x')
  6367. he = UnevaluatedExpr(1/x)
  6368. ucode_str = \
  6369. """\
  6370. 1\n\
  6371. ─\n\
  6372. x\
  6373. """
  6374. assert upretty(he) == ucode_str
  6375. ucode_str = \
  6376. """\
  6377. 2\n\
  6378. ⎛1⎞ \n\
  6379. ⎜─⎟ \n\
  6380. ⎝x⎠ \
  6381. """
  6382. assert upretty(he**2) == ucode_str
  6383. ucode_str = \
  6384. """\
  6385. 1\n\
  6386. 1 + ─\n\
  6387. x\
  6388. """
  6389. assert upretty(he + 1) == ucode_str
  6390. ucode_str = \
  6391. ('''\
  6392. 1\n\
  6393. x⋅─\n\
  6394. x\
  6395. ''')
  6396. assert upretty(x*he) == ucode_str
  6397. def test_issue_10472():
  6398. M = (Matrix([[0, 0], [0, 0]]), Matrix([0, 0]))
  6399. ucode_str = \
  6400. """\
  6401. ⎛⎡0 0⎤ ⎡0⎤⎞
  6402. ⎜⎢ ⎥, ⎢ ⎥⎟
  6403. ⎝⎣0 0⎦ ⎣0⎦⎠\
  6404. """
  6405. assert upretty(M) == ucode_str
  6406. def test_MatrixElement_printing():
  6407. # test cases for issue #11821
  6408. A = MatrixSymbol("A", 1, 3)
  6409. B = MatrixSymbol("B", 1, 3)
  6410. C = MatrixSymbol("C", 1, 3)
  6411. ascii_str1 = "A_00"
  6412. ucode_str1 = "A₀₀"
  6413. assert pretty(A[0, 0]) == ascii_str1
  6414. assert upretty(A[0, 0]) == ucode_str1
  6415. ascii_str1 = "3*A_00"
  6416. ucode_str1 = "3⋅A₀₀"
  6417. assert pretty(3*A[0, 0]) == ascii_str1
  6418. assert upretty(3*A[0, 0]) == ucode_str1
  6419. ascii_str1 = "(-B + A)[0, 0]"
  6420. ucode_str1 = "(-B + A)[0, 0]"
  6421. F = C[0, 0].subs(C, A - B)
  6422. assert pretty(F) == ascii_str1
  6423. assert upretty(F) == ucode_str1
  6424. def test_issue_12675():
  6425. x, y, t, j = symbols('x y t j')
  6426. e = CoordSys3D('e')
  6427. ucode_str = \
  6428. """\
  6429. ⎛ t⎞ \n\
  6430. ⎜⎛x⎞ ⎟ j_e\n\
  6431. ⎜⎜─⎟ ⎟ \n\
  6432. ⎝⎝y⎠ ⎠ \
  6433. """
  6434. assert upretty((x/y)**t*e.j) == ucode_str
  6435. ucode_str = \
  6436. """\
  6437. ⎛1⎞ \n\
  6438. ⎜─⎟ j_e\n\
  6439. ⎝y⎠ \
  6440. """
  6441. assert upretty((1/y)*e.j) == ucode_str
  6442. def test_MatrixSymbol_printing():
  6443. # test cases for issue #14237
  6444. A = MatrixSymbol("A", 3, 3)
  6445. B = MatrixSymbol("B", 3, 3)
  6446. C = MatrixSymbol("C", 3, 3)
  6447. assert pretty(-A*B*C) == "-A*B*C"
  6448. assert pretty(A - B) == "-B + A"
  6449. assert pretty(A*B*C - A*B - B*C) == "-A*B -B*C + A*B*C"
  6450. # issue #14814
  6451. x = MatrixSymbol('x', n, n)
  6452. y = MatrixSymbol('y*', n, n)
  6453. assert pretty(x + y) == "x + y*"
  6454. ascii_str = \
  6455. """\
  6456. 2 \n\
  6457. -2*y* -a*x\
  6458. """
  6459. assert pretty(-a*x + -2*y*y) == ascii_str
  6460. def test_degree_printing():
  6461. expr1 = 90*degree
  6462. assert pretty(expr1) == '90°'
  6463. expr2 = x*degree
  6464. assert pretty(expr2) == 'x°'
  6465. expr3 = cos(x*degree + 90*degree)
  6466. assert pretty(expr3) == 'cos(x° + 90°)'
  6467. def test_vector_expr_pretty_printing():
  6468. A = CoordSys3D('A')
  6469. 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)"
  6470. assert upretty(x*Cross(A.i, A.j)) == 'x⋅(i_A)×(j_A)'
  6471. assert upretty(Curl(A.x*A.i + 3*A.y*A.j)) == "∇×((x_A) i_A + (3⋅y_A) j_A)"
  6472. assert upretty(Divergence(A.x*A.i + 3*A.y*A.j)) == "∇⋅((x_A) i_A + (3⋅y_A) j_A)"
  6473. 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)"
  6474. assert upretty(Gradient(A.x+3*A.y)) == "∇(x_A + 3⋅y_A)"
  6475. assert upretty(Laplacian(A.x+3*A.y)) == "∆(x_A + 3⋅y_A)"
  6476. # TODO: add support for ASCII pretty.
  6477. def test_pretty_print_tensor_expr():
  6478. L = TensorIndexType("L")
  6479. i, j, k = tensor_indices("i j k", L)
  6480. i0 = tensor_indices("i_0", L)
  6481. A, B, C, D = tensor_heads("A B C D", [L])
  6482. H = TensorHead("H", [L, L])
  6483. expr = -i
  6484. ascii_str = \
  6485. """\
  6486. -i\
  6487. """
  6488. ucode_str = \
  6489. """\
  6490. -i\
  6491. """
  6492. assert pretty(expr) == ascii_str
  6493. assert upretty(expr) == ucode_str
  6494. expr = A(i)
  6495. ascii_str = \
  6496. """\
  6497. i\n\
  6498. A \n\
  6499. \
  6500. """
  6501. ucode_str = \
  6502. """\
  6503. i\n\
  6504. A \n\
  6505. \
  6506. """
  6507. assert pretty(expr) == ascii_str
  6508. assert upretty(expr) == ucode_str
  6509. expr = A(i0)
  6510. ascii_str = \
  6511. """\
  6512. i_0\n\
  6513. A \n\
  6514. \
  6515. """
  6516. ucode_str = \
  6517. """\
  6518. i₀\n\
  6519. A \n\
  6520. \
  6521. """
  6522. assert pretty(expr) == ascii_str
  6523. assert upretty(expr) == ucode_str
  6524. expr = A(-i)
  6525. ascii_str = \
  6526. """\
  6527. \n\
  6528. A \n\
  6529. i\
  6530. """
  6531. ucode_str = \
  6532. """\
  6533. \n\
  6534. A \n\
  6535. i\
  6536. """
  6537. assert pretty(expr) == ascii_str
  6538. assert upretty(expr) == ucode_str
  6539. expr = -3*A(-i)
  6540. ascii_str = \
  6541. """\
  6542. \n\
  6543. -3*A \n\
  6544. i\
  6545. """
  6546. ucode_str = \
  6547. """\
  6548. \n\
  6549. -3⋅A \n\
  6550. i\
  6551. """
  6552. assert pretty(expr) == ascii_str
  6553. assert upretty(expr) == ucode_str
  6554. expr = H(i, -j)
  6555. ascii_str = \
  6556. """\
  6557. i \n\
  6558. H \n\
  6559. j\
  6560. """
  6561. ucode_str = \
  6562. """\
  6563. i \n\
  6564. H \n\
  6565. j\
  6566. """
  6567. assert pretty(expr) == ascii_str
  6568. assert upretty(expr) == ucode_str
  6569. expr = H(i, -i)
  6570. ascii_str = \
  6571. """\
  6572. L_0 \n\
  6573. H \n\
  6574. L_0\
  6575. """
  6576. ucode_str = \
  6577. """\
  6578. L₀ \n\
  6579. H \n\
  6580. L₀\
  6581. """
  6582. assert pretty(expr) == ascii_str
  6583. assert upretty(expr) == ucode_str
  6584. expr = H(i, -j)*A(j)*B(k)
  6585. ascii_str = \
  6586. """\
  6587. i L_0 k\n\
  6588. H *A *B \n\
  6589. L_0 \
  6590. """
  6591. ucode_str = \
  6592. """\
  6593. i L₀ k\n\
  6594. H ⋅A ⋅B \n\
  6595. L₀ \
  6596. """
  6597. assert pretty(expr) == ascii_str
  6598. assert upretty(expr) == ucode_str
  6599. expr = (1+x)*A(i)
  6600. ascii_str = \
  6601. """\
  6602. i\n\
  6603. (x + 1)*A \n\
  6604. \
  6605. """
  6606. ucode_str = \
  6607. """\
  6608. i\n\
  6609. (x + 1)⋅A \n\
  6610. \
  6611. """
  6612. assert pretty(expr) == ascii_str
  6613. assert upretty(expr) == ucode_str
  6614. expr = A(i) + 3*B(i)
  6615. ascii_str = \
  6616. """\
  6617. i i\n\
  6618. 3*B + A \n\
  6619. \
  6620. """
  6621. ucode_str = \
  6622. """\
  6623. i i\n\
  6624. 3⋅B + A \n\
  6625. \
  6626. """
  6627. assert pretty(expr) == ascii_str
  6628. assert upretty(expr) == ucode_str
  6629. def test_pretty_print_tensor_partial_deriv():
  6630. from sympy.tensor.toperators import PartialDerivative
  6631. L = TensorIndexType("L")
  6632. i, j, k = tensor_indices("i j k", L)
  6633. A, B, C, D = tensor_heads("A B C D", [L])
  6634. H = TensorHead("H", [L, L])
  6635. expr = PartialDerivative(A(i), A(j))
  6636. ascii_str = \
  6637. """\
  6638. d / i\\\n\
  6639. ---|A |\n\
  6640. j\\ /\n\
  6641. dA \n\
  6642. \
  6643. """
  6644. ucode_str = \
  6645. """\
  6646. ∂ ⎛ i⎞\n\
  6647. ───⎜A ⎟\n\
  6648. j⎝ ⎠\n\
  6649. ∂A \n\
  6650. \
  6651. """
  6652. assert pretty(expr) == ascii_str
  6653. assert upretty(expr) == ucode_str
  6654. expr = A(i)*PartialDerivative(H(k, -i), A(j))
  6655. ascii_str = \
  6656. """\
  6657. L_0 d / k \\\n\
  6658. A *---|H |\n\
  6659. j\\ L_0/\n\
  6660. dA \n\
  6661. \
  6662. """
  6663. ucode_str = \
  6664. """\
  6665. L₀ ∂ ⎛ k ⎞\n\
  6666. A ⋅───⎜H ⎟\n\
  6667. j⎝ L₀⎠\n\
  6668. ∂A \n\
  6669. \
  6670. """
  6671. assert pretty(expr) == ascii_str
  6672. assert upretty(expr) == ucode_str
  6673. expr = A(i)*PartialDerivative(B(k)*C(-i) + 3*H(k, -i), A(j))
  6674. ascii_str = \
  6675. """\
  6676. L_0 d / k k \\\n\
  6677. A *---|3*H + B *C |\n\
  6678. j\\ L_0 L_0/\n\
  6679. dA \n\
  6680. \
  6681. """
  6682. ucode_str = \
  6683. """\
  6684. L₀ ∂ ⎛ k k ⎞\n\
  6685. A ⋅───⎜3⋅H + B ⋅C ⎟\n\
  6686. j⎝ L₀ L₀⎠\n\
  6687. ∂A \n\
  6688. \
  6689. """
  6690. assert pretty(expr) == ascii_str
  6691. assert upretty(expr) == ucode_str
  6692. expr = (A(i) + B(i))*PartialDerivative(C(j), D(j))
  6693. ascii_str = \
  6694. """\
  6695. / i i\\ d / L_0\\\n\
  6696. |A + B |*-----|C |\n\
  6697. \\ / L_0\\ /\n\
  6698. dD \n\
  6699. \
  6700. """
  6701. ucode_str = \
  6702. """\
  6703. ⎛ i i⎞ ∂ ⎛ L₀⎞\n\
  6704. ⎜A + B ⎟⋅────⎜C ⎟\n\
  6705. ⎝ ⎠ L₀⎝ ⎠\n\
  6706. ∂D \n\
  6707. \
  6708. """
  6709. assert pretty(expr) == ascii_str
  6710. assert upretty(expr) == ucode_str
  6711. expr = (A(i) + B(i))*PartialDerivative(C(-i), D(j))
  6712. ascii_str = \
  6713. """\
  6714. / L_0 L_0\\ d / \\\n\
  6715. |A + B |*---|C |\n\
  6716. \\ / j\\ L_0/\n\
  6717. dD \n\
  6718. \
  6719. """
  6720. ucode_str = \
  6721. """\
  6722. ⎛ L₀ L₀⎞ ∂ ⎛ ⎞\n\
  6723. ⎜A + B ⎟⋅───⎜C ⎟\n\
  6724. ⎝ ⎠ j⎝ L₀⎠\n\
  6725. ∂D \n\
  6726. \
  6727. """
  6728. assert pretty(expr) == ascii_str
  6729. assert upretty(expr) == ucode_str
  6730. expr = PartialDerivative(B(-i) + A(-i), A(-j), A(-n))
  6731. ucode_str = """\
  6732. 2 \n\
  6733. ∂ ⎛ ⎞\n\
  6734. ───────⎜A + B ⎟\n\
  6735. ⎝ i i⎠\n\
  6736. ∂A ∂A \n\
  6737. n j \
  6738. """
  6739. assert upretty(expr) == ucode_str
  6740. expr = PartialDerivative(3*A(-i), A(-j), A(-n))
  6741. ucode_str = """\
  6742. 2 \n\
  6743. ∂ ⎛ ⎞\n\
  6744. ───────⎜3⋅A ⎟\n\
  6745. ⎝ i⎠\n\
  6746. ∂A ∂A \n\
  6747. n j \
  6748. """
  6749. assert upretty(expr) == ucode_str
  6750. expr = TensorElement(H(i, j), {i:1})
  6751. ascii_str = \
  6752. """\
  6753. i=1,j\n\
  6754. H \n\
  6755. \
  6756. """
  6757. ucode_str = ascii_str
  6758. assert pretty(expr) == ascii_str
  6759. assert upretty(expr) == ucode_str
  6760. expr = TensorElement(H(i, j), {i: 1, j: 1})
  6761. ascii_str = \
  6762. """\
  6763. i=1,j=1\n\
  6764. H \n\
  6765. \
  6766. """
  6767. ucode_str = ascii_str
  6768. assert pretty(expr) == ascii_str
  6769. assert upretty(expr) == ucode_str
  6770. expr = TensorElement(H(i, j), {j: 1})
  6771. ascii_str = \
  6772. """\
  6773. i,j=1\n\
  6774. H \n\
  6775. \
  6776. """
  6777. ucode_str = ascii_str
  6778. expr = TensorElement(H(-i, j), {-i: 1})
  6779. ascii_str = \
  6780. """\
  6781. j\n\
  6782. H \n\
  6783. i=1 \
  6784. """
  6785. ucode_str = ascii_str
  6786. assert pretty(expr) == ascii_str
  6787. assert upretty(expr) == ucode_str
  6788. def test_issue_15560():
  6789. a = MatrixSymbol('a', 1, 1)
  6790. e = pretty(a*(KroneckerProduct(a, a)))
  6791. result = 'a*(a x a)'
  6792. assert e == result
  6793. def test_print_polylog():
  6794. # Part of issue 6013
  6795. uresult = 'Li₂(3)'
  6796. aresult = 'polylog(2, 3)'
  6797. assert pretty(polylog(2, 3)) == aresult
  6798. assert upretty(polylog(2, 3)) == uresult
  6799. # Issue #25312
  6800. def test_print_expint_polylog_symbolic_order():
  6801. s, z = symbols("s, z")
  6802. uresult = 'Liₛ(z)'
  6803. aresult = 'polylog(s, z)'
  6804. assert pretty(polylog(s, z)) == aresult
  6805. assert upretty(polylog(s, z)) == uresult
  6806. # TODO: TBD polylog(s - 1, z)
  6807. uresult = 'Eₛ(z)'
  6808. aresult = 'expint(s, z)'
  6809. assert pretty(expint(s, z)) == aresult
  6810. assert upretty(expint(s, z)) == uresult
  6811. def test_print_polylog_long_order_issue_25309():
  6812. s, z = symbols("s, z")
  6813. ucode_str = \
  6814. """\
  6815. ⎛ 2 ⎞\n\
  6816. polylog⎝s , z⎠\
  6817. """
  6818. assert upretty(polylog(s**2, z)) == ucode_str
  6819. def test_print_lerchphi():
  6820. # Part of issue 6013
  6821. a = Symbol('a')
  6822. pretty(lerchphi(a, 1, 2))
  6823. uresult = 'Φ(a, 1, 2)'
  6824. aresult = 'lerchphi(a, 1, 2)'
  6825. assert pretty(lerchphi(a, 1, 2)) == aresult
  6826. assert upretty(lerchphi(a, 1, 2)) == uresult
  6827. def test_issue_15583():
  6828. N = mechanics.ReferenceFrame('N')
  6829. result = '(n_x, n_y, n_z)'
  6830. e = pretty((N.x, N.y, N.z))
  6831. assert e == result
  6832. def test_matrixSymbolBold():
  6833. # Issue 15871
  6834. def boldpretty(expr):
  6835. return xpretty(expr, use_unicode=True, wrap_line=False, mat_symbol_style="bold")
  6836. from sympy.matrices.expressions.trace import trace
  6837. A = MatrixSymbol("A", 2, 2)
  6838. assert boldpretty(trace(A)) == 'tr(𝐀)'
  6839. A = MatrixSymbol("A", 3, 3)
  6840. B = MatrixSymbol("B", 3, 3)
  6841. C = MatrixSymbol("C", 3, 3)
  6842. assert boldpretty(-A) == '-𝐀'
  6843. assert boldpretty(A - A*B - B) == '-𝐁 -𝐀⋅𝐁 + 𝐀'
  6844. assert boldpretty(-A*B - A*B*C - B) == '-𝐁 -𝐀⋅𝐁 -𝐀⋅𝐁⋅𝐂'
  6845. A = MatrixSymbol("Addot", 3, 3)
  6846. assert boldpretty(A) == '𝐀̈'
  6847. omega = MatrixSymbol("omega", 3, 3)
  6848. assert boldpretty(omega) == 'ω'
  6849. omega = MatrixSymbol("omeganorm", 3, 3)
  6850. assert boldpretty(omega) == '‖ω‖'
  6851. a = Symbol('alpha')
  6852. b = Symbol('b')
  6853. c = MatrixSymbol("c", 3, 1)
  6854. d = MatrixSymbol("d", 3, 1)
  6855. assert boldpretty(a*B*c+b*d) == 'b⋅𝐝 + α⋅𝐁⋅𝐜'
  6856. d = MatrixSymbol("delta", 3, 1)
  6857. B = MatrixSymbol("Beta", 3, 3)
  6858. assert boldpretty(a*B*c+b*d) == 'b⋅δ + α⋅Β⋅𝐜'
  6859. A = MatrixSymbol("A_2", 3, 3)
  6860. assert boldpretty(A) == '𝐀₂'
  6861. def test_center_accent():
  6862. assert center_accent('a', '\N{COMBINING TILDE}') == 'ã'
  6863. assert center_accent('aa', '\N{COMBINING TILDE}') == 'aã'
  6864. assert center_accent('aaa', '\N{COMBINING TILDE}') == 'aãa'
  6865. assert center_accent('aaaa', '\N{COMBINING TILDE}') == 'aaãa'
  6866. assert center_accent('aaaaa', '\N{COMBINING TILDE}') == 'aaãaa'
  6867. assert center_accent('abcdefg', '\N{COMBINING FOUR DOTS ABOVE}') == 'abcd⃜efg'
  6868. def test_imaginary_unit():
  6869. from sympy.printing.pretty import pretty # b/c it was redefined above
  6870. assert pretty(1 + I, use_unicode=False) == '1 + I'
  6871. assert pretty(1 + I, use_unicode=True) == '1 + ⅈ'
  6872. assert pretty(1 + I, use_unicode=False, imaginary_unit='j') == '1 + I'
  6873. assert pretty(1 + I, use_unicode=True, imaginary_unit='j') == '1 + ⅉ'
  6874. raises(TypeError, lambda: pretty(I, imaginary_unit=I))
  6875. raises(ValueError, lambda: pretty(I, imaginary_unit="kkk"))
  6876. def test_str_special_matrices():
  6877. from sympy.matrices import Identity, ZeroMatrix, OneMatrix
  6878. assert pretty(Identity(4)) == 'I'
  6879. assert upretty(Identity(4)) == '𝕀'
  6880. assert pretty(ZeroMatrix(2, 2)) == '0'
  6881. assert upretty(ZeroMatrix(2, 2)) == '𝟘'
  6882. assert pretty(OneMatrix(2, 2)) == '1'
  6883. assert upretty(OneMatrix(2, 2)) == '𝟙'
  6884. def test_pretty_misc_functions():
  6885. assert pretty(LambertW(x)) == 'W(x)'
  6886. assert upretty(LambertW(x)) == 'W(x)'
  6887. assert pretty(LambertW(x, y)) == 'W(x, y)'
  6888. assert upretty(LambertW(x, y)) == 'W(x, y)'
  6889. assert pretty(airyai(x)) == 'Ai(x)'
  6890. assert upretty(airyai(x)) == 'Ai(x)'
  6891. assert pretty(airybi(x)) == 'Bi(x)'
  6892. assert upretty(airybi(x)) == 'Bi(x)'
  6893. assert pretty(airyaiprime(x)) == "Ai'(x)"
  6894. assert upretty(airyaiprime(x)) == "Ai'(x)"
  6895. assert pretty(airybiprime(x)) == "Bi'(x)"
  6896. assert upretty(airybiprime(x)) == "Bi'(x)"
  6897. assert pretty(fresnelc(x)) == 'C(x)'
  6898. assert upretty(fresnelc(x)) == 'C(x)'
  6899. assert pretty(fresnels(x)) == 'S(x)'
  6900. assert upretty(fresnels(x)) == 'S(x)'
  6901. assert pretty(Heaviside(x)) == 'Heaviside(x)'
  6902. assert upretty(Heaviside(x)) == 'θ(x)'
  6903. assert pretty(Heaviside(x, y)) == 'Heaviside(x, y)'
  6904. assert upretty(Heaviside(x, y)) == 'θ(x, y)'
  6905. assert pretty(dirichlet_eta(x)) == 'dirichlet_eta(x)'
  6906. assert upretty(dirichlet_eta(x)) == 'η(x)'
  6907. def test_hadamard_power():
  6908. m, n, p = symbols('m, n, p', integer=True)
  6909. A = MatrixSymbol('A', m, n)
  6910. B = MatrixSymbol('B', m, n)
  6911. # Testing printer:
  6912. expr = hadamard_power(A, n)
  6913. ascii_str = \
  6914. """\
  6915. .n\n\
  6916. A \
  6917. """
  6918. ucode_str = \
  6919. """\
  6920. ∘n\n\
  6921. A \
  6922. """
  6923. assert pretty(expr) == ascii_str
  6924. assert upretty(expr) == ucode_str
  6925. expr = hadamard_power(A, 1+n)
  6926. ascii_str = \
  6927. """\
  6928. .(n + 1)\n\
  6929. A \
  6930. """
  6931. ucode_str = \
  6932. """\
  6933. ∘(n + 1)\n\
  6934. A \
  6935. """
  6936. assert pretty(expr) == ascii_str
  6937. assert upretty(expr) == ucode_str
  6938. expr = hadamard_power(A*B.T, 1+n)
  6939. ascii_str = \
  6940. """\
  6941. .(n + 1)\n\
  6942. / T\\ \n\
  6943. \\A*B / \
  6944. """
  6945. ucode_str = \
  6946. """\
  6947. ∘(n + 1)\n\
  6948. ⎛ T⎞ \n\
  6949. ⎝A⋅B ⎠ \
  6950. """
  6951. assert pretty(expr) == ascii_str
  6952. assert upretty(expr) == ucode_str
  6953. def test_issue_17258():
  6954. n = Symbol('n', integer=True)
  6955. assert pretty(Sum(n, (n, -oo, 1))) == \
  6956. ' 1 \n'\
  6957. ' __ \n'\
  6958. ' \\ ` \n'\
  6959. ' ) n\n'\
  6960. ' /_, \n'\
  6961. 'n = -oo '
  6962. assert upretty(Sum(n, (n, -oo, 1))) == \
  6963. """\
  6964. 1 \n\
  6965. ___ \n\
  6966. ╲ \n\
  6967. ╲ \n\
  6968. ╱ n\n\
  6969. ╱ \n\
  6970. ‾‾‾ \n\
  6971. n = -∞ \
  6972. """
  6973. def test_is_combining():
  6974. line = "v̇_m"
  6975. assert [is_combining(sym) for sym in line] == \
  6976. [False, True, False, False]
  6977. def test_issue_17616():
  6978. assert pretty(pi**(1/exp(1))) == \
  6979. ' / -1\\\n'\
  6980. ' \\e /\n'\
  6981. 'pi '
  6982. assert upretty(pi**(1/exp(1))) == \
  6983. ' ⎛ -1⎞\n'\
  6984. ' ⎝ℯ ⎠\n'\
  6985. 'π '
  6986. assert pretty(pi**(1/pi)) == \
  6987. ' 1 \n'\
  6988. ' --\n'\
  6989. ' pi\n'\
  6990. 'pi '
  6991. assert upretty(pi**(1/pi)) == \
  6992. ' 1\n'\
  6993. ' ─\n'\
  6994. ' π\n'\
  6995. 'π '
  6996. assert pretty(pi**(1/EulerGamma)) == \
  6997. ' 1 \n'\
  6998. ' ----------\n'\
  6999. ' EulerGamma\n'\
  7000. 'pi '
  7001. assert upretty(pi**(1/EulerGamma)) == \
  7002. ' 1\n'\
  7003. ' ─\n'\
  7004. ' γ\n'\
  7005. 'π '
  7006. z = Symbol("x_17")
  7007. assert upretty(7**(1/z)) == \
  7008. 'x₁₇___\n'\
  7009. ' ╲╱ 7 '
  7010. assert pretty(7**(1/z)) == \
  7011. 'x_17___\n'\
  7012. ' \\/ 7 '
  7013. def test_issue_17857():
  7014. assert pretty(Range(-oo, oo)) == '{..., -1, 0, 1, ...}'
  7015. assert pretty(Range(oo, -oo, -1)) == '{..., 1, 0, -1, ...}'
  7016. def test_issue_18272():
  7017. x = Symbol('x')
  7018. n = Symbol('n')
  7019. assert upretty(ConditionSet(x, Eq(-x + exp(x), 0), S.Complexes)) == \
  7020. '⎧ │ ⎛ x ⎞⎫\n'\
  7021. '⎨x │ x ∊ ℂ ∧ ⎝-x + ℯ = 0⎠⎬\n'\
  7022. '⎩ │ ⎭'
  7023. assert upretty(ConditionSet(x, Contains(n/2, Interval(0, oo)), FiniteSet(-n/2, n/2))) == \
  7024. '⎧ │ ⎧-n n⎫ ⎛n ⎞⎫\n'\
  7025. '⎨x │ x ∊ ⎨───, ─⎬ ∧ ⎜─ ∈ [0, ∞)⎟⎬\n'\
  7026. '⎩ │ ⎩ 2 2⎭ ⎝2 ⎠⎭'
  7027. assert upretty(ConditionSet(x, Eq(Piecewise((1, x >= 3), (x/2 - 1/2, x >= 2), (1/2, x >= 1),
  7028. (x/2, True)) - 1/2, 0), Interval(0, 3))) == \
  7029. '⎧ │ ⎛⎛⎧ 1 for x ≥ 3⎞ ⎞⎫\n'\
  7030. '⎪ │ ⎜⎜⎪ ⎟ ⎟⎪\n'\
  7031. '⎪ │ ⎜⎜⎪x ⎟ ⎟⎪\n'\
  7032. '⎪ │ ⎜⎜⎪─ - 0.5 for x ≥ 2⎟ ⎟⎪\n'\
  7033. '⎪ │ ⎜⎜⎪2 ⎟ ⎟⎪\n'\
  7034. '⎨x │ x ∊ [0, 3] ∧ ⎜⎜⎨ ⎟ - 0.5 = 0⎟⎬\n'\
  7035. '⎪ │ ⎜⎜⎪ 0.5 for x ≥ 1⎟ ⎟⎪\n'\
  7036. '⎪ │ ⎜⎜⎪ ⎟ ⎟⎪\n'\
  7037. '⎪ │ ⎜⎜⎪ x ⎟ ⎟⎪\n'\
  7038. '⎪ │ ⎜⎜⎪ ─ otherwise⎟ ⎟⎪\n'\
  7039. '⎩ │ ⎝⎝⎩ 2 ⎠ ⎠⎭'
  7040. def test_Str():
  7041. from sympy.core.symbol import Str
  7042. assert pretty(Str('x')) == 'x'
  7043. def test_symbolic_probability():
  7044. mu = symbols("mu")
  7045. sigma = symbols("sigma", positive=True)
  7046. X = Normal("X", mu, sigma)
  7047. assert pretty(Expectation(X)) == r'E[X]'
  7048. assert pretty(Variance(X)) == r'Var(X)'
  7049. assert pretty(Probability(X > 0)) == r'P(X > 0)'
  7050. Y = Normal("Y", mu, sigma)
  7051. assert pretty(Covariance(X, Y)) == 'Cov(X, Y)'
  7052. def test_issue_21758():
  7053. from sympy.functions.elementary.piecewise import piecewise_fold
  7054. from sympy.series.fourier import FourierSeries
  7055. x = Symbol('x')
  7056. k, n = symbols('k n')
  7057. fo = FourierSeries(x, (x, -pi, pi), (0, SeqFormula(0, (k, 1, oo)), SeqFormula(
  7058. Piecewise((-2*pi*cos(n*pi)/n + 2*sin(n*pi)/n**2, (n > -oo) & (n < oo) & Ne(n, 0)),
  7059. (0, True))*sin(n*x)/pi, (n, 1, oo))))
  7060. assert upretty(piecewise_fold(fo)) == \
  7061. '⎧ 2⋅sin(3⋅x) \n'\
  7062. '⎪2⋅sin(x) - sin(2⋅x) + ────────── + … for n > -∞ ∧ n < ∞ ∧ n ≠ 0\n'\
  7063. '⎨ 3 \n'\
  7064. '⎪ \n'\
  7065. '⎩ 0 otherwise '
  7066. assert pretty(FourierSeries(x, (x, -pi, pi), (0, SeqFormula(0, (k, 1, oo)),
  7067. SeqFormula(0, (n, 1, oo))))) == '0'
  7068. def test_diffgeom():
  7069. from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField
  7070. x,y = symbols('x y', real=True)
  7071. m = Manifold('M', 2)
  7072. assert pretty(m) == 'M'
  7073. p = Patch('P', m)
  7074. assert pretty(p) == "P"
  7075. rect = CoordSystem('rect', p, [x, y])
  7076. assert pretty(rect) == "rect"
  7077. b = BaseScalarField(rect, 0)
  7078. assert pretty(b) == "x"
  7079. def test_deprecated_prettyForm():
  7080. with warns_deprecated_sympy():
  7081. from sympy.printing.pretty.pretty_symbology import xstr
  7082. assert xstr(1) == '1'
  7083. with warns_deprecated_sympy():
  7084. from sympy.printing.pretty.stringpict import prettyForm
  7085. p = prettyForm('s', unicode='s')
  7086. with warns_deprecated_sympy():
  7087. assert p.unicode == p.s == 's'
  7088. def test_center():
  7089. assert center('1', 2) == '1 '
  7090. assert center('1', 3) == ' 1 '
  7091. assert center('1', 3, '-') == '-1-'
  7092. assert center('1', 5, '-') == '--1--'