Javascript 如何包含和使用math.js

Javascript 如何包含和使用math.js,javascript,math.js,Javascript,Math.js,我正在尝试使用math.js(),但我不知道如何将其包含在HTML文件中 我已经了解到,您可以通过将以下内容添加到文件中来将其包括在内: var math = require('mathjs'); 但这对我不起作用 我意识到这听起来像是一个非常愚蠢的问题,但我自己也试图解决它,但没有成功 要回答我的问题,只需显示一个成功使用math.js的完整HTML文件(而不仅仅是其中的一段代码) 谢谢 我尝试了Alexander O'Mara的建议,得到了以下结果: /** * Math.js can e

我正在尝试使用math.js(),但我不知道如何将其包含在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)); }

他们有一个restful API,您可以使用。或者,您应该能够从下载产品并将其包含在您的JS中

在您的头标签中:

<script src="math.js" type="text/javascript"></script>

您不应该在浏览器中使用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>

//使用math.js
数学sqrt(-4);//2i

检查文档:

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

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

你需要的每一个脚本,在Google中搜索CDN(在本例中,将其包含到你的页面标题中),这样你就不需要下载任何东西到你的服务器上


有关CDN的更多信息:
谷歌托管的图书馆是一个稳定、可靠、高速的, 全球可用的内容分发网络,适用于最受欢迎的, 开源JavaScript库

谷歌直接与每个图书馆的关键利益相关者合作 努力并接受最新版本的发布

每个人都喜欢谷歌CDN,对吗?甚至微软也有自己的CDN 问题是,它们只托管最流行的库。我们 托管所有流行的库-JavaScript、CSS、SWF、图像等! 现在加入我们的GitHub

进口

初始化

用法


那么,您是否首先包含了
require.js
?这是commonjs(节点)方式,也许您需要浏览器示例。请尝试您的建议,查看我编辑的问题以了解我的结果
import { create, all } from 'mathjs'
const math = create(all,  {})
console.log(math.sqrt(-4).toString())