mathml.red 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082
  1. module mathml;
  2. % Version 5 August 1999
  3. % Modified by FJW, 22 May 2000
  4. % Modified by Winfried Neun , 1 August 2000
  5. % Modified by Winfried Neun , 18 December 2000
  6. fluid '(atts ch cha char count file!* pvar rdci!* rdelems!* rdlist!*
  7. rdreln!* space safe_atts temp2 unary!* !*mathprint
  8. consts_compl consts_int flagg found_compl found_int consts_mat_int
  9. consts_mat_compl found_mat_compl found_mat_int indent);
  10. %Declaration of some switches.
  11. %_mathml_ allows all output to be printed in mathml.
  12. %_both_ allows all output to be printed in mathml and in normal reduce
  13. %output.
  14. load!-package 'assist;
  15. load!-package 'ineq;
  16. load!-package 'matrix;
  17. roundconstants();
  18. global '(f);
  19. global '(!*mathml);
  20. switch mathml;
  21. global '(!*both);
  22. switch both;
  23. global '(!*web);
  24. switch web;
  25. LISP (FILE!*:=nil);
  26. !*mathml:=nil;
  27. !*both:=nil;
  28. !*web:=nil;
  29. off both;
  30. off mathml;
  31. off web;
  32. %Declaration of a series of lists which contain the function to be executed
  33. %when the token (cadr) is found.
  34. %Tokens to be found between <ci></ci> tags.
  35. RDci!*:='
  36. ((!&imaginaryi!; . (consts 'i))
  37. (!&ii!; . (consts 'i))
  38. (!&exponential!; . (consts 'e))
  39. (!&ee!; . (consts 'e))
  40. (!&pi!; . (consts 'p))
  41. (!&differentiald!; . (const 'd))
  42. (!&dd!; . (consts 'd)));
  43. %Tokens to be found between <reln></reln> tags.
  44. RDreln!*:=
  45. '((tendsto . (tendstoRD ))
  46. (eq!/ . (relationRD 'eq))
  47. (neq!/ . (relationRD 'neq))
  48. (lt!/ . (relationRD 'lt))
  49. (gt!/ . (relationRD 'gt))
  50. (geq!/ . (relationRD 'geq))
  51. (leq!/ . (relationRD 'leq))
  52. (in!/ . (inRD ))
  53. (notin!/ . (notinRD ))
  54. (subset!/ . (relationRD 'subset))
  55. (prsubset!/ . (relationRD 'prsubset))
  56. (notprsubset!/ . (notprsubsetRD ))
  57. (notsubset!/ . (notsubsetRD )));
  58. %Tokens to be found between <apply></apply> tags.
  59. RDlist!*:= append(RDreln!*,
  60. '((divide!/ . (divideRD))
  61. (setdiff!/ . ( setdiffRD))
  62. (select!/ . (selectRD))
  63. (transpose!/ . ( transposeRD))
  64. (determinant!/ . ( determinantRD))
  65. (fn . ( applyfnRD))
  66. (union!/ . (unionRD))
  67. (intersect!/ . (intersectionRD))
  68. (implies!/ . ( impliesRD))
  69. (not!/ . ( notRD))
  70. (xor!/ . (xorRD))
  71. (or!/ . (orRD))
  72. (and!/ . (andRD))
  73. (mean!/ . ( meanRD))
  74. (var!/ . ( varRD))
  75. (sdev!/ . ( sdevRD))
  76. (moment!/ . ( momentRD))
  77. (median!/ . ( medianRD))
  78. (sin!/ . ( sinRD))
  79. (sec!/ . ( secRD))
  80. (sinh!/ . ( sinhRD))
  81. (sech!/ . ( sechRD))
  82. (arcsin!/ . ( arcsinRD))
  83. (cos!/ . ( cosRD))
  84. (csc!/ . ( cscRD))
  85. (cosh!/ . ( coshRD))
  86. (csch!/ . ( cschRD))
  87. (arccos!/ . ( arccosRD))
  88. (tan!/ . ( tanRD))
  89. (cot!/ . ( cotRD))
  90. (tanh!/ . ( tanhRD))
  91. (coth!/ . ( cothRD))
  92. (arctan!/ . ( arctanRD))
  93. (abs!/ . ( absRD))
  94. (ln!/ . ( lnRD))
  95. (plus!/ . ( plusRD))
  96. (times!/ . ( timesRD))
  97. (power!/ . ( powerRD))
  98. (exp!/ . ( expRD))
  99. (factorial!/ . ( factorialRD))
  100. (quotient!/ . ( quotientRD))
  101. (max!/ . ( maxRD))
  102. (min!/ . ( minRD))
  103. (minus!/ . ( minusRD))
  104. (rem!/ . (remRD))
  105. (conjugate!/ . ( conjugateRD))
  106. (root!/ . ( rootRD))
  107. (gcd!/ . ( gcdRD))
  108. (log!/ . (logRD))
  109. (int!/ . (intRD))
  110. (sum!/ . ( sumRD))
  111. (limit!/ . (limitRD))
  112. (condition . (conditionRD))
  113. (product!/ . (productRD))
  114. (diff!/ . (diffRD))
  115. (partialdiff!/ . (partialdiffRD))));
  116. RDelems!* :=
  117. '((reln . (relnRD !/reln "</reln>"))
  118. (set . ( setRD !/set "</set>"))
  119. (fn . ( fnRD !/fn "</fn>"))
  120. (declare . ( declareRD !/declare "</declare>"))
  121. (list . ( listRD !/list "</list>"))
  122. (matrix . ( matrixRD !/matrix "</matrix>"))
  123. (cn . ( cnML !/cn "</cn>"))
  124. (ci . ( ciML !/ci "</ci>"))
  125. (lambda . ( lambdaRD !/lambda "</lambda>")));
  126. unary!* :=
  127. '((determinant . (unary determinant))
  128. (transpose . (unary transpose))
  129. (sum . (sum_prodML sum))
  130. (prod . (sum_prodML product))
  131. (df . (dfML nil)) % FJW: (df.(dfML df))
  132. (impart . (complpart impart))
  133. (repart . (complpart repart))
  134. (abs . (unary abs))
  135. (gcd . (n_nary gcd))
  136. (set . (setML set))
  137. (factorial . (unary factorial))
  138. (max . (n_nary max))
  139. (min . (n_nary min))
  140. (cos . (unary cos))
  141. (sin . (unary sin))
  142. (sec . (unary sec))
  143. (cosh . (unary cosh))
  144. (cot . (unary cot))
  145. (coth . (unary coth))
  146. (csch . (unary csch))
  147. (acos . (trigML acos))
  148. (asin . (trigML asin))
  149. (atan . (trigML atan))
  150. (sech . (unary sech))
  151. (sinh . (unary sinh))
  152. (tan . (unary tan))
  153. (tanh . (unary tanh))
  154. (csc . (unary csc))
  155. (quotient . (quotientML nil))
  156. (plus . (n_nary plus))
  157. (times . (n_nary times))
  158. (expt . (n_nary power))
  159. (sqrt . (sqrtML sqrt))
  160. (log . (unary log))
  161. (logb . (log_baseML logb))
  162. (log10 . (log_baseML log10))
  163. (ln . (unary ln))
  164. (eq . (reln eq))
  165. (neq . (reln neq))
  166. (gt . (reln gt))
  167. (lt . (reln lt))
  168. (greaterp . (reln gt))
  169. (lessp . (reln lt))
  170. (geq . (reln geq))
  171. (leq . (reln leq))
  172. (union . (sets union))
  173. (intersection . (sets intersection))
  174. (in . (reln in))
  175. (notin . (reln notin))
  176. (subset . (reln subset))
  177. (prsubset . (reln prsubset))
  178. (notsubset . (reln notsubset))
  179. (notprsubset . (reln notprsubset))
  180. (setdf . (sets setdf))
  181. (arbcomplex . (printsub2 cadr arbcomplex))
  182. (arbint . (printsub2 cadr arbint))
  183. (mat . (matrixML nil))
  184. (minus . (minusML nil))
  185. (int . (integralML nil))
  186. (equal . (equalML nil))
  187. (list . (listML nil)));
  188. %The next three functions are the lexer. When called they returns the next
  189. %mathml token in the input stream.
  190. symbolic procedure lex();
  191. begin scalar token;
  192. token:=nil;
  193. if atts neq nil then safe_atts:=atts;
  194. atts:=nil;
  195. if ch neq !$EOF!$ then <<
  196. if ch=space then while (ch:=readch())=space do
  197. else
  198. if ch='!< then char:=get_token()
  199. else char:=get_content();
  200. if char neq nil then
  201. << count:=count+1;
  202. token:=reverse char;
  203. char:=butes(token);
  204. %By decomenting the following line, the tokens read in are one by one
  205. %printed onto the output stream.
  206. % print char;
  207. attributes(char,token)>>
  208. else lex(); >>
  209. end;
  210. symbolic procedure get_token();
  211. begin scalar d;
  212. d:=();
  213. while (ch:=readch()) neq '!> do d:=cons(ch,d);
  214. return cons('!$,d);
  215. end;
  216. symbolic procedure get_content();
  217. begin scalar d;
  218. d:=();
  219. while (ch:=readch()) neq '!< AND ch neq !$EOF!$
  220. do
  221. if ch neq space AND id2int(ch)>10 then
  222. d:=cons(ch,d);
  223. if d neq nil then d:=cons('!$,d);
  224. return d;
  225. end;
  226. %This function will search the list of attributes _att_ for the attribute
  227. %named _key_
  228. symbolic procedure search_att( att, key);
  229. begin scalar l, stop,d;
  230. l:=nil;
  231. d:=();
  232. stop:=0;
  233. att:= find(att, key);
  234. if att neq '(stop) then
  235. <<
  236. while (car att='! ) do att:=cdr att;
  237. if (car att = '!=) then
  238. <<
  239. att:=cdr att;
  240. while (car att='! ) do att:=cdr att;
  241. if (car att='!") then
  242. << att:=cdr att;
  243. while (stop=0) do
  244. << d:=cons(car att, d);
  245. att:=cdr att;
  246. if (car att='! ) OR (car att='!$) then stop:=1
  247. >>
  248. >>
  249. else
  250. while (stop=0) do
  251. << d:=cons(car att, d);
  252. att:=cdr att;
  253. if (car att='! ) OR (car att='!$) then stop:=1
  254. >>
  255. >>
  256. else
  257. errorML(compress key,1);
  258. if car d='!" then d:=cdr d;
  259. return reverse d
  260. >>
  261. end;
  262. symbolic procedure find(fatt, fkey);
  263. begin;
  264. return if fkey= '() then if fatt neq nil then cdr fatt else '(stop)
  265. else
  266. find(member(car fkey, fatt), cdr fkey);
  267. end;
  268. symbolic procedure attributes(a,b);
  269. begin scalar l;
  270. l:=length a;
  271. for a:=1:l do b:=cdr b;
  272. while (car b='! ) do b:=cdr b;
  273. if b neq '(!$) then atts:=b;
  274. end;
  275. symbolic procedure butes( str );
  276. %Removes all attributes to a token.
  277. begin cha;
  278. cha:=car str;
  279. return if (cha='! OR cha='!$) then <<'(); >>
  280. else cons(car str, butes cdr str);
  281. end;
  282. %This is the MAIN function. It is given the name of a file which contains
  283. %the mathml input. It launches the program by calling parseML().
  284. symbolic procedure mml(ff);
  285. begin;
  286. FILE!*:=t;
  287. ff:= open(ff, 'input);
  288. ff:= rds(ff);
  289. parseML();
  290. close rds ff;
  291. FILE!*:=nil;
  292. end;
  293. %This function starts the parsing mechanism, which is a recursive descent
  294. %parsing.
  295. symbolic procedure parseML();
  296. begin scalar res, vswitch;
  297. res:=nil;
  298. vswitch:=nil;
  299. % FLUID '(safe_atts char ch atts count temp space temp2);
  300. space:=int2id(32);
  301. count:=0;
  302. ch:=readch();
  303. temp2:=nil;
  304. lex();
  305. if char='(m a t h) then
  306. res:=mathML()
  307. else errorML("<math>",2);
  308. lex();
  309. if char='(!/ m a t h) then
  310. terpri()
  311. else errorML("</math>",19);
  312. return algebraic res;
  313. end;
  314. %The two next functions differ in that one of them parses from the next
  315. %token onwards, and the other one from the actual token onwards.
  316. symbolic procedure mathML();
  317. begin scalar a;
  318. a:=nil;
  319. lex();
  320. return sub_math();
  321. end;
  322. symbolic procedure mathML2();
  323. begin scalar a;
  324. a:=nil;
  325. return sub_math();
  326. end;
  327. %Parses all tokens which legally follow a mathml token.
  328. symbolic procedure sub_math();
  329. begin scalar a,aa;
  330. if char='(a p p l y)
  331. then <<a := applyML();
  332. if char neq '(!/ a p p l y) then errorML("</apply>",3)>>
  333. else if char='(v e c t o r)
  334. then <<a := vectorRD();
  335. if char neq '(!/ v e c t o r)
  336. then errorML("</vector>",2)>>
  337. else if (aa := assoc(compress!* char, RDelems!*))
  338. then <<a := apply(cadr aa, '() );
  339. if compress!* char neq third aa
  340. then errorML(third cdr aa, 2)>>;
  341. return a
  342. end;
  343. symbolic procedure compress!* u;
  344. begin scalar x;
  345. if digit car u then return compress u;
  346. for each j in u do
  347. if j eq '!/ or j eq '!- or j eq '!; or j eq '!.
  348. then x := j . '!! . x
  349. else x := j . x;
  350. return intern compress reversip x
  351. end;
  352. %The next two functions parse the <cn> and <ci> tokens and extracts its
  353. %content to be used by the function calling it. It will have different
  354. %behaviours according to the type of the <cn> data.
  355. symbolic procedure cnML();
  356. begin scalar type, sep, tt,aa;
  357. %Must check that what is being returned is an int.
  358. type:=nil; sep:=nil;
  359. type:=search_att(atts, '(t y p e));
  360. lex();
  361. tt := char;
  362. lex();
  363. if type='(c o n s t a n t) then
  364. <<
  365. if (aa:=assoc(intern compress tt, RDci!*)) then
  366. return apply(first cdr aa, rest cdr aa) >>;
  367. if IDP compress tt then errorML(compress tt, 16);
  368. if type=nil then return compress tt;
  369. if member(type, '((r e a l) (i n t e g e r))) neq nil then
  370. return compress tt;
  371. 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)
  372. (c o m p l e x !- p o l a r))) neq nil then
  373. << sep:=sepRD();
  374. if type='(r a t i o n a l) then <<lex();return alg_quotient(compress tt, sep)>>
  375. else
  376. if type='(c o m p l e x !- c a r t e s i a n) then
  377. << lex();return comp2(compress tt, sep) >>else
  378. if type='(c o m p l e x !- p o l a r) then
  379. <<sep:= po2ca(compress tt, sep);
  380. <<lex();return comp2(car sep, cadr sep)>> >>
  381. >>;
  382. end;
  383. symbolic procedure ciML();
  384. begin scalar test, type,aa, tt;
  385. aa:=nil; type:=nil; test:=nil;
  386. type:=search_att(atts, '(t y p e));
  387. lex();
  388. tt := char;
  389. lex();
  390. << test:=compress tt;
  391. if NUMBERP test then errorML(test, 4);
  392. test:=intern test;
  393. return test>>
  394. end;
  395. %returns the algebraic value of the constant values.
  396. algebraic procedure consts(c);
  397. begin;
  398. if c=i then return i;
  399. if c=d then return d;
  400. if c=e then return e;
  401. if c=p then return pi;
  402. end;
  403. %Constructs a complex number.
  404. algebraic procedure comp2(a,b);
  405. begin;
  406. return a+b*i;
  407. end;
  408. %Returns the two values separated by a <sep/> tag.
  409. symbolic procedure sepRD();
  410. begin scalar p1, p2;
  411. p1:=nil; p2:=nil;
  412. if char neq '(s e p !/) then errorML("<sep/>",2);
  413. lex();
  414. p2:=compress char;
  415. return p2;
  416. end;
  417. %Creates a vector by using function matrix_row.
  418. symbolic procedure vectorRD();
  419. begin scalar a;
  420. a:=nil;
  421. a:=matrixrowRD();
  422. a:=lisp aeval list('mat, a);
  423. return a;
  424. end;
  425. %The following functions construct the matrix from the mathml information.
  426. symbolic procedure matrixRD();
  427. begin scalar b1, b2, stop;
  428. stop:=0;
  429. b1:='();
  430. b2:=nil;
  431. while stop=0 do
  432. <<
  433. lex();
  434. if char='(m a t r i x r o w) then
  435. <<b2:=matrixrowRD();
  436. if b1 neq nil then b1:=append(b1, list b2)
  437. else b1:=list b2;
  438. if char neq '(!/ m a t r i x r o w) then
  439. errorML("</matrixrow>",2)>>
  440. else stop:=1
  441. >>;
  442. return aeval cons ('mat ,b1);
  443. end;
  444. symbolic procedure matrixrowRD();
  445. begin scalar a;
  446. a:=nil;
  447. a:=mathML();
  448. return if a=nil then nil
  449. else cons(a, matrixrowRD());
  450. end;
  451. %returns a lambda function constructed from the information supplied.
  452. symbolic procedure lambdaRD();
  453. begin scalar b1, b2;
  454. lex();
  455. b1:=bvarRD();
  456. b1:=car b1;
  457. b2:=mathML();
  458. lex();
  459. return algebraic( (lambda b1; b2) b1 );
  460. end;
  461. %returns a set constructed from the information supplied.
  462. symbolic procedure setRD();
  463. begin scalar setvars;
  464. atts:='(t y p e != s e t !$);
  465. setvars:= cons('list,stats_getargs());
  466. setvars:=cons(car setvars, norepeat(cdr setvars));
  467. return setvars;
  468. end;
  469. %This function will keep one copy only of any repeating elements
  470. symbolic procedure norepeat(args);
  471. begin;
  472. return if args=nil then nil else
  473. if length args=1 then list car args
  474. else append(list car args, norepeat(delall(car args, cdr args)));
  475. end;
  476. %This function will delete all occurences of element x in list l
  477. symbolic procedure delall(x,l);
  478. if l=nil then nil
  479. else if x=car l then delall(x, cdr l)
  480. else append(list car l ,delall(x, cdr l));
  481. %returns a list constructed from the information supplied.
  482. symbolic procedure listRD();
  483. begin scalar setvars, lorder, tmp;
  484. lorder:=search_att(atts, '(o r d e r));
  485. atts:='(t y p e != l i s t !$);
  486. setvars:= cons('list,stats_getargs());
  487. tmp := setvars;
  488. if lorder='(l e x i c o g r a p h i c) then
  489. setvars:=algebraic sortlist (setvars, lexog);
  490. if lorder='(n u m e r i c) then
  491. setvars:=algebraic sortlist (setvars, numer)
  492. else
  493. setvars:=algebraic sortlist (setvars, pred);
  494. if setvars = nil then setvars:= tmp;
  495. return setvars;
  496. end;
  497. %Defines the predicate function used by function _sortlist_. Sortlist comes
  498. %from package assist, and its documentation can be found in assist's
  499. %documentation
  500. %This one will sort all elements in numerical and alphanumerical order
  501. symbolic procedure pred(u,v);
  502. begin;
  503. return if NUMBERP u and NUMBERP v then <<if u<v then t>> else
  504. if IDP u and IDP v then <<if id2int(u) < id2int(v) then t>>
  505. else if NUMBERP u and IDP v then <<if u<id2int(v) then t>> else
  506. if IDP u and NUMBERP v then <<if id2int(u)<v then t>>;
  507. end;
  508. %This one sorts in alphanumerical order
  509. symbolic procedure lexog(u,v);
  510. begin;
  511. return if IDP u and IDP v then <<if id2int(u) < id2int(v) then t>>
  512. else t;
  513. end;
  514. %This one sorts in numerical order
  515. symbolic procedure numer(u,v);
  516. begin;
  517. return if NUMBERP u and NUMBERP v then <<if u<v then t>>
  518. else t;
  519. end;
  520. %Makes the next token in the inputstream an operator.
  521. symbolic procedure fnRD();
  522. begin scalar b1;
  523. lex();
  524. if char neq '(c i) then errorML(compress char,20)
  525. else b1:= mathML2();
  526. if ATOM b1 then algebraic operator b1;
  527. lex();
  528. return b1;
  529. end;
  530. %Reads the declare construct and sets the value of the given variable to
  531. %the given value.
  532. symbolic procedure declareRD();
  533. begin scalar b1, b2, flagg, at;
  534. at:=atts;
  535. flagg := nil;
  536. b1:=mathML();
  537. clear b1;
  538. clear reval b1;
  539. lex();
  540. if at neq nil then
  541. put(b1, 'type, search_att(at,'(t y p e)));
  542. if search_att(at, '(t y p e)) = '(v e c t o r) then
  543. flagg:=t;
  544. if char='(!/ d e c l a r e) then return nil;
  545. b2 :=mathML2();
  546. if get(b1, 'type)='(f n) then
  547. << algebraic operator b1>>;
  548. if flagg = t then setk(b1, b2)
  549. else algebraic set(b1, b2);
  550. lex();
  551. return nil;
  552. end;
  553. %This function will determine if the next token is a valid token following
  554. %an apply token. It then calls the appropriate function if succesful.
  555. symbolic procedure applyML();
  556. begin scalar aa;
  557. lex();
  558. if (aa := assoc(compress!* char, RDlist!*))
  559. then return apply(first cdr aa, rest cdr aa)
  560. else if char='(i d e n t !/) or char='(c o m p o s e !/)
  561. then return nil
  562. else if char='(i n v e r s e !/) then return t
  563. else errorML(compress!* char, 17)
  564. end;
  565. %Reads the next two elements and returns their setdifference.
  566. symbolic procedure setdiffRD();
  567. begin scalar b1, b2;
  568. b1:=mathML();
  569. b2:=mathML();
  570. lex();
  571. if b1=reval b1 and b2=reval b2 then return list('setdiff,b1, b2)
  572. else
  573. if b1=reval b1 then return list('setdiff, b1, reval b2) else
  574. if b2=reval b2 then return list('setdiff, reval b1, b2) else
  575. return append(list('set), setdiff(reval b1, reval b2));
  576. end;
  577. %Reads through a select construct and acts accordingly.
  578. symbolic procedure selectRD();
  579. begin scalar a1, res;
  580. a1:=stats_getargs();
  581. if caar a1='mat then res:=mat_select(a1);
  582. if caar a1='list then res:=list_select(a1);
  583. if ATOM res then return res;
  584. return cons('list, res);
  585. end;
  586. symbolic procedure mat_select(a1);
  587. begin
  588. if length car a1=2 then return nth(cadar a1, cadr a1)
  589. else
  590. if length a1=2 then return nth(cdar a1, cadr a1);
  591. if length a1=3 then return nth(nth(cdar a1, caddr a1), cadr a1);
  592. end;
  593. symbolic procedure list_select(a1);
  594. begin scalar b1;
  595. b1:=cdar a1;
  596. return nth(b1, cadr a1);
  597. end;
  598. %Returns the transpose of the element contained in the transpose tags.
  599. symbolic procedure transposeRD();
  600. begin scalar a, res;
  601. a:=mathML();
  602. res:=algebraic(tp a);
  603. lex();
  604. return res;
  605. end;
  606. %Returns the determinant of the given element.
  607. symbolic procedure determinantRD();
  608. begin scalar a, res;
  609. a:=mathML();
  610. res:=alg_det a;
  611. lex();
  612. return res;
  613. end;
  614. algebraic procedure alg_det(a);
  615. begin;
  616. return det a;
  617. end;
  618. %Takes the given function name, makes it an operator, and then
  619. %applies it to the arguments specified in the mathml input.
  620. symbolic procedure applyfnRD();
  621. begin scalar b1, b2, c1;
  622. b1:=nil; b2:=nil; c1:=nil;
  623. b1:=fnRD();
  624. b2:=stats_getargs();
  625. b2:=cons(b1, b2);
  626. c1:=algebraic b2;
  627. return c1;
  628. end;
  629. %Returns the union of the elements specified.
  630. symbolic procedure unionRD();
  631. begin scalar b1, a1, a2,type,res;
  632. b1:=stats_getargs();
  633. a1:=car b1;
  634. a2:=cadr b1;
  635. if PAIRP a1 AND PAIRP a2 then <<
  636. type := car a1;
  637. a1:=cons('list, eval_list cdr a1);
  638. a2:=cons('list, eval_list cdr a2);
  639. res:=algebraic union(a1,a2);
  640. >>
  641. else <<
  642. type := 'list;
  643. res := cons('list,cons(a1,list a2));
  644. >>;
  645. return cons(type, cdr res);
  646. end;
  647. %Returns the intersection of the elements specified.
  648. symbolic procedure intersectionRD();
  649. begin scalar b1, a1, a2,type,res;
  650. b1:=stats_getargs();
  651. a1:=car b1;
  652. a2:=cadr b1;
  653. if PAIRP a1 AND PAIRP a2 then <<
  654. type := car a1;
  655. a1:=cons('list, eval_list cdr a1);
  656. a2:=cons('list, eval_list cdr a2);
  657. res:=algebraic intersect(a1,a2);
  658. >>
  659. else <<
  660. type := 'list;
  661. res := cons('list,cons(a1,list a2));
  662. >>;
  663. return cons(type, cdr res);
  664. end;
  665. %Takes all the arguments in a list, and forces an evaluation on them if they can be
  666. %evaluated.
  667. symbolic procedure eval_list(args);
  668. begin;
  669. return if args=nil then nil
  670. else cons(reval car args, eval_list(cdr args));
  671. end;
  672. %Takes all the arguments in a list of sets, and evaluates them if they can
  673. %be evaluated.
  674. symbolic procedure eval_list_sets(args);
  675. begin scalar ab;
  676. return if args=nil then nil
  677. else <<if PAIRP reval car args then
  678. <<
  679. if car reval car args='list then
  680. ab:=cons('set, cdr reval car args)>>
  681. else ab:=reval car args;
  682. cons(ab, eval_list_sets(cdr args))>>;
  683. end;
  684. %Sets global variable temp2 to 'stop if an evaluatable element is found in
  685. %list args.
  686. symbolic procedure constants(args);
  687. begin scalar b1;
  688. if args neq nil then b1:=car args;
  689. return if args=nil then nil
  690. else <<if b1=reval b1 AND IDP b1 OR PAIRP b1 then temp2:='stop
  691. else constants(cdr args)>>;
  692. end;
  693. %Return boolean values of the arguments given.
  694. symbolic procedure notRD();
  695. begin scalar a;
  696. a:=mathML();
  697. lex();
  698. return not(reval a);
  699. end;
  700. symbolic procedure impliesRD();
  701. begin scalar a1,b1,c1;
  702. a1:=mathML();
  703. b1:=mathML();
  704. if b1='false then b1:=nil;
  705. if a1='false then a1:=nil;
  706. if reval a1 AND not reval b1 then c1:=nil
  707. else c1:=t;
  708. lex();
  709. return c1;
  710. end;
  711. symbolic procedure andRD();
  712. begin scalar a;
  713. a:=stats_getargs();
  714. a:=subst(nil, 'false, a);
  715. a:=and2RD(a);
  716. return a;
  717. end;
  718. symbolic procedure and2RD(args);
  719. begin
  720. return if length args=1 then reval car args
  721. else and(reval car args, and2RD(cdr args));
  722. end;
  723. symbolic procedure orRD();
  724. begin scalar a;
  725. a:=stats_getargs();
  726. a:=subst(nil, 'false, a);
  727. a:=or2RD(a);
  728. return a;
  729. end;
  730. symbolic procedure or2RD(args);
  731. begin
  732. return if length args=1 then reval car args
  733. else or(reval car args, or2RD(cdr args));
  734. end;
  735. symbolic procedure xorRD();
  736. begin scalar a;
  737. a:=stats_getargs();
  738. a:=subst(nil, 'false, a);
  739. a:=xor2RD(a);
  740. return a;
  741. end;
  742. symbolic procedure xor2RD(args);
  743. begin
  744. return if args=() then nil
  745. else alg_xor(reval car args, xor2RD(cdr args));
  746. end;
  747. symbolic procedure alg_xor(a,b);
  748. begin;
  749. return and(or(a,b),not(and(a,b)));
  750. end;
  751. %All defined trigonometric functions.
  752. algebraic procedure sinRD();
  753. begin scalar a;
  754. a:=symbolic mathML();
  755. symbolic lex();
  756. return sin(a);
  757. end;
  758. algebraic procedure secRD();
  759. begin scalar a;
  760. a:=symbolic mathML();
  761. symbolic lex();
  762. return sec(a);
  763. end;
  764. algebraic procedure sinhRD();
  765. begin scalar a;
  766. a:=symbolic mathML();
  767. symbolic lex();
  768. return sinh(a);
  769. end;
  770. algebraic procedure sechRD();
  771. begin scalar a;
  772. a:=symbolic mathML();
  773. symbolic lex();
  774. return sech(a);
  775. end;
  776. algebraic procedure arcsinRD();
  777. begin scalar a;
  778. a:=symbolic mathML();
  779. symbolic lex();
  780. return asin(a);
  781. end;
  782. algebraic procedure cosRD();
  783. begin scalar a;
  784. a:=symbolic mathML();
  785. symbolic lex();
  786. return cos(a);
  787. end;
  788. algebraic procedure cscRD();
  789. begin scalar a;
  790. a:=symbolic mathML();
  791. symbolic lex();
  792. return csc(a);
  793. end;
  794. algebraic procedure coshRD();
  795. begin scalar a;
  796. a:=symbolic mathML();
  797. symbolic lex();
  798. return cosh(a);
  799. end;
  800. algebraic procedure cschRD();
  801. begin scalar a;
  802. a:=symbolic mathML();
  803. symbolic lex();
  804. return csch(a);
  805. end;
  806. algebraic procedure arccosRD();
  807. begin scalar a;
  808. a:=symbolic mathML();
  809. symbolic lex();
  810. return acos(a);
  811. end;
  812. algebraic procedure tanRD();
  813. begin scalar a;
  814. a:=symbolic mathML();
  815. symbolic lex();
  816. return tan(a);
  817. end;
  818. algebraic procedure cotRD();
  819. begin scalar a;
  820. a:=symbolic mathML();
  821. symbolic lex();
  822. return cot(a);
  823. end;
  824. algebraic procedure tanhRD();
  825. begin scalar a;
  826. a:=symbolic mathML();
  827. symbolic lex();
  828. return tanh(a);
  829. end;
  830. algebraic procedure cothRD();
  831. begin scalar a;
  832. a:=symbolic mathML();
  833. symbolic lex();
  834. return coth(a);
  835. end;
  836. algebraic procedure arctanRD();
  837. begin scalar a;
  838. a:=symbolic mathML();
  839. symbolic lex();
  840. return atan(a);
  841. end;
  842. %Reads the condition tag.
  843. symbolic procedure conditionRD();
  844. begin scalar a;
  845. lex();
  846. if char='(r e l n) then a:=relnRD()
  847. else a:=mathML();
  848. lex();
  849. return a;
  850. end;
  851. %This function will read all legal tags following the <reln> tag.
  852. symbolic procedure relnRD();
  853. begin scalar a,aa;
  854. lex();
  855. if (aa := assoc(compress!* char, RDreln!*))
  856. then a := apply(first cdr aa, rest cdr aa)
  857. else errorML(compress!* char, 18);
  858. return if a=t then t else if null a then 'false else a;
  859. end;
  860. symbolic procedure relationRD( type );
  861. begin scalar args,a;
  862. args:=stats_getargs();
  863. if type='(quote eq) then <<a:= 'equal . args>> else
  864. %if type='(quote eq) then <<a:= alg_eq(args)>> else
  865. %if type='(quote neq) then <<a:= alg_neq(args)>> else
  866. %if type='(quote lt) then <<a:= alg_lt(args)>> else
  867. %if type='(quote gt) then <<a:= alg_gt(args)>> else
  868. if type='(quote lt) then <<a:= 'lessp . args>> else
  869. if type='(quote gt) then <<a:= 'greaterp . args>> else
  870. if type='(quote subset) then <<a:=subsetRD(args)>> else
  871. if type='(quote prsubset) then <<a:=prsubsetRD(args)>> else
  872. %if type='(quote geq) then <<a:= alg_geq(args)>> else
  873. %if type='(quote leq) then <<a:= alg_leq(args)>>;
  874. if type='(quote geq) then a:= 'geq . args else
  875. if type='(quote leq) then a:= 'leq . args;
  876. return if a=t then t
  877. else if a=nil then 'false else a;
  878. end;
  879. %The following functions do all the necessay actions in order to evaluate
  880. %what should be by the tags.
  881. symbolic procedure notsubsetRD();
  882. begin scalar b1, b2;
  883. b1:=mathML();
  884. b2:=mathML();
  885. lex();
  886. if b1=reval b1 AND b2=reval b2 then
  887. return list('notsubset, b1, b2);
  888. if b1= reval b1 then
  889. return list('notsubset, b1,cons ('set, cdr reval b2));
  890. if b2= reval b2 then
  891. return list('notsubset, cons('set,cdr reval b1), b2);
  892. if intersection(cdr reval b1,cdr reval b2)=nil then
  893. return t
  894. else
  895. return nil;
  896. end;
  897. symbolic procedure notprsubsetRD();
  898. begin scalar b1, b2;
  899. b1:=mathML();
  900. b2:=mathML();
  901. lex();
  902. if b1=reval b1 AND b2=reval b2 then
  903. return list('notprsubset, b1, b2);
  904. if b1= reval b1 then
  905. return list('notprsubset, b1,cons('set, cdr reval b2));
  906. if b2= reval b2 then
  907. return list('notprsubset, cons('set,cdr reval b1), b2);
  908. if reval b1 = reval b2 then return t;
  909. if intersection(cdr reval b1,cdr reval b2)=nil then return t else
  910. return nil;
  911. end;
  912. symbolic procedure subsetRD(sets);
  913. begin scalar args,val;
  914. args:=sets;
  915. val:=t;
  916. while (length args > 1) do
  917. << if NUMBERP reval car args then
  918. errorML(reval car args,5);
  919. if car args = reval car args OR cadr args = reval cadr args then
  920. << args:='();
  921. val:=cons('subset, eval_list_sets(sets))>>
  922. else
  923. << val:=AND(val, alg_subset(reval car args, reval cadr args));
  924. args:=cdr args >>
  925. >>;
  926. return val;
  927. end;
  928. symbolic procedure alg_subset(a,b);
  929. begin;
  930. if a=b then return t
  931. else
  932. if setdiff(a,b)=nil then return t else return nil;
  933. end;
  934. symbolic procedure prsubsetRD(sets);
  935. begin scalar args, val;
  936. val:=t;
  937. while (length args > 1) do
  938. << if car args = reval car args OR cadr args = reval cadr args then
  939. << args:='();
  940. val:=cons('prsubset, eval_list_sets(sets))>>
  941. else
  942. << val:=AND(val, alg_prsubset(reval car args, reval cadr args));
  943. args:=cdr args >> >>;
  944. return val;
  945. end;
  946. symbolic procedure alg_prsubset(a,b);
  947. begin;
  948. if setdiff(a,b)=nil then return t else return nil;
  949. end;
  950. symbolic procedure inRD();
  951. begin scalar b1,b2;
  952. b1:= mathML();
  953. b2:= mathML();
  954. lex();
  955. if b2 = reval b2 AND ATOM b2 then
  956. <<
  957. if b2='n then <<if FIXP b1 then return t else return nil>>;
  958. if b2='r then <<if NUMBERP b1 then return t else return nil>>;
  959. return list('in, reval b1, b2)
  960. >>;
  961. if MEMBER(reval b1,reval b2) neq nil then return t
  962. else return nil;
  963. end;
  964. symbolic procedure notinRD();
  965. begin scalar b1,b2;
  966. b1:= mathML();
  967. b2:= mathML();
  968. lex();
  969. if b2 = reval b2 AND ATOM b2 then
  970. <<
  971. if b2='N then if FIXP b1 then return nil else return nil;
  972. if b2='R then if NUMBERP b1 then return nil else return nil;
  973. return list('notin, reval b1, b2)>>;
  974. if MEMBER(reval b1,reval b2) neq nil then return nil
  975. else return t;
  976. end;
  977. symbolic procedure alg_eq(args);
  978. begin;
  979. constants(args);
  980. return alg_eq2 eval_list args;
  981. end;
  982. symbolic procedure alg_eq2(args);
  983. begin;
  984. return if length args=1 then t
  985. else if (reval car args eq reval cadr args) then
  986. alg_eq2(cdr args);
  987. end;
  988. symbolic procedure alg_neq(args);
  989. begin;
  990. constants(args);
  991. return alg_neq2(eval_list(args));
  992. end;
  993. symbolic procedure alg_neq2(args);
  994. begin;
  995. return if length args=1 then t
  996. else if (reval car args neq reval cadr args) then
  997. alg_neq2(cdr args);
  998. end;
  999. symbolic procedure alg_lt(args);
  1000. begin;
  1001. constants(args);
  1002. if temp2='stop then
  1003. <<temp2:=nil; return append(list 'lt, eval_list(args))>>
  1004. else return alg_lt2(eval_list(args));
  1005. end;
  1006. symbolic procedure alg_lt2(args);
  1007. begin;
  1008. return if length args=1 then t
  1009. else
  1010. if (NUMBERP reval car args AND NUMBERP reval cadr args )then
  1011. <<if (reval car args < reval cadr args) then
  1012. alg_lt2(cdr args)
  1013. else nil>>
  1014. else errorML("",6);
  1015. end;
  1016. symbolic procedure alg_gt(args);
  1017. begin;
  1018. constants(args);
  1019. if temp2='stop then
  1020. <<temp2:=nil; return append(list 'gt, eval_list(args))>>
  1021. else return alg_gt2(eval_list(args));
  1022. end;
  1023. symbolic procedure alg_gt2(args);
  1024. begin;
  1025. return if length args=1 then t
  1026. else
  1027. if (NUMBERP reval car args AND NUMBERP reval cadr args )then
  1028. <<if (reval car args > reval cadr args) then
  1029. alg_gt2(cdr args)
  1030. else nil>>
  1031. else errorML("",6);
  1032. end;
  1033. symbolic procedure alg_geq(args);
  1034. begin;
  1035. constants(args);
  1036. if temp2='stop then
  1037. <<temp2:=nil; return append(list 'g_eq, eval_list(args))>>
  1038. else return alg_geq2(eval_list(args));
  1039. end;
  1040. symbolic procedure alg_geq2(args);
  1041. begin;
  1042. return if length args=1 then t
  1043. else
  1044. if (NUMBERP reval car args AND NUMBERP reval cadr args )then
  1045. <<if (reval car args >= reval cadr args) then
  1046. alg_geq2(cdr args)
  1047. else nil>>
  1048. else errorML("",6);
  1049. end;
  1050. symbolic procedure alg_leq(args);
  1051. begin;
  1052. constants(args);
  1053. if temp2='stop then
  1054. <<temp2:=nil; return append(list 'l_eq, eval_list(args))>>
  1055. else return alg_leq2(eval_list(args));
  1056. end;
  1057. symbolic procedure alg_leq2(args);
  1058. begin;
  1059. return if length args=1 then t
  1060. else
  1061. if (NUMBERP reval car args AND NUMBERP reval cadr args )then
  1062. <<if (reval car args <= reval cadr args) then
  1063. alg_leq2(cdr args)
  1064. else nil>>
  1065. else errorML("",6);
  1066. end;
  1067. %Interprets the <tendsto> tag when used in the <limit> tag.
  1068. symbolic procedure tendstoRD();
  1069. begin scalar attr, arg1 ,arg2;
  1070. if intersection(atts, '(t y p e)) neq nil then
  1071. attr:=search_att(atts, '(t y p e))
  1072. else attr:=nil;
  1073. arg1:=mathML();
  1074. arg2:=mathML();
  1075. lex();
  1076. return list (attr,arg2);
  1077. end;
  1078. %Returns the limit of the information given. Uses the Reduce package
  1079. %LIMITS.
  1080. symbolic procedure limitRD();
  1081. begin scalar var, condi, low, exp;
  1082. lex();
  1083. if char='(b v a r) then
  1084. << var:=bvarRD();
  1085. if eqn(cadr var,1) then var:=car var
  1086. else
  1087. errorML("<degree>",8);
  1088. lex()>>
  1089. else var:=nil;
  1090. if char='(l o w l i m i t) then
  1091. << low:=lowlimitRD();
  1092. lex()>>
  1093. else if char='(c o n d i t i o n) then
  1094. << condi:=conditionRD();
  1095. if char neq '(!/ c o n d i t i o n) then
  1096. errorML("</condition>",2);
  1097. lex()>>
  1098. else condi:=nil;
  1099. exp:=mathML2();
  1100. lex();
  1101. if condi=nil then
  1102. return alg_limit(exp, var, low, 'norm);
  1103. if low=nil then
  1104. if car condi='(a b o v e) then
  1105. return alg_limit(exp, var, cadr condi, 'plus)
  1106. else return alg_limit(exp, var, cadr condi, 'min);
  1107. end;
  1108. algebraic procedure alg_limit(exp, var, tendto, type);
  1109. begin;
  1110. if type='norm then return limit(exp, var, tendto);
  1111. if type='plus then return limit!+(exp,var,tendto);
  1112. if type='min then return limit!-(exp,var,tendto);
  1113. end;
  1114. %Returns the sum.
  1115. symbolic procedure sumRD();
  1116. begin scalar svar, low, upper, express, res;
  1117. svar:=nil; low:=nil; upper:=nil; express:=nil; res:=nil;
  1118. lex();
  1119. if char='(b v a r) then
  1120. <<svar:=bvarRD();
  1121. if eqn(cadr svar,1) then svar:=car svar
  1122. else
  1123. errorML("<degree>",7);
  1124. lex()>>
  1125. else errorML("<bvar>",9);
  1126. if char='(l o w l i m i t) then
  1127. << low:=lowlimitRD();
  1128. lex();
  1129. if char='(u p l i m i t) then
  1130. << upper:=upperlimitRD();
  1131. lex()>>
  1132. else errorML("<uplimit>",10) >>
  1133. else if char='(i n t e r v a l) then
  1134. << res:=intervalRD();
  1135. lex();
  1136. low:=car res;
  1137. upper:=cadr res >>
  1138. else errorML("<lowlimit> or <interval>",11);
  1139. express:=mathML2();
  1140. lex();
  1141. return algebraic sum(express, svar, low, upper);
  1142. end;
  1143. algebraic procedure alg_sum( low, upper, formu);
  1144. begin scalar temp,var2;
  1145. algebraic;
  1146. temp:=0;
  1147. var2:=symbolic svar;
  1148. for tt:=low:upper do
  1149. << set(var2,tt);
  1150. temp:=temp+formu;
  1151. clear symbolic svar;
  1152. var2:=symbolic svar>>;
  1153. symbolic;
  1154. return temp;
  1155. end;
  1156. %Returns the product.
  1157. symbolic procedure productRD();
  1158. begin scalar pvar, low, upper, pexpress, res;
  1159. lex();
  1160. if char='(b v a r) then
  1161. <<pvar:=bvarRD();
  1162. if eqn(cadr pvar,1) then pvar:=car pvar
  1163. else
  1164. errorML("<degree>",12);
  1165. lex()>>
  1166. else errorML("<bvar>",9);
  1167. if char='(l o w l i m i t) then
  1168. << low:=lowlimitRD();
  1169. lex();
  1170. if char='(u p l i m i t) then
  1171. << upper:=upperlimitRD();
  1172. lex()>>
  1173. else errorML("<uplimit>",10)>>
  1174. else if char='(i n t e r v a l) then
  1175. << res:=intervalRD();
  1176. lex();
  1177. low:=car res;
  1178. upper:=cadr res >>
  1179. else errorML("<lowlimit> or <interval>",11);
  1180. pexpress:=mathML2();
  1181. lex();
  1182. return algebraic prod(pexpress, pvar, low, upper);
  1183. end;
  1184. algebraic procedure alg_prod( low, upper, formu);
  1185. begin scalar temp,var2;
  1186. algebraic;
  1187. temp:=1;
  1188. var2:=symbolic pvar;
  1189. for tt:=low:upper do
  1190. << set(var2,tt);
  1191. temp:=temp*formu;
  1192. clear symbolic pvar;
  1193. var2:=symbolic pvar>>;
  1194. symbolic;
  1195. return temp;
  1196. end;
  1197. %Returns the partial derivative.
  1198. symbolic procedure partialdiffRD();
  1199. begin scalar res, bvar, express;
  1200. lex();
  1201. bvar:=getargsRD();
  1202. express:=mathML2();
  1203. lex();
  1204. res:=differentiate(express, bvar);
  1205. return res;
  1206. end;
  1207. symbolic procedure differentiate(express, bvar);
  1208. begin scalar temp,diffed;
  1209. return
  1210. if eqn(length bvar,0) then express
  1211. else
  1212. <<temp:=car bvar;
  1213. diffed:=alg_df(express, car temp, cadr temp);
  1214. differentiate(diffed, cdr bvar)>>;
  1215. end;
  1216. %This function reads through the a series of <bvar> tags and extracts the
  1217. %variables.
  1218. symbolic procedure getargsRD();
  1219. begin scalar a;
  1220. %Dont forget. This function leaves the file pointer on
  1221. %the next token after the last bvar. So you need to use mathML2 after.
  1222. if char='(b v a r) then
  1223. <<a:=bvarRD();
  1224. lex();
  1225. return cons (a,getargsRD())>>;
  1226. end;
  1227. %Returns the derivative.
  1228. symbolic procedure diffRD();
  1229. begin scalar bvar, degree, express, res;
  1230. lex();
  1231. if char='(b v a r) then
  1232. <<bvar:=bvarRD();
  1233. degree:=cadr bvar;
  1234. bvar:=car bvar; lex()>>
  1235. else <<bvar:=nil; degree:=nil>>;
  1236. express:=mathML2();
  1237. lex();
  1238. res:=alg_df(express, bvar, degree);
  1239. return res;
  1240. end;
  1241. algebraic procedure alg_df(a,b,c);
  1242. begin;
  1243. return df(a,b,c);
  1244. end;
  1245. %This function will calculate the integral. Takes in the expression, then
  1246. %the bound variable, and finally the limits if they exist.
  1247. symbolic procedure intRD();
  1248. begin scalar bvar, low, upper, int, exp;
  1249. lex();
  1250. if char='(b v a r) then
  1251. <<bvar:=bvarRD();
  1252. if eqn(cadr bvar,1) then bvar:=car bvar
  1253. else
  1254. errorML("",13);
  1255. lex()>>
  1256. else errorML("<bvar>",14);
  1257. if char='(l o w l i m i t) then <<low:=lowlimitRD(); lex()>>
  1258. else low:=nil;
  1259. if char='(u p l i m i t) then <<upper:=upperlimitRD(); lex()>>
  1260. else upper:=nil;
  1261. if char='(i n t e r v a l) then
  1262. <<int:=intervalRD();
  1263. low:=car int;
  1264. upper:=cadr int;
  1265. lex()>>
  1266. else int:=nil;
  1267. exp:=mathML2();
  1268. lex();
  1269. return alg_int(exp, bvar, low, upper);
  1270. end;
  1271. algebraic procedure alg_int(exp, bvar, low, upper);
  1272. begin scalar res;
  1273. if (low='nil) AND (upper=nil) then res:= int(exp, bvar)
  1274. else res:= int(exp,bvar,low,upper);
  1275. return res;
  1276. end;
  1277. %Here we parse bound variables. The function reads the variable as well as
  1278. %the degree if there is one.
  1279. symbolic procedure bvarRD();
  1280. begin scalar var, deg;
  1281. lex();
  1282. if char='(d e g r e e) then
  1283. errorML("<bvar>",15);
  1284. var:=mathML2();
  1285. lex();
  1286. if char='(d e g r e e) then
  1287. << deg:=mathML();
  1288. lex();
  1289. if char neq '(!/ d e g r e e) then
  1290. error("</degree>",2);
  1291. lex()>>
  1292. else deg:=1;
  1293. if char='(!/ b v a r) then return list(var, deg)
  1294. else errorML("</bvar>", 2);
  1295. end;
  1296. %Functions used to parse the limits of an integral, sum, or product.
  1297. symbolic procedure lowlimitRD();
  1298. begin scalar lowlimit;
  1299. lowlimit:=mathML();
  1300. lex();
  1301. if char='(!/ l o w l i m i t) then return lowlimit
  1302. else errorML("</lowlimit>", 2);
  1303. end;
  1304. symbolic procedure upperlimitRD();
  1305. begin scalar upperlimit;
  1306. upperlimit:=mathML();
  1307. lex();
  1308. if char='(!/ u p l i m i t) then return upperlimit
  1309. else errorML("</uplimit>", 2);
  1310. end;
  1311. symbolic procedure intervalRD();
  1312. begin scalar l,u;
  1313. l:=mathML();
  1314. u:=mathML();
  1315. lex();
  1316. if char='(!/ i n t e r v a l) then return list(l,u)
  1317. else errorML("</interval>", 2);
  1318. end;
  1319. %Following functions just evaluate calculus functions.
  1320. symbolic procedure lnRD();
  1321. begin scalar a;
  1322. a:=alg_ln(mathML());
  1323. lex();
  1324. return a;
  1325. end;
  1326. algebraic procedure alg_ln(a);
  1327. begin;
  1328. return ln(a);
  1329. end;
  1330. symbolic procedure logRD();
  1331. begin scalar a, a1, base;
  1332. base:=nil;
  1333. lex();
  1334. if char='(l o g b a s e) then
  1335. <<base:=logbaseRD();
  1336. lex()>>;
  1337. a1:=mathML2();
  1338. lex();
  1339. a:=alg_log(a1, base);
  1340. return a;
  1341. end;
  1342. algebraic procedure alg_log(a, base);
  1343. begin;
  1344. if base=nil then return log(a)
  1345. else
  1346. return logb(a, base);
  1347. end;
  1348. symbolic procedure logbaseRD();
  1349. begin scalar a;
  1350. a:=mathML();
  1351. lex();
  1352. if char='(!/ l o g b a s e) then return a
  1353. else errorML("</logbase>",2);
  1354. end;
  1355. symbolic procedure conjugateRD();
  1356. begin scalar a;
  1357. a:= alg_conj(mathML());
  1358. lex();
  1359. return a;
  1360. end;
  1361. algebraic procedure alg_conj(a);
  1362. begin;
  1363. return conj(a);
  1364. end;
  1365. symbolic procedure minusRD();
  1366. begin scalar c,b;
  1367. c:=mathML();
  1368. b:=mathML();
  1369. if b=nil then c:=alg_minus(c)
  1370. else <<
  1371. c:=alg_difference(c,b);
  1372. lex()>>;
  1373. return c;
  1374. end;
  1375. algebraic procedure alg_minus(a);
  1376. begin;
  1377. return -a;
  1378. end;
  1379. algebraic procedure alg_difference(a,b);
  1380. begin;
  1381. return difference(a,b);
  1382. end;
  1383. symbolic procedure absRD();
  1384. begin scalar a;
  1385. a:=alg_abs(mathML());
  1386. lex();
  1387. return a;
  1388. end;
  1389. algebraic procedure alg_abs(a);
  1390. begin;
  1391. return abs(a);
  1392. end;
  1393. symbolic procedure rootRD();
  1394. begin scalar b,deg;
  1395. lex();
  1396. if char='(d e g r e e) then
  1397. << deg:=mathML();
  1398. lex();
  1399. if char neq '(!/ d e g r e e) then
  1400. error("</degree>","Syntax ERROR: Missing end tag");
  1401. lex()>>
  1402. else deg:=2;
  1403. b:=mathML2();
  1404. lex();
  1405. return alg_root(b,deg);
  1406. end;
  1407. algebraic procedure alg_root(b,a);
  1408. begin;
  1409. return b**(1/a);
  1410. end;
  1411. symbolic procedure remRD();
  1412. begin scalar a, a1, a2;
  1413. a1:=mathml();
  1414. a2:=mathml();
  1415. a:=alg_remainder(a1, a2);
  1416. lex();
  1417. return a;
  1418. end;
  1419. algebraic procedure alg_remainder(a,b);
  1420. begin;
  1421. return remainder(a,b);
  1422. end;
  1423. symbolic procedure factorialRD();
  1424. begin scalar a;
  1425. a:=alg_factorial(mathML());
  1426. lex();
  1427. return a;
  1428. end;
  1429. algebraic procedure alg_factorial(a);
  1430. begin;
  1431. return factorial(a);
  1432. end;
  1433. symbolic procedure expRD();
  1434. begin scalar a;
  1435. a:= alg_exp(mathML());
  1436. lex();
  1437. return a;
  1438. end;
  1439. algebraic procedure alg_exp(a);
  1440. begin;
  1441. return exp(a);
  1442. end;
  1443. symbolic procedure quotientRD();
  1444. begin scalar a, a1, a2;
  1445. a1:=mathML();
  1446. a2:=mathML();
  1447. if IDP reval a1 OR IDP reval a2 then a:=alg_quotient(a1,a2)
  1448. else
  1449. a:= (reval a1)/(reval a2);
  1450. lex();
  1451. return a;
  1452. end;
  1453. algebraic procedure alg_quotient(a,b);
  1454. begin;
  1455. return a/b;
  1456. end;
  1457. symbolic procedure divideRD();
  1458. begin scalar a, a1, a2;
  1459. a1:=mathML();
  1460. a2:=mathML();
  1461. if a2 = 0 then errorML("", 21);
  1462. a:=alg_divide(a1,a2);
  1463. lex();
  1464. return a;
  1465. end;
  1466. algebraic procedure alg_divide(a,b);
  1467. begin;
  1468. return quotient(a,b);
  1469. end;
  1470. symbolic procedure gcdRD();
  1471. begin scalar c1;
  1472. c1:=stats_getargs();
  1473. constants(c1);
  1474. if temp2='stop then
  1475. << temp2:=nil;
  1476. return cons('gcd, eval_list(c1))>>
  1477. else return gcdRD2(c1);
  1478. end;
  1479. symbolic procedure gcdRD2(args);
  1480. begin scalar a;
  1481. a:=reval car args;
  1482. return if length args=1 then car args
  1483. else alg_gcd2(a, gcdRD2(cdr args));
  1484. end;
  1485. algebraic procedure alg_gcd2(a , b);
  1486. begin;
  1487. return gcd(a,b);
  1488. end;
  1489. symbolic procedure minRD();
  1490. begin scalar a;
  1491. a:=mathML();
  1492. return if a=nil then nil
  1493. else alg_min(a,minRD());
  1494. end;
  1495. algebraic procedure alg_min(a,b);
  1496. begin;
  1497. return min(b,a);
  1498. end;
  1499. symbolic procedure maxRD();
  1500. begin scalar a;
  1501. a:=mathML();
  1502. return if a=nil then nil
  1503. else alg_max(a,maxRD());
  1504. end;
  1505. algebraic procedure alg_max(a,b);
  1506. begin;
  1507. return max(a,b)
  1508. end;
  1509. lisp operator plusRD;
  1510. symbolic procedure plusRD();
  1511. begin scalar abc1;
  1512. abc1:=nil;
  1513. abc1:=mathML();
  1514. return if abc1 = nil then 0
  1515. else alg_plus(abc1, plusRD());
  1516. end;
  1517. algebraic procedure alg_plus(acb1,b);
  1518. begin;
  1519. return acb1+b;
  1520. end;
  1521. symbolic procedure timesRD();
  1522. begin scalar a;
  1523. a:=nil;
  1524. a:=mathML();
  1525. return if a=nil then 1
  1526. else alg_times(a, timesRD());
  1527. end;
  1528. algebraic procedure alg_times(a,b);
  1529. begin;
  1530. if b=i then return a*i;
  1531. return a*b;
  1532. end;
  1533. symbolic procedure powerRD();
  1534. begin scalar var,power;
  1535. var:=mathML();
  1536. power:=mathML();
  1537. lex();
  1538. return alg_expt(var,power);
  1539. end;
  1540. algebraic procedure alg_expt(a,b);
  1541. begin;
  1542. return expt(a,b);
  1543. end;
  1544. %The following function is in charge of providing the correct error message
  1545. %as well as closing the input/output stream, and exiting the program
  1546. %correctly.
  1547. symbolic procedure errorML( str, msg );
  1548. begin;
  1549. terpri();
  1550. princ "***** Error in token number ";
  1551. princ count;
  1552. princ " (<";
  1553. princ compress char;
  1554. princ ">)";
  1555. terpri();
  1556. if msg=1 then
  1557. << princ "Needed attribute";
  1558. princ str;
  1559. princ " and none was found.">> else
  1560. if msg=2 then
  1561. << princ "Missing tag: ";
  1562. princ str >> else
  1563. if msg=3 then
  1564. << princ "Undefined error!";
  1565. princ " Token number "; princ sub1 count;
  1566. princ " probably mispelled or an";
  1567. princ "ambiguous or erroneous use of <apply></apply>.">> else
  1568. if msg=4 then
  1569. << princ "Numerical constant ";
  1570. princ str;
  1571. princ " was enclosed between <ci></ci> tags.";
  1572. terpri();
  1573. princ "Correct syntax: <cn>";
  1574. princ str;
  1575. princ "</cn>.">> else
  1576. if msg=5 then
  1577. << princ "All arguments must be sets";
  1578. terpri();
  1579. princ str;
  1580. princ " does not represent a set.">> else
  1581. if msg=6 then
  1582. << princ "Non-numeric argument in arithmetic.">> else
  1583. if msg=7 then
  1584. << princ "The degree quantifier is of no use in the sumation";
  1585. princ "operator.">> else
  1586. if msg=8 then
  1587. << princ "The degree quantifier is of no use in the limit";
  1588. princ " operator.">> else
  1589. if msg=9 then
  1590. << princ "The index of sumation has not been specified.";
  1591. terpri();
  1592. princ "Please use <bvar></bvar> tags to specify an index.">>
  1593. else
  1594. if msg=10 then
  1595. << princ "Upperlimit not specified.">> else
  1596. if msg=11 then
  1597. << princ "Upper and lower limits have not been specified.">> else
  1598. if msg=12 then
  1599. << princ "The degree quantifier is of no use in the product";
  1600. princ " operator.">> else
  1601. if msg=13 then
  1602. << princ "The degree quantifier is not allowed in the integral";
  1603. princ " operator.">> else
  1604. if msg=14 then
  1605. << princ "Variable of integration not specified.";
  1606. princ "Please use <bvar></bvar> tags to specify variable.">>
  1607. else
  1608. if msg=15 then
  1609. << princ "Incorrect use of <bvar></bvar> tags.";
  1610. princ " Correct use:";
  1611. terpri();
  1612. princ
  1613. "<bvar> bound_var </bvar> [<degree> degree </degree>] </bvar>">> else
  1614. if msg=16 then
  1615. << princ "Symbolic constant ";
  1616. princ str;
  1617. princ " was enclosed between <cn></cn> tags.";
  1618. terpri();
  1619. princ "Correct syntax: <ci> ";
  1620. princ str;
  1621. princ " </ci>";
  1622. terpri();
  1623. princ "or <cn type=""constant""> </cn>";
  1624. princ "if using constants &ImaginaryI;, &ii;, &ExponentialE;, &ee; or &pi;."
  1625. >> else
  1626. if msg=17 then
  1627. << princ "Unknown tag: <";
  1628. princ str;princ ">.";
  1629. terpri();
  1630. princ "Token not allowed within <apply></apply> tags.";
  1631. terpri();
  1632. princ "Might be: <"; princ str; princ "/>.">> else
  1633. if msg=18 then
  1634. << princ "Unknown tag: <";
  1635. princ str;princ ">.";
  1636. terpri();
  1637. princ "Not allowed within <reln></reln> tags.">> else
  1638. if msg=19 then
  1639. << princ "Undefined error!";
  1640. princ " Token "; princ sub1 count;
  1641. princ " is probably mispelled";
  1642. terpri();
  1643. princ "or unknown, ";
  1644. princ "or the </math> tag is missing">> else
  1645. if msg=20 then
  1646. << princ "Function ";
  1647. princ str;
  1648. princ "()";
  1649. princ " was not enclosed in <ci></ci> tags.";
  1650. terpri();
  1651. princ "Correct syntax: <fn><ci>";
  1652. princ str;
  1653. princ "</ci></fn>.">> else
  1654. if msg=21 then
  1655. << princ "Error, division by 0">>;
  1656. terpri();
  1657. if FILE!*=t then close rds f;
  1658. FILE!*:=nil;
  1659. rederr("");
  1660. rederr("");
  1661. terpri();
  1662. end;
  1663. %Following function are in charge of parsing statistics related mathml.
  1664. symbolic procedure meanRD();
  1665. begin scalar b, size, args;
  1666. args:=stats_getargs();
  1667. b:=0;
  1668. size:=length( args );
  1669. while (args neq ()) do
  1670. << b:=alg_plus(b, car args);
  1671. args:= cdr args >>;
  1672. return alg_quotient(b,size);
  1673. end;
  1674. symbolic procedure sdevRD( );
  1675. begin scalar args,mean,b,size;
  1676. args:=stats_getargs();
  1677. mean:=alg_mean( args );
  1678. size:=length(args);
  1679. while(args neq ()) do
  1680. << b:=alg_plus(b, alg_expt(alg_difference(car args, mean),2));
  1681. args:=cdr args; >>;
  1682. return b;
  1683. end;
  1684. symbolic procedure varRD( );
  1685. begin scalar args;
  1686. args:=stats_getargs();
  1687. return alg_expt(sdev( args ), 2);
  1688. end;
  1689. symbolic procedure medianRD( );
  1690. begin scalar args, siz, si;
  1691. args:=stats_getargs();
  1692. args:=cons('list, args);
  1693. args:=sortl(args);
  1694. args:=cdr args;
  1695. si:=length args;
  1696. siz:=si/2;
  1697. if remainder(si,2)=0 then
  1698. return alg_quotient(alg_plus(nth(args,siz),nth(args,(siz+1))),2)
  1699. else return nth(args, siz);
  1700. end;
  1701. algebraic procedure sortl(args);
  1702. begin scalar rr;
  1703. rr:=sortlist(args, pred);
  1704. if rr=nil then return sortnumlist(args)
  1705. else return rr;
  1706. end;
  1707. symbolic procedure momentRD( );
  1708. begin scalar args,size,d,i;
  1709. args:=stats_getargs();
  1710. if char='(d e g r e e) then
  1711. <<i:=mathML();
  1712. lex();
  1713. if char='(!/ d e g r e e) then lex()
  1714. else errorML("</degree>",2)>>
  1715. else i:=1;
  1716. d:=();
  1717. size:=length args;
  1718. while args neq () do
  1719. << d:=cons(alg_expt(car args, i),d);
  1720. args:=cdr args>>;
  1721. return alg_mean(d);
  1722. end;
  1723. symbolic procedure alg_mean ( args );
  1724. begin scalar b, size, args;
  1725. b:=0;
  1726. size:=length( args );
  1727. while (args neq ()) do
  1728. << b:=alg_plus(b, car args);
  1729. args:= cdr args >>;
  1730. return alg_quotient(b,size);
  1731. end;
  1732. symbolic procedure sdev( args );
  1733. begin scalar mean,b,size;
  1734. mean:=alg_mean( args );
  1735. size:=length(args);
  1736. while(args neq ()) do
  1737. << b:=alg_plus(b, alg_expt(alg_difference(car args, mean),2));
  1738. args:=cdr args; >>;
  1739. return b;
  1740. end;
  1741. %The following function gets all arguments from the mathml input.
  1742. symbolic procedure stats_getargs();
  1743. begin scalar ww;
  1744. ww:=nil;
  1745. ww:=mathML();
  1746. if ww neq nil then <<
  1747. return cons (ww,stats_getargs())>>;
  1748. end;
  1749. %Transforms polar-complex to cartesian-complex.
  1750. symbolic procedure po2ca(r,p);
  1751. begin scalar theta,x,y;
  1752. theta:=rad p;
  1753. x:=r*cos(theta);
  1754. y:=r*sin(theta);
  1755. return(list(x,y))
  1756. end;
  1757. symbolic procedure rad(mu); %note approx. pi
  1758. begin scalar b;
  1759. b:=mu*3.141529/180;
  1760. return b
  1761. end;
  1762. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1763. %Here start the functions in charge of pasing reduce's output and printing%
  1764. %it out in mathml. %
  1765. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1766. %This the mathml printer which reads reduce output and translates it to
  1767. %mathml.
  1768. symbolic procedure math_ml_printer (mode,u);
  1769. <<
  1770. if !*both=t then
  1771. (<< if u=t then else maprin(u); terpri!* nil>>) where outputhandler!* := nil;
  1772. if mode neq 'terpri then
  1773. << % FLUID '(indent, flagg,found_int, found_compl, consts_compl, consts_int);
  1774. % FLUID '(found_mat_int, found_mat_compl, consts_mat_int, consts_mat_compl);
  1775. found_mat_int=0$;
  1776. found_mat_compl=0$;
  1777. indent:=0$
  1778. consts_compl:=()$
  1779. consts_mat_compl:=()$
  1780. consts_int:=()$
  1781. consts_mat_int:=()$
  1782. found_int:=0$
  1783. found_compl:=0$
  1784. flagg:=0$
  1785. if (PAIRP u) then <<
  1786. if !*web=t then printout("<EMBED TYPE=""text/mathml"" MMLDATA=""");
  1787. printout("<math>");
  1788. indent:=3;
  1789. if ((car u)='setq) then
  1790. <<if (PAIRP caddr u) then
  1791. if (issq(caddr u)=1) then arbitrary_c( PREPSQ cadr caddr u )
  1792. else
  1793. if (caaddr u='mat) then arbitrary_c(caddr u)
  1794. else
  1795. if (caaddr u='list) then arbitrary_c( !*a2k caddr u);
  1796. setqML( u )>>
  1797. else
  1798. if ((car u)='list) then
  1799. << arbitrary_c( !*a2k u );
  1800. listML(cdr u)>>
  1801. else
  1802. if ((car u)='mat) then
  1803. << arbitrary_c( u );
  1804. matrixML(cdr u)>>
  1805. else
  1806. if ((car u)='!*sq) then
  1807. << arbitrary_c(PREPSQ (cadr u));
  1808. expression(PREPSQ (cadr u))>>
  1809. else expression(u);
  1810. indent:=indent-3;
  1811. close_forall();
  1812. indent:=0;
  1813. printout( "</math>" );
  1814. if !*web=t then princ(""" HEIGHT=300 WIDTH=500>");
  1815. terpri()
  1816. >>
  1817. else
  1818. if (ATOM u) then <<
  1819. if !*web=t then printout("<EMBED TYPE=""text/mathml"" MMLDATA="" ");
  1820. printout( "<math>" );
  1821. indent:=3;
  1822. expression( u );
  1823. indent:=0;
  1824. printout( "</math>" );
  1825. if !*web=t then princ(" "" HEIGHT=300 WIDTH=500>");
  1826. terpri() >>
  1827. else ; >> >>;
  1828. %Prints out vectors.
  1829. symbolic procedure vectorML( elem );
  1830. begin;
  1831. printout("<vector>");
  1832. indent:=indent+3;
  1833. multi_elem(car elem);
  1834. indent:=indent-3;
  1835. printout("</vector>")
  1836. end;
  1837. %Following functions print out matrices.
  1838. symbolic procedure matrixML( elem );
  1839. begin;
  1840. if length elem=1 then vectorML( elem )
  1841. else
  1842. << printout("<matrix>");
  1843. indent:=indent+3;
  1844. matrix_rows(elem);
  1845. indent:=indent-3;
  1846. printout("</matrix>")
  1847. >>;
  1848. end;
  1849. symbolic procedure matrix_rows( elem );
  1850. begin;
  1851. if (elem neq()) then
  1852. << printout("<matrixrow>");
  1853. indent:=indent+3;
  1854. row(car elem);
  1855. indent:=indent-3;
  1856. printout("</matrixrow>");
  1857. matrix_rows( cdr elem ); >>
  1858. end;
  1859. symbolic procedure row( elem );
  1860. begin;
  1861. if (elem neq()) then
  1862. << expression(car elem); row(cdr elem);>>
  1863. end;
  1864. %This function searches for arbitrary integers, or complex in the reduce
  1865. %output. If so, it declares these variables in a forall statement.
  1866. symbolic procedure arbitrary_c( elem );
  1867. begin;
  1868. found_int:=nil;
  1869. found_mat_int:=nil;
  1870. found_compl:=nil;
  1871. found_mat_compl:=nil;
  1872. if (PAIRP elem) then <<
  1873. if (car elem='mat) then
  1874. << isarb_mat_compl(cdr elem);
  1875. isarb_mat_int(cdr elem)>>
  1876. else
  1877. << isarb_compl(elem);
  1878. isarb_int(elem)>>;
  1879. if ((found_compl=1) OR (found_int=1)) then
  1880. << flagg:=1;
  1881. printout( "<apply><forall/>" );
  1882. indent:=indent+3;
  1883. print_arb_compl(elem);
  1884. print_arb_int(elem);
  1885. printout( "<condition>");
  1886. indent:=indent+3;
  1887. if ((found_compl=1) AND (found_int=1)) then
  1888. << printout( "<apply><and/>" );
  1889. indent:=indent+3>>
  1890. else
  1891. if ((length consts_compl) > 1) then
  1892. << printout( "<apply><and/>" );
  1893. indent:=indent+3>>
  1894. else
  1895. if ((length consts_int) > 1) then
  1896. << printout( "<apply><and/>" );
  1897. indent:=indent+3>>;
  1898. if (found_compl=1) then
  1899. in_complexML( consts_compl );
  1900. if (found_int=1) then
  1901. in_integerML( consts_int );
  1902. if ((found_compl=1) AND (found_int=1)) then
  1903. << indent:=indent-3;
  1904. printout( "</apply>" )>>
  1905. else
  1906. if ((length consts_compl) > 1) then
  1907. << indent:=indent-3;
  1908. printout( "</apply>" )>>
  1909. else
  1910. if ((length consts_int) > 1) then
  1911. << indent:=indent-3;
  1912. printout( "</apply>" )>>;
  1913. indent:=indent-3;
  1914. printout( "</condition>" )>>;
  1915. if ((found_mat_compl=1) OR (found_mat_int=1)) then
  1916. << flagg:=1;
  1917. printout( "<apply><forall/>" );
  1918. indent:=indent+3;
  1919. printarb_mat_compl(cdr elem);
  1920. printarb_mat_int(cdr elem);
  1921. printout( "<condition>");
  1922. indent:=indent+3;
  1923. if ((found_mat_compl=1) AND (found_mat_int=1)) then
  1924. << printout( "<apply><and/>" );
  1925. indent:=indent+3>>
  1926. else
  1927. if ((length consts_mat_compl) > 1) then
  1928. << printout( "<apply><and/>" );
  1929. indent:=indent+3>>
  1930. else
  1931. if ((length consts_mat_int) > 1) then
  1932. << printout( "<apply><and/>" );
  1933. indent:=indent+3>>;
  1934. if (found_mat_compl=1) then
  1935. in_complexML( consts_mat_compl );
  1936. if (found_mat_int=1) then
  1937. in_integerML( consts_mat_int );
  1938. if ((found_mat_compl=1) AND (found_mat_int=1)) then
  1939. << indent:=indent-3;
  1940. printout( "</apply>" )>>
  1941. else
  1942. if ((length consts_mat_compl) > 1) then
  1943. << indent:=indent-3;
  1944. printout( "</apply>" )>>
  1945. else
  1946. if ((length consts_mat_int) > 1) then
  1947. << indent:=indent-3;
  1948. printout( "</apply>" )>>;
  1949. indent:=indent-3;
  1950. printout( "</condition>" )>>;
  1951. >>
  1952. end;
  1953. symbolic procedure in_complexML( elem );
  1954. begin;
  1955. if (elem neq ()) then <<
  1956. printout("<reln><in/>");
  1957. indent:=indent+3;
  1958. printsub2( car elem, 'compl );
  1959. printout("<complexes/>");
  1960. % printout("<ci type=""set""> C </ci>");
  1961. indent:=indent-3;
  1962. printout("</reln>");
  1963. in_complexML( cdr elem )>>;
  1964. end;
  1965. symbolic procedure in_integerML( elem );
  1966. begin;
  1967. if (elem neq ()) then <<
  1968. printout("<reln><in/>");
  1969. indent:=indent+3;
  1970. printsub2( car elem, 'int );
  1971. printout("<integers/>");
  1972. % printout("<ci type=""set""> Z </ci>");
  1973. indent:=indent-3;
  1974. printout("</reln>");
  1975. in_integerML( cdr elem )>>;
  1976. end;
  1977. symbolic procedure close_forall();
  1978. begin;
  1979. if (flagg=1) then printout("</apply>");
  1980. end;
  1981. %Prints out setq statements as <declare> statements.
  1982. symbolic procedure setqML( elem );
  1983. begin;
  1984. printout( "<declare>" );
  1985. indent:=indent+3;
  1986. expression(cadr elem);
  1987. expression( caddr elem);
  1988. indent:=indent-3;
  1989. printout( "</declare>" );
  1990. end;
  1991. %Prints out lists.
  1992. symbolic procedure listML( elem );
  1993. begin;
  1994. printout( "<list>" );
  1995. indent:=indent+3;
  1996. multilists( elem );
  1997. indent:=indent-3;
  1998. printout( "</list>" );
  1999. end;
  2000. symbolic procedure multilists( elem );
  2001. begin;
  2002. if elem neq nil then
  2003. if ((LENGTH elem)=1) then expression (car elem)
  2004. else <<expression(car elem); multilists(cdr elem);>>
  2005. end;
  2006. %This function takes in a reduce expression, and parses it. It also takes
  2007. %expressions created by the above program.
  2008. symbolic procedure expression( elem );
  2009. begin scalar aa;
  2010. if (ATOM elem) then f4( elem ) else
  2011. if car elem='!:RD!: then <<printout elem>> else
  2012. <<
  2013. if (aa:=assoc(car elem, unary!*)) then <<
  2014. if caddr aa = nil then
  2015. apply(cadr aa, list cdr elem)
  2016. else
  2017. apply(cadr aa, list(cdr elem, caddr aa)) >> else
  2018. if ((car elem)= '!*sq) then expression (PREPSQ (cadr elem)) else
  2019. operator_fn(elem);>>;
  2020. end;
  2021. %Prints out sum, or products.
  2022. symbolic procedure sum_prodML( elem, tty );
  2023. begin;
  2024. printout("<apply>");
  2025. princ "<"; princ tty; princ "/>";
  2026. indent:=indent+3;
  2027. printout("<bvar>");
  2028. indent:=indent+3;
  2029. expression( cadr elem );
  2030. indent:=indent-3;
  2031. printout("</bvar>");
  2032. printout("<lowlimit>");
  2033. indent:=indent+3;
  2034. expression( caddr elem );
  2035. indent:=indent-3;
  2036. printout("</lowlimit>");
  2037. printout("<uplimit>");
  2038. indent:=indent+3;
  2039. expression( cadddr elem );
  2040. indent:=indent-3;
  2041. printout("</uplimit>");
  2042. expression car elem;
  2043. indent:=indent-3;
  2044. printout("</apply>");
  2045. end;
  2046. %Prints out derivatives.
  2047. symbolic procedure dfml( elem );
  2048. begin scalar test;
  2049. test:=cdr elem;
  2050. if length test=1 OR (length test=2 AND NUMBERP cadr test) then
  2051. printout("<apply><diff/>")
  2052. else
  2053. printout("<apply><partialdiff/>");
  2054. indent:=indent+3;
  2055. dfargs(cdr elem); % FJW: two statements swapped
  2056. expression(car elem);
  2057. indent:=indent-3;
  2058. printout("</apply>");
  2059. end;
  2060. symbolic procedure dfargs( elem );
  2061. begin;
  2062. if elem neq nil then
  2063. << if length elem>1 then
  2064. << if NUMBERP cadr elem then
  2065. <<printout("<bvar>");
  2066. indent:=indent+3;
  2067. expression car elem;
  2068. degreeML(cadr elem);
  2069. indent:=indent-3;
  2070. printout("</bvar>");
  2071. dfargs(cddr elem)>>
  2072. else
  2073. <<printout("<bvar>");
  2074. indent:=indent+3;
  2075. expression car elem;
  2076. indent:=indent-3;
  2077. printout("</bvar>");
  2078. dfargs(cdr elem)>>; >>
  2079. else
  2080. << printout("<bvar>");
  2081. indent:=indent+3;
  2082. expression car elem;
  2083. indent:=indent-3;
  2084. printout("</bvar>");
  2085. dfargs(cdr elem)>> >>;
  2086. end;
  2087. %Prints out degree statements.
  2088. symbolic procedure degreeML( elem );
  2089. begin;
  2090. printout("<degree>");
  2091. indent:=indent+3;
  2092. expression( elem );
  2093. indent:=indent-3;
  2094. printout("</degree>");
  2095. end;
  2096. symbolic procedure complpart( elem, tty);
  2097. begin;
  2098. printout("<apply><fn><");
  2099. princ tty;
  2100. princ "></fn>";
  2101. indent:=indent+3;
  2102. expression(car elem);
  2103. indent:=indent-3;
  2104. printout("<apply>");
  2105. end;
  2106. %Prints out set theory related functions.
  2107. symbolic procedure sets(elem, tty);
  2108. begin;
  2109. printout("<apply>");
  2110. princ "<"; princ tty; princ "/>";
  2111. indent:=indent+3;
  2112. multi_elem( elem );
  2113. indent:=indent-3;
  2114. printout("</apply>");
  2115. end;
  2116. %Prints out relns.
  2117. symbolic procedure reln(elem, tty);
  2118. begin;
  2119. printout("<reln>");
  2120. princ "<"; princ tty; princ "/>";
  2121. indent:=indent+3;
  2122. multi_elem( elem );
  2123. indent:=indent-3;
  2124. printout("</reln>");
  2125. end;
  2126. %Prints out a set.
  2127. symbolic procedure setML( elem );
  2128. begin;
  2129. printout("<set>");
  2130. indent:=indent+3;
  2131. multi_elem( elem );
  2132. indent:=indent-3;
  2133. printout("</set>");
  2134. end;
  2135. %Prints out unknown functions as a function. It prints out all variables
  2136. %declared a soperators.
  2137. symbolic procedure operator_fn( elem );
  2138. begin scalar asso;
  2139. asso := atsoc(car elem, '((arcsinh . sinh)(arcsech . sech)
  2140. (arccosh . cosh)(arccsch csch) (arctanh . tanh)
  2141. (arccoth . coth)) );
  2142. if asso then <<
  2143. printout "<apply><inverse/>";
  2144. princ cdr asso; >>
  2145. else <<
  2146. printout("<apply><fn><ci>");
  2147. princ car elem;
  2148. princ "</ci></fn>" >>;
  2149. indent:=indent+3;
  2150. multi_args(cdr elem);
  2151. indent:=indent-3;
  2152. printout("</apply>");
  2153. end;
  2154. %Reads through a list and prints out each component.
  2155. symbolic procedure multi_args( elem );
  2156. begin;
  2157. if (elem neq ()) then <<expression(car elem); multi_args( cdr elem );>>
  2158. end;
  2159. %Prints out all trigonometric functions which have not the same tag name,
  2160. %as reduce function.
  2161. symbolic procedure trigML(elem, type);
  2162. begin;
  2163. printout("<apply>");
  2164. if ((type='acos) OR (type='asin) OR (type='atan)) then
  2165. << if (type='acos) then princ "<arccos/>";
  2166. if (type='asin) then princ "<arcsin/>";
  2167. if (type='atan) then princ "<arctan/>">>;
  2168. indent:=indent+3;
  2169. expression(car elem);
  2170. indent:=indent-3;
  2171. printout("</apply>");
  2172. end;
  2173. %Prints out all unary functions such as log, or many trig functions.
  2174. symbolic procedure unary( elem, type );
  2175. begin;
  2176. printout("<apply>");
  2177. princ "<";
  2178. princ type;
  2179. princ "/>";
  2180. indent:=indent+3;
  2181. expression(car elem );
  2182. indent:=indent-3;
  2183. printout("</apply>");
  2184. end;
  2185. %Prints out logs with a base.
  2186. symbolic procedure log_baseML(elem, type);
  2187. begin;
  2188. printout("<apply><log/>");
  2189. indent:=indent+3;
  2190. printout("<logbase>");
  2191. indent:=indent+3;
  2192. if (type='logb) then expression(cadr elem);
  2193. if (type='log10) then f4(10);
  2194. indent:=indent-3;
  2195. printout("</logbase>");
  2196. expression(car elem);
  2197. indent:=indent-3;
  2198. printout("<apply>");
  2199. end;
  2200. %Prints out equal relns.
  2201. symbolic procedure equalML( elem );
  2202. begin;
  2203. printout( "<reln><eq/>" );
  2204. indent:=indent+3;
  2205. expression(car elem);
  2206. expression(cadr elem);
  2207. indent:=indent-3;
  2208. printout( "</reln>" );
  2209. end;
  2210. %Prints out square roots.
  2211. symbolic procedure sqrtML( elem , type);
  2212. begin;
  2213. printout( "<apply><root/>" );
  2214. indent:=indent+3;
  2215. printout( "<degree><cn> 2 </cn></degree>" );
  2216. expression( car elem );
  2217. indent:=indent-3;
  2218. printout( "</apply>" );
  2219. end;
  2220. %Prints out integrals.
  2221. symbolic procedure integralML( elem );
  2222. begin;
  2223. printout( "<apply><int/>" );
  2224. indent:=indent+3;
  2225. printout( "<bvar>" );
  2226. indent:=indent+3;
  2227. expression (cadr elem);
  2228. indent:=indent-3;
  2229. printout( "</bvar>" );
  2230. if (length cdr elem >1) then
  2231. << printout("<lowlimit>");
  2232. indent:=indent+3;
  2233. expression( caddr elem );
  2234. indent:=indent-3;
  2235. printout("</lowlimit>");
  2236. printout("<uplimit>");
  2237. indent:=indent+3;
  2238. expression( cadddr elem );
  2239. indent:=indent-3;
  2240. printout("</uplimit>")>>;
  2241. expression( car elem );
  2242. indent:=indent-3;
  2243. printout( "</apply>" );
  2244. end;
  2245. %Prints out quotients.
  2246. symbolic procedure quotientML( elem );
  2247. begin;
  2248. if (NUMBERP car elem) AND (NUMBERP cadr elem) then <<
  2249. if !*web=nil then printout("<cn type=""rational""> ")
  2250. else printout("<cn type=&quot;rational&quot;> ");
  2251. princ car elem;
  2252. princ " <sep/> ";
  2253. princ cadr elem;
  2254. princ " </cn>">>
  2255. else <<
  2256. printout( "<apply><divide/>" );
  2257. indent:=indent+3;
  2258. expression( car elem );
  2259. expression( cadr elem );
  2260. indent:=indent-3;
  2261. printout( "</apply>" )>>;
  2262. end;
  2263. %Prints out all n_nary functions.
  2264. symbolic procedure n_nary( elem, type );
  2265. begin;
  2266. if car elem = 'e AND type = 'power then unary(cdr elem, 'exp)
  2267. else <<
  2268. printout( "<apply>" );
  2269. princ "<";
  2270. princ type;
  2271. princ "/>";
  2272. indent:=indent+3;
  2273. multi_elem( elem );
  2274. indent:=indent-3;
  2275. printout( "</apply>" )>>
  2276. end;
  2277. symbolic procedure multi_elem( elem );
  2278. begin;
  2279. if ((length elem)=1) then expression( car elem )
  2280. else <<expression( car elem ); multi_elem( cdr elem );>>
  2281. end;
  2282. symbolic procedure minusML( elem );
  2283. begin;
  2284. printout( "<apply><minus/>" );
  2285. indent:=indent+3;
  2286. multiminus( elem );
  2287. indent:=indent-3;
  2288. printout( "</apply>" );
  2289. end;
  2290. symbolic procedure multiminus( elem );
  2291. begin;
  2292. expression(car elem);
  2293. if ((length elem)=2) then expression (cadr elem);
  2294. end;
  2295. %Prints out all pieces of data: i.e terminal symbols.
  2296. %They can be numbers, identifiers, or constants.
  2297. symbolic procedure f4(exp);
  2298. begin;
  2299. if (exp='pi) then
  2300. princ "<pi/>"
  2301. else if (exp='euler_gamma) then
  2302. princ "<eulergamma/>"
  2303. else if (exp='true) then
  2304. princ "<true/>"
  2305. else if (exp='false) then
  2306. princ "<false/>"
  2307. else if (exp='!Na!N) then
  2308. princ "<notanumber/>"
  2309. else
  2310. if (exp='infinity) then
  2311. << if !*web=nil then printout("<cn type=""constant"">")
  2312. else printout("<cn type=&quot;constant&quot;>");
  2313. princ "&infin;";
  2314. princ "</cn>">>
  2315. else <<
  2316. if (exp='e) then
  2317. << if !*web=nil then printout("<cn type=""constant"">")
  2318. else printout("<cn type=&quot;constant&quot;>");
  2319. princ "&ExponentialE;";
  2320. princ "</cn>">>
  2321. else <<
  2322. if (exp='i) then
  2323. << if !*web=nil then printout("<cn type=""constant"">")
  2324. else printout("<cn type=&quot;constant&quot;>");
  2325. princ "&ImaginaryI;";
  2326. princ "</cn>">>
  2327. else <<
  2328. if (NUMBERP exp) then
  2329. << printout "<cn";
  2330. if (FLOATP exp) then
  2331. <<if !*web=nil then princ " type=""real"">"
  2332. else princ " type=&quot;real&quot;>" >>
  2333. else
  2334. if (FIXP exp) then
  2335. <<if !*web=nil then princ " type=""integer"">"
  2336. else princ " type=&quot;integer&quot;>" >>
  2337. else princ ">";
  2338. princ exp;
  2339. princ "</cn>">>;
  2340. if (IDP exp) then
  2341. << printout "<ci";
  2342. if (listp exp) then
  2343. <<if !*web=nil then princ " type=""list"">"
  2344. else princ " type=&quot;list&quot;>">>
  2345. else
  2346. if (vectorp exp) then
  2347. <<if !*web=nil then princ " type=""vector"">"
  2348. else princ " type=&quot;vector&quot;>">>
  2349. else princ ">";
  2350. princ exp;
  2351. princ "</ci>">>;
  2352. >>
  2353. >>
  2354. >>
  2355. end;
  2356. %Functions used to print out variables with a subscript.
  2357. symbolic procedure printsub( subscript, type );
  2358. begin;
  2359. printout("<bvar>");
  2360. indent:=indent+3;
  2361. printout("<ci>");
  2362. indent:=indent+3;
  2363. printout( "<msub>" );
  2364. indent:=indent+3;
  2365. if (type='compl) then printout( "<mi>c</mi>" );
  2366. if (type='int) then printout( "<mi>d</mi>" );
  2367. printout( "<mn>" );
  2368. princ subscript;
  2369. princ "</mn>";
  2370. indent:=indent-3;
  2371. printout( "</msub>" );
  2372. indent:=indent-3;
  2373. printout("</ci>");
  2374. indent:=indent-3;
  2375. printout("</bvar>");
  2376. end;
  2377. symbolic procedure printsub2( subscript, type );
  2378. begin;
  2379. printout("<ci>");
  2380. indent:=indent+3;
  2381. printout( "<msub>" );
  2382. indent:=indent+3;
  2383. if (type='compl) then printout( "<mi>c</mi>" );
  2384. if (type='int) then printout( "<mi>d</mi>" );
  2385. printout( "<mn>" );
  2386. princ subscript;
  2387. princ "</mn>";
  2388. indent:=indent-3;
  2389. printout( "</msub>" );
  2390. indent:=indent-3;
  2391. printout("</ci>");
  2392. end;
  2393. %Prints out expressions in math form. Plagiarised from reduce code of
  2394. %mathprint
  2395. symbolic procedure ma_print l;
  2396. begin scalar temp;
  2397. temp:=outputhandler!*;
  2398. outputhandler!*:=nil;
  2399. terpri!* nil;
  2400. if !*web=nil then maprin "<cn type=""real"">"
  2401. else maprin "<cn type=&quot;real&quot;>";
  2402. maprin l;
  2403. maprin "</cn>";
  2404. terpri!* nil;
  2405. outputhandler!*:=temp;
  2406. end;
  2407. %Function in charge of doing all printing in order to make sure the
  2408. %indentation is always correct.
  2409. symbolic procedure printout( str );
  2410. begin;
  2411. if !*web = nil then terpri();
  2412. if !*web = nil then for i := 1:indent
  2413. do << princ " " >>;
  2414. if PAIRP str then
  2415. <<if car str='!:rd!: OR car str='!:rn!: then ma_print str
  2416. else princ str>>
  2417. else princ str;
  2418. end;
  2419. %Following functions are quite obscure. They find arbitrary constants in
  2420. %expressions and matrices. Then record them, and everytime they appear, are
  2421. %replaced with a fancy subscripts C, or D.
  2422. symbolic procedure issq( elem );
  2423. begin scalar value;
  2424. value:=0;
  2425. if (ATOM elem) then value:=0
  2426. else <<if ((car elem)='!*sq) then value:=1
  2427. else value:=0>>;
  2428. return value;
  2429. end;
  2430. symbolic procedure isarb_compl( elem );
  2431. begin;
  2432. if (PAIRP elem) then <<
  2433. if ((car elem)= 'arbcomplex) then found_compl:=1
  2434. else multi_isarb_compl(cdr elem);>>
  2435. end;
  2436. symbolic procedure multi_isarb_compl( elem );
  2437. begin;
  2438. if (PAIRP elem) then <<
  2439. if (elem=()) then
  2440. else <<isarb_compl(car elem); multi_isarb_compl( cdr elem);>> >>
  2441. end;
  2442. symbolic procedure isarb_int( elem );
  2443. begin;
  2444. if (PAIRP elem) then <<
  2445. if ((car elem)= 'arbint) then found_int:=1
  2446. else multi_isarb_int(cdr elem);>>
  2447. end;
  2448. symbolic procedure multi_isarb_int( elem );
  2449. begin;
  2450. if (PAIRP elem) then <<
  2451. if (elem=()) then
  2452. else <<isarb_int(car elem); multi_isarb_int( cdr elem);>> >>
  2453. end;
  2454. symbolic procedure print_arb_compl( elem );
  2455. begin;
  2456. if (PAIRP elem) then <<
  2457. if ((car elem)= 'arbcomplex) then
  2458. << if (xnp(list (cadr elem),consts_compl) eq nil) then
  2459. << printsub(cadr elem, 'compl);
  2460. consts_compl:=cons(cadr elem, consts_compl)>> >>
  2461. else multi_compl(cdr elem);>>
  2462. end;
  2463. symbolic procedure multi_compl( elem );
  2464. begin;
  2465. if (elem=()) then
  2466. else <<print_arb_compl(car elem); multi_compl( cdr elem);>>
  2467. end;
  2468. symbolic procedure print_arb_int( elem );
  2469. begin;
  2470. if (PAIRP elem) then <<
  2471. if ((car elem)= 'arbint) then
  2472. << if (xnp(list (cadr elem),consts_int) eq nil) then
  2473. << printsub(cadr elem, 'int);
  2474. consts_int:=cons(cadr elem, consts_int)>> >>
  2475. else multi_int(cdr elem);>>
  2476. end;
  2477. symbolic procedure multi_int( elem );
  2478. begin;
  2479. if (elem=()) then
  2480. else <<print_arb_int(car elem); multi_int( cdr elem);>>
  2481. end;
  2482. symbolic procedure isarb_mat_int( elem );
  2483. begin;
  2484. if (elem neq()) then
  2485. << isarb_row_int(car elem);
  2486. isarb_mat_int( cdr elem ); >>
  2487. end;
  2488. symbolic procedure isarb_row_int( elem );
  2489. begin;
  2490. if (elem neq()) then
  2491. << if (issq(car elem)=1) then
  2492. if (PAIRP (PREPSQ cadr (car elem))) then
  2493. if (car (PREPSQ cadr (car elem))='arbint) then
  2494. found_mat_int:=1;
  2495. isarb_row_int(cdr elem);>>
  2496. end;
  2497. symbolic procedure isarb_mat_compl( elem );
  2498. begin;
  2499. if (elem neq()) then
  2500. <<
  2501. isarb_row_compl(car elem);
  2502. isarb_mat_compl( cdr elem ); >>
  2503. end;
  2504. symbolic procedure isarb_row_compl( elem );
  2505. begin;
  2506. if (elem neq()) then
  2507. << if (issq(car elem)=1) then
  2508. if (PAIRP (PREPSQ cadr (car elem))) then
  2509. if (car (PREPSQ cadr (car elem))='arbcomplex) then
  2510. found_mat_compl:=1;
  2511. isarb_row_compl(cdr elem);>>
  2512. end;
  2513. symbolic procedure printarb_mat_compl( elem );
  2514. begin;
  2515. if (elem neq()) then
  2516. << printarb_row_compl(car elem);
  2517. printarb_mat_compl( cdr elem ); >>
  2518. end;
  2519. symbolic procedure printarb_row_compl( elem );
  2520. begin scalar value;
  2521. if (elem neq()) then
  2522. << if (issq(car elem)=1) then
  2523. if (PAIRP (PREPSQ cadr (car elem))) then
  2524. << value:=cadr PREPSQ cadr car elem;
  2525. if (car (PREPSQ cadr (car elem)))='arbcomplex then
  2526. if (xnp(list (value), consts_mat_compl) eq nil) then
  2527. << printsub(value, 'compl);
  2528. consts_mat_compl:=cons(value, consts_mat_compl)>> >>;
  2529. printarb_row_compl(cdr elem);>>
  2530. end;
  2531. symbolic procedure printarb_mat_int( elem );
  2532. begin;
  2533. if (elem neq()) then
  2534. <<
  2535. printarb_row_int(car elem);
  2536. printarb_mat_int( cdr elem ); >>
  2537. end;
  2538. symbolic procedure printarb_row_int( elem );
  2539. begin scalar value;
  2540. if (elem neq()) then
  2541. << if (issq(car elem)=1) then
  2542. if (PAIRP (PREPSQ cadr (car elem))) then
  2543. << value:=cadr PREPSQ cadr car elem;
  2544. if (car (PREPSQ cadr (car elem)))='arbint then
  2545. if (xnp(list (value), consts_mat_int) eq nil) then
  2546. << printsub(value, 'int);
  2547. consts_mat_int:=cons(value, consts_mat_int)>> >>;
  2548. printarb_row_int(cdr elem);>>
  2549. end;
  2550. %Following function is the same as math_ml_printer, just that it prints out
  2551. %input given from mml, which reads from files, and not form the reduce
  2552. %normal output stream.
  2553. symbolic procedure math_ml (u);
  2554. << % FLUID '(indent flagg found_int found_compl consts_compl
  2555. % consts_int !*mathprint);
  2556. % FLUID '(found_mat_int found_mat_compl consts_mat_int
  2557. % consts_mat_compl);
  2558. !*mathprint:=0;
  2559. found_mat_int=0$;
  2560. found_mat_compl=0$;
  2561. indent:=0$
  2562. consts_compl:=()$
  2563. consts_mat_compl:=()$
  2564. consts_int:=()$
  2565. consts_mat_int:=()$
  2566. found_int:=0$
  2567. found_compl:=0$
  2568. flagg:=0$
  2569. if (PAIRP u) then <<
  2570. printout("<math>");
  2571. indent:=3;
  2572. if ((car u)='setq) then
  2573. <<if (PAIRP caddr u) then
  2574. if (issq(caddr u)=1) then arbitrary_c( PREPSQ cadr caddr u )
  2575. else
  2576. if (caaddr u='mat) then arbitrary_c(caddr u)
  2577. else
  2578. if (caaddr u='list) then arbitrary_c( !*a2k caddr u);
  2579. setqML( u )>>
  2580. else
  2581. if ((car u)='list) then
  2582. << arbitrary_c( !*a2k u );
  2583. listML(cdr u)>>
  2584. else
  2585. if ((car u)='mat) then
  2586. << arbitrary_c( u );
  2587. matrixML(cdr u)>>
  2588. else
  2589. if ((car u)='!*sq) then
  2590. << arbitrary_c(PREPSQ (cadr u));
  2591. expression(PREPSQ (cadr u))>>
  2592. else expression(u);
  2593. indent:=indent-3;
  2594. close_forall();
  2595. indent:=0;
  2596. printout( "</math>" )
  2597. >>
  2598. else
  2599. if (ATOM u) then <<
  2600. printout( "<math>" );
  2601. indent:=3;
  2602. expression( u );
  2603. indent:=0;
  2604. printout( "</math>" )>>
  2605. else ; >>;
  2606. %This function executes certain commands when switches state are changed.
  2607. %It will change the outputhandler!* when mathml is set to on or both is set
  2608. %to on. And then modify it accroding to the switches states.
  2609. symbolic procedure onoff(u,bool);
  2610. begin scalar x,y;
  2611. if not idp u then typerr(u,"switch")
  2612. else if not flagp(u,'switch)
  2613. then rerror(rlisp,25,list(u,"not defined as switch"));
  2614. x := intern compress append(explode '!*,explode u);
  2615. if !*switchcheck and lispeval x eq bool then return nil
  2616. else if y := atsoc(bool,get(u,'simpfg))
  2617. then lispeval('progn . append(cdr y,list nil));
  2618. if bool and x eq '!*!r!a!i!s!e then x := '!*raise; % Special case.
  2619. if x='!*web AND bool=t then
  2620. outputhandler!*:='math_ml_printer;
  2621. if x='!*web AND bool=nil then
  2622. if !*mathml neq t then outputhandler!*:=nil;
  2623. if x='!*mathml AND bool=t then
  2624. outputhandler!*:='math_ml_printer;
  2625. if x='!*mathml AND bool=nil then
  2626. if !*both=nil then
  2627. outputhandler!*:=nil;
  2628. if x='!*both AND bool=t then
  2629. outputhandler!*:='math_ml_printer;
  2630. if x='!*both AND bool=nil then
  2631. if !*mathml=nil then
  2632. outputhandler!*:=nil
  2633. else outputhandler!*:='math_ml_printer;
  2634. set(x,bool);
  2635. end;
  2636. lisp operator mml;
  2637. lisp operator parseml;
  2638. endmodule;
  2639. end;