operation.js (4185B)
1 import { Ok } from "./utility/result.js"; 2 import * as Result from "./utility/result.js"; 3 import { ComplexNumberError, DivisionByZeroError, NegativeInfinityError, } from "./operationError.js"; 4 export const Convolution = { 5 fromAlgebraicOperation(op) { 6 if (op === "Add" || op === "Subtract" || op === "Multiply") { 7 return op; 8 } 9 return undefined; 10 }, 11 canDoAlgebraicOperation(op) { 12 return Convolution.fromAlgebraicOperation(op) !== undefined; 13 }, 14 toFn(t) { 15 switch (t) { 16 case "Add": 17 return (a, b) => a + b; 18 case "Subtract": 19 return (a, b) => a - b; 20 case "Multiply": 21 return (a, b) => a * b; 22 default: 23 throw new Error("This should never happen"); 24 } 25 }, 26 }; 27 const power = (a, b) => { 28 const result = a ** b; 29 if (Number.isNaN(result)) { 30 if (Number.isNaN(a) || Number.isNaN(b)) { 31 return Ok(result); 32 } 33 return Result.Err(new ComplexNumberError()); 34 } 35 return Ok(result); 36 }; 37 const add = (a, b) => Ok(a + b); 38 const subtract = (a, b) => Ok(a - b); 39 const multiply = (a, b) => Ok(a * b); 40 const divide = (a, b) => { 41 if (b !== 0) { 42 return Ok(a / b); 43 } 44 else { 45 return Result.Err(new DivisionByZeroError()); 46 } 47 }; 48 const logarithm = (a, b) => { 49 if (b === 1) { 50 return Result.Err(new DivisionByZeroError()); 51 } 52 else if (b === 0) { 53 return Ok(0); 54 } 55 else if (a > 0 && b > 0) { 56 return Ok(Math.log(a) / Math.log(b)); 57 } 58 else if (a === 0) { 59 return Result.Err(new NegativeInfinityError()); 60 } 61 else { 62 return Result.Err(new ComplexNumberError()); 63 } 64 }; 65 const buildLogarithmWithThreshold = (threshold) => { 66 const fn = (a, b) => { 67 if (a < threshold) { 68 return Ok(0); 69 } 70 else { 71 return logarithm(a, b); 72 } 73 }; 74 return fn; 75 }; 76 export const Algebraic = { 77 toFn(x) { 78 if (x === "Add") { 79 return add; 80 } 81 else if (x === "Subtract") { 82 return subtract; 83 } 84 else if (x === "Multiply") { 85 return multiply; 86 } 87 else if (x === "Power") { 88 return power; 89 } 90 else if (x === "Divide") { 91 return divide; 92 } 93 else if (x === "Logarithm") { 94 return logarithm; 95 } 96 else if (x.NAME === "LogarithmWithThreshold") { 97 return buildLogarithmWithThreshold(x.VAL); 98 } 99 else { 100 throw new Error(`Unknown operation ${x}`); 101 } 102 }, 103 toString(x) { 104 if (x === "Add") { 105 return "+"; 106 } 107 else if (x === "Subtract") { 108 return "-"; 109 } 110 else if (x === "Multiply") { 111 return "*"; 112 } 113 else if (x === "Power") { 114 return "**"; 115 } 116 else if (x === "Divide") { 117 return "/"; 118 } 119 else if (x === "Logarithm") { 120 return "log"; 121 } 122 else if (x.NAME === "LogarithmWithThreshold") { 123 return "log"; 124 } 125 else { 126 throw new Error(`Unknown operation ${x}`); 127 } 128 }, 129 }; 130 export const Scale = { 131 toFn(x) { 132 if (x === "Multiply") { 133 return multiply; 134 } 135 else if (x === "Divide") { 136 return divide; 137 } 138 else if (x === "Power") { 139 return power; 140 } 141 else if (x === "Logarithm") { 142 return logarithm; 143 } 144 else if (x.NAME === "LogarithmWithThreshold") { 145 return buildLogarithmWithThreshold(x.VAL); 146 } 147 else { 148 throw new Error(`Unknown scale operation ${x}`); 149 } 150 }, 151 toIntegralSumCacheFn(x) { 152 if (x === "Multiply") { 153 return (a, b) => a * b; 154 } 155 else if (x === "Divide") { 156 return (a, b) => a / b; 157 } 158 else { 159 return undefined; 160 } 161 }, 162 toIntegralCacheFn(x) { 163 return undefined; 164 }, 165 }; 166 //# sourceMappingURL=operation.js.map