1 | 'use strict'
|
2 |
|
3 | const util = require('../../utils/index')
|
4 | const object = util.object
|
5 | const string = util.string
|
6 |
|
7 | function factory (type, config, load, typed) {
|
8 | const matrix = load(require('../../type/matrix/function/matrix'))
|
9 | const subtract = load(require('../arithmetic/subtract'))
|
10 | const multiply = load(require('../arithmetic/multiply'))
|
11 | const unaryMinus = load(require('../arithmetic/unaryMinus'))
|
12 | const lup = load(require('../algebra/decomposition/lup'))
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const det = typed('det', {
|
40 | 'any': function (x) {
|
41 | return object.clone(x)
|
42 | },
|
43 |
|
44 | 'Array | Matrix': function det (x) {
|
45 | let size
|
46 | if (type.isMatrix(x)) {
|
47 | size = x.size()
|
48 | } else if (Array.isArray(x)) {
|
49 | x = matrix(x)
|
50 | size = x.size()
|
51 | } else {
|
52 |
|
53 | size = []
|
54 | }
|
55 |
|
56 | switch (size.length) {
|
57 | case 0:
|
58 |
|
59 | return object.clone(x)
|
60 |
|
61 | case 1:
|
62 |
|
63 | if (size[0] === 1) {
|
64 | return object.clone(x.valueOf()[0])
|
65 | } else {
|
66 | throw new RangeError('Matrix must be square ' +
|
67 | '(size: ' + string.format(size) + ')')
|
68 | }
|
69 |
|
70 | case 2:
|
71 |
|
72 | const rows = size[0]
|
73 | const cols = size[1]
|
74 | if (rows === cols) {
|
75 | return _det(x.clone().valueOf(), rows, cols)
|
76 | } else {
|
77 | throw new RangeError('Matrix must be square ' +
|
78 | '(size: ' + string.format(size) + ')')
|
79 | }
|
80 |
|
81 | default:
|
82 |
|
83 | throw new RangeError('Matrix must be two dimensional ' +
|
84 | '(size: ' + string.format(size) + ')')
|
85 | }
|
86 | }
|
87 | })
|
88 |
|
89 | det.toTex = { 1: `\\det\\left(\${args[0]}\\right)` }
|
90 |
|
91 | return det
|
92 |
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function _det (matrix, rows, cols) {
|
102 | if (rows === 1) {
|
103 |
|
104 | return object.clone(matrix[0][0])
|
105 | } else if (rows === 2) {
|
106 |
|
107 |
|
108 | return subtract(
|
109 | multiply(matrix[0][0], matrix[1][1]),
|
110 | multiply(matrix[1][0], matrix[0][1])
|
111 | )
|
112 | } else {
|
113 |
|
114 | const decomp = lup(matrix)
|
115 |
|
116 |
|
117 | let det = decomp.U[0][0]
|
118 | for (let i = 1; i < rows; i++) {
|
119 | det = multiply(det, decomp.U[i][i])
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 | let evenCycles = 0
|
125 | let i = 0
|
126 | const visited = []
|
127 | while (true) {
|
128 | while (visited[i]) {
|
129 | i++
|
130 | }
|
131 | if (i >= rows) break
|
132 | let j = i
|
133 | let cycleLen = 0
|
134 | while (!visited[decomp.p[j]]) {
|
135 | visited[decomp.p[j]] = true
|
136 | j = decomp.p[j]
|
137 | cycleLen++
|
138 | }
|
139 | if (cycleLen % 2 === 0) {
|
140 | evenCycles++
|
141 | }
|
142 | }
|
143 |
|
144 | return evenCycles % 2 === 0 ? det : unaryMinus(det)
|
145 | }
|
146 | }
|
147 | }
|
148 |
|
149 | exports.name = 'det'
|
150 | exports.factory = factory
|