首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何包含和使用math.js

如何包含和使用math.js
EN

Stack Overflow用户
提问于 2016-07-16 23:14:15
回答 4查看 21.2K关注 0票数 4

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

我读到过,您可以通过将以下内容添加到文件中来包含它:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
var math = require('mathjs');

但这对我不起作用

我知道这听起来是个很愚蠢的问题,但我试着自己解决这个问题,但没有成功。

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

谢谢

我尝试了Alexander O‘’Mara的建议,得到了这样的结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/** * 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)); }
EN

回答 4

Stack Overflow用户

发布于 2016-07-16 23:17:55

你不应该在浏览器中使用require。你应该试试这样的东西:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!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

票数 5
EN

Stack Overflow用户

发布于 2016-07-16 23:49:04

对您来说,最简单的方法是包含CDN中的math.js

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<head>
    <script src=https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.3.0/math.min.js></script>
</head>

每一个你需要的脚本,在谷歌中搜索CDN (在这个例子中是mathjs cdn,并将它包含在你的页面标题中。这样你就不需要下载任何东西到你的服务器上。

CDN的更多信息:

谷歌托管库

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

谷歌直接与每个库的关键利益相关者合作,并在最新版本发布时接受它们。

每个人都喜欢Google CDN,对吧?甚至微软也有自己的CDN。问题是,它们只托管最受欢迎的库。我们托管了所有流行的库- JavaScript,CSS,SWF,image,等等!现在就加入我们的GitHub吧!

票数 4
EN

Stack Overflow用户

发布于 2016-07-16 23:16:58

他们有一个restful API,你可以使用,http://api.mathjs.org。或者,您应该能够从here下载产品并将其包含在JS中。

在您的head标签中:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<script src="math.js" type="text/javascript"></script>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38416492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文