【问题标题】:how to include and use math.js如何包含和使用 math.js
【发布时间】:2016-07-16 23:14:14
【问题描述】:

我正在尝试使用 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)); }

【问题讨论】:

标签: javascript math.js


【解决方案1】:

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

【讨论】:

  • 刚刚尝试了您的建议,请查看我编辑的问题以了解我的结果
【解决方案2】:

对您来说最简单的方法是从 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 对吧?甚至微软也有自己的 CDN。问题是,它们只托管最流行的库。我们 托管所有流行的库 - JavaScript、CSS、SWF、图像等! 现在就加入我们的 GitHub!

【讨论】:

    【解决方案3】:

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

    在你的头部标签中:

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

    【讨论】:

      【解决方案4】:

      导入

      import { create, all } from 'mathjs'
      

      初始化

      const math = create(all,  {})
      

      用法

      console.log(math.sqrt(-4).toString()) 
      

      【讨论】:

        【解决方案5】:

        可以演示使用math.js可以运行。 让我尝试在此网页上显示可运行代码作为答案。

        function print (value) {
          const precision = 14
          console.log(math.format(value, precision))
        }
        
        print(math.round(math.e, 3))   // 2.718
        print(math.atan2(3, -3) / math.pi )
        print(math.log(10000, 10))
        print(math.sqrt(-4))
        print(math.pow([[-1, 2], [3, 1]], 2))     // [[7, 0], [0, 7]]
        print(math.evaluate('12 / (2.3 + 0.7)'))
        print(math.evaluate('12.7 cm to inch'))    // 5 inch
        print(math.evaluate('sin(45 deg) ^ 2'))    // 0.5
        print(math.evaluate('det([-1, 2; 3, 1])')) // -7
        
        // matrix inversion
        var mat_a = [[1, 2], [3, -4]]
        var b1 = [[2], [1]]
        var mat_ai = math.inv(mat_a) 
        // check the inversion; OK
        print( math.multiply(mat_a, mat_ai) )
        // get sol A.x=b; x=?
        var x = math.multiply(mat_ai, b1)
        print( {x} )  // solution x
        print( math.multiply(mat_a, x) ) // expect b; OK
        
        // use of linear-eq solve
        // Matrix must be a lower triangular matrix if use `lsolve()`
        var x2 = math.lusolve(mat_a, b1)
        print( {x2} ); // OK
        
        // Use lup(A) instead of A
        // This is preferable method
        var x3 = math.lusolve(math.lup(mat_a), b1)
        print( {x3} ); // OK
        
        // LU decomposition
        var m = [[2, 1], [1, 4]]
        var r = math.lup(m)
        print({r}) //ok {'r': {'L': [[1, 0], [0.5, 1]], 'U': [[2, 1], [0, 3.5]], 'p': [0, 1]}}
        &lt;script src='https://cdnjs.cloudflare.com/ajax/libs/mathjs/9.2.0/math.js'&gt;&lt;/script&gt;

        【讨论】:

          猜你喜欢
          • 2019-02-13
          • 2018-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-23
          • 1970-01-01
          相关资源
          最近更新 更多