reduce.tex 316 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733
  1. % The REDUCE User's Manual --- LaTeX version.
  2. % Codemist Version with additional material in the same volume
  3. % To create this manual, the following steps are recommended:
  4. % latex Codemist
  5. % latex Codemist
  6. % latex Codemist
  7. % makeindex Codemist
  8. % latex Codemist
  9. %% Does not contain
  10. %% bibl.tex sl.tex
  11. \documentclass[11pt,a4paper]{book}
  12. \usepackage{makeidx}
  13. \usepackage{times}
  14. \hyphenation{unique}
  15. \hyphenation{effect}
  16. \hyphenation{Stand-ard}
  17. \hyphenation{libr-ary}
  18. \hyphenation{direct-ory}
  19. \hyphenation{state-ment}
  20. \hyphenation{argu-ment}
  21. \hyphenation{oper-ators}
  22. \hyphenation{symb-olic}
  23. \hyphenation{needs}
  24. \hyphenation{GVARSLAST}
  25. \hyphenation{ODE-SOLVE}
  26. \hyphenation{hyper-geometric}
  27. \hyphenation{equat-ion}
  28. \hyphenation{equat-ions}
  29. \hyphenation{OFF}
  30. \hyphenation{Opt-ions}
  31. \hyphenation{execu-tion}
  32. \hyphenation{poly-nom-ials}
  33. \hyphenation{func-t-ions}
  34. \hyphenation{Inte-grals}
  35. \hyphenation{Stutt-gart}
  36. \setlength{\parindent}{0pt}
  37. \setlength{\parskip}{6pt}
  38. \setlength{\hfuzz}{5pt} % don't complain about tiny overfull boxes
  39. \setlength{\vfuzz}{1pt}
  40. \renewcommand{\sloppy}{\tolerance=9999\relax%}
  41. \setlength{\emergencystretch}{0.2\hsize}}
  42. \tolerance=1000
  43. \raggedbottom
  44. % \newlength{\reduceboxwidth}
  45. \setlength{\reduceboxwidth}{4in}
  46. \newlength{\redboxwidth}
  47. \setlength{\redboxwidth}{3.5in}
  48. \newlength{\rboxwidth}
  49. \setlength{\rboxwidth}{2.6in}
  50. \newcommand{\REDUCE}{REDUCE}
  51. \newcommand{\RLISP}{RLISP}
  52. \newcommand{\underscore}{\_}
  53. \newcommand{\ttindex}[1]{{\renewcommand{\_}{\protect\underscore}%
  54. \index{#1@{\tt #1}}}}
  55. \newcommand{\COMPATNOTE}{{\em Compatibility Note:\ }}
  56. % \meta{...} is an alternative sentential form in descriptions using \it.
  57. \newcommand{\meta}[1]{\mbox{$\langle$\it#1\/$\rangle$}}
  58. % Will print out a heading in bold, and then indent the following text.
  59. \def\indented{\list{}{
  60. \itemindent\listparindent
  61. \rightmargin\leftmargin}\item[]}
  62. \let\endindented=\endlist
  63. \newenvironment{describe}[1]{\par{\bf #1}\begin{indented}}{\end{indented}}
  64. % Close up default vertical spacings:
  65. \setlength{\topsep}{0.5\baselineskip} % above and below environments
  66. \setlength{\itemsep}{\topsep}
  67. \setlength{\abovedisplayskip}{\topsep} % for "long" equations
  68. \setlength{\belowdisplayskip}{\topsep}
  69. \newcommand{\key}[1]{\fbox{\sf #1}}
  70. \newcommand{\extendedmanual}[1]{#1}
  71. %%\pagenumbering{roman}
  72. \pagestyle{empty}
  73. \makeindex
  74. \begin{document}
  75. \pagestyle{empty}
  76. % \s{...} is a sentential form in descriptions. Enclosed \em text in <...>
  77. \newcommand{\s}[1] {$<${\em #1}$>$}
  78. % \meta{...} is an alternative sentential form in descriptions using \it.
  79. %\newcommand{\meta}[1]{\mbox{$\langle$\it#1\/$\rangle$}}
  80. % \k{...} is a keyword. Just do in bold for the moment.
  81. \renewcommand{\k}[1] {{\bf #1}}
  82. % \f is a function name. Just do this as tt.
  83. \newcommand{\f}[1] {{\tt #1}}
  84. % An example macro for numbering and indenting examples.
  85. \newcounter{examplectr}
  86. \newcommand{\example}{\refstepcounter{examplectr}
  87. \noindent{\bf Example \theexamplectr}}
  88. \setcounter{examplectr}{0}
  89. % \documentstyle[11pt,makeidx]{book}
  90. \setlength{\parindent}{0pt}
  91. \setlength{\parskip}{6pt}
  92. \setlength{\hfuzz}{5pt} % don't complain about tiny overfull boxes
  93. \setlength{\vfuzz}{1pt}
  94. \renewcommand{\sloppy}{\tolerance=9999\relax%}
  95. \setlength{\emergencystretch}{0.2\hsize}}
  96. \tolerance=1000
  97. \raggedbottom
  98. \newlength{\reduceboxwidth}
  99. \setlength{\reduceboxwidth}{4in}
  100. \newlength{\redboxwidth}
  101. \setlength{\redboxwidth}{3.5in}
  102. \newlength{\rboxwidth}
  103. \setlength{\rboxwidth}{2.6in}
  104. \newcommand{\REDUCE}{REDUCE}
  105. \newcommand{\RLISP}{RLISP}
  106. \newcommand{\underscore}{\_}
  107. \newcommand{\ttindex}[1]{{\renewcommand{\_}{\protect\underscore}%
  108. \index{#1@{\tt #1}}}}
  109. \newcommand{\COMPATNOTE}{{\em Compatibility Note:\ }}
  110. % \meta{...} is an alternative sentential form in descriptions using \it.
  111. \newcommand{\meta}[1]{\mbox{$\langle$\it#1\/$\rangle$}}
  112. % Close up default vertical spacings:
  113. \setlength{\topsep}{0.5\baselineskip} % above and below environments
  114. \setlength{\itemsep}{\topsep}
  115. \setlength{\abovedisplayskip}{\topsep} % for "long" equations
  116. \setlength{\belowdisplayskip}{\topsep}
  117. \newcommand{\key}[1]{\fbox{\sf #1}}
  118. \newcommand{\extendedmanual}[1]{}
  119. \pagestyle{headings}
  120. \makeindex\begin{document}\pagestyle{empty}
  121. \begin{titlepage}
  122. \vspace*{\fill}
  123. \begin{center}
  124. {\Huge\bf {\REDUCE}} \\ [0.2cm]
  125. {\LARGE\bf User's Manual\vspace{0.4cm} \\
  126. Version 3.8}
  127. \vspace{0.5in}\large\bf
  128. Anthony C.\ Hearn \\
  129. Santa Monica, CA, USA
  130. \vspace{0.1in}
  131. \bf Email: reduce@rand.org
  132. \vspace{0.5in}
  133. \large\bf July 2003
  134. \end{center}
  135. \end{titlepage}
  136. \newpage
  137. \vspace*{3.0in}
  138. \noindent Copyright \copyright 2003 Anthony C. Hearn. All rights reserved. \\
  139. \mbox{}\\
  140. %
  141. \noindent Registered system holders may reproduce all or any part of this
  142. publication for internal purposes, provided that the source of the
  143. material is clearly acknowledged, and the copyright notice is retained.
  144. \pagestyle{headings}\setcounter{page}{0}\tableofcontents
  145. \chapter*{Abstract}
  146. \addcontentsline{toc}{chapter}{Abstract}
  147. This document provides the user with a description of the algebraic
  148. programming system {\REDUCE}. The capabilities of this system include:
  149. \begin{enumerate}
  150. \item expansion and ordering of polynomials and rational functions,
  151. \item substitutions and pattern matching in a wide variety of forms,
  152. \item automatic and user controlled simplification of expressions,
  153. \item calculations with symbolic matrices,
  154. \item arbitrary precision integer and real arithmetic,
  155. \item facilities for defining new functions and extending program syntax,
  156. \item analytic differentiation and integration,
  157. \item factorization of polynomials,
  158. \item facilities for the solution of a variety of algebraic equations,
  159. \item facilities for the output of expressions in a variety of formats,
  160. \item facilities for generating numerical programs from symbolic input,
  161. \item Dirac matrix calculations of interest to high energy physicists.
  162. \end{enumerate}
  163. \chapter*{Acknowledgment}
  164. The production of this version of the manual has been the result of the
  165. contributions of a large number of individuals who have taken the time and
  166. effort to suggest improvements to previous versions, and to draft new
  167. sections. Particular thanks are due to Gerry Rayna, who provided a draft
  168. rewrite of most of the first half of the manual. Other people who have
  169. made significant contributions have included John Fitch, Martin Griss,
  170. Stan Kameny, Jed Marti, Herbert Melenk, Don Morrison, Arthur Norman,
  171. Eberhard Schr\"ufer, Larry Seward and Walter Tietze. Finally, Richard
  172. Hitt produced a {\TeX} version of the {\REDUCE} 3.3 manual, which has been
  173. a useful guide for the production of the {\LaTeX} version of this manual.
  174. \chapter{Introductory Information}
  175. \index{Introduction}{\REDUCE} is a system for carrying out algebraic
  176. operations accurately, no matter how complicated the expressions become.
  177. It can manipulate polynomials in a variety of forms, both expanding and
  178. factoring them, and extract various parts of them as required. {\REDUCE} can
  179. also do differentiation and integration, but we shall only show trivial
  180. examples of this in this introduction. Other topics not
  181. considered include the use of arrays, the definition of procedures and
  182. operators, the specific routines for high energy physics calculations, the
  183. use of files to eliminate repetitious typing and for saving results, and
  184. the editing of the input text.
  185. Also not considered in any detail in this introduction are the many options
  186. that are available for varying computational procedures, output forms,
  187. number systems used, and so on.
  188. {\REDUCE} is designed to be an interactive system, so that the user can input
  189. an algebraic expression and see its value before moving on to the next
  190. calculation. For those systems that do not support interactive use, or
  191. for those calculations, especially long ones, for which a standard script
  192. can be defined, {\REDUCE} can also be used in batch mode. In this case,
  193. a sequence of commands can be given to {\REDUCE} and results obtained
  194. without any user interaction during the computation.
  195. In this introduction, we shall limit ourselves to the interactive use of
  196. {\REDUCE}, since this illustrates most completely the capabilities of the
  197. system. When {\REDUCE} is called, it begins by printing a banner message
  198. like:
  199. \begin{verbatim}
  200. REDUCE 3.8, 15-Jul-2003 ...
  201. \end{verbatim}
  202. where the version number and the system release date will change from time
  203. to time. It then prompts the user for input by:
  204. \begin{verbatim}
  205. 1:
  206. \end{verbatim}
  207. You can now type a {\REDUCE} statement, terminated by a semicolon to indicate
  208. the end of the expression, for example:
  209. \begin{verbatim}
  210. (x+y+z)^2;
  211. \end{verbatim}
  212. This expression would normally be followed by another character (a
  213. \key{Return} on an ASCII keyboard) to ``wake up'' the system, which would
  214. then input the expression, evaluate it, and return the result:
  215. \begin{verbatim}
  216. 2 2 2
  217. X + 2*X*Y + 2*X*Z + Y + 2*Y*Z + Z
  218. \end{verbatim}
  219. Let us review this simple example to learn a little more about the way that
  220. {\REDUCE} works. First, we note that {\REDUCE} deals with variables, and
  221. constants like other computer languages, but that in evaluating the former,
  222. a variable can stand for itself. Expression evaluation normally follows
  223. the rules of high school algebra, so the only surprise in the above example
  224. might be that the expression was expanded. {\REDUCE} normally expands
  225. expressions where possible, collecting like terms and ordering the
  226. variables in a specific manner. However, expansion, ordering of variables,
  227. format of output and so on is under control of the user, and various
  228. declarations are available to manipulate these.
  229. Another characteristic of the above example is the use of lower case on
  230. input and upper case on output. In fact, input may be in either mode, but
  231. output is usually in lower case. To make the difference between input and
  232. output more distinct in this manual, all expressions intended for input
  233. will be shown in lower case and output in upper case. However, for
  234. stylistic reasons, we represent all single identifiers in the text in
  235. upper case.
  236. Finally, the numerical prompt can be used to reference the result in a
  237. later computation.
  238. As a further illustration of the system features, the user should try:
  239. \begin{verbatim}
  240. for i:= 1:40 product i;
  241. \end{verbatim}
  242. The result in this case is the value of 40!,
  243. \begin{verbatim}
  244. 815915283247897734345611269596115894272000000000
  245. \end{verbatim}
  246. You can also get the same result by saying
  247. \begin{verbatim}
  248. factorial 40;
  249. \end{verbatim}
  250. Since we want exact results in algebraic calculations, it is essential that
  251. integer arithmetic be performed to arbitrary precision, as in the above
  252. example. Furthermore, the {\tt FOR} statement in the above is illustrative of a
  253. whole range of combining forms that {\REDUCE} supports for the convenience of
  254. the user.
  255. Among the many options in {\REDUCE} is the use of other number systems, such
  256. as multiple precision floating point with any specified number of digits ---
  257. of use if roundoff in, say, the $100^{th}$ digit is all that can be tolerated.
  258. In many cases, it is necessary to use the results of one calculation in
  259. succeeding calculations. One way to do this is via an assignment for a
  260. variable, such as
  261. \begin{verbatim}
  262. u := (x+y+z)^2;
  263. \end{verbatim}
  264. If we now use {\tt U} in later calculations, the value of the right-hand
  265. side of the above will be used.
  266. The results of a given calculation are also saved in the variable
  267. {\tt WS}\ttindex{WS} (for WorkSpace), so this can be used in the next
  268. calculation for further processing.
  269. For example, the expression
  270. \begin{verbatim}
  271. df(ws,x);
  272. \end{verbatim}
  273. following the previous evaluation will calculate the derivative of
  274. {\tt (x+y+z)\verb|^|2} with respect to {\tt X}. Alternatively,
  275. \begin{verbatim}
  276. int(ws,y);
  277. \end{verbatim}
  278. would calculate the integral of the same expression with respect to y.
  279. {\REDUCE} is also capable of handling symbolic matrices. For example,
  280. \begin{verbatim}
  281. matrix m(2,2);
  282. \end{verbatim}
  283. declares m to be a two by two matrix, and
  284. \begin{verbatim}
  285. m := mat((a,b),(c,d));
  286. \end{verbatim}
  287. gives its elements values. Expressions that include {\tt M} and make
  288. algebraic sense may now be evaluated, such as {\tt 1/m} to give the
  289. inverse, {\tt 2*m - u*m\verb|^|2} to give us another matrix and {\tt det(m)}
  290. to give us the determinant of {\tt M}.
  291. {\REDUCE} has a wide range of substitution capabilities. The system knows
  292. about elementary functions, but does not automatically invoke many of their
  293. well-known properties. For example, products of trigonometrical functions
  294. are not converted automatically into multiple angle expressions, but if the
  295. user wants this, he can say, for example:
  296. \begin{verbatim}
  297. (sin(a+b)+cos(a+b))*(sin(a-b)-cos(a-b))
  298. where cos(~x)*cos(~y) = (cos(x+y)+cos(x-y))/2,
  299. cos(~x)*sin(~y) = (sin(x+y)-sin(x-y))/2,
  300. sin(~x)*sin(~y) = (cos(x-y)-cos(x+y))/2;
  301. \end{verbatim}
  302. where the tilde in front of the variables {\tt X} and {\tt Y} indicates
  303. that the rules apply for all values of those variables.
  304. The result of this calculation is
  305. \begin{verbatim}
  306. -(COS(2*A) + SIN(2*B))
  307. \end{verbatim}
  308. \extendedmanual{See also the user-contributed packages ASSIST
  309. (chapter~\ref{ASSIST}), CAMAL (chapter~\ref{CAMAL}) and TRIGSIMP
  310. (chapter~\ref{TRIGSIMP}).}
  311. Another very commonly used capability of the system, and an illustration
  312. of one of the many output modes of {\REDUCE}, is the ability to output
  313. results in a FORTRAN compatible form. Such results can then be used in a
  314. FORTRAN based numerical calculation. This is particularly useful as a way
  315. of generating algebraic formulas to be used as the basis of extensive
  316. numerical calculations.
  317. For example, the statements
  318. \begin{verbatim}
  319. on fort;
  320. df(log(x)*(sin(x)+cos(x))/sqrt(x),x,2);
  321. \end{verbatim}
  322. will result in the output
  323. \begin{verbatim}
  324. ANS=(-4.*LOG(X)*COS(X)*X**2-4.*LOG(X)*COS(X)*X+3.*
  325. . LOG(X)*COS(X)-4.*LOG(X)*SIN(X)*X**2+4.*LOG(X)*
  326. . SIN(X)*X+3.*LOG(X)*SIN(X)+8.*COS(X)*X-8.*COS(X)-8.
  327. . *SIN(X)*X-8.*SIN(X))/(4.*SQRT(X)*X**2)
  328. \end{verbatim}
  329. These algebraic manipulations illustrate the algebraic mode of {\REDUCE}.
  330. {\REDUCE} is based on Standard Lisp. A symbolic mode is also available for
  331. executing Lisp statements. These statements follow the syntax of Lisp,
  332. e.g.
  333. \begin{verbatim}
  334. symbolic car '(a);
  335. \end{verbatim}
  336. Communication between the two modes is possible.
  337. With this simple introduction, you are now in a position to study the
  338. material in the full {\REDUCE} manual in order to learn just how extensive
  339. the range of facilities really is. If further tutorial material is
  340. desired, the seven {\REDUCE} Interactive Lessons by David R. Stoutemyer are
  341. recommended. These are normally distributed with the system.
  342. \chapter{Structure of Programs}
  343. A {\REDUCE} program\index{Program structure} consists of a set of
  344. functional commands which are evaluated sequentially by the computer.
  345. These commands are built up from declarations, statements and expressions.
  346. Such entities are composed of sequences of numbers, variables, operators,
  347. strings, reserved words and delimiters (such as commas and parentheses),
  348. which in turn are sequences of basic characters.
  349. \section{The {\REDUCE} Standard Character Set}
  350. \index{Character set}The basic characters which are used to build
  351. {\REDUCE} symbols are the following:
  352. \begin{enumerate}
  353. \item The 26 letters {\tt a} through {\tt z}
  354. \item The 10 decimal digits {\tt 0} through {\tt 9}
  355. \item The special characters \_\_ ! " \$ \% ' ( ) * + , - . / : ; $<$ $>$
  356. = \{ \} $<$blank$>$
  357. \end{enumerate}
  358. With the exception of strings and characters preceded by an
  359. exclamation mark\index{Exclamation mark}, the case
  360. of characters is ignored: depending of the underlying LISP
  361. they will all be converted internally into lower case or
  362. upper case: {\tt ALPHA}, {\tt Alpha} and {\tt alpha}
  363. represent the same symbol. Most implementations allow you to switch
  364. this conversion off. The operating instructions for a particular
  365. implementation should be consulted on this point. For portability, we
  366. shall limit ourselves to the standard character set in this exposition.
  367. \section{Numbers}
  368. \index{Number}There are several different types of numbers available in
  369. \REDUCE. Integers consist of a signed or unsigned sequence of decimal
  370. digits written without a decimal point, for example:
  371. \begin{verbatim}
  372. -2, 5396, +32
  373. \end{verbatim}
  374. In principle, there is no practical limit on the number of digits
  375. permitted as exact arithmetic is used in most implementations. (You should
  376. however check the specific instructions for your particular system
  377. implementation to make sure that this is true.) For example, if you ask
  378. for the value of $2^{2000}$ you get it
  379. displayed as a number of 603 decimal digits, taking up nine lines of
  380. output on an interactive display. It should be borne in mind of course
  381. that computations with such long numbers can be quite slow.
  382. Numbers that aren't integers are usually represented as the quotient of
  383. two integers, in lowest terms: that is, as rational numbers.
  384. In essentially all versions of {\REDUCE} it is also possible (but not always
  385. desirable!) to ask {\REDUCE} to work with floating point approximations to
  386. numbers again, to any precision. Such numbers are called {\em real}.
  387. \index{Real} They can be input in two ways:
  388. \begin{enumerate}
  389. \item as a signed or unsigned sequence of any number of decimal digits
  390. with an embedded or trailing decimal point.
  391. \item as in 1. followed by a decimal exponent which is written as the
  392. letter {\tt E} followed by a signed or unsigned integer.
  393. \end{enumerate}
  394. e.g. {\tt 32. +32.0 0.32E2} and {\tt 320.E-1} are all representations of
  395. 32.
  396. The declaration {\tt SCIENTIFIC\_NOTATION}\ttindex{SCIENTIFIC\_NOTATION}
  397. controls the output format of floating point numbers. At
  398. the default settings, any number with five or less digits before the
  399. decimal point is printed in a fixed-point notation, e.g., {\tt 12345.6}.
  400. Numbers with more than five digits are printed in scientific notation,
  401. e.g., {\tt 1.234567E+5}. Similarly, by default, any number with eleven or
  402. more zeros after the decimal point is printed in scientific notation. To
  403. change these defaults, {\tt SCIENTIFIC\_NOTATION} can be used in one of two
  404. ways. {\tt SCIENTIFIC\_NOTATION} {\em m};, where {\em m\/} is a positive
  405. integer, sets the printing format so that a number with more than {\em m\/}
  406. digits before the decimal point, or {\em m\/} or more zeros after the
  407. decimal point, is printed in scientific notation. {\tt SCIENTIFIC\_NOTATION}
  408. \{{\em m,n}\}, with {\em m\/} and {\em n\/} both positive integers, sets the
  409. format so that a number with more than {\em m\/} digits before the decimal
  410. point, or {\em n\/} or more zeros after the decimal point is printed in
  411. scientific notation.
  412. {\it CAUTION:} The unsigned part of any number\index{Number} may {\em not\/}
  413. begin with a decimal point, as this causes confusion with the {\tt CONS} (.)
  414. operator, i.e., NOT ALLOWED: {\tt .5 -.23 +.12};
  415. use {\tt 0.5 -0.23 +0.12} instead.
  416. \section{Identifiers}
  417. Identifiers\index{Identifier} in {\REDUCE} consist of one or more
  418. alphanumeric characters (i.e. alphabetic letters or decimal
  419. digits) the first of which must be alphabetic. The maximum number of
  420. characters allowed is implementation dependent, although twenty-four is
  421. permitted in most implementations. In addition, the underscore character
  422. (\_) is considered a letter if it is {\it within} an identifier. For example,
  423. \begin{verbatim}
  424. a az p1 q23p a_very_long_variable
  425. \end{verbatim}
  426. are all identifiers, whereas
  427. \begin{verbatim}
  428. _a
  429. \end{verbatim}
  430. is not.
  431. A sequence of alphanumeric characters in which the first is a digit is
  432. interpreted as a product. For example, {\tt 2ab3c} is interpreted as
  433. {\tt 2*ab3c}. There is one exception to this: If the first letter after a
  434. digit is {\tt E}, the system will try to interpret that part of the
  435. sequence as a real number\index{Real}, which may fail in some cases. For
  436. example, {\tt 2E12} is the real number $2.0*10^{12}$, {\tt 2e3c} is
  437. 2000.0*C, and {\tt 2ebc} gives an error.
  438. Special characters, such as $-$, *, and blank, may be used in identifiers
  439. too, even as the first character, but each must be preceded by an
  440. exclamation mark in input. For example:
  441. \begin{verbatim}
  442. light!-years d!*!*n good! morning
  443. !$sign !5goldrings
  444. \end{verbatim}
  445. {\it CAUTION:} Many system identifiers have such special characters in their
  446. names (especially * and =). If the user accidentally picks the name of one
  447. of them for his own purposes it may have catastrophic consequences for his
  448. {\REDUCE} run. Users are therefore advised to avoid such names.
  449. Identifiers are used as variables, labels and to name arrays, operators
  450. and procedures.
  451. \subsection*{Restrictions}
  452. The reserved words listed in another section may not be used as
  453. identifiers. No spaces may appear within an identifier, and an identifier
  454. may not extend over a line of text. (Hyphenation of an identifier, by
  455. using a reserved character as a hyphen before an end-of-line character is
  456. possible in some versions of {\REDUCE}).
  457. \section{Variables}
  458. Every variable\index{Variable} is named by an identifier, and is given a
  459. specific type. The type is of no concern to the ordinary user. Most
  460. variables are allowed to have the default type, called {\em scalar}.
  461. These can receive, as values, the representation of any ordinary algebraic
  462. expression. In the absence of such a value, they stand for themselves.
  463. \subsection*{Reserved Variables}
  464. Several variables\index{Reserved variable} in {\REDUCE} have particular
  465. properties which should not be changed by the user. These variables
  466. include:
  467. \begin{list}{}{\renewcommand{\makelabel}[1]{{\tt#1}\hspace{\fill}}%
  468. \settowidth{\labelwidth}{\tt INFINITY}%
  469. \setlength{\labelsep}{1em}%
  470. \settowidth{\leftmargin}{\tt INFINITY\hspace*{\labelsep}}}
  471. \item[E] Intended to represent the base of
  472. \ttindex{E}
  473. the natural logarithms. {\tt log(e)}, if it occurs in an expression, is
  474. automatically replaced by 1. If {\tt ROUNDED}\ttindex{ROUNDED} is
  475. on, {\tt E} is replaced by the value of E to the current degree of
  476. floating point precision\index{Numerical precision}.
  477. \item[I] Intended to represent the square
  478. \ttindex{I}
  479. root of $-1$. {\tt i\verb|^|2} is replaced by $-1$, and appropriately for higher
  480. powers of {\tt I}. This applies only to the symbol {\tt I} used on the top
  481. level, not as a formal parameter in a procedure, a local variable, nor in
  482. the context {\tt for i:= ...}
  483. \item[INFINITY] Intended to represent $\infty$
  484. \ttindex{INFINITY}
  485. in limit and power series calculations for example. Note however that the
  486. current system does {\em not\/} do proper arithmetic on $\infty$. For example,
  487. {\tt infinity + infinity} is {\tt 2*infinity}.
  488. \item[NIL] In {\REDUCE} (algebraic mode only)
  489. taken as a synonym for zero. Therefore {\tt NIL} cannot be used as a
  490. variable.
  491. \item[PI] Intended to represent the circular
  492. \ttindex{PI}
  493. constant. With {\tt ROUNDED} on, it is replaced by the value of $\pi$ to
  494. the current degree of floating point precision.
  495. \item[T] Should not be used as a formal
  496. \ttindex{T}
  497. parameter or local variable in procedures, since conflict arises with the
  498. symbolic mode meaning of T as {\em true}.
  499. \end{list}
  500. Other reserved variables, such as {\tt LOW\_POW}, described in other sections,
  501. are listed in Appendix A.
  502. Using these reserved variables\index{Reserved variable} inappropriately
  503. will lead to errors.
  504. There are also internal variables used by {\REDUCE} that have similar
  505. restrictions. These usually have an asterisk in their names, so it is
  506. unlikely a casual user would use one. An example of such a variable is
  507. {\tt K!*} used in the asymptotic command package.
  508. Certain words are reserved in {\REDUCE}. They may only be used in the manner
  509. intended. A list of these is given in the section ``Reserved Identifiers''.
  510. There are, of course, an impossibly large number of such names to keep in
  511. mind. The reader may therefore want to make himself a copy of the list,
  512. deleting the names he doesn't think he is likely to use by mistake.
  513. \section{Strings}
  514. Strings\index{String} are used in {\tt WRITE} statements, in other
  515. output statements (such as error messages), and to name files. A string
  516. consists of any number of characters enclosed in double quotes. For example:
  517. \begin{verbatim}
  518. "A String".
  519. \end{verbatim}
  520. Lower case characters within a string are not converted to upper case.
  521. The string {\tt ""} represents the empty string. A double quote may be
  522. included in a string by preceding it by another double quote. Thus
  523. {\tt "a""b"} is the string {\tt a"b}, and {\tt """"} is the string {\tt "}.
  524. \section{Comments}
  525. Text can be included in program\index{Program} listings for the
  526. convenience of human readers, in such a way that {\REDUCE} pays no
  527. attention to it. There are two ways to do this:
  528. \begin{enumerate}
  529. \item Everything from the word {\tt COMMENT}\ttindex{COMMENT} to the next
  530. statement terminator, normally ; or \$, is ignored. Such comments
  531. can be placed anywhere a blank could properly appear. (Note that {\tt END}
  532. and $>>$ are {\em not\/} treated as {\tt COMMENT} delimiters!)
  533. \item Everything from the symbol {\tt \%}\index{Percent sign} to the end
  534. of the line on which it appears is ignored. Such comments can be placed
  535. as the last part of any line. Statement terminators have no special
  536. meaning in such comments. Remember to put a semicolon before the {\tt \%}
  537. if the earlier part of the line is intended to be so terminated. Remember
  538. also to begin each line of a multi-line {\tt \%} comment with a {\tt \%}
  539. sign.
  540. \end{enumerate}
  541. \section{Operators}
  542. \label{sec-operators}
  543. Operators\index{Operator} in {\REDUCE} are specified by name and type.
  544. There are two types, infix\index{Infix operator} and prefix.
  545. \index{Prefix operator} Operators can be purely abstract, just symbols
  546. with no properties; they can have values assigned (using {\tt :=} or
  547. simple {\tt LET} declarations) for specific arguments; they can have
  548. properties declared for some collection of arguments (using more general
  549. {\tt LET} declarations); or they can be fully defined (usually by a
  550. procedure declaration).
  551. Infix operators\index{Infix operator} have a definite precedence with
  552. respect to one another, and normally occur between their arguments.
  553. For example:
  554. \begin{quote}
  555. \begin{tabbing}
  556. {\tt a + b - c} \hspace{1.5in} \= (spaces optional) \\
  557. {\tt x<y and y=z} \> (spaces required where shown)
  558. \end{tabbing}
  559. \end{quote}
  560. Spaces can be freely inserted between operators and variables or operators
  561. and operators. They are required only where operator names are spelled out
  562. with letters (such as the {\tt AND} in the example) and must be unambiguously
  563. separated from another such or from a variable (like {\tt Y}). Wherever one
  564. space can be used, so can any larger number.
  565. Prefix operators occur to the left of their arguments, which are written as
  566. a list enclosed in parentheses and separated by commas, as with normal
  567. mathematical functions, e.g.,
  568. \begin{verbatim}
  569. cos(u)
  570. df(x^2,x)
  571. q(v+w)
  572. \end{verbatim}
  573. Unmatched parentheses, incorrect groupings of infix operators
  574. \index{Infix operator} and the like, naturally lead to syntax errors. The
  575. parentheses can be omitted (replaced by a space following the
  576. operator\index{Operator} name) if the operator is unary and the argument
  577. is a single symbol or begins with a prefix operator name:
  578. \begin{quote}
  579. \begin{tabbing}
  580. {\tt cos y} \hspace{1.75in} \= means cos(y) \\
  581. {\tt cos (-y)} \> -- parentheses necessary \\
  582. {\tt log cos y} \> means log(cos(y)) \\
  583. {\tt log cos (a+b)} \> means log(cos(a+b))
  584. \end{tabbing}
  585. \end{quote}
  586. but
  587. \begin{quote}
  588. \begin{tabbing}
  589. {\tt cos a*b} \hspace{1.6in} \= means (cos a)*b \\
  590. {\tt cos -y} \> is erroneous (treated as a variable \\
  591. \> ``cos'' minus the variable y)
  592. \end{tabbing}
  593. \end{quote}
  594. A unary prefix operator\index{Prefix operator} has a precedence
  595. \index{Operator precedence} higher than any infix operator, including
  596. unary infix operators. \index{Infix operator}
  597. In other words, {\REDUCE} will always interpret {\tt cos~y + 3} as
  598. {\tt (cos~y) + 3} rather than as {\tt cos(y + 3)}.
  599. Infix operators may also be used in a prefix format on input, e.g.,
  600. {\tt +(a,b,c)}. On output, however, such expressions will always be
  601. printed in infix form (i.e., {\tt a + b + c} for this example).
  602. A number of prefix operators are built into the system with predefined
  603. properties. Users may also add new operators and define their rules for
  604. simplification. The built in operators are described in another section.
  605. \subsection*{Built-In Infix Operators}
  606. The following infix operators\index{Infix operator} are built into the
  607. system. They are all defined internally as procedures.
  608. \begin{verbatim}
  609. <infix operator>::= where|:=|or|and|member|memq|=|neq|eq|
  610. >=|>|<=|<|+|-|*|/|^|**|.
  611. \end{verbatim}
  612. These operators may be further divided into the following subclasses:
  613. \begin{verbatim}
  614. <assignment operator> ::= :=
  615. <logical operator> ::= or|and|member|memq
  616. <relational operator> ::= =|neq|eq|>=|>|<=|<
  617. <substitution operator> ::= where
  618. <arithmetic operator> ::= +|-|*|/|^|**
  619. <construction operator> ::= .
  620. \end{verbatim}
  621. {\tt MEMQ} and {\tt EQ} are not used in the algebraic mode of
  622. {\REDUCE}. They are explained in the section on symbolic mode.
  623. {\tt WHERE} is described in the section on substitutions.
  624. In previous versions of {\REDUCE}, {\em not} was also defined as an infix
  625. operator. In the present version it is a regular prefix operator, and
  626. interchangeable with {\em null}.
  627. For compatibility with the intermediate language used by {\REDUCE}, each
  628. special character infix operator\index{Infix operator} has an alternative
  629. alphanumeric identifier associated with it. These identifiers may be used
  630. interchangeably with the corresponding special character names on input.
  631. This correspondence is as follows:
  632. \begin{quote}
  633. \begin{tabbing}
  634. {\tt := setq} \hspace{0.5in} \= (the assignment operator) \\
  635. {\tt = equal} \\
  636. {\tt >= geq} \\
  637. {\tt > greaterp} \\
  638. {\tt <= leq} \\
  639. {\tt < lessp} \\
  640. {\tt + plus} \\
  641. {\tt - difference} \> (if unary, {\tt minus}) \\
  642. {\tt * times} \\
  643. {\tt / quotient} \> (if unary, {\tt recip}) \\
  644. {\tt \verb|^| or ** expt} \> (raising to a power) \\
  645. {\tt . cons}
  646. \end{tabbing}
  647. \end{quote}
  648. Note: {\tt NEQ} is used to mean {\em not equal}. There is no special
  649. symbol provided for it.
  650. The above operators\index{Operator} are binary, except {\tt NOT} which is
  651. unary and {\tt +} and {\tt *} which are nary (i.e., taking an arbitrary
  652. number of arguments). In addition, {\tt -} and {\tt /} may be used as
  653. unary operators, e.g., /2 means the same as 1/2. Any other operator is
  654. parsed as a binary operator using a left association rule. Thus {\tt
  655. a/b/c} is interpreted as {\tt (a/b)/c}. There are two exceptions to this
  656. rule: {\tt :=} and {\tt .} are right associative. Example: {\tt a:=b:=c}
  657. is interpreted as {\tt a:=(b:=c)}. Unlike ALGOL and PASCAL, {\tt \verb|^|} is
  658. left associative. In other words, {\tt a\verb|^|b\verb|^|c} is interpreted as
  659. {\tt (a\verb|^|b)\verb|^|c}.
  660. The operators\index{Operator} {\tt $<$}, {\tt $<$=}, {\tt $>$}, {\tt $>$=}
  661. can only be used for making comparisons between numbers. No meaning is
  662. currently assigned to this kind of comparison between general expressions.
  663. Parentheses may be used to specify the order of combination. If
  664. parentheses are omitted then this order is by the ordering of the
  665. precedence list\index{Operator precedence} defined by the right-hand side
  666. of the {\tt <infix operator>}\index{Infix operator} table
  667. at the beginning of this section,
  668. from lowest to highest. In other words, {\tt WHERE} has the lowest
  669. precedence, and {\tt .} (the dot operator) the highest.
  670. \chapter{Expressions}
  671. {\REDUCE} expressions\index{Expression} may be of several types and consist
  672. of sequences of numbers, variables, operators, left and right parentheses
  673. and commas. The most common types are as follows:
  674. \section{Scalar Expressions}
  675. \index{Scalar}Using the arithmetic operations {\tt + - * / \verb|^|}
  676. (power) and parentheses, scalar expressions are composed from numbers,
  677. ordinary ``scalar'' variables (identifiers), array names with subscripts,
  678. operator or procedure names with arguments and statement expressions.
  679. {\it Examples:}
  680. \begin{verbatim}
  681. x
  682. x^3 - 2*y/(2*z^2 - df(x,z))
  683. (p^2 + m^2)^(1/2)*log (y/m)
  684. a(5) + b(i,q)
  685. \end{verbatim}
  686. The symbol ** may be used as an alternative to the caret symbol (\verb+^+)
  687. for forming powers, particularly in those systems that do not support a
  688. caret symbol.
  689. Statement expressions, usually in parentheses, can also form part of
  690. a scalar\index{Scalar} expression, as in the example
  691. \begin{verbatim}
  692. w + (c:=x+y) + z .
  693. \end{verbatim}
  694. When the algebraic value of an expression is needed, {\REDUCE} determines it,
  695. starting with the algebraic values of the parts, roughly as follows:
  696. Variables and operator symbols with an argument list have the algebraic
  697. values they were last assigned, or if never assigned stand for themselves.
  698. However, array elements have the algebraic values they were last assigned,
  699. or, if never assigned, are taken to be 0.
  700. Procedures are evaluated with the values of their actual parameters.
  701. In evaluating expressions, the standard rules of algebra are applied.
  702. Unfortunately, this algebraic evaluation of an expression is not as
  703. unambiguous as is numerical evaluation. This process is generally referred
  704. to as ``simplification''\index{Simplification} in the sense that the
  705. evaluation usually but not always produces a simplified form for the
  706. expression.
  707. There are many options available to the user for carrying out such
  708. simplification\index{Simplification}. If the user doesn't specify any
  709. method, the default method is used. The default evaluation of an
  710. expression involves expansion of the expression and collection of like
  711. terms, ordering of the terms, evaluation of derivatives and other
  712. functions and substitution for any expressions which have values assigned
  713. or declared (see assignments and {\tt LET} statements). In many cases,
  714. this is all that the user needs.
  715. The declarations by which the user can exercise some control over the way
  716. in which the evaluation is performed are explained in other sections. For
  717. example, if a real (floating point) number is encountered during
  718. evaluation, the system will normally convert it into a ratio of two
  719. integers. If the user wants to use real arithmetic, he can effect this by
  720. the command {\tt on rounded;}.\ttindex{ROUNDED} Other modes for
  721. coefficient arithmetic are described elsewhere.
  722. If an illegal action occurs during evaluation (such as division by zero)
  723. or functions are called with the wrong number of arguments, and so on, an
  724. appropriate error message is generated.
  725. % A list of such error messages is given in an appendix.
  726. \section{Integer Expressions}
  727. \index{Integer}These are expressions which, because of the values of the
  728. constants and variables in them, evaluate to whole numbers.
  729. {\it Examples:}
  730. \begin{verbatim}
  731. 2, 37 * 999, (x + 3)^2 - x^2 - 6*x
  732. \end{verbatim}
  733. are obviously integer expressions.
  734. \begin{verbatim}
  735. j + k - 2 * j^2
  736. \end{verbatim}
  737. is an integer expression when {\tt J} and {\tt K} have values that are
  738. integers, or if not integers are such that ``the variables and fractions
  739. cancel out'', as in
  740. \begin{verbatim}
  741. k - 7/3 - j + 2/3 + 2*j^2.
  742. \end{verbatim}
  743. \section{Boolean Expressions}
  744. \label{sec-boolean}
  745. A boolean expression\index{Boolean} returns a truth value. In the
  746. algebraic mode of {\REDUCE}, boolean expressions have the syntactical form:
  747. \begin{verbatim}
  748. <expression> <relational operator> <expression>
  749. \end{verbatim}
  750. or
  751. \begin{verbatim}
  752. <boolean operator> (<arguments>)
  753. \end{verbatim}
  754. or
  755. \begin{verbatim}
  756. <boolean expression> <logical operator>
  757. <boolean expression>.
  758. \end{verbatim}
  759. Parentheses can also be used to control the precedence of expressions.
  760. In addition to the logical and relational operators defined earlier as
  761. infix operators, the following boolean operators are also defined:\\
  762. \mbox{}\\
  763. \ttindex{EVENP}\ttindex{FIXP}\ttindex{FREEOF}\ttindex{NUMBERP}
  764. \ttindex{ORDP}\ttindex{PRIMEP}
  765. {\renewcommand{\arraystretch}{2}
  766. \begin{tabular}{lp{\redboxwidth}}
  767. {\tt EVENP(U)} & determines if the number {\tt U} is even or not; \\
  768. {\tt FIXP(U)} & determines if the expression {\tt U} is integer or not; \\
  769. {\tt FREEOF(U,V)} & determines if the expression
  770. {\tt U} does not contain the kernel {\tt V} anywhere in its
  771. structure; \\
  772. {\tt NUMBERP(U)} & determines if {\tt U} is a number or not; \\
  773. {\tt ORDP(U,V)} & determines if {\tt U} is ordered
  774. ahead of {\tt V} by some canonical ordering (based on the expression structure
  775. and an internal ordering of identifiers); \\
  776. {\tt PRIMEP(U)} & true if {\tt U} is a prime object, i.e., any object
  777. other than 0 and plus or minus 1 which is only exactly divisible
  778. by itself or a unit.
  779. \\
  780. \end{tabular}}
  781. {\it Examples:}
  782. \begin{verbatim}
  783. j<1
  784. x>0 or x=-2
  785. numberp x
  786. fixp x and evenp x
  787. numberp x and x neq 0
  788. \end{verbatim}
  789. Boolean expressions can only appear directly within {\tt IF}, {\tt FOR},
  790. {\tt WHILE}, and {\tt UNTIL} statements, as described in other sections.
  791. Such expressions cannot be used in place of ordinary algebraic expressions,
  792. or assigned to a variable.
  793. NB: For those familiar with symbolic mode, the meaning of some of
  794. these operators is different in that mode. For example, {\tt NUMBERP} is
  795. true only for integers and reals in symbolic mode.
  796. When two or more boolean expressions are combined with {\tt AND}, they are
  797. evaluated one by one until a {\em false\/} expression is found. The rest are
  798. not evaluated. Thus
  799. \begin{verbatim}
  800. numberp x and numberp y and x>y
  801. \end{verbatim}
  802. does not attempt to make the {\tt x>y} comparison unless {\tt X} and {\tt Y}
  803. are both verified to be numbers.
  804. Similarly, evaluation of a sequence of boolean expressions connected by
  805. {\tt OR} stops as soon as a {\em true\/} expression is found.
  806. NB: In a boolean expression, and in a place where a boolean expression is
  807. expected, the algebraic value 0 is interpreted as {\em false}, while all
  808. other algebraic values are converted to {\em true}. So in algebraic mode
  809. a procedure can be written for direct usage in boolean expressions,
  810. returning say 1 or 0 as its value as in
  811. \begin{verbatim}
  812. procedure polynomialp(u,x);
  813. if den(u)=1 and deg(u,x)>=1 then 1 else 0;
  814. \end{verbatim}
  815. One can then use this in a boolean construct, such as
  816. \begin{verbatim}
  817. if polynomialp(q,z) and not polynomialp(q,y) then ...
  818. \end{verbatim}
  819. In addition, any procedure that does not have a defined return value
  820. (for example, a block without a {\tt RETURN} statement in it)
  821. has the boolean value {\em false}.
  822. \section{Equations}
  823. Equations\index{Equation} are a particular type of expression with the syntax
  824. \begin{verbatim}
  825. <expression> = <expression>.
  826. \end{verbatim}
  827. In addition to their role as boolean expressions, they can also be used as
  828. arguments to several operators (e.g., {\tt SOLVE}), and can be
  829. returned as values.
  830. Under normal circumstances, the right-hand-side of the equation is
  831. evaluated but not the left-hand-side. This also applies to any substitutions
  832. made by the {\tt SUB}\ttindex{SUB} operator. If both sides are to be
  833. evaluated, the switch {\tt EVALLHSEQP}\ttindex{EVALLHSEQP} should be
  834. turned on.
  835. To facilitate the handling of equations, two selectors, {\tt LHS}
  836. \ttindex{LHS} and {\tt RHS},\ttindex{RHS} which return the left- and
  837. right-hand sides of a equation\index{Equation} respectively, are provided.
  838. For example,
  839. \begin{verbatim}
  840. lhs(a+b=c) -> a+b
  841. and
  842. rhs(a+b=c) -> c.
  843. \end{verbatim}
  844. \section{Proper Statements as Expressions}
  845. Several kinds of proper statements\index{Proper statement} deliver
  846. an algebraic or numerical result of some kind, which can in turn be used as
  847. an expression or part of an expression. For example, an assignment
  848. statement itself has a value, namely the value assigned. So
  849. \begin{verbatim}
  850. 2 * (x := a+b)
  851. \end{verbatim}
  852. is equal to {\tt 2*(a+b)}, as well as having the ``side-effect''\index{Side
  853. effect} of assigning the value {\tt a+b} to {\tt X}. In context,
  854. \begin{verbatim}
  855. y := 2 * (x := a+b);
  856. \end{verbatim}
  857. sets {\tt X} to {\tt a+b} and {\tt Y} to {\tt 2*(a+b)}.
  858. The sections on the various proper statement\index{Proper statement} types
  859. indicate which of these statements are also useful as expressions.
  860. \chapter{Lists}
  861. A list\index{List} is an object consisting of a sequence of other objects
  862. (including lists themselves), separated by commas and surrounded by
  863. braces. Examples of lists are:
  864. \begin{verbatim}
  865. {a,b,c}
  866. {1,a-b,c=d}
  867. {{a},{{b,c},d},e}.
  868. \end{verbatim}
  869. The empty list is represented as
  870. \begin{verbatim}
  871. {}.
  872. \end{verbatim}
  873. \section{Operations on Lists}\index{List operation}
  874. Several operators in the system return their results as lists, and a user
  875. can create new lists using braces and commas. Alternatively, one can use
  876. the operator LIST to construct a list. An important class of operations
  877. on lists are MAP and SELECT operations. For details, please refer to the
  878. chapters on MAP, SELECT and the FOR command. See also the documentation
  879. on the ASSIST package.
  880. To facilitate the use of
  881. lists, a number of operators are also available for manipulating
  882. them. {\tt PART(<list>,n)}\ttindex{PART} for example will return the
  883. $n^{th}$ element of a list. {\tt LENGTH}\ttindex{LENGTH} will return the
  884. length of a list. Several operators are also defined uniquely for lists.
  885. For those familiar with them, these operators in fact mirror the
  886. operations defined for Lisp lists. These operators are as follows:
  887. \subsection{LIST}
  888. The operator LIST is an alternative to the usage of curly brackets. LIST
  889. accepts an arbitrary number of arguments and returns a list
  890. of its arguments. This operator is useful in cases where operators
  891. have to be passed as arguments. E.g.,
  892. \begin{verbatim}
  893. list(a,list(list(b,c),d),e); -> {{a},{{b,c},d},e}
  894. \end{verbatim}
  895. \subsection{FIRST}
  896. This operator\ttindex{FIRST} returns the first member of a list. An error
  897. occurs if the argument is not a list, or the list is empty.
  898. \subsection{SECOND}
  899. {\tt SECOND}\ttindex{SECOND} returns the second member of a list. An error
  900. occurs if the argument is not a list or has no second element.
  901. \subsection{THIRD}
  902. This operator\ttindex{THIRD} returns the third member of a list. An error
  903. occurs if the argument is not a list or has no third element.
  904. \subsection{REST}
  905. {\tt REST}\ttindex{REST} returns its argument with the first element
  906. removed. An error occurs if the argument is not a list, or is empty.
  907. \subsection{$.$ (Cons) Operator}
  908. This operator\ttindex{. (CONS)} adds (``conses'') an expression to the
  909. front of a list. For example:
  910. \begin{verbatim}
  911. a . {b,c} -> {a,b,c}.
  912. \end{verbatim}
  913. \subsection{APPEND}
  914. This operator\ttindex{APPEND} appends its first argument to its second to
  915. form a new list.
  916. {\it Examples:}
  917. \begin{verbatim}
  918. append({a,b},{c,d}) -> {a,b,c,d}
  919. append({{a,b}},{c,d}) -> {{a,b},c,d}.
  920. \end{verbatim}
  921. \subsection{REVERSE}
  922. The operator {\tt REVERSE}\ttindex{REVERSE} returns its argument with the
  923. elements in the reverse order. It only applies to the top level list, not
  924. any lower level lists that may occur. Examples are:\index{List operation}
  925. \begin{verbatim}
  926. reverse({a,b,c}) -> {c,b,a}
  927. reverse({{a,b,c},d}) -> {d,{a,b,c}}.
  928. \end{verbatim}
  929. \subsection{List Arguments of Other Operators}
  930. If an operator other than those specifically defined for lists is given a
  931. single argument that is a list, then the result of this operation will be
  932. a list in which that operator is applied to each element of the list. For
  933. example, the result of evaluating {\tt log\{a,b,c\}} is the expression
  934. {\tt \{LOG(A),LOG(B),LOG(C)\}}.
  935. There are two ways to inhibit this operator distribution. Firstly, the
  936. switch {\tt LISTARGS},\ttindex{LISTARGS} if on, will globally inhibit
  937. such distribution. Secondly, one can inhibit this distribution for a
  938. specific operator by the declaration {\tt LISTARGP}.\ttindex{LISTARGP} For
  939. example, with the declaration {\tt listargp log}, {\tt log\{a,b,c\}} would
  940. evaluate to {\tt LOG(\{A,B,C\})}.
  941. If an operator has more than one argument, no such distribution occurs.
  942. \subsection{Caveats and Examples}
  943. Some of the natural list operations such as {\it member} or {\it delete}
  944. are available only after loading the package {\it ASSIST}.
  945. Please note that a non-list as second argument to CONS
  946. (a "dotted pair" in LISP terms) is not allowed
  947. and causes an "invalid as list" error.
  948. \begin{verbatim}
  949. a := 17 . 4;
  950. ***** 17 4 invalid as list
  951. \end{verbatim}
  952. Also, the initialization of a scalar variable is not the empty list --
  953. one has to set list type variables explicitly, as in the following
  954. example:
  955. \begin{verbatim}
  956. load_package assist;
  957. procedure lotto (n,m);
  958. begin scalar list_1_n, luckies, hit;
  959. list_1_n := {};
  960. luckies := {};
  961. for k:=1:n do list_1_n := k . list_1_n;
  962. for k:=1:m do
  963. << hit := part(list_1_n,random(n-k+1) + 1);
  964. list_1_n := delete(hit,list_1_n);
  965. luckies := hit . luckies >>;
  966. return luckies;
  967. end; % In Germany, try lotto (49,6);
  968. \end{verbatim}
  969. {\it Another example:} Find all coefficients of a multivariate
  970. polynomial with respect to a list of variables:
  971. \begin{verbatim}
  972. procedure allcoeffs(q,lis); % q : polynomial, lis: list of vars
  973. allcoeffs1 (list q,lis);
  974. procedure allcoeffs1(q,lis);
  975. if lis={} then q else
  976. allcoeffs1(foreach qq in q join coeff(qq,first lis),rest lis);
  977. \end{verbatim}
  978. \chapter{Statements}
  979. A statement\index{Statement} is any combination of reserved words and
  980. expressions, and has the syntax \index{Proper statement}
  981. \begin{verbatim}
  982. <statement> ::= <expression>|<proper statement>
  983. \end{verbatim}
  984. A {\REDUCE} program consists of a series of commands which are statements
  985. followed by a terminator:\index{Terminator}\index{Semicolon}
  986. \index{Dollar sign}
  987. \begin{verbatim}
  988. <terminator> ::= ;|$
  989. \end{verbatim}
  990. The division of the program into lines is arbitrary. Several statements
  991. can be on one line, or one statement can be freely broken onto several
  992. lines. If the program is run interactively, statements ending with ; or \$
  993. are not processed until an end-of-line character is encountered. This
  994. character can vary from system to system, but is normally the \key{Return}
  995. key on an ASCII terminal. Specific systems may also use additional keys
  996. as statement terminators.
  997. If a statement is a proper statement\index{Proper statement}, the
  998. appropriate action takes place.
  999. Depending on the nature of the proper statement some result or response may
  1000. or may not be printed out, and the response may or may not depend on the
  1001. terminator used.
  1002. If a statement is an expression, it is evaluated. If the terminator is a
  1003. semicolon, the result is printed. If the terminator is a dollar sign, the
  1004. result is not printed. Because it is not usually possible to know in
  1005. advance how large an expression will be, no explicit format statements are
  1006. offered to the user. However, a variety of output declarations are
  1007. available so that the output can be produced in different forms. These
  1008. output declarations are explained in Section~\ref{sec-output}.
  1009. The following sub-sections describe the types of proper statements
  1010. \index{Proper statement} in {\REDUCE}.
  1011. \section{Assignment Statements}
  1012. These statements\index{Assignment} have the syntax
  1013. \begin{verbatim}
  1014. <assignment statement> ::= <expression> := <expression>
  1015. \end{verbatim}
  1016. The {\tt <expression>} on the left side is normally the name of a variable, an
  1017. operator symbol with its list of arguments filled in, or an array name with
  1018. the proper number of integer subscript values within the array bounds. For
  1019. example:
  1020. \begin{quote}
  1021. \begin{tabbing}
  1022. {\tt a1 := b + c} \\
  1023. {\tt h(l,m) := x-2*y} \hspace{1in} \= (where {\tt h} is an operator) \\
  1024. {\tt k(3,5) := x-2*y} \> (where {\tt k} is a 2-dim. array)
  1025. \end{tabbing}
  1026. \end{quote}
  1027. More general assignments\index{Assignment} such as {\tt a+b := c} are also
  1028. allowed. The effect of these is explained in Section~\ref{sec-gensubs}.
  1029. An assignment statement causes the expression on the right-hand-side to be
  1030. evaluated. If the left-hand-side is a variable, the value of the
  1031. right-hand-side is assigned to that unevaluated variable. If the
  1032. left-hand-side is an operator or array expression, the arguments of that
  1033. operator or array are evaluated, but no other simplification done. The
  1034. evaluated right-hand-side is then assigned to the resulting expression.
  1035. For example, if {\tt A} is a single-dimensional array, {\tt a(1+1) := b}
  1036. assigns the value {\tt B} to the array element {\tt a(2)}.
  1037. If a semicolon is used as the terminator when an assignment
  1038. \index{Assignment} is issued as a command (i.e. not as a part of a group
  1039. statement or procedure or other similar construct), the left-hand side
  1040. symbol of the assignment statement is printed out, followed by a
  1041. ``{\tt :=}'', followed by the value of the expression on the right.
  1042. It is also possible to write a multiple assignment statement:
  1043. \index{Multiple assignment statement}
  1044. \begin{verbatim}
  1045. <expression> := ... := <expression> := <expression>
  1046. \end{verbatim}
  1047. In this form, each {\tt <expression>} but the last is set to the value of
  1048. the last {\tt <expression>}. If a semicolon is used as a terminator, each
  1049. expression except the last is printed followed by a ``{\tt :=}'' ending
  1050. with the value of the last expression.
  1051. \subsection{Set Statement}
  1052. In some cases, it is desirable to perform an assignment in which {\em both\/}
  1053. the left- and right-hand sides of an assignment\index{Assignment} are
  1054. evaluated. In this case, the {\tt SET}\ttindex{SET} statement can be used
  1055. with the syntax:
  1056. \begin{verbatim}
  1057. SET(<expression>,<expression>);
  1058. \end{verbatim}
  1059. For example, the statements
  1060. \begin{verbatim}
  1061. j := 23;
  1062. set(mkid(a,j),x);
  1063. \end{verbatim}
  1064. assigns the value {\tt X} to {\tt A23}.
  1065. \section{Group Statements}
  1066. The group statement\index{Group statement} is a construct used where
  1067. {\REDUCE} expects a single statement, but a series of actions needs to be
  1068. performed. It is formed by enclosing one or more statements (of any kind)
  1069. between the symbols {\tt $<<$} and {\tt $>>$}, separated by semicolons or
  1070. dollar signs -- it doesn't matter which. The statements are executed one
  1071. after another.
  1072. Examples will be given in the sections on {\tt IF}\ttindex{IF} and other
  1073. types of statements in which the {\tt $<<$} \ldots {\tt $>>$} construct is
  1074. useful.
  1075. If the last statement in the enclosed group has a value, then that is also
  1076. the value of the group statement. Care must be taken not to have a
  1077. semicolon or dollar sign after the last grouped statement, if the value of
  1078. the group is relevant: such an extra terminator causes the group to have
  1079. the value NIL or zero.
  1080. \section{Conditional Statements}
  1081. The conditional statement\index{Conditional statement} has the following
  1082. syntax:
  1083. \begin{verbatim}
  1084. <conditional statement> ::=
  1085. IF <boolean expression> THEN <statement> [ELSE <statement>]
  1086. \end{verbatim}
  1087. The boolean expression is evaluated. If this is {\em true}, the first
  1088. {\tt <statement>} is executed. If it is {\em false}, the second is.
  1089. {\it Examples:}
  1090. \begin{verbatim}
  1091. if x=5 then a:=b+c else d:=e+f
  1092. if x=5 and numberp y
  1093. then <<ff:=q1; a:=b+c>>
  1094. else <<ff:=q2; d:=e+f>>
  1095. \end{verbatim}
  1096. Note the use of the group statement\index{Group statement}.
  1097. \\
  1098. Conditional statements associate to the right; i.e.,\ttindex{IF}
  1099. \begin{verbatim}
  1100. IF <a> THEN <b> ELSE IF <c> THEN <d> ELSE <e>
  1101. \end{verbatim}
  1102. is equivalent to:
  1103. \begin{verbatim}
  1104. IF <a> THEN <b> ELSE (IF <c> THEN <d> ELSE <e>)
  1105. \end{verbatim}
  1106. In addition, the construction
  1107. \begin{verbatim}
  1108. IF <a> THEN IF <b> THEN <c> ELSE <d>
  1109. \end{verbatim}
  1110. parses as
  1111. \begin{verbatim}
  1112. IF <a> THEN (IF <b> THEN <c> ELSE <d>).
  1113. \end{verbatim}
  1114. If the value of the conditional statement\index{Conditional
  1115. statement} is of primary interest, it is often called a conditional
  1116. expression instead. Its value is the value of whichever statement was
  1117. executed. (If the executed statement has no value, the conditional
  1118. expression has no value or the value 0, depending on how it is used.)
  1119. {\it Examples:}
  1120. \begin{verbatim}
  1121. a:=if x<5 then 123 else 456;
  1122. b:=u + v^(if numberp z then 10*z else 1) + w;
  1123. \end{verbatim}
  1124. If the value is of no concern, the {\tt ELSE} clause may be omitted if no
  1125. action is required in the {\em false\/} case.
  1126. \begin{verbatim}
  1127. if x=5 then a:=b+c;
  1128. \end{verbatim}
  1129. Note: As explained in Section~\ref{sec-boolean},a
  1130. if a scalar or numerical expression is used in place of
  1131. the boolean expression -- for example, a variable is written there -- the
  1132. {\em true\/} alternative is followed unless the expression has the value 0.
  1133. \section{FOR Statements}
  1134. The {\tt FOR} statement is used to define a variety of program
  1135. loops\index{Loop}. Its general syntax is as follows:\ttindex{UNTIL}
  1136. \ttindex{DO}\ttindex{PRODUCT}\ttindex{SUM}\ttindex{COLLECT}\ttindex{JOIN}
  1137. \begin{small}
  1138. \[ \mbox{\tt FOR} \left\{ \begin{array}{@{}ccc@{}}
  1139. \mbox{\tt \meta{var} := \meta{number} } \left\{ \begin{array}{@{}c@{}}
  1140. \mbox{\tt STEP \meta{number} UNTIL} \\
  1141. \mbox{\tt :}
  1142. \end{array}
  1143. \right\} \mbox{\tt \meta{number}} \\[3mm]
  1144. \multicolumn{1}{c}{\mbox{\tt EACH \meta{var}
  1145. \(\left\{
  1146. \begin{tabular}{@{}c@{}}
  1147. IN \\ ON
  1148. \end{tabular}
  1149. \right\}\)
  1150. \meta{list}}}
  1151. \end{array}
  1152. \right\} \mbox{\tt \meta{action} \meta{exprn}} \]
  1153. \end{small}%
  1154. %
  1155. where
  1156. \begin{center}
  1157. \tt \meta{action} ::= do|product|sum|collect|join.
  1158. \end{center}
  1159. The assignment\index{Assignment} form of the {\tt FOR} statement defines an
  1160. iteration over the indicated numerical range. If expressions that do not
  1161. evaluate to numbers are used in the designated places, an error will
  1162. result.
  1163. The {\tt FOR EACH}\ttindex{FOR EACH} form of the {\tt FOR} statement is
  1164. designed to iterate down a list. Again, an error will occur if a list is
  1165. not used.
  1166. The action {\tt DO}\ttindex{DO} means that {\tt <exprn>} is simply
  1167. evaluated and no value kept; the statement returning 0 in this case (or no
  1168. value at the top level). {\tt COLLECT} means that the results of
  1169. evaluating {\tt <exprn>} each time are linked together to make a list,
  1170. and {\tt JOIN} means that the values of {\tt <exprn>} are themselves
  1171. lists that are joined to make one list (similar to {\tt CONC} in Lisp).
  1172. Finally, {\tt PRODUCT}\ttindex{PRODUCT} and {\tt SUM}\ttindex{SUM}
  1173. form the respective combined value out of the values of {\tt <exprn>}.
  1174. In all cases, {\tt <exprn>} is evaluated algebraically within the
  1175. scope of the current value of {\tt <var>}. If {\tt <action>} is
  1176. {\tt DO}\ttindex{DO}, then nothing else happens. In other cases, {\tt
  1177. <action>} is a binary operator that causes a result to be built up and
  1178. returned by {\tt FOR}. In those cases, the loop\index{Loop} is
  1179. initialized to a default value ({\tt 0} for {\tt SUM},\ttindex{SUM} {\tt
  1180. 1} for {\tt PRODUCT},\ttindex{PRODUCT} and an empty list for the other
  1181. actions). The test for the end condition is made before any action is
  1182. taken. As in Pascal, if the variable is out of range in the assignment
  1183. case, or the {\tt <list>} is empty in the {\tt FOR EACH}\ttindex{FOR EACH}
  1184. case, {\tt <exprn>} is not evaluated at all.
  1185. {\it Examples:}
  1186. \begin{enumerate}
  1187. \item If {\tt A}, {\tt B} have been declared to be arrays, the following
  1188. stores $5^{2}$ through $10^{2}$ in {\tt A(5)} through {\tt A(10)}, and at
  1189. the same time stores the cubes in the {\tt B} array:
  1190. \begin{verbatim}
  1191. for i := 5 step 1 until 10 do <<a(i):=i^2; b(i):=i^3>>
  1192. \end{verbatim}
  1193. \item As a convenience, the common construction
  1194. \begin{verbatim}
  1195. STEP 1 UNTIL
  1196. \end{verbatim}
  1197. may be abbreviated to a colon. Thus, instead of the above we could write:
  1198. \begin{verbatim}
  1199. for i := 5:10 do <<a(i):=i^2; b(i):=i^3>>
  1200. \end{verbatim}
  1201. \item The following sets {\tt C} to the sum of the squares of 1,3,5,7,9;
  1202. and {\tt D} to the expression {\tt x*(x+1)*(x+2)*(x+3)*(x+4):}
  1203. \begin{verbatim}
  1204. c := for j:=1 step 2 until 9 sum j^2;
  1205. d := for k:=0 step 1 until 4 product (x+k);
  1206. \end{verbatim}
  1207. \item The following forms a list of the squares of the elements of the list
  1208. {\tt \{a,b,c\}:}\ttindex{FOR EACH}
  1209. \begin{verbatim}
  1210. for each x in {a,b,c} collect x^2;
  1211. \end{verbatim}
  1212. \item The following forms a list of the listed squares of the elements of the
  1213. list {\tt \{a,b,c\}}
  1214. (i.e., {\tt \{\{A\verb|^|2\},\{B\verb|^|2\},\{C\verb|^|2\}\}):}
  1215. \begin{verbatim}
  1216. for each x in {a,b,c} collect {x^2};
  1217. \end{verbatim}
  1218. \item The following also forms a list of the squares of the elements of
  1219. the list {\tt \{a,b,c\},} since the {\tt JOIN} operation joins the
  1220. individual lists into one list:\ttindex{FOR EACH}
  1221. \begin{verbatim}
  1222. for each x in {a,b,c} join {x^2};
  1223. \end{verbatim}
  1224. \end{enumerate}
  1225. The control variable used in the {\tt FOR} statement is actually a new
  1226. variable, not related to the variable of the same name outside the {\tt
  1227. FOR} statement. In other words, executing a statement {\tt for i:=} \ldots
  1228. doesn't change the system's assumption that $i^{2} = -1$.
  1229. Furthermore, in algebraic mode, the value of the control variable is
  1230. substituted in {\tt <exprn>} only if it occurs explicitly in that
  1231. expression. It will not replace a variable of the same name in the value
  1232. of that expression. For example:
  1233. \begin{verbatim}
  1234. b := a; for a := 1:2 do write b;
  1235. \end{verbatim}
  1236. prints {\tt A} twice, not 1 followed by 2.
  1237. \section{WHILE \ldots DO}
  1238. The\ttindex{WHILE} {\tt FOR \ldots DO}\ttindex{DO} feature allows easy
  1239. coding of a repeated operation in which the number of repetitions is known
  1240. in advance. If the criterion for repetition is more complicated, {\tt
  1241. WHILE \ldots DO} can often be used. Its syntax is:
  1242. \begin{verbatim}
  1243. WHILE <boolean expression> DO <statement>
  1244. \end{verbatim}
  1245. The {\tt WHILE \ldots DO} controls the single statement following {\tt DO}.
  1246. If several statements are to be repeated, as is almost always the case,
  1247. they must be grouped using the $<<$ \ldots $>>$ or {\tt BEGIN \ldots END}
  1248. as in the example below.
  1249. The {\tt WHILE} condition is tested each time {\em before\/} the action
  1250. following the {\tt DO} is attempted. If the condition is false to begin
  1251. with, the action is not performed at all. Make sure that what is to be
  1252. tested has an appropriate value initially.
  1253. {\it Example:}
  1254. Suppose we want to add up a series of terms, generated one by one, until
  1255. we reach a term which is less than 1/1000 in value. For our simple
  1256. example, let us suppose the first term equals 1 and each term is obtained
  1257. from the one before by taking one third of it and adding one third its
  1258. square. We would write:
  1259. \begin{verbatim}
  1260. ex:=0; term:=1;
  1261. while num(term - 1/1000) >= 0 do
  1262. <<ex := ex+term; term:=(term + term^2)/3>>;
  1263. ex;
  1264. \end{verbatim}
  1265. As long as {\tt TERM} is greater than or equal to ({\tt >=}) 1/1000 it will
  1266. be added to {\tt EX} and the next {\tt TERM} calculated. As soon as {\tt
  1267. TERM} becomes less than 1/1000 the {\tt WHILE} test fails and the {\tt
  1268. TERM} will not be added.
  1269. \section{REPEAT \ldots UNTIL}
  1270. \ttindex{REPEAT} {\tt REPEAT \ldots UNTIL} is very similar in purpose to
  1271. {\tt WHILE \ldots DO}. Its syntax is:
  1272. \begin{verbatim}
  1273. REPEAT <statement> UNTIL <boolean expression>
  1274. \end{verbatim}
  1275. (PASCAL users note: Only a single statement -- usually a group statement
  1276. -- is allowed between the {\tt REPEAT} and the {\tt UNTIL.)}
  1277. There are two essential differences:
  1278. \begin{enumerate}
  1279. \item The test is performed {\em after\/} the controlled statement (or group of
  1280. statements) is executed, so the controlled statement is always executed at
  1281. least once.
  1282. \item The test is a test for when to stop rather than when to continue, so its
  1283. ``polarity'' is the opposite of that in {\tt WHILE \ldots DO.}
  1284. \end{enumerate}
  1285. As an example, we rewrite the example from the {\tt WHILE \ldots DO} section:
  1286. \begin{samepage}
  1287. \begin{verbatim}
  1288. ex:=0; term:=1;
  1289. repeat <<ex := ex+term; term := (term + term^2)/3>>
  1290. until num(term - 1/1000) < 0;
  1291. ex;
  1292. \end{verbatim}
  1293. \end{samepage}
  1294. In this case, the answer will be the same as before, because in neither
  1295. case is a term added to {\tt EX} which is less than 1/1000.
  1296. \section{Compound Statements}
  1297. \index{Compound statement}Often the desired process can best (or only) be
  1298. described as a series of steps to be carried out one after the other. In
  1299. many cases, this can be achieved by use of the group statement\index{Group
  1300. statement}. However, each step often provides some intermediate
  1301. result, until at the end we have the final result wanted. Alternatively,
  1302. iterations on the steps are needed that are not possible with constructs
  1303. such as {\tt WHILE}\ttindex{WHILE} or {\tt REPEAT}\ttindex{REPEAT}
  1304. statements. In such cases the steps of the process must be
  1305. enclosed between the words {\tt BEGIN} and {\tt END}\ttindex{BEGIN \ldots
  1306. END} forming what is technically called a {\em block\/}\index{Block} or
  1307. {\em compound\/} statement. Such a compound statement can in fact be used
  1308. wherever a group statement appears. The converse is not true: {\tt BEGIN
  1309. \ldots END} can be used in ways that {\tt $<<$} \ldots {\tt $>>$} cannot.
  1310. If intermediate results must be formed, local variables must be provided
  1311. in which to store them. {\em Local\/} means that their values are deleted as
  1312. soon as the block's operations are complete, and there is no conflict with
  1313. variables outside the block that happen to have the same name. Local
  1314. variables are created by a {\tt SCALAR}\ttindex{SCALAR} declaration
  1315. immediately after the {\tt BEGIN}:
  1316. \begin{verbatim}
  1317. scalar a,b,c,z;
  1318. \end{verbatim}
  1319. If more convenient, several {\tt SCALAR} declarations can be given one after
  1320. another:
  1321. \begin{verbatim}
  1322. scalar a,b,c;
  1323. scalar z;
  1324. \end{verbatim}
  1325. In place of {\tt SCALAR} one can also use the declarations
  1326. {\tt INTEGER}\ttindex{INTEGER} or {\tt REAL}\ttindex{REAL}. In the present
  1327. version of {\REDUCE} variables declared {\tt INTEGER} are expected to have
  1328. only integer values, and are initialized to 0. {\tt REAL}
  1329. variables on the other hand are currently treated as algebraic mode {\tt
  1330. SCALAR}s.
  1331. {\it CAUTION:} {\tt INTEGER}, {\tt REAL} and {\tt SCALAR} declarations can
  1332. only be given immediately after a {\tt BEGIN}. An error will result if
  1333. they are used after other statements in a block (including {\tt ARRAY} and
  1334. {\tt OPERATOR} declarations, which are global in scope), or outside the
  1335. top-most block (e.g., at the top level). All variables declared {\tt
  1336. SCALAR} are automatically initialized to zero in algebraic mode ({\tt NIL}
  1337. in symbolic mode).
  1338. Any symbols not declared as local variables in a block refer to the
  1339. variables of the same name in the current calling environment. In
  1340. particular, if they are not so declared at a higher level (e.g., in a
  1341. surrounding block or as parameters in a calling procedure), their values can
  1342. be permanently changed.
  1343. Following the {\tt SCALAR}\ttindex{SCALAR} declaration(s), if any, write the
  1344. statements to be executed, one after the other, separated by delimiters
  1345. (e.g., {\tt ;} or {\tt \$}) (it doesn't matter which). However, from a
  1346. stylistic point of view, {\tt ;} is preferred.
  1347. The last statement in the body, just before {\tt END}, need not have a
  1348. terminator (since the {\tt BEGIN \ldots END} are in a sense brackets
  1349. confining the block statements). The last statement must also be the
  1350. command {\tt RETURN}\ttindex{RETURN} followed by the variable or
  1351. expression whose value is to be the value returned by the procedure. If
  1352. the {\tt RETURN} is omitted (or nothing is written after the word
  1353. {\tt RETURN}) the procedure will have no value or the value zero, depending
  1354. on how it is used (and {\tt NIL} in symbolic mode). Remember to put a
  1355. terminator after the {\tt END}.
  1356. {\it Example:}
  1357. Given a previously assigned integer value for {\tt N}, the following block
  1358. will compute the Legendre polynomial of degree {\tt N} in the variable
  1359. {\tt X}:
  1360. \begin{verbatim}
  1361. begin scalar seed,deriv,top,fact;
  1362. seed:=1/(y^2 - 2*x*y +1)^(1/2);
  1363. deriv:=df(seed,y,n);
  1364. top:=sub(y=0,deriv);
  1365. fact:=for i:=1:n product i;
  1366. return top/fact
  1367. end;
  1368. \end{verbatim}
  1369. \subsection{Compound Statements with GO TO}
  1370. It is possible to have more complicated structures inside the {\tt BEGIN
  1371. \ldots END}\ttindex{BEGIN \ldots END} brackets than indicated in the
  1372. previous example. That the individual lines of the program need not be
  1373. assignment\index{Assignment} statements, but could be almost any other
  1374. kind of statement or command, needs no explanation. For example,
  1375. conditional statements, and {\tt WHILE}\ttindex{WHILE} and {\tt REPEAT}
  1376. \ttindex{REPEAT} constructions, have an obvious role in defining more
  1377. intricate blocks.
  1378. If these structured constructs don't suffice, it is possible to use labels
  1379. \index{Label} and {\tt GO} {\tt TO}s\ttindex{GO TO} within a compound
  1380. statement,\index{Compound statement} and also to use {\tt RETURN}
  1381. \ttindex{RETURN} in places within the block other than just before the
  1382. {\tt END}. The following subsections discuss these matters in detail.
  1383. For many readers the following example, presenting one possible definition
  1384. of a process to calculate the factorial of {\tt N} for preassigned {\tt N}
  1385. will suffice:
  1386. {\it Example:}
  1387. \begin{verbatim}
  1388. begin scalar m;
  1389. m:=1;
  1390. l: if n=0 then return m;
  1391. m:=m*n;
  1392. n:=n-1;
  1393. go to l
  1394. end;
  1395. \end{verbatim}
  1396. \subsection{Labels and GO TO Statements}
  1397. \index{Label}\ttindex{GO TO}Within a {\tt BEGIN \ldots END} compound
  1398. statement it is possible to label statements, and transfer to them out of
  1399. sequence using {\tt GO} {\tt TO} statements. Only statements on the top
  1400. level inside compound statements can be labeled, not ones inside
  1401. subsidiary constructions like {\tt $<<$} \ldots {\tt $>>$}, {\tt IF} \ldots
  1402. {\tt THEN} \ldots , {\tt WHILE} \ldots {\tt DO} \ldots , etc.
  1403. Labels and {\tt GO TO} statements have the syntax:
  1404. \begin{verbatim}
  1405. <go to statement> ::= GO TO <label> | GOTO <label>
  1406. <label> ::= <identifier>
  1407. <labeled statement> ::= <label>:<statement>
  1408. \end{verbatim}
  1409. Note that statement names cannot be used as labels.
  1410. While {\tt GO TO} is an unconditional transfer, it is frequently used
  1411. in conditional statements such as
  1412. \begin{verbatim}
  1413. if x>5 then go to abcd;
  1414. \end{verbatim}
  1415. giving the effect of a conditional transfer.
  1416. Transfers using {\tt GO TO}s can only occur within the block in which the
  1417. {\tt GO TO} is used. In other words, you cannot transfer from an inner
  1418. block to an outer block using a {\tt GO TO}. However, if a group statement
  1419. occurs within a compound statement, it is possible to jump out of that group
  1420. statement to a point within the compound statement using a {\tt GO TO}.
  1421. \subsection{RETURN Statements}
  1422. The value corresponding to a {\tt BEGIN \ldots END} compound statement,
  1423. \ttindex{BEGIN \ldots END} such as a procedure body, is normally 0 ({\tt
  1424. NIL} in symbolic mode). By executing a {\tt RETURN}\ttindex{RETURN}
  1425. statement in the compound statement a different value can be returned.
  1426. After a {\tt RETURN} statement is executed, no further statements within
  1427. the compound statement are executed.
  1428. {\tt Examples:}
  1429. \begin{verbatim}
  1430. return x+y;
  1431. return m;
  1432. return;
  1433. \end{verbatim}
  1434. Note that parentheses are not required around the {\tt x+y}, although they
  1435. are permitted. The last example is equivalent to {\tt return 0} or {\tt
  1436. return nil}, depending on whether the block is used as part of an
  1437. expression or not.
  1438. Since {\tt RETURN}\ttindex{RETURN} actually moves up only one
  1439. block\index{Block} level, in a sense the casual user is not expected to
  1440. understand, we tabulate some cautions concerning its use.
  1441. \begin{enumerate}
  1442. \item {\tt RETURN} can be used on the top level inside the compound
  1443. statement, i.e. as one of the statements bracketed together by the {\tt
  1444. BEGIN \ldots END}\ttindex{BEGIN \ldots END}
  1445. \item {\tt RETURN} can be used within a top level {\tt $<<$} \ldots {\tt
  1446. $>>$} construction within the compound statement. In this case, the {\tt
  1447. RETURN} transfers control out of both the group statement and the compound
  1448. statement.
  1449. \item {\tt RETURN} can be used within an {\tt IF} \ldots {\tt THEN} \ldots
  1450. {\tt ELSE} \ldots on the top level within the compound statement.
  1451. \end{enumerate}
  1452. NOTE: At present, there is no construct provided to permit early
  1453. termination of a {\tt FOR}\ttindex{FOR}, {\tt WHILE}\ttindex{WHILE},
  1454. or {\tt REPEAT}\ttindex{REPEAT} statement. In particular, the use of
  1455. {\tt RETURN} in such cases results in a syntax error. For example,
  1456. \begin{verbatim}
  1457. begin scalar y;
  1458. y := for i:=0:99 do if a(i)=x then return b(i);
  1459. ...
  1460. \end{verbatim}
  1461. will lead to an error.
  1462. \chapter{Commands and Declarations}
  1463. A command\index{Command} is an order to the system to do something. Some
  1464. commands cause visible results (such as calling for input or output);
  1465. others, usually called declarations\index{Declaration}, set options,
  1466. define properties of variables, or define procedures. Commands are
  1467. formally defined as a statement followed by a terminator
  1468. \begin{verbatim}
  1469. <command> ::= <statement> <terminator>
  1470. <terminator> ::= ;|$
  1471. \end{verbatim}
  1472. Some {\REDUCE} commands and declarations are described in the following
  1473. sub-sections.
  1474. \section{Array Declarations}
  1475. Array\ttindex{ARRAY} declarations in {\REDUCE} are similar to FORTRAN
  1476. dimension statements. For example:
  1477. \begin{verbatim}
  1478. array a(10),b(2,3,4);
  1479. \end{verbatim}
  1480. Array indices each range from 0 to the value declared. An element of an
  1481. array is referred to in standard FORTRAN notation, e.g. {\tt A(2)}.
  1482. We can also use an expression for defining an array bound, provided the
  1483. value of the expression is a positive integer. For example, if {\tt X} has the
  1484. value 10 and {\tt Y} the value 7 then
  1485. {\tt array c(5*x+y)} is the same as {\tt array c(57)}.
  1486. If an array is referenced by an index outside its range, an error occurs.
  1487. If the array is to be one-dimensional, and the bound a number or a variable
  1488. (not a more general expression) the parentheses may be omitted:
  1489. \begin{verbatim}
  1490. array a 10, c 57;
  1491. \end{verbatim}
  1492. The operator {\tt LENGTH}\ttindex{LENGTH} applied to an array name
  1493. returns a list of its dimensions.
  1494. All array elements are initialized to 0 at declaration time. In other words,
  1495. an array element has an {\em instant evaluation\/}\index{Instant evaluation}
  1496. property and cannot stand for itself. If this is required, then an
  1497. operator should be used instead.
  1498. Array declarations can appear anywhere in a program. Once a symbol is
  1499. declared to name an array, it can not also be used as a variable, or to
  1500. name an operator or a procedure. It can however be re-declared to be an
  1501. array, and its size may be changed at that time. An array name can also
  1502. continue to be used as a parameter in a procedure, or a local variable in
  1503. a compound statement, although this use is not recommended, since it can
  1504. lead to user confusion over the type of the variable.
  1505. Arrays once declared are global in scope, and so can then be referenced
  1506. anywhere in the program. In other words, unlike arrays in most other
  1507. languages, a declaration within a block (or a procedure) does not limit
  1508. the scope of the array to that block, nor does the array go away on
  1509. exiting the block (use {\tt CLEAR} instead for this purpose).
  1510. \section{Mode Handling Declarations}\index{Mode}
  1511. The {\tt ON}\ttindex{ON} and {\tt OFF}\ttindex{OFF} declarations are
  1512. available to the user for controlling various system options. Each option
  1513. is represented by a {\em switch\/}\index{Switch} name. {\tt ON} and {\tt OFF}
  1514. take a list of switch names as argument and turn them on and off
  1515. respectively, e.g.,
  1516. \begin{verbatim}
  1517. on time;
  1518. \end{verbatim}
  1519. causes the system to print a message after each command giving the elapsed
  1520. CPU time since the last command, or since {\tt TIME}\ttindex{TIME} was
  1521. last turned off, or the session began. Another useful switch with
  1522. interactive use is {\tt DEMO},\ttindex{DEMO} which causes the system to
  1523. pause after each command in a file (with the exception of comments)
  1524. until a \key{Return} is typed on the terminal. This
  1525. enables a user to set up a demonstration file and step through it command
  1526. by command.
  1527. As with most declarations, arguments to {\tt ON} and {\tt OFF} may be
  1528. strung together separated by commas. For example,
  1529. \begin{verbatim}
  1530. off time,demo;
  1531. \end{verbatim}
  1532. will turn off both the time messages and the demonstration switch.
  1533. We note here that while most {\tt ON} and {\tt OFF} commands are obeyed
  1534. almost instantaneously, some trigger time-consuming actions such as
  1535. reading in necessary modules from secondary storage.
  1536. A diagnostic message is printed if {\tt ON}\ttindex{ON} or {\tt OFF}
  1537. \ttindex{OFF} are used with a switch that is not known to the system. For
  1538. example, if you misspell {\tt DEMO} and type
  1539. \begin{verbatim}
  1540. on demq;
  1541. \end{verbatim}
  1542. you will get the message\index{Switch}
  1543. \begin{verbatim}
  1544. ***** DEMQ not defined as switch.
  1545. \end{verbatim}
  1546. \section{END}
  1547. The identifier {\tt END}\ttindex{END} has two separate uses.
  1548. 1) Its use in a {\tt BEGIN \ldots END} bracket has been discussed in
  1549. connection with compound statements.
  1550. 2) Files to be read using {\tt IN} should end with an extra {\tt END};
  1551. command. The reason for this is explained in the section on the {\tt IN}
  1552. command. This use of {\tt END} does not allow an immediately
  1553. preceding {\tt END} (such as the {\tt END} of a procedure definition), so
  1554. we advise using {\tt ;END;} there.
  1555. %3) A command {\tt END}; entered at the top level transfers control to the
  1556. %Lisp system\index{Lisp} which is the host of the {\REDUCE} system. All
  1557. %files opened by {\tt IN} or {\tt OUT} statements are closed in the
  1558. %process. {\tt END;} does not stop {\REDUCE}. Those familiar with Lisp can
  1559. %experiment with typing identifiers and ({\tt <function name> <argument
  1560. %list>}) lists to see the value returned by Lisp. (No terminators, other
  1561. %than the RETURN key, should be used.) The data structures created during
  1562. %the {\REDUCE} run are accessible.
  1563. %You remain in this Lisp mode until you explicitly re-enter {\REDUCE} by
  1564. %saying {\tt (BEGIN)} at the Lisp top level. In most systems, a Lisp error
  1565. %also returns you to {\REDUCE} (exceptions are noted in the operating
  1566. %instructions for your particular {\REDUCE} implementation). In either
  1567. %case, you will return to {\REDUCE} in the same mode, algebraic or
  1568. %symbolic, that you were in before the {\tt END};. If you are in
  1569. %Lisp mode\index{Lisp mode} by mistake -- which is usually the case,
  1570. %the result of typing more {\tt END}s\ttindex{END} than {\tt BEGIN}s --
  1571. %type {\tt (BEGIN)} in parentheses and hit the RETURN key.
  1572. \section{BYE Command}\ttindex{BYE}
  1573. The command {\tt BYE}; (or alternatively {\tt QUIT};)\ttindex{QUIT}
  1574. stops the execution
  1575. of {\REDUCE}, closes all open output files, and returns you to the calling
  1576. program (usually the operating system). Your {\REDUCE} session is
  1577. normally destroyed.
  1578. \section{SHOWTIME Command}\ttindex{SHOWTIME}
  1579. {\tt SHOWTIME}; prints the elapsed time since the last call of this
  1580. command or, on its first call, since the current {\REDUCE} session began.
  1581. The time is normally given in milliseconds and gives the time as measured
  1582. by a system clock. The operations covered by this measure are system
  1583. dependent.
  1584. \section{DEFINE Command}
  1585. The command {\tt DEFINE}\ttindex{DEFINE} allows a user to supply a new name for
  1586. any identifier or replace it by any well-formed expression. Its argument
  1587. is a list of expressions of the form
  1588. \begin{verbatim}
  1589. <identifier> = <number>|<identifier>|<operator>|
  1590. <reserved word>|<expression>
  1591. \end{verbatim}
  1592. {\it Example:}
  1593. \begin{verbatim}
  1594. define be==,x=y+z;
  1595. \end{verbatim}
  1596. means that {\tt BE} will be interpreted as an equal sign, and {\tt X}
  1597. as the expression {\tt y+z} from then on. This renaming is done at parse
  1598. time, and therefore takes precedence over any other replacement declared
  1599. for the same identifier. It stays in effect until the end of the
  1600. {\REDUCE} run.
  1601. The identifiers {\tt ALGEBRAIC} and {\tt SYMBOLIC} have properties which
  1602. prevent {\tt DEFINE}\ttindex{DEFINE} from being used on them. To define
  1603. {\tt ALG} to be a synonym for {\tt ALGEBRAIC}, use the more complicated
  1604. construction
  1605. \begin{verbatim}
  1606. put('alg,'newnam,'algebraic);
  1607. \end{verbatim}
  1608. \chapter{Built-in Prefix Operators}
  1609. In the following subsections are descriptions of the most useful prefix
  1610. \index{Prefix}
  1611. operators built into {\REDUCE} that are not defined in other sections (such
  1612. as substitution operators). Some are fully defined internally as
  1613. procedures; others are more nearly abstract operators, with only some of
  1614. their properties known to the system.
  1615. In many cases, an operator is described by a prototypical header line as
  1616. follows. Each formal parameter is given a name and followed by its allowed
  1617. type. The names of classes referred to in the definition are printed in
  1618. lower case, and parameter names in upper case. If a parameter type is not
  1619. commonly used, it may be a specific set enclosed in brackets {\tt \{} \ldots
  1620. {\tt \}}.
  1621. Operators that accept formal parameter lists of arbitrary length have the
  1622. parameter and type class enclosed in square brackets indicating that zero
  1623. or more occurrences of that argument are permitted. Optional parameters
  1624. and their type classes are enclosed in angle brackets.
  1625. \section{Numerical Operators}\index{Numerical operator}
  1626. {\REDUCE} includes a number of functions that are analogs of those found
  1627. in most numerical systems. With numerical arguments, such functions
  1628. return the expected result. However, they may also be called with
  1629. non-numerical arguments. In such cases, except where noted, the system
  1630. attempts to simplify the expression as far as it can. In such cases, a
  1631. residual expression involving the original operator usually remains.
  1632. These operators are as follows:
  1633. \subsection{ABS}
  1634. {\tt ABS}\ttindex{ABS} returns the absolute value
  1635. of its single argument, if that argument has a numerical value.
  1636. A non-numerical argument is returned as an absolute value, with an overall
  1637. numerical coefficient taken outside the absolute value operator. For example:
  1638. \begin{verbatim}
  1639. abs(-3/4) -> 3/4
  1640. abs(2a) -> 2*ABS(A)
  1641. abs(i) -> 1
  1642. abs(-x) -> ABS(X)
  1643. \end{verbatim}
  1644. \subsection{CEILING}\ttindex{CEILING}
  1645. This operator returns the ceiling (i.e., the least integer greater than
  1646. the given argument) if its single argument has a numerical value. A
  1647. non-numerical argument is returned as an expression in the original
  1648. operator. For example:
  1649. \begin{verbatim}
  1650. ceiling(-5/4) -> -1
  1651. ceiling(-a) -> CEILING(-A)
  1652. \end{verbatim}
  1653. \subsection{CONJ}\ttindex{CONJ}
  1654. This returns the complex conjugate
  1655. of an expression, if that argument has an numerical value. A
  1656. non-numerical argument is returned as an expression in the operators
  1657. {\tt REPART}\ttindex{REPART} and {\tt IMPART}\ttindex{IMPART}. For example:
  1658. \begin{verbatim}
  1659. conj(1+i) -> 1-I
  1660. conj(a+i*b) -> REPART(A) - REPART(B)*I - IMPART(A)*I
  1661. - IMPART(B)
  1662. \end{verbatim}
  1663. \subsection{FACTORIAL}\ttindex{FACTORIAL}
  1664. If the single argument of {\tt FACTORIAL} evaluates to a non-negative
  1665. integer, its factorial is returned. Otherwise an expression involving
  1666. {\tt FACTORIAL} is returned. For example:
  1667. \begin{verbatim}
  1668. factorial(5) -> 120
  1669. factorial(a) -> FACTORIAL(A)
  1670. \end{verbatim}
  1671. \subsection{FIX}\ttindex{FIX}
  1672. This operator returns the fixed value (i.e., the integer part of
  1673. the given argument) if its single argument has a numerical value. A
  1674. non-numerical argument is returned as an expression in the original
  1675. operator. For example:
  1676. \begin{verbatim}
  1677. fix(-5/4) -> -1
  1678. fix(a) -> FIX(A)
  1679. \end{verbatim}
  1680. \subsection{FLOOR}\ttindex{FLOOR}
  1681. This operator returns the floor (i.e., the greatest integer less than
  1682. the given argument) if its single argument has a numerical value. A
  1683. non-numerical argument is returned as an expression in the original
  1684. operator. For example:
  1685. \begin{verbatim}
  1686. floor(-5/4) -> -2
  1687. floor(a) -> FLOOR(A)
  1688. \end{verbatim}
  1689. \subsection{IMPART}\ttindex{IMPART}
  1690. This operator returns the imaginary part of an expression, if that argument
  1691. has an numerical value. A non-numerical argument is returned as an expression
  1692. in the operators {\tt REPART}\ttindex{REPART} and {\tt IMPART}. For example:
  1693. \begin{verbatim}
  1694. impart(1+i) -> 1
  1695. impart(a+i*b) -> REPART(B) + IMPART(A)
  1696. \end{verbatim}
  1697. \subsection{MAX/MIN}
  1698. {\tt MAX} and {\tt MIN}\ttindex{MAX}\ttindex{MIN} can take an arbitrary
  1699. number of expressions as their arguments. If all arguments evaluate to
  1700. numerical values, the maximum or minimum of the argument list is returned.
  1701. If any argument is non-numeric, an appropriately reduced expression is
  1702. returned. For example:
  1703. \begin{verbatim}
  1704. max(2,-3,4,5) -> 5
  1705. min(2,-2) -> -2.
  1706. max(a,2,3) -> MAX(A,3)
  1707. min(x) -> X
  1708. \end{verbatim}
  1709. {\tt MAX} or {\tt MIN} of an empty list returns 0.
  1710. \subsection{NEXTPRIME}\ttindex{NEXTPRIME}
  1711. {\tt NEXTPRIME} returns the next prime greater than its integer argument,
  1712. using a probabilistic algorithm. A type error occurs if the value of the
  1713. argument is not an integer. For example:
  1714. \begin{verbatim}
  1715. nextprime(5) -> 7
  1716. nextprime(-2) -> 2
  1717. nextprime(-7) -> -5
  1718. nextprime 1000000 -> 1000003
  1719. \end{verbatim}
  1720. whereas {\tt nextprime(a)} gives a type error.
  1721. \subsection{RANDOM}\ttindex{RANDOM}
  1722. {\tt random(}{\em n\/}{\tt)} returns a random number $r$ in the range $0
  1723. \leq r < n$. A type error occurs if the value of the argument is not a
  1724. positive integer in algebraic mode, or positive number in symbolic mode.
  1725. For example:
  1726. \begin{verbatim}
  1727. random(5) -> 3
  1728. random(1000) -> 191
  1729. \end{verbatim}
  1730. whereas {\tt random(a)} gives a type error.
  1731. \subsection{RANDOM\_NEW\_SEED}\ttindex{RANDOM\_NEW\_SEED}
  1732. {\tt random\_new\_seed(}{\em n\/}{\tt)} reseeds the random number generator
  1733. to a sequence determined by the integer argument $n$. It can be used to
  1734. ensure that a repeatable pseudo-random sequence will be delivered
  1735. regardless of any previous use of {\tt RANDOM}, or can be called early in
  1736. a run with an argument derived from something variable (such as the time
  1737. of day) to arrange that different runs of a REDUCE program will use
  1738. different random sequences. When a fresh copy of REDUCE is first created
  1739. it is as if {\tt random\_new\_seed(1)} has been obeyed.
  1740. A type error occurs if the value of the argument is not a positive integer.
  1741. \subsection{REPART}\ttindex{REPART}
  1742. This returns the real part of an expression, if that argument has an
  1743. numerical value. A non-numerical argument is returned as an expression in
  1744. the operators {\tt REPART} and {\tt IMPART}\ttindex{IMPART}. For example:
  1745. \begin{verbatim}
  1746. repart(1+i) -> 1
  1747. repart(a+i*b) -> REPART(A) - IMPART(B)
  1748. \end{verbatim}
  1749. \subsection{ROUND}\ttindex{ROUND}
  1750. This operator returns the rounded value (i.e, the nearest integer) of its
  1751. single argument if that argument has a numerical value. A non-numeric
  1752. argument is returned as an expression in the original operator. For
  1753. example:
  1754. \begin{verbatim}
  1755. round(-5/4) -> -1
  1756. round(a) -> ROUND(A)
  1757. \end{verbatim}
  1758. \subsection{SIGN}\ttindex{SIGN}
  1759. {\tt SIGN} tries to evaluate the sign of its argument. If this
  1760. is possible {\tt SIGN} returns one of 1, 0 or -1. Otherwise, the result
  1761. is the original form or a simplified variant. For example:
  1762. \begin{verbatim}
  1763. sign(-5) -> -1
  1764. sign(-a^2*b) -> -SIGN(B)
  1765. \end{verbatim}
  1766. Note that even powers of formal expressions are assumed to be
  1767. positive only as long as the switch {\tt COMPLEX} is off.
  1768. \section{Mathematical Functions}
  1769. {\REDUCE} knows that the following represent mathematical functions
  1770. \index{Mathematical function} that can
  1771. take arbitrary scalar expressions as their single argument:
  1772. \begin{verbatim}
  1773. ACOS ACOSH ACOT ACOTH ACSC ACSCH ASEC ASECH ASIN ASINH
  1774. ATAN ATANH ATAN2 COS COSH COT COTH CSC CSCH DILOG EI EXP
  1775. HYPOT LN LOG LOGB LOG10 SEC SECH SIN SINH SQRT TAN TANH
  1776. \end{verbatim}
  1777. \ttindex{ACOS}\ttindex{ACOSH}\ttindex{ACOT}
  1778. \ttindex{ACOTH}\ttindex{ACSC}\ttindex{ACSCH}\ttindex{ASEC}
  1779. \ttindex{ASECH}\ttindex{ASIN}
  1780. \ttindex{ASINH}\ttindex{ATAN}\ttindex{ATANH}
  1781. \ttindex{ATAN2}\ttindex{COS}
  1782. \ttindex{COSH}\ttindex{COT}\ttindex{COTH}\ttindex{CSC}
  1783. \ttindex{CSCH}\ttindex{DILOG}\ttindex{Ei}\ttindex{EXP}
  1784. \ttindex{HYPOT}\ttindex{LN}\ttindex{LOG}\ttindex{LOGB}\ttindex{LOG10}
  1785. \ttindex{SEC}\ttindex{SECH}\ttindex{SIN}
  1786. \ttindex{SINH}\ttindex{SQRT}\ttindex{TAN}\ttindex{TANH}
  1787. where {\tt LOG} is the natural logarithm (and equivalent to {\tt LN}),
  1788. and {\tt LOGB} has two arguments of which the second is the logarithmic base.
  1789. The derivatives of all these functions are also known to the system.
  1790. {\REDUCE} knows various elementary identities and properties
  1791. of these functions. For example:
  1792. \begin{verbatim}
  1793. cos(-x) = cos(x) sin(-x) = - sin (x)
  1794. cos(n*pi) = (-1)^n sin(n*pi) = 0
  1795. log(e) = 1 e^(i*pi/2) = i
  1796. log(1) = 0 e^(i*pi) = -1
  1797. log(e^x) = x e^(3*i*pi/2) = -i
  1798. \end{verbatim}
  1799. Beside these identities, there are a lot of simplifications
  1800. for elementary functions
  1801. defined in the {\REDUCE} system as rulelists. In order to
  1802. view these, the SHOWRULES operator can be used, e.g.
  1803. \begin{verbatim}
  1804. SHOWRULES tan;
  1805. {tan(~n*arbint(~i)*pi + ~(~ x)) => tan(x) when fixp(n),
  1806. tan(~x)
  1807. => trigquot(sin(x),cos(x)) when knowledge_about(sin,x,tan)
  1808. ,
  1809. ~x + ~(~ k)*pi
  1810. tan(----------------)
  1811. ~d
  1812. x k 1
  1813. => - cot(---) when x freeof pi and abs(---)=---,
  1814. d d 2
  1815. ~(~ w) + ~(~ k)*pi w + remainder(k,d)*pi
  1816. tan(--------------------) => tan(-----------------------)
  1817. ~(~ d) d
  1818. k
  1819. when w freeof pi and ratnump(---) and fixp(k)
  1820. d
  1821. k
  1822. and abs(---)>=1,
  1823. d
  1824. tan(atan(~x)) => x,
  1825. 2
  1826. df(tan(~x),~x) => 1 + tan(x) }
  1827. \end{verbatim}
  1828. For further simplification, especially of expressions involving
  1829. trigonometric functions, see the TRIGSIMP\ttindex{TRIGSIMP} package
  1830. documentation.
  1831. Functions not listed above may be defined in the special functions
  1832. package SPECFN\ttindex{SPECFN}.
  1833. The user can add further rules for the reduction of expressions involving
  1834. these operators by using the {\tt LET}\ttindex{LET} command.
  1835. % The square root function can be input using the name {\tt SQRT}, or the
  1836. % power operation {\tt \verb|^|(1/2)}. On output, unsimplified square roots
  1837. % are normally represented by the operator {\tt SQRT} rather than a
  1838. % fractional power.
  1839. In many cases it is desirable to expand product arguments of logarithms,
  1840. or collect a sum of logarithms into a single logarithm. Since these are
  1841. inverse operations, it is not possible to provide rules for doing both at
  1842. the same time and preserve the {\REDUCE} concept of idempotent evaluation.
  1843. As an alternative, REDUCE provides two switches {\tt EXPANDLOGS}
  1844. \ttindex{EXPANDLOGS} and {\tt COMBINELOGS}\ttindex{COMBINELOGS} to carry
  1845. out these operations. Both are off by default. Thus to expand {\tt
  1846. LOG(X*Y)} into a sum of logs, one can say
  1847. \begin{verbatim}
  1848. ON EXPANDLOGS; LOG(X*Y);
  1849. \end{verbatim}
  1850. and to combine this sum into a single log:
  1851. \begin{verbatim}
  1852. ON COMBINELOGS; LOG(X) + LOG(Y);
  1853. \end{verbatim}
  1854. At the present time, it is possible to have both switches on at once,
  1855. which could lead to infinite recursion. However, an expression is
  1856. switched from one form to the other in this case. Users should not rely
  1857. on this behavior, since it may change in the next release.
  1858. The current version of {\REDUCE} does a poor job of simplifying surds. In
  1859. particular, expressions involving the product of variables raised to
  1860. non-integer powers do not usually have their powers combined internally,
  1861. even though they are printed as if those powers were combined. For
  1862. example, the expression
  1863. \begin{verbatim}
  1864. x^(1/3)*x^(1/6);
  1865. \end{verbatim}
  1866. will print as
  1867. \begin{verbatim}
  1868. SQRT(X)
  1869. \end{verbatim}
  1870. but will have an internal form containing the two exponentiated terms.
  1871. If you now subtract {\tt sqrt(x)} from this expression, you will {\em not\/}
  1872. get zero. Instead, the confusing form
  1873. \begin{verbatim}
  1874. SQRT(X) - SQRT(X)
  1875. \end{verbatim}
  1876. will result. To combine such exponentiated terms, the switch
  1877. {\tt COMBINEEXPT}\ttindex{COMBINEEXPT} should be turned on.
  1878. The square root function can be input using the name {\tt SQRT}, or the
  1879. power operation {\tt \verb|^|(1/2)}. On output, unsimplified square roots
  1880. are normally represented by the operator {\tt SQRT} rather than a
  1881. fractional power. With the default system switch settings, the argument
  1882. of a square root is first simplified, and any divisors of the expression
  1883. that are perfect squares taken outside the square root argument. The
  1884. remaining expression is left under the square root.
  1885. % However, if the switch {\tt REDUCED}\ttindex{REDUCED} is on,
  1886. % multiplicative factors in the argument of the square root are also
  1887. % separated, becoming individual square roots. Thus with {\tt REDUCED} off,
  1888. Thus the expression
  1889. \begin{verbatim}
  1890. sqrt(-8a^2*b)
  1891. \end{verbatim}
  1892. becomes
  1893. \begin{verbatim}
  1894. 2*a*sqrt(-2*b).
  1895. \end{verbatim}
  1896. % whereas with {\tt REDUCED} on, it would become
  1897. % \begin{verbatim}
  1898. % 2*a*i*sqrt(2)*sqrt(b) .
  1899. % \end{verbatim}
  1900. % The switch {\tt REDUCED}\ttindex{REDUCED} also applies to other rational
  1901. % powers in addition to square roots.
  1902. Note that such simplifications can cause trouble if {\tt A} is eventually
  1903. given a value that is a negative number. If it is important that the
  1904. positive property of the square root and higher even roots always be
  1905. preserved, the switch {\tt PRECISE}\ttindex{PRECISE} should be set on
  1906. (the default value).
  1907. This causes any non-numerical factors taken out of surds to be represented
  1908. by their absolute value form.
  1909. With % both {\tt REDUCED} and
  1910. {\tt PRECISE} on then, the above example would become
  1911. \begin{verbatim}
  1912. 2*abs(a)*sqrt(-2*b).
  1913. \end{verbatim}
  1914. The statement that {\REDUCE} knows very little about these functions
  1915. applies only in the mathematically exact {\tt off rounded} mode. If
  1916. {\tt ROUNDED}\ttindex{ROUNDED} is on, any of the functions
  1917. \begin{verbatim}
  1918. ACOS ACOSH ACOT ACOTH ACSC ACSCH ASEC ASECH ASIN ASINH
  1919. ATAN ATANH ATAN2 COS COSH COT COTH CSC CSCH EXP HYPOT
  1920. LN LOG LOGB LOG10 SEC SECH SIN SINH SQRT TAN TANH
  1921. \end{verbatim}
  1922. \ttindex{ACOS}\ttindex{ACOSH}\ttindex{ACOT}\ttindex{ACOTH}
  1923. \ttindex{ACSC}\ttindex{ACSCH}\ttindex{ASEC}\ttindex{ASECH}
  1924. \ttindex{ASIN}\ttindex{ASINH}\ttindex{ATAN}\ttindex{ATANH}
  1925. \ttindex{ATAN2}\ttindex{COS}\ttindex{COSH}\ttindex{COT}
  1926. \ttindex{COTH}\ttindex{CSC}\ttindex{CSCH}\ttindex{EXP}\ttindex{HYPOT}
  1927. \ttindex{LN}\ttindex{LOG}\ttindex{LOGB}\ttindex{LOG10}\ttindex{SEC}
  1928. \ttindex{SECH}\ttindex{SIN}\ttindex{SINH}\ttindex{SQRT}\ttindex{TAN}
  1929. \ttindex{TANH}
  1930. when given a numerical argument has its value calculated to the current
  1931. degree of floating point precision. In addition, real (non-integer
  1932. valued) powers of numbers will also be evaluated.
  1933. If the {\tt COMPLEX} switch is turned on in addition to {\tt ROUNDED},
  1934. these functions will also calculate a real or complex result, again to
  1935. the current degree of floating point precision,
  1936. if given complex arguments. For example, with {\tt on rounded,complex;}
  1937. \begin{verbatim}
  1938. 2.3^(5.6i) -> -0.0480793490914 - 0.998843519372*I
  1939. cos(2+3i) -> -4.18962569097 - 9.10922789376*I
  1940. \end{verbatim}
  1941. \section{DF Operator}
  1942. The operator {\tt DF}\ttindex{DF} is used to represent partial
  1943. differentiation\index{Differentiation} with respect
  1944. to one or more variables. It is used with the syntax:
  1945. \begin{verbatim}
  1946. DF(EXPRN:algebraic[,VAR:kernel<,NUM:integer>]):algebraic.
  1947. \end{verbatim}
  1948. The first argument is the expression to be differentiated. The remaining
  1949. arguments specify the differentiation variables and the number of times
  1950. they are applied.
  1951. The number {\tt NUM} may be omitted if it is 1. For example,
  1952. \begin{quote}
  1953. \begin{tabbing}
  1954. {\tt df(y,x1,2,x2,x3,2)} \= = $\partial^{5}y/\partial x_{1}^{2} \
  1955. \partial x_{2}\partial x_{3}^{2}.$\kill
  1956. {\tt df(y,x)} \> = $\partial y/\partial x$ \\
  1957. {\tt df(y,x,2)} \> = $\partial^{2}y/\partial x^{2}$ \\
  1958. {\tt df(y,x1,2,x2,x3,2)} \> = $\partial^{5}y/\partial x_{1}^{2} \
  1959. \partial x_{2}\partial x_{3}^{2}.$
  1960. \end{tabbing}
  1961. \end{quote}
  1962. The evaluation of {\tt df(y,x)} proceeds as follows: first, the values of
  1963. {\tt Y} and {\tt X} are found. Let us assume that {\tt X} has no assigned
  1964. value, so its value is {\tt X}. Each term or other part of the value of
  1965. {\tt Y} that contains the variable {\tt X} is differentiated by the
  1966. standard rules. If {\tt Z} is another variable, not {\tt X} itself, then
  1967. its derivative with respect to {\tt X} is taken to be 0, unless {\tt Z}
  1968. has previously been declared to {\tt DEPEND} on {\tt X}, in which
  1969. case the derivative is reported as the symbol {\tt df(z,x)}.
  1970. \subsection{Adding Differentiation Rules}
  1971. The {\tt LET}\ttindex{LET} statement can be used to introduce
  1972. rules for differentiation of user-defined operators. Its general form is
  1973. \begin{verbatim}
  1974. FOR ALL <var1>,...,<varn>
  1975. LET DF(<operator><varlist>,<vari>)=<expression>
  1976. \end{verbatim}
  1977. where {\tt <varlist>} ::= ({\tt <var1>},\dots,{\tt <varn>}), and
  1978. {\tt <var1>},...,{\tt <varn>} are the dummy variable arguments of
  1979. {\tt <operator>}.
  1980. An analogous form applies to infix operators.
  1981. {\it Examples:}
  1982. \begin{verbatim}
  1983. for all x let df(tan x,x)= 1 + tan(x)^2;
  1984. \end{verbatim}
  1985. (This is how the tan differentiation rule appears in the {\REDUCE}
  1986. source.)
  1987. \begin{verbatim}
  1988. for all x,y let df(f(x,y),x)=2*f(x,y),
  1989. df(f(x,y),y)=x*f(x,y);
  1990. \end{verbatim}
  1991. Notice that all dummy arguments of the relevant operator must be declared
  1992. arbitrary by the {\tt FOR ALL} command, and that rules may be supplied for
  1993. operators with any number of arguments. If no differentiation rule
  1994. appears for an argument in an operator, the differentiation routines will
  1995. return as result an expression in terms of {\tt DF}\ttindex{DF}. For
  1996. example, if the rule for the differentiation with respect to the second
  1997. argument of {\tt F} is not supplied, the evaluation of {\tt df(f(x,z),z)}
  1998. would leave this expression unchanged. (No {\tt DEPEND} declaration
  1999. is needed here, since {\tt f(x,z)} obviously ``depends on'' {\tt Z}.)
  2000. Once such a rule has been defined for a given operator, any future
  2001. differentiation\index{Differentiation} rules for that operator must be
  2002. defined with the same number of arguments for that operator, otherwise we
  2003. get the error message
  2004. \begin{verbatim}
  2005. Incompatible DF rule argument length for <operator>
  2006. \end{verbatim}
  2007. \section{INT Operator}
  2008. {\tt INT}\ttindex{INT} is an operator in {\REDUCE} for indefinite
  2009. integration\index{Integration}\index{Indefinite integration} using a
  2010. combination of the Risch-Norman algorithm and pattern matching. It is
  2011. used with the syntax:
  2012. \begin{verbatim}
  2013. INT(EXPRN:algebraic,VAR:kernel):algebraic.
  2014. \end{verbatim}
  2015. This will return correctly the indefinite integral for expressions comprising
  2016. polynomials, log functions, exponential functions and tan and atan. The
  2017. arbitrary constant is not represented. If the integral cannot be done in
  2018. closed terms, it returns a formal integral for the answer in one of two ways:
  2019. \begin{enumerate}
  2020. \item It returns the input, {\tt INT(\ldots,\ldots)} unchanged.
  2021. \item It returns an expression involving {\tt INT}s of some
  2022. other functions (sometimes more complicated than
  2023. the original one, unfortunately).
  2024. \end{enumerate}
  2025. Rational functions can be integrated when the denominator is factorizable
  2026. by the program. In addition it will attempt to integrate expressions
  2027. involving error functions, dilogarithms and other trigonometric
  2028. expressions. In these cases it might not always succeed in finding the
  2029. solution, even if one exists.
  2030. {\it Examples:}
  2031. \begin{verbatim}
  2032. int(log(x),x) -> X*(LOG(X) - 1),
  2033. int(e^x,x) -> E**X.
  2034. \end{verbatim}
  2035. The program checks that the second argument is a variable and gives an
  2036. error if it is not.
  2037. {\it Note:} If the {\tt int} operator is called with 4 arguments,
  2038. {\REDUCE} will implicitly call the definite integration package (DEFINT)
  2039. and this package will interpret the third and fourth arguments as the lower
  2040. and upper limit of integration, respectively. For details, consult
  2041. the documentation on the DEFINT package.
  2042. \subsection{Options}
  2043. The switch {\tt TRINT} when on will trace the operation of the algorithm. It
  2044. produces a great deal of output in a somewhat illegible form, and is not
  2045. of much interest to the general user. It is normally off.
  2046. If the switch {\tt FAILHARD} is on the algorithm will terminate with an
  2047. error if the integral cannot be done in closed terms, rather than return a
  2048. formal integration form. {\tt FAILHARD} is normally off.
  2049. The switch {\tt NOLNR} suppresses the use of the linear properties of
  2050. integration in cases when the integral cannot be found in closed terms.
  2051. It is normally off.
  2052. \subsection{Advanced Use}
  2053. If a function appears in the integrand that is not one of the functions
  2054. {\tt EXP, ERF, TAN, ATAN, LOG, DILOG}\ttindex{EXP}\ttindex{ERF}
  2055. \ttindex{TAN}\ttindex{ATAN}\ttindex{LOG}\ttindex{DILOG}
  2056. then the algorithm will make an
  2057. attempt to integrate the argument if it can, differentiate it and reach a
  2058. known function. However the answer cannot be guaranteed in this case. If
  2059. a function is known to be algebraically independent of this set it can be
  2060. flagged transcendental by
  2061. \begin{verbatim}
  2062. flag('(trilog),'transcendental);
  2063. \end{verbatim}
  2064. in which case this function will be added to the permitted field
  2065. descriptors for a genuine decision procedure. If this is done the user is
  2066. responsible for the mathematical correctness of his actions.
  2067. The standard version does not deal with algebraic extensions. Thus
  2068. integration of expressions involving square roots and other like things
  2069. can lead to trouble. A contributed package that supports integration of
  2070. functions involving square roots is available, however
  2071. (ALGINT\extendedmanual{, chapter~\ref{ALGINT}}).
  2072. In addition there is a definite integration
  2073. package, DEFINT\extendedmanual{( chapter~\ref{DEFINT})}.
  2074. \subsection{References}
  2075. A. C. Norman \& P. M. A. Moore, ``Implementing the New Risch
  2076. Algorithm'', Proc. 4th International Symposium on Advanced
  2077. Comp. Methods in Theor. Phys., CNRS, Marseilles, 1977.
  2078. S. J. Harrington, ``A New Symbolic Integration System in Reduce'',
  2079. Comp. Journ. 22 (1979) 2.
  2080. A. C. Norman \& J. H. Davenport, ``Symbolic Integration --- The Dust
  2081. Settles?'', Proc. EUROSAM 79, Lecture Notes in Computer
  2082. Science 72, Springer-Verlag, Berlin Heidelberg New York
  2083. (1979) 398-407.
  2084. %\subsection{Definite Integration} \index{Definite integration}
  2085. %
  2086. %If {\tt INT} is used with the syntax
  2087. %
  2088. %\begin{verbatim}
  2089. % INT(EXPRN:algebraic,VAR:kernel,LOWER:algebraic,UPPER:algebraic):algebraic.
  2090. %\end{verbatim}
  2091. %
  2092. %The definite integral of {\tt EXPRN} with respect to {\tt VAR} is
  2093. %calculated between the limits {\tt LOWER} and {\tt UPPER}. In the present
  2094. %system, this is calculated either by pattern matching, or by first finding
  2095. %the indefinite integral, and then substituting the limits into this.
  2096. \section{LENGTH Operator}
  2097. {\tt LENGTH}\ttindex{LENGTH} is a generic operator for finding the
  2098. length of various objects in the system. The meaning depends on the type
  2099. of the object. In particular, the length of an algebraic expression is
  2100. the number of additive top-level terms its expanded representation.
  2101. {\it Examples:}
  2102. \begin{verbatim}
  2103. length(a+b) -> 2
  2104. length(2) -> 1.
  2105. \end{verbatim}
  2106. Other objects that support a length operator include arrays, lists and
  2107. matrices. The explicit meaning in these cases is included in the description
  2108. of these objects.
  2109. \section{MAP Operator}\ttindex{MAP}
  2110. The {\tt MAP} operator applies a uniform evaluation pattern to all members
  2111. of a composite structure: a matrix, a list, or the arguments of an
  2112. operator expression. The evaluation pattern can be a unary procedure, an
  2113. operator, or an algebraic expression with one free variable.
  2114. It is used with the syntax:
  2115. \begin{verbatim}
  2116. MAP(U:function,V:object)
  2117. \end{verbatim}
  2118. Here {\tt object} is a list, a matrix or an operator expression.
  2119. {\tt Function} can be one of the following:
  2120. \begin{enumerate}
  2121. \item the name of an operator for a single argument: the operator
  2122. is evaluated once with each element of {\tt object} as its single argument;
  2123. \item an algebraic expression with exactly one free variable, that is
  2124. a variable preceded by the tilde symbol. The expression
  2125. is evaluated for each element of {\tt object}, where the element is
  2126. substituted for the free variable;
  2127. \item a replacement rule of the form {\tt var => rep}
  2128. where {\tt var} is a variable (a kernel without a subscript)
  2129. and {\tt rep} is an expression that contains {\tt var}.
  2130. {\tt Rep} is evaluated for each element of {\tt object} where
  2131. the element is substituted for {\tt var}. {\tt Var} may be
  2132. optionally preceded by a tilde.
  2133. \end{enumerate}
  2134. The rule form for {\tt function} is needed when more than
  2135. one free variable occurs.
  2136. Examples:
  2137. \begin{verbatim}
  2138. map(abs,{1,-2,a,-a}) -> {1,2,ABS(A),ABS(A)}
  2139. map(int(~w,x), mat((x^2,x^5),(x^4,x^5))) ->
  2140. [ 3 6 ]
  2141. [ x x ]
  2142. [---- ----]
  2143. [ 3 6 ]
  2144. [ ]
  2145. [ 5 6 ]
  2146. [ x x ]
  2147. [---- ----]
  2148. [ 5 6 ]
  2149. map(~w*6, x^2/3 = y^3/2 -1) -> 2*X^2=3*(Y^3-2)
  2150. \end{verbatim}
  2151. You can use {\tt MAP} in nested expressions. However, you cannot
  2152. apply {\tt MAP} to a non-composed object, e.g. an identifier or a number.
  2153. \section{MKID Operator}\ttindex{MKID}
  2154. In many applications, it is useful to create a set of identifiers for
  2155. naming objects in a consistent manner. In most cases, it is sufficient to
  2156. create such names from two components. The operator {\tt MKID} is provided
  2157. for this purpose. Its syntax is:
  2158. \begin{verbatim}
  2159. MKID(U:id,V:id|non-negative integer):id
  2160. \end{verbatim}
  2161. for example
  2162. \begin{verbatim}
  2163. mkid(a,3) -> A3
  2164. mkid(apple,s) -> APPLES
  2165. \end{verbatim}
  2166. while {\tt mkid(a+b,2)} gives an error.
  2167. The {\tt SET}\ttindex{SET} operator can be used to give a value to the
  2168. identifiers created by {\tt MKID}, for example
  2169. \begin{verbatim}
  2170. set(mkid(a,3),3);
  2171. \end{verbatim}
  2172. will give {\tt A3} the value 2.
  2173. \section{PF Operator}\ttindex{PF}
  2174. {\tt PF(<exp>,<var>)} transforms the expression {\tt <exp>} into a list of
  2175. partial fractions with respect to the main variable, {\tt <var>}. {\tt PF}
  2176. does a complete partial fraction decomposition, and as the algorithms used
  2177. are fairly unsophisticated (factorization and the extended Euclidean
  2178. algorithm), the code may be unacceptably slow in complicated cases.
  2179. {\it Example:}
  2180. Given {\tt 2/((x+1)\verb|^|2*(x+2))} in the workspace,
  2181. {\tt pf(ws,x);} gives the result
  2182. \begin{verbatim}
  2183. 2 - 2 2
  2184. {-------,-------,--------------} .
  2185. X + 2 X + 1 2
  2186. X + 2*X + 1
  2187. \end{verbatim}
  2188. If you want the denominators in factored form, use {\tt off exp;}.
  2189. Thus, with {\tt 2/((x+1)\verb|^|2*(x+2))} in the workspace, the commands
  2190. {\tt off exp; pf(ws,x);} give the result
  2191. \begin{verbatim}
  2192. 2 - 2 2
  2193. {-------,-------,----------} .
  2194. X + 2 X + 1 2
  2195. (X + 1)
  2196. \end{verbatim}
  2197. To recombine the terms, {\tt FOR EACH \ldots SUM} can be used. So with
  2198. the above list in the workspace, {\tt for each j in ws sum j;} returns the
  2199. result
  2200. \begin{verbatim}
  2201. 2
  2202. ------------------
  2203. 2
  2204. (X + 2)*(X + 1)
  2205. \end{verbatim}
  2206. Alternatively, one can use the operations on lists to extract any desired
  2207. term.
  2208. \section{SELECT Operator}\ttindex{SELECT}
  2209. \ttindex{map}\ttindex{list}
  2210. The {\tt SELECT} operator extracts from a list,
  2211. or from the arguments of an n--ary operator, elements corresponding
  2212. to a boolean predicate. It is used with the syntax:
  2213. \begin{verbatim}
  2214. SELECT(U:function,V:list)
  2215. \end{verbatim}
  2216. {\tt Function} can be one of the following forms:
  2217. \begin{enumerate}
  2218. \item the name of an operator for a single argument: the operator
  2219. is evaluated once with each element of {\tt object} as its single argument;
  2220. \item an algebraic expression with exactly one free variable, that is
  2221. a variable preceded by the tilde symbol. The expression
  2222. is evaluated for each element of \meta{object}, where the element is
  2223. substituted for the free variable;
  2224. \item a replacement rule of the form \meta{var $=>$ rep}
  2225. where {\tt var} is a variable (a kernel without subscript)
  2226. and {\tt rep} is an expression that contains {\tt var}.
  2227. {\tt Rep} is evaluated for each element of {\tt object} where
  2228. the element is substituted for {\tt var}. {\tt var} may be
  2229. optionally preceded by a tilde.
  2230. \end{enumerate}
  2231. The rule form for {\tt function} is needed when more than
  2232. one free variable occurs.
  2233. The result of evaluating {\tt function} is
  2234. interpreted as a boolean value corresponding to the conventions of
  2235. {\REDUCE}. These values are composed with the leading operator of the
  2236. input expression.
  2237. {\it Examples:}
  2238. \begin{verbatim}
  2239. select( ~w>0 , {1,-1,2,-3,3}) -> {1,2,3}
  2240. select(evenp deg(~w,y),part((x+y)^5,0):=list)
  2241. -> {X^5 ,10*X^3*Y^2 ,5*X*Y^4}
  2242. select(evenp deg(~w,x),2x^2+3x^3+4x^4) -> 4X^4 + 2X^2
  2243. \end{verbatim}
  2244. \section{SOLVE Operator}\ttindex{SOLVE}
  2245. SOLVE is an operator for solving one or more simultaneous algebraic
  2246. equations. It is used with the syntax:
  2247. \begin{verbatim}
  2248. SOLVE(EXPRN:algebraic[,VAR:kernel|,VARLIST:list of kernels])
  2249. :list.
  2250. \end{verbatim}
  2251. {\tt EXPRN} is of the form {\tt <expression>} or
  2252. \{ {\tt <expression1>},{\tt <expression2>}, \dots \}. Each expression is an
  2253. algebraic equation, or is the difference of the two sides of the equation.
  2254. The second argument is either a kernel or a list of kernels representing
  2255. the unknowns in the system. This argument may be omitted if the number of
  2256. distinct, non-constant, top-level kernels equals the number of unknowns,
  2257. in which case these kernels are presumed to be the unknowns.
  2258. For one equation, {\tt SOLVE}\ttindex{SOLVE} recursively uses
  2259. factorization and decomposition, together with the known inverses of
  2260. {\tt LOG}, {\tt SIN}, {\tt COS}, {\tt \verb|^|}, {\tt ACOS}, {\tt ASIN}, and
  2261. linear, quadratic, cubic, quartic, or binomial factors. Solutions
  2262. of equations built with exponentials or logarithms are often
  2263. expressed in terms of Lambert's {\tt W} function.\index{Lambert's W}
  2264. This function is (partially) implemented in the special functions package.
  2265. Linear equations are solved by the multi-step elimination method due to
  2266. Bareiss, unless the switch {\tt CRAMER}\ttindex{CRAMER} is on, in which
  2267. case Cramer's method is used. The Bareiss method is usually more
  2268. efficient unless the system is large and dense.
  2269. Non-linear equations are solved using the Groebner basis package.
  2270. \index{Groebner} Users should note that this can be quite a
  2271. time consuming process.
  2272. {\it Examples:}
  2273. \begin{verbatim}
  2274. solve(log(sin(x+3))^5 = 8,x);
  2275. solve(a*log(sin(x+3))^5 - b, sin(x+3));
  2276. solve({a*x+y=3,y=-2},{x,y});
  2277. \end{verbatim}
  2278. {\tt SOLVE} returns a list of solutions. If there is one unknown, each
  2279. solution is an equation for the unknown. If a complete solution was
  2280. found, the unknown will appear by itself on the left-hand side of the
  2281. equation. On the other hand, if the solve package could not find a
  2282. solution, the ``solution'' will be an equation for the unknown in terms
  2283. of the operator {\tt ROOT\_OF}\ttindex{ROOT\_OF}. If there
  2284. are several unknowns, each solution will be a list of equations for the
  2285. unknowns. For example,
  2286. \begin{verbatim}
  2287. solve(x^2=1,x); -> {X=-1,X=1}
  2288. solve(x^7-x^6+x^2=1,x)
  2289. 6
  2290. -> {X=ROOT_OF(X_ + X_ + 1,X_,TAG_1),X=1}
  2291. solve({x+3y=7,y-x=1},{x,y}) -> {{X=1,Y=2}}.
  2292. \end{verbatim}
  2293. The TAG argument is used to uniquely identify those particular solutions.
  2294. Solution multiplicities are stored in the global variable {\tt
  2295. ROOT\_MULTIPLICITIES} rather than the solution list. The value of this
  2296. variable is a list of the multiplicities of the solutions for the last
  2297. call of {\tt SOLVE}. \ttindex{SOLVE} For example,
  2298. \begin{verbatim}
  2299. solve(x^2=2x-1,x); root_multiplicities;
  2300. \end{verbatim}
  2301. gives the results
  2302. \begin{verbatim}
  2303. {X=1}
  2304. {2}
  2305. \end{verbatim}
  2306. If you want the multiplicities explicitly displayed, the switch
  2307. {\tt MULTIPLICITIES}\ttindex{MULTIPLICITIES} can be turned on. For example
  2308. \begin{verbatim}
  2309. on multiplicities; solve(x^2=2x-1,x);
  2310. \end{verbatim}
  2311. yields the result
  2312. \begin{verbatim}
  2313. {X=1,X=1}
  2314. \end{verbatim}
  2315. \subsection{Handling of Undetermined Solutions}
  2316. When {\tt SOLVE} cannot find a solution to an equation, it normally
  2317. returns an equation for the relevant indeterminates in terms of the
  2318. operator {\tt ROOT\_OF}.\ttindex{ROOT\_OF} For example, the expression
  2319. \begin{verbatim}
  2320. solve(cos(x) + log(x),x);
  2321. \end{verbatim}
  2322. returns the result
  2323. \begin{verbatim}
  2324. {X=ROOT_OF(COS(X_) + LOG(X_),X_,TAG_1)} .
  2325. \end{verbatim}
  2326. An expression with a top-level {\tt ROOT\_OF} operator is implicitly a
  2327. list with an unknown number of elements (since we don't always know how
  2328. many solutions an equation has). If a substitution is made into such an
  2329. expression, closed form solutions can emerge. If this occurs, the {\tt
  2330. ROOT\_OF} construct is replaced by an operator {\tt ONE\_OF}.\ttindex{ONE\_OF}
  2331. At this point it is of course possible to transform the result of the
  2332. original {\tt SOLVE} operator expression into a standard {\tt SOLVE}
  2333. solution. To effect this, the operator {\tt EXPAND\_CASES}
  2334. \ttindex{EXPAND\_CASES} can be used.
  2335. The following example shows the use of these facilities:
  2336. \extendedmanual{\newpage}
  2337. \begin{verbatim}
  2338. solve(-a*x^3+a*x^2+x^4-x^3-4*x^2+4,x);
  2339. 2 3
  2340. {X=ROOT_OF(A*X_ - X_ + 4*X_ + 4,X_,TAG_2),X=1}
  2341. sub(a=-1,ws);
  2342. {X=ONE_OF({2,-1,-2},TAG_2),X=1}
  2343. expand_cases ws;
  2344. {X=2,X=-1,X=-2,X=1}
  2345. \end{verbatim}
  2346. \subsection{Solutions of Equations Involving Cubics and Quartics}
  2347. Since roots of cubics and quartics can often be very messy, a switch
  2348. {\tt FULLROOTS}\ttindex{FULLROOTS} is available, that, when off (the
  2349. default), will prevent the production of a result in closed form. The
  2350. {\tt ROOT\_OF} construct will be used in this case instead.
  2351. In constructing the solutions of cubics and quartics, trigonometrical
  2352. forms are used where appropriate. This option is under the control of a
  2353. switch {\tt TRIGFORM},\ttindex{TRIGFORM} which is normally on.
  2354. The following example illustrates the use of these facilities:
  2355. \begin{verbatim}
  2356. let xx = solve(x^3+x+1,x);
  2357. xx;
  2358. 3
  2359. {X=ROOT_OF(X_ + X_ + 1,X_)}
  2360. on fullroots;
  2361. xx;
  2362. - SQRT(31)*I
  2363. ATAN(---------------)
  2364. 3*SQRT(3)
  2365. {X=(I*(SQRT(3)*SIN(-----------------------)
  2366. 3
  2367. \end{verbatim}
  2368. \newpage
  2369. \begin{verbatim}
  2370. - SQRT(31)*I
  2371. ATAN(---------------)
  2372. 3*SQRT(3)
  2373. - COS(-----------------------)))/SQRT(3),
  2374. 3
  2375. - SQRT(31)*I
  2376. ATAN(---------------)
  2377. 3*SQRT(3)
  2378. X=( - I*(SQRT(3)*SIN(-----------------------)
  2379. 3
  2380. - SQRT(31)*I
  2381. ATAN(---------------)
  2382. 3*SQRT(3)
  2383. + COS(-----------------------)))/SQRT(
  2384. 3
  2385. 3),
  2386. - SQRT(31)*I
  2387. ATAN(---------------)
  2388. 3*SQRT(3)
  2389. 2*COS(-----------------------)*I
  2390. 3
  2391. X=----------------------------------}
  2392. SQRT(3)
  2393. off trigform;
  2394. xx;
  2395. 2/3
  2396. {X=( - (SQRT(31) - 3*SQRT(3)) *SQRT(3)*I
  2397. 2/3 2/3
  2398. - (SQRT(31) - 3*SQRT(3)) - 2 *SQRT(3)*I
  2399. 2/3 1/3 1/3
  2400. + 2 )/(2*(SQRT(31) - 3*SQRT(3)) *6
  2401. 1/6
  2402. *3 ),
  2403. 2/3
  2404. X=((SQRT(31) - 3*SQRT(3)) *SQRT(3)*I
  2405. 2/3 2/3
  2406. - (SQRT(31) - 3*SQRT(3)) + 2 *SQRT(3)*I
  2407. 2/3 1/3 1/3
  2408. + 2 )/(2*(SQRT(31) - 3*SQRT(3)) *6
  2409. 1/6
  2410. *3 ),
  2411. 2/3 2/3
  2412. (SQRT(31) - 3*SQRT(3)) - 2
  2413. X=-------------------------------------}
  2414. 1/3 1/3 1/6
  2415. (SQRT(31) - 3*SQRT(3)) *6 *3
  2416. \end{verbatim}
  2417. \subsection{Other Options}
  2418. If {\tt SOLVESINGULAR}\ttindex{SOLVESINGULAR} is on (the default setting),
  2419. degenerate systems such as {\tt x+y=0}, {\tt 2x+2y=0} will be solved by
  2420. introducing appropriate arbitrary constants.
  2421. The consistent singular equation 0=0 or equations involving functions with
  2422. multiple inverses may introduce unique new indeterminant kernels
  2423. {\tt ARBCOMPLEX(j)}, or {\tt ARBINT(j)}, ($j$=1,2,...), % {\tt ARBREAL(j)},
  2424. representing arbitrary complex or integer numbers respectively. To
  2425. automatically select the principal branches, do {\tt off allbranch;} .
  2426. \ttindex{ALLBRANCH} To avoid the introduction of new indeterminant kernels
  2427. do {\tt OFF ARBVARS}\ttindex{ARBVARS} -- then no equations are generated for the free
  2428. variables and their original names are used to express the solution forms.
  2429. To suppress solutions of consistent singular equations do
  2430. {\tt OFF SOLVESINGULAR}.
  2431. To incorporate additional inverse functions do, for example:
  2432. \begin{verbatim}
  2433. put('sinh,'inverse,'asinh);
  2434. put('asinh,'inverse,'sinh);
  2435. \end{verbatim}
  2436. together with any desired simplification rules such as
  2437. \begin{verbatim}
  2438. for all x let sinh(asinh(x))=x, asinh(sinh(x))=x;
  2439. \end{verbatim}
  2440. For completeness, functions with non-unique inverses should be treated as
  2441. {\tt \verb|^|}, {\tt SIN}, and {\tt COS} are in the {\tt SOLVE}
  2442. \ttindex{SOLVE} module source.
  2443. Arguments of {\tt ASIN} and {\tt ACOS} are not checked to ensure that the
  2444. absolute value of the real part does not exceed 1; and arguments of
  2445. {\tt LOG} are not checked to ensure that the absolute value of the imaginary
  2446. part does not exceed $\pi$; but checks (perhaps involving user response
  2447. for non-numerical arguments) could be introduced using
  2448. {\tt LET}\ttindex{LET} statements for these operators.
  2449. \subsection{Parameters and Variable Dependency}
  2450. The proper design of a variable sequence
  2451. supplied as a second argument to {\tt SOLVE} is important
  2452. for the structure of the solution of an equation system.
  2453. Any unknown in the system
  2454. not in this list is considered totally free. E.g.\ the call
  2455. \begin{verbatim}
  2456. solve({x=2*z,z=2*y},{z});
  2457. \end{verbatim}
  2458. produces an empty list as a result because there is no function
  2459. $z=z(x,y)$ which fulfills both equations for arbitrary $x$ and $y$ values.
  2460. In such a case the share variable {\tt requirements}\ttindex{requirements}
  2461. displays a set of restrictions for the parameters of the system:
  2462. \begin{verbatim}
  2463. requirements;
  2464. {x - 4*y}
  2465. \end{verbatim}
  2466. The non-existence of a formal solution is caused by a
  2467. contradiction which disappears only if the parameters
  2468. of the initial system are set such that all members
  2469. of the requirements list take the value zero.
  2470. For a linear system the set is complete: a solution
  2471. of the requirements list makes the initial
  2472. system solvable. E.g.\ in the above case a substitution
  2473. $x=4y$ makes the equation set consistent. For a non-linear
  2474. system only one inconsistency is detected. If such a system
  2475. has more than one inconsistency, you must reduce them
  2476. one after the other.
  2477. \footnote{
  2478. The difference between linear and non--linear
  2479. inconsistent systems is based on the algorithms which
  2480. produce this information as a side effect when attempting
  2481. to find a formal solution; example:
  2482. $solve(\{x=a,x=b,y=c,y=d\},\{x,y\}$ gives a set $\{a-b,c-d\}$
  2483. while $solve(\{x^2=a,x^2=b,y^2=c,y^2=d\},\{x,y\}$ leads to $\{a-b\}$.
  2484. }
  2485. The set shows you also the dependency among the parameters: here
  2486. one of $x$ and $y$ is free and a formal solution of the system can be
  2487. computed by adding it to the variable list of {\tt solve}.
  2488. The requirement set is not unique -- there may be other such sets.
  2489. A system with parameters may have a formal solution, e.g.\
  2490. \begin{verbatim}
  2491. solve({x=a*z+1,0=b*z-y},{z,x});
  2492. y a*y + b
  2493. {{z=---,x=---------}}
  2494. b b
  2495. \end{verbatim}
  2496. which is not valid for all possible values of the parameters.
  2497. The variable {\tt assumptions}\ttindex{assumptions} contains then a list of
  2498. restrictions: the solutions are valid only as long
  2499. as none of these expressions vanishes. Any zero of one of them
  2500. represents a special case that is not covered by the
  2501. formal solution. In the above case the value is
  2502. \extendedmanual{\newpage}
  2503. \begin{verbatim}
  2504. assumptions;
  2505. {b}
  2506. \end{verbatim}
  2507. which excludes formally the case $b=0$; obviously this special
  2508. parameter value makes the system singular. The set of assumptions
  2509. is complete for both, linear and non--linear systems.
  2510. {\tt SOLVE} rearranges the variable sequence
  2511. to reduce the (expected) computing time. This behavior is controlled
  2512. by the switch {\tt varopt}\ttindex{varopt}, which is on by default.
  2513. If it is turned off, the supplied variable sequence is used
  2514. or the system kernel ordering is taken if the variable
  2515. list is omitted. The effect is demonstrated by an example:
  2516. \begin{verbatim}
  2517. s:= {y^3+3x=0,x^2+y^2=1};
  2518. solve(s,{y,x});
  2519. 6 2
  2520. {{y=root_of(y_ + 9*y_ - 9,y_),
  2521. 3
  2522. - y
  2523. x=-------}}
  2524. 3
  2525. off varopt; solve(s,{y,x});
  2526. 6 4 2
  2527. {{x=root_of(x_ - 3*x_ + 12*x_ - 1,x_),
  2528. 4 2
  2529. x*( - x + 2*x - 10)
  2530. y=-----------------------}}
  2531. 3
  2532. \end{verbatim}
  2533. In the first case, {\tt solve} forms the solution as a set of
  2534. pairs $(y_i,x(y_i))$ because the degree of $x$ is higher --
  2535. such a rearrangement makes the internal computation of the Gr\"obner basis
  2536. generally faster. For the second case the explicitly given variable sequence
  2537. is used such that the solution has now the form $(x_i,y(x_i))$.
  2538. Controlling the variable sequence is especially important if
  2539. the system has one or more free variables.
  2540. As an alternative to turning off {\tt varopt}, a partial dependency among
  2541. the variables can be declared using the {\tt depend}\index{depend}
  2542. statement: {\tt solve} then rearranges the variable sequence but keeps any
  2543. variable ahead of those on which it depends.
  2544. \extendedmanual{\newpage}
  2545. \begin{verbatim}
  2546. on varopt;
  2547. s:={a^3+b,b^2+c}$
  2548. solve(s,{a,b,c});
  2549. 3 6
  2550. {{a=arbcomplex(1),b= - a ,c= - a }}
  2551. depend a,c; depend b,c; solve(s,{a,b,c});
  2552. {{c=arbcomplex(2),
  2553. 6
  2554. a=root_of(a_ + c,a_),
  2555. 3
  2556. b= - a }}
  2557. \end{verbatim}
  2558. Here {\tt solve} is forced to put $c$ after $a$ and after $b$, but
  2559. there is no obstacle to interchanging $a$ and $b$.
  2560. \section{Even and Odd Operators}\index{Even operator}\index{Odd operator}
  2561. An operator can be declared to be {\em even\/} or {\em odd\/} in its first
  2562. argument by the declarations {\tt EVEN}\ttindex{EVEN} and
  2563. {\tt ODD}\ttindex{ODD} respectively. Expressions involving an operator
  2564. declared in this manner are transformed if the first argument contains a
  2565. minus sign. Any other arguments are not affected. In addition, if say
  2566. {\tt F} is declared odd, then {\tt f(0)} is replaced by zero unless
  2567. {\tt F} is also declared {\em non zero\/} by the declaration
  2568. {\tt NONZERO}\ttindex{NONZERO}. For example, the declarations
  2569. \begin{verbatim}
  2570. even f1; odd f2;
  2571. \end{verbatim}
  2572. mean that
  2573. \begin{verbatim}
  2574. f1(-a) -> F1(A)
  2575. f2(-a) -> -F2(A)
  2576. f1(-a,-b) -> F1(A,-B)
  2577. f2(0) -> 0.
  2578. \end{verbatim}
  2579. To inhibit the last transformation, say {\tt nonzero f2;}.
  2580. \section{Linear Operators}\index{Linear operator}
  2581. An operator can be declared to be linear in its first argument over powers
  2582. of its second argument. If an operator {\tt F} is so declared, {\tt F} of
  2583. any sum is broken up into sums of {\tt F}s, and any factors that are not
  2584. powers of the variable are taken outside. This means that {\tt F} must
  2585. have (at least) two arguments. In addition, the second argument must be
  2586. an identifier (or more generally a kernel), not an expression.
  2587. {\it Example:}
  2588. If {\tt F} were declared linear, then
  2589. \begin{verbatim}
  2590. 5
  2591. f(a*x^5+b*x+c,x) -> F(X ,X)*A + F(X,X)*B + F(1,X)*C
  2592. \end{verbatim}
  2593. More precisely, not only will the variable and its powers remain within the
  2594. scope of the {\tt F} operator, but so will any variable and its powers that
  2595. had been declared to {\tt DEPEND} on the prescribed variable; and so would
  2596. any expression that contains that variable or a dependent variable on any
  2597. level, e.g. {\tt cos(sin(x))}.
  2598. To declare operators {\tt F} and {\tt G} to be linear operators,
  2599. use:\ttindex{LINEAR}
  2600. \begin{verbatim}
  2601. linear f,g;
  2602. \end{verbatim}
  2603. The analysis is done of the first argument with respect to the second; any
  2604. other arguments are ignored. It uses the following rules of evaluation:
  2605. \begin{quote}
  2606. \begin{tabbing}
  2607. {\tt f(0) -> 0} \\
  2608. {\tt f(-y,x) -> -F(Y,X)} \\
  2609. {\tt f(y+z,x) -> F(Y,X)+F(Z,X)} \\
  2610. {\tt f(y*z,x) -> Z*F(Y,X)} \hspace{0.5in}\= if Z does not depend on X \\
  2611. {\tt f(y/z,x) -> F(Y,X)/Z} \> if Z does not depend on X
  2612. \end{tabbing}
  2613. \end{quote}
  2614. To summarize, {\tt Y} ``depends'' on the indeterminate {\tt X} in the above
  2615. if either of the following hold:
  2616. \begin{enumerate}
  2617. \item {\tt Y} is an expression that contains {\tt X} at any level as a
  2618. variable, e.g.: {\tt cos(sin(x))}
  2619. \item Any variable in the expression {\tt Y} has been declared dependent on
  2620. {\tt X} by use of the declaration {\tt DEPEND}.
  2621. \end{enumerate}
  2622. The use of such linear operators\index{Linear operator} can be seen in the
  2623. paper Fox, J.A. and A. C. Hearn, ``Analytic Computation of Some Integrals
  2624. in Fourth Order Quantum Electrodynamics'' Journ. Comp. Phys. 14 (1974)
  2625. 301-317, which contains a complete listing of a program for definite
  2626. integration\index{Integration} of some expressions that arise in fourth
  2627. order quantum electrodynamics.
  2628. \section{Non-Commuting Operators}\index{Non-commuting operator}
  2629. An operator can be declared to be non-commutative under multiplication by
  2630. the declaration {\tt NONCOM}.\ttindex{NONCOM}
  2631. {\it Example:}
  2632. After the declaration \\
  2633. {\tt noncom u,v;}\\
  2634. the expressions {\tt
  2635. u(x)*u(y)-u(y)*u(x)} and {\tt u(x)*v(y)-v(y)*u(x)} will remain unchanged
  2636. on simplification, and in particular will not simplify to zero.
  2637. Note that it is the operator ({\tt U} and {\tt V} in the above example)
  2638. and not the variable that has the non-commutative property.
  2639. The {\tt LET}\ttindex{LET} statement may be used to introduce rules of
  2640. evaluation for such operators. In particular, the boolean operator
  2641. {\tt ORDP}\ttindex{ORDP} is useful for introducing an ordering on such
  2642. expressions.
  2643. {\it Example:}
  2644. The rule
  2645. \begin{verbatim}
  2646. for all x,y such that x neq y and ordp(x,y)
  2647. let u(x)*u(y)= u(y)*u(x)+comm(x,y);
  2648. \end{verbatim}
  2649. would introduce the commutator of {\tt u(x)} and {\tt u(y)} for all
  2650. {\tt X} and {\tt Y}. Note that since {\tt ordp(x,x)} is {\em true}, the
  2651. equality check is necessary in the degenerate case to avoid a circular
  2652. loop in the rule.
  2653. \section{Symmetric and Antisymmetric Operators}
  2654. An operator can be declared to be symmetric with respect to its arguments
  2655. by the declaration {\tt SYMMETRIC}.\ttindex{SYMMETRIC} For example
  2656. \begin{verbatim}
  2657. symmetric u,v;
  2658. \end{verbatim}
  2659. means that any expression involving the top level operators {\tt U} or
  2660. {\tt V} will have its arguments reordered to conform to the internal order
  2661. used by {\REDUCE}. The user can change this order for kernels by the
  2662. command {\tt KORDER}.
  2663. For example, {\tt u(x,v(1,2))} would become {\tt u(v(2,1),x)}, since
  2664. numbers are ordered in decreasing order, and expressions are ordered in
  2665. decreasing order of complexity.
  2666. Similarly the declaration {\tt ANTISYMMETRIC}\ttindex{ANTISYMMETRIC}
  2667. declares an operator antisymmetric. For example,
  2668. \begin{verbatim}
  2669. antisymmetric l,m;
  2670. \end{verbatim}
  2671. means that any expression involving the top level operators {\tt L} or
  2672. {\tt M} will have its arguments reordered to conform to the internal order
  2673. of the system, and the sign of the expression changed if there are an odd
  2674. number of argument interchanges necessary to bring about the new order.
  2675. For example, {\tt l(x,m(1,2))} would become {\tt -l(-m(2,1),x)} since one
  2676. interchange occurs with each operator. An expression like {\tt l(x,x)}
  2677. would also be replaced by 0.
  2678. \section{Declaring New Prefix Operators}
  2679. The user may add new prefix\index{Prefix} operators to the system by
  2680. using the declaration {\tt OPERATOR}. For example:
  2681. \begin{verbatim}
  2682. operator h,g1,arctan;
  2683. \end{verbatim}
  2684. adds the prefix operators {\tt H}, {\tt G1} and {\tt ARCTAN} to the system.
  2685. This allows symbols like {\tt h(w), h(x,y,z), g1(p+q), arctan(u/v)} to be
  2686. used in expressions, but no meaning or properties of the operator are
  2687. implied. The same operator symbol can be used equally well as a 0-, 1-, 2-,
  2688. 3-, etc.-place operator.
  2689. To give a meaning to an operator symbol, or express some of its
  2690. properties, {\tt LET}\ttindex{LET} statements can be used, or the operator
  2691. can be given a definition as a procedure.
  2692. If the user forgets to declare an identifier as an operator, the system
  2693. will prompt the user to do so in interactive mode, or do it automatically
  2694. in non-interactive mode. A diagnostic message will also be printed if an
  2695. identifier is declared {\tt OPERATOR} more than once.
  2696. Operators once declared are global in scope, and so can then be referenced
  2697. anywhere in the program. In other words, a declaration within a block (or
  2698. a procedure) does not limit the scope of the operator to that block, nor
  2699. does the operator go away on exiting the block (use {\tt CLEAR} instead
  2700. for this purpose).
  2701. \section{Declaring New Infix Operators}
  2702. Users can add new infix operators by using the declarations
  2703. {\tt INFIX}\ttindex{INFIX} and {\tt PRECEDENCE}.\ttindex{PRECEDENCE}
  2704. For example,
  2705. \begin{verbatim}
  2706. infix mm;
  2707. precedence mm,-;
  2708. \end{verbatim}
  2709. The declaration {\tt infix mm;} would allow one to use the symbol
  2710. {\tt MM} as an infix operator:
  2711. \begin{quote}
  2712. \hspace{0.2in} {\tt a mm b} \hspace{0.3in} instead of \hspace{0.3in}
  2713. {\tt mm(a,b)}.
  2714. \end{quote}
  2715. The declaration {\tt precedence mm,-;} says that {\tt MM} should be
  2716. inserted into the infix operator precedence list just {\em after\/}
  2717. the $-$ operator. This gives it higher precedence than $-$ and lower
  2718. precedence than * . Thus
  2719. \begin{quote}
  2720. \hspace{0.2in}{\tt a - b mm c - d}\hspace{.3in} means \hspace{.3in}
  2721. {\tt a - (b mm c) - d},
  2722. \end{quote}
  2723. while
  2724. \begin{quote}
  2725. \hspace{0.2in}{\tt a * b mm c * d}\hspace{.3in} means \hspace{.3in}
  2726. {\tt (a * b) mm (c * d)}.
  2727. \end{quote}
  2728. Both infix and prefix\index{Prefix} operators have no transformation
  2729. properties unless {\tt LET}\ttindex{LET} statements or procedure
  2730. declarations are used to assign a meaning.
  2731. We should note here that infix operators so defined are always binary:
  2732. \begin{quote}
  2733. \hspace{0.2in}{\tt a mm b mm c}\hspace{.3in} means \hspace{.3in}
  2734. {\tt (a mm b) mm c}.
  2735. \end{quote}
  2736. \section{Creating/Removing Variable Dependency}
  2737. There are several facilities in {\REDUCE}, such as the differentiation
  2738. \index{Differentiation}
  2739. operator and the linear operator\index{Linear operator} facility, that
  2740. can utilize knowledge of the dependency between various variables, or
  2741. kernels. Such dependency may be expressed by the command {\tt
  2742. DEPEND}.\ttindex{DEPEND} This takes an arbitrary number of arguments and
  2743. sets up a dependency of the first argument on the remaining arguments.
  2744. For example,
  2745. \begin{verbatim}
  2746. depend x,y,z;
  2747. \end{verbatim}
  2748. says that {\tt X} is dependent on both {\tt Y} and {\tt Z}.
  2749. \begin{verbatim}
  2750. depend z,cos(x),y;
  2751. \end{verbatim}
  2752. says that {\tt Z} is dependent on {\tt COS(X)} and {\tt Y}.
  2753. Dependencies introduced by {\tt DEPEND} can be removed by {\tt NODEPEND}.
  2754. \ttindex{NODEPEND} The arguments of this are the same as for {\tt DEPEND}.
  2755. For example, given the above dependencies,
  2756. \begin{verbatim}
  2757. nodepend z,cos(x);
  2758. \end{verbatim}
  2759. says that {\tt Z} is no longer dependent on {\tt COS(X)}, although it remains
  2760. dependent on {\tt Y}.
  2761. \chapter{Display and Structuring of Expressions}\index{Display}
  2762. \index{Structuring}
  2763. In this section, we consider a variety of commands and operators that
  2764. permit the user to obtain various parts of algebraic expressions and also
  2765. display their structure in a variety of forms. Also presented are some
  2766. additional concepts in the {\REDUCE} design that help the user gain a better
  2767. understanding of the structure of the system.
  2768. \section{Kernels}\index{Kernel}
  2769. {\REDUCE} is designed so that each operator in the system has an
  2770. evaluation (or simplification)\index{Simplification} function associated
  2771. with it that transforms the expression into an internal canonical form.
  2772. \index{Canonical form} This form, which bears little resemblance to the
  2773. original expression, is described in detail in Hearn, A. C., ``{\REDUCE} 2:
  2774. A System and Language for Algebraic Manipulation,'' Proc. of the Second
  2775. Symposium on Symbolic and Algebraic Manipulation, ACM, New York (1971)
  2776. 128-133.
  2777. The evaluation function may transform its arguments in one of two
  2778. alternative ways. First, it may convert the expression into other
  2779. operators in the system, leaving no functions of the original operator for
  2780. further manipulation. This is in a sense true of the evaluation functions
  2781. associated with the operators {\tt +}, {\tt *} and {\tt /} , for example,
  2782. because the canonical form\index{Canonical form} does not include these
  2783. operators explicitly. It is also true of an operator such as the
  2784. determinant operator {\tt DET}\ttindex{DET} because the relevant
  2785. evaluation function calculates the appropriate determinant, and the
  2786. operator {\tt DET} no longer appears. On the other hand, the evaluation
  2787. process may leave some residual functions of the relevant operator. For
  2788. example, with the operator {\tt COS}, a residual expression like {\tt
  2789. COS(X)} may remain after evaluation unless a rule for the reduction of
  2790. cosines into exponentials, for example, were introduced. These residual
  2791. functions of an operator are termed {\em kernels\/}\index{Kernel} and are
  2792. stored uniquely like variables. Subsequently, the kernel is carried
  2793. through the calculation as a variable unless transformations are
  2794. introduced for the operator at a later stage.
  2795. In those cases where the evaluation process leaves an operator expression
  2796. with non-trivial arguments, the form of the argument can vary depending on
  2797. the state of the system at the point of evaluation. Such arguments are
  2798. normally produced in expanded form with no terms factored or grouped in
  2799. any way. For example, the expression {\tt cos(2*x+2*y)} will normally be
  2800. returned in the same form. If the argument {\tt 2*x+2*y} were evaluated
  2801. at the top level, however, it would be printed as {\tt 2*(X+Y)}. If it is
  2802. desirable to have the arguments themselves in a similar form, the switch
  2803. {\tt INTSTR}\ttindex{INTSTR} (for ``internal structure''), if on, will
  2804. cause this to happen.
  2805. In cases where the arguments of the kernel operators may be reordered, the
  2806. system puts them in a canonical order, based on an internal intrinsic
  2807. ordering of the variables. However, some commands allow arguments in the
  2808. form of kernels, and the user has no way of telling what internal order the
  2809. system will assign to these arguments. To resolve this difficulty, we
  2810. introduce the notion of a {\em kernel form\/}\index{kernel form} as an
  2811. expression that transforms to a kernel on evaluation.
  2812. Examples of kernel forms are:
  2813. \begin{verbatim}
  2814. a
  2815. cos(x*y)
  2816. log(sin(x))
  2817. \end{verbatim}
  2818. whereas
  2819. \begin{verbatim}
  2820. a*b
  2821. (a+b)^4
  2822. \end{verbatim}
  2823. are not.
  2824. We see that kernel forms can usually be used as generalized variables, and
  2825. most algebraic properties associated with variables may also be associated
  2826. with kernels.
  2827. \section{The Expression Workspace}\index{Workspace}
  2828. Several mechanisms are available for saving and retrieving previously
  2829. evaluated expressions. The simplest of these refers to the last algebraic
  2830. expression simplified. When an assignment of an algebraic expression is
  2831. made, or an expression is evaluated at the top level, (i.e., not inside a
  2832. compound statement or procedure) the results of the evaluation are
  2833. automatically saved in a variable {\tt WS} that we shall refer to as the
  2834. workspace. (More precisely, the expression is assigned to the variable
  2835. {\tt WS} that is then available for further manipulation.)
  2836. {\it Example:}
  2837. If we evaluate the expression {\tt (x+y)\verb|^|2} at the top level and next
  2838. wish to differentiate it with respect to {\tt Y}, we can simply say
  2839. \begin{verbatim}
  2840. df(ws,y);
  2841. \end{verbatim}
  2842. to get the desired answer.
  2843. If the user wishes to assign the workspace to a variable or expression for
  2844. later use, the {\tt SAVEAS}\ttindex{SAVEAS} statement can be used. It
  2845. has the syntax
  2846. \begin{verbatim}
  2847. SAVEAS <expression>
  2848. \end{verbatim}
  2849. For example, after the differentiation in the last example, the workspace
  2850. holds the expression {\tt 2*x+2*y}. If we wish to assign this to the
  2851. variable {\tt Z} we can now say
  2852. \begin{verbatim}
  2853. saveas z;
  2854. \end{verbatim}
  2855. If the user wishes to save the expression in a form that allows him to use
  2856. some of its variables as arbitrary parameters, the {\tt FOR ALL}
  2857. command can be used.
  2858. {\it Example:}
  2859. \begin{verbatim}
  2860. for all x saveas h(x);
  2861. \end{verbatim}
  2862. with the above expression would mean that {\tt h(z)} evaluates to {\tt
  2863. 2*Y+2*Z}.
  2864. A further method for referencing more than the last expression is described
  2865. in the section on interactive use of {\REDUCE}.
  2866. \section{Output of Expressions}
  2867. A considerable degree of flexibility is available in {\REDUCE} in the
  2868. printing of expressions generated during calculations. No explicit format
  2869. statements are supplied, as these are in most cases of little use in
  2870. algebraic calculations, where the size of output or its composition is not
  2871. generally known in advance. Instead, {\REDUCE} provides a series of mode
  2872. options to the user that should enable him to produce his output in a
  2873. comprehensible and possibly pleasing form.
  2874. The most extreme option offered is to suppress the output entirely from
  2875. any top level evaluation. This is accomplished by turning off the switch
  2876. {\tt OUTPUT}\ttindex{OUTPUT} which is normally on. It is useful for
  2877. limiting output when loading large files or producing ``clean'' output from
  2878. the prettyprint programs.
  2879. In most circumstances, however, we wish to view the output, so we need to
  2880. know how to format it appropriately. As we mentioned earlier, an
  2881. algebraic expression is normally printed in an expanded form, filling the
  2882. whole output line with terms. Certain output declarations,\index{Output
  2883. declaration} however, can be used to affect this format. To begin with,
  2884. we look at an operator for changing the length of the output line.
  2885. \subsection{LINELENGTH Operator}\ttindex{LINELENGTH}
  2886. This operator is used with the syntax
  2887. \begin{verbatim}
  2888. LINELENGTH(NUM:integer):integer
  2889. \end{verbatim}
  2890. and sets the output line length to the integer {\tt NUM}. It returns the
  2891. previous output line length (so that it can be stored for later resetting
  2892. of the output line if needed).
  2893. \subsection{Output Declarations}
  2894. We now describe a number of switches and declarations that are available
  2895. for controlling output formats. It should be noted, however, that the
  2896. transformation of large expressions to produce these varied output formats
  2897. can take a lot of computing time and space. If a user wishes to speed up
  2898. the printing of the output in such cases, he can turn off the switch {\tt
  2899. PRI}.\ttindex{PRI} If this is done, then output is produced in one fixed
  2900. format, which basically reflects the internal form of the expression, and
  2901. none of the options below apply. {\tt PRI} is normally on.
  2902. With {\tt PRI} on, the output declarations\index{Output declaration}
  2903. and switches available are as follows:
  2904. \subsubsection{ORDER Declaration}
  2905. The declaration {\tt ORDER}\ttindex{ORDER} may be used to order variables
  2906. on output. The syntax is:
  2907. \begin{verbatim}
  2908. order v1,...vn;
  2909. \end{verbatim}
  2910. where the {\tt vi} are kernels. Thus,
  2911. \begin{verbatim}
  2912. order x,y,z;
  2913. \end{verbatim}
  2914. orders {\tt X} ahead of {\tt Y}, {\tt Y} ahead of {\tt Z} and all three
  2915. ahead of other variables not given an order. {\tt order nil;} resets the
  2916. output order to the system default. The order of variables may be changed
  2917. by further calls of {\tt ORDER}, but then the reordered variables would
  2918. have an order lower than those in earlier {\tt ORDER}\ttindex{ORDER} calls.
  2919. Thus,
  2920. \begin{verbatim}
  2921. order x,y,z;
  2922. order y,x;
  2923. \end{verbatim}
  2924. would order {\tt Z} ahead of {\tt Y} and {\tt X}. The default ordering is
  2925. usually alphabetic.
  2926. \subsubsection{FACTOR Declaration}
  2927. This declaration takes a list of identifiers or kernels\index{Kernel}
  2928. as argument. {\tt FACTOR}\ttindex{FACTOR} is not a factoring command
  2929. (use {\tt FACTORIZE} or the {\tt FACTOR} switch for this purpose); rather it
  2930. is a separation command. All terms involving fixed powers of the declared
  2931. expressions are printed as a product of the fixed powers and a sum of the
  2932. rest of the terms.
  2933. All expressions involving a given prefix operator may also be factored by
  2934. putting the operator name in the list of factored identifiers. For example:
  2935. \begin{verbatim}
  2936. factor x,cos,sin(x);
  2937. \end{verbatim}
  2938. causes all powers of {\tt X} and {\tt SIN(X)} and all functions of
  2939. {\tt COS} to be factored.
  2940. Note that {\tt FACTOR} does not affect the order of its arguments. You
  2941. should also use {\tt ORDER} if this is important.
  2942. The declaration {\tt remfac v1,...,vn;}\ttindex{REMFAC} removes the
  2943. factoring flag from the expressions {\tt v1} through {\tt vn}.
  2944. \subsection{Output Control Switches}
  2945. \label{sec-output}
  2946. In addition to these declarations, the form of the output can be modified
  2947. by switching various output control switches using the declarations
  2948. {\tt ON} and {\tt OFF}. We shall illustrate the use of these switches by an
  2949. example, namely the printing of the expression
  2950. \begin{verbatim}
  2951. x^2*(y^2+2*y)+x*(y^2+z)/(2*a) .
  2952. \end{verbatim}
  2953. The relevant switches are as follows:
  2954. \subsubsection{ALLFAC Switch}
  2955. This switch will cause the system to search the whole expression, or any
  2956. sub-expression enclosed in parentheses, for simple multiplicative factors
  2957. and print them outside the parentheses. Thus our expression with {\tt ALLFAC}
  2958. \ttindex{ALLFAC}
  2959. off will print as
  2960. \begin{verbatim}
  2961. 2 2 2 2
  2962. (2*X *Y *A + 4*X *Y*A + X*Y + X*Z)/(2*A)
  2963. \end{verbatim}
  2964. and with {\tt ALLFAC} on as
  2965. \begin{verbatim}
  2966. 2 2
  2967. X*(2*X*Y *A + 4*X*Y*A + Y + Z)/(2*A) .
  2968. \end{verbatim}
  2969. {\tt ALLFAC} is normally on, and is on in the following examples, except
  2970. where otherwise stated.
  2971. \subsubsection{DIV Switch}\ttindex{DIV}
  2972. This switch makes the system search the denominator of an expression for
  2973. simple factors that it divides into the numerator, so that rational
  2974. fractions and negative powers appear in the output. With {\tt DIV} on, our
  2975. expression would print as
  2976. \begin{verbatim}
  2977. 2 2 (-1) (-1)
  2978. X*(X*Y + 2*X*Y + 1/2*Y *A + 1/2*A *Z) .
  2979. \end{verbatim}
  2980. {\tt DIV} is normally off.
  2981. \subsubsection{LIST Switch}\ttindex{LIST}
  2982. This switch causes the system to print each term in any sum on a separate
  2983. line. With {\tt LIST} on, our expression prints as
  2984. \begin{verbatim}
  2985. 2
  2986. X*(2*X*Y *A
  2987. + 4*X*Y*A
  2988. 2
  2989. + Y
  2990. + Z)/(2*A) .
  2991. \end{verbatim}
  2992. {\tt LIST} is normally off.
  2993. \subsubsection{NOSPLIT Switch}\ttindex{NOSPLIT}
  2994. Under normal circumstances, the printing routines try to break an expression
  2995. across lines at a natural point. This is a fairly expensive process. If
  2996. you are not overly concerned about where the end-of-line breaks come, you
  2997. can speed up the printing of expressions by turning off the switch
  2998. {\tt NOSPLIT}. This switch is normally on.
  2999. \subsubsection{RAT Switch}\ttindex{RAT}
  3000. This switch is only useful with expressions in which variables are
  3001. factored with {\tt FACTOR}. With this mode, the overall denominator of the
  3002. expression is printed with each factored sub-expression. We assume a prior
  3003. declaration {\tt factor x;} in the following output. We first print the
  3004. expression with {\tt RAT off}:
  3005. \begin{verbatim}
  3006. 2 2
  3007. (2*X *Y*A*(Y + 2) + X*(Y + Z))/(2*A) .
  3008. \end{verbatim}
  3009. With {\tt RAT} on the output becomes:
  3010. \extendedmanual{\newpage}
  3011. \begin{verbatim}
  3012. 2 2
  3013. X *Y*(Y + 2) + X*(Y + Z)/(2*A) .
  3014. \end{verbatim}
  3015. {\tt RAT} is normally off.
  3016. Next, if we leave {\tt X} factored, and turn on both {\tt DIV} and
  3017. {\tt RAT}, the result becomes
  3018. \begin{verbatim}
  3019. 2 (-1) 2
  3020. X *Y*(Y + 2) + 1/2*X*A *(Y + Z) .
  3021. \end{verbatim}
  3022. Finally, with {\tt X} factored, {\tt RAT} on and {\tt ALLFAC}\ttindex{ALLFAC}
  3023. off we retrieve the original structure
  3024. \begin{verbatim}
  3025. 2 2 2
  3026. X *(Y + 2*Y) + X*(Y + Z)/(2*A) .
  3027. \end{verbatim}
  3028. \subsubsection{RATPRI Switch}\ttindex{RATPRI}
  3029. If the numerator and denominator of an expression can each be printed in
  3030. one line, the output routines will print them in a two dimensional
  3031. notation, with numerator and denominator on separate lines and a line of
  3032. dashes in between. For example, {\tt (a+b)/2} will print as
  3033. \begin{verbatim}
  3034. A + B
  3035. -----
  3036. 2
  3037. \end{verbatim}
  3038. Turning this switch off causes such expressions to be output in a linear
  3039. form.
  3040. \subsubsection{REVPRI Switch}\ttindex{REVPRI}
  3041. The normal ordering of terms in output is from highest to lowest power.
  3042. In some situations (e.g., when a power series is output), the opposite
  3043. ordering is more convenient. The switch {\tt REVPRI} if on causes such a
  3044. reverse ordering of terms. For example, the expression
  3045. {\tt y*(x+1)\verb|^|2+(y+3)\verb|^|2} will normally print as
  3046. \begin{verbatim}
  3047. 2 2
  3048. X *Y + 2*X*Y + Y + 7*Y + 9
  3049. \end{verbatim}
  3050. whereas with {\tt REVPRI} on, it will print as
  3051. \begin{verbatim}
  3052. 2 2
  3053. 9 + 7*Y + Y + 2*X*Y + X *Y.
  3054. \end{verbatim}
  3055. \subsection{WRITE Command}\ttindex{WRITE}
  3056. In simple cases no explicit output\index{Output} command is necessary in
  3057. {\REDUCE}, since the value of any expression is automatically printed if a
  3058. semicolon is used as a delimiter. There are, however, several situations
  3059. in which such a command is useful.
  3060. In a {\tt FOR}, {\tt WHILE}, or {\tt REPEAT} statement it may be desired
  3061. to output something each time the statement within the loop construct is
  3062. repeated.
  3063. It may be desired for a procedure to output intermediate results or other
  3064. information while it is running. It may be desired to have results labeled
  3065. in special ways, especially if the output is directed to a file or device
  3066. other than the terminal.
  3067. The {\tt WRITE} command consists of the word {\tt WRITE} followed by one
  3068. or more items separated by commas, and followed by a terminator. There
  3069. are three kinds of items that can be used:
  3070. \begin{enumerate}
  3071. \item Expressions (including variables and constants). The expression is
  3072. evaluated, and the result is printed out.
  3073. \item Assignments. The expression on the right side of the {\tt :=}
  3074. operator is evaluated, and is assigned to the variable on the left; then
  3075. the symbol on the left is printed, followed by a ``{\tt :=}'', followed by
  3076. the value of the expression on the right -- almost exactly the way an
  3077. assignment followed by a semicolon prints out normally. (The difference is
  3078. that if the {\tt WRITE} is in a {\tt FOR} statement and the left-hand side
  3079. of the assignment is an array position or something similar containing the
  3080. variable of the {\tt FOR} iteration, then the value of that variable is
  3081. inserted in the printout.)
  3082. \item Arbitrary strings of characters, preceded and followed by double-quote
  3083. marks (e.g., {\tt "string"}).
  3084. \end{enumerate}
  3085. The items specified by a single {\tt WRITE} statement print side by side
  3086. on one line. (The line is broken automatically if it is too long.) Strings
  3087. print exactly as quoted. The {\tt WRITE} command itself however does not
  3088. return a value.
  3089. The print line is closed at the end of a {\tt WRITE} command evaluation.
  3090. Therefore the command {\tt WRITE "";} (specifying nothing to be printed
  3091. except the empty string) causes a line to be skipped.
  3092. {\it Examples:}
  3093. \begin{enumerate}
  3094. \item If {\tt A} is {\tt X+5}, {\tt B} is itself, {\tt C} is 123, {\tt M} is
  3095. an array, and {\tt Q}=3, then
  3096. \begin{verbatim}
  3097. write m(q):=a," ",b/c," THANK YOU";
  3098. \end{verbatim}
  3099. will set {\tt M(3)} to {\tt x+5} and print
  3100. \begin{verbatim}
  3101. M(Q) := X + 5 B/123 THANK YOU
  3102. \end{verbatim}
  3103. The blanks between the {\tt 5} and {\tt B}, and the
  3104. {\tt 3} and {\tt T}, come from the blanks in the quoted strings.
  3105. \item To print a table of the squares of the integers from 1 to 20:
  3106. \begin{verbatim}
  3107. for i:=1:20 do write i," ",i^2;
  3108. \end{verbatim}
  3109. \item To print a table of the squares of the integers from 1 to 20, and at
  3110. the same time store them in positions 1 to 20 of an array {\tt A:}
  3111. \begin{verbatim}
  3112. for i:=1:20 do <<a(i):=i^2; write i," ",a(i)>>;
  3113. \end{verbatim}
  3114. This will give us two columns of numbers. If we had used
  3115. \begin{verbatim}
  3116. for i:=1:20 do write i," ",a(i):=i^2;
  3117. \end{verbatim}
  3118. we would also get {\tt A(}i{\tt ) := } repeated on each line.
  3119. \item The following more complete example calculates the famous f and g
  3120. series, first reported in Sconzo, P., LeSchack, A. R., and Tobey, R.,
  3121. ``Symbolic Computation of f and g Series by Computer'', Astronomical Journal
  3122. 70 (May 1965).
  3123. \begin{verbatim}
  3124. x1:= -sig*(mu+2*eps)$
  3125. x2:= eps - 2*sig^2$
  3126. x3:= -3*mu*sig$
  3127. f:= 1$
  3128. g:= 0$
  3129. for i:= 1 step 1 until 10 do begin
  3130. f1:= -mu*g+x1*df(f,eps)+x2*df(f,sig)+x3*df(f,mu);
  3131. write "f(",i,") := ",f1;
  3132. g1:= f+x1*df(g,eps)+x2*df(g,sig)+x3*df(g,mu);
  3133. write "g(",i,") := ",g1;
  3134. f:=f1$
  3135. g:=g1$
  3136. end;
  3137. \end{verbatim}
  3138. A portion of the output, to illustrate the printout from the {\tt WRITE}
  3139. command, is as follows:
  3140. \begin{verbatim}
  3141. ... <prior output> ...
  3142. 2
  3143. F(4) := MU*(3*EPS - 15*SIG + MU)
  3144. G(4) := 6*SIG*MU
  3145. 2
  3146. F(5) := 15*SIG*MU*( - 3*EPS + 7*SIG - MU)
  3147. 2
  3148. G(5) := MU*(9*EPS - 45*SIG + MU)
  3149. ... <more output> ...
  3150. \end{verbatim}
  3151. \end{enumerate}
  3152. \subsection{Suppression of Zeros}
  3153. It is sometimes annoying to have zero assignments (i.e. assignments of the
  3154. form {\tt <expression> := 0}) printed, especially in printing large arrays
  3155. with many zero elements. The output from such assignments can be
  3156. suppressed by turning on the switch {\tt NERO}.\ttindex{NERO}
  3157. \subsection{{FORTRAN} Style Output Of Expressions}
  3158. It is naturally possible to evaluate expressions numerically in {\REDUCE} by
  3159. giving all variables and sub-expressions numerical values. However, as we
  3160. pointed out elsewhere the user must declare real arithmetical operation by
  3161. turning on the switch {\tt ROUNDED}\ttindex{ROUNDED}. However, it should be
  3162. remembered that arithmetic in {\REDUCE} is not particularly fast, since
  3163. results are interpreted rather than evaluated in a compiled form. The user
  3164. with a large amount of numerical computation after all necessary algebraic
  3165. manipulations have been performed is therefore well advised to perform
  3166. these calculations in a FORTRAN\index{FORTRAN} or similar system. For
  3167. this purpose, {\REDUCE} offers facilities for users to produce FORTRAN
  3168. compatible files for numerical processing.
  3169. First, when the switch {\tt FORT}\ttindex{FORT} is on, the system will
  3170. print expressions in a FORTRAN notation. Expressions begin in column
  3171. seven. If an expression extends over one line, a continuation mark (.)
  3172. followed by a blank appears on subsequent cards. After a certain number
  3173. of lines have been produced (according to the value of the variable {\tt
  3174. CARD\_NO}),\ttindex{CARD\_NO} a new expression is started. If the
  3175. expression printed arises from an assignment to a variable, the variable
  3176. is printed as the name of the expression. Otherwise the expression is
  3177. given the default name {\tt ANS}. An error occurs if identifiers or
  3178. numbers are outside the bounds permitted by FORTRAN.
  3179. A second option is to use the {\tt WRITE} command to produce other programs.
  3180. {\it Example:}
  3181. The following {\REDUCE} statements
  3182. \begin{verbatim}
  3183. on fort;
  3184. out "forfil";
  3185. write "C this is a fortran program";
  3186. write " 1 format(e13.5)";
  3187. write " u=1.23";
  3188. write " v=2.17";
  3189. write " w=5.2";
  3190. x:=(u+v+w)^11;
  3191. write "C it was foolish to expand this expression";
  3192. write " print 1,x";
  3193. write " end";
  3194. shut "forfil";
  3195. off fort;
  3196. \end{verbatim}
  3197. will generate a file {\tt forfil} that contains:
  3198. {\small
  3199. \begin{verbatim}
  3200. c this is a fortran program
  3201. 1 format(e13.5)
  3202. u=1.23
  3203. v=2.17
  3204. w=5.2
  3205. ans1=1320.*u**3*v*w**7+165.*u**3*w**8+55.*u**2*v**9+495.*u
  3206. . **2*v**8*w+1980.*u**2*v**7*w**2+4620.*u**2*v**6*w**3+
  3207. . 6930.*u**2*v**5*w**4+6930.*u**2*v**4*w**5+4620.*u**2*v**3*
  3208. . w**6+1980.*u**2*v**2*w**7+495.*u**2*v*w**8+55.*u**2*w**9+
  3209. . 11.*u*v**10+110.*u*v**9*w+495.*u*v**8*w**2+1320.*u*v**7*w
  3210. . **3+2310.*u*v**6*w**4+2772.*u*v**5*w**5+2310.*u*v**4*w**6
  3211. . +1320.*u*v**3*w**7+495.*u*v**2*w**8+110.*u*v*w**9+11.*u*w
  3212. . **10+v**11+11.*v**10*w+55.*v**9*w**2+165.*v**8*w**3+330.*
  3213. . v**7*w**4+462.*v**6*w**5+462.*v**5*w**6+330.*v**4*w**7+
  3214. . 165.*v**3*w**8+55.*v**2*w**9+11.*v*w**10+w**11
  3215. x=u**11+11.*u**10*v+11.*u**10*w+55.*u**9*v**2+110.*u**9*v*
  3216. . w+55.*u**9*w**2+165.*u**8*v**3+495.*u**8*v**2*w+495.*u**8
  3217. . *v*w**2+165.*u**8*w**3+330.*u**7*v**4+1320.*u**7*v**3*w+
  3218. . 1980.*u**7*v**2*w**2+1320.*u**7*v*w**3+330.*u**7*w**4+462.
  3219. . *u**6*v**5+2310.*u**6*v**4*w+4620.*u**6*v**3*w**2+4620.*u
  3220. . **6*v**2*w**3+2310.*u**6*v*w**4+462.*u**6*w**5+462.*u**5*
  3221. . v**6+2772.*u**5*v**5*w+6930.*u**5*v**4*w**2+9240.*u**5*v
  3222. . **3*w**3+6930.*u**5*v**2*w**4+2772.*u**5*v*w**5+462.*u**5
  3223. . *w**6+330.*u**4*v**7+2310.*u**4*v**6*w+6930.*u**4*v**5*w
  3224. . **2+11550.*u**4*v**4*w**3+11550.*u**4*v**3*w**4+6930.*u**
  3225. . 4*v**2*w**5+2310.*u**4*v*w**6+330.*u**4*w**7+165.*u**3*v
  3226. . **8+1320.*u**3*v**7*w+4620.*u**3*v**6*w**2+9240.*u**3*v**
  3227. . 5*w**3+11550.*u**3*v**4*w**4+9240.*u**3*v**3*w**5+4620.*u
  3228. . **3*v**2*w**6+ans1
  3229. c it was foolish to expand this expression
  3230. print 1,x
  3231. end
  3232. \end{verbatim}
  3233. }
  3234. If the arguments of a {\tt WRITE} statement include an expression that
  3235. requires continuation records, the output will need editing, since the
  3236. output routine prints the arguments of {\tt WRITE} sequentially, and the
  3237. continuation mechanism therefore generates its auxiliary variables after
  3238. the preceding expression has been printed.
  3239. Finally, since there is no direct analog of {\em list\/} in FORTRAN,
  3240. a comment line of the form
  3241. \begin{verbatim}
  3242. c ***** invalid fortran construct (list) not printed
  3243. \end{verbatim}
  3244. will be printed if you try to print a list with {\tt FORT} on.
  3245. \subsubsection{{FORTRAN} Output Options}\index{Output}\index{FORTRAN}
  3246. There are a number of methods available to change the default format of the
  3247. FORTRAN output.
  3248. The breakup of the expression into subparts is such that the number of
  3249. continuation lines produced is less than a given number. This number can
  3250. be modified by the assignment
  3251. \begin{verbatim}
  3252. card_no := <number>;
  3253. \end{verbatim}
  3254. where {\tt <number>} is the {\em total\/} number of cards allowed in a
  3255. statement. The default value of {\tt CARD\_NO} is 20.
  3256. The width of the output expression is also adjustable by the assignment
  3257. \begin{verbatim}
  3258. fort_width := <integer>;
  3259. \end{verbatim}
  3260. \ttindex{FORT\_WIDTH} which sets the total width of a given line to
  3261. {\tt <integer>}. The initial FORTRAN output width is 70.
  3262. {\REDUCE} automatically inserts a decimal point after each isolated integer
  3263. coefficient in a FORTRAN expression (so that, for example, 4 becomes
  3264. {\tt 4.} ). To prevent this, set the {\tt PERIOD}\ttindex{PERIOD}
  3265. mode switch to {\tt OFF}.
  3266. FORTRAN output is normally produced in lower case. If upper case is desired,
  3267. the switch {\tt FORTUPPER}\ttindex{FORTUPPER} should be turned on.
  3268. Finally, the default name {\tt ANS} assigned to an unnamed expression and
  3269. its subparts can be changed by the operator {\tt VARNAME}.
  3270. \ttindex{VARNAME} This takes a single identifier as argument, which then
  3271. replaces {\tt ANS} as the expression name. The value of {\tt VARNAME} is
  3272. its argument.
  3273. Further facilities for the production of FORTRAN and other language output
  3274. are provided by the SCOPE and GENTRAN
  3275. packages\extendedmanual{described in chapters~\ref{GENTRAN} and \ref{SCOPE}}.
  3276. \subsection{Saving Expressions for Later Use as Input}
  3277. \index{Saving an expression}
  3278. It is often useful to save an expression on an external file for use later
  3279. as input in further calculations. The commands for opening and closing
  3280. output files are explained elsewhere. However, we see in the examples on
  3281. output of expressions that the standard ``natural'' method of printing
  3282. expressions is not compatible with the input syntax. So to print the
  3283. expression in an input compatible form we must inhibit this natural style
  3284. by turning off the switch {\tt NAT}.\ttindex{NAT} If this is done, a
  3285. dollar sign will also be printed at the end of the expression.
  3286. {\it Example:}
  3287. The following sequence of commands
  3288. \begin{verbatim}
  3289. off nat; out "out"; x := (y+z)^2; write "end";
  3290. shut "out"; on nat;
  3291. \end{verbatim}
  3292. will generate a file {\tt out} that contains
  3293. \begin{verbatim}
  3294. X := Y**2 + 2*Y*Z + Z**2$
  3295. END$
  3296. \end{verbatim}
  3297. \subsection{Displaying Expression Structure}\index{Displaying structure}
  3298. In those cases where the final result has a complicated form, it is often
  3299. convenient to display the skeletal structure of the answer. The operator
  3300. {\tt STRUCTR},\ttindex{STRUCTR} that takes a single expression as argument,
  3301. will do this for you. Its syntax is:
  3302. \begin{verbatim}
  3303. STRUCTR(EXPRN:algebraic[,ID1:identifier[,ID2:identifier]]);
  3304. \end{verbatim}
  3305. The structure is printed effectively as a tree, in which the subparts are
  3306. laid out with auxiliary names. If the optional {\tt ID1} is absent, the
  3307. auxiliary names are prefixed by the root {\tt ANS}. This root may be
  3308. changed by the operator {\tt VARNAME}\ttindex{VARNAME}. If the
  3309. optional {\tt ID1} is present, and is an array name, the subparts are
  3310. named as elements of that array, otherwise {\tt ID1} is used as the root
  3311. prefix. (The second optional argument {\tt ID2} is explained later.)
  3312. The {\tt EXPRN} can be either a scalar or a matrix expression. Use of any
  3313. other will result in an error.
  3314. {\it Example:}
  3315. Let us suppose that the workspace contains
  3316. {\tt ((A+B)\verb|^|2+C)\verb|^|3+D}.
  3317. Then the input {\tt STRUCTR WS;} will (with {\tt EXP} off) result in the
  3318. output:\newpage
  3319. \begin{verbatim}
  3320. ANS3
  3321. where
  3322. 3
  3323. ANS3 := ANS2 + D
  3324. 2
  3325. ANS2 := ANS1 + C
  3326. ANS1 := A + B
  3327. \end{verbatim}
  3328. The workspace remains unchanged after this operation, since {\tt STRUCTR}
  3329. \ttindex{STRUCTR} in the default situation returns
  3330. no value (if {\tt STRUCTR} is used as a sub-expression, its value is taken
  3331. to be 0). In addition, the sub-expressions are normally only displayed
  3332. and not retained. If you wish to access the sub-expressions with their
  3333. displayed names, the switch {\tt SAVESTRUCTR}\ttindex{SAVESTRUCTR} should be
  3334. turned on. In this case, {\tt STRUCTR} returns a list whose first element
  3335. is a representation for the expression, and subsequent elements are the
  3336. sub-expression relations. Thus, with {\tt SAVESTRUCTR} on, {\tt STRUCTR WS}
  3337. in the above example would return
  3338. \vspace{-11pt}
  3339. \begin{verbatim}
  3340. 3 2
  3341. {ANS3,ANS3=ANS2 + D,ANS2=ANS1 + C,ANS1=A + B}
  3342. \end{verbatim}
  3343. The {\tt PART}\ttindex{PART} operator can
  3344. be used to retrieve the required parts of the expression. For example, to
  3345. get the value of {\tt ANS2} in the above, one could say:
  3346. \begin{verbatim}
  3347. part(ws,3,2);
  3348. \end{verbatim}
  3349. If {\tt FORT} is on, then the results are printed in the reverse order; the
  3350. algorithm in fact guaranteeing that no sub-expression will be referenced
  3351. before it is defined. The second optional argument {\tt ID2} may also be
  3352. used in this case to name the actual expression (or expressions in the
  3353. case of a matrix argument).
  3354. {\it Example:}
  3355. Let us suppose that {\tt M}, a 2 by 1 matrix, contains the elements {\tt
  3356. ((a+b)\verb|^|2 + c)\verb|^|3 + d} and {\tt (a + b)*(c + d)} respectively,
  3357. and that {\tt V} has been declared to be an array. With {\tt EXP} off and
  3358. {\tt FORT} on, the statement {\tt structr(2*m,v,k);} will result in the output
  3359. \begin{verbatim}
  3360. V(1)=A+B
  3361. V(2)=V(1)**2+C
  3362. V(3)=V(2)**3+D
  3363. V(4)=C+D
  3364. K(1,1)=2.*V(3)
  3365. K(2,1)=2.*V(1)*V(4)
  3366. \end{verbatim}
  3367. \section{Changing the Internal Order of Variables}
  3368. The internal ordering of variables (more specifically kernels) can have
  3369. a significant effect on the space and time associated with a calculation.
  3370. In its default state, {\REDUCE} uses a specific order for this which may
  3371. vary between sessions. However, it is possible for the user to change
  3372. this internal order by means of the declaration
  3373. {\tt KORDER}\ttindex{KORDER}. The syntax for this is:
  3374. \begin{verbatim}
  3375. korder v1,...,vn;
  3376. \end{verbatim}
  3377. where the {\tt Vi} are kernels\index{Kernel}. With this declaration, the
  3378. {\tt Vi} are ordered internally ahead of any other kernels in the system.
  3379. {\tt V1} has the highest order, {\tt V2} the next highest, and so on. A
  3380. further call of {\tt KORDER} replaces a previous one. {\tt KORDER NIL;}
  3381. resets the internal order to the system default.
  3382. Unlike the {\tt ORDER}\ttindex{ORDER} declaration, that has a purely
  3383. cosmetic effect on the way results are printed, the use of {\tt KORDER}
  3384. can have a significant effect on computation time. In critical cases
  3385. then, the user can experiment with the ordering of the variables used to
  3386. determine the optimum set for a given problem.
  3387. \section{Obtaining Parts of Algebraic Expressions}
  3388. There are many occasions where it is desirable to obtain a specific part
  3389. of an expression, or even change such a part to another expression. A
  3390. number of operators are available in {\REDUCE} for this purpose, and will be
  3391. described in this section. In addition, operators for obtaining specific
  3392. parts of polynomials and rational functions (such as a denominator) are
  3393. described in another section.
  3394. \subsection{COEFF Operator}\ttindex{COEFF}
  3395. Syntax:
  3396. \begin{verbatim}
  3397. COEFF(EXPRN:polynomial,VAR:kernel)
  3398. \end{verbatim}
  3399. {\tt COEFF} is an operator that partitions {\tt EXPRN} into its various
  3400. coefficients with respect to {\tt VAR} and returns them as a list, with
  3401. the coefficient independent of {\tt VAR} first.
  3402. Under normal circumstances, an error results if {\tt EXPRN} is not a
  3403. polynomial in {\tt VAR}, although the coefficients themselves can be
  3404. rational as long as they do not depend on {\tt VAR}. However, if the
  3405. switch {\tt RATARG}\ttindex{RATARG} is on, denominators are not checked for
  3406. dependence on {\tt VAR}, and are taken to be part of the coefficients.
  3407. {\it Example:}
  3408. \begin{verbatim}
  3409. coeff((y^2+z)^3/z,y);
  3410. \end{verbatim}
  3411. returns the result
  3412. \begin{verbatim}
  3413. 2
  3414. {Z ,0,3*Z,0,3,0,1/Z}.
  3415. \end{verbatim}
  3416. whereas
  3417. \begin{verbatim}
  3418. coeff((y^2+z)^3/y,y);
  3419. \end{verbatim}
  3420. gives an error if {\tt RATARG} is off, and the result
  3421. \begin{verbatim}
  3422. 3 2
  3423. {Z /Y,0,3*Z /Y,0,3*Z/Y,0,1/Y}
  3424. \end{verbatim}
  3425. if {\tt RATARG} is on.
  3426. The length of the result of {\tt COEFF} is the highest power of {\tt VAR}
  3427. encountered plus 1. In the above examples it is 7. In addition, the
  3428. variable {\tt HIGH\_POW}\ttindex{HIGH\_POW} is set to the highest non-zero
  3429. power found in {\tt EXPRN} during the evaluation, and {\tt LOW\_POW}
  3430. \ttindex{LOW\_POW} to the lowest non-zero power, or zero if there is a
  3431. constant term. If {\tt EXPRN} is a constant, then {\tt HIGH\_POW} and
  3432. {\tt LOW\_POW} are both set to zero.
  3433. \subsection{COEFFN Operator}\ttindex{COEFFN}
  3434. The {\tt COEFFN} operator is designed to give the user a particular
  3435. coefficient of a variable in a polynomial, as opposed to {\tt COEFF} that
  3436. returns all coefficients. {\tt COEFFN} is used with the syntax
  3437. \begin{verbatim}
  3438. COEFFN(EXPRN:polynomial,VAR:kernel,N:integer)
  3439. \end{verbatim}
  3440. It returns the $n^{th}$ coefficient of {\tt VAR} in the polynomial
  3441. {\tt EXPRN}.
  3442. \subsection{PART Operator}\ttindex{PART}
  3443. Syntax:
  3444. \begin{verbatim}
  3445. PART(EXPRN:algebraic[,INTEXP:integer])
  3446. \end{verbatim}
  3447. This operator works on the form of the expression as printed {\em or as it
  3448. would have been printed at that point in the calculation\/} bearing in mind
  3449. all the relevant switch settings at that point. The reader therefore
  3450. needs some familiarity with the way that expressions are represented in
  3451. prefix form in {\REDUCE} to use these operators effectively. Furthermore,
  3452. it is assumed that {\tt PRI} is {\tt ON} at that point in the calculation.
  3453. The reason for this is that with {\tt PRI} off, an expression is printed
  3454. by walking the tree representing the expression internally. To save
  3455. space, it is never actually transformed into the equivalent prefix
  3456. expression as occurs when {\tt PRI} is on. However, the operations on
  3457. polynomials described elsewhere can be equally well used in this case to
  3458. obtain the relevant parts.
  3459. The evaluation proceeds recursively down the integer expression list. In
  3460. other words,
  3461. \begin{verbatim}
  3462. PART(<expression>,<integer1>,<integer2>)
  3463. -> PART(PART(<expression>,<integer1>),<integer2>)
  3464. \end{verbatim}
  3465. and so on, and
  3466. \begin{verbatim}
  3467. PART(<expression>) -> <expression>.
  3468. \end{verbatim}
  3469. {\tt INTEXP} can be any expression that evaluates to an integer. If the
  3470. integer is positive, then that term of the expression is found. If the
  3471. integer is 0, the operator is returned. Finally, if the integer is
  3472. negative, the counting is from the tail of the expression rather than the
  3473. head.
  3474. For example, if the expression {\tt a+b} is printed as {\tt A+B} (i.e.,
  3475. the ordering of the variables is alphabetical), then
  3476. \begin{verbatim}
  3477. part(a+b,2) -> B
  3478. part(a+b,-1) -> B
  3479. and
  3480. part(a+b,0) -> PLUS
  3481. \end{verbatim}
  3482. An operator {\tt ARGLENGTH}\ttindex{ARGLENGTH} is available to determine
  3483. the number of arguments of the top level operator in an expression. If
  3484. the expression does not contain a top level operator, then $-1$ is returned.
  3485. For example,
  3486. \begin{verbatim}
  3487. arglength(a+b+c) -> 3
  3488. arglength(f()) -> 0
  3489. arglength(a) -> -1
  3490. \end{verbatim}
  3491. \subsection{Substituting for Parts of Expressions}
  3492. {\tt PART} may also be used to substitute for a given part of an
  3493. expression. In this case, the {\tt PART} construct appears on the
  3494. left-hand side of an assignment statement, and the expression to replace
  3495. the given part on the right-hand side.
  3496. For example, with the normal settings of the {\REDUCE} switches:
  3497. \begin{verbatim}
  3498. xx := a+b;
  3499. part(xx,2) := c; -> A+C
  3500. part(c+d,0) := -; -> C-D
  3501. \end{verbatim}
  3502. Note that {\tt xx} in the above is not changed by this substitution. In
  3503. addition, unlike expressions such as array and matrix elements that have
  3504. an {\em instant evaluation\/}\index{Instant evaluation} property, the values
  3505. of {\tt part(xx,2)} and {\tt part(c+d,0)} are also not changed.
  3506. \chapter{Polynomials and Rationals}
  3507. Many operations in computer algebra are concerned with polynomials
  3508. \index{Polynomial} and rational functions\index{Rational function}. In
  3509. this section, we review some of the switches and operators available for
  3510. this purpose. These are in addition to those that work on general
  3511. expressions (such as {\tt DF} and {\tt INT}) described elsewhere. In the
  3512. case of operators, the arguments are first simplified before the
  3513. operations are applied. In addition, they operate only on arguments of
  3514. prescribed types, and produce a type mismatch error if given arguments
  3515. which cannot be interpreted in the required mode with the current switch
  3516. settings. For example, if an argument is required to be a kernel and
  3517. {\tt a/2} is used (with no other rules for {\tt A}), an error
  3518. \begin{verbatim}
  3519. A/2 invalid as kernel
  3520. \end{verbatim}
  3521. will result.
  3522. With the exception of those that select various parts of a polynomial or
  3523. rational function, these operations have potentially significant effects on
  3524. the space and time associated with a given calculation. The user should
  3525. therefore experiment with their use in a given calculation in order to
  3526. determine the optimum set for a given problem.
  3527. One such operation provided by the system is an operator {\tt LENGTH}
  3528. \ttindex{LENGTH} which returns the number of top level terms in the
  3529. numerator of its argument. For example,
  3530. \begin{verbatim}
  3531. length ((a+b+c)^3/(c+d));
  3532. \end{verbatim}
  3533. has the value 10. To get the number of terms in the denominator, one
  3534. would first select the denominator by the operator {\tt DEN}\ttindex{DEN}
  3535. and then call {\tt LENGTH}, as in
  3536. \begin{verbatim}
  3537. length den ((a+b+c)^3/(c+d));
  3538. \end{verbatim}
  3539. Other operations currently supported, the relevant switches and operators,
  3540. and the required argument and value modes of the latter, follow.
  3541. \section{Controlling the Expansion of Expressions}
  3542. The switch {\tt EXP}\ttindex{EXP} controls the expansion of expressions. If
  3543. it is off, no expansion of powers or products of expressions occurs.
  3544. Users should note however that in this case results come out in a normal
  3545. but not necessarily canonical form. This means that zero expressions
  3546. simplify to zero, but that two equivalent expressions need not necessarily
  3547. simplify to the same form.
  3548. {\it Example:} With {\tt EXP} on, the two expressions
  3549. \begin{verbatim}
  3550. (a+b)*(a+2*b)
  3551. \end{verbatim}
  3552. and
  3553. \begin{verbatim}
  3554. a^2+3*a*b+2*b^2
  3555. \end{verbatim}
  3556. will both simplify to the latter form. With {\tt EXP}
  3557. off, they would remain unchanged, unless the complete factoring {\tt
  3558. (ALLFAC)} option were in force. {\tt EXP} is normally on.
  3559. Several operators that expect a polynomial as an argument behave
  3560. differently when {\tt EXP} is off, since there is often only one term at
  3561. the top level. For example, with {\tt EXP} off
  3562. \begin{verbatim}
  3563. length((a+b+c)^3/(c+d));
  3564. \end{verbatim}
  3565. returns the value 1.
  3566. \section{Factorization of Polynomials}\index{Factorization}
  3567. {\REDUCE} is capable of factorizing univariate and multivariate polynomials
  3568. that have integer coefficients, finding all factors that also have integer
  3569. coefficients. The package for doing this was written by Dr. Arthur C.
  3570. Norman and Ms. P. Mary Ann Moore at The University of Cambridge. It is
  3571. described in P. M. A. Moore and A. C. Norman, ``Implementing a Polynomial
  3572. Factorization and GCD Package'', Proc. SYMSAC '81, ACM (New York) (1981),
  3573. 109-116.
  3574. The easiest way to use this facility is to turn on the switch
  3575. {\tt FACTOR},\ttindex{FACTOR} which causes all expressions to be output in
  3576. a factored form. For example, with {\tt FACTOR} on, the expression
  3577. {\tt A\verb|^|2-B\verb|^|2} is returned as {\tt (A+B)*(A-B)}.
  3578. It is also possible to factorize a given expression explicitly. The
  3579. operator {\tt FACTORIZE}\ttindex{FACTORIZE} that invokes this facility is
  3580. used with the syntax
  3581. \begin{verbatim}
  3582. FACTORIZE(EXPRN:polynomial[,INTEXP:prime integer]):list,
  3583. \end{verbatim}
  3584. the optional argument of which will be described later. Thus to find and
  3585. display all factors of the cyclotomic polynomial $x^{105}-1$, one could
  3586. write:
  3587. \begin{verbatim}
  3588. factorize(x^105-1);
  3589. \end{verbatim}
  3590. The result is a list of factor,exponent pairs.
  3591. In the above example, there is no overall numerical factor in the result,
  3592. so the results will consist only of polynomials in x. The number of such
  3593. polynomials can be found by using the operator {\tt LENGTH}.\ttindex{LENGTH}
  3594. If there is a numerical factor, as in factorizing $12x^{2}-12$,
  3595. that factor will appear as the first member of the result.
  3596. It will however not be factored further. Prime factors of such numbers
  3597. can be found, using a probabilistic algorithm, by turning on the switch
  3598. {\tt IFACTOR}.\ttindex{IFACTOR} For example,
  3599. \begin{verbatim}
  3600. on ifactor; factorize(12x^2-12);
  3601. \end{verbatim}
  3602. would result in the output
  3603. \begin{verbatim}
  3604. {{2,2},{3,1},{X + 1,1},{X - 1,1}}.
  3605. \end{verbatim}
  3606. If the first argument of {\tt FACTORIZE} is an integer, it will be
  3607. decomposed into its prime components, whether or not {\tt IFACTOR} is on.
  3608. Note that the {\tt IFACTOR} switch only affects the result of {\tt FACTORIZE}.
  3609. It has no effect if the {\tt FACTOR}\ttindex{FACTOR} switch is also on.
  3610. The order in which the factors occur in the result (with the exception of
  3611. a possible overall numerical coefficient which comes first) can be system
  3612. dependent and should not be relied on. Similarly it should be noted that
  3613. any pair of individual factors can be negated without altering their
  3614. product, and that {\REDUCE} may sometimes do that.
  3615. The factorizer works by first reducing multivariate problems to univariate
  3616. ones and then solving the univariate ones modulo small primes. It normally
  3617. selects both evaluation points and primes using a random number generator
  3618. that should lead to different detailed behavior each time any particular
  3619. problem is tackled. If, for some reason, it is known that a certain
  3620. (probably univariate) factorization can be performed effectively with a
  3621. known prime, {\tt P} say, this value of {\tt P} can be handed to
  3622. {\tt FACTORIZE}\ttindex{FACTORIZE} as a second
  3623. argument. An error will occur if a non-prime is provided to {\tt FACTORIZE} in
  3624. this manner. It is also an error to specify a prime that divides the
  3625. discriminant of the polynomial being factored, but users should note that
  3626. this condition is not checked by the program, so this capability should be
  3627. used with care.
  3628. Factorization can be performed over a number of polynomial coefficient
  3629. domains in addition to integers. The particular description of the relevant
  3630. domain should be consulted to see if factorization is supported. For
  3631. example, the following statements will factorize $x^{4}+1$ modulo 7:
  3632. \begin{verbatim}
  3633. setmod 7;
  3634. on modular;
  3635. factorize(x^4+1);
  3636. \end{verbatim}
  3637. The factorization module is provided with a trace facility that may be useful
  3638. as a way of monitoring progress on large problems, and of satisfying
  3639. curiosity about the internal workings of the package. The most simple use
  3640. of this is enabled by issuing the {\REDUCE} command\ttindex{TRFAC}
  3641. {\tt on trfac;} .
  3642. Following this, all calls to the factorizer will generate informative
  3643. messages reporting on such things as the reduction of multivariate to
  3644. univariate cases, the choice of a prime and the reconstruction of full
  3645. factors from their images. Further levels of detail in the trace are
  3646. intended mainly for system tuners and for the investigation of suspected
  3647. bugs. For example, {\tt TRALLFAC} gives tracing information at all levels
  3648. of detail. The switch that can be set by {\tt on timings;} makes it
  3649. possible for one who is familiar with the algorithms used to determine
  3650. what part of the factorization code is consuming the most resources.
  3651. {\tt on overview}; reduces the amount of detail presented in other forms of
  3652. trace. Other forms of trace output are enabled by directives of the form
  3653. \begin{verbatim}
  3654. symbolic set!-trace!-factor(<number>,<filename>);
  3655. \end{verbatim}
  3656. where useful numbers are 1, 2, 3 and 100, 101, ... . This facility is
  3657. intended to make it possible to discover in fairly great detail what just
  3658. some small part of the code has been doing --- the numbers refer mainly to
  3659. depths of recursion when the factorizer calls itself, and to the split
  3660. between its work forming and factorizing images and reconstructing full
  3661. factors from these. If {\tt NIL} is used in place of a filename the trace
  3662. output requested is directed to the standard output stream. After use of
  3663. this trace facility the generated trace files should be closed by calling
  3664. \begin{verbatim}
  3665. symbolic close!-trace!-files();
  3666. \end{verbatim}
  3667. {\it NOTE:} Using the factorizer with {\tt MCD}\ttindex{MCD} off will
  3668. result in an error.
  3669. \section{Cancellation of Common Factors}
  3670. Facilities are available in {\REDUCE} for cancelling common factors in the
  3671. numerators and denominators of expressions, at the option of the user. The
  3672. system will perform this greatest common divisor computation if the switch
  3673. {\tt GCD}\ttindex{GCD} is on. ({\tt GCD} is normally off.)
  3674. A check is automatically made, however, for common variable and numerical
  3675. products in the numerators and denominators of expressions, and the
  3676. appropriate cancellations made.
  3677. When {\tt GCD} is on, and {\tt EXP} is off, a check is made for square
  3678. free factors in an expression. This includes separating out and
  3679. independently checking the content of a given polynomial where
  3680. appropriate. (For an explanation of these terms, see Anthony C. Hearn,
  3681. ``Non-Modular Computation of Polynomial GCDs Using Trial Division'', Proc.
  3682. EUROSAM 79, published as Lecture Notes on Comp. Science, Springer-Verlag,
  3683. Berlin, No 72 (1979) 227-239.)
  3684. {\it Example:} With {\tt EXP}\ttindex{EXP} off and {\tt GCD}\ttindex{GCD}
  3685. on,
  3686. the polynomial {\tt a*c+a*d+b*c+b*d} would be returned as {\tt (A+B)*(C+D)}.
  3687. Under normal circumstances, GCDs are computed using an algorithm described
  3688. in the above paper. It is also possible in {\REDUCE} to compute GCDs using
  3689. an alternative algorithm, called the EZGCD Algorithm, which uses modular
  3690. arithmetic. The switch {\tt EZGCD}\ttindex{EZGCD}, if on in addition to
  3691. {\tt GCD}, makes this happen.
  3692. In non-trivial cases, the EZGCD algorithm is almost always better
  3693. than the basic algorithm, often by orders of magnitude. We therefore
  3694. {\em strongly\/} advise users to use the {\tt EZGCD} switch where they have the
  3695. resources available for supporting the package.
  3696. For a description of the EZGCD algorithm, see J. Moses and D.Y.Y. Yun,
  3697. ``The EZ GCD Algorithm'', Proc. ACM 1973, ACM, New York (1973) 159-166.
  3698. {\it NOTE:}
  3699. This package shares code with the factorizer, so a certain amount of trace
  3700. information can be produced using the factorizer trace switches.
  3701. \subsection{Determining the GCD of Two Polynomials}
  3702. This operator, used with the syntax
  3703. \begin{verbatim}
  3704. GCD(EXPRN1:polynomial,EXPRN2:polynomial):polynomial,
  3705. \end{verbatim}
  3706. returns the greatest common divisor of the two polynomials {\tt EXPRN1} and
  3707. {\tt EXPRN2}.
  3708. {\it Examples:}
  3709. \begin{verbatim}
  3710. gcd(x^2+2*x+1,x^2+3*x+2) -> X+1
  3711. gcd(2*x^2-2*y^2,4*x+4*y) -> 2*X+2*Y
  3712. gcd(x^2+y^2,x-y) -> 1.
  3713. \end{verbatim}
  3714. \section{Working with Least Common Multiples}
  3715. Greatest common divisor calculations can often become expensive if
  3716. extensive work with large rational expressions is required. However, in
  3717. many cases, the only significant cancellations arise from the fact that
  3718. there are often common factors in the various denominators which are
  3719. combined when two rationals are added. Since these denominators tend to be
  3720. smaller and more regular in structure than the numerators, considerable
  3721. savings in both time and space can occur if a full GCD check is made when
  3722. the denominators are combined and only a partial check when numerators are
  3723. constructed. In other words, the true least common multiple of the
  3724. denominators is computed at each step. The switch {\tt LCM}\ttindex{LCM}
  3725. is available for this purpose, and is normally on.
  3726. In addition, the operator {\tt LCM},\ttindex{LCM} used with the syntax
  3727. \begin{verbatim}
  3728. LCM(EXPRN1:polynomial,EXPRN2:polynomial):polynomial,
  3729. \end{verbatim}
  3730. returns the least common multiple of the two polynomials {\tt EXPRN1} and
  3731. {\tt EXPRN2}.
  3732. {\it Examples:}
  3733. \begin{verbatim}
  3734. lcm(x^2+2*x+1,x^2+3*x+2) -> X**3 + 4*X**2 + 5*X + 2
  3735. lcm(2*x^2-2*y^2,4*x+4*y) -> 4*(X**2 - Y**2)
  3736. \end{verbatim}
  3737. \section{Controlling Use of Common Denominators}
  3738. When two rational functions are added, {\REDUCE} normally produces an
  3739. expression over a common denominator. However, if the user does not want
  3740. denominators combined, he or she can turn off the switch {\tt MCD}
  3741. \ttindex{MCD} which controls this process. The latter switch is
  3742. particularly useful if no greatest common divisor calculations are
  3743. desired, or excessive differentiation of rational functions is required.
  3744. {\it CAUTION:} With {\tt MCD} off, results are not guaranteed to come out in
  3745. either normal or canonical form. In other words, an expression equivalent
  3746. to zero may in fact not be simplified to zero. This option is therefore
  3747. most useful for avoiding expression swell during intermediate parts of a
  3748. calculation.
  3749. {\tt MCD}\ttindex{MCD} is normally on.
  3750. \section{REMAINDER Operator}\ttindex{REMAINDER}
  3751. This operator is used with the syntax
  3752. \begin{verbatim}
  3753. REMAINDER(EXPRN1:polynomial,EXPRN2:polynomial):polynomial.
  3754. \end{verbatim}
  3755. It returns the remainder when {\tt EXPRN1} is divided by {\tt EXPRN2}. This
  3756. is the true remainder based on the internal ordering of the variables, and
  3757. not the pseudo-remainder. The pseudo-remainder \ttindex{PSEUDO\_REMAINDER}
  3758. and in general pseudo-division \ttindex{PSEUDO\_DIVIDE} of polynomials
  3759. can be calculated after loading the {\tt polydiv} package.
  3760. Please refer to the documentation of this package for details.
  3761. {\it Examples:}
  3762. \begin{verbatim}
  3763. remainder((x+y)*(x+2*y),x+3*y) -> 2*Y**2
  3764. remainder(2*x+y,2) -> Y.
  3765. \end{verbatim}
  3766. {\it CAUTION:} In the default case, remainders are calculated over the
  3767. integers. If you need the remainder with respect to another domain, it
  3768. must be declared explicitly.
  3769. {\it Example:}
  3770. \begin{verbatim}
  3771. remainder(x^2-2,x+sqrt(2)); -> X^2 - 2
  3772. load_package arnum;
  3773. defpoly sqrt2**2-2;
  3774. remainder(x^2-2,x+sqrt2); -> 0
  3775. \end{verbatim}
  3776. \section{RESULTANT Operator}\ttindex{RESULTANT}
  3777. This is used with the syntax
  3778. \begin{verbatim}
  3779. RESULTANT(EXPRN1:polynomial,EXPRN2:polynomial,VAR:kernel):
  3780. polynomial.
  3781. \end{verbatim}
  3782. It computes the resultant of the two given polynomials with respect to the
  3783. given variable, the coefficients of the polynomials can be taken from any
  3784. domain. The result can be identified as the determinant of a
  3785. Sylvester matrix, but can often also be thought of informally as the
  3786. result obtained when the given variable is eliminated between the two input
  3787. polynomials. If the two input polynomials have a non-trivial GCD their
  3788. resultant vanishes.
  3789. The switch {\tt Bezout}\ttindex{Bezout} controls the computation of the
  3790. resultants. It is off by default. In this case a subresultant algorithm
  3791. is used. If the switch Bezout is turned on, the resultant is computed via
  3792. the Bezout Matrix. However, in the latter case, only polynomial coefficients
  3793. are permitted.
  3794. \begin{samepage}
  3795. The sign conventions used by the resultant function follow those in R.
  3796. Loos, ``Computing in Algebraic Extensions'' in ``Computer Algebra --- Symbolic
  3797. and Algebraic Computation'', Second Ed., Edited by B. Buchberger, G.E.
  3798. Collins and R. Loos, Springer-Verlag, 1983. Namely, with {\tt A} and {\tt B}
  3799. not dependent on {\tt X}:
  3800. \begin{verbatim}
  3801. deg(p)*deg(q)
  3802. resultant(p(x),q(x),x)= (-1) *resultant(q,p,x)
  3803. deg(p)
  3804. resultant(a,p(x),x) = a
  3805. resultant(a,b,x) = 1
  3806. \end{verbatim}
  3807. \end{samepage}
  3808. {\it Examples:}
  3809. \begin{samepage}
  3810. \begin{verbatim}
  3811. 2
  3812. resultant(x/r*u+y,u*y,u) -> - y
  3813. \end{verbatim}
  3814. \end{samepage}
  3815. {\it calculation in an algebraic extension:}
  3816. \begin{samepage}
  3817. \begin{verbatim}
  3818. load arnum;
  3819. defpoly sqrt2**2 - 2;
  3820. resultant(x + sqrt2,sqrt2 * x +1,x) -> -1
  3821. \end{verbatim}
  3822. \end{samepage}
  3823. {\it or in a modular domain:}
  3824. \begin{samepage}
  3825. \begin{verbatim}
  3826. setmod 17;
  3827. on modular;
  3828. resultant(2x+1,3x+4,x) -> 5
  3829. \end{verbatim}
  3830. \end{samepage}
  3831. \section{DECOMPOSE Operator}\ttindex{DECOMPOSE}
  3832. The {\tt DECOMPOSE} operator takes a multivariate polynomial as argument,
  3833. and returns an expression and a list of equations from which the
  3834. original polynomial can be found by composition. Its syntax is:
  3835. \begin{verbatim}
  3836. DECOMPOSE(EXPRN:polynomial):list.
  3837. \end{verbatim}
  3838. For example:
  3839. \begin{verbatim}
  3840. decompose(x^8-88*x^7+2924*x^6-43912*x^5+263431*x^4-
  3841. 218900*x^3+65690*x^2-7700*x+234)
  3842. 2 2 2
  3843. -> {U + 35*U + 234, U=V + 10*V, V=X - 22*X}
  3844. 2
  3845. decompose(u^2+v^2+2u*v+1) -> {W + 1, W=U + V}
  3846. \end{verbatim}
  3847. Users should note however that, unlike factorization, this decomposition
  3848. is not unique.
  3849. \section{INTERPOL operator}\ttindex{INTERPOL}
  3850. Syntax:
  3851. \begin{verbatim}
  3852. INTERPOL(<values>,<variable>,<points>);
  3853. \end{verbatim}
  3854. where {\tt <values>} and {\tt <points>} are lists of equal length and
  3855. {\tt <variable>} is an algebraic expression (preferably a kernel).
  3856. {\tt INTERPOL} generates an interpolation polynomial {\em f\/} in the given
  3857. variable of degree length({\tt <values>})-1. The unique polynomial {\em f\/}
  3858. is defined by the property that for corresponding elements {\em v\/} of
  3859. {\tt <values>} and {\em p\/} of {\tt <points>} the relation $f(p)=v$ holds.
  3860. The Aitken-Neville interpolation algorithm is used which guarantees a
  3861. stable result even with rounded numbers and an ill-conditioned problem.
  3862. \section{Obtaining Parts of Polynomials and Rationals}
  3863. These operators select various parts of a polynomial or rational function
  3864. structure. Except for the cost of rearrangement of the structure, these
  3865. operations take very little time to perform.
  3866. For those operators in this section that take a kernel {\tt VAR} as their
  3867. second argument, an error results if the first expression is not a
  3868. polynomial in {\tt VAR}, although the coefficients themselves can be
  3869. rational as long as they do not depend on {\tt VAR}. However, if the
  3870. switch {\tt RATARG}\ttindex{RATARG} is on, denominators are not checked
  3871. for dependence on {\tt VAR}, and are taken to be part of the coefficients.
  3872. \subsection{DEG Operator}\ttindex{DEG}
  3873. This operator is used with the syntax
  3874. \begin{verbatim}
  3875. DEG(EXPRN:polynomial,VAR:kernel):integer.
  3876. \end{verbatim}
  3877. It returns the leading degree\index{Degree} of the polynomial {\tt EXPRN}
  3878. in the variable {\tt VAR}. If {\tt VAR} does not occur as a variable in
  3879. {\tt EXPRN}, 0 is returned.
  3880. {\it Examples:}
  3881. \begin{verbatim}
  3882. deg((a+b)*(c+2*d)^2,a) -> 1
  3883. deg((a+b)*(c+2*d)^2,d) -> 2
  3884. deg((a+b)*(c+2*d)^2,e) -> 0.
  3885. \end{verbatim}
  3886. Note also that if {\tt RATARG} is on,
  3887. \begin{verbatim}
  3888. deg((a+b)^3/a,a) -> 3
  3889. \end{verbatim}
  3890. since in this case, the denominator {\tt A} is considered part of the
  3891. coefficients of the numerator in {\tt A}. With {\tt RATARG} off, however,
  3892. an error would result in this case.
  3893. \subsection{DEN Operator}\ttindex{DEN}
  3894. This is used with the syntax:
  3895. \begin{verbatim}
  3896. DEN(EXPRN:rational):polynomial.
  3897. \end{verbatim}
  3898. It returns the denominator of the rational expression {\tt EXPRN}. If
  3899. {\tt EXPRN} is a polynomial, 1 is returned.
  3900. {\it Examples:}
  3901. \begin{verbatim}
  3902. den(x/y^2) -> Y**2
  3903. den(100/6) -> 3
  3904. [since 100/6 is first simplified to 50/3]
  3905. den(a/4+b/6) -> 12
  3906. den(a+b) -> 1
  3907. \end{verbatim}
  3908. \subsection{LCOF Operator}\ttindex{LCOF}
  3909. LCOF is used with the syntax
  3910. \begin{verbatim}
  3911. LCOF(EXPRN:polynomial,VAR:kernel):polynomial.
  3912. \end{verbatim}
  3913. It returns the leading coefficient\index{Leading coefficient} of the
  3914. polynomial {\tt EXPRN} in the variable {\tt VAR}. If {\tt VAR} does not
  3915. occur as a variable in {\tt EXPRN}, {\tt EXPRN} is returned.
  3916. \extendedmanual{\newpage}
  3917. {\it Examples:}
  3918. \begin{verbatim}
  3919. lcof((a+b)*(c+2*d)^2,a) -> C**2+4*C*D+4*D**2
  3920. lcof((a+b)*(c+2*d)^2,d) -> 4*(A+B)
  3921. lcof((a+b)*(c+2*d),e) -> A*C+2*A*D+B*C+2*B*D
  3922. \end{verbatim}
  3923. \subsection{LPOWER Operator}\ttindex{LPOWER}
  3924. \begin{samepage}
  3925. Syntax:
  3926. \begin{verbatim}
  3927. LPOWER(EXPRN:polynomial,VAR:kernel):polynomial.
  3928. \end{verbatim}
  3929. LPOWER returns the leading power of {\tt EXPRN} with respect to {\tt VAR}.
  3930. If {\tt EXPRN} does not depend on {\tt VAR}, 1 is returned.
  3931. \end{samepage}
  3932. {\it Examples:}
  3933. \begin{verbatim}
  3934. lpower((a+b)*(c+2*d)^2,a) -> A
  3935. lpower((a+b)*(c+2*d)^2,d) -> D**2
  3936. lpower((a+b)*(c+2*d),e) -> 1
  3937. \end{verbatim}
  3938. \subsection{LTERM Operator}\ttindex{LTERM}
  3939. \begin{samepage}
  3940. Syntax:
  3941. \begin{verbatim}
  3942. LTERM(EXPRN:polynomial,VAR:kernel):polynomial.
  3943. \end{verbatim}
  3944. LTERM returns the leading term of {\tt EXPRN} with respect to {\tt VAR}.
  3945. If {\tt EXPRN} does not depend on {\tt VAR}, {\tt EXPRN} is returned.
  3946. \end{samepage}
  3947. {\it Examples:}
  3948. \begin{verbatim}
  3949. lterm((a+b)*(c+2*d)^2,a) -> A*(C**2+4*C*D+4*D**2)
  3950. lterm((a+b)*(c+2*d)^2,d) -> 4*D**2*(A+B)
  3951. lterm((a+b)*(c+2*d),e) -> A*C+2*A*D+B*C+2*B*D
  3952. \end{verbatim}
  3953. {\COMPATNOTE} In some earlier versions of REDUCE, {\tt LTERM} returned
  3954. {\tt 0} if the {\tt EXPRN} did not depend on {\tt VAR}. In the present
  3955. version, {\tt EXPRN} is always equal to {\tt LTERM(EXPRN,VAR)} $+$ {\tt
  3956. REDUCT(EXPRN,VAR)}.
  3957. \subsection{MAINVAR Operator}\ttindex{MAINVAR}
  3958. Syntax:
  3959. \begin{verbatim}
  3960. MAINVAR(EXPRN:polynomial):expression.
  3961. \end{verbatim}
  3962. Returns the main variable (based on the internal polynomial representation)
  3963. of {\tt EXPRN}. If {\tt EXPRN} is a domain element, 0 is returned.
  3964. {\it Examples:}
  3965. Assuming {\tt A} has higher kernel order than {\tt B}, {\tt C}, or {\tt D}:
  3966. \begin{verbatim}
  3967. mainvar((a+b)*(c+2*d)^2) -> A
  3968. mainvar(2) -> 0
  3969. \end{verbatim}
  3970. \subsection{NUM Operator}\ttindex{NUM}
  3971. Syntax:
  3972. \begin{verbatim}
  3973. NUM(EXPRN:rational):polynomial.
  3974. \end{verbatim}
  3975. Returns the numerator of the rational expression {\tt EXPRN}. If {\tt EXPRN}
  3976. is a polynomial, that polynomial is returned.
  3977. {\it Examples:}
  3978. \begin{verbatim}
  3979. num(x/y^2) -> X
  3980. num(100/6) -> 50
  3981. num(a/4+b/6) -> 3*A+2*B
  3982. num(a+b) -> A+B
  3983. \end{verbatim}
  3984. \subsection{REDUCT Operator}\ttindex{REDUCT}
  3985. Syntax:
  3986. \begin{verbatim}
  3987. REDUCT(EXPRN:polynomial,VAR:kernel):polynomial.
  3988. \end{verbatim}
  3989. Returns the reductum of {\tt EXPRN} with respect to {\tt VAR} (i.e., the
  3990. part of {\tt EXPRN} left after the leading term is removed). If {\tt
  3991. EXPRN} does not depend on the variable {\tt VAR}, 0 is returned.
  3992. {\it Examples:}
  3993. \begin{verbatim}
  3994. reduct((a+b)*(c+2*d),a) -> B*(C + 2*D)
  3995. reduct((a+b)*(c+2*d),d) -> C*(A + B)
  3996. reduct((a+b)*(c+2*d),e) -> 0
  3997. \end{verbatim}
  3998. {\COMPATNOTE} In some earlier versions of REDUCE, {\tt REDUCT} returned
  3999. {\tt EXPRN} if it did not depend on {\tt VAR}. In the present version, {\tt
  4000. EXPRN} is always equal to {\tt LTERM(EXPRN,VAR)} $+$ {\tt
  4001. REDUCT(EXPRN,VAR)}.
  4002. \section{Polynomial Coefficient Arithmetic}\index{Coefficient}
  4003. {\REDUCE} allows for a variety of numerical domains for the numerical
  4004. coefficients of polynomials used in calculations. The default mode is
  4005. integer arithmetic, although the possibility of using real coefficients
  4006. \index{Real coefficient} has been discussed elsewhere. Rational
  4007. coefficients have also been available by using integer coefficients in
  4008. both the numerator and denominator of an expression, using the {\tt ON
  4009. DIV}\ttindex{DIV} option to print the coefficients as rationals.
  4010. However, {\REDUCE} includes several other coefficient options in its basic
  4011. version which we shall describe in this section. All such coefficient
  4012. modes are supported in a table-driven manner so that it is
  4013. straightforward to extend the range of possibilities. A description of
  4014. how to do this is given in R.J. Bradford, A.C. Hearn, J.A. Padget and
  4015. E. Schr\"ufer, ``Enlarging the {\REDUCE} Domain of Computation,'' Proc. of
  4016. SYMSAC '86, ACM, New York (1986), 100--106.
  4017. \subsection{Rational Coefficients in Polynomials}\index{Coefficient}
  4018. \index{Rational coefficient}
  4019. Instead of treating rational numbers as the numerator and denominator of a
  4020. rational expression, it is also possible to use them as polynomial
  4021. coefficients directly. This is accomplished by turning on the switch
  4022. {\tt RATIONAL}.\ttindex{RATIONAL}
  4023. {\it Example:} With {\tt RATIONAL} off, the input expression {\tt a/2}
  4024. would be converted into a rational expression, whose numerator was {\tt A}
  4025. and denominator 2. With {\tt RATIONAL} on, the same input would become a
  4026. rational expression with numerator {\tt 1/2*A} and denominator {\tt 1}.
  4027. Thus the latter can be used in operations that require polynomial input
  4028. whereas the former could not.
  4029. \subsection{Real Coefficients in Polynomials}\index{Coefficient}
  4030. \index{Real coefficient}
  4031. The switch {\tt ROUNDED}\ttindex{ROUNDED} permits the use of arbitrary
  4032. sized real coefficients in polynomial expressions. The actual precision
  4033. of these coefficients can be set by the operator {\tt PRECISION}.
  4034. \ttindex{PRECISION} For example, {\tt precision 50;} sets the precision to
  4035. fifty decimal digits. The default precision is system dependent and can
  4036. be found by {\tt precision 0;}. In this mode, denominators are
  4037. automatically made monic, and an appropriate adjustment is made to the
  4038. numerator.
  4039. {\it Example:} With {\tt ROUNDED} on, the input expression {\tt a/2} would
  4040. be converted into a rational expression whose numerator is {\tt 0.5*A} and
  4041. denominator {\tt 1}.
  4042. Internally, {\REDUCE} uses floating point numbers up to the precision
  4043. supported by the underlying machine hardware, and so-called {\em
  4044. bigfloats} for higher precision or whenever necessary to represent numbers
  4045. whose value cannot be represented in floating point. The internal
  4046. precision is two decimal digits greater than the external precision to
  4047. guard against roundoff inaccuracies. Bigfloats represent the fraction and
  4048. exponent parts of a floating-point number by means of (arbitrary
  4049. precision) integers, which is a more precise representation in many cases
  4050. than the machine floating point arithmetic, but not as efficient. If a
  4051. case arises where use of the machine arithmetic leads to problems, a user
  4052. can force {\REDUCE} to use the bigfloat representation at all precisions by
  4053. turning on the switch {\tt ROUNDBF}.\ttindex{ROUNDBF} In rare cases,
  4054. this switch is turned on by the system, and the user informed by the
  4055. message
  4056. \begin{verbatim}
  4057. ROUNDBF turned on to increase accuracy
  4058. \end{verbatim}
  4059. Rounded numbers are normally printed to the specified precision. However,
  4060. if the user wishes to print such numbers with less precision, the printing
  4061. precision can be set by the command {\tt PRINT\_PRECISION}.
  4062. \ttindex{PRINT\_PRECISION} For example, {\tt print\_precision 5;} will
  4063. cause such numbers to be printed with five digits maximum.
  4064. Under normal circumstances when {\tt ROUNDED} is on, {\REDUCE} converts the
  4065. number 1.0 to the integer 1. If this is not desired, the switch
  4066. {\tt NOCONVERT}\ttindex{NOCONVERT} can be turned on.
  4067. Numbers that are stored internally as bigfloats are normally printed with
  4068. a space between every five digits to improve readability. If this
  4069. feature is not required, it can be suppressed by turning off the switch
  4070. {\tt BFSPACE}.\ttindex{BFSPACE}
  4071. Further information on the bigfloat arithmetic may be found in T. Sasaki,
  4072. ``Manual for Arbitrary Precision Real Arithmetic System in {\REDUCE}'',
  4073. Department of Computer Science, University of Utah, Technical Note No.
  4074. TR-8 (1979).
  4075. When a real number is input, it is normally truncated to the precision in
  4076. effect at the time the number is read. If it is desired to keep the full
  4077. precision of all numbers input, the switch {\tt ADJPREC}\ttindex{ADJPREC}
  4078. (for {\em adjust precision\/}) can be turned on. While on, {\tt ADJPREC}
  4079. will automatically increase the precision, when necessary, to match that
  4080. of any integer or real input, and a message printed to inform the user of
  4081. the precision increase.
  4082. When {\tt ROUNDED} is on, rational numbers are normally converted to
  4083. rounded representation. However, if a user wishes to keep such numbers in
  4084. a rational form until used in an operation that returns a real number,
  4085. the switch {\tt ROUNDALL}\ttindex{ROUNDALL} can be turned off. This
  4086. switch is normally on.
  4087. Results from rounded calculations are returned in rounded form with two
  4088. exceptions: if the result is recognized as {\tt 0} or {\tt 1} to the
  4089. current precision, the integer result is returned.
  4090. \subsection{Modular Number Coefficients in Polynomials}\index{Coefficient}
  4091. \index{Modular coefficient}
  4092. {\REDUCE} includes facilities for manipulating polynomials whose
  4093. coefficients are computed modulo a given base. To use this option, two
  4094. commands must be used; {\tt SETMOD} {\tt <integer>},\ttindex{SETMOD} to set
  4095. the prime modulus, and {\tt ON MODULAR}\ttindex{MODULAR} to cause the
  4096. actual modular calculations to occur.
  4097. For example, with {\tt setmod 3;} and {\tt on modular;}, the polynomial
  4098. {\tt (a+2*b)\verb|^|3} would become {\tt A\verb|^|3+2*B\verb|^|3}.
  4099. The argument of {\tt SETMOD} is evaluated algebraically, except that
  4100. non-modular (integer) arithmetic is used. Thus the sequence
  4101. \begin{verbatim}
  4102. setmod 3; on modular; setmod 7;
  4103. \end{verbatim}
  4104. will correctly set the modulus to 7.
  4105. Modular numbers are by default represented by integers in the interval
  4106. [0,p-1] where p is the current modulus. Sometimes it is more convenient
  4107. to use an equivalent symmetric representation in the interval
  4108. [-p/2+1,p/2], or more precisely
  4109. [-floor((p-1)/2), ceiling((p-1)/2)],
  4110. especially if the modular numbers map objects that include
  4111. negative quantities. The switch {\tt BALANCED\_MOD}\ttindex{BALANCED\_MOD}
  4112. allows you to select the symmetric representation for output.
  4113. Users should note that the modular calculations are on the polynomial
  4114. coefficients only. It is not currently possible to reduce the exponents
  4115. since no check for a prime modulus is made (which would allow
  4116. $x^{p-1}$ to be reduced to 1 mod p). Note also that any division by a
  4117. number not co-prime with the modulus will result in the error ``Invalid
  4118. modular division''.
  4119. \subsection{Complex Number Coefficients in Polynomials}\index{Coefficient}
  4120. \index{Complex coefficient}
  4121. Although {\REDUCE} routinely treats the square of the variable {\em i\/} as
  4122. equivalent to $-1$, this is not sufficient to reduce expressions involving
  4123. {\em i\/} to lowest terms, or to factor such expressions over the complex
  4124. numbers. For example, in the default case,
  4125. \begin{verbatim}
  4126. factorize(a^2+1);
  4127. \end{verbatim}
  4128. gives the result
  4129. \begin{verbatim}
  4130. {{A**2+1,1}}
  4131. \end{verbatim}
  4132. and
  4133. \begin{verbatim}
  4134. (a^2+b^2)/(a+i*b)
  4135. \end{verbatim}
  4136. is not reduced further. However, if the switch
  4137. {\tt COMPLEX}\ttindex{COMPLEX} is turned on, full complex arithmetic is then
  4138. carried out. In other words, the above factorization will give the result
  4139. \begin{verbatim}
  4140. {{A + I,1},{A - I,1}}
  4141. \end{verbatim}
  4142. and the quotient will be reduced to {\tt A-I*B}.
  4143. The switch {\tt COMPLEX} may be combined with {\tt ROUNDED} to give complex
  4144. real numbers; the appropriate arithmetic is performed in this case.
  4145. Complex conjugation is used to remove complex numbers from denominators of
  4146. expressions. To do this if {\tt COMPLEX} is off, you must turn the switch
  4147. {\tt RATIONALIZE}\ttindex{RATIONALIZE} on.
  4148. \chapter{Substitution Commands}\index{Substitution}
  4149. An important class of commands in {\REDUCE} define
  4150. substitutions for variables and expressions to be made during the
  4151. evaluation of expressions. Such substitutions use the prefix operator
  4152. {\tt SUB}, various forms of the command {\tt LET}, and rule sets.
  4153. \section{SUB Operator}\ttindex{SUB}
  4154. Syntax:
  4155. \begin{verbatim}
  4156. SUB(<substitution_list>,EXPRN1:algebraic):algebraic
  4157. \end{verbatim}
  4158. where {\tt <substitution\_list>} is a list of one or more equations of the
  4159. form
  4160. \begin{verbatim}
  4161. VAR:kernel=EXPRN:algebraic
  4162. \end{verbatim}
  4163. or a kernel that evaluates to such a list.
  4164. The {\tt SUB} operator gives the algebraic result of replacing every
  4165. occurrence of the variable {\tt VAR} in the expression {\tt EXPRN1} by the
  4166. expression {\tt EXPRN}. Specifically, {\tt EXPRN1} is first evaluated
  4167. using all available rules. Next the substitutions are made, and finally
  4168. the substituted expression is reevaluated. When more than one variable
  4169. occurs in the substitution list, the substitution is performed by
  4170. recursively walking down the tree representing {\tt EXPRN1}, and replacing
  4171. every {\tt VAR} found by the appropriate {\tt EXPRN}. The {\tt EXPRN} are
  4172. not themselves searched for any occurrences of the various {\tt VAR}s.
  4173. The trivial case {\tt SUB(EXPRN1)} returns the algebraic value of
  4174. {\tt EXPRN1}.
  4175. {\it Examples:}
  4176. \begin{verbatim}
  4177. 2 2
  4178. sub({x=a+y,y=y+1},x^2+y^2) -> A + 2*A*Y + 2*Y + 2*Y + 1
  4179. \end{verbatim}
  4180. and with {\tt s := \{x=a+y,y=y+1\}},
  4181. \begin{verbatim}
  4182. 2 2
  4183. sub(s,x^2+y^2) -> A + 2*A*Y + 2*Y + 2*Y + 1
  4184. \end{verbatim}
  4185. Note that the global assignments {\tt x:=a+y}, etc., do not take place.
  4186. {\tt EXPRN1} can be any valid algebraic expression whose type is such that
  4187. a substitution process is defined for it (e.g., scalar expressions, lists
  4188. and matrices). An error will occur if an expression of an invalid type
  4189. for substitution occurs either in {\tt EXPRN} or {\tt EXPRN1}.
  4190. The braces around the substitution list may also be omitted, as in:
  4191. \begin{verbatim}
  4192. 2 2
  4193. sub(x=a+y,y=y+1,x^2+y^2) -> A + 2*A*Y + 2*Y + 2*Y + 1
  4194. \end{verbatim}
  4195. \section{LET Rules}\ttindex{LET}
  4196. Unlike substitutions introduced via {\tt SUB}, {\tt LET}
  4197. rules are global in scope and stay in effect until replaced or {\tt CLEAR}ed.
  4198. The simplest use of the {\tt LET} statement is in the form
  4199. \begin{verbatim}
  4200. LET <substitution list>
  4201. \end{verbatim}
  4202. where {\tt <substitution list>} is a list of rules separated by commas, each
  4203. of the form:
  4204. \begin{verbatim}
  4205. <variable> = <expression>
  4206. \end{verbatim}
  4207. or
  4208. \begin{verbatim}
  4209. <prefix operator>(<argument>,...,<argument>) = <expression>
  4210. \end{verbatim}
  4211. or
  4212. \begin{verbatim}
  4213. <argument> <infix operator>,..., <argument> = <expression>
  4214. \end{verbatim}
  4215. For example,
  4216. \begin{verbatim}
  4217. let {x => y^2,
  4218. h(u,v) => u - v,
  4219. cos(pi/3) => 1/2,
  4220. a*b => c,
  4221. l+m => n,
  4222. w^3 => 2*z - 3,
  4223. z^10 => 0}
  4224. \end{verbatim}
  4225. The list brackets can be left out if preferred. The above rules could
  4226. also have been entered as seven separate {\tt LET} statements.
  4227. After such {\tt LET} rules have been input, {\tt X} will always be
  4228. evaluated as the square of {\tt Y}, and so on. This is so even if at the
  4229. time the {\tt LET} rule was input, the variable {\tt Y} had a value other
  4230. than {\tt Y}. (In contrast, the assignment {\tt x:=y\verb|^|2} will set {\tt X}
  4231. equal to the square of the current value of {\tt Y}, which could be quite
  4232. different.)
  4233. The rule {\tt let a*b=c} means that whenever {\tt A} and {\tt B} are both
  4234. factors in an expression their product will be replaced by {\tt C}. For
  4235. example, {\tt a\verb|^|5*b\verb|^|7*w} would be replaced by
  4236. {\tt c\verb|^|5*b\verb|^|2*w}.
  4237. The rule for {\tt l+m} will not only replace all occurrences of {\tt l+m}
  4238. by {\tt N}, but will also normally replace {\tt L} by {\tt n-m}, but not
  4239. {\tt M} by {\tt n-l}. A more complete description of this case is given
  4240. in Section~\ref{sec-gensubs}.
  4241. The rule pertaining to {\tt w\verb|^|3} will apply to any power of {\tt W}
  4242. greater than or equal to the third.
  4243. Note especially the last example, {\tt let z\verb|^|10=0}. This declaration
  4244. means, in effect: ignore the tenth or any higher power of {\tt Z}. Such
  4245. declarations, when appropriate, often speed up a computation to a
  4246. considerable degree. (See\index{Asymptotic command}
  4247. Section~\ref{sec-asymp} for more details.)
  4248. Any new operators occurring in such {\tt LET} rules will be automatically
  4249. declared {\tt OPERATOR} by the system, if the rules are being read from a
  4250. file. If they are being entered interactively, the system will ask
  4251. {\tt DECLARE} ... {\tt OPERATOR?} . Answer {\tt Y} or {\tt N} and hit
  4252. \key{Return}.
  4253. In each of these examples, substitutions are only made for the explicit
  4254. expressions given; i.e., none of the variables may be considered arbitrary
  4255. in any sense. For example, the command
  4256. \begin{verbatim}
  4257. let h(u,v) = u - v;
  4258. \end{verbatim}
  4259. will cause {\tt h(u,v)} to evaluate to {\tt U - V}, but will not affect
  4260. {\tt h(u,z)} or {\tt H} with any arguments other than precisely the
  4261. symbols {\tt U,V}.
  4262. These simple {\tt LET} rules are on the same logical level as assignments
  4263. made with the := operator. An assignment {\tt x := p+q} cancels a rule
  4264. {\tt let x = y\verb|^|2} made earlier, and vice versa.
  4265. {\it CAUTION:} A recursive rule such as
  4266. \begin{verbatim}
  4267. let x = x + 1;
  4268. \end{verbatim}
  4269. is erroneous, since any subsequent evaluation of {\tt X} would lead to a
  4270. non-terminating chain of substitutions:
  4271. \begin{verbatim}
  4272. x -> x + 1 -> (x + 1) + 1 -> ((x + 1) + 1) + 1 -> ...
  4273. \end{verbatim}
  4274. Similarly, coupled substitutions such as
  4275. \begin{verbatim}
  4276. let l = m + n, n = l + r;
  4277. \end{verbatim}
  4278. would lead to the same error. As a result, if you try to evaluate an {\tt X},
  4279. {\tt L} or {\tt N} defined as above, you will get an error such as
  4280. \begin{verbatim}
  4281. X improperly defined in terms of itself
  4282. \end{verbatim}
  4283. Array and matrix elements can appear on the left-hand side of a {\tt LET}
  4284. statement. However, because of their {\em instant evaluation\/}
  4285. \index{Instant evaluation} property, it is the value of the element that
  4286. is substituted for, rather than the element itself. E.g.,
  4287. \begin{verbatim}
  4288. array a(5);
  4289. a(2) := b;
  4290. let a(2) = c;
  4291. \end{verbatim}
  4292. results in {\tt B} being substituted by {\tt C}; the assignment for
  4293. {\tt a(2)} does not change.
  4294. Finally, if an error occurs in any equation in a {\tt LET} statement
  4295. (including generalized statements involving {\tt FOR ALL} and {\tt SUCH
  4296. THAT)}, the remaining rules are not evaluated.
  4297. \subsection{FOR ALL \ldots LET}\ttindex{FOR ALL}
  4298. If a substitution for all possible values of a given argument of an
  4299. operator is required, the declaration {\tt FOR ALL} may be used. The
  4300. syntax of such a command is
  4301. \begin{verbatim}
  4302. FOR ALL <variable>,...,<variable>
  4303. <LET statement> <terminator>
  4304. \end{verbatim}
  4305. e.g.,
  4306. \begin{verbatim}
  4307. for all x,y let h(x,y) = x-y;
  4308. for all x let k(x,y) = x^y;
  4309. \end{verbatim}
  4310. The first of these declarations would cause {\tt h(a,b)} to be evaluated
  4311. as {\tt A-B}, {\tt h(u+v,u+w)} to be {\tt V-W}, etc. If the operator
  4312. symbol {\tt H} is used with more or fewer argument places, not two, the
  4313. {\tt LET} would have no effect, and no error would result.
  4314. The second declaration would cause {\tt k(a,y)} to be evaluated as
  4315. {\tt a\verb|^|y}, but would have no effect on {\tt k(a,z)} since the rule
  4316. didn't say {\tt FOR ALL Y} ... .
  4317. Where we used {\tt X} and {\tt Y} in the examples, any variables could
  4318. have been used. This use of a variable doesn't affect the value it may
  4319. have outside the {\tt LET} statement. However, you should remember what
  4320. variables you actually used. If you want to delete the rule subsequently,
  4321. you must use the same variables in the {\tt CLEAR} command.
  4322. It is possible to use more complicated expressions as a template for a
  4323. {\tt LET} statement, as explained in the section on substitutions for
  4324. general expressions. In nearly all cases, the rule will be accepted, and
  4325. a consistent application made by the system. However, if there is a sole
  4326. constant or a sole free variable on the left-hand side of a rule (e.g.,
  4327. {\tt let 2=3} or {\tt for all x let x=2)}, then the system is unable to
  4328. handle the rule, and the error message
  4329. \begin{verbatim}
  4330. Substitution for ... not allowed
  4331. \end{verbatim}
  4332. will be issued. Any variable listed in the {\tt FOR ALL} part will have
  4333. its symbol preceded by an equal sign: {\tt X} in the above example will
  4334. appear as {\tt =X}. An error will also occur if a variable in the
  4335. {\tt FOR ALL} part is not properly matched on both sides of the {\tt LET}
  4336. equation.
  4337. \subsection{FOR ALL \ldots SUCH THAT \ldots LET}
  4338. \ttindex{FOR ALL}\ttindex{SUCH THAT}
  4339. If a substitution is desired for more than a single value of a variable in
  4340. an operator or other expression, but not all values, a conditional form of
  4341. the {\tt FOR ALL \ldots LET} declaration can be used.
  4342. {\it Example:}
  4343. \begin{verbatim}
  4344. for all x such that numberp x and x<0 let h(x)=0;
  4345. \end{verbatim}
  4346. will cause {\tt h(-5)} to be evaluated as 0, but {\tt H} of a positive
  4347. integer, or of an argument that is not an integer at all, would not be
  4348. affected. Any boolean expression can follow the {\tt SUCH THAT} keywords.
  4349. \subsection{Removing Assignments and Substitution Rules}\ttindex{CLEAR}
  4350. The user may remove all assignments and substitution rules from any
  4351. expression by the command {\tt CLEAR}, in the form
  4352. \begin{verbatim}
  4353. CLEAR <expression>,...,<expression><terminator>
  4354. \end{verbatim}
  4355. e.g.
  4356. \begin{verbatim}
  4357. clear x, h(x,y);
  4358. \end{verbatim}
  4359. Because of their {\em instant evaluation\/} property, array and matrix elements
  4360. cannot be cleared with {\tt CLEAR}. For example, if {\tt A} is an array,
  4361. you must say
  4362. \begin{verbatim}
  4363. a(3) := 0;
  4364. \end{verbatim}
  4365. rather than
  4366. \begin{verbatim}
  4367. clear a(3);
  4368. \end{verbatim}
  4369. to ``clear'' element {\tt a(3)}.
  4370. On the other hand, a whole array (or matrix) {\tt A} can be cleared by the
  4371. command {\tt clear a}; This means much more than resetting to 0 all the
  4372. elements of {\tt A}. The fact that {\tt A} is an array, and what its
  4373. dimensions are, are forgotten, so {\tt A} can be redefined as another type
  4374. of object, for example an operator.
  4375. The more general types of {\tt LET} declarations can also be deleted by
  4376. using {\tt CLEAR}. Simply repeat the {\tt LET} rule to be deleted, using
  4377. {\tt CLEAR} in place of {\tt LET}, and omitting the equal sign and
  4378. right-hand part. The same dummy variables must be used in the {\tt FOR
  4379. ALL} part, and the boolean expression in the {\tt SUCH THAT} part must be
  4380. written the same way. (The placing of blanks doesn't have to be
  4381. identical.)
  4382. {\it Example:} The {\tt LET} rule
  4383. \begin{verbatim}
  4384. for all x such that numberp x and x<0 let h(x)=0;
  4385. \end{verbatim}
  4386. can be erased by the command
  4387. \begin{verbatim}
  4388. for all x such that numberp x and x<0 clear h(x);
  4389. \end{verbatim}
  4390. \subsection{Overlapping LET Rules}
  4391. {\tt CLEAR} is not the only way to delete a {\tt LET} rule. A new {\tt
  4392. LET} rule identical to the first, but with a different expression after
  4393. the equal sign, replaces the first. Replacements are also made in other
  4394. cases where the existing rule would be in conflict with the new rule. For
  4395. example, a rule for {\tt x\verb|^|4} would replace a rule for {\tt x\verb|^|5}.
  4396. The user should however be cautioned against having several {\tt LET}
  4397. rules in effect that relate to the same expression. No guarantee can be
  4398. given as to which rules will be applied by {\REDUCE} or in what order. It
  4399. is best to {\tt CLEAR} an old rule before entering a new related {\tt LET}
  4400. rule.
  4401. \subsection{Substitutions for General Expressions}
  4402. \label{sec-gensubs}
  4403. The examples of substitutions discussed in other sections have involved
  4404. very simple rules. However, the substitution mechanism used in {\REDUCE} is
  4405. very general, and can handle arbitrarily complicated rules without
  4406. difficulty.
  4407. The general substitution mechanism used in {\REDUCE} is discussed in Hearn, A.
  4408. C., ``{\REDUCE}, A User-Oriented Interactive System for Algebraic
  4409. Simplification,'' Interactive Systems for Experimental Applied Mathematics,
  4410. (edited by M. Klerer and J. Reinfelds), Academic Press, New York (1968),
  4411. 79-90, and Hearn. A. C., ``The Problem of Substitution,'' Proc. 1968 Summer
  4412. Institute on Symbolic Mathematical Computation, IBM Programming Laboratory
  4413. Report FSC 69-0312 (1969). For the reasons given in these
  4414. references, {\REDUCE} does not attempt to implement a general pattern
  4415. matching algorithm. However, the present system uses far more sophisticated
  4416. techniques than those discussed in the above papers. It is now possible for
  4417. the rules appearing in arguments of {\tt LET} to have the form
  4418. \begin{verbatim}
  4419. <substitution expression> = <expression>
  4420. \end{verbatim}
  4421. where any rule to which a sensible meaning can be assigned is permitted.
  4422. However, this meaning can vary according to the form of {\tt <substitution
  4423. expression>}. The semantic rules associated with the application of the
  4424. substitution are completely consistent, but somewhat complicated by the
  4425. pragmatic need to perform such substitutions as efficiently as possible.
  4426. The following rules explain how the majority of the cases are handled.
  4427. To begin with, the {\tt <substitution expression>} is first partly
  4428. simplified by collecting like terms and putting identifiers (and kernels)
  4429. in the system order. However, no substitutions are performed on any part
  4430. of the expression with the exception of expressions with the {\em instant
  4431. evaluation\/} property, such as array and matrix elements, whose actual
  4432. values are used. It should also be noted that the system order used is
  4433. not changeable by the user, even with the {\tt KORDER} command. Specific
  4434. cases are then handled as follows:
  4435. \begin{enumerate}
  4436. \item If the resulting simplified rule has a left-hand side that is an
  4437. identifier, an expression with a top-level algebraic operator or a power,
  4438. then the rule is added without further change to the appropriate table.
  4439. \item If the operator * appears at the top level of the simplified left-hand
  4440. side, then any constant arguments in that expression are moved to the
  4441. right-hand side of the rule. The remaining left-hand side is then added
  4442. to the appropriate table. For example,
  4443. \begin{verbatim}
  4444. let 2*x*y=3
  4445. \end{verbatim}
  4446. becomes
  4447. \begin{verbatim}
  4448. let x*y=3/2
  4449. \end{verbatim}
  4450. so that {\tt x*y} is added to the product substitution table, and when
  4451. this rule is applied, the expression {\tt x*y} becomes 3/2, but {\tt X} or
  4452. {\tt Y} by themselves are not replaced.
  4453. \item If the operators {\tt +}, {\tt -} or {\tt /} appear at the top level
  4454. of the simplified left-hand side, all but the first term is moved to the
  4455. right-hand side of the rule. Thus the rules
  4456. \begin{verbatim}
  4457. let l+m=n, x/2=y, a-b=c
  4458. \end{verbatim}
  4459. become
  4460. \begin{verbatim}
  4461. let l=n-m, x=2*y, a=c+b.
  4462. \end{verbatim}
  4463. \end{enumerate}
  4464. One problem that can occur in this case is that if a quantified expression
  4465. is moved to the right-hand side, a given free variable might no longer
  4466. appear on the left-hand side, resulting in an error because of the
  4467. unmatched free variable. E.g.,
  4468. \begin{verbatim}
  4469. for all x,y let f(x)+f(y)=x*y
  4470. \end{verbatim}
  4471. would become
  4472. \begin{verbatim}
  4473. for all x,y let f(x)=x*y-f(y)
  4474. \end{verbatim}
  4475. which no longer has {\tt Y} on both sides.
  4476. The fact that array and matrix elements are evaluated in the left-hand side
  4477. of rules can lead to confusion at times. Consider for example the
  4478. statements
  4479. \begin{verbatim}
  4480. array a(5); let x+a(2)=3; let a(3)=4;
  4481. \end{verbatim}
  4482. The left-hand side of the first rule will become {\tt X}, and the second
  4483. 0. Thus the first rule will be instantiated as a substitution for
  4484. {\tt X}, and the second will result in an error.
  4485. The order in which a list of rules is applied is not easily understandable
  4486. without a detailed knowledge of the system simplification protocol. It is
  4487. also possible for this order to change from release to release, as improved
  4488. substitution techniques are implemented. Users should therefore assume
  4489. that the order of application of rules is arbitrary, and program
  4490. accordingly.
  4491. After a substitution has been made, the expression being evaluated is
  4492. reexamined in case a new allowed substitution has been generated. This
  4493. process is continued until no more substitutions can be made.
  4494. As mentioned elsewhere, when a substitution expression appears in a
  4495. product, the substitution is made if that expression divides the product.
  4496. For example, the rule
  4497. \begin{verbatim}
  4498. let a^2*c = 3*z;
  4499. \end{verbatim}
  4500. would cause {\tt a\verb|^|2*c*x} to be replaced by {\tt 3*Z*X} and
  4501. {\tt a\verb|^|2*c\verb|^|2} by {\tt 3*Z*C}. If the substitution is desired only
  4502. when the substitution expression appears in a product with the explicit
  4503. powers supplied in the rule, the command {\tt MATCH} should be used
  4504. instead.\ttindex{MATCH}
  4505. For example,
  4506. \begin{verbatim}
  4507. match a^2*c = 3*z;
  4508. \end{verbatim}
  4509. would cause {\tt a\verb|^|2*c*x} to be replaced by {\tt 3*Z*X}, but
  4510. {\tt a\verb|^|2*c\verb|^|2} would not be replaced. {\tt MATCH} can also be used
  4511. with the {\tt FOR ALL} constructions described above.
  4512. To remove substitution rules of the type discussed in this section, the
  4513. {\tt CLEAR}\ttindex{CLEAR} command can be used, combined, if necessary,
  4514. with the same {\tt FOR ALL} clause with which the rule was defined, for
  4515. example:
  4516. \begin{verbatim}
  4517. for all x clear log(e^x),e^log(x),cos(w*t+theta(x));
  4518. \end{verbatim}
  4519. Note, however, that the arbitrary variable names in this case {\em must\/}
  4520. be the same as those used in defining the substitution.
  4521. \section{Rule Lists} \index{Rule lists}
  4522. Rule lists offer an alternative approach to defining substitutions that is
  4523. different from either {\tt SUB} or {\tt LET}. In fact, they provide the
  4524. best features of both, since they have all the capabilities of {\tt LET},
  4525. but the rules can also be applied locally as is possible with {\tt SUB}.
  4526. In time, they will be used more and more in {\REDUCE}. However, since they
  4527. are relatively new, much of the {\REDUCE} code you see uses the older
  4528. constructs.
  4529. A rule list is a list of {\em rules\/} that have the syntax
  4530. \begin{verbatim}
  4531. <expression> => <expression> (WHEN <boolean expression>)
  4532. \end{verbatim}
  4533. For example,
  4534. \begin{verbatim}
  4535. {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  4536. cos(~n*pi) => (-1)^n when remainder(n,2)=0}
  4537. \end{verbatim}
  4538. The tilde preceding a variable marks that variable as {\em free\/} for that
  4539. rule, much as a variable in a {\tt FOR ALL} clause in a {\tt LET}
  4540. statement. The first occurrence of that variable in each relevant rule
  4541. must be so marked on input, otherwise inconsistent results can occur.
  4542. For example, the rule list
  4543. \begin{verbatim}
  4544. {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  4545. cos(x)^2 => (1+cos(2x))/2}
  4546. \end{verbatim}
  4547. designed to replace products of cosines, would not be correct, since the
  4548. second rule would only apply to the explicit argument {\tt X}. Later
  4549. occurrences in the same rule may also be marked, but this is optional
  4550. (internally, all such rules are stored with each relevant variable
  4551. explicitly marked). The optional {\tt WHEN}\ttindex{WHEN} clause allows
  4552. constraints to be placed on the application of the rule, much as the {\tt
  4553. SUCH THAT} clause in a {\tt LET} statement.
  4554. A rule list may be named, for example
  4555. \begin{verbatim}
  4556. trig1 := {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  4557. cos(~x)*sin(~y) => (sin(x+y)-sin(x-y))/2,
  4558. sin(~x)*sin(~y) => (cos(x-y)-cos(x+y))/2,
  4559. cos(~x)^2 => (1+cos(2*x))/2,
  4560. sin(~x)^2 => (1-cos(2*x))/2};
  4561. \end{verbatim}
  4562. Such named rule lists may be inspected as needed. E.g., the command
  4563. {\tt trig1;} would cause the above list to be printed.
  4564. Rule lists may be used in two ways. They can be globally instantiated by
  4565. means of the command {\tt LET}.\ttindex{LET} For example,
  4566. \begin{verbatim}
  4567. let trig1;
  4568. \end{verbatim}
  4569. would cause the above list of rules to be globally active from then on until
  4570. cancelled by the command {\tt CLEARRULES},\ttindex{CLEARRULES} as in
  4571. \begin{verbatim}
  4572. clearrules trig1;
  4573. \end{verbatim}
  4574. {\tt CLEARRULES} has the syntax
  4575. \begin{verbatim}
  4576. CLEARRULES <rule list>|<name of rule list>(,...) .
  4577. \end{verbatim}
  4578. The second way to use rule lists is to invoke them locally by means of a
  4579. {\tt WHERE}\ttindex{WHERE} clause. For example
  4580. \begin{verbatim}
  4581. cos(a)*cos(b+c)
  4582. where {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2};
  4583. \end{verbatim}
  4584. or
  4585. \begin{verbatim}
  4586. cos(a)*sin(b) where trigrules;
  4587. \end{verbatim}
  4588. The syntax of an expression with a {\tt WHERE} clause is:
  4589. \begin{verbatim}
  4590. <expression>
  4591. WHERE <rule>|<rule list>(,<rule>|<rule list> ...)
  4592. \end{verbatim}
  4593. so the first example above could also be written
  4594. \begin{verbatim}
  4595. cos(a)*cos(b+c)
  4596. where cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2;
  4597. \end{verbatim}
  4598. The effect of this construct is that the rule list(s) in the {\tt WHERE}
  4599. clause only apply to the expression on the left of {\tt WHERE}. They have
  4600. no effect outside the expression. In particular, they do not affect
  4601. previously defined {\tt WHERE} clauses or {\tt LET} statements. For
  4602. example, the sequence
  4603. \begin{verbatim}
  4604. let a=2;
  4605. a where a=>4;
  4606. a;
  4607. \end{verbatim}
  4608. would result in the output
  4609. \begin{verbatim}
  4610. 4
  4611. 2
  4612. \end{verbatim}
  4613. Although {\tt WHERE} has a precedence less than any other infix operator,
  4614. it still binds higher than keywords such as {\tt ELSE}, {\tt THEN},
  4615. {\tt DO}, {\tt REPEAT} and so on. Thus the expression
  4616. \begin{verbatim}
  4617. if a=2 then 3 else a+2 where a=3
  4618. \end{verbatim}
  4619. will parse as
  4620. \begin{verbatim}
  4621. if a=2 then 3 else (a+2 where a=3)
  4622. \end{verbatim}
  4623. {\tt WHERE} may be used to introduce auxiliary variables in symbolic mode
  4624. expressions, as described in Section~\ref{sec-lambda}. However, the
  4625. symbolic mode use has different semantics, so expressions do not carry
  4626. from one mode to the other.
  4627. \COMPATNOTE In order to provide compatibility with older versions of rule
  4628. lists released through the Network Library, it is currently possible to use
  4629. an equal sign interchangeably with the replacement sign {\tt =>} in rules
  4630. and {\tt LET} statements. However, since this will change in future
  4631. versions, the replacement sign is preferable in rules and the equal sign
  4632. in non-rule-based {\tt LET} statements.
  4633. \subsection*{Advanced Use of Rule Lists}
  4634. Some advanced features of the rule list mechanism make it possible to
  4635. write more complicated rules than those discussed so far, and in many
  4636. cases to write more compact rule lists. These features are:
  4637. \begin{itemize}
  4638. \item Free operators
  4639. \item Double slash operator
  4640. \item Double tilde variables.
  4641. \end{itemize}
  4642. A {\bf free operator} in the left hand side of a pattern will match any
  4643. operator with the same number of arguments. The free operator is written
  4644. in the same style as a variable. For example, the implementation of the
  4645. product rule of differentiation can be written as:
  4646. \begin{verbatim}
  4647. operator diff, !~f, !~g;
  4648. prule := {diff(~f(~x) * ~g(~x),x) =>
  4649. diff(f(x),x) * g(x) + diff(g(x),x) * f(x)};
  4650. let prule;
  4651. diff(sin(z)*cos(z),z);
  4652. cos(z)*diff(sin(z),z) + diff(cos(z),z)*sin(z)
  4653. \end{verbatim}
  4654. The {\bf double slash operator} may be used as an alternative to a single
  4655. slash (quotient) in order to match quotients properly. E.g., in the
  4656. example of the Gamma function above, one can use:
  4657. \begin{verbatim}
  4658. gammarule :=
  4659. {gamma(~z)//(~c*gamma(~zz)) => gamma(z)/(c*gamma(zz-1)*zz)
  4660. when fixp(zz -z) and (zz -z) >0,
  4661. gamma(~z)//gamma(~zz) => gamma(z)/(gamma(zz-1)*zz)
  4662. when fixp(zz -z) and (zz -z) >0};
  4663. let gammarule;
  4664. gamma(z)/gamma(z+3);
  4665. 1
  4666. ----------------------
  4667. 3 2
  4668. z + 6*z + 11*z + 6
  4669. \end{verbatim}
  4670. The above example suffers from the fact that two rules had to be
  4671. written in order to perform the required operation. This can be simplified
  4672. by the use of {\bf double tilde variables}. E.g. the rule list
  4673. \begin{verbatim}
  4674. GGrule := {
  4675. gamma(~z)//(~~c*gamma(~zz)) => gamma(z)/(c*gamma(zz-1)*zz)
  4676. when fixp(zz -z) and (zz -z) >0};
  4677. \end{verbatim}
  4678. will implement the same operation in a much more compact way.
  4679. In general, double tilde variables are bound to the neutral element
  4680. with respect to the operation in which they are used.
  4681. \begin{tabular}{lll}
  4682. Pattern given & Argument used & Binding \\
  4683. \\
  4684. \symbol{126}z + \symbol{126}\symbol{126}y & x & z=x; y=0 \\
  4685. \symbol{126}z + \symbol{126}\symbol{126}y & x+3 & z=x; y=3 or z=3; y=x \\
  4686. \\
  4687. \symbol{126}z * \symbol{126}\symbol{126}y & x & z=x; y=1\\
  4688. \symbol{126}z * \symbol{126}\symbol{126}y & x*3 & z=x; y=3 or z=3; y=x\\
  4689. \\
  4690. \symbol{126}z / \symbol{126}\symbol{126}y & x & z=x; y=1\\
  4691. \symbol{126}z / \symbol{126}\symbol{126}y & x/3 & z=x; y=3 \\
  4692. \\
  4693. \end{tabular}
  4694. Remarks: A double tilde variable as the numerator of a pattern is not allowed.
  4695. Also, using double tilde variables may lead to recursion errors when the
  4696. zero case is not handled properly.
  4697. \begin{verbatim}
  4698. let f(~~a * ~x,x) => a * f(x,x) when freeof (a,x);
  4699. f(z,z);
  4700. ***** f(z,z) improperly defined in terms of itself
  4701. % BUT:
  4702. let ff(~~a * ~x,x)
  4703. => a * ff(x,x) when freeof (a,x) and a neq 1;
  4704. ff(z,z);
  4705. ff(z,z)
  4706. ff(3*z,z);
  4707. 3*ff(z,z)
  4708. \end{verbatim}
  4709. \subsection*{Displaying Rules Associated with an Operator}
  4710. The operator {\tt SHOWRULES}\ttindex{SHOWRULES} takes a single identifier
  4711. as argument, and returns in rule-list form the operator rules associated
  4712. with that argument. For example:
  4713. \begin{verbatim}
  4714. showrules log;
  4715. {LOG(E) => 1,
  4716. LOG(1) => 0,
  4717. ~X
  4718. LOG(E ) => ~X,
  4719. 1
  4720. DF(LOG(~X),~X) => ----}
  4721. ~X
  4722. \end{verbatim}
  4723. Such rules can then be manipulated further as with any list. For example
  4724. {\tt rhs first ws;} has the value {\tt 1}. Note that an operator may
  4725. have other properties that cannot be displayed in such a form, such as the
  4726. fact it is an odd function, or has a definition defined as a procedure.
  4727. \subsection*{Order of Application of Rules}
  4728. If rules have overlapping domains, their order of application is
  4729. important. In general, it is very difficult to specify this order
  4730. precisely, so that it is best to assume that the order is arbitrary.
  4731. However, if only one operator is involved, the order of application of the
  4732. rules for this operator can be determined from the following:
  4733. \begin{enumerate}
  4734. \item Rules containing at least one free variable apply before all rules
  4735. without free variables.
  4736. \item Rules activated in the most recent {\tt LET}
  4737. command are applied first.
  4738. \item {\tt LET} with several entries generate
  4739. the same order of application as a corresponding sequence of commands with
  4740. one rule or rule set each.
  4741. \item Within a rule set, the rules containing at least
  4742. one free variable are applied in their given order.
  4743. In other words, the first member of the list is applied first.
  4744. \item Consistent with the first item, any rule in a rule list that
  4745. contains no free variables is applied after all rules containing free
  4746. variables.
  4747. \end{enumerate}
  4748. {\it Example:} The following rule set enables the computation of exact
  4749. values of the Gamma function:
  4750. \begin{verbatim}
  4751. operator gamma,gamma_error;
  4752. gamma_rules :=
  4753. {gamma(~x)=>sqrt(pi)/2 when x=1/2,
  4754. gamma(~n)=>factorial(n-1) when fixp n and n>0,
  4755. gamma(~n)=>gamma_error(n) when fixp n,
  4756. gamma(~x)=>(x-1)*gamma(x-1) when fixp(2*x) and x>1,
  4757. gamma(~x)=>gamma(x+1)/x when fixp(2*x)};
  4758. \end{verbatim}
  4759. Here, rule by rule, cases of known or definitely uncomputable values
  4760. are sorted out; e.g. the rule leading to the error expression
  4761. will be applied for negative integers only, since the positive
  4762. integers are caught by the preceding rule, and the
  4763. last rule will apply for negative odd multiples of $1/2$ only.
  4764. Alternatively the first rule could have been written as
  4765. \begin{verbatim}
  4766. gamma(1/2) => sqrt(pi)/2,
  4767. \end{verbatim}
  4768. but then the case $x=1/2$ should be excluded in the {\tt WHEN} part of the
  4769. last rule explicitly because a rule without free variables cannot take
  4770. precedence over the other rules.
  4771. \section{Asymptotic Commands} \index{Asymptotic command}
  4772. \label{sec-asymp}
  4773. In expansions of polynomials involving variables that are known to be
  4774. small, it is often desirable to throw away all powers of these variables
  4775. beyond a certain point to avoid unnecessary computation. The command {\tt
  4776. LET} may be used to do this. For example, if only powers of {\tt X} up to
  4777. {\tt x\verb|^|7} are needed, the command
  4778. \begin{verbatim}
  4779. let x^8 = 0;
  4780. \end{verbatim}
  4781. will cause the system to delete all powers of {\tt X} higher than 7.
  4782. {\it CAUTION:} This particular simplification works differently from most
  4783. substitution mechanisms in {\REDUCE} in that it is applied during
  4784. polynomial manipulation rather than to the whole evaluated expression.
  4785. Thus, with the above rule in effect, {\tt x\verb|^|10/x\verb|^|5} would give the
  4786. result zero, since the numerator would simplify to zero. Similarly
  4787. {\tt x\verb|^|20/x\verb|^|10} would give a {\tt Zero divisor} error message,
  4788. since both numerator and denominator would first simplify to zero.
  4789. The method just described is not adequate when expressions involve several
  4790. variables having different degrees of smallness. In this case, it is
  4791. necessary to supply an asymptotic weight to each variable and count up the
  4792. total weight of each product in an expanded expression before deciding
  4793. whether to keep the term or not. There are two associated commands in the
  4794. system to permit this type of asymptotic constraint. The command {\tt WEIGHT}
  4795. \ttindex{WEIGHT}
  4796. takes a list of equations of the form
  4797. \begin{verbatim}
  4798. <kernel form> = <number>
  4799. \end{verbatim}
  4800. where {\tt <number>} must be a positive integer (not just evaluate to a
  4801. positive integer). This command assigns the weight {\tt <number>} to the
  4802. relevant kernel form. A check is then made in all algebraic evaluations
  4803. to see if the total weight of the term is greater than the weight level
  4804. assigned to the calculation. If it is, the term is deleted. To compute
  4805. the total weight of a product, the individual weights of each kernel form
  4806. are multiplied by their corresponding powers and then added.
  4807. The weight level of the system is initially set to 1. The user may change
  4808. this setting by the command\ttindex{WTLEVEL}
  4809. \begin{verbatim}
  4810. wtlevel <number>;
  4811. \end{verbatim}
  4812. which sets {\tt <number>} as the new weight level of the system.
  4813. {\tt <number>} must evaluate to a positive integer. WTLEVEL will also
  4814. allow NIL as an argument, in which case the current weight level is returned.
  4815. \chapter{File Handling Commands}\index{File handling}
  4816. In many applications, it is desirable to load previously prepared {\REDUCE}
  4817. files into the system, or to write output on other files. {\REDUCE} offers
  4818. four commands for this purpose, namely, {\tt IN}, {\tt OUT}, {\tt SHUT},
  4819. {\tt LOAD}, and {\tt LOAD\_PACKAGE}. The first\ttindex{IN}\ttindex{OUT}
  4820. \ttindex{SHUT} three operators are described here; {\tt LOAD} and {\tt
  4821. LOAD\_PACKAGE} are discussed in Section~\ref{sec-load}.
  4822. \section{IN Command}\ttindex{IN}
  4823. This command takes a list of file names as argument and directs the system
  4824. to input\index{Input} each file (that should contain {\REDUCE} statements
  4825. and commands) into the system. File names can either be an identifier or
  4826. a string. The explicit format of these will be system dependent and, in
  4827. many cases, site dependent. The explicit instructions for the
  4828. implementation being used should therefore be consulted for further
  4829. details. For example:
  4830. \begin{verbatim}
  4831. in f1,"ggg.rr.s";
  4832. \end{verbatim}
  4833. will first load file {\tt F1}, then {\tt ggg.rr.s}. When a semicolon is
  4834. used as the terminator of the IN statement, the statements in the file are
  4835. echoed on the terminal or written on the current output file. If \$
  4836. \index{Command terminator} is used as the terminator, the input is not
  4837. shown. Echoing of all or part of the input file can be prevented, even if
  4838. a semicolon was used, by placing an {\tt off echo;}\ttindex{ECHO} command
  4839. in the input file.
  4840. Files to be read using {\tt IN} should end with {\tt ;END;}. Note the two
  4841. semicolons! First of all, this is protection against obscure difficulties
  4842. the user will have if there are, by mistake, more {\tt BEGIN}s than
  4843. {\tt END}s on the file. Secondly, it triggers some file control book-keeping
  4844. which may improve system efficiency. If {\tt END} is omitted, an error
  4845. message {\tt "End-of-file read"} will occur.
  4846. \section{OUT Command}\ttindex{OUT}
  4847. This command takes a single file name as argument, and directs output to
  4848. that file from then on, until another {\tt OUT} changes the output file,
  4849. or {\tt SHUT} closes it. Output can go to only one file at a time,
  4850. although many can be open. If the file has previously been used for
  4851. output during the current job, and not {\tt SHUT},\ttindex{SHUT} the new
  4852. output is appended to the end of the file. Any existing file is erased
  4853. before its first use for output in a job, or if it had been {\tt SHUT}
  4854. before the new {\tt OUT}.
  4855. To output on the terminal without closing the output file, the reserved
  4856. file name T (for terminal) may be used. For example,
  4857. {\tt out ofile;} will direct output to the file {\tt OFILE} and
  4858. {\tt out t;} will direct output to the user's terminal.
  4859. The output sent to the file will be in the same form that it would have on
  4860. the terminal. In particular {\tt x\verb|^|2} would appear on two lines, an
  4861. {\tt X} on the lower line and a 2 on the line above. If the purpose of the
  4862. output file is to save results to be read in later, this is not an
  4863. appropriate form. We first must turn off the {\tt NAT} switch that
  4864. specifies that output should be in standard mathematical notation.
  4865. {\it Example:} To create a file {\tt ABCD} from which it will be possible
  4866. to read -- using {\tt IN} -- the value of the expression {\tt XYZ}:
  4867. \begin{verbatim}
  4868. off echo$ % needed if your input is from a file.
  4869. off nat$ % output in IN-readable form. Each expression
  4870. % printed will end with a $ .
  4871. out abcd$ % output to new file
  4872. linelength 72$ % for systems with fixed input line length.
  4873. xyz:=xyz; % will output "XYZ := " followed by the value
  4874. % of XYZ
  4875. write ";end"$ % standard for ending files for IN
  4876. shut abcd$ % save ABCD, return to terminal output
  4877. on nat$ % restore usual output form
  4878. \end{verbatim}
  4879. \section{SHUT Command}\ttindex{SHUT}
  4880. This command takes a list of names of files that have been previously
  4881. opened via an {\tt OUT} statement and closes them. Most systems require this
  4882. action by the user before he ends the {\REDUCE} job (if not sooner),
  4883. otherwise the output may be lost. If a file is shut and a further {\tt OUT}
  4884. command issued for the same file, the file is erased before the new output
  4885. is written.
  4886. If it is the current output file that is shut, output will switch to the
  4887. terminal. Attempts to shut files that have not been opened by {\tt OUT},
  4888. or an input file, will lead to errors.
  4889. \chapter{Commands for Interactive Use}\index{Interactive use}
  4890. {\REDUCE} is designed as an interactive system, but naturally it can also
  4891. operate in a batch processing or background mode by taking its input
  4892. command by command from the relevant input stream. There is a basic
  4893. difference, however, between interactive and batch use of the system. In
  4894. the former case, whenever the system discovers an ambiguity at some point
  4895. in a calculation, such as a forgotten type assignment for instance, it asks
  4896. the user for the correct interpretation. In batch operation, it is not
  4897. practical to terminate the calculation at such points and require
  4898. resubmission of the job, so the system makes the most obvious guess of the
  4899. user's intentions and continues the calculation.
  4900. There is also a difference in the handling of errors. In the former case,
  4901. the computation can continue since the user has the opportunity to correct
  4902. the mistake. In batch mode, the error may lead to consequent erroneous
  4903. (and possibly time consuming) computations. So in the default case, no
  4904. further evaluation occurs, although the remainder of the input is checked
  4905. for syntax errors. A message {\tt "Continuing with parsing only"}
  4906. informs the user that this is happening. On the other hand, the switch
  4907. {\tt ERRCONT},\ttindex{ERRCONT} if on, will cause the system to continue
  4908. evaluating expressions after such errors occur.
  4909. When a syntactical error occurs, the place where the system detected the
  4910. error is marked with three dollar signs (\$\$\$). In interactive mode, the
  4911. user can then use {\tt ED}\ttindex{ED} to correct the error, or retype the
  4912. command. When a non-syntactical error occurs in interactive mode, the
  4913. command being evaluated at the time the last error occurred is saved, and
  4914. may later be reevaluated by the command {\tt RETRY}.\ttindex{RETRY}
  4915. \section{Referencing Previous Results}
  4916. It is often useful to be able to reference results of previous
  4917. computations during a {\REDUCE} session. For this purpose, {\REDUCE}
  4918. maintains a history\index{History} of all interactive inputs and the
  4919. results of all interactive computations during a given session. These
  4920. results are referenced by the command number that {\REDUCE} prints
  4921. automatically in interactive mode. To use an input expression in a new
  4922. computation, one writes {\tt input(}$n${\tt )},\ttindex{INPUT} where
  4923. $n$ is the command number. To use an output expression, one writes {\tt
  4924. WS(}$n${\tt )}.\ttindex{WS} {\tt WS} references the previous command.
  4925. E.g., if command number 1 was {\tt INT(X-1,X)}; and the result of command
  4926. number 7 was {\tt X-1}, then
  4927. \begin{verbatim}
  4928. 2*input(1)-ws(7)^2;
  4929. \end{verbatim}
  4930. would give the result {\tt -1}, whereas
  4931. \begin{verbatim}
  4932. 2*ws(1)-ws(7)^2;
  4933. \end{verbatim}
  4934. would yield the same result, but {\em without\/} a recomputation of the
  4935. integral.
  4936. The operator {\tt DISPLAY}\ttindex{DISPLAY} is available to display previous
  4937. inputs. If its argument is a positive integer, {\it n} say, then the
  4938. previous n inputs are displayed. If its argument is {\tt ALL} (or in fact
  4939. any non-numerical expression), then all previous inputs are displayed.
  4940. \section{Interactive Editing}
  4941. It is possible when working interactively to edit any {\REDUCE} input that
  4942. comes from the user's terminal, and also some user-defined procedure
  4943. definitions. At the top level, one can access any previous command string
  4944. by the command {\tt ed(}$n${\tt )},\ttindex{ED} where n is the desired
  4945. command number as prompted by the system in interactive mode. {\tt ED};
  4946. (i.e. no argument) accesses the previous command.
  4947. After {\tt ED} has been called, you can now edit the displayed string using a
  4948. string editor with the following commands:
  4949. \begin{tabular}{lp{\rboxwidth}}
  4950. {\tt~~~~~ B} & move pointer to beginning \\
  4951. {\tt~~~~~ C<character>} & replace next character by
  4952. {\em character} \\
  4953. {\tt~~~~~ D} & delete next character \\
  4954. {\tt~~~~~ E} & end editing and reread text \\
  4955. {\tt~~~~~ F<character>} & move pointer to next
  4956. occurrence of {\em character} \\[1.7pt]
  4957. {\tt~~~~~ I<string><escape>} &
  4958. insert {\em string\/} in front of pointer \\
  4959. {\tt~~~~~ K<character>} & delete all characters
  4960. until {\em character} \\
  4961. {\tt~~~~~ P} & print string from current pointer \\
  4962. {\tt~~~~~ Q} & give up with error exit \\
  4963. {\tt~~~~~ S<string><escape>} &
  4964. search for first occurrence of {\em string},
  4965. positioning pointer just before it \\
  4966. {\tt~~~~~ space} or {\tt X} & move pointer right
  4967. one character.
  4968. \end{tabular}
  4969. The above table can be displayed online by typing a question mark followed
  4970. by a carriage return to the editor. The editor prompts with an angle
  4971. bracket. Commands can be combined on a single line, and all command
  4972. sequences must be followed by a carriage return to become effective.
  4973. Thus, to change the command {\tt x := a+1;} to {\tt x := a+2}; and cause
  4974. it to be executed, the following edit command sequence could be used:
  4975. \begin{verbatim}
  4976. f1c2e<return>.
  4977. \end{verbatim}
  4978. The interactive editor may also be used to edit a user-defined procedure that
  4979. has not been compiled. To do this, one says:
  4980. \ttindex{EDITDEF}
  4981. \begin{verbatim}
  4982. editdef <id>;
  4983. \end{verbatim}
  4984. where {\tt <id>} is the name of the procedure. The procedure definition
  4985. will then be displayed in editing mode, and may then be edited and
  4986. redefined on exiting from the editor.
  4987. Some versions of {\REDUCE} now include input editing that uses the
  4988. capabilities of modern window systems. Please consult your system
  4989. dependent documentation to see if this is possible. Such editing
  4990. techniques are usually much easier to use then {\tt ED} or {\tt EDITDEF}.
  4991. \section{Interactive File Control}
  4992. If input is coming from an external file, the system treats it as a batch
  4993. processed calculation. If the user desires interactive
  4994. \index{Interactive use} response in this case, he can include the command
  4995. {\tt on int};\ttindex{INT} in the file. Likewise, he can issue the
  4996. command {\tt off int}; in the main program if he does not desire continual
  4997. questioning from the system. Regardless of the setting of {\tt INT},
  4998. input commands from a file are not kept in the system, and so cannot be
  4999. edited using {\tt ED}. However, many implementations of {\REDUCE} provide
  5000. a link to an external system editor that can be used for such editing.
  5001. The specific instructions for the particular implementation should be
  5002. consulted for information on this.
  5003. Two commands are available in {\REDUCE} for interactive use of files. {\tt
  5004. PAUSE};\ttindex{PAUSE} may be inserted at any point in an input file. When
  5005. this command is encountered on input, the system prints the message {\tt
  5006. CONT?} on the user's terminal and halts. If the user responds {\tt Y}
  5007. (for yes), the calculation continues from that point in the file. If the
  5008. user responds {\tt N} (for no), control is returned to the terminal, and
  5009. the user can input further statements and commands. Later on he can use
  5010. the command {\tt cont;}\ttindex{CONT} to transfer control back to the
  5011. point in the file following the last {\tt PAUSE} encountered. A top-level
  5012. {\tt pause;}\ttindex{PAUSE} from the user's terminal has no effect.
  5013. \chapter{Matrix Calculations} \index{Matrix calculations}
  5014. A very powerful feature of {\REDUCE} is the ease with which matrix
  5015. calculations can be performed. To extend our syntax to this class of
  5016. calculations we need to add another prefix operator, {\tt MAT},
  5017. \ttindex{MAT} and a further
  5018. variable and expression type as follows:
  5019. \section{MAT Operator}\ttindex{MAT}
  5020. This prefix operator is used to represent $n\times m$ matrices. {\tt
  5021. MAT} has {\em n} arguments interpreted as rows of the matrix, each of
  5022. which is a list of {\em m} expressions representing elements in that row.
  5023. For example, the matrix
  5024. \[ \left( \begin{array}{lcr} a & b & c \\ d & e & f \end{array} \right) \]
  5025. would be written as {\tt mat((a,b,c),(d,e,f))}.
  5026. Note that the single column matrix
  5027. \[ \left( \begin{array}{c} x \\ y \end{array} \right) \]
  5028. becomes {\tt mat((x),(y))}. The inside parentheses are required to
  5029. distinguish it from the single row matrix
  5030. \[ \left( \begin{array}{lr} x & y \end{array} \right) \]
  5031. that would be written as {\tt mat((x,y))}.
  5032. \section{Matrix Variables}
  5033. An identifier may be declared a matrix variable by the declaration {\tt
  5034. MATRIX}.\ttindex{MATRIX}
  5035. The size of the matrix may be declared explicitly in the matrix
  5036. declaration, or by default in assigning such a variable to a matrix
  5037. expression. For example,
  5038. \begin{verbatim}
  5039. matrix x(2,1),y(3,4),z;
  5040. \end{verbatim}
  5041. declares {\tt X} to be a 2 x 1 (column) matrix, {\tt Y} to be a 3 x 4
  5042. matrix and {\tt Z} a matrix whose size is to be declared later.
  5043. Matrix declarations can appear anywhere in a program. Once a symbol is
  5044. declared to name a matrix, it can not also be used to name an array,
  5045. operator or a procedure, or used as an ordinary variable. It can however
  5046. be redeclared to be a matrix, and its size may be changed at that time.
  5047. Note however that matrices once declared are {\em global\/} in scope, and so
  5048. can then be referenced anywhere in the program. In other words, a
  5049. declaration within a block (or a procedure) does not limit the scope of
  5050. the matrix to that block, nor does the matrix go away on exiting the block
  5051. (use {\tt CLEAR} instead for this purpose). An element of a matrix is
  5052. referred to in the expected manner; thus {\tt x(1,1)} gives the first
  5053. element of the matrix {\tt X} defined above. References to elements of a
  5054. matrix whose size has not yet been declared leads to an error. All
  5055. elements of a matrix whose size is declared are initialized to 0. As a
  5056. result, a matrix element has an {\em instant evaluation\/}\index{Instant
  5057. evaluation} property and cannot stand for itself. If this is required,
  5058. then an operator should be used to name the matrix elements as in:
  5059. \begin{verbatim}
  5060. matrix m; operator x; m := mat((x(1,1),x(1,2));
  5061. \end{verbatim}
  5062. \section{Matrix Expressions}
  5063. These follow the normal rules of matrix algebra as defined by the
  5064. following syntax:\ttindex{MAT}
  5065. \begin{verbatim}
  5066. <matrix expression> ::=
  5067. MAT<matrix description>|<matrix variable>|
  5068. <scalar expression>*<matrix expression>|
  5069. <matrix expression>*<matrix expression>
  5070. <matrix expression>+<matrix expression>|
  5071. <matrix expression>^<integer>|
  5072. <matrix expression>/<matrix expression>
  5073. \end{verbatim}
  5074. Sums and products of matrix expressions must be of compatible size;
  5075. otherwise an error will result during their evaluation. Similarly, only
  5076. square matrices may be raised to a power. A negative power is computed as
  5077. the inverse of the matrix raised to the corresponding positive power.
  5078. {\tt a/b} is interpreted as {\tt a*b\verb|^|(-1)}.
  5079. {\it Examples:}
  5080. Assuming {\tt X} and {\tt Y} have been declared as matrices, the following
  5081. are matrix expressions
  5082. \begin{verbatim}
  5083. y
  5084. y^2*x-3*y^(-2)*x
  5085. y + mat((1,a),(b,c))/2
  5086. \end{verbatim}
  5087. The computation of the quotient of two matrices normally uses a two-step
  5088. elimination method due to Bareiss. An alternative method using Cramer's
  5089. method is also available. This is usually less efficient than the Bareiss
  5090. method unless the matrices are large and dense, although we have no solid
  5091. statistics on this as yet. To use Cramer's method instead, the switch
  5092. {\tt CRAMER}\ttindex{CRAMER} should be turned on.
  5093. \section{Operators with Matrix Arguments}
  5094. The operator {\tt LENGTH}\ttindex{LENGTH} applied to a matrix returns a
  5095. list of the number of rows and columns in the matrix. Other operators
  5096. useful in matrix calculations are defined in the following subsections.
  5097. Attention is also drawn to the LINALG
  5098. \extendedmanual{(chapter~\ref{LINALG})} and NORMFORM
  5099. \extendedmanual{(chapter~\ref{NORMFORM})} packages.
  5100. \subsection{DET Operator}\ttindex{DET}
  5101. Syntax:
  5102. \begin{verbatim}
  5103. DET(EXPRN:matrix_expression):algebraic.
  5104. \end{verbatim}
  5105. The operator {\tt DET} is used to represent the determinant of a square
  5106. matrix expression. E.g.,
  5107. \begin{verbatim}
  5108. det(y^2)
  5109. \end{verbatim}
  5110. is a scalar expression whose value is the determinant of the square of the
  5111. matrix {\tt Y}, and
  5112. \begin{verbatim}
  5113. det mat((a,b,c),(d,e,f),(g,h,j));
  5114. \end{verbatim}
  5115. is a scalar expression whose value is the determinant of the matrix
  5116. \[ \left( \begin{array}{lcr} a & b & c \\ d & e & f \\ g & h & j
  5117. \end{array} \right) \]
  5118. Determinant expressions have the {\em instant evaluation\/} property.
  5119. \index{Instant evaluation} In other words, the statement
  5120. \begin{verbatim}
  5121. let det mat((a,b),(c,d)) = 2;
  5122. \end{verbatim}
  5123. sets the {\em value\/} of the determinant to 2, and does not set up a rule
  5124. for the determinant itself.
  5125. \subsection{MATEIGEN Operator}\ttindex{MATEIGEN}
  5126. Syntax:
  5127. \begin{verbatim}
  5128. MATEIGEN(EXPRN:matrix_expression,ID):list.
  5129. \end{verbatim}
  5130. {\tt MATEIGEN} calculates the eigenvalue equation and the corresponding
  5131. eigenvectors of a matrix, using the variable {\tt ID} to denote the
  5132. eigenvalue. A square free decomposition of the characteristic polynomial
  5133. is carried out. The result is a list of lists of 3 elements, where the
  5134. first element is a square free factor of the characteristic polynomial,
  5135. the second its multiplicity and the third the corresponding eigenvector
  5136. (as an {\em n} by 1 matrix). If the square free decomposition was
  5137. successful, the product of the first elements in the lists is the minimal
  5138. polynomial. In the case of degeneracy, several eigenvectors can exist for
  5139. the same eigenvalue, which manifests itself in the appearance of more than
  5140. one arbitrary variable in the eigenvector. To extract the various parts
  5141. of the result use the operations defined on lists.
  5142. {\it Example:}
  5143. The command
  5144. \begin{verbatim}
  5145. mateigen(mat((2,-1,1),(0,1,1),(-1,1,1)),eta);
  5146. \end{verbatim}
  5147. gives the output
  5148. \begin{verbatim}
  5149. {{ETA - 1,2,
  5150. [ARBCOMPLEX(1)]
  5151. [ ]
  5152. [ARBCOMPLEX(1)]
  5153. [ ]
  5154. [ 0 ]
  5155. },
  5156. {ETA - 2,1,
  5157. [ 0 ]
  5158. [ ]
  5159. [ARBCOMPLEX(2)]
  5160. [ ]
  5161. [ARBCOMPLEX(2)]
  5162. }}
  5163. \end{verbatim}
  5164. \subsection{TP Operator}\ttindex{TP}
  5165. Syntax:
  5166. \begin{verbatim}
  5167. TP(EXPRN:matrix_expression):matrix.
  5168. \end{verbatim}
  5169. This operator takes a single matrix argument and returns its transpose.
  5170. \subsection{Trace Operator}\ttindex{TRACE}
  5171. Syntax:
  5172. \begin{verbatim}
  5173. TRACE(EXPRN:matrix_expression):algebraic.
  5174. \end{verbatim}
  5175. The operator {\tt TRACE} is used to represent the trace of a square matrix.
  5176. \subsection{Matrix Cofactors}\ttindex{COFACTOR}
  5177. Syntax:
  5178. \begin{verbatim}
  5179. COFACTOR(EXPRN:matrix_expression,ROW:integer,COLUMN:integer):
  5180. algebraic
  5181. \end{verbatim}
  5182. The operator {\tt COFACTOR} returns the cofactor of the element in row
  5183. {\tt ROW} and column {\tt COLUMN} of the matrix {\tt MATRIX}. Errors occur
  5184. if {\tt ROW} or {\tt COLUMN} do not simplify to integer expressions or if
  5185. {\tt MATRIX} is not square.
  5186. \subsection{NULLSPACE Operator}\ttindex{NULLSPACE}
  5187. Syntax:
  5188. \begin{verbatim}
  5189. NULLSPACE(EXPRN:matrix_expression):list
  5190. \end{verbatim}
  5191. {\tt NULLSPACE} calculates for a matrix {\tt A} a list of linear
  5192. independent vectors (a basis) whose linear combinations satisfy the
  5193. equation $A x = 0$. The basis is provided in a form such that as many
  5194. upper components as possible are isolated.
  5195. Note that with {\tt b := nullspace a} the expression {\tt length b} is the
  5196. {\em nullity\/} of A, and that {\tt second length a - length b} calculates the
  5197. {\em rank\/} of A. The rank of a matrix expression can also be found more
  5198. directly by the {\tt RANK} operator described below.
  5199. {\it Example:} The command
  5200. \begin{verbatim}
  5201. nullspace mat((1,2,3,4),(5,6,7,8));
  5202. \end{verbatim}
  5203. gives the output
  5204. \begin{verbatim}
  5205. {
  5206. [ 1 ]
  5207. [ ]
  5208. [ 0 ]
  5209. [ ]
  5210. [ - 3]
  5211. [ ]
  5212. [ 2 ]
  5213. ,
  5214. [ 0 ]
  5215. [ ]
  5216. [ 1 ]
  5217. [ ]
  5218. [ - 2]
  5219. [ ]
  5220. [ 1 ]
  5221. }
  5222. \end{verbatim}
  5223. In addition to the {\REDUCE} matrix form, {\tt NULLSPACE} accepts as input a
  5224. matrix given as a list of lists, that is interpreted as a row matrix. If
  5225. that form of input is chosen, the vectors in the result will be
  5226. represented by lists as well. This additional input syntax facilitates
  5227. the use of {\tt NULLSPACE} in applications different from classical linear
  5228. algebra.
  5229. \subsection{RANK Operator}\ttindex{RANK}
  5230. Syntax:
  5231. \begin{verbatim}
  5232. RANK(EXPRN:matrix_expression):integer
  5233. \end{verbatim}
  5234. {\tt RANK} calculates the rank of its argument, that, like {\tt NULLSPACE}
  5235. can either be a standard matrix expression, or a list of lists, that can
  5236. be interpreted either as a row matrix or a set of equations.
  5237. {\tt Example:}
  5238. \begin{verbatim}
  5239. rank mat((a,b,c),(d,e,f));
  5240. \end{verbatim}
  5241. returns the value 2.
  5242. \section{Matrix Assignments} \index{Matrix assignment}
  5243. Matrix expressions may appear in the right-hand side of assignment
  5244. statements. If the left-hand side of the assignment, which must be a
  5245. variable, has not already been declared a matrix, it is declared by default
  5246. to the size of the right-hand side. The variable is then set to the value
  5247. of the right-hand side.
  5248. Such an assignment may be used very conveniently to find the solution of a
  5249. set of linear equations. For example, to find the solution of the
  5250. following set of equations
  5251. \begin{verbatim}
  5252. a11*x(1) + a12*x(2) = y1
  5253. a21*x(1) + a22*x(2) = y2
  5254. \end{verbatim}
  5255. we simply write
  5256. \begin{verbatim}
  5257. x := 1/mat((a11,a12),(a21,a22))*mat((y1),(y2));
  5258. \end{verbatim}
  5259. \section{Evaluating Matrix Elements}
  5260. Once an element of a matrix has been assigned, it may be referred to in
  5261. standard array element notation. Thus {\tt y(2,1)} refers to the element
  5262. in the second row and first column of the matrix {\tt Y}.
  5263. \chapter{Procedures}\ttindex{PROCEDURE}
  5264. It is often useful to name a statement for repeated use in calculations
  5265. with varying parameters, or to define a complete evaluation procedure for
  5266. an operator. {\REDUCE} offers a procedural declaration for this purpose. Its
  5267. general syntax is:
  5268. \begin{verbatim}
  5269. [<procedural type>] PROCEDURE <name>[<varlist>];<statement>;
  5270. \end{verbatim}
  5271. where
  5272. \begin{verbatim}
  5273. <varlist> ::= (<variable>,...,<variable>)
  5274. \end{verbatim}
  5275. This will be explained more fully in the following sections.
  5276. In the algebraic mode of {\REDUCE} the {\tt <procedure type>} can be
  5277. omitted, since the default is {\tt ALGEBRAIC}. Procedures of type {\tt
  5278. INTEGER} or {\tt REAL} may also be used. In the former case, the system
  5279. checks that the value of the procedure is an integer. At present, such
  5280. checking is not done for a real procedure, although this will change in
  5281. the future when a more complete type checking mechanism is installed.
  5282. Users should therefore only use these types when appropriate. An empty
  5283. variable list may also be omitted.
  5284. All user-defined procedures are automatically declared to be operators.
  5285. In order to allow users relatively easy access to the whole {\REDUCE} source
  5286. program, system procedures are not protected against user redefinition. If
  5287. a procedure is redefined, a message
  5288. \begin{verbatim}
  5289. *** <procedure name> REDEFINED
  5290. \end{verbatim}
  5291. is printed. If this occurs, and the user is not redefining his own
  5292. procedure, he is well advised to rename it, and possibly start over
  5293. (because he has {\em already\/} redefined some internal procedure whose correct
  5294. functioning may be required for his job!)
  5295. All required procedures should be defined at the top level, since they
  5296. have global scope throughout a program. In particular, an attempt to
  5297. define a procedure within a procedure will cause an error to occur.
  5298. \section{Procedure Heading}\index{Procedure heading}
  5299. Each procedure has a heading consisting of the word {\tt PROCEDURE}
  5300. (optionally preceded by the word {\tt ALGEBRAIC}), followed by the name of
  5301. the procedure to be defined, and followed by its formal parameters -- the
  5302. symbols that will be used in the body of the definition to illustrate
  5303. what is to be done. There are three cases:
  5304. \begin{enumerate}
  5305. \item No parameters. Simply follow the procedure name with a terminator
  5306. (semicolon or dollar sign).
  5307. \begin{verbatim}
  5308. procedure abc;
  5309. \end{verbatim}
  5310. When such a procedure is used in an expression or command, {\tt abc()}, with
  5311. empty parentheses, must be written.
  5312. \item One parameter. Enclose it in parentheses {\em or\/} just leave at
  5313. least one space, then follow with a terminator.
  5314. \begin{verbatim}
  5315. procedure abc(x);
  5316. \end{verbatim}
  5317. or
  5318. \begin{verbatim}
  5319. procedure abc x;
  5320. \end{verbatim}
  5321. \item More than one parameter. Enclose them in parentheses, separated by
  5322. commas, then follow with a terminator.
  5323. \begin{verbatim}
  5324. procedure abc(x,y,z);
  5325. \end{verbatim}
  5326. \end{enumerate}
  5327. Referring to the last example, if later in some expression being evaluated
  5328. the symbols {\tt abc(u,p*q,123)} appear, the operations of the procedure
  5329. body will be carried out as if {\tt X} had the same value as {\tt U} does,
  5330. {\tt Y} the same value as {\tt p*q} does, and {\tt Z} the value 123. The
  5331. values of {\tt X}, {\tt Y}, {\tt Z}, after the procedure body operations
  5332. are completed are unchanged. So, normally, are the values of {\tt U},
  5333. {\tt P}, {\tt Q}, and (of course) 123. (This is technically referred to as
  5334. call by value.)\index{Call by value}
  5335. The reader will have noted the word {\em normally\/} a few lines earlier. The
  5336. call by value protections can be bypassed if necessary, as described
  5337. elsewhere.
  5338. \section{Procedure Body}\index{Procedure body}
  5339. Following the delimiter that ends the procedure heading must be a {\em
  5340. single} statement defining the action to be performed or the value to be
  5341. delivered. A terminator must follow the statement. If it is a semicolon,
  5342. the name of the procedure just defined is printed. It is not printed if a
  5343. dollar sign is used.
  5344. If the result wanted is given by a formula of some kind, the body is just
  5345. that formula, using the variables in the procedure heading.
  5346. {\it Simple Example:}
  5347. If {\tt f(x)} is to mean {\tt (x+5)*(x+6)/(x+7)}, the entire procedure
  5348. definition could read
  5349. \begin{verbatim}
  5350. procedure f x; (x+5)*(x+6)/(x+7);
  5351. \end{verbatim}
  5352. Then {\tt f(10)} would evaluate to 240/17, {\tt f(a-6)} to
  5353. {\tt A*(A-1)/(A+1)}, and so on.
  5354. {\it More Complicated Example:}
  5355. Suppose we need a function {\tt p(n,x)} that, for any positive integer
  5356. {\tt N}, is the Legendre polynomial\index{Legendre polynomials} of order
  5357. {\em n}. We can define this operator using the
  5358. textbook formula defining these functions:
  5359. \begin{displaymath}
  5360. p_n(x) = \displaystyle{1\over{n!}}\
  5361. \displaystyle{d^n\over dy^n}\ \displaystyle{{1\over{(y^2 - 2xy + 1)
  5362. ^{{1\over2}}}}}\Bigg\vert_{y=0}
  5363. \end{displaymath}
  5364. Put into words, the Legendre polynomial $p_n(x)$ is the result of
  5365. substituting $y=0$ in the $n^{th}$ partial derivative with respect to $y$
  5366. of a certain fraction involving $x$ and $y$, then dividing that by $n!$.
  5367. This verbal formula can easily be written in {\REDUCE}:
  5368. \begin{verbatim}
  5369. procedure p(n,x);
  5370. sub(y=0,df(1/(y^2-2*x*y+1)^(1/2),y,n))
  5371. /(for i:=1:n product i);
  5372. \end{verbatim}
  5373. Having input this definition, the expression evaluation
  5374. \begin{verbatim}
  5375. 2p(2,w);
  5376. \end{verbatim}
  5377. would result in the output
  5378. \begin{verbatim}
  5379. 2
  5380. 3*W - 1 .
  5381. \end{verbatim}
  5382. If the desired process is best described as a series of steps, then a group
  5383. or compound statement can be used.
  5384. \extendedmanual{\newpage}
  5385. {\it Example:}
  5386. The above Legendre polynomial example can be rewritten as a series of steps
  5387. instead of a single formula as follows:
  5388. \begin{verbatim}
  5389. procedure p(n,x);
  5390. begin scalar seed,deriv,top,fact;
  5391. seed:=1/(y^2 - 2*x*y +1)^(1/2);
  5392. deriv:=df(seed,y,n);
  5393. top:=sub(y=0,deriv);
  5394. fact:=for i:=1:n product i;
  5395. return top/fact
  5396. end;
  5397. \end{verbatim}
  5398. Procedures may also be defined recursively. In other words, the procedure
  5399. body\index{Procedure body} can include references to the procedure name
  5400. itself, or to other procedures that themselves reference the given
  5401. procedure. As an example, we can define the Legendre polynomial through
  5402. its standard recurrence relation:
  5403. \begin{verbatim}
  5404. procedure p(n,x);
  5405. if n<0 then rederr "Invalid argument to P(N,X)"
  5406. else if n=0 then 1
  5407. else if n=1 then x
  5408. else ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n;
  5409. \end{verbatim}
  5410. The operator {\tt REDERR}\ttindex{REDERR} in the above example provides
  5411. for a simple error exit from an algebraic procedure (and also a block).
  5412. It can take a string as argument.
  5413. It should be noted however that all the above definitions of {\tt p(n,x)} are
  5414. quite inefficient if extensive use is to be made of such polynomials, since
  5415. each call effectively recomputes all lower order polynomials. It would be
  5416. better to store these expressions in an array, and then use say the
  5417. recurrence relation to compute only those polynomials that have not already
  5418. been derived. We leave it as an exercise for the reader to write such a
  5419. definition.
  5420. \section{Using LET Inside Procedures}
  5421. By using {\tt LET}\ttindex{LET} instead of an assignment in the procedure
  5422. body\index{Procedure body} it is possible to bypass the call-by-value
  5423. \index{Call by value} protection. If {\tt X} is a formal parameter or local
  5424. variable of the procedure (i.e. is in the heading or in a local
  5425. declaration), and {\tt LET} is used instead of {\tt :=} to make an
  5426. assignment to {\tt X}, e.g.
  5427. \begin{verbatim}
  5428. let x = 123;
  5429. \end{verbatim}
  5430. then it is the variable that is the value of {\tt X} that is changed.
  5431. This effect also occurs with local variables defined in a block. If the
  5432. value of {\tt X} is not a variable, but a more general expression, then it
  5433. is that expression that is used on the left-hand side of the {\tt LET}
  5434. statement. For example, if {\tt X} had the value {\tt p*q}, it is as if
  5435. {\tt let p*q = 123} had been executed.
  5436. \section{LET Rules as Procedures}
  5437. The {\tt LET}\ttindex{LET} statement offers an alternative syntax and
  5438. semantics for procedure definition.
  5439. In place of
  5440. \begin{verbatim}
  5441. procedure abc(x,y,z); <procedure body>;
  5442. \end{verbatim}
  5443. one can write
  5444. \begin{verbatim}
  5445. for all x,y,z let abc(x,y,z) = <procedure body>;
  5446. \end{verbatim}
  5447. There are several differences to note.
  5448. If the procedure body contains an assignment to one of the formal
  5449. parameters, e.g.
  5450. \begin{verbatim}
  5451. x := 123;
  5452. \end{verbatim}
  5453. in the {\tt PROCEDURE} case it is a variable holding a copy of the first
  5454. actual argument that is changed. The actual argument is not changed.
  5455. In the {\tt LET} case, the actual argument is changed. Thus, if {\tt ABC}
  5456. is defined using {\tt LET}, and {\tt abc(u,v,w)} is evaluated, the value
  5457. of {\tt U} changes to 123. That is, the {\tt LET} form of definition
  5458. allows the user to bypass the protections that are enforced by the call
  5459. by value conventions of standard {\tt PROCEDURE} definitions.
  5460. {\it Example:} We take our earlier {\tt FACTORIAL}\ttindex{FACTORIAL}
  5461. procedure and write it as a {\tt LET} statement.
  5462. \begin{verbatim}
  5463. for all n let factorial n =
  5464. begin scalar m,s;
  5465. m:=1; s:=n;
  5466. l1: if s=0 then return m;
  5467. m:=m*s;
  5468. s:=s-1;
  5469. go to l1
  5470. end;
  5471. \end{verbatim}
  5472. The reader will notice that we introduced a new local variable, {\tt S},
  5473. and set it equal to {\tt N}. The original form of the procedure contained
  5474. the statement {\tt n:=n-1;}. If the user asked for the value of {\tt
  5475. factorial(5)} then {\tt N} would correspond to, not just have the value
  5476. of, 5, and {\REDUCE} would object to trying to execute the statement
  5477. 5 := $5-1$.
  5478. If {\tt PQR} is a procedure with no parameters,
  5479. \begin{verbatim}
  5480. procedure pqr;
  5481. <procedure body>;
  5482. \end{verbatim}
  5483. it can be written as a {\tt LET} statement quite simply:
  5484. \begin{verbatim}
  5485. let pqr = <procedure body>;
  5486. \end{verbatim}
  5487. To call {\em procedure\/} {\tt PQR}, if defined in the latter form, the empty
  5488. parentheses would not be used: use {\tt PQR} not {\tt PQR()} where a call
  5489. on the procedure is needed.
  5490. The two notations for a procedure with no arguments can be combined. {\tt PQR}
  5491. can be defined in the standard {\tt PROCEDURE} form. Then a {\tt LET}
  5492. statement
  5493. \begin{verbatim}
  5494. let pqr = pqr();
  5495. \end{verbatim}
  5496. would allow a user to use {\tt PQR} instead of {\tt PQR()} in calling the
  5497. procedure.
  5498. A feature available with {\tt LET}-defined procedures and not with procedures
  5499. defined in the standard way is the possibility of defining partial
  5500. functions.\index{Function}
  5501. \begin{verbatim}
  5502. for all x such that numberp x let uvw(x)=<procedure body>;
  5503. \end{verbatim}
  5504. Now {\tt UVW} of an integer would be calculated as prescribed by the procedure
  5505. body, while {\tt UVW} of a general argument, such as {\tt Z} or {\tt p+q}
  5506. (assuming these evaluate to themselves) would simply stay {\tt uvw(z)}
  5507. or {\tt uvw(p+q)} as the case may be.
  5508. \section{REMEMBER Statement}\ttindex{REMEMBER}
  5509. Setting the remember option for an algebraic procedure by
  5510. \begin{verbatim}
  5511. REMEMBER (PROCNAME:procedure);
  5512. \end{verbatim}
  5513. saves all intermediate results of such procedure evaluations, including
  5514. recursive calls. Subsequent calls to the procedure can then be determined
  5515. from the saved results, and thus the number of evaluations (or the
  5516. complexity) can be reduced. This mode of evalation costs extra memory, of
  5517. course. In addition, the procedure must be free of side--effects.
  5518. The following examples show the effect of the remember statement
  5519. on two well--known examples.
  5520. \begin{samepage}
  5521. \begin{verbatim}
  5522. procedure H(n); % Hofstadter's function
  5523. if numberp n then
  5524. << cnn := cnn +1; % counts the calls
  5525. if n < 3 then 1 else H(n-H(n-1))+H(n-H(n-2))>>;
  5526. remember h;
  5527. > << cnn := 0; H(100); cnn>>;
  5528. 100
  5529. % H has been called 100 times only.
  5530. procedure A(m,n); % Ackermann function
  5531. if m=0 then n+1 else
  5532. if n=0 then A(m-1,1) else
  5533. A(m-1,A(m,n-1));
  5534. remember a;
  5535. A(3,3);
  5536. \end{verbatim}
  5537. \end{samepage}
  5538. \chapter{Symbolic Mode}\index{Symbolic mode}
  5539. At the system level, {\REDUCE} is based on a version of the programming
  5540. language Lisp\index{Lisp} known as {\em Standard Lisp\/} which is described
  5541. in J. Marti, Hearn, A. C., Griss, M. L. and Griss, C., ``Standard LISP
  5542. Report" SIGPLAN Notices, ACM, New York, 14, No 10 (1979) 48-68. We shall
  5543. assume in this section that the reader is familiar with the material in
  5544. that paper. This also assumes implicitly that the reader has a reasonable
  5545. knowledge about Lisp in general, say at the level of the LISP 1.5
  5546. Programmer's Manual (McCarthy, J., Abrahams, P. W., Edwards, D. J., Hart,
  5547. T. P. and Levin, M. I., ``LISP 1.5 Programmer's Manual'', M.I.T. Press,
  5548. 1965) or any of the books mentioned at the end of this section. Persons
  5549. unfamiliar with this material will have some difficulty understanding this
  5550. section.
  5551. Although {\REDUCE} is designed primarily for algebraic calculations, its
  5552. source language is general enough to allow for a full range of Lisp-like
  5553. symbolic calculations. To achieve this generality, however, it is
  5554. necessary to provide the user with two modes of evaluation, namely an
  5555. algebraic mode\index{Algebraic mode} and a symbolic mode.\index{Symbolic
  5556. mode} To enter symbolic mode, the user types {\tt symbolic;}
  5557. \ttindex{SYMBOLIC} (or {\tt lisp;})\ttindex{LISP} and to return to
  5558. algebraic mode one types {\tt algebraic;}.\ttindex{ALGEBRAIC}
  5559. Evaluations proceed differently in each mode so the user is advised to
  5560. check what mode he is in if a puzzling error arises. He can find his mode
  5561. by typing\ttindex{EVAL\_MODE}
  5562. \begin{verbatim}
  5563. eval_mode;
  5564. \end{verbatim}
  5565. The current mode will then be printed as {\tt ALGEBRAIC} or {\tt SYMBOLIC}.
  5566. Expression evaluation may proceed in either mode at any level of a
  5567. calculation, provided the results are passed from mode to mode in a
  5568. compatible manner. One simply prefixes the relevant expression by the
  5569. appropriate mode. If the mode name prefixes an expression at the top
  5570. level, it will then be handled as if the global system mode had been
  5571. changed for the scope of that particular calculation.
  5572. For example, if the current mode is {\tt ALGEBRAIC}, then the commands
  5573. \extendedmanual{\newpage}
  5574. \begin{verbatim}
  5575. symbolic car '(a);
  5576. x+y;
  5577. \end{verbatim}
  5578. will cause the first expression to be evaluated and printed in symbolic
  5579. mode and the second in algebraic mode. Only the second evaluation will
  5580. thus affect the expression workspace. On the other hand, the statement
  5581. \begin{verbatim}
  5582. x + symbolic car '(12);
  5583. \end{verbatim}
  5584. will result in the algebraic value {\tt X+12}.
  5585. The use of {\tt SYMBOLIC} (and equivalently {\tt ALGEBRAIC}) in this
  5586. manner is the same as any operator. That means that parentheses could be
  5587. omitted in the above examples since the meaning is obvious. In other
  5588. cases, parentheses must be used, as in
  5589. \begin{verbatim}
  5590. symbolic(x := 'a);
  5591. \end{verbatim}
  5592. Omitting the parentheses, as in
  5593. \begin{verbatim}
  5594. symbolic x := a;
  5595. \end{verbatim}
  5596. would be wrong, since it would parse as
  5597. \begin{verbatim}
  5598. symbolic(x) := a;
  5599. \end{verbatim}
  5600. For convenience, it is assumed that any operator whose {\em first\/} argument is
  5601. quoted is being evaluated in symbolic mode, regardless of the mode in
  5602. effect at that time. Thus, the first example above could be equally well
  5603. written:
  5604. \begin{verbatim}
  5605. car '(a);
  5606. \end{verbatim}
  5607. Except where explicit limitations have been made, most {\REDUCE} algebraic
  5608. constructions carry over into symbolic mode.\index{Symbolic mode}
  5609. However, there are some differences. First, expression evaluation now
  5610. becomes Lisp evaluation. Secondly, assignment statements are handled
  5611. differently, as we shall discuss shortly. Thirdly, local variables and array
  5612. elements are initialized to {\tt NIL} rather than {\tt 0}. (In fact, any
  5613. variables not explicitly declared {\tt INTEGER} are also initialized to
  5614. {\tt NIL} in algebraic mode, but the algebraic evaluator recognizes {\tt
  5615. NIL} as {\tt 0}.) Finally, function definitions follow the conventions of
  5616. Standard Lisp.
  5617. To begin with, we mention a few extensions to our basic syntax which are
  5618. designed primarily if not exclusively for symbolic mode.
  5619. \section{Symbolic Infix Operators}
  5620. There are three binary infix operators in {\REDUCE} intended for use in
  5621. symbolic mode, namely . {\tt (CONS), EQ and MEMQ}. The precedence of
  5622. these operators was given in another section.
  5623. \section{Symbolic Expressions}
  5624. These consist of scalar variables and operators and follow the normal
  5625. rules of the Lisp meta language.
  5626. {\it Examples:}
  5627. \begin{verbatim}
  5628. x
  5629. car u . reverse v
  5630. simp (u+v^2)
  5631. \end{verbatim}
  5632. \section{Quoted Expressions}\ttindex{QUOTE}
  5633. Because symbolic evaluation requires that each variable or expression has a
  5634. value, it is necessary to add to {\REDUCE} the concept of a quoted expression
  5635. by analogy with the Lisp {\tt QUOTE} function. This is provided by the single
  5636. quote mark {\tt '}. For example,
  5637. \begin{quote}
  5638. \begin{tabbing}
  5639. {\tt '(a b c)} \= represents the Lisp S-expression \= {\tt (quote (a b
  5640. c))}\kill
  5641. {\tt 'a} \> represents the Lisp S-expression \>
  5642. {\tt (quote a)} \\
  5643. {\tt '(a b c)} \> represents the Lisp S-expression \> {\tt (quote (a b c))}
  5644. \end{tabbing}
  5645. \end{quote}
  5646. Note, however, that strings are constants and therefore evaluate to
  5647. themselves in symbolic mode. Thus, to print the string {\tt "A String"}, one
  5648. would write
  5649. \begin{verbatim}
  5650. prin2 "A String";
  5651. \end{verbatim}
  5652. Within a quoted expression, identifier syntax rules are those of {\REDUCE}.
  5653. Thus {\tt (A~!.~~B)} is the list consisting of the three elements {\tt A},
  5654. {\tt .}, and {\tt B}, whereas {\tt (A . B)} is the dotted pair of {\tt A}
  5655. and {\tt B}.
  5656. \section{Lambda Expressions}\ttindex{LAMBDA}
  5657. \label{sec-lambda}
  5658. {\tt LAMBDA} expressions provide the means for constructing Lisp {\tt LAMBDA}
  5659. expressions in symbolic mode. They may not be used in algebraic mode.
  5660. Syntax:
  5661. \begin{verbatim}
  5662. <LAMBDA expression> ::=
  5663. LAMBDA <varlist><terminator><statement>
  5664. \end{verbatim}
  5665. where
  5666. \begin{verbatim}
  5667. <varlist> ::= (<variable>,...,<variable>)
  5668. \end{verbatim}
  5669. e.g.,
  5670. \begin{verbatim}
  5671. lambda (x,y); car x . cdr y;
  5672. \end{verbatim}
  5673. is equivalent to the Lisp {\tt LAMBDA} expression
  5674. \begin{verbatim}
  5675. (lambda (x y) (cons (car x) (cdr y)))
  5676. \end{verbatim}
  5677. The parentheses may be omitted in specifying the variable list if desired.
  5678. {\tt LAMBDA} expressions may be used in symbolic mode in place of prefix
  5679. operators, or as an argument of the reserved word {\tt FUNCTION}.
  5680. In those cases where a {\tt LAMBDA} expression is used to introduce local
  5681. variables to avoid recomputation, a {\tt WHERE} statement can also be
  5682. used. For example, the expression
  5683. \begin{verbatim}
  5684. (lambda (x,y); list(car x,cdr x,car y,cdr y))
  5685. (reverse u,reverse v)
  5686. \end{verbatim}
  5687. can also be written
  5688. \begin{verbatim}
  5689. {car x,cdr x,car y,cdr y} where x=reverse u,y=reverse v
  5690. \end{verbatim}
  5691. Where possible, {\tt WHERE} syntax is preferred to {\tt LAMBDA} syntax,
  5692. since it is more natural.
  5693. \section{Symbolic Assignment Statements}\index{Assignment}
  5694. In symbolic mode, if the left side of an assignment statement is a
  5695. variable, a {\tt SETQ} of the right-hand side to that variable occurs. If
  5696. the left-hand side is an expression, it must be of the form of an array
  5697. element, otherwise an error will result. For example, {\tt x:=y}
  5698. translates into {\tt (SETQ X Y)} whereas {\tt a(3) := 3} will be valid if
  5699. {\tt A} has been previously declared a single dimensioned array of at
  5700. least four elements.
  5701. \section{FOR EACH Statement}\ttindex{FOR EACH}
  5702. The {\tt FOR EACH} form of the {\tt FOR} statement, designed for iteration
  5703. down a list, is more general in symbolic mode. Its syntax is:
  5704. \begin{verbatim}
  5705. FOR EACH ID:identifier {IN|ON} LST:list
  5706. {DO|COLLECT|JOIN|PRODUCT|SUM} EXPRN:S-expr
  5707. \end{verbatim}
  5708. As in algebraic mode, if the keyword {\tt IN} is used, iteration is on
  5709. each element of the list. With {\tt ON}, iteration is on the whole list
  5710. remaining at each point in the iteration. As a result, we have the
  5711. following equivalence between each form of {\tt FOR EACH} and the various
  5712. mapping functions in Lisp:
  5713. \begin{center}
  5714. {\tt
  5715. \begin{tabular}{|l|lr r|} \hline
  5716. & DO & COLLECT & JOIN \\ \hline
  5717. IN & MAPC & MAPCAR & MAPCAN \\
  5718. ON & MAP & MAPLIST & MAPCON \\ \hline
  5719. \end{tabular}}
  5720. \end{center}
  5721. {\it Example:} To list each element of the list {\tt (a b c)}:
  5722. \begin{verbatim}
  5723. for each x in '(a b c) collect list x;
  5724. \end{verbatim}
  5725. \section{Symbolic Procedures}\index{Symbolic procedure}
  5726. All the functions described in the Standard Lisp Report are available to
  5727. users in symbolic mode. Additional functions may also be defined as
  5728. symbolic procedures. For example, to define the Lisp function {\tt ASSOC},
  5729. the following could be used:
  5730. \begin{verbatim}
  5731. symbolic procedure assoc(u,v);
  5732. if null v then nil
  5733. else if u = caar v then car v
  5734. else assoc(u, cdr v);
  5735. \end{verbatim}
  5736. If the default mode were symbolic, then {\tt SYMBOLIC} could be omitted in
  5737. the above definition. {\tt MACRO}s\ttindex{MACRO} may be defined by
  5738. prefixing the keyword {\tt PROCEDURE} by the word {\tt MACRO}.
  5739. (In fact, ordinary functions may be defined with the keyword {\tt EXPR}
  5740. \ttindex{EXPR} prefixing {\tt PROCEDURE} as was used in the Standard Lisp
  5741. Report.) For example, we could define a {\tt MACRO CONSCONS} by
  5742. \begin{verbatim}
  5743. symbolic macro procedure conscons l;
  5744. expand(cdr l,'cons);
  5745. \end{verbatim}
  5746. Another form of macro, the {\tt SMACRO}\ttindex{SMACRO} is also available.
  5747. These are described in the Standard Lisp Report. The Report also defines
  5748. a function type {\tt FEXPR}.\ttindex{FEXPR}
  5749. However, its use is discouraged since it is hard to implement efficiently,
  5750. and most uses can be replaced by macros. At the present time, there are
  5751. no {\tt FEXPR}s in the core REDUCE system.
  5752. \section{Standard Lisp Equivalent of Reduce Input}
  5753. A user can obtain the Standard Lisp equivalent of his {\REDUCE} input by
  5754. turning on the switch {\tt DEFN}\ttindex{DEFN} (for definition). The
  5755. system then prints the Lisp translation of his input but does not evaluate
  5756. it. Normal operation is resumed when {\tt DEFN} is turned off.
  5757. \section{Communicating with Algebraic Mode}\index{Mode communication}
  5758. One of the principal motivations for a user of the algebraic facilities of
  5759. {\REDUCE} to learn about symbolic mode\index{Symbolic mode} is that it
  5760. gives one access to a wider range of techniques than is possible in
  5761. algebraic mode\index{Algebraic mode} alone. For example, if a user
  5762. wishes to use parts of the system defined in the basic system source code,
  5763. or refine their algebraic code definitions to make them more efficient,
  5764. then it is necessary to understand the source language in fairly complete
  5765. detail. Moreover, it is also necessary to know a little more about the
  5766. way {\REDUCE} operates internally. Basically, {\REDUCE} considers
  5767. expressions in two forms: prefix form, which follow the normal Lisp rules
  5768. of function composition, and so-called canonical form, which uses a
  5769. completely different syntax.
  5770. Once these details are understood, the most critical problem faced by a
  5771. user is how to make expressions and procedures communicate between symbolic
  5772. and algebraic mode. The purpose of this section is to teach a user the
  5773. basic principles for this.
  5774. If one wants to evaluate an expression in algebraic mode, and then use
  5775. that expression in symbolic mode calculations, or vice versa, the easiest
  5776. way to do this is to assign a variable to that expression whose value is
  5777. easily obtainable in both modes. To facilitate this, a declaration {\tt
  5778. SHARE}\ttindex{SHARE} is available. {\tt SHARE} takes a list of
  5779. identifiers as argument, and marks these variables as having recognizable
  5780. values in both modes. The declaration may be used in either mode.
  5781. E.g.,
  5782. \begin{verbatim}
  5783. share x,y;
  5784. \end{verbatim}
  5785. says that {\tt X} and {\tt Y} will receive values to be used in both modes.
  5786. If a {\tt SHARE} declaration is made for a variable with a previously
  5787. assigned algebraic value, that value is also made available in symbolic
  5788. mode.
  5789. \subsection{Passing Algebraic Mode Values to Symbolic Mode}
  5790. If one wishes to work with parts of an algebraic mode
  5791. \index{Algebraic mode} expression in symbolic mode,\index{Symbolic mode}
  5792. one simply makes an assignment\index{Assignment} of a shared variable to
  5793. the relevant expression in algebraic mode. For example, if one wishes to
  5794. work with {\tt (a+b)\verb|^|2}, one would say, in algebraic mode:
  5795. \begin{verbatim}
  5796. x := (a+b)^2;
  5797. \end{verbatim}
  5798. assuming that {\tt X} was declared shared as above. If we now change to
  5799. symbolic mode and say
  5800. \begin{verbatim}
  5801. x;
  5802. \end{verbatim}
  5803. its value will be printed as a prefix form with the syntax:
  5804. \begin{verbatim}
  5805. (*SQ <standard quotient> T)
  5806. \end{verbatim}
  5807. This particular format reflects the fact that the algebraic mode processor
  5808. currently likes to transfer prefix forms from command to command, but
  5809. doesn't like to reconvert standard forms\index{Standard form} (which
  5810. represent polynomials) and standard quotients back to a true Lisp prefix
  5811. form for the expression (which would result in excessive computation). So
  5812. {\tt *SQ} is used to tell the algebraic processor that it is dealing with
  5813. a prefix form which is really a standard quotient\index{Standard
  5814. quotient} and the second argument ({\tt T} or {\tt NIL}) tells it whether
  5815. it needs further processing (essentially, an {\em already simplified\/}
  5816. flag).
  5817. So to get the true standard quotient form in symbolic mode, one needs
  5818. {\tt CADR} of the variable. E.g.,
  5819. \begin{verbatim}
  5820. z := cadr x;
  5821. \end{verbatim}
  5822. would store in {\tt Z} the standard quotient form for {\tt (a+b)\verb|^|2}.
  5823. Once you have this expression, you can now manipulate it as you wish. To
  5824. facilitate this, a standard set of selectors\index{Selector} and
  5825. constructors\index{Constructor} are available for getting at parts of the
  5826. form. Those presently defined are as follows:
  5827. \extendedmanual{\newpage}
  5828. \begin{center}
  5829. \vspace{10pt}
  5830. {\large REDUCE Selectors\par}
  5831. %\end{center}
  5832. %\begin{center}
  5833. \renewcommand{\arraystretch}{1.5}
  5834. \begin{tabular}{lp{\rboxwidth}}
  5835. {\tt DENR} & denominator of standard quotient \\
  5836. %
  5837. {\tt LC} & leading coefficient of polynomial \\
  5838. %
  5839. {\tt LDEG} & leading degree of polynomial \\
  5840. %
  5841. {\tt LPOW} & leading power of polynomial \\
  5842. %
  5843. {\tt LT} & leading term of polynomial \\
  5844. %
  5845. {\tt MVAR} & main variable of polynomial \\
  5846. %
  5847. {\tt NUMR} & numerator (of standard quotient) \\
  5848. %
  5849. {\tt PDEG} & degree of a power \\
  5850. %
  5851. {\tt RED} & reductum of polynomial \\
  5852. %
  5853. {\tt TC} & coefficient of a term \\
  5854. %
  5855. {\tt TDEG} & degree of a term \\
  5856. %
  5857. {\tt TPOW} & power of a term
  5858. \end{tabular}
  5859. \end{center}
  5860. \begin{center}
  5861. \vspace{10pt}
  5862. {\large REDUCE Constructors \par}
  5863. %\end{center}
  5864. %\begin{center}
  5865. \renewcommand{\arraystretch}{1.5}
  5866. \begin{tabular}{lp{\redboxwidth}}
  5867. \verb|.+| & add a term to a polynomial \\
  5868. %
  5869. \verb|./| & divide (two polynomials to get quotient) \\
  5870. \verb|.*| & multiply power by coefficient to produce term \\
  5871. %
  5872. \verb|.^| & raise a variable to a power
  5873. \end{tabular}
  5874. \end{center}
  5875. For example, to find the numerator of the standard quotient above, one
  5876. could say:
  5877. \begin{verbatim}
  5878. numr z;
  5879. \end{verbatim}
  5880. or to find the leading term of the numerator:
  5881. \begin{verbatim}
  5882. lt numr z;
  5883. \end{verbatim}
  5884. Conversion between various data structures is facilitated by the use of a
  5885. set of functions defined for this purpose. Those currently implemented
  5886. include:
  5887. {\renewcommand{\arraystretch}{1.5}
  5888. \begin{tabular}{lp{\reduceboxwidth}}
  5889. {\tt !*A2F} & convert an algebraic expression to
  5890. a standard form. If result is rational, an error results; \\
  5891. %
  5892. {\tt !*A2K} & converts an algebraic expression to
  5893. a kernel. If this is not possible, an error results; \\
  5894. %
  5895. {\tt !*F2A} & converts a standard form to an
  5896. algebraic expression; \\
  5897. %
  5898. {\tt !*F2Q} & convert a standard form to a
  5899. standard quotient; \\
  5900. %
  5901. {\tt !*K2F} & convert a kernel to a standard form; \\
  5902. {\tt !*K2Q} & convert a kernel to a standard quotient; \\
  5903. %
  5904. {\tt !*P2F} & convert a standard power to a
  5905. standard form; \\
  5906. %
  5907. {\tt !*P2Q} & convert a standard power to a standard quotient; \\
  5908. %
  5909. {\tt !*Q2F} & convert a standard quotient to a
  5910. standard form. If the quotient denominator is not 1, an error results; \\
  5911. %
  5912. {\tt !*Q2K} & convert a standard quotient to a
  5913. kernel. If this is not possible, an error results; \\
  5914. %
  5915. {\tt !*T2F} & convert a standard term to a standard form \\
  5916. %
  5917. {\tt !*T2Q} & convert a standard term to a standard quotient.
  5918. \end{tabular}}
  5919. \subsection{Passing Symbolic Mode Values to Algebraic Mode}
  5920. In order to pass the value of a shared variable from symbolic mode to
  5921. algebraic mode, the only thing to do is make sure that the value in
  5922. symbolic mode is a prefix expression. E.g., one uses
  5923. {\tt (expt (plus a b) 2)} for {\tt (a+b)\verb|^|2}, or the format ({\tt *sq
  5924. <standard quotient> t}) as described above. However, if you have
  5925. been working with parts of a standard form they will probably not be in
  5926. this form. In that case, you can do the following:
  5927. \begin{enumerate}
  5928. \item If it is a standard quotient, call {\tt PREPSQ} on it. This takes a
  5929. standard quotient as argument, and returns a prefix expression.
  5930. Alternatively, you can call {\tt MK!*SQ} on it, which returns a prefix
  5931. form like ({\tt *SQ <standard quotient> T)} and avoids translation of
  5932. the expression into a true prefix form.
  5933. \item If it is a standard form, call {\tt PREPF} on it. This takes a
  5934. standard form as argument, and returns the equivalent prefix expression.
  5935. Alternatively, you can convert it to a standard quotient and then call
  5936. {\tt MK!*SQ}.
  5937. \item If it is a part of a standard form, you must usually first build up a
  5938. standard form out of it, and then go to step 2. The conversion functions
  5939. described earlier may be used for this purpose. For example,
  5940. \begin{enumerate}
  5941. \item If {\tt Z} is an expression which is a term, {\tt !*T2F Z} is a
  5942. standard form.
  5943. \item If {\tt Z} is a standard power, {\tt !*P2F Z} is a standard form.
  5944. \item If {\tt Z} is a variable, you can pass it direct to algebraic mode.
  5945. \end{enumerate}
  5946. \end{enumerate}
  5947. For example, to pass the leading term of {\tt (a+b)\verb|^|2} back to
  5948. algebraic mode, one could say:
  5949. \begin{verbatim}
  5950. y:= mk!*sq !*t2q lt numr z;
  5951. \end{verbatim}
  5952. where {\tt Y} has been declared shared as above. If you now go back to
  5953. algebraic mode, you can work with {\tt Y} in the usual way.
  5954. \subsection{Complete Example}
  5955. The following is the complete code for doing the above steps. The end
  5956. result will be that the square of the leading term of $(a+b)^{2}$ is
  5957. calculated.
  5958. %%\begin{tabular}{lp{\rboxwidth}}
  5959. %%{\tt share x,y;} & {\tt \% declare {\tt X} and
  5960. %%{\tt Y} as shared} \\
  5961. %%{\tt x := (a+b)\verb|^|2;} & {\tt \% store (a+b)\verb|^|2 in X} \\
  5962. %%{\tt symbolic;} & {\tt \% transfer to symbolic mode} \\
  5963. %%{\tt z := cadr x;} & {\tt \% store a true standard quotient \newline
  5964. %% \% in Z} \\[1.7pt]
  5965. %%{\tt lt numr z;} & {\tt \% print the leading term of the \newline
  5966. %% \% numerator of Z} \\
  5967. %%{\tt y := mk!*sq !*t2q lt numr z;} & {\tt \% store the
  5968. %% prefix form of this \newline
  5969. %% \% leading term in Y} \\
  5970. %%{\tt algebraic;} & {\tt \% return to algebraic mode} \\
  5971. %%{\tt y\verb|^|2;} & {\tt \% evaluate square of the leading \newline
  5972. %%\% term of (a+b)\verb|^|2}
  5973. %%\end{tabular}
  5974. \begin{verbatim}
  5975. share x,y; % declare X and Y as shared
  5976. x := (a+b)^2; % store (a+b)^2 in X
  5977. symbolic; % transfer to symbolic mode
  5978. z := cadr x; % store a true standard quotient in Z
  5979. lt numr z; % print the leading term of the
  5980. % numerator of Z
  5981. y := mk!*sq !*t2q lt numr z; % store the prefix form of this
  5982. % leading term in Y
  5983. algebraic; % return to algebraic mode
  5984. y^2; % evaluate square of the leading term
  5985. % of (a+b)^2
  5986. \end{verbatim}
  5987. \subsection{Defining Procedures for Intermode Communication}
  5988. If one wishes to define a procedure in symbolic mode for use as an
  5989. operator in algebraic mode, it is necessary to declare this fact to the
  5990. system by using the declaration {\tt OPERATOR}\ttindex{OPERATOR} in
  5991. symbolic mode. Thus
  5992. \begin{verbatim}
  5993. symbolic operator leadterm;
  5994. \end{verbatim}
  5995. would declare the procedure {\tt LEADTERM} as an algebraic operator. This
  5996. declaration {\em must\/} be made in symbolic mode as the effect in algebraic
  5997. mode is different. The value of such a procedure must be a prefix form.
  5998. The algebraic processor will pass arguments to such procedures in prefix
  5999. form. Therefore if you want to work with the arguments as standard
  6000. quotients you must first convert them to that form by using the function
  6001. {\tt SIMP!*}. This function takes a prefix form as argument and returns the
  6002. evaluated standard quotient.
  6003. For example, if you want to define a procedure {\tt LEADTERM} which gives the
  6004. leading term of an algebraic expression, one could do this as follows:
  6005. \begin{samepage}
  6006. \begin{verbatim}
  6007. symbolic operator leadterm; % Declare LEADTERM as a symbolic
  6008. % mode procedure to be used in
  6009. % algebraic mode.
  6010. symbolic procedure leadterm u; % Define LEADTERM.
  6011. mk!*sq !*t2q lt numr simp!* u;
  6012. \end{verbatim}
  6013. \end{samepage}
  6014. Note that this operator has a different effect than the operator {\tt LTERM}
  6015. \ttindex{LTERM}. In the latter case, the calculation is done
  6016. with respect to the second argument of the operator. In the example here,
  6017. we simply extract the leading term with respect to the system's choice of
  6018. main variable.
  6019. Finally, if you wish to use the algebraic evaluator on an argument in a
  6020. symbolic mode definition, the function {\tt REVAL} can be used. The one
  6021. argument of {\tt REVAL} must be the prefix form of an expression. {\tt
  6022. REVAL} returns the evaluated expression as a true Lisp prefix form.
  6023. \section{Rlisp '88}
  6024. Rlisp '88 is a superset of the Rlisp that has been traditionally used for
  6025. the support of REDUCE. It is fully documented in the book
  6026. Marti, J.B., ``{RLISP} '88: An Evolutionary Approach to Program Design
  6027. and Reuse'', World Scientific, Singapore (1993).
  6028. Rlisp '88 adds to the traditional Rlisp the following facilities:
  6029. \begin{enumerate}
  6030. \item more general versions of the looping constructs {\tt for},
  6031. {\tt repeat} and {\tt while};
  6032. \item support for a backquote construct;
  6033. \item support for active comments;
  6034. \item support for vectors of the form name[index];
  6035. \item support for simple structures;
  6036. \item support for records.
  6037. \end{enumerate}
  6038. In addition, ``--'' is a letter in Rlisp '88. In other words, {\tt A-B} is an
  6039. identifier, not the difference of the identifiers {\tt A} and {\tt B}. If
  6040. the latter construct is required, it is necessary to put spaces around the
  6041. - character. For compatibility between the two versions of Rlisp, we
  6042. recommend this convention be used in all symbolic mode programs.
  6043. To use Rlisp '88, type {\tt on rlisp88;}\ttindex{RLISP88}. This switches to
  6044. symbolic mode with the Rlisp '88 syntax and extensions. While in this
  6045. environment, it is impossible to switch to algebraic mode, or prefix
  6046. expressions by ``algebraic''. However, symbolic mode programs written in
  6047. Rlisp '88 may be run in algebraic mode provided the rlisp88 package has been
  6048. loaded. We also expect that many of the extensions defined in Rlisp '88
  6049. will migrate to the basic Rlisp over time. To return to traditional Rlisp
  6050. or to switch to algebraic mode, say ``off rlisp88''.
  6051. \section{References}
  6052. There are a number of useful books which can give you further information
  6053. about LISP. Here is a selection:
  6054. Allen, J.R., ``The Anatomy of LISP'', McGraw Hill, New York, 1978.
  6055. McCarthy J., P.W. Abrahams, J. Edwards, T.P. Hart and
  6056. M.I. Levin, ``LISP 1.5 Programmer's Manual'', M.I.T. Press, 1965.
  6057. Touretzky, D.S, ``{LISP}: A Gentle Introduction to Symbolic Computation'',
  6058. Harper \& Row, New York, 1984.
  6059. Winston, P.H. and Horn, B.K.P., ``LISP'', Addison-Wesley, 1981.
  6060. \chapter{Calculations in High Energy Physics}
  6061. A set of {\REDUCE} commands is provided for users interested in symbolic
  6062. calculations in high energy physics. Several extensions to our basic
  6063. syntax are necessary, however, to allow for the different data structures
  6064. encountered.
  6065. \section{High Energy Physics Operators}
  6066. \label{HEPHYS}
  6067. We begin by introducing three new operators required in these calculations.
  6068. \subsection{. (Cons) Operator}\index{Dot product}
  6069. Syntax:
  6070. \begin{verbatim}
  6071. (EXPRN1:vector_expression)
  6072. . (EXPRN2:vector_expression):algebraic.
  6073. \end{verbatim}
  6074. The binary {\tt .} operator, which is normally used to denote the addition
  6075. of an element to the front of a list, can also be used in algebraic mode
  6076. to denote the scalar product of two Lorentz four-vectors. For this to
  6077. happen, the second argument must be recognizable as a vector expression
  6078. \index{High energy vector expression} at the time of
  6079. evaluation. With this meaning, this operator is often referred to as the
  6080. {\em dot\/} operator. In the present system, the index handling routines all
  6081. assume that Lorentz four-vectors are used, but these routines could be
  6082. rewritten to handle other cases.
  6083. Components of vectors can be represented by including representations of
  6084. unit vectors in the system. Thus if {\tt EO} represents the unit vector
  6085. {\tt (1,0,0,0)}, {\tt (p.eo)} represents the zeroth component of the
  6086. four-vector P. Our metric and notation follows Bjorken and Drell
  6087. ``Relativistic Quantum Mechanics'' (McGraw-Hill, New York, 1965).
  6088. Similarly, an arbitrary component {\tt P} may be represented by
  6089. {\tt (p.u)}. If contraction over components of vectors is required, then
  6090. the declaration {\tt INDEX}\ttindex{INDEX} must be used. Thus
  6091. \begin{verbatim}
  6092. index u;
  6093. \end{verbatim}
  6094. declares {\tt U} as an index, and the simplification of
  6095. \begin{verbatim}
  6096. p.u * q.u
  6097. \end{verbatim}
  6098. would result in
  6099. \begin{verbatim}
  6100. P.Q
  6101. \end{verbatim}
  6102. The metric tensor $g^{\mu \nu}$ may be represented by {\tt (u.v)}. If
  6103. contraction over {\tt U} and {\tt V} is required, then they should be
  6104. declared as indices.
  6105. Errors occur if indices are not properly matched in expressions.
  6106. If a user later wishes to remove the index property from specific vectors,
  6107. he can do it with the declaration {\tt REMIND}.\ttindex{REMIND} Thus
  6108. {\tt remind v1...vn;} removes the index flags from the variables {\tt V1}
  6109. through {\tt Vn}. However, these variables remain vectors in the system.
  6110. \subsection{G Operator for Gamma Matrices}\index{Dirac $\gamma$ matrix}
  6111. \ttindex{G}
  6112. Syntax:
  6113. \begin{verbatim}
  6114. G(ID:identifier[,EXPRN:vector_expression])
  6115. :gamma_matrix_expression.
  6116. \end{verbatim}
  6117. {\tt G} is an n-ary operator used to denote a product of $\gamma$ matrices
  6118. contracted with Lorentz four-vectors. Gamma matrices are associated with
  6119. fermion lines in a Feynman diagram. If more than one such line occurs,
  6120. then a different set of $\gamma$ matrices (operating in independent spin
  6121. spaces) is required to represent each line. To facilitate this, the first
  6122. argument of {\tt G} is a line identification identifier (not a number)
  6123. used to distinguish different lines.
  6124. Thus
  6125. \begin{verbatim}
  6126. g(l1,p) * g(l2,q)
  6127. \end{verbatim}
  6128. denotes the product of {\tt $\gamma$.p} associated with a fermion line
  6129. identified as {\tt L1}, and {\tt $\gamma$.q} associated with another line
  6130. identified as {\tt L2} and where {\tt p} and {\tt q} are Lorentz
  6131. four-vectors. A product of $\gamma$ matrices associated with the same
  6132. line may be written in a contracted form.
  6133. Thus
  6134. \begin{verbatim}
  6135. g(l1,p1,p2,...,p3) = g(l1,p1)*g(l1,p2)*...*g(l1,p3) .
  6136. \end{verbatim}
  6137. The vector {\tt A} is reserved in arguments of G to denote the special
  6138. $\gamma$ matrix $\gamma^{5}$. Thus
  6139. \begin{quote}
  6140. \begin{tabbing}
  6141. \ \ \ \ \ {\tt g(l,a)}\hspace{0.2in} \= =\ \ \ $\gamma^{5}$ \hspace{0.5in}
  6142. \= associated with the line {\tt L} \\[0.1in]
  6143. \ \ \ \ \ {\tt g(l,p,a)} \> =\ \ \ $\gamma$.p $\times \gamma^{5}$ \>
  6144. associated with the line {\tt L}.
  6145. \end{tabbing}
  6146. \end{quote}
  6147. $\gamma^{\mu}$ (associated with the line {\tt L}) may be written as
  6148. {\tt g(l,u)}, with {\tt U} flagged as an index if contraction over {\tt U}
  6149. is required.
  6150. The notation of Bjorken and Drell is assumed in all operations involving
  6151. $\gamma$ matrices.
  6152. \subsection{EPS Operator}\ttindex{EPS}
  6153. Syntax:
  6154. \begin{verbatim}
  6155. EPS(EXPRN1:vector_expression,...,EXPRN4:vector_exp)
  6156. :vector_exp.
  6157. \end{verbatim}
  6158. The operator {\tt EPS} has four arguments, and is used only to denote the
  6159. completely antisymmetric tensor of order 4 and its contraction with Lorentz
  6160. four-vectors. Thus
  6161. \[ \epsilon_{i j k l} = \left\{ \begin{array}{cl}
  6162. +1 & \mbox{if $i,j,k,l$ is an even permutation
  6163. of 0,1,2,3} \\
  6164. -1 & \mbox{if an odd permutation} \\
  6165. 0 & \mbox{otherwise}
  6166. \end{array}
  6167. \right. \]
  6168. A contraction of the form $\epsilon_{i j \mu \nu}p_{\mu}q_{\nu}$ may be
  6169. written as {\tt eps(i,j,p,q)}, with {\tt I} and {\tt J} flagged as indices,
  6170. and so on.
  6171. \section{Vector Variables}
  6172. Apart from the line identification identifier in the {\tt G} operator, all
  6173. other arguments of the operators in this section are vectors. Variables
  6174. used as such must be declared so by the type declaration {\tt VECTOR},
  6175. \ttindex{VECTOR} for example:
  6176. \begin{verbatim}
  6177. vector p1,p2;
  6178. \end{verbatim}
  6179. declares {\tt P1} and {\tt P2} to be vectors. Variables declared as
  6180. indices or given a mass\ttindex{MASS} are automatically declared
  6181. vector by these declarations.
  6182. \section{Additional Expression Types}
  6183. Two additional expression types are necessary for high energy
  6184. calculations, namely
  6185. \subsection{Vector Expressions}\index{High energy vector expression}
  6186. These follow the normal rules of vector combination. Thus the product of a
  6187. scalar or numerical expression and a vector expression is a vector, as are
  6188. the sum and difference of vector expressions. If these rules are not
  6189. followed, error messages are printed. Furthermore, if the system finds an
  6190. undeclared variable where it expects a vector variable, it will ask the
  6191. user in interactive mode whether to make that variable a vector or not. In
  6192. batch mode, the declaration will be made automatically and the user
  6193. informed of this by a message.
  6194. {\tt Examples:}
  6195. Assuming {\tt P} and {\tt Q} have been declared vectors, the following are
  6196. vector expressions
  6197. \begin{verbatim}
  6198. p
  6199. 2*q/3
  6200. 2*x*y*p - p.q*q/(3*q.q)
  6201. \end{verbatim}
  6202. whereas {\tt p*q} and {\tt p/q} are not.
  6203. \subsection{Dirac Expressions}
  6204. These denote those expressions which involve $\gamma$ matrices. A $\gamma$
  6205. matrix is implicitly a 4 $\times$ 4 matrix, and so the product, sum and
  6206. difference of such expressions, or the product of a scalar and Dirac
  6207. expression is again a Dirac expression. There are no Dirac variables in
  6208. the system, so whenever a scalar variable appears in a Dirac expression
  6209. without an associated $\gamma$ matrix expression, an implicit unit 4
  6210. by 4 matrix is assumed. For example, {\tt g(l,p) + m} denotes {\tt
  6211. g(l,p) + m*<unit 4 by 4 matrix>}. Multiplication of Dirac
  6212. expressions, as for matrix expressions, is of course non-commutative.
  6213. \section{Trace Calculations}\index{High energy trace}
  6214. When a Dirac expression is evaluated, the system computes one quarter of
  6215. the trace of each $\gamma$ matrix product in the expansion of the expression.
  6216. One quarter of each trace is taken in order to avoid confusion between the
  6217. trace of the scalar {\tt M}, say, and {\tt M} representing {\tt M * <unit
  6218. 4 by 4 matrix>}. Contraction over indices occurring in such expressions is
  6219. also performed. If an unmatched index is found in such an expression, an
  6220. error occurs.
  6221. The algorithms used for trace calculations are the best available at the
  6222. time this system was produced. For example, in addition to the algorithm
  6223. developed by Chisholm for contracting indices in products of traces,
  6224. {\REDUCE} uses the elegant algorithm of Kahane for contracting indices in
  6225. $\gamma$ matrix products. These algorithms are described in Chisholm, J. S.
  6226. R., Il Nuovo Cimento X, 30, 426 (1963) and Kahane, J., Journal Math.
  6227. Phys. 9, 1732 (1968).
  6228. It is possible to prevent the trace calculation over any line identifier
  6229. by the declaration {\tt NOSPUR}.\ttindex{NOSPUR} For example,
  6230. \begin{verbatim}
  6231. nospur l1,l2;
  6232. \end{verbatim}
  6233. will mean that no traces are taken of $\gamma$ matrix terms involving the line
  6234. numbers {\tt L1} and {\tt L2}. However, in some calculations involving
  6235. more than one line, a catastrophic error
  6236. \begin{verbatim}
  6237. This NOSPUR option not implemented
  6238. \end{verbatim}
  6239. can occur (for the reason stated!) If you encounter this error, please let
  6240. us know!
  6241. A trace of a $\gamma$ matrix expression involving a line identifier which has
  6242. been declared {\tt NOSPUR} may be later taken by making the declaration
  6243. {\tt SPUR}.\ttindex{SPUR}
  6244. See also the CVIT package for an alternative
  6245. mechanism\extendedmanual{ (chapter~\ref{CVIT})}.
  6246. \section{Mass Declarations}\ttindex{MASS}
  6247. It is often necessary to put a particle ``on the mass shell'' in a
  6248. calculation. This can, of course, be accomplished with a {\tt LET}
  6249. command such as
  6250. \begin{verbatim}
  6251. let p.p= m^2;
  6252. \end{verbatim}
  6253. but an alternative method is provided by two commands {\tt MASS} and
  6254. {\tt MSHELL}.\ttindex{MSHELL}
  6255. {\tt MASS} takes a list of equations of the form:
  6256. \begin{verbatim}
  6257. <vector variable> = <scalar variable>
  6258. \end{verbatim}
  6259. for example,
  6260. \begin{verbatim}
  6261. mass p1=m, q1=mu;
  6262. \end{verbatim}
  6263. The only effect of this command is to associate the relevant scalar
  6264. variable as a mass with the corresponding vector. If we now say
  6265. \begin{verbatim}
  6266. mshell <vector variable>,...,<vector variable>;
  6267. \end{verbatim}
  6268. and a mass has been associated with these arguments, a substitution of the
  6269. form
  6270. \begin{verbatim}
  6271. <vector variable>.<vector variable> = <mass>^2
  6272. \end{verbatim}
  6273. is set up. An error results if the variable has no preassigned mass.
  6274. \section{Example}
  6275. We give here as an example of a simple calculation in high energy physics
  6276. the computation of the Compton scattering cross-section as given in
  6277. Bjorken and Drell Eqs. (7.72) through (7.74). We wish to compute the trace of
  6278. $$\left. \alpha^2\over2 \right. \left({k^\prime\over k}\right)^2
  6279. \left({\gamma.p_f+m\over2m}\right)\left({\gamma.e^\prime \gamma.e
  6280. \gamma.k_i\over2k.p_i} + {\gamma.e\gamma.e^\prime
  6281. \gamma.k_f\over2k^\prime.p_i}\right)
  6282. \left({\gamma.p_i+m\over2m}\right)$$
  6283. $$
  6284. \left({\gamma.k_i\gamma.e\gamma.e^\prime\over2k.p_i} +
  6285. {\gamma.k_f\gamma.e^\prime\gamma.e\over2k^\prime.p_i}
  6286. \right)
  6287. $$
  6288. where $k_i$ and $k_f$ are the four-momenta of incoming and outgoing photons
  6289. (with polarization vectors $e$ and $e^\prime$ and laboratory energies
  6290. $k$ and $k^\prime$
  6291. respectively) and $p_i$, $p_f$ are incident and final electron four-momenta.
  6292. Omitting therefore an overall factor
  6293. ${\alpha^2\over2m^2}\left({k^\prime\over k}\right)^2$ we need to find
  6294. one quarter of the trace of
  6295. $${
  6296. \left( \gamma.p_f + m\right)
  6297. \left({\gamma.e^\prime \gamma.e\gamma.k_i\over2k.p_i} +
  6298. {\gamma.e\gamma.e^\prime \gamma.k_f\over 2k^\prime.p_i}\right) \left(
  6299. \gamma.p_i + m\right)}$$
  6300. $${
  6301. \left({\gamma.k_i\gamma.e\gamma.e^\prime\over 2k.p_i} +
  6302. {\gamma.k_f\gamma.e^\prime \gamma.e\over2k^\prime.p_i}\right) }$$
  6303. A straightforward REDUCE program for this, with appropriate substitutions
  6304. (using {\tt P1} for $p_i$, {\tt PF} for $p_f$, {\tt KI}
  6305. for $k_i$ and {\tt KF} for $k_f$) is
  6306. \begin{verbatim}
  6307. on div; % this gives output in same form as Bjorken and Drell.
  6308. mass ki= 0, kf= 0, p1= m, pf= m; vector e,ep;
  6309. % if e is used as a vector, it loses its scalar identity as
  6310. % the base of natural logarithms.
  6311. mshell ki,kf,p1,pf;
  6312. let p1.e= 0, p1.ep= 0, p1.pf= m^2+ki.kf, p1.ki= m*k,p1.kf=
  6313. m*kp, pf.e= -kf.e, pf.ep= ki.ep, pf.ki= m*kp, pf.kf=
  6314. m*k, ki.e= 0, ki.kf= m*(k-kp), kf.ep= 0, e.e= -1,
  6315. ep.ep=-1;
  6316. for all p let gp(p)= g(l,p)+m;
  6317. comment this is just to save us a lot of writing;
  6318. gp(pf)*(g(l,ep,e,ki)/(2*ki.p1) + g(l,e,ep,kf)/(2*kf.p1))
  6319. * gp(p1)*(g(l,ki,e,ep)/(2*ki.p1) + g(l,kf,ep,e)/
  6320. (2*kf.p1))$
  6321. write "The Compton cxn is",ws;
  6322. \end{verbatim}
  6323. (We use {\tt P1} instead of {\tt PI} in the above to avoid confusion with
  6324. the reserved variable {\tt PI}).
  6325. This program will print the following result
  6326. \begin{verbatim}
  6327. (-1) (-1) 2
  6328. The Compton cxn is 1/2*K*KP + 1/2*K *KP + 2*E.EP - 1
  6329. \end{verbatim}
  6330. \section{Extensions to More Than Four Dimensions}
  6331. In our discussion so far, we have assumed that we are working in the
  6332. normal four dimensions of QED calculations. However, in most cases, the
  6333. programs will also work in an arbitrary number of dimensions. The command
  6334. \ttindex{VECDIM}
  6335. \begin{verbatim}
  6336. vecdim <expression>;
  6337. \end{verbatim}
  6338. sets the appropriate dimension. The dimension can be symbolic as well as
  6339. numerical. Users should note however, that the {\tt EPS} operator and the
  6340. $\gamma_{5}$ symbol ({\tt A}) are not properly defined in other than four
  6341. dimensions and will lead to an error if used.
  6342. \chapter{{\REDUCE} and Rlisp Utilities}
  6343. {\REDUCE} and its associated support language system Rlisp\index{Rlisp}
  6344. include a number of utilities which have proved useful for program
  6345. development over the years. The following are supported in most of the
  6346. implementations of {\REDUCE} currently available.
  6347. \section{The Standard Lisp Compiler}\index{Compiler}
  6348. Many versions of {\REDUCE} include a Standard Lisp compiler that is
  6349. automatically loaded on demand. You should check your system specific
  6350. user guide to make sure you have such a compiler. To make the compiler
  6351. active, the switch {\tt COMP}\ttindex{COMP} should be turned on. Any
  6352. further definitions input after this will be compiled automatically. If
  6353. the compiler used is a derivative version of the original Griss-Hearn
  6354. compiler,
  6355. (M. L. Griss and A.
  6356. C. Hearn, ``A Portable LISP Compiler", SOFTWARE --- Practice and Experience
  6357. 11 (1981) 541-605),
  6358. there are other switches that might also be
  6359. used in this regard. However, these additional switches are not supported
  6360. in all compilers. They are as follows:
  6361. %\ttindex{PLAP}\ttindex{PGWD}\ttindex{PWRDS}
  6362. {\renewcommand{\arraystretch}{2}
  6363. \begin{tabular}{lp{\reduceboxwidth}}
  6364. {\tt PLAP} & If ON, causes the printing of the
  6365. portable macros produced by the compiler; \\
  6366. %
  6367. {\tt PGWD} & If ON, causes the printing of the
  6368. actual assembly language instructions generated from the macros; \\
  6369. %
  6370. {\tt PWRDS} & If ON, causes a statistic
  6371. message of the form \newline
  6372. {\tt <function> COMPILED, <words> WORDS, <words> LEFT} \newline
  6373. to be printed. The first number is the number of words of binary
  6374. program space the compiled function took, and the second number
  6375. the number of words left unused in binary program space. \\
  6376. \end{tabular}}
  6377. \section{Fast Loading Code Generation Program}\index{Fast loading of code}
  6378. \label{sec-load}
  6379. In most versions of {\REDUCE}, it is possible to take any set of Lisp, Rlisp
  6380. or {\REDUCE} commands and build a fast loading version of them. In Rlisp or
  6381. {\REDUCE}, one does the following:
  6382. \begin{verbatim}
  6383. faslout <filename>;
  6384. <commands or IN statements>
  6385. faslend;
  6386. \end{verbatim}
  6387. To load such a file, one uses the command {\tt LOAD},\ttindex{LOAD}
  6388. e.g. {\tt load foo;}
  6389. or {\tt load foo,bah;}
  6390. This process produces a fast-loading version of the original file. In some
  6391. implementations, this means another file is created with the same name but
  6392. a different extension. For example, in PSL-based systems, the extension is
  6393. {\tt b} (for binary). In CSL-based systems, however, this process adds the
  6394. fast-loading code to a single file in which all such code is stored.
  6395. Particular functions are provided by CSL for managing this file, and
  6396. described in the CSL user documentation.
  6397. In doing this build, as with the production of a Standard Lisp form of
  6398. such statements, it is important to remember that some of the commands
  6399. must be instantiated during the building process. For example, macros
  6400. must be expanded, and some property list operations must happen.
  6401. The {\REDUCE} sources should be consulted for further details on this.
  6402. % To facilitate this, the {\tt EVAL} and {\tt IGNORE} flags may be
  6403. % used. Note also that there can be no {\tt LOAD} command within the input
  6404. % statements.
  6405. To avoid excessive printout, input statements should be followed by a \$
  6406. instead of the semicolon. With {\tt LOAD} however, the input doesn't
  6407. print out regardless of which terminator is used with the command.
  6408. If you subsequently change the source files used in producing a fast
  6409. loading file, don't forget to repeat the above process in order to update
  6410. the fast loading file correspondingly. Remember also that the text which
  6411. is read in during the creation of the fast load file, in the compiling
  6412. process described above, is {\em not\/} stored in your {\REDUCE}
  6413. environment, but only translated and output. If you want to use the file
  6414. just created, you must then use {\tt LOAD} to load the output of the
  6415. fast-loading file generation program.
  6416. When the file to be loaded contains a complete package for a given
  6417. application, {\tt LOAD\_PACKAGE}\ttindex{LOAD\_PACKAGE} rather than
  6418. {\tt LOAD} should be used. The syntax is the same. However,
  6419. {\tt LOAD\_PACKAGE} does some additional bookkeeping such as recording that
  6420. this package has now been loaded, that is required for the correct
  6421. operation of the system.
  6422. \section{The Standard Lisp Cross Reference Program}\index{Cross reference}
  6423. {\tt CREF}\ttindex{CREF} is a Standard Lisp program for processing a
  6424. set of Standard LISP function definitions to produce:
  6425. \begin{enumerate}
  6426. \item A ``summary'' showing:
  6427. \begin{enumerate}
  6428. \item A list of files processed;
  6429. \item A list of ``entry points'' (functions which are not called or
  6430. are only called by themselves);
  6431. \item A list of undefined functions (functions called but not
  6432. defined in this set of functions);
  6433. \item A list of variables that were used non-locally but not
  6434. declared {\tt GLOBAL} or {\tt FLUID} before their use;
  6435. \item A list of variables that were declared {\tt GLOBAL} but not used
  6436. as {\tt FLUID}s, i.e., bound in a function;
  6437. \item A list of {\tt FLUID} variables that were not bound in a function
  6438. so that one might consider declaring them {\tt GLOBAL}s;
  6439. \item A list of all {\tt GLOBAL} variables present;
  6440. \item A list of all {\tt FLUID} variables present;
  6441. \item A list of all functions present.
  6442. \end{enumerate}
  6443. \item A ``global variable usage'' table, showing for each non-local
  6444. variable:
  6445. \begin{enumerate}
  6446. \item Functions in which it is used as a declared {\tt FLUID} or {\tt GLOBAL};
  6447. \item Functions in which it is used but not declared;
  6448. \item Functions in which it is bound;
  6449. \item Functions in which it is changed by {\tt SETQ}.
  6450. \end{enumerate}
  6451. \item A ``function usage'' table showing for each function:
  6452. \begin{enumerate}
  6453. \item Where it is defined;
  6454. \item Functions which call this function;
  6455. \item Functions called by it;
  6456. \item Non-local variables used.
  6457. \end{enumerate}
  6458. \end{enumerate}
  6459. The program will also check that functions are called with the correct
  6460. number of arguments, and print a diagnostic message otherwise.
  6461. The output is alphabetized on the first seven characters of each function
  6462. name.
  6463. \subsection{Restrictions}
  6464. Algebraic procedures in {\REDUCE} are treated as if they were symbolic, so
  6465. that algebraic constructs will actually appear as calls to symbolic
  6466. functions, such as {\tt AEVAL}.
  6467. \subsection{Usage}
  6468. To invoke the cross reference program, the switch {\tt CREF}
  6469. \ttindex{CREF} is used. {\tt on cref} causes the cref program to load
  6470. and the cross-referencing process to begin. After all the required
  6471. definitions are loaded, {\tt off cref} will cause the cross-reference
  6472. listing to be produced. For example, if you wish to cross-reference all
  6473. functions in the file {\tt tst.red}, and produce the cross-reference
  6474. listing in the file {\tt tst.crf}, the following sequence can be used:
  6475. \begin{verbatim}
  6476. out "tst.crf";
  6477. on cref;
  6478. in "tst.red"$
  6479. off cref;
  6480. shut "tst.crf";
  6481. \end{verbatim}
  6482. To process more than one file, more {\tt IN} statements may be added
  6483. before the call of {\tt off cref}, or the {\tt IN} statement changed to
  6484. include a list of files.
  6485. \subsection{Options}
  6486. Functions with the flag {\tt NOLIST} will not be examined or output.
  6487. Initially, all Standard Lisp functions are so flagged. (In fact, they are
  6488. kept on a list {\tt NOLIST!*}, so if you wish to see references to {\em
  6489. all} functions, then {\tt CREF} should be first loaded with the command {\tt
  6490. load cref}, and this variable then set to {\tt NIL}).
  6491. It should also be remembered that any macros with the property list flag
  6492. {\tt EXPAND}, or, if the switch {\tt FORCE} is on, without the property
  6493. list flag {\tt NOEXPAND}, will be expanded before the definition is seen
  6494. by the cross-reference program, so this flag can also be used to select
  6495. those macros you require expanded and those you do not.
  6496. \section{Prettyprinting Reduce Expressions}\index{Prettyprinting}
  6497. {\REDUCE} includes a module for printing {\REDUCE} syntax in a standard
  6498. format. This module is activated by the switch {\tt PRET},
  6499. \ttindex{PRET} which is normally off.
  6500. Since the system converts algebraic input into an equivalent symbolic form,
  6501. the printing program tries to interpret this as an algebraic expression
  6502. before printing it. In most cases, this can be done successfully. However,
  6503. there will be occasional instances where results are printed in symbolic
  6504. mode form that bears little resemblance to the original input, even though
  6505. it is formally equivalent.
  6506. If you want to prettyprint a whole file, say {\tt off output,msg;}
  6507. \ttindex{MSG} and (hopefully) only clean output will result. Unlike {\tt
  6508. DEFN},\ttindex{DEFN} input is also evaluated with {\tt PRET}
  6509. \ttindex{PRET} on.
  6510. \section{Prettyprinting Standard Lisp S-Expressions}\index{Prettyprinting}
  6511. REDUCE includes a module for printing
  6512. S-expressions in a standard format. The Standard Lisp function for this
  6513. purpose is {\tt PRETTYPRINT}\ttindex{PRETTYPRINT} which takes a Lisp
  6514. expression and prints the formatted equivalent.
  6515. Users can also have their {\REDUCE} input printed in this form by use of
  6516. the switch {\tt DEFN}.\ttindex{DEFN} This is in fact a convenient way to
  6517. convert {\REDUCE} (or Rlisp) syntax into Lisp. {\tt off msg;} will prevent
  6518. warning messages from being printed.
  6519. NOTE: When {\tt DEFN} is on, input is not evaluated.
  6520. \chapter {Maintaining {\REDUCE}}
  6521. {\REDUCE} continues to evolve both in terms of the number of facilities
  6522. available, and the power of the individual facilities. Corrections are
  6523. made as bugs are discovered, and awkward features simplified. In order to
  6524. provide users with easy access to such enhancements, a {\em {\REDUCE}
  6525. network library\/} has been established from which material can be extracted
  6526. by anyone with electronic mail access to the Internet computer network.
  6527. In addition to miscellaneous documents, source and utility files, the
  6528. library includes a bibliography of papers referencing {\REDUCE} which
  6529. contains over 800 entries. Instructions on using this library are sent to
  6530. all registered {\REDUCE} users who provide a network address. If you
  6531. would like a more complete list of the contents of the library, send to
  6532. {\em reduce-netlib@rand.org\/} the single line message {\em send index\/} or
  6533. {\em help}. The current {\REDUCE} information
  6534. package can be obtained from the network library by including on a
  6535. separate line {\em send info-package\/} and a demonstration file by
  6536. including the line {\em send demonstration}. If you prefer, hard copies
  6537. of the information package and the bibliography are available from the
  6538. {\REDUCE} secretary at RAND, 1700 Main Street, P.O. Box 2138, Santa
  6539. Monica, CA 90407-2138 ({\em reduce@rand.org}). Copies of the network
  6540. library are also maintained at other addresses. At the time of writing,
  6541. {\em reduce-netlib@can.nl\/} and {\em reduce-netlib@pi.cc.u-tokyo.ac.jp\/}
  6542. may also be used instead of {\em reduce-netlib@rand.org}.
  6543. A World Wide Web {\REDUCE} server with URL
  6544. \begin{verbatim}
  6545. http://www.rrz.uni-koeln.de/REDUCE/
  6546. \end{verbatim}
  6547. is also supported. In addition to general information about {\REDUCE}, this
  6548. server has pointers to the network library, the demonstration versions,
  6549. examples of {\REDUCE} programming, a set of manuals, and the {\REDUCE} online
  6550. help system.
  6551. Finally, there is a {\REDUCE} electronic forum accessible from the same
  6552. networks. This enables {\REDUCE} users to raise questions and discuss
  6553. ideas concerning the use and development of {\REDUCE} with other users.
  6554. Additions and changes to the network library and new releases of {\REDUCE}
  6555. are also announced in this forum. Any user with appropriate electronic
  6556. mail access is encouraged to register for membership in this forum. To do
  6557. so, send a message requesting inclusion to \\
  6558. {\em reduce-forum-request@rand.org}.
  6559. \appendix
  6560. \chapter{Reserved Identifiers}
  6561. We list here all identifiers that are normally reserved in \REDUCE{}
  6562. including names of commands, operators and switches initially in the system.
  6563. Excluded are words that are reserved in specific implementations of the
  6564. system.
  6565. \vspace{13pt}
  6566. \begin{list}{}{\renewcommand{\makelabel}[1]{#1\hspace{\fill}}%
  6567. \settowidth{\labelwidth}{Numerical Operators}%
  6568. \setlength{\labelsep}{1em}%
  6569. \settowidth{\leftmargin}{Numerical Operators\hspace*{\labelsep}}%
  6570. \sloppy}
  6571. \item[Commands] {\tt ALGEBRAIC} {\tt ANTISYMMETRIC}
  6572. {\tt ARRAY} {\tt BYE} {\tt CLEAR} \linebreak
  6573. {\tt CLEARRULES} {\tt COMMENT} {\tt
  6574. CONT} {\tt DECOMPOSE} {\tt DEFINE} {\tt DEPEND} {\tt DISPLAY} {\tt ED}
  6575. {\tt EDITDEF} {\tt END} {\tt EVEN} {\tt FACTOR} {\tt FOR} {\tt FORALL}
  6576. {\tt FOREACH} {\tt GO} {\tt GOTO} {\tt IF} {\tt IN} {\tt INDEX} {\tt INFIX}
  6577. {\tt INPUT} {\tt INTEGER} {\tt KORDER} {\tt LET} {\tt LINEAR} {\tt LISP}
  6578. {\tt LISTARGP} {\tt LOAD} {\tt LOAD\_PACKAGE} {\tt MASS} {\tt MATCH} {\tt
  6579. MATRIX} {\tt MSHELL} {\tt NODEPEND} {\tt NONCOM} {\tt NONZERO} {\tt NOSPUR}
  6580. {\tt ODD} {\tt OFF}
  6581. {\tt ON} {\tt OPERATOR} {\tt ORDER} {\tt OUT} {\tt PAUSE} {\tt PRECEDENCE}
  6582. {\tt PRINT\_PRECISION} {\tt PROCEDURE} {\tt QUIT} {\tt REAL} {\tt REMFAC}
  6583. {\tt REMIND} {\tt RETRY} {\tt RETURN} {\tt SAVEAS} {\tt SCALAR} {\tt
  6584. SETMOD} {\tt SHARE} {\tt SHOWTIME} {\tt SHUT} {\tt SPUR} {\tt SYMBOLIC}
  6585. {\tt SYMMETRIC} {\tt VECDIM} {\tt VECTOR} {\tt WEIGHT} {\tt WRITE} {\tt
  6586. WTLEVEL}
  6587. \item[Boolean Operators] {\tt EVENP} {\tt FIXP}
  6588. {\tt FREEOF} {\tt NUMBERP} {\tt ORDP} {\tt PRIMEP}
  6589. \item[Infix Operators]
  6590. \verb|:=| \verb|=| \verb|>=| \verb|>| \verb|<=| \verb|<| \verb|=>|
  6591. \verb|+| \verb|*| \verb|/| \verb|^| \verb|**| \verb|.| {\tt WHERE}
  6592. {\tt SETQ} {\tt OR} {\tt AND} {\tt MEMBER} {\tt MEMQ} {\tt
  6593. EQUAL} {\tt NEQ} {\tt EQ} {\tt GEQ} {\tt GREATERP} {\tt LEQ} {\tt LESSP}
  6594. {\tt PLUS} {\tt DIFFERENCE} {\tt MINUS} {\tt TIMES} {\tt QUOTIENT} {\tt
  6595. EXPT} {\tt CONS}
  6596. \item[Numerical Operators] {\tt ABS} {\tt ACOS}
  6597. {\tt ACOSH} {\tt ACOT} {\tt ACOTH} {\tt ACSC} {\tt ACSCH} {\tt ASEC} {\tt
  6598. ASECH} {\tt ASIN} {\tt ASINH} {\tt ATAN} {\tt ATANH} {\tt ATAN2} {\tt COS}
  6599. {\tt COSH} {\tt COT} {\tt COTH} {\tt CSC} {\tt CSCH} {\tt EXP} {\tt
  6600. FACTORIAL} {\tt FIX} {\tt FLOOR} {\tt HYPOT} {\tt LN} {\tt LOG} {\tt LOGB}
  6601. {\tt LOG10} {\tt NEXTPRIME} {\tt ROUND} {\tt SEC} {\tt SECH} {\tt SIN}
  6602. {\tt SINH} {\tt SQRT} {\tt TAN} {\tt TANH}
  6603. \item[Prefix Operators] {\tt APPEND} {\tt
  6604. ARGLENGTH} {\tt CEILING} {\tt COEFF} {\tt COEFFN} {\tt COFACTOR} {\tt
  6605. CONJ} {\tt DEG} {\tt DEN} {\tt DET} {\tt DF} {\tt DILOG} {\tt EI}
  6606. {\tt EPS} {\tt ERF} {\tt FACTORIZE} {\tt FIRST} {\tt GCD} {\tt G} {\tt
  6607. IMPART} {\tt INT} {\tt INTERPOL} {\tt LCM} {\tt LCOF} {\tt LENGTH} {\tt
  6608. LHS} {\tt LINELENGTH} {\tt LTERM} {\tt MAINVAR} {\tt MAT} {\tt MATEIGEN}
  6609. {\tt MAX} {\tt MIN} {\tt MKID} {\tt NULLSPACE} {\tt NUM} {\tt PART} {\tt
  6610. PF} {\tt PRECISION} {\tt RANDOM} {\tt RANDOM\_NEW\_SEED} {\tt RANK} {\tt
  6611. REDERR} {\tt REDUCT} {\tt REMAINDER} {\tt REPART} {\tt REST} {\tt
  6612. RESULTANT} {\tt REVERSE} {\tt RHS} {\tt SECOND} {\tt SET} {\tt SHOWRULES}
  6613. {\tt SIGN} {\tt SOLVE} {\tt STRUCTR} {\tt SUB} {\tt SUM} {\tt THIRD} {\tt
  6614. TP} {\tt TRACE} {\tt VARNAME}
  6615. \item[Reserved Variables] {\tt CARD\_NO} {\tt E} {\tt EVAL\_MODE}
  6616. {\tt FORT\_WIDTH} {\tt HIGH\_POW} {\tt I} {\tt INFINITY} {\tt K!*} {\tt
  6617. LOW\_POW} {\tt NIL} {\tt PI} {\tt ROOT\_MULTIPLICITY} {\tt T}
  6618. \item[Switches] {\tt ADJPREC} {\tt ALGINT} {\tt ALLBRANCH} {\tt ALLFAC}
  6619. {\tt BFSPACE} {\tt COMBINEEXPT} {\tt COMBINELOGS}
  6620. {\tt COMP} {\tt COMPLEX} {\tt CRAMER} {\tt CREF} {\tt DEFN} {\tt DEMO}
  6621. {\tt DIV} {\tt ECHO} {\tt ERRCONT} {\tt EVALLHSEQP} {\tt EXP} {\tt
  6622. EXPANDLOGS} {\tt EZGCD} {\tt FACTOR} {\tt FORT} {\tt FULLROOTS} {\tt GCD}
  6623. {\tt IFACTOR} {\tt INT} {\tt INTSTR} {\tt LCM} {\tt LIST} {\tt LISTARGS}
  6624. {\tt MCD} {\tt MODULAR} {\tt MSG} {\tt MULTIPLICITIES} {\tt NAT} {\tt
  6625. NERO} {\tt NOSPLIT} {\tt OUTPUT} {\tt PERIOD} {\tt PRECISE} {\tt PRET}
  6626. {\tt PRI} {\tt RAT} {\tt RATARG} {\tt RATIONAL} {\tt RATIONALIZE} {\tt
  6627. RATPRI} {\tt REVPRI} {\tt RLISP88} {\tt ROUNDALL} {\tt ROUNDBF} {\tt
  6628. ROUNDED} {\tt SAVESTRUCTR} {\tt SOLVESINGULAR} {\tt TIME} {\tt TRA} {\tt
  6629. TRFAC} {\tt TRIGFORM} {\tt TRINT}
  6630. \item[Other Reserved Ids] {\tt BEGIN} {\tt DO} {\tt
  6631. EXPR} {\tt FEXPR} {\tt INPUT} {\tt LAMBDA} {\tt
  6632. LISP} {\tt MACRO} {\tt PRODUCT} {\tt REPEAT} {\tt SMACRO} {\tt
  6633. SUM} {\tt UNTIL} {\tt WHEN} {\tt WHILE} {\tt WS}
  6634. \end{list}
  6635. \printindex\end{document}