Math.js有一個常規解析器來執行此操作。這意味着你可以簡單地(對於一些「簡單」的大值)添加你需要的東西。
(假設你克隆了Math.js from Github) 一輪添加nor
,其他是相似的。
在/lib/expression/parse.js中添加對象所需的內容NAMED_DELIMITERS
(並且不要像我一樣忘記逗號;-))。
var NAMED_DELIMITERS = {
'mod': true,
'to': true,
'in': true,
'and': true,
'xor': true,
'or': true,
'nor': true,
'not': true
};
將其添加到/lib/expression/operators.js中列出的運算符中。例如: -
{ //logical nor
'OperatorNode:nor': {
associativity: 'left',
associativeWith: []
}
},
編寫工作代碼(只是從現有的功能構建它們),並把該目錄中的文件/ lib中/功能/邏輯/。
'use strict';
function factory (type, config, load, typed) {
var latex = require('../../utils/latex');
var matrix = load(require('../../type/matrix/function/matrix'));
var algorithm03 = load(require('../../type/matrix/utils/algorithm03'));
var algorithm05 = load(require('../../type/matrix/utils/algorithm05'));
var algorithm12 = load(require('../../type/matrix/utils/algorithm12'));
var algorithm13 = load(require('../../type/matrix/utils/algorithm13'));
var algorithm14 = load(require('../../type/matrix/utils/algorithm14'));
var nor = typed('nor', {
'number, number': function (x, y) {
return !(!!(x || y));
},
'Complex, Complex': function (x, y) {
return !((x.re !== 0 || x.im !== 0) || (y.re !== 0 || y.im !== 0));
},
'BigNumber, BigNumber': function (x, y) {
return !((!x.isZero() && !x.isNaN()) || (!y.isZero() && !y.isNaN()));
},
'Unit, Unit': function (x, y) {
return !((x.value !== 0 && x.value !== null) || (y.value !== 0 && y.value !== null));
},
'Matrix, Matrix': function (x, y) {
// result
var c;
// process matrix storage
switch (x.storage()) {
case 'sparse':
switch (y.storage()) {
case 'sparse':
// sparse + sparse
c = algorithm05(x, y, nor);
break;
default:
// sparse + dense
c = algorithm03(y, x, nor, true);
break;
}
break;
default:
switch (y.storage()) {
case 'sparse':
// dense + sparse
c = algorithm03(x, y, nor, false);
break;
default:
// dense + dense
c = algorithm13(x, y, nor);
break;
}
break;
}
return c;
},
'Array, Array': function (x, y) {
// use matrix implementation
return nor(matrix(x), matrix(y)).valueOf();
},
'Array, Matrix': function (x, y) {
// use matrix implementation
return nor(matrix(x), y);
},
'Matrix, Array': function (x, y) {
// use matrix implementation
return nor(x, matrix(y));
},
'Matrix, any': function (x, y) {
// result
var c;
// check storage format
switch (x.storage()) {
case 'sparse':
c = algorithm12(x, y, nor, false);
break;
default:
c = algorithm14(x, y, nor, false);
break;
}
return c;
},
'any, Matrix': function (x, y) {
// result
var c;
// check storage format
switch (y.storage()) {
case 'sparse':
c = algorithm12(y, x, nor, true);
break;
default:
c = algorithm14(y, x, nor, true);
break;
}
return c;
},
'Array, any': function (x, y) {
// use matrix implementation
return algorithm14(matrix(x), y, nor, false).valueOf();
},
'any, Array': function (x, y) {
// use matrix implementation
return algorithm14(matrix(y), x, nor, true).valueOf();
}
});
nor.toTex = '\\left(${args[0]}' + latex.operators['nor'] + '${args[1]}\\right)';
return nor;
}
exports.name = 'nor';
exports.factory = factory;
將這些文件添加到駐留在同一目錄中的index.js。
module.exports = [
require('./and'),
require('./not'),
require('./or'),
require('./nor'),
require('./xor')
];
正確的符號添加到乳膠辭典/lib/utils/latex.js
exports.operators = {
// ...
'nor': '\\curlywedge'
};
Math.js是相當清晰的,它應該是沒有問題的補充你所需要的,如果不是……這就是Stackoverflow的用途,不是嗎? ;-)
更新文檔。在文件./lib/expression/docs/function/logical/nor.js把
module.exports = {
'name': 'nor',
'category': 'Logical',
'syntax': [
'x or y',
'or(x, y)'
],
'description': 'Logical nor. Test if neither value is defined with a nonzero/nonempty value.',
'examples': [
'true nor false',
'false nor false',
'0 nor 4'
],
'seealso': [
'not', 'and', 'xor', 'or'
]
};
與
更新文件./lib/expression/docs/index.js的文檔索引
docs['nor'] = require('./function/logical/or');
更新測試。將文件test/function/logical/or.test.js複製到文件test/function/logical/nor.test.js中,並將每個or
替換爲nor
,並將所有布爾值撤銷。但下列情況除外:
it('should nor two booleans', function() {
assert.strictEqual(nor(false, false), true);
assert.strictEqual(nor(false, true), false);
assert.strictEqual(nor(true, false), false);
assert.strictEqual(nor(true, true), false);
});
it('should nor mixed numbers and booleans', function() {
assert.strictEqual(nor(2, false), false);
assert.strictEqual(nor(2, true), false);
assert.strictEqual(nor(0, false), true);
assert.strictEqual(nor(0, true), false);
assert.strictEqual(nor(false, 2), false);
assert.strictEqual(nor(true, 2), false);
assert.strictEqual(nor(true, 0), false);
});
構建math.js運行:
npm install
npm run build
建設過程可能會持續一段時間(兩分半鐘的minify
)。
運行測試。
npm test
在行202測試/功能/邏輯/ nor.test.js測試失敗,我不知道這是否是在執行nor
(不可能的,但可能),或者在稀疏矩陣實施優化。
如果有效:將你的修改提供給math.js,但它們被接受的機會非常小(句法糖有時雖然並不總是被皺起),所以不要太失望。
添加一些示例將使問題清晰 – Tushar
聽起來您最大的障礙將是擴展math.js使用的解析器。我認爲你應該首先研究一下,因爲操作符的邏輯很簡單:!(A || B)','!(A && B)','A == B' – James