2016-07-16 90 views
1

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

我已閱讀,你可以通過添加這文件包含它:

var math = require('mathjs'); 

但沒有對工作我

我知道這聽起來像一個真正愚蠢的問題,但我有試圖找出自己,並沒有取得成功。

回答我的問題只是顯示一個完整的HTML文件(不只是一點點從一個代碼),成功地使用math.js

感謝

我試了一下亞歷山大·奧馬拉建議和我這個:

/** * 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)); } 
+1

http://mathjs.org/docs/getting_started.html – NTL

+0

那麼,您是否首先包含'require.js'? –

+0

這是commonjs(節點)的方式,也許你想要瀏覽器的例子。 –

回答

2

他們有一個可以使用的寧靜API,http://api.mathjs.org。或者你應該能夠從here下載製作,並將其包含在你的JS中。

在你的腦袋標籤:

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

你不應該使用需要在瀏覽器中。你應該嘗試這樣的事:

<!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

+0

剛剛嘗試過您的建議,請參閱我編輯的問題以獲得結果 – fred

+0

請格式化您的結果 –

+0

我不知道如何 – fred

1

給你最簡單的方法是將包括math.js從CDN:

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

你需要每個腳本,搜查Google for the CDN(在此示例中爲mathjs cdn,並將其包含到您的頁面HEAD中。這樣您就不需要將任何內容下載到您的服務器上)

關於CDN 210

更多信息:

的谷歌託管庫是一個穩定,可靠,高速, 全球可用的內容分發網絡最流行, 開源JavaScript庫。

Google直接與關鍵利益相關者合作,爲每個庫 努力,並接受發佈後的最新版本。

每個人都喜歡的谷歌CDN吧?即使是微軟也運行他們自己的 CDN。問題是,他們只託管最受歡迎的圖書館。我們 承載所有流行的庫 - JavaScript,CSS,SWF,圖像等! 立即加入我們的GitHub!