首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在math.js中实现NOR,NAND,XNOR?

如何在math.js中实现NOR,NAND,XNOR?
EN

Stack Overflow用户
提问于 2015-10-04 09:06:07
回答 1查看 4K关注 0票数 1

我想有人帮我。有一个非常好的JS库math.js。它实现布尔逻辑运算符NOT,OR,AND,XOR。我需要其他的: NOR,NAND,XNOR。

我知道OR也不知道,但是,存在一个问题,我需要这个操作符来使用它们作为字符串输入进行评估,例如:

(A或B) XNOR C B NAND (非(A或B和C))

math.js有一个解析器,可以分隔(?)具有节点的结构的字符串并对其进行计算。但是,它不知道,或,和,异或。

我的要求清楚了吗?有人帮我吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-10-04 16:11:57

Math.js有一个常规的解析器来这样做。这意味着您可以简单地(对于一些“简单”的大值)添加您所需的内容。

(假设你克隆了Math.js来自吉突布)一轮添加nor,其他的都是相似的。

在/lib/表达式/parse.js中,在对象NAMED_DELIMITERS中添加所需的内容(并且不要像我一样忘记逗号;-)。

代码语言:javascript
复制
var NAMED_DELIMITERS = {
  'mod': true,
  'to': true,
  'in': true,
  'and': true,
  'xor': true,
  'or': true,
  'nor': true,
  'not': true
};

将其添加到/lib/表达式/operators.js中列出的操作符中。例如:

代码语言:javascript
复制
{ //logical nor
  'OperatorNode:nor': {
    associativity: 'left',
    associativeWith: []
  }
},

编写工作代码(只需从现有函数构建它们),并将文件放在目录/lib/function/logical/中。

代码语言:javascript
复制
'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中。

代码语言:javascript
复制
module.exports = [
  require('./and'),
  require('./not'),
  require('./or'),
  require('./nor'),
  require('./xor')
];

在/lib/utils/latex.js中将正确的符号添加到Latex字典中

代码语言:javascript
复制
exports.operators = {
  // ...
  'nor': '\\curlywedge'
};

Math.js很容易读懂,添加所需的内容,如果不是…,应该不会有问题。well…这就是斯塔克溢出的目的,不是吗?;-)

更新文件。在./lib/表达式/docs/function/logical/nor.js文件中

代码语言:javascript
复制
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/表达式/docs/index.js更新文件中的doc索引。

代码语言:javascript
复制
docs['nor'] = require('./function/logical/or');

更新测试。将文件test/function/logical/or.test.js复制到文件test/function/logical/nor.test.js,并将每个or替换为nor,并反转所有布尔值。除下列情况外:

代码语言:javascript
复制
  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:

代码语言:javascript
复制
 npm install
 npm run build

构建过程可能会持续一段时间(minify需要两分钟半)。

做测试。

代码语言:javascript
复制
npm test

第202行的test /function/logical.test.js中的测试失败,我不知道它是在nor实现中(不太可能,但可能)还是在稀疏矩阵优化的实现中。

如果有效的话:向math.js提供您的更改,但是它们被接受的可能性很小(有时,尽管不总是对语法糖表示不满),所以不要太失望。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32931817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档