Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double Integration is very slow, while almost instant in maxima. #154

Closed
DUOLabs333 opened this issue Feb 10, 2022 · 52 comments
Closed

Double Integration is very slow, while almost instant in maxima. #154

DUOLabs333 opened this issue Feb 10, 2022 · 52 comments

Comments

@DUOLabs333
Copy link

DUOLabs333 commented Feb 10, 2022

Integrate[Integrate[20000*E^y/(1-x/2),{x,-5,0}],{y,-3,0}]+Integrate[Integrate [20000*E^y/(1+x/2),{x,0,5}],{y,-3,0}] is much slower in Mathics than maxima.

@DUOLabs333 DUOLabs333 changed the title Fails to integrate, but succeeds in Maxima Double Integration is very slow, while almost instant in maxima. Feb 11, 2022
@DUOLabs333
Copy link
Author

It seems that Maxima keeps the intermediate results in symbolic form, while Mathics (sympy) tries to expand it.

@rocky
Copy link
Member

rocky commented Feb 19, 2022

This is just one instance of an infinite number of examples of a long-standing problem regarding how evaluation is done in Mathics and how it uses SymPy as a backend computation engine.

It is being worked on currently, albeit slowly.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Feb 19, 2022

So, is Mathics working on its own engine or are you waiting for Sympy to fix it on their end?

@rocky
Copy link
Member

rocky commented Feb 19, 2022

This issue is probably our problem. However what you can do to verify is rewrite your example in SymPy and see how long that takes. The expectation is that Mathics will be comparable to this speed.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Feb 19, 2022

Yes, mathics is much slower. Sympy is almost instant.

@mmatera
Copy link
Contributor

mmatera commented Feb 20, 2022

Notice that iterated integrals are slower than integrals over several variables:

In[1]:= Timing[Integrate[Integrate[1,{y,0,E^x}],{x,0,Log[13]}]]
Out[1]= {0.0838364, 12}

In[2]:= Timing[Integrate[1,{y,0,E^x},{x,0,Log[13]}]]
Out[2]= {0.0450555, 12}

This is due to the conversion back and forward to sympy is done two times. Something that could help is to define a rule

Integrate[Integrate[f_,x_],y_]->Integrate[f,x,y]
to reduce the conversion to one step.

@rocky
Copy link
Member

rocky commented Feb 20, 2022

I am not necessarily opposed to this, but in the long term we need a way to have SymPy applied to larger expressions in one go automatically.

Otherwise we'll be adding rules for every combination of composition of SymPy functions imaginable. So lots of work, lots of Rules which could keep us busy for a long time and in the end cause lots of Rule bloat, when this is finally solved.

So maybe this could be isolated into some startup code were this is mentioned should we find one day it is no longer needed.

@mmatera
Copy link
Contributor

mmatera commented Feb 20, 2022

Otherwise we'll be adding rules for every combination of composition of SymPy functions imaginable. So lots of work, lots of Rules which could keep us busy for a long time and in the end cause lots of Rule bloat, when this is finally solved.

Actually, my proposal does not work, due to the workflow of the evaluation process: my rule is never applied, because the inner integral is evaluated before the outer one.

So, yes, the best option would be to try to covert the expression into sympy in an earlier stage of the evaluation process.

@mmatera
Copy link
Contributor

mmatera commented Feb 20, 2022

I see one place in mathics django, and two places in mathicsscript.

So a very simple thing is to keep the get_leaves() function as an alias to get_elements() for a little bit....

I think this is an answer to PR #157

@mmatera
Copy link
Contributor

mmatera commented Feb 20, 2022

So, yes, the best option would be to try to covert the expression into sympy in an earlier stage of the evaluation process.

Regarding this, it is not trivial to implement: Since in WL there are many symbols that are not the kind of things that sympy handles, and also that users can change definitions at runtime, it is not possible to just try to translate an expression to sympy in an early stage of the evaluation process. To be sure that it works, you need to check that the expression consists of just Builtin symbols tagged as Sympy symbols, or symbols without another definition.

@rocky
Copy link
Member

rocky commented Feb 20, 2022

A couple ideas here. In compiled code and functions symbols defined with local scope this isn't a problem.

I also don't see a reason why in SymPy (and NumPy, or mpmath) alternate representations can't be saved inside definitions of variables.

We may ultimately need some sort of garbage collection and symbol invalidation mechanism, but that would be tied to the same thing that has to happen in Mathics for its own symbol definitions.

@DUOLabs333
Copy link
Author

Ok, I know why mathics takes so long -- it does quite a bit of simplification on the integral, while Sympy by default doesn't. Due to this, I'm not sure that this is an issue that should be fixed.

@mmatera
Copy link
Contributor

mmatera commented Feb 20, 2022

The routine could be improved in several ways. If you have some idea to do that, I can help you to write tests and benchmarks.

@TiagoCavalcante
Copy link
Contributor

We may ultimately need some sort of garbage collection and symbol invalidation mechanism, but that would be tied to the same thing that has to happen in Mathics for its own symbol definitions.

@rocky IMHO that can be 100% done. That may be done without changing the whole codebase, but it can't be done the right way. By right I mean, an interpreter may be "converted" to a compiler in the down to wire, but it wouldn't be as clear as the code of other compilers because its creators studied to write an interpreter, not a compiler.

I believe that the current work in improving the documentation and changing names is giving anyone who reads the PRs the acknowledgment to make a compiler given the acknowledgment to make an interpreter. And so I'd like to thank you for your last PRs.

Conclusion: that can be done, but I believe it is better to finish the clean-up before.

@mmatera
Copy link
Contributor

mmatera commented Feb 21, 2022

I think most of the power of WL is that is an interpreted language, with a subset of instructions that can be compiled. If we want to achieve a performance comparable to sympy or even WMA, we need to use different strategies in different situations. The good news is that Python is quite flexible for this kind of things. In any case, having a cleaner, less entangled and better-documented code is going to be very useful for that purpose.

@mmatera
Copy link
Contributor

mmatera commented Feb 24, 2022

I will reopen this because this motivates some investigation about the slowness of the evaluation process. The long time that this evaluation takes seems not to be due to the integrate method itself, or the call to simplify, but to some sorting step in the evaluation f a sum.

@mmatera mmatera reopened this Feb 24, 2022
@mmatera
Copy link
Contributor

mmatera commented Feb 24, 2022

I run

mathics -e 'TraceEvaluation[Integrate[Integrate[20000*E^y/(1-x/2),{x,-5,0}],{y,-3,0}], ShowTimeBySteps->True]' > output.txt

and I find something interesting: until the line 1667 of the output.txt, we see that the evaluation takes 2.39 seconds.
Then, a trivial evaluation ( Evaluating System`Plus that produces again System`Plus) we pass to 31.44 seconds, and the total evaluation took 31.58 seconds. It means that during that trivial evaluation, Python used ~29 seconds doing internal stuff(garbage collection? allocating memory?). So it seems that the problem is not in the Integrate implementation.

output.txt

@DUOLabs333
Copy link
Author

Weird, TraceEvaluation doesn't work for me.

@mmatera
Copy link
Contributor

mmatera commented Feb 25, 2022

Does it works without the ShowTimeBySteps option? What is the output of ?? TraceEvaluation ?

@DUOLabs333
Copy link
Author

DUOLabs333 commented Feb 25, 2022

Yes, and

'TraceEvaluation[expr]'
           Evaluate expr and print each step of the evaluation.
       
       Attributes[TraceEvaluation] = {HoldAll, Protected}

@mmatera
Copy link
Contributor

mmatera commented Feb 25, 2022

I added the option recently, so probably you need to pull a more recent master branch to have the feature.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Feb 25, 2022

Yes, the slow down is here from_sympy(sympy_result), specifically SymbolPlus.

@DUOLabs333
Copy link
Author

Where is Symbol("Plus") defined?

@mmatera
Copy link
Contributor

mmatera commented Feb 25, 2022

Find it yourself: in a terminal, inside the Mathics-core folder,

grep -R 'class Plus(Builtin)' .

@DUOLabs333
Copy link
Author

Thanks -- I didn't know what it was called.

@DUOLabs333
Copy link
Author

I think the slow down is this: Integrate[20000*E^y/(1-x/2),{x,-5,0}] -- it expands out into the full result, so it takes a while to simplify.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Feb 25, 2022

Interestingly, Integrate[Integrate[20000*E^y/(1-x/2),{x,-5,0}],y] is quite fast.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Mar 5, 2022

Integrate[Integrate[20000*E^y/(x/2),{x,1,5}],{y,-3,0}] is quite fast, so for some reason, the 1+x/2 slows things down considerably, even though it shouldn't even matter, given that they simplify out into C*E^y anyways.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Mar 5, 2022

With my own tests, it seems something is wrong with Simplify: if I remove Simplify from Integrate, Integrate [20000*E^y/(x/2),{x,1,5}] gives me 40000 Log[5] E ^ y. However, Simplify[%] actually expands out the 40000Log[5] into Log[<some big number>]. This seems to be a sympy problem.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Mar 5, 2022

Now I figured out the main slowdown (the fact that Plus was shown to be the cause turned out to be a red herring): The operation that had to be simplified was

Long expression ``` (-3)) + log(834834849946771739398189689082073204583662289259068340276041649667354200727734589673389681513577713873539837848933190179606976144122997595128699535728320278363957406122229638224738204682669411866779189953562385625098534028963157002141434435766064825439570929370947171195530287038771512921939842790811789083206311869666759937072704838536276763097428217011154877394825632531526080279238373371205312548447782767838298575486298870535383071815816905267716282827942340209177487489616542641785326573561204967691349525621813444744533073930646993209513176515537307510149023800833155342703884114030938959633949908510767023180096372306557649408465912537367211595379915662483043235507977101788356671956599279720747997238316210743662374309339761734218815936341354427834322628332516860689459907665286685217536973037196834709641210799161120121559009259337164141972352666700287873579394398857672293064582766556355752729808807741162761544167365697639995849466890391238613929514863507508309787666794223027295562202862275754648771867110723850634646969154905137724169046882314810075051641044594164817382006618892661374655768663768555739939356511851339811663593178246737739116641708194217210081267725817283314256027290750039537836058030665889572922132965066226867555445774932826697567405895695230068732812067449364589837399626337876333391126334730049901130624431908511079626893714307378905516624473638952558189576265941755632412802953922911041766063191742035545093834592463843668548906917712033174285993689846003368734587523868522037128452702805658747575931430734414123821322037201966506929421129577503766822740755587867628433305265664860770706568931529435063745017051340779539187698665469417340597419942181431205452577945964206458742066332348094269972618355460624414162566158955663911188137237225256125414435762534954957383887584870588915727761716237645149266105159834394551887169556028769636466362768412336584061744406947883443532499844681874910258064425873297622668632845092726618853031103572483689248821562117603873950512156134075484807987068210522230674210112405441365515774557825875839327552337754697179412977834665151081997259014887660505288111131537012992829739218747641869809973949572244795480707901244648117471775608230044482104358935603490226477173854971087485088957765499545284183929835834731772524453600226690173029282359434171749779955308794550763843069392552689121407988719090504201319243054682869020148314540052019436613936295121280397125739033101441741981022480528836076813008876148457393828320789219040625750211306902603462842072472825344596955287406309148536699221958605252475938832273803194351657086257852162175421005566895059430431576340626958359569683749694916252288711591390131930442475721681533061576186986135527400873786184620439138050284087041011291416688479729430090823177545586305446391890233258159269865482733883229020467164937981492035158463297581785389018821648314816200605238302154036762847734221986575538062899070702612718306597008988936118757966374765891398676414888155553192230812817637505018124767100684902332601472975667589015603076386718718593200600343332209542845740290741841549150755891638941184296364635843338813650633175984163269056233901857777845296204354934723973622528206981029245555689575492499579926205174285938073912614508861421439198860755421550574547248563515503603056227605549413210492726937280675019965510776031294435942876843797471739417956390123004705267796744951287887708985328413436860844194260722779851869847116742844025516463762243294766557417205291348374889775418971078811983755385711854156379748356045490226042898218134782000690180279616711924726013481116098244991099551954521493062977099969612551199927789853994988638369065516996157508640019980905581084506361228035891830684915269084336403238135937061585101464389819681198999734399544575241613910897774769696551328599095818777996468783819310092432040330183275144590772001340824708868095710406810169627160368770683965208857079266599287978174830574632942458205080624648554246004569079537186496363449616680127158914282139568922449469880740227176580920546770531424990560296474578142642115353199245474818338261989044751690093523984262068102378318187400280847470488592053402871243086768576502918084436985264949162591649236048297190189969538233878471162183357233335031480315510014750060367977870437278142157964518520885053293050174678445235576159240754652663021113103951100828523464692970618269075961521725200668178788174581840102438242800102451205261366548662180084965660723918221920800437959028076088081065887573293756428263205977133826598614242703764439470972931947990472742090832929690332023571048468727712230567164831118114120102146720219131882703926380760112304778736874743187576305218061759875248024747810139612654564618111351364417002448809031462744901188885852680316095506593503031229482547155913528218181720862089287459153263798022989622964491852773122956268929553653960831749689145736210068748817495083326029017524265820204726445684208770545490558196998728258447197439730021658914984838212930798816727614236712252372224011887597332120102176035223769606869510295708857445728611287510273692687118686036289718945205497571634316359399335719309006458407195432257913355425750185995249897858628087871306739267118388739535920514356678195516201093833306727177572125284306548444556402131739999733434167703805365021136028471862888869011656390954632815933871543903382425252537143432138800838096334315643505345829956507173831230515547511703207534481987713708916691433318583073197054037003711134150647327000436875353049443287276939380936609935694990087780055044007046828237038088116175041961450259215428646440588293556042269558761561200777138228979009910864718564557291638631432452076936470879520526882218908600445108822038792284707522700092964440089440050926859530761148578502499649461563018307690918269232773113881653640290212174467265036246802845613799488532599049859408911884922275346273870602249560906331817601019895893983645670148675199896260092349107407275315085204869703097115936834503056649068683911090802624250067983551516330605095385196881299943352716165849245907329889348290176846803168775053476942412321036153340430312128794179190797206986106240032205515237433237781319873078211209227061581859442089420923245449450907461832300828255919413008988725931249875877879537369199017081092080563734673138239651345221208360779417132521935216017715230376031612387733571601077762618026337116874162545850811533062986926115681501116901488268027721886510851282425103632635334867335509031401343489019534939731102722856870393855451588832936160789625468040191043947867826429392745032872032946389628001865387150695671088691324840824643594663904535435538911448797599530475017241457769740611853273265665733790120609412832865718541589541460680039358036895863961309014100328136666499602609182603116344778221623638133396356738460708812467722297471211949297221393070071522651711201112149659611832915080147000704931810405478628424364865518014808083180531390942940632643755423003056529301615410621551306577061008376995669822843066903845300639007220510316387196496288808916809292521268104502471573770690011238119754568313972686634014241395494019455124764359852390728351891685191332061622690838868301094441295772062852614650964328314351001083778907451427027011306156307441829082301650505879118497938075423326307643326488520795418244149461006279886104501133392923085763992551169087326305281775108864253632820188530269846082142852405790102420788607626724080448781399729847748762835774867521829458261510041981998744410726134711309947861829448150805710963522932565139056637781502999125000625795929324623030437588649705172526463427591019962849558399135712177091433404196189533730377598483884320432679777735793935604675941140195222488981703337174970659602282095101005930931420592012883029667351344496336380000452778367382325469364006825866028265817442259474552569980913746349291709735023253623225275541006181098787138971883551363540898256826490766104157185256234946347867442541469178163973803043024765235193215262535640017447916265280851457434462510427794553174830991374528137244010763681729129468640496867239872005160383822816720445737397430601479965346347665446095695420846684395586272650757771641519127954737214369453987208936907912816439929246293665215746726405325343019576412440877244465621131067349766384359035688738074920227849611970318145541174254812627739773133420321052693387125336437589627726459846340244942480962478732510545431632280693412817799592058924549557028372493846715784087672240087062354137867598654255308878570483139940781466432194774457827116400771010691352979040456659095407494739808802812300389237456135543957627785949996414377786610285043214627043422845586076962702894980050301330001321519500003254345604750831556522726824774885788423917500946139228684993287429648397917772893169316348523593782440702394513050473376430831873947854018086920653932194690843143214905155557258691562955101274673125285580492351091337731147045367341106538755415004910405718562127341963186969839699341895757997181787658246103232043825625183950337821516387992061152199928103824269349143686458281619935723818228201346351743303773395501860936483499885238568163256286606199845475474204484828577961014753082868260238335170964886844155105391078483126738272951394476410326892493174862890670661895972267029062237939022203646033162528656165525333985716508791166589176005069287883659508081679715426731377598252233583675081592891477959677221979848596380466561256511398859882512111540779354848729793349205937007462765544962261151775658525293839860470659548965897101979212593722512811005378205244992579534687664208863680010006524696890329247456772207829279538175633579847020695575762349385095202334281291832170049112917785463494383209550176005507307638002027768029994756073877195587292949212046862057922152356743558496971673644344440680988356846693188644369902488128413519012922851165608803140172112412151426652593762304227704408196104217594617947854414845981587019407290802854336027617560163627254140967562512447886290807235956884661228187012045527879681687461005190891947704937262671500391867774060261703550528322552650292904003092738430854906216765874888193561296565405816951097979617268602919892738246906347362392443378139073734219251740783680809603760824676481600216810115473342718221754741998495499921024075923933860052724455267492402051272066049225438701998016538538477991598475740122299910602823304445297719954824754536848860109858236015261581203259922145067071126591076486578595419227280415044738948346112497298922326930104454976519401910466587946326310993677910933548025621324521780235717386418379563973753708088200134132794038955654167748028756960624115393039102299620391055480676172552410675577818314250691277312198659793210951908533451074369910601521273059689826201093485915989434491257420275702125961431827953927924157612217654543031768495760313487464607209787352418471132057544707490119769746758078815072988197509830119058023257712798046527945576368541253742844777961745867145849296187644025552102187654689955612589491390861848214877560801864944100041284596853047894666807012068113232797317915997933114987847206313050477603868029363895570178281387076652184384010861948109441483437669471224579305605640422256978159361826749610039308056487703503092007846715640544901987346905419611693465606000961277439987132856589023917194420302370876920160778674543772151434873826904130923694875198061430348886174400069269941264258642569320759807634150347908860552426746345131252106609089894380613773762294020371226733388289263432212596225584077380333880103380062520395986418108922804972986877294948790810470222382951747048466165549476891412487130702584772368860637360868813531703174559686615268712303774622846459322942478592555307273393194537957329560361238073936465035500560109447172767754370939071801187010487630192696351460785484644342769000617734277953384432457911459577767201469775271573494158036993263351956661333441856030312118777218311988376101079128286235179438416752375246976830956903108299309696162468459554398924499839540340606715858140687131142681443964342240049736092329525913775275098659467625707019067911675501098706983706411588131984451482591456174786976521107320791428199776995077785742498437465582583550291235832833013063564032200061503103296171970728845287803445174380912715491894818456725442516380922604893226434542797862580294284565941055926575321144609566726574717355559863883804816579661464121418333600799180347867492229178668388314504882677970191324191373167665507759049876461404617304268721508529356055771658693772537730773142851214169755322559753935248770814272825498170384504715716856026215713146974808164850273578861215745872216271863799913893683993722680333716746612961920689966038145528022531461120407195454211156572939964670764513241931841136123841622054692576300981556947904749558380920844112230012929171787664612225241048424529454049991024332325939879400802125607719202846974455000323373492931804110492005694674228281623606110975189173397799573576921808431531334429323536291469089494342948918491747283768336788062837085834467188439384216691158378946975380365187169197302576589300782112660835082406800480253160224799313088264840661745522501596948833265966013218022521253848160550248669310838002729204539286496406642356168415448168820151201365081266632761080782172942374335931948275384302711844923777753696893926734710321665871787526645864794367725272171172175571730577357343541060574127880088748332421578698078177531263529425002528078448409255635724083927346183213522134962602219217620331074414314221870134399702659652175157293283880836340352092212473763215407302701107499953860854109606599771927446658857379857275520830643159338127987703801418110639826601286991406480921847362304439546999283548557012437971867785025621470382702322298443333919554654895613970945176797017140542333924173182586192371792889231191934316410960734509992110832907473525412364472694785443796815542686752863446601087947165465723365209367013546763425130928668969003898865933332136345784176067809220957449778424139538789121381522240340022523435206780651155995001582516902521566280306662383944524033961706286113312232522759269192176181244717980156499951836292228214015277158039287700597918149569676079029345065386161844488842070405654556159045191889837482193690525316846001108011796438631828988945539104326290170986029502058639412716790709207249596260917118017181300646627261400070944924062756631804056303485542454931824991395393221991156130197162058766939650775091194515788465236100335754627470915724937753694277591917327547706434723979598536078820148523319336082004281231772172250863451083867780434974200582161855075399880526612156113797807436304445311610643931308988152099777125072202047740708001315525766706304168898201163517826326637061123862518162018075349581014467746450009957857627852547981204497125364555626883612714224913382919740317132665554500802068866073045198520542188000867367475145800781967635135360374744815699704310391908423066167509836757459570007866564015891161308532204236219611089519460214349770724321606415282155062271931393466470892961198095263580627472166769907681075384340125310736664889046230387042254641822023633104074806462204614084110867363662664350892792227148967482686992702559809326052679973041635133855658598161965731965227205849394692582617589417871251800344632486777911088612504555753404482492187580443355904507325036261446517702843799106958629964211111105238220571108169461073586427810733717940664869368141765893519249239655364170921280455126743819653677829295267575880012334758606521508979643015773694423876886909468006167507634897089121336184246966062433835808240782523410014959546887697365746994152325525516000967124482762200633147366005268650004371818632852344488784776352074071951694762936821258384130195998815079294521823457924543407256480492333469071769655654844289468947378609879828845854359764855361367781070380751079160978733133323470731276293816445824265354221344789316314920421748979330695481096806092536894353795950275554745707230111387758108645509622134124911557372288578183641156300580033547056513668863386285868821505405927683328624508220831286203206821299379733820438378885168246377525114839043697031237395695303975953378295475413217002197195803899567607738922171258035884273352586100532905787971496348965841115689730315645337435297449434136677148032520805363715535852727502908250333740841629842891274045747654503561293372786975932108642112713235956371355038087125073857419979049510918131201484615055899185125612577044969115796731702061580174268892914408379012120103565221491387797897055880886713275375458563970202077424476883399612110190579858301544894044240254365520225981124678820249790404908362520219864506305864129960357562262955550030490124987606919155185909637088606300985288582178250863115724394458378806226446982124610146746403067925529436574788364820689441868002338382082329352238834373322914764054203405927851476755310812731308885391624284638792673425545514294247179367374050446076604545662049899666447550493100947453182884372049139451268307398159664568878018839882396362236966927014253296968307793188078081455450453849251797688350222709422203510340027359438891707474849270996045394445664237058245980021053770759490306070148693359319321157780763984081651476305572767678768876848729296082705556358768792933761943203573746472561675462159368214079437883703963300328761690534257499653510915956984700290914179917596495469014522717794426453564377660858911012461041610190187789592849360034378856778677364116575890590411441474069537831885098581783822177111610253328088162740140437879999948779921425447064446037397019607642427515027569015131388615388050373585673332293043505834911141951176314891811077771326172340137828086584690990750095972076966271090830456093141634553755003365469884620869522734973823760484041084420562297465058951688260741860709278723793613936144425356659758595289197579979668577708072420961126394637178014227273790093112863567048683257871943820823903469736032588034925068832835953937340350294759072411424198182443932385155982150605463917611156576234560280748811532771691714869948539984501964001535204491508907114112697036847903301300986907250374158212370497216269627985952359469776044467955545917675515361200156050901621905356548482368867064501617370055306394319695485360224301610158259375332333283809684335043661770011113381921529585268257812710822018327168600303099702461489551023460680626461272971443510818721860092976492545974541082728798719399662682774375352031945965955402294774725215230636710166342020884516457371785154956315872166643739524827070402936384471487734164676227889089339254919194448547270927471927491105725591847477561533831269447924360242501076980796542233088684340380239388967288001264009995796915885215315198291644698149214311862751367578598069969798834622188165630512837093913205574613252462130797460997645611823719404625255139173167184993571947267520255455728723881058064947197733206300590027099715518636967659937808364074546708363274974393265337509993458951697322418520297837854418242213226240414880523042055275173260084218250829185546344061096130990285053345730177482218765356164484628592613592964632463688110819369898839371676303887009117738107818358553409813019311738296324837171952721719602148561845057606104678577545316851083825745061546218049709473373670614957530655953780277032918064961346498735007730247187733878809332441420788174947513070383229255569511581632816834750859193014425758816238716074120933234779944713822140932597376211125291379849079458415094571021419429410067924093025849108006397593446526090879408891577806254033109451872429523814412059897895395379851920988471382040847190200798320196473692607801137263696011327207899393157951615860714123247432290322464259686405111709487514490490522651566823641751061543772804286335648283975821542820121798635053403845346812089457173561765479019398861345618293292702745055591036813864025866984289477668899189013574001240466943036636476852637683136270132260609775714193920215361446533934488181142513751844563627588599090988327067123673411349330852596435563026304195470641310148363003272040896533820786456837684305794305518287856832380836391220964824361094136195335101543974639290233897886662062693700000439640310385071120182106168128708213230388093358635946671442556312604361067878846191886723512588776435378249715052581644962252774180762237560762018827447921044406330329536833748139615864706719209434278254489343874549680712495536653242718052009493524281708312348989530203069688221047479722599144230128573798025192577300353014483549465016098323290609985515915433904437969178197426120144086035383243215518578254974967699788634601724283912879503567984547906706329143345983106207473354573949277610001567065481815974573447034218862636728030334417145085767805282886447426811962047253872138819833323029174244326032095763655632345504684847633802312006345454710486344982474421901309153339768100372643320714833959379464884559521607134448406036794918621790181241004534512203297335652409737954198378702787511577754903990739022101981215723457397233545691115419011936207885011989588548114637144307937170412147890345542918543032207909184041254867730733884797998793872083679631773839397937913360562248339977294790248219834832168713752794920358024739472746507096934950239307045957325144858277667753450491338740871646938233534996183082280863828523512013130953436709931621247643859124366692233951248055549059768612617291388497666829253186516226169855262150546945519074825813126234090135107766135647440709902341304058188126163933876924888924304522316923615874915005607948649139864283179493285784601696033520418553076784719566801103231126277631883554340737409046063553296334841293977267062724291325684987422314451604139196795148288347442315655667719171787113789847284309404110419139141944263527718812013274721798694415390283221942433302006350042925053445327309326854565812401994836339472230757029898296414398785288624193216923749754007112465561393432856918635866260024097471181805928940498853016192332969043367449842626753441079854016750755746049084535949118585281291089500872949555250713666991082075255986743032542642560241290233870892511137691145437155337043392248841530162756385391108411410984382317707551863234858945488675742013735311014245399195123934293490792611988102549915049324427259717627020554545057246262375093217581714682983322288239719899234438195297925145482981123542236754488425513366197856717525885133933227967710695647479148384458258068852427217624977773294323901314124526075243021651075084391099452329774194529952710563865686360792859502106607281604748030620260929374458850748884184137670121169716964217217093449168932363710012632052718729021238189265331896218174256221572429535958339837888384994452756886734956633186304288486221067692596137080816325938693402689916106016196937438570772893198072574311618897786085207242948659522720417586210578249931495802056282570408395881220552858212617758655432564671637645489922188991919491804625353727020439996208731166129538600389490288740334869009485435380911383000864873423969853559816390269123378811043224912321713498126960793368690947860435710723504238851021352845508175275440782784424931874253652040502642715102352115940464185200893932915997358177049599590942579048552164931929259062444245685041130677390354018460941898209325555967581746757856718612302871419991598160542319105712024977194700461890295808150655717948406606509789284923442722009071476925246865702301660375906271900471808104268964884409876485988302843841219371772645323778140019520639288229636079980684649723147593723277309309042848300352742668917630929832829047676231600945002792149157851454579234786470811217679437463557989681750107092118364446629059712327398079835551914972535579980205646285647188548231887023414864682105021429822680296951627999392731047208218938578485969180229675231524981723493981210478585010321427445459325292762108772693293393939341643940738692084867123494677992996138975592216395633962537126492471178529185266684483418871288563326841907482741134090051438312514395306707065157843179819711471005573907917508825023252579088911695466410515380421616081407930868090369533416865133362343751382500342907917438020657257636570857258826617533288631990191484289118318145720923415819156414809381160923065580381157008804616364481910892386745969445366075505118498806571195998142967561588838264048189602082153571173058761164891540501596276464366189884216983953806937797574099738652742791253051636186482841219842646292517690540575368335207752615099133205203815205495807345220460927932466485559727161970882798114520899885050934154149725523886565413402560744658316364502077859076011538618211486635311087752408131437754349602227030281502242129716493066734125782828699026203029114155558054604698998457317694120934582011318112706885678757429078673343135010081271983851275784347391697300843217022927982297241981795136087570134925502202356208769839583457511324658454000857788452281270798689530801682159495650018728727282993762299294900729222193156410840130410349499698650983191289676220610306328842191343498693875765631145577307148859828864231447115953265774281342289225438372714813914486849265827651014226924689228342862484871653480035542486412160649915584890240804767672988193848307671354314517382001239095795396403199585027801346173005837979554064942771649581118955726887478942549986255074204356153214901457872899914210918341875986503758779948170620236018614371555787480228363398925652720599614184882510631653352152428269239613618724241615288558417100585310769594068148814179999574573415442777473929387210187008614112313002811452141511981985836201456136605306184881727224269182077214619834909999715348219187408060081565681895846179698940065064235398338725702695488133735704211138442007515751421372579028964048359138467868659584880270445057676787868040932807421057606034973205834259958565393144638588276469530642563424727702223924951205570348995787305851024179207143460766218801722000546459387449051162379366838273966812383267978973318368249537078870726245827063748141918119839780220567157800663968227037135349717169493922072968281027409348319230615053310224946313964878721872805816654584088253455052354283000789448539267021068428339546169464102305708373517750831289262642169792560675529743344521291971081991670283213368356686159953821033092039914523535147973834509381200978872966115409856754103674230983192817898578463431291799900674784619212819224937269465055417846745120507455032918813154367585393615960222439288274706215338446435006791150194189066811491962740205048187143206791922741961391916300646154218523864569824770791944183194423207635658459421803148567113680223556886341211449374863222682326478252832598060221707891415507104266025324428001410316009450464742349090358436320347155651234541392846267056752901578478408920979832578576766235374494034116734666779268710332528284158583152455534158391183837387439823236889379906561855488594605430390253507689030735581462361816643131638474942283664278657995855817272638113346365166766821784858697961482674443730544935084804450449587397764796887138697588704780552310759145848565562424953982984808722281431392805417240399050361959021245325898682173878362555395361854676728700760417227613093849101269191218183834810212396437127627432976933856674344974685327236841546418481772399147646096120567989829124359824089017348870140625934324640676341675960805084613506485998753736244570706535208932087963245533046694209394365833441970002866356193604078435472261234748357236324857871065843922363307268936392813964760372052517246673310108436719560435599225219884843141646985317241148779800139810180259770923541528265386526925778713530649367478973657388398520578610823354189877459004031548069831508137383669739362363598224103376493916663510265639963468846968058250625017336116039975286177749311265372197237243176573664437659848133218732746739000939430066970561419455143583118521604600627812674729275154961607479327241913118299276835795824955395642226225236714564613809427934044323793422380390627381401235825342141836689222490068563529766461537184177258210328910382312392136502595894302463709632506729201702190083228397451119133055001453396959807179808790996550447239478478707078676254257138289519684796004781320685023581759975414183530699880585804110499878010738753490558935592571068694069772572461146518318300996412163297204116367188267131509394844117142584771738325433154848071416503154097581410415487786297980754501154021255122301278168955033470783877786717646357787329251382455311960344635805452140075647572411059393295929395347526127856419424654394435737785755903084334779800210766296831998558403836576621305379204909534968447090824386121114669582411153730995637519015407634428135045794744301326391058888756714905237919377114182130337360216714124443462717281342827571509339888076021651319608155220846261077955236686607883944107826953131508522310503534013690732191391183659181881919494591400131239965733007997031355612749170641343293946547305988625165823886132101016724233466448665501942907506364105075392358420999508099212726380479443238831464334048744541744123697523036587752350999378283587534405652096401628229706564731781744839469030248805427547626411092228608802808009318697236234322312763400804891699957072086114789963273114724346196721056935044282520415089005126978524600051570420300236366147560229808217011304396774158737841166870749597168510617625621507633738986061995940499605504161789846877190489807167749415465580724707755567233733610939151482054727912168107420689073683754618449217270989318354315064275453844324298178044057593111528924984340357061024061847782018796687188373425445604287392979718464010627499048614842261517929929684393922154594500356792999640688791399564882723477040921071089220952894468675475300473165987791010085066779322981889369084048598020805955125764391177319403904628603553268356523830284113968657191339787208811229357326385672253792804666069914623174979641059539451668888803733219202723360406669377651647950965705708761093417987220939994068457103636439119382310119677744381427797771480658735529827486087439014951010235829445791114966970170870142482139175667150370476710497855344604855246565682209831540393906927315462213695080325726835827247504174469725472309856625811449244327182272688905430716421794626291035262229249258195816993713488091295361897165031325796965028506770180536837493003257526156627701714979535006446548231994591145412896014995320704198922964821876337272471129759457605491997158735422136413662250569015076996604687830624695170253313413797196610237765913217337009208455404365610626260231977815698110809416378717544253835569180972277282997915851889317082876404992500159721046314058475566318411200477635841751372928248751611523045693169347072459739771274692265862084504869805475725142353439006808483216884882408232853283197368272827084816674333291761712550696756254473879813664536772820808941183264552911822506981936943516194542933266176680619774324330414127793662315949140916009990716109013268176639854009557775350119838286508176756695486717564690996357698769562248418718773577806133092459504812505378804013002357071178142398016908271467265669726136881643365277964198170886308985404930739874776334721710427612009787834667304107416132542551164809578747001176448726725355688933103099861645927064956952104383361043057358079950249766592780115033916219091789337674440005761207438179289494219038067425483511132578371132236357762608031189229831835062790045997372158625794219620943010015991103073896560544229631631811190131637969634007933852948814601269126854106656041661345158050182472393672167631441922564078271634250807698774244195965792797352613460937807711172188049586139823482183431072626191922573693680914148167229478505916750415112923077768028884989576805430632493566752348131906379668228378938898220170424834840536633565123507836665107011790272719444091003677202063474814339096653275978078084191851053893861345096434280109968904462860520084889270810281309775297953089063236201717057521671328858153944915285377059393338099633088644301313911641474237452465011141868185963714658433755203645985297028882513305137798347217792595168797283705887522481067434289142349274653916267605219643728184871592700504499491458018322200148366557609352375429183931279920886694090255866648605996573335741480180506363983495844282815295978490166163955525015563729855343303814671981034111427730775686088540100854013390248524697890125381876049643995109914280851555521587320629832422343413659094574927188724215417968916554599799787360344628916217669670358579193317028267590826166205541690974670644290843630730434812943715995412986095660557948079321305191963339303278785481111940831653504257365246172348716127540310333164984637649996944367121994836707976926096726786745123949788218580149397834916332145078104966169963098403798653709959526648809285766026070712966248351368680388738929917743410205685761365599616815315853057289740897166313730580912630197821936556242641143096775029172469720762892504477699674192708299380302535250945562186985977359512674790963025120607227778057714206289136124800367508516051400965412994943348621460331837376059605189650407960010568851174149449639345326408181429204501290138809269221407139757668278988052768673919650897844484178079892504156330769592746864701943526817566054742362853317466575342872752333433007794219343761511737429462333778233508501742337006837032182364154921602523640749049548353118897293343601391992584878041437348142075331974838369150043166514310018943466489939510164550218385509643282959728834601253684148527996482597403128404964301397802025234108888549896973364274951055921411904925974771769252411284497060141738295098794015876255394703090024275126203266221960608267379960294463992136414378402356006467165269169408584679033633565575399761988888857465231191400578374467052674881644689498252451355541803103037925219652849010155636322580804875851331510016888631634485795529738385146552589629718193200267792325228834709461950309997670383110224074677453670158670051224000001) ```

This takes a very long time, probably as sympy tries to figure out the best way to simplify it. This seems to be a sympy problem though.

@DUOLabs333
Copy link
Author

Here's the relevant issue.

@mmatera
Copy link
Contributor

mmatera commented Mar 7, 2022

Here's the relevant issue.

This is interesting because it allows the implementation of the Simplify option ComplexityFunction which is available in WMA. In any case, it seems that the default complexity function is not good for this context.

@DUOLabs333
Copy link
Author

DUOLabs333 commented Mar 7, 2022

I've edited the count_ops function, and fixed the slowdown problem. However, since this changes the form of the output, I'm not sure whether I should submit a PR, as some tests in other parts may fail (though they shouldn't, probably).

@oscargus
Copy link

oscargus commented Mar 7, 2022

My guess is that we won't modify the count_op function (as it is well defined). It may be interesting with an alternative complexity function though since the whole simplification is a heuristics anyway.

@rocky
Copy link
Member

rocky commented Mar 7, 2022

I would warn against rushing to any decision on how to address this until

  • possibilities are discussed - I guess we are doing that here. Right now I see changing the default behavior proposed or hooking into Assumptions using a ComplexityFunction.
  • we understand and can describe how we think Mathematica works here, possibly what works well there (there are a number of examples where we are currently lacking) and
  • come up with a suite of examples or test cases by which we can judge and benchmark results.

One thing that I have been extremely impressed by is often we can find examples of shallow thinking of the kind "I have a problem here, I mess around with code and it fixes my problem here" without bothering to understand either the problem too deeply or why certain seemingly complexities are there in the code or exist in the problem.

(It is true that decisions as to why complexities were added often aren't documented so ripping out code is a natural thing to want to do.)

I've come across this kind of shallow thinking recently when someone added a "default" implementations to a virtual class so that subclasses don't have to think about implementing it. The default implementation does the most pessimistic thing possible, so now this contributes to slowdowns, because it "saves" someone the effort of having to understand what the undocumented function does let alone what the correct way to implement it in a subclass is.

@oscargus thanks for your responses. It does feel like Mathics should have a good handle at a high level as to what's going on and should be able to come up with a complexity function. I looked at https://docs.sympy.org/latest/modules/simplify/simplify.html which is helpful and then looked at the source which is easily found from that. In the docs somewhere I think it would be useful to list the kinds of heuristics used e.g.

Feature looked for in simplification (roughly in this order):

  • expression contains a KroneckerDelta
  • expression contains a BesselBase
  • expression contains a TrigonometricFunction or HyperbolicFunction
  • expression contains a log
  • expression contains a CombinatorialFunction, or gamma
  • expression contains a Sum
  • expression contains an Integral
  • expression contains a Product
  • expression contains a Quantity
    ...

@rocky
Copy link
Member

rocky commented Mar 7, 2022

I also would like to note that Mathematica does indeed have a ComplexityFunction, so that suggests that this is the direction to go in.

And the LeafCount complexity function is similar to count_ops.

@mmatera
Copy link
Contributor

mmatera commented Mar 8, 2022

I also would like to note that Mathematica does indeed have a ComplexityFunction, so that suggests that this is the direction to go in.

And the LeafCount complexity function is similar to count_ops.

@rocky The default ComplexityFunction does not behave exactly like LeafCount, because it assigns to numbers a complexity value depending on the size of their decimal representation. In #188 I have implemented a complexity function that mimics this behavior.

@mmatera
Copy link
Contributor

mmatera commented Mar 8, 2022

Using #188, still we have a bottleneck at some point, but at least the output is cleaner.

@rocky
Copy link
Member

rocky commented Mar 8, 2022

@rocky The default ComplexityFunction does not behave exactly like LeafCount, because it assigns to numbers a complexity value depending on the size of their decimal representation. In #188 I have implemented a complexity function that mimics this behavior.

Yeah, I sort of realized that which is why I wrote "similar" rather than same. But maybe count_ops could be turned into another kind of complexity function, since SymPy has that built in.

@mmatera
Copy link
Contributor

mmatera commented Mar 8, 2022

@rocky, The point is that I couldn't find the specification for the default ComplexFunction in WMA. I just mimic the behavior in some simple cases.

@rocky
Copy link
Member

rocky commented Mar 8, 2022

Sure that makes sense. My point is that since this is a knob that SymPy offers we may as well make use of it. Presumably it was added because it we felt useful in some cases, and people familiar with SymPy may want to use it.

Also another ComplexityFunction might be "SymPy" for whatever it is that SymPy does by default.

@oscargus
Copy link

oscargus commented Mar 8, 2022

In the docs somewhere I think it would be useful to list the kinds of heuristics used e.g.

It would indeed! However, I do not think that anyone really knows all the steps (and what each step actually does)... But definitely, some sort of description would be helpful. I'll open an issue for that. (I think that often people have just tried the "best" order of stuff. Some are more or less obvious, but other times it can be beneficial to run things twice as something has simplified later in the flow that was beneficial to an earlier step.)

@mmatera
Copy link
Contributor

mmatera commented Mar 8, 2022

In the docs somewhere I think it would be useful to list the kinds of heuristics used e.g.

It would indeed! However, I do not think that anyone really knows all the steps (and what each step actually does)... But definitely, some sort of description would be helpful. I'll open an issue for that. (I think that often people have just tried the "best" order of stuff. Some are more or less obvious, but other times it can be beneficial to run things twice as something has simplified later in the flow that was beneficial to an earlier step.)

Indeed, is useful to get the behaviour in WMA: by default, Sympy tries to simplify expressions reducing the number of atoms involved in an expression, while WMA tries to simplify for "reading". For example,
Log[1267650600228229401496703205376] is good for a computer, but for reading, it is much clearer to say
100 Log[2].

@mmatera
Copy link
Contributor

mmatera commented Mar 8, 2022

Regarding the documentation, by now Mathics only has a small number of simplification rules. Most of the simplification work is done by the symplify method from Sympy.

@rocky
Copy link
Member

rocky commented Mar 8, 2022

Regarding the documentation, by now Mathics only has a small number of simplification rules. Most of the simplification work is done by the symplify method from Sympy.

Sure. Documentation is there to try to be helpful and show off what we have. Although SymPy is doing all the heavy lifting, that doesn't absolve us of the responsibility to explain what kinds of things are possible laying out categories of things.

In our documentation, we could link to SymPy documentation for simplify (although the linking mechanism in Django is not great). However I think it also preferable to give interesting example as is done in https://reference.wolfram.com/language/ref/Simplify.html

@mmatera
Copy link
Contributor

mmatera commented Mar 8, 2022

Yep. However, the examples that came to my mind are not well covered by sympy. The next time I have time I am going to look at possible good examples.

@rocky
Copy link
Member

rocky commented Mar 8, 2022

From https://docs.sympy.org/latest/tutorial/simplification.html

Simplify[Gamma[x] / Gamma[x - 2]]

The other places where I would expect simplification is suggested in the SymPy code and listed above:

  • expression contains a KroneckerDelta
  • expression contains a BesselBase
  • expression contains a HyperbolicFunction

@rocky
Copy link
Member

rocky commented Mar 8, 2022

In the docs somewhere I think it would be useful to list the kinds of heuristics used e.g.

It would indeed! .. but other times it can be beneficial to run things twice as something has simplified later in the flow that was beneficial to an earlier step.)

That kind of thing sounds like something Mathics could wrap in a ComplexityFunction.

So much that could be done, so few people and little time to do and so many other things needing attending.

@DUOLabs333
Copy link
Author

Doesn't this under Properties & Relations show what function is used?

@axkr
Copy link

axkr commented May 25, 2022

The point is that I couldn't find the specification for the default ComplexFunction in WMA. I just mimic the behavior in some simple cases.

image

@DUOLabs333
Copy link
Author

DUOLabs333 commented May 31, 2022

Running Simplify[Integrate[Integrate[20000*E^y/(1-x/2),{x,-5,0}],{y,-3,0}]+Integrate[Integrate[20000*E^y/(1+x/2),{x,0,5}],{y,-3,0}],Co mplexityFunction->SimplifyCount] gets me https://pastebin.com/k4TAkKpF. It's an OverflowError, which is weird as running SimplifyCount works well (if a bit slow).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants