This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
factorial.hhs
76 lines (63 loc) · 2.54 KB
/
factorial.hhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @author Jason Reynolds
* @param input - The number, array or Mat to find the factorial of
* @returns - The same type as the input but with factorial operated on it. If it wasn't a number, factorial was applied in an element-wise fashion
*
* This function takes a number, array or Mat and finds the factorial of it, where the factorial is the element-wise factorial for matrix like structures
* and the normal factorial for numbers. Recall factorial for a number is num! = (num)*(num-1)*(num-2)*...*2*1 . Ex: 5! = 5*4*3*2*1 = 120.
*/
function factorial(input) {
*import math: is_number
*import math: ndim
*import math: deep_copy
if (arguments.length === 0) {
throw new Error('Exception occurred in factorial - no argument given');
}
else if (arguments.length !== 1) {
throw new Error('Exception occurred in factorial - wrong argument number');
}
if(!(is_number(input)) && !(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in factorial - input must be a number, array, matrix or tensor');
}
if (is_number(input)) {
if (input < 0 || parseInt(input) !== input) {
throw new Error('Exception occurred in factorial - input number must be a non-negative integer');
}
return mathjs.factorial(input);
}
//declare raw_in to be a clone of Mat if input is Mat, or input itself otherwise
let in_type = (input instanceof Mat || input instanceof Tensor)
let raw_in = (in_type) ? input.clone().val : deep_copy(input);
let result = factorial_helper(raw_in);
let dim = ndim(result);
if (dim === 1) {
return result;
}
else if (dim === 2) {
return mat(result);
}
else {
return new Tensor(result);
}
function factorial_helper(input) {
let dim = ndim(input);
if (dim === 1) {
let result = deep_copy(input);
for (let i = 0; i < input.length; i++) {
if (input[i] < 0 || input[i] !== parseInt(input[i])) {
throw new Error('Exception occurred in factorial - input number must be a non-negative integer');
}
result[i] = mathjs.factorial(input[i]);
}
return result;
}
else {
let result = [];
for (let i = 0; i < input.length; i++) {
let temp = factorial_helper(input[i]);
result.push(temp);
}
return result;
}
}
}