Symbolic sqrt(2) and sin(pi)

I asked this as a genuine question but looking at Symbolics.jl more closely I get the impression that the answer is that there is no way to do this even for sqrt(2). If that’s the case then this is something very different from other CAS…

Am I misunderstanding this? How can sqrt(2) be represented exactly?

4 Likes

Maybe there’s a missing layer at the moment, something like this?

SymbolicCall(SymbolicValue(sqrt), SymbolicValue(2))

or maybe

SymbolicCall(SymbolicValue(:sqrt), SymbolicValue(:2))

One response would be that it’s just :(sqrt(2)) but that seems different.

1 Like

I don’t understand enough about Julia to know what this means but I’ll take it that there is no good way to represent sqrt(2) as an exact expression. That seems like a major problem to me.

1 Like

:(sqrt(2)) is equivalent to Meta.parse("sqrt(2)"), which is like ast.parse("sqrt(2)") in Python.

Is that good enough? Or do we need a more semantic representation (not just a syntax tree)?

Okay so:

julia> using Symbolics
[ Info: Precompiling Symbolics [0c5d862f-8b57-4792-8d23-62f2024744c7]

julia> Term(sqrt, [2])
ERROR: UndefVarError: Term not defined
Stacktrace:
 [1] top-level scope at REPL[1]:1

julia> Symbolics.Term(sqrt,[2])
sqrt(2)

Am I supposed to spell the using differently to make Term accessible (apart from just using Symbolics: Term)?

It seems that Term works more or less as I would expect for element:

julia> expr = Term(sqrt,[2])
sqrt(2)

julia> expr * expr
sqrt(2)^2

julia> expr * expr + 1
1 + sqrt(2)^2

julia> @syms x
(x,)

julia> expr2 = x + expr
x + sqrt(2)

However if I now substitute an exact value for x I get a floating-point result:

julia> substitute(expr2, Dict([x => 0]))
1.4142135623730951

How can I:

  1. Explicitly request for an expression like sqrt(2) to evaluate to an approximate floating point result with some specified precision/digits
  2. Prevent the numerical evaluation when performing an exact substitution.
  3. Evaluate something like sin(pi) exactly.

(Feel free to point to any docs that explain this but I’ve skimmed them and I don’t see these things explained or any mention of Term.)

4 Likes

It seems I was looking in the wrong place because this is explained in the SymbolicUtils docs:
https://symbolicutils.juliasymbolics.org/api/

I guess that the answer here is to use toexpr although I don’t see how to change the accuracy:

julia> expr
sqrt(2)

julia> toexpr(expr)
:((sqrt)(2))

julia> eval(toexpr(expr))
1.4142135623730951

How would I compute that with higher precision? More precisely how would I request a result with a given precision? SymPy’s evalf can compute the result accurate to any requested precision (not the same as computing using a given precision). It is slow because of this. The equivalent of toexpr is not really evalf but lambdify which is used to make functions that can be called with numpy etc. Is there an equivalent of evalf that can do things like this:

In [18]: ((10**100 * pi) % 1).evalf(20)
Out[18]: 0.82148086513282306647

I doesn’t look like symbolics actually supports any exact evaluation. It seems to be more of a floating point library with symbols. Note that this is very different from what sympy does e.g.:

In [20]: sin(10**10000*pi)
Out[20]: 0

With Julia I get:

julia> sin(BigInt(10)^10000*pi)
0.9242897329862240904757015163414326685882171473043200522236191072809156717509005

That’s not using Symbolics but I don’t immediately see any way to get Symbolics to compute something like that exactly (without using Term and writing my own rules). This is a core feature that would typically be expected in any general purpose CAS. I realise that Symbolics is still young but I’m asking about this because I want to understand what the intended design is for how these things would be handled. Note that the evaluation architecture is a significant reason for the slowness of SymPy for some operations. Many things in SymPy could be much faster if this was changed (but it’s a lot of work and not backwards compatible etc.).

Looking at the SymbolicsUtils docs it seems that it is based on the same architecture as SymPy’s Basic class. It also looks like the same fundamental design flaws have been copied over. I can use Term to prevent evaluation when constructing an expression just like in SymPy I can use evaluate=False. It doesn’t really work though because any operation (e.g. substitute) that rebuilds the expression tree will then cause it to evaluate. Actually the way this is implemented in SymbolicUtils might be even more problematic because it seems to default to fixed-precision floating point even for exact expressions. If I’m understanding this correctly then I strongly recommend taking a different approach where evaluation is something that only happens at the explicit request of the user.

6 Likes

To expand on this, if you only want up to a given number of digits, you can use e.g. ArbNumerics and it will just compose:

julia> using Symbolics: Term, toexpr                         
                                                             
julia> using ArbNumerics                                     
                                                             
julia> ex = Term(sqrt, [ArbFloat(2; digits=60)])             
sqrt(2.0)                                                    
                                                             
julia> eval(Symbolics.toexpr(ex))                            
1.41421356237309504880168872420969807856967187537694807317668
                                                          
julia> ex = Term(sqrt, [ArbFloat(2; digits=31)])             
sqrt(2.0)                                                    
                                                             
julia> eval(Symbolics.toexpr(ex))                            
1.41421356237309504880168872421                              

That’s the power julia enables here - as far as I’m aware, Symbolics.jl does not have any code specific to ArbNumerics.jl, yet it just works with custom types that have functions like sqrt defined on them.

7 Likes

Yes, exactly. This kind of handling of floating point number differences isn’t the domain of a CAS. If someone how ended up there, there’s either a design error in the language that is being used or the CAS. Symbolics.jl just lets you use anything from the entire Julia programming language, there’s nothing hardcoded for ArbNumerics.jl or MPFR BigFloats here. You can guess how it will act because it just acts like Julia, so use the numbers you want in the way that you want. We plan to add tracer options in the future which can change default capture types, but that is just sugar.

BTW, the sin(pi) thing is a Julia Base issue:

julia> @which sin(pi)
sin(x::Real) in Base.Math at math.jl:404

that is unrelated to Symbolics.jl. IMO there is a missing irrational dispatch for that.

3 Likes

Replacing sin(x*pi) with sinpi(x) would help?

julia> setprecision(100_000)
100000

julia> sin(BigInt(10)^10000*pi)
6.2291952554614160595714553377768776304733861578927208509155785804589737175845776190271243662928273254627758471990538908896437289209144688944856633452612543512586041464286920381009994890655242697807378198746724448339355760541377587435337632005819199942175343771025178135082185942395662403008358550987699025777846466218629948018723355155266607146778809161453859061478492928115116456804932875211966928124714147002250783861894569764811038772366436395853243192131847235452839090556579624664377241245295931322812035833160707211136786611955973417028541367235387920728883219471790339850530208889643643772699170849247200257950461599583075813091298219011226984228746483063997112674542690974323109140297893980768779558563706239891287353195528406674553585152328731242200480286576982687792792034655747398327268738687274256887181965202284905305321896576541911716048266554364436149377329885281518128154132723429182375251350731341642606386143847098871822927361544804831641561341822397134592609924841543245800728073700999399883316429250239616760682455936947707143946936340830802570484232943686591558802285929042308606087200317185022514859949027504555813240853542280313119231000729420360638918632242889336112163332014222768713946607913119366189561334739073292364944061830663732522909188007231228049097729404110196895495977971723744708881355162967856548617995748493762086746317731261139185070461147719058497057507451360168240144732552279219161932916653023681426772394224689683200598605665410706512726367866965947648360674365770548505092671580883056715970668812378089566687605481448799597161922729835143662387338158333094147527935563045236974693752577693487437173281256746330874060138699025008822606290356126843101248420125196379864789342101334108796233625340716503370269849634203643703247201002182623046474515872162737921856167043154374756521786678610974959394726962521261918200136868868298997481862699837384966528574800135217228805448984847283055542541503053525604079701859658229227637636784542243283732924428737569554963784645342460494865693810975019602666157698843845782602351851951008053001524357814458310815104457181293607359756042407928513926640752114172839912126294951209798189116061244409060132150043796047181064811755814606329162778263726127924495759522751486966146427952773241299615071357645093528661497214741390959561691602173024208828099598728176965875628937900848599897196804219732182730203147595987172968910100660584573756505679763562902900807275793175078325763960878129582136948750887376611244677845949477064161276606263909817256103792095742066935991990736552671568309123001513527654052833239743439696384733803746119355381303479521987314265580478637275496331242455271534221527827238653314226351011581064118803819923096598499638581130877438028626702055610706259055847112866077548672925689229574083680846460022471602814721029659241237469962087857616542063755544408296428029010762548214833359346891256531783652355791091857327971018687793015867170146143805507228944067909657741628845136262159848279866274943386391423057285225515246533257501877813752467690643727668690990304857097659494765712863270992351617887143791839672598213307644745643807473079664717659068956049134219557960325135036581020919801695721067437004386096970007814087817226304156359703532285836542148848594855280029700584282954377167985254072342595390374630432552769167361960507030871756206141685759755944794955073893315680111878825808242136949080855513206453108777246646037289217688902308371485265693385677254857959703729369440945669091342072244603141149420989897968404248942705577239647482434292537211727182366263807452578526819623661495243410197640431311930209072536500900756933593679902413144474475663818512418427870855442073704896197263179946823895429385076651174244197185938621563031494723757769617045620686339946852354601969261250931344041431336073944327829676635177256831841159598518965698822489266304206193822496818718072698874126869249810054916543305153522575111539626567017803311766461941876653768307613595214738915114403170233741267837848279621711527459365764063817861003102648485777846338996048657129686509135678005856566246935100739566098120437336627841405897255628666668246432645758153394410768687270159215155586256298259017742949136716939680271490381839227834527963041422351532206000378161449100095004218547822087496093603428011079413968022543490564504974137096183234868523990755820142003967057212626945957582884201120827300442985720760760211834306345190744789596074945484918657094632947905942092226111253114356348859208255543456340456331588254138838556999834074136150305450191446602737193292966827090664864408547030357367904356636653566287170982065693496060752686480777671432560014561713042057349882609198250302402117238908473488556406961441986089667017781456559368246803225187669240495810241612307387312804089864316272624055158518451196065114449945598891981287922837650184210255407082019595790217789290621789291895046580415227614695173480352777724508253174940870341238677319617947166139975667512713733394076423572074294429754937196826341543805745826343164368176675956222203688260104285673477094097300493234401701285691161859293996134143331239093147168768614155753341979502370390771885754166510286681728222640313582945995552486769933941673168366086547131670563710663563862656517132977888257297118728283078513807515613908695707140696288070734390653441437685103274183965651043770575265429426604498558423503958376748673927947875089004094823351527865640803735878367870923465337162434708788614955593291551520749555186476590022367271114165841594202640913873713125759072694168695433524407041651040142514295331759671583916142803258629252543853668822410146675739503244328140183846787569253636181966609321888973927162208702787139045405435857140293310752159236154956352350241041780351188731114121565202474832328100005306558590706186277381936199679663351917009450767972615051322630795081163782078604248366805417739602410958799638275996740384175916774468343376938079917161743989722753895904204508441462052942570543469235743408662687829783981242921621141561958092996942138039539347186265888943521222099485305083425121799570340760306451056387721769644543770180874513413698489782310626510116900707328296317017861638609816742807238212375688992349407432527605212265201960484417319045306135753270537844265772630278030257553730404522542353023454507564239130509573900567947836333710575998177425339635490672762540951401365841203530163341399129259152441505842232988884030093489765491613638547290535239932922954836189727910738821821373908941704025595509698209842433778594931762880096051149228839059193124953083569345656699673217842731034615335159016868893667828284697601382849477319105482269464457369354201165383105634267706309276026548511653604938851683524347406300467032637059305168025683537591010278310739152497929267416488506602164364892672768951250021620883972638861480858511200026321716884284201621525790255706064102617823698465150902182020756805633387680203323377347214120763676433595862318757841275020744438922494391527853546261261653884838604819243603452331412866435935808645223933766501585217789210411820338061552711379609606091596742196531088333368820688700465100609294408564410551820533767314582215326501114346925687122130684308135594947230440902359602285498903088221418616588774868345381774759436249538445862391687353270774013333605021479815009072646131287167913594348212735157141945110505028392714321280168527912079885879378711575216114588688818483851010335343571373419408895400272889461955083127003507624246526443127984483974850359827123613158837880302989406513499383133815209255151794945252159578006553441006769695393494509219239341682913749485957990977399978974428237599198797470597398928833582814929980803158676347646073036058264457595979766850440585277452863784836881378951122699834495464687582384507050955411669089588056023213740474975271899763840563840420822396777704412574923895692685705898717550851271546847613225201942962155453506687322612157820088536247747423178392465183491963506251179872999797780241613376288397038058809215055851611928080552825377257229389841396784439699381820104819078753350765882554548273173586431021660057985633830221418264777828795408408219360539279937605759810247418730603915895910364476146651601773165435983974786945426386740130485945433428383712661638403349908041224903246339717707132587331536211004308396740066164681922627403775887587659911423157309899950883362737117904002200794037685750093360315002991204722819693264650123900463438261143078208399242862702756573035661761195250290023452158527680859911977315840459249712644829502602228281562205168422091946325746275718515430589611199291472509639265526607162743292686238612125417092236942849403073230046561689440328018730209783455636897152039461396204973470583533360944009995548259190650089214983126173317086462718613159388680105686912273118509552455404876439335541519502213501255533623000663415735131949390226163587481379691823369034203098855517602956728179063062385168262069957588853392494699060427790183358639133757353930827483324682039690711430878080139954054853012220118705649206222066778178469700012532593255713234178988343833012152511737545023838935655981896698623930545020866303712114446798229607036134097056141518614666103308067737293939882817522803683956618723557192957431811302993004661006507711819860892099762468763409984586465005289527598330319286504822147668081200343745709733713230749053422531039700549749611482700080424292093579194262860190545693893459668719568899438209545644522454004214175710540587898416049335014628093570914876601646372355336405161502945120696544968991549161292764943329793356783052725129518693221104540847729926451538197127621420078735251186983107579069830863887420105723312462918149598091679384401661221402668231302042083851622804060649467688652803972075275448682183441983118667834689851702873131165886191465353040437356529087934648081623704964161763089136782243131836819442222878188252407451973148740494123563498061326858407011580023162654013225311294299569453032858563923719894505732199268187098366832545152983545189549093206582569857418574982197684004122046026251600218524205478733284873736874167995247609432055545935020846092434424370646885517654091344064272429321190044633743277665948419787722550405845036699310579033665011565213048762251116463160349549840866208648830411837849907894702113302845740842254606232065291325663899963832236606713109642619168542709281695913841977476053850361455570418087541046597776831486258541481940647049280962522443702479743555424102740994242051358088022188948125185778246292798912400096370584995057543139698319698400714875322437360721067435172788002328971019632270916138767245738898190296997730666566040350054903222177287982901982208115626628117930838088332477576165687842526831821470799153750390636760151832044950394431072308726499328165491891591137040827828578083091548122977704835651634692270913158709973748126713962217462859416553459442512042125951719241687212266550505529478323245375197117130388807624890424900213867210379118067772830694151288503903953755600489575887106261099543007526558654558606616178757061110261366215321388261212678333799788475984322418497670390632396556883838275135787665717059361013830149460423193249144014464798326678822372653180557614383409636770362266692925318942384745121586253032075554519929226845690899765620418108080230488682533685315375513074062313208478770290356970117062184042161676169102954187536270352458862207052789293715690609072204364826933091424224241297278707976673787312156246909228154995282882864814776427436109709790991108809713794943844787250474115534406480523753777110833324561157940625334460229207417102486220441493002828630750763565408597131815280607802759987976988594333666224365688727640565409187175971523116235682113957076749440819231713695610919275732614057033745887153072563139779904167582767698982659908185821891149986971032791232523706763964090465621711265466378397629064005064256183015954758617031061911068658325754096130981614194150480011286340363597147389719431730075161264828896390990950405896633469322535538229648164951133505158948190871701495452297995859729192219129916626640974511036055163167953430508278744246455210561222076063082886421330931548311234377771468134131620028300777688512849206166411420083197673093407907216474408120367977423604387231140900144755694587511029881074025172808110773028966126192264881081173654566135163184575024166148354082335318755540140527510192916898825097483308124559574140749877241073371816013211948891150040862734370710552001020423986943613900787755113266258362756923530479836467722285411013952268578807230763973120512299868547802366173939941605900111867033963011243838890229285955980477436399272682720888395716722664244294040307932361164750377589339867567786084022351717809968505243295050262162666225704085085753178529799333689992842256184347049420382627220803497636149566771971988443987506280234073684367062388295302547379038563078052704679547186871419845169560136265820268631566998588112672196465511268994031219950377881891984086664533561685837490116568395566246534426902704247173891161130531148129017077991176186548359262213977369153611024401229987545380988711834735727064093342492889435511489956371386213511202643524683411986899709897837889555207831059294402564120363237276373453365679369475937403121789643999722428417549385709452037569343035330700089791322474993288554362968234185715157728741874247509605551885624764863710937193281066229313052014387465253211792374076869019230821310235278905815605016984754602075892815152468134980380875149693023626488854094761113452389617194503829653485957447289240964665382129494442319767741524032107849464355561962686855428996088016041727574266018893605425353091976657817320826256905470067544848365404288455132579420657846924763247959613120744648811087181131938848607645393807710848037947601059957013611527632994472621425550962659728515144496399839862413438943552359831654365602355439136361391583720859426226457380784928663687574972659965162133522278825967627428409090550509194838779526983778287267516529895199959559473171113271400661090671749798064642279344578197650423143356671456398108048153170444614293393268360576134582296420378866473750902935161296743138388685453636469879143897091417068932198968924975874651882288473411678774554398842121076952380278176865059376695182805039113082874841613017558586435591771206929671992362309121013283376212583656535618152400875518333878747313654797231478602487770165741388754402429316566099479832537050770596155878329597722083771069046266168535653308821067822830038963896747725883654438904212675990098114399270632993575611437828516256363287834229065550479510200784332394242915003380484529096834522877731271728293437808377102093735855723370071838879830907943761145926126645420747997707490201178317811634182738390286211673402425590231268024134092579770314414074598015237411441845356874971472410409419771656073125277224279317522407137377499979737387519882255189044392515227434947204646425704128620905701335480158391156957267403268662315621663653384388847511421695431554700646250997858682427715839096718851290026731790344073577690254569289293640725612890064066285176593969696242366279246844889080197107648044679284233401348087891465176800554085023473067840390153391423050791051498047366883674613808526102847330992863318230845301030203148340081044182482597249153845030633694827066599602663703198769252626109853706833788666919842568018444736837547001609454571699056027164033769487187061445582376622477252470344355569220044823249727754533229143903391961347406037847630784031508002820591062979210839546568855462434619468050578771265248969428395048142774706767513432710581762666140699436104382133696306101072577564706326133799588417319735360442767342673456848385606426258340280766460112903437053006055502536115436498949970090292094587801851745275159318616969127882705221068476071064812198272139939435215922010661995073845956919749185868274243779985357404583361198289539882772189566201036916639821343767603732993017532994450332069669805348791605902338685626630362219981699304061683389992684808224917531991401996431576738586998883176574895161070575979327258834346078593910855343246854952190595563162876213940502170576117614054906191774557259516215535106350328226461676747641836581250174425240067327607425722612703176066913472629956068964482928096190431153045334581236686485601653662548675005858307641173726518787667844636384234716332980448212084401611196254156828194466935418463150934156679750918161764625095443455345999484807464502070932256221468112309544096871325111517567560528465648372680954805187149252543993102343149130560833915977307607796273384132140246447470065995036294151622229707644947607508857265403455217518383185116624677562271823368449068845795008246680968064118309118123780879652410477410561045265702898238687842958789937343222645587767449174574439145224843282254965685285825553589160118003108867164726155323950377790001304331038454661228425943391601997416932856362299389729872081038378560085569181680325390890975135151904025220597253305978920607570509415416105078346636226033201150527397592415939841362093392531978492839704570531951269937790723640842176280024466490226532832231321791413175680982794571181974617754038842652504078930540800145471053102712921745974414032996931982681413495700672309901669747942924863984766970869047889148375114296751626854972363222203156742052025800331584900757339188307919281993585084089102774974162862974118249344017049330389722862507544244369572820048543537526419696982454752377300952263018094047480298218763773496590696705855146605191021025443668987252838029219743097999354469476696351687809806857534886205445284101783800631809717866779267380791425799571525834684947515229421100747229401244706964059772951145588451039937564715095244355312270405077836767030362195717581768372164243176423298697908028237639516267163659679427453374922935644876477737547758551696018705335165162618060083371223050822558413192442346376497722456093442959516305935871573614028652445645339911151351711068734588971914600796354281889654514681988006393137263566764124125185468322178621153563696617541027562042306891618536076420305562560140384429759146256074249614060085194656828455330225591764340431936672336010269753281103215063736624844960404714507377372926703848414371281922833048741555736003925793244417868433596548363696746612901046340739738534218356380120706541666158094317855060478031171269387322552469385246913122410421060757073892836093706122549466483290052946644975285183428547939839979245560823624086450916475334884086007549103223416083076659471519662168328213737523684566775560038550984353194211306023075127943670861184066979280851711839524013341160873911246529241847821977661833560911107243878999996910523477739507321196945539924672999844174387268229027137165174995719110525441136561919316251674895051611084204437388186230323683144927254113768523510761013499081639602376526344300975933539712536104235297313297778060192945947498389503635343182541580304604818291583546281179216136811580917637750547722533705421987195090226959543214655065144808421581068630978689279958437860306144533403146927325230488822351776122706954360908314771690844367323005474494614959817256571254845755594061246813380409321387684293094726535153432718799395016333684448895978150807133397512713053859785384219139733758254733466429303625796238966306429849073438009443604492679165429525422550614217829032094272382541194380864252642900476313510616326009196810027054241602711468064765850855931661019887180057432139227168222490753375909710319343153227291410744999485168168478575826321461311391498363919574747556660640223747224485215468538767744533266376865641979147273107305966429559234313951121394100261876410271313210471363290263309805705475186741555824016912991931543319715950899270659818589967651376229303248120234907102794964559529402349654404873775706541431273344701426677174227913635825993801253408407954306403640494130327848401203534745855654764649903475381445529393129138378456403126025789420293745754402133302487810580684564096950035283318336130548968505358375359878678445356819589197218705721362132525514387530554567772449363564741446440280566290065500811972925754503326977753872544674840381021956301672906808754551146104489572172125223473732950137971787627755821806136467893150104572138832325722560020707368136239504630555648373674183846152259144494913224772198691302006264053644408456756001501231380712682577431364093638356288981970490928597290658659577397948317485930045187284220942757169367406371217963607276147119558170075267310313993121539773306635924728010285717850717386728486494271999106777540230444019693114555059222570129625884701866197201054164219485729502653346614751640375744098772561595085919043754522891414957193260839473215059803925485419462712037200150025085250090959988756252019914868402306641265234353055736728142351699251476107406531461635000876389932699776308541832124373288414682198715237885557870768410260636379585316834556474223547554952476401595813925525924098174692018622482530457838042241883639109373477896187315967201005279204889250932564537220862115668272367246760197808397690439867030704368151526259675851742168279171050951045834298839100853697914421721214725026742996066063830613727555429792119237518445651104891704925096204588006826031584075144156552222279904885339225566415437629918304609734145875381343311105739561635010391670412979749422308070139951445968413375117951504258445974030517966948404640154878511720270361574121027274929022854038881874211985134131644902874098815982163204128615361689334838906702288057221628527667909691005669053006265304820000516245754619048316559656575555778442350016767520308426846204292292181387572647871716401329193475857567600937278816464537130723623419173338312223041654780004939225103609865067329137583702546930973721920070147482473838563431197047718220857884109765382231945561871177005621258325488412027635062855811964374306226863923349322086735338093300081110998500198180242237572041614141505401837245144666589451126614418309261119406898258814913139656849624746525696075355059973746104555027386061897496002358848811377019862876128747108587215015503591589671846621075223361735948917182635570087966559479939206094159893600636832413817552462441540979333401493042095457994530609466768145410793414786961578986703769254532321585081133810026251686670479990815751899937269160732357266127308473183713288274316131666746619682188683862033215792426452354488244217929986359029507815804861280802766469137868823404988705765868605129530917075216271145354765089704470834543723037092022223829952501590575804342636031551529513583838621019206561309557075964688342289250048635385468493323730646572121678123706208786179678189437580039594178112137983299058332223093830393184180599003206776617538844158601588727963897904948892906207330038129633453028396790302106388190259255665098912612456189061217467603856846750288831372512637351265611903642277570592114975078353659410265299629996347866301840130839707892446671272760096996435307146522533976085194449481731585025228715546841398139409977606880968122612075299123424989107042316060153768139305533390944486589089706647793349987583598705502385859118253555097730758183571788748493318026483779993522990059964930842662261423550372046259481766708519461053972123408548046233368297000625500789335188310361277431255694394512739943429612372653612119833866444673611922122884286376400703852732794104207056886654670765285610065391562535107737295068455363376593735911038999215465124525716447264378986280723441952072957689323197036917779046238705104878838199297683517450545939250335867902380249030230088879693013113267290145340855606743964920612348919820096206418738338000402648908843385492373140977443816601559955229789567153964360063607482617001385205658669226927081680681227141783682446795510097070928058706321217182642963932245572621225494570836809080696304421108659477495358140409725768111901733241972747321737485236651075238325072789278769335151256800060153679422973574458875515916845277488332898777017845449605728792457252326177424561213192702274981792287213238527811764142218986171881611026224857632940502866876670026612321482084455978978591912685862466522597225361221876025043241024125686817870062368959426991934775985466198692069677281279036262923496871441282706482598908257364709713806226468930224883770593729401076268485883601612500832587244698521037198961004743121282703056922175684510466763798160017747655236949266538172383404266604434703120839190211162903882884949493658466257451307015538218837139373916047592103776376187668584939395248581505058746394020838381930586105328462865511952293664289045859286595099762963649095828725653273696695970377602842623848575080469223025574537021779134365622328916334332605480385649587094077689920714041415396702599894625526530438335775213675088914858762127013392576152683366925196409542611853252292930259833298819229461000301124876096335663788692831214970380223133192111051901622008596133764389096103973284866495291293984413040529618127523214425555058796021316882133500599626493855431050548184294202081011476200270172971236263178962260464628572702019606759037821609795915644534894054006578650507178869970989367958506990145134616512567589533279300117397104656464072115861003885947760292429610719260622495020657405751092692319967370840896774324971994312187201140892209234707893310015065372595874895190303523487956139647693506746597550648757943465289121192007215646831013447260052720934085973891943435202629971145206653603008283693543038439891477841171750904374769500827989844031668442789961911999197831348021807731207773021908885128156496790685120004053849044926090199493266783648261891632146846730213684894594668497988614428157093670765703123227922409371473098857639913162301609591663778750887494156492637466025377922165520768594901414601787700115389988556906226868123722586261244162539000474842436024454775019691848977743338713957268915148782611928669044888561387755803845368912976125098635138047902331957444657679946398058069428095410458479732565320081683165461321863142192814783464248311534816512917696566924641054909136096408208507819169831354834820197974852382040217267563150065539763912517420222391237074707409931137459301625655369128686024322958745088162672262990086531381425473116134832936276934097761348260889973806799274552540309858134354652963971919291806025259617416657489998161768012184007461247857716093373851062214465742046499976679925886184547113397610899689516399527715231873244837654689479140949836562375751316564959184082064898433795934801697378935210812356361417920417188199897450171282905301001725646579028315769822372083310302724634022032865479440890344967212484369789296741287626375027228499888975289603385741124038309894182166758427641400387297128932283665708671005064286231685273517135852343598061885809244135922382370734232549843568268639892470737316962210716463540552178656355910885388452967673464843272315298393616429329495550316227091896005190734008923785380650145954415107967952105383626908638816509528042636771027000141361496050698500696289516507808035761279315751601032657448716422760668951052576520901270263030862403850786864221487468429142223597042893288577519400718146550367594284227693674216721802396916588003879410381712803867047701824472252385572064357858224990385221464929680275832507100235222381638646931573299381115307187211329144279641420300882898418754110526477679526210047597797239093376339400926209701087892613803814514168211216891738514955675123831262483436608771015627422110500411672488036674269685816206529074179713124834309456347164765592231821758836806328007029517278461137596014143470134168601285197528064250394793495121341293869318318873464175852783589584290759364726220783174193555648187620197246838352548922024201483573206348541581554877806499790859743925131694386295850686297620580999581615307344725045436396731477919745000263525564071600071799418368044323763386004973597785262345616864197137084315251756299886867708548224454966377785015214107932752034742964471519938747024381464750372074320624540157338744604837746391782978969692863284978072847889083273547790328260706234755653735063414263331806712601244244640413405376104968663673452744497406314393380631595851290273876776538375609905870013892662585944869013481609125637608769021646135871490726028047289495786025406466692525213012736552560650594990603546603161996548808402896308788941466324827370846884176384790910166124759384505093118598695568009286593720637594333395922541291636632520811018872532584798974694186667640086625560308565460443005955859172331229826655468385658059044850813343410906071462474694249984352580919295158836783427525690432719830486027484583121165927330452424884549732336876807654772818807641284146967934214566751755143186308634590244230408796575769718756282361510069184157797018107390497459818531530506179037167753214689815807004437961747863051611456799159436512767643319516594678307580569925255088627769290305646306306550568453671861787795003632759291667820391709324457769288158960099914944420323351359492460625475408201277917222267539986321155857430876290248743165925681921073528440130494021211818371223570676102735613583644582113927580306639874800796470571148664410643416701560585521476714164192784844980088518640310173775612225344758312382572301724172602695989646046961956724011936238601863802791145653769546240346981628032089611431825604881740067449685077108287975246323497787492276231894578487992467736938436307915538840891564204839737040776139650424143048874797254158464552748815315778023878670105477463899600001821364631550961547419198138866749059417582925648923187132000824086141166770885730318601675482736361783051811198851626379729427577955574229254375806209755478119460213845717233957802953325326409341230443816478315619910099779060423342216603594947660751044472229086798198530600407670616892772255039377042432416186985050714402674644026490922259763284331979636218240713552996546452480374758189934302039935149125943811195354472699721410162966542990190856273380941061879319453410231701573859138961987953494923982165605797659256730853392534725623960797515764483217565322116896851308493166313749197523964042186037587037831603930318645404856951079227088020245308389958711374534487183861910036300695438610474379210420008826355958169e-20104

julia> sinpi(BigInt(10)^10000)
0.0

If you don’t want to scroll all the way to the right, it’s 6.229e-20104. Very close to 0.
But you need a lot more than the default precision before you start getting approximately 0 as an answer.

4 Likes

I still think there should be a Base.sin(::Irrational{:π}) = false kind of thing (maybe just 0 since false might scare some people).

2 Likes

I get the same result. However,

using Symbolics
Symbolics.Term(sqrt,[2])

works.

I still think there should be a Base.sin(::Irrational{:π}) = false kind of thing (maybe just 0 since false might scare some people).

Wouldn’t help since 2pi is a float64 (which is pretty annoying when writing generic code). We’d need a “taking irrationals seriously” thing where n*pi would also be an irrational, but it would probably lead to madness pretty soon.

9 Likes

If I may ask: What is the purpose of wrapping the inputs in [ ]? Why not just Term(sqrt, 2), which seems to give the same results, but is dramatically faster?

jl> @btime Term(sqrt, big(2))
  54.888 ns (3 allocations: 56 bytes)
sqrt(2)

jl> @btime Term(sqrt, [big(2)])
  742.268 ns (8 allocations: 344 bytes)
sqrt(2)

jl> @btime Term(sqrt, 2)
  6.200 ns (1 allocation: 16 bytes)
sqrt(2)

jl> @btime Term(sqrt, [2])
  781.111 ns (6 allocations: 304 bytes)
sqrt(2)

When you use eval(toexpr()) the speed difference is small, but is there any difference in meaning?

1 Like

There’s still something I don’t get:

julia> using Symbolics: Term

julia> @variables x
(x,)

julia> expr1 = Term(sqrt, [big(2)])
sqrt(2)

julia> expr2 = expr1 + x
x + sqrt(2)

julia> expr3 = substitute(expr2, Dict([x => 0]))
1.414213562373095048801688724209698078569671875376948073176679737990732478462102

julia> typeof(expr1)
Term{Real,Nothing}

julia> typeof(expr3)
Num

What I expected to happen is that expr1 and expr3 would be the same but instead expr3 does not seem to be a symbolic object. You describe it as exact but the printed representation looks more like a high-precision approximation. In an exact representation I would expect to be able to query the object in some way to discover that it represents sqrt(2) symbolically but I don’t see a way to do that with expr3.

2 Likes

Yeah, the bigger thing to do would probably be to handle it in the switch to SymbolicTracing.jl.

I’m actually surprised that works. It should be very simpler to the Expr object which has an op and arguments. I guess the analysis code is now generic enough that it just iterates through the latter argument, and numbers are iterable so it works? @shashi

We should probably handle that in SymbolicTracing.jl as mentioned above.

2 Likes

Maybe I can see where we have a mismatch of expectations here. I guess symbolic expressions in SymbolicUtils do not really represent mathematical objects but rather delayed or parametrised evaluation of Julia code. I’m expecting that something like Term(sqrt, 2) will give an object that exactly represents the irrational number \sqrt{2} but really it represents the result of the code sqrt(2) in Julia which is a numerical approximation of \sqrt{2}. That’s why there are different ways to write it like Term(sqrt, big(2)) because they are different numerical approximations.

I can see how this approach could be very useful in applications using symbolics to control numerics but it is very different from exact symbolic computation. I haven’t really got my head round what the implications of this would be but I expect that it would be problematic to implement some of the traditional exact symbolic algorithms if it isn’t possible to represent and manipulate expressions involving irrational numbers exactly. Much of the theory of symbolic computation that underpins the cornerstone algorithms in other CAS is based on finitely generated extensions of the rationals and \sqrt{2} is really the simplest form of that.

11 Likes

Yes, and then as I keep mentioning, think about the changes when a Cassette-like SymbolicTracing changes parsing of literals to a Literal(2) and you get something a bit more flexible, but that’s just sugar. For now, stick the literal you want in.

I guess you mean that I can use Literal(2) directly but I’m not sure where I would use it from. Is that from Symbolics or SymbolicUtils?

I mean, take a look at Cassette and SymbolicTracing and you’ll see where that’s going.

This seems like a very good point and I’m not entirely understanding the tracing answer. It seems like there are (at least) two different things that you want to represent here: the Julia expression sqrt(2) and the mathematical expression sqrt(2). Tracing seems like a convenient way to construct representations of mathematical expressions, but doesn’t seem like it addresses the question of representation in the first place.

5 Likes