1/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. 2 3Permission is hereby granted, free of charge, to any person obtaining a copy 4of this software and associated documentation files (the "Software"), to deal 5in the Software without restriction, including without limitation the rights 6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7copies of the Software, and to permit persons to whom the Software is 8furnished to do so, subject to the following conditions: 9 10The above copyright notice and this permission notice shall be included in 11all copies or substantial portions of the Software. 12 13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19THE SOFTWARE. */ 20 21describe("mat2", function() { 22 var mat2 = require("../../src/gl-matrix/mat2.js"); 23 24 var out, matA, matB, identity, result; 25 26 beforeEach(function() { 27 matA = [1, 2, 28 3, 4]; 29 30 matB = [5, 6, 31 7, 8]; 32 33 out = [0, 0, 34 0, 0]; 35 36 identity = [1, 0, 37 0, 1]; 38 }); 39 40 describe("create", function() { 41 beforeEach(function() { result = mat2.create(); }); 42 it("should return a 4 element array initialized to a 2x2 identity matrix", function() { expect(result).toBeEqualish(identity); }); 43 }); 44 45 describe("clone", function() { 46 beforeEach(function() { result = mat2.clone(matA); }); 47 it("should return a 4 element array initialized to the values in matA", function() { expect(result).toBeEqualish(matA); }); 48 }); 49 50 describe("copy", function() { 51 beforeEach(function() { result = mat2.copy(out, matA); }); 52 it("should place values into out", function() { expect(out).toBeEqualish(matA); }); 53 it("should return out", function() { expect(result).toBe(out); }); 54 }); 55 56 describe("identity", function() { 57 beforeEach(function() { result = mat2.identity(out); }); 58 it("should place values into out", function() { expect(result).toBeEqualish(identity); }); 59 it("should return out", function() { expect(result).toBe(out); }); 60 }); 61 62 describe("transpose", function() { 63 describe("with a separate output matrix", function() { 64 beforeEach(function() { result = mat2.transpose(out, matA); }); 65 66 it("should place values into out", function() { expect(out).toBeEqualish([1, 3, 2, 4]); }); 67 it("should return out", function() { expect(result).toBe(out); }); 68 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 69 }); 70 71 describe("when matA is the output matrix", function() { 72 beforeEach(function() { result = mat2.transpose(matA, matA); }); 73 74 it("should place values into matA", function() { expect(matA).toBeEqualish([1, 3, 2, 4]); }); 75 it("should return matA", function() { expect(result).toBe(matA); }); 76 }); 77 }); 78 79 describe("invert", function() { 80 describe("with a separate output matrix", function() { 81 beforeEach(function() { result = mat2.invert(out, matA); }); 82 83 it("should place values into out", function() { expect(out).toBeEqualish([-2, 1, 1.5, -0.5]); }); 84 it("should return out", function() { expect(result).toBe(out); }); 85 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 86 }); 87 88 describe("when matA is the output matrix", function() { 89 beforeEach(function() { result = mat2.invert(matA, matA); }); 90 91 it("should place values into matA", function() { expect(matA).toBeEqualish([-2, 1, 1.5, -0.5]); }); 92 it("should return matA", function() { expect(result).toBe(matA); }); 93 }); 94 }); 95 96 describe("adjoint", function() { 97 describe("with a separate output matrix", function() { 98 beforeEach(function() { result = mat2.adjoint(out, matA); }); 99 100 it("should place values into out", function() { expect(out).toBeEqualish([4, -2, -3, 1]); }); 101 it("should return out", function() { expect(result).toBe(out); }); 102 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 103 }); 104 105 describe("when matA is the output matrix", function() { 106 beforeEach(function() { result = mat2.adjoint(matA, matA); }); 107 108 it("should place values into matA", function() { expect(matA).toBeEqualish([4, -2, -3, 1]); }); 109 it("should return matA", function() { expect(result).toBe(matA); }); 110 }); 111 }); 112 113 describe("determinant", function() { 114 beforeEach(function() { result = mat2.determinant(matA); }); 115 116 it("should return the determinant", function() { expect(result).toEqual(-2); }); 117 }); 118 119 describe("multiply", function() { 120 it("should have an alias called 'mul'", function() { expect(mat2.mul).toEqual(mat2.multiply); }); 121 122 describe("with a separate output matrix", function() { 123 beforeEach(function() { result = mat2.multiply(out, matA, matB); }); 124 125 it("should place values into out", function() { expect(out).toBeEqualish([23, 34, 31, 46]); }); 126 it("should return out", function() { expect(result).toBe(out); }); 127 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 128 it("should not modify matB", function() { expect(matB).toBeEqualish([5, 6, 7, 8]); }); 129 }); 130 131 describe("when matA is the output matrix", function() { 132 beforeEach(function() { result = mat2.multiply(matA, matA, matB); }); 133 134 it("should place values into matA", function() { expect(matA).toBeEqualish([23, 34, 31, 46]); }); 135 it("should return matA", function() { expect(result).toBe(matA); }); 136 it("should not modify matB", function() { expect(matB).toBeEqualish([5, 6, 7, 8]); }); 137 }); 138 139 describe("when matB is the output matrix", function() { 140 beforeEach(function() { result = mat2.multiply(matB, matA, matB); }); 141 142 it("should place values into matB", function() { expect(matB).toBeEqualish([23, 34, 31, 46]); }); 143 it("should return matB", function() { expect(result).toBe(matB); }); 144 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 145 }); 146 }); 147 148 describe("rotate", function() { 149 describe("with a separate output matrix", function() { 150 beforeEach(function() { result = mat2.rotate(out, matA, Math.PI * 0.5); }); 151 152 it("should place values into out", function() { expect(out).toBeEqualish([3, 4, -1, -2]); }); 153 it("should return out", function() { expect(result).toBe(out); }); 154 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 155 }); 156 157 describe("when matA is the output matrix", function() { 158 beforeEach(function() { result = mat2.rotate(matA, matA, Math.PI * 0.5); }); 159 160 it("should place values into matA", function() { expect(matA).toBeEqualish([3, 4, -1, -2]); }); 161 it("should return matA", function() { expect(result).toBe(matA); }); 162 }); 163 }); 164 165 describe("scale", function() { 166 var vecA; 167 beforeEach(function() { vecA = [2, 3]; }); 168 169 describe("with a separate output matrix", function() { 170 beforeEach(function() { result = mat2.scale(out, matA, vecA); }); 171 172 it("should place values into out", function() { expect(out).toBeEqualish([2, 4, 9, 12]); }); 173 it("should return out", function() { expect(result).toBe(out); }); 174 it("should not modify matA", function() { expect(matA).toBeEqualish([1, 2, 3, 4]); }); 175 }); 176 177 describe("when matA is the output matrix", function() { 178 beforeEach(function() { result = mat2.scale(matA, matA, vecA); }); 179 180 it("should place values into matA", function() { expect(matA).toBeEqualish([2, 4, 9, 12]); }); 181 it("should return matA", function() { expect(result).toBe(matA); }); 182 }); 183 }); 184 185 describe("str", function() { 186 beforeEach(function() { result = mat2.str(matA); }); 187 188 it("should return a string representation of the matrix", function() { expect(result).toEqual("mat2(1, 2, 3, 4)"); }); 189 }); 190 191 describe("frob", function() { 192 beforeEach(function() { result = mat2.frob(matA); }); 193 it("should return the Frobenius Norm of the matrix", function() { expect(result).toEqual( Math.sqrt(Math.pow(1, 2) + Math.pow(2, 2) + Math.pow(3, 2) + Math.pow(4, 2))); }); 194 }); 195 196 describe("LDU", function() { 197 beforeEach(function() {L = mat2.create(); D = mat2.create(); U = mat2.create(); result = mat2.LDU(L, D, U, [4,3,6,3]); 198 L_result = mat2.create(); L_result[2] = 1.5; 199 D_result = mat2.create(); 200 U_result = mat2.create(); 201 U_result[0] = 4; U_result[1] = 3; U_result[3] = -1.5; 202 }); 203 it("should return a lower triangular, a diagonal and an upper triangular matrix", function() { 204 expect(result[0]).toBeEqualish(L_result); 205 expect(result[1]).toBeEqualish(D_result); 206 expect(result[2]).toBeEqualish(U_result); 207 }); 208 }); 209 210}); 211