如何包含和使用 math.js

新手上路,请多包涵

我正在尝试使用 math.js ( http://mathjs.org/docs/reference/functions/inv.html ) 但我看不到如何将它包含在 HTML 文件中。

我读过您可以通过将其添加到文件中来包含它:

 var math = require('mathjs');

但这对我不起作用

我意识到这听起来像是一个非常愚蠢的问题,但我试图自己弄清楚但没有成功。

要回答我的问题,只需显示一个成功使用 math.js 的完整 HTML 文件(而不仅仅是其中的一些代码)

谢谢

我尝试了 Alexander O’Mara 的建议,我得到了这个:

 /** * Math.js can easily be extended with functions and variables using the * `import` function. The function `import` accepts a module name or an object * containing functions and variables. */ // load math.js (using node.js) var math = require('../index'); /** * Define new functions and variables */ math.import({ myConstant: 42, hello: function (name) { return 'hello, ' + name + '!'; } }); // defined methods can be used in both JavaScript as well as the parser print(math.myConstant * 2); // 84 print(math.hello('user')); // 'hello, user!' print(math.eval('myConstant + 10')); // 52 print(math.eval('hello("user")')); // 'hello, user!' /** * Import the math library numbers.js, https://github.com/sjkaliski/numbers.js * The library must be installed first using npm: * npm install numbers */ try { // load the numbers.js library var numbers = require('numbers'); // import the numbers.js library into math.js math.import(numbers, {wrap: true, silent: true}); if (math.fibonacci) { // calculate fibonacci print(math.fibonacci(7)); // 13 print(math.eval('fibonacci(7)')); // 13 } } catch (err) { console.log('Warning: To import numbers.js, the library must ' + 'be installed first via `npm install numbers`.'); } /** * Import the math library numeric.js, http://numericjs.com/ * The library must be installed first using npm: * npm install numeric */ try { // load the numeric.js library var numeric = require('numeric'); // import the numeric.js library into math.js math.import(numeric, {wrap: true, silent: true}); if (math.eig) { // calculate eigenvalues of a matrix print(math.eval('eig([1, 2; 4, 3])').lambda.x); // [5, -1]; // solve AX = b var A = math.eval('[1, 2, 3; 2, -1, 1; 3, 0, -1]'); var b = [9, 8, 3]; print(math.solve(A, b)); // [2, -1, 3] } } catch (err) { console.log('Warning: To import numeric.js, the library must ' + 'be installed first via `npm install numeric`.'); } /** * By default, the function import does not allow overriding existing functions. * Existing functions can be overridden by specifying option `override: true` */ math.import({ pi: 3.14 }, { override: true }); print(math.pi); // returns 3.14 instead of 3.141592653589793 /** * Helper function to output a value in the console. Value will be formatted. * @param {*} value */ function print (value) { var precision = 14; console.log(math.format(value, precision)); }

原文由 fred 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 809
1 个回答

最简单的方法是从 CDN 中包含 math.js

 <head>
    <script src=https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.3.0/math.min.js></script>
</head>

您需要的每个脚本,在 Google 中搜索 CDN(在此示例中 mathjs cdn ,并将其包含到您的页面 HEAD 中。这样您就不需要将任何内容下载到您的服务器。


有关 CDN 的更多信息:

Google 托管库是一个稳定、可靠、高速、全球可用的内容分发网络,适用于最流行的开源 JavaScript 库。

Google 直接与每个图书馆工作的主要利益相关者合作,并在发布最新版本时接受它们。

每个人都喜欢 Google CDN,对吗?甚至 Microsoft 也运行自己的 CDN。问题是,他们只托管最受欢迎的图书馆。我们托管所有流行的库 - JavaScript、CSS、SWF、图像等!现在就在 GitHub 上加入我们吧!

原文由 Aminadav Glickshtein 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏