创建一个标识矩阵

时间:2016-12-09 20:41:27

标签: javascript matrix

我必须通过以下方式创建一个Identity矩阵,

"你的Matrix对象应该有一个'createIdentity'函数,它不需要 参数。该函数应返回一个新构造的Matrix对象,其单元格设置为a 单位矩阵。此函数应该像C#中的静态方法一样。要在JavaScript中执行此操作 对象,不要将函数添加到Matrix原型,而是直接添加到Matrix对象。 (例如。 Matrix.createIdentity = function(){etc。)"

到目前为止,我所做的是:

var Matrix = (function () {
function Matrix(pX0, pX1, pX2, pY0, pY1, pY2, pZ0, pZ1, pZ2) {

    this.matrix = [
        [pX0, pX1, pX2],
        [pY0, pY1, pY2],
        [pZ0, pZ1, pZ2]
    ];
}
Matrix.prototype.getX0 = function () {
    return this.mX0;
};
Matrix.prototype.setX0 = function (pX0) {
    this.mX0 = pX0;
};
Matrix.prototype.getX1 = function () {
    return this.mX1;
};
Matrix.prototype.setX1 = function (pX1) {
    this.mX1 = pX1;
};
Matrix.prototype.getX2 = function () {
    return this.mX2;
};
Matrix.prototype.setX2 = function (pX2) {
    this.mX2 = pX2;
};


Matrix.prototype.getY0 = function () {
    return this.mY0;
};
Matrix.prototype.setY0 = function (pY0) {
    this.mY0 = pY0;
};
Matrix.prototype.getY1 = function () {
    return this.mY1;
};
Matrix.prototype.setY1 = function (pY1) {
    this.mY1 = pY1;
};
Matrix.prototype.getY2 = function () {
    return this.mY2;
};
Matrix.prototype.setY2 = function (pY2) {
    this.mY2 = pY2;
};


Matrix.prototype.getZ0 = function () {
    return this.mZ0;
};
Matrix.prototype.setZ0 = function (pZ0) {
    this.mZ0 = pZ0;
};
Matrix.prototype.getZ1 = function () {
    return this.mZ0;
};
Matrix.prototype.setZ1 = function (pZ1) {
    this.mZ1 = pZ1;
};
Matrix.prototype.getZ2 = function () {
    return this.mZ2;
};
Matrix.prototype.setZ2 = function (pZ2) {
    this.mZ2 = pZ2;
};
Matrix.prototype.getElement = function (pRow, pColumn) {
    return this.matrix[pRow][pColumn];
};

 Matrix.createIdentity = function () {

    var Test = new Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
    Test = [
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]
    ];
    return new Test();

};
//Matrix.createIdentity = function () {

//    this.Matrix = [
//        [this.Matrix.setX0(1), this.Matrix.setX1(0), this.Matrix.setX2(0)],
//        [this.Matrix.setY0(0), this.Matrix.setY1(1), this.Matrix.setY2(0)],
//        [this.Matrix.setZ0(0), this.Matrix.setZ1(0), this.Matrix.setZ2(1)]
//    ];
//    return new Matrix();

//};

这是它必须通过的测试,这个测试是在茉莉花中:

describe("Create Identity", function () {
    var matrix = Matrix.createIdentity();
    it("Element (0,0) Set", function () {
        expect(matrix.getElement(0, 0)).toEqual(1);
    });

    it("Element (0,1) Set", function () {
        expect(matrix.getElement(0, 1)).toEqual(0);
    });

    it("Element (0,2) Set", function () {
        expect(matrix.getElement(0, 2)).toEqual(0);
    });

    it("Element (1,0) Set", function () {
        expect(matrix.getElement(1, 0)).toEqual(0);
    });

    it("Element (1,1) Set", function () {
        expect(matrix.getElement(1, 1)).toEqual(1);
    });

    it("Element (1,2) Set", function () {
        expect(matrix.getElement(1, 2)).toEqual(0);
    });

    it("Element (2,0) Set", function () {
        expect(matrix.getElement(2, 0)).toEqual(0);
    });

    it("Element (2,1) Set", function () {
        expect(matrix.getElement(2, 1)).toEqual(0);
    });

    it("Element (2,2) Set", function () {
        expect(matrix.getElement(2, 2)).toEqual(1);
    });

});

我没有看到我如何达不到给出描述的要求。我已经尝试了几种方法来通过测试,但我似乎无法通过它。我做错了什么?

3 个答案:

答案 0 :(得分:0)

您的功能应该只是:

Matrix

现有函数返回一个对象,一个不是getElement并且没有nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.greensungh.jupiter_SHM.model.UsersInfo] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='size must be between 6 and 25', propertyPath=Password, rootBeanClass=class com.greensungh.jupiter_SHM.model.UsersInfo, messageTemplate='{javax.validation.constraints.Size.message}'} ]] with root cause javax.validation.ConstraintViolationException: Validation failed for classes [com.greensungh.jupiter_SHM.model.UsersInfo] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='size must be between 6 and 25', propertyPath=Password, rootBeanClass=class com.greensungh.jupiter_SHM.model.UsersInfo, messageTemplate='{javax.validation.constraints.Size.message}'} 方法的数组。

答案 1 :(得分:0)

您需要创建一个新的Matrix对象才能返回。现在,您正在创建一个Matrix对象,用数组覆盖Matrix对象,然后尝试从数组中创建一个新对象。

您的代码应该只是:

Matrix.createIdentity = function () {
    return new Matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
};

答案 2 :(得分:0)

由于您不需要参数,您只需返回固定的单位矩阵:

this.matrix = [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
];