diff --git a/examples/advanced/custom_argument_parsing.js b/examples/advanced/custom_argument_parsing.js index 19198b8df3..ea2bc838f4 100644 --- a/examples/advanced/custom_argument_parsing.js +++ b/examples/advanced/custom_argument_parsing.js @@ -59,13 +59,13 @@ integrate.transform = function (args, math, scope) { // create a new scope, linked to the provided scope. We use this new scope // to apply the variable. - const fnScope = Object.create(scope) + const fnScope = new Map(scope) // construct a function which evaluates the first parameter f after applying // a value for parameter x. const fnCode = args[0].compile() const f = function (x) { - fnScope[variable] = x + fnScope.set(variable, x) return fnCode.evaluate(fnScope) } diff --git a/examples/advanced/custom_loading.js b/examples/advanced/custom_loading.mjs similarity index 90% rename from examples/advanced/custom_loading.js rename to examples/advanced/custom_loading.mjs index 5c0d2704aa..4ec327e1d0 100644 --- a/examples/advanced/custom_loading.js +++ b/examples/advanced/custom_loading.mjs @@ -29,5 +29,5 @@ console.log('d =', format(d)) // outputs "d = 7/9" // Now, when bundling your application for use in the browser, only the used // parts of math.js will be bundled. For example to create a bundle using Webpack: // -// npx webpack custom_loading.js -o custom_loading.bundle.js --mode=production +// npx webpack custom_loading.mjs -o custom_loading.bundle.js --mode=production // diff --git a/examples/advanced/more_secure_eval.js b/examples/advanced/more_secure_eval.js index 46329ba7c3..650c26883c 100644 --- a/examples/advanced/more_secure_eval.js +++ b/examples/advanced/more_secure_eval.js @@ -33,4 +33,9 @@ math.import({ }, { override: true }) console.log(limitedEvaluate('sqrt(16)')) // Ok, 4 -console.log(limitedEvaluate('parse("2+3")')) // Error: Function parse is disabled +try { + console.log(limitedEvaluate('parse("2+3")')) // Error: Function parse is disabled + console.log('Oops, it worked!') +} catch (err) { + console.log(err.toString()) +} diff --git a/test/node-tests/examples.test.js b/test/node-tests/examples.test.js new file mode 100644 index 0000000000..b7e33ded2d --- /dev/null +++ b/test/node-tests/examples.test.js @@ -0,0 +1,45 @@ +// Only use native node.js API's and references to ./lib here, this file is not transpiled! +const assert = require('assert') +const cp = require('child_process') +const path = require('path') +const fs = require('fs') + +describe('examples/', () => { + const exampleDir = path.resolve(__dirname, '../../examples/') + const referenceDir = path.resolve(__dirname, './examples/') + const rawExamples = fs.readdirSync(exampleDir, { withFileTypes: true }) + const examples = [] + for (const example of rawExamples) { + if (example.isDirectory()) { + const subDir = path.join(exampleDir, example.name) + const subFiles = fs.readdirSync(subDir) + for (const subFile of subFiles) { + if (subFile.substr(-3) === '.js') { + examples.push(path.join(example.name, subFile)) + } + } + } else { + if (example.name.substr(-3) === '.js') { + examples.push(example.name) + } + } + } + for (const example of examples) { + it(example, (done) => { + const source = path.join(exampleDir, example) + const outfile = path.join(referenceDir, example + '.out') + const expected = fs.readFileSync(outfile).toString() + let expectError = '' + const errfile = path.join(referenceDir, example + '.err') + if (fs.existsSync(errfile)) { + expectError = fs.readFileSync(errfile).toString() + } + cp.exec('node ' + source, (error, stdout, stderr) => { + assert.strictEqual(error, null) + assert.strictEqual(stderr, expectError) + assert.strictEqual(stdout, expected) + done() + }) + }) + } +}) diff --git a/test/node-tests/examples/advanced/convert_fraction_to_bignumber.js.out b/test/node-tests/examples/advanced/convert_fraction_to_bignumber.js.out new file mode 100644 index 0000000000..bb97d45d6d --- /dev/null +++ b/test/node-tests/examples/advanced/convert_fraction_to_bignumber.js.out @@ -0,0 +1,3 @@ +Fraction 7/12 +BigNumber 2 +BigNumber 3.4 diff --git a/test/node-tests/examples/advanced/custom_argument_parsing.js.out b/test/node-tests/examples/advanced/custom_argument_parsing.js.out new file mode 100644 index 0000000000..837a20d184 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_argument_parsing.js.out @@ -0,0 +1,3 @@ +0.6667254718034714 +0.6667254718034714 +4.000000000000003 diff --git a/test/node-tests/examples/advanced/custom_datatype.js.out b/test/node-tests/examples/advanced/custom_datatype.js.out new file mode 100644 index 0000000000..8f62f67938 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_datatype.js.out @@ -0,0 +1,2 @@ +CustomValue:5 +CustomValue:9 diff --git a/test/node-tests/examples/advanced/custom_evaluate_using_factories.js.out b/test/node-tests/examples/advanced/custom_evaluate_using_factories.js.out new file mode 100644 index 0000000000..8351c19397 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_evaluate_using_factories.js.out @@ -0,0 +1 @@ +14 diff --git a/test/node-tests/examples/advanced/custom_evaluate_using_import.js.out b/test/node-tests/examples/advanced/custom_evaluate_using_import.js.out new file mode 100644 index 0000000000..8351c19397 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_evaluate_using_import.js.out @@ -0,0 +1 @@ +14 diff --git a/test/node-tests/examples/advanced/custom_relational_functions.js.err b/test/node-tests/examples/advanced/custom_relational_functions.js.err new file mode 100644 index 0000000000..85f0f8c7b7 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_relational_functions.js.err @@ -0,0 +1,2 @@ +"a" == "b" Error: Cannot convert "a" to a number +"a" == "a" Error: Cannot convert "a" to a number diff --git a/test/node-tests/examples/advanced/custom_relational_functions.js.out b/test/node-tests/examples/advanced/custom_relational_functions.js.out new file mode 100644 index 0000000000..ca803fe417 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_relational_functions.js.out @@ -0,0 +1,9 @@ +default (compare string by their numerical value) +2 < 10 true +"2" < "10" true + +custom (compare strings lexically) +2 < 10 true +"2" < "10" false +"a" == "b" false +"a" == "a" true diff --git a/test/node-tests/examples/advanced/custom_scope_objects.js.out b/test/node-tests/examples/advanced/custom_scope_objects.js.out new file mode 100644 index 0000000000..2dcc27eef3 --- /dev/null +++ b/test/node-tests/examples/advanced/custom_scope_objects.js.out @@ -0,0 +1,31 @@ +Object scope: { + x: 3, + y: 6, + scalar: 1, + area: [Function: area] { + signatures: { 'any,any': [Function (anonymous)] }, + syntax: 'area(length, width)' + }, + A: 18 +} +Map-like scope (simple Map): undefined +Map-like scope (MapScope example): Map(5) { + 'x' => 3, + 'y' => 6, + 'scalar' => 1, + 'area' => [Function: area] { + signatures: { 'any,any': [Function (anonymous)] }, + syntax: 'area(length, width)' + }, + 'A' => 18 +} +Map-like scope (AdvancedScope example): Map(5) { + 'x' => 3, + 'y' => 6, + 'scalar' => 1, + 'area' => [Function: area] { + signatures: { 'any,any': [Function (anonymous)] }, + syntax: 'area(length, width)' + }, + 'A' => 18 +} diff --git a/test/node-tests/examples/advanced/expression_trees.js.out b/test/node-tests/examples/advanced/expression_trees.js.out new file mode 100644 index 0000000000..eb7d18fed7 --- /dev/null +++ b/test/node-tests/examples/advanced/expression_trees.js.out @@ -0,0 +1,13 @@ +Filter all symbol nodes "x" in the expression "x^2 + x/4 + 3*y" +SymbolNode x +SymbolNode x + +Traverse the expression tree of expression "3 * x + 2" +OperatorNode + +OperatorNode * +ConstantNode 3 +SymbolNode x +ConstantNode 2 + +Replace all symbol nodes "x" in expression "x^2 + 5*x" with a constant 3 +3 ^ 2 + 5 * 3 diff --git a/test/node-tests/examples/advanced/function_transform.js.out b/test/node-tests/examples/advanced/function_transform.js.out new file mode 100644 index 0000000000..253647a494 --- /dev/null +++ b/test/node-tests/examples/advanced/function_transform.js.out @@ -0,0 +1,7 @@ +Using expression parser: +input: a=2, b=4 +result: 6 +2+4=6 + +Using plain JavaScript: +2+4=6 diff --git a/test/node-tests/examples/advanced/more_secure_eval.js.out b/test/node-tests/examples/advanced/more_secure_eval.js.out new file mode 100644 index 0000000000..27f7fea3e3 --- /dev/null +++ b/test/node-tests/examples/advanced/more_secure_eval.js.out @@ -0,0 +1,2 @@ +4 +Error: Function parse is disabled diff --git a/test/node-tests/examples/advanced/use_bigint.js.out b/test/node-tests/examples/advanced/use_bigint.js.out new file mode 100644 index 0000000000..d6e76648cb --- /dev/null +++ b/test/node-tests/examples/advanced/use_bigint.js.out @@ -0,0 +1,3 @@ +9598 +9598n +805379990314524847071706061374175615743750676558197679651390868354455970396216396052484734015148048629597960198906183251579692980333245834578400487792968292635039894880866709312250952842846108542629205066119072051640876259972504887196222775478550717298588174264347891219765331637302429347373485705467424651748138838234358614452358781077420055365962828090942014792741489700281457142935362316603197135825234031552344931307905798698445809961128191784704470456199531912333114859913890248348200651060941118764804087356278852576716385500276923488396608942249697809207717015716720648252385211329596348438513911614856035111686487703861764097125640225308998337304586358087884514659296178432147077333105618781318345771164334797561488689681159140919547745795161564758487116405300875129969297183250742338357681025743713281633896067800401860166790738535520022386337328460307018244506581133258450568461639375040281109861594744295536007692981109006746148324366497736113204826806438634426759868499578187530416967554934470115612132790440411209094769821812389081816574472559566647479305444179995363282213092606479655536394945506379436781446320720166108343340978363661089593105141814723736435794926124771230348207120293396983022296469890990722637521725534614155477148868503850878449136694754962434095986358894837775578604149409088477334794724206605462246699043589971927177042877465060503361165456369384538535694916950642287922898551338190910379699265107941787522744955355028561220008547656226155037868615784162975454897983473834127652554458036643826755828487611935698500481657190834549060336969836797083222894583050705678742515012054939866694939832122350747279872853716132410925279113318964936814056089727464145772614940864953608875917722011031097137896003486418033267961006733361912230433723227053877210713166620206313635723178201529354111356323983181808380898358170625858717808092230070693586026582498471787014470261548829764779531670513183372087764469734315798316787417464054852717010952463901399323718886477825273820356651109265662242195454127360924209878578398481265724006991375248482483153094981959505498492606995981043228903096977308034831960179181881430450431685963914632817452122792418443517498208125732127934750699771537829194502730434021397844131676634258089083940584633633120893476149102821622420132634039799687965582938921879387081857181692994533140777273524680975994913398456346683157670074090006611642369155617077954368916353601803517157009668045919049469419028748489639142318064716982660556755844277036312678465775301010665489057841260465006709617889487744172229612025118803734234759260173983761911067136030710431273625673826894462594111127950161031348647018573613016632128575711957928854373859613964889301194418678744462000161483803935929105273444687574457138330467927309742525061188987340914457883594638662326707350029246821156765483244935894117942225646432743340742994989497473382650437588193056770530166357274036544391949362010872800587548945859270823908957468945528235402393512362034715545007040995092712115806475838512697812102832342060875748159342486479467420383152781240671511467198001315120843951928925745862408791587942575051424570869820084700369432256464451071672837525998378445581152499782018864341946264833159689275400940454583834981467923766306888421909702283825389098275269687332875014092540731275608699431550081102929330096803068000878727891511144181526542324808480921947442374199016174112424053662158710697834166425238873347285563967545931424906594252773601020402572777207443431544090936381797315200776807437784430077444968197753811571441363575167660276211038412993016843321552474510680845385709891679219580776173107355508970712301727196380243324342187799416311367609283524651928081547188248918999326644853447114019416238583994971632005358620116971203870706507133471339269900493761843912119713014778150820261819757056477093870576918024744251496932261178559236979155269607476188474728783235375045309394400991878399093150314539718587888715728654817286110698666683940192895012465952681045840176821171983619152949375501226942950972764425427270155530762280123513114226802677941436325160998269612116882898899201689884562586067067798536172584116981554902326921484287341432095358240298002420711392247420022589957440128900884205931764423473587691244638605535288919224952564383408775994262326961129385042038575512825160067304212720456529604815146591099735903239254558973267220789129874502505199795745778500798299449394172202079273259933509227220565598819281691601918086298160617312357670243659692360178962145189533834077309334065805084210900931009312518559041448895429606694464733028280684313805842929660737108596421662320510959910964107519101029551549183916370409195758467784515550108703501067066299380246101524782289664369722318101028988393065297971616836319108757408766361070148722353198704621780392349844223632097910055947710296287823280649862224530456198383639051296293495153867368421018720956515441752257286975563028250051282189943353720731053866125628656161344200537897079479409844446076867745551445548514287106537646780833103131375807854132070934384013523609636021763598092742957423939974177904448802018406314311282994380515289932198502617454168313493863884499738619752544604489998172690998434190173746534432307630130001130200433063669274938827365514310851994396459367436506480768626572148188790828843615145409006550147897400826887917911929402045776037452959731078801679509776124682821690368755441617840913132378461305903817850758244624730003545784978016911112839753576483937617661194348991180666412156652191368488257796282210555796852109371656427111696930694814258826609988705021377526236376687251652433635467984193072900431850235950005671219961016326099778670650266228557439458902567297560729024303680246883823774069952208191247943599870651825512346498248147181383999541835805908283992446133713106373645009395897625301073864411046581629459439912004284098371835034883568145108299686846928518951680722877811559985307162386838982719711455010545366884883579875763536205007943398584312227834240553872598494398133102264658476592743069434200705905061881459876350306101919902174381407182344972316973945207950708917514250616506954879748417144164412051677373650826915366725656377881064230257949512253448764420447945330465249727408793636139090460383797428408663252148281147133654293592061228763938141425857529162182276009588044526706088090926607012006813574299122129334963881666175794281409205635899157280973296954339125346022835722704154541849652063626994609641588505815870573136715713661566406950809269061859718416001884792951512019152012032071963200232736967416802757845197037494299904773542399814566622029712536045775743042034351341849334183186200574027865249920692994137138250767875962657599659475638381431425458051866291172499384384388938515477978171332564682654663241625504960813741214252304168305599393770137268651777306223858038361262272349064081024127302498197229542538906703239255746099197992458893484942860167405980596599370951689461079132524650207486649980955126797041351953553263803794927103262347067454798507995927186103703810792056928463254855233090170936738260633015747976113342812769786360467341063178852091963952700687691462863849553410559578873363570403319160579751409828940648624741576712490533161255688554492772368279705583328058290329029532162191815074755653834883062809628011286919180140887477907088494788316365196907463036371673115647025090091604932758136558968016728214378383611236770818141687733843679835772111761404694631691223086509004987700348618569830475246421009588787635160779232096902561523574558158192333996608230365113123969589625351858405479673800927014835526741789936401322682918032278742680507763174603622109009684716566219248148231349126907325471977653708763174075088456815746820889101851508368289691003699721666883127946333736933710404032723170147384965980989007097465414071517592496603001933130952758992185877791880151335651234379362068576906229486199127577013210306550978940114325853619819530016592411864431331396312890237425921931105704346849036649779600869696011811743665462142360199136344303373760482867564823974886422684072503349094950347464692904792167166939729825969261816483110353283584531722623247779340391050274165212564192278940376933216174359131959719845079731725516952771946296621816776724294008319778495739601742116365692785763911003679474941003446794939983920405204293718234200883064938241680651725166275164089684938085044973883903941135358054032942341649935428203791115095149058599551910073766957943552866326748575012936981790824004887771079308208710903510144834355268826789838525989766759925293412293146081117836893374310424678523483464804054706303941889833183393629052242010147935876453121045429003916936860776105527856445400824546702219063451506731480963509988147184101947085607166500078476257486386883055353887956356014117371801648302640116497204544710684004361365186597302049740370302849474725341191930172378455558628216671080101853556808292682031068849014957014810170702440208140821692534946678280953926305239919975465894661649327134198937712341958412992394371924036495385684433819497168344147368580624695858042899860346573561815515560408837196606748655743234131313494741877312394733606456231957658048982831986975820716807370966189413164627429610348192372553491464886671476556502716398566284789637619797839031834138878286848459677279186306531749989991297933537746259507464121605964543002128780858731767125210864120794690749187251719736054418099248837633858258501875883632802694700365686180842462677220528086094719839323605444611015091485553582849496565785967751099749046247434197080109412035169423830074591740467297623704961225441885565874346248758055043430060237970185473121362211352350783961816537200986817360467837611263927931087353225654219504434441841170737352433531442662244741481145701298716987240800836394272659727085399692249476394091100192272940131168468787421527932032002552934451466468143665262702824296195952256570639103624783453078475136736521780837026348868042263722044440491437268413035318922769862845767329154501013066625565510461773517749662196299583304480866859589736405067834087846546609552353690385790767090819735190162307319029336260148901733018164282056356202809578903358246567034835945677336022124393108784707050482975668836442002106855927108947630918360297798661276930403629084019396278265657382792918949317847699585870633609604653865853547125029270898563572307151861230857670925516678514928232353435823020315743913425921564567307468036031544363441409572949538898585228607612609049948888563172953025692594060144745100789851270234141906580639028066412262380031754308453263060156339043156084315584146708931173763780126329110775999400847721925027506017015565539989049436417994403643992492724884398874645399185617239783109893722437282096796941882933913351326719237083142014269972819192786465058056950701602242519607858090803803728763795762619160860850822112917069926507233622736108899873879332506579896873850599155558372765880762042993642746730746778511208726392409664873467976653504441440256682080589078982405408855596650201685511737383229543650168024108142780911388840969645117788695013824335215727292531696603002509544923999100113943263677207123094025026998261901123096643602577157667497768459472230267741636229476313071163002274650724556715962773787553035619820662584882091630785952809496306062954697183235337184400894063744301675806008213252203698897762223824282053563912665355486747532406407117907959923290400812425708026067918139699650062779385584483698034474006821735466082618721887526306235769729778254413938991397741978284051287377601258600691897632327084379792700198059881173319818725530300216068624652702158303242165694167325824595341416150254290180786048422061051681058412732912548220826650924920837633620848624381880022107001724338118292604051822461166661139115060434588348195181168079760559187623259070773830035289749879010254808065599699018263264191675606290679421953406646050726655143938257208632490668889075185523526260258508786929407581317757751872987714704693328320862532460823900381998240969886836132164094644312526332997036709296733966808276211686588292571801406645264261831388876391867860098663117919465554116257252110891569413784646368431039681337058943441594603907651008615309520467912171150253683575624845786225424463876896189686806576132434780177250121972891747402302560945921818147850150789381995138962387915417870570136033859369525910196212324168681850912515335025262878835980957980268921096094957944471823640892119182808339169528563559860904818970140214860673102393191339663895786188455743576025923502397788903389628747250535434796108355530751123689679410275974877852408016407621511952646934806727892800407892159329071476001553594129125993402251100678856058788766133107661119033254132814159247937467790352141218473569556256170562606272299700159624997676559710490616498927309515568146046101742379526490619955606708091018843807898227823588051947928313110088541416575327562134271553558449030948800023336155026625785207072538450999644313011066785205093699151372116292823106685550605878856591484166529596954020725617727028000480540850486213293226183116190387407729039978953796928469324848349143686197271967320915161751791180455975630460284334529159191557077492493115548401275515722245011804060132157340713564866660163427308813024949198559865371544922326234965098081177014160219879742796887475565529771809579944789950065430185862854765663750977336727109474956333946684423798124675001263250276133442958039225717275869267933356083324149715164461911224261927981366342422276138067467608035809559808498848191792595431172174627330923025772804582636341966096449728754966317810798446134062859259668965327754423149782180275800503846699188074723005713601771926216618621917621973634119145938567842648899964232872674622859526773372776605474031142683840217278800325571993209182296265022086412223833348391847331027338775241290902755637363033159604175422832488476805403710159963218316478902922068497067814499685661538223004480156563078868003854298696062750673264948752637387097706327131941231747037756822722522656813155281185188338165317473176831028208707434511556686030645078761782627396084721673552724973208949258170957562287026584900505318376678268580706021426286501705156300540020055569576437116382045809397208965055453830216166168650610296546660936154078113038011917403108757334746967880515629737899097598343226843659160193376475117864025385452704560996287193775176164365268800550328841770954960988046699482880786832270713442782823499884234146002074035907886446093865759987767839901921008258685421047187419830646682803122209575193542945101246793023364657750320288363108700036430902143002795404794631834155474939065193094514987506593797269322651158834454321474402484843331277454737207458318842535205763291913704621933829207557316285265852665111906231719231677799013070309510063912147639365590456602353395975253541598182980624585906781811188999296353168837284880279030907665879701825697739176614252340710685307647274356558654805560268868104075843560192203186485815807376500482973480584587385696805900161956254294826528426944731484073855031296773115258462565880669237851808590271579861760791319179046811686162517869592185088672077066348477275715045800468597298170656296371711249477803812021146394587104950278642700818323225667660405453093145091427545001334232934876467704998713246941603282439300581346356418147653437823076211207481458188349853218497949432450797836238006427490427619895583618177554715060322459727978463623529613839302061645099939841832984275595844739423607530041868236068715542494798956237611544464067293074461505778592980917525933051335638514745465295591959967543945600901158768879421210155695815404884271584104768537884814569226716616730338526690530181217347591144172238550863381650433093534907984111124997607363508611065232854136489765319187790045253438333876078493169162045733149274416229263515539460537064987501892578647733317530918528634772125002893459746863469570249589623933807886557778886224138054263502664396753630392670945167839784325399460137081301167460837801660757717673291112639363950519302632018876141934191007002916874646428788987440464011128226183445040219496806298162226280232167713210006426200249724967059183944066181708497746526525082311915254433963761380410197342671826762654674353446952259207123967042028635916479269725813048263030614886451529877599629841721503061666269384970435407365436911228976527605982618139809724409024466886780504897753002192227783245323250177407075007486354320499002129414260550924827686300906725621419988337443801110557274931965052585993836151291475532946301004398552561793228301998227790445925809633470373022267770280323138183419065683555626603916221003882310048643201703772571061224659594094477549458949175949746559835447309333620807602823772348793511166942727726837180529321063741009973803066308162485589478575887463728381586159683974979960427675022724339569846757605500582283704771504198862332844240033052533075999252851687358381041223348082937352052588286406508837600135014287095538395070179334425566642090426064199568807450556008053553669019722802971863607365252390070678582363993082575878218394794048041850119462543253146634349932042072989988941452832863415539064951702590463152092304492237921768572573849741441417203712611384593353034646823519532917377159444122166810205889493021551884265309563014104015653763005035463237549282377087399455431796250554151804016823200490481610620397009675466805532203464664259700456963549037489861355895289079740689654452070370038627133868451535935828995481916346537372193066250464881705105087130602212463722052860722879379520242705617489478566204840191094276842341133869301363905778788427636859818135358942815840964076241224345052237993036672764325931689181130399166642988040234000649993186440000374230922983019668674475112489790544380973720621809336052912091596983108760919134121708765019584140451176542654519062156538936998191740181550861792251049634743625712806443950772341755846188024012208126697560719425291347713954321580049899695794323459001133477107333027130536802679345420877157455313679435784115113933761586832753765227785397724127065478773057043118953225018256390871179848772796703412823734656539905880215065035894308194262508117444586406066323908580439309636812293303533200725389391499620725760110873601054536263830502077753901680985683052665697805087610125804761789910641863142593574958645730870764754605048226914168905535046869753696981949483630177097580560560484987948540543481114394184259859391596468813941490715063057747264261703961616424729334158507177810291142190481361429910154239781825406069123522938367400279679102930041466818440712836925825496284867709944009483318633787982886238354184317487986831452052834487387820129240143339906491178997034920981463234278361556316755807121531759196147186290465624128813644886676283093441749127198915111407380145785101898983217694116319801742358519150087847744788628745280437656216941209226935113965697538004863099121745235700871156451319401571818284922442262765049835195662075707727322228800052507901421479608312331929945669393561234300741461162076663019709466418896589372898010561238572829162246881576409683370822080604984719699473149n diff --git a/test/node-tests/examples/algebra.js.out b/test/node-tests/examples/algebra.js.out new file mode 100644 index 0000000000..aca127ff41 --- /dev/null +++ b/test/node-tests/examples/algebra.js.out @@ -0,0 +1,14 @@ +simplify expressions +7 / 2 +5 * x +24 +2 * x ^ 2 + x + 3 +-y +3 * x +12 + +calculate derivatives +4 * x + 3 +2 * cos(2 x) +2 * x + 1 +7 diff --git a/test/node-tests/examples/basic_usage.js.out b/test/node-tests/examples/basic_usage.js.out new file mode 100644 index 0000000000..8d21337b72 --- /dev/null +++ b/test/node-tests/examples/basic_usage.js.out @@ -0,0 +1,24 @@ +functions and constants +2.718 +0.75 +4 +2i +[[7, 0], [0, 7]] +2 * x + 1 + +expressions +7.8 +5 inch +0.5 +3 + 2i +-7 + +chained operations +14 + +mixed use of data types +[9, 10] +15 mm +[-3, -2, -1] +[6, 8] + diff --git a/test/node-tests/examples/bignumbers.js.out b/test/node-tests/examples/bignumbers.js.out new file mode 100644 index 0000000000..6ad436a35f --- /dev/null +++ b/test/node-tests/examples/bignumbers.js.out @@ -0,0 +1,19 @@ +round-off errors with numbers +0.30000000000000004 +1.4999999999999998 + +no round-off errors with BigNumbers +0.3 +1.5 + +create BigNumbers from strings when exceeding the range of a number +Infinity +1.2e+500 + +BigNumbers still have a limited precision and are no silve bullet +0.99999999999999999999 + +use BigNumbers in the expression parser +0.3 +1.5 + diff --git a/test/node-tests/examples/chaining.js.out b/test/node-tests/examples/chaining.js.out new file mode 100644 index 0000000000..4a1789ebbf --- /dev/null +++ b/test/node-tests/examples/chaining.js.out @@ -0,0 +1,7 @@ +14 +0.5 +"0.6666666666666666" +0.66666666666667 +2.6666666666667 +3 +[[24, 6], [9, 12]] diff --git a/test/node-tests/examples/complex_numbers.js.out b/test/node-tests/examples/complex_numbers.js.out new file mode 100644 index 0000000000..677f94297e --- /dev/null +++ b/test/node-tests/examples/complex_numbers.js.out @@ -0,0 +1,24 @@ +create and manipulate complex numbers +2 + 3i +2 +3 +2 + 3i +5 + 3i +3 - 7i + +perform operations +8 - 4i +36 - 26i +-9.6541254768548 + 2.8416922956064i +2 +2i + +create complex numbers with polar coordinates +1 + i +5 0.9272952180016122 + + +comparision and sorting operations +equal false +values: [5 + 3i, 3 - 7i, 1 + i] +sorted: [1 + i, 3 - 7i, 5 + 3i] diff --git a/test/node-tests/examples/expressions.js.out b/test/node-tests/examples/expressions.js.out new file mode 100644 index 0000000000..6237310faa --- /dev/null +++ b/test/node-tests/examples/expressions.js.out @@ -0,0 +1,69 @@ +1. USING FUNCTION MATH.EVAL + +evaluate expressions +5 +2i +5.08 cm +0.70710678118655 + +evaluate multiple expressions at once +[3, 4, 12] + +evaluate expressions providing a scope with variables and functions +12 +6.8 +6.8 +"hello, hero!" +8 +8 + +2. USING FUNCTION MATH.PARSE + +parse an expression into a node tree +"sqrt(3 ^ 2 + 4 ^ 2)" +5 + +provide a scope +"x ^ a" +9 +27 + +3. USING FUNCTION MATH.COMPILE + +compile an expression +5 + +provide a scope +10 + +4. USING A PARSER + +evaluate expressions +5 +2i +5.08 cm +0.70710678118655 + +define variables and functions +3.5 +6.5 +f2(x, y) +8 + +manipulate matrices +[[1, 2], [3, 4]] +[[0, 0], [0, 0]] +[5, 6] +[[5, 6], [0, 0]] +[7, 8] +[[5, 6], [7, 8]] +[[19, 22], [43, 50]] +43 +[[19], [43]] + +get and set variables and function in the scope of the parser +x = 3.5 +f2 = f2(x, y) +h = 27 +250 +"hello, hero!" diff --git a/test/node-tests/examples/fractions.js.out b/test/node-tests/examples/fractions.js.out new file mode 100644 index 0000000000..cd8332ef81 --- /dev/null +++ b/test/node-tests/examples/fractions.js.out @@ -0,0 +1,32 @@ +basic usage +1/8 +8/25 +1/3 +1/3 +2/3 +2/7 + +round-off errors with numbers +0.30000000000000004 +1.4999999999999998 + +no round-off errors with fractions :) +0.3 +1.5 + +represent an infinite number of repeating digits +0.(3) +0.(285714) +2.(09) + +use fractions in the expression parser +3/10 +3/2 +23/11 + +output formatting of fractions +2/3 +2/3 +0.(6) +0.(6) + diff --git a/test/node-tests/examples/import.js.out b/test/node-tests/examples/import.js.out new file mode 100644 index 0000000000..f824650aa7 --- /dev/null +++ b/test/node-tests/examples/import.js.out @@ -0,0 +1,7 @@ +84 +"hello, user!" +52 +"hello, user!" +Warning: To use numbers.js, the library must be installed first via `npm install numbers`. +Warning: To use numeric.js, the library must be installed first via `npm install numeric`. +3.14 diff --git a/test/node-tests/examples/matrices.js.out b/test/node-tests/examples/matrices.js.out new file mode 100644 index 0000000000..579553b610 --- /dev/null +++ b/test/node-tests/examples/matrices.js.out @@ -0,0 +1,42 @@ +create a matrix +[1, 4, 9, 16, 25] +[[1, 1, 1], [1, 1, 1]] +[2, 3] +[1, 4, 9, 16, 25] +[1, 4, 9, 16, 25] + +perform operations +[1, 2, 3, 4, 5] +[1, 2, 6, 24, 120] + +manipulate matrices +[[1, 2], [3, 4]] +[[5, 6], [1, 1]] +[[5, 6], [7, 8]] +[[19, 22], [43, 50]] +43 + +get a sub matrix +[[1, 0, 0], [0, 2, 0], [0, 0, 3]] +[[2, 0], [0, 3]] +[1, 2, 3, 4, 5] +[2, 3, 4] + +resizing a matrix +[[[0, 0], [0, 0]], [[0, 0], [0, 0]]] +[2, 2, 2] +[[0, 0], [0, 0]] +[2, 2] + +set a value outside a matrices range +[0, 0, 6] + +set a value outside a matrices range, setting other new entries to null +[null, null, 6] + +create ranges +[1, 2, 3, 4, 5] +[0, 3, 6, 9, 12, 15] +[2, 1, 0, -1, -2] +[1, 2, 6, 24, 120] + diff --git a/test/node-tests/examples/objects.js.out b/test/node-tests/examples/objects.js.out new file mode 100644 index 0000000000..bb34d0b3a6 --- /dev/null +++ b/test/node-tests/examples/objects.js.out @@ -0,0 +1,8 @@ +{"x": 3, "y": 4} +{"name": "John"} +{"a": 2, "b": {"c": 3, "d": 4}} +42 +42 +43 +43 +{"prop": 43} diff --git a/test/node-tests/examples/serialization.js.out b/test/node-tests/examples/serialization.js.out new file mode 100644 index 0000000000..667d776468 --- /dev/null +++ b/test/node-tests/examples/serialization.js.out @@ -0,0 +1,3 @@ +{"mathjs":"Complex","re":2,"im":3} +Unit +5 cm diff --git a/test/node-tests/examples/sparse_matrices.js.out b/test/node-tests/examples/sparse_matrices.js.out new file mode 100644 index 0000000000..61c43730ea --- /dev/null +++ b/test/node-tests/examples/sparse_matrices.js.out @@ -0,0 +1,5 @@ +creating a 1000x1000 sparse matrix... +doing some operations on the sparse matrix... +size(e)= [ 1000, 1000 ] +already done +now try this with a dense matrix :) diff --git a/test/node-tests/examples/units.js.out b/test/node-tests/examples/units.js.out new file mode 100644 index 0000000000..2fe259e98c --- /dev/null +++ b/test/node-tests/examples/units.js.out @@ -0,0 +1,49 @@ +create units +45 cm +0.1 m + +perform operations +55 cm +0.2 m +1 m / s +1728 in^3 + +convert to another type or to a number +10 cm +3.9370078740157 inch +10 +10 + +parse expressions +5.08 cm +0.70710678118655 +25 m / s + +50 + +simplify units +100 kPa +39.24 kJ + +compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol +gas constant (Rg) = 8.314 (N m) / (mol K) +P = 14.7 psi +T = 65 degF +v = Rg * T / P = 23.910432393453 L / mol + +compute speed of fluid flowing out of hole in a container +g = 9.81 m / s^2 +h = 1 m +v = (2 g h) ^ 0.5 = 4.42944691807 m / s + +electrical power consumption: +460 V * 20 A * 30 days to kWh = 6624 kWh + +circuit design: +24 V / (6 mA) = 4 kohm + +operations on arrays: +B (magnetic field strength) = [1 T, 0 T, 0 T] +v (particle velocity) = [0 m / s, 1 m / s, 0 m / s] +q (particle charge) = 1 C +F (force) = q (v cross B) = [0 N, 0 N, -1 N]