javascript - 如何包含和使用 math.js

标签 javascript 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)); }

最佳答案

您不应该在浏览器中使用 require 。你应该尝试这样的事情:

<!DOCTYPE HTML>
<html>
<head>
  <script src="math.js" type="text/javascript"></script>
</head>
<body>
  <script type="text/javascript">
    // use math.js
    math.sqrt(-4); // 2i
  </script>
</body>
</html>

查看文档:http://mathjs.org/docs/getting_started.html

关于javascript - 如何包含和使用 math.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38416492/

相关文章:

javascript - Internet Explorer 9 拖动事件 block 事件队列

javascript - 多重选择菜单值并用值更新表

javascript - 使用 Three.js 和 Fresnel Shader 可视化网格所有面的问题

javascript - Bootstrap 下拉元素未正确对齐

javascript - 无法使用 math.js 使 Lotka-Volterra 方程稳定振荡

node.js - NodeJS - 作为字符串循环遍历范围

javascript - 如何在 math.js 中实现 NOR、NAND、XNOR?

javascript - 如何在手机间隙打开图库而不是相机?

javascript - 为什么 math.js 的 math.simplify ("1e-10").toString() 返回 0?