1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082 |
- module mathml;
- % Version 5 August 1999
-
- % Modified by FJW, 22 May 2000
- % Modified by Winfried Neun , 1 August 2000
- % Modified by Winfried Neun , 18 December 2000
- fluid '(atts ch cha char count file!* pvar rdci!* rdelems!* rdlist!*
- rdreln!* space safe_atts temp2 unary!* !*mathprint
- consts_compl consts_int flagg found_compl found_int consts_mat_int
- consts_mat_compl found_mat_compl found_mat_int indent);
- %Declaration of some switches.
- %_mathml_ allows all output to be printed in mathml.
- %_both_ allows all output to be printed in mathml and in normal reduce
- %output.
- load!-package 'assist;
- load!-package 'ineq;
- load!-package 'matrix;
- roundconstants();
- global '(f);
- global '(!*mathml);
- switch mathml;
- global '(!*both);
- switch both;
- global '(!*web);
- switch web;
- LISP (FILE!*:=nil);
- !*mathml:=nil;
- !*both:=nil;
- !*web:=nil;
- off both;
- off mathml;
- off web;
- %Declaration of a series of lists which contain the function to be executed
- %when the token (cadr) is found.
- %Tokens to be found between <ci></ci> tags.
- RDci!*:='
- ((!&imaginaryi!; . (consts 'i))
- (!&ii!; . (consts 'i))
- (!&exponential!; . (consts 'e))
- (!&ee!; . (consts 'e))
- (!&pi!; . (consts 'p))
- (!&differentiald!; . (const 'd))
- (!&dd!; . (consts 'd)));
- %Tokens to be found between <reln></reln> tags.
- RDreln!*:=
- '((tendsto . (tendstoRD ))
- (eq!/ . (relationRD 'eq))
- (neq!/ . (relationRD 'neq))
- (lt!/ . (relationRD 'lt))
- (gt!/ . (relationRD 'gt))
- (geq!/ . (relationRD 'geq))
- (leq!/ . (relationRD 'leq))
- (in!/ . (inRD ))
- (notin!/ . (notinRD ))
- (subset!/ . (relationRD 'subset))
- (prsubset!/ . (relationRD 'prsubset))
- (notprsubset!/ . (notprsubsetRD ))
- (notsubset!/ . (notsubsetRD )));
- %Tokens to be found between <apply></apply> tags.
- RDlist!*:= append(RDreln!*,
- '((divide!/ . (divideRD))
- (setdiff!/ . ( setdiffRD))
- (select!/ . (selectRD))
- (transpose!/ . ( transposeRD))
- (determinant!/ . ( determinantRD))
- (fn . ( applyfnRD))
- (union!/ . (unionRD))
- (intersect!/ . (intersectionRD))
- (implies!/ . ( impliesRD))
- (not!/ . ( notRD))
- (xor!/ . (xorRD))
- (or!/ . (orRD))
- (and!/ . (andRD))
- (mean!/ . ( meanRD))
- (var!/ . ( varRD))
- (sdev!/ . ( sdevRD))
- (moment!/ . ( momentRD))
- (median!/ . ( medianRD))
- (sin!/ . ( sinRD))
- (sec!/ . ( secRD))
- (sinh!/ . ( sinhRD))
- (sech!/ . ( sechRD))
- (arcsin!/ . ( arcsinRD))
- (cos!/ . ( cosRD))
- (csc!/ . ( cscRD))
- (cosh!/ . ( coshRD))
- (csch!/ . ( cschRD))
- (arccos!/ . ( arccosRD))
- (tan!/ . ( tanRD))
- (cot!/ . ( cotRD))
- (tanh!/ . ( tanhRD))
- (coth!/ . ( cothRD))
- (arctan!/ . ( arctanRD))
- (abs!/ . ( absRD))
- (ln!/ . ( lnRD))
- (plus!/ . ( plusRD))
- (times!/ . ( timesRD))
- (power!/ . ( powerRD))
- (exp!/ . ( expRD))
- (factorial!/ . ( factorialRD))
- (quotient!/ . ( quotientRD))
- (max!/ . ( maxRD))
- (min!/ . ( minRD))
- (minus!/ . ( minusRD))
- (rem!/ . (remRD))
- (conjugate!/ . ( conjugateRD))
- (root!/ . ( rootRD))
- (gcd!/ . ( gcdRD))
- (log!/ . (logRD))
- (int!/ . (intRD))
- (sum!/ . ( sumRD))
- (limit!/ . (limitRD))
- (condition . (conditionRD))
- (product!/ . (productRD))
- (diff!/ . (diffRD))
- (partialdiff!/ . (partialdiffRD))));
- RDelems!* :=
- '((reln . (relnRD !/reln "</reln>"))
- (set . ( setRD !/set "</set>"))
- (fn . ( fnRD !/fn "</fn>"))
- (declare . ( declareRD !/declare "</declare>"))
- (list . ( listRD !/list "</list>"))
- (matrix . ( matrixRD !/matrix "</matrix>"))
- (cn . ( cnML !/cn "</cn>"))
- (ci . ( ciML !/ci "</ci>"))
- (lambda . ( lambdaRD !/lambda "</lambda>")));
- unary!* :=
- '((determinant . (unary determinant))
- (transpose . (unary transpose))
- (sum . (sum_prodML sum))
- (prod . (sum_prodML product))
- (df . (dfML nil)) % FJW: (df.(dfML df))
- (impart . (complpart impart))
- (repart . (complpart repart))
- (abs . (unary abs))
- (gcd . (n_nary gcd))
- (set . (setML set))
- (factorial . (unary factorial))
- (max . (n_nary max))
- (min . (n_nary min))
- (cos . (unary cos))
- (sin . (unary sin))
- (sec . (unary sec))
- (cosh . (unary cosh))
- (cot . (unary cot))
- (coth . (unary coth))
- (csch . (unary csch))
- (acos . (trigML acos))
- (asin . (trigML asin))
- (atan . (trigML atan))
- (sech . (unary sech))
- (sinh . (unary sinh))
- (tan . (unary tan))
- (tanh . (unary tanh))
- (csc . (unary csc))
- (quotient . (quotientML nil))
- (plus . (n_nary plus))
- (times . (n_nary times))
- (expt . (n_nary power))
- (sqrt . (sqrtML sqrt))
- (log . (unary log))
- (logb . (log_baseML logb))
- (log10 . (log_baseML log10))
- (ln . (unary ln))
- (eq . (reln eq))
- (neq . (reln neq))
- (gt . (reln gt))
- (lt . (reln lt))
- (greaterp . (reln gt))
- (lessp . (reln lt))
- (geq . (reln geq))
- (leq . (reln leq))
- (union . (sets union))
- (intersection . (sets intersection))
- (in . (reln in))
- (notin . (reln notin))
- (subset . (reln subset))
- (prsubset . (reln prsubset))
- (notsubset . (reln notsubset))
- (notprsubset . (reln notprsubset))
- (setdf . (sets setdf))
- (arbcomplex . (printsub2 cadr arbcomplex))
- (arbint . (printsub2 cadr arbint))
- (mat . (matrixML nil))
- (minus . (minusML nil))
- (int . (integralML nil))
- (equal . (equalML nil))
- (list . (listML nil)));
- %The next three functions are the lexer. When called they returns the next
- %mathml token in the input stream.
- symbolic procedure lex();
- begin scalar token;
- token:=nil;
- if atts neq nil then safe_atts:=atts;
- atts:=nil;
- if ch neq !$EOF!$ then <<
- if ch=space then while (ch:=readch())=space do
- else
- if ch='!< then char:=get_token()
- else char:=get_content();
- if char neq nil then
- << count:=count+1;
- token:=reverse char;
- char:=butes(token);
- %By decomenting the following line, the tokens read in are one by one
- %printed onto the output stream.
- % print char;
- attributes(char,token)>>
- else lex(); >>
- end;
- symbolic procedure get_token();
- begin scalar d;
- d:=();
- while (ch:=readch()) neq '!> do d:=cons(ch,d);
- return cons('!$,d);
- end;
- symbolic procedure get_content();
- begin scalar d;
- d:=();
- while (ch:=readch()) neq '!< AND ch neq !$EOF!$
- do
- if ch neq space AND id2int(ch)>10 then
- d:=cons(ch,d);
- if d neq nil then d:=cons('!$,d);
- return d;
- end;
- %This function will search the list of attributes _att_ for the attribute
- %named _key_
- symbolic procedure search_att( att, key);
- begin scalar l, stop,d;
- l:=nil;
- d:=();
- stop:=0;
- att:= find(att, key);
- if att neq '(stop) then
- <<
- while (car att='! ) do att:=cdr att;
- if (car att = '!=) then
- <<
- att:=cdr att;
- while (car att='! ) do att:=cdr att;
- if (car att='!") then
- << att:=cdr att;
- while (stop=0) do
- << d:=cons(car att, d);
- att:=cdr att;
- if (car att='! ) OR (car att='!$) then stop:=1
- >>
- >>
- else
- while (stop=0) do
- << d:=cons(car att, d);
- att:=cdr att;
- if (car att='! ) OR (car att='!$) then stop:=1
- >>
- >>
- else
- errorML(compress key,1);
- if car d='!" then d:=cdr d;
- return reverse d
- >>
- end;
-
- symbolic procedure find(fatt, fkey);
- begin;
- return if fkey= '() then if fatt neq nil then cdr fatt else '(stop)
- else
- find(member(car fkey, fatt), cdr fkey);
- end;
-
- symbolic procedure attributes(a,b);
- begin scalar l;
- l:=length a;
- for a:=1:l do b:=cdr b;
- while (car b='! ) do b:=cdr b;
- if b neq '(!$) then atts:=b;
- end;
- symbolic procedure butes( str );
- %Removes all attributes to a token.
- begin cha;
- cha:=car str;
- return if (cha='! OR cha='!$) then <<'(); >>
- else cons(car str, butes cdr str);
- end;
- %This is the MAIN function. It is given the name of a file which contains
- %the mathml input. It launches the program by calling parseML().
- symbolic procedure mml(ff);
- begin;
- FILE!*:=t;
- ff:= open(ff, 'input);
- ff:= rds(ff);
- parseML();
- close rds ff;
- FILE!*:=nil;
- end;
- %This function starts the parsing mechanism, which is a recursive descent
- %parsing.
- symbolic procedure parseML();
- begin scalar res, vswitch;
- res:=nil;
- vswitch:=nil;
- % FLUID '(safe_atts char ch atts count temp space temp2);
- space:=int2id(32);
- count:=0;
- ch:=readch();
- temp2:=nil;
- lex();
- if char='(m a t h) then
- res:=mathML()
- else errorML("<math>",2);
- lex();
- if char='(!/ m a t h) then
- terpri()
- else errorML("</math>",19);
- return algebraic res;
- end;
- %The two next functions differ in that one of them parses from the next
- %token onwards, and the other one from the actual token onwards.
- symbolic procedure mathML();
- begin scalar a;
- a:=nil;
- lex();
- return sub_math();
- end;
- symbolic procedure mathML2();
- begin scalar a;
- a:=nil;
- return sub_math();
- end;
- %Parses all tokens which legally follow a mathml token.
- symbolic procedure sub_math();
- begin scalar a,aa;
- if char='(a p p l y)
- then <<a := applyML();
- if char neq '(!/ a p p l y) then errorML("</apply>",3)>>
- else if char='(v e c t o r)
- then <<a := vectorRD();
- if char neq '(!/ v e c t o r)
- then errorML("</vector>",2)>>
- else if (aa := assoc(compress!* char, RDelems!*))
- then <<a := apply(cadr aa, '() );
- if compress!* char neq third aa
- then errorML(third cdr aa, 2)>>;
- return a
- end;
- symbolic procedure compress!* u;
- begin scalar x;
- if digit car u then return compress u;
- for each j in u do
- if j eq '!/ or j eq '!- or j eq '!; or j eq '!.
- then x := j . '!! . x
- else x := j . x;
- return intern compress reversip x
- end;
- %The next two functions parse the <cn> and <ci> tokens and extracts its
- %content to be used by the function calling it. It will have different
- %behaviours according to the type of the <cn> data.
- symbolic procedure cnML();
- begin scalar type, sep, tt,aa;
- %Must check that what is being returned is an int.
- type:=nil; sep:=nil;
- type:=search_att(atts, '(t y p e));
- lex();
- tt := char;
- lex();
- if type='(c o n s t a n t) then
- <<
- if (aa:=assoc(intern compress tt, RDci!*)) then
- return apply(first cdr aa, rest cdr aa) >>;
- if IDP compress tt then errorML(compress tt, 16);
- if type=nil then return compress tt;
- if member(type, '((r e a l) (i n t e g e r))) neq nil then
- return compress tt;
- if member(type, '((r a t i o n a l) (c o m p l e x !- c a r t e s i a n)
- (c o m p l e x !- p o l a r))) neq nil then
- << sep:=sepRD();
- if type='(r a t i o n a l) then <<lex();return alg_quotient(compress tt, sep)>>
- else
- if type='(c o m p l e x !- c a r t e s i a n) then
- << lex();return comp2(compress tt, sep) >>else
- if type='(c o m p l e x !- p o l a r) then
- <<sep:= po2ca(compress tt, sep);
- <<lex();return comp2(car sep, cadr sep)>> >>
- >>;
- end;
- symbolic procedure ciML();
- begin scalar test, type,aa, tt;
- aa:=nil; type:=nil; test:=nil;
- type:=search_att(atts, '(t y p e));
- lex();
- tt := char;
- lex();
- << test:=compress tt;
- if NUMBERP test then errorML(test, 4);
- test:=intern test;
- return test>>
- end;
- %returns the algebraic value of the constant values.
- algebraic procedure consts(c);
- begin;
- if c=i then return i;
- if c=d then return d;
- if c=e then return e;
- if c=p then return pi;
- end;
- %Constructs a complex number.
- algebraic procedure comp2(a,b);
- begin;
- return a+b*i;
- end;
- %Returns the two values separated by a <sep/> tag.
- symbolic procedure sepRD();
- begin scalar p1, p2;
- p1:=nil; p2:=nil;
- if char neq '(s e p !/) then errorML("<sep/>",2);
- lex();
- p2:=compress char;
- return p2;
- end;
- %Creates a vector by using function matrix_row.
- symbolic procedure vectorRD();
- begin scalar a;
- a:=nil;
- a:=matrixrowRD();
- a:=lisp aeval list('mat, a);
- return a;
- end;
- %The following functions construct the matrix from the mathml information.
- symbolic procedure matrixRD();
- begin scalar b1, b2, stop;
- stop:=0;
- b1:='();
- b2:=nil;
- while stop=0 do
- <<
- lex();
- if char='(m a t r i x r o w) then
- <<b2:=matrixrowRD();
- if b1 neq nil then b1:=append(b1, list b2)
- else b1:=list b2;
- if char neq '(!/ m a t r i x r o w) then
- errorML("</matrixrow>",2)>>
- else stop:=1
- >>;
- return aeval cons ('mat ,b1);
- end;
- symbolic procedure matrixrowRD();
- begin scalar a;
- a:=nil;
- a:=mathML();
- return if a=nil then nil
- else cons(a, matrixrowRD());
- end;
- %returns a lambda function constructed from the information supplied.
- symbolic procedure lambdaRD();
- begin scalar b1, b2;
- lex();
- b1:=bvarRD();
- b1:=car b1;
- b2:=mathML();
- lex();
- return algebraic( (lambda b1; b2) b1 );
- end;
- %returns a set constructed from the information supplied.
- symbolic procedure setRD();
- begin scalar setvars;
- atts:='(t y p e != s e t !$);
- setvars:= cons('list,stats_getargs());
- setvars:=cons(car setvars, norepeat(cdr setvars));
- return setvars;
- end;
- %This function will keep one copy only of any repeating elements
- symbolic procedure norepeat(args);
- begin;
- return if args=nil then nil else
- if length args=1 then list car args
- else append(list car args, norepeat(delall(car args, cdr args)));
- end;
- %This function will delete all occurences of element x in list l
- symbolic procedure delall(x,l);
- if l=nil then nil
- else if x=car l then delall(x, cdr l)
- else append(list car l ,delall(x, cdr l));
- %returns a list constructed from the information supplied.
- symbolic procedure listRD();
- begin scalar setvars, lorder, tmp;
- lorder:=search_att(atts, '(o r d e r));
- atts:='(t y p e != l i s t !$);
- setvars:= cons('list,stats_getargs());
- tmp := setvars;
- if lorder='(l e x i c o g r a p h i c) then
- setvars:=algebraic sortlist (setvars, lexog);
- if lorder='(n u m e r i c) then
- setvars:=algebraic sortlist (setvars, numer)
- else
- setvars:=algebraic sortlist (setvars, pred);
- if setvars = nil then setvars:= tmp;
- return setvars;
- end;
-
- %Defines the predicate function used by function _sortlist_. Sortlist comes
- %from package assist, and its documentation can be found in assist's
- %documentation
- %This one will sort all elements in numerical and alphanumerical order
- symbolic procedure pred(u,v);
- begin;
- return if NUMBERP u and NUMBERP v then <<if u<v then t>> else
- if IDP u and IDP v then <<if id2int(u) < id2int(v) then t>>
- else if NUMBERP u and IDP v then <<if u<id2int(v) then t>> else
- if IDP u and NUMBERP v then <<if id2int(u)<v then t>>;
- end;
- %This one sorts in alphanumerical order
- symbolic procedure lexog(u,v);
- begin;
- return if IDP u and IDP v then <<if id2int(u) < id2int(v) then t>>
- else t;
- end;
- %This one sorts in numerical order
- symbolic procedure numer(u,v);
- begin;
- return if NUMBERP u and NUMBERP v then <<if u<v then t>>
- else t;
- end;
- %Makes the next token in the inputstream an operator.
- symbolic procedure fnRD();
- begin scalar b1;
- lex();
- if char neq '(c i) then errorML(compress char,20)
- else b1:= mathML2();
- if ATOM b1 then algebraic operator b1;
- lex();
- return b1;
- end;
- %Reads the declare construct and sets the value of the given variable to
- %the given value.
- symbolic procedure declareRD();
- begin scalar b1, b2, flagg, at;
- at:=atts;
- flagg := nil;
- b1:=mathML();
- clear b1;
- clear reval b1;
- lex();
- if at neq nil then
- put(b1, 'type, search_att(at,'(t y p e)));
- if search_att(at, '(t y p e)) = '(v e c t o r) then
- flagg:=t;
- if char='(!/ d e c l a r e) then return nil;
- b2 :=mathML2();
- if get(b1, 'type)='(f n) then
- << algebraic operator b1>>;
- if flagg = t then setk(b1, b2)
- else algebraic set(b1, b2);
- lex();
- return nil;
- end;
- %This function will determine if the next token is a valid token following
- %an apply token. It then calls the appropriate function if succesful.
- symbolic procedure applyML();
- begin scalar aa;
- lex();
- if (aa := assoc(compress!* char, RDlist!*))
- then return apply(first cdr aa, rest cdr aa)
- else if char='(i d e n t !/) or char='(c o m p o s e !/)
- then return nil
- else if char='(i n v e r s e !/) then return t
- else errorML(compress!* char, 17)
- end;
- %Reads the next two elements and returns their setdifference.
- symbolic procedure setdiffRD();
- begin scalar b1, b2;
- b1:=mathML();
- b2:=mathML();
- lex();
- if b1=reval b1 and b2=reval b2 then return list('setdiff,b1, b2)
- else
- if b1=reval b1 then return list('setdiff, b1, reval b2) else
- if b2=reval b2 then return list('setdiff, reval b1, b2) else
- return append(list('set), setdiff(reval b1, reval b2));
- end;
- %Reads through a select construct and acts accordingly.
- symbolic procedure selectRD();
- begin scalar a1, res;
- a1:=stats_getargs();
- if caar a1='mat then res:=mat_select(a1);
- if caar a1='list then res:=list_select(a1);
- if ATOM res then return res;
- return cons('list, res);
- end;
- symbolic procedure mat_select(a1);
- begin
- if length car a1=2 then return nth(cadar a1, cadr a1)
- else
- if length a1=2 then return nth(cdar a1, cadr a1);
- if length a1=3 then return nth(nth(cdar a1, caddr a1), cadr a1);
- end;
- symbolic procedure list_select(a1);
- begin scalar b1;
- b1:=cdar a1;
- return nth(b1, cadr a1);
- end;
- %Returns the transpose of the element contained in the transpose tags.
- symbolic procedure transposeRD();
- begin scalar a, res;
- a:=mathML();
- res:=algebraic(tp a);
- lex();
- return res;
- end;
- %Returns the determinant of the given element.
- symbolic procedure determinantRD();
- begin scalar a, res;
- a:=mathML();
- res:=alg_det a;
- lex();
- return res;
- end;
- algebraic procedure alg_det(a);
- begin;
- return det a;
- end;
- %Takes the given function name, makes it an operator, and then
- %applies it to the arguments specified in the mathml input.
- symbolic procedure applyfnRD();
- begin scalar b1, b2, c1;
- b1:=nil; b2:=nil; c1:=nil;
- b1:=fnRD();
- b2:=stats_getargs();
- b2:=cons(b1, b2);
- c1:=algebraic b2;
- return c1;
- end;
- %Returns the union of the elements specified.
- symbolic procedure unionRD();
- begin scalar b1, a1, a2,type,res;
- b1:=stats_getargs();
- a1:=car b1;
- a2:=cadr b1;
- if PAIRP a1 AND PAIRP a2 then <<
- type := car a1;
- a1:=cons('list, eval_list cdr a1);
- a2:=cons('list, eval_list cdr a2);
- res:=algebraic union(a1,a2);
- >>
- else <<
- type := 'list;
- res := cons('list,cons(a1,list a2));
- >>;
- return cons(type, cdr res);
- end;
- %Returns the intersection of the elements specified.
- symbolic procedure intersectionRD();
- begin scalar b1, a1, a2,type,res;
- b1:=stats_getargs();
- a1:=car b1;
- a2:=cadr b1;
- if PAIRP a1 AND PAIRP a2 then <<
- type := car a1;
- a1:=cons('list, eval_list cdr a1);
- a2:=cons('list, eval_list cdr a2);
- res:=algebraic intersect(a1,a2);
- >>
- else <<
- type := 'list;
- res := cons('list,cons(a1,list a2));
- >>;
- return cons(type, cdr res);
- end;
- %Takes all the arguments in a list, and forces an evaluation on them if they can be
- %evaluated.
- symbolic procedure eval_list(args);
- begin;
- return if args=nil then nil
- else cons(reval car args, eval_list(cdr args));
- end;
- %Takes all the arguments in a list of sets, and evaluates them if they can
- %be evaluated.
- symbolic procedure eval_list_sets(args);
- begin scalar ab;
- return if args=nil then nil
- else <<if PAIRP reval car args then
- <<
- if car reval car args='list then
- ab:=cons('set, cdr reval car args)>>
- else ab:=reval car args;
- cons(ab, eval_list_sets(cdr args))>>;
- end;
- %Sets global variable temp2 to 'stop if an evaluatable element is found in
- %list args.
- symbolic procedure constants(args);
- begin scalar b1;
- if args neq nil then b1:=car args;
- return if args=nil then nil
- else <<if b1=reval b1 AND IDP b1 OR PAIRP b1 then temp2:='stop
- else constants(cdr args)>>;
- end;
- %Return boolean values of the arguments given.
- symbolic procedure notRD();
- begin scalar a;
- a:=mathML();
- lex();
- return not(reval a);
- end;
- symbolic procedure impliesRD();
- begin scalar a1,b1,c1;
- a1:=mathML();
- b1:=mathML();
- if b1='false then b1:=nil;
- if a1='false then a1:=nil;
- if reval a1 AND not reval b1 then c1:=nil
- else c1:=t;
- lex();
- return c1;
- end;
- symbolic procedure andRD();
- begin scalar a;
- a:=stats_getargs();
- a:=subst(nil, 'false, a);
- a:=and2RD(a);
- return a;
- end;
- symbolic procedure and2RD(args);
- begin
- return if length args=1 then reval car args
- else and(reval car args, and2RD(cdr args));
- end;
- symbolic procedure orRD();
- begin scalar a;
- a:=stats_getargs();
- a:=subst(nil, 'false, a);
- a:=or2RD(a);
- return a;
- end;
- symbolic procedure or2RD(args);
- begin
- return if length args=1 then reval car args
- else or(reval car args, or2RD(cdr args));
- end;
- symbolic procedure xorRD();
- begin scalar a;
- a:=stats_getargs();
- a:=subst(nil, 'false, a);
- a:=xor2RD(a);
- return a;
- end;
- symbolic procedure xor2RD(args);
- begin
- return if args=() then nil
- else alg_xor(reval car args, xor2RD(cdr args));
- end;
- symbolic procedure alg_xor(a,b);
- begin;
- return and(or(a,b),not(and(a,b)));
- end;
- %All defined trigonometric functions.
- algebraic procedure sinRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return sin(a);
- end;
- algebraic procedure secRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return sec(a);
- end;
- algebraic procedure sinhRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return sinh(a);
- end;
- algebraic procedure sechRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return sech(a);
- end;
- algebraic procedure arcsinRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return asin(a);
- end;
- algebraic procedure cosRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return cos(a);
- end;
- algebraic procedure cscRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return csc(a);
- end;
- algebraic procedure coshRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return cosh(a);
- end;
- algebraic procedure cschRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return csch(a);
- end;
- algebraic procedure arccosRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return acos(a);
- end;
- algebraic procedure tanRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return tan(a);
- end;
- algebraic procedure cotRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return cot(a);
- end;
- algebraic procedure tanhRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return tanh(a);
- end;
- algebraic procedure cothRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return coth(a);
- end;
- algebraic procedure arctanRD();
- begin scalar a;
- a:=symbolic mathML();
- symbolic lex();
- return atan(a);
- end;
- %Reads the condition tag.
- symbolic procedure conditionRD();
- begin scalar a;
- lex();
- if char='(r e l n) then a:=relnRD()
- else a:=mathML();
- lex();
- return a;
- end;
- %This function will read all legal tags following the <reln> tag.
- symbolic procedure relnRD();
- begin scalar a,aa;
- lex();
- if (aa := assoc(compress!* char, RDreln!*))
- then a := apply(first cdr aa, rest cdr aa)
- else errorML(compress!* char, 18);
- return if a=t then t else if null a then 'false else a;
- end;
- symbolic procedure relationRD( type );
- begin scalar args,a;
- args:=stats_getargs();
- if type='(quote eq) then <<a:= 'equal . args>> else
- %if type='(quote eq) then <<a:= alg_eq(args)>> else
- %if type='(quote neq) then <<a:= alg_neq(args)>> else
- %if type='(quote lt) then <<a:= alg_lt(args)>> else
- %if type='(quote gt) then <<a:= alg_gt(args)>> else
- if type='(quote lt) then <<a:= 'lessp . args>> else
- if type='(quote gt) then <<a:= 'greaterp . args>> else
- if type='(quote subset) then <<a:=subsetRD(args)>> else
- if type='(quote prsubset) then <<a:=prsubsetRD(args)>> else
- %if type='(quote geq) then <<a:= alg_geq(args)>> else
- %if type='(quote leq) then <<a:= alg_leq(args)>>;
- if type='(quote geq) then a:= 'geq . args else
- if type='(quote leq) then a:= 'leq . args;
- return if a=t then t
- else if a=nil then 'false else a;
- end;
- %The following functions do all the necessay actions in order to evaluate
- %what should be by the tags.
- symbolic procedure notsubsetRD();
- begin scalar b1, b2;
- b1:=mathML();
- b2:=mathML();
- lex();
- if b1=reval b1 AND b2=reval b2 then
- return list('notsubset, b1, b2);
- if b1= reval b1 then
- return list('notsubset, b1,cons ('set, cdr reval b2));
- if b2= reval b2 then
- return list('notsubset, cons('set,cdr reval b1), b2);
- if intersection(cdr reval b1,cdr reval b2)=nil then
- return t
- else
- return nil;
- end;
- symbolic procedure notprsubsetRD();
- begin scalar b1, b2;
- b1:=mathML();
- b2:=mathML();
- lex();
- if b1=reval b1 AND b2=reval b2 then
- return list('notprsubset, b1, b2);
- if b1= reval b1 then
- return list('notprsubset, b1,cons('set, cdr reval b2));
- if b2= reval b2 then
- return list('notprsubset, cons('set,cdr reval b1), b2);
- if reval b1 = reval b2 then return t;
- if intersection(cdr reval b1,cdr reval b2)=nil then return t else
- return nil;
- end;
- symbolic procedure subsetRD(sets);
- begin scalar args,val;
- args:=sets;
- val:=t;
- while (length args > 1) do
- << if NUMBERP reval car args then
- errorML(reval car args,5);
- if car args = reval car args OR cadr args = reval cadr args then
- << args:='();
- val:=cons('subset, eval_list_sets(sets))>>
- else
- << val:=AND(val, alg_subset(reval car args, reval cadr args));
- args:=cdr args >>
- >>;
- return val;
- end;
- symbolic procedure alg_subset(a,b);
- begin;
- if a=b then return t
- else
- if setdiff(a,b)=nil then return t else return nil;
- end;
- symbolic procedure prsubsetRD(sets);
- begin scalar args, val;
- val:=t;
- while (length args > 1) do
- << if car args = reval car args OR cadr args = reval cadr args then
- << args:='();
- val:=cons('prsubset, eval_list_sets(sets))>>
- else
- << val:=AND(val, alg_prsubset(reval car args, reval cadr args));
- args:=cdr args >> >>;
- return val;
- end;
- symbolic procedure alg_prsubset(a,b);
- begin;
- if setdiff(a,b)=nil then return t else return nil;
- end;
- symbolic procedure inRD();
- begin scalar b1,b2;
- b1:= mathML();
- b2:= mathML();
- lex();
- if b2 = reval b2 AND ATOM b2 then
- <<
- if b2='n then <<if FIXP b1 then return t else return nil>>;
- if b2='r then <<if NUMBERP b1 then return t else return nil>>;
- return list('in, reval b1, b2)
- >>;
- if MEMBER(reval b1,reval b2) neq nil then return t
- else return nil;
- end;
- symbolic procedure notinRD();
- begin scalar b1,b2;
- b1:= mathML();
- b2:= mathML();
- lex();
- if b2 = reval b2 AND ATOM b2 then
- <<
- if b2='N then if FIXP b1 then return nil else return nil;
- if b2='R then if NUMBERP b1 then return nil else return nil;
- return list('notin, reval b1, b2)>>;
- if MEMBER(reval b1,reval b2) neq nil then return nil
- else return t;
- end;
- symbolic procedure alg_eq(args);
- begin;
- constants(args);
- return alg_eq2 eval_list args;
- end;
- symbolic procedure alg_eq2(args);
- begin;
- return if length args=1 then t
- else if (reval car args eq reval cadr args) then
- alg_eq2(cdr args);
- end;
- symbolic procedure alg_neq(args);
- begin;
- constants(args);
- return alg_neq2(eval_list(args));
- end;
- symbolic procedure alg_neq2(args);
- begin;
- return if length args=1 then t
- else if (reval car args neq reval cadr args) then
- alg_neq2(cdr args);
- end;
- symbolic procedure alg_lt(args);
- begin;
- constants(args);
- if temp2='stop then
- <<temp2:=nil; return append(list 'lt, eval_list(args))>>
- else return alg_lt2(eval_list(args));
- end;
- symbolic procedure alg_lt2(args);
- begin;
- return if length args=1 then t
- else
- if (NUMBERP reval car args AND NUMBERP reval cadr args )then
- <<if (reval car args < reval cadr args) then
- alg_lt2(cdr args)
- else nil>>
- else errorML("",6);
- end;
- symbolic procedure alg_gt(args);
- begin;
- constants(args);
- if temp2='stop then
- <<temp2:=nil; return append(list 'gt, eval_list(args))>>
- else return alg_gt2(eval_list(args));
- end;
- symbolic procedure alg_gt2(args);
- begin;
- return if length args=1 then t
- else
- if (NUMBERP reval car args AND NUMBERP reval cadr args )then
- <<if (reval car args > reval cadr args) then
- alg_gt2(cdr args)
- else nil>>
- else errorML("",6);
- end;
- symbolic procedure alg_geq(args);
- begin;
- constants(args);
- if temp2='stop then
- <<temp2:=nil; return append(list 'g_eq, eval_list(args))>>
- else return alg_geq2(eval_list(args));
- end;
- symbolic procedure alg_geq2(args);
- begin;
- return if length args=1 then t
- else
- if (NUMBERP reval car args AND NUMBERP reval cadr args )then
- <<if (reval car args >= reval cadr args) then
- alg_geq2(cdr args)
- else nil>>
- else errorML("",6);
- end;
- symbolic procedure alg_leq(args);
- begin;
- constants(args);
- if temp2='stop then
- <<temp2:=nil; return append(list 'l_eq, eval_list(args))>>
- else return alg_leq2(eval_list(args));
- end;
- symbolic procedure alg_leq2(args);
- begin;
- return if length args=1 then t
- else
- if (NUMBERP reval car args AND NUMBERP reval cadr args )then
- <<if (reval car args <= reval cadr args) then
- alg_leq2(cdr args)
- else nil>>
- else errorML("",6);
- end;
- %Interprets the <tendsto> tag when used in the <limit> tag.
- symbolic procedure tendstoRD();
- begin scalar attr, arg1 ,arg2;
- if intersection(atts, '(t y p e)) neq nil then
- attr:=search_att(atts, '(t y p e))
- else attr:=nil;
- arg1:=mathML();
- arg2:=mathML();
- lex();
- return list (attr,arg2);
- end;
-
- %Returns the limit of the information given. Uses the Reduce package
- %LIMITS.
- symbolic procedure limitRD();
- begin scalar var, condi, low, exp;
- lex();
- if char='(b v a r) then
- << var:=bvarRD();
- if eqn(cadr var,1) then var:=car var
- else
- errorML("<degree>",8);
- lex()>>
- else var:=nil;
- if char='(l o w l i m i t) then
- << low:=lowlimitRD();
- lex()>>
- else if char='(c o n d i t i o n) then
- << condi:=conditionRD();
- if char neq '(!/ c o n d i t i o n) then
- errorML("</condition>",2);
- lex()>>
- else condi:=nil;
- exp:=mathML2();
- lex();
- if condi=nil then
- return alg_limit(exp, var, low, 'norm);
- if low=nil then
- if car condi='(a b o v e) then
- return alg_limit(exp, var, cadr condi, 'plus)
- else return alg_limit(exp, var, cadr condi, 'min);
- end;
- algebraic procedure alg_limit(exp, var, tendto, type);
- begin;
- if type='norm then return limit(exp, var, tendto);
- if type='plus then return limit!+(exp,var,tendto);
- if type='min then return limit!-(exp,var,tendto);
- end;
-
- %Returns the sum.
-
- symbolic procedure sumRD();
- begin scalar svar, low, upper, express, res;
- svar:=nil; low:=nil; upper:=nil; express:=nil; res:=nil;
- lex();
- if char='(b v a r) then
- <<svar:=bvarRD();
- if eqn(cadr svar,1) then svar:=car svar
- else
- errorML("<degree>",7);
- lex()>>
- else errorML("<bvar>",9);
- if char='(l o w l i m i t) then
- << low:=lowlimitRD();
- lex();
- if char='(u p l i m i t) then
- << upper:=upperlimitRD();
- lex()>>
- else errorML("<uplimit>",10) >>
- else if char='(i n t e r v a l) then
- << res:=intervalRD();
- lex();
- low:=car res;
- upper:=cadr res >>
- else errorML("<lowlimit> or <interval>",11);
- express:=mathML2();
- lex();
- return algebraic sum(express, svar, low, upper);
- end;
- algebraic procedure alg_sum( low, upper, formu);
- begin scalar temp,var2;
- algebraic;
- temp:=0;
- var2:=symbolic svar;
- for tt:=low:upper do
- << set(var2,tt);
- temp:=temp+formu;
- clear symbolic svar;
- var2:=symbolic svar>>;
- symbolic;
- return temp;
- end;
- %Returns the product.
- symbolic procedure productRD();
- begin scalar pvar, low, upper, pexpress, res;
- lex();
- if char='(b v a r) then
- <<pvar:=bvarRD();
- if eqn(cadr pvar,1) then pvar:=car pvar
- else
- errorML("<degree>",12);
- lex()>>
- else errorML("<bvar>",9);
- if char='(l o w l i m i t) then
- << low:=lowlimitRD();
- lex();
- if char='(u p l i m i t) then
- << upper:=upperlimitRD();
- lex()>>
- else errorML("<uplimit>",10)>>
- else if char='(i n t e r v a l) then
- << res:=intervalRD();
- lex();
- low:=car res;
- upper:=cadr res >>
- else errorML("<lowlimit> or <interval>",11);
- pexpress:=mathML2();
- lex();
- return algebraic prod(pexpress, pvar, low, upper);
- end;
- algebraic procedure alg_prod( low, upper, formu);
- begin scalar temp,var2;
- algebraic;
- temp:=1;
- var2:=symbolic pvar;
- for tt:=low:upper do
- << set(var2,tt);
- temp:=temp*formu;
- clear symbolic pvar;
- var2:=symbolic pvar>>;
- symbolic;
- return temp;
- end;
- %Returns the partial derivative.
- symbolic procedure partialdiffRD();
- begin scalar res, bvar, express;
- lex();
- bvar:=getargsRD();
- express:=mathML2();
- lex();
- res:=differentiate(express, bvar);
- return res;
- end;
-
- symbolic procedure differentiate(express, bvar);
- begin scalar temp,diffed;
- return
- if eqn(length bvar,0) then express
- else
- <<temp:=car bvar;
- diffed:=alg_df(express, car temp, cadr temp);
- differentiate(diffed, cdr bvar)>>;
- end;
- %This function reads through the a series of <bvar> tags and extracts the
- %variables.
- symbolic procedure getargsRD();
- begin scalar a;
- %Dont forget. This function leaves the file pointer on
- %the next token after the last bvar. So you need to use mathML2 after.
- if char='(b v a r) then
- <<a:=bvarRD();
- lex();
- return cons (a,getargsRD())>>;
- end;
- %Returns the derivative.
- symbolic procedure diffRD();
- begin scalar bvar, degree, express, res;
- lex();
- if char='(b v a r) then
- <<bvar:=bvarRD();
- degree:=cadr bvar;
- bvar:=car bvar; lex()>>
- else <<bvar:=nil; degree:=nil>>;
- express:=mathML2();
- lex();
- res:=alg_df(express, bvar, degree);
- return res;
- end;
- algebraic procedure alg_df(a,b,c);
- begin;
- return df(a,b,c);
- end;
- %This function will calculate the integral. Takes in the expression, then
- %the bound variable, and finally the limits if they exist.
- symbolic procedure intRD();
- begin scalar bvar, low, upper, int, exp;
- lex();
- if char='(b v a r) then
- <<bvar:=bvarRD();
- if eqn(cadr bvar,1) then bvar:=car bvar
- else
- errorML("",13);
- lex()>>
- else errorML("<bvar>",14);
- if char='(l o w l i m i t) then <<low:=lowlimitRD(); lex()>>
- else low:=nil;
- if char='(u p l i m i t) then <<upper:=upperlimitRD(); lex()>>
- else upper:=nil;
- if char='(i n t e r v a l) then
- <<int:=intervalRD();
- low:=car int;
- upper:=cadr int;
- lex()>>
- else int:=nil;
-
- exp:=mathML2();
- lex();
- return alg_int(exp, bvar, low, upper);
- end;
- algebraic procedure alg_int(exp, bvar, low, upper);
- begin scalar res;
- if (low='nil) AND (upper=nil) then res:= int(exp, bvar)
- else res:= int(exp,bvar,low,upper);
- return res;
- end;
- %Here we parse bound variables. The function reads the variable as well as
- %the degree if there is one.
- symbolic procedure bvarRD();
- begin scalar var, deg;
- lex();
- if char='(d e g r e e) then
- errorML("<bvar>",15);
- var:=mathML2();
- lex();
- if char='(d e g r e e) then
- << deg:=mathML();
- lex();
- if char neq '(!/ d e g r e e) then
- error("</degree>",2);
- lex()>>
- else deg:=1;
- if char='(!/ b v a r) then return list(var, deg)
- else errorML("</bvar>", 2);
- end;
- %Functions used to parse the limits of an integral, sum, or product.
- symbolic procedure lowlimitRD();
- begin scalar lowlimit;
- lowlimit:=mathML();
- lex();
- if char='(!/ l o w l i m i t) then return lowlimit
- else errorML("</lowlimit>", 2);
- end;
- symbolic procedure upperlimitRD();
- begin scalar upperlimit;
- upperlimit:=mathML();
- lex();
- if char='(!/ u p l i m i t) then return upperlimit
- else errorML("</uplimit>", 2);
- end;
- symbolic procedure intervalRD();
- begin scalar l,u;
- l:=mathML();
- u:=mathML();
- lex();
- if char='(!/ i n t e r v a l) then return list(l,u)
- else errorML("</interval>", 2);
- end;
- %Following functions just evaluate calculus functions.
- symbolic procedure lnRD();
- begin scalar a;
- a:=alg_ln(mathML());
- lex();
- return a;
- end;
- algebraic procedure alg_ln(a);
- begin;
- return ln(a);
- end;
- symbolic procedure logRD();
- begin scalar a, a1, base;
- base:=nil;
- lex();
- if char='(l o g b a s e) then
- <<base:=logbaseRD();
- lex()>>;
- a1:=mathML2();
- lex();
- a:=alg_log(a1, base);
- return a;
- end;
- algebraic procedure alg_log(a, base);
- begin;
- if base=nil then return log(a)
- else
- return logb(a, base);
- end;
- symbolic procedure logbaseRD();
- begin scalar a;
- a:=mathML();
- lex();
- if char='(!/ l o g b a s e) then return a
- else errorML("</logbase>",2);
- end;
- symbolic procedure conjugateRD();
- begin scalar a;
- a:= alg_conj(mathML());
- lex();
- return a;
- end;
- algebraic procedure alg_conj(a);
- begin;
- return conj(a);
- end;
- symbolic procedure minusRD();
- begin scalar c,b;
- c:=mathML();
- b:=mathML();
- if b=nil then c:=alg_minus(c)
- else <<
- c:=alg_difference(c,b);
- lex()>>;
- return c;
- end;
- algebraic procedure alg_minus(a);
- begin;
- return -a;
- end;
- algebraic procedure alg_difference(a,b);
- begin;
- return difference(a,b);
- end;
- symbolic procedure absRD();
- begin scalar a;
- a:=alg_abs(mathML());
- lex();
- return a;
- end;
- algebraic procedure alg_abs(a);
- begin;
- return abs(a);
- end;
- symbolic procedure rootRD();
- begin scalar b,deg;
- lex();
- if char='(d e g r e e) then
- << deg:=mathML();
- lex();
- if char neq '(!/ d e g r e e) then
- error("</degree>","Syntax ERROR: Missing end tag");
- lex()>>
- else deg:=2;
- b:=mathML2();
- lex();
- return alg_root(b,deg);
- end;
- algebraic procedure alg_root(b,a);
- begin;
- return b**(1/a);
- end;
- symbolic procedure remRD();
- begin scalar a, a1, a2;
- a1:=mathml();
- a2:=mathml();
- a:=alg_remainder(a1, a2);
- lex();
- return a;
- end;
- algebraic procedure alg_remainder(a,b);
- begin;
- return remainder(a,b);
- end;
- symbolic procedure factorialRD();
- begin scalar a;
- a:=alg_factorial(mathML());
- lex();
- return a;
- end;
- algebraic procedure alg_factorial(a);
- begin;
- return factorial(a);
- end;
- symbolic procedure expRD();
- begin scalar a;
- a:= alg_exp(mathML());
- lex();
- return a;
- end;
- algebraic procedure alg_exp(a);
- begin;
- return exp(a);
- end;
- symbolic procedure quotientRD();
- begin scalar a, a1, a2;
- a1:=mathML();
- a2:=mathML();
- if IDP reval a1 OR IDP reval a2 then a:=alg_quotient(a1,a2)
- else
- a:= (reval a1)/(reval a2);
- lex();
- return a;
- end;
- algebraic procedure alg_quotient(a,b);
- begin;
- return a/b;
- end;
- symbolic procedure divideRD();
- begin scalar a, a1, a2;
- a1:=mathML();
- a2:=mathML();
- if a2 = 0 then errorML("", 21);
- a:=alg_divide(a1,a2);
- lex();
- return a;
- end;
- algebraic procedure alg_divide(a,b);
- begin;
- return quotient(a,b);
- end;
- symbolic procedure gcdRD();
- begin scalar c1;
- c1:=stats_getargs();
- constants(c1);
- if temp2='stop then
- << temp2:=nil;
- return cons('gcd, eval_list(c1))>>
- else return gcdRD2(c1);
- end;
- symbolic procedure gcdRD2(args);
- begin scalar a;
- a:=reval car args;
- return if length args=1 then car args
- else alg_gcd2(a, gcdRD2(cdr args));
- end;
- algebraic procedure alg_gcd2(a , b);
- begin;
- return gcd(a,b);
- end;
- symbolic procedure minRD();
- begin scalar a;
- a:=mathML();
- return if a=nil then nil
- else alg_min(a,minRD());
- end;
- algebraic procedure alg_min(a,b);
- begin;
- return min(b,a);
- end;
- symbolic procedure maxRD();
- begin scalar a;
- a:=mathML();
- return if a=nil then nil
- else alg_max(a,maxRD());
- end;
- algebraic procedure alg_max(a,b);
- begin;
- return max(a,b)
- end;
-
- lisp operator plusRD;
- symbolic procedure plusRD();
- begin scalar abc1;
- abc1:=nil;
- abc1:=mathML();
- return if abc1 = nil then 0
- else alg_plus(abc1, plusRD());
- end;
- algebraic procedure alg_plus(acb1,b);
- begin;
- return acb1+b;
- end;
- symbolic procedure timesRD();
- begin scalar a;
- a:=nil;
- a:=mathML();
- return if a=nil then 1
- else alg_times(a, timesRD());
- end;
- algebraic procedure alg_times(a,b);
- begin;
- if b=i then return a*i;
- return a*b;
- end;
- symbolic procedure powerRD();
- begin scalar var,power;
- var:=mathML();
- power:=mathML();
- lex();
- return alg_expt(var,power);
- end;
- algebraic procedure alg_expt(a,b);
- begin;
- return expt(a,b);
- end;
- %The following function is in charge of providing the correct error message
- %as well as closing the input/output stream, and exiting the program
- %correctly.
-
- symbolic procedure errorML( str, msg );
- begin;
- terpri();
- princ "***** Error in token number ";
- princ count;
- princ " (<";
- princ compress char;
- princ ">)";
- terpri();
- if msg=1 then
- << princ "Needed attribute";
- princ str;
- princ " and none was found.">> else
- if msg=2 then
- << princ "Missing tag: ";
- princ str >> else
- if msg=3 then
- << princ "Undefined error!";
- princ " Token number "; princ sub1 count;
- princ " probably mispelled or an";
- princ "ambiguous or erroneous use of <apply></apply>.">> else
- if msg=4 then
- << princ "Numerical constant ";
- princ str;
- princ " was enclosed between <ci></ci> tags.";
- terpri();
- princ "Correct syntax: <cn>";
- princ str;
- princ "</cn>.">> else
- if msg=5 then
- << princ "All arguments must be sets";
- terpri();
- princ str;
- princ " does not represent a set.">> else
- if msg=6 then
- << princ "Non-numeric argument in arithmetic.">> else
- if msg=7 then
- << princ "The degree quantifier is of no use in the sumation";
- princ "operator.">> else
- if msg=8 then
- << princ "The degree quantifier is of no use in the limit";
- princ " operator.">> else
- if msg=9 then
- << princ "The index of sumation has not been specified.";
- terpri();
- princ "Please use <bvar></bvar> tags to specify an index.">>
- else
- if msg=10 then
- << princ "Upperlimit not specified.">> else
- if msg=11 then
- << princ "Upper and lower limits have not been specified.">> else
- if msg=12 then
- << princ "The degree quantifier is of no use in the product";
- princ " operator.">> else
- if msg=13 then
- << princ "The degree quantifier is not allowed in the integral";
- princ " operator.">> else
- if msg=14 then
- << princ "Variable of integration not specified.";
- princ "Please use <bvar></bvar> tags to specify variable.">>
- else
- if msg=15 then
- << princ "Incorrect use of <bvar></bvar> tags.";
- princ " Correct use:";
- terpri();
- princ
- "<bvar> bound_var </bvar> [<degree> degree </degree>] </bvar>">> else
- if msg=16 then
- << princ "Symbolic constant ";
- princ str;
- princ " was enclosed between <cn></cn> tags.";
- terpri();
- princ "Correct syntax: <ci> ";
- princ str;
- princ " </ci>";
- terpri();
- princ "or <cn type=""constant""> </cn>";
- princ "if using constants ⅈ, ⅈ, ⅇ, ⅇ or π."
- >> else
- if msg=17 then
- << princ "Unknown tag: <";
- princ str;princ ">.";
- terpri();
- princ "Token not allowed within <apply></apply> tags.";
- terpri();
- princ "Might be: <"; princ str; princ "/>.">> else
- if msg=18 then
- << princ "Unknown tag: <";
- princ str;princ ">.";
- terpri();
- princ "Not allowed within <reln></reln> tags.">> else
- if msg=19 then
- << princ "Undefined error!";
- princ " Token "; princ sub1 count;
- princ " is probably mispelled";
- terpri();
- princ "or unknown, ";
- princ "or the </math> tag is missing">> else
- if msg=20 then
- << princ "Function ";
- princ str;
- princ "()";
- princ " was not enclosed in <ci></ci> tags.";
- terpri();
- princ "Correct syntax: <fn><ci>";
- princ str;
- princ "</ci></fn>.">> else
- if msg=21 then
- << princ "Error, division by 0">>;
-
-
- terpri();
- if FILE!*=t then close rds f;
- FILE!*:=nil;
- rederr("");
- rederr("");
- terpri();
- end;
- %Following function are in charge of parsing statistics related mathml.
- symbolic procedure meanRD();
- begin scalar b, size, args;
- args:=stats_getargs();
- b:=0;
- size:=length( args );
- while (args neq ()) do
- << b:=alg_plus(b, car args);
- args:= cdr args >>;
- return alg_quotient(b,size);
- end;
- symbolic procedure sdevRD( );
- begin scalar args,mean,b,size;
- args:=stats_getargs();
- mean:=alg_mean( args );
- size:=length(args);
- while(args neq ()) do
- << b:=alg_plus(b, alg_expt(alg_difference(car args, mean),2));
- args:=cdr args; >>;
- return b;
- end;
- symbolic procedure varRD( );
- begin scalar args;
- args:=stats_getargs();
- return alg_expt(sdev( args ), 2);
- end;
- symbolic procedure medianRD( );
- begin scalar args, siz, si;
- args:=stats_getargs();
- args:=cons('list, args);
- args:=sortl(args);
- args:=cdr args;
- si:=length args;
- siz:=si/2;
- if remainder(si,2)=0 then
- return alg_quotient(alg_plus(nth(args,siz),nth(args,(siz+1))),2)
- else return nth(args, siz);
- end;
- algebraic procedure sortl(args);
- begin scalar rr;
- rr:=sortlist(args, pred);
- if rr=nil then return sortnumlist(args)
- else return rr;
- end;
- symbolic procedure momentRD( );
- begin scalar args,size,d,i;
- args:=stats_getargs();
- if char='(d e g r e e) then
- <<i:=mathML();
- lex();
- if char='(!/ d e g r e e) then lex()
- else errorML("</degree>",2)>>
- else i:=1;
- d:=();
- size:=length args;
- while args neq () do
- << d:=cons(alg_expt(car args, i),d);
- args:=cdr args>>;
- return alg_mean(d);
- end;
- symbolic procedure alg_mean ( args );
- begin scalar b, size, args;
- b:=0;
- size:=length( args );
- while (args neq ()) do
- << b:=alg_plus(b, car args);
- args:= cdr args >>;
- return alg_quotient(b,size);
- end;
- symbolic procedure sdev( args );
- begin scalar mean,b,size;
- mean:=alg_mean( args );
- size:=length(args);
- while(args neq ()) do
- << b:=alg_plus(b, alg_expt(alg_difference(car args, mean),2));
- args:=cdr args; >>;
- return b;
- end;
- %The following function gets all arguments from the mathml input.
- symbolic procedure stats_getargs();
- begin scalar ww;
- ww:=nil;
- ww:=mathML();
- if ww neq nil then <<
- return cons (ww,stats_getargs())>>;
- end;
- %Transforms polar-complex to cartesian-complex.
- symbolic procedure po2ca(r,p);
- begin scalar theta,x,y;
- theta:=rad p;
- x:=r*cos(theta);
- y:=r*sin(theta);
- return(list(x,y))
- end;
- symbolic procedure rad(mu); %note approx. pi
- begin scalar b;
- b:=mu*3.141529/180;
- return b
- end;
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %Here start the functions in charge of pasing reduce's output and printing%
- %it out in mathml. %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %This the mathml printer which reads reduce output and translates it to
- %mathml.
- symbolic procedure math_ml_printer (mode,u);
- <<
- if !*both=t then
- (<< if u=t then else maprin(u); terpri!* nil>>) where outputhandler!* := nil;
- if mode neq 'terpri then
- << % FLUID '(indent, flagg,found_int, found_compl, consts_compl, consts_int);
- % FLUID '(found_mat_int, found_mat_compl, consts_mat_int, consts_mat_compl);
- found_mat_int=0$;
- found_mat_compl=0$;
- indent:=0$
- consts_compl:=()$
- consts_mat_compl:=()$
- consts_int:=()$
- consts_mat_int:=()$
- found_int:=0$
- found_compl:=0$
- flagg:=0$
- if (PAIRP u) then <<
- if !*web=t then printout("<EMBED TYPE=""text/mathml"" MMLDATA=""");
- printout("<math>");
- indent:=3;
- if ((car u)='setq) then
- <<if (PAIRP caddr u) then
- if (issq(caddr u)=1) then arbitrary_c( PREPSQ cadr caddr u )
- else
- if (caaddr u='mat) then arbitrary_c(caddr u)
- else
- if (caaddr u='list) then arbitrary_c( !*a2k caddr u);
- setqML( u )>>
- else
- if ((car u)='list) then
- << arbitrary_c( !*a2k u );
- listML(cdr u)>>
- else
- if ((car u)='mat) then
- << arbitrary_c( u );
- matrixML(cdr u)>>
- else
- if ((car u)='!*sq) then
- << arbitrary_c(PREPSQ (cadr u));
- expression(PREPSQ (cadr u))>>
- else expression(u);
- indent:=indent-3;
- close_forall();
- indent:=0;
- printout( "</math>" );
- if !*web=t then princ(""" HEIGHT=300 WIDTH=500>");
- terpri()
- >>
- else
- if (ATOM u) then <<
- if !*web=t then printout("<EMBED TYPE=""text/mathml"" MMLDATA="" ");
- printout( "<math>" );
- indent:=3;
- expression( u );
- indent:=0;
- printout( "</math>" );
- if !*web=t then princ(" "" HEIGHT=300 WIDTH=500>");
- terpri() >>
- else ; >> >>;
-
- %Prints out vectors.
- symbolic procedure vectorML( elem );
- begin;
- printout("<vector>");
- indent:=indent+3;
- multi_elem(car elem);
- indent:=indent-3;
- printout("</vector>")
- end;
- %Following functions print out matrices.
- symbolic procedure matrixML( elem );
- begin;
- if length elem=1 then vectorML( elem )
- else
- << printout("<matrix>");
- indent:=indent+3;
- matrix_rows(elem);
- indent:=indent-3;
- printout("</matrix>")
- >>;
- end;
- symbolic procedure matrix_rows( elem );
- begin;
- if (elem neq()) then
- << printout("<matrixrow>");
- indent:=indent+3;
- row(car elem);
- indent:=indent-3;
- printout("</matrixrow>");
- matrix_rows( cdr elem ); >>
- end;
- symbolic procedure row( elem );
- begin;
- if (elem neq()) then
- << expression(car elem); row(cdr elem);>>
- end;
- %This function searches for arbitrary integers, or complex in the reduce
- %output. If so, it declares these variables in a forall statement.
- symbolic procedure arbitrary_c( elem );
- begin;
- found_int:=nil;
- found_mat_int:=nil;
- found_compl:=nil;
- found_mat_compl:=nil;
- if (PAIRP elem) then <<
- if (car elem='mat) then
- << isarb_mat_compl(cdr elem);
- isarb_mat_int(cdr elem)>>
- else
- << isarb_compl(elem);
- isarb_int(elem)>>;
- if ((found_compl=1) OR (found_int=1)) then
- << flagg:=1;
- printout( "<apply><forall/>" );
- indent:=indent+3;
- print_arb_compl(elem);
- print_arb_int(elem);
- printout( "<condition>");
- indent:=indent+3;
- if ((found_compl=1) AND (found_int=1)) then
- << printout( "<apply><and/>" );
- indent:=indent+3>>
- else
- if ((length consts_compl) > 1) then
- << printout( "<apply><and/>" );
- indent:=indent+3>>
- else
- if ((length consts_int) > 1) then
- << printout( "<apply><and/>" );
- indent:=indent+3>>;
- if (found_compl=1) then
- in_complexML( consts_compl );
- if (found_int=1) then
- in_integerML( consts_int );
- if ((found_compl=1) AND (found_int=1)) then
- << indent:=indent-3;
- printout( "</apply>" )>>
- else
- if ((length consts_compl) > 1) then
- << indent:=indent-3;
- printout( "</apply>" )>>
- else
- if ((length consts_int) > 1) then
- << indent:=indent-3;
- printout( "</apply>" )>>;
- indent:=indent-3;
- printout( "</condition>" )>>;
- if ((found_mat_compl=1) OR (found_mat_int=1)) then
- << flagg:=1;
- printout( "<apply><forall/>" );
- indent:=indent+3;
- printarb_mat_compl(cdr elem);
- printarb_mat_int(cdr elem);
- printout( "<condition>");
- indent:=indent+3;
- if ((found_mat_compl=1) AND (found_mat_int=1)) then
- << printout( "<apply><and/>" );
- indent:=indent+3>>
- else
- if ((length consts_mat_compl) > 1) then
- << printout( "<apply><and/>" );
- indent:=indent+3>>
- else
- if ((length consts_mat_int) > 1) then
- << printout( "<apply><and/>" );
- indent:=indent+3>>;
- if (found_mat_compl=1) then
- in_complexML( consts_mat_compl );
- if (found_mat_int=1) then
- in_integerML( consts_mat_int );
- if ((found_mat_compl=1) AND (found_mat_int=1)) then
- << indent:=indent-3;
- printout( "</apply>" )>>
- else
- if ((length consts_mat_compl) > 1) then
- << indent:=indent-3;
- printout( "</apply>" )>>
- else
- if ((length consts_mat_int) > 1) then
- << indent:=indent-3;
- printout( "</apply>" )>>;
- indent:=indent-3;
- printout( "</condition>" )>>;
- >>
- end;
- symbolic procedure in_complexML( elem );
- begin;
- if (elem neq ()) then <<
- printout("<reln><in/>");
- indent:=indent+3;
- printsub2( car elem, 'compl );
- printout("<complexes/>");
- % printout("<ci type=""set""> C </ci>");
- indent:=indent-3;
- printout("</reln>");
- in_complexML( cdr elem )>>;
- end;
- symbolic procedure in_integerML( elem );
- begin;
- if (elem neq ()) then <<
- printout("<reln><in/>");
- indent:=indent+3;
- printsub2( car elem, 'int );
- printout("<integers/>");
- % printout("<ci type=""set""> Z </ci>");
- indent:=indent-3;
- printout("</reln>");
- in_integerML( cdr elem )>>;
- end;
- symbolic procedure close_forall();
- begin;
- if (flagg=1) then printout("</apply>");
- end;
- %Prints out setq statements as <declare> statements.
- symbolic procedure setqML( elem );
- begin;
- printout( "<declare>" );
- indent:=indent+3;
- expression(cadr elem);
- expression( caddr elem);
- indent:=indent-3;
- printout( "</declare>" );
- end;
- %Prints out lists.
- symbolic procedure listML( elem );
- begin;
- printout( "<list>" );
- indent:=indent+3;
- multilists( elem );
- indent:=indent-3;
- printout( "</list>" );
- end;
- symbolic procedure multilists( elem );
- begin;
- if elem neq nil then
- if ((LENGTH elem)=1) then expression (car elem)
- else <<expression(car elem); multilists(cdr elem);>>
- end;
- %This function takes in a reduce expression, and parses it. It also takes
- %expressions created by the above program.
- symbolic procedure expression( elem );
- begin scalar aa;
- if (ATOM elem) then f4( elem ) else
- if car elem='!:RD!: then <<printout elem>> else
- <<
-
- if (aa:=assoc(car elem, unary!*)) then <<
- if caddr aa = nil then
- apply(cadr aa, list cdr elem)
- else
- apply(cadr aa, list(cdr elem, caddr aa)) >> else
- if ((car elem)= '!*sq) then expression (PREPSQ (cadr elem)) else
- operator_fn(elem);>>;
- end;
- %Prints out sum, or products.
- symbolic procedure sum_prodML( elem, tty );
- begin;
- printout("<apply>");
- princ "<"; princ tty; princ "/>";
- indent:=indent+3;
- printout("<bvar>");
- indent:=indent+3;
- expression( cadr elem );
- indent:=indent-3;
- printout("</bvar>");
- printout("<lowlimit>");
- indent:=indent+3;
- expression( caddr elem );
- indent:=indent-3;
- printout("</lowlimit>");
- printout("<uplimit>");
- indent:=indent+3;
- expression( cadddr elem );
- indent:=indent-3;
- printout("</uplimit>");
- expression car elem;
- indent:=indent-3;
- printout("</apply>");
- end;
- %Prints out derivatives.
- symbolic procedure dfml( elem );
- begin scalar test;
- test:=cdr elem;
- if length test=1 OR (length test=2 AND NUMBERP cadr test) then
- printout("<apply><diff/>")
- else
- printout("<apply><partialdiff/>");
- indent:=indent+3;
- dfargs(cdr elem); % FJW: two statements swapped
- expression(car elem);
- indent:=indent-3;
- printout("</apply>");
- end;
- symbolic procedure dfargs( elem );
- begin;
- if elem neq nil then
- << if length elem>1 then
- << if NUMBERP cadr elem then
- <<printout("<bvar>");
- indent:=indent+3;
- expression car elem;
- degreeML(cadr elem);
- indent:=indent-3;
- printout("</bvar>");
- dfargs(cddr elem)>>
- else
- <<printout("<bvar>");
- indent:=indent+3;
- expression car elem;
- indent:=indent-3;
- printout("</bvar>");
- dfargs(cdr elem)>>; >>
- else
- << printout("<bvar>");
- indent:=indent+3;
- expression car elem;
- indent:=indent-3;
- printout("</bvar>");
- dfargs(cdr elem)>> >>;
- end;
- %Prints out degree statements.
- symbolic procedure degreeML( elem );
- begin;
- printout("<degree>");
- indent:=indent+3;
- expression( elem );
- indent:=indent-3;
- printout("</degree>");
- end;
-
- symbolic procedure complpart( elem, tty);
- begin;
- printout("<apply><fn><");
- princ tty;
- princ "></fn>";
- indent:=indent+3;
- expression(car elem);
- indent:=indent-3;
- printout("<apply>");
- end;
- %Prints out set theory related functions.
- symbolic procedure sets(elem, tty);
- begin;
- printout("<apply>");
- princ "<"; princ tty; princ "/>";
- indent:=indent+3;
- multi_elem( elem );
- indent:=indent-3;
- printout("</apply>");
- end;
- %Prints out relns.
- symbolic procedure reln(elem, tty);
- begin;
- printout("<reln>");
- princ "<"; princ tty; princ "/>";
- indent:=indent+3;
- multi_elem( elem );
- indent:=indent-3;
- printout("</reln>");
- end;
- %Prints out a set.
- symbolic procedure setML( elem );
- begin;
- printout("<set>");
- indent:=indent+3;
- multi_elem( elem );
- indent:=indent-3;
- printout("</set>");
- end;
- %Prints out unknown functions as a function. It prints out all variables
- %declared a soperators.
- symbolic procedure operator_fn( elem );
- begin scalar asso;
- asso := atsoc(car elem, '((arcsinh . sinh)(arcsech . sech)
- (arccosh . cosh)(arccsch csch) (arctanh . tanh)
- (arccoth . coth)) );
- if asso then <<
- printout "<apply><inverse/>";
- princ cdr asso; >>
- else <<
- printout("<apply><fn><ci>");
- princ car elem;
- princ "</ci></fn>" >>;
- indent:=indent+3;
- multi_args(cdr elem);
- indent:=indent-3;
- printout("</apply>");
- end;
- %Reads through a list and prints out each component.
- symbolic procedure multi_args( elem );
- begin;
- if (elem neq ()) then <<expression(car elem); multi_args( cdr elem );>>
- end;
- %Prints out all trigonometric functions which have not the same tag name,
- %as reduce function.
- symbolic procedure trigML(elem, type);
- begin;
- printout("<apply>");
- if ((type='acos) OR (type='asin) OR (type='atan)) then
- << if (type='acos) then princ "<arccos/>";
- if (type='asin) then princ "<arcsin/>";
- if (type='atan) then princ "<arctan/>">>;
- indent:=indent+3;
- expression(car elem);
- indent:=indent-3;
- printout("</apply>");
- end;
- %Prints out all unary functions such as log, or many trig functions.
- symbolic procedure unary( elem, type );
- begin;
- printout("<apply>");
- princ "<";
- princ type;
- princ "/>";
- indent:=indent+3;
- expression(car elem );
- indent:=indent-3;
- printout("</apply>");
- end;
- %Prints out logs with a base.
- symbolic procedure log_baseML(elem, type);
- begin;
- printout("<apply><log/>");
- indent:=indent+3;
- printout("<logbase>");
- indent:=indent+3;
- if (type='logb) then expression(cadr elem);
- if (type='log10) then f4(10);
- indent:=indent-3;
- printout("</logbase>");
- expression(car elem);
- indent:=indent-3;
- printout("<apply>");
- end;
- %Prints out equal relns.
- symbolic procedure equalML( elem );
- begin;
- printout( "<reln><eq/>" );
- indent:=indent+3;
- expression(car elem);
- expression(cadr elem);
- indent:=indent-3;
- printout( "</reln>" );
- end;
- %Prints out square roots.
- symbolic procedure sqrtML( elem , type);
- begin;
- printout( "<apply><root/>" );
- indent:=indent+3;
- printout( "<degree><cn> 2 </cn></degree>" );
- expression( car elem );
- indent:=indent-3;
- printout( "</apply>" );
- end;
- %Prints out integrals.
- symbolic procedure integralML( elem );
- begin;
- printout( "<apply><int/>" );
- indent:=indent+3;
- printout( "<bvar>" );
- indent:=indent+3;
- expression (cadr elem);
- indent:=indent-3;
- printout( "</bvar>" );
- if (length cdr elem >1) then
- << printout("<lowlimit>");
- indent:=indent+3;
- expression( caddr elem );
- indent:=indent-3;
- printout("</lowlimit>");
- printout("<uplimit>");
- indent:=indent+3;
- expression( cadddr elem );
- indent:=indent-3;
- printout("</uplimit>")>>;
- expression( car elem );
- indent:=indent-3;
- printout( "</apply>" );
- end;
- %Prints out quotients.
- symbolic procedure quotientML( elem );
- begin;
- if (NUMBERP car elem) AND (NUMBERP cadr elem) then <<
- if !*web=nil then printout("<cn type=""rational""> ")
- else printout("<cn type="rational"> ");
- princ car elem;
- princ " <sep/> ";
- princ cadr elem;
- princ " </cn>">>
- else <<
- printout( "<apply><divide/>" );
- indent:=indent+3;
- expression( car elem );
- expression( cadr elem );
- indent:=indent-3;
- printout( "</apply>" )>>;
- end;
- %Prints out all n_nary functions.
- symbolic procedure n_nary( elem, type );
- begin;
- if car elem = 'e AND type = 'power then unary(cdr elem, 'exp)
- else <<
- printout( "<apply>" );
- princ "<";
- princ type;
- princ "/>";
- indent:=indent+3;
- multi_elem( elem );
- indent:=indent-3;
- printout( "</apply>" )>>
- end;
- symbolic procedure multi_elem( elem );
- begin;
- if ((length elem)=1) then expression( car elem )
- else <<expression( car elem ); multi_elem( cdr elem );>>
- end;
- symbolic procedure minusML( elem );
- begin;
- printout( "<apply><minus/>" );
- indent:=indent+3;
- multiminus( elem );
- indent:=indent-3;
- printout( "</apply>" );
- end;
- symbolic procedure multiminus( elem );
- begin;
- expression(car elem);
- if ((length elem)=2) then expression (cadr elem);
- end;
- %Prints out all pieces of data: i.e terminal symbols.
- %They can be numbers, identifiers, or constants.
- symbolic procedure f4(exp);
- begin;
- if (exp='pi) then
- princ "<pi/>"
- else if (exp='euler_gamma) then
- princ "<eulergamma/>"
- else if (exp='true) then
- princ "<true/>"
- else if (exp='false) then
- princ "<false/>"
- else if (exp='!Na!N) then
- princ "<notanumber/>"
- else
- if (exp='infinity) then
- << if !*web=nil then printout("<cn type=""constant"">")
- else printout("<cn type="constant">");
- princ "∞";
- princ "</cn>">>
- else <<
- if (exp='e) then
- << if !*web=nil then printout("<cn type=""constant"">")
- else printout("<cn type="constant">");
- princ "ⅇ";
- princ "</cn>">>
- else <<
- if (exp='i) then
- << if !*web=nil then printout("<cn type=""constant"">")
- else printout("<cn type="constant">");
- princ "ⅈ";
- princ "</cn>">>
- else <<
- if (NUMBERP exp) then
- << printout "<cn";
- if (FLOATP exp) then
- <<if !*web=nil then princ " type=""real"">"
- else princ " type="real">" >>
- else
- if (FIXP exp) then
- <<if !*web=nil then princ " type=""integer"">"
- else princ " type="integer">" >>
- else princ ">";
- princ exp;
- princ "</cn>">>;
- if (IDP exp) then
- << printout "<ci";
- if (listp exp) then
- <<if !*web=nil then princ " type=""list"">"
- else princ " type="list">">>
- else
- if (vectorp exp) then
- <<if !*web=nil then princ " type=""vector"">"
- else princ " type="vector">">>
- else princ ">";
- princ exp;
- princ "</ci>">>;
- >>
- >>
- >>
- end;
- %Functions used to print out variables with a subscript.
- symbolic procedure printsub( subscript, type );
- begin;
- printout("<bvar>");
- indent:=indent+3;
- printout("<ci>");
- indent:=indent+3;
- printout( "<msub>" );
- indent:=indent+3;
- if (type='compl) then printout( "<mi>c</mi>" );
- if (type='int) then printout( "<mi>d</mi>" );
- printout( "<mn>" );
- princ subscript;
- princ "</mn>";
- indent:=indent-3;
- printout( "</msub>" );
- indent:=indent-3;
- printout("</ci>");
- indent:=indent-3;
- printout("</bvar>");
- end;
- symbolic procedure printsub2( subscript, type );
- begin;
- printout("<ci>");
- indent:=indent+3;
- printout( "<msub>" );
- indent:=indent+3;
- if (type='compl) then printout( "<mi>c</mi>" );
- if (type='int) then printout( "<mi>d</mi>" );
- printout( "<mn>" );
- princ subscript;
- princ "</mn>";
- indent:=indent-3;
- printout( "</msub>" );
- indent:=indent-3;
- printout("</ci>");
- end;
- %Prints out expressions in math form. Plagiarised from reduce code of
- %mathprint
- symbolic procedure ma_print l;
- begin scalar temp;
- temp:=outputhandler!*;
- outputhandler!*:=nil;
- terpri!* nil;
- if !*web=nil then maprin "<cn type=""real"">"
- else maprin "<cn type="real">";
- maprin l;
- maprin "</cn>";
- terpri!* nil;
- outputhandler!*:=temp;
- end;
- %Function in charge of doing all printing in order to make sure the
- %indentation is always correct.
- symbolic procedure printout( str );
- begin;
- if !*web = nil then terpri();
- if !*web = nil then for i := 1:indent
- do << princ " " >>;
- if PAIRP str then
- <<if car str='!:rd!: OR car str='!:rn!: then ma_print str
- else princ str>>
- else princ str;
- end;
- %Following functions are quite obscure. They find arbitrary constants in
- %expressions and matrices. Then record them, and everytime they appear, are
- %replaced with a fancy subscripts C, or D.
- symbolic procedure issq( elem );
- begin scalar value;
- value:=0;
- if (ATOM elem) then value:=0
- else <<if ((car elem)='!*sq) then value:=1
- else value:=0>>;
- return value;
- end;
- symbolic procedure isarb_compl( elem );
- begin;
- if (PAIRP elem) then <<
- if ((car elem)= 'arbcomplex) then found_compl:=1
- else multi_isarb_compl(cdr elem);>>
- end;
- symbolic procedure multi_isarb_compl( elem );
- begin;
- if (PAIRP elem) then <<
- if (elem=()) then
- else <<isarb_compl(car elem); multi_isarb_compl( cdr elem);>> >>
- end;
- symbolic procedure isarb_int( elem );
- begin;
- if (PAIRP elem) then <<
- if ((car elem)= 'arbint) then found_int:=1
- else multi_isarb_int(cdr elem);>>
- end;
- symbolic procedure multi_isarb_int( elem );
- begin;
- if (PAIRP elem) then <<
- if (elem=()) then
- else <<isarb_int(car elem); multi_isarb_int( cdr elem);>> >>
- end;
- symbolic procedure print_arb_compl( elem );
- begin;
- if (PAIRP elem) then <<
- if ((car elem)= 'arbcomplex) then
- << if (xnp(list (cadr elem),consts_compl) eq nil) then
- << printsub(cadr elem, 'compl);
- consts_compl:=cons(cadr elem, consts_compl)>> >>
- else multi_compl(cdr elem);>>
- end;
- symbolic procedure multi_compl( elem );
- begin;
- if (elem=()) then
- else <<print_arb_compl(car elem); multi_compl( cdr elem);>>
- end;
- symbolic procedure print_arb_int( elem );
- begin;
- if (PAIRP elem) then <<
- if ((car elem)= 'arbint) then
- << if (xnp(list (cadr elem),consts_int) eq nil) then
- << printsub(cadr elem, 'int);
- consts_int:=cons(cadr elem, consts_int)>> >>
- else multi_int(cdr elem);>>
- end;
- symbolic procedure multi_int( elem );
- begin;
- if (elem=()) then
- else <<print_arb_int(car elem); multi_int( cdr elem);>>
- end;
- symbolic procedure isarb_mat_int( elem );
- begin;
- if (elem neq()) then
- << isarb_row_int(car elem);
- isarb_mat_int( cdr elem ); >>
- end;
- symbolic procedure isarb_row_int( elem );
- begin;
- if (elem neq()) then
- << if (issq(car elem)=1) then
- if (PAIRP (PREPSQ cadr (car elem))) then
- if (car (PREPSQ cadr (car elem))='arbint) then
- found_mat_int:=1;
- isarb_row_int(cdr elem);>>
- end;
- symbolic procedure isarb_mat_compl( elem );
- begin;
- if (elem neq()) then
- <<
- isarb_row_compl(car elem);
- isarb_mat_compl( cdr elem ); >>
- end;
- symbolic procedure isarb_row_compl( elem );
- begin;
- if (elem neq()) then
- << if (issq(car elem)=1) then
- if (PAIRP (PREPSQ cadr (car elem))) then
- if (car (PREPSQ cadr (car elem))='arbcomplex) then
- found_mat_compl:=1;
- isarb_row_compl(cdr elem);>>
- end;
- symbolic procedure printarb_mat_compl( elem );
- begin;
- if (elem neq()) then
- << printarb_row_compl(car elem);
- printarb_mat_compl( cdr elem ); >>
- end;
- symbolic procedure printarb_row_compl( elem );
- begin scalar value;
- if (elem neq()) then
- << if (issq(car elem)=1) then
- if (PAIRP (PREPSQ cadr (car elem))) then
- << value:=cadr PREPSQ cadr car elem;
- if (car (PREPSQ cadr (car elem)))='arbcomplex then
- if (xnp(list (value), consts_mat_compl) eq nil) then
- << printsub(value, 'compl);
- consts_mat_compl:=cons(value, consts_mat_compl)>> >>;
- printarb_row_compl(cdr elem);>>
- end;
- symbolic procedure printarb_mat_int( elem );
- begin;
- if (elem neq()) then
- <<
- printarb_row_int(car elem);
- printarb_mat_int( cdr elem ); >>
- end;
- symbolic procedure printarb_row_int( elem );
- begin scalar value;
- if (elem neq()) then
- << if (issq(car elem)=1) then
- if (PAIRP (PREPSQ cadr (car elem))) then
- << value:=cadr PREPSQ cadr car elem;
- if (car (PREPSQ cadr (car elem)))='arbint then
- if (xnp(list (value), consts_mat_int) eq nil) then
- << printsub(value, 'int);
- consts_mat_int:=cons(value, consts_mat_int)>> >>;
- printarb_row_int(cdr elem);>>
- end;
- %Following function is the same as math_ml_printer, just that it prints out
- %input given from mml, which reads from files, and not form the reduce
- %normal output stream.
-
- symbolic procedure math_ml (u);
- << % FLUID '(indent flagg found_int found_compl consts_compl
- % consts_int !*mathprint);
- % FLUID '(found_mat_int found_mat_compl consts_mat_int
- % consts_mat_compl);
- !*mathprint:=0;
- found_mat_int=0$;
- found_mat_compl=0$;
- indent:=0$
- consts_compl:=()$
- consts_mat_compl:=()$
- consts_int:=()$
- consts_mat_int:=()$
- found_int:=0$
- found_compl:=0$
- flagg:=0$
- if (PAIRP u) then <<
- printout("<math>");
- indent:=3;
- if ((car u)='setq) then
- <<if (PAIRP caddr u) then
- if (issq(caddr u)=1) then arbitrary_c( PREPSQ cadr caddr u )
- else
- if (caaddr u='mat) then arbitrary_c(caddr u)
- else
- if (caaddr u='list) then arbitrary_c( !*a2k caddr u);
- setqML( u )>>
- else
- if ((car u)='list) then
- << arbitrary_c( !*a2k u );
- listML(cdr u)>>
- else
- if ((car u)='mat) then
- << arbitrary_c( u );
- matrixML(cdr u)>>
- else
- if ((car u)='!*sq) then
- << arbitrary_c(PREPSQ (cadr u));
- expression(PREPSQ (cadr u))>>
- else expression(u);
- indent:=indent-3;
- close_forall();
- indent:=0;
- printout( "</math>" )
- >>
- else
- if (ATOM u) then <<
- printout( "<math>" );
- indent:=3;
- expression( u );
- indent:=0;
- printout( "</math>" )>>
- else ; >>;
-
- %This function executes certain commands when switches state are changed.
- %It will change the outputhandler!* when mathml is set to on or both is set
- %to on. And then modify it accroding to the switches states.
- symbolic procedure onoff(u,bool);
- begin scalar x,y;
- if not idp u then typerr(u,"switch")
- else if not flagp(u,'switch)
- then rerror(rlisp,25,list(u,"not defined as switch"));
- x := intern compress append(explode '!*,explode u);
- if !*switchcheck and lispeval x eq bool then return nil
- else if y := atsoc(bool,get(u,'simpfg))
- then lispeval('progn . append(cdr y,list nil));
- if bool and x eq '!*!r!a!i!s!e then x := '!*raise; % Special case.
- if x='!*web AND bool=t then
- outputhandler!*:='math_ml_printer;
- if x='!*web AND bool=nil then
- if !*mathml neq t then outputhandler!*:=nil;
- if x='!*mathml AND bool=t then
- outputhandler!*:='math_ml_printer;
- if x='!*mathml AND bool=nil then
- if !*both=nil then
- outputhandler!*:=nil;
- if x='!*both AND bool=t then
- outputhandler!*:='math_ml_printer;
- if x='!*both AND bool=nil then
- if !*mathml=nil then
- outputhandler!*:=nil
- else outputhandler!*:='math_ml_printer;
- set(x,bool);
- end;
- lisp operator mml;
- lisp operator parseml;
- endmodule;
- end;
|