Merge lp:~gang65/ubuntu-calculator-app/ubuntu-calculator-app-math-2.4-upgrade into lp:ubuntu-calculator-app
- ubuntu-calculator-app-math-2.4-upgrade
- Merge into trunk
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | Bartosz Kosiorek | ||||
Approved revision: | 230 | ||||
Merge reported by: | Bartosz Kosiorek | ||||
Merged at revision: | not available | ||||
Proposed branch: | lp:~gang65/ubuntu-calculator-app/ubuntu-calculator-app-math-2.4-upgrade | ||||
Merge into: | lp:ubuntu-calculator-app | ||||
Diff against target: |
62095 lines (+30259/-29499) 3 files modified
app/engine/math.js (+30243/-29485) debian/changelog (+2/-0) po/com.ubuntu.calculator.pot (+14/-14) |
||||
To merge this branch: | bzr merge lp:~gang65/ubuntu-calculator-app/ubuntu-calculator-app-math-2.4-upgrade | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Niklas Wenzel (community) | Approve | ||
Jenkins Bot | continuous-integration | Approve | |
Ubuntu Phone Apps Jenkins Bot | continuous-integration | Approve | |
Review via email: mp+274958@code.launchpad.net |
Commit message
Upgrade math.js to 2.4.0 to resolve wrong calculation of sin and cos functions,
for values around multiples of tau (i.e. sin(7)) (LP: #1507799)
Description of the change
Upgrade math.js to 2.4.0 to resolve wrong calculation of sin and cos functions,
for values around multiples of tau (i.e. sin(7)) (LP: #1507799)
- 230. By Bartosz Kosiorek
-
Updated translation template
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote : | # |
Jenkins Bot (ubuntu-core-apps-jenkins-bot) wrote : | # |
FAILED: Continuous integration, rev:230
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
Jenkins Bot (ubuntu-core-apps-jenkins-bot) wrote : | # |
PASSED: Continuous integration, rev:230
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild:
https:/
Nicholas Skaggs (nskaggs) wrote : | # |
Let me know if you have further troubles. I'll be fixing the messages jenkins leaves to be a bit saner.
Niklas Wenzel (nikwen) wrote : | # |
Thank you for the patch, Bartosz! :)
I gave it a try and couldn't manage to find any regressions. :p I can confirm that the bug this is supposed to fix is indeed fixed and that the autopilot tests ran fine on my machine.
Looking at math.js's issue list on Github, I can also not find any regression in version 2.4 that would be relevant to our case.
Therefore, I'd say, let's get this in. :)
Good job, Bartosz!
Preview Diff
1 | === modified file 'app/engine/math.js' | |||
2 | --- app/engine/math.js 2015-08-30 23:09:36 +0000 | |||
3 | +++ app/engine/math.js 2015-10-19 22:40:49 +0000 | |||
4 | @@ -14,8 +14,8 @@ | |||
5 | 14 | * It features real and complex numbers, units, matrices, a large set of | 14 | * It features real and complex numbers, units, matrices, a large set of |
6 | 15 | * mathematical functions, and a flexible expression parser. | 15 | * mathematical functions, and a flexible expression parser. |
7 | 16 | * | 16 | * |
10 | 17 | * @version 2.2.0 | 17 | * @version 2.4.0 |
11 | 18 | * @date 2015-08-30 | 18 | * @date 2015-10-09 |
12 | 19 | * | 19 | * |
13 | 20 | * @license | 20 | * @license |
14 | 21 | * Copyright (C) 2013-2015 Jos de Jong <wjosdejong@gmail.com> | 21 | * Copyright (C) 2013-2015 Jos de Jong <wjosdejong@gmail.com> |
15 | @@ -133,10 +133,10 @@ | |||
16 | 133 | /* 2 */ | 133 | /* 2 */ |
17 | 134 | /***/ function(module, exports, __webpack_require__) { | 134 | /***/ function(module, exports, __webpack_require__) { |
18 | 135 | 135 | ||
23 | 136 | var isFactory = __webpack_require__(5).isFactory; | 136 | var isFactory = __webpack_require__(3).isFactory; |
24 | 137 | var deepExtend = __webpack_require__(5).deepExtend; | 137 | var deepExtend = __webpack_require__(3).deepExtend; |
25 | 138 | var typedFactory = __webpack_require__(6); | 138 | var typedFactory = __webpack_require__(4); |
26 | 139 | var emitter = __webpack_require__(3); | 139 | var emitter = __webpack_require__(8); |
27 | 140 | 140 | ||
28 | 141 | var importFactory = __webpack_require__(10); | 141 | var importFactory = __webpack_require__(10); |
29 | 142 | var configFactory = __webpack_require__(12); | 142 | var configFactory = __webpack_require__(12); |
30 | @@ -261,101 +261,6 @@ | |||
31 | 261 | 261 | ||
32 | 262 | /***/ }, | 262 | /***/ }, |
33 | 263 | /* 3 */ | 263 | /* 3 */ |
34 | 264 | /***/ function(module, exports, __webpack_require__) { | ||
35 | 265 | |||
36 | 266 | var Emitter = __webpack_require__(4); | ||
37 | 267 | |||
38 | 268 | /** | ||
39 | 269 | * Extend given object with emitter functions `on`, `off`, `once`, `emit` | ||
40 | 270 | * @param {Object} obj | ||
41 | 271 | * @return {Object} obj | ||
42 | 272 | */ | ||
43 | 273 | exports.mixin = function (obj) { | ||
44 | 274 | // create event emitter | ||
45 | 275 | var emitter = new Emitter(); | ||
46 | 276 | |||
47 | 277 | // bind methods to obj (we don't want to expose the emitter.e Array...) | ||
48 | 278 | obj.on = emitter.on.bind(emitter); | ||
49 | 279 | obj.off = emitter.off.bind(emitter); | ||
50 | 280 | obj.once = emitter.once.bind(emitter); | ||
51 | 281 | obj.emit = emitter.emit.bind(emitter); | ||
52 | 282 | |||
53 | 283 | return obj; | ||
54 | 284 | }; | ||
55 | 285 | |||
56 | 286 | |||
57 | 287 | /***/ }, | ||
58 | 288 | /* 4 */ | ||
59 | 289 | /***/ function(module, exports) { | ||
60 | 290 | |||
61 | 291 | function E () { | ||
62 | 292 | // Keep this empty so it's easier to inherit from | ||
63 | 293 | // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) | ||
64 | 294 | } | ||
65 | 295 | |||
66 | 296 | E.prototype = { | ||
67 | 297 | on: function (name, callback, ctx) { | ||
68 | 298 | var e = this.e || (this.e = {}); | ||
69 | 299 | |||
70 | 300 | (e[name] || (e[name] = [])).push({ | ||
71 | 301 | fn: callback, | ||
72 | 302 | ctx: ctx | ||
73 | 303 | }); | ||
74 | 304 | |||
75 | 305 | return this; | ||
76 | 306 | }, | ||
77 | 307 | |||
78 | 308 | once: function (name, callback, ctx) { | ||
79 | 309 | var self = this; | ||
80 | 310 | var fn = function () { | ||
81 | 311 | self.off(name, fn); | ||
82 | 312 | callback.apply(ctx, arguments); | ||
83 | 313 | }; | ||
84 | 314 | |||
85 | 315 | return this.on(name, fn, ctx); | ||
86 | 316 | }, | ||
87 | 317 | |||
88 | 318 | emit: function (name) { | ||
89 | 319 | var data = [].slice.call(arguments, 1); | ||
90 | 320 | var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); | ||
91 | 321 | var i = 0; | ||
92 | 322 | var len = evtArr.length; | ||
93 | 323 | |||
94 | 324 | for (i; i < len; i++) { | ||
95 | 325 | evtArr[i].fn.apply(evtArr[i].ctx, data); | ||
96 | 326 | } | ||
97 | 327 | |||
98 | 328 | return this; | ||
99 | 329 | }, | ||
100 | 330 | |||
101 | 331 | off: function (name, callback) { | ||
102 | 332 | var e = this.e || (this.e = {}); | ||
103 | 333 | var evts = e[name]; | ||
104 | 334 | var liveEvents = []; | ||
105 | 335 | |||
106 | 336 | if (evts && callback) { | ||
107 | 337 | for (var i = 0, len = evts.length; i < len; i++) { | ||
108 | 338 | if (evts[i].fn !== callback) liveEvents.push(evts[i]); | ||
109 | 339 | } | ||
110 | 340 | } | ||
111 | 341 | |||
112 | 342 | // Remove event from queue to prevent memory leak | ||
113 | 343 | // Suggested by https://github.com/lazd | ||
114 | 344 | // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 | ||
115 | 345 | |||
116 | 346 | (liveEvents.length) | ||
117 | 347 | ? e[name] = liveEvents | ||
118 | 348 | : delete e[name]; | ||
119 | 349 | |||
120 | 350 | return this; | ||
121 | 351 | } | ||
122 | 352 | }; | ||
123 | 353 | |||
124 | 354 | module.exports = E; | ||
125 | 355 | |||
126 | 356 | |||
127 | 357 | /***/ }, | ||
128 | 358 | /* 5 */ | ||
129 | 359 | /***/ function(module, exports) { | 264 | /***/ function(module, exports) { |
130 | 360 | 265 | ||
131 | 361 | 'use strict'; | 266 | 'use strict'; |
132 | @@ -602,11 +507,11 @@ | |||
133 | 602 | 507 | ||
134 | 603 | 508 | ||
135 | 604 | /***/ }, | 509 | /***/ }, |
137 | 605 | /* 6 */ | 510 | /* 4 */ |
138 | 606 | /***/ function(module, exports, __webpack_require__) { | 511 | /***/ function(module, exports, __webpack_require__) { |
139 | 607 | 512 | ||
142 | 608 | var typedFunction = __webpack_require__(7); | 513 | var typedFunction = __webpack_require__(5); |
143 | 609 | var digits = __webpack_require__(8).digits; | 514 | var digits = __webpack_require__(6).digits; |
144 | 610 | 515 | ||
145 | 611 | // returns a new instance of typed-function | 516 | // returns a new instance of typed-function |
146 | 612 | var createTyped = function () { | 517 | var createTyped = function () { |
147 | @@ -765,7 +670,7 @@ | |||
148 | 765 | 670 | ||
149 | 766 | 671 | ||
150 | 767 | /***/ }, | 672 | /***/ }, |
152 | 768 | /* 7 */ | 673 | /* 5 */ |
153 | 769 | /***/ function(module, exports, __webpack_require__) { | 674 | /***/ function(module, exports, __webpack_require__) { |
154 | 770 | 675 | ||
155 | 771 | var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** | 676 | var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/** |
156 | @@ -2022,7 +1927,7 @@ | |||
157 | 2022 | */ | 1927 | */ |
158 | 2023 | function convert (value, type) { | 1928 | function convert (value, type) { |
159 | 2024 | var from = getTypeOf(value); | 1929 | var from = getTypeOf(value); |
161 | 2025 | 1930 | ||
162 | 2026 | // check conversion is needed | 1931 | // check conversion is needed |
163 | 2027 | if (type === from) { | 1932 | if (type === from) { |
164 | 2028 | return value; | 1933 | return value; |
165 | @@ -2076,12 +1981,12 @@ | |||
166 | 2076 | 1981 | ||
167 | 2077 | 1982 | ||
168 | 2078 | /***/ }, | 1983 | /***/ }, |
170 | 2079 | /* 8 */ | 1984 | /* 6 */ |
171 | 2080 | /***/ function(module, exports, __webpack_require__) { | 1985 | /***/ function(module, exports, __webpack_require__) { |
172 | 2081 | 1986 | ||
173 | 2082 | 'use strict'; | 1987 | 'use strict'; |
174 | 2083 | 1988 | ||
176 | 2084 | var NumberFormatter = __webpack_require__(9); | 1989 | var NumberFormatter = __webpack_require__(7); |
177 | 2085 | 1990 | ||
178 | 2086 | /** | 1991 | /** |
179 | 2087 | * Test whether value is a number | 1992 | * Test whether value is a number |
180 | @@ -2343,7 +2248,7 @@ | |||
181 | 2343 | 2248 | ||
182 | 2344 | 2249 | ||
183 | 2345 | /***/ }, | 2250 | /***/ }, |
185 | 2346 | /* 9 */ | 2251 | /* 7 */ |
186 | 2347 | /***/ function(module, exports) { | 2252 | /***/ function(module, exports) { |
187 | 2348 | 2253 | ||
188 | 2349 | 'use strict'; | 2254 | 'use strict'; |
189 | @@ -2556,15 +2461,110 @@ | |||
190 | 2556 | 2461 | ||
191 | 2557 | 2462 | ||
192 | 2558 | /***/ }, | 2463 | /***/ }, |
193 | 2464 | /* 8 */ | ||
194 | 2465 | /***/ function(module, exports, __webpack_require__) { | ||
195 | 2466 | |||
196 | 2467 | var Emitter = __webpack_require__(9); | ||
197 | 2468 | |||
198 | 2469 | /** | ||
199 | 2470 | * Extend given object with emitter functions `on`, `off`, `once`, `emit` | ||
200 | 2471 | * @param {Object} obj | ||
201 | 2472 | * @return {Object} obj | ||
202 | 2473 | */ | ||
203 | 2474 | exports.mixin = function (obj) { | ||
204 | 2475 | // create event emitter | ||
205 | 2476 | var emitter = new Emitter(); | ||
206 | 2477 | |||
207 | 2478 | // bind methods to obj (we don't want to expose the emitter.e Array...) | ||
208 | 2479 | obj.on = emitter.on.bind(emitter); | ||
209 | 2480 | obj.off = emitter.off.bind(emitter); | ||
210 | 2481 | obj.once = emitter.once.bind(emitter); | ||
211 | 2482 | obj.emit = emitter.emit.bind(emitter); | ||
212 | 2483 | |||
213 | 2484 | return obj; | ||
214 | 2485 | }; | ||
215 | 2486 | |||
216 | 2487 | |||
217 | 2488 | /***/ }, | ||
218 | 2489 | /* 9 */ | ||
219 | 2490 | /***/ function(module, exports) { | ||
220 | 2491 | |||
221 | 2492 | function E () { | ||
222 | 2493 | // Keep this empty so it's easier to inherit from | ||
223 | 2494 | // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) | ||
224 | 2495 | } | ||
225 | 2496 | |||
226 | 2497 | E.prototype = { | ||
227 | 2498 | on: function (name, callback, ctx) { | ||
228 | 2499 | var e = this.e || (this.e = {}); | ||
229 | 2500 | |||
230 | 2501 | (e[name] || (e[name] = [])).push({ | ||
231 | 2502 | fn: callback, | ||
232 | 2503 | ctx: ctx | ||
233 | 2504 | }); | ||
234 | 2505 | |||
235 | 2506 | return this; | ||
236 | 2507 | }, | ||
237 | 2508 | |||
238 | 2509 | once: function (name, callback, ctx) { | ||
239 | 2510 | var self = this; | ||
240 | 2511 | var fn = function () { | ||
241 | 2512 | self.off(name, fn); | ||
242 | 2513 | callback.apply(ctx, arguments); | ||
243 | 2514 | }; | ||
244 | 2515 | |||
245 | 2516 | return this.on(name, fn, ctx); | ||
246 | 2517 | }, | ||
247 | 2518 | |||
248 | 2519 | emit: function (name) { | ||
249 | 2520 | var data = [].slice.call(arguments, 1); | ||
250 | 2521 | var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); | ||
251 | 2522 | var i = 0; | ||
252 | 2523 | var len = evtArr.length; | ||
253 | 2524 | |||
254 | 2525 | for (i; i < len; i++) { | ||
255 | 2526 | evtArr[i].fn.apply(evtArr[i].ctx, data); | ||
256 | 2527 | } | ||
257 | 2528 | |||
258 | 2529 | return this; | ||
259 | 2530 | }, | ||
260 | 2531 | |||
261 | 2532 | off: function (name, callback) { | ||
262 | 2533 | var e = this.e || (this.e = {}); | ||
263 | 2534 | var evts = e[name]; | ||
264 | 2535 | var liveEvents = []; | ||
265 | 2536 | |||
266 | 2537 | if (evts && callback) { | ||
267 | 2538 | for (var i = 0, len = evts.length; i < len; i++) { | ||
268 | 2539 | if (evts[i].fn !== callback) liveEvents.push(evts[i]); | ||
269 | 2540 | } | ||
270 | 2541 | } | ||
271 | 2542 | |||
272 | 2543 | // Remove event from queue to prevent memory leak | ||
273 | 2544 | // Suggested by https://github.com/lazd | ||
274 | 2545 | // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 | ||
275 | 2546 | |||
276 | 2547 | (liveEvents.length) | ||
277 | 2548 | ? e[name] = liveEvents | ||
278 | 2549 | : delete e[name]; | ||
279 | 2550 | |||
280 | 2551 | return this; | ||
281 | 2552 | } | ||
282 | 2553 | }; | ||
283 | 2554 | |||
284 | 2555 | module.exports = E; | ||
285 | 2556 | |||
286 | 2557 | |||
287 | 2558 | /***/ }, | ||
288 | 2559 | /* 10 */ | 2559 | /* 10 */ |
289 | 2560 | /***/ function(module, exports, __webpack_require__) { | 2560 | /***/ function(module, exports, __webpack_require__) { |
290 | 2561 | 2561 | ||
291 | 2562 | 'use strict'; | 2562 | 'use strict'; |
292 | 2563 | 2563 | ||
297 | 2564 | var lazy = __webpack_require__(5).lazy; | 2564 | var lazy = __webpack_require__(3).lazy; |
298 | 2565 | var isFactory = __webpack_require__(5).isFactory; | 2565 | var isFactory = __webpack_require__(3).isFactory; |
299 | 2566 | var traverse = __webpack_require__(5).traverse; | 2566 | var traverse = __webpack_require__(3).traverse; |
300 | 2567 | var extend = __webpack_require__(5).extend; | 2567 | var extend = __webpack_require__(3).extend; |
301 | 2568 | var ArgumentsError = __webpack_require__(11); | 2568 | var ArgumentsError = __webpack_require__(11); |
302 | 2569 | 2569 | ||
303 | 2570 | function factory (type, config, load, typed, math) { | 2570 | function factory (type, config, load, typed, math) { |
304 | @@ -2863,7 +2863,7 @@ | |||
305 | 2863 | 2863 | ||
306 | 2864 | 'use strict'; | 2864 | 'use strict'; |
307 | 2865 | 2865 | ||
309 | 2866 | var object = __webpack_require__(5); | 2866 | var object = __webpack_require__(3); |
310 | 2867 | 2867 | ||
311 | 2868 | function factory (type, config, load, typed, math) { | 2868 | function factory (type, config, load, typed, math) { |
312 | 2869 | /** | 2869 | /** |
313 | @@ -2915,12 +2915,12 @@ | |||
314 | 2915 | /***/ function(module, exports, __webpack_require__) { | 2915 | /***/ function(module, exports, __webpack_require__) { |
315 | 2916 | 2916 | ||
316 | 2917 | module.exports = [ | 2917 | module.exports = [ |
323 | 2918 | __webpack_require__(244), // data types (Matrix, Complex, Unit, ...) | 2918 | __webpack_require__(14), // data types (Matrix, Complex, Unit, ...) |
324 | 2919 | __webpack_require__(281), // constants | 2919 | __webpack_require__(76), // constants |
325 | 2920 | __webpack_require__(283), // expression parsing | 2920 | __webpack_require__(80), // expression parsing |
326 | 2921 | __webpack_require__(14), // functions | 2921 | __webpack_require__(312), // functions |
327 | 2922 | __webpack_require__(489), // serialization utility (math.json.reviver) | 2922 | __webpack_require__(495), // serialization utility (math.json.reviver) |
328 | 2923 | __webpack_require__(491) // errors | 2923 | __webpack_require__(497) // errors |
329 | 2924 | ]; | 2924 | ]; |
330 | 2925 | 2925 | ||
331 | 2926 | 2926 | ||
332 | @@ -2929,20 +2929,16 @@ | |||
333 | 2929 | /***/ function(module, exports, __webpack_require__) { | 2929 | /***/ function(module, exports, __webpack_require__) { |
334 | 2930 | 2930 | ||
335 | 2931 | module.exports = [ | 2931 | module.exports = [ |
336 | 2932 | __webpack_require__(61), | ||
337 | 2933 | __webpack_require__(91), | ||
338 | 2934 | __webpack_require__(120), | ||
339 | 2935 | __webpack_require__(136), | ||
340 | 2936 | __webpack_require__(149), | ||
341 | 2937 | __webpack_require__(154), | ||
342 | 2938 | __webpack_require__(156), | ||
343 | 2939 | __webpack_require__(15), | 2932 | __webpack_require__(15), |
350 | 2940 | __webpack_require__(161), | 2933 | __webpack_require__(20), |
351 | 2941 | __webpack_require__(173), | 2934 | __webpack_require__(21), |
352 | 2942 | __webpack_require__(179), | 2935 | __webpack_require__(26), |
353 | 2943 | __webpack_require__(191), | 2936 | __webpack_require__(31), |
354 | 2944 | __webpack_require__(232), | 2937 | __webpack_require__(37), |
355 | 2945 | __webpack_require__(234) | 2938 | __webpack_require__(69), |
356 | 2939 | __webpack_require__(70), | ||
357 | 2940 | __webpack_require__(72), | ||
358 | 2941 | __webpack_require__(73) | ||
359 | 2946 | ]; | 2942 | ]; |
360 | 2947 | 2943 | ||
361 | 2948 | 2944 | ||
362 | @@ -2951,23 +2947,11 @@ | |||
363 | 2951 | /***/ function(module, exports, __webpack_require__) { | 2947 | /***/ function(module, exports, __webpack_require__) { |
364 | 2952 | 2948 | ||
365 | 2953 | module.exports = [ | 2949 | module.exports = [ |
366 | 2950 | // type | ||
367 | 2954 | __webpack_require__(16), | 2951 | __webpack_require__(16), |
384 | 2955 | __webpack_require__(24), | 2952 | |
385 | 2956 | __webpack_require__(43), | 2953 | // construction function |
386 | 2957 | __webpack_require__(46), | 2954 | __webpack_require__(18) |
371 | 2958 | __webpack_require__(47), | ||
372 | 2959 | __webpack_require__(48), | ||
373 | 2960 | __webpack_require__(49), | ||
374 | 2961 | __webpack_require__(50), | ||
375 | 2962 | __webpack_require__(52), | ||
376 | 2963 | __webpack_require__(53), | ||
377 | 2964 | __webpack_require__(54), | ||
378 | 2965 | __webpack_require__(55), | ||
379 | 2966 | __webpack_require__(56), | ||
380 | 2967 | __webpack_require__(57), | ||
381 | 2968 | __webpack_require__(58), | ||
382 | 2969 | __webpack_require__(59), | ||
383 | 2970 | __webpack_require__(60) | ||
387 | 2971 | ]; | 2955 | ]; |
388 | 2972 | 2956 | ||
389 | 2973 | 2957 | ||
390 | @@ -2975,28177 +2959,8 @@ | |||
391 | 2975 | /* 16 */ | 2959 | /* 16 */ |
392 | 2976 | /***/ function(module, exports, __webpack_require__) { | 2960 | /***/ function(module, exports, __webpack_require__) { |
393 | 2977 | 2961 | ||
394 | 2978 | 'use strict'; | ||
395 | 2979 | |||
396 | 2980 | var clone = __webpack_require__(5).clone; | ||
397 | 2981 | var isInteger = __webpack_require__(8).isInteger; | ||
398 | 2982 | var array = __webpack_require__(18); | ||
399 | 2983 | var IndexError = __webpack_require__(17); | ||
400 | 2984 | var DimensionError = __webpack_require__(22); | ||
401 | 2985 | |||
402 | 2986 | function factory (type, config, load, typed) { | ||
403 | 2987 | var matrix = load(__webpack_require__(23)); | ||
404 | 2988 | |||
405 | 2989 | /** | ||
406 | 2990 | * Concatenate two or more matrices. | ||
407 | 2991 | * | ||
408 | 2992 | * Syntax: | ||
409 | 2993 | * | ||
410 | 2994 | * math.concat(A, B, C, ...) | ||
411 | 2995 | * math.concat(A, B, C, ..., dim) | ||
412 | 2996 | * | ||
413 | 2997 | * Where: | ||
414 | 2998 | * | ||
415 | 2999 | * - `dim: number` is a zero-based dimension over which to concatenate the matrices. | ||
416 | 3000 | * By default the last dimension of the matrices. | ||
417 | 3001 | * | ||
418 | 3002 | * Examples: | ||
419 | 3003 | * | ||
420 | 3004 | * var A = [[1, 2], [5, 6]]; | ||
421 | 3005 | * var B = [[3, 4], [7, 8]]; | ||
422 | 3006 | * | ||
423 | 3007 | * math.concat(A, B); // returns [[1, 2, 3, 4], [5, 6, 7, 8]] | ||
424 | 3008 | * math.concat(A, B, 0); // returns [[1, 2], [5, 6], [3, 4], [7, 8]] | ||
425 | 3009 | * math.concat('hello', ' ', 'world'); // returns 'hello world' | ||
426 | 3010 | * | ||
427 | 3011 | * See also: | ||
428 | 3012 | * | ||
429 | 3013 | * size, squeeze, subset, transpose | ||
430 | 3014 | * | ||
431 | 3015 | * @param {... Array | Matrix} args Two or more matrices | ||
432 | 3016 | * @return {Array | Matrix} Concatenated matrix | ||
433 | 3017 | */ | ||
434 | 3018 | var concat = typed('concat', { | ||
435 | 3019 | // TODO: change signature to '...Array | Matrix, dim?' when supported | ||
436 | 3020 | '...Array | Matrix | number | BigNumber': function (args) { | ||
437 | 3021 | var i; | ||
438 | 3022 | var len = args.length; | ||
439 | 3023 | var dim = -1; // zero-based dimension | ||
440 | 3024 | var prevDim; | ||
441 | 3025 | var asMatrix = false; | ||
442 | 3026 | var matrices = []; // contains multi dimensional arrays | ||
443 | 3027 | |||
444 | 3028 | for (i = 0; i < len; i++) { | ||
445 | 3029 | var arg = args[i]; | ||
446 | 3030 | |||
447 | 3031 | // test whether we need to return a Matrix (if not we return an Array) | ||
448 | 3032 | if (arg && arg.isMatrix === true) { | ||
449 | 3033 | asMatrix = true; | ||
450 | 3034 | } | ||
451 | 3035 | |||
452 | 3036 | if (typeof arg === 'number' || (arg && arg.isBigNumber === true)) { | ||
453 | 3037 | if (i !== len - 1) { | ||
454 | 3038 | throw new Error('Dimension must be specified as last argument'); | ||
455 | 3039 | } | ||
456 | 3040 | |||
457 | 3041 | // last argument contains the dimension on which to concatenate | ||
458 | 3042 | prevDim = dim; | ||
459 | 3043 | dim = arg.valueOf(); // change BigNumber to number | ||
460 | 3044 | |||
461 | 3045 | if (!isInteger(dim)) { | ||
462 | 3046 | throw new TypeError('Integer number expected for dimension'); | ||
463 | 3047 | } | ||
464 | 3048 | |||
465 | 3049 | if (dim < 0) { | ||
466 | 3050 | // TODO: would be more clear when throwing a DimensionError here | ||
467 | 3051 | throw new IndexError(dim); | ||
468 | 3052 | } | ||
469 | 3053 | if (i > 0 && dim > prevDim) { | ||
470 | 3054 | // TODO: would be more clear when throwing a DimensionError here | ||
471 | 3055 | throw new IndexError(dim, prevDim + 1); | ||
472 | 3056 | } | ||
473 | 3057 | } | ||
474 | 3058 | else { | ||
475 | 3059 | // this is a matrix or array | ||
476 | 3060 | var m = clone(arg).valueOf(); | ||
477 | 3061 | var size = array.size(m); | ||
478 | 3062 | matrices[i] = m; | ||
479 | 3063 | prevDim = dim; | ||
480 | 3064 | dim = size.length - 1; | ||
481 | 3065 | |||
482 | 3066 | // verify whether each of the matrices has the same number of dimensions | ||
483 | 3067 | if (i > 0 && dim != prevDim) { | ||
484 | 3068 | throw new DimensionError(prevDim + 1, dim + 1); | ||
485 | 3069 | } | ||
486 | 3070 | } | ||
487 | 3071 | } | ||
488 | 3072 | |||
489 | 3073 | if (matrices.length == 0) { | ||
490 | 3074 | throw new SyntaxError('At least one matrix expected'); | ||
491 | 3075 | } | ||
492 | 3076 | |||
493 | 3077 | var res = matrices.shift(); | ||
494 | 3078 | while (matrices.length) { | ||
495 | 3079 | res = _concat(res, matrices.shift(), dim, 0); | ||
496 | 3080 | } | ||
497 | 3081 | |||
498 | 3082 | return asMatrix ? matrix(res) : res; | ||
499 | 3083 | }, | ||
500 | 3084 | |||
501 | 3085 | '...string': function (args) { | ||
502 | 3086 | return args.join(''); | ||
503 | 3087 | } | ||
504 | 3088 | }); | ||
505 | 3089 | |||
506 | 3090 | concat.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; | ||
507 | 3091 | |||
508 | 3092 | return concat; | ||
509 | 3093 | } | ||
510 | 3094 | |||
511 | 3095 | /** | ||
512 | 3096 | * Recursively concatenate two matrices. | ||
513 | 3097 | * The contents of the matrices is not cloned. | ||
514 | 3098 | * @param {Array} a Multi dimensional array | ||
515 | 3099 | * @param {Array} b Multi dimensional array | ||
516 | 3100 | * @param {number} concatDim The dimension on which to concatenate (zero-based) | ||
517 | 3101 | * @param {number} dim The current dim (zero-based) | ||
518 | 3102 | * @return {Array} c The concatenated matrix | ||
519 | 3103 | * @private | ||
520 | 3104 | */ | ||
521 | 3105 | function _concat(a, b, concatDim, dim) { | ||
522 | 3106 | if (dim < concatDim) { | ||
523 | 3107 | // recurse into next dimension | ||
524 | 3108 | if (a.length != b.length) { | ||
525 | 3109 | throw new DimensionError(a.length, b.length); | ||
526 | 3110 | } | ||
527 | 3111 | |||
528 | 3112 | var c = []; | ||
529 | 3113 | for (var i = 0; i < a.length; i++) { | ||
530 | 3114 | c[i] = _concat(a[i], b[i], concatDim, dim + 1); | ||
531 | 3115 | } | ||
532 | 3116 | return c; | ||
533 | 3117 | } | ||
534 | 3118 | else { | ||
535 | 3119 | // concatenate this dimension | ||
536 | 3120 | return a.concat(b); | ||
537 | 3121 | } | ||
538 | 3122 | } | ||
539 | 3123 | |||
540 | 3124 | exports.name = 'concat'; | ||
541 | 3125 | exports.factory = factory; | ||
542 | 3126 | |||
543 | 3127 | |||
544 | 3128 | /***/ }, | ||
545 | 3129 | /* 17 */ | ||
546 | 3130 | /***/ function(module, exports) { | ||
547 | 3131 | |||
548 | 3132 | 'use strict'; | ||
549 | 3133 | |||
550 | 3134 | /** | ||
551 | 3135 | * Create a range error with the message: | ||
552 | 3136 | * 'Index out of range (index < min)' | ||
553 | 3137 | * 'Index out of range (index < max)' | ||
554 | 3138 | * | ||
555 | 3139 | * @param {number} index The actual index | ||
556 | 3140 | * @param {number} [min=0] Minimum index (included) | ||
557 | 3141 | * @param {number} [max] Maximum index (excluded) | ||
558 | 3142 | * @extends RangeError | ||
559 | 3143 | */ | ||
560 | 3144 | function IndexError(index, min, max) { | ||
561 | 3145 | if (!(this instanceof IndexError)) { | ||
562 | 3146 | throw new SyntaxError('Constructor must be called with the new operator'); | ||
563 | 3147 | } | ||
564 | 3148 | |||
565 | 3149 | this.index = index; | ||
566 | 3150 | if (arguments.length < 3) { | ||
567 | 3151 | this.min = 0; | ||
568 | 3152 | this.max = min; | ||
569 | 3153 | } | ||
570 | 3154 | else { | ||
571 | 3155 | this.min = min; | ||
572 | 3156 | this.max = max; | ||
573 | 3157 | } | ||
574 | 3158 | |||
575 | 3159 | if (this.min !== undefined && this.index < this.min) { | ||
576 | 3160 | this.message = 'Index out of range (' + this.index + ' < ' + this.min + ')'; | ||
577 | 3161 | } | ||
578 | 3162 | else if (this.max !== undefined && this.index >= this.max) { | ||
579 | 3163 | this.message = 'Index out of range (' + this.index + ' > ' + (this.max - 1) + ')'; | ||
580 | 3164 | } | ||
581 | 3165 | else { | ||
582 | 3166 | this.message = 'Index out of range (' + this.index + ')'; | ||
583 | 3167 | } | ||
584 | 3168 | |||
585 | 3169 | this.stack = (new Error()).stack; | ||
586 | 3170 | } | ||
587 | 3171 | |||
588 | 3172 | IndexError.prototype = new RangeError(); | ||
589 | 3173 | IndexError.prototype.constructor = RangeError; | ||
590 | 3174 | IndexError.prototype.name = 'IndexError'; | ||
591 | 3175 | IndexError.prototype.isIndexError = true; | ||
592 | 3176 | |||
593 | 3177 | module.exports = IndexError; | ||
594 | 3178 | |||
595 | 3179 | |||
596 | 3180 | /***/ }, | ||
597 | 3181 | /* 18 */ | ||
598 | 3182 | /***/ function(module, exports, __webpack_require__) { | ||
599 | 3183 | |||
600 | 3184 | 'use strict'; | ||
601 | 3185 | |||
602 | 3186 | var number = __webpack_require__(8); | ||
603 | 3187 | var string = __webpack_require__(20); | ||
604 | 3188 | var object = __webpack_require__(5); | ||
605 | 3189 | var types = __webpack_require__(19); | ||
606 | 3190 | |||
607 | 3191 | var DimensionError = __webpack_require__(22); | ||
608 | 3192 | var IndexError = __webpack_require__(17); | ||
609 | 3193 | |||
610 | 3194 | /** | ||
611 | 3195 | * Calculate the size of a multi dimensional array. | ||
612 | 3196 | * This function checks the size of the first entry, it does not validate | ||
613 | 3197 | * whether all dimensions match. (use function `validate` for that) | ||
614 | 3198 | * @param {Array} x | ||
615 | 3199 | * @Return {Number[]} size | ||
616 | 3200 | */ | ||
617 | 3201 | exports.size = function (x) { | ||
618 | 3202 | var s = []; | ||
619 | 3203 | |||
620 | 3204 | while (Array.isArray(x)) { | ||
621 | 3205 | s.push(x.length); | ||
622 | 3206 | x = x[0]; | ||
623 | 3207 | } | ||
624 | 3208 | |||
625 | 3209 | return s; | ||
626 | 3210 | }; | ||
627 | 3211 | |||
628 | 3212 | /** | ||
629 | 3213 | * Recursively validate whether each element in a multi dimensional array | ||
630 | 3214 | * has a size corresponding to the provided size array. | ||
631 | 3215 | * @param {Array} array Array to be validated | ||
632 | 3216 | * @param {number[]} size Array with the size of each dimension | ||
633 | 3217 | * @param {number} dim Current dimension | ||
634 | 3218 | * @throws DimensionError | ||
635 | 3219 | * @private | ||
636 | 3220 | */ | ||
637 | 3221 | function _validate(array, size, dim) { | ||
638 | 3222 | var i; | ||
639 | 3223 | var len = array.length; | ||
640 | 3224 | |||
641 | 3225 | if (len != size[dim]) { | ||
642 | 3226 | throw new DimensionError(len, size[dim]); | ||
643 | 3227 | } | ||
644 | 3228 | |||
645 | 3229 | if (dim < size.length - 1) { | ||
646 | 3230 | // recursively validate each child array | ||
647 | 3231 | var dimNext = dim + 1; | ||
648 | 3232 | for (i = 0; i < len; i++) { | ||
649 | 3233 | var child = array[i]; | ||
650 | 3234 | if (!Array.isArray(child)) { | ||
651 | 3235 | throw new DimensionError(size.length - 1, size.length, '<'); | ||
652 | 3236 | } | ||
653 | 3237 | _validate(array[i], size, dimNext); | ||
654 | 3238 | } | ||
655 | 3239 | } | ||
656 | 3240 | else { | ||
657 | 3241 | // last dimension. none of the childs may be an array | ||
658 | 3242 | for (i = 0; i < len; i++) { | ||
659 | 3243 | if (Array.isArray(array[i])) { | ||
660 | 3244 | throw new DimensionError(size.length + 1, size.length, '>'); | ||
661 | 3245 | } | ||
662 | 3246 | } | ||
663 | 3247 | } | ||
664 | 3248 | } | ||
665 | 3249 | |||
666 | 3250 | /** | ||
667 | 3251 | * Validate whether each element in a multi dimensional array has | ||
668 | 3252 | * a size corresponding to the provided size array. | ||
669 | 3253 | * @param {Array} array Array to be validated | ||
670 | 3254 | * @param {number[]} size Array with the size of each dimension | ||
671 | 3255 | * @throws DimensionError | ||
672 | 3256 | */ | ||
673 | 3257 | exports.validate = function(array, size) { | ||
674 | 3258 | var isScalar = (size.length == 0); | ||
675 | 3259 | if (isScalar) { | ||
676 | 3260 | // scalar | ||
677 | 3261 | if (Array.isArray(array)) { | ||
678 | 3262 | throw new DimensionError(array.length, 0); | ||
679 | 3263 | } | ||
680 | 3264 | } | ||
681 | 3265 | else { | ||
682 | 3266 | // array | ||
683 | 3267 | _validate(array, size, 0); | ||
684 | 3268 | } | ||
685 | 3269 | }; | ||
686 | 3270 | |||
687 | 3271 | /** | ||
688 | 3272 | * Test whether index is an integer number with index >= 0 and index < length | ||
689 | 3273 | * @param {number} index Zero-based index | ||
690 | 3274 | * @param {number} [length] Length of the array | ||
691 | 3275 | */ | ||
692 | 3276 | exports.validateIndex = function(index, length) { | ||
693 | 3277 | if (!number.isNumber(index) || !number.isInteger(index)) { | ||
694 | 3278 | throw new TypeError('Index must be an integer (value: ' + index + ')'); | ||
695 | 3279 | } | ||
696 | 3280 | if (index < 0) { | ||
697 | 3281 | throw new IndexError(index); | ||
698 | 3282 | } | ||
699 | 3283 | if (length !== undefined && index >= length) { | ||
700 | 3284 | throw new IndexError(index, length); | ||
701 | 3285 | } | ||
702 | 3286 | }; | ||
703 | 3287 | |||
704 | 3288 | // a constant used to specify an undefined defaultValue | ||
705 | 3289 | exports.UNINITIALIZED = {}; | ||
706 | 3290 | |||
707 | 3291 | /** | ||
708 | 3292 | * Resize a multi dimensional array. The resized array is returned. | ||
709 | 3293 | * @param {Array} array Array to be resized | ||
710 | 3294 | * @param {Array.<number>} size Array with the size of each dimension | ||
711 | 3295 | * @param {*} [defaultValue=0] Value to be filled in in new entries, | ||
712 | 3296 | * zero by default. To leave new entries undefined, | ||
713 | 3297 | * specify array.UNINITIALIZED as defaultValue | ||
714 | 3298 | * @return {Array} array The resized array | ||
715 | 3299 | */ | ||
716 | 3300 | exports.resize = function(array, size, defaultValue) { | ||
717 | 3301 | // TODO: add support for scalars, having size=[] ? | ||
718 | 3302 | |||
719 | 3303 | // check the type of the arguments | ||
720 | 3304 | if (!Array.isArray(array) || !Array.isArray(size)) { | ||
721 | 3305 | throw new TypeError('Array expected'); | ||
722 | 3306 | } | ||
723 | 3307 | if (size.length === 0) { | ||
724 | 3308 | throw new Error('Resizing to scalar is not supported'); | ||
725 | 3309 | } | ||
726 | 3310 | |||
727 | 3311 | // check whether size contains positive integers | ||
728 | 3312 | size.forEach(function (value) { | ||
729 | 3313 | if (!number.isNumber(value) || !number.isInteger(value) || value < 0) { | ||
730 | 3314 | throw new TypeError('Invalid size, must contain positive integers ' + | ||
731 | 3315 | '(size: ' + string.format(size) + ')'); | ||
732 | 3316 | } | ||
733 | 3317 | }); | ||
734 | 3318 | |||
735 | 3319 | // recursively resize the array | ||
736 | 3320 | var _defaultValue = (defaultValue !== undefined) ? defaultValue : 0; | ||
737 | 3321 | _resize(array, size, 0, _defaultValue); | ||
738 | 3322 | |||
739 | 3323 | return array; | ||
740 | 3324 | }; | ||
741 | 3325 | |||
742 | 3326 | /** | ||
743 | 3327 | * Recursively resize a multi dimensional array | ||
744 | 3328 | * @param {Array} array Array to be resized | ||
745 | 3329 | * @param {number[]} size Array with the size of each dimension | ||
746 | 3330 | * @param {number} dim Current dimension | ||
747 | 3331 | * @param {*} [defaultValue] Value to be filled in in new entries, | ||
748 | 3332 | * undefined by default. | ||
749 | 3333 | * @private | ||
750 | 3334 | */ | ||
751 | 3335 | function _resize (array, size, dim, defaultValue) { | ||
752 | 3336 | var i; | ||
753 | 3337 | var elem; | ||
754 | 3338 | var oldLen = array.length; | ||
755 | 3339 | var newLen = size[dim]; | ||
756 | 3340 | var minLen = Math.min(oldLen, newLen); | ||
757 | 3341 | |||
758 | 3342 | // apply new length | ||
759 | 3343 | array.length = newLen; | ||
760 | 3344 | |||
761 | 3345 | if (dim < size.length - 1) { | ||
762 | 3346 | // non-last dimension | ||
763 | 3347 | var dimNext = dim + 1; | ||
764 | 3348 | |||
765 | 3349 | // resize existing child arrays | ||
766 | 3350 | for (i = 0; i < minLen; i++) { | ||
767 | 3351 | // resize child array | ||
768 | 3352 | elem = array[i]; | ||
769 | 3353 | if (!Array.isArray(elem)) { | ||
770 | 3354 | elem = [elem]; // add a dimension | ||
771 | 3355 | array[i] = elem; | ||
772 | 3356 | } | ||
773 | 3357 | _resize(elem, size, dimNext, defaultValue); | ||
774 | 3358 | } | ||
775 | 3359 | |||
776 | 3360 | // create new child arrays | ||
777 | 3361 | for (i = minLen; i < newLen; i++) { | ||
778 | 3362 | // get child array | ||
779 | 3363 | elem = []; | ||
780 | 3364 | array[i] = elem; | ||
781 | 3365 | |||
782 | 3366 | // resize new child array | ||
783 | 3367 | _resize(elem, size, dimNext, defaultValue); | ||
784 | 3368 | } | ||
785 | 3369 | } | ||
786 | 3370 | else { | ||
787 | 3371 | // last dimension | ||
788 | 3372 | |||
789 | 3373 | // remove dimensions of existing values | ||
790 | 3374 | for (i = 0; i < minLen; i++) { | ||
791 | 3375 | while (Array.isArray(array[i])) { | ||
792 | 3376 | array[i] = array[i][0]; | ||
793 | 3377 | } | ||
794 | 3378 | } | ||
795 | 3379 | |||
796 | 3380 | if(defaultValue !== exports.UNINITIALIZED) { | ||
797 | 3381 | // fill new elements with the default value | ||
798 | 3382 | for (i = minLen; i < newLen; i++) { | ||
799 | 3383 | array[i] = object.clone(defaultValue); | ||
800 | 3384 | } | ||
801 | 3385 | } | ||
802 | 3386 | } | ||
803 | 3387 | } | ||
804 | 3388 | |||
805 | 3389 | /** | ||
806 | 3390 | * Squeeze a multi dimensional array | ||
807 | 3391 | * @param {Array} array | ||
808 | 3392 | * @param {Array} [size] | ||
809 | 3393 | * @returns {Array} returns the array itself | ||
810 | 3394 | */ | ||
811 | 3395 | exports.squeeze = function(array, size) { | ||
812 | 3396 | var s = size || exports.size(array); | ||
813 | 3397 | |||
814 | 3398 | // squeeze outer dimensions | ||
815 | 3399 | while (Array.isArray(array) && array.length === 1) { | ||
816 | 3400 | array = array[0]; | ||
817 | 3401 | s.shift(); | ||
818 | 3402 | } | ||
819 | 3403 | |||
820 | 3404 | // find the first dimension to be squeezed | ||
821 | 3405 | var dims = s.length; | ||
822 | 3406 | while (s[dims - 1] === 1) { | ||
823 | 3407 | dims--; | ||
824 | 3408 | } | ||
825 | 3409 | |||
826 | 3410 | // squeeze inner dimensions | ||
827 | 3411 | if (dims < s.length) { | ||
828 | 3412 | array = _squeeze(array, dims, 0); | ||
829 | 3413 | s.length = dims; | ||
830 | 3414 | } | ||
831 | 3415 | |||
832 | 3416 | return array; | ||
833 | 3417 | }; | ||
834 | 3418 | |||
835 | 3419 | /** | ||
836 | 3420 | * Recursively squeeze a multi dimensional array | ||
837 | 3421 | * @param {Array} array | ||
838 | 3422 | * @param {number} dims Required number of dimensions | ||
839 | 3423 | * @param {number} dim Current dimension | ||
840 | 3424 | * @returns {Array | *} Returns the squeezed array | ||
841 | 3425 | * @private | ||
842 | 3426 | */ | ||
843 | 3427 | function _squeeze (array, dims, dim) { | ||
844 | 3428 | var i, ii; | ||
845 | 3429 | |||
846 | 3430 | if (dim < dims) { | ||
847 | 3431 | var next = dim + 1; | ||
848 | 3432 | for (i = 0, ii = array.length; i < ii; i++) { | ||
849 | 3433 | array[i] = _squeeze(array[i], dims, next); | ||
850 | 3434 | } | ||
851 | 3435 | } | ||
852 | 3436 | else { | ||
853 | 3437 | while (Array.isArray(array)) { | ||
854 | 3438 | array = array[0]; | ||
855 | 3439 | } | ||
856 | 3440 | } | ||
857 | 3441 | |||
858 | 3442 | return array; | ||
859 | 3443 | } | ||
860 | 3444 | |||
861 | 3445 | /** | ||
862 | 3446 | * Unsqueeze a multi dimensional array: add dimensions when missing | ||
863 | 3447 | * @param {Array} array | ||
864 | 3448 | * @param {number} dims Desired number of dimensions of the array | ||
865 | 3449 | * @param {number} [outer] Number of outer dimensions to be added | ||
866 | 3450 | * @param {Array} [size] Current size of array | ||
867 | 3451 | * @returns {Array} returns the array itself | ||
868 | 3452 | * @private | ||
869 | 3453 | */ | ||
870 | 3454 | exports.unsqueeze = function(array, dims, outer, size) { | ||
871 | 3455 | var s = size || exports.size(array); | ||
872 | 3456 | |||
873 | 3457 | // unsqueeze outer dimensions | ||
874 | 3458 | if (outer) { | ||
875 | 3459 | for (var i = 0; i < outer; i++) { | ||
876 | 3460 | array = [array]; | ||
877 | 3461 | s.unshift(1); | ||
878 | 3462 | } | ||
879 | 3463 | } | ||
880 | 3464 | |||
881 | 3465 | // unsqueeze inner dimensions | ||
882 | 3466 | array = _unsqueeze(array, dims, 0); | ||
883 | 3467 | while (s.length < dims) { | ||
884 | 3468 | s.push(1); | ||
885 | 3469 | } | ||
886 | 3470 | |||
887 | 3471 | return array; | ||
888 | 3472 | }; | ||
889 | 3473 | |||
890 | 3474 | /** | ||
891 | 3475 | * Recursively unsqueeze a multi dimensional array | ||
892 | 3476 | * @param {Array} array | ||
893 | 3477 | * @param {number} dims Required number of dimensions | ||
894 | 3478 | * @param {number} dim Current dimension | ||
895 | 3479 | * @returns {Array | *} Returns the squeezed array | ||
896 | 3480 | * @private | ||
897 | 3481 | */ | ||
898 | 3482 | function _unsqueeze (array, dims, dim) { | ||
899 | 3483 | var i, ii; | ||
900 | 3484 | |||
901 | 3485 | if (Array.isArray(array)) { | ||
902 | 3486 | var next = dim + 1; | ||
903 | 3487 | for (i = 0, ii = array.length; i < ii; i++) { | ||
904 | 3488 | array[i] = _unsqueeze(array[i], dims, next); | ||
905 | 3489 | } | ||
906 | 3490 | } | ||
907 | 3491 | else { | ||
908 | 3492 | for (var d = dim; d < dims; d++) { | ||
909 | 3493 | array = [array]; | ||
910 | 3494 | } | ||
911 | 3495 | } | ||
912 | 3496 | |||
913 | 3497 | return array; | ||
914 | 3498 | } | ||
915 | 3499 | /** | ||
916 | 3500 | * Flatten a multi dimensional array, put all elements in a one dimensional | ||
917 | 3501 | * array | ||
918 | 3502 | * @param {Array} array A multi dimensional array | ||
919 | 3503 | * @return {Array} The flattened array (1 dimensional) | ||
920 | 3504 | */ | ||
921 | 3505 | exports.flatten = function(array) { | ||
922 | 3506 | if (!Array.isArray(array)) { | ||
923 | 3507 | //if not an array, return as is | ||
924 | 3508 | return array; | ||
925 | 3509 | } | ||
926 | 3510 | var flat = []; | ||
927 | 3511 | |||
928 | 3512 | array.forEach(function callback(value) { | ||
929 | 3513 | if (Array.isArray(value)) { | ||
930 | 3514 | value.forEach(callback); //traverse through sub-arrays recursively | ||
931 | 3515 | } | ||
932 | 3516 | else { | ||
933 | 3517 | flat.push(value); | ||
934 | 3518 | } | ||
935 | 3519 | }); | ||
936 | 3520 | |||
937 | 3521 | return flat; | ||
938 | 3522 | }; | ||
939 | 3523 | |||
940 | 3524 | /** | ||
941 | 3525 | * Test whether an object is an array | ||
942 | 3526 | * @param {*} value | ||
943 | 3527 | * @return {boolean} isArray | ||
944 | 3528 | */ | ||
945 | 3529 | exports.isArray = Array.isArray; | ||
946 | 3530 | |||
947 | 3531 | |||
948 | 3532 | /***/ }, | ||
949 | 3533 | /* 19 */ | ||
950 | 3534 | /***/ function(module, exports) { | ||
951 | 3535 | |||
952 | 3536 | 'use strict'; | ||
953 | 3537 | |||
954 | 3538 | /** | ||
955 | 3539 | * Determine the type of a variable | ||
956 | 3540 | * | ||
957 | 3541 | * type(x) | ||
958 | 3542 | * | ||
959 | 3543 | * The following types are recognized: | ||
960 | 3544 | * | ||
961 | 3545 | * 'undefined' | ||
962 | 3546 | * 'null' | ||
963 | 3547 | * 'boolean' | ||
964 | 3548 | * 'number' | ||
965 | 3549 | * 'string' | ||
966 | 3550 | * 'Array' | ||
967 | 3551 | * 'Function' | ||
968 | 3552 | * 'Date' | ||
969 | 3553 | * 'RegExp' | ||
970 | 3554 | * 'Object' | ||
971 | 3555 | * | ||
972 | 3556 | * @param {*} x | ||
973 | 3557 | * @return {string} Returns the name of the type. Primitive types are lower case, | ||
974 | 3558 | * non-primitive types are upper-camel-case. | ||
975 | 3559 | * For example 'number', 'string', 'Array', 'Date'. | ||
976 | 3560 | */ | ||
977 | 3561 | exports.type = function(x) { | ||
978 | 3562 | var type = typeof x; | ||
979 | 3563 | |||
980 | 3564 | if (type === 'object') { | ||
981 | 3565 | if (x === null) return 'null'; | ||
982 | 3566 | if (x instanceof Boolean) return 'boolean'; | ||
983 | 3567 | if (x instanceof Number) return 'number'; | ||
984 | 3568 | if (x instanceof String) return 'string'; | ||
985 | 3569 | if (Array.isArray(x)) return 'Array'; | ||
986 | 3570 | if (x instanceof Date) return 'Date'; | ||
987 | 3571 | if (x instanceof RegExp) return 'RegExp'; | ||
988 | 3572 | |||
989 | 3573 | return 'Object'; | ||
990 | 3574 | } | ||
991 | 3575 | |||
992 | 3576 | if (type === 'function') return 'Function'; | ||
993 | 3577 | |||
994 | 3578 | return type; | ||
995 | 3579 | }; | ||
996 | 3580 | |||
997 | 3581 | |||
998 | 3582 | /***/ }, | ||
999 | 3583 | /* 20 */ | ||
1000 | 3584 | /***/ function(module, exports, __webpack_require__) { | ||
1001 | 3585 | |||
1002 | 3586 | 'use strict'; | ||
1003 | 3587 | |||
1004 | 3588 | var formatNumber = __webpack_require__(8).format; | ||
1005 | 3589 | var formatBigNumber = __webpack_require__(21).format; | ||
1006 | 3590 | |||
1007 | 3591 | /** | ||
1008 | 3592 | * Test whether value is a string | ||
1009 | 3593 | * @param {*} value | ||
1010 | 3594 | * @return {boolean} isString | ||
1011 | 3595 | */ | ||
1012 | 3596 | exports.isString = function(value) { | ||
1013 | 3597 | return typeof value === 'string'; | ||
1014 | 3598 | }; | ||
1015 | 3599 | |||
1016 | 3600 | /** | ||
1017 | 3601 | * Check if a text ends with a certain string. | ||
1018 | 3602 | * @param {string} text | ||
1019 | 3603 | * @param {string} search | ||
1020 | 3604 | */ | ||
1021 | 3605 | exports.endsWith = function(text, search) { | ||
1022 | 3606 | var start = text.length - search.length; | ||
1023 | 3607 | var end = text.length; | ||
1024 | 3608 | return (text.substring(start, end) === search); | ||
1025 | 3609 | }; | ||
1026 | 3610 | |||
1027 | 3611 | /** | ||
1028 | 3612 | * Format a value of any type into a string. | ||
1029 | 3613 | * | ||
1030 | 3614 | * Usage: | ||
1031 | 3615 | * math.format(value) | ||
1032 | 3616 | * math.format(value, precision) | ||
1033 | 3617 | * | ||
1034 | 3618 | * If value is a function, the returned string is 'function' unless the function | ||
1035 | 3619 | * has a property `description`, in that case this properties value is returned. | ||
1036 | 3620 | * | ||
1037 | 3621 | * Example usage: | ||
1038 | 3622 | * math.format(2/7); // '0.2857142857142857' | ||
1039 | 3623 | * math.format(math.pi, 3); // '3.14' | ||
1040 | 3624 | * math.format(new Complex(2, 3)); // '2 + 3i' | ||
1041 | 3625 | * math.format('hello'); // '"hello"' | ||
1042 | 3626 | * | ||
1043 | 3627 | * @param {*} value Value to be stringified | ||
1044 | 3628 | * @param {Object | number | Function} [options] Formatting options. See | ||
1045 | 3629 | * lib/utils/number:format for a | ||
1046 | 3630 | * description of the available | ||
1047 | 3631 | * options. | ||
1048 | 3632 | * @return {string} str | ||
1049 | 3633 | */ | ||
1050 | 3634 | exports.format = function(value, options) { | ||
1051 | 3635 | if (typeof value === 'number') { | ||
1052 | 3636 | return formatNumber(value, options); | ||
1053 | 3637 | } | ||
1054 | 3638 | |||
1055 | 3639 | if (value && value.isBigNumber === true) { | ||
1056 | 3640 | return formatBigNumber(value, options); | ||
1057 | 3641 | } | ||
1058 | 3642 | |||
1059 | 3643 | if (value && value.isFraction === true) { | ||
1060 | 3644 | if (!options || options.fraction !== 'decimal') { | ||
1061 | 3645 | // output as ratio, like '1/3' | ||
1062 | 3646 | return (value.s * value.n) + '/' + value.d; | ||
1063 | 3647 | } | ||
1064 | 3648 | else { | ||
1065 | 3649 | // output as decimal, like '0.(3)' | ||
1066 | 3650 | return value.toString(); | ||
1067 | 3651 | } | ||
1068 | 3652 | } | ||
1069 | 3653 | |||
1070 | 3654 | if (Array.isArray(value)) { | ||
1071 | 3655 | return formatArray(value, options); | ||
1072 | 3656 | } | ||
1073 | 3657 | |||
1074 | 3658 | if (exports.isString(value)) { | ||
1075 | 3659 | return '"' + value + '"'; | ||
1076 | 3660 | } | ||
1077 | 3661 | |||
1078 | 3662 | if (typeof value === 'function') { | ||
1079 | 3663 | return value.syntax ? value.syntax + '' : 'function'; | ||
1080 | 3664 | } | ||
1081 | 3665 | |||
1082 | 3666 | if (typeof value === 'object') { | ||
1083 | 3667 | if (typeof value.format === 'function') { | ||
1084 | 3668 | return value.format(options); | ||
1085 | 3669 | } | ||
1086 | 3670 | else { | ||
1087 | 3671 | return value.toString(); | ||
1088 | 3672 | } | ||
1089 | 3673 | } | ||
1090 | 3674 | |||
1091 | 3675 | return String(value); | ||
1092 | 3676 | }; | ||
1093 | 3677 | |||
1094 | 3678 | /** | ||
1095 | 3679 | * Recursively format an n-dimensional matrix | ||
1096 | 3680 | * Example output: "[[1, 2], [3, 4]]" | ||
1097 | 3681 | * @param {Array} array | ||
1098 | 3682 | * @param {Object | number | Function} [options] Formatting options. See | ||
1099 | 3683 | * lib/utils/number:format for a | ||
1100 | 3684 | * description of the available | ||
1101 | 3685 | * options. | ||
1102 | 3686 | * @returns {string} str | ||
1103 | 3687 | */ | ||
1104 | 3688 | function formatArray (array, options) { | ||
1105 | 3689 | if (Array.isArray(array)) { | ||
1106 | 3690 | var str = '['; | ||
1107 | 3691 | var len = array.length; | ||
1108 | 3692 | for (var i = 0; i < len; i++) { | ||
1109 | 3693 | if (i != 0) { | ||
1110 | 3694 | str += ', '; | ||
1111 | 3695 | } | ||
1112 | 3696 | str += formatArray(array[i], options); | ||
1113 | 3697 | } | ||
1114 | 3698 | str += ']'; | ||
1115 | 3699 | return str; | ||
1116 | 3700 | } | ||
1117 | 3701 | else { | ||
1118 | 3702 | return exports.format(array, options); | ||
1119 | 3703 | } | ||
1120 | 3704 | } | ||
1121 | 3705 | |||
1122 | 3706 | |||
1123 | 3707 | /***/ }, | ||
1124 | 3708 | /* 21 */ | ||
1125 | 3709 | /***/ function(module, exports) { | ||
1126 | 3710 | |||
1127 | 3711 | /** | ||
1128 | 3712 | * Convert a BigNumber to a formatted string representation. | ||
1129 | 3713 | * | ||
1130 | 3714 | * Syntax: | ||
1131 | 3715 | * | ||
1132 | 3716 | * format(value) | ||
1133 | 3717 | * format(value, options) | ||
1134 | 3718 | * format(value, precision) | ||
1135 | 3719 | * format(value, fn) | ||
1136 | 3720 | * | ||
1137 | 3721 | * Where: | ||
1138 | 3722 | * | ||
1139 | 3723 | * {number} value The value to be formatted | ||
1140 | 3724 | * {Object} options An object with formatting options. Available options: | ||
1141 | 3725 | * {string} notation | ||
1142 | 3726 | * Number notation. Choose from: | ||
1143 | 3727 | * 'fixed' Always use regular number notation. | ||
1144 | 3728 | * For example '123.40' and '14000000' | ||
1145 | 3729 | * 'exponential' Always use exponential notation. | ||
1146 | 3730 | * For example '1.234e+2' and '1.4e+7' | ||
1147 | 3731 | * 'auto' (default) Regular number notation for numbers | ||
1148 | 3732 | * having an absolute value between | ||
1149 | 3733 | * `lower` and `upper` bounds, and uses | ||
1150 | 3734 | * exponential notation elsewhere. | ||
1151 | 3735 | * Lower bound is included, upper bound | ||
1152 | 3736 | * is excluded. | ||
1153 | 3737 | * For example '123.4' and '1.4e7'. | ||
1154 | 3738 | * {number} precision A number between 0 and 16 to round | ||
1155 | 3739 | * the digits of the number. | ||
1156 | 3740 | * In case of notations 'exponential' and | ||
1157 | 3741 | * 'auto', `precision` defines the total | ||
1158 | 3742 | * number of significant digits returned | ||
1159 | 3743 | * and is undefined by default. | ||
1160 | 3744 | * In case of notation 'fixed', | ||
1161 | 3745 | * `precision` defines the number of | ||
1162 | 3746 | * significant digits after the decimal | ||
1163 | 3747 | * point, and is 0 by default. | ||
1164 | 3748 | * {Object} exponential An object containing two parameters, | ||
1165 | 3749 | * {number} lower and {number} upper, | ||
1166 | 3750 | * used by notation 'auto' to determine | ||
1167 | 3751 | * when to return exponential notation. | ||
1168 | 3752 | * Default values are `lower=1e-3` and | ||
1169 | 3753 | * `upper=1e5`. | ||
1170 | 3754 | * Only applicable for notation `auto`. | ||
1171 | 3755 | * {Function} fn A custom formatting function. Can be used to override the | ||
1172 | 3756 | * built-in notations. Function `fn` is called with `value` as | ||
1173 | 3757 | * parameter and must return a string. Is useful for example to | ||
1174 | 3758 | * format all values inside a matrix in a particular way. | ||
1175 | 3759 | * | ||
1176 | 3760 | * Examples: | ||
1177 | 3761 | * | ||
1178 | 3762 | * format(6.4); // '6.4' | ||
1179 | 3763 | * format(1240000); // '1.24e6' | ||
1180 | 3764 | * format(1/3); // '0.3333333333333333' | ||
1181 | 3765 | * format(1/3, 3); // '0.333' | ||
1182 | 3766 | * format(21385, 2); // '21000' | ||
1183 | 3767 | * format(12.071, {notation: 'fixed'}); // '12' | ||
1184 | 3768 | * format(2.3, {notation: 'fixed', precision: 2}); // '2.30' | ||
1185 | 3769 | * format(52.8, {notation: 'exponential'}); // '5.28e+1' | ||
1186 | 3770 | * | ||
1187 | 3771 | * @param {BigNumber} value | ||
1188 | 3772 | * @param {Object | Function | number} [options] | ||
1189 | 3773 | * @return {string} str The formatted value | ||
1190 | 3774 | */ | ||
1191 | 3775 | exports.format = function (value, options) { | ||
1192 | 3776 | if (typeof options === 'function') { | ||
1193 | 3777 | // handle format(value, fn) | ||
1194 | 3778 | return options(value); | ||
1195 | 3779 | } | ||
1196 | 3780 | |||
1197 | 3781 | // handle special cases | ||
1198 | 3782 | if (!value.isFinite()) { | ||
1199 | 3783 | return value.isNaN() ? 'NaN' : (value.gt(0) ? 'Infinity' : '-Infinity'); | ||
1200 | 3784 | } | ||
1201 | 3785 | |||
1202 | 3786 | // default values for options | ||
1203 | 3787 | var notation = 'auto'; | ||
1204 | 3788 | var precision = undefined; | ||
1205 | 3789 | |||
1206 | 3790 | if (options !== undefined) { | ||
1207 | 3791 | // determine notation from options | ||
1208 | 3792 | if (options.notation) { | ||
1209 | 3793 | notation = options.notation; | ||
1210 | 3794 | } | ||
1211 | 3795 | |||
1212 | 3796 | // determine precision from options | ||
1213 | 3797 | if (typeof options === 'number') { | ||
1214 | 3798 | precision = options; | ||
1215 | 3799 | } | ||
1216 | 3800 | else if (options.precision) { | ||
1217 | 3801 | precision = options.precision; | ||
1218 | 3802 | } | ||
1219 | 3803 | } | ||
1220 | 3804 | |||
1221 | 3805 | // handle the various notations | ||
1222 | 3806 | switch (notation) { | ||
1223 | 3807 | case 'fixed': | ||
1224 | 3808 | return exports.toFixed(value, precision); | ||
1225 | 3809 | |||
1226 | 3810 | case 'exponential': | ||
1227 | 3811 | return exports.toExponential(value, precision); | ||
1228 | 3812 | |||
1229 | 3813 | case 'auto': | ||
1230 | 3814 | // determine lower and upper bound for exponential notation. | ||
1231 | 3815 | // TODO: implement support for upper and lower to be BigNumbers themselves | ||
1232 | 3816 | var lower = 1e-3; | ||
1233 | 3817 | var upper = 1e5; | ||
1234 | 3818 | if (options && options.exponential) { | ||
1235 | 3819 | if (options.exponential.lower !== undefined) { | ||
1236 | 3820 | lower = options.exponential.lower; | ||
1237 | 3821 | } | ||
1238 | 3822 | if (options.exponential.upper !== undefined) { | ||
1239 | 3823 | upper = options.exponential.upper; | ||
1240 | 3824 | } | ||
1241 | 3825 | } | ||
1242 | 3826 | |||
1243 | 3827 | // adjust the configuration of the BigNumber constructor (yeah, this is quite tricky...) | ||
1244 | 3828 | var oldConfig = { | ||
1245 | 3829 | toExpNeg: value.constructor.toExpNeg, | ||
1246 | 3830 | toExpPos: value.constructor.toExpPos | ||
1247 | 3831 | }; | ||
1248 | 3832 | |||
1249 | 3833 | value.constructor.config({ | ||
1250 | 3834 | toExpNeg: Math.round(Math.log(lower) / Math.LN10), | ||
1251 | 3835 | toExpPos: Math.round(Math.log(upper) / Math.LN10) | ||
1252 | 3836 | }); | ||
1253 | 3837 | |||
1254 | 3838 | // handle special case zero | ||
1255 | 3839 | if (value.isZero()) return '0'; | ||
1256 | 3840 | |||
1257 | 3841 | // determine whether or not to output exponential notation | ||
1258 | 3842 | var str; | ||
1259 | 3843 | var abs = value.abs(); | ||
1260 | 3844 | if (abs.gte(lower) && abs.lt(upper)) { | ||
1261 | 3845 | // normal number notation | ||
1262 | 3846 | str = value.toSignificantDigits(precision).toFixed(); | ||
1263 | 3847 | } | ||
1264 | 3848 | else { | ||
1265 | 3849 | // exponential notation | ||
1266 | 3850 | str = exports.toExponential(value, precision); | ||
1267 | 3851 | } | ||
1268 | 3852 | |||
1269 | 3853 | // remove trailing zeros after the decimal point | ||
1270 | 3854 | return str.replace(/((\.\d*?)(0+))($|e)/, function () { | ||
1271 | 3855 | var digits = arguments[2]; | ||
1272 | 3856 | var e = arguments[4]; | ||
1273 | 3857 | return (digits !== '.') ? digits + e : e; | ||
1274 | 3858 | }); | ||
1275 | 3859 | |||
1276 | 3860 | default: | ||
1277 | 3861 | throw new Error('Unknown notation "' + notation + '". ' + | ||
1278 | 3862 | 'Choose "auto", "exponential", or "fixed".'); | ||
1279 | 3863 | } | ||
1280 | 3864 | }; | ||
1281 | 3865 | |||
1282 | 3866 | /** | ||
1283 | 3867 | * Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3' | ||
1284 | 3868 | * @param {BigNumber} value | ||
1285 | 3869 | * @param {number} [precision] Number of digits in formatted output. | ||
1286 | 3870 | * If not provided, the maximum available digits | ||
1287 | 3871 | * is used. | ||
1288 | 3872 | * @returns {string} str | ||
1289 | 3873 | */ | ||
1290 | 3874 | exports.toExponential = function (value, precision) { | ||
1291 | 3875 | if (precision !== undefined) { | ||
1292 | 3876 | return value.toExponential(precision - 1); // Note the offset of one | ||
1293 | 3877 | } | ||
1294 | 3878 | else { | ||
1295 | 3879 | return value.toExponential(); | ||
1296 | 3880 | } | ||
1297 | 3881 | }; | ||
1298 | 3882 | |||
1299 | 3883 | /** | ||
1300 | 3884 | * Format a number with fixed notation. | ||
1301 | 3885 | * @param {BigNumber} value | ||
1302 | 3886 | * @param {number} [precision=0] Optional number of decimals after the | ||
1303 | 3887 | * decimal point. Zero by default. | ||
1304 | 3888 | */ | ||
1305 | 3889 | exports.toFixed = function (value, precision) { | ||
1306 | 3890 | return value.toFixed(precision || 0); | ||
1307 | 3891 | // Note: the (precision || 0) is needed as the toFixed of BigNumber has an | ||
1308 | 3892 | // undefined default precision instead of 0. | ||
1309 | 3893 | } | ||
1310 | 3894 | |||
1311 | 3895 | |||
1312 | 3896 | /***/ }, | ||
1313 | 3897 | /* 22 */ | ||
1314 | 3898 | /***/ function(module, exports) { | ||
1315 | 3899 | |||
1316 | 3900 | 'use strict'; | ||
1317 | 3901 | |||
1318 | 3902 | /** | ||
1319 | 3903 | * Create a range error with the message: | ||
1320 | 3904 | * 'Dimension mismatch (<actual size> != <expected size>)' | ||
1321 | 3905 | * @param {number | number[]} actual The actual size | ||
1322 | 3906 | * @param {number | number[]} expected The expected size | ||
1323 | 3907 | * @param {string} [relation='!='] Optional relation between actual | ||
1324 | 3908 | * and expected size: '!=', '<', etc. | ||
1325 | 3909 | * @extends RangeError | ||
1326 | 3910 | */ | ||
1327 | 3911 | function DimensionError(actual, expected, relation) { | ||
1328 | 3912 | if (!(this instanceof DimensionError)) { | ||
1329 | 3913 | throw new SyntaxError('Constructor must be called with the new operator'); | ||
1330 | 3914 | } | ||
1331 | 3915 | |||
1332 | 3916 | this.actual = actual; | ||
1333 | 3917 | this.expected = expected; | ||
1334 | 3918 | this.relation = relation; | ||
1335 | 3919 | |||
1336 | 3920 | this.message = 'Dimension mismatch (' + | ||
1337 | 3921 | (Array.isArray(actual) ? ('[' + actual.join(', ') + ']') : actual) + | ||
1338 | 3922 | ' ' + (this.relation || '!=') + ' ' + | ||
1339 | 3923 | (Array.isArray(expected) ? ('[' + expected.join(', ') + ']') : expected) + | ||
1340 | 3924 | ')'; | ||
1341 | 3925 | |||
1342 | 3926 | this.stack = (new Error()).stack; | ||
1343 | 3927 | } | ||
1344 | 3928 | |||
1345 | 3929 | DimensionError.prototype = new RangeError(); | ||
1346 | 3930 | DimensionError.prototype.constructor = RangeError; | ||
1347 | 3931 | DimensionError.prototype.name = 'DimensionError'; | ||
1348 | 3932 | DimensionError.prototype.isDimensionError = true; | ||
1349 | 3933 | |||
1350 | 3934 | module.exports = DimensionError; | ||
1351 | 3935 | |||
1352 | 3936 | |||
1353 | 3937 | /***/ }, | ||
1354 | 3938 | /* 23 */ | ||
1355 | 3939 | /***/ function(module, exports) { | ||
1356 | 3940 | |||
1357 | 3941 | 'use strict'; | ||
1358 | 3942 | |||
1359 | 3943 | function factory (type, config, load, typed) { | ||
1360 | 3944 | /** | ||
1361 | 3945 | * Create a Matrix. The function creates a new `math.type.Matrix` object from | ||
1362 | 3946 | * an `Array`. A Matrix has utility functions to manipulate the data in the | ||
1363 | 3947 | * matrix, like getting the size and getting or setting values in the matrix. | ||
1364 | 3948 | * Supported storage formats are 'dense' and 'sparse'. | ||
1365 | 3949 | * | ||
1366 | 3950 | * Syntax: | ||
1367 | 3951 | * | ||
1368 | 3952 | * math.matrix() // creates an empty matrix using default storage format (dense). | ||
1369 | 3953 | * math.matrix(data) // creates a matrix with initial data using default storage format (dense). | ||
1370 | 3954 | * math.matrix('dense') // creates an empty matrix using the given storage format. | ||
1371 | 3955 | * math.matrix(data, 'dense') // creates a matrix with initial data using the given storage format. | ||
1372 | 3956 | * math.matrix(data, 'sparse') // creates a sparse matrix with initial data. | ||
1373 | 3957 | * math.matrix(data, 'sparse', 'number') // creates a sparse matrix with initial data, number data type. | ||
1374 | 3958 | * | ||
1375 | 3959 | * Examples: | ||
1376 | 3960 | * | ||
1377 | 3961 | * var m = math.matrix([[1, 2], [3, 4]]); | ||
1378 | 3962 | * m.size(); // Array [2, 2] | ||
1379 | 3963 | * m.resize([3, 2], 5); | ||
1380 | 3964 | * m.valueOf(); // Array [[1, 2], [3, 4], [5, 5]] | ||
1381 | 3965 | * m.get([1, 0]) // number 3 | ||
1382 | 3966 | * | ||
1383 | 3967 | * See also: | ||
1384 | 3968 | * | ||
1385 | 3969 | * bignumber, boolean, complex, index, number, string, unit, sparse | ||
1386 | 3970 | * | ||
1387 | 3971 | * @param {Array | Matrix} [data] A multi dimensional array | ||
1388 | 3972 | * @param {string} [format] The Matrix storage format | ||
1389 | 3973 | * | ||
1390 | 3974 | * @return {Matrix} The created matrix | ||
1391 | 3975 | */ | ||
1392 | 3976 | var matrix = typed('matrix', { | ||
1393 | 3977 | '': function () { | ||
1394 | 3978 | return _create([]); | ||
1395 | 3979 | }, | ||
1396 | 3980 | |||
1397 | 3981 | 'string': function (format) { | ||
1398 | 3982 | return _create([], format); | ||
1399 | 3983 | }, | ||
1400 | 3984 | |||
1401 | 3985 | 'string, string': function (format, datatype) { | ||
1402 | 3986 | return _create([], format, datatype); | ||
1403 | 3987 | }, | ||
1404 | 3988 | |||
1405 | 3989 | 'Array': function (data) { | ||
1406 | 3990 | return _create(data); | ||
1407 | 3991 | }, | ||
1408 | 3992 | |||
1409 | 3993 | 'Matrix': function (data) { | ||
1410 | 3994 | return _create(data, data.storage()); | ||
1411 | 3995 | }, | ||
1412 | 3996 | |||
1413 | 3997 | 'Array | Matrix, string': _create, | ||
1414 | 3998 | |||
1415 | 3999 | 'Array | Matrix, string, string': _create | ||
1416 | 4000 | }); | ||
1417 | 4001 | |||
1418 | 4002 | matrix.toTex = { | ||
1419 | 4003 | 0: '\\begin{bmatrix}\\end{bmatrix}', | ||
1420 | 4004 | 1: '\\left(${args[0]}\\right)', | ||
1421 | 4005 | 2: '\\left(${args[0]}\\right)' | ||
1422 | 4006 | }; | ||
1423 | 4007 | |||
1424 | 4008 | return matrix; | ||
1425 | 4009 | |||
1426 | 4010 | /** | ||
1427 | 4011 | * Create a new Matrix with given storage format | ||
1428 | 4012 | * @param {Array} data | ||
1429 | 4013 | * @param {string} [format] | ||
1430 | 4014 | * @param {string} [datatype] | ||
1431 | 4015 | * @returns {Matrix} Returns a new Matrix | ||
1432 | 4016 | * @private | ||
1433 | 4017 | */ | ||
1434 | 4018 | function _create(data, format, datatype) { | ||
1435 | 4019 | // get storage format constructor | ||
1436 | 4020 | var M = type.Matrix.storage(format || 'default'); | ||
1437 | 4021 | |||
1438 | 4022 | // create instance | ||
1439 | 4023 | return new M(data, datatype); | ||
1440 | 4024 | } | ||
1441 | 4025 | } | ||
1442 | 4026 | |||
1443 | 4027 | exports.name = 'matrix'; | ||
1444 | 4028 | exports.factory = factory; | ||
1445 | 4029 | |||
1446 | 4030 | |||
1447 | 4031 | /***/ }, | ||
1448 | 4032 | /* 24 */ | ||
1449 | 4033 | /***/ function(module, exports, __webpack_require__) { | ||
1450 | 4034 | |||
1451 | 4035 | 'use strict'; | ||
1452 | 4036 | |||
1453 | 4037 | var size = __webpack_require__(18).size; | ||
1454 | 4038 | |||
1455 | 4039 | function factory (type, config, load, typed) { | ||
1456 | 4040 | var matrix = load(__webpack_require__(23)); | ||
1457 | 4041 | var subtract = load(__webpack_require__(25)); | ||
1458 | 4042 | var multiply = load(__webpack_require__(40)); | ||
1459 | 4043 | |||
1460 | 4044 | /** | ||
1461 | 4045 | * Calculate the cross product for two vectors in three dimensional space. | ||
1462 | 4046 | * The cross product of `A = [a1, a2, a3]` and `B =[b1, b2, b3]` is defined | ||
1463 | 4047 | * as: | ||
1464 | 4048 | * | ||
1465 | 4049 | * cross(A, B) = [ | ||
1466 | 4050 | * a2 * b3 - a3 * b2, | ||
1467 | 4051 | * a3 * b1 - a1 * b3, | ||
1468 | 4052 | * a1 * b2 - a2 * b1 | ||
1469 | 4053 | * ] | ||
1470 | 4054 | * | ||
1471 | 4055 | * Syntax: | ||
1472 | 4056 | * | ||
1473 | 4057 | * math.cross(x, y) | ||
1474 | 4058 | * | ||
1475 | 4059 | * Examples: | ||
1476 | 4060 | * | ||
1477 | 4061 | * math.cross([1, 1, 0], [0, 1, 1]); // Returns [1, -1, 1] | ||
1478 | 4062 | * math.cross([3, -3, 1], [4, 9, 2]); // Returns [-15, -2, 39] | ||
1479 | 4063 | * math.cross([2, 3, 4], [5, 6, 7]); // Returns [-3, 6, -3] | ||
1480 | 4064 | * | ||
1481 | 4065 | * See also: | ||
1482 | 4066 | * | ||
1483 | 4067 | * dot, multiply | ||
1484 | 4068 | * | ||
1485 | 4069 | * @param {Array | Matrix} x First vector | ||
1486 | 4070 | * @param {Array | Matrix} y Second vector | ||
1487 | 4071 | * @return {Array | Matrix} Returns the cross product of `x` and `y` | ||
1488 | 4072 | */ | ||
1489 | 4073 | var cross = typed('cross', { | ||
1490 | 4074 | 'Matrix, Matrix': function (x, y) { | ||
1491 | 4075 | return matrix(_cross(x.toArray(), y.toArray())); | ||
1492 | 4076 | }, | ||
1493 | 4077 | |||
1494 | 4078 | 'Matrix, Array': function (x, y) { | ||
1495 | 4079 | return matrix(_cross(x.toArray(), y)); | ||
1496 | 4080 | }, | ||
1497 | 4081 | |||
1498 | 4082 | 'Array, Matrix': function (x, y) { | ||
1499 | 4083 | return matrix(_cross(x, y.toArray())); | ||
1500 | 4084 | }, | ||
1501 | 4085 | |||
1502 | 4086 | 'Array, Array': _cross | ||
1503 | 4087 | }); | ||
1504 | 4088 | |||
1505 | 4089 | cross.toTex = '\\left(${args[0]}\\right)\\times\\left(${args[1]}\\right)'; | ||
1506 | 4090 | |||
1507 | 4091 | return cross; | ||
1508 | 4092 | |||
1509 | 4093 | /** | ||
1510 | 4094 | * Calculate the cross product for two arrays | ||
1511 | 4095 | * @param {Array} x First vector | ||
1512 | 4096 | * @param {Array} y Second vector | ||
1513 | 4097 | * @returns {Array} Returns the cross product of x and y | ||
1514 | 4098 | * @private | ||
1515 | 4099 | */ | ||
1516 | 4100 | function _cross(x, y) { | ||
1517 | 4101 | var xSize= size(x); | ||
1518 | 4102 | var ySize = size(y); | ||
1519 | 4103 | |||
1520 | 4104 | if (xSize.length != 1 || ySize.length != 1 || xSize[0] != 3 || ySize[0] != 3) { | ||
1521 | 4105 | throw new RangeError('Vectors with length 3 expected ' + | ||
1522 | 4106 | '(Size A = [' + xSize.join(', ') + '], B = [' + ySize.join(', ') + '])'); | ||
1523 | 4107 | } | ||
1524 | 4108 | |||
1525 | 4109 | return [ | ||
1526 | 4110 | subtract(multiply(x[1], y[2]), multiply(x[2], y[1])), | ||
1527 | 4111 | subtract(multiply(x[2], y[0]), multiply(x[0], y[2])), | ||
1528 | 4112 | subtract(multiply(x[0], y[1]), multiply(x[1], y[0])) | ||
1529 | 4113 | ]; | ||
1530 | 4114 | } | ||
1531 | 4115 | } | ||
1532 | 4116 | |||
1533 | 4117 | exports.name = 'cross'; | ||
1534 | 4118 | exports.factory = factory; | ||
1535 | 4119 | |||
1536 | 4120 | |||
1537 | 4121 | /***/ }, | ||
1538 | 4122 | /* 25 */ | ||
1539 | 4123 | /***/ function(module, exports, __webpack_require__) { | ||
1540 | 4124 | |||
1541 | 4125 | 'use strict'; | ||
1542 | 4126 | |||
1543 | 4127 | var DimensionError = __webpack_require__(22); | ||
1544 | 4128 | |||
1545 | 4129 | function factory (type, config, load, typed) { | ||
1546 | 4130 | var latex = __webpack_require__(26); | ||
1547 | 4131 | |||
1548 | 4132 | var matrix = load(__webpack_require__(23)); | ||
1549 | 4133 | var addScalar = load(__webpack_require__(27)); | ||
1550 | 4134 | var unaryMinus = load(__webpack_require__(28)); | ||
1551 | 4135 | |||
1552 | 4136 | var algorithm01 = load(__webpack_require__(30)); | ||
1553 | 4137 | var algorithm03 = load(__webpack_require__(31)); | ||
1554 | 4138 | var algorithm05 = load(__webpack_require__(32)); | ||
1555 | 4139 | var algorithm10 = load(__webpack_require__(34)); | ||
1556 | 4140 | var algorithm13 = load(__webpack_require__(35)); | ||
1557 | 4141 | var algorithm14 = load(__webpack_require__(39)); | ||
1558 | 4142 | |||
1559 | 4143 | /** | ||
1560 | 4144 | * Subtract two values, `x - y`. | ||
1561 | 4145 | * For matrices, the function is evaluated element wise. | ||
1562 | 4146 | * | ||
1563 | 4147 | * Syntax: | ||
1564 | 4148 | * | ||
1565 | 4149 | * math.subtract(x, y) | ||
1566 | 4150 | * | ||
1567 | 4151 | * Examples: | ||
1568 | 4152 | * | ||
1569 | 4153 | * math.subtract(5.3, 2); // returns number 3.3 | ||
1570 | 4154 | * | ||
1571 | 4155 | * var a = math.complex(2, 3); | ||
1572 | 4156 | * var b = math.complex(4, 1); | ||
1573 | 4157 | * math.subtract(a, b); // returns Complex -2 + 2i | ||
1574 | 4158 | * | ||
1575 | 4159 | * math.subtract([5, 7, 4], 4); // returns Array [1, 3, 0] | ||
1576 | 4160 | * | ||
1577 | 4161 | * var c = math.unit('2.1 km'); | ||
1578 | 4162 | * var d = math.unit('500m'); | ||
1579 | 4163 | * math.subtract(c, d); // returns Unit 1.6 km | ||
1580 | 4164 | * | ||
1581 | 4165 | * See also: | ||
1582 | 4166 | * | ||
1583 | 4167 | * add | ||
1584 | 4168 | * | ||
1585 | 4169 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x | ||
1586 | 4170 | * Initial value | ||
1587 | 4171 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y | ||
1588 | 4172 | * Value to subtract from `x` | ||
1589 | 4173 | * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} | ||
1590 | 4174 | * Subtraction of `x` and `y` | ||
1591 | 4175 | */ | ||
1592 | 4176 | var subtract = typed('subtract', { | ||
1593 | 4177 | |||
1594 | 4178 | 'number, number': function (x, y) { | ||
1595 | 4179 | return x - y; | ||
1596 | 4180 | }, | ||
1597 | 4181 | |||
1598 | 4182 | 'Complex, Complex': function (x, y) { | ||
1599 | 4183 | return new type.Complex ( | ||
1600 | 4184 | x.re - y.re, | ||
1601 | 4185 | x.im - y.im | ||
1602 | 4186 | ); | ||
1603 | 4187 | }, | ||
1604 | 4188 | |||
1605 | 4189 | 'BigNumber, BigNumber': function (x, y) { | ||
1606 | 4190 | return x.minus(y); | ||
1607 | 4191 | }, | ||
1608 | 4192 | |||
1609 | 4193 | 'Fraction, Fraction': function (x, y) { | ||
1610 | 4194 | return x.sub(y); | ||
1611 | 4195 | }, | ||
1612 | 4196 | |||
1613 | 4197 | 'Unit, Unit': function (x, y) { | ||
1614 | 4198 | if (x.value == null) { | ||
1615 | 4199 | throw new Error('Parameter x contains a unit with undefined value'); | ||
1616 | 4200 | } | ||
1617 | 4201 | |||
1618 | 4202 | if (y.value == null) { | ||
1619 | 4203 | throw new Error('Parameter y contains a unit with undefined value'); | ||
1620 | 4204 | } | ||
1621 | 4205 | |||
1622 | 4206 | if (!x.equalBase(y)) { | ||
1623 | 4207 | throw new Error('Units do not match'); | ||
1624 | 4208 | } | ||
1625 | 4209 | |||
1626 | 4210 | var res = x.clone(); | ||
1627 | 4211 | res.value -= y.value; | ||
1628 | 4212 | res.fixPrefix = false; | ||
1629 | 4213 | |||
1630 | 4214 | return res; | ||
1631 | 4215 | }, | ||
1632 | 4216 | |||
1633 | 4217 | 'Matrix, Matrix': function (x, y) { | ||
1634 | 4218 | // matrix sizes | ||
1635 | 4219 | var xsize = x.size(); | ||
1636 | 4220 | var ysize = y.size(); | ||
1637 | 4221 | |||
1638 | 4222 | // check dimensions | ||
1639 | 4223 | if (xsize.length !== ysize.length) | ||
1640 | 4224 | throw new DimensionError(xsize.length, ysize.length); | ||
1641 | 4225 | |||
1642 | 4226 | // result | ||
1643 | 4227 | var c; | ||
1644 | 4228 | |||
1645 | 4229 | // process matrix storage | ||
1646 | 4230 | switch (x.storage()) { | ||
1647 | 4231 | case 'sparse': | ||
1648 | 4232 | switch (y.storage()) { | ||
1649 | 4233 | case 'sparse': | ||
1650 | 4234 | // sparse - sparse | ||
1651 | 4235 | c = algorithm05(x, y, subtract); | ||
1652 | 4236 | break; | ||
1653 | 4237 | default: | ||
1654 | 4238 | // sparse - dense | ||
1655 | 4239 | c = algorithm03(y, x, subtract, true); | ||
1656 | 4240 | break; | ||
1657 | 4241 | } | ||
1658 | 4242 | break; | ||
1659 | 4243 | default: | ||
1660 | 4244 | switch (y.storage()) { | ||
1661 | 4245 | case 'sparse': | ||
1662 | 4246 | // dense - sparse | ||
1663 | 4247 | c = algorithm01(x, y, subtract, false); | ||
1664 | 4248 | break; | ||
1665 | 4249 | default: | ||
1666 | 4250 | // dense - dense | ||
1667 | 4251 | c = algorithm13(x, y, subtract); | ||
1668 | 4252 | break; | ||
1669 | 4253 | } | ||
1670 | 4254 | break; | ||
1671 | 4255 | } | ||
1672 | 4256 | return c; | ||
1673 | 4257 | }, | ||
1674 | 4258 | |||
1675 | 4259 | 'Array, Array': function (x, y) { | ||
1676 | 4260 | // use matrix implementation | ||
1677 | 4261 | return subtract(matrix(x), matrix(y)).valueOf(); | ||
1678 | 4262 | }, | ||
1679 | 4263 | |||
1680 | 4264 | 'Array, Matrix': function (x, y) { | ||
1681 | 4265 | // use matrix implementation | ||
1682 | 4266 | return subtract(matrix(x), y); | ||
1683 | 4267 | }, | ||
1684 | 4268 | |||
1685 | 4269 | 'Matrix, Array': function (x, y) { | ||
1686 | 4270 | // use matrix implementation | ||
1687 | 4271 | return subtract(x, matrix(y)); | ||
1688 | 4272 | }, | ||
1689 | 4273 | |||
1690 | 4274 | 'Matrix, any': function (x, y) { | ||
1691 | 4275 | // result | ||
1692 | 4276 | var c; | ||
1693 | 4277 | // check storage format | ||
1694 | 4278 | switch (x.storage()) { | ||
1695 | 4279 | case 'sparse': | ||
1696 | 4280 | // algorithm 7 is faster than 9 since it calls f() for nonzero items only! | ||
1697 | 4281 | c = algorithm10(x, unaryMinus(y), addScalar); | ||
1698 | 4282 | break; | ||
1699 | 4283 | default: | ||
1700 | 4284 | c = algorithm14(x, y, subtract); | ||
1701 | 4285 | break; | ||
1702 | 4286 | } | ||
1703 | 4287 | return c; | ||
1704 | 4288 | }, | ||
1705 | 4289 | |||
1706 | 4290 | 'any, Matrix': function (x, y) { | ||
1707 | 4291 | // result | ||
1708 | 4292 | var c; | ||
1709 | 4293 | // check storage format | ||
1710 | 4294 | switch (y.storage()) { | ||
1711 | 4295 | case 'sparse': | ||
1712 | 4296 | c = algorithm10(y, x, subtract, true); | ||
1713 | 4297 | break; | ||
1714 | 4298 | default: | ||
1715 | 4299 | c = algorithm14(y, x, subtract, true); | ||
1716 | 4300 | break; | ||
1717 | 4301 | } | ||
1718 | 4302 | return c; | ||
1719 | 4303 | }, | ||
1720 | 4304 | |||
1721 | 4305 | 'Array, any': function (x, y) { | ||
1722 | 4306 | // use matrix implementation | ||
1723 | 4307 | return algorithm14(matrix(x), y, subtract, false).valueOf(); | ||
1724 | 4308 | }, | ||
1725 | 4309 | |||
1726 | 4310 | 'any, Array': function (x, y) { | ||
1727 | 4311 | // use matrix implementation | ||
1728 | 4312 | return algorithm14(matrix(y), x, subtract, true).valueOf(); | ||
1729 | 4313 | } | ||
1730 | 4314 | }); | ||
1731 | 4315 | |||
1732 | 4316 | subtract.toTex = '\\left(${args[0]}' + latex.operators['subtract'] + '${args[1]}\\right)'; | ||
1733 | 4317 | |||
1734 | 4318 | return subtract; | ||
1735 | 4319 | } | ||
1736 | 4320 | |||
1737 | 4321 | exports.name = 'subtract'; | ||
1738 | 4322 | exports.factory = factory; | ||
1739 | 4323 | |||
1740 | 4324 | |||
1741 | 4325 | /***/ }, | ||
1742 | 4326 | /* 26 */ | ||
1743 | 4327 | /***/ function(module, exports) { | ||
1744 | 4328 | |||
1745 | 4329 | 'use strict'; | ||
1746 | 4330 | |||
1747 | 4331 | exports.symbols = { | ||
1748 | 4332 | // GREEK LETTERS | ||
1749 | 4333 | Alpha: 'A', alpha: '\\alpha', | ||
1750 | 4334 | Beta: 'B', beta: '\\beta', | ||
1751 | 4335 | Gamma: '\\Gamma', gamma: '\\gamma', | ||
1752 | 4336 | Delta: '\\Delta', delta: '\\delta', | ||
1753 | 4337 | Epsilon: 'E', epsilon: '\\epsilon', varepsilon: '\\varepsilon', | ||
1754 | 4338 | Zeta: 'Z', zeta: '\\zeta', | ||
1755 | 4339 | Eta: 'H', eta: '\\eta', | ||
1756 | 4340 | Theta: '\\Theta', theta: '\\theta', vartheta: '\\vartheta', | ||
1757 | 4341 | Iota: 'I', iota: '\\iota', | ||
1758 | 4342 | Kappa: 'K', kappa: '\\kappa', varkappa: '\\varkappa', | ||
1759 | 4343 | Lambda: '\\Lambda', lambda: '\\lambda', | ||
1760 | 4344 | Mu: 'M', mu: '\\mu', | ||
1761 | 4345 | Nu: 'N', nu: '\\nu', | ||
1762 | 4346 | Xi: '\\Xi', xi: '\\xi', | ||
1763 | 4347 | Omicron: 'O', omicron: 'o', | ||
1764 | 4348 | Pi: '\\Pi', pi: '\\pi', varpi: '\\varpi', | ||
1765 | 4349 | Rho: 'P', rho: '\\rho', varrho: '\\varrho', | ||
1766 | 4350 | Sigma: '\\Sigma', sigma: '\\sigma', varsigma: '\\varsigma', | ||
1767 | 4351 | Tau: 'T', tau: '\\tau', | ||
1768 | 4352 | Upsilon: '\\Upsilon', upsilon: '\\upsilon', | ||
1769 | 4353 | Phi: '\\Phi', phi: '\\phi', varphi: '\\varphi', | ||
1770 | 4354 | Chi: 'X', chi: '\\chi', | ||
1771 | 4355 | Psi: '\\Psi', psi: '\\psi', | ||
1772 | 4356 | Omega: '\\Omega', omega: '\\omega', | ||
1773 | 4357 | //logic | ||
1774 | 4358 | 'true': '\\mathrm{True}', | ||
1775 | 4359 | 'false': '\\mathrm{False}', | ||
1776 | 4360 | //other | ||
1777 | 4361 | i: 'i', //TODO use \i ?? | ||
1778 | 4362 | inf: '\\infty', | ||
1779 | 4363 | Inf: '\\infty', | ||
1780 | 4364 | infinity: '\\infty', | ||
1781 | 4365 | Infinity: '\\infty', | ||
1782 | 4366 | oo: '\\infty', | ||
1783 | 4367 | lim: '\\lim', | ||
1784 | 4368 | 'undefined': '\\mathbf{?}' | ||
1785 | 4369 | }; | ||
1786 | 4370 | |||
1787 | 4371 | exports.operators = { | ||
1788 | 4372 | 'transpose': '^\\top', | ||
1789 | 4373 | 'factorial': '!', | ||
1790 | 4374 | 'pow': '^', | ||
1791 | 4375 | 'dotPow': '.^\\wedge', //TODO find ideal solution | ||
1792 | 4376 | 'unaryPlus': '+', | ||
1793 | 4377 | 'unaryMinus': '-', | ||
1794 | 4378 | 'bitNot': '~', //TODO find ideal solution | ||
1795 | 4379 | 'not': '\\neg', | ||
1796 | 4380 | 'multiply': '\\cdot', | ||
1797 | 4381 | 'divide': '\\frac', //TODO how to handle that properly? | ||
1798 | 4382 | 'dotMultiply': '.\\cdot', //TODO find ideal solution | ||
1799 | 4383 | 'dotDivide': '.:', //TODO find ideal solution | ||
1800 | 4384 | 'mod': '\\mod', | ||
1801 | 4385 | 'add': '+', | ||
1802 | 4386 | 'subtract': '-', | ||
1803 | 4387 | 'to': '\\rightarrow', | ||
1804 | 4388 | 'leftShift': '<<', | ||
1805 | 4389 | 'rightArithShift': '>>', | ||
1806 | 4390 | 'rightLogShift': '>>>', | ||
1807 | 4391 | 'equal': '=', | ||
1808 | 4392 | 'unequal': '\\neq', | ||
1809 | 4393 | 'smaller': '<', | ||
1810 | 4394 | 'larger': '>', | ||
1811 | 4395 | 'smallerEq': '\\leq', | ||
1812 | 4396 | 'largerEq': '\\geq', | ||
1813 | 4397 | 'bitAnd': '\\&', | ||
1814 | 4398 | 'bitXor': '\\underline{|}', | ||
1815 | 4399 | 'bitOr': '|', | ||
1816 | 4400 | 'and': '\\wedge', | ||
1817 | 4401 | 'xor': '\\veebar', | ||
1818 | 4402 | 'or': '\\vee' | ||
1819 | 4403 | }; | ||
1820 | 4404 | |||
1821 | 4405 | exports.defaultTemplate = '\\mathrm{${name}}\\left(${args}\\right)'; | ||
1822 | 4406 | |||
1823 | 4407 | var units = { | ||
1824 | 4408 | deg: '^\\circ' | ||
1825 | 4409 | }; | ||
1826 | 4410 | |||
1827 | 4411 | //@param {string} name | ||
1828 | 4412 | //@param {boolean} isUnit | ||
1829 | 4413 | exports.toSymbol = function (name, isUnit) { | ||
1830 | 4414 | isUnit = typeof isUnit === 'undefined' ? false : isUnit; | ||
1831 | 4415 | if (isUnit) { | ||
1832 | 4416 | if (units.hasOwnProperty(name)) { | ||
1833 | 4417 | return units[name]; | ||
1834 | 4418 | } | ||
1835 | 4419 | return '\\mathrm{' + name + '}'; | ||
1836 | 4420 | } | ||
1837 | 4421 | |||
1838 | 4422 | if (exports.symbols.hasOwnProperty(name)) { | ||
1839 | 4423 | return exports.symbols[name]; | ||
1840 | 4424 | } | ||
1841 | 4425 | else if (name.indexOf('_') !== -1) { | ||
1842 | 4426 | //symbol with index (eg. alpha_1) | ||
1843 | 4427 | var index = name.indexOf('_'); | ||
1844 | 4428 | return exports.toSymbol(name.substring(0, index)) + '_{' | ||
1845 | 4429 | + exports.toSymbol(name.substring(index + 1)) + '}'; | ||
1846 | 4430 | } | ||
1847 | 4431 | return name; | ||
1848 | 4432 | }; | ||
1849 | 4433 | |||
1850 | 4434 | |||
1851 | 4435 | /***/ }, | ||
1852 | 4436 | /* 27 */ | ||
1853 | 4437 | /***/ function(module, exports) { | ||
1854 | 4438 | |||
1855 | 4439 | 'use strict'; | ||
1856 | 4440 | |||
1857 | 4441 | function factory(type, config, load, typed) { | ||
1858 | 4442 | |||
1859 | 4443 | /** | ||
1860 | 4444 | * Add two scalar values, `x + y`. | ||
1861 | 4445 | * This function is meant for internal use: it is used by the public function | ||
1862 | 4446 | * `add` | ||
1863 | 4447 | * | ||
1864 | 4448 | * This function does not support collections (Array or Matrix), and does | ||
1865 | 4449 | * not validate the number of of inputs. | ||
1866 | 4450 | * | ||
1867 | 4451 | * @param {number | BigNumber | Fraction | Complex | Unit} x First value to add | ||
1868 | 4452 | * @param {number | BigNumber | Fraction | Complex} y Second value to add | ||
1869 | 4453 | * @return {number | BigNumber | Fraction | Complex | Unit} Sum of `x` and `y` | ||
1870 | 4454 | * @private | ||
1871 | 4455 | */ | ||
1872 | 4456 | return typed('add', { | ||
1873 | 4457 | |||
1874 | 4458 | 'number, number': function (x, y) { | ||
1875 | 4459 | return x + y; | ||
1876 | 4460 | }, | ||
1877 | 4461 | |||
1878 | 4462 | 'Complex, Complex': function (x, y) { | ||
1879 | 4463 | return new type.Complex( | ||
1880 | 4464 | x.re + y.re, | ||
1881 | 4465 | x.im + y.im | ||
1882 | 4466 | ); | ||
1883 | 4467 | }, | ||
1884 | 4468 | |||
1885 | 4469 | 'BigNumber, BigNumber': function (x, y) { | ||
1886 | 4470 | return x.plus(y); | ||
1887 | 4471 | }, | ||
1888 | 4472 | |||
1889 | 4473 | 'Fraction, Fraction': function (x, y) { | ||
1890 | 4474 | return x.add(y); | ||
1891 | 4475 | }, | ||
1892 | 4476 | |||
1893 | 4477 | 'Unit, Unit': function (x, y) { | ||
1894 | 4478 | if (x.value == null) throw new Error('Parameter x contains a unit with undefined value'); | ||
1895 | 4479 | if (y.value == null) throw new Error('Parameter y contains a unit with undefined value'); | ||
1896 | 4480 | if (!x.equalBase(y)) throw new Error('Units do not match'); | ||
1897 | 4481 | |||
1898 | 4482 | var res = x.clone(); | ||
1899 | 4483 | res.value += y.value; | ||
1900 | 4484 | res.fixPrefix = false; | ||
1901 | 4485 | return res; | ||
1902 | 4486 | } | ||
1903 | 4487 | }); | ||
1904 | 4488 | } | ||
1905 | 4489 | |||
1906 | 4490 | exports.factory = factory; | ||
1907 | 4491 | |||
1908 | 4492 | |||
1909 | 4493 | /***/ }, | ||
1910 | 4494 | /* 28 */ | ||
1911 | 4495 | /***/ function(module, exports, __webpack_require__) { | ||
1912 | 4496 | |||
1913 | 4497 | 'use strict'; | ||
1914 | 4498 | |||
1915 | 4499 | var deepMap = __webpack_require__(29); | ||
1916 | 4500 | |||
1917 | 4501 | function factory (type, config, load, typed) { | ||
1918 | 4502 | var latex = __webpack_require__(26); | ||
1919 | 4503 | |||
1920 | 4504 | /** | ||
1921 | 4505 | * Inverse the sign of a value, apply a unary minus operation. | ||
1922 | 4506 | * | ||
1923 | 4507 | * For matrices, the function is evaluated element wise. Boolean values and | ||
1924 | 4508 | * strings will be converted to a number. For complex numbers, both real and | ||
1925 | 4509 | * complex value are inverted. | ||
1926 | 4510 | * | ||
1927 | 4511 | * Syntax: | ||
1928 | 4512 | * | ||
1929 | 4513 | * math.unaryMinus(x) | ||
1930 | 4514 | * | ||
1931 | 4515 | * Examples: | ||
1932 | 4516 | * | ||
1933 | 4517 | * math.unaryMinus(3.5); // returns -3.5 | ||
1934 | 4518 | * math.unaryMinus(-4.2); // returns 4.2 | ||
1935 | 4519 | * | ||
1936 | 4520 | * See also: | ||
1937 | 4521 | * | ||
1938 | 4522 | * add, subtract, unaryPlus | ||
1939 | 4523 | * | ||
1940 | 4524 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Number to be inverted. | ||
1941 | 4525 | * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Returns the value with inverted sign. | ||
1942 | 4526 | */ | ||
1943 | 4527 | var unaryMinus = typed('unaryMinus', { | ||
1944 | 4528 | 'number': function (x) { | ||
1945 | 4529 | return -x; | ||
1946 | 4530 | }, | ||
1947 | 4531 | |||
1948 | 4532 | 'Complex': function (x) { | ||
1949 | 4533 | return new type.Complex(-x.re, -x.im); | ||
1950 | 4534 | }, | ||
1951 | 4535 | |||
1952 | 4536 | 'BigNumber': function (x) { | ||
1953 | 4537 | return x.neg(); | ||
1954 | 4538 | }, | ||
1955 | 4539 | |||
1956 | 4540 | 'Fraction': function (x) { | ||
1957 | 4541 | var tmp = x.clone(); | ||
1958 | 4542 | tmp.s = -tmp.s; | ||
1959 | 4543 | return tmp; | ||
1960 | 4544 | }, | ||
1961 | 4545 | |||
1962 | 4546 | 'Unit': function (x) { | ||
1963 | 4547 | var res = x.clone(); | ||
1964 | 4548 | res.value = -x.value; | ||
1965 | 4549 | return res; | ||
1966 | 4550 | }, | ||
1967 | 4551 | |||
1968 | 4552 | 'Array | Matrix': function (x) { | ||
1969 | 4553 | // deep map collection, skip zeros since unaryMinus(0) = 0 | ||
1970 | 4554 | return deepMap(x, unaryMinus, true); | ||
1971 | 4555 | } | ||
1972 | 4556 | |||
1973 | 4557 | // TODO: add support for string | ||
1974 | 4558 | }); | ||
1975 | 4559 | |||
1976 | 4560 | unaryMinus.toTex = latex.operators['unaryMinus'] + '\\left(${args[0]}\\right)'; | ||
1977 | 4561 | |||
1978 | 4562 | return unaryMinus; | ||
1979 | 4563 | } | ||
1980 | 4564 | |||
1981 | 4565 | exports.name = 'unaryMinus'; | ||
1982 | 4566 | exports.factory = factory; | ||
1983 | 4567 | |||
1984 | 4568 | |||
1985 | 4569 | /***/ }, | ||
1986 | 4570 | /* 29 */ | ||
1987 | 4571 | /***/ function(module, exports) { | ||
1988 | 4572 | |||
1989 | 4573 | 'use strict'; | ||
1990 | 4574 | |||
1991 | 4575 | /** | ||
1992 | 4576 | * Execute the callback function element wise for each element in array and any | ||
1993 | 4577 | * nested array | ||
1994 | 4578 | * Returns an array with the results | ||
1995 | 4579 | * @param {Array | Matrix} array | ||
1996 | 4580 | * @param {Function} callback The callback is called with two parameters: | ||
1997 | 4581 | * value1 and value2, which contain the current | ||
1998 | 4582 | * element of both arrays. | ||
1999 | 4583 | * @param {boolean} [skipZeros] Invoke callback function for non-zero values only. | ||
2000 | 4584 | * | ||
2001 | 4585 | * @return {Array | Matrix} res | ||
2002 | 4586 | */ | ||
2003 | 4587 | module.exports = function deepMap(array, callback, skipZeros) { | ||
2004 | 4588 | if (array && (typeof array.map === 'function')) { | ||
2005 | 4589 | // TODO: replace array.map with a for loop to improve performance | ||
2006 | 4590 | return array.map(function (x) { | ||
2007 | 4591 | return deepMap(x, callback, skipZeros); | ||
2008 | 4592 | }); | ||
2009 | 4593 | } | ||
2010 | 4594 | else { | ||
2011 | 4595 | return callback(array); | ||
2012 | 4596 | } | ||
2013 | 4597 | }; | ||
2014 | 4598 | |||
2015 | 4599 | |||
2016 | 4600 | /***/ }, | ||
2017 | 4601 | /* 30 */ | ||
2018 | 4602 | /***/ function(module, exports, __webpack_require__) { | ||
2019 | 4603 | |||
2020 | 4604 | 'use strict'; | ||
2021 | 4605 | |||
2022 | 4606 | var DimensionError = __webpack_require__(22); | ||
2023 | 4607 | |||
2024 | 4608 | function factory (type, config, load, typed) { | ||
2025 | 4609 | |||
2026 | 4610 | var DenseMatrix = type.DenseMatrix; | ||
2027 | 4611 | |||
2028 | 4612 | /** | ||
2029 | 4613 | * Iterates over SparseMatrix nonzero items and invokes the callback function f(Dij, Sij). | ||
2030 | 4614 | * Callback function invoked NNZ times (number of nonzero items in SparseMatrix). | ||
2031 | 4615 | * | ||
2032 | 4616 | * | ||
2033 | 4617 | * ┌ f(Dij, Sij) ; S(i,j) !== 0 | ||
2034 | 4618 | * C(i,j) = ┤ | ||
2035 | 4619 | * â”” Dij ; otherwise | ||
2036 | 4620 | * | ||
2037 | 4621 | * | ||
2038 | 4622 | * @param {Matrix} denseMatrix The DenseMatrix instance (D) | ||
2039 | 4623 | * @param {Matrix} sparseMatrix The SparseMatrix instance (S) | ||
2040 | 4624 | * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) | ||
2041 | 4625 | * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) | ||
2042 | 4626 | * | ||
2043 | 4627 | * @return {Matrix} DenseMatrix (C) | ||
2044 | 4628 | * | ||
2045 | 4629 | * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 | ||
2046 | 4630 | */ | ||
2047 | 4631 | var algorithm01 = function (denseMatrix, sparseMatrix, callback, inverse) { | ||
2048 | 4632 | // dense matrix arrays | ||
2049 | 4633 | var adata = denseMatrix._data; | ||
2050 | 4634 | var asize = denseMatrix._size; | ||
2051 | 4635 | var adt = denseMatrix._datatype; | ||
2052 | 4636 | // sparse matrix arrays | ||
2053 | 4637 | var bvalues = sparseMatrix._values; | ||
2054 | 4638 | var bindex = sparseMatrix._index; | ||
2055 | 4639 | var bptr = sparseMatrix._ptr; | ||
2056 | 4640 | var bsize = sparseMatrix._size; | ||
2057 | 4641 | var bdt = sparseMatrix._datatype; | ||
2058 | 4642 | |||
2059 | 4643 | // validate dimensions | ||
2060 | 4644 | if (asize.length !== bsize.length) | ||
2061 | 4645 | throw new DimensionError(asize.length, bsize.length); | ||
2062 | 4646 | |||
2063 | 4647 | // check rows & columns | ||
2064 | 4648 | if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) | ||
2065 | 4649 | throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); | ||
2066 | 4650 | |||
2067 | 4651 | // sparse matrix cannot be a Pattern matrix | ||
2068 | 4652 | if (!bvalues) | ||
2069 | 4653 | throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); | ||
2070 | 4654 | |||
2071 | 4655 | // rows & columns | ||
2072 | 4656 | var rows = asize[0]; | ||
2073 | 4657 | var columns = asize[1]; | ||
2074 | 4658 | |||
2075 | 4659 | // process data types | ||
2076 | 4660 | var dt = typeof adt === 'string' && adt === bdt ? adt : undefined; | ||
2077 | 4661 | // callback function | ||
2078 | 4662 | var cf = dt ? typed.find(callback, [dt, dt]) : callback; | ||
2079 | 4663 | |||
2080 | 4664 | // vars | ||
2081 | 4665 | var i, j; | ||
2082 | 4666 | |||
2083 | 4667 | // result (DenseMatrix) | ||
2084 | 4668 | var cdata = []; | ||
2085 | 4669 | // initialize c | ||
2086 | 4670 | for (i = 0; i < rows; i++) | ||
2087 | 4671 | cdata[i] = []; | ||
2088 | 4672 | |||
2089 | 4673 | // workspace | ||
2090 | 4674 | var x = []; | ||
2091 | 4675 | // marks indicating we have a value in x for a given column | ||
2092 | 4676 | var w = []; | ||
2093 | 4677 | |||
2094 | 4678 | // loop columns in b | ||
2095 | 4679 | for (j = 0; j < columns; j++) { | ||
2096 | 4680 | // column mark | ||
2097 | 4681 | var mark = j + 1; | ||
2098 | 4682 | // values in column j | ||
2099 | 4683 | for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { | ||
2100 | 4684 | // row | ||
2101 | 4685 | i = bindex[k]; | ||
2102 | 4686 | // update workspace | ||
2103 | 4687 | x[i] = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); | ||
2104 | 4688 | // mark i as updated | ||
2105 | 4689 | w[i] = mark; | ||
2106 | 4690 | } | ||
2107 | 4691 | // loop rows | ||
2108 | 4692 | for (i = 0; i < rows; i++) { | ||
2109 | 4693 | // check row is in workspace | ||
2110 | 4694 | if (w[i] === mark) { | ||
2111 | 4695 | // c[i][j] was already calculated | ||
2112 | 4696 | cdata[i][j] = x[i]; | ||
2113 | 4697 | } | ||
2114 | 4698 | else { | ||
2115 | 4699 | // item does not exist in S | ||
2116 | 4700 | cdata[i][j] = adata[i][j]; | ||
2117 | 4701 | } | ||
2118 | 4702 | } | ||
2119 | 4703 | } | ||
2120 | 4704 | |||
2121 | 4705 | // return dense matrix | ||
2122 | 4706 | return new DenseMatrix({ | ||
2123 | 4707 | data: cdata, | ||
2124 | 4708 | size: [rows, columns], | ||
2125 | 4709 | datatype: dt | ||
2126 | 4710 | }); | ||
2127 | 4711 | }; | ||
2128 | 4712 | |||
2129 | 4713 | return algorithm01; | ||
2130 | 4714 | } | ||
2131 | 4715 | |||
2132 | 4716 | exports.name = 'algorithm01'; | ||
2133 | 4717 | exports.factory = factory; | ||
2134 | 4718 | |||
2135 | 4719 | |||
2136 | 4720 | /***/ }, | ||
2137 | 4721 | /* 31 */ | ||
2138 | 4722 | /***/ function(module, exports, __webpack_require__) { | ||
2139 | 4723 | |||
2140 | 4724 | 'use strict'; | ||
2141 | 4725 | |||
2142 | 4726 | var DimensionError = __webpack_require__(22); | ||
2143 | 4727 | |||
2144 | 4728 | function factory (type, config, load, typed) { | ||
2145 | 4729 | |||
2146 | 4730 | var DenseMatrix = type.DenseMatrix; | ||
2147 | 4731 | |||
2148 | 4732 | /** | ||
2149 | 4733 | * Iterates over SparseMatrix items and invokes the callback function f(Dij, Sij). | ||
2150 | 4734 | * Callback function invoked M*N times. | ||
2151 | 4735 | * | ||
2152 | 4736 | * | ||
2153 | 4737 | * ┌ f(Dij, Sij) ; S(i,j) !== 0 | ||
2154 | 4738 | * C(i,j) = ┤ | ||
2155 | 4739 | * â”” f(Dij, 0) ; otherwise | ||
2156 | 4740 | * | ||
2157 | 4741 | * | ||
2158 | 4742 | * @param {Matrix} denseMatrix The DenseMatrix instance (D) | ||
2159 | 4743 | * @param {Matrix} sparseMatrix The SparseMatrix instance (C) | ||
2160 | 4744 | * @param {Function} callback The f(Dij,Sij) operation to invoke, where Dij = DenseMatrix(i,j) and Sij = SparseMatrix(i,j) | ||
2161 | 4745 | * @param {boolean} inverse A true value indicates callback should be invoked f(Sij,Dij) | ||
2162 | 4746 | * | ||
2163 | 4747 | * @return {Matrix} DenseMatrix (C) | ||
2164 | 4748 | * | ||
2165 | 4749 | * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97477571 | ||
2166 | 4750 | */ | ||
2167 | 4751 | var algorithm03 = function (denseMatrix, sparseMatrix, callback, inverse) { | ||
2168 | 4752 | // dense matrix arrays | ||
2169 | 4753 | var adata = denseMatrix._data; | ||
2170 | 4754 | var asize = denseMatrix._size; | ||
2171 | 4755 | var adt = denseMatrix._datatype; | ||
2172 | 4756 | // sparse matrix arrays | ||
2173 | 4757 | var bvalues = sparseMatrix._values; | ||
2174 | 4758 | var bindex = sparseMatrix._index; | ||
2175 | 4759 | var bptr = sparseMatrix._ptr; | ||
2176 | 4760 | var bsize = sparseMatrix._size; | ||
2177 | 4761 | var bdt = sparseMatrix._datatype; | ||
2178 | 4762 | |||
2179 | 4763 | // validate dimensions | ||
2180 | 4764 | if (asize.length !== bsize.length) | ||
2181 | 4765 | throw new DimensionError(asize.length, bsize.length); | ||
2182 | 4766 | |||
2183 | 4767 | // check rows & columns | ||
2184 | 4768 | if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) | ||
2185 | 4769 | throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); | ||
2186 | 4770 | |||
2187 | 4771 | // sparse matrix cannot be a Pattern matrix | ||
2188 | 4772 | if (!bvalues) | ||
2189 | 4773 | throw new Error('Cannot perform operation on Dense Matrix and Pattern Sparse Matrix'); | ||
2190 | 4774 | |||
2191 | 4775 | // rows & columns | ||
2192 | 4776 | var rows = asize[0]; | ||
2193 | 4777 | var columns = asize[1]; | ||
2194 | 4778 | |||
2195 | 4779 | // datatype | ||
2196 | 4780 | var dt; | ||
2197 | 4781 | // zero value | ||
2198 | 4782 | var zero = 0; | ||
2199 | 4783 | // callback signature to use | ||
2200 | 4784 | var cf = callback; | ||
2201 | 4785 | |||
2202 | 4786 | // process data types | ||
2203 | 4787 | if (typeof adt === 'string' && adt === bdt) { | ||
2204 | 4788 | // datatype | ||
2205 | 4789 | dt = adt; | ||
2206 | 4790 | // convert 0 to the same datatype | ||
2207 | 4791 | zero = typed.convert(0, dt); | ||
2208 | 4792 | // callback | ||
2209 | 4793 | cf = typed.find(callback, [dt, dt]); | ||
2210 | 4794 | } | ||
2211 | 4795 | |||
2212 | 4796 | // result (DenseMatrix) | ||
2213 | 4797 | var cdata = []; | ||
2214 | 4798 | |||
2215 | 4799 | // initialize dense matrix | ||
2216 | 4800 | for (var z = 0; z < rows; z++) { | ||
2217 | 4801 | // initialize row | ||
2218 | 4802 | cdata[z] = []; | ||
2219 | 4803 | } | ||
2220 | 4804 | |||
2221 | 4805 | // workspace | ||
2222 | 4806 | var x = []; | ||
2223 | 4807 | // marks indicating we have a value in x for a given column | ||
2224 | 4808 | var w = []; | ||
2225 | 4809 | |||
2226 | 4810 | // loop columns in b | ||
2227 | 4811 | for (var j = 0; j < columns; j++) { | ||
2228 | 4812 | // column mark | ||
2229 | 4813 | var mark = j + 1; | ||
2230 | 4814 | // values in column j | ||
2231 | 4815 | for (var k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { | ||
2232 | 4816 | // row | ||
2233 | 4817 | var i = bindex[k]; | ||
2234 | 4818 | // update workspace | ||
2235 | 4819 | x[i] = inverse ? cf(bvalues[k], adata[i][j]) : cf(adata[i][j], bvalues[k]); | ||
2236 | 4820 | w[i] = mark; | ||
2237 | 4821 | } | ||
2238 | 4822 | // process workspace | ||
2239 | 4823 | for (var y = 0; y < rows; y++) { | ||
2240 | 4824 | // check we have a calculated value for current row | ||
2241 | 4825 | if (w[y] === mark) { | ||
2242 | 4826 | // use calculated value | ||
2243 | 4827 | cdata[y][j] = x[y]; | ||
2244 | 4828 | } | ||
2245 | 4829 | else { | ||
2246 | 4830 | // calculate value | ||
2247 | 4831 | cdata[y][j] = inverse ? cf(zero, adata[y][j]) : cf(adata[y][j], zero); | ||
2248 | 4832 | } | ||
2249 | 4833 | } | ||
2250 | 4834 | } | ||
2251 | 4835 | |||
2252 | 4836 | // return dense matrix | ||
2253 | 4837 | return new DenseMatrix({ | ||
2254 | 4838 | data: cdata, | ||
2255 | 4839 | size: [rows, columns], | ||
2256 | 4840 | datatype: dt | ||
2257 | 4841 | }); | ||
2258 | 4842 | }; | ||
2259 | 4843 | |||
2260 | 4844 | return algorithm03; | ||
2261 | 4845 | } | ||
2262 | 4846 | |||
2263 | 4847 | exports.name = 'algorithm03'; | ||
2264 | 4848 | exports.factory = factory; | ||
2265 | 4849 | |||
2266 | 4850 | |||
2267 | 4851 | /***/ }, | ||
2268 | 4852 | /* 32 */ | ||
2269 | 4853 | /***/ function(module, exports, __webpack_require__) { | ||
2270 | 4854 | |||
2271 | 4855 | 'use strict'; | ||
2272 | 4856 | |||
2273 | 4857 | var DimensionError = __webpack_require__(22); | ||
2274 | 4858 | |||
2275 | 4859 | function factory (type, config, load, typed) { | ||
2276 | 4860 | |||
2277 | 4861 | var equalScalar = load(__webpack_require__(33)); | ||
2278 | 4862 | |||
2279 | 4863 | var SparseMatrix = type.SparseMatrix; | ||
2280 | 4864 | |||
2281 | 4865 | /** | ||
2282 | 4866 | * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). | ||
2283 | 4867 | * Callback function invoked MAX(NNZA, NNZB) times | ||
2284 | 4868 | * | ||
2285 | 4869 | * | ||
2286 | 4870 | * ┌ f(Aij, Bij) ; A(i,j) !== 0 || B(i,j) !== 0 | ||
2287 | 4871 | * C(i,j) = ┤ | ||
2288 | 4872 | * â”” 0 ; otherwise | ||
2289 | 4873 | * | ||
2290 | 4874 | * | ||
2291 | 4875 | * @param {Matrix} a The SparseMatrix instance (A) | ||
2292 | 4876 | * @param {Matrix} b The SparseMatrix instance (B) | ||
2293 | 4877 | * @param {Function} callback The f(Aij,Bij) operation to invoke | ||
2294 | 4878 | * | ||
2295 | 4879 | * @return {Matrix} SparseMatrix (C) | ||
2296 | 4880 | * | ||
2297 | 4881 | * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 | ||
2298 | 4882 | */ | ||
2299 | 4883 | var algorithm05 = function (a, b, callback) { | ||
2300 | 4884 | // sparse matrix arrays | ||
2301 | 4885 | var avalues = a._values; | ||
2302 | 4886 | var aindex = a._index; | ||
2303 | 4887 | var aptr = a._ptr; | ||
2304 | 4888 | var asize = a._size; | ||
2305 | 4889 | var adt = a._datatype; | ||
2306 | 4890 | // sparse matrix arrays | ||
2307 | 4891 | var bvalues = b._values; | ||
2308 | 4892 | var bindex = b._index; | ||
2309 | 4893 | var bptr = b._ptr; | ||
2310 | 4894 | var bsize = b._size; | ||
2311 | 4895 | var bdt = b._datatype; | ||
2312 | 4896 | |||
2313 | 4897 | // validate dimensions | ||
2314 | 4898 | if (asize.length !== bsize.length) | ||
2315 | 4899 | throw new DimensionError(asize.length, bsize.length); | ||
2316 | 4900 | |||
2317 | 4901 | // check rows & columns | ||
2318 | 4902 | if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) | ||
2319 | 4903 | throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); | ||
2320 | 4904 | |||
2321 | 4905 | // rows & columns | ||
2322 | 4906 | var rows = asize[0]; | ||
2323 | 4907 | var columns = asize[1]; | ||
2324 | 4908 | |||
2325 | 4909 | // datatype | ||
2326 | 4910 | var dt; | ||
2327 | 4911 | // equal signature to use | ||
2328 | 4912 | var eq = equalScalar; | ||
2329 | 4913 | // zero value | ||
2330 | 4914 | var zero = 0; | ||
2331 | 4915 | // callback signature to use | ||
2332 | 4916 | var cf = callback; | ||
2333 | 4917 | |||
2334 | 4918 | // process data types | ||
2335 | 4919 | if (typeof adt === 'string' && adt === bdt) { | ||
2336 | 4920 | // datatype | ||
2337 | 4921 | dt = adt; | ||
2338 | 4922 | // find signature that matches (dt, dt) | ||
2339 | 4923 | eq = typed.find(equalScalar, [dt, dt]); | ||
2340 | 4924 | // convert 0 to the same datatype | ||
2341 | 4925 | zero = typed.convert(0, dt); | ||
2342 | 4926 | // callback | ||
2343 | 4927 | cf = typed.find(callback, [dt, dt]); | ||
2344 | 4928 | } | ||
2345 | 4929 | |||
2346 | 4930 | // result arrays | ||
2347 | 4931 | var cvalues = avalues && bvalues ? [] : undefined; | ||
2348 | 4932 | var cindex = []; | ||
2349 | 4933 | var cptr = []; | ||
2350 | 4934 | // matrix | ||
2351 | 4935 | var c = new SparseMatrix({ | ||
2352 | 4936 | values: cvalues, | ||
2353 | 4937 | index: cindex, | ||
2354 | 4938 | ptr: cptr, | ||
2355 | 4939 | size: [rows, columns], | ||
2356 | 4940 | datatype: dt | ||
2357 | 4941 | }); | ||
2358 | 4942 | |||
2359 | 4943 | // workspaces | ||
2360 | 4944 | var xa = cvalues ? [] : undefined; | ||
2361 | 4945 | var xb = cvalues ? [] : undefined; | ||
2362 | 4946 | // marks indicating we have a value in x for a given column | ||
2363 | 4947 | var wa = []; | ||
2364 | 4948 | var wb = []; | ||
2365 | 4949 | |||
2366 | 4950 | // vars | ||
2367 | 4951 | var i, j, k, k1; | ||
2368 | 4952 | |||
2369 | 4953 | // loop columns | ||
2370 | 4954 | for (j = 0; j < columns; j++) { | ||
2371 | 4955 | // update cptr | ||
2372 | 4956 | cptr[j] = cindex.length; | ||
2373 | 4957 | // columns mark | ||
2374 | 4958 | var mark = j + 1; | ||
2375 | 4959 | // loop values A(:,j) | ||
2376 | 4960 | for (k = aptr[j], k1 = aptr[j + 1]; k < k1; k++) { | ||
2377 | 4961 | // row | ||
2378 | 4962 | i = aindex[k]; | ||
2379 | 4963 | // push index | ||
2380 | 4964 | cindex.push(i); | ||
2381 | 4965 | // update workspace | ||
2382 | 4966 | wa[i] = mark; | ||
2383 | 4967 | // check we need to process values | ||
2384 | 4968 | if (xa) | ||
2385 | 4969 | xa[i] = avalues[k]; | ||
2386 | 4970 | } | ||
2387 | 4971 | // loop values B(:,j) | ||
2388 | 4972 | for (k = bptr[j], k1 = bptr[j + 1]; k < k1; k++) { | ||
2389 | 4973 | // row | ||
2390 | 4974 | i = bindex[k]; | ||
2391 | 4975 | // check row existed in A | ||
2392 | 4976 | if (wa[i] !== mark) { | ||
2393 | 4977 | // push index | ||
2394 | 4978 | cindex.push(i); | ||
2395 | 4979 | } | ||
2396 | 4980 | // update workspace | ||
2397 | 4981 | wb[i] = mark; | ||
2398 | 4982 | // check we need to process values | ||
2399 | 4983 | if (xb) | ||
2400 | 4984 | xb[i] = bvalues[k]; | ||
2401 | 4985 | } | ||
2402 | 4986 | // check we need to process values (non pattern matrix) | ||
2403 | 4987 | if (cvalues) { | ||
2404 | 4988 | // initialize first index in j | ||
2405 | 4989 | k = cptr[j]; | ||
2406 | 4990 | // loop index in j | ||
2407 | 4991 | while (k < cindex.length) { | ||
2408 | 4992 | // row | ||
2409 | 4993 | i = cindex[k]; | ||
2410 | 4994 | // marks | ||
2411 | 4995 | var wai = wa[i]; | ||
2412 | 4996 | var wbi = wb[i]; | ||
2413 | 4997 | // check Aij or Bij are nonzero | ||
2414 | 4998 | if (wai === mark || wbi === mark) { | ||
2415 | 4999 | // matrix values @ i,j | ||
2416 | 5000 | var va = wai === mark ? xa[i] : zero; | ||
2417 | 5001 | var vb = wbi === mark ? xb[i] : zero; | ||
2418 | 5002 | // Cij | ||
2419 | 5003 | var vc = cf(va, vb); | ||
2420 | 5004 | // check for zero | ||
2421 | 5005 | if (!eq(vc, zero)) { | ||
2422 | 5006 | // push value | ||
2423 | 5007 | cvalues.push(vc); | ||
2424 | 5008 | // increment pointer | ||
2425 | 5009 | k++; | ||
2426 | 5010 | } | ||
2427 | 5011 | else { | ||
2428 | 5012 | // remove value @ i, do not increment pointer | ||
2429 | 5013 | cindex.splice(k, 1); | ||
2430 | 5014 | } | ||
2431 | 5015 | } | ||
2432 | 5016 | } | ||
2433 | 5017 | } | ||
2434 | 5018 | } | ||
2435 | 5019 | // update cptr | ||
2436 | 5020 | cptr[columns] = cindex.length; | ||
2437 | 5021 | |||
2438 | 5022 | // return sparse matrix | ||
2439 | 5023 | return c; | ||
2440 | 5024 | }; | ||
2441 | 5025 | |||
2442 | 5026 | return algorithm05; | ||
2443 | 5027 | } | ||
2444 | 5028 | |||
2445 | 5029 | exports.name = 'algorithm05'; | ||
2446 | 5030 | exports.factory = factory; | ||
2447 | 5031 | |||
2448 | 5032 | |||
2449 | 5033 | /***/ }, | ||
2450 | 5034 | /* 33 */ | ||
2451 | 5035 | /***/ function(module, exports, __webpack_require__) { | ||
2452 | 5036 | |||
2453 | 5037 | 'use strict'; | ||
2454 | 5038 | |||
2455 | 5039 | var nearlyEqual = __webpack_require__(8).nearlyEqual; | ||
2456 | 5040 | |||
2457 | 5041 | function factory (type, config, load, typed) { | ||
2458 | 5042 | |||
2459 | 5043 | /** | ||
2460 | 5044 | * Test whether two values are equal. | ||
2461 | 5045 | * | ||
2462 | 5046 | * @param {number | BigNumber | Fraction | boolean | Complex | Unit} x First value to compare | ||
2463 | 5047 | * @param {number | BigNumber | Fraction | boolean | Complex} y Second value to compare | ||
2464 | 5048 | * @return {boolean} Returns true when the compared values are equal, else returns false | ||
2465 | 5049 | * @private | ||
2466 | 5050 | */ | ||
2467 | 5051 | var equalScalar = typed('equalScalar', { | ||
2468 | 5052 | |||
2469 | 5053 | 'boolean, boolean': function (x, y) { | ||
2470 | 5054 | return x === y; | ||
2471 | 5055 | }, | ||
2472 | 5056 | |||
2473 | 5057 | 'number, number': function (x, y) { | ||
2474 | 5058 | return x === y || nearlyEqual(x, y, config.epsilon); | ||
2475 | 5059 | }, | ||
2476 | 5060 | |||
2477 | 5061 | 'BigNumber, BigNumber': function (x, y) { | ||
2478 | 5062 | return x.eq(y); | ||
2479 | 5063 | }, | ||
2480 | 5064 | |||
2481 | 5065 | 'Fraction, Fraction': function (x, y) { | ||
2482 | 5066 | return x.equals(y); | ||
2483 | 5067 | }, | ||
2484 | 5068 | |||
2485 | 5069 | 'Complex, Complex': function (x, y) { | ||
2486 | 5070 | return (x.re === y.re || nearlyEqual(x.re, y.re, config.epsilon)) && | ||
2487 | 5071 | (x.im === y.im || nearlyEqual(x.im, y.im, config.epsilon)); | ||
2488 | 5072 | }, | ||
2489 | 5073 | |||
2490 | 5074 | 'Unit, Unit': function (x, y) { | ||
2491 | 5075 | if (!x.equalBase(y)) { | ||
2492 | 5076 | throw new Error('Cannot compare units with different base'); | ||
2493 | 5077 | } | ||
2494 | 5078 | return x.value === y.value || nearlyEqual(x.value, y.value, config.epsilon); | ||
2495 | 5079 | }, | ||
2496 | 5080 | |||
2497 | 5081 | 'string, string': function (x, y) { | ||
2498 | 5082 | return x === y; | ||
2499 | 5083 | } | ||
2500 | 5084 | }); | ||
2501 | 5085 | |||
2502 | 5086 | return equalScalar; | ||
2503 | 5087 | } | ||
2504 | 5088 | |||
2505 | 5089 | exports.factory = factory; | ||
2506 | 5090 | |||
2507 | 5091 | |||
2508 | 5092 | /***/ }, | ||
2509 | 5093 | /* 34 */ | ||
2510 | 5094 | /***/ function(module, exports) { | ||
2511 | 5095 | |||
2512 | 5096 | 'use strict'; | ||
2513 | 5097 | |||
2514 | 5098 | function factory (type, config, load, typed) { | ||
2515 | 5099 | |||
2516 | 5100 | var DenseMatrix = type.DenseMatrix; | ||
2517 | 5101 | |||
2518 | 5102 | /** | ||
2519 | 5103 | * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). | ||
2520 | 5104 | * Callback function invoked NZ times (number of nonzero items in S). | ||
2521 | 5105 | * | ||
2522 | 5106 | * | ||
2523 | 5107 | * ┌ f(Sij, b) ; S(i,j) !== 0 | ||
2524 | 5108 | * C(i,j) = ┤ | ||
2525 | 5109 | * â”” b ; otherwise | ||
2526 | 5110 | * | ||
2527 | 5111 | * | ||
2528 | 5112 | * @param {Matrix} s The SparseMatrix instance (S) | ||
2529 | 5113 | * @param {Scalar} b The Scalar value | ||
2530 | 5114 | * @param {Function} callback The f(Aij,b) operation to invoke | ||
2531 | 5115 | * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) | ||
2532 | 5116 | * | ||
2533 | 5117 | * @return {Matrix} DenseMatrix (C) | ||
2534 | 5118 | * | ||
2535 | 5119 | * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 | ||
2536 | 5120 | */ | ||
2537 | 5121 | var algorithm10 = function (s, b, callback, inverse) { | ||
2538 | 5122 | // sparse matrix arrays | ||
2539 | 5123 | var avalues = s._values; | ||
2540 | 5124 | var aindex = s._index; | ||
2541 | 5125 | var aptr = s._ptr; | ||
2542 | 5126 | var asize = s._size; | ||
2543 | 5127 | var adt = s._datatype; | ||
2544 | 5128 | |||
2545 | 5129 | // sparse matrix cannot be a Pattern matrix | ||
2546 | 5130 | if (!avalues) | ||
2547 | 5131 | throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); | ||
2548 | 5132 | |||
2549 | 5133 | // rows & columns | ||
2550 | 5134 | var rows = asize[0]; | ||
2551 | 5135 | var columns = asize[1]; | ||
2552 | 5136 | |||
2553 | 5137 | // datatype | ||
2554 | 5138 | var dt; | ||
2555 | 5139 | // callback signature to use | ||
2556 | 5140 | var cf = callback; | ||
2557 | 5141 | |||
2558 | 5142 | // process data types | ||
2559 | 5143 | if (typeof adt === 'string') { | ||
2560 | 5144 | // datatype | ||
2561 | 5145 | dt = adt; | ||
2562 | 5146 | // convert b to the same datatype | ||
2563 | 5147 | b = typed.convert(b, dt); | ||
2564 | 5148 | // callback | ||
2565 | 5149 | cf = typed.find(callback, [dt, dt]); | ||
2566 | 5150 | } | ||
2567 | 5151 | |||
2568 | 5152 | // result arrays | ||
2569 | 5153 | var cdata = []; | ||
2570 | 5154 | // matrix | ||
2571 | 5155 | var c = new DenseMatrix({ | ||
2572 | 5156 | data: cdata, | ||
2573 | 5157 | size: [rows, columns], | ||
2574 | 5158 | datatype: dt | ||
2575 | 5159 | }); | ||
2576 | 5160 | |||
2577 | 5161 | // workspaces | ||
2578 | 5162 | var x = []; | ||
2579 | 5163 | // marks indicating we have a value in x for a given column | ||
2580 | 5164 | var w = []; | ||
2581 | 5165 | |||
2582 | 5166 | // loop columns | ||
2583 | 5167 | for (var j = 0; j < columns; j++) { | ||
2584 | 5168 | // columns mark | ||
2585 | 5169 | var mark = j + 1; | ||
2586 | 5170 | // values in j | ||
2587 | 5171 | for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { | ||
2588 | 5172 | // row | ||
2589 | 5173 | var r = aindex[k]; | ||
2590 | 5174 | // update workspace | ||
2591 | 5175 | x[r] = avalues[k]; | ||
2592 | 5176 | w[r] = mark; | ||
2593 | 5177 | } | ||
2594 | 5178 | // loop rows | ||
2595 | 5179 | for (var i = 0; i < rows; i++) { | ||
2596 | 5180 | // initialize C on first column | ||
2597 | 5181 | if (j === 0) { | ||
2598 | 5182 | // create row array | ||
2599 | 5183 | cdata[i] = []; | ||
2600 | 5184 | } | ||
2601 | 5185 | // check sparse matrix has a value @ i,j | ||
2602 | 5186 | if (w[i] === mark) { | ||
2603 | 5187 | // invoke callback, update C | ||
2604 | 5188 | cdata[i][j] = inverse ? cf(b, x[i]) : cf(x[i], b); | ||
2605 | 5189 | } | ||
2606 | 5190 | else { | ||
2607 | 5191 | // dense matrix value @ i, j | ||
2608 | 5192 | cdata[i][j] = b; | ||
2609 | 5193 | } | ||
2610 | 5194 | } | ||
2611 | 5195 | } | ||
2612 | 5196 | |||
2613 | 5197 | // return sparse matrix | ||
2614 | 5198 | return c; | ||
2615 | 5199 | }; | ||
2616 | 5200 | |||
2617 | 5201 | return algorithm10; | ||
2618 | 5202 | } | ||
2619 | 5203 | |||
2620 | 5204 | exports.name = 'algorithm10'; | ||
2621 | 5205 | exports.factory = factory; | ||
2622 | 5206 | |||
2623 | 5207 | |||
2624 | 5208 | /***/ }, | ||
2625 | 5209 | /* 35 */ | ||
2626 | 5210 | /***/ function(module, exports, __webpack_require__) { | ||
2627 | 5211 | |||
2628 | 5212 | 'use strict'; | ||
2629 | 5213 | |||
2630 | 5214 | var util = __webpack_require__(36); | ||
2631 | 5215 | var DimensionError = __webpack_require__(22); | ||
2632 | 5216 | |||
2633 | 5217 | var string = util.string, | ||
2634 | 5218 | isString = string.isString; | ||
2635 | 5219 | |||
2636 | 5220 | function factory (type, config, load, typed) { | ||
2637 | 5221 | |||
2638 | 5222 | var DenseMatrix = type.DenseMatrix; | ||
2639 | 5223 | |||
2640 | 5224 | /** | ||
2641 | 5225 | * Iterates over DenseMatrix items and invokes the callback function f(Aij..z, Bij..z). | ||
2642 | 5226 | * Callback function invoked MxN times. | ||
2643 | 5227 | * | ||
2644 | 5228 | * C(i,j,...z) = f(Aij..z, Bij..z) | ||
2645 | 5229 | * | ||
2646 | 5230 | * @param {Matrix} a The DenseMatrix instance (A) | ||
2647 | 5231 | * @param {Matrix} b The DenseMatrix instance (B) | ||
2648 | 5232 | * @param {Function} callback The f(Aij..z,Bij..z) operation to invoke | ||
2649 | 5233 | * | ||
2650 | 5234 | * @return {Matrix} DenseMatrix (C) | ||
2651 | 5235 | * | ||
2652 | 5236 | * https://github.com/josdejong/mathjs/pull/346#issuecomment-97658658 | ||
2653 | 5237 | */ | ||
2654 | 5238 | var algorithm13 = function (a, b, callback) { | ||
2655 | 5239 | // a arrays | ||
2656 | 5240 | var adata = a._data; | ||
2657 | 5241 | var asize = a._size; | ||
2658 | 5242 | var adt = a._datatype; | ||
2659 | 5243 | // b arrays | ||
2660 | 5244 | var bdata = b._data; | ||
2661 | 5245 | var bsize = b._size; | ||
2662 | 5246 | var bdt = b._datatype; | ||
2663 | 5247 | // c arrays | ||
2664 | 5248 | var csize = []; | ||
2665 | 5249 | |||
2666 | 5250 | // validate dimensions | ||
2667 | 5251 | if (asize.length !== bsize.length) | ||
2668 | 5252 | throw new DimensionError(asize.length, bsize.length); | ||
2669 | 5253 | |||
2670 | 5254 | // validate each one of the dimension sizes | ||
2671 | 5255 | for (var s = 0; s < asize.length; s++) { | ||
2672 | 5256 | // must match | ||
2673 | 5257 | if (asize[s] !== bsize[s]) | ||
2674 | 5258 | throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); | ||
2675 | 5259 | // update dimension in c | ||
2676 | 5260 | csize[s] = asize[s]; | ||
2677 | 5261 | } | ||
2678 | 5262 | |||
2679 | 5263 | // datatype | ||
2680 | 5264 | var dt; | ||
2681 | 5265 | // callback signature to use | ||
2682 | 5266 | var cf = callback; | ||
2683 | 5267 | |||
2684 | 5268 | // process data types | ||
2685 | 5269 | if (typeof adt === 'string' && adt === bdt) { | ||
2686 | 5270 | // datatype | ||
2687 | 5271 | dt = adt; | ||
2688 | 5272 | // convert b to the same datatype | ||
2689 | 5273 | b = typed.convert(b, dt); | ||
2690 | 5274 | // callback | ||
2691 | 5275 | cf = typed.find(callback, [dt, dt]); | ||
2692 | 5276 | } | ||
2693 | 5277 | |||
2694 | 5278 | // populate cdata, iterate through dimensions | ||
2695 | 5279 | var cdata = csize.length > 0 ? _iterate(cf, 0, csize, csize[0], adata, bdata) : []; | ||
2696 | 5280 | |||
2697 | 5281 | // c matrix | ||
2698 | 5282 | return new DenseMatrix({ | ||
2699 | 5283 | data: cdata, | ||
2700 | 5284 | size: csize, | ||
2701 | 5285 | datatype: dt | ||
2702 | 5286 | }); | ||
2703 | 5287 | }; | ||
2704 | 5288 | |||
2705 | 5289 | // recursive function | ||
2706 | 5290 | var _iterate = function (f, level, s, n, av, bv) { | ||
2707 | 5291 | // initialize array for this level | ||
2708 | 5292 | var cv = []; | ||
2709 | 5293 | // check we reach the last level | ||
2710 | 5294 | if (level === s.length - 1) { | ||
2711 | 5295 | // loop arrays in last level | ||
2712 | 5296 | for (var i = 0; i < n; i++) { | ||
2713 | 5297 | // invoke callback and store value | ||
2714 | 5298 | cv[i] = f(av[i], bv[i]); | ||
2715 | 5299 | } | ||
2716 | 5300 | } | ||
2717 | 5301 | else { | ||
2718 | 5302 | // iterate current level | ||
2719 | 5303 | for (var j = 0; j < n; j++) { | ||
2720 | 5304 | // iterate next level | ||
2721 | 5305 | cv[j] = _iterate(f, level + 1, s, s[level + 1], av[j], bv[j]); | ||
2722 | 5306 | } | ||
2723 | 5307 | } | ||
2724 | 5308 | return cv; | ||
2725 | 5309 | }; | ||
2726 | 5310 | |||
2727 | 5311 | return algorithm13; | ||
2728 | 5312 | } | ||
2729 | 5313 | |||
2730 | 5314 | exports.name = 'algorithm13'; | ||
2731 | 5315 | exports.factory = factory; | ||
2732 | 5316 | |||
2733 | 5317 | |||
2734 | 5318 | /***/ }, | ||
2735 | 5319 | /* 36 */ | ||
2736 | 5320 | /***/ function(module, exports, __webpack_require__) { | ||
2737 | 5321 | |||
2738 | 5322 | 'use strict'; | ||
2739 | 5323 | |||
2740 | 5324 | exports.array = __webpack_require__(18); | ||
2741 | 5325 | exports['boolean'] = __webpack_require__(37); | ||
2742 | 5326 | exports['function'] = __webpack_require__(38); | ||
2743 | 5327 | exports.number = __webpack_require__(8); | ||
2744 | 5328 | exports.object = __webpack_require__(5); | ||
2745 | 5329 | exports.string = __webpack_require__(20); | ||
2746 | 5330 | exports.types = __webpack_require__(19); | ||
2747 | 5331 | exports.emitter = __webpack_require__(3); | ||
2748 | 5332 | |||
2749 | 5333 | |||
2750 | 5334 | /***/ }, | ||
2751 | 5335 | /* 37 */ | ||
2752 | 5336 | /***/ function(module, exports) { | ||
2753 | 5337 | |||
2754 | 5338 | 'use strict'; | ||
2755 | 5339 | |||
2756 | 5340 | /** | ||
2757 | 5341 | * Test whether value is a boolean | ||
2758 | 5342 | * @param {*} value | ||
2759 | 5343 | * @return {boolean} isBoolean | ||
2760 | 5344 | */ | ||
2761 | 5345 | exports.isBoolean = function(value) { | ||
2762 | 5346 | return typeof value == 'boolean'; | ||
2763 | 5347 | }; | ||
2764 | 5348 | |||
2765 | 5349 | |||
2766 | 5350 | /***/ }, | ||
2767 | 5351 | /* 38 */ | ||
2768 | 5352 | /***/ function(module, exports) { | ||
2769 | 5353 | |||
2770 | 5354 | // function utils | ||
2771 | 5355 | |||
2772 | 5356 | /* | ||
2773 | 5357 | * Memoize a given function by caching the computed result. | ||
2774 | 5358 | * The cache of a memoized function can be cleared by deleting the `cache` | ||
2775 | 5359 | * property of the function. | ||
2776 | 5360 | * | ||
2777 | 5361 | * @param {function} fn The function to be memoized. | ||
2778 | 5362 | * Must be a pure function. | ||
2779 | 5363 | * @param {function(args: Array)} [hasher] A custom hash builder. | ||
2780 | 5364 | * Is JSON.stringify by default. | ||
2781 | 5365 | * @return {function} Returns the memoized function | ||
2782 | 5366 | */ | ||
2783 | 5367 | exports.memoize = function(fn, hasher) { | ||
2784 | 5368 | return function memoize() { | ||
2785 | 5369 | if (typeof memoize.cache !== 'object') { | ||
2786 | 5370 | memoize.cache = {}; | ||
2787 | 5371 | } | ||
2788 | 5372 | |||
2789 | 5373 | var args = []; | ||
2790 | 5374 | for (var i = 0; i < arguments.length; i++) { | ||
2791 | 5375 | args[i] = arguments[i]; | ||
2792 | 5376 | } | ||
2793 | 5377 | |||
2794 | 5378 | var hash = hasher ? hasher(args) : JSON.stringify(args); | ||
2795 | 5379 | if (!(hash in memoize.cache)) { | ||
2796 | 5380 | return memoize.cache[hash] = fn.apply(fn, args); | ||
2797 | 5381 | } | ||
2798 | 5382 | return memoize.cache[hash]; | ||
2799 | 5383 | }; | ||
2800 | 5384 | }; | ||
2801 | 5385 | |||
2802 | 5386 | |||
2803 | 5387 | /***/ }, | ||
2804 | 5388 | /* 39 */ | ||
2805 | 5389 | /***/ function(module, exports, __webpack_require__) { | ||
2806 | 5390 | |||
2807 | 5391 | 'use strict'; | ||
2808 | 5392 | |||
2809 | 5393 | var clone = __webpack_require__(5).clone; | ||
2810 | 5394 | |||
2811 | 5395 | function factory (type, config, load, typed) { | ||
2812 | 5396 | |||
2813 | 5397 | var DenseMatrix = type.DenseMatrix; | ||
2814 | 5398 | |||
2815 | 5399 | /** | ||
2816 | 5400 | * Iterates over DenseMatrix items and invokes the callback function f(Aij..z, b). | ||
2817 | 5401 | * Callback function invoked MxN times. | ||
2818 | 5402 | * | ||
2819 | 5403 | * C(i,j,...z) = f(Aij..z, b) | ||
2820 | 5404 | * | ||
2821 | 5405 | * @param {Matrix} a The DenseMatrix instance (A) | ||
2822 | 5406 | * @param {Scalar} b The Scalar value | ||
2823 | 5407 | * @param {Function} callback The f(Aij..z,b) operation to invoke | ||
2824 | 5408 | * @param {boolean} inverse A true value indicates callback should be invoked f(b,Aij..z) | ||
2825 | 5409 | * | ||
2826 | 5410 | * @return {Matrix} DenseMatrix (C) | ||
2827 | 5411 | * | ||
2828 | 5412 | * https://github.com/josdejong/mathjs/pull/346#issuecomment-97659042 | ||
2829 | 5413 | */ | ||
2830 | 5414 | var algorithm14 = function (a, b, callback, inverse) { | ||
2831 | 5415 | // a arrays | ||
2832 | 5416 | var adata = a._data; | ||
2833 | 5417 | var asize = a._size; | ||
2834 | 5418 | var adt = a._datatype; | ||
2835 | 5419 | |||
2836 | 5420 | // datatype | ||
2837 | 5421 | var dt; | ||
2838 | 5422 | // callback signature to use | ||
2839 | 5423 | var cf = callback; | ||
2840 | 5424 | |||
2841 | 5425 | // process data types | ||
2842 | 5426 | if (typeof adt === 'string') { | ||
2843 | 5427 | // datatype | ||
2844 | 5428 | dt = adt; | ||
2845 | 5429 | // convert b to the same datatype | ||
2846 | 5430 | b = typed.convert(b, dt); | ||
2847 | 5431 | // callback | ||
2848 | 5432 | cf = typed.find(callback, [dt, dt]); | ||
2849 | 5433 | } | ||
2850 | 5434 | |||
2851 | 5435 | // populate cdata, iterate through dimensions | ||
2852 | 5436 | var cdata = asize.length > 0 ? _iterate(cf, 0, asize, asize[0], adata, b, inverse) : []; | ||
2853 | 5437 | |||
2854 | 5438 | // c matrix | ||
2855 | 5439 | return new DenseMatrix({ | ||
2856 | 5440 | data: cdata, | ||
2857 | 5441 | size: clone(asize), | ||
2858 | 5442 | datatype: dt | ||
2859 | 5443 | }); | ||
2860 | 5444 | }; | ||
2861 | 5445 | |||
2862 | 5446 | // recursive function | ||
2863 | 5447 | var _iterate = function (f, level, s, n, av, bv, inverse) { | ||
2864 | 5448 | // initialize array for this level | ||
2865 | 5449 | var cv = []; | ||
2866 | 5450 | // check we reach the last level | ||
2867 | 5451 | if (level === s.length - 1) { | ||
2868 | 5452 | // loop arrays in last level | ||
2869 | 5453 | for (var i = 0; i < n; i++) { | ||
2870 | 5454 | // invoke callback and store value | ||
2871 | 5455 | cv[i] = inverse ? f(bv, av[i]) : f(av[i], bv); | ||
2872 | 5456 | } | ||
2873 | 5457 | } | ||
2874 | 5458 | else { | ||
2875 | 5459 | // iterate current level | ||
2876 | 5460 | for (var j = 0; j < n; j++) { | ||
2877 | 5461 | // iterate next level | ||
2878 | 5462 | cv[j] = _iterate(f, level + 1, s, s[level + 1], av[j], bv, inverse); | ||
2879 | 5463 | } | ||
2880 | 5464 | } | ||
2881 | 5465 | return cv; | ||
2882 | 5466 | }; | ||
2883 | 5467 | |||
2884 | 5468 | return algorithm14; | ||
2885 | 5469 | } | ||
2886 | 5470 | |||
2887 | 5471 | exports.name = 'algorithm14'; | ||
2888 | 5472 | exports.factory = factory; | ||
2889 | 5473 | |||
2890 | 5474 | |||
2891 | 5475 | /***/ }, | ||
2892 | 5476 | /* 40 */ | ||
2893 | 5477 | /***/ function(module, exports, __webpack_require__) { | ||
2894 | 5478 | |||
2895 | 5479 | 'use strict'; | ||
2896 | 5480 | |||
2897 | 5481 | var extend = __webpack_require__(5).extend; | ||
2898 | 5482 | var array = __webpack_require__(18); | ||
2899 | 5483 | |||
2900 | 5484 | function factory (type, config, load, typed) { | ||
2901 | 5485 | var latex = __webpack_require__(26); | ||
2902 | 5486 | |||
2903 | 5487 | var matrix = load(__webpack_require__(23)); | ||
2904 | 5488 | var addScalar = load(__webpack_require__(27)); | ||
2905 | 5489 | var multiplyScalar = load(__webpack_require__(41)); | ||
2906 | 5490 | var equalScalar = load(__webpack_require__(33)); | ||
2907 | 5491 | |||
2908 | 5492 | var algorithm11 = load(__webpack_require__(42)); | ||
2909 | 5493 | var algorithm14 = load(__webpack_require__(39)); | ||
2910 | 5494 | |||
2911 | 5495 | var DenseMatrix = type.DenseMatrix; | ||
2912 | 5496 | var SparseMatrix = type.SparseMatrix; | ||
2913 | 5497 | |||
2914 | 5498 | /** | ||
2915 | 5499 | * Multiply two values, `x * y`. The result is squeezed. | ||
2916 | 5500 | * For matrices, the matrix product is calculated. | ||
2917 | 5501 | * | ||
2918 | 5502 | * Syntax: | ||
2919 | 5503 | * | ||
2920 | 5504 | * math.multiply(x, y) | ||
2921 | 5505 | * | ||
2922 | 5506 | * Examples: | ||
2923 | 5507 | * | ||
2924 | 5508 | * math.multiply(4, 5.2); // returns number 20.8 | ||
2925 | 5509 | * | ||
2926 | 5510 | * var a = math.complex(2, 3); | ||
2927 | 5511 | * var b = math.complex(4, 1); | ||
2928 | 5512 | * math.multiply(a, b); // returns Complex 5 + 14i | ||
2929 | 5513 | * | ||
2930 | 5514 | * var c = [[1, 2], [4, 3]]; | ||
2931 | 5515 | * var d = [[1, 2, 3], [3, -4, 7]]; | ||
2932 | 5516 | * math.multiply(c, d); // returns Array [[7, -6, 17], [13, -4, 33]] | ||
2933 | 5517 | * | ||
2934 | 5518 | * var e = math.unit('2.1 km'); | ||
2935 | 5519 | * math.multiply(3, e); // returns Unit 6.3 km | ||
2936 | 5520 | * | ||
2937 | 5521 | * See also: | ||
2938 | 5522 | * | ||
2939 | 5523 | * divide | ||
2940 | 5524 | * | ||
2941 | 5525 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to multiply | ||
2942 | 5526 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to multiply | ||
2943 | 5527 | * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Multiplication of `x` and `y` | ||
2944 | 5528 | */ | ||
2945 | 5529 | var multiply = typed('multiply', extend({ | ||
2946 | 5530 | // we extend the signatures of multiplyScalar with signatures dealing with matrices | ||
2947 | 5531 | |||
2948 | 5532 | 'Array, Array': function (x, y) { | ||
2949 | 5533 | // check dimensions | ||
2950 | 5534 | _validateMatrixDimensions(array.size(x), array.size(y)); | ||
2951 | 5535 | |||
2952 | 5536 | // use dense matrix implementation | ||
2953 | 5537 | var m = multiply(matrix(x), matrix(y)); | ||
2954 | 5538 | // return array or scalar | ||
2955 | 5539 | return (m && m.isMatrix === true) ? m.valueOf() : m; | ||
2956 | 5540 | }, | ||
2957 | 5541 | |||
2958 | 5542 | 'Matrix, Matrix': function (x, y) { | ||
2959 | 5543 | // dimensions | ||
2960 | 5544 | var xsize = x.size(); | ||
2961 | 5545 | var ysize = y.size(); | ||
2962 | 5546 | |||
2963 | 5547 | // check dimensions | ||
2964 | 5548 | _validateMatrixDimensions(xsize, ysize); | ||
2965 | 5549 | |||
2966 | 5550 | // process dimensions | ||
2967 | 5551 | if (xsize.length === 1) { | ||
2968 | 5552 | // process y dimensions | ||
2969 | 5553 | if (ysize.length === 1) { | ||
2970 | 5554 | // Vector * Vector | ||
2971 | 5555 | return _multiplyVectorVector(x, y, xsize[0]); | ||
2972 | 5556 | } | ||
2973 | 5557 | // Vector * Matrix | ||
2974 | 5558 | return _multiplyVectorMatrix(x, y); | ||
2975 | 5559 | } | ||
2976 | 5560 | // process y dimensions | ||
2977 | 5561 | if (ysize.length === 1) { | ||
2978 | 5562 | // Matrix * Vector | ||
2979 | 5563 | return _multiplyMatrixVector(x, y); | ||
2980 | 5564 | } | ||
2981 | 5565 | // Matrix * Matrix | ||
2982 | 5566 | return _multiplyMatrixMatrix(x, y); | ||
2983 | 5567 | }, | ||
2984 | 5568 | |||
2985 | 5569 | 'Matrix, Array': function (x, y) { | ||
2986 | 5570 | // use Matrix * Matrix implementation | ||
2987 | 5571 | return multiply(x, matrix(y)); | ||
2988 | 5572 | }, | ||
2989 | 5573 | |||
2990 | 5574 | 'Array, Matrix': function (x, y) { | ||
2991 | 5575 | // use Matrix * Matrix implementation | ||
2992 | 5576 | return multiply(matrix(x, y.storage()), y); | ||
2993 | 5577 | }, | ||
2994 | 5578 | |||
2995 | 5579 | 'Matrix, any': function (x, y) { | ||
2996 | 5580 | // result | ||
2997 | 5581 | var c; | ||
2998 | 5582 | |||
2999 | 5583 | // process storage format | ||
3000 | 5584 | switch (x.storage()) { | ||
3001 | 5585 | case 'sparse': | ||
3002 | 5586 | c = algorithm11(x, y, multiplyScalar, false); | ||
3003 | 5587 | break; | ||
3004 | 5588 | case 'dense': | ||
3005 | 5589 | c = algorithm14(x, y, multiplyScalar, false); | ||
3006 | 5590 | break; | ||
3007 | 5591 | } | ||
3008 | 5592 | return c; | ||
3009 | 5593 | }, | ||
3010 | 5594 | |||
3011 | 5595 | 'any, Matrix': function (x, y) { | ||
3012 | 5596 | // result | ||
3013 | 5597 | var c; | ||
3014 | 5598 | // check storage format | ||
3015 | 5599 | switch (y.storage()) { | ||
3016 | 5600 | case 'sparse': | ||
3017 | 5601 | c = algorithm11(y, x, multiplyScalar, true); | ||
3018 | 5602 | break; | ||
3019 | 5603 | case 'dense': | ||
3020 | 5604 | c = algorithm14(y, x, multiplyScalar, true); | ||
3021 | 5605 | break; | ||
3022 | 5606 | } | ||
3023 | 5607 | return c; | ||
3024 | 5608 | }, | ||
3025 | 5609 | |||
3026 | 5610 | 'Array, any': function (x, y) { | ||
3027 | 5611 | // use matrix implementation | ||
3028 | 5612 | return algorithm14(matrix(x), y, multiplyScalar, false).valueOf(); | ||
3029 | 5613 | }, | ||
3030 | 5614 | |||
3031 | 5615 | 'any, Array': function (x, y) { | ||
3032 | 5616 | // use matrix implementation | ||
3033 | 5617 | return algorithm14(matrix(y), x, multiplyScalar, true).valueOf(); | ||
3034 | 5618 | } | ||
3035 | 5619 | }, multiplyScalar.signatures)); | ||
3036 | 5620 | |||
3037 | 5621 | var _validateMatrixDimensions = function (size1, size2) { | ||
3038 | 5622 | // check left operand dimensions | ||
3039 | 5623 | switch (size1.length) { | ||
3040 | 5624 | case 1: | ||
3041 | 5625 | // check size2 | ||
3042 | 5626 | switch (size2.length) { | ||
3043 | 5627 | case 1: | ||
3044 | 5628 | // Vector x Vector | ||
3045 | 5629 | if (size1[0] !== size2[0]) { | ||
3046 | 5630 | // throw error | ||
3047 | 5631 | throw new RangeError('Dimension mismatch in multiplication. Vectors must have the same length'); | ||
3048 | 5632 | } | ||
3049 | 5633 | break; | ||
3050 | 5634 | case 2: | ||
3051 | 5635 | // Vector x Matrix | ||
3052 | 5636 | if (size1[0] !== size2[0]) { | ||
3053 | 5637 | // throw error | ||
3054 | 5638 | throw new RangeError('Dimension mismatch in multiplication. Vector length (' + size1[0] + ') must match Matrix rows (' + size2[0] + ')'); | ||
3055 | 5639 | } | ||
3056 | 5640 | break; | ||
3057 | 5641 | default: | ||
3058 | 5642 | throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix B has ' + size2.length + ' dimensions)'); | ||
3059 | 5643 | } | ||
3060 | 5644 | break; | ||
3061 | 5645 | case 2: | ||
3062 | 5646 | // check size2 | ||
3063 | 5647 | switch (size2.length) { | ||
3064 | 5648 | case 1: | ||
3065 | 5649 | // Matrix x Vector | ||
3066 | 5650 | if (size1[1] !== size2[0]) { | ||
3067 | 5651 | // throw error | ||
3068 | 5652 | throw new RangeError('Dimension mismatch in multiplication. Matrix columns (' + size1[1] + ') must match Vector length (' + size2[0] + ')'); | ||
3069 | 5653 | } | ||
3070 | 5654 | break; | ||
3071 | 5655 | case 2: | ||
3072 | 5656 | // Matrix x Matrix | ||
3073 | 5657 | if (size1[1] !== size2[0]) { | ||
3074 | 5658 | // throw error | ||
3075 | 5659 | throw new RangeError('Dimension mismatch in multiplication. Matrix A columns (' + size1[1] + ') must match Matrix B rows (' + size2[0] + ')'); | ||
3076 | 5660 | } | ||
3077 | 5661 | break; | ||
3078 | 5662 | default: | ||
3079 | 5663 | throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix B has ' + size2.length + ' dimensions)'); | ||
3080 | 5664 | } | ||
3081 | 5665 | break; | ||
3082 | 5666 | default: | ||
3083 | 5667 | throw new Error('Can only multiply a 1 or 2 dimensional matrix (Matrix A has ' + size1.length + ' dimensions)'); | ||
3084 | 5668 | } | ||
3085 | 5669 | }; | ||
3086 | 5670 | |||
3087 | 5671 | /** | ||
3088 | 5672 | * C = A * B | ||
3089 | 5673 | * | ||
3090 | 5674 | * @param {Matrix} a Dense Vector (N) | ||
3091 | 5675 | * @param {Matrix} b Dense Vector (N) | ||
3092 | 5676 | * | ||
3093 | 5677 | * @return {number} Scalar value | ||
3094 | 5678 | */ | ||
3095 | 5679 | var _multiplyVectorVector = function (a, b, n) { | ||
3096 | 5680 | // check empty vector | ||
3097 | 5681 | if (n === 0) | ||
3098 | 5682 | throw new Error('Cannot multiply two empty vectors'); | ||
3099 | 5683 | |||
3100 | 5684 | // a dense | ||
3101 | 5685 | var adata = a._data; | ||
3102 | 5686 | var adt = a._datatype; | ||
3103 | 5687 | // b dense | ||
3104 | 5688 | var bdata = b._data; | ||
3105 | 5689 | var bdt = b._datatype; | ||
3106 | 5690 | |||
3107 | 5691 | // datatype | ||
3108 | 5692 | var dt; | ||
3109 | 5693 | // addScalar signature to use | ||
3110 | 5694 | var af = addScalar; | ||
3111 | 5695 | // multiplyScalar signature to use | ||
3112 | 5696 | var mf = multiplyScalar; | ||
3113 | 5697 | |||
3114 | 5698 | // process data types | ||
3115 | 5699 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3116 | 5700 | // datatype | ||
3117 | 5701 | dt = adt; | ||
3118 | 5702 | // find signatures that matches (dt, dt) | ||
3119 | 5703 | af = typed.find(addScalar, [dt, dt]); | ||
3120 | 5704 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3121 | 5705 | } | ||
3122 | 5706 | |||
3123 | 5707 | // result (do not initialize it with zero) | ||
3124 | 5708 | var c = mf(adata[0], bdata[0]); | ||
3125 | 5709 | // loop data | ||
3126 | 5710 | for (var i = 1; i < n; i++) { | ||
3127 | 5711 | // multiply and accumulate | ||
3128 | 5712 | c = af(c, mf(adata[i], bdata[i])); | ||
3129 | 5713 | } | ||
3130 | 5714 | return c; | ||
3131 | 5715 | }; | ||
3132 | 5716 | |||
3133 | 5717 | /** | ||
3134 | 5718 | * C = A * B | ||
3135 | 5719 | * | ||
3136 | 5720 | * @param {Matrix} a Dense Vector (M) | ||
3137 | 5721 | * @param {Matrix} b Matrix (MxN) | ||
3138 | 5722 | * | ||
3139 | 5723 | * @return {Matrix} Dense Vector (N) | ||
3140 | 5724 | */ | ||
3141 | 5725 | var _multiplyVectorMatrix = function (a, b) { | ||
3142 | 5726 | // process storage | ||
3143 | 5727 | switch (b.storage()) { | ||
3144 | 5728 | case 'dense': | ||
3145 | 5729 | return _multiplyVectorDenseMatrix(a, b); | ||
3146 | 5730 | } | ||
3147 | 5731 | throw new Error('Not implemented'); | ||
3148 | 5732 | }; | ||
3149 | 5733 | |||
3150 | 5734 | /** | ||
3151 | 5735 | * C = A * B | ||
3152 | 5736 | * | ||
3153 | 5737 | * @param {Matrix} a Dense Vector (M) | ||
3154 | 5738 | * @param {Matrix} b Dense Matrix (MxN) | ||
3155 | 5739 | * | ||
3156 | 5740 | * @return {Matrix} Dense Vector (N) | ||
3157 | 5741 | */ | ||
3158 | 5742 | var _multiplyVectorDenseMatrix = function (a, b) { | ||
3159 | 5743 | // a dense | ||
3160 | 5744 | var adata = a._data; | ||
3161 | 5745 | var asize = a._size; | ||
3162 | 5746 | var adt = a._datatype; | ||
3163 | 5747 | // b dense | ||
3164 | 5748 | var bdata = b._data; | ||
3165 | 5749 | var bsize = b._size; | ||
3166 | 5750 | var bdt = b._datatype; | ||
3167 | 5751 | // rows & columns | ||
3168 | 5752 | var alength = asize[0]; | ||
3169 | 5753 | var bcolumns = bsize[1]; | ||
3170 | 5754 | |||
3171 | 5755 | // datatype | ||
3172 | 5756 | var dt; | ||
3173 | 5757 | // addScalar signature to use | ||
3174 | 5758 | var af = addScalar; | ||
3175 | 5759 | // multiplyScalar signature to use | ||
3176 | 5760 | var mf = multiplyScalar; | ||
3177 | 5761 | |||
3178 | 5762 | // process data types | ||
3179 | 5763 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3180 | 5764 | // datatype | ||
3181 | 5765 | dt = adt; | ||
3182 | 5766 | // find signatures that matches (dt, dt) | ||
3183 | 5767 | af = typed.find(addScalar, [dt, dt]); | ||
3184 | 5768 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3185 | 5769 | } | ||
3186 | 5770 | |||
3187 | 5771 | // result | ||
3188 | 5772 | var c = []; | ||
3189 | 5773 | |||
3190 | 5774 | // loop matrix columns | ||
3191 | 5775 | for (var j = 0; j < bcolumns; j++) { | ||
3192 | 5776 | // sum (do not initialize it with zero) | ||
3193 | 5777 | var sum = mf(adata[0], bdata[0][j]); | ||
3194 | 5778 | // loop vector | ||
3195 | 5779 | for (var i = 1; i < alength; i++) { | ||
3196 | 5780 | // multiply & accumulate | ||
3197 | 5781 | sum = af(sum, mf(adata[i], bdata[i][j])); | ||
3198 | 5782 | } | ||
3199 | 5783 | c[j] = sum; | ||
3200 | 5784 | } | ||
3201 | 5785 | |||
3202 | 5786 | // check we need to squeeze the result into a scalar | ||
3203 | 5787 | if (bcolumns === 1) | ||
3204 | 5788 | return c[0]; | ||
3205 | 5789 | |||
3206 | 5790 | // return matrix | ||
3207 | 5791 | return new DenseMatrix({ | ||
3208 | 5792 | data: c, | ||
3209 | 5793 | size: [bcolumns], | ||
3210 | 5794 | datatype: dt | ||
3211 | 5795 | }); | ||
3212 | 5796 | }; | ||
3213 | 5797 | |||
3214 | 5798 | /** | ||
3215 | 5799 | * C = A * B | ||
3216 | 5800 | * | ||
3217 | 5801 | * @param {Matrix} a Matrix (MxN) | ||
3218 | 5802 | * @param {Matrix} b Dense Vector (N) | ||
3219 | 5803 | * | ||
3220 | 5804 | * @return {Matrix} Dense Vector (M) | ||
3221 | 5805 | */ | ||
3222 | 5806 | var _multiplyMatrixVector = function (a, b) { | ||
3223 | 5807 | // process storage | ||
3224 | 5808 | switch (a.storage()) { | ||
3225 | 5809 | case 'dense': | ||
3226 | 5810 | return _multiplyDenseMatrixVector(a, b); | ||
3227 | 5811 | case 'sparse': | ||
3228 | 5812 | return _multiplySparseMatrixVector(a, b); | ||
3229 | 5813 | } | ||
3230 | 5814 | }; | ||
3231 | 5815 | |||
3232 | 5816 | /** | ||
3233 | 5817 | * C = A * B | ||
3234 | 5818 | * | ||
3235 | 5819 | * @param {Matrix} a Matrix (MxN) | ||
3236 | 5820 | * @param {Matrix} b Matrix (NxC) | ||
3237 | 5821 | * | ||
3238 | 5822 | * @return {Matrix} Matrix (MxC) | ||
3239 | 5823 | */ | ||
3240 | 5824 | var _multiplyMatrixMatrix = function (a, b) { | ||
3241 | 5825 | // process storage | ||
3242 | 5826 | switch (a.storage()) { | ||
3243 | 5827 | case 'dense': | ||
3244 | 5828 | // process storage | ||
3245 | 5829 | switch (b.storage()) { | ||
3246 | 5830 | case 'dense': | ||
3247 | 5831 | return _multiplyDenseMatrixDenseMatrix(a, b); | ||
3248 | 5832 | case 'sparse': | ||
3249 | 5833 | return _multiplyDenseMatrixSparseMatrix(a, b); | ||
3250 | 5834 | } | ||
3251 | 5835 | break; | ||
3252 | 5836 | case 'sparse': | ||
3253 | 5837 | // process storage | ||
3254 | 5838 | switch (b.storage()) { | ||
3255 | 5839 | case 'dense': | ||
3256 | 5840 | return _multiplySparseMatrixDenseMatrix(a, b); | ||
3257 | 5841 | case 'sparse': | ||
3258 | 5842 | return _multiplySparseMatrixSparseMatrix(a, b); | ||
3259 | 5843 | } | ||
3260 | 5844 | break; | ||
3261 | 5845 | } | ||
3262 | 5846 | }; | ||
3263 | 5847 | |||
3264 | 5848 | /** | ||
3265 | 5849 | * C = A * B | ||
3266 | 5850 | * | ||
3267 | 5851 | * @param {Matrix} a DenseMatrix (MxN) | ||
3268 | 5852 | * @param {Matrix} b Dense Vector (N) | ||
3269 | 5853 | * | ||
3270 | 5854 | * @return {Matrix} Dense Vector (M) | ||
3271 | 5855 | */ | ||
3272 | 5856 | var _multiplyDenseMatrixVector = function (a, b) { | ||
3273 | 5857 | // a dense | ||
3274 | 5858 | var adata = a._data; | ||
3275 | 5859 | var asize = a._size; | ||
3276 | 5860 | var adt = a._datatype; | ||
3277 | 5861 | // b dense | ||
3278 | 5862 | var bdata = b._data; | ||
3279 | 5863 | var bdt = b._datatype; | ||
3280 | 5864 | // rows & columns | ||
3281 | 5865 | var arows = asize[0]; | ||
3282 | 5866 | var acolumns = asize[1]; | ||
3283 | 5867 | |||
3284 | 5868 | // datatype | ||
3285 | 5869 | var dt; | ||
3286 | 5870 | // addScalar signature to use | ||
3287 | 5871 | var af = addScalar; | ||
3288 | 5872 | // multiplyScalar signature to use | ||
3289 | 5873 | var mf = multiplyScalar; | ||
3290 | 5874 | |||
3291 | 5875 | // process data types | ||
3292 | 5876 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3293 | 5877 | // datatype | ||
3294 | 5878 | dt = adt; | ||
3295 | 5879 | // find signatures that matches (dt, dt) | ||
3296 | 5880 | af = typed.find(addScalar, [dt, dt]); | ||
3297 | 5881 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3298 | 5882 | } | ||
3299 | 5883 | |||
3300 | 5884 | // result | ||
3301 | 5885 | var c = []; | ||
3302 | 5886 | |||
3303 | 5887 | // loop matrix a rows | ||
3304 | 5888 | for (var i = 0; i < arows; i++) { | ||
3305 | 5889 | // current row | ||
3306 | 5890 | var row = adata[i]; | ||
3307 | 5891 | // sum (do not initialize it with zero) | ||
3308 | 5892 | var sum = mf(row[0], bdata[0]); | ||
3309 | 5893 | // loop matrix a columns | ||
3310 | 5894 | for (var j = 1; j < acolumns; j++) { | ||
3311 | 5895 | // multiply & accumulate | ||
3312 | 5896 | sum = af(sum, mf(row[j], bdata[j])); | ||
3313 | 5897 | } | ||
3314 | 5898 | c[i] = sum; | ||
3315 | 5899 | } | ||
3316 | 5900 | // check we need to squeeze the result into a scalar | ||
3317 | 5901 | if (arows === 1) | ||
3318 | 5902 | return c[0]; | ||
3319 | 5903 | |||
3320 | 5904 | // return matrix | ||
3321 | 5905 | return new DenseMatrix({ | ||
3322 | 5906 | data: c, | ||
3323 | 5907 | size: [arows], | ||
3324 | 5908 | datatype: dt | ||
3325 | 5909 | }); | ||
3326 | 5910 | }; | ||
3327 | 5911 | |||
3328 | 5912 | /** | ||
3329 | 5913 | * C = A * B | ||
3330 | 5914 | * | ||
3331 | 5915 | * @param {Matrix} a DenseMatrix (MxN) | ||
3332 | 5916 | * @param {Matrix} b DenseMatrix (NxC) | ||
3333 | 5917 | * | ||
3334 | 5918 | * @return {Matrix} DenseMatrix (MxC) | ||
3335 | 5919 | */ | ||
3336 | 5920 | var _multiplyDenseMatrixDenseMatrix = function (a, b) { | ||
3337 | 5921 | // a dense | ||
3338 | 5922 | var adata = a._data; | ||
3339 | 5923 | var asize = a._size; | ||
3340 | 5924 | var adt = a._datatype; | ||
3341 | 5925 | // b dense | ||
3342 | 5926 | var bdata = b._data; | ||
3343 | 5927 | var bsize = b._size; | ||
3344 | 5928 | var bdt = b._datatype; | ||
3345 | 5929 | // rows & columns | ||
3346 | 5930 | var arows = asize[0]; | ||
3347 | 5931 | var acolumns = asize[1]; | ||
3348 | 5932 | var bcolumns = bsize[1]; | ||
3349 | 5933 | |||
3350 | 5934 | // datatype | ||
3351 | 5935 | var dt; | ||
3352 | 5936 | // addScalar signature to use | ||
3353 | 5937 | var af = addScalar; | ||
3354 | 5938 | // multiplyScalar signature to use | ||
3355 | 5939 | var mf = multiplyScalar; | ||
3356 | 5940 | |||
3357 | 5941 | // process data types | ||
3358 | 5942 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3359 | 5943 | // datatype | ||
3360 | 5944 | dt = adt; | ||
3361 | 5945 | // find signatures that matches (dt, dt) | ||
3362 | 5946 | af = typed.find(addScalar, [dt, dt]); | ||
3363 | 5947 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3364 | 5948 | } | ||
3365 | 5949 | |||
3366 | 5950 | // result | ||
3367 | 5951 | var c = []; | ||
3368 | 5952 | |||
3369 | 5953 | // loop matrix a rows | ||
3370 | 5954 | for (var i = 0; i < arows; i++) { | ||
3371 | 5955 | // current row | ||
3372 | 5956 | var row = adata[i]; | ||
3373 | 5957 | // initialize row array | ||
3374 | 5958 | c[i] = []; | ||
3375 | 5959 | // loop matrix b columns | ||
3376 | 5960 | for (var j = 0; j < bcolumns; j++) { | ||
3377 | 5961 | // sum (avoid initializing sum to zero) | ||
3378 | 5962 | var sum = mf(row[0], bdata[0][j]); | ||
3379 | 5963 | // loop matrix a columns | ||
3380 | 5964 | for (var x = 1; x < acolumns; x++) { | ||
3381 | 5965 | // multiply & accumulate | ||
3382 | 5966 | sum = af(sum, mf(row[x], bdata[x][j])); | ||
3383 | 5967 | } | ||
3384 | 5968 | c[i][j] = sum; | ||
3385 | 5969 | } | ||
3386 | 5970 | } | ||
3387 | 5971 | // check we need to squeeze the result into a scalar | ||
3388 | 5972 | if (arows === 1 && bcolumns === 1) | ||
3389 | 5973 | return c[0][0]; | ||
3390 | 5974 | |||
3391 | 5975 | // return matrix | ||
3392 | 5976 | return new DenseMatrix({ | ||
3393 | 5977 | data: c, | ||
3394 | 5978 | size: [arows, bcolumns], | ||
3395 | 5979 | datatype: dt | ||
3396 | 5980 | }); | ||
3397 | 5981 | }; | ||
3398 | 5982 | |||
3399 | 5983 | /** | ||
3400 | 5984 | * C = A * B | ||
3401 | 5985 | * | ||
3402 | 5986 | * @param {Matrix} a DenseMatrix (MxN) | ||
3403 | 5987 | * @param {Matrix} b SparseMatrix (NxC) | ||
3404 | 5988 | * | ||
3405 | 5989 | * @return {Matrix} SparseMatrix (MxC) | ||
3406 | 5990 | */ | ||
3407 | 5991 | var _multiplyDenseMatrixSparseMatrix = function (a, b) { | ||
3408 | 5992 | // a dense | ||
3409 | 5993 | var adata = a._data; | ||
3410 | 5994 | var asize = a._size; | ||
3411 | 5995 | var adt = a._datatype; | ||
3412 | 5996 | // b sparse | ||
3413 | 5997 | var bvalues = b._values; | ||
3414 | 5998 | var bindex = b._index; | ||
3415 | 5999 | var bptr = b._ptr; | ||
3416 | 6000 | var bsize = b._size; | ||
3417 | 6001 | var bdt = b._datatype; | ||
3418 | 6002 | // validate b matrix | ||
3419 | 6003 | if (!bvalues) | ||
3420 | 6004 | throw new Error('Cannot multiply Dense Matrix times Pattern only Matrix'); | ||
3421 | 6005 | // rows & columns | ||
3422 | 6006 | var arows = asize[0]; | ||
3423 | 6007 | var bcolumns = bsize[1]; | ||
3424 | 6008 | |||
3425 | 6009 | // datatype | ||
3426 | 6010 | var dt; | ||
3427 | 6011 | // addScalar signature to use | ||
3428 | 6012 | var af = addScalar; | ||
3429 | 6013 | // multiplyScalar signature to use | ||
3430 | 6014 | var mf = multiplyScalar; | ||
3431 | 6015 | // equalScalar signature to use | ||
3432 | 6016 | var eq = equalScalar; | ||
3433 | 6017 | // zero value | ||
3434 | 6018 | var zero = 0; | ||
3435 | 6019 | |||
3436 | 6020 | // process data types | ||
3437 | 6021 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3438 | 6022 | // datatype | ||
3439 | 6023 | dt = adt; | ||
3440 | 6024 | // find signatures that matches (dt, dt) | ||
3441 | 6025 | af = typed.find(addScalar, [dt, dt]); | ||
3442 | 6026 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3443 | 6027 | eq = typed.find(equalScalar, [dt, dt]); | ||
3444 | 6028 | // convert 0 to the same datatype | ||
3445 | 6029 | zero = typed.convert(0, dt); | ||
3446 | 6030 | } | ||
3447 | 6031 | |||
3448 | 6032 | // result | ||
3449 | 6033 | var cvalues = []; | ||
3450 | 6034 | var cindex = []; | ||
3451 | 6035 | var cptr = []; | ||
3452 | 6036 | // c matrix | ||
3453 | 6037 | var c = new SparseMatrix({ | ||
3454 | 6038 | values : cvalues, | ||
3455 | 6039 | index: cindex, | ||
3456 | 6040 | ptr: cptr, | ||
3457 | 6041 | size: [arows, bcolumns], | ||
3458 | 6042 | datatype: dt | ||
3459 | 6043 | }); | ||
3460 | 6044 | |||
3461 | 6045 | // loop b columns | ||
3462 | 6046 | for (var jb = 0; jb < bcolumns; jb++) { | ||
3463 | 6047 | // update ptr | ||
3464 | 6048 | cptr[jb] = cindex.length; | ||
3465 | 6049 | // indeces in column jb | ||
3466 | 6050 | var kb0 = bptr[jb]; | ||
3467 | 6051 | var kb1 = bptr[jb + 1]; | ||
3468 | 6052 | // do not process column jb if no data exists | ||
3469 | 6053 | if (kb1 > kb0) { | ||
3470 | 6054 | // last row mark processed | ||
3471 | 6055 | var last = 0; | ||
3472 | 6056 | // loop a rows | ||
3473 | 6057 | for (var i = 0; i < arows; i++) { | ||
3474 | 6058 | // column mark | ||
3475 | 6059 | var mark = i + 1; | ||
3476 | 6060 | // C[i, jb] | ||
3477 | 6061 | var cij; | ||
3478 | 6062 | // values in b column j | ||
3479 | 6063 | for (var kb = kb0; kb < kb1; kb++) { | ||
3480 | 6064 | // row | ||
3481 | 6065 | var ib = bindex[kb]; | ||
3482 | 6066 | // check value has been initialized | ||
3483 | 6067 | if (last !== mark) { | ||
3484 | 6068 | // first value in column jb | ||
3485 | 6069 | cij = mf(adata[i][ib], bvalues[kb]); | ||
3486 | 6070 | // update mark | ||
3487 | 6071 | last = mark; | ||
3488 | 6072 | } | ||
3489 | 6073 | else { | ||
3490 | 6074 | // accumulate value | ||
3491 | 6075 | cij = af(cij, mf(adata[i][ib], bvalues[kb])); | ||
3492 | 6076 | } | ||
3493 | 6077 | } | ||
3494 | 6078 | // check column has been processed and value != 0 | ||
3495 | 6079 | if (last === mark && !eq(cij, zero)) { | ||
3496 | 6080 | // push row & value | ||
3497 | 6081 | cindex.push(i); | ||
3498 | 6082 | cvalues.push(cij); | ||
3499 | 6083 | } | ||
3500 | 6084 | } | ||
3501 | 6085 | } | ||
3502 | 6086 | } | ||
3503 | 6087 | // update ptr | ||
3504 | 6088 | cptr[bcolumns] = cindex.length; | ||
3505 | 6089 | |||
3506 | 6090 | // check we need to squeeze the result into a scalar | ||
3507 | 6091 | if (arows === 1 && bcolumns === 1) | ||
3508 | 6092 | return cvalues.length === 1 ? cvalues[0] : 0; | ||
3509 | 6093 | |||
3510 | 6094 | // return sparse matrix | ||
3511 | 6095 | return c; | ||
3512 | 6096 | }; | ||
3513 | 6097 | |||
3514 | 6098 | /** | ||
3515 | 6099 | * C = A * B | ||
3516 | 6100 | * | ||
3517 | 6101 | * @param {Matrix} a SparseMatrix (MxN) | ||
3518 | 6102 | * @param {Matrix} b Dense Vector (N) | ||
3519 | 6103 | * | ||
3520 | 6104 | * @return {Matrix} SparseMatrix (M, 1) | ||
3521 | 6105 | */ | ||
3522 | 6106 | var _multiplySparseMatrixVector = function (a, b) { | ||
3523 | 6107 | // a sparse | ||
3524 | 6108 | var avalues = a._values; | ||
3525 | 6109 | var aindex = a._index; | ||
3526 | 6110 | var aptr = a._ptr; | ||
3527 | 6111 | var adt = a._datatype; | ||
3528 | 6112 | // validate a matrix | ||
3529 | 6113 | if (!avalues) | ||
3530 | 6114 | throw new Error('Cannot multiply Pattern only Matrix times Dense Matrix'); | ||
3531 | 6115 | // b dense | ||
3532 | 6116 | var bdata = b._data; | ||
3533 | 6117 | var bdt = b._datatype; | ||
3534 | 6118 | // rows & columns | ||
3535 | 6119 | var arows = a._size[0]; | ||
3536 | 6120 | var brows = b._size[0]; | ||
3537 | 6121 | // result | ||
3538 | 6122 | var cvalues = []; | ||
3539 | 6123 | var cindex = []; | ||
3540 | 6124 | var cptr = []; | ||
3541 | 6125 | |||
3542 | 6126 | // datatype | ||
3543 | 6127 | var dt; | ||
3544 | 6128 | // addScalar signature to use | ||
3545 | 6129 | var af = addScalar; | ||
3546 | 6130 | // multiplyScalar signature to use | ||
3547 | 6131 | var mf = multiplyScalar; | ||
3548 | 6132 | // equalScalar signature to use | ||
3549 | 6133 | var eq = equalScalar; | ||
3550 | 6134 | // zero value | ||
3551 | 6135 | var zero = 0; | ||
3552 | 6136 | |||
3553 | 6137 | // process data types | ||
3554 | 6138 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3555 | 6139 | // datatype | ||
3556 | 6140 | dt = adt; | ||
3557 | 6141 | // find signatures that matches (dt, dt) | ||
3558 | 6142 | af = typed.find(addScalar, [dt, dt]); | ||
3559 | 6143 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3560 | 6144 | eq = typed.find(equalScalar, [dt, dt]); | ||
3561 | 6145 | // convert 0 to the same datatype | ||
3562 | 6146 | zero = typed.convert(0, dt); | ||
3563 | 6147 | } | ||
3564 | 6148 | |||
3565 | 6149 | // workspace | ||
3566 | 6150 | var x = []; | ||
3567 | 6151 | // vector with marks indicating a value x[i] exists in a given column | ||
3568 | 6152 | var w = []; | ||
3569 | 6153 | |||
3570 | 6154 | // update ptr | ||
3571 | 6155 | cptr[0] = 0; | ||
3572 | 6156 | // rows in b | ||
3573 | 6157 | for (var ib = 0; ib < brows; ib++) { | ||
3574 | 6158 | // b[ib] | ||
3575 | 6159 | var vbi = bdata[ib]; | ||
3576 | 6160 | // check b[ib] != 0, avoid loops | ||
3577 | 6161 | if (!eq(vbi, zero)) { | ||
3578 | 6162 | // A values & index in ib column | ||
3579 | 6163 | for (var ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { | ||
3580 | 6164 | // a row | ||
3581 | 6165 | var ia = aindex[ka]; | ||
3582 | 6166 | // check value exists in current j | ||
3583 | 6167 | if (!w[ia]) { | ||
3584 | 6168 | // ia is new entry in j | ||
3585 | 6169 | w[ia] = true; | ||
3586 | 6170 | // add i to pattern of C | ||
3587 | 6171 | cindex.push(ia); | ||
3588 | 6172 | // x(ia) = A | ||
3589 | 6173 | x[ia] = mf(vbi, avalues[ka]); | ||
3590 | 6174 | } | ||
3591 | 6175 | else { | ||
3592 | 6176 | // i exists in C already | ||
3593 | 6177 | x[ia] = af(x[ia], mf(vbi, avalues[ka])); | ||
3594 | 6178 | } | ||
3595 | 6179 | } | ||
3596 | 6180 | } | ||
3597 | 6181 | } | ||
3598 | 6182 | // copy values from x to column jb of c | ||
3599 | 6183 | for (var p1 = cindex.length, p = 0; p < p1; p++) { | ||
3600 | 6184 | // row | ||
3601 | 6185 | var ic = cindex[p]; | ||
3602 | 6186 | // copy value | ||
3603 | 6187 | cvalues[p] = x[ic]; | ||
3604 | 6188 | } | ||
3605 | 6189 | // update ptr | ||
3606 | 6190 | cptr[1] = cindex.length; | ||
3607 | 6191 | |||
3608 | 6192 | // check we need to squeeze the result into a scalar | ||
3609 | 6193 | if (arows === 1) | ||
3610 | 6194 | return cvalues.length === 1 ? cvalues[0] : 0; | ||
3611 | 6195 | |||
3612 | 6196 | // return sparse matrix | ||
3613 | 6197 | return new SparseMatrix({ | ||
3614 | 6198 | values : cvalues, | ||
3615 | 6199 | index: cindex, | ||
3616 | 6200 | ptr: cptr, | ||
3617 | 6201 | size: [arows, 1], | ||
3618 | 6202 | datatype: dt | ||
3619 | 6203 | }); | ||
3620 | 6204 | }; | ||
3621 | 6205 | |||
3622 | 6206 | /** | ||
3623 | 6207 | * C = A * B | ||
3624 | 6208 | * | ||
3625 | 6209 | * @param {Matrix} a SparseMatrix (MxN) | ||
3626 | 6210 | * @param {Matrix} b DenseMatrix (NxC) | ||
3627 | 6211 | * | ||
3628 | 6212 | * @return {Matrix} SparseMatrix (MxC) | ||
3629 | 6213 | */ | ||
3630 | 6214 | var _multiplySparseMatrixDenseMatrix = function (a, b) { | ||
3631 | 6215 | // a sparse | ||
3632 | 6216 | var avalues = a._values; | ||
3633 | 6217 | var aindex = a._index; | ||
3634 | 6218 | var aptr = a._ptr; | ||
3635 | 6219 | var adt = a._datatype; | ||
3636 | 6220 | // validate a matrix | ||
3637 | 6221 | if (!avalues) | ||
3638 | 6222 | throw new Error('Cannot multiply Pattern only Matrix times Dense Matrix'); | ||
3639 | 6223 | // b dense | ||
3640 | 6224 | var bdata = b._data; | ||
3641 | 6225 | var bdt = b._datatype; | ||
3642 | 6226 | // rows & columns | ||
3643 | 6227 | var arows = a._size[0]; | ||
3644 | 6228 | var brows = b._size[0]; | ||
3645 | 6229 | var bcolumns = b._size[1]; | ||
3646 | 6230 | |||
3647 | 6231 | // datatype | ||
3648 | 6232 | var dt; | ||
3649 | 6233 | // addScalar signature to use | ||
3650 | 6234 | var af = addScalar; | ||
3651 | 6235 | // multiplyScalar signature to use | ||
3652 | 6236 | var mf = multiplyScalar; | ||
3653 | 6237 | // equalScalar signature to use | ||
3654 | 6238 | var eq = equalScalar; | ||
3655 | 6239 | // zero value | ||
3656 | 6240 | var zero = 0; | ||
3657 | 6241 | |||
3658 | 6242 | // process data types | ||
3659 | 6243 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3660 | 6244 | // datatype | ||
3661 | 6245 | dt = adt; | ||
3662 | 6246 | // find signatures that matches (dt, dt) | ||
3663 | 6247 | af = typed.find(addScalar, [dt, dt]); | ||
3664 | 6248 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3665 | 6249 | eq = typed.find(equalScalar, [dt, dt]); | ||
3666 | 6250 | // convert 0 to the same datatype | ||
3667 | 6251 | zero = typed.convert(0, dt); | ||
3668 | 6252 | } | ||
3669 | 6253 | |||
3670 | 6254 | // result | ||
3671 | 6255 | var cvalues = []; | ||
3672 | 6256 | var cindex = []; | ||
3673 | 6257 | var cptr = []; | ||
3674 | 6258 | // c matrix | ||
3675 | 6259 | var c = new SparseMatrix({ | ||
3676 | 6260 | values : cvalues, | ||
3677 | 6261 | index: cindex, | ||
3678 | 6262 | ptr: cptr, | ||
3679 | 6263 | size: [arows, bcolumns], | ||
3680 | 6264 | datatype: dt | ||
3681 | 6265 | }); | ||
3682 | 6266 | |||
3683 | 6267 | // workspace | ||
3684 | 6268 | var x = []; | ||
3685 | 6269 | // vector with marks indicating a value x[i] exists in a given column | ||
3686 | 6270 | var w = []; | ||
3687 | 6271 | |||
3688 | 6272 | // loop b columns | ||
3689 | 6273 | for (var jb = 0; jb < bcolumns; jb++) { | ||
3690 | 6274 | // update ptr | ||
3691 | 6275 | cptr[jb] = cindex.length; | ||
3692 | 6276 | // mark in workspace for current column | ||
3693 | 6277 | var mark = jb + 1; | ||
3694 | 6278 | // rows in jb | ||
3695 | 6279 | for (var ib = 0; ib < brows; ib++) { | ||
3696 | 6280 | // b[ib, jb] | ||
3697 | 6281 | var vbij = bdata[ib][jb]; | ||
3698 | 6282 | // check b[ib, jb] != 0, avoid loops | ||
3699 | 6283 | if (!eq(vbij, zero)) { | ||
3700 | 6284 | // A values & index in ib column | ||
3701 | 6285 | for (var ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { | ||
3702 | 6286 | // a row | ||
3703 | 6287 | var ia = aindex[ka]; | ||
3704 | 6288 | // check value exists in current j | ||
3705 | 6289 | if (w[ia] !== mark) { | ||
3706 | 6290 | // ia is new entry in j | ||
3707 | 6291 | w[ia] = mark; | ||
3708 | 6292 | // add i to pattern of C | ||
3709 | 6293 | cindex.push(ia); | ||
3710 | 6294 | // x(ia) = A | ||
3711 | 6295 | x[ia] = mf(vbij, avalues[ka]); | ||
3712 | 6296 | } | ||
3713 | 6297 | else { | ||
3714 | 6298 | // i exists in C already | ||
3715 | 6299 | x[ia] = af(x[ia], mf(vbij, avalues[ka])); | ||
3716 | 6300 | } | ||
3717 | 6301 | } | ||
3718 | 6302 | } | ||
3719 | 6303 | } | ||
3720 | 6304 | // copy values from x to column jb of c | ||
3721 | 6305 | for (var p0 = cptr[jb], p1 = cindex.length, p = p0; p < p1; p++) { | ||
3722 | 6306 | // row | ||
3723 | 6307 | var ic = cindex[p]; | ||
3724 | 6308 | // copy value | ||
3725 | 6309 | cvalues[p] = x[ic]; | ||
3726 | 6310 | } | ||
3727 | 6311 | } | ||
3728 | 6312 | // update ptr | ||
3729 | 6313 | cptr[bcolumns] = cindex.length; | ||
3730 | 6314 | |||
3731 | 6315 | // check we need to squeeze the result into a scalar | ||
3732 | 6316 | if (arows === 1 && bcolumns === 1) | ||
3733 | 6317 | return cvalues.length === 1 ? cvalues[0] : 0; | ||
3734 | 6318 | |||
3735 | 6319 | // return sparse matrix | ||
3736 | 6320 | return c; | ||
3737 | 6321 | }; | ||
3738 | 6322 | |||
3739 | 6323 | /** | ||
3740 | 6324 | * C = A * B | ||
3741 | 6325 | * | ||
3742 | 6326 | * @param {Matrix} a SparseMatrix (MxN) | ||
3743 | 6327 | * @param {Matrix} b SparseMatrix (NxC) | ||
3744 | 6328 | * | ||
3745 | 6329 | * @return {Matrix} SparseMatrix (MxC) | ||
3746 | 6330 | */ | ||
3747 | 6331 | var _multiplySparseMatrixSparseMatrix = function (a, b) { | ||
3748 | 6332 | // a sparse | ||
3749 | 6333 | var avalues = a._values; | ||
3750 | 6334 | var aindex = a._index; | ||
3751 | 6335 | var aptr = a._ptr; | ||
3752 | 6336 | var adt = a._datatype; | ||
3753 | 6337 | // b sparse | ||
3754 | 6338 | var bvalues = b._values; | ||
3755 | 6339 | var bindex = b._index; | ||
3756 | 6340 | var bptr = b._ptr; | ||
3757 | 6341 | var bdt = b._datatype; | ||
3758 | 6342 | |||
3759 | 6343 | // rows & columns | ||
3760 | 6344 | var arows = a._size[0]; | ||
3761 | 6345 | var bcolumns = b._size[1]; | ||
3762 | 6346 | // flag indicating both matrices (a & b) contain data | ||
3763 | 6347 | var values = avalues && bvalues; | ||
3764 | 6348 | |||
3765 | 6349 | // datatype | ||
3766 | 6350 | var dt; | ||
3767 | 6351 | // addScalar signature to use | ||
3768 | 6352 | var af = addScalar; | ||
3769 | 6353 | // multiplyScalar signature to use | ||
3770 | 6354 | var mf = multiplyScalar; | ||
3771 | 6355 | |||
3772 | 6356 | // process data types | ||
3773 | 6357 | if (adt && bdt && adt === bdt && typeof adt === 'string') { | ||
3774 | 6358 | // datatype | ||
3775 | 6359 | dt = adt; | ||
3776 | 6360 | // find signatures that matches (dt, dt) | ||
3777 | 6361 | af = typed.find(addScalar, [dt, dt]); | ||
3778 | 6362 | mf = typed.find(multiplyScalar, [dt, dt]); | ||
3779 | 6363 | } | ||
3780 | 6364 | |||
3781 | 6365 | // result | ||
3782 | 6366 | var cvalues = values ? [] : undefined; | ||
3783 | 6367 | var cindex = []; | ||
3784 | 6368 | var cptr = []; | ||
3785 | 6369 | // c matrix | ||
3786 | 6370 | var c = new SparseMatrix({ | ||
3787 | 6371 | values : cvalues, | ||
3788 | 6372 | index: cindex, | ||
3789 | 6373 | ptr: cptr, | ||
3790 | 6374 | size: [arows, bcolumns], | ||
3791 | 6375 | datatype: dt | ||
3792 | 6376 | }); | ||
3793 | 6377 | |||
3794 | 6378 | // workspace | ||
3795 | 6379 | var x = values ? [] : undefined; | ||
3796 | 6380 | // vector with marks indicating a value x[i] exists in a given column | ||
3797 | 6381 | var w = []; | ||
3798 | 6382 | // variables | ||
3799 | 6383 | var ka, ka0, ka1, kb, kb0, kb1, ia, ib; | ||
3800 | 6384 | // loop b columns | ||
3801 | 6385 | for (var jb = 0; jb < bcolumns; jb++) { | ||
3802 | 6386 | // update ptr | ||
3803 | 6387 | cptr[jb] = cindex.length; | ||
3804 | 6388 | // mark in workspace for current column | ||
3805 | 6389 | var mark = jb + 1; | ||
3806 | 6390 | // B values & index in j | ||
3807 | 6391 | for (kb0 = bptr[jb], kb1 = bptr[jb + 1], kb = kb0; kb < kb1; kb++) { | ||
3808 | 6392 | // b row | ||
3809 | 6393 | ib = bindex[kb]; | ||
3810 | 6394 | // check we need to process values | ||
3811 | 6395 | if (values) { | ||
3812 | 6396 | // loop values in a[:,ib] | ||
3813 | 6397 | for (ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { | ||
3814 | 6398 | // row | ||
3815 | 6399 | ia = aindex[ka]; | ||
3816 | 6400 | // check value exists in current j | ||
3817 | 6401 | if (w[ia] !== mark) { | ||
3818 | 6402 | // ia is new entry in j | ||
3819 | 6403 | w[ia] = mark; | ||
3820 | 6404 | // add i to pattern of C | ||
3821 | 6405 | cindex.push(ia); | ||
3822 | 6406 | // x(ia) = A | ||
3823 | 6407 | x[ia] = mf(bvalues[kb], avalues[ka]); | ||
3824 | 6408 | } | ||
3825 | 6409 | else { | ||
3826 | 6410 | // i exists in C already | ||
3827 | 6411 | x[ia] = af(x[ia], mf(bvalues[kb], avalues[ka])); | ||
3828 | 6412 | } | ||
3829 | 6413 | } | ||
3830 | 6414 | } | ||
3831 | 6415 | else { | ||
3832 | 6416 | // loop values in a[:,ib] | ||
3833 | 6417 | for (ka0 = aptr[ib], ka1 = aptr[ib + 1], ka = ka0; ka < ka1; ka++) { | ||
3834 | 6418 | // row | ||
3835 | 6419 | ia = aindex[ka]; | ||
3836 | 6420 | // check value exists in current j | ||
3837 | 6421 | if (w[ia] !== mark) { | ||
3838 | 6422 | // ia is new entry in j | ||
3839 | 6423 | w[ia] = mark; | ||
3840 | 6424 | // add i to pattern of C | ||
3841 | 6425 | cindex.push(ia); | ||
3842 | 6426 | } | ||
3843 | 6427 | } | ||
3844 | 6428 | } | ||
3845 | 6429 | } | ||
3846 | 6430 | // check we need to process matrix values (pattern matrix) | ||
3847 | 6431 | if (values) { | ||
3848 | 6432 | // copy values from x to column jb of c | ||
3849 | 6433 | for (var p0 = cptr[jb], p1 = cindex.length, p = p0; p < p1; p++) { | ||
3850 | 6434 | // row | ||
3851 | 6435 | var ic = cindex[p]; | ||
3852 | 6436 | // copy value | ||
3853 | 6437 | cvalues[p] = x[ic]; | ||
3854 | 6438 | } | ||
3855 | 6439 | } | ||
3856 | 6440 | } | ||
3857 | 6441 | // update ptr | ||
3858 | 6442 | cptr[bcolumns] = cindex.length; | ||
3859 | 6443 | |||
3860 | 6444 | // check we need to squeeze the result into a scalar | ||
3861 | 6445 | if (arows === 1 && bcolumns === 1 && values) | ||
3862 | 6446 | return cvalues.length === 1 ? cvalues[0] : 0; | ||
3863 | 6447 | |||
3864 | 6448 | // return sparse matrix | ||
3865 | 6449 | return c; | ||
3866 | 6450 | }; | ||
3867 | 6451 | |||
3868 | 6452 | multiply.toTex = '\\left(${args[0]}' + latex.operators['multiply'] + '${args[1]}\\right)'; | ||
3869 | 6453 | |||
3870 | 6454 | return multiply; | ||
3871 | 6455 | } | ||
3872 | 6456 | |||
3873 | 6457 | exports.name = 'multiply'; | ||
3874 | 6458 | exports.factory = factory; | ||
3875 | 6459 | |||
3876 | 6460 | |||
3877 | 6461 | /***/ }, | ||
3878 | 6462 | /* 41 */ | ||
3879 | 6463 | /***/ function(module, exports) { | ||
3880 | 6464 | |||
3881 | 6465 | 'use strict'; | ||
3882 | 6466 | |||
3883 | 6467 | function factory(type, config, load, typed) { | ||
3884 | 6468 | |||
3885 | 6469 | /** | ||
3886 | 6470 | * Multiply two scalar values, `x * y`. | ||
3887 | 6471 | * This function is meant for internal use: it is used by the public function | ||
3888 | 6472 | * `multiply` | ||
3889 | 6473 | * | ||
3890 | 6474 | * This function does not support collections (Array or Matrix), and does | ||
3891 | 6475 | * not validate the number of of inputs. | ||
3892 | 6476 | * | ||
3893 | 6477 | * @param {number | BigNumber | Fraction | Complex | Unit} x First value to multiply | ||
3894 | 6478 | * @param {number | BigNumber | Fraction | Complex} y Second value to multiply | ||
3895 | 6479 | * @return {number | BigNumber | Fraction | Complex | Unit} Multiplication of `x` and `y` | ||
3896 | 6480 | * @private | ||
3897 | 6481 | */ | ||
3898 | 6482 | var multiplyScalar = typed('multiplyScalar', { | ||
3899 | 6483 | |||
3900 | 6484 | 'number, number': function (x, y) { | ||
3901 | 6485 | return x * y; | ||
3902 | 6486 | }, | ||
3903 | 6487 | |||
3904 | 6488 | 'Complex, Complex': function (x, y) { | ||
3905 | 6489 | return new type.Complex( | ||
3906 | 6490 | x.re * y.re - x.im * y.im, | ||
3907 | 6491 | x.re * y.im + x.im * y.re | ||
3908 | 6492 | ); | ||
3909 | 6493 | }, | ||
3910 | 6494 | |||
3911 | 6495 | 'BigNumber, BigNumber': function (x, y) { | ||
3912 | 6496 | return x.times(y); | ||
3913 | 6497 | }, | ||
3914 | 6498 | |||
3915 | 6499 | 'Fraction, Fraction': function (x, y) { | ||
3916 | 6500 | return x.mul(y); | ||
3917 | 6501 | }, | ||
3918 | 6502 | |||
3919 | 6503 | 'number, Unit': function (x, y) { | ||
3920 | 6504 | var res = y.clone(); | ||
3921 | 6505 | res.value = (res.value === null) ? res._normalize(x) : (res.value * x); | ||
3922 | 6506 | return res; | ||
3923 | 6507 | }, | ||
3924 | 6508 | |||
3925 | 6509 | 'Unit, number': function (x, y) { | ||
3926 | 6510 | var res = x.clone(); | ||
3927 | 6511 | res.value = (res.value === null) ? res._normalize(y) : (res.value * y); | ||
3928 | 6512 | return res; | ||
3929 | 6513 | }, | ||
3930 | 6514 | |||
3931 | 6515 | 'Unit, Unit': function (x, y) { | ||
3932 | 6516 | return x.multiply(y); | ||
3933 | 6517 | } | ||
3934 | 6518 | |||
3935 | 6519 | }); | ||
3936 | 6520 | |||
3937 | 6521 | return multiplyScalar; | ||
3938 | 6522 | } | ||
3939 | 6523 | |||
3940 | 6524 | exports.factory = factory; | ||
3941 | 6525 | |||
3942 | 6526 | |||
3943 | 6527 | /***/ }, | ||
3944 | 6528 | /* 42 */ | ||
3945 | 6529 | /***/ function(module, exports, __webpack_require__) { | ||
3946 | 6530 | |||
3947 | 6531 | 'use strict'; | ||
3948 | 6532 | |||
3949 | 6533 | function factory (type, config, load, typed) { | ||
3950 | 6534 | |||
3951 | 6535 | var equalScalar = load(__webpack_require__(33)); | ||
3952 | 6536 | |||
3953 | 6537 | var SparseMatrix = type.SparseMatrix; | ||
3954 | 6538 | |||
3955 | 6539 | /** | ||
3956 | 6540 | * Iterates over SparseMatrix S nonzero items and invokes the callback function f(Sij, b). | ||
3957 | 6541 | * Callback function invoked NZ times (number of nonzero items in S). | ||
3958 | 6542 | * | ||
3959 | 6543 | * | ||
3960 | 6544 | * ┌ f(Sij, b) ; S(i,j) !== 0 | ||
3961 | 6545 | * C(i,j) = ┤ | ||
3962 | 6546 | * â”” 0 ; otherwise | ||
3963 | 6547 | * | ||
3964 | 6548 | * | ||
3965 | 6549 | * @param {Matrix} s The SparseMatrix instance (S) | ||
3966 | 6550 | * @param {Scalar} b The Scalar value | ||
3967 | 6551 | * @param {Function} callback The f(Aij,b) operation to invoke | ||
3968 | 6552 | * @param {boolean} inverse A true value indicates callback should be invoked f(b,Sij) | ||
3969 | 6553 | * | ||
3970 | 6554 | * @return {Matrix} SparseMatrix (C) | ||
3971 | 6555 | * | ||
3972 | 6556 | * https://github.com/josdejong/mathjs/pull/346#issuecomment-97626813 | ||
3973 | 6557 | */ | ||
3974 | 6558 | var algorithm11 = function (s, b, callback, inverse) { | ||
3975 | 6559 | // sparse matrix arrays | ||
3976 | 6560 | var avalues = s._values; | ||
3977 | 6561 | var aindex = s._index; | ||
3978 | 6562 | var aptr = s._ptr; | ||
3979 | 6563 | var asize = s._size; | ||
3980 | 6564 | var adt = s._datatype; | ||
3981 | 6565 | |||
3982 | 6566 | // sparse matrix cannot be a Pattern matrix | ||
3983 | 6567 | if (!avalues) | ||
3984 | 6568 | throw new Error('Cannot perform operation on Pattern Sparse Matrix and Scalar value'); | ||
3985 | 6569 | |||
3986 | 6570 | // rows & columns | ||
3987 | 6571 | var rows = asize[0]; | ||
3988 | 6572 | var columns = asize[1]; | ||
3989 | 6573 | |||
3990 | 6574 | // datatype | ||
3991 | 6575 | var dt; | ||
3992 | 6576 | // equal signature to use | ||
3993 | 6577 | var eq = equalScalar; | ||
3994 | 6578 | // zero value | ||
3995 | 6579 | var zero = 0; | ||
3996 | 6580 | // callback signature to use | ||
3997 | 6581 | var cf = callback; | ||
3998 | 6582 | |||
3999 | 6583 | // process data types | ||
4000 | 6584 | if (typeof adt === 'string') { | ||
4001 | 6585 | // datatype | ||
4002 | 6586 | dt = adt; | ||
4003 | 6587 | // find signature that matches (dt, dt) | ||
4004 | 6588 | eq = typed.find(equalScalar, [dt, dt]); | ||
4005 | 6589 | // convert 0 to the same datatype | ||
4006 | 6590 | zero = typed.convert(0, dt); | ||
4007 | 6591 | // convert b to the same datatype | ||
4008 | 6592 | b = typed.convert(b, dt); | ||
4009 | 6593 | // callback | ||
4010 | 6594 | cf = typed.find(callback, [dt, dt]); | ||
4011 | 6595 | } | ||
4012 | 6596 | |||
4013 | 6597 | // result arrays | ||
4014 | 6598 | var cvalues = []; | ||
4015 | 6599 | var cindex = []; | ||
4016 | 6600 | var cptr = []; | ||
4017 | 6601 | // matrix | ||
4018 | 6602 | var c = new SparseMatrix({ | ||
4019 | 6603 | values: cvalues, | ||
4020 | 6604 | index: cindex, | ||
4021 | 6605 | ptr: cptr, | ||
4022 | 6606 | size: [rows, columns], | ||
4023 | 6607 | datatype: dt | ||
4024 | 6608 | }); | ||
4025 | 6609 | |||
4026 | 6610 | // loop columns | ||
4027 | 6611 | for (var j = 0; j < columns; j++) { | ||
4028 | 6612 | // initialize ptr | ||
4029 | 6613 | cptr[j] = cindex.length; | ||
4030 | 6614 | // values in j | ||
4031 | 6615 | for (var k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { | ||
4032 | 6616 | // row | ||
4033 | 6617 | var i = aindex[k]; | ||
4034 | 6618 | // invoke callback | ||
4035 | 6619 | var v = inverse ? cf(b, avalues[k]) : cf(avalues[k], b); | ||
4036 | 6620 | // check value is zero | ||
4037 | 6621 | if (!eq(v, zero)) { | ||
4038 | 6622 | // push index & value | ||
4039 | 6623 | cindex.push(i); | ||
4040 | 6624 | cvalues.push(v); | ||
4041 | 6625 | } | ||
4042 | 6626 | } | ||
4043 | 6627 | } | ||
4044 | 6628 | // update ptr | ||
4045 | 6629 | cptr[columns] = cindex.length; | ||
4046 | 6630 | |||
4047 | 6631 | // return sparse matrix | ||
4048 | 6632 | return c; | ||
4049 | 6633 | }; | ||
4050 | 6634 | |||
4051 | 6635 | return algorithm11; | ||
4052 | 6636 | } | ||
4053 | 6637 | |||
4054 | 6638 | exports.name = 'algorithm11'; | ||
4055 | 6639 | exports.factory = factory; | ||
4056 | 6640 | |||
4057 | 6641 | |||
4058 | 6642 | /***/ }, | ||
4059 | 6643 | /* 43 */ | ||
4060 | 6644 | /***/ function(module, exports, __webpack_require__) { | ||
4061 | 6645 | |||
4062 | 6646 | 'use strict'; | ||
4063 | 6647 | |||
4064 | 6648 | var util = __webpack_require__(36); | ||
4065 | 6649 | var object = util.object; | ||
4066 | 6650 | var string = util.string; | ||
4067 | 6651 | |||
4068 | 6652 | function factory (type, config, load, typed) { | ||
4069 | 6653 | var matrix = load(__webpack_require__(23)); | ||
4070 | 6654 | var add = load(__webpack_require__(44)); | ||
4071 | 6655 | var subtract = load(__webpack_require__(25)); | ||
4072 | 6656 | var multiply = load(__webpack_require__(40)); | ||
4073 | 6657 | var unaryMinus = load(__webpack_require__(28)); | ||
4074 | 6658 | |||
4075 | 6659 | /** | ||
4076 | 6660 | * Calculate the determinant of a matrix. | ||
4077 | 6661 | * | ||
4078 | 6662 | * Syntax: | ||
4079 | 6663 | * | ||
4080 | 6664 | * math.det(x) | ||
4081 | 6665 | * | ||
4082 | 6666 | * Examples: | ||
4083 | 6667 | * | ||
4084 | 6668 | * math.det([[1, 2], [3, 4]]); // returns -2 | ||
4085 | 6669 | * | ||
4086 | 6670 | * var A = [ | ||
4087 | 6671 | * [-2, 2, 3], | ||
4088 | 6672 | * [-1, 1, 3], | ||
4089 | 6673 | * [2, 0, -1] | ||
4090 | 6674 | * ] | ||
4091 | 6675 | * math.det(A); // returns 6 | ||
4092 | 6676 | * | ||
4093 | 6677 | * See also: | ||
4094 | 6678 | * | ||
4095 | 6679 | * inv | ||
4096 | 6680 | * | ||
4097 | 6681 | * @param {Array | Matrix} x A matrix | ||
4098 | 6682 | * @return {number} The determinant of `x` | ||
4099 | 6683 | */ | ||
4100 | 6684 | var det = typed('det', { | ||
4101 | 6685 | 'any': function (x) { | ||
4102 | 6686 | return object.clone(x); | ||
4103 | 6687 | }, | ||
4104 | 6688 | |||
4105 | 6689 | 'Array | Matrix': function det (x) { | ||
4106 | 6690 | var size; | ||
4107 | 6691 | if (x && x.isMatrix === true) { | ||
4108 | 6692 | size = x.size(); | ||
4109 | 6693 | } | ||
4110 | 6694 | else if (Array.isArray(x)) { | ||
4111 | 6695 | x = matrix(x); | ||
4112 | 6696 | size = x.size(); | ||
4113 | 6697 | } | ||
4114 | 6698 | else { | ||
4115 | 6699 | // a scalar | ||
4116 | 6700 | size = []; | ||
4117 | 6701 | } | ||
4118 | 6702 | |||
4119 | 6703 | switch (size.length) { | ||
4120 | 6704 | case 0: | ||
4121 | 6705 | // scalar | ||
4122 | 6706 | return object.clone(x); | ||
4123 | 6707 | |||
4124 | 6708 | case 1: | ||
4125 | 6709 | // vector | ||
4126 | 6710 | if (size[0] == 1) { | ||
4127 | 6711 | return object.clone(x.valueOf()[0]); | ||
4128 | 6712 | } | ||
4129 | 6713 | else { | ||
4130 | 6714 | throw new RangeError('Matrix must be square ' + | ||
4131 | 6715 | '(size: ' + string.format(size) + ')'); | ||
4132 | 6716 | } | ||
4133 | 6717 | |||
4134 | 6718 | case 2: | ||
4135 | 6719 | // two dimensional array | ||
4136 | 6720 | var rows = size[0]; | ||
4137 | 6721 | var cols = size[1]; | ||
4138 | 6722 | if (rows == cols) { | ||
4139 | 6723 | return _det(x.clone().valueOf(), rows, cols); | ||
4140 | 6724 | } | ||
4141 | 6725 | else { | ||
4142 | 6726 | throw new RangeError('Matrix must be square ' + | ||
4143 | 6727 | '(size: ' + string.format(size) + ')'); | ||
4144 | 6728 | } | ||
4145 | 6729 | |||
4146 | 6730 | default: | ||
4147 | 6731 | // multi dimensional array | ||
4148 | 6732 | throw new RangeError('Matrix must be two dimensional ' + | ||
4149 | 6733 | '(size: ' + string.format(size) + ')'); | ||
4150 | 6734 | } | ||
4151 | 6735 | } | ||
4152 | 6736 | }); | ||
4153 | 6737 | |||
4154 | 6738 | det.toTex = '\\det\\left(${args[0]}\\right)'; | ||
4155 | 6739 | |||
4156 | 6740 | return det; | ||
4157 | 6741 | |||
4158 | 6742 | /** | ||
4159 | 6743 | * Calculate the determinant of a matrix | ||
4160 | 6744 | * @param {Array[]} matrix A square, two dimensional matrix | ||
4161 | 6745 | * @param {number} rows Number of rows of the matrix (zero-based) | ||
4162 | 6746 | * @param {number} cols Number of columns of the matrix (zero-based) | ||
4163 | 6747 | * @returns {number} det | ||
4164 | 6748 | * @private | ||
4165 | 6749 | */ | ||
4166 | 6750 | function _det (matrix, rows, cols) { | ||
4167 | 6751 | if (rows == 1) { | ||
4168 | 6752 | // this is a 1 x 1 matrix | ||
4169 | 6753 | return object.clone(matrix[0][0]); | ||
4170 | 6754 | } | ||
4171 | 6755 | else if (rows == 2) { | ||
4172 | 6756 | // this is a 2 x 2 matrix | ||
4173 | 6757 | // the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12 | ||
4174 | 6758 | return subtract( | ||
4175 | 6759 | multiply(matrix[0][0], matrix[1][1]), | ||
4176 | 6760 | multiply(matrix[1][0], matrix[0][1]) | ||
4177 | 6761 | ); | ||
4178 | 6762 | } | ||
4179 | 6763 | else { | ||
4180 | 6764 | // this is an n x n matrix | ||
4181 | 6765 | var compute_mu = function (matrix) { | ||
4182 | 6766 | var i, j; | ||
4183 | 6767 | |||
4184 | 6768 | // Compute the matrix with zero lower triangle, same upper triangle, | ||
4185 | 6769 | // and diagonals given by the negated sum of the below diagonal | ||
4186 | 6770 | // elements. | ||
4187 | 6771 | var mu = new Array(matrix.length); | ||
4188 | 6772 | var sum = 0; | ||
4189 | 6773 | for (i = 1; i < matrix.length; i++) { | ||
4190 | 6774 | sum = add(sum, matrix[i][i]); | ||
4191 | 6775 | } | ||
4192 | 6776 | |||
4193 | 6777 | for (i = 0; i < matrix.length; i++) { | ||
4194 | 6778 | mu[i] = new Array(matrix.length); | ||
4195 | 6779 | mu[i][i] = unaryMinus(sum); | ||
4196 | 6780 | |||
4197 | 6781 | for (j = 0; j < i; j++) { | ||
4198 | 6782 | mu[i][j] = 0; // TODO: make bignumber 0 in case of bignumber computation | ||
4199 | 6783 | } | ||
4200 | 6784 | |||
4201 | 6785 | for (j = i + 1; j < matrix.length; j++) { | ||
4202 | 6786 | mu[i][j] = matrix[i][j]; | ||
4203 | 6787 | } | ||
4204 | 6788 | |||
4205 | 6789 | if (i+1 < matrix.length) { | ||
4206 | 6790 | sum = subtract(sum, matrix[i + 1][i + 1]); | ||
4207 | 6791 | } | ||
4208 | 6792 | } | ||
4209 | 6793 | |||
4210 | 6794 | return mu; | ||
4211 | 6795 | }; | ||
4212 | 6796 | |||
4213 | 6797 | var fa = matrix; | ||
4214 | 6798 | for (var i = 0; i < rows - 1; i++) { | ||
4215 | 6799 | fa = multiply(compute_mu(fa), matrix); | ||
4216 | 6800 | } | ||
4217 | 6801 | |||
4218 | 6802 | if (rows % 2 == 0) { | ||
4219 | 6803 | return unaryMinus(fa[0][0]); | ||
4220 | 6804 | } else { | ||
4221 | 6805 | return fa[0][0]; | ||
4222 | 6806 | } | ||
4223 | 6807 | } | ||
4224 | 6808 | } | ||
4225 | 6809 | } | ||
4226 | 6810 | |||
4227 | 6811 | exports.name = 'det'; | ||
4228 | 6812 | exports.factory = factory; | ||
4229 | 6813 | |||
4230 | 6814 | |||
4231 | 6815 | |||
4232 | 6816 | /***/ }, | ||
4233 | 6817 | /* 44 */ | ||
4234 | 6818 | /***/ function(module, exports, __webpack_require__) { | ||
4235 | 6819 | |||
4236 | 6820 | 'use strict'; | ||
4237 | 6821 | |||
4238 | 6822 | var extend = __webpack_require__(5).extend; | ||
4239 | 6823 | |||
4240 | 6824 | function factory (type, config, load, typed) { | ||
4241 | 6825 | |||
4242 | 6826 | var matrix = load(__webpack_require__(23)); | ||
4243 | 6827 | var addScalar = load(__webpack_require__(27)); | ||
4244 | 6828 | var latex = __webpack_require__(26); | ||
4245 | 6829 | |||
4246 | 6830 | var algorithm01 = load(__webpack_require__(30)); | ||
4247 | 6831 | var algorithm04 = load(__webpack_require__(45)); | ||
4248 | 6832 | var algorithm10 = load(__webpack_require__(34)); | ||
4249 | 6833 | var algorithm13 = load(__webpack_require__(35)); | ||
4250 | 6834 | var algorithm14 = load(__webpack_require__(39)); | ||
4251 | 6835 | |||
4252 | 6836 | /** | ||
4253 | 6837 | * Add two values, `x + y`. | ||
4254 | 6838 | * For matrices, the function is evaluated element wise. | ||
4255 | 6839 | * | ||
4256 | 6840 | * Syntax: | ||
4257 | 6841 | * | ||
4258 | 6842 | * math.add(x, y) | ||
4259 | 6843 | * | ||
4260 | 6844 | * Examples: | ||
4261 | 6845 | * | ||
4262 | 6846 | * math.add(2, 3); // returns number 5 | ||
4263 | 6847 | * | ||
4264 | 6848 | * var a = math.complex(2, 3); | ||
4265 | 6849 | * var b = math.complex(-4, 1); | ||
4266 | 6850 | * math.add(a, b); // returns Complex -2 + 4i | ||
4267 | 6851 | * | ||
4268 | 6852 | * math.add([1, 2, 3], 4); // returns Array [5, 6, 7] | ||
4269 | 6853 | * | ||
4270 | 6854 | * var c = math.unit('5 cm'); | ||
4271 | 6855 | * var d = math.unit('2.1 mm'); | ||
4272 | 6856 | * math.add(c, d); // returns Unit 52.1 mm | ||
4273 | 6857 | * | ||
4274 | 6858 | * math.add("2.3", "4"); // returns number 6.3 | ||
4275 | 6859 | * | ||
4276 | 6860 | * See also: | ||
4277 | 6861 | * | ||
4278 | 6862 | * subtract | ||
4279 | 6863 | * | ||
4280 | 6864 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First value to add | ||
4281 | 6865 | * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second value to add | ||
4282 | 6866 | * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Sum of `x` and `y` | ||
4283 | 6867 | */ | ||
4284 | 6868 | var add = typed('add', extend({ | ||
4285 | 6869 | // we extend the signatures of addScalar with signatures dealing with matrices | ||
4286 | 6870 | |||
4287 | 6871 | 'Matrix, Matrix': function (x, y) { | ||
4288 | 6872 | // result | ||
4289 | 6873 | var c; | ||
4290 | 6874 | |||
4291 | 6875 | // process matrix storage | ||
4292 | 6876 | switch (x.storage()) { | ||
4293 | 6877 | case 'sparse': | ||
4294 | 6878 | switch (y.storage()) { | ||
4295 | 6879 | case 'sparse': | ||
4296 | 6880 | // sparse + sparse | ||
4297 | 6881 | c = algorithm04(x, y, addScalar); | ||
4298 | 6882 | break; | ||
4299 | 6883 | default: | ||
4300 | 6884 | // sparse + dense | ||
4301 | 6885 | c = algorithm01(y, x, addScalar, true); | ||
4302 | 6886 | break; | ||
4303 | 6887 | } | ||
4304 | 6888 | break; | ||
4305 | 6889 | default: | ||
4306 | 6890 | switch (y.storage()) { | ||
4307 | 6891 | case 'sparse': | ||
4308 | 6892 | // dense + sparse | ||
4309 | 6893 | c = algorithm01(x, y, addScalar, false); | ||
4310 | 6894 | break; | ||
4311 | 6895 | default: | ||
4312 | 6896 | // dense + dense | ||
4313 | 6897 | c = algorithm13(x, y, addScalar); | ||
4314 | 6898 | break; | ||
4315 | 6899 | } | ||
4316 | 6900 | break; | ||
4317 | 6901 | } | ||
4318 | 6902 | return c; | ||
4319 | 6903 | }, | ||
4320 | 6904 | |||
4321 | 6905 | 'Array, Array': function (x, y) { | ||
4322 | 6906 | // use matrix implementation | ||
4323 | 6907 | return add(matrix(x), matrix(y)).valueOf(); | ||
4324 | 6908 | }, | ||
4325 | 6909 | |||
4326 | 6910 | 'Array, Matrix': function (x, y) { | ||
4327 | 6911 | // use matrix implementation | ||
4328 | 6912 | return add(matrix(x), y); | ||
4329 | 6913 | }, | ||
4330 | 6914 | |||
4331 | 6915 | 'Matrix, Array': function (x, y) { | ||
4332 | 6916 | // use matrix implementation | ||
4333 | 6917 | return add(x, matrix(y)); | ||
4334 | 6918 | }, | ||
4335 | 6919 | |||
4336 | 6920 | 'Matrix, any': function (x, y) { | ||
4337 | 6921 | // result | ||
4338 | 6922 | var c; | ||
4339 | 6923 | // check storage format | ||
4340 | 6924 | switch (x.storage()) { | ||
4341 | 6925 | case 'sparse': | ||
4342 | 6926 | c = algorithm10(x, y, addScalar, false); | ||
4343 | 6927 | break; | ||
4344 | 6928 | default: | ||
4345 | 6929 | c = algorithm14(x, y, addScalar, false); | ||
4346 | 6930 | break; | ||
4347 | 6931 | } | ||
4348 | 6932 | return c; | ||
4349 | 6933 | }, | ||
4350 | 6934 | |||
4351 | 6935 | 'any, Matrix': function (x, y) { | ||
4352 | 6936 | // result | ||
4353 | 6937 | var c; | ||
4354 | 6938 | // check storage format | ||
4355 | 6939 | switch (y.storage()) { | ||
4356 | 6940 | case 'sparse': | ||
4357 | 6941 | c = algorithm10(y, x, addScalar, true); | ||
4358 | 6942 | break; | ||
4359 | 6943 | default: | ||
4360 | 6944 | c = algorithm14(y, x, addScalar, true); | ||
4361 | 6945 | break; | ||
4362 | 6946 | } | ||
4363 | 6947 | return c; | ||
4364 | 6948 | }, | ||
4365 | 6949 | |||
4366 | 6950 | 'Array, any': function (x, y) { | ||
4367 | 6951 | // use matrix implementation | ||
4368 | 6952 | return algorithm14(matrix(x), y, addScalar, false).valueOf(); | ||
4369 | 6953 | }, | ||
4370 | 6954 | |||
4371 | 6955 | 'any, Array': function (x, y) { | ||
4372 | 6956 | // use matrix implementation | ||
4373 | 6957 | return algorithm14(matrix(y), x, addScalar, true).valueOf(); | ||
4374 | 6958 | } | ||
4375 | 6959 | }, addScalar.signatures)); | ||
4376 | 6960 | |||
4377 | 6961 | add.toTex = '\\left(${args[0]}' + latex.operators['add'] + '${args[1]}\\right)'; | ||
4378 | 6962 | |||
4379 | 6963 | return add; | ||
4380 | 6964 | } | ||
4381 | 6965 | |||
4382 | 6966 | exports.name = 'add'; | ||
4383 | 6967 | exports.factory = factory; | ||
4384 | 6968 | |||
4385 | 6969 | |||
4386 | 6970 | /***/ }, | ||
4387 | 6971 | /* 45 */ | ||
4388 | 6972 | /***/ function(module, exports, __webpack_require__) { | ||
4389 | 6973 | |||
4390 | 6974 | 'use strict'; | ||
4391 | 6975 | |||
4392 | 6976 | var DimensionError = __webpack_require__(22); | ||
4393 | 6977 | |||
4394 | 6978 | function factory (type, config, load, typed) { | ||
4395 | 6979 | |||
4396 | 6980 | var equalScalar = load(__webpack_require__(33)); | ||
4397 | 6981 | |||
4398 | 6982 | var SparseMatrix = type.SparseMatrix; | ||
4399 | 6983 | |||
4400 | 6984 | /** | ||
4401 | 6985 | * Iterates over SparseMatrix A and SparseMatrix B nonzero items and invokes the callback function f(Aij, Bij). | ||
4402 | 6986 | * Callback function invoked MAX(NNZA, NNZB) times | ||
4403 | 6987 | * | ||
4404 | 6988 | * | ||
4405 | 6989 | * ┌ f(Aij, Bij) ; A(i,j) !== 0 && B(i,j) !== 0 | ||
4406 | 6990 | * C(i,j) = ┤ A(i,j) ; A(i,j) !== 0 | ||
4407 | 6991 | * â”” B(i,j) ; B(i,j) !== 0 | ||
4408 | 6992 | * | ||
4409 | 6993 | * | ||
4410 | 6994 | * @param {Matrix} a The SparseMatrix instance (A) | ||
4411 | 6995 | * @param {Matrix} b The SparseMatrix instance (B) | ||
4412 | 6996 | * @param {Function} callback The f(Aij,Bij) operation to invoke | ||
4413 | 6997 | * | ||
4414 | 6998 | * @return {Matrix} SparseMatrix (C) | ||
4415 | 6999 | * | ||
4416 | 7000 | * see https://github.com/josdejong/mathjs/pull/346#issuecomment-97620294 | ||
4417 | 7001 | */ | ||
4418 | 7002 | var algorithm04 = function (a, b, callback) { | ||
4419 | 7003 | // sparse matrix arrays | ||
4420 | 7004 | var avalues = a._values; | ||
4421 | 7005 | var aindex = a._index; | ||
4422 | 7006 | var aptr = a._ptr; | ||
4423 | 7007 | var asize = a._size; | ||
4424 | 7008 | var adt = a._datatype; | ||
4425 | 7009 | // sparse matrix arrays | ||
4426 | 7010 | var bvalues = b._values; | ||
4427 | 7011 | var bindex = b._index; | ||
4428 | 7012 | var bptr = b._ptr; | ||
4429 | 7013 | var bsize = b._size; | ||
4430 | 7014 | var bdt = b._datatype; | ||
4431 | 7015 | |||
4432 | 7016 | // validate dimensions | ||
4433 | 7017 | if (asize.length !== bsize.length) | ||
4434 | 7018 | throw new DimensionError(asize.length, bsize.length); | ||
4435 | 7019 | |||
4436 | 7020 | // check rows & columns | ||
4437 | 7021 | if (asize[0] !== bsize[0] || asize[1] !== bsize[1]) | ||
4438 | 7022 | throw new RangeError('Dimension mismatch. Matrix A (' + asize + ') must match Matrix B (' + bsize + ')'); | ||
4439 | 7023 | |||
4440 | 7024 | // rows & columns | ||
4441 | 7025 | var rows = asize[0]; | ||
4442 | 7026 | var columns = asize[1]; | ||
4443 | 7027 | |||
4444 | 7028 | // datatype | ||
4445 | 7029 | var dt; | ||
4446 | 7030 | // equal signature to use | ||
4447 | 7031 | var eq = equalScalar; | ||
4448 | 7032 | // zero value | ||
4449 | 7033 | var zero = 0; | ||
4450 | 7034 | // callback signature to use | ||
4451 | 7035 | var cf = callback; | ||
4452 | 7036 | |||
4453 | 7037 | // process data types | ||
4454 | 7038 | if (typeof adt === 'string' && adt === bdt) { | ||
4455 | 7039 | // datatype | ||
4456 | 7040 | dt = adt; | ||
4457 | 7041 | // find signature that matches (dt, dt) | ||
4458 | 7042 | eq = typed.find(equalScalar, [dt, dt]); | ||
4459 | 7043 | // convert 0 to the same datatype | ||
4460 | 7044 | zero = typed.convert(0, dt); | ||
4461 | 7045 | // callback | ||
4462 | 7046 | cf = typed.find(callback, [dt, dt]); | ||
4463 | 7047 | } | ||
4464 | 7048 | |||
4465 | 7049 | // result arrays | ||
4466 | 7050 | var cvalues = avalues && bvalues ? [] : undefined; | ||
4467 | 7051 | var cindex = []; | ||
4468 | 7052 | var cptr = []; | ||
4469 | 7053 | // matrix | ||
4470 | 7054 | var c = new SparseMatrix({ | ||
4471 | 7055 | values: cvalues, | ||
4472 | 7056 | index: cindex, | ||
4473 | 7057 | ptr: cptr, | ||
4474 | 7058 | size: [rows, columns], | ||
4475 | 7059 | datatype: dt | ||
4476 | 7060 | }); | ||
4477 | 7061 | |||
4478 | 7062 | // workspace | ||
4479 | 7063 | var xa = avalues && bvalues ? [] : undefined; | ||
4480 | 7064 | var xb = avalues && bvalues ? [] : undefined; | ||
4481 | 7065 | // marks indicating we have a value in x for a given column | ||
4482 | 7066 | var wa = []; | ||
4483 | 7067 | var wb = []; | ||
4484 | 7068 | |||
4485 | 7069 | // vars | ||
4486 | 7070 | var i, j, k, k0, k1; | ||
4487 | 7071 | |||
4488 | 7072 | // loop columns | ||
4489 | 7073 | for (j = 0; j < columns; j++) { | ||
4490 | 7074 | // update cptr | ||
4491 | 7075 | cptr[j] = cindex.length; | ||
4492 | 7076 | // columns mark | ||
4493 | 7077 | var mark = j + 1; | ||
4494 | 7078 | // loop A(:,j) | ||
4495 | 7079 | for (k0 = aptr[j], k1 = aptr[j + 1], k = k0; k < k1; k++) { | ||
4496 | 7080 | // row | ||
4497 | 7081 | i = aindex[k]; | ||
4498 | 7082 | // update c | ||
4499 | 7083 | cindex.push(i); | ||
4500 | 7084 | // update workspace | ||
4501 | 7085 | wa[i] = mark; | ||
4502 | 7086 | // check we need to process values | ||
4503 | 7087 | if (xa) | ||
4504 | 7088 | xa[i] = avalues[k]; | ||
4505 | 7089 | } | ||
4506 | 7090 | // loop B(:,j) | ||
4507 | 7091 | for (k0 = bptr[j], k1 = bptr[j + 1], k = k0; k < k1; k++) { | ||
4508 | 7092 | // row | ||
4509 | 7093 | i = bindex[k]; | ||
4510 | 7094 | // check row exists in A | ||
4511 | 7095 | if (wa[i] === mark) { | ||
4512 | 7096 | // update record in xa @ i | ||
4513 | 7097 | if (xa) { | ||
4514 | 7098 | // invoke callback | ||
4515 | 7099 | var v = cf(xa[i], bvalues[k]); | ||
4516 | 7100 | // check for zero | ||
4517 | 7101 | if (!eq(v, zero)) { | ||
4518 | 7102 | // update workspace | ||
4519 | 7103 | xa[i] = v; | ||
4520 | 7104 | } | ||
4521 | 7105 | else { | ||
4522 | 7106 | // remove mark (index will be removed later) | ||
4523 | 7107 | wa[i] = null; | ||
4524 | 7108 | } | ||
4525 | 7109 | } | ||
4526 | 7110 | } | ||
4527 | 7111 | else { | ||
4528 | 7112 | // update c | ||
4529 | 7113 | cindex.push(i); | ||
4530 | 7114 | // update workspace | ||
4531 | 7115 | wb[i] = mark; | ||
4532 | 7116 | // check we need to process values | ||
4533 | 7117 | if (xb) | ||
4534 | 7118 | xb[i] = bvalues[k]; | ||
4535 | 7119 | } | ||
4536 | 7120 | } | ||
4537 | 7121 | // check we need to process values (non pattern matrix) | ||
4538 | 7122 | if (xa && xb) { | ||
4539 | 7123 | // initialize first index in j | ||
4540 | 7124 | k = cptr[j]; | ||
4541 | 7125 | // loop index in j | ||
4542 | 7126 | while (k < cindex.length) { | ||
4543 | 7127 | // row | ||
4544 | 7128 | i = cindex[k]; | ||
4545 | 7129 | // check workspace has value @ i | ||
4546 | 7130 | if (wa[i] === mark) { | ||
4547 | 7131 | // push value (Aij != 0 || (Aij != 0 && Bij != 0)) | ||
4548 | 7132 | cvalues[k] = xa[i]; | ||
4549 | 7133 | // increment pointer | ||
4550 | 7134 | k++; | ||
4551 | 7135 | } | ||
4552 | 7136 | else if (wb[i] === mark) { | ||
4553 | 7137 | // push value (bij != 0) | ||
4554 | 7138 | cvalues[k] = xb[i]; | ||
4555 | 7139 | // increment pointer | ||
4556 | 7140 | k++; | ||
4557 | 7141 | } | ||
4558 | 7142 | else { | ||
4559 | 7143 | // remove index @ k | ||
4560 | 7144 | cindex.splice(k, 1); | ||
4561 | 7145 | } | ||
4562 | 7146 | } | ||
4563 | 7147 | } | ||
4564 | 7148 | } | ||
4565 | 7149 | // update cptr | ||
4566 | 7150 | cptr[columns] = cindex.length; | ||
4567 | 7151 | |||
4568 | 7152 | // return sparse matrix | ||
4569 | 7153 | return c; | ||
4570 | 7154 | }; | ||
4571 | 7155 | |||
4572 | 7156 | return algorithm04; | ||
4573 | 7157 | } | ||
4574 | 7158 | |||
4575 | 7159 | exports.name = 'algorithm04'; | ||
4576 | 7160 | exports.factory = factory; | ||
4577 | 7161 | |||
4578 | 7162 | |||
4579 | 7163 | /***/ }, | ||
4580 | 7164 | /* 46 */ | ||
4581 | 7165 | /***/ function(module, exports, __webpack_require__) { | ||
4582 | 7166 | |||
4583 | 7167 | 'use strict'; | ||
4584 | 7168 | |||
4585 | 7169 | var array = __webpack_require__(18); | ||
4586 | 7170 | var clone = __webpack_require__(5).clone; | ||
4587 | 7171 | var isInteger = __webpack_require__(8).isInteger; | ||
4588 | 7172 | |||
4589 | 7173 | function factory (type, config, load, typed) { | ||
4590 | 7174 | |||
4591 | 7175 | var matrix = load(__webpack_require__(23)); | ||
4592 | 7176 | |||
4593 | 7177 | /** | ||
4594 | 7178 | * Create a diagonal matrix or retrieve the diagonal of a matrix | ||
4595 | 7179 | * | ||
4596 | 7180 | * When `x` is a vector, a matrix with vector `x` on the diagonal will be returned. | ||
4597 | 7181 | * When `x` is a two dimensional matrix, the matrixes `k`th diagonal will be returned as vector. | ||
4598 | 7182 | * When k is positive, the values are placed on the super diagonal. | ||
4599 | 7183 | * When k is negative, the values are placed on the sub diagonal. | ||
4600 | 7184 | * | ||
4601 | 7185 | * Syntax: | ||
4602 | 7186 | * | ||
4603 | 7187 | * math.diag(X) | ||
4604 | 7188 | * math.diag(X, format) | ||
4605 | 7189 | * math.diag(X, k) | ||
4606 | 7190 | * math.diag(X, k, format) | ||
4607 | 7191 | * | ||
4608 | 7192 | * Examples: | ||
4609 | 7193 | * | ||
4610 | 7194 | * // create a diagonal matrix | ||
4611 | 7195 | * math.diag([1, 2, 3]); // returns [[1, 0, 0], [0, 2, 0], [0, 0, 3]] | ||
4612 | 7196 | * math.diag([1, 2, 3], 1); // returns [[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 3]] | ||
4613 | 7197 | * math.diag([1, 2, 3], -1); // returns [[0, 0, 0], [1, 0, 0], [0, 2, 0], [0, 0, 3]] | ||
4614 | 7198 | * | ||
4615 | 7199 | * // retrieve the diagonal from a matrix | ||
4616 | 7200 | * var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | ||
4617 | 7201 | * math.diag(a); // returns [1, 5, 9] | ||
4618 | 7202 | * | ||
4619 | 7203 | * See also: | ||
4620 | 7204 | * | ||
4621 | 7205 | * ones, zeros, eye | ||
4622 | 7206 | * | ||
4623 | 7207 | * @param {Matrix | Array} x A two dimensional matrix or a vector | ||
4624 | 7208 | * @param {number | BigNumber} [k=0] The diagonal where the vector will be filled | ||
4625 | 7209 | * in or retrieved. | ||
4626 | 7210 | * @param {string} [format='dense'] The matrix storage format. | ||
4627 | 7211 | * | ||
4628 | 7212 | * @returns {Matrix | Array} Diagonal matrix from input vector, or diagonal from input matrix. | ||
4629 | 7213 | */ | ||
4630 | 7214 | var diag = typed('diag', { | ||
4631 | 7215 | // FIXME: simplify this huge amount of signatures as soon as typed-function supports optional arguments | ||
4632 | 7216 | |||
4633 | 7217 | 'Array': function (x) { | ||
4634 | 7218 | return _diag(x, 0, array.size(x), null); | ||
4635 | 7219 | }, | ||
4636 | 7220 | |||
4637 | 7221 | 'Array, number': function (x, k) { | ||
4638 | 7222 | return _diag(x, k, array.size(x), null); | ||
4639 | 7223 | }, | ||
4640 | 7224 | |||
4641 | 7225 | 'Array, BigNumber': function (x, k) { | ||
4642 | 7226 | return _diag(x, k.toNumber(), array.size(x), null); | ||
4643 | 7227 | }, | ||
4644 | 7228 | |||
4645 | 7229 | 'Array, string': function (x, format) { | ||
4646 | 7230 | return _diag(x, 0, array.size(x), format); | ||
4647 | 7231 | }, | ||
4648 | 7232 | |||
4649 | 7233 | 'Array, number, string': function (x, k, format) { | ||
4650 | 7234 | return _diag(x, k, array.size(x), format); | ||
4651 | 7235 | }, | ||
4652 | 7236 | |||
4653 | 7237 | 'Array, BigNumber, string': function (x, k, format) { | ||
4654 | 7238 | return _diag(x, k.toNumber(), array.size(x), format); | ||
4655 | 7239 | }, | ||
4656 | 7240 | |||
4657 | 7241 | 'Matrix': function (x) { | ||
4658 | 7242 | return _diag(x, 0, x.size(), x.storage()); | ||
4659 | 7243 | }, | ||
4660 | 7244 | |||
4661 | 7245 | 'Matrix, number': function (x, k) { | ||
4662 | 7246 | return _diag(x, k, x.size(), x.storage()); | ||
4663 | 7247 | }, | ||
4664 | 7248 | |||
4665 | 7249 | 'Matrix, BigNumber': function (x, k) { | ||
4666 | 7250 | return _diag(x, k.toNumber(), x.size(), x.storage()); | ||
4667 | 7251 | }, | ||
4668 | 7252 | |||
4669 | 7253 | 'Matrix, string': function (x, format) { | ||
4670 | 7254 | return _diag(x, 0, x.size(), format); | ||
4671 | 7255 | }, | ||
4672 | 7256 | |||
4673 | 7257 | 'Matrix, number, string': function (x, k, format) { | ||
4674 | 7258 | return _diag(x, k, x.size(), format); | ||
4675 | 7259 | }, | ||
4676 | 7260 | |||
4677 | 7261 | 'Matrix, BigNumber, string': function (x, k, format) { | ||
4678 | 7262 | return _diag(x, k.toNumber(), x.size(), format); | ||
4679 | 7263 | } | ||
4680 | 7264 | }); | ||
4681 | 7265 | |||
4682 | 7266 | diag.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; | ||
4683 | 7267 | |||
4684 | 7268 | return diag; | ||
4685 | 7269 | |||
4686 | 7270 | /** | ||
4687 | 7271 | * Creeate diagonal matrix from a vector or vice versa | ||
4688 | 7272 | * @param {Array | Matrix} x | ||
4689 | 7273 | * @param {number} k | ||
4690 | 7274 | * @param {string} format Storage format for matrix. If null, | ||
4691 | 7275 | * an Array is returned | ||
4692 | 7276 | * @returns {Array | Matrix} | ||
4693 | 7277 | * @private | ||
4694 | 7278 | */ | ||
4695 | 7279 | function _diag (x, k, size, format) { | ||
4696 | 7280 | if (!isInteger(k)) { | ||
4697 | 7281 | throw new TypeError ('Second parameter in function diag must be an integer'); | ||
4698 | 7282 | } | ||
4699 | 7283 | |||
4700 | 7284 | var kSuper = k > 0 ? k : 0; | ||
4701 | 7285 | var kSub = k < 0 ? -k : 0; | ||
4702 | 7286 | |||
4703 | 7287 | // check dimensions | ||
4704 | 7288 | switch (size.length) { | ||
4705 | 7289 | case 1: | ||
4706 | 7290 | return _createDiagonalMatrix(x, k, format, size[0], kSub, kSuper); | ||
4707 | 7291 | case 2: | ||
4708 | 7292 | return _getDiagonal(x, k, format, size, kSub, kSuper); | ||
4709 | 7293 | } | ||
4710 | 7294 | throw new RangeError('Matrix for function diag must be 2 dimensional'); | ||
4711 | 7295 | } | ||
4712 | 7296 | |||
4713 | 7297 | function _createDiagonalMatrix(x, k, format, l, kSub, kSuper) { | ||
4714 | 7298 | // matrix size | ||
4715 | 7299 | var ms = [l + kSub, l + kSuper]; | ||
4716 | 7300 | // get matrix constructor | ||
4717 | 7301 | var F = type.Matrix.storage(format || 'dense'); | ||
4718 | 7302 | // create diagonal matrix | ||
4719 | 7303 | var m = F.diagonal(ms, x, k); | ||
4720 | 7304 | // check we need to return a matrix | ||
4721 | 7305 | return format !== null ? m : m.valueOf(); | ||
4722 | 7306 | } | ||
4723 | 7307 | |||
4724 | 7308 | function _getDiagonal(x, k, format, s, kSub, kSuper) { | ||
4725 | 7309 | // check x is a Matrix | ||
4726 | 7310 | if (x && x.isMatrix === true) { | ||
4727 | 7311 | // get diagonal matrix | ||
4728 | 7312 | var dm = x.diagonal(k); | ||
4729 | 7313 | // check we need to return a matrix | ||
4730 | 7314 | if (format !== null) { | ||
4731 | 7315 | // check we need to change matrix format | ||
4732 | 7316 | if (format !== dm.storage()) | ||
4733 | 7317 | return matrix(dm, format); | ||
4734 | 7318 | return dm; | ||
4735 | 7319 | } | ||
4736 | 7320 | return dm.valueOf(); | ||
4737 | 7321 | } | ||
4738 | 7322 | // vector size | ||
4739 | 7323 | var n = Math.min(s[0] - kSub, s[1] - kSuper); | ||
4740 | 7324 | // diagonal values | ||
4741 | 7325 | var vector = []; | ||
4742 | 7326 | // loop diagonal | ||
4743 | 7327 | for (var i = 0; i < n; i++) { | ||
4744 | 7328 | vector[i] = clone(x[i + kSub][i + kSuper]); | ||
4745 | 7329 | } | ||
4746 | 7330 | // check we need to return a matrix | ||
4747 | 7331 | return format !== null ? matrix(vector) : vector; | ||
4748 | 7332 | } | ||
4749 | 7333 | } | ||
4750 | 7334 | |||
4751 | 7335 | exports.name = 'diag'; | ||
4752 | 7336 | exports.factory = factory; | ||
4753 | 7337 | |||
4754 | 7338 | |||
4755 | 7339 | /***/ }, | ||
4756 | 7340 | /* 47 */ | ||
4757 | 7341 | /***/ function(module, exports, __webpack_require__) { | ||
4758 | 7342 | |||
4759 | 7343 | 'use strict'; | ||
4760 | 7344 | |||
4761 | 7345 | var size = __webpack_require__(18).size; | ||
4762 | 7346 | |||
4763 | 7347 | function factory (type, config, load, typed) { | ||
4764 | 7348 | var add = load(__webpack_require__(44)); | ||
4765 | 7349 | var multiply = load(__webpack_require__(40)); | ||
4766 | 7350 | |||
4767 | 7351 | /** | ||
4768 | 7352 | * Calculate the dot product of two vectors. The dot product of | ||
4769 | 7353 | * `A = [a1, a2, a3, ..., an]` and `B = [b1, b2, b3, ..., bn]` is defined as: | ||
4770 | 7354 | * | ||
4771 | 7355 | * dot(A, B) = a1 * b1 + a2 * b2 + a3 * b3 + ... + an * bn | ||
4772 | 7356 | * | ||
4773 | 7357 | * Syntax: | ||
4774 | 7358 | * | ||
4775 | 7359 | * math.dot(x, y) | ||
4776 | 7360 | * | ||
4777 | 7361 | * Examples: | ||
4778 | 7362 | * | ||
4779 | 7363 | * math.dot([2, 4, 1], [2, 2, 3]); // returns number 15 | ||
4780 | 7364 | * math.multiply([2, 4, 1], [2, 2, 3]); // returns number 15 | ||
4781 | 7365 | * | ||
4782 | 7366 | * See also: | ||
4783 | 7367 | * | ||
4784 | 7368 | * multiply, cross | ||
4785 | 7369 | * | ||
4786 | 7370 | * @param {Array | Matrix} x First vector | ||
4787 | 7371 | * @param {Array | Matrix} y Second vector | ||
4788 | 7372 | * @return {number} Returns the dot product of `x` and `y` | ||
4789 | 7373 | */ | ||
4790 | 7374 | var dot = typed('dot', { | ||
4791 | 7375 | 'Matrix, Matrix': function (x, y) { | ||
4792 | 7376 | return _dot(x.toArray(), y.toArray()); | ||
4793 | 7377 | }, | ||
4794 | 7378 | |||
4795 | 7379 | 'Matrix, Array': function (x, y) { | ||
4796 | 7380 | return _dot(x.toArray(), y); | ||
4797 | 7381 | }, | ||
4798 | 7382 | |||
4799 | 7383 | 'Array, Matrix': function (x, y) { | ||
4800 | 7384 | return _dot(x, y.toArray()); | ||
4801 | 7385 | }, | ||
4802 | 7386 | |||
4803 | 7387 | 'Array, Array': _dot | ||
4804 | 7388 | }); | ||
4805 | 7389 | |||
4806 | 7390 | dot.toTex = '\\left(${args[0]}\\cdot${args[1]}\\right)'; | ||
4807 | 7391 | |||
4808 | 7392 | return dot; | ||
4809 | 7393 | |||
4810 | 7394 | /** | ||
4811 | 7395 | * Calculate the dot product for two arrays | ||
4812 | 7396 | * @param {Array} x First vector | ||
4813 | 7397 | * @param {Array} y Second vector | ||
4814 | 7398 | * @returns {number} Returns the dot product of x and y | ||
4815 | 7399 | * @private | ||
4816 | 7400 | */ | ||
4817 | 7401 | // TODO: double code with math.multiply | ||
4818 | 7402 | function _dot(x, y) { | ||
4819 | 7403 | var xSize= size(x); | ||
4820 | 7404 | var ySize = size(y); | ||
4821 | 7405 | var len = xSize[0]; | ||
4822 | 7406 | |||
4823 | 7407 | if (xSize.length !== 1 || ySize.length !== 1) throw new RangeError('Vector expected'); // TODO: better error message | ||
4824 | 7408 | if (xSize[0] != ySize[0]) throw new RangeError('Vectors must have equal length (' + xSize[0] + ' != ' + ySize[0] + ')'); | ||
4825 | 7409 | if (len == 0) throw new RangeError('Cannot calculate the dot product of empty vectors'); | ||
4826 | 7410 | |||
4827 | 7411 | var prod = 0; | ||
4828 | 7412 | for (var i = 0; i < len; i++) { | ||
4829 | 7413 | prod = add(prod, multiply(x[i], y[i])); | ||
4830 | 7414 | } | ||
4831 | 7415 | |||
4832 | 7416 | return prod; | ||
4833 | 7417 | } | ||
4834 | 7418 | } | ||
4835 | 7419 | |||
4836 | 7420 | exports.name = 'dot'; | ||
4837 | 7421 | exports.factory = factory; | ||
4838 | 7422 | |||
4839 | 7423 | |||
4840 | 7424 | /***/ }, | ||
4841 | 7425 | /* 48 */ | ||
4842 | 7426 | /***/ function(module, exports, __webpack_require__) { | ||
4843 | 7427 | |||
4844 | 7428 | 'use strict'; | ||
4845 | 7429 | |||
4846 | 7430 | var array = __webpack_require__(18); | ||
4847 | 7431 | var isInteger = __webpack_require__(8).isInteger; | ||
4848 | 7432 | |||
4849 | 7433 | function factory (type, config, load, typed) { | ||
4850 | 7434 | |||
4851 | 7435 | var matrix = load(__webpack_require__(23)); | ||
4852 | 7436 | |||
4853 | 7437 | /** | ||
4854 | 7438 | * Create a 2-dimensional identity matrix with size m x n or n x n. | ||
4855 | 7439 | * The matrix has ones on the diagonal and zeros elsewhere. | ||
4856 | 7440 | * | ||
4857 | 7441 | * Syntax: | ||
4858 | 7442 | * | ||
4859 | 7443 | * math.eye(n) | ||
4860 | 7444 | * math.eye(n, format) | ||
4861 | 7445 | * math.eye(m, n) | ||
4862 | 7446 | * math.eye(m, n, format) | ||
4863 | 7447 | * math.eye([m, n]) | ||
4864 | 7448 | * math.eye([m, n], format) | ||
4865 | 7449 | * | ||
4866 | 7450 | * Examples: | ||
4867 | 7451 | * | ||
4868 | 7452 | * math.eye(3); // returns [[1, 0, 0], [0, 1, 0], [0, 0, 1]] | ||
4869 | 7453 | * math.eye(3, 2); // returns [[1, 0], [0, 1], [0, 0]] | ||
4870 | 7454 | * | ||
4871 | 7455 | * var A = [[1, 2, 3], [4, 5, 6]]; | ||
4872 | 7456 | * math.eye(math.size(b)); // returns [[1, 0, 0], [0, 1, 0]] | ||
4873 | 7457 | * | ||
4874 | 7458 | * See also: | ||
4875 | 7459 | * | ||
4876 | 7460 | * diag, ones, zeros, size, range | ||
4877 | 7461 | * | ||
4878 | 7462 | * @param {...number | Matrix | Array} size The size for the matrix | ||
4879 | 7463 | * @param {string} [format] The Matrix storage format | ||
4880 | 7464 | * | ||
4881 | 7465 | * @return {Matrix | Array | number} A matrix with ones on the diagonal. | ||
4882 | 7466 | */ | ||
4883 | 7467 | var eye = typed('eye', { | ||
4884 | 7468 | '': function () { | ||
4885 | 7469 | return (config.matrix === 'matrix') ? matrix([]) : []; | ||
4886 | 7470 | }, | ||
4887 | 7471 | |||
4888 | 7472 | 'string': function (format) { | ||
4889 | 7473 | return matrix(format); | ||
4890 | 7474 | }, | ||
4891 | 7475 | |||
4892 | 7476 | 'number | BigNumber': function (rows) { | ||
4893 | 7477 | return _eye(rows, rows, config.matrix === 'matrix' ? 'default' : undefined); | ||
4894 | 7478 | }, | ||
4895 | 7479 | |||
4896 | 7480 | 'number | BigNumber, string': function (rows, format) { | ||
4897 | 7481 | return _eye(rows, rows, format); | ||
4898 | 7482 | }, | ||
4899 | 7483 | |||
4900 | 7484 | 'number | BigNumber, number | BigNumber': function (rows, cols) { | ||
4901 | 7485 | return _eye(rows, cols, config.matrix === 'matrix' ? 'default' : undefined); | ||
4902 | 7486 | }, | ||
4903 | 7487 | |||
4904 | 7488 | 'number | BigNumber, number | BigNumber, string': function (rows, cols, format) { | ||
4905 | 7489 | return _eye(rows, cols, format); | ||
4906 | 7490 | }, | ||
4907 | 7491 | |||
4908 | 7492 | 'Array': function (size) { | ||
4909 | 7493 | return _eyeVector(size); | ||
4910 | 7494 | }, | ||
4911 | 7495 | |||
4912 | 7496 | 'Array, string': function (size, format) { | ||
4913 | 7497 | return _eyeVector(size, format); | ||
4914 | 7498 | }, | ||
4915 | 7499 | |||
4916 | 7500 | 'Matrix': function (size) { | ||
4917 | 7501 | return _eyeVector(size.valueOf(), size.storage()); | ||
4918 | 7502 | }, | ||
4919 | 7503 | |||
4920 | 7504 | 'Matrix, string': function (size, format) { | ||
4921 | 7505 | return _eyeVector(size.valueOf(), format); | ||
4922 | 7506 | } | ||
4923 | 7507 | }); | ||
4924 | 7508 | |||
4925 | 7509 | eye.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; | ||
4926 | 7510 | |||
4927 | 7511 | return eye; | ||
4928 | 7512 | |||
4929 | 7513 | function _eyeVector (size, format) { | ||
4930 | 7514 | switch (size.length) { | ||
4931 | 7515 | case 0: return format ? matrix(format) : []; | ||
4932 | 7516 | case 1: return _eye(size[0], size[0], format); | ||
4933 | 7517 | case 2: return _eye(size[0], size[1], format); | ||
4934 | 7518 | default: throw new Error('Vector containing two values expected'); | ||
4935 | 7519 | } | ||
4936 | 7520 | } | ||
4937 | 7521 | |||
4938 | 7522 | /** | ||
4939 | 7523 | * Create an identity matrix | ||
4940 | 7524 | * @param {number | BigNumber} rows | ||
4941 | 7525 | * @param {number | BigNumber} cols | ||
4942 | 7526 | * @param {string} [format] | ||
4943 | 7527 | * @returns {Matrix} | ||
4944 | 7528 | * @private | ||
4945 | 7529 | */ | ||
4946 | 7530 | function _eye (rows, cols, format) { | ||
4947 | 7531 | // BigNumber constructor with the right precision | ||
4948 | 7532 | var Big = (rows && rows.isBigNumber === true) | ||
4949 | 7533 | ? type.BigNumber | ||
4950 | 7534 | : (cols && cols.isBigNumber === true) | ||
4951 | 7535 | ? type.BigNumber | ||
4952 | 7536 | : null; | ||
4953 | 7537 | |||
4954 | 7538 | if (rows && rows.isBigNumber === true) rows = rows.toNumber(); | ||
4955 | 7539 | if (cols && cols.isBigNumber === true) cols = cols.toNumber(); | ||
4956 | 7540 | |||
4957 | 7541 | if (!isInteger(rows) || rows < 1) { | ||
4958 | 7542 | throw new Error('Parameters in function eye must be positive integers'); | ||
4959 | 7543 | } | ||
4960 | 7544 | if (!isInteger(cols) || cols < 1) { | ||
4961 | 7545 | throw new Error('Parameters in function eye must be positive integers'); | ||
4962 | 7546 | } | ||
4963 | 7547 | |||
4964 | 7548 | var one = Big ? new type.BigNumber(1) : 1; | ||
4965 | 7549 | var defaultValue = Big ? new Big(0) : 0; | ||
4966 | 7550 | var size = [rows, cols]; | ||
4967 | 7551 | |||
4968 | 7552 | // check we need to return a matrix | ||
4969 | 7553 | if (format) { | ||
4970 | 7554 | // get matrix storage constructor | ||
4971 | 7555 | var F = type.Matrix.storage(format); | ||
4972 | 7556 | // create diagonal matrix (use optimized implementation for storage format) | ||
4973 | 7557 | return F.diagonal(size, one, 0, defaultValue); | ||
4974 | 7558 | } | ||
4975 | 7559 | |||
4976 | 7560 | // create and resize array | ||
4977 | 7561 | var res = array.resize([], size, defaultValue); | ||
4978 | 7562 | // fill in ones on the diagonal | ||
4979 | 7563 | var minimum = rows < cols ? rows : cols; | ||
4980 | 7564 | // fill diagonal | ||
4981 | 7565 | for (var d = 0; d < minimum; d++) { | ||
4982 | 7566 | res[d][d] = one; | ||
4983 | 7567 | } | ||
4984 | 7568 | return res; | ||
4985 | 7569 | } | ||
4986 | 7570 | } | ||
4987 | 7571 | |||
4988 | 7572 | exports.name = 'eye'; | ||
4989 | 7573 | exports.factory = factory; | ||
4990 | 7574 | |||
4991 | 7575 | |||
4992 | 7576 | /***/ }, | ||
4993 | 7577 | /* 49 */ | ||
4994 | 7578 | /***/ function(module, exports, __webpack_require__) { | ||
4995 | 7579 | |||
4996 | 7580 | 'use strict'; | ||
4997 | 7581 | |||
4998 | 7582 | var clone = __webpack_require__(5).clone; | ||
4999 | 7583 | var _flatten = __webpack_require__(18).flatten; | ||
5000 | 7584 |
PASSED: Continuous integration, rev:230 91.189. 93.70:8080/ job/ubuntu- calculator- app-ci/ 364/ 91.189. 93.70:8080/ job/ubuntu- calculator- app-vivid- amd64-ci/ 86
http://
Executed test runs:
SUCCESS: http://
Click here to trigger a rebuild: 91.189. 93.70:8080/ job/ubuntu- calculator- app-ci/ 364/rebuild
http://